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/fence.h>
29 #include <linux/anon_inodes.h>
30 #include <linux/export.h>
31 #include <linux/debugfs.h>
32 #include <linux/seq_file.h>
33 #include <linux/poll.h>
34 #include <linux/reservation.h>
36 static inline int is_dma_buf_file(struct file
*);
39 struct list_head head
;
43 static struct dma_buf_list db_list
;
45 static int dma_buf_release(struct inode
*inode
, struct file
*file
)
47 struct dma_buf
*dmabuf
;
49 if (!is_dma_buf_file(file
))
52 dmabuf
= file
->private_data
;
54 BUG_ON(dmabuf
->vmapping_counter
);
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.
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.
64 BUG_ON(dmabuf
->cb_shared
.active
|| dmabuf
->cb_excl
.active
);
66 dmabuf
->ops
->release(dmabuf
);
68 mutex_lock(&db_list
.lock
);
69 list_del(&dmabuf
->list_node
);
70 mutex_unlock(&db_list
.lock
);
72 if (dmabuf
->resv
== (struct reservation_object
*)&dmabuf
[1])
73 reservation_object_fini(dmabuf
->resv
);
79 static int dma_buf_mmap_internal(struct file
*file
, struct vm_area_struct
*vma
)
81 struct dma_buf
*dmabuf
;
83 if (!is_dma_buf_file(file
))
86 dmabuf
= file
->private_data
;
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
)
93 return dmabuf
->ops
->mmap(dmabuf
, vma
);
96 static loff_t
dma_buf_llseek(struct file
*file
, loff_t offset
, int whence
)
98 struct dma_buf
*dmabuf
;
101 if (!is_dma_buf_file(file
))
104 dmabuf
= file
->private_data
;
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
)
111 else if (whence
== SEEK_SET
)
119 return base
+ offset
;
122 static void dma_buf_poll_cb(struct fence
*fence
, struct fence_cb
*cb
)
124 struct dma_buf_poll_cb_t
*dcb
= (struct dma_buf_poll_cb_t
*)cb
;
127 spin_lock_irqsave(&dcb
->poll
->lock
, flags
);
128 wake_up_locked_poll(dcb
->poll
, dcb
->active
);
130 spin_unlock_irqrestore(&dcb
->poll
->lock
, flags
);
133 static unsigned int dma_buf_poll(struct file
*file
, poll_table
*poll
)
135 struct dma_buf
*dmabuf
;
136 struct reservation_object
*resv
;
137 struct reservation_object_list
*fobj
;
138 struct fence
*fence_excl
;
139 unsigned long events
;
140 unsigned shared_count
, seq
;
142 dmabuf
= file
->private_data
;
143 if (!dmabuf
|| !dmabuf
->resv
)
148 poll_wait(file
, &dmabuf
->poll
, poll
);
150 events
= poll_requested_events(poll
) & (POLLIN
| POLLOUT
);
155 seq
= read_seqcount_begin(&resv
->seq
);
158 fobj
= rcu_dereference(resv
->fence
);
160 shared_count
= fobj
->shared_count
;
163 fence_excl
= rcu_dereference(resv
->fence_excl
);
164 if (read_seqcount_retry(&resv
->seq
, seq
)) {
169 if (fence_excl
&& (!(events
& POLLOUT
) || shared_count
== 0)) {
170 struct dma_buf_poll_cb_t
*dcb
= &dmabuf
->cb_excl
;
171 unsigned long pevents
= POLLIN
;
173 if (shared_count
== 0)
176 spin_lock_irq(&dmabuf
->poll
.lock
);
178 dcb
->active
|= pevents
;
181 dcb
->active
= pevents
;
182 spin_unlock_irq(&dmabuf
->poll
.lock
);
184 if (events
& pevents
) {
185 if (!fence_get_rcu(fence_excl
)) {
186 /* force a recheck */
188 dma_buf_poll_cb(NULL
, &dcb
->cb
);
189 } else if (!fence_add_callback(fence_excl
, &dcb
->cb
,
192 fence_put(fence_excl
);
195 * No callback queued, wake up any additional
198 fence_put(fence_excl
);
199 dma_buf_poll_cb(NULL
, &dcb
->cb
);
204 if ((events
& POLLOUT
) && shared_count
> 0) {
205 struct dma_buf_poll_cb_t
*dcb
= &dmabuf
->cb_shared
;
208 /* Only queue a new callback if no event has fired yet */
209 spin_lock_irq(&dmabuf
->poll
.lock
);
213 dcb
->active
= POLLOUT
;
214 spin_unlock_irq(&dmabuf
->poll
.lock
);
216 if (!(events
& POLLOUT
))
219 for (i
= 0; i
< shared_count
; ++i
) {
220 struct fence
*fence
= rcu_dereference(fobj
->shared
[i
]);
222 if (!fence_get_rcu(fence
)) {
224 * fence refcount dropped to zero, this means
225 * that fobj has been freed
227 * call dma_buf_poll_cb and force a recheck!
230 dma_buf_poll_cb(NULL
, &dcb
->cb
);
233 if (!fence_add_callback(fence
, &dcb
->cb
,
242 /* No callback queued, wake up any additional waiters. */
243 if (i
== shared_count
)
244 dma_buf_poll_cb(NULL
, &dcb
->cb
);
252 static const struct file_operations dma_buf_fops
= {
253 .release
= dma_buf_release
,
254 .mmap
= dma_buf_mmap_internal
,
255 .llseek
= dma_buf_llseek
,
256 .poll
= dma_buf_poll
,
260 * is_dma_buf_file - Check if struct file* is associated with dma_buf
262 static inline int is_dma_buf_file(struct file
*file
)
264 return file
->f_op
== &dma_buf_fops
;
268 * dma_buf_export_named - Creates a new dma_buf, and associates an anon file
269 * with this buffer, so it can be exported.
270 * Also connect the allocator specific data and ops to the buffer.
271 * Additionally, provide a name string for exporter; useful in debugging.
273 * @priv: [in] Attach private data of allocator to this buffer
274 * @ops: [in] Attach allocator-defined dma buf ops to the new buffer.
275 * @size: [in] Size of the buffer
276 * @flags: [in] mode flags for the file.
277 * @exp_name: [in] name of the exporting module - useful for debugging.
278 * @resv: [in] reservation-object, NULL to allocate default one.
280 * Returns, on success, a newly created dma_buf object, which wraps the
281 * supplied private data and operations for dma_buf_ops. On either missing
282 * ops, or error in allocating struct dma_buf, will return negative error.
285 struct dma_buf
*dma_buf_export_named(void *priv
, const struct dma_buf_ops
*ops
,
286 size_t size
, int flags
, const char *exp_name
,
287 struct reservation_object
*resv
)
289 struct dma_buf
*dmabuf
;
291 size_t alloc_size
= sizeof(struct dma_buf
);
293 alloc_size
+= sizeof(struct reservation_object
);
295 /* prevent &dma_buf[1] == dma_buf->resv */
298 if (WARN_ON(!priv
|| !ops
300 || !ops
->unmap_dma_buf
305 return ERR_PTR(-EINVAL
);
308 dmabuf
= kzalloc(alloc_size
, GFP_KERNEL
);
310 return ERR_PTR(-ENOMEM
);
315 dmabuf
->exp_name
= exp_name
;
316 init_waitqueue_head(&dmabuf
->poll
);
317 dmabuf
->cb_excl
.poll
= dmabuf
->cb_shared
.poll
= &dmabuf
->poll
;
318 dmabuf
->cb_excl
.active
= dmabuf
->cb_shared
.active
= 0;
321 resv
= (struct reservation_object
*)&dmabuf
[1];
322 reservation_object_init(resv
);
326 file
= anon_inode_getfile("dmabuf", &dma_buf_fops
, dmabuf
, flags
);
329 return ERR_CAST(file
);
332 file
->f_mode
|= FMODE_LSEEK
;
335 mutex_init(&dmabuf
->lock
);
336 INIT_LIST_HEAD(&dmabuf
->attachments
);
338 mutex_lock(&db_list
.lock
);
339 list_add(&dmabuf
->list_node
, &db_list
.head
);
340 mutex_unlock(&db_list
.lock
);
344 EXPORT_SYMBOL_GPL(dma_buf_export_named
);
348 * dma_buf_fd - returns a file descriptor for the given dma_buf
349 * @dmabuf: [in] pointer to dma_buf for which fd is required.
350 * @flags: [in] flags to give to fd
352 * On success, returns an associated 'fd'. Else, returns error.
354 int dma_buf_fd(struct dma_buf
*dmabuf
, int flags
)
358 if (!dmabuf
|| !dmabuf
->file
)
361 fd
= get_unused_fd_flags(flags
);
365 fd_install(fd
, dmabuf
->file
);
369 EXPORT_SYMBOL_GPL(dma_buf_fd
);
372 * dma_buf_get - returns the dma_buf structure related to an fd
373 * @fd: [in] fd associated with the dma_buf to be returned
375 * On success, returns the dma_buf structure associated with an fd; uses
376 * file's refcounting done by fget to increase refcount. returns ERR_PTR
379 struct dma_buf
*dma_buf_get(int fd
)
386 return ERR_PTR(-EBADF
);
388 if (!is_dma_buf_file(file
)) {
390 return ERR_PTR(-EINVAL
);
393 return file
->private_data
;
395 EXPORT_SYMBOL_GPL(dma_buf_get
);
398 * dma_buf_put - decreases refcount of the buffer
399 * @dmabuf: [in] buffer to reduce refcount of
401 * Uses file's refcounting done implicitly by fput()
403 void dma_buf_put(struct dma_buf
*dmabuf
)
405 if (WARN_ON(!dmabuf
|| !dmabuf
->file
))
410 EXPORT_SYMBOL_GPL(dma_buf_put
);
413 * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
414 * calls attach() of dma_buf_ops to allow device-specific attach functionality
415 * @dmabuf: [in] buffer to attach device to.
416 * @dev: [in] device to be attached.
418 * Returns struct dma_buf_attachment * for this attachment; returns ERR_PTR on
421 struct dma_buf_attachment
*dma_buf_attach(struct dma_buf
*dmabuf
,
424 struct dma_buf_attachment
*attach
;
427 if (WARN_ON(!dmabuf
|| !dev
))
428 return ERR_PTR(-EINVAL
);
430 attach
= kzalloc(sizeof(struct dma_buf_attachment
), GFP_KERNEL
);
432 return ERR_PTR(-ENOMEM
);
435 attach
->dmabuf
= dmabuf
;
437 mutex_lock(&dmabuf
->lock
);
439 if (dmabuf
->ops
->attach
) {
440 ret
= dmabuf
->ops
->attach(dmabuf
, dev
, attach
);
444 list_add(&attach
->node
, &dmabuf
->attachments
);
446 mutex_unlock(&dmabuf
->lock
);
451 mutex_unlock(&dmabuf
->lock
);
454 EXPORT_SYMBOL_GPL(dma_buf_attach
);
457 * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
458 * optionally calls detach() of dma_buf_ops for device-specific detach
459 * @dmabuf: [in] buffer to detach from.
460 * @attach: [in] attachment to be detached; is free'd after this call.
463 void dma_buf_detach(struct dma_buf
*dmabuf
, struct dma_buf_attachment
*attach
)
465 if (WARN_ON(!dmabuf
|| !attach
))
468 mutex_lock(&dmabuf
->lock
);
469 list_del(&attach
->node
);
470 if (dmabuf
->ops
->detach
)
471 dmabuf
->ops
->detach(dmabuf
, attach
);
473 mutex_unlock(&dmabuf
->lock
);
476 EXPORT_SYMBOL_GPL(dma_buf_detach
);
479 * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
480 * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
482 * @attach: [in] attachment whose scatterlist is to be returned
483 * @direction: [in] direction of DMA transfer
485 * Returns sg_table containing the scatterlist to be returned; returns ERR_PTR
488 struct sg_table
*dma_buf_map_attachment(struct dma_buf_attachment
*attach
,
489 enum dma_data_direction direction
)
491 struct sg_table
*sg_table
= ERR_PTR(-EINVAL
);
495 if (WARN_ON(!attach
|| !attach
->dmabuf
))
496 return ERR_PTR(-EINVAL
);
498 sg_table
= attach
->dmabuf
->ops
->map_dma_buf(attach
, direction
);
500 sg_table
= ERR_PTR(-ENOMEM
);
504 EXPORT_SYMBOL_GPL(dma_buf_map_attachment
);
507 * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
508 * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
510 * @attach: [in] attachment to unmap buffer from
511 * @sg_table: [in] scatterlist info of the buffer to unmap
512 * @direction: [in] direction of DMA transfer
515 void dma_buf_unmap_attachment(struct dma_buf_attachment
*attach
,
516 struct sg_table
*sg_table
,
517 enum dma_data_direction direction
)
521 if (WARN_ON(!attach
|| !attach
->dmabuf
|| !sg_table
))
524 attach
->dmabuf
->ops
->unmap_dma_buf(attach
, sg_table
,
527 EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment
);
531 * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
532 * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
533 * preparations. Coherency is only guaranteed in the specified range for the
534 * specified access direction.
535 * @dmabuf: [in] buffer to prepare cpu access for.
536 * @start: [in] start of range for cpu access.
537 * @len: [in] length of range for cpu access.
538 * @direction: [in] length of range for cpu access.
540 * Can return negative error values, returns 0 on success.
542 int dma_buf_begin_cpu_access(struct dma_buf
*dmabuf
, size_t start
, size_t len
,
543 enum dma_data_direction direction
)
547 if (WARN_ON(!dmabuf
))
550 if (dmabuf
->ops
->begin_cpu_access
)
551 ret
= dmabuf
->ops
->begin_cpu_access(dmabuf
, start
, len
, direction
);
555 EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access
);
558 * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
559 * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
560 * actions. Coherency is only guaranteed in the specified range for the
561 * specified access direction.
562 * @dmabuf: [in] buffer to complete cpu access for.
563 * @start: [in] start of range for cpu access.
564 * @len: [in] length of range for cpu access.
565 * @direction: [in] length of range for cpu access.
567 * This call must always succeed.
569 void dma_buf_end_cpu_access(struct dma_buf
*dmabuf
, size_t start
, size_t len
,
570 enum dma_data_direction direction
)
574 if (dmabuf
->ops
->end_cpu_access
)
575 dmabuf
->ops
->end_cpu_access(dmabuf
, start
, len
, direction
);
577 EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access
);
580 * dma_buf_kmap_atomic - Map a page of the buffer object into kernel address
581 * space. The same restrictions as for kmap_atomic and friends apply.
582 * @dmabuf: [in] buffer to map page from.
583 * @page_num: [in] page in PAGE_SIZE units to map.
585 * This call must always succeed, any necessary preparations that might fail
586 * need to be done in begin_cpu_access.
588 void *dma_buf_kmap_atomic(struct dma_buf
*dmabuf
, unsigned long page_num
)
592 return dmabuf
->ops
->kmap_atomic(dmabuf
, page_num
);
594 EXPORT_SYMBOL_GPL(dma_buf_kmap_atomic
);
597 * dma_buf_kunmap_atomic - Unmap a page obtained by dma_buf_kmap_atomic.
598 * @dmabuf: [in] buffer to unmap page from.
599 * @page_num: [in] page in PAGE_SIZE units to unmap.
600 * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap_atomic.
602 * This call must always succeed.
604 void dma_buf_kunmap_atomic(struct dma_buf
*dmabuf
, unsigned long page_num
,
609 if (dmabuf
->ops
->kunmap_atomic
)
610 dmabuf
->ops
->kunmap_atomic(dmabuf
, page_num
, vaddr
);
612 EXPORT_SYMBOL_GPL(dma_buf_kunmap_atomic
);
615 * dma_buf_kmap - Map a page of the buffer object into kernel address space. The
616 * same restrictions as for kmap and friends apply.
617 * @dmabuf: [in] buffer to map page from.
618 * @page_num: [in] page in PAGE_SIZE units to map.
620 * This call must always succeed, any necessary preparations that might fail
621 * need to be done in begin_cpu_access.
623 void *dma_buf_kmap(struct dma_buf
*dmabuf
, unsigned long page_num
)
627 return dmabuf
->ops
->kmap(dmabuf
, page_num
);
629 EXPORT_SYMBOL_GPL(dma_buf_kmap
);
632 * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap.
633 * @dmabuf: [in] buffer to unmap page from.
634 * @page_num: [in] page in PAGE_SIZE units to unmap.
635 * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap.
637 * This call must always succeed.
639 void dma_buf_kunmap(struct dma_buf
*dmabuf
, unsigned long page_num
,
644 if (dmabuf
->ops
->kunmap
)
645 dmabuf
->ops
->kunmap(dmabuf
, page_num
, vaddr
);
647 EXPORT_SYMBOL_GPL(dma_buf_kunmap
);
651 * dma_buf_mmap - Setup up a userspace mmap with the given vma
652 * @dmabuf: [in] buffer that should back the vma
653 * @vma: [in] vma for the mmap
654 * @pgoff: [in] offset in pages where this mmap should start within the
657 * This function adjusts the passed in vma so that it points at the file of the
658 * dma_buf operation. It also adjusts the starting pgoff and does bounds
659 * checking on the size of the vma. Then it calls the exporters mmap function to
660 * set up the mapping.
662 * Can return negative error values, returns 0 on success.
664 int dma_buf_mmap(struct dma_buf
*dmabuf
, struct vm_area_struct
*vma
,
667 struct file
*oldfile
;
670 if (WARN_ON(!dmabuf
|| !vma
))
673 /* check for offset overflow */
674 if (pgoff
+ ((vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
) < pgoff
)
677 /* check for overflowing the buffer's size */
678 if (pgoff
+ ((vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
) >
679 dmabuf
->size
>> PAGE_SHIFT
)
682 /* readjust the vma */
683 get_file(dmabuf
->file
);
684 oldfile
= vma
->vm_file
;
685 vma
->vm_file
= dmabuf
->file
;
686 vma
->vm_pgoff
= pgoff
;
688 ret
= dmabuf
->ops
->mmap(dmabuf
, vma
);
690 /* restore old parameters on failure */
691 vma
->vm_file
= oldfile
;
700 EXPORT_SYMBOL_GPL(dma_buf_mmap
);
703 * dma_buf_vmap - Create virtual mapping for the buffer object into kernel
704 * address space. Same restrictions as for vmap and friends apply.
705 * @dmabuf: [in] buffer to vmap
707 * This call may fail due to lack of virtual mapping address space.
708 * These calls are optional in drivers. The intended use for them
709 * is for mapping objects linear in kernel space for high use objects.
710 * Please attempt to use kmap/kunmap before thinking about these interfaces.
712 * Returns NULL on error.
714 void *dma_buf_vmap(struct dma_buf
*dmabuf
)
718 if (WARN_ON(!dmabuf
))
721 if (!dmabuf
->ops
->vmap
)
724 mutex_lock(&dmabuf
->lock
);
725 if (dmabuf
->vmapping_counter
) {
726 dmabuf
->vmapping_counter
++;
727 BUG_ON(!dmabuf
->vmap_ptr
);
728 ptr
= dmabuf
->vmap_ptr
;
732 BUG_ON(dmabuf
->vmap_ptr
);
734 ptr
= dmabuf
->ops
->vmap(dmabuf
);
735 if (WARN_ON_ONCE(IS_ERR(ptr
)))
740 dmabuf
->vmap_ptr
= ptr
;
741 dmabuf
->vmapping_counter
= 1;
744 mutex_unlock(&dmabuf
->lock
);
747 EXPORT_SYMBOL_GPL(dma_buf_vmap
);
750 * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
751 * @dmabuf: [in] buffer to vunmap
752 * @vaddr: [in] vmap to vunmap
754 void dma_buf_vunmap(struct dma_buf
*dmabuf
, void *vaddr
)
756 if (WARN_ON(!dmabuf
))
759 BUG_ON(!dmabuf
->vmap_ptr
);
760 BUG_ON(dmabuf
->vmapping_counter
== 0);
761 BUG_ON(dmabuf
->vmap_ptr
!= vaddr
);
763 mutex_lock(&dmabuf
->lock
);
764 if (--dmabuf
->vmapping_counter
== 0) {
765 if (dmabuf
->ops
->vunmap
)
766 dmabuf
->ops
->vunmap(dmabuf
, vaddr
);
767 dmabuf
->vmap_ptr
= NULL
;
769 mutex_unlock(&dmabuf
->lock
);
771 EXPORT_SYMBOL_GPL(dma_buf_vunmap
);
773 #ifdef CONFIG_DEBUG_FS
774 static int dma_buf_describe(struct seq_file
*s
)
777 struct dma_buf
*buf_obj
;
778 struct dma_buf_attachment
*attach_obj
;
779 int count
= 0, attach_count
;
782 ret
= mutex_lock_interruptible(&db_list
.lock
);
787 seq_puts(s
, "\nDma-buf Objects:\n");
788 seq_puts(s
, "size\tflags\tmode\tcount\texp_name\n");
790 list_for_each_entry(buf_obj
, &db_list
.head
, list_node
) {
791 ret
= mutex_lock_interruptible(&buf_obj
->lock
);
795 "\tERROR locking buffer object: skipping\n");
799 seq_printf(s
, "%08zu\t%08x\t%08x\t%08ld\t%s\n",
801 buf_obj
->file
->f_flags
, buf_obj
->file
->f_mode
,
802 file_count(buf_obj
->file
),
805 seq_puts(s
, "\tAttached Devices:\n");
808 list_for_each_entry(attach_obj
, &buf_obj
->attachments
, node
) {
811 seq_printf(s
, "%s\n", dev_name(attach_obj
->dev
));
815 seq_printf(s
, "Total %d devices attached\n\n",
819 size
+= buf_obj
->size
;
820 mutex_unlock(&buf_obj
->lock
);
823 seq_printf(s
, "\nTotal %d objects, %zu bytes\n", count
, size
);
825 mutex_unlock(&db_list
.lock
);
829 static int dma_buf_show(struct seq_file
*s
, void *unused
)
831 void (*func
)(struct seq_file
*) = s
->private;
836 static int dma_buf_debug_open(struct inode
*inode
, struct file
*file
)
838 return single_open(file
, dma_buf_show
, inode
->i_private
);
841 static const struct file_operations dma_buf_debug_fops
= {
842 .open
= dma_buf_debug_open
,
845 .release
= single_release
,
848 static struct dentry
*dma_buf_debugfs_dir
;
850 static int dma_buf_init_debugfs(void)
853 dma_buf_debugfs_dir
= debugfs_create_dir("dma_buf", NULL
);
854 if (IS_ERR(dma_buf_debugfs_dir
)) {
855 err
= PTR_ERR(dma_buf_debugfs_dir
);
856 dma_buf_debugfs_dir
= NULL
;
860 err
= dma_buf_debugfs_create_file("bufinfo", dma_buf_describe
);
863 pr_debug("dma_buf: debugfs: failed to create node bufinfo\n");
868 static void dma_buf_uninit_debugfs(void)
870 if (dma_buf_debugfs_dir
)
871 debugfs_remove_recursive(dma_buf_debugfs_dir
);
874 int dma_buf_debugfs_create_file(const char *name
,
875 int (*write
)(struct seq_file
*))
879 d
= debugfs_create_file(name
, S_IRUGO
, dma_buf_debugfs_dir
,
880 write
, &dma_buf_debug_fops
);
882 return PTR_ERR_OR_ZERO(d
);
885 static inline int dma_buf_init_debugfs(void)
889 static inline void dma_buf_uninit_debugfs(void)
894 static int __init
dma_buf_init(void)
896 mutex_init(&db_list
.lock
);
897 INIT_LIST_HEAD(&db_list
.head
);
898 dma_buf_init_debugfs();
901 subsys_initcall(dma_buf_init
);
903 static void __exit
dma_buf_deinit(void)
905 dma_buf_uninit_debugfs();
907 __exitcall(dma_buf_deinit
);