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> |
Maarten Lankhorst | 3aac450 | 2014-07-01 12:57:26 +0200 | [diff] [blame] | 28 | #include <linux/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 | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 31 | #include <linux/debugfs.h> |
| 32 | #include <linux/seq_file.h> |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 33 | #include <linux/poll.h> |
Maarten Lankhorst | 3aac450 | 2014-07-01 12:57:26 +0200 | [diff] [blame] | 34 | #include <linux/reservation.h> |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 35 | |
| 36 | static inline int is_dma_buf_file(struct file *); |
| 37 | |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 38 | struct dma_buf_list { |
| 39 | struct list_head head; |
| 40 | struct mutex lock; |
| 41 | }; |
| 42 | |
| 43 | static struct dma_buf_list db_list; |
| 44 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 45 | static int dma_buf_release(struct inode *inode, struct file *file) |
| 46 | { |
| 47 | struct dma_buf *dmabuf; |
| 48 | |
| 49 | if (!is_dma_buf_file(file)) |
| 50 | return -EINVAL; |
| 51 | |
| 52 | dmabuf = file->private_data; |
| 53 | |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 54 | BUG_ON(dmabuf->vmapping_counter); |
| 55 | |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 56 | /* |
| 57 | * Any fences that a dma-buf poll can wait on should be signaled |
| 58 | * before releasing dma-buf. This is the responsibility of each |
| 59 | * driver that uses the reservation objects. |
| 60 | * |
| 61 | * If you hit this BUG() it means someone dropped their ref to the |
| 62 | * dma-buf while still having pending operation to the buffer. |
| 63 | */ |
| 64 | BUG_ON(dmabuf->cb_shared.active || dmabuf->cb_excl.active); |
| 65 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 66 | dmabuf->ops->release(dmabuf); |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 67 | |
| 68 | mutex_lock(&db_list.lock); |
| 69 | list_del(&dmabuf->list_node); |
| 70 | mutex_unlock(&db_list.lock); |
| 71 | |
Maarten Lankhorst | 3aac450 | 2014-07-01 12:57:26 +0200 | [diff] [blame] | 72 | if (dmabuf->resv == (struct reservation_object *)&dmabuf[1]) |
| 73 | reservation_object_fini(dmabuf->resv); |
| 74 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 75 | kfree(dmabuf); |
| 76 | return 0; |
| 77 | } |
| 78 | |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 79 | static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma) |
| 80 | { |
| 81 | struct dma_buf *dmabuf; |
| 82 | |
| 83 | if (!is_dma_buf_file(file)) |
| 84 | return -EINVAL; |
| 85 | |
| 86 | dmabuf = file->private_data; |
| 87 | |
| 88 | /* check for overflowing the buffer's size */ |
| 89 | if (vma->vm_pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) > |
| 90 | dmabuf->size >> PAGE_SHIFT) |
| 91 | return -EINVAL; |
| 92 | |
| 93 | return dmabuf->ops->mmap(dmabuf, vma); |
| 94 | } |
| 95 | |
Christopher James Halse Rogers | 19e8697 | 2013-09-10 11:36:45 +0530 | [diff] [blame] | 96 | static loff_t dma_buf_llseek(struct file *file, loff_t offset, int whence) |
| 97 | { |
| 98 | struct dma_buf *dmabuf; |
| 99 | loff_t base; |
| 100 | |
| 101 | if (!is_dma_buf_file(file)) |
| 102 | return -EBADF; |
| 103 | |
| 104 | dmabuf = file->private_data; |
| 105 | |
| 106 | /* only support discovering the end of the buffer, |
| 107 | but also allow SEEK_SET to maintain the idiomatic |
| 108 | SEEK_END(0), SEEK_CUR(0) pattern */ |
| 109 | if (whence == SEEK_END) |
| 110 | base = dmabuf->size; |
| 111 | else if (whence == SEEK_SET) |
| 112 | base = 0; |
| 113 | else |
| 114 | return -EINVAL; |
| 115 | |
| 116 | if (offset != 0) |
| 117 | return -EINVAL; |
| 118 | |
| 119 | return base + offset; |
| 120 | } |
| 121 | |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 122 | static void dma_buf_poll_cb(struct fence *fence, struct fence_cb *cb) |
| 123 | { |
| 124 | struct dma_buf_poll_cb_t *dcb = (struct dma_buf_poll_cb_t *)cb; |
| 125 | unsigned long flags; |
| 126 | |
| 127 | spin_lock_irqsave(&dcb->poll->lock, flags); |
| 128 | wake_up_locked_poll(dcb->poll, dcb->active); |
| 129 | dcb->active = 0; |
| 130 | spin_unlock_irqrestore(&dcb->poll->lock, flags); |
| 131 | } |
| 132 | |
| 133 | static unsigned int dma_buf_poll(struct file *file, poll_table *poll) |
| 134 | { |
| 135 | struct dma_buf *dmabuf; |
| 136 | struct reservation_object *resv; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 137 | struct reservation_object_list *fobj; |
| 138 | struct fence *fence_excl; |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 139 | unsigned long events; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 140 | unsigned shared_count, seq; |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 141 | |
| 142 | dmabuf = file->private_data; |
| 143 | if (!dmabuf || !dmabuf->resv) |
| 144 | return POLLERR; |
| 145 | |
| 146 | resv = dmabuf->resv; |
| 147 | |
| 148 | poll_wait(file, &dmabuf->poll, poll); |
| 149 | |
| 150 | events = poll_requested_events(poll) & (POLLIN | POLLOUT); |
| 151 | if (!events) |
| 152 | return 0; |
| 153 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 154 | retry: |
| 155 | seq = read_seqcount_begin(&resv->seq); |
| 156 | rcu_read_lock(); |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 157 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 158 | fobj = rcu_dereference(resv->fence); |
| 159 | if (fobj) |
| 160 | shared_count = fobj->shared_count; |
| 161 | else |
| 162 | shared_count = 0; |
| 163 | fence_excl = rcu_dereference(resv->fence_excl); |
| 164 | if (read_seqcount_retry(&resv->seq, seq)) { |
| 165 | rcu_read_unlock(); |
| 166 | goto retry; |
| 167 | } |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 168 | |
| 169 | if (fence_excl && (!(events & POLLOUT) || shared_count == 0)) { |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 170 | struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_excl; |
| 171 | unsigned long pevents = POLLIN; |
| 172 | |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 173 | if (shared_count == 0) |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 174 | pevents |= POLLOUT; |
| 175 | |
| 176 | spin_lock_irq(&dmabuf->poll.lock); |
| 177 | if (dcb->active) { |
| 178 | dcb->active |= pevents; |
| 179 | events &= ~pevents; |
| 180 | } else |
| 181 | dcb->active = pevents; |
| 182 | spin_unlock_irq(&dmabuf->poll.lock); |
| 183 | |
| 184 | if (events & pevents) { |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 185 | if (!fence_get_rcu(fence_excl)) { |
| 186 | /* force a recheck */ |
| 187 | events &= ~pevents; |
| 188 | dma_buf_poll_cb(NULL, &dcb->cb); |
| 189 | } else if (!fence_add_callback(fence_excl, &dcb->cb, |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 190 | dma_buf_poll_cb)) { |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 191 | events &= ~pevents; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 192 | fence_put(fence_excl); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 193 | } else { |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 194 | /* |
| 195 | * No callback queued, wake up any additional |
| 196 | * waiters. |
| 197 | */ |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 198 | fence_put(fence_excl); |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 199 | dma_buf_poll_cb(NULL, &dcb->cb); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 200 | } |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 204 | if ((events & POLLOUT) && shared_count > 0) { |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 205 | struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_shared; |
| 206 | int i; |
| 207 | |
| 208 | /* Only queue a new callback if no event has fired yet */ |
| 209 | spin_lock_irq(&dmabuf->poll.lock); |
| 210 | if (dcb->active) |
| 211 | events &= ~POLLOUT; |
| 212 | else |
| 213 | dcb->active = POLLOUT; |
| 214 | spin_unlock_irq(&dmabuf->poll.lock); |
| 215 | |
| 216 | if (!(events & POLLOUT)) |
| 217 | goto out; |
| 218 | |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 219 | for (i = 0; i < shared_count; ++i) { |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 220 | struct fence *fence = rcu_dereference(fobj->shared[i]); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 221 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 222 | if (!fence_get_rcu(fence)) { |
| 223 | /* |
| 224 | * fence refcount dropped to zero, this means |
| 225 | * that fobj has been freed |
| 226 | * |
| 227 | * call dma_buf_poll_cb and force a recheck! |
| 228 | */ |
| 229 | events &= ~POLLOUT; |
| 230 | dma_buf_poll_cb(NULL, &dcb->cb); |
| 231 | break; |
| 232 | } |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 233 | if (!fence_add_callback(fence, &dcb->cb, |
| 234 | dma_buf_poll_cb)) { |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 235 | fence_put(fence); |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 236 | events &= ~POLLOUT; |
| 237 | break; |
| 238 | } |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 239 | fence_put(fence); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 240 | } |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 241 | |
| 242 | /* No callback queued, wake up any additional waiters. */ |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 243 | if (i == shared_count) |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 244 | dma_buf_poll_cb(NULL, &dcb->cb); |
| 245 | } |
| 246 | |
| 247 | out: |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 248 | rcu_read_unlock(); |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 249 | return events; |
| 250 | } |
| 251 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 252 | static const struct file_operations dma_buf_fops = { |
| 253 | .release = dma_buf_release, |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 254 | .mmap = dma_buf_mmap_internal, |
Christopher James Halse Rogers | 19e8697 | 2013-09-10 11:36:45 +0530 | [diff] [blame] | 255 | .llseek = dma_buf_llseek, |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 256 | .poll = dma_buf_poll, |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 257 | }; |
| 258 | |
| 259 | /* |
| 260 | * is_dma_buf_file - Check if struct file* is associated with dma_buf |
| 261 | */ |
| 262 | static inline int is_dma_buf_file(struct file *file) |
| 263 | { |
| 264 | return file->f_op == &dma_buf_fops; |
| 265 | } |
| 266 | |
| 267 | /** |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 268 | * 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] | 269 | * with this buffer, so it can be exported. |
| 270 | * Also connect the allocator specific data and ops to the buffer. |
Sumit Semwal | 78df969 | 2013-03-22 18:22:16 +0530 | [diff] [blame] | 271 | * Additionally, provide a name string for exporter; useful in debugging. |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 272 | * |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 273 | * @exp_info: [in] holds all the export related information provided |
| 274 | * by the exporter. see struct dma_buf_export_info |
| 275 | * for further details. |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 276 | * |
| 277 | * Returns, on success, a newly created dma_buf object, which wraps the |
| 278 | * supplied private data and operations for dma_buf_ops. On either missing |
| 279 | * ops, or error in allocating struct dma_buf, will return negative error. |
| 280 | * |
| 281 | */ |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 282 | 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] | 283 | { |
| 284 | struct dma_buf *dmabuf; |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 285 | struct reservation_object *resv = exp_info->resv; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 286 | struct file *file; |
Maarten Lankhorst | 3aac450 | 2014-07-01 12:57:26 +0200 | [diff] [blame] | 287 | size_t alloc_size = sizeof(struct dma_buf); |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 288 | if (!exp_info->resv) |
Maarten Lankhorst | 3aac450 | 2014-07-01 12:57:26 +0200 | [diff] [blame] | 289 | alloc_size += sizeof(struct reservation_object); |
| 290 | else |
| 291 | /* prevent &dma_buf[1] == dma_buf->resv */ |
| 292 | alloc_size += 1; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 293 | |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 294 | if (WARN_ON(!exp_info->priv |
| 295 | || !exp_info->ops |
| 296 | || !exp_info->ops->map_dma_buf |
| 297 | || !exp_info->ops->unmap_dma_buf |
| 298 | || !exp_info->ops->release |
| 299 | || !exp_info->ops->kmap_atomic |
| 300 | || !exp_info->ops->kmap |
| 301 | || !exp_info->ops->mmap)) { |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 302 | return ERR_PTR(-EINVAL); |
| 303 | } |
| 304 | |
Maarten Lankhorst | 3aac450 | 2014-07-01 12:57:26 +0200 | [diff] [blame] | 305 | dmabuf = kzalloc(alloc_size, GFP_KERNEL); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 306 | if (dmabuf == NULL) |
| 307 | return ERR_PTR(-ENOMEM); |
| 308 | |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 309 | dmabuf->priv = exp_info->priv; |
| 310 | dmabuf->ops = exp_info->ops; |
| 311 | dmabuf->size = exp_info->size; |
| 312 | dmabuf->exp_name = exp_info->exp_name; |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 313 | init_waitqueue_head(&dmabuf->poll); |
| 314 | dmabuf->cb_excl.poll = dmabuf->cb_shared.poll = &dmabuf->poll; |
| 315 | dmabuf->cb_excl.active = dmabuf->cb_shared.active = 0; |
| 316 | |
Maarten Lankhorst | 3aac450 | 2014-07-01 12:57:26 +0200 | [diff] [blame] | 317 | if (!resv) { |
| 318 | resv = (struct reservation_object *)&dmabuf[1]; |
| 319 | reservation_object_init(resv); |
| 320 | } |
| 321 | dmabuf->resv = resv; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 322 | |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 323 | file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf, |
| 324 | exp_info->flags); |
Tuomas Tynkkynen | 9022e24 | 2013-08-27 16:30:38 +0300 | [diff] [blame] | 325 | if (IS_ERR(file)) { |
| 326 | kfree(dmabuf); |
| 327 | return ERR_CAST(file); |
| 328 | } |
Christopher James Halse Rogers | 19e8697 | 2013-09-10 11:36:45 +0530 | [diff] [blame] | 329 | |
| 330 | file->f_mode |= FMODE_LSEEK; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 331 | dmabuf->file = file; |
| 332 | |
| 333 | mutex_init(&dmabuf->lock); |
| 334 | INIT_LIST_HEAD(&dmabuf->attachments); |
| 335 | |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 336 | mutex_lock(&db_list.lock); |
| 337 | list_add(&dmabuf->list_node, &db_list.head); |
| 338 | mutex_unlock(&db_list.lock); |
| 339 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 340 | return dmabuf; |
| 341 | } |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 342 | EXPORT_SYMBOL_GPL(dma_buf_export); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 343 | |
| 344 | /** |
| 345 | * dma_buf_fd - returns a file descriptor for the given dma_buf |
| 346 | * @dmabuf: [in] pointer to dma_buf for which fd is required. |
Dave Airlie | 55c1c4c | 2012-03-16 10:34:02 +0000 | [diff] [blame] | 347 | * @flags: [in] flags to give to fd |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 348 | * |
| 349 | * On success, returns an associated 'fd'. Else, returns error. |
| 350 | */ |
Dave Airlie | 55c1c4c | 2012-03-16 10:34:02 +0000 | [diff] [blame] | 351 | int dma_buf_fd(struct dma_buf *dmabuf, int flags) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 352 | { |
Borislav Petkov | f5e097f | 2012-12-11 16:05:26 +0100 | [diff] [blame] | 353 | int fd; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 354 | |
| 355 | if (!dmabuf || !dmabuf->file) |
| 356 | return -EINVAL; |
| 357 | |
Borislav Petkov | f5e097f | 2012-12-11 16:05:26 +0100 | [diff] [blame] | 358 | fd = get_unused_fd_flags(flags); |
| 359 | if (fd < 0) |
| 360 | return fd; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 361 | |
| 362 | fd_install(fd, dmabuf->file); |
| 363 | |
| 364 | return fd; |
| 365 | } |
| 366 | EXPORT_SYMBOL_GPL(dma_buf_fd); |
| 367 | |
| 368 | /** |
| 369 | * dma_buf_get - returns the dma_buf structure related to an fd |
| 370 | * @fd: [in] fd associated with the dma_buf to be returned |
| 371 | * |
| 372 | * On success, returns the dma_buf structure associated with an fd; uses |
| 373 | * file's refcounting done by fget to increase refcount. returns ERR_PTR |
| 374 | * otherwise. |
| 375 | */ |
| 376 | struct dma_buf *dma_buf_get(int fd) |
| 377 | { |
| 378 | struct file *file; |
| 379 | |
| 380 | file = fget(fd); |
| 381 | |
| 382 | if (!file) |
| 383 | return ERR_PTR(-EBADF); |
| 384 | |
| 385 | if (!is_dma_buf_file(file)) { |
| 386 | fput(file); |
| 387 | return ERR_PTR(-EINVAL); |
| 388 | } |
| 389 | |
| 390 | return file->private_data; |
| 391 | } |
| 392 | EXPORT_SYMBOL_GPL(dma_buf_get); |
| 393 | |
| 394 | /** |
| 395 | * dma_buf_put - decreases refcount of the buffer |
| 396 | * @dmabuf: [in] buffer to reduce refcount of |
| 397 | * |
| 398 | * Uses file's refcounting done implicitly by fput() |
| 399 | */ |
| 400 | void dma_buf_put(struct dma_buf *dmabuf) |
| 401 | { |
| 402 | if (WARN_ON(!dmabuf || !dmabuf->file)) |
| 403 | return; |
| 404 | |
| 405 | fput(dmabuf->file); |
| 406 | } |
| 407 | EXPORT_SYMBOL_GPL(dma_buf_put); |
| 408 | |
| 409 | /** |
| 410 | * dma_buf_attach - Add the device to dma_buf's attachments list; optionally, |
| 411 | * calls attach() of dma_buf_ops to allow device-specific attach functionality |
| 412 | * @dmabuf: [in] buffer to attach device to. |
| 413 | * @dev: [in] device to be attached. |
| 414 | * |
Colin Cross | fee0c54 | 2013-12-20 16:43:50 -0800 | [diff] [blame] | 415 | * Returns struct dma_buf_attachment * for this attachment; returns ERR_PTR on |
| 416 | * error. |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 417 | */ |
| 418 | struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf, |
| 419 | struct device *dev) |
| 420 | { |
| 421 | struct dma_buf_attachment *attach; |
| 422 | int ret; |
| 423 | |
Laurent Pinchart | d1aa06a | 2012-01-26 12:27:23 +0100 | [diff] [blame] | 424 | if (WARN_ON(!dmabuf || !dev)) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 425 | return ERR_PTR(-EINVAL); |
| 426 | |
| 427 | attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL); |
| 428 | if (attach == NULL) |
Laurent Pinchart | a9fbc3b | 2012-01-26 12:27:24 +0100 | [diff] [blame] | 429 | return ERR_PTR(-ENOMEM); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 430 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 431 | attach->dev = dev; |
| 432 | attach->dmabuf = dmabuf; |
Laurent Pinchart | 2ed9201 | 2012-01-26 12:27:25 +0100 | [diff] [blame] | 433 | |
| 434 | mutex_lock(&dmabuf->lock); |
| 435 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 436 | if (dmabuf->ops->attach) { |
| 437 | ret = dmabuf->ops->attach(dmabuf, dev, attach); |
| 438 | if (ret) |
| 439 | goto err_attach; |
| 440 | } |
| 441 | list_add(&attach->node, &dmabuf->attachments); |
| 442 | |
| 443 | mutex_unlock(&dmabuf->lock); |
| 444 | return attach; |
| 445 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 446 | err_attach: |
| 447 | kfree(attach); |
| 448 | mutex_unlock(&dmabuf->lock); |
| 449 | return ERR_PTR(ret); |
| 450 | } |
| 451 | EXPORT_SYMBOL_GPL(dma_buf_attach); |
| 452 | |
| 453 | /** |
| 454 | * dma_buf_detach - Remove the given attachment from dmabuf's attachments list; |
| 455 | * optionally calls detach() of dma_buf_ops for device-specific detach |
| 456 | * @dmabuf: [in] buffer to detach from. |
| 457 | * @attach: [in] attachment to be detached; is free'd after this call. |
| 458 | * |
| 459 | */ |
| 460 | void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach) |
| 461 | { |
Laurent Pinchart | d1aa06a | 2012-01-26 12:27:23 +0100 | [diff] [blame] | 462 | if (WARN_ON(!dmabuf || !attach)) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 463 | return; |
| 464 | |
| 465 | mutex_lock(&dmabuf->lock); |
| 466 | list_del(&attach->node); |
| 467 | if (dmabuf->ops->detach) |
| 468 | dmabuf->ops->detach(dmabuf, attach); |
| 469 | |
| 470 | mutex_unlock(&dmabuf->lock); |
| 471 | kfree(attach); |
| 472 | } |
| 473 | EXPORT_SYMBOL_GPL(dma_buf_detach); |
| 474 | |
| 475 | /** |
| 476 | * dma_buf_map_attachment - Returns the scatterlist table of the attachment; |
| 477 | * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the |
| 478 | * dma_buf_ops. |
| 479 | * @attach: [in] attachment whose scatterlist is to be returned |
| 480 | * @direction: [in] direction of DMA transfer |
| 481 | * |
Colin Cross | fee0c54 | 2013-12-20 16:43:50 -0800 | [diff] [blame] | 482 | * Returns sg_table containing the scatterlist to be returned; returns ERR_PTR |
| 483 | * on error. |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 484 | */ |
| 485 | struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach, |
| 486 | enum dma_data_direction direction) |
| 487 | { |
| 488 | struct sg_table *sg_table = ERR_PTR(-EINVAL); |
| 489 | |
| 490 | might_sleep(); |
| 491 | |
Laurent Pinchart | d1aa06a | 2012-01-26 12:27:23 +0100 | [diff] [blame] | 492 | if (WARN_ON(!attach || !attach->dmabuf)) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 493 | return ERR_PTR(-EINVAL); |
| 494 | |
Laurent Pinchart | d1aa06a | 2012-01-26 12:27:23 +0100 | [diff] [blame] | 495 | sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction); |
Colin Cross | fee0c54 | 2013-12-20 16:43:50 -0800 | [diff] [blame] | 496 | if (!sg_table) |
| 497 | sg_table = ERR_PTR(-ENOMEM); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 498 | |
| 499 | return sg_table; |
| 500 | } |
| 501 | EXPORT_SYMBOL_GPL(dma_buf_map_attachment); |
| 502 | |
| 503 | /** |
| 504 | * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might |
| 505 | * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of |
| 506 | * dma_buf_ops. |
| 507 | * @attach: [in] attachment to unmap buffer from |
| 508 | * @sg_table: [in] scatterlist info of the buffer to unmap |
Sumit Semwal | 33ea2dc | 2012-01-27 15:09:27 +0530 | [diff] [blame] | 509 | * @direction: [in] direction of DMA transfer |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 510 | * |
| 511 | */ |
| 512 | void dma_buf_unmap_attachment(struct dma_buf_attachment *attach, |
Sumit Semwal | 33ea2dc | 2012-01-27 15:09:27 +0530 | [diff] [blame] | 513 | struct sg_table *sg_table, |
| 514 | enum dma_data_direction direction) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 515 | { |
Rob Clark | b6fa0cd | 2012-09-28 09:29:43 +0200 | [diff] [blame] | 516 | might_sleep(); |
| 517 | |
Laurent Pinchart | d1aa06a | 2012-01-26 12:27:23 +0100 | [diff] [blame] | 518 | if (WARN_ON(!attach || !attach->dmabuf || !sg_table)) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 519 | return; |
| 520 | |
Sumit Semwal | 33ea2dc | 2012-01-27 15:09:27 +0530 | [diff] [blame] | 521 | attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, |
| 522 | direction); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 523 | } |
| 524 | EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment); |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 525 | |
| 526 | |
| 527 | /** |
| 528 | * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the |
| 529 | * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific |
| 530 | * preparations. Coherency is only guaranteed in the specified range for the |
| 531 | * specified access direction. |
Randy Dunlap | efb4df82 | 2012-04-17 17:03:30 -0700 | [diff] [blame] | 532 | * @dmabuf: [in] buffer to prepare cpu access for. |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 533 | * @start: [in] start of range for cpu access. |
| 534 | * @len: [in] length of range for cpu access. |
| 535 | * @direction: [in] length of range for cpu access. |
| 536 | * |
| 537 | * Can return negative error values, returns 0 on success. |
| 538 | */ |
| 539 | int dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len, |
| 540 | enum dma_data_direction direction) |
| 541 | { |
| 542 | int ret = 0; |
| 543 | |
| 544 | if (WARN_ON(!dmabuf)) |
| 545 | return -EINVAL; |
| 546 | |
| 547 | if (dmabuf->ops->begin_cpu_access) |
| 548 | ret = dmabuf->ops->begin_cpu_access(dmabuf, start, len, direction); |
| 549 | |
| 550 | return ret; |
| 551 | } |
| 552 | EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access); |
| 553 | |
| 554 | /** |
| 555 | * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the |
| 556 | * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific |
| 557 | * actions. Coherency is only guaranteed in the specified range for the |
| 558 | * specified access direction. |
Randy Dunlap | efb4df82 | 2012-04-17 17:03:30 -0700 | [diff] [blame] | 559 | * @dmabuf: [in] buffer to complete cpu access for. |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 560 | * @start: [in] start of range for cpu access. |
| 561 | * @len: [in] length of range for cpu access. |
| 562 | * @direction: [in] length of range for cpu access. |
| 563 | * |
| 564 | * This call must always succeed. |
| 565 | */ |
| 566 | void dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len, |
| 567 | enum dma_data_direction direction) |
| 568 | { |
| 569 | WARN_ON(!dmabuf); |
| 570 | |
| 571 | if (dmabuf->ops->end_cpu_access) |
| 572 | dmabuf->ops->end_cpu_access(dmabuf, start, len, direction); |
| 573 | } |
| 574 | EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access); |
| 575 | |
| 576 | /** |
| 577 | * dma_buf_kmap_atomic - Map a page of the buffer object into kernel address |
| 578 | * space. The same restrictions as for kmap_atomic and friends apply. |
Randy Dunlap | efb4df82 | 2012-04-17 17:03:30 -0700 | [diff] [blame] | 579 | * @dmabuf: [in] buffer to map page from. |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 580 | * @page_num: [in] page in PAGE_SIZE units to map. |
| 581 | * |
| 582 | * This call must always succeed, any necessary preparations that might fail |
| 583 | * need to be done in begin_cpu_access. |
| 584 | */ |
| 585 | void *dma_buf_kmap_atomic(struct dma_buf *dmabuf, unsigned long page_num) |
| 586 | { |
| 587 | WARN_ON(!dmabuf); |
| 588 | |
| 589 | return dmabuf->ops->kmap_atomic(dmabuf, page_num); |
| 590 | } |
| 591 | EXPORT_SYMBOL_GPL(dma_buf_kmap_atomic); |
| 592 | |
| 593 | /** |
| 594 | * dma_buf_kunmap_atomic - Unmap a page obtained by dma_buf_kmap_atomic. |
Randy Dunlap | efb4df82 | 2012-04-17 17:03:30 -0700 | [diff] [blame] | 595 | * @dmabuf: [in] buffer to unmap page from. |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 596 | * @page_num: [in] page in PAGE_SIZE units to unmap. |
| 597 | * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap_atomic. |
| 598 | * |
| 599 | * This call must always succeed. |
| 600 | */ |
| 601 | void dma_buf_kunmap_atomic(struct dma_buf *dmabuf, unsigned long page_num, |
| 602 | void *vaddr) |
| 603 | { |
| 604 | WARN_ON(!dmabuf); |
| 605 | |
| 606 | if (dmabuf->ops->kunmap_atomic) |
| 607 | dmabuf->ops->kunmap_atomic(dmabuf, page_num, vaddr); |
| 608 | } |
| 609 | EXPORT_SYMBOL_GPL(dma_buf_kunmap_atomic); |
| 610 | |
| 611 | /** |
| 612 | * dma_buf_kmap - Map a page of the buffer object into kernel address space. The |
| 613 | * same restrictions as for kmap and friends apply. |
Randy Dunlap | efb4df82 | 2012-04-17 17:03:30 -0700 | [diff] [blame] | 614 | * @dmabuf: [in] buffer to map page from. |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 615 | * @page_num: [in] page in PAGE_SIZE units to map. |
| 616 | * |
| 617 | * This call must always succeed, any necessary preparations that might fail |
| 618 | * need to be done in begin_cpu_access. |
| 619 | */ |
| 620 | void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num) |
| 621 | { |
| 622 | WARN_ON(!dmabuf); |
| 623 | |
| 624 | return dmabuf->ops->kmap(dmabuf, page_num); |
| 625 | } |
| 626 | EXPORT_SYMBOL_GPL(dma_buf_kmap); |
| 627 | |
| 628 | /** |
| 629 | * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap. |
Randy Dunlap | efb4df82 | 2012-04-17 17:03:30 -0700 | [diff] [blame] | 630 | * @dmabuf: [in] buffer to unmap page from. |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 631 | * @page_num: [in] page in PAGE_SIZE units to unmap. |
| 632 | * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap. |
| 633 | * |
| 634 | * This call must always succeed. |
| 635 | */ |
| 636 | void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num, |
| 637 | void *vaddr) |
| 638 | { |
| 639 | WARN_ON(!dmabuf); |
| 640 | |
| 641 | if (dmabuf->ops->kunmap) |
| 642 | dmabuf->ops->kunmap(dmabuf, page_num, vaddr); |
| 643 | } |
| 644 | EXPORT_SYMBOL_GPL(dma_buf_kunmap); |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 645 | |
| 646 | |
| 647 | /** |
| 648 | * dma_buf_mmap - Setup up a userspace mmap with the given vma |
Sumit Semwal | 12c4727 | 2012-05-23 15:27:40 +0530 | [diff] [blame] | 649 | * @dmabuf: [in] buffer that should back the vma |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 650 | * @vma: [in] vma for the mmap |
| 651 | * @pgoff: [in] offset in pages where this mmap should start within the |
| 652 | * dma-buf buffer. |
| 653 | * |
| 654 | * 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] | 655 | * dma_buf operation. It also adjusts the starting pgoff and does bounds |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 656 | * checking on the size of the vma. Then it calls the exporters mmap function to |
| 657 | * set up the mapping. |
| 658 | * |
| 659 | * Can return negative error values, returns 0 on success. |
| 660 | */ |
| 661 | int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma, |
| 662 | unsigned long pgoff) |
| 663 | { |
John Sheu | 495c10c | 2013-02-11 17:50:24 -0800 | [diff] [blame] | 664 | struct file *oldfile; |
| 665 | int ret; |
| 666 | |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 667 | if (WARN_ON(!dmabuf || !vma)) |
| 668 | return -EINVAL; |
| 669 | |
| 670 | /* check for offset overflow */ |
| 671 | if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) < pgoff) |
| 672 | return -EOVERFLOW; |
| 673 | |
| 674 | /* check for overflowing the buffer's size */ |
| 675 | if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) > |
| 676 | dmabuf->size >> PAGE_SHIFT) |
| 677 | return -EINVAL; |
| 678 | |
| 679 | /* readjust the vma */ |
John Sheu | 495c10c | 2013-02-11 17:50:24 -0800 | [diff] [blame] | 680 | get_file(dmabuf->file); |
| 681 | oldfile = vma->vm_file; |
| 682 | vma->vm_file = dmabuf->file; |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 683 | vma->vm_pgoff = pgoff; |
| 684 | |
John Sheu | 495c10c | 2013-02-11 17:50:24 -0800 | [diff] [blame] | 685 | ret = dmabuf->ops->mmap(dmabuf, vma); |
| 686 | if (ret) { |
| 687 | /* restore old parameters on failure */ |
| 688 | vma->vm_file = oldfile; |
| 689 | fput(dmabuf->file); |
| 690 | } else { |
| 691 | if (oldfile) |
| 692 | fput(oldfile); |
| 693 | } |
| 694 | return ret; |
| 695 | |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 696 | } |
| 697 | EXPORT_SYMBOL_GPL(dma_buf_mmap); |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 698 | |
| 699 | /** |
Sumit Semwal | 12c4727 | 2012-05-23 15:27:40 +0530 | [diff] [blame] | 700 | * dma_buf_vmap - Create virtual mapping for the buffer object into kernel |
| 701 | * address space. Same restrictions as for vmap and friends apply. |
| 702 | * @dmabuf: [in] buffer to vmap |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 703 | * |
| 704 | * This call may fail due to lack of virtual mapping address space. |
| 705 | * These calls are optional in drivers. The intended use for them |
| 706 | * is for mapping objects linear in kernel space for high use objects. |
| 707 | * Please attempt to use kmap/kunmap before thinking about these interfaces. |
Colin Cross | fee0c54 | 2013-12-20 16:43:50 -0800 | [diff] [blame] | 708 | * |
| 709 | * Returns NULL on error. |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 710 | */ |
| 711 | void *dma_buf_vmap(struct dma_buf *dmabuf) |
| 712 | { |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 713 | void *ptr; |
| 714 | |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 715 | if (WARN_ON(!dmabuf)) |
| 716 | return NULL; |
| 717 | |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 718 | if (!dmabuf->ops->vmap) |
| 719 | return NULL; |
| 720 | |
| 721 | mutex_lock(&dmabuf->lock); |
| 722 | if (dmabuf->vmapping_counter) { |
| 723 | dmabuf->vmapping_counter++; |
| 724 | BUG_ON(!dmabuf->vmap_ptr); |
| 725 | ptr = dmabuf->vmap_ptr; |
| 726 | goto out_unlock; |
| 727 | } |
| 728 | |
| 729 | BUG_ON(dmabuf->vmap_ptr); |
| 730 | |
| 731 | ptr = dmabuf->ops->vmap(dmabuf); |
Colin Cross | fee0c54 | 2013-12-20 16:43:50 -0800 | [diff] [blame] | 732 | if (WARN_ON_ONCE(IS_ERR(ptr))) |
| 733 | ptr = NULL; |
| 734 | if (!ptr) |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 735 | goto out_unlock; |
| 736 | |
| 737 | dmabuf->vmap_ptr = ptr; |
| 738 | dmabuf->vmapping_counter = 1; |
| 739 | |
| 740 | out_unlock: |
| 741 | mutex_unlock(&dmabuf->lock); |
| 742 | return ptr; |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 743 | } |
| 744 | EXPORT_SYMBOL_GPL(dma_buf_vmap); |
| 745 | |
| 746 | /** |
| 747 | * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap. |
Sumit Semwal | 12c4727 | 2012-05-23 15:27:40 +0530 | [diff] [blame] | 748 | * @dmabuf: [in] buffer to vunmap |
Randy Dunlap | 6e7b4a5 | 2012-06-09 15:02:59 -0700 | [diff] [blame] | 749 | * @vaddr: [in] vmap to vunmap |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 750 | */ |
| 751 | void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr) |
| 752 | { |
| 753 | if (WARN_ON(!dmabuf)) |
| 754 | return; |
| 755 | |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 756 | BUG_ON(!dmabuf->vmap_ptr); |
| 757 | BUG_ON(dmabuf->vmapping_counter == 0); |
| 758 | BUG_ON(dmabuf->vmap_ptr != vaddr); |
| 759 | |
| 760 | mutex_lock(&dmabuf->lock); |
| 761 | if (--dmabuf->vmapping_counter == 0) { |
| 762 | if (dmabuf->ops->vunmap) |
| 763 | dmabuf->ops->vunmap(dmabuf, vaddr); |
| 764 | dmabuf->vmap_ptr = NULL; |
| 765 | } |
| 766 | mutex_unlock(&dmabuf->lock); |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 767 | } |
| 768 | EXPORT_SYMBOL_GPL(dma_buf_vunmap); |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 769 | |
| 770 | #ifdef CONFIG_DEBUG_FS |
| 771 | static int dma_buf_describe(struct seq_file *s) |
| 772 | { |
| 773 | int ret; |
| 774 | struct dma_buf *buf_obj; |
| 775 | struct dma_buf_attachment *attach_obj; |
| 776 | int count = 0, attach_count; |
| 777 | size_t size = 0; |
| 778 | |
| 779 | ret = mutex_lock_interruptible(&db_list.lock); |
| 780 | |
| 781 | if (ret) |
| 782 | return ret; |
| 783 | |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 784 | seq_puts(s, "\nDma-buf Objects:\n"); |
| 785 | seq_puts(s, "size\tflags\tmode\tcount\texp_name\n"); |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 786 | |
| 787 | list_for_each_entry(buf_obj, &db_list.head, list_node) { |
| 788 | ret = mutex_lock_interruptible(&buf_obj->lock); |
| 789 | |
| 790 | if (ret) { |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 791 | seq_puts(s, |
| 792 | "\tERROR locking buffer object: skipping\n"); |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 793 | continue; |
| 794 | } |
| 795 | |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 796 | seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\n", |
| 797 | buf_obj->size, |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 798 | buf_obj->file->f_flags, buf_obj->file->f_mode, |
Al Viro | a1f6dba | 2014-08-20 11:05:50 -0400 | [diff] [blame] | 799 | file_count(buf_obj->file), |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 800 | buf_obj->exp_name); |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 801 | |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 802 | seq_puts(s, "\tAttached Devices:\n"); |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 803 | attach_count = 0; |
| 804 | |
| 805 | list_for_each_entry(attach_obj, &buf_obj->attachments, node) { |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 806 | seq_puts(s, "\t"); |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 807 | |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 808 | seq_printf(s, "%s\n", dev_name(attach_obj->dev)); |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 809 | attach_count++; |
| 810 | } |
| 811 | |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 812 | seq_printf(s, "Total %d devices attached\n\n", |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 813 | attach_count); |
| 814 | |
| 815 | count++; |
| 816 | size += buf_obj->size; |
| 817 | mutex_unlock(&buf_obj->lock); |
| 818 | } |
| 819 | |
| 820 | seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size); |
| 821 | |
| 822 | mutex_unlock(&db_list.lock); |
| 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | static int dma_buf_show(struct seq_file *s, void *unused) |
| 827 | { |
| 828 | void (*func)(struct seq_file *) = s->private; |
| 829 | func(s); |
| 830 | return 0; |
| 831 | } |
| 832 | |
| 833 | static int dma_buf_debug_open(struct inode *inode, struct file *file) |
| 834 | { |
| 835 | return single_open(file, dma_buf_show, inode->i_private); |
| 836 | } |
| 837 | |
| 838 | static const struct file_operations dma_buf_debug_fops = { |
| 839 | .open = dma_buf_debug_open, |
| 840 | .read = seq_read, |
| 841 | .llseek = seq_lseek, |
| 842 | .release = single_release, |
| 843 | }; |
| 844 | |
| 845 | static struct dentry *dma_buf_debugfs_dir; |
| 846 | |
| 847 | static int dma_buf_init_debugfs(void) |
| 848 | { |
| 849 | int err = 0; |
| 850 | dma_buf_debugfs_dir = debugfs_create_dir("dma_buf", NULL); |
| 851 | if (IS_ERR(dma_buf_debugfs_dir)) { |
| 852 | err = PTR_ERR(dma_buf_debugfs_dir); |
| 853 | dma_buf_debugfs_dir = NULL; |
| 854 | return err; |
| 855 | } |
| 856 | |
| 857 | err = dma_buf_debugfs_create_file("bufinfo", dma_buf_describe); |
| 858 | |
| 859 | if (err) |
| 860 | pr_debug("dma_buf: debugfs: failed to create node bufinfo\n"); |
| 861 | |
| 862 | return err; |
| 863 | } |
| 864 | |
| 865 | static void dma_buf_uninit_debugfs(void) |
| 866 | { |
| 867 | if (dma_buf_debugfs_dir) |
| 868 | debugfs_remove_recursive(dma_buf_debugfs_dir); |
| 869 | } |
| 870 | |
| 871 | int dma_buf_debugfs_create_file(const char *name, |
| 872 | int (*write)(struct seq_file *)) |
| 873 | { |
| 874 | struct dentry *d; |
| 875 | |
| 876 | d = debugfs_create_file(name, S_IRUGO, dma_buf_debugfs_dir, |
| 877 | write, &dma_buf_debug_fops); |
| 878 | |
Sachin Kamat | 28ce420 | 2013-07-15 15:51:22 +0530 | [diff] [blame] | 879 | return PTR_ERR_OR_ZERO(d); |
Sumit Semwal | b89e356 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 880 | } |
| 881 | #else |
| 882 | static inline int dma_buf_init_debugfs(void) |
| 883 | { |
| 884 | return 0; |
| 885 | } |
| 886 | static inline void dma_buf_uninit_debugfs(void) |
| 887 | { |
| 888 | } |
| 889 | #endif |
| 890 | |
| 891 | static int __init dma_buf_init(void) |
| 892 | { |
| 893 | mutex_init(&db_list.lock); |
| 894 | INIT_LIST_HEAD(&db_list.head); |
| 895 | dma_buf_init_debugfs(); |
| 896 | return 0; |
| 897 | } |
| 898 | subsys_initcall(dma_buf_init); |
| 899 | |
| 900 | static void __exit dma_buf_deinit(void) |
| 901 | { |
| 902 | dma_buf_uninit_debugfs(); |
| 903 | } |
| 904 | __exitcall(dma_buf_deinit); |