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