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