2 * Framework for buffer objects that can be shared across devices/subsystems.
4 * Copyright(C) 2011 Linaro Limited. All rights reserved.
5 * Author: Sumit Semwal <sumit.semwal@ti.com>
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.
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.
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
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/>.
26 #include <linux/slab.h>
27 #include <linux/dma-buf.h>
28 #include <linux/anon_inodes.h>
29 #include <linux/export.h>
30 #include <linux/debugfs.h>
31 #include <linux/seq_file.h>
33 static inline int is_dma_buf_file(struct file *);
36 struct list_head head;
40 static struct dma_buf_list db_list;
42 static int dma_buf_release(struct inode *inode, struct file *file)
44 struct dma_buf *dmabuf;
46 if (!is_dma_buf_file(file))
49 dmabuf = file->private_data;
51 BUG_ON(dmabuf->vmapping_counter);
53 dmabuf->ops->release(dmabuf);
55 mutex_lock(&db_list.lock);
56 list_del(&dmabuf->list_node);
57 mutex_unlock(&db_list.lock);
63 static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
65 struct dma_buf *dmabuf;
67 if (!is_dma_buf_file(file))
70 dmabuf = file->private_data;
72 /* check for overflowing the buffer's size */
73 if (vma->vm_pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
74 dmabuf->size >> PAGE_SHIFT)
77 return dmabuf->ops->mmap(dmabuf, vma);
80 static const struct file_operations dma_buf_fops = {
81 .release = dma_buf_release,
82 .mmap = dma_buf_mmap_internal,
86 * is_dma_buf_file - Check if struct file* is associated with dma_buf
88 static inline int is_dma_buf_file(struct file *file)
90 return file->f_op == &dma_buf_fops;
94 * dma_buf_export_named - Creates a new dma_buf, and associates an anon file
95 * with this buffer, so it can be exported.
96 * Also connect the allocator specific data and ops to the buffer.
97 * Additionally, provide a name string for exporter; useful in debugging.
99 * @priv: [in] Attach private data of allocator to this buffer
100 * @ops: [in] Attach allocator-defined dma buf ops to the new buffer.
101 * @size: [in] Size of the buffer
102 * @flags: [in] mode flags for the file.
103 * @exp_name: [in] name of the exporting module - useful for debugging.
105 * Returns, on success, a newly created dma_buf object, which wraps the
106 * supplied private data and operations for dma_buf_ops. On either missing
107 * ops, or error in allocating struct dma_buf, will return negative error.
110 struct dma_buf *dma_buf_export_named(void *priv, const struct dma_buf_ops *ops,
111 size_t size, int flags, const char *exp_name)
113 struct dma_buf *dmabuf;
116 if (WARN_ON(!priv || !ops
118 || !ops->unmap_dma_buf
123 return ERR_PTR(-EINVAL);
126 dmabuf = kzalloc(sizeof(struct dma_buf), GFP_KERNEL);
128 return ERR_PTR(-ENOMEM);
133 dmabuf->exp_name = exp_name;
135 file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf, flags);
138 return ERR_CAST(file);
142 mutex_init(&dmabuf->lock);
143 INIT_LIST_HEAD(&dmabuf->attachments);
145 mutex_lock(&db_list.lock);
146 list_add(&dmabuf->list_node, &db_list.head);
147 mutex_unlock(&db_list.lock);
151 EXPORT_SYMBOL_GPL(dma_buf_export_named);
155 * dma_buf_fd - returns a file descriptor for the given dma_buf
156 * @dmabuf: [in] pointer to dma_buf for which fd is required.
157 * @flags: [in] flags to give to fd
159 * On success, returns an associated 'fd'. Else, returns error.
161 int dma_buf_fd(struct dma_buf *dmabuf, int flags)
165 if (!dmabuf || !dmabuf->file)
168 fd = get_unused_fd_flags(flags);
172 fd_install(fd, dmabuf->file);
176 EXPORT_SYMBOL_GPL(dma_buf_fd);
179 * dma_buf_get - returns the dma_buf structure related to an fd
180 * @fd: [in] fd associated with the dma_buf to be returned
182 * On success, returns the dma_buf structure associated with an fd; uses
183 * file's refcounting done by fget to increase refcount. returns ERR_PTR
186 struct dma_buf *dma_buf_get(int fd)
193 return ERR_PTR(-EBADF);
195 if (!is_dma_buf_file(file)) {
197 return ERR_PTR(-EINVAL);
200 return file->private_data;
202 EXPORT_SYMBOL_GPL(dma_buf_get);
205 * dma_buf_put - decreases refcount of the buffer
206 * @dmabuf: [in] buffer to reduce refcount of
208 * Uses file's refcounting done implicitly by fput()
210 void dma_buf_put(struct dma_buf *dmabuf)
212 if (WARN_ON(!dmabuf || !dmabuf->file))
217 EXPORT_SYMBOL_GPL(dma_buf_put);
220 * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
221 * calls attach() of dma_buf_ops to allow device-specific attach functionality
222 * @dmabuf: [in] buffer to attach device to.
223 * @dev: [in] device to be attached.
225 * Returns struct dma_buf_attachment * for this attachment; may return negative
229 struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
232 struct dma_buf_attachment *attach;
235 if (WARN_ON(!dmabuf || !dev))
236 return ERR_PTR(-EINVAL);
238 attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL);
240 return ERR_PTR(-ENOMEM);
243 attach->dmabuf = dmabuf;
245 mutex_lock(&dmabuf->lock);
247 if (dmabuf->ops->attach) {
248 ret = dmabuf->ops->attach(dmabuf, dev, attach);
252 list_add(&attach->node, &dmabuf->attachments);
254 mutex_unlock(&dmabuf->lock);
259 mutex_unlock(&dmabuf->lock);
262 EXPORT_SYMBOL_GPL(dma_buf_attach);
265 * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
266 * optionally calls detach() of dma_buf_ops for device-specific detach
267 * @dmabuf: [in] buffer to detach from.
268 * @attach: [in] attachment to be detached; is free'd after this call.
271 void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
273 if (WARN_ON(!dmabuf || !attach))
276 mutex_lock(&dmabuf->lock);
277 list_del(&attach->node);
278 if (dmabuf->ops->detach)
279 dmabuf->ops->detach(dmabuf, attach);
281 mutex_unlock(&dmabuf->lock);
284 EXPORT_SYMBOL_GPL(dma_buf_detach);
287 * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
288 * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
290 * @attach: [in] attachment whose scatterlist is to be returned
291 * @direction: [in] direction of DMA transfer
293 * Returns sg_table containing the scatterlist to be returned; may return NULL
297 struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
298 enum dma_data_direction direction)
300 struct sg_table *sg_table = ERR_PTR(-EINVAL);
304 if (WARN_ON(!attach || !attach->dmabuf))
305 return ERR_PTR(-EINVAL);
307 sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
311 EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
314 * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
315 * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
317 * @attach: [in] attachment to unmap buffer from
318 * @sg_table: [in] scatterlist info of the buffer to unmap
319 * @direction: [in] direction of DMA transfer
322 void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
323 struct sg_table *sg_table,
324 enum dma_data_direction direction)
328 if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
331 attach->dmabuf->ops->unmap_dma_buf(attach, sg_table,
334 EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
338 * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
339 * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
340 * preparations. Coherency is only guaranteed in the specified range for the
341 * specified access direction.
342 * @dmabuf: [in] buffer to prepare cpu access for.
343 * @start: [in] start of range for cpu access.
344 * @len: [in] length of range for cpu access.
345 * @direction: [in] length of range for cpu access.
347 * Can return negative error values, returns 0 on success.
349 int dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
350 enum dma_data_direction direction)
354 if (WARN_ON(!dmabuf))
357 if (dmabuf->ops->begin_cpu_access)
358 ret = dmabuf->ops->begin_cpu_access(dmabuf, start, len, direction);
362 EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access);
365 * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
366 * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
367 * actions. Coherency is only guaranteed in the specified range for the
368 * specified access direction.
369 * @dmabuf: [in] buffer to complete cpu access for.
370 * @start: [in] start of range for cpu access.
371 * @len: [in] length of range for cpu access.
372 * @direction: [in] length of range for cpu access.
374 * This call must always succeed.
376 void dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
377 enum dma_data_direction direction)
381 if (dmabuf->ops->end_cpu_access)
382 dmabuf->ops->end_cpu_access(dmabuf, start, len, direction);
384 EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access);
387 * dma_buf_kmap_atomic - Map a page of the buffer object into kernel address
388 * space. The same restrictions as for kmap_atomic and friends apply.
389 * @dmabuf: [in] buffer to map page from.
390 * @page_num: [in] page in PAGE_SIZE units to map.
392 * This call must always succeed, any necessary preparations that might fail
393 * need to be done in begin_cpu_access.
395 void *dma_buf_kmap_atomic(struct dma_buf *dmabuf, unsigned long page_num)
399 return dmabuf->ops->kmap_atomic(dmabuf, page_num);
401 EXPORT_SYMBOL_GPL(dma_buf_kmap_atomic);
404 * dma_buf_kunmap_atomic - Unmap a page obtained by dma_buf_kmap_atomic.
405 * @dmabuf: [in] buffer to unmap page from.
406 * @page_num: [in] page in PAGE_SIZE units to unmap.
407 * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap_atomic.
409 * This call must always succeed.
411 void dma_buf_kunmap_atomic(struct dma_buf *dmabuf, unsigned long page_num,
416 if (dmabuf->ops->kunmap_atomic)
417 dmabuf->ops->kunmap_atomic(dmabuf, page_num, vaddr);
419 EXPORT_SYMBOL_GPL(dma_buf_kunmap_atomic);
422 * dma_buf_kmap - Map a page of the buffer object into kernel address space. The
423 * same restrictions as for kmap and friends apply.
424 * @dmabuf: [in] buffer to map page from.
425 * @page_num: [in] page in PAGE_SIZE units to map.
427 * This call must always succeed, any necessary preparations that might fail
428 * need to be done in begin_cpu_access.
430 void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num)
434 return dmabuf->ops->kmap(dmabuf, page_num);
436 EXPORT_SYMBOL_GPL(dma_buf_kmap);
439 * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap.
440 * @dmabuf: [in] buffer to unmap page from.
441 * @page_num: [in] page in PAGE_SIZE units to unmap.
442 * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap.
444 * This call must always succeed.
446 void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num,
451 if (dmabuf->ops->kunmap)
452 dmabuf->ops->kunmap(dmabuf, page_num, vaddr);
454 EXPORT_SYMBOL_GPL(dma_buf_kunmap);
458 * dma_buf_mmap - Setup up a userspace mmap with the given vma
459 * @dmabuf: [in] buffer that should back the vma
460 * @vma: [in] vma for the mmap
461 * @pgoff: [in] offset in pages where this mmap should start within the
464 * This function adjusts the passed in vma so that it points at the file of the
465 * dma_buf operation. It alsog adjusts the starting pgoff and does bounds
466 * checking on the size of the vma. Then it calls the exporters mmap function to
467 * set up the mapping.
469 * Can return negative error values, returns 0 on success.
471 int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
474 struct file *oldfile;
477 if (WARN_ON(!dmabuf || !vma))
480 /* check for offset overflow */
481 if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) < pgoff)
484 /* check for overflowing the buffer's size */
485 if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
486 dmabuf->size >> PAGE_SHIFT)
489 /* readjust the vma */
490 get_file(dmabuf->file);
491 oldfile = vma->vm_file;
492 vma->vm_file = dmabuf->file;
493 vma->vm_pgoff = pgoff;
495 ret = dmabuf->ops->mmap(dmabuf, vma);
497 /* restore old parameters on failure */
498 vma->vm_file = oldfile;
507 EXPORT_SYMBOL_GPL(dma_buf_mmap);
510 * dma_buf_vmap - Create virtual mapping for the buffer object into kernel
511 * address space. Same restrictions as for vmap and friends apply.
512 * @dmabuf: [in] buffer to vmap
514 * This call may fail due to lack of virtual mapping address space.
515 * These calls are optional in drivers. The intended use for them
516 * is for mapping objects linear in kernel space for high use objects.
517 * Please attempt to use kmap/kunmap before thinking about these interfaces.
519 void *dma_buf_vmap(struct dma_buf *dmabuf)
523 if (WARN_ON(!dmabuf))
526 if (!dmabuf->ops->vmap)
529 mutex_lock(&dmabuf->lock);
530 if (dmabuf->vmapping_counter) {
531 dmabuf->vmapping_counter++;
532 BUG_ON(!dmabuf->vmap_ptr);
533 ptr = dmabuf->vmap_ptr;
537 BUG_ON(dmabuf->vmap_ptr);
539 ptr = dmabuf->ops->vmap(dmabuf);
540 if (IS_ERR_OR_NULL(ptr))
543 dmabuf->vmap_ptr = ptr;
544 dmabuf->vmapping_counter = 1;
547 mutex_unlock(&dmabuf->lock);
550 EXPORT_SYMBOL_GPL(dma_buf_vmap);
553 * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
554 * @dmabuf: [in] buffer to vunmap
555 * @vaddr: [in] vmap to vunmap
557 void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
559 if (WARN_ON(!dmabuf))
562 BUG_ON(!dmabuf->vmap_ptr);
563 BUG_ON(dmabuf->vmapping_counter == 0);
564 BUG_ON(dmabuf->vmap_ptr != vaddr);
566 mutex_lock(&dmabuf->lock);
567 if (--dmabuf->vmapping_counter == 0) {
568 if (dmabuf->ops->vunmap)
569 dmabuf->ops->vunmap(dmabuf, vaddr);
570 dmabuf->vmap_ptr = NULL;
572 mutex_unlock(&dmabuf->lock);
574 EXPORT_SYMBOL_GPL(dma_buf_vunmap);
576 #ifdef CONFIG_DEBUG_FS
577 static int dma_buf_describe(struct seq_file *s)
580 struct dma_buf *buf_obj;
581 struct dma_buf_attachment *attach_obj;
582 int count = 0, attach_count;
585 ret = mutex_lock_interruptible(&db_list.lock);
590 seq_printf(s, "\nDma-buf Objects:\n");
591 seq_printf(s, "\texp_name\tsize\tflags\tmode\tcount\n");
593 list_for_each_entry(buf_obj, &db_list.head, list_node) {
594 ret = mutex_lock_interruptible(&buf_obj->lock);
598 "\tERROR locking buffer object: skipping\n");
604 seq_printf(s, "\t%s\t%08zu\t%08x\t%08x\t%08ld\n",
605 buf_obj->exp_name, buf_obj->size,
606 buf_obj->file->f_flags, buf_obj->file->f_mode,
607 (long)(buf_obj->file->f_count.counter));
609 seq_printf(s, "\t\tAttached Devices:\n");
612 list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
613 seq_printf(s, "\t\t");
615 seq_printf(s, "%s\n", attach_obj->dev->init_name);
619 seq_printf(s, "\n\t\tTotal %d devices attached\n",
623 size += buf_obj->size;
624 mutex_unlock(&buf_obj->lock);
627 seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size);
629 mutex_unlock(&db_list.lock);
633 static int dma_buf_show(struct seq_file *s, void *unused)
635 void (*func)(struct seq_file *) = s->private;
640 static int dma_buf_debug_open(struct inode *inode, struct file *file)
642 return single_open(file, dma_buf_show, inode->i_private);
645 static const struct file_operations dma_buf_debug_fops = {
646 .open = dma_buf_debug_open,
649 .release = single_release,
652 static struct dentry *dma_buf_debugfs_dir;
654 static int dma_buf_init_debugfs(void)
657 dma_buf_debugfs_dir = debugfs_create_dir("dma_buf", NULL);
658 if (IS_ERR(dma_buf_debugfs_dir)) {
659 err = PTR_ERR(dma_buf_debugfs_dir);
660 dma_buf_debugfs_dir = NULL;
664 err = dma_buf_debugfs_create_file("bufinfo", dma_buf_describe);
667 pr_debug("dma_buf: debugfs: failed to create node bufinfo\n");
672 static void dma_buf_uninit_debugfs(void)
674 if (dma_buf_debugfs_dir)
675 debugfs_remove_recursive(dma_buf_debugfs_dir);
678 int dma_buf_debugfs_create_file(const char *name,
679 int (*write)(struct seq_file *))
683 d = debugfs_create_file(name, S_IRUGO, dma_buf_debugfs_dir,
684 write, &dma_buf_debug_fops);
686 return PTR_ERR_OR_ZERO(d);
689 static inline int dma_buf_init_debugfs(void)
693 static inline void dma_buf_uninit_debugfs(void)
698 static int __init dma_buf_init(void)
700 mutex_init(&db_list.lock);
701 INIT_LIST_HEAD(&db_list.head);
702 dma_buf_init_debugfs();
705 subsys_initcall(dma_buf_init);
707 static void __exit dma_buf_deinit(void)
709 dma_buf_uninit_debugfs();
711 __exitcall(dma_buf_deinit);