Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Framework for buffer objects that can be shared across devices/subsystems. |
| 3 | * |
| 4 | * Copyright(C) 2011 Linaro Limited. All rights reserved. |
| 5 | * Author: Sumit Semwal <sumit.semwal@ti.com> |
| 6 | * |
| 7 | * Many thanks to linaro-mm-sig list, and specially |
| 8 | * Arnd Bergmann <arnd@arndb.de>, Rob Clark <rob@ti.com> and |
| 9 | * Daniel Vetter <daniel@ffwll.ch> for their support in creation and |
| 10 | * refining of this idea. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify it |
| 13 | * under the terms of the GNU General Public License version 2 as published by |
| 14 | * the Free Software Foundation. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 17 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 19 | * more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | */ |
| 24 | |
| 25 | #include <linux/fs.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/dma-buf.h> |
| 28 | #include <linux/anon_inodes.h> |
| 29 | #include <linux/export.h> |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 30 | #include <linux/debugfs.h> |
| 31 | #include <linux/seq_file.h> |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 32 | |
| 33 | static inline int is_dma_buf_file(struct file *); |
| 34 | |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 35 | struct dma_buf_list { |
| 36 | struct list_head head; |
| 37 | struct mutex lock; |
| 38 | }; |
| 39 | |
| 40 | static struct dma_buf_list db_list; |
| 41 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 42 | static int dma_buf_release(struct inode *inode, struct file *file) |
| 43 | { |
| 44 | struct dma_buf *dmabuf; |
| 45 | |
| 46 | if (!is_dma_buf_file(file)) |
| 47 | return -EINVAL; |
| 48 | |
| 49 | dmabuf = file->private_data; |
| 50 | |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 51 | BUG_ON(dmabuf->vmapping_counter); |
| 52 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 53 | dmabuf->ops->release(dmabuf); |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 54 | |
| 55 | mutex_lock(&db_list.lock); |
| 56 | list_del(&dmabuf->list_node); |
| 57 | mutex_unlock(&db_list.lock); |
| 58 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 59 | kfree(dmabuf); |
| 60 | return 0; |
| 61 | } |
| 62 | |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 63 | static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma) |
| 64 | { |
| 65 | struct dma_buf *dmabuf; |
| 66 | |
| 67 | if (!is_dma_buf_file(file)) |
| 68 | return -EINVAL; |
| 69 | |
| 70 | dmabuf = file->private_data; |
| 71 | |
| 72 | /* check for overflowing the buffer's size */ |
| 73 | if (vma->vm_pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) > |
| 74 | dmabuf->size >> PAGE_SHIFT) |
| 75 | return -EINVAL; |
| 76 | |
| 77 | return dmabuf->ops->mmap(dmabuf, vma); |
| 78 | } |
| 79 | |
Christopher James Halse Rogers | 19e8697 | 2013-09-10 11:36:45 +0530 | [diff] [blame] | 80 | static loff_t dma_buf_llseek(struct file *file, loff_t offset, int whence) |
| 81 | { |
| 82 | struct dma_buf *dmabuf; |
| 83 | loff_t base; |
| 84 | |
| 85 | if (!is_dma_buf_file(file)) |
| 86 | return -EBADF; |
| 87 | |
| 88 | dmabuf = file->private_data; |
| 89 | |
| 90 | /* only support discovering the end of the buffer, |
| 91 | but also allow SEEK_SET to maintain the idiomatic |
| 92 | SEEK_END(0), SEEK_CUR(0) pattern */ |
| 93 | if (whence == SEEK_END) |
| 94 | base = dmabuf->size; |
| 95 | else if (whence == SEEK_SET) |
| 96 | base = 0; |
| 97 | else |
| 98 | return -EINVAL; |
| 99 | |
| 100 | if (offset != 0) |
| 101 | return -EINVAL; |
| 102 | |
| 103 | return base + offset; |
| 104 | } |
| 105 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 106 | static const struct file_operations dma_buf_fops = { |
| 107 | .release = dma_buf_release, |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 108 | .mmap = dma_buf_mmap_internal, |
Christopher James Halse Rogers | 19e8697 | 2013-09-10 11:36:45 +0530 | [diff] [blame] | 109 | .llseek = dma_buf_llseek, |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | /* |
| 113 | * is_dma_buf_file - Check if struct file* is associated with dma_buf |
| 114 | */ |
| 115 | static inline int is_dma_buf_file(struct file *file) |
| 116 | { |
| 117 | return file->f_op == &dma_buf_fops; |
| 118 | } |
| 119 | |
| 120 | /** |
Sumit Semwal | 78df969 | 2013-03-22 18:22:16 +0530 | [diff] [blame] | 121 | * dma_buf_export_named - Creates a new dma_buf, and associates an anon file |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 122 | * with this buffer, so it can be exported. |
| 123 | * Also connect the allocator specific data and ops to the buffer. |
Sumit Semwal | 78df969 | 2013-03-22 18:22:16 +0530 | [diff] [blame] | 124 | * Additionally, provide a name string for exporter; useful in debugging. |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 125 | * |
| 126 | * @priv: [in] Attach private data of allocator to this buffer |
| 127 | * @ops: [in] Attach allocator-defined dma buf ops to the new buffer. |
| 128 | * @size: [in] Size of the buffer |
| 129 | * @flags: [in] mode flags for the file. |
Sumit Semwal | 78df969 | 2013-03-22 18:22:16 +0530 | [diff] [blame] | 130 | * @exp_name: [in] name of the exporting module - useful for debugging. |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 131 | * |
| 132 | * Returns, on success, a newly created dma_buf object, which wraps the |
| 133 | * supplied private data and operations for dma_buf_ops. On either missing |
| 134 | * ops, or error in allocating struct dma_buf, will return negative error. |
| 135 | * |
| 136 | */ |
Sumit Semwal | 78df969 | 2013-03-22 18:22:16 +0530 | [diff] [blame] | 137 | struct dma_buf *dma_buf_export_named(void *priv, const struct dma_buf_ops *ops, |
| 138 | size_t size, int flags, const char *exp_name) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 139 | { |
| 140 | struct dma_buf *dmabuf; |
| 141 | struct file *file; |
| 142 | |
| 143 | if (WARN_ON(!priv || !ops |
| 144 | || !ops->map_dma_buf |
| 145 | || !ops->unmap_dma_buf |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 146 | || !ops->release |
| 147 | || !ops->kmap_atomic |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 148 | || !ops->kmap |
| 149 | || !ops->mmap)) { |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 150 | return ERR_PTR(-EINVAL); |
| 151 | } |
| 152 | |
| 153 | dmabuf = kzalloc(sizeof(struct dma_buf), GFP_KERNEL); |
| 154 | if (dmabuf == NULL) |
| 155 | return ERR_PTR(-ENOMEM); |
| 156 | |
| 157 | dmabuf->priv = priv; |
| 158 | dmabuf->ops = ops; |
| 159 | dmabuf->size = size; |
Sumit Semwal | 78df969 | 2013-03-22 18:22:16 +0530 | [diff] [blame] | 160 | dmabuf->exp_name = exp_name; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 161 | |
| 162 | file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf, flags); |
Tuomas Tynkkynen | 9022e24 | 2013-08-27 16:30:38 +0300 | [diff] [blame] | 163 | if (IS_ERR(file)) { |
| 164 | kfree(dmabuf); |
| 165 | return ERR_CAST(file); |
| 166 | } |
Christopher James Halse Rogers | 19e8697 | 2013-09-10 11:36:45 +0530 | [diff] [blame] | 167 | |
| 168 | file->f_mode |= FMODE_LSEEK; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 169 | dmabuf->file = file; |
| 170 | |
| 171 | mutex_init(&dmabuf->lock); |
| 172 | INIT_LIST_HEAD(&dmabuf->attachments); |
| 173 | |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 174 | mutex_lock(&db_list.lock); |
| 175 | list_add(&dmabuf->list_node, &db_list.head); |
| 176 | mutex_unlock(&db_list.lock); |
| 177 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 178 | return dmabuf; |
| 179 | } |
Sumit Semwal | 78df969 | 2013-03-22 18:22:16 +0530 | [diff] [blame] | 180 | EXPORT_SYMBOL_GPL(dma_buf_export_named); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 181 | |
| 182 | |
| 183 | /** |
| 184 | * dma_buf_fd - returns a file descriptor for the given dma_buf |
| 185 | * @dmabuf: [in] pointer to dma_buf for which fd is required. |
Dave Airlie | 55c1c4c | 2012-03-16 10:34:02 +0000 | [diff] [blame] | 186 | * @flags: [in] flags to give to fd |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 187 | * |
| 188 | * On success, returns an associated 'fd'. Else, returns error. |
| 189 | */ |
Dave Airlie | 55c1c4c | 2012-03-16 10:34:02 +0000 | [diff] [blame] | 190 | int dma_buf_fd(struct dma_buf *dmabuf, int flags) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 191 | { |
Borislav Petkov | f5e097f | 2012-12-11 16:05:26 +0100 | [diff] [blame] | 192 | int fd; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 193 | |
| 194 | if (!dmabuf || !dmabuf->file) |
| 195 | return -EINVAL; |
| 196 | |
Borislav Petkov | f5e097f | 2012-12-11 16:05:26 +0100 | [diff] [blame] | 197 | fd = get_unused_fd_flags(flags); |
| 198 | if (fd < 0) |
| 199 | return fd; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 200 | |
| 201 | fd_install(fd, dmabuf->file); |
| 202 | |
| 203 | return fd; |
| 204 | } |
| 205 | EXPORT_SYMBOL_GPL(dma_buf_fd); |
| 206 | |
| 207 | /** |
| 208 | * dma_buf_get - returns the dma_buf structure related to an fd |
| 209 | * @fd: [in] fd associated with the dma_buf to be returned |
| 210 | * |
| 211 | * On success, returns the dma_buf structure associated with an fd; uses |
| 212 | * file's refcounting done by fget to increase refcount. returns ERR_PTR |
| 213 | * otherwise. |
| 214 | */ |
| 215 | struct dma_buf *dma_buf_get(int fd) |
| 216 | { |
| 217 | struct file *file; |
| 218 | |
| 219 | file = fget(fd); |
| 220 | |
| 221 | if (!file) |
| 222 | return ERR_PTR(-EBADF); |
| 223 | |
| 224 | if (!is_dma_buf_file(file)) { |
| 225 | fput(file); |
| 226 | return ERR_PTR(-EINVAL); |
| 227 | } |
| 228 | |
| 229 | return file->private_data; |
| 230 | } |
| 231 | EXPORT_SYMBOL_GPL(dma_buf_get); |
| 232 | |
| 233 | /** |
| 234 | * dma_buf_put - decreases refcount of the buffer |
| 235 | * @dmabuf: [in] buffer to reduce refcount of |
| 236 | * |
| 237 | * Uses file's refcounting done implicitly by fput() |
| 238 | */ |
| 239 | void dma_buf_put(struct dma_buf *dmabuf) |
| 240 | { |
| 241 | if (WARN_ON(!dmabuf || !dmabuf->file)) |
| 242 | return; |
| 243 | |
| 244 | fput(dmabuf->file); |
| 245 | } |
| 246 | EXPORT_SYMBOL_GPL(dma_buf_put); |
| 247 | |
| 248 | /** |
| 249 | * dma_buf_attach - Add the device to dma_buf's attachments list; optionally, |
| 250 | * calls attach() of dma_buf_ops to allow device-specific attach functionality |
| 251 | * @dmabuf: [in] buffer to attach device to. |
| 252 | * @dev: [in] device to be attached. |
| 253 | * |
Colin Cross | fee0c54 | 2013-12-20 16:43:50 -0800 | [diff] [blame] | 254 | * Returns struct dma_buf_attachment * for this attachment; returns ERR_PTR on |
| 255 | * error. |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 256 | */ |
| 257 | struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf, |
| 258 | struct device *dev) |
| 259 | { |
| 260 | struct dma_buf_attachment *attach; |
| 261 | int ret; |
| 262 | |
Laurent Pinchart | d1aa06a | 2012-01-26 12:27:23 +0100 | [diff] [blame] | 263 | if (WARN_ON(!dmabuf || !dev)) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 264 | return ERR_PTR(-EINVAL); |
| 265 | |
| 266 | attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL); |
| 267 | if (attach == NULL) |
Laurent Pinchart | a9fbc3b | 2012-01-26 12:27:24 +0100 | [diff] [blame] | 268 | return ERR_PTR(-ENOMEM); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 269 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 270 | attach->dev = dev; |
| 271 | attach->dmabuf = dmabuf; |
Laurent Pinchart | 2ed9201 | 2012-01-26 12:27:25 +0100 | [diff] [blame] | 272 | |
| 273 | mutex_lock(&dmabuf->lock); |
| 274 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 275 | if (dmabuf->ops->attach) { |
| 276 | ret = dmabuf->ops->attach(dmabuf, dev, attach); |
| 277 | if (ret) |
| 278 | goto err_attach; |
| 279 | } |
| 280 | list_add(&attach->node, &dmabuf->attachments); |
| 281 | |
| 282 | mutex_unlock(&dmabuf->lock); |
| 283 | return attach; |
| 284 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 285 | err_attach: |
| 286 | kfree(attach); |
| 287 | mutex_unlock(&dmabuf->lock); |
| 288 | return ERR_PTR(ret); |
| 289 | } |
| 290 | EXPORT_SYMBOL_GPL(dma_buf_attach); |
| 291 | |
| 292 | /** |
| 293 | * dma_buf_detach - Remove the given attachment from dmabuf's attachments list; |
| 294 | * optionally calls detach() of dma_buf_ops for device-specific detach |
| 295 | * @dmabuf: [in] buffer to detach from. |
| 296 | * @attach: [in] attachment to be detached; is free'd after this call. |
| 297 | * |
| 298 | */ |
| 299 | void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach) |
| 300 | { |
Laurent Pinchart | d1aa06a | 2012-01-26 12:27:23 +0100 | [diff] [blame] | 301 | if (WARN_ON(!dmabuf || !attach)) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 302 | return; |
| 303 | |
| 304 | mutex_lock(&dmabuf->lock); |
| 305 | list_del(&attach->node); |
| 306 | if (dmabuf->ops->detach) |
| 307 | dmabuf->ops->detach(dmabuf, attach); |
| 308 | |
| 309 | mutex_unlock(&dmabuf->lock); |
| 310 | kfree(attach); |
| 311 | } |
| 312 | EXPORT_SYMBOL_GPL(dma_buf_detach); |
| 313 | |
| 314 | /** |
| 315 | * dma_buf_map_attachment - Returns the scatterlist table of the attachment; |
| 316 | * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the |
| 317 | * dma_buf_ops. |
| 318 | * @attach: [in] attachment whose scatterlist is to be returned |
| 319 | * @direction: [in] direction of DMA transfer |
| 320 | * |
Colin Cross | fee0c54 | 2013-12-20 16:43:50 -0800 | [diff] [blame] | 321 | * Returns sg_table containing the scatterlist to be returned; returns ERR_PTR |
| 322 | * on error. |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 323 | */ |
| 324 | struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach, |
| 325 | enum dma_data_direction direction) |
| 326 | { |
| 327 | struct sg_table *sg_table = ERR_PTR(-EINVAL); |
| 328 | |
| 329 | might_sleep(); |
| 330 | |
Laurent Pinchart | d1aa06a | 2012-01-26 12:27:23 +0100 | [diff] [blame] | 331 | if (WARN_ON(!attach || !attach->dmabuf)) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 332 | return ERR_PTR(-EINVAL); |
| 333 | |
Laurent Pinchart | d1aa06a | 2012-01-26 12:27:23 +0100 | [diff] [blame] | 334 | sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction); |
Colin Cross | fee0c54 | 2013-12-20 16:43:50 -0800 | [diff] [blame] | 335 | if (!sg_table) |
| 336 | sg_table = ERR_PTR(-ENOMEM); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 337 | |
| 338 | return sg_table; |
| 339 | } |
| 340 | EXPORT_SYMBOL_GPL(dma_buf_map_attachment); |
| 341 | |
| 342 | /** |
| 343 | * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might |
| 344 | * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of |
| 345 | * dma_buf_ops. |
| 346 | * @attach: [in] attachment to unmap buffer from |
| 347 | * @sg_table: [in] scatterlist info of the buffer to unmap |
Sumit Semwal | 33ea2dc | 2012-01-27 15:09:27 +0530 | [diff] [blame] | 348 | * @direction: [in] direction of DMA transfer |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 349 | * |
| 350 | */ |
| 351 | void dma_buf_unmap_attachment(struct dma_buf_attachment *attach, |
Sumit Semwal | 33ea2dc | 2012-01-27 15:09:27 +0530 | [diff] [blame] | 352 | struct sg_table *sg_table, |
| 353 | enum dma_data_direction direction) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 354 | { |
Rob Clark | b6fa0cd | 2012-09-28 09:29:43 +0200 | [diff] [blame] | 355 | might_sleep(); |
| 356 | |
Laurent Pinchart | d1aa06a | 2012-01-26 12:27:23 +0100 | [diff] [blame] | 357 | if (WARN_ON(!attach || !attach->dmabuf || !sg_table)) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 358 | return; |
| 359 | |
Sumit Semwal | 33ea2dc | 2012-01-27 15:09:27 +0530 | [diff] [blame] | 360 | attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, |
| 361 | direction); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 362 | } |
| 363 | EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment); |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 364 | |
| 365 | |
| 366 | /** |
| 367 | * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the |
| 368 | * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific |
| 369 | * preparations. Coherency is only guaranteed in the specified range for the |
| 370 | * specified access direction. |
Randy Dunlap | efb4df82 | 2012-04-17 17:03:30 -0700 | [diff] [blame] | 371 | * @dmabuf: [in] buffer to prepare cpu access for. |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 372 | * @start: [in] start of range for cpu access. |
| 373 | * @len: [in] length of range for cpu access. |
| 374 | * @direction: [in] length of range for cpu access. |
| 375 | * |
| 376 | * Can return negative error values, returns 0 on success. |
| 377 | */ |
| 378 | int dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len, |
| 379 | enum dma_data_direction direction) |
| 380 | { |
| 381 | int ret = 0; |
| 382 | |
| 383 | if (WARN_ON(!dmabuf)) |
| 384 | return -EINVAL; |
| 385 | |
| 386 | if (dmabuf->ops->begin_cpu_access) |
| 387 | ret = dmabuf->ops->begin_cpu_access(dmabuf, start, len, direction); |
| 388 | |
| 389 | return ret; |
| 390 | } |
| 391 | EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access); |
| 392 | |
| 393 | /** |
| 394 | * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the |
| 395 | * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific |
| 396 | * actions. Coherency is only guaranteed in the specified range for the |
| 397 | * specified access direction. |
Randy Dunlap | efb4df82 | 2012-04-17 17:03:30 -0700 | [diff] [blame] | 398 | * @dmabuf: [in] buffer to complete cpu access for. |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 399 | * @start: [in] start of range for cpu access. |
| 400 | * @len: [in] length of range for cpu access. |
| 401 | * @direction: [in] length of range for cpu access. |
| 402 | * |
| 403 | * This call must always succeed. |
| 404 | */ |
| 405 | void dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len, |
| 406 | enum dma_data_direction direction) |
| 407 | { |
| 408 | WARN_ON(!dmabuf); |
| 409 | |
| 410 | if (dmabuf->ops->end_cpu_access) |
| 411 | dmabuf->ops->end_cpu_access(dmabuf, start, len, direction); |
| 412 | } |
| 413 | EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access); |
| 414 | |
| 415 | /** |
| 416 | * dma_buf_kmap_atomic - Map a page of the buffer object into kernel address |
| 417 | * space. The same restrictions as for kmap_atomic and friends apply. |
Randy Dunlap | efb4df82 | 2012-04-17 17:03:30 -0700 | [diff] [blame] | 418 | * @dmabuf: [in] buffer to map page from. |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 419 | * @page_num: [in] page in PAGE_SIZE units to map. |
| 420 | * |
| 421 | * This call must always succeed, any necessary preparations that might fail |
| 422 | * need to be done in begin_cpu_access. |
| 423 | */ |
| 424 | void *dma_buf_kmap_atomic(struct dma_buf *dmabuf, unsigned long page_num) |
| 425 | { |
| 426 | WARN_ON(!dmabuf); |
| 427 | |
| 428 | return dmabuf->ops->kmap_atomic(dmabuf, page_num); |
| 429 | } |
| 430 | EXPORT_SYMBOL_GPL(dma_buf_kmap_atomic); |
| 431 | |
| 432 | /** |
| 433 | * dma_buf_kunmap_atomic - Unmap a page obtained by dma_buf_kmap_atomic. |
Randy Dunlap | efb4df82 | 2012-04-17 17:03:30 -0700 | [diff] [blame] | 434 | * @dmabuf: [in] buffer to unmap page from. |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 435 | * @page_num: [in] page in PAGE_SIZE units to unmap. |
| 436 | * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap_atomic. |
| 437 | * |
| 438 | * This call must always succeed. |
| 439 | */ |
| 440 | void dma_buf_kunmap_atomic(struct dma_buf *dmabuf, unsigned long page_num, |
| 441 | void *vaddr) |
| 442 | { |
| 443 | WARN_ON(!dmabuf); |
| 444 | |
| 445 | if (dmabuf->ops->kunmap_atomic) |
| 446 | dmabuf->ops->kunmap_atomic(dmabuf, page_num, vaddr); |
| 447 | } |
| 448 | EXPORT_SYMBOL_GPL(dma_buf_kunmap_atomic); |
| 449 | |
| 450 | /** |
| 451 | * dma_buf_kmap - Map a page of the buffer object into kernel address space. The |
| 452 | * same restrictions as for kmap and friends apply. |
Randy Dunlap | efb4df82 | 2012-04-17 17:03:30 -0700 | [diff] [blame] | 453 | * @dmabuf: [in] buffer to map page from. |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 454 | * @page_num: [in] page in PAGE_SIZE units to map. |
| 455 | * |
| 456 | * This call must always succeed, any necessary preparations that might fail |
| 457 | * need to be done in begin_cpu_access. |
| 458 | */ |
| 459 | void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num) |
| 460 | { |
| 461 | WARN_ON(!dmabuf); |
| 462 | |
| 463 | return dmabuf->ops->kmap(dmabuf, page_num); |
| 464 | } |
| 465 | EXPORT_SYMBOL_GPL(dma_buf_kmap); |
| 466 | |
| 467 | /** |
| 468 | * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap. |
Randy Dunlap | efb4df82 | 2012-04-17 17:03:30 -0700 | [diff] [blame] | 469 | * @dmabuf: [in] buffer to unmap page from. |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 470 | * @page_num: [in] page in PAGE_SIZE units to unmap. |
| 471 | * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap. |
| 472 | * |
| 473 | * This call must always succeed. |
| 474 | */ |
| 475 | void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num, |
| 476 | void *vaddr) |
| 477 | { |
| 478 | WARN_ON(!dmabuf); |
| 479 | |
| 480 | if (dmabuf->ops->kunmap) |
| 481 | dmabuf->ops->kunmap(dmabuf, page_num, vaddr); |
| 482 | } |
| 483 | EXPORT_SYMBOL_GPL(dma_buf_kunmap); |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 484 | |
| 485 | |
| 486 | /** |
| 487 | * dma_buf_mmap - Setup up a userspace mmap with the given vma |
Sumit Semwal | 12c4727 | 2012-05-23 15:27:40 +0530 | [diff] [blame] | 488 | * @dmabuf: [in] buffer that should back the vma |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 489 | * @vma: [in] vma for the mmap |
| 490 | * @pgoff: [in] offset in pages where this mmap should start within the |
| 491 | * dma-buf buffer. |
| 492 | * |
| 493 | * This function adjusts the passed in vma so that it points at the file of the |
| 494 | * dma_buf operation. It alsog adjusts the starting pgoff and does bounds |
| 495 | * checking on the size of the vma. Then it calls the exporters mmap function to |
| 496 | * set up the mapping. |
| 497 | * |
| 498 | * Can return negative error values, returns 0 on success. |
| 499 | */ |
| 500 | int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma, |
| 501 | unsigned long pgoff) |
| 502 | { |
John Sheu | 495c10c | 2013-02-11 17:50:24 -0800 | [diff] [blame] | 503 | struct file *oldfile; |
| 504 | int ret; |
| 505 | |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 506 | if (WARN_ON(!dmabuf || !vma)) |
| 507 | return -EINVAL; |
| 508 | |
| 509 | /* check for offset overflow */ |
| 510 | if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) < pgoff) |
| 511 | return -EOVERFLOW; |
| 512 | |
| 513 | /* check for overflowing the buffer's size */ |
| 514 | if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) > |
| 515 | dmabuf->size >> PAGE_SHIFT) |
| 516 | return -EINVAL; |
| 517 | |
| 518 | /* readjust the vma */ |
John Sheu | 495c10c | 2013-02-11 17:50:24 -0800 | [diff] [blame] | 519 | get_file(dmabuf->file); |
| 520 | oldfile = vma->vm_file; |
| 521 | vma->vm_file = dmabuf->file; |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 522 | vma->vm_pgoff = pgoff; |
| 523 | |
John Sheu | 495c10c | 2013-02-11 17:50:24 -0800 | [diff] [blame] | 524 | ret = dmabuf->ops->mmap(dmabuf, vma); |
| 525 | if (ret) { |
| 526 | /* restore old parameters on failure */ |
| 527 | vma->vm_file = oldfile; |
| 528 | fput(dmabuf->file); |
| 529 | } else { |
| 530 | if (oldfile) |
| 531 | fput(oldfile); |
| 532 | } |
| 533 | return ret; |
| 534 | |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 535 | } |
| 536 | EXPORT_SYMBOL_GPL(dma_buf_mmap); |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 537 | |
| 538 | /** |
Sumit Semwal | 12c4727 | 2012-05-23 15:27:40 +0530 | [diff] [blame] | 539 | * dma_buf_vmap - Create virtual mapping for the buffer object into kernel |
| 540 | * address space. Same restrictions as for vmap and friends apply. |
| 541 | * @dmabuf: [in] buffer to vmap |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 542 | * |
| 543 | * This call may fail due to lack of virtual mapping address space. |
| 544 | * These calls are optional in drivers. The intended use for them |
| 545 | * is for mapping objects linear in kernel space for high use objects. |
| 546 | * Please attempt to use kmap/kunmap before thinking about these interfaces. |
Colin Cross | fee0c54 | 2013-12-20 16:43:50 -0800 | [diff] [blame] | 547 | * |
| 548 | * Returns NULL on error. |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 549 | */ |
| 550 | void *dma_buf_vmap(struct dma_buf *dmabuf) |
| 551 | { |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 552 | void *ptr; |
| 553 | |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 554 | if (WARN_ON(!dmabuf)) |
| 555 | return NULL; |
| 556 | |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 557 | if (!dmabuf->ops->vmap) |
| 558 | return NULL; |
| 559 | |
| 560 | mutex_lock(&dmabuf->lock); |
| 561 | if (dmabuf->vmapping_counter) { |
| 562 | dmabuf->vmapping_counter++; |
| 563 | BUG_ON(!dmabuf->vmap_ptr); |
| 564 | ptr = dmabuf->vmap_ptr; |
| 565 | goto out_unlock; |
| 566 | } |
| 567 | |
| 568 | BUG_ON(dmabuf->vmap_ptr); |
| 569 | |
| 570 | ptr = dmabuf->ops->vmap(dmabuf); |
Colin Cross | fee0c54 | 2013-12-20 16:43:50 -0800 | [diff] [blame] | 571 | if (WARN_ON_ONCE(IS_ERR(ptr))) |
| 572 | ptr = NULL; |
| 573 | if (!ptr) |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 574 | goto out_unlock; |
| 575 | |
| 576 | dmabuf->vmap_ptr = ptr; |
| 577 | dmabuf->vmapping_counter = 1; |
| 578 | |
| 579 | out_unlock: |
| 580 | mutex_unlock(&dmabuf->lock); |
| 581 | return ptr; |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 582 | } |
| 583 | EXPORT_SYMBOL_GPL(dma_buf_vmap); |
| 584 | |
| 585 | /** |
| 586 | * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap. |
Sumit Semwal | 12c4727 | 2012-05-23 15:27:40 +0530 | [diff] [blame] | 587 | * @dmabuf: [in] buffer to vunmap |
Randy Dunlap | 6e7b4a5 | 2012-06-09 15:02:59 -0700 | [diff] [blame] | 588 | * @vaddr: [in] vmap to vunmap |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 589 | */ |
| 590 | void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr) |
| 591 | { |
| 592 | if (WARN_ON(!dmabuf)) |
| 593 | return; |
| 594 | |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 595 | BUG_ON(!dmabuf->vmap_ptr); |
| 596 | BUG_ON(dmabuf->vmapping_counter == 0); |
| 597 | BUG_ON(dmabuf->vmap_ptr != vaddr); |
| 598 | |
| 599 | mutex_lock(&dmabuf->lock); |
| 600 | if (--dmabuf->vmapping_counter == 0) { |
| 601 | if (dmabuf->ops->vunmap) |
| 602 | dmabuf->ops->vunmap(dmabuf, vaddr); |
| 603 | dmabuf->vmap_ptr = NULL; |
| 604 | } |
| 605 | mutex_unlock(&dmabuf->lock); |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 606 | } |
| 607 | EXPORT_SYMBOL_GPL(dma_buf_vunmap); |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 608 | |
| 609 | #ifdef CONFIG_DEBUG_FS |
| 610 | static int dma_buf_describe(struct seq_file *s) |
| 611 | { |
| 612 | int ret; |
| 613 | struct dma_buf *buf_obj; |
| 614 | struct dma_buf_attachment *attach_obj; |
| 615 | int count = 0, attach_count; |
| 616 | size_t size = 0; |
| 617 | |
| 618 | ret = mutex_lock_interruptible(&db_list.lock); |
| 619 | |
| 620 | if (ret) |
| 621 | return ret; |
| 622 | |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 623 | seq_puts(s, "\nDma-buf Objects:\n"); |
| 624 | seq_puts(s, "size\tflags\tmode\tcount\texp_name\n"); |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 625 | |
| 626 | list_for_each_entry(buf_obj, &db_list.head, list_node) { |
| 627 | ret = mutex_lock_interruptible(&buf_obj->lock); |
| 628 | |
| 629 | if (ret) { |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 630 | seq_puts(s, |
| 631 | "\tERROR locking buffer object: skipping\n"); |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 632 | continue; |
| 633 | } |
| 634 | |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 635 | seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\n", |
| 636 | buf_obj->size, |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 637 | buf_obj->file->f_flags, buf_obj->file->f_mode, |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 638 | (long)(buf_obj->file->f_count.counter), |
| 639 | buf_obj->exp_name); |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 640 | |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 641 | seq_puts(s, "\tAttached Devices:\n"); |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 642 | attach_count = 0; |
| 643 | |
| 644 | list_for_each_entry(attach_obj, &buf_obj->attachments, node) { |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 645 | seq_puts(s, "\t"); |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 646 | |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 647 | seq_printf(s, "%s\n", dev_name(attach_obj->dev)); |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 648 | attach_count++; |
| 649 | } |
| 650 | |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 651 | seq_printf(s, "Total %d devices attached\n\n", |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 652 | attach_count); |
| 653 | |
| 654 | count++; |
| 655 | size += buf_obj->size; |
| 656 | mutex_unlock(&buf_obj->lock); |
| 657 | } |
| 658 | |
| 659 | seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size); |
| 660 | |
| 661 | mutex_unlock(&db_list.lock); |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | static int dma_buf_show(struct seq_file *s, void *unused) |
| 666 | { |
| 667 | void (*func)(struct seq_file *) = s->private; |
| 668 | func(s); |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | static int dma_buf_debug_open(struct inode *inode, struct file *file) |
| 673 | { |
| 674 | return single_open(file, dma_buf_show, inode->i_private); |
| 675 | } |
| 676 | |
| 677 | static const struct file_operations dma_buf_debug_fops = { |
| 678 | .open = dma_buf_debug_open, |
| 679 | .read = seq_read, |
| 680 | .llseek = seq_lseek, |
| 681 | .release = single_release, |
| 682 | }; |
| 683 | |
| 684 | static struct dentry *dma_buf_debugfs_dir; |
| 685 | |
| 686 | static int dma_buf_init_debugfs(void) |
| 687 | { |
| 688 | int err = 0; |
| 689 | dma_buf_debugfs_dir = debugfs_create_dir("dma_buf", NULL); |
| 690 | if (IS_ERR(dma_buf_debugfs_dir)) { |
| 691 | err = PTR_ERR(dma_buf_debugfs_dir); |
| 692 | dma_buf_debugfs_dir = NULL; |
| 693 | return err; |
| 694 | } |
| 695 | |
| 696 | err = dma_buf_debugfs_create_file("bufinfo", dma_buf_describe); |
| 697 | |
| 698 | if (err) |
| 699 | pr_debug("dma_buf: debugfs: failed to create node bufinfo\n"); |
| 700 | |
| 701 | return err; |
| 702 | } |
| 703 | |
| 704 | static void dma_buf_uninit_debugfs(void) |
| 705 | { |
| 706 | if (dma_buf_debugfs_dir) |
| 707 | debugfs_remove_recursive(dma_buf_debugfs_dir); |
| 708 | } |
| 709 | |
| 710 | int dma_buf_debugfs_create_file(const char *name, |
| 711 | int (*write)(struct seq_file *)) |
| 712 | { |
| 713 | struct dentry *d; |
| 714 | |
| 715 | d = debugfs_create_file(name, S_IRUGO, dma_buf_debugfs_dir, |
| 716 | write, &dma_buf_debug_fops); |
| 717 | |
Sachin Kamat | 28ce420 | 2013-07-15 15:51:22 +0530 | [diff] [blame] | 718 | return PTR_ERR_OR_ZERO(d); |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 719 | } |
| 720 | #else |
| 721 | static inline int dma_buf_init_debugfs(void) |
| 722 | { |
| 723 | return 0; |
| 724 | } |
| 725 | static inline void dma_buf_uninit_debugfs(void) |
| 726 | { |
| 727 | } |
| 728 | #endif |
| 729 | |
| 730 | static int __init dma_buf_init(void) |
| 731 | { |
| 732 | mutex_init(&db_list.lock); |
| 733 | INIT_LIST_HEAD(&db_list.head); |
| 734 | dma_buf_init_debugfs(); |
| 735 | return 0; |
| 736 | } |
| 737 | subsys_initcall(dma_buf_init); |
| 738 | |
| 739 | static void __exit dma_buf_deinit(void) |
| 740 | { |
| 741 | dma_buf_uninit_debugfs(); |
| 742 | } |
| 743 | __exitcall(dma_buf_deinit); |