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