dma-buf: add DMA_BUF_SET_NAME ioctls
[linux-2.6-block.git] / drivers / dma-buf / dma-buf.c
CommitLineData
d15bd7ee
SS
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>
f54d1867 28#include <linux/dma-fence.h>
d15bd7ee
SS
29#include <linux/anon_inodes.h>
30#include <linux/export.h>
b89e3563 31#include <linux/debugfs.h>
9abdffe2 32#include <linux/module.h>
b89e3563 33#include <linux/seq_file.h>
9b495a58 34#include <linux/poll.h>
3aac4502 35#include <linux/reservation.h>
b02da6f8 36#include <linux/mm.h>
ed63bb1d 37#include <linux/mount.h>
d15bd7ee 38
c11e391d 39#include <uapi/linux/dma-buf.h>
ed63bb1d 40#include <uapi/linux/magic.h>
c11e391d 41
d15bd7ee
SS
42static inline int is_dma_buf_file(struct file *);
43
b89e3563
SS
44struct dma_buf_list {
45 struct list_head head;
46 struct mutex lock;
47};
48
49static struct dma_buf_list db_list;
50
bb2bb903
GH
51static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen)
52{
53 struct dma_buf *dmabuf;
54 char name[DMA_BUF_NAME_LEN];
55 size_t ret = 0;
56
57 dmabuf = dentry->d_fsdata;
58 mutex_lock(&dmabuf->lock);
59 if (dmabuf->name)
60 ret = strlcpy(name, dmabuf->name, DMA_BUF_NAME_LEN);
61 mutex_unlock(&dmabuf->lock);
62
63 return dynamic_dname(dentry, buffer, buflen, "/%s:%s",
64 dentry->d_name.name, ret > 0 ? name : "");
65}
66
ed63bb1d 67static const struct dentry_operations dma_buf_dentry_ops = {
bb2bb903 68 .d_dname = dmabuffs_dname,
ed63bb1d
GH
69};
70
71static struct vfsmount *dma_buf_mnt;
72
73static struct dentry *dma_buf_fs_mount(struct file_system_type *fs_type,
74 int flags, const char *name, void *data)
75{
76 return mount_pseudo(fs_type, "dmabuf:", NULL, &dma_buf_dentry_ops,
77 DMA_BUF_MAGIC);
78}
79
80static struct file_system_type dma_buf_fs_type = {
81 .name = "dmabuf",
82 .mount = dma_buf_fs_mount,
83 .kill_sb = kill_anon_super,
84};
85
d15bd7ee
SS
86static int dma_buf_release(struct inode *inode, struct file *file)
87{
88 struct dma_buf *dmabuf;
89
90 if (!is_dma_buf_file(file))
91 return -EINVAL;
92
93 dmabuf = file->private_data;
94
f00b4dad
DV
95 BUG_ON(dmabuf->vmapping_counter);
96
9b495a58
ML
97 /*
98 * Any fences that a dma-buf poll can wait on should be signaled
99 * before releasing dma-buf. This is the responsibility of each
100 * driver that uses the reservation objects.
101 *
102 * If you hit this BUG() it means someone dropped their ref to the
103 * dma-buf while still having pending operation to the buffer.
104 */
105 BUG_ON(dmabuf->cb_shared.active || dmabuf->cb_excl.active);
106
d15bd7ee 107 dmabuf->ops->release(dmabuf);
b89e3563
SS
108
109 mutex_lock(&db_list.lock);
110 list_del(&dmabuf->list_node);
111 mutex_unlock(&db_list.lock);
112
3aac4502
ML
113 if (dmabuf->resv == (struct reservation_object *)&dmabuf[1])
114 reservation_object_fini(dmabuf->resv);
115
9abdffe2 116 module_put(dmabuf->owner);
d15bd7ee
SS
117 kfree(dmabuf);
118 return 0;
119}
120
4c78513e
DV
121static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
122{
123 struct dma_buf *dmabuf;
124
125 if (!is_dma_buf_file(file))
126 return -EINVAL;
127
128 dmabuf = file->private_data;
129
e3a9d6c5
AD
130 /* check if buffer supports mmap */
131 if (!dmabuf->ops->mmap)
132 return -EINVAL;
133
4c78513e 134 /* check for overflowing the buffer's size */
b02da6f8 135 if (vma->vm_pgoff + vma_pages(vma) >
4c78513e
DV
136 dmabuf->size >> PAGE_SHIFT)
137 return -EINVAL;
138
139 return dmabuf->ops->mmap(dmabuf, vma);
140}
141
19e8697b
CJHR
142static loff_t dma_buf_llseek(struct file *file, loff_t offset, int whence)
143{
144 struct dma_buf *dmabuf;
145 loff_t base;
146
147 if (!is_dma_buf_file(file))
148 return -EBADF;
149
150 dmabuf = file->private_data;
151
152 /* only support discovering the end of the buffer,
153 but also allow SEEK_SET to maintain the idiomatic
154 SEEK_END(0), SEEK_CUR(0) pattern */
155 if (whence == SEEK_END)
156 base = dmabuf->size;
157 else if (whence == SEEK_SET)
158 base = 0;
159 else
160 return -EINVAL;
161
162 if (offset != 0)
163 return -EINVAL;
164
165 return base + offset;
166}
167
e7e21c72
DV
168/**
169 * DOC: fence polling
170 *
171 * To support cross-device and cross-driver synchronization of buffer access
f641d3b5 172 * implicit fences (represented internally in the kernel with &struct fence) can
e7e21c72
DV
173 * be attached to a &dma_buf. The glue for that and a few related things are
174 * provided in the &reservation_object structure.
175 *
176 * Userspace can query the state of these implicitly tracked fences using poll()
177 * and related system calls:
178 *
a9a08845 179 * - Checking for EPOLLIN, i.e. read access, can be use to query the state of the
e7e21c72
DV
180 * most recent write or exclusive fence.
181 *
a9a08845 182 * - Checking for EPOLLOUT, i.e. write access, can be used to query the state of
e7e21c72
DV
183 * all attached fences, shared and exclusive ones.
184 *
185 * Note that this only signals the completion of the respective fences, i.e. the
186 * DMA transfers are complete. Cache flushing and any other necessary
187 * preparations before CPU access can begin still need to happen.
188 */
189
f54d1867 190static void dma_buf_poll_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
9b495a58
ML
191{
192 struct dma_buf_poll_cb_t *dcb = (struct dma_buf_poll_cb_t *)cb;
193 unsigned long flags;
194
195 spin_lock_irqsave(&dcb->poll->lock, flags);
196 wake_up_locked_poll(dcb->poll, dcb->active);
197 dcb->active = 0;
198 spin_unlock_irqrestore(&dcb->poll->lock, flags);
199}
200
afc9a42b 201static __poll_t dma_buf_poll(struct file *file, poll_table *poll)
9b495a58
ML
202{
203 struct dma_buf *dmabuf;
204 struct reservation_object *resv;
04a5faa8 205 struct reservation_object_list *fobj;
f54d1867 206 struct dma_fence *fence_excl;
01699437 207 __poll_t events;
3c3b177a 208 unsigned shared_count, seq;
9b495a58
ML
209
210 dmabuf = file->private_data;
211 if (!dmabuf || !dmabuf->resv)
a9a08845 212 return EPOLLERR;
9b495a58
ML
213
214 resv = dmabuf->resv;
215
216 poll_wait(file, &dmabuf->poll, poll);
217
a9a08845 218 events = poll_requested_events(poll) & (EPOLLIN | EPOLLOUT);
9b495a58
ML
219 if (!events)
220 return 0;
221
3c3b177a
ML
222retry:
223 seq = read_seqcount_begin(&resv->seq);
224 rcu_read_lock();
9b495a58 225
3c3b177a
ML
226 fobj = rcu_dereference(resv->fence);
227 if (fobj)
228 shared_count = fobj->shared_count;
229 else
230 shared_count = 0;
231 fence_excl = rcu_dereference(resv->fence_excl);
232 if (read_seqcount_retry(&resv->seq, seq)) {
233 rcu_read_unlock();
234 goto retry;
235 }
04a5faa8 236
a9a08845 237 if (fence_excl && (!(events & EPOLLOUT) || shared_count == 0)) {
9b495a58 238 struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_excl;
a9a08845 239 __poll_t pevents = EPOLLIN;
9b495a58 240
04a5faa8 241 if (shared_count == 0)
a9a08845 242 pevents |= EPOLLOUT;
9b495a58
ML
243
244 spin_lock_irq(&dmabuf->poll.lock);
245 if (dcb->active) {
246 dcb->active |= pevents;
247 events &= ~pevents;
248 } else
249 dcb->active = pevents;
250 spin_unlock_irq(&dmabuf->poll.lock);
251
252 if (events & pevents) {
f54d1867 253 if (!dma_fence_get_rcu(fence_excl)) {
3c3b177a
ML
254 /* force a recheck */
255 events &= ~pevents;
256 dma_buf_poll_cb(NULL, &dcb->cb);
f54d1867
CW
257 } else if (!dma_fence_add_callback(fence_excl, &dcb->cb,
258 dma_buf_poll_cb)) {
9b495a58 259 events &= ~pevents;
f54d1867 260 dma_fence_put(fence_excl);
04a5faa8 261 } else {
9b495a58
ML
262 /*
263 * No callback queued, wake up any additional
264 * waiters.
265 */
f54d1867 266 dma_fence_put(fence_excl);
9b495a58 267 dma_buf_poll_cb(NULL, &dcb->cb);
04a5faa8 268 }
9b495a58
ML
269 }
270 }
271
a9a08845 272 if ((events & EPOLLOUT) && shared_count > 0) {
9b495a58
ML
273 struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_shared;
274 int i;
275
276 /* Only queue a new callback if no event has fired yet */
277 spin_lock_irq(&dmabuf->poll.lock);
278 if (dcb->active)
a9a08845 279 events &= ~EPOLLOUT;
9b495a58 280 else
a9a08845 281 dcb->active = EPOLLOUT;
9b495a58
ML
282 spin_unlock_irq(&dmabuf->poll.lock);
283
a9a08845 284 if (!(events & EPOLLOUT))
9b495a58
ML
285 goto out;
286
04a5faa8 287 for (i = 0; i < shared_count; ++i) {
f54d1867 288 struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
04a5faa8 289
f54d1867 290 if (!dma_fence_get_rcu(fence)) {
3c3b177a
ML
291 /*
292 * fence refcount dropped to zero, this means
293 * that fobj has been freed
294 *
295 * call dma_buf_poll_cb and force a recheck!
296 */
a9a08845 297 events &= ~EPOLLOUT;
3c3b177a
ML
298 dma_buf_poll_cb(NULL, &dcb->cb);
299 break;
300 }
f54d1867
CW
301 if (!dma_fence_add_callback(fence, &dcb->cb,
302 dma_buf_poll_cb)) {
303 dma_fence_put(fence);
a9a08845 304 events &= ~EPOLLOUT;
9b495a58
ML
305 break;
306 }
f54d1867 307 dma_fence_put(fence);
04a5faa8 308 }
9b495a58
ML
309
310 /* No callback queued, wake up any additional waiters. */
04a5faa8 311 if (i == shared_count)
9b495a58
ML
312 dma_buf_poll_cb(NULL, &dcb->cb);
313 }
314
315out:
3c3b177a 316 rcu_read_unlock();
9b495a58
ML
317 return events;
318}
319
bb2bb903
GH
320/**
321 * dma_buf_set_name - Set a name to a specific dma_buf to track the usage.
322 * The name of the dma-buf buffer can only be set when the dma-buf is not
323 * attached to any devices. It could theoritically support changing the
324 * name of the dma-buf if the same piece of memory is used for multiple
325 * purpose between different devices.
326 *
327 * @dmabuf [in] dmabuf buffer that will be renamed.
328 * @buf: [in] A piece of userspace memory that contains the name of
329 * the dma-buf.
330 *
331 * Returns 0 on success. If the dma-buf buffer is already attached to
332 * devices, return -EBUSY.
333 *
334 */
335static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf)
336{
337 char *name = strndup_user(buf, DMA_BUF_NAME_LEN);
338 long ret = 0;
339
340 if (IS_ERR(name))
341 return PTR_ERR(name);
342
343 mutex_lock(&dmabuf->lock);
344 if (!list_empty(&dmabuf->attachments)) {
345 ret = -EBUSY;
346 kfree(name);
347 goto out_unlock;
348 }
349 kfree(dmabuf->name);
350 dmabuf->name = name;
351
352out_unlock:
353 mutex_unlock(&dmabuf->lock);
354 return ret;
355}
356
c11e391d
DV
357static long dma_buf_ioctl(struct file *file,
358 unsigned int cmd, unsigned long arg)
359{
360 struct dma_buf *dmabuf;
361 struct dma_buf_sync sync;
362 enum dma_data_direction direction;
18b862dc 363 int ret;
c11e391d
DV
364
365 dmabuf = file->private_data;
366
367 switch (cmd) {
368 case DMA_BUF_IOCTL_SYNC:
369 if (copy_from_user(&sync, (void __user *) arg, sizeof(sync)))
370 return -EFAULT;
371
372 if (sync.flags & ~DMA_BUF_SYNC_VALID_FLAGS_MASK)
373 return -EINVAL;
374
375 switch (sync.flags & DMA_BUF_SYNC_RW) {
376 case DMA_BUF_SYNC_READ:
377 direction = DMA_FROM_DEVICE;
378 break;
379 case DMA_BUF_SYNC_WRITE:
380 direction = DMA_TO_DEVICE;
381 break;
382 case DMA_BUF_SYNC_RW:
383 direction = DMA_BIDIRECTIONAL;
384 break;
385 default:
386 return -EINVAL;
387 }
388
389 if (sync.flags & DMA_BUF_SYNC_END)
18b862dc 390 ret = dma_buf_end_cpu_access(dmabuf, direction);
c11e391d 391 else
18b862dc 392 ret = dma_buf_begin_cpu_access(dmabuf, direction);
c11e391d 393
18b862dc 394 return ret;
bb2bb903
GH
395
396 case DMA_BUF_SET_NAME:
397 return dma_buf_set_name(dmabuf, (const char __user *)arg);
398
c11e391d
DV
399 default:
400 return -ENOTTY;
401 }
402}
403
d15bd7ee
SS
404static const struct file_operations dma_buf_fops = {
405 .release = dma_buf_release,
4c78513e 406 .mmap = dma_buf_mmap_internal,
19e8697b 407 .llseek = dma_buf_llseek,
9b495a58 408 .poll = dma_buf_poll,
c11e391d 409 .unlocked_ioctl = dma_buf_ioctl,
888022c0
MS
410#ifdef CONFIG_COMPAT
411 .compat_ioctl = dma_buf_ioctl,
412#endif
d15bd7ee
SS
413};
414
415/*
416 * is_dma_buf_file - Check if struct file* is associated with dma_buf
417 */
418static inline int is_dma_buf_file(struct file *file)
419{
420 return file->f_op == &dma_buf_fops;
421}
422
ed63bb1d
GH
423static struct file *dma_buf_getfile(struct dma_buf *dmabuf, int flags)
424{
425 struct file *file;
426 struct inode *inode = alloc_anon_inode(dma_buf_mnt->mnt_sb);
427
428 if (IS_ERR(inode))
429 return ERR_CAST(inode);
430
431 inode->i_size = dmabuf->size;
432 inode_set_bytes(inode, dmabuf->size);
433
434 file = alloc_file_pseudo(inode, dma_buf_mnt, "dmabuf",
435 flags, &dma_buf_fops);
436 if (IS_ERR(file))
437 goto err_alloc_file;
438 file->f_flags = flags & (O_ACCMODE | O_NONBLOCK);
439 file->private_data = dmabuf;
bb2bb903 440 file->f_path.dentry->d_fsdata = dmabuf;
ed63bb1d
GH
441
442 return file;
443
444err_alloc_file:
445 iput(inode);
446 return file;
447}
448
2904a8c1
DV
449/**
450 * DOC: dma buf device access
451 *
452 * For device DMA access to a shared DMA buffer the usual sequence of operations
453 * is fairly simple:
454 *
455 * 1. The exporter defines his exporter instance using
456 * DEFINE_DMA_BUF_EXPORT_INFO() and calls dma_buf_export() to wrap a private
457 * buffer object into a &dma_buf. It then exports that &dma_buf to userspace
458 * as a file descriptor by calling dma_buf_fd().
459 *
460 * 2. Userspace passes this file-descriptors to all drivers it wants this buffer
461 * to share with: First the filedescriptor is converted to a &dma_buf using
c138782d 462 * dma_buf_get(). Then the buffer is attached to the device using
2904a8c1
DV
463 * dma_buf_attach().
464 *
465 * Up to this stage the exporter is still free to migrate or reallocate the
466 * backing storage.
467 *
c138782d 468 * 3. Once the buffer is attached to all devices userspace can initiate DMA
2904a8c1
DV
469 * access to the shared buffer. In the kernel this is done by calling
470 * dma_buf_map_attachment() and dma_buf_unmap_attachment().
471 *
472 * 4. Once a driver is done with a shared buffer it needs to call
473 * dma_buf_detach() (after cleaning up any mappings) and then release the
474 * reference acquired with dma_buf_get by calling dma_buf_put().
475 *
476 * For the detailed semantics exporters are expected to implement see
477 * &dma_buf_ops.
478 */
479
d15bd7ee 480/**
d8fbe341 481 * dma_buf_export - Creates a new dma_buf, and associates an anon file
d15bd7ee
SS
482 * with this buffer, so it can be exported.
483 * Also connect the allocator specific data and ops to the buffer.
78df9695 484 * Additionally, provide a name string for exporter; useful in debugging.
d15bd7ee 485 *
d8fbe341 486 * @exp_info: [in] holds all the export related information provided
f641d3b5 487 * by the exporter. see &struct dma_buf_export_info
d8fbe341 488 * for further details.
d15bd7ee
SS
489 *
490 * Returns, on success, a newly created dma_buf object, which wraps the
491 * supplied private data and operations for dma_buf_ops. On either missing
492 * ops, or error in allocating struct dma_buf, will return negative error.
493 *
2904a8c1
DV
494 * For most cases the easiest way to create @exp_info is through the
495 * %DEFINE_DMA_BUF_EXPORT_INFO macro.
d15bd7ee 496 */
d8fbe341 497struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
d15bd7ee
SS
498{
499 struct dma_buf *dmabuf;
d8fbe341 500 struct reservation_object *resv = exp_info->resv;
d15bd7ee 501 struct file *file;
3aac4502 502 size_t alloc_size = sizeof(struct dma_buf);
a026df4c 503 int ret;
5136629d 504
d8fbe341 505 if (!exp_info->resv)
3aac4502
ML
506 alloc_size += sizeof(struct reservation_object);
507 else
508 /* prevent &dma_buf[1] == dma_buf->resv */
509 alloc_size += 1;
d15bd7ee 510
d8fbe341
SS
511 if (WARN_ON(!exp_info->priv
512 || !exp_info->ops
513 || !exp_info->ops->map_dma_buf
514 || !exp_info->ops->unmap_dma_buf
e3a9d6c5 515 || !exp_info->ops->release)) {
d15bd7ee
SS
516 return ERR_PTR(-EINVAL);
517 }
518
9abdffe2
SS
519 if (!try_module_get(exp_info->owner))
520 return ERR_PTR(-ENOENT);
521
3aac4502 522 dmabuf = kzalloc(alloc_size, GFP_KERNEL);
9abdffe2 523 if (!dmabuf) {
a026df4c
CW
524 ret = -ENOMEM;
525 goto err_module;
9abdffe2 526 }
d15bd7ee 527
d8fbe341
SS
528 dmabuf->priv = exp_info->priv;
529 dmabuf->ops = exp_info->ops;
530 dmabuf->size = exp_info->size;
531 dmabuf->exp_name = exp_info->exp_name;
9abdffe2 532 dmabuf->owner = exp_info->owner;
9b495a58
ML
533 init_waitqueue_head(&dmabuf->poll);
534 dmabuf->cb_excl.poll = dmabuf->cb_shared.poll = &dmabuf->poll;
535 dmabuf->cb_excl.active = dmabuf->cb_shared.active = 0;
536
3aac4502
ML
537 if (!resv) {
538 resv = (struct reservation_object *)&dmabuf[1];
539 reservation_object_init(resv);
540 }
541 dmabuf->resv = resv;
d15bd7ee 542
ed63bb1d 543 file = dma_buf_getfile(dmabuf, exp_info->flags);
9022e24e 544 if (IS_ERR(file)) {
a026df4c
CW
545 ret = PTR_ERR(file);
546 goto err_dmabuf;
9022e24e 547 }
19e8697b
CJHR
548
549 file->f_mode |= FMODE_LSEEK;
d15bd7ee
SS
550 dmabuf->file = file;
551
552 mutex_init(&dmabuf->lock);
553 INIT_LIST_HEAD(&dmabuf->attachments);
554
b89e3563
SS
555 mutex_lock(&db_list.lock);
556 list_add(&dmabuf->list_node, &db_list.head);
557 mutex_unlock(&db_list.lock);
558
d15bd7ee 559 return dmabuf;
a026df4c
CW
560
561err_dmabuf:
562 kfree(dmabuf);
563err_module:
564 module_put(exp_info->owner);
565 return ERR_PTR(ret);
d15bd7ee 566}
d8fbe341 567EXPORT_SYMBOL_GPL(dma_buf_export);
d15bd7ee
SS
568
569/**
570 * dma_buf_fd - returns a file descriptor for the given dma_buf
571 * @dmabuf: [in] pointer to dma_buf for which fd is required.
55c1c4ca 572 * @flags: [in] flags to give to fd
d15bd7ee
SS
573 *
574 * On success, returns an associated 'fd'. Else, returns error.
575 */
55c1c4ca 576int dma_buf_fd(struct dma_buf *dmabuf, int flags)
d15bd7ee 577{
f5e097f0 578 int fd;
d15bd7ee
SS
579
580 if (!dmabuf || !dmabuf->file)
581 return -EINVAL;
582
f5e097f0
BP
583 fd = get_unused_fd_flags(flags);
584 if (fd < 0)
585 return fd;
d15bd7ee
SS
586
587 fd_install(fd, dmabuf->file);
588
589 return fd;
590}
591EXPORT_SYMBOL_GPL(dma_buf_fd);
592
593/**
594 * dma_buf_get - returns the dma_buf structure related to an fd
595 * @fd: [in] fd associated with the dma_buf to be returned
596 *
597 * On success, returns the dma_buf structure associated with an fd; uses
598 * file's refcounting done by fget to increase refcount. returns ERR_PTR
599 * otherwise.
600 */
601struct dma_buf *dma_buf_get(int fd)
602{
603 struct file *file;
604
605 file = fget(fd);
606
607 if (!file)
608 return ERR_PTR(-EBADF);
609
610 if (!is_dma_buf_file(file)) {
611 fput(file);
612 return ERR_PTR(-EINVAL);
613 }
614
615 return file->private_data;
616}
617EXPORT_SYMBOL_GPL(dma_buf_get);
618
619/**
620 * dma_buf_put - decreases refcount of the buffer
621 * @dmabuf: [in] buffer to reduce refcount of
622 *
2904a8c1
DV
623 * Uses file's refcounting done implicitly by fput().
624 *
625 * If, as a result of this call, the refcount becomes 0, the 'release' file
e9b4d7b5
DV
626 * operation related to this fd is called. It calls &dma_buf_ops.release vfunc
627 * in turn, and frees the memory allocated for dmabuf when exported.
d15bd7ee
SS
628 */
629void dma_buf_put(struct dma_buf *dmabuf)
630{
631 if (WARN_ON(!dmabuf || !dmabuf->file))
632 return;
633
634 fput(dmabuf->file);
635}
636EXPORT_SYMBOL_GPL(dma_buf_put);
637
638/**
639 * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
640 * calls attach() of dma_buf_ops to allow device-specific attach functionality
641 * @dmabuf: [in] buffer to attach device to.
642 * @dev: [in] device to be attached.
643 *
2904a8c1
DV
644 * Returns struct dma_buf_attachment pointer for this attachment. Attachments
645 * must be cleaned up by calling dma_buf_detach().
646 *
647 * Returns:
648 *
649 * A pointer to newly created &dma_buf_attachment on success, or a negative
650 * error code wrapped into a pointer on failure.
651 *
652 * Note that this can fail if the backing storage of @dmabuf is in a place not
653 * accessible to @dev, and cannot be moved to a more suitable place. This is
654 * indicated with the error code -EBUSY.
d15bd7ee
SS
655 */
656struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
657 struct device *dev)
658{
659 struct dma_buf_attachment *attach;
660 int ret;
661
d1aa06a1 662 if (WARN_ON(!dmabuf || !dev))
d15bd7ee
SS
663 return ERR_PTR(-EINVAL);
664
db7942b6 665 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
34d84ec4 666 if (!attach)
a9fbc3b7 667 return ERR_PTR(-ENOMEM);
d15bd7ee 668
d15bd7ee
SS
669 attach->dev = dev;
670 attach->dmabuf = dmabuf;
2ed9201b
LP
671
672 mutex_lock(&dmabuf->lock);
673
d15bd7ee 674 if (dmabuf->ops->attach) {
a19741e5 675 ret = dmabuf->ops->attach(dmabuf, attach);
d15bd7ee
SS
676 if (ret)
677 goto err_attach;
678 }
679 list_add(&attach->node, &dmabuf->attachments);
680
681 mutex_unlock(&dmabuf->lock);
f13e143e 682
d15bd7ee
SS
683 return attach;
684
d15bd7ee
SS
685err_attach:
686 kfree(attach);
687 mutex_unlock(&dmabuf->lock);
688 return ERR_PTR(ret);
689}
690EXPORT_SYMBOL_GPL(dma_buf_attach);
691
692/**
693 * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
694 * optionally calls detach() of dma_buf_ops for device-specific detach
695 * @dmabuf: [in] buffer to detach from.
696 * @attach: [in] attachment to be detached; is free'd after this call.
697 *
2904a8c1 698 * Clean up a device attachment obtained by calling dma_buf_attach().
d15bd7ee
SS
699 */
700void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
701{
d1aa06a1 702 if (WARN_ON(!dmabuf || !attach))
d15bd7ee
SS
703 return;
704
f13e143e
CK
705 if (attach->sgt)
706 dmabuf->ops->unmap_dma_buf(attach, attach->sgt, attach->dir);
707
d15bd7ee
SS
708 mutex_lock(&dmabuf->lock);
709 list_del(&attach->node);
710 if (dmabuf->ops->detach)
711 dmabuf->ops->detach(dmabuf, attach);
712
713 mutex_unlock(&dmabuf->lock);
714 kfree(attach);
715}
716EXPORT_SYMBOL_GPL(dma_buf_detach);
717
718/**
719 * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
720 * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
721 * dma_buf_ops.
722 * @attach: [in] attachment whose scatterlist is to be returned
723 * @direction: [in] direction of DMA transfer
724 *
fee0c54e 725 * Returns sg_table containing the scatterlist to be returned; returns ERR_PTR
2904a8c1
DV
726 * on error. May return -EINTR if it is interrupted by a signal.
727 *
c138782d 728 * A mapping must be unmapped by using dma_buf_unmap_attachment(). Note that
2904a8c1
DV
729 * the underlying backing storage is pinned for as long as a mapping exists,
730 * therefore users/importers should not hold onto a mapping for undue amounts of
731 * time.
d15bd7ee
SS
732 */
733struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
734 enum dma_data_direction direction)
735{
531beb06 736 struct sg_table *sg_table;
d15bd7ee
SS
737
738 might_sleep();
739
d1aa06a1 740 if (WARN_ON(!attach || !attach->dmabuf))
d15bd7ee
SS
741 return ERR_PTR(-EINVAL);
742
f13e143e
CK
743 if (attach->sgt) {
744 /*
745 * Two mappings with different directions for the same
746 * attachment are not allowed.
747 */
748 if (attach->dir != direction &&
749 attach->dir != DMA_BIDIRECTIONAL)
750 return ERR_PTR(-EBUSY);
751
752 return attach->sgt;
753 }
754
d1aa06a1 755 sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
fee0c54e
CC
756 if (!sg_table)
757 sg_table = ERR_PTR(-ENOMEM);
d15bd7ee 758
f13e143e
CK
759 if (!IS_ERR(sg_table) && attach->dmabuf->ops->cache_sgt_mapping) {
760 attach->sgt = sg_table;
761 attach->dir = direction;
762 }
763
d15bd7ee
SS
764 return sg_table;
765}
766EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
767
768/**
769 * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
770 * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
771 * dma_buf_ops.
772 * @attach: [in] attachment to unmap buffer from
773 * @sg_table: [in] scatterlist info of the buffer to unmap
33ea2dcb 774 * @direction: [in] direction of DMA transfer
d15bd7ee 775 *
2904a8c1 776 * This unmaps a DMA mapping for @attached obtained by dma_buf_map_attachment().
d15bd7ee
SS
777 */
778void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
33ea2dcb
SS
779 struct sg_table *sg_table,
780 enum dma_data_direction direction)
d15bd7ee 781{
b6fa0cd6
RC
782 might_sleep();
783
d1aa06a1 784 if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
d15bd7ee
SS
785 return;
786
f13e143e
CK
787 if (attach->sgt == sg_table)
788 return;
789
790 attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
d15bd7ee
SS
791}
792EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
fc13020e 793
0959a168
DV
794/**
795 * DOC: cpu access
796 *
797 * There are mutliple reasons for supporting CPU access to a dma buffer object:
798 *
799 * - Fallback operations in the kernel, for example when a device is connected
800 * over USB and the kernel needs to shuffle the data around first before
801 * sending it away. Cache coherency is handled by braketing any transactions
802 * with calls to dma_buf_begin_cpu_access() and dma_buf_end_cpu_access()
803 * access.
804 *
805 * To support dma_buf objects residing in highmem cpu access is page-based
806 * using an api similar to kmap. Accessing a dma_buf is done in aligned chunks
807 * of PAGE_SIZE size. Before accessing a chunk it needs to be mapped, which
808 * returns a pointer in kernel virtual address space. Afterwards the chunk
809 * needs to be unmapped again. There is no limit on how often a given chunk
810 * can be mapped and unmapped, i.e. the importer does not need to call
811 * begin_cpu_access again before mapping the same chunk again.
812 *
813 * Interfaces::
814 * void \*dma_buf_kmap(struct dma_buf \*, unsigned long);
815 * void dma_buf_kunmap(struct dma_buf \*, unsigned long, void \*);
816 *
f664a526
CK
817 * Implementing the functions is optional for exporters and for importers all
818 * the restrictions of using kmap apply.
0959a168
DV
819 *
820 * dma_buf kmap calls outside of the range specified in begin_cpu_access are
821 * undefined. If the range is not PAGE_SIZE aligned, kmap needs to succeed on
822 * the partial chunks at the beginning and end but may return stale or bogus
823 * data outside of the range (in these partial chunks).
824 *
0959a168
DV
825 * For some cases the overhead of kmap can be too high, a vmap interface
826 * is introduced. This interface should be used very carefully, as vmalloc
827 * space is a limited resources on many architectures.
828 *
829 * Interfaces::
830 * void \*dma_buf_vmap(struct dma_buf \*dmabuf)
831 * void dma_buf_vunmap(struct dma_buf \*dmabuf, void \*vaddr)
832 *
833 * The vmap call can fail if there is no vmap support in the exporter, or if
834 * it runs out of vmalloc space. Fallback to kmap should be implemented. Note
835 * that the dma-buf layer keeps a reference count for all vmap access and
836 * calls down into the exporter's vmap function only when no vmapping exists,
837 * and only unmaps it once. Protection against concurrent vmap/vunmap calls is
838 * provided by taking the dma_buf->lock mutex.
839 *
840 * - For full compatibility on the importer side with existing userspace
841 * interfaces, which might already support mmap'ing buffers. This is needed in
842 * many processing pipelines (e.g. feeding a software rendered image into a
843 * hardware pipeline, thumbnail creation, snapshots, ...). Also, Android's ION
844 * framework already supported this and for DMA buffer file descriptors to
845 * replace ION buffers mmap support was needed.
846 *
847 * There is no special interfaces, userspace simply calls mmap on the dma-buf
848 * fd. But like for CPU access there's a need to braket the actual access,
849 * which is handled by the ioctl (DMA_BUF_IOCTL_SYNC). Note that
850 * DMA_BUF_IOCTL_SYNC can fail with -EAGAIN or -EINTR, in which case it must
851 * be restarted.
852 *
853 * Some systems might need some sort of cache coherency management e.g. when
854 * CPU and GPU domains are being accessed through dma-buf at the same time.
855 * To circumvent this problem there are begin/end coherency markers, that
856 * forward directly to existing dma-buf device drivers vfunc hooks. Userspace
857 * can make use of those markers through the DMA_BUF_IOCTL_SYNC ioctl. The
858 * sequence would be used like following:
859 *
860 * - mmap dma-buf fd
861 * - for each drawing/upload cycle in CPU 1. SYNC_START ioctl, 2. read/write
862 * to mmap area 3. SYNC_END ioctl. This can be repeated as often as you
863 * want (with the new data being consumed by say the GPU or the scanout
864 * device)
865 * - munmap once you don't need the buffer any more
866 *
867 * For correctness and optimal performance, it is always required to use
868 * SYNC_START and SYNC_END before and after, respectively, when accessing the
869 * mapped address. Userspace cannot rely on coherent access, even when there
870 * are systems where it just works without calling these ioctls.
871 *
872 * - And as a CPU fallback in userspace processing pipelines.
873 *
874 * Similar to the motivation for kernel cpu access it is again important that
875 * the userspace code of a given importing subsystem can use the same
876 * interfaces with a imported dma-buf buffer object as with a native buffer
877 * object. This is especially important for drm where the userspace part of
878 * contemporary OpenGL, X, and other drivers is huge, and reworking them to
879 * use a different way to mmap a buffer rather invasive.
880 *
881 * The assumption in the current dma-buf interfaces is that redirecting the
882 * initial mmap is all that's needed. A survey of some of the existing
883 * subsystems shows that no driver seems to do any nefarious thing like
884 * syncing up with outstanding asynchronous processing on the device or
885 * allocating special resources at fault time. So hopefully this is good
886 * enough, since adding interfaces to intercept pagefaults and allow pte
887 * shootdowns would increase the complexity quite a bit.
888 *
889 * Interface::
890 * int dma_buf_mmap(struct dma_buf \*, struct vm_area_struct \*,
891 * unsigned long);
892 *
893 * If the importing subsystem simply provides a special-purpose mmap call to
894 * set up a mapping in userspace, calling do_mmap with dma_buf->file will
895 * equally achieve that for a dma-buf object.
896 */
897
ae4e46b1
CW
898static int __dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
899 enum dma_data_direction direction)
900{
901 bool write = (direction == DMA_BIDIRECTIONAL ||
902 direction == DMA_TO_DEVICE);
903 struct reservation_object *resv = dmabuf->resv;
904 long ret;
905
906 /* Wait on any implicit rendering fences */
907 ret = reservation_object_wait_timeout_rcu(resv, write, true,
908 MAX_SCHEDULE_TIMEOUT);
909 if (ret < 0)
910 return ret;
911
912 return 0;
913}
fc13020e
DV
914
915/**
916 * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
917 * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
918 * preparations. Coherency is only guaranteed in the specified range for the
919 * specified access direction.
efb4df82 920 * @dmabuf: [in] buffer to prepare cpu access for.
fc13020e
DV
921 * @direction: [in] length of range for cpu access.
922 *
0959a168
DV
923 * After the cpu access is complete the caller should call
924 * dma_buf_end_cpu_access(). Only when cpu access is braketed by both calls is
925 * it guaranteed to be coherent with other DMA access.
926 *
fc13020e
DV
927 * Can return negative error values, returns 0 on success.
928 */
831e9da7 929int dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
fc13020e
DV
930 enum dma_data_direction direction)
931{
932 int ret = 0;
933
934 if (WARN_ON(!dmabuf))
935 return -EINVAL;
936
937 if (dmabuf->ops->begin_cpu_access)
831e9da7 938 ret = dmabuf->ops->begin_cpu_access(dmabuf, direction);
fc13020e 939
ae4e46b1
CW
940 /* Ensure that all fences are waited upon - but we first allow
941 * the native handler the chance to do so more efficiently if it
942 * chooses. A double invocation here will be reasonably cheap no-op.
943 */
944 if (ret == 0)
945 ret = __dma_buf_begin_cpu_access(dmabuf, direction);
946
fc13020e
DV
947 return ret;
948}
949EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access);
950
951/**
952 * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
953 * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
954 * actions. Coherency is only guaranteed in the specified range for the
955 * specified access direction.
efb4df82 956 * @dmabuf: [in] buffer to complete cpu access for.
fc13020e
DV
957 * @direction: [in] length of range for cpu access.
958 *
0959a168
DV
959 * This terminates CPU access started with dma_buf_begin_cpu_access().
960 *
87e332d5 961 * Can return negative error values, returns 0 on success.
fc13020e 962 */
18b862dc
CW
963int dma_buf_end_cpu_access(struct dma_buf *dmabuf,
964 enum dma_data_direction direction)
fc13020e 965{
18b862dc
CW
966 int ret = 0;
967
fc13020e
DV
968 WARN_ON(!dmabuf);
969
970 if (dmabuf->ops->end_cpu_access)
18b862dc
CW
971 ret = dmabuf->ops->end_cpu_access(dmabuf, direction);
972
973 return ret;
fc13020e
DV
974}
975EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access);
976
fc13020e
DV
977/**
978 * dma_buf_kmap - Map a page of the buffer object into kernel address space. The
979 * same restrictions as for kmap and friends apply.
efb4df82 980 * @dmabuf: [in] buffer to map page from.
fc13020e
DV
981 * @page_num: [in] page in PAGE_SIZE units to map.
982 *
983 * This call must always succeed, any necessary preparations that might fail
984 * need to be done in begin_cpu_access.
985 */
986void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num)
987{
988 WARN_ON(!dmabuf);
989
09ea0dfb
GH
990 if (!dmabuf->ops->map)
991 return NULL;
f9b67f00 992 return dmabuf->ops->map(dmabuf, page_num);
fc13020e
DV
993}
994EXPORT_SYMBOL_GPL(dma_buf_kmap);
995
996/**
997 * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap.
efb4df82 998 * @dmabuf: [in] buffer to unmap page from.
fc13020e
DV
999 * @page_num: [in] page in PAGE_SIZE units to unmap.
1000 * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap.
1001 *
1002 * This call must always succeed.
1003 */
1004void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num,
1005 void *vaddr)
1006{
1007 WARN_ON(!dmabuf);
1008
f9b67f00
LG
1009 if (dmabuf->ops->unmap)
1010 dmabuf->ops->unmap(dmabuf, page_num, vaddr);
fc13020e
DV
1011}
1012EXPORT_SYMBOL_GPL(dma_buf_kunmap);
4c78513e
DV
1013
1014
1015/**
1016 * dma_buf_mmap - Setup up a userspace mmap with the given vma
12c4727e 1017 * @dmabuf: [in] buffer that should back the vma
4c78513e
DV
1018 * @vma: [in] vma for the mmap
1019 * @pgoff: [in] offset in pages where this mmap should start within the
5136629d 1020 * dma-buf buffer.
4c78513e
DV
1021 *
1022 * This function adjusts the passed in vma so that it points at the file of the
ecf1dbac 1023 * dma_buf operation. It also adjusts the starting pgoff and does bounds
4c78513e
DV
1024 * checking on the size of the vma. Then it calls the exporters mmap function to
1025 * set up the mapping.
1026 *
1027 * Can return negative error values, returns 0 on success.
1028 */
1029int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
1030 unsigned long pgoff)
1031{
495c10cc
JS
1032 struct file *oldfile;
1033 int ret;
1034
4c78513e
DV
1035 if (WARN_ON(!dmabuf || !vma))
1036 return -EINVAL;
1037
e3a9d6c5
AD
1038 /* check if buffer supports mmap */
1039 if (!dmabuf->ops->mmap)
1040 return -EINVAL;
1041
4c78513e 1042 /* check for offset overflow */
b02da6f8 1043 if (pgoff + vma_pages(vma) < pgoff)
4c78513e
DV
1044 return -EOVERFLOW;
1045
1046 /* check for overflowing the buffer's size */
b02da6f8 1047 if (pgoff + vma_pages(vma) >
4c78513e
DV
1048 dmabuf->size >> PAGE_SHIFT)
1049 return -EINVAL;
1050
1051 /* readjust the vma */
495c10cc
JS
1052 get_file(dmabuf->file);
1053 oldfile = vma->vm_file;
1054 vma->vm_file = dmabuf->file;
4c78513e
DV
1055 vma->vm_pgoff = pgoff;
1056
495c10cc
JS
1057 ret = dmabuf->ops->mmap(dmabuf, vma);
1058 if (ret) {
1059 /* restore old parameters on failure */
1060 vma->vm_file = oldfile;
1061 fput(dmabuf->file);
1062 } else {
1063 if (oldfile)
1064 fput(oldfile);
1065 }
1066 return ret;
1067
4c78513e
DV
1068}
1069EXPORT_SYMBOL_GPL(dma_buf_mmap);
98f86c9e
DA
1070
1071/**
12c4727e
SS
1072 * dma_buf_vmap - Create virtual mapping for the buffer object into kernel
1073 * address space. Same restrictions as for vmap and friends apply.
1074 * @dmabuf: [in] buffer to vmap
98f86c9e
DA
1075 *
1076 * This call may fail due to lack of virtual mapping address space.
1077 * These calls are optional in drivers. The intended use for them
1078 * is for mapping objects linear in kernel space for high use objects.
1079 * Please attempt to use kmap/kunmap before thinking about these interfaces.
fee0c54e
CC
1080 *
1081 * Returns NULL on error.
98f86c9e
DA
1082 */
1083void *dma_buf_vmap(struct dma_buf *dmabuf)
1084{
f00b4dad
DV
1085 void *ptr;
1086
98f86c9e
DA
1087 if (WARN_ON(!dmabuf))
1088 return NULL;
1089
f00b4dad
DV
1090 if (!dmabuf->ops->vmap)
1091 return NULL;
1092
1093 mutex_lock(&dmabuf->lock);
1094 if (dmabuf->vmapping_counter) {
1095 dmabuf->vmapping_counter++;
1096 BUG_ON(!dmabuf->vmap_ptr);
1097 ptr = dmabuf->vmap_ptr;
1098 goto out_unlock;
1099 }
1100
1101 BUG_ON(dmabuf->vmap_ptr);
1102
1103 ptr = dmabuf->ops->vmap(dmabuf);
fee0c54e
CC
1104 if (WARN_ON_ONCE(IS_ERR(ptr)))
1105 ptr = NULL;
1106 if (!ptr)
f00b4dad
DV
1107 goto out_unlock;
1108
1109 dmabuf->vmap_ptr = ptr;
1110 dmabuf->vmapping_counter = 1;
1111
1112out_unlock:
1113 mutex_unlock(&dmabuf->lock);
1114 return ptr;
98f86c9e
DA
1115}
1116EXPORT_SYMBOL_GPL(dma_buf_vmap);
1117
1118/**
1119 * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
12c4727e 1120 * @dmabuf: [in] buffer to vunmap
6e7b4a59 1121 * @vaddr: [in] vmap to vunmap
98f86c9e
DA
1122 */
1123void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
1124{
1125 if (WARN_ON(!dmabuf))
1126 return;
1127
f00b4dad
DV
1128 BUG_ON(!dmabuf->vmap_ptr);
1129 BUG_ON(dmabuf->vmapping_counter == 0);
1130 BUG_ON(dmabuf->vmap_ptr != vaddr);
1131
1132 mutex_lock(&dmabuf->lock);
1133 if (--dmabuf->vmapping_counter == 0) {
1134 if (dmabuf->ops->vunmap)
1135 dmabuf->ops->vunmap(dmabuf, vaddr);
1136 dmabuf->vmap_ptr = NULL;
1137 }
1138 mutex_unlock(&dmabuf->lock);
98f86c9e
DA
1139}
1140EXPORT_SYMBOL_GPL(dma_buf_vunmap);
b89e3563
SS
1141
1142#ifdef CONFIG_DEBUG_FS
eb0b947e 1143static int dma_buf_debug_show(struct seq_file *s, void *unused)
b89e3563
SS
1144{
1145 int ret;
1146 struct dma_buf *buf_obj;
1147 struct dma_buf_attachment *attach_obj;
5eb2c72c
RK
1148 struct reservation_object *robj;
1149 struct reservation_object_list *fobj;
1150 struct dma_fence *fence;
1151 unsigned seq;
1152 int count = 0, attach_count, shared_count, i;
b89e3563
SS
1153 size_t size = 0;
1154
1155 ret = mutex_lock_interruptible(&db_list.lock);
1156
1157 if (ret)
1158 return ret;
1159
c0b00a52 1160 seq_puts(s, "\nDma-buf Objects:\n");
ed63bb1d
GH
1161 seq_printf(s, "%-8s\t%-8s\t%-8s\t%-8s\texp_name\t%-8s\n",
1162 "size", "flags", "mode", "count", "ino");
b89e3563
SS
1163
1164 list_for_each_entry(buf_obj, &db_list.head, list_node) {
1165 ret = mutex_lock_interruptible(&buf_obj->lock);
1166
1167 if (ret) {
c0b00a52
SS
1168 seq_puts(s,
1169 "\tERROR locking buffer object: skipping\n");
b89e3563
SS
1170 continue;
1171 }
1172
bb2bb903 1173 seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\t%08lu\t%s\n",
c0b00a52 1174 buf_obj->size,
b89e3563 1175 buf_obj->file->f_flags, buf_obj->file->f_mode,
a1f6dbac 1176 file_count(buf_obj->file),
ed63bb1d 1177 buf_obj->exp_name,
bb2bb903
GH
1178 file_inode(buf_obj->file)->i_ino,
1179 buf_obj->name ?: "");
b89e3563 1180
5eb2c72c
RK
1181 robj = buf_obj->resv;
1182 while (true) {
1183 seq = read_seqcount_begin(&robj->seq);
1184 rcu_read_lock();
1185 fobj = rcu_dereference(robj->fence);
1186 shared_count = fobj ? fobj->shared_count : 0;
1187 fence = rcu_dereference(robj->fence_excl);
1188 if (!read_seqcount_retry(&robj->seq, seq))
1189 break;
1190 rcu_read_unlock();
1191 }
1192
1193 if (fence)
1194 seq_printf(s, "\tExclusive fence: %s %s %ssignalled\n",
1195 fence->ops->get_driver_name(fence),
1196 fence->ops->get_timeline_name(fence),
1197 dma_fence_is_signaled(fence) ? "" : "un");
1198 for (i = 0; i < shared_count; i++) {
1199 fence = rcu_dereference(fobj->shared[i]);
1200 if (!dma_fence_get_rcu(fence))
1201 continue;
1202 seq_printf(s, "\tShared fence: %s %s %ssignalled\n",
1203 fence->ops->get_driver_name(fence),
1204 fence->ops->get_timeline_name(fence),
1205 dma_fence_is_signaled(fence) ? "" : "un");
5e383a97 1206 dma_fence_put(fence);
5eb2c72c
RK
1207 }
1208 rcu_read_unlock();
1209
c0b00a52 1210 seq_puts(s, "\tAttached Devices:\n");
b89e3563
SS
1211 attach_count = 0;
1212
1213 list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
9eddb41d 1214 seq_printf(s, "\t%s\n", dev_name(attach_obj->dev));
b89e3563
SS
1215 attach_count++;
1216 }
1217
c0b00a52 1218 seq_printf(s, "Total %d devices attached\n\n",
b89e3563
SS
1219 attach_count);
1220
1221 count++;
1222 size += buf_obj->size;
1223 mutex_unlock(&buf_obj->lock);
1224 }
1225
1226 seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size);
1227
1228 mutex_unlock(&db_list.lock);
1229 return 0;
1230}
1231
2674305a 1232DEFINE_SHOW_ATTRIBUTE(dma_buf_debug);
b89e3563
SS
1233
1234static struct dentry *dma_buf_debugfs_dir;
1235
1236static int dma_buf_init_debugfs(void)
1237{
bd3e2208 1238 struct dentry *d;
b89e3563 1239 int err = 0;
5136629d 1240
bd3e2208
MK
1241 d = debugfs_create_dir("dma_buf", NULL);
1242 if (IS_ERR(d))
1243 return PTR_ERR(d);
5136629d 1244
bd3e2208 1245 dma_buf_debugfs_dir = d;
b89e3563 1246
bd3e2208
MK
1247 d = debugfs_create_file("bufinfo", S_IRUGO, dma_buf_debugfs_dir,
1248 NULL, &dma_buf_debug_fops);
1249 if (IS_ERR(d)) {
b89e3563 1250 pr_debug("dma_buf: debugfs: failed to create node bufinfo\n");
b7479990
MK
1251 debugfs_remove_recursive(dma_buf_debugfs_dir);
1252 dma_buf_debugfs_dir = NULL;
bd3e2208 1253 err = PTR_ERR(d);
b7479990 1254 }
b89e3563
SS
1255
1256 return err;
1257}
1258
1259static void dma_buf_uninit_debugfs(void)
1260{
298b6a81 1261 debugfs_remove_recursive(dma_buf_debugfs_dir);
b89e3563 1262}
b89e3563
SS
1263#else
1264static inline int dma_buf_init_debugfs(void)
1265{
1266 return 0;
1267}
1268static inline void dma_buf_uninit_debugfs(void)
1269{
1270}
1271#endif
1272
1273static int __init dma_buf_init(void)
1274{
ed63bb1d
GH
1275 dma_buf_mnt = kern_mount(&dma_buf_fs_type);
1276 if (IS_ERR(dma_buf_mnt))
1277 return PTR_ERR(dma_buf_mnt);
1278
b89e3563
SS
1279 mutex_init(&db_list.lock);
1280 INIT_LIST_HEAD(&db_list.head);
1281 dma_buf_init_debugfs();
1282 return 0;
1283}
1284subsys_initcall(dma_buf_init);
1285
1286static void __exit dma_buf_deinit(void)
1287{
1288 dma_buf_uninit_debugfs();
ed63bb1d 1289 kern_unmount(dma_buf_mnt);
b89e3563
SS
1290}
1291__exitcall(dma_buf_deinit);