| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [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/dma-fence.h> |
| 29 | #include <linux/anon_inodes.h> |
| 30 | #include <linux/export.h> |
| 31 | #include <linux/debugfs.h> |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/seq_file.h> |
| 34 | #include <linux/poll.h> |
| 35 | #include <linux/reservation.h> |
| 36 | #include <linux/mm.h> |
| 37 | #include <linux/mount.h> |
| 38 | #include <linux/module.h> |
| 39 | |
| 40 | #include <uapi/linux/dma-buf.h> |
| 41 | #include <uapi/linux/magic.h> |
| 42 | |
| 43 | static inline int is_dma_buf_file(struct file *); |
| 44 | |
| 45 | struct dma_buf_list { |
| 46 | struct list_head head; |
| 47 | struct mutex lock; |
| 48 | }; |
| 49 | |
| 50 | static struct dma_buf_list db_list; |
| 51 | |
| 52 | static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen) |
| 53 | { |
| 54 | struct dma_buf *dmabuf; |
| 55 | char name[DMA_BUF_NAME_LEN]; |
| 56 | size_t ret = 0; |
| 57 | |
| 58 | dmabuf = dentry->d_fsdata; |
| 59 | mutex_lock(&dmabuf->lock); |
| 60 | if (dmabuf->name) |
| 61 | ret = strlcpy(name, dmabuf->name, DMA_BUF_NAME_LEN); |
| 62 | mutex_unlock(&dmabuf->lock); |
| 63 | |
| 64 | return dynamic_dname(dentry, buffer, buflen, "/%s:%s", |
| 65 | dentry->d_name.name, ret > 0 ? name : ""); |
| 66 | } |
| 67 | |
| 68 | static const struct dentry_operations dma_buf_dentry_ops = { |
| 69 | .d_dname = dmabuffs_dname, |
| 70 | }; |
| 71 | |
| 72 | static struct vfsmount *dma_buf_mnt; |
| 73 | |
| 74 | static struct dentry *dma_buf_fs_mount(struct file_system_type *fs_type, |
| 75 | int flags, const char *name, void *data) |
| 76 | { |
| 77 | return mount_pseudo(fs_type, "dmabuf:", NULL, &dma_buf_dentry_ops, |
| 78 | DMA_BUF_MAGIC); |
| 79 | } |
| 80 | |
| 81 | static struct file_system_type dma_buf_fs_type = { |
| 82 | .name = "dmabuf", |
| 83 | .mount = dma_buf_fs_mount, |
| 84 | .kill_sb = kill_anon_super, |
| 85 | }; |
| 86 | |
| 87 | static int dma_buf_release(struct inode *inode, struct file *file) |
| 88 | { |
| 89 | struct dma_buf *dmabuf; |
| 90 | |
| 91 | if (!is_dma_buf_file(file)) |
| 92 | return -EINVAL; |
| 93 | |
| 94 | dmabuf = file->private_data; |
| 95 | |
| 96 | BUG_ON(dmabuf->vmapping_counter); |
| 97 | |
| 98 | /* |
| 99 | * Any fences that a dma-buf poll can wait on should be signaled |
| 100 | * before releasing dma-buf. This is the responsibility of each |
| 101 | * driver that uses the reservation objects. |
| 102 | * |
| 103 | * If you hit this BUG() it means someone dropped their ref to the |
| 104 | * dma-buf while still having pending operation to the buffer. |
| 105 | */ |
| 106 | BUG_ON(dmabuf->cb_shared.active || dmabuf->cb_excl.active); |
| 107 | |
| 108 | dmabuf->ops->release(dmabuf); |
| 109 | |
| 110 | mutex_lock(&db_list.lock); |
| 111 | list_del(&dmabuf->list_node); |
| 112 | mutex_unlock(&db_list.lock); |
| 113 | |
| 114 | if (dmabuf->resv == (struct reservation_object *)&dmabuf[1]) |
| 115 | reservation_object_fini(dmabuf->resv); |
| 116 | |
| 117 | module_put(dmabuf->owner); |
| 118 | kfree(dmabuf); |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma) |
| 123 | { |
| 124 | struct dma_buf *dmabuf; |
| 125 | |
| 126 | if (!is_dma_buf_file(file)) |
| 127 | return -EINVAL; |
| 128 | |
| 129 | dmabuf = file->private_data; |
| 130 | |
| 131 | /* check for overflowing the buffer's size */ |
| 132 | if (vma->vm_pgoff + vma_pages(vma) > |
| 133 | dmabuf->size >> PAGE_SHIFT) |
| 134 | return -EINVAL; |
| 135 | |
| 136 | return dmabuf->ops->mmap(dmabuf, vma); |
| 137 | } |
| 138 | |
| 139 | static loff_t dma_buf_llseek(struct file *file, loff_t offset, int whence) |
| 140 | { |
| 141 | struct dma_buf *dmabuf; |
| 142 | loff_t base; |
| 143 | |
| 144 | if (!is_dma_buf_file(file)) |
| 145 | return -EBADF; |
| 146 | |
| 147 | dmabuf = file->private_data; |
| 148 | |
| 149 | /* only support discovering the end of the buffer, |
| 150 | but also allow SEEK_SET to maintain the idiomatic |
| 151 | SEEK_END(0), SEEK_CUR(0) pattern */ |
| 152 | if (whence == SEEK_END) |
| 153 | base = dmabuf->size; |
| 154 | else if (whence == SEEK_SET) |
| 155 | base = 0; |
| 156 | else |
| 157 | return -EINVAL; |
| 158 | |
| 159 | if (offset != 0) |
| 160 | return -EINVAL; |
| 161 | |
| 162 | return base + offset; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * DOC: fence polling |
| 167 | * |
| 168 | * To support cross-device and cross-driver synchronization of buffer access |
| 169 | * implicit fences (represented internally in the kernel with &struct fence) can |
| 170 | * be attached to a &dma_buf. The glue for that and a few related things are |
| 171 | * provided in the &reservation_object structure. |
| 172 | * |
| 173 | * Userspace can query the state of these implicitly tracked fences using poll() |
| 174 | * and related system calls: |
| 175 | * |
| 176 | * - Checking for EPOLLIN, i.e. read access, can be use to query the state of the |
| 177 | * most recent write or exclusive fence. |
| 178 | * |
| 179 | * - Checking for EPOLLOUT, i.e. write access, can be used to query the state of |
| 180 | * all attached fences, shared and exclusive ones. |
| 181 | * |
| 182 | * Note that this only signals the completion of the respective fences, i.e. the |
| 183 | * DMA transfers are complete. Cache flushing and any other necessary |
| 184 | * preparations before CPU access can begin still need to happen. |
| 185 | */ |
| 186 | |
| 187 | static void dma_buf_poll_cb(struct dma_fence *fence, struct dma_fence_cb *cb) |
| 188 | { |
| 189 | struct dma_buf_poll_cb_t *dcb = (struct dma_buf_poll_cb_t *)cb; |
| 190 | unsigned long flags; |
| 191 | |
| 192 | spin_lock_irqsave(&dcb->poll->lock, flags); |
| 193 | wake_up_locked_poll(dcb->poll, dcb->active); |
| 194 | dcb->active = 0; |
| 195 | spin_unlock_irqrestore(&dcb->poll->lock, flags); |
| 196 | } |
| 197 | |
| 198 | static __poll_t dma_buf_poll(struct file *file, poll_table *poll) |
| 199 | { |
| 200 | struct dma_buf *dmabuf; |
| 201 | struct reservation_object *resv; |
| 202 | struct reservation_object_list *fobj; |
| 203 | struct dma_fence *fence_excl; |
| 204 | __poll_t events; |
| 205 | unsigned shared_count, seq; |
| 206 | |
| 207 | dmabuf = file->private_data; |
| 208 | if (!dmabuf || !dmabuf->resv) |
| 209 | return EPOLLERR; |
| 210 | |
| 211 | resv = dmabuf->resv; |
| 212 | |
| 213 | poll_wait(file, &dmabuf->poll, poll); |
| 214 | |
| 215 | events = poll_requested_events(poll) & (EPOLLIN | EPOLLOUT); |
| 216 | if (!events) |
| 217 | return 0; |
| 218 | |
| 219 | retry: |
| 220 | seq = read_seqcount_begin(&resv->seq); |
| 221 | rcu_read_lock(); |
| 222 | |
| 223 | fobj = rcu_dereference(resv->fence); |
| 224 | if (fobj) |
| 225 | shared_count = fobj->shared_count; |
| 226 | else |
| 227 | shared_count = 0; |
| 228 | fence_excl = rcu_dereference(resv->fence_excl); |
| 229 | if (read_seqcount_retry(&resv->seq, seq)) { |
| 230 | rcu_read_unlock(); |
| 231 | goto retry; |
| 232 | } |
| 233 | |
| 234 | if (fence_excl && (!(events & EPOLLOUT) || shared_count == 0)) { |
| 235 | struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_excl; |
| 236 | __poll_t pevents = EPOLLIN; |
| 237 | |
| 238 | if (shared_count == 0) |
| 239 | pevents |= EPOLLOUT; |
| 240 | |
| 241 | spin_lock_irq(&dmabuf->poll.lock); |
| 242 | if (dcb->active) { |
| 243 | dcb->active |= pevents; |
| 244 | events &= ~pevents; |
| 245 | } else |
| 246 | dcb->active = pevents; |
| 247 | spin_unlock_irq(&dmabuf->poll.lock); |
| 248 | |
| 249 | if (events & pevents) { |
| 250 | if (!dma_fence_get_rcu(fence_excl)) { |
| 251 | /* force a recheck */ |
| 252 | events &= ~pevents; |
| 253 | dma_buf_poll_cb(NULL, &dcb->cb); |
| 254 | } else if (!dma_fence_add_callback(fence_excl, &dcb->cb, |
| 255 | dma_buf_poll_cb)) { |
| 256 | events &= ~pevents; |
| 257 | dma_fence_put(fence_excl); |
| 258 | } else { |
| 259 | /* |
| 260 | * No callback queued, wake up any additional |
| 261 | * waiters. |
| 262 | */ |
| 263 | dma_fence_put(fence_excl); |
| 264 | dma_buf_poll_cb(NULL, &dcb->cb); |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | if ((events & EPOLLOUT) && shared_count > 0) { |
| 270 | struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_shared; |
| 271 | int i; |
| 272 | |
| 273 | /* Only queue a new callback if no event has fired yet */ |
| 274 | spin_lock_irq(&dmabuf->poll.lock); |
| 275 | if (dcb->active) |
| 276 | events &= ~EPOLLOUT; |
| 277 | else |
| 278 | dcb->active = EPOLLOUT; |
| 279 | spin_unlock_irq(&dmabuf->poll.lock); |
| 280 | |
| 281 | if (!(events & EPOLLOUT)) |
| 282 | goto out; |
| 283 | |
| 284 | for (i = 0; i < shared_count; ++i) { |
| 285 | struct dma_fence *fence = rcu_dereference(fobj->shared[i]); |
| 286 | |
| 287 | if (!dma_fence_get_rcu(fence)) { |
| 288 | /* |
| 289 | * fence refcount dropped to zero, this means |
| 290 | * that fobj has been freed |
| 291 | * |
| 292 | * call dma_buf_poll_cb and force a recheck! |
| 293 | */ |
| 294 | events &= ~EPOLLOUT; |
| 295 | dma_buf_poll_cb(NULL, &dcb->cb); |
| 296 | break; |
| 297 | } |
| 298 | if (!dma_fence_add_callback(fence, &dcb->cb, |
| 299 | dma_buf_poll_cb)) { |
| 300 | dma_fence_put(fence); |
| 301 | events &= ~EPOLLOUT; |
| 302 | break; |
| 303 | } |
| 304 | dma_fence_put(fence); |
| 305 | } |
| 306 | |
| 307 | /* No callback queued, wake up any additional waiters. */ |
| 308 | if (i == shared_count) |
| 309 | dma_buf_poll_cb(NULL, &dcb->cb); |
| 310 | } |
| 311 | |
| 312 | out: |
| 313 | rcu_read_unlock(); |
| 314 | return events; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * dma_buf_set_name - Set a name to a specific dma_buf to track the usage. |
| 319 | * The name of the dma-buf buffer can only be set when the dma-buf is not |
| 320 | * attached to any devices. It could theoritically support changing the |
| 321 | * name of the dma-buf if the same piece of memory is used for multiple |
| 322 | * purpose between different devices. |
| 323 | * |
| 324 | * @dmabuf [in] dmabuf buffer that will be renamed. |
| 325 | * @buf: [in] A piece of userspace memory that contains the name of |
| 326 | * the dma-buf. |
| 327 | * |
| 328 | * Returns 0 on success. If the dma-buf buffer is already attached to |
| 329 | * devices, return -EBUSY. |
| 330 | * |
| 331 | */ |
| 332 | static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf) |
| 333 | { |
| 334 | char *name = strndup_user(buf, DMA_BUF_NAME_LEN); |
| 335 | long ret = 0; |
| 336 | |
| 337 | if (IS_ERR(name)) |
| 338 | return PTR_ERR(name); |
| 339 | |
| 340 | mutex_lock(&dmabuf->lock); |
| 341 | if (!list_empty(&dmabuf->attachments)) { |
| 342 | ret = -EBUSY; |
| 343 | kfree(name); |
| 344 | goto out_unlock; |
| 345 | } |
| 346 | kfree(dmabuf->name); |
| 347 | dmabuf->name = name; |
| 348 | |
| 349 | out_unlock: |
| 350 | mutex_unlock(&dmabuf->lock); |
| 351 | return ret; |
| 352 | } |
| 353 | |
| 354 | static long dma_buf_ioctl(struct file *file, |
| 355 | unsigned int cmd, unsigned long arg) |
| 356 | { |
| 357 | struct dma_buf *dmabuf; |
| 358 | struct dma_buf_sync sync; |
| 359 | enum dma_data_direction direction; |
| 360 | int ret; |
| 361 | |
| 362 | dmabuf = file->private_data; |
| 363 | |
| 364 | switch (cmd) { |
| 365 | case DMA_BUF_IOCTL_SYNC: |
| 366 | if (copy_from_user(&sync, (void __user *) arg, sizeof(sync))) |
| 367 | return -EFAULT; |
| 368 | |
| 369 | if (sync.flags & ~DMA_BUF_SYNC_VALID_FLAGS_MASK) |
| 370 | return -EINVAL; |
| 371 | |
| 372 | switch (sync.flags & DMA_BUF_SYNC_RW) { |
| 373 | case DMA_BUF_SYNC_READ: |
| 374 | direction = DMA_FROM_DEVICE; |
| 375 | break; |
| 376 | case DMA_BUF_SYNC_WRITE: |
| 377 | direction = DMA_TO_DEVICE; |
| 378 | break; |
| 379 | case DMA_BUF_SYNC_RW: |
| 380 | direction = DMA_BIDIRECTIONAL; |
| 381 | break; |
| 382 | default: |
| 383 | return -EINVAL; |
| 384 | } |
| 385 | |
| 386 | if (sync.flags & DMA_BUF_SYNC_END) |
| 387 | ret = dma_buf_end_cpu_access(dmabuf, direction); |
| 388 | else |
| 389 | ret = dma_buf_begin_cpu_access(dmabuf, direction); |
| 390 | |
| 391 | return ret; |
| 392 | |
| 393 | case DMA_BUF_SET_NAME: |
| 394 | return dma_buf_set_name(dmabuf, (const char __user *)arg); |
| 395 | |
| 396 | default: |
| 397 | return -ENOTTY; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | static void dma_buf_show_fdinfo(struct seq_file *m, struct file *file) |
| 402 | { |
| 403 | struct dma_buf *dmabuf = file->private_data; |
| 404 | |
| 405 | seq_printf(m, "size:\t%zu\n", dmabuf->size); |
| 406 | /* Don't count the temporary reference taken inside procfs seq_show */ |
| 407 | seq_printf(m, "count:\t%ld\n", file_count(dmabuf->file) - 1); |
| 408 | seq_printf(m, "exp_name:\t%s\n", dmabuf->exp_name); |
| 409 | mutex_lock(&dmabuf->lock); |
| 410 | if (dmabuf->name) |
| 411 | seq_printf(m, "name:\t%s\n", dmabuf->name); |
| 412 | mutex_unlock(&dmabuf->lock); |
| 413 | } |
| 414 | |
| 415 | static const struct file_operations dma_buf_fops = { |
| 416 | .release = dma_buf_release, |
| 417 | .mmap = dma_buf_mmap_internal, |
| 418 | .llseek = dma_buf_llseek, |
| 419 | .poll = dma_buf_poll, |
| 420 | .unlocked_ioctl = dma_buf_ioctl, |
| 421 | #ifdef CONFIG_COMPAT |
| 422 | .compat_ioctl = dma_buf_ioctl, |
| 423 | #endif |
| 424 | .show_fdinfo = dma_buf_show_fdinfo, |
| 425 | }; |
| 426 | |
| 427 | /* |
| 428 | * is_dma_buf_file - Check if struct file* is associated with dma_buf |
| 429 | */ |
| 430 | static inline int is_dma_buf_file(struct file *file) |
| 431 | { |
| 432 | return file->f_op == &dma_buf_fops; |
| 433 | } |
| 434 | |
| 435 | static struct file *dma_buf_getfile(struct dma_buf *dmabuf, int flags) |
| 436 | { |
| 437 | struct file *file; |
| 438 | struct inode *inode = alloc_anon_inode(dma_buf_mnt->mnt_sb); |
| 439 | |
| 440 | if (IS_ERR(inode)) |
| 441 | return ERR_CAST(inode); |
| 442 | |
| 443 | inode->i_size = dmabuf->size; |
| 444 | inode_set_bytes(inode, dmabuf->size); |
| 445 | |
| 446 | file = alloc_file_pseudo(inode, dma_buf_mnt, "dmabuf", |
| 447 | flags, &dma_buf_fops); |
| 448 | if (IS_ERR(file)) |
| 449 | goto err_alloc_file; |
| 450 | file->f_flags = flags & (O_ACCMODE | O_NONBLOCK); |
| 451 | file->private_data = dmabuf; |
| 452 | file->f_path.dentry->d_fsdata = dmabuf; |
| 453 | |
| 454 | return file; |
| 455 | |
| 456 | err_alloc_file: |
| 457 | iput(inode); |
| 458 | return file; |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * DOC: dma buf device access |
| 463 | * |
| 464 | * For device DMA access to a shared DMA buffer the usual sequence of operations |
| 465 | * is fairly simple: |
| 466 | * |
| 467 | * 1. The exporter defines his exporter instance using |
| 468 | * DEFINE_DMA_BUF_EXPORT_INFO() and calls dma_buf_export() to wrap a private |
| 469 | * buffer object into a &dma_buf. It then exports that &dma_buf to userspace |
| 470 | * as a file descriptor by calling dma_buf_fd(). |
| 471 | * |
| 472 | * 2. Userspace passes this file-descriptors to all drivers it wants this buffer |
| 473 | * to share with: First the filedescriptor is converted to a &dma_buf using |
| 474 | * dma_buf_get(). Then the buffer is attached to the device using |
| 475 | * dma_buf_attach(). |
| 476 | * |
| 477 | * Up to this stage the exporter is still free to migrate or reallocate the |
| 478 | * backing storage. |
| 479 | * |
| 480 | * 3. Once the buffer is attached to all devices userspace can initiate DMA |
| 481 | * access to the shared buffer. In the kernel this is done by calling |
| 482 | * dma_buf_map_attachment() and dma_buf_unmap_attachment(). |
| 483 | * |
| 484 | * 4. Once a driver is done with a shared buffer it needs to call |
| 485 | * dma_buf_detach() (after cleaning up any mappings) and then release the |
| 486 | * reference acquired with dma_buf_get by calling dma_buf_put(). |
| 487 | * |
| 488 | * For the detailed semantics exporters are expected to implement see |
| 489 | * &dma_buf_ops. |
| 490 | */ |
| 491 | |
| 492 | /** |
| 493 | * dma_buf_export - Creates a new dma_buf, and associates an anon file |
| 494 | * with this buffer, so it can be exported. |
| 495 | * Also connect the allocator specific data and ops to the buffer. |
| 496 | * Additionally, provide a name string for exporter; useful in debugging. |
| 497 | * |
| 498 | * @exp_info: [in] holds all the export related information provided |
| 499 | * by the exporter. see &struct dma_buf_export_info |
| 500 | * for further details. |
| 501 | * |
| 502 | * Returns, on success, a newly created dma_buf object, which wraps the |
| 503 | * supplied private data and operations for dma_buf_ops. On either missing |
| 504 | * ops, or error in allocating struct dma_buf, will return negative error. |
| 505 | * |
| 506 | * For most cases the easiest way to create @exp_info is through the |
| 507 | * %DEFINE_DMA_BUF_EXPORT_INFO macro. |
| 508 | */ |
| 509 | struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info) |
| 510 | { |
| 511 | struct dma_buf *dmabuf; |
| 512 | struct reservation_object *resv = exp_info->resv; |
| 513 | struct file *file; |
| 514 | size_t alloc_size = sizeof(struct dma_buf); |
| 515 | int ret; |
| 516 | |
| 517 | if (!exp_info->resv) |
| 518 | alloc_size += sizeof(struct reservation_object); |
| 519 | else |
| 520 | /* prevent &dma_buf[1] == dma_buf->resv */ |
| 521 | alloc_size += 1; |
| 522 | |
| 523 | if (WARN_ON(!exp_info->priv |
| 524 | || !exp_info->ops |
| 525 | || !exp_info->ops->map_dma_buf |
| 526 | || !exp_info->ops->unmap_dma_buf |
| 527 | || !exp_info->ops->release |
| 528 | || !exp_info->ops->map |
| 529 | || !exp_info->ops->mmap)) { |
| 530 | return ERR_PTR(-EINVAL); |
| 531 | } |
| 532 | |
| 533 | if (!try_module_get(exp_info->owner)) |
| 534 | return ERR_PTR(-ENOENT); |
| 535 | |
| 536 | dmabuf = kzalloc(alloc_size, GFP_KERNEL); |
| 537 | if (!dmabuf) { |
| 538 | ret = -ENOMEM; |
| 539 | goto err_module; |
| 540 | } |
| 541 | |
| 542 | dmabuf->priv = exp_info->priv; |
| 543 | dmabuf->ops = exp_info->ops; |
| 544 | dmabuf->size = exp_info->size; |
| 545 | dmabuf->exp_name = exp_info->exp_name; |
| 546 | dmabuf->owner = exp_info->owner; |
| 547 | init_waitqueue_head(&dmabuf->poll); |
| 548 | dmabuf->cb_excl.poll = dmabuf->cb_shared.poll = &dmabuf->poll; |
| 549 | dmabuf->cb_excl.active = dmabuf->cb_shared.active = 0; |
| 550 | |
| 551 | if (!resv) { |
| 552 | resv = (struct reservation_object *)&dmabuf[1]; |
| 553 | reservation_object_init(resv); |
| 554 | } |
| 555 | dmabuf->resv = resv; |
| 556 | |
| 557 | file = dma_buf_getfile(dmabuf, exp_info->flags); |
| 558 | if (IS_ERR(file)) { |
| 559 | ret = PTR_ERR(file); |
| 560 | goto err_dmabuf; |
| 561 | } |
| 562 | |
| 563 | file->f_mode |= FMODE_LSEEK; |
| 564 | dmabuf->file = file; |
| 565 | |
| 566 | mutex_init(&dmabuf->lock); |
| 567 | INIT_LIST_HEAD(&dmabuf->attachments); |
| 568 | |
| 569 | mutex_lock(&db_list.lock); |
| 570 | list_add(&dmabuf->list_node, &db_list.head); |
| 571 | mutex_unlock(&db_list.lock); |
| 572 | |
| 573 | return dmabuf; |
| 574 | |
| 575 | err_dmabuf: |
| 576 | kfree(dmabuf); |
| 577 | err_module: |
| 578 | module_put(exp_info->owner); |
| 579 | return ERR_PTR(ret); |
| 580 | } |
| 581 | EXPORT_SYMBOL_GPL(dma_buf_export); |
| 582 | |
| 583 | /** |
| 584 | * dma_buf_fd - returns a file descriptor for the given dma_buf |
| 585 | * @dmabuf: [in] pointer to dma_buf for which fd is required. |
| 586 | * @flags: [in] flags to give to fd |
| 587 | * |
| 588 | * On success, returns an associated 'fd'. Else, returns error. |
| 589 | */ |
| 590 | int dma_buf_fd(struct dma_buf *dmabuf, int flags) |
| 591 | { |
| 592 | int fd; |
| 593 | |
| 594 | if (!dmabuf || !dmabuf->file) |
| 595 | return -EINVAL; |
| 596 | |
| 597 | fd = get_unused_fd_flags(flags); |
| 598 | if (fd < 0) |
| 599 | return fd; |
| 600 | |
| 601 | fd_install(fd, dmabuf->file); |
| 602 | |
| 603 | return fd; |
| 604 | } |
| 605 | EXPORT_SYMBOL_GPL(dma_buf_fd); |
| 606 | |
| 607 | /** |
| 608 | * dma_buf_get - returns the dma_buf structure related to an fd |
| 609 | * @fd: [in] fd associated with the dma_buf to be returned |
| 610 | * |
| 611 | * On success, returns the dma_buf structure associated with an fd; uses |
| 612 | * file's refcounting done by fget to increase refcount. returns ERR_PTR |
| 613 | * otherwise. |
| 614 | */ |
| 615 | struct dma_buf *dma_buf_get(int fd) |
| 616 | { |
| 617 | struct file *file; |
| 618 | |
| 619 | file = fget(fd); |
| 620 | |
| 621 | if (!file) |
| 622 | return ERR_PTR(-EBADF); |
| 623 | |
| 624 | if (!is_dma_buf_file(file)) { |
| 625 | fput(file); |
| 626 | return ERR_PTR(-EINVAL); |
| 627 | } |
| 628 | |
| 629 | return file->private_data; |
| 630 | } |
| 631 | EXPORT_SYMBOL_GPL(dma_buf_get); |
| 632 | |
| 633 | /** |
| 634 | * dma_buf_put - decreases refcount of the buffer |
| 635 | * @dmabuf: [in] buffer to reduce refcount of |
| 636 | * |
| 637 | * Uses file's refcounting done implicitly by fput(). |
| 638 | * |
| 639 | * If, as a result of this call, the refcount becomes 0, the 'release' file |
| 640 | * operation related to this fd is called. It calls &dma_buf_ops.release vfunc |
| 641 | * in turn, and frees the memory allocated for dmabuf when exported. |
| 642 | */ |
| 643 | void dma_buf_put(struct dma_buf *dmabuf) |
| 644 | { |
| 645 | if (WARN_ON(!dmabuf || !dmabuf->file)) |
| 646 | return; |
| 647 | |
| 648 | fput(dmabuf->file); |
| 649 | } |
| 650 | EXPORT_SYMBOL_GPL(dma_buf_put); |
| 651 | |
| 652 | /** |
| 653 | * dma_buf_attach - Add the device to dma_buf's attachments list; optionally, |
| 654 | * calls attach() of dma_buf_ops to allow device-specific attach functionality |
| 655 | * @dmabuf: [in] buffer to attach device to. |
| 656 | * @dev: [in] device to be attached. |
| 657 | * |
| 658 | * Returns struct dma_buf_attachment pointer for this attachment. Attachments |
| 659 | * must be cleaned up by calling dma_buf_detach(). |
| 660 | * |
| 661 | * Returns: |
| 662 | * |
| 663 | * A pointer to newly created &dma_buf_attachment on success, or a negative |
| 664 | * error code wrapped into a pointer on failure. |
| 665 | * |
| 666 | * Note that this can fail if the backing storage of @dmabuf is in a place not |
| 667 | * accessible to @dev, and cannot be moved to a more suitable place. This is |
| 668 | * indicated with the error code -EBUSY. |
| 669 | */ |
| 670 | struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf, |
| 671 | struct device *dev) |
| 672 | { |
| 673 | struct dma_buf_attachment *attach; |
| 674 | int ret; |
| 675 | |
| 676 | if (WARN_ON(!dmabuf || !dev)) |
| 677 | return ERR_PTR(-EINVAL); |
| 678 | |
| 679 | attach = kzalloc(sizeof(*attach), GFP_KERNEL); |
| 680 | if (!attach) |
| 681 | return ERR_PTR(-ENOMEM); |
| 682 | |
| 683 | attach->dev = dev; |
| 684 | attach->dmabuf = dmabuf; |
| 685 | |
| 686 | mutex_lock(&dmabuf->lock); |
| 687 | |
| 688 | if (dmabuf->ops->attach) { |
| 689 | ret = dmabuf->ops->attach(dmabuf, attach); |
| 690 | if (ret) |
| 691 | goto err_attach; |
| 692 | } |
| 693 | list_add(&attach->node, &dmabuf->attachments); |
| 694 | |
| 695 | mutex_unlock(&dmabuf->lock); |
| 696 | return attach; |
| 697 | |
| 698 | err_attach: |
| 699 | kfree(attach); |
| 700 | mutex_unlock(&dmabuf->lock); |
| 701 | return ERR_PTR(ret); |
| 702 | } |
| 703 | EXPORT_SYMBOL_GPL(dma_buf_attach); |
| 704 | |
| 705 | /** |
| 706 | * dma_buf_detach - Remove the given attachment from dmabuf's attachments list; |
| 707 | * optionally calls detach() of dma_buf_ops for device-specific detach |
| 708 | * @dmabuf: [in] buffer to detach from. |
| 709 | * @attach: [in] attachment to be detached; is free'd after this call. |
| 710 | * |
| 711 | * Clean up a device attachment obtained by calling dma_buf_attach(). |
| 712 | */ |
| 713 | void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach) |
| 714 | { |
| 715 | if (WARN_ON(!dmabuf || !attach)) |
| 716 | return; |
| 717 | |
| 718 | mutex_lock(&dmabuf->lock); |
| 719 | list_del(&attach->node); |
| 720 | if (dmabuf->ops->detach) |
| 721 | dmabuf->ops->detach(dmabuf, attach); |
| 722 | |
| 723 | mutex_unlock(&dmabuf->lock); |
| 724 | kfree(attach); |
| 725 | } |
| 726 | EXPORT_SYMBOL_GPL(dma_buf_detach); |
| 727 | |
| 728 | /** |
| 729 | * dma_buf_map_attachment - Returns the scatterlist table of the attachment; |
| 730 | * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the |
| 731 | * dma_buf_ops. |
| 732 | * @attach: [in] attachment whose scatterlist is to be returned |
| 733 | * @direction: [in] direction of DMA transfer |
| 734 | * |
| 735 | * Returns sg_table containing the scatterlist to be returned; returns ERR_PTR |
| 736 | * on error. May return -EINTR if it is interrupted by a signal. |
| 737 | * |
| 738 | * A mapping must be unmapped by using dma_buf_unmap_attachment(). Note that |
| 739 | * the underlying backing storage is pinned for as long as a mapping exists, |
| 740 | * therefore users/importers should not hold onto a mapping for undue amounts of |
| 741 | * time. |
| 742 | */ |
| 743 | struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach, |
| 744 | enum dma_data_direction direction) |
| 745 | { |
| 746 | struct sg_table *sg_table; |
| 747 | |
| 748 | might_sleep(); |
| 749 | |
| 750 | if (WARN_ON(!attach || !attach->dmabuf)) |
| 751 | return ERR_PTR(-EINVAL); |
| 752 | |
| 753 | sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction); |
| 754 | if (!sg_table) |
| 755 | sg_table = ERR_PTR(-ENOMEM); |
| 756 | |
| 757 | return sg_table; |
| 758 | } |
| 759 | EXPORT_SYMBOL_GPL(dma_buf_map_attachment); |
| 760 | |
| 761 | /** |
| 762 | * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might |
| 763 | * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of |
| 764 | * dma_buf_ops. |
| 765 | * @attach: [in] attachment to unmap buffer from |
| 766 | * @sg_table: [in] scatterlist info of the buffer to unmap |
| 767 | * @direction: [in] direction of DMA transfer |
| 768 | * |
| 769 | * This unmaps a DMA mapping for @attached obtained by dma_buf_map_attachment(). |
| 770 | */ |
| 771 | void dma_buf_unmap_attachment(struct dma_buf_attachment *attach, |
| 772 | struct sg_table *sg_table, |
| 773 | enum dma_data_direction direction) |
| 774 | { |
| 775 | might_sleep(); |
| 776 | |
| 777 | if (WARN_ON(!attach || !attach->dmabuf || !sg_table)) |
| 778 | return; |
| 779 | |
| 780 | attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, |
| 781 | direction); |
| 782 | } |
| 783 | EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment); |
| 784 | |
| 785 | /** |
| 786 | * DOC: cpu access |
| 787 | * |
| 788 | * There are mutliple reasons for supporting CPU access to a dma buffer object: |
| 789 | * |
| 790 | * - Fallback operations in the kernel, for example when a device is connected |
| 791 | * over USB and the kernel needs to shuffle the data around first before |
| 792 | * sending it away. Cache coherency is handled by braketing any transactions |
| 793 | * with calls to dma_buf_begin_cpu_access() and dma_buf_end_cpu_access() |
| 794 | * access. |
| 795 | * |
| 796 | * To support dma_buf objects residing in highmem cpu access is page-based |
| 797 | * using an api similar to kmap. Accessing a dma_buf is done in aligned chunks |
| 798 | * of PAGE_SIZE size. Before accessing a chunk it needs to be mapped, which |
| 799 | * returns a pointer in kernel virtual address space. Afterwards the chunk |
| 800 | * needs to be unmapped again. There is no limit on how often a given chunk |
| 801 | * can be mapped and unmapped, i.e. the importer does not need to call |
| 802 | * begin_cpu_access again before mapping the same chunk again. |
| 803 | * |
| 804 | * Interfaces:: |
| 805 | * void \*dma_buf_kmap(struct dma_buf \*, unsigned long); |
| 806 | * void dma_buf_kunmap(struct dma_buf \*, unsigned long, void \*); |
| 807 | * |
| 808 | * Implementing the functions is optional for exporters and for importers all |
| 809 | * the restrictions of using kmap apply. |
| 810 | * |
| 811 | * dma_buf kmap calls outside of the range specified in begin_cpu_access are |
| 812 | * undefined. If the range is not PAGE_SIZE aligned, kmap needs to succeed on |
| 813 | * the partial chunks at the beginning and end but may return stale or bogus |
| 814 | * data outside of the range (in these partial chunks). |
| 815 | * |
| 816 | * For some cases the overhead of kmap can be too high, a vmap interface |
| 817 | * is introduced. This interface should be used very carefully, as vmalloc |
| 818 | * space is a limited resources on many architectures. |
| 819 | * |
| 820 | * Interfaces:: |
| 821 | * void \*dma_buf_vmap(struct dma_buf \*dmabuf) |
| 822 | * void dma_buf_vunmap(struct dma_buf \*dmabuf, void \*vaddr) |
| 823 | * |
| 824 | * The vmap call can fail if there is no vmap support in the exporter, or if |
| 825 | * it runs out of vmalloc space. Fallback to kmap should be implemented. Note |
| 826 | * that the dma-buf layer keeps a reference count for all vmap access and |
| 827 | * calls down into the exporter's vmap function only when no vmapping exists, |
| 828 | * and only unmaps it once. Protection against concurrent vmap/vunmap calls is |
| 829 | * provided by taking the dma_buf->lock mutex. |
| 830 | * |
| 831 | * - For full compatibility on the importer side with existing userspace |
| 832 | * interfaces, which might already support mmap'ing buffers. This is needed in |
| 833 | * many processing pipelines (e.g. feeding a software rendered image into a |
| 834 | * hardware pipeline, thumbnail creation, snapshots, ...). Also, Android's ION |
| 835 | * framework already supported this and for DMA buffer file descriptors to |
| 836 | * replace ION buffers mmap support was needed. |
| 837 | * |
| 838 | * There is no special interfaces, userspace simply calls mmap on the dma-buf |
| 839 | * fd. But like for CPU access there's a need to braket the actual access, |
| 840 | * which is handled by the ioctl (DMA_BUF_IOCTL_SYNC). Note that |
| 841 | * DMA_BUF_IOCTL_SYNC can fail with -EAGAIN or -EINTR, in which case it must |
| 842 | * be restarted. |
| 843 | * |
| 844 | * Some systems might need some sort of cache coherency management e.g. when |
| 845 | * CPU and GPU domains are being accessed through dma-buf at the same time. |
| 846 | * To circumvent this problem there are begin/end coherency markers, that |
| 847 | * forward directly to existing dma-buf device drivers vfunc hooks. Userspace |
| 848 | * can make use of those markers through the DMA_BUF_IOCTL_SYNC ioctl. The |
| 849 | * sequence would be used like following: |
| 850 | * |
| 851 | * - mmap dma-buf fd |
| 852 | * - for each drawing/upload cycle in CPU 1. SYNC_START ioctl, 2. read/write |
| 853 | * to mmap area 3. SYNC_END ioctl. This can be repeated as often as you |
| 854 | * want (with the new data being consumed by say the GPU or the scanout |
| 855 | * device) |
| 856 | * - munmap once you don't need the buffer any more |
| 857 | * |
| 858 | * For correctness and optimal performance, it is always required to use |
| 859 | * SYNC_START and SYNC_END before and after, respectively, when accessing the |
| 860 | * mapped address. Userspace cannot rely on coherent access, even when there |
| 861 | * are systems where it just works without calling these ioctls. |
| 862 | * |
| 863 | * - And as a CPU fallback in userspace processing pipelines. |
| 864 | * |
| 865 | * Similar to the motivation for kernel cpu access it is again important that |
| 866 | * the userspace code of a given importing subsystem can use the same |
| 867 | * interfaces with a imported dma-buf buffer object as with a native buffer |
| 868 | * object. This is especially important for drm where the userspace part of |
| 869 | * contemporary OpenGL, X, and other drivers is huge, and reworking them to |
| 870 | * use a different way to mmap a buffer rather invasive. |
| 871 | * |
| 872 | * The assumption in the current dma-buf interfaces is that redirecting the |
| 873 | * initial mmap is all that's needed. A survey of some of the existing |
| 874 | * subsystems shows that no driver seems to do any nefarious thing like |
| 875 | * syncing up with outstanding asynchronous processing on the device or |
| 876 | * allocating special resources at fault time. So hopefully this is good |
| 877 | * enough, since adding interfaces to intercept pagefaults and allow pte |
| 878 | * shootdowns would increase the complexity quite a bit. |
| 879 | * |
| 880 | * Interface:: |
| 881 | * int dma_buf_mmap(struct dma_buf \*, struct vm_area_struct \*, |
| 882 | * unsigned long); |
| 883 | * |
| 884 | * If the importing subsystem simply provides a special-purpose mmap call to |
| 885 | * set up a mapping in userspace, calling do_mmap with dma_buf->file will |
| 886 | * equally achieve that for a dma-buf object. |
| 887 | */ |
| 888 | |
| 889 | static int __dma_buf_begin_cpu_access(struct dma_buf *dmabuf, |
| 890 | enum dma_data_direction direction) |
| 891 | { |
| 892 | bool write = (direction == DMA_BIDIRECTIONAL || |
| 893 | direction == DMA_TO_DEVICE); |
| 894 | struct reservation_object *resv = dmabuf->resv; |
| 895 | long ret; |
| 896 | |
| 897 | /* Wait on any implicit rendering fences */ |
| 898 | ret = reservation_object_wait_timeout_rcu(resv, write, true, |
| 899 | MAX_SCHEDULE_TIMEOUT); |
| 900 | if (ret < 0) |
| 901 | return ret; |
| 902 | |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | /** |
| 907 | * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the |
| 908 | * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific |
| 909 | * preparations. Coherency is only guaranteed in the specified range for the |
| 910 | * specified access direction. |
| 911 | * @dmabuf: [in] buffer to prepare cpu access for. |
| 912 | * @direction: [in] length of range for cpu access. |
| 913 | * |
| 914 | * After the cpu access is complete the caller should call |
| 915 | * dma_buf_end_cpu_access(). Only when cpu access is braketed by both calls is |
| 916 | * it guaranteed to be coherent with other DMA access. |
| 917 | * |
| 918 | * Can return negative error values, returns 0 on success. |
| 919 | */ |
| 920 | int dma_buf_begin_cpu_access(struct dma_buf *dmabuf, |
| 921 | enum dma_data_direction direction) |
| 922 | { |
| 923 | int ret = 0; |
| 924 | |
| 925 | if (WARN_ON(!dmabuf)) |
| 926 | return -EINVAL; |
| 927 | |
| 928 | if (dmabuf->ops->begin_cpu_access) |
| 929 | ret = dmabuf->ops->begin_cpu_access(dmabuf, direction); |
| 930 | |
| 931 | /* Ensure that all fences are waited upon - but we first allow |
| 932 | * the native handler the chance to do so more efficiently if it |
| 933 | * chooses. A double invocation here will be reasonably cheap no-op. |
| 934 | */ |
| 935 | if (ret == 0) |
| 936 | ret = __dma_buf_begin_cpu_access(dmabuf, direction); |
| 937 | |
| 938 | return ret; |
| 939 | } |
| 940 | EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access); |
| 941 | |
| 942 | /** |
| 943 | * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the |
| 944 | * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific |
| 945 | * actions. Coherency is only guaranteed in the specified range for the |
| 946 | * specified access direction. |
| 947 | * @dmabuf: [in] buffer to complete cpu access for. |
| 948 | * @direction: [in] length of range for cpu access. |
| 949 | * |
| 950 | * This terminates CPU access started with dma_buf_begin_cpu_access(). |
| 951 | * |
| 952 | * Can return negative error values, returns 0 on success. |
| 953 | */ |
| 954 | int dma_buf_end_cpu_access(struct dma_buf *dmabuf, |
| 955 | enum dma_data_direction direction) |
| 956 | { |
| 957 | int ret = 0; |
| 958 | |
| 959 | WARN_ON(!dmabuf); |
| 960 | |
| 961 | if (dmabuf->ops->end_cpu_access) |
| 962 | ret = dmabuf->ops->end_cpu_access(dmabuf, direction); |
| 963 | |
| 964 | return ret; |
| 965 | } |
| 966 | EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access); |
| 967 | |
| 968 | /** |
| 969 | * dma_buf_kmap - Map a page of the buffer object into kernel address space. The |
| 970 | * same restrictions as for kmap and friends apply. |
| 971 | * @dmabuf: [in] buffer to map page from. |
| 972 | * @page_num: [in] page in PAGE_SIZE units to map. |
| 973 | * |
| 974 | * This call must always succeed, any necessary preparations that might fail |
| 975 | * need to be done in begin_cpu_access. |
| 976 | */ |
| 977 | void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num) |
| 978 | { |
| 979 | WARN_ON(!dmabuf); |
| 980 | |
| 981 | if (!dmabuf->ops->map) |
| 982 | return NULL; |
| 983 | return dmabuf->ops->map(dmabuf, page_num); |
| 984 | } |
| 985 | EXPORT_SYMBOL_GPL(dma_buf_kmap); |
| 986 | |
| 987 | /** |
| 988 | * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap. |
| 989 | * @dmabuf: [in] buffer to unmap page from. |
| 990 | * @page_num: [in] page in PAGE_SIZE units to unmap. |
| 991 | * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap. |
| 992 | * |
| 993 | * This call must always succeed. |
| 994 | */ |
| 995 | void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num, |
| 996 | void *vaddr) |
| 997 | { |
| 998 | WARN_ON(!dmabuf); |
| 999 | |
| 1000 | if (dmabuf->ops->unmap) |
| 1001 | dmabuf->ops->unmap(dmabuf, page_num, vaddr); |
| 1002 | } |
| 1003 | EXPORT_SYMBOL_GPL(dma_buf_kunmap); |
| 1004 | |
| 1005 | |
| 1006 | /** |
| 1007 | * dma_buf_mmap - Setup up a userspace mmap with the given vma |
| 1008 | * @dmabuf: [in] buffer that should back the vma |
| 1009 | * @vma: [in] vma for the mmap |
| 1010 | * @pgoff: [in] offset in pages where this mmap should start within the |
| 1011 | * dma-buf buffer. |
| 1012 | * |
| 1013 | * This function adjusts the passed in vma so that it points at the file of the |
| 1014 | * dma_buf operation. It also adjusts the starting pgoff and does bounds |
| 1015 | * checking on the size of the vma. Then it calls the exporters mmap function to |
| 1016 | * set up the mapping. |
| 1017 | * |
| 1018 | * Can return negative error values, returns 0 on success. |
| 1019 | */ |
| 1020 | int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma, |
| 1021 | unsigned long pgoff) |
| 1022 | { |
| 1023 | struct file *oldfile; |
| 1024 | int ret; |
| 1025 | |
| 1026 | if (WARN_ON(!dmabuf || !vma)) |
| 1027 | return -EINVAL; |
| 1028 | |
| 1029 | /* check for offset overflow */ |
| 1030 | if (pgoff + vma_pages(vma) < pgoff) |
| 1031 | return -EOVERFLOW; |
| 1032 | |
| 1033 | /* check for overflowing the buffer's size */ |
| 1034 | if (pgoff + vma_pages(vma) > |
| 1035 | dmabuf->size >> PAGE_SHIFT) |
| 1036 | return -EINVAL; |
| 1037 | |
| 1038 | /* readjust the vma */ |
| 1039 | get_file(dmabuf->file); |
| 1040 | oldfile = vma->vm_file; |
| 1041 | vma->vm_file = dmabuf->file; |
| 1042 | vma->vm_pgoff = pgoff; |
| 1043 | |
| 1044 | ret = dmabuf->ops->mmap(dmabuf, vma); |
| 1045 | if (ret) { |
| 1046 | /* restore old parameters on failure */ |
| 1047 | vma->vm_file = oldfile; |
| 1048 | fput(dmabuf->file); |
| 1049 | } else { |
| 1050 | if (oldfile) |
| 1051 | fput(oldfile); |
| 1052 | } |
| 1053 | return ret; |
| 1054 | |
| 1055 | } |
| 1056 | EXPORT_SYMBOL_GPL(dma_buf_mmap); |
| 1057 | |
| 1058 | /** |
| 1059 | * dma_buf_vmap - Create virtual mapping for the buffer object into kernel |
| 1060 | * address space. Same restrictions as for vmap and friends apply. |
| 1061 | * @dmabuf: [in] buffer to vmap |
| 1062 | * |
| 1063 | * This call may fail due to lack of virtual mapping address space. |
| 1064 | * These calls are optional in drivers. The intended use for them |
| 1065 | * is for mapping objects linear in kernel space for high use objects. |
| 1066 | * Please attempt to use kmap/kunmap before thinking about these interfaces. |
| 1067 | * |
| 1068 | * Returns NULL on error. |
| 1069 | */ |
| 1070 | void *dma_buf_vmap(struct dma_buf *dmabuf) |
| 1071 | { |
| 1072 | void *ptr; |
| 1073 | |
| 1074 | if (WARN_ON(!dmabuf)) |
| 1075 | return NULL; |
| 1076 | |
| 1077 | if (!dmabuf->ops->vmap) |
| 1078 | return NULL; |
| 1079 | |
| 1080 | mutex_lock(&dmabuf->lock); |
| 1081 | if (dmabuf->vmapping_counter) { |
| 1082 | dmabuf->vmapping_counter++; |
| 1083 | BUG_ON(!dmabuf->vmap_ptr); |
| 1084 | ptr = dmabuf->vmap_ptr; |
| 1085 | goto out_unlock; |
| 1086 | } |
| 1087 | |
| 1088 | BUG_ON(dmabuf->vmap_ptr); |
| 1089 | |
| 1090 | ptr = dmabuf->ops->vmap(dmabuf); |
| 1091 | if (WARN_ON_ONCE(IS_ERR(ptr))) |
| 1092 | ptr = NULL; |
| 1093 | if (!ptr) |
| 1094 | goto out_unlock; |
| 1095 | |
| 1096 | dmabuf->vmap_ptr = ptr; |
| 1097 | dmabuf->vmapping_counter = 1; |
| 1098 | |
| 1099 | out_unlock: |
| 1100 | mutex_unlock(&dmabuf->lock); |
| 1101 | return ptr; |
| 1102 | } |
| 1103 | EXPORT_SYMBOL_GPL(dma_buf_vmap); |
| 1104 | |
| 1105 | /** |
| 1106 | * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap. |
| 1107 | * @dmabuf: [in] buffer to vunmap |
| 1108 | * @vaddr: [in] vmap to vunmap |
| 1109 | */ |
| 1110 | void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr) |
| 1111 | { |
| 1112 | if (WARN_ON(!dmabuf)) |
| 1113 | return; |
| 1114 | |
| 1115 | BUG_ON(!dmabuf->vmap_ptr); |
| 1116 | BUG_ON(dmabuf->vmapping_counter == 0); |
| 1117 | BUG_ON(dmabuf->vmap_ptr != vaddr); |
| 1118 | |
| 1119 | mutex_lock(&dmabuf->lock); |
| 1120 | if (--dmabuf->vmapping_counter == 0) { |
| 1121 | if (dmabuf->ops->vunmap) |
| 1122 | dmabuf->ops->vunmap(dmabuf, vaddr); |
| 1123 | dmabuf->vmap_ptr = NULL; |
| 1124 | } |
| 1125 | mutex_unlock(&dmabuf->lock); |
| 1126 | } |
| 1127 | EXPORT_SYMBOL_GPL(dma_buf_vunmap); |
| 1128 | |
| 1129 | #ifdef CONFIG_DEBUG_FS |
| 1130 | static int dma_buf_debug_show(struct seq_file *s, void *unused) |
| 1131 | { |
| 1132 | int ret; |
| 1133 | struct dma_buf *buf_obj; |
| 1134 | struct dma_buf_attachment *attach_obj; |
| 1135 | struct reservation_object *robj; |
| 1136 | struct reservation_object_list *fobj; |
| 1137 | struct dma_fence *fence; |
| 1138 | unsigned seq; |
| 1139 | int count = 0, attach_count, shared_count, i; |
| 1140 | size_t size = 0; |
| 1141 | |
| 1142 | ret = mutex_lock_interruptible(&db_list.lock); |
| 1143 | |
| 1144 | if (ret) |
| 1145 | return ret; |
| 1146 | |
| 1147 | seq_puts(s, "\nDma-buf Objects:\n"); |
| 1148 | seq_printf(s, "%-18s\t%-8s\t%-8s\t%-8s\t%-8s\texp_name\t%-8s\n", |
| 1149 | "priv", "size", "flags", "mode", "count", "ino"); |
| 1150 | |
| 1151 | list_for_each_entry(buf_obj, &db_list.head, list_node) { |
| 1152 | ret = mutex_lock_interruptible(&buf_obj->lock); |
| 1153 | |
| 1154 | if (ret) { |
| 1155 | seq_puts(s, |
| 1156 | "\tERROR locking buffer object: skipping\n"); |
| 1157 | continue; |
| 1158 | } |
| 1159 | |
| 1160 | seq_printf(s, "0x%p\t%08zu\t%08x\t%08x\t%08ld\t%s\t%08lu\t%s\n", |
| 1161 | buf_obj->priv, |
| 1162 | buf_obj->size, |
| 1163 | buf_obj->file->f_flags, buf_obj->file->f_mode, |
| 1164 | file_count(buf_obj->file), |
| 1165 | buf_obj->exp_name, |
| 1166 | file_inode(buf_obj->file)->i_ino, |
| 1167 | buf_obj->name ?: ""); |
| 1168 | |
| 1169 | robj = buf_obj->resv; |
| 1170 | while (true) { |
| 1171 | seq = read_seqcount_begin(&robj->seq); |
| 1172 | rcu_read_lock(); |
| 1173 | fobj = rcu_dereference(robj->fence); |
| 1174 | shared_count = fobj ? fobj->shared_count : 0; |
| 1175 | fence = rcu_dereference(robj->fence_excl); |
| 1176 | if (!read_seqcount_retry(&robj->seq, seq)) |
| 1177 | break; |
| 1178 | rcu_read_unlock(); |
| 1179 | } |
| 1180 | |
| 1181 | if (fence) |
| 1182 | seq_printf(s, "\tExclusive fence: %s %s %ssignalled\n", |
| 1183 | fence->ops->get_driver_name(fence), |
| 1184 | fence->ops->get_timeline_name(fence), |
| 1185 | dma_fence_is_signaled(fence) ? "" : "un"); |
| 1186 | for (i = 0; i < shared_count; i++) { |
| 1187 | fence = rcu_dereference(fobj->shared[i]); |
| 1188 | if (!dma_fence_get_rcu(fence)) |
| 1189 | continue; |
| 1190 | seq_printf(s, "\tShared fence: %s %s %ssignalled\n", |
| 1191 | fence->ops->get_driver_name(fence), |
| 1192 | fence->ops->get_timeline_name(fence), |
| 1193 | dma_fence_is_signaled(fence) ? "" : "un"); |
| 1194 | dma_fence_put(fence); |
| 1195 | } |
| 1196 | rcu_read_unlock(); |
| 1197 | |
| 1198 | seq_puts(s, "\tAttached Devices:\n"); |
| 1199 | attach_count = 0; |
| 1200 | |
| 1201 | list_for_each_entry(attach_obj, &buf_obj->attachments, node) { |
| 1202 | seq_printf(s, "\t%s\n", dev_name(attach_obj->dev)); |
| 1203 | attach_count++; |
| 1204 | } |
| 1205 | |
| 1206 | seq_printf(s, "Total %d devices attached\n\n", |
| 1207 | attach_count); |
| 1208 | |
| 1209 | count++; |
| 1210 | size += buf_obj->size; |
| 1211 | mutex_unlock(&buf_obj->lock); |
| 1212 | } |
| 1213 | |
| 1214 | seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size); |
| 1215 | |
| 1216 | mutex_unlock(&db_list.lock); |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
| 1220 | static int dma_buf_debug_open(struct inode *inode, struct file *file) |
| 1221 | { |
| 1222 | return single_open(file, dma_buf_debug_show, NULL); |
| 1223 | } |
| 1224 | |
| 1225 | static const struct file_operations dma_buf_debug_fops = { |
| 1226 | .open = dma_buf_debug_open, |
| 1227 | .read = seq_read, |
| 1228 | .llseek = seq_lseek, |
| 1229 | .release = single_release, |
| 1230 | }; |
| 1231 | |
| 1232 | static struct dentry *dma_buf_debugfs_dir; |
| 1233 | |
| 1234 | static int dma_buf_init_debugfs(void) |
| 1235 | { |
| 1236 | struct dentry *d; |
| 1237 | int err = 0; |
| 1238 | |
| 1239 | d = debugfs_create_dir("dma_buf", NULL); |
| 1240 | if (IS_ERR(d)) |
| 1241 | return PTR_ERR(d); |
| 1242 | |
| 1243 | dma_buf_debugfs_dir = d; |
| 1244 | |
| 1245 | d = debugfs_create_file("bufinfo", S_IRUGO, dma_buf_debugfs_dir, |
| 1246 | NULL, &dma_buf_debug_fops); |
| 1247 | if (IS_ERR(d)) { |
| 1248 | pr_debug("dma_buf: debugfs: failed to create node bufinfo\n"); |
| 1249 | debugfs_remove_recursive(dma_buf_debugfs_dir); |
| 1250 | dma_buf_debugfs_dir = NULL; |
| 1251 | err = PTR_ERR(d); |
| 1252 | } |
| 1253 | |
| 1254 | return err; |
| 1255 | } |
| 1256 | |
| 1257 | static void dma_buf_uninit_debugfs(void) |
| 1258 | { |
| 1259 | debugfs_remove_recursive(dma_buf_debugfs_dir); |
| 1260 | } |
| 1261 | #else |
| 1262 | static inline int dma_buf_init_debugfs(void) |
| 1263 | { |
| 1264 | return 0; |
| 1265 | } |
| 1266 | static inline void dma_buf_uninit_debugfs(void) |
| 1267 | { |
| 1268 | } |
| 1269 | #endif |
| 1270 | |
| 1271 | static int __init dma_buf_init(void) |
| 1272 | { |
| 1273 | dma_buf_mnt = kern_mount(&dma_buf_fs_type); |
| 1274 | if (IS_ERR(dma_buf_mnt)) |
| 1275 | return PTR_ERR(dma_buf_mnt); |
| 1276 | |
| 1277 | mutex_init(&db_list.lock); |
| 1278 | INIT_LIST_HEAD(&db_list.head); |
| 1279 | dma_buf_init_debugfs(); |
| 1280 | return 0; |
| 1281 | } |
| 1282 | subsys_initcall(dma_buf_init); |
| 1283 | |
| 1284 | static void __exit dma_buf_deinit(void) |
| 1285 | { |
| 1286 | dma_buf_uninit_debugfs(); |
| 1287 | kern_unmount(dma_buf_mnt); |
| 1288 | } |
| 1289 | module_exit(dma_buf_deinit); |
| 1290 | MODULE_LICENSE("GPL"); |