blob: 412629601ad3b42e130bb042329ec4e8e925a566 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Sumit Semwald15bd7e2011-12-26 14:53:15 +05302/*
3 * Framework for buffer objects that can be shared across devices/subsystems.
4 *
5 * Copyright(C) 2011 Linaro Limited. All rights reserved.
6 * Author: Sumit Semwal <sumit.semwal@ti.com>
7 *
8 * Many thanks to linaro-mm-sig list, and specially
9 * Arnd Bergmann <arnd@arndb.de>, Rob Clark <rob@ti.com> and
10 * Daniel Vetter <daniel@ffwll.ch> for their support in creation and
11 * refining of this idea.
Sumit Semwald15bd7e2011-12-26 14:53:15 +053012 */
13
14#include <linux/fs.h>
15#include <linux/slab.h>
16#include <linux/dma-buf.h>
Chris Wilsonf54d1862016-10-25 13:00:45 +010017#include <linux/dma-fence.h>
Sumit Semwald15bd7e2011-12-26 14:53:15 +053018#include <linux/anon_inodes.h>
19#include <linux/export.h>
Sumit Semwalb89e35632013-04-04 11:44:37 +053020#include <linux/debugfs.h>
Sumit Semwal9abdffe2015-05-05 14:56:15 +053021#include <linux/module.h>
Sumit Semwalb89e35632013-04-04 11:44:37 +053022#include <linux/seq_file.h>
Maarten Lankhorst9b495a52014-07-01 12:57:43 +020023#include <linux/poll.h>
Christian König52791ee2019-08-11 10:06:32 +020024#include <linux/dma-resv.h>
Muhammad Falak R Wanib02da6f2016-05-23 17:08:42 +053025#include <linux/mm.h>
Greg Hackmanned63bb12019-06-13 15:34:06 -070026#include <linux/mount.h>
Linus Torvalds933a90b2019-07-19 10:42:02 -070027#include <linux/pseudo_fs.h>
Sumit Semwald15bd7e2011-12-26 14:53:15 +053028
Daniel Vetterc11e3912016-02-11 20:04:51 -020029#include <uapi/linux/dma-buf.h>
Greg Hackmanned63bb12019-06-13 15:34:06 -070030#include <uapi/linux/magic.h>
Daniel Vetterc11e3912016-02-11 20:04:51 -020031
Sumit Semwald15bd7e2011-12-26 14:53:15 +053032static inline int is_dma_buf_file(struct file *);
33
Sumit Semwalb89e35632013-04-04 11:44:37 +053034struct dma_buf_list {
35 struct list_head head;
36 struct mutex lock;
37};
38
39static struct dma_buf_list db_list;
40
Greg Hackmannbb2bb902019-06-13 15:34:07 -070041static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen)
42{
43 struct dma_buf *dmabuf;
44 char name[DMA_BUF_NAME_LEN];
45 size_t ret = 0;
46
47 dmabuf = dentry->d_fsdata;
Christian König15fd5522018-07-03 16:42:26 +020048 dma_resv_lock(dmabuf->resv, NULL);
Greg Hackmannbb2bb902019-06-13 15:34:07 -070049 if (dmabuf->name)
50 ret = strlcpy(name, dmabuf->name, DMA_BUF_NAME_LEN);
Christian König15fd5522018-07-03 16:42:26 +020051 dma_resv_unlock(dmabuf->resv);
Greg Hackmannbb2bb902019-06-13 15:34:07 -070052
53 return dynamic_dname(dentry, buffer, buflen, "/%s:%s",
54 dentry->d_name.name, ret > 0 ? name : "");
55}
56
Sumit Semwal4ab59c32020-06-11 17:14:18 +053057static void dma_buf_release(struct dentry *dentry)
Sumit Semwald15bd7e2011-12-26 14:53:15 +053058{
59 struct dma_buf *dmabuf;
60
Sumit Semwal4ab59c32020-06-11 17:14:18 +053061 dmabuf = dentry->d_fsdata;
Sumit Semwald15bd7e2011-12-26 14:53:15 +053062
Daniel Vetterf00b4da2012-12-20 14:14:23 +010063 BUG_ON(dmabuf->vmapping_counter);
64
Maarten Lankhorst9b495a52014-07-01 12:57:43 +020065 /*
66 * Any fences that a dma-buf poll can wait on should be signaled
67 * before releasing dma-buf. This is the responsibility of each
68 * driver that uses the reservation objects.
69 *
70 * If you hit this BUG() it means someone dropped their ref to the
71 * dma-buf while still having pending operation to the buffer.
72 */
73 BUG_ON(dmabuf->cb_shared.active || dmabuf->cb_excl.active);
74
Sumit Semwald15bd7e2011-12-26 14:53:15 +053075 dmabuf->ops->release(dmabuf);
Sumit Semwalb89e35632013-04-04 11:44:37 +053076
77 mutex_lock(&db_list.lock);
78 list_del(&dmabuf->list_node);
79 mutex_unlock(&db_list.lock);
80
Christian König52791ee2019-08-11 10:06:32 +020081 if (dmabuf->resv == (struct dma_resv *)&dmabuf[1])
82 dma_resv_fini(dmabuf->resv);
Maarten Lankhorst3aac4502014-07-01 12:57:26 +020083
Sumit Semwal9abdffe2015-05-05 14:56:15 +053084 module_put(dmabuf->owner);
Cong Wangd1f37222019-12-26 22:32:04 -080085 kfree(dmabuf->name);
Sumit Semwald15bd7e2011-12-26 14:53:15 +053086 kfree(dmabuf);
Sumit Semwal4ab59c32020-06-11 17:14:18 +053087}
88
89static const struct dentry_operations dma_buf_dentry_ops = {
90 .d_dname = dmabuffs_dname,
91 .d_release = dma_buf_release,
92};
93
94static struct vfsmount *dma_buf_mnt;
95
96static int dma_buf_fs_init_context(struct fs_context *fc)
97{
98 struct pseudo_fs_context *ctx;
99
100 ctx = init_pseudo(fc, DMA_BUF_MAGIC);
101 if (!ctx)
102 return -ENOMEM;
103 ctx->dops = &dma_buf_dentry_ops;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530104 return 0;
105}
106
Sumit Semwal4ab59c32020-06-11 17:14:18 +0530107static struct file_system_type dma_buf_fs_type = {
108 .name = "dmabuf",
109 .init_fs_context = dma_buf_fs_init_context,
110 .kill_sb = kill_anon_super,
111};
112
Daniel Vetter4c785132012-04-24 14:38:52 +0530113static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
114{
115 struct dma_buf *dmabuf;
116
117 if (!is_dma_buf_file(file))
118 return -EINVAL;
119
120 dmabuf = file->private_data;
121
Andrew F. Davise3a9d6c2019-03-29 11:52:01 -0500122 /* check if buffer supports mmap */
123 if (!dmabuf->ops->mmap)
124 return -EINVAL;
125
Daniel Vetter4c785132012-04-24 14:38:52 +0530126 /* check for overflowing the buffer's size */
Muhammad Falak R Wanib02da6f2016-05-23 17:08:42 +0530127 if (vma->vm_pgoff + vma_pages(vma) >
Daniel Vetter4c785132012-04-24 14:38:52 +0530128 dmabuf->size >> PAGE_SHIFT)
129 return -EINVAL;
130
131 return dmabuf->ops->mmap(dmabuf, vma);
132}
133
Christopher James Halse Rogers19e86972013-09-10 11:36:45 +0530134static loff_t dma_buf_llseek(struct file *file, loff_t offset, int whence)
135{
136 struct dma_buf *dmabuf;
137 loff_t base;
138
139 if (!is_dma_buf_file(file))
140 return -EBADF;
141
142 dmabuf = file->private_data;
143
144 /* only support discovering the end of the buffer,
145 but also allow SEEK_SET to maintain the idiomatic
146 SEEK_END(0), SEEK_CUR(0) pattern */
147 if (whence == SEEK_END)
148 base = dmabuf->size;
149 else if (whence == SEEK_SET)
150 base = 0;
151 else
152 return -EINVAL;
153
154 if (offset != 0)
155 return -EINVAL;
156
157 return base + offset;
158}
159
Daniel Vettere7e21c72016-12-09 22:50:55 +0100160/**
161 * DOC: fence polling
162 *
163 * To support cross-device and cross-driver synchronization of buffer access
Daniel Vetterf641d3b2016-12-29 21:48:24 +0100164 * implicit fences (represented internally in the kernel with &struct fence) can
Daniel Vettere7e21c72016-12-09 22:50:55 +0100165 * be attached to a &dma_buf. The glue for that and a few related things are
Christian König52791ee2019-08-11 10:06:32 +0200166 * provided in the &dma_resv structure.
Daniel Vettere7e21c72016-12-09 22:50:55 +0100167 *
168 * Userspace can query the state of these implicitly tracked fences using poll()
169 * and related system calls:
170 *
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800171 * - Checking for EPOLLIN, i.e. read access, can be use to query the state of the
Daniel Vettere7e21c72016-12-09 22:50:55 +0100172 * most recent write or exclusive fence.
173 *
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800174 * - Checking for EPOLLOUT, i.e. write access, can be used to query the state of
Daniel Vettere7e21c72016-12-09 22:50:55 +0100175 * all attached fences, shared and exclusive ones.
176 *
177 * Note that this only signals the completion of the respective fences, i.e. the
178 * DMA transfers are complete. Cache flushing and any other necessary
179 * preparations before CPU access can begin still need to happen.
180 */
181
Chris Wilsonf54d1862016-10-25 13:00:45 +0100182static void dma_buf_poll_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200183{
184 struct dma_buf_poll_cb_t *dcb = (struct dma_buf_poll_cb_t *)cb;
185 unsigned long flags;
186
187 spin_lock_irqsave(&dcb->poll->lock, flags);
188 wake_up_locked_poll(dcb->poll, dcb->active);
189 dcb->active = 0;
190 spin_unlock_irqrestore(&dcb->poll->lock, flags);
191}
192
Al Viroafc9a422017-07-03 06:39:46 -0400193static __poll_t dma_buf_poll(struct file *file, poll_table *poll)
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200194{
195 struct dma_buf *dmabuf;
Christian König52791ee2019-08-11 10:06:32 +0200196 struct dma_resv *resv;
197 struct dma_resv_list *fobj;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100198 struct dma_fence *fence_excl;
Al Viro01699432017-07-03 03:14:15 -0400199 __poll_t events;
Chris Wilsonb016cd62019-08-14 19:24:01 +0100200 unsigned shared_count, seq;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200201
202 dmabuf = file->private_data;
203 if (!dmabuf || !dmabuf->resv)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800204 return EPOLLERR;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200205
206 resv = dmabuf->resv;
207
208 poll_wait(file, &dmabuf->poll, poll);
209
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800210 events = poll_requested_events(poll) & (EPOLLIN | EPOLLOUT);
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200211 if (!events)
212 return 0;
213
Chris Wilsonb016cd62019-08-14 19:24:01 +0100214retry:
215 seq = read_seqcount_begin(&resv->seq);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200216 rcu_read_lock();
Chris Wilsonb016cd62019-08-14 19:24:01 +0100217
218 fobj = rcu_dereference(resv->fence);
219 if (fobj)
220 shared_count = fobj->shared_count;
221 else
222 shared_count = 0;
223 fence_excl = rcu_dereference(resv->fence_excl);
224 if (read_seqcount_retry(&resv->seq, seq)) {
225 rcu_read_unlock();
226 goto retry;
227 }
228
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800229 if (fence_excl && (!(events & EPOLLOUT) || shared_count == 0)) {
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200230 struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_excl;
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800231 __poll_t pevents = EPOLLIN;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200232
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200233 if (shared_count == 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800234 pevents |= EPOLLOUT;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200235
236 spin_lock_irq(&dmabuf->poll.lock);
237 if (dcb->active) {
238 dcb->active |= pevents;
239 events &= ~pevents;
240 } else
241 dcb->active = pevents;
242 spin_unlock_irq(&dmabuf->poll.lock);
243
244 if (events & pevents) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100245 if (!dma_fence_get_rcu(fence_excl)) {
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200246 /* force a recheck */
247 events &= ~pevents;
248 dma_buf_poll_cb(NULL, &dcb->cb);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100249 } else if (!dma_fence_add_callback(fence_excl, &dcb->cb,
250 dma_buf_poll_cb)) {
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200251 events &= ~pevents;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100252 dma_fence_put(fence_excl);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200253 } else {
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200254 /*
255 * No callback queued, wake up any additional
256 * waiters.
257 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100258 dma_fence_put(fence_excl);
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200259 dma_buf_poll_cb(NULL, &dcb->cb);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200260 }
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200261 }
262 }
263
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800264 if ((events & EPOLLOUT) && shared_count > 0) {
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200265 struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_shared;
266 int i;
267
268 /* Only queue a new callback if no event has fired yet */
269 spin_lock_irq(&dmabuf->poll.lock);
270 if (dcb->active)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800271 events &= ~EPOLLOUT;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200272 else
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800273 dcb->active = EPOLLOUT;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200274 spin_unlock_irq(&dmabuf->poll.lock);
275
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800276 if (!(events & EPOLLOUT))
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200277 goto out;
278
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200279 for (i = 0; i < shared_count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100280 struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200281
Chris Wilsonf54d1862016-10-25 13:00:45 +0100282 if (!dma_fence_get_rcu(fence)) {
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200283 /*
284 * fence refcount dropped to zero, this means
285 * that fobj has been freed
286 *
287 * call dma_buf_poll_cb and force a recheck!
288 */
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800289 events &= ~EPOLLOUT;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200290 dma_buf_poll_cb(NULL, &dcb->cb);
291 break;
292 }
Chris Wilsonf54d1862016-10-25 13:00:45 +0100293 if (!dma_fence_add_callback(fence, &dcb->cb,
294 dma_buf_poll_cb)) {
295 dma_fence_put(fence);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800296 events &= ~EPOLLOUT;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200297 break;
298 }
Chris Wilsonf54d1862016-10-25 13:00:45 +0100299 dma_fence_put(fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200300 }
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200301
302 /* No callback queued, wake up any additional waiters. */
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200303 if (i == shared_count)
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200304 dma_buf_poll_cb(NULL, &dcb->cb);
305 }
306
307out:
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200308 rcu_read_unlock();
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200309 return events;
310}
311
Greg Hackmannbb2bb902019-06-13 15:34:07 -0700312/**
313 * dma_buf_set_name - Set a name to a specific dma_buf to track the usage.
314 * The name of the dma-buf buffer can only be set when the dma-buf is not
315 * attached to any devices. It could theoritically support changing the
316 * name of the dma-buf if the same piece of memory is used for multiple
317 * purpose between different devices.
318 *
319 * @dmabuf [in] dmabuf buffer that will be renamed.
320 * @buf: [in] A piece of userspace memory that contains the name of
321 * the dma-buf.
322 *
323 * Returns 0 on success. If the dma-buf buffer is already attached to
324 * devices, return -EBUSY.
325 *
326 */
327static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf)
328{
329 char *name = strndup_user(buf, DMA_BUF_NAME_LEN);
330 long ret = 0;
331
332 if (IS_ERR(name))
333 return PTR_ERR(name);
334
Christian König15fd5522018-07-03 16:42:26 +0200335 dma_resv_lock(dmabuf->resv, NULL);
Greg Hackmannbb2bb902019-06-13 15:34:07 -0700336 if (!list_empty(&dmabuf->attachments)) {
337 ret = -EBUSY;
338 kfree(name);
339 goto out_unlock;
340 }
341 kfree(dmabuf->name);
342 dmabuf->name = name;
343
344out_unlock:
Christian König15fd5522018-07-03 16:42:26 +0200345 dma_resv_unlock(dmabuf->resv);
Greg Hackmannbb2bb902019-06-13 15:34:07 -0700346 return ret;
347}
348
Daniel Vetterc11e3912016-02-11 20:04:51 -0200349static long dma_buf_ioctl(struct file *file,
350 unsigned int cmd, unsigned long arg)
351{
352 struct dma_buf *dmabuf;
353 struct dma_buf_sync sync;
354 enum dma_data_direction direction;
Chris Wilson18b862d2016-03-18 20:02:39 +0000355 int ret;
Daniel Vetterc11e3912016-02-11 20:04:51 -0200356
357 dmabuf = file->private_data;
358
359 switch (cmd) {
360 case DMA_BUF_IOCTL_SYNC:
361 if (copy_from_user(&sync, (void __user *) arg, sizeof(sync)))
362 return -EFAULT;
363
364 if (sync.flags & ~DMA_BUF_SYNC_VALID_FLAGS_MASK)
365 return -EINVAL;
366
367 switch (sync.flags & DMA_BUF_SYNC_RW) {
368 case DMA_BUF_SYNC_READ:
369 direction = DMA_FROM_DEVICE;
370 break;
371 case DMA_BUF_SYNC_WRITE:
372 direction = DMA_TO_DEVICE;
373 break;
374 case DMA_BUF_SYNC_RW:
375 direction = DMA_BIDIRECTIONAL;
376 break;
377 default:
378 return -EINVAL;
379 }
380
381 if (sync.flags & DMA_BUF_SYNC_END)
Chris Wilson18b862d2016-03-18 20:02:39 +0000382 ret = dma_buf_end_cpu_access(dmabuf, direction);
Daniel Vetterc11e3912016-02-11 20:04:51 -0200383 else
Chris Wilson18b862d2016-03-18 20:02:39 +0000384 ret = dma_buf_begin_cpu_access(dmabuf, direction);
Daniel Vetterc11e3912016-02-11 20:04:51 -0200385
Chris Wilson18b862d2016-03-18 20:02:39 +0000386 return ret;
Greg Hackmannbb2bb902019-06-13 15:34:07 -0700387
Daniel Vettera5bff922020-04-07 15:30:02 +0200388 case DMA_BUF_SET_NAME_A:
389 case DMA_BUF_SET_NAME_B:
Greg Hackmannbb2bb902019-06-13 15:34:07 -0700390 return dma_buf_set_name(dmabuf, (const char __user *)arg);
391
Daniel Vetterc11e3912016-02-11 20:04:51 -0200392 default:
393 return -ENOTTY;
394 }
395}
396
Greg Hackmannbcc07112019-06-13 15:34:08 -0700397static void dma_buf_show_fdinfo(struct seq_file *m, struct file *file)
398{
399 struct dma_buf *dmabuf = file->private_data;
400
401 seq_printf(m, "size:\t%zu\n", dmabuf->size);
402 /* Don't count the temporary reference taken inside procfs seq_show */
403 seq_printf(m, "count:\t%ld\n", file_count(dmabuf->file) - 1);
404 seq_printf(m, "exp_name:\t%s\n", dmabuf->exp_name);
Christian König15fd5522018-07-03 16:42:26 +0200405 dma_resv_lock(dmabuf->resv, NULL);
Greg Hackmannbcc07112019-06-13 15:34:08 -0700406 if (dmabuf->name)
407 seq_printf(m, "name:\t%s\n", dmabuf->name);
Christian König15fd5522018-07-03 16:42:26 +0200408 dma_resv_unlock(dmabuf->resv);
Greg Hackmannbcc07112019-06-13 15:34:08 -0700409}
410
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530411static const struct file_operations dma_buf_fops = {
Daniel Vetter4c785132012-04-24 14:38:52 +0530412 .mmap = dma_buf_mmap_internal,
Christopher James Halse Rogers19e86972013-09-10 11:36:45 +0530413 .llseek = dma_buf_llseek,
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200414 .poll = dma_buf_poll,
Daniel Vetterc11e3912016-02-11 20:04:51 -0200415 .unlocked_ioctl = dma_buf_ioctl,
Arnd Bergmann1832f2d2018-09-11 21:59:08 +0200416 .compat_ioctl = compat_ptr_ioctl,
Greg Hackmannbcc07112019-06-13 15:34:08 -0700417 .show_fdinfo = dma_buf_show_fdinfo,
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530418};
419
420/*
421 * is_dma_buf_file - Check if struct file* is associated with dma_buf
422 */
423static inline int is_dma_buf_file(struct file *file)
424{
425 return file->f_op == &dma_buf_fops;
426}
427
Greg Hackmanned63bb12019-06-13 15:34:06 -0700428static struct file *dma_buf_getfile(struct dma_buf *dmabuf, int flags)
429{
430 struct file *file;
431 struct inode *inode = alloc_anon_inode(dma_buf_mnt->mnt_sb);
432
433 if (IS_ERR(inode))
434 return ERR_CAST(inode);
435
436 inode->i_size = dmabuf->size;
437 inode_set_bytes(inode, dmabuf->size);
438
439 file = alloc_file_pseudo(inode, dma_buf_mnt, "dmabuf",
440 flags, &dma_buf_fops);
441 if (IS_ERR(file))
442 goto err_alloc_file;
443 file->f_flags = flags & (O_ACCMODE | O_NONBLOCK);
444 file->private_data = dmabuf;
Greg Hackmannbb2bb902019-06-13 15:34:07 -0700445 file->f_path.dentry->d_fsdata = dmabuf;
Greg Hackmanned63bb12019-06-13 15:34:06 -0700446
447 return file;
448
449err_alloc_file:
450 iput(inode);
451 return file;
452}
453
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530454/**
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100455 * DOC: dma buf device access
456 *
457 * For device DMA access to a shared DMA buffer the usual sequence of operations
458 * is fairly simple:
459 *
460 * 1. The exporter defines his exporter instance using
461 * DEFINE_DMA_BUF_EXPORT_INFO() and calls dma_buf_export() to wrap a private
462 * buffer object into a &dma_buf. It then exports that &dma_buf to userspace
463 * as a file descriptor by calling dma_buf_fd().
464 *
465 * 2. Userspace passes this file-descriptors to all drivers it wants this buffer
466 * to share with: First the filedescriptor is converted to a &dma_buf using
Liviu Dudauc1387822017-11-01 14:06:30 +0000467 * dma_buf_get(). Then the buffer is attached to the device using
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100468 * dma_buf_attach().
469 *
470 * Up to this stage the exporter is still free to migrate or reallocate the
471 * backing storage.
472 *
Liviu Dudauc1387822017-11-01 14:06:30 +0000473 * 3. Once the buffer is attached to all devices userspace can initiate DMA
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100474 * access to the shared buffer. In the kernel this is done by calling
475 * dma_buf_map_attachment() and dma_buf_unmap_attachment().
476 *
477 * 4. Once a driver is done with a shared buffer it needs to call
478 * dma_buf_detach() (after cleaning up any mappings) and then release the
479 * reference acquired with dma_buf_get by calling dma_buf_put().
480 *
481 * For the detailed semantics exporters are expected to implement see
482 * &dma_buf_ops.
483 */
484
485/**
Sumit Semwald8fbe342015-01-23 12:53:43 +0530486 * dma_buf_export - Creates a new dma_buf, and associates an anon file
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530487 * with this buffer, so it can be exported.
488 * Also connect the allocator specific data and ops to the buffer.
Sumit Semwal78df9692013-03-22 18:22:16 +0530489 * Additionally, provide a name string for exporter; useful in debugging.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530490 *
Sumit Semwald8fbe342015-01-23 12:53:43 +0530491 * @exp_info: [in] holds all the export related information provided
Daniel Vetterf641d3b2016-12-29 21:48:24 +0100492 * by the exporter. see &struct dma_buf_export_info
Sumit Semwald8fbe342015-01-23 12:53:43 +0530493 * for further details.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530494 *
495 * Returns, on success, a newly created dma_buf object, which wraps the
496 * supplied private data and operations for dma_buf_ops. On either missing
497 * ops, or error in allocating struct dma_buf, will return negative error.
498 *
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100499 * For most cases the easiest way to create @exp_info is through the
500 * %DEFINE_DMA_BUF_EXPORT_INFO macro.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530501 */
Sumit Semwald8fbe342015-01-23 12:53:43 +0530502struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530503{
504 struct dma_buf *dmabuf;
Christian König52791ee2019-08-11 10:06:32 +0200505 struct dma_resv *resv = exp_info->resv;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530506 struct file *file;
Maarten Lankhorst3aac4502014-07-01 12:57:26 +0200507 size_t alloc_size = sizeof(struct dma_buf);
Chris Wilsona026df42016-07-18 12:16:22 +0100508 int ret;
Jagan Teki51366292015-05-21 01:09:31 +0530509
Sumit Semwald8fbe342015-01-23 12:53:43 +0530510 if (!exp_info->resv)
Christian König52791ee2019-08-11 10:06:32 +0200511 alloc_size += sizeof(struct dma_resv);
Maarten Lankhorst3aac4502014-07-01 12:57:26 +0200512 else
513 /* prevent &dma_buf[1] == dma_buf->resv */
514 alloc_size += 1;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530515
Sumit Semwald8fbe342015-01-23 12:53:43 +0530516 if (WARN_ON(!exp_info->priv
517 || !exp_info->ops
518 || !exp_info->ops->map_dma_buf
519 || !exp_info->ops->unmap_dma_buf
Andrew F. Davise3a9d6c2019-03-29 11:52:01 -0500520 || !exp_info->ops->release)) {
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530521 return ERR_PTR(-EINVAL);
522 }
523
Christian König15fd5522018-07-03 16:42:26 +0200524 if (WARN_ON(exp_info->ops->cache_sgt_mapping &&
Christian Königbd2275e2020-02-18 16:57:24 +0100525 (exp_info->ops->pin || exp_info->ops->unpin)))
Christian König15fd5522018-07-03 16:42:26 +0200526 return ERR_PTR(-EINVAL);
527
Christian Königbd2275e2020-02-18 16:57:24 +0100528 if (WARN_ON(!exp_info->ops->pin != !exp_info->ops->unpin))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530529 return ERR_PTR(-EINVAL);
530
Sumit Semwal9abdffe2015-05-05 14:56:15 +0530531 if (!try_module_get(exp_info->owner))
532 return ERR_PTR(-ENOENT);
533
Maarten Lankhorst3aac4502014-07-01 12:57:26 +0200534 dmabuf = kzalloc(alloc_size, GFP_KERNEL);
Sumit Semwal9abdffe2015-05-05 14:56:15 +0530535 if (!dmabuf) {
Chris Wilsona026df42016-07-18 12:16:22 +0100536 ret = -ENOMEM;
537 goto err_module;
Sumit Semwal9abdffe2015-05-05 14:56:15 +0530538 }
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530539
Sumit Semwald8fbe342015-01-23 12:53:43 +0530540 dmabuf->priv = exp_info->priv;
541 dmabuf->ops = exp_info->ops;
542 dmabuf->size = exp_info->size;
543 dmabuf->exp_name = exp_info->exp_name;
Sumit Semwal9abdffe2015-05-05 14:56:15 +0530544 dmabuf->owner = exp_info->owner;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200545 init_waitqueue_head(&dmabuf->poll);
546 dmabuf->cb_excl.poll = dmabuf->cb_shared.poll = &dmabuf->poll;
547 dmabuf->cb_excl.active = dmabuf->cb_shared.active = 0;
548
Maarten Lankhorst3aac4502014-07-01 12:57:26 +0200549 if (!resv) {
Christian König52791ee2019-08-11 10:06:32 +0200550 resv = (struct dma_resv *)&dmabuf[1];
551 dma_resv_init(resv);
Maarten Lankhorst3aac4502014-07-01 12:57:26 +0200552 }
553 dmabuf->resv = resv;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530554
Greg Hackmanned63bb12019-06-13 15:34:06 -0700555 file = dma_buf_getfile(dmabuf, exp_info->flags);
Tuomas Tynkkynen9022e242013-08-27 16:30:38 +0300556 if (IS_ERR(file)) {
Chris Wilsona026df42016-07-18 12:16:22 +0100557 ret = PTR_ERR(file);
558 goto err_dmabuf;
Tuomas Tynkkynen9022e242013-08-27 16:30:38 +0300559 }
Christopher James Halse Rogers19e86972013-09-10 11:36:45 +0530560
561 file->f_mode |= FMODE_LSEEK;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530562 dmabuf->file = file;
563
564 mutex_init(&dmabuf->lock);
565 INIT_LIST_HEAD(&dmabuf->attachments);
566
Sumit Semwalb89e35632013-04-04 11:44:37 +0530567 mutex_lock(&db_list.lock);
568 list_add(&dmabuf->list_node, &db_list.head);
569 mutex_unlock(&db_list.lock);
570
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530571 return dmabuf;
Chris Wilsona026df42016-07-18 12:16:22 +0100572
573err_dmabuf:
574 kfree(dmabuf);
575err_module:
576 module_put(exp_info->owner);
577 return ERR_PTR(ret);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530578}
Sumit Semwald8fbe342015-01-23 12:53:43 +0530579EXPORT_SYMBOL_GPL(dma_buf_export);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530580
581/**
582 * dma_buf_fd - returns a file descriptor for the given dma_buf
583 * @dmabuf: [in] pointer to dma_buf for which fd is required.
Dave Airlie55c1c4c2012-03-16 10:34:02 +0000584 * @flags: [in] flags to give to fd
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530585 *
586 * On success, returns an associated 'fd'. Else, returns error.
587 */
Dave Airlie55c1c4c2012-03-16 10:34:02 +0000588int dma_buf_fd(struct dma_buf *dmabuf, int flags)
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530589{
Borislav Petkovf5e097f2012-12-11 16:05:26 +0100590 int fd;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530591
592 if (!dmabuf || !dmabuf->file)
593 return -EINVAL;
594
Borislav Petkovf5e097f2012-12-11 16:05:26 +0100595 fd = get_unused_fd_flags(flags);
596 if (fd < 0)
597 return fd;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530598
599 fd_install(fd, dmabuf->file);
600
601 return fd;
602}
603EXPORT_SYMBOL_GPL(dma_buf_fd);
604
605/**
606 * dma_buf_get - returns the dma_buf structure related to an fd
607 * @fd: [in] fd associated with the dma_buf to be returned
608 *
609 * On success, returns the dma_buf structure associated with an fd; uses
610 * file's refcounting done by fget to increase refcount. returns ERR_PTR
611 * otherwise.
612 */
613struct dma_buf *dma_buf_get(int fd)
614{
615 struct file *file;
616
617 file = fget(fd);
618
619 if (!file)
620 return ERR_PTR(-EBADF);
621
622 if (!is_dma_buf_file(file)) {
623 fput(file);
624 return ERR_PTR(-EINVAL);
625 }
626
627 return file->private_data;
628}
629EXPORT_SYMBOL_GPL(dma_buf_get);
630
631/**
632 * dma_buf_put - decreases refcount of the buffer
633 * @dmabuf: [in] buffer to reduce refcount of
634 *
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100635 * Uses file's refcounting done implicitly by fput().
636 *
637 * If, as a result of this call, the refcount becomes 0, the 'release' file
Daniel Vettere9b4d7b2016-12-29 21:48:25 +0100638 * operation related to this fd is called. It calls &dma_buf_ops.release vfunc
639 * in turn, and frees the memory allocated for dmabuf when exported.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530640 */
641void dma_buf_put(struct dma_buf *dmabuf)
642{
643 if (WARN_ON(!dmabuf || !dmabuf->file))
644 return;
645
646 fput(dmabuf->file);
647}
648EXPORT_SYMBOL_GPL(dma_buf_put);
649
650/**
Christian König15fd5522018-07-03 16:42:26 +0200651 * dma_buf_dynamic_attach - Add the device to dma_buf's attachments list; optionally,
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530652 * calls attach() of dma_buf_ops to allow device-specific attach functionality
Christian König15fd5522018-07-03 16:42:26 +0200653 * @dmabuf: [in] buffer to attach device to.
654 * @dev: [in] device to be attached.
Randy Dunlap6f49c252020-04-07 21:20:34 -0700655 * @importer_ops: [in] importer operations for the attachment
656 * @importer_priv: [in] importer private pointer for the attachment
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530657 *
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100658 * Returns struct dma_buf_attachment pointer for this attachment. Attachments
659 * must be cleaned up by calling dma_buf_detach().
660 *
661 * Returns:
662 *
663 * A pointer to newly created &dma_buf_attachment on success, or a negative
664 * error code wrapped into a pointer on failure.
665 *
666 * Note that this can fail if the backing storage of @dmabuf is in a place not
667 * accessible to @dev, and cannot be moved to a more suitable place. This is
668 * indicated with the error code -EBUSY.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530669 */
Christian König15fd5522018-07-03 16:42:26 +0200670struct dma_buf_attachment *
671dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
Christian Königbb42df42018-07-03 16:42:26 +0200672 const struct dma_buf_attach_ops *importer_ops,
673 void *importer_priv)
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530674{
675 struct dma_buf_attachment *attach;
676 int ret;
677
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100678 if (WARN_ON(!dmabuf || !dev))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530679 return ERR_PTR(-EINVAL);
680
Christian König4981cdb2020-02-19 13:32:43 +0100681 if (WARN_ON(importer_ops && !importer_ops->move_notify))
682 return ERR_PTR(-EINVAL);
683
Markus Elfringdb7942b2017-05-08 10:50:09 +0200684 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
Markus Elfring34d84ec2017-05-08 10:54:17 +0200685 if (!attach)
Laurent Pincharta9fbc3b2012-01-26 12:27:24 +0100686 return ERR_PTR(-ENOMEM);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530687
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530688 attach->dev = dev;
689 attach->dmabuf = dmabuf;
Christian König09606b52018-03-22 17:09:42 +0100690 if (importer_ops)
691 attach->peer2peer = importer_ops->allow_peer2peer;
Christian Königbb42df42018-07-03 16:42:26 +0200692 attach->importer_ops = importer_ops;
693 attach->importer_priv = importer_priv;
Laurent Pinchart2ed92012012-01-26 12:27:25 +0100694
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530695 if (dmabuf->ops->attach) {
Christian Königa19741e2018-05-28 11:47:52 +0200696 ret = dmabuf->ops->attach(dmabuf, attach);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530697 if (ret)
698 goto err_attach;
699 }
Christian König15fd5522018-07-03 16:42:26 +0200700 dma_resv_lock(dmabuf->resv, NULL);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530701 list_add(&attach->node, &dmabuf->attachments);
Christian König15fd5522018-07-03 16:42:26 +0200702 dma_resv_unlock(dmabuf->resv);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530703
Christian König15fd5522018-07-03 16:42:26 +0200704 /* When either the importer or the exporter can't handle dynamic
705 * mappings we cache the mapping here to avoid issues with the
706 * reservation object lock.
707 */
708 if (dma_buf_attachment_is_dynamic(attach) !=
709 dma_buf_is_dynamic(dmabuf)) {
710 struct sg_table *sgt;
711
Christian Königbb42df42018-07-03 16:42:26 +0200712 if (dma_buf_is_dynamic(attach->dmabuf)) {
Christian König15fd5522018-07-03 16:42:26 +0200713 dma_resv_lock(attach->dmabuf->resv, NULL);
Christian Königbb42df42018-07-03 16:42:26 +0200714 ret = dma_buf_pin(attach);
715 if (ret)
716 goto err_unlock;
717 }
Christian König15fd5522018-07-03 16:42:26 +0200718
719 sgt = dmabuf->ops->map_dma_buf(attach, DMA_BIDIRECTIONAL);
720 if (!sgt)
721 sgt = ERR_PTR(-ENOMEM);
722 if (IS_ERR(sgt)) {
723 ret = PTR_ERR(sgt);
Christian Königbb42df42018-07-03 16:42:26 +0200724 goto err_unpin;
Christian König15fd5522018-07-03 16:42:26 +0200725 }
726 if (dma_buf_is_dynamic(attach->dmabuf))
727 dma_resv_unlock(attach->dmabuf->resv);
728 attach->sgt = sgt;
729 attach->dir = DMA_BIDIRECTIONAL;
730 }
731
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530732 return attach;
733
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530734err_attach:
735 kfree(attach);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530736 return ERR_PTR(ret);
Christian König15fd5522018-07-03 16:42:26 +0200737
Christian Königbb42df42018-07-03 16:42:26 +0200738err_unpin:
739 if (dma_buf_is_dynamic(attach->dmabuf))
740 dma_buf_unpin(attach);
741
Christian König15fd5522018-07-03 16:42:26 +0200742err_unlock:
743 if (dma_buf_is_dynamic(attach->dmabuf))
744 dma_resv_unlock(attach->dmabuf->resv);
745
746 dma_buf_detach(dmabuf, attach);
747 return ERR_PTR(ret);
748}
749EXPORT_SYMBOL_GPL(dma_buf_dynamic_attach);
750
751/**
752 * dma_buf_attach - Wrapper for dma_buf_dynamic_attach
753 * @dmabuf: [in] buffer to attach device to.
754 * @dev: [in] device to be attached.
755 *
756 * Wrapper to call dma_buf_dynamic_attach() for drivers which still use a static
757 * mapping.
758 */
759struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
760 struct device *dev)
761{
Christian Königbb42df42018-07-03 16:42:26 +0200762 return dma_buf_dynamic_attach(dmabuf, dev, NULL, NULL);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530763}
764EXPORT_SYMBOL_GPL(dma_buf_attach);
765
766/**
767 * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
768 * optionally calls detach() of dma_buf_ops for device-specific detach
769 * @dmabuf: [in] buffer to detach from.
770 * @attach: [in] attachment to be detached; is free'd after this call.
771 *
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100772 * Clean up a device attachment obtained by calling dma_buf_attach().
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530773 */
774void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
775{
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100776 if (WARN_ON(!dmabuf || !attach))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530777 return;
778
Christian König15fd5522018-07-03 16:42:26 +0200779 if (attach->sgt) {
780 if (dma_buf_is_dynamic(attach->dmabuf))
781 dma_resv_lock(attach->dmabuf->resv, NULL);
782
Christian Königf13e1432018-07-03 16:42:26 +0200783 dmabuf->ops->unmap_dma_buf(attach, attach->sgt, attach->dir);
784
Christian Königbb42df42018-07-03 16:42:26 +0200785 if (dma_buf_is_dynamic(attach->dmabuf)) {
786 dma_buf_unpin(attach);
Christian König15fd5522018-07-03 16:42:26 +0200787 dma_resv_unlock(attach->dmabuf->resv);
Christian Königbb42df42018-07-03 16:42:26 +0200788 }
Christian König15fd5522018-07-03 16:42:26 +0200789 }
790
Christian König15fd5522018-07-03 16:42:26 +0200791 dma_resv_lock(dmabuf->resv, NULL);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530792 list_del(&attach->node);
Christian König15fd5522018-07-03 16:42:26 +0200793 dma_resv_unlock(dmabuf->resv);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530794 if (dmabuf->ops->detach)
795 dmabuf->ops->detach(dmabuf, attach);
796
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530797 kfree(attach);
798}
799EXPORT_SYMBOL_GPL(dma_buf_detach);
800
801/**
Christian Königbb42df42018-07-03 16:42:26 +0200802 * dma_buf_pin - Lock down the DMA-buf
803 *
804 * @attach: [in] attachment which should be pinned
805 *
806 * Returns:
807 * 0 on success, negative error code on failure.
808 */
809int dma_buf_pin(struct dma_buf_attachment *attach)
810{
811 struct dma_buf *dmabuf = attach->dmabuf;
812 int ret = 0;
813
814 dma_resv_assert_held(dmabuf->resv);
815
816 if (dmabuf->ops->pin)
817 ret = dmabuf->ops->pin(attach);
818
819 return ret;
820}
821EXPORT_SYMBOL_GPL(dma_buf_pin);
822
823/**
824 * dma_buf_unpin - Remove lock from DMA-buf
825 *
826 * @attach: [in] attachment which should be unpinned
827 */
828void dma_buf_unpin(struct dma_buf_attachment *attach)
829{
830 struct dma_buf *dmabuf = attach->dmabuf;
831
832 dma_resv_assert_held(dmabuf->resv);
833
834 if (dmabuf->ops->unpin)
835 dmabuf->ops->unpin(attach);
836}
837EXPORT_SYMBOL_GPL(dma_buf_unpin);
838
839/**
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530840 * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
841 * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
842 * dma_buf_ops.
843 * @attach: [in] attachment whose scatterlist is to be returned
844 * @direction: [in] direction of DMA transfer
845 *
Colin Crossfee0c542013-12-20 16:43:50 -0800846 * Returns sg_table containing the scatterlist to be returned; returns ERR_PTR
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100847 * on error. May return -EINTR if it is interrupted by a signal.
848 *
Liviu Dudauc1387822017-11-01 14:06:30 +0000849 * A mapping must be unmapped by using dma_buf_unmap_attachment(). Note that
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100850 * the underlying backing storage is pinned for as long as a mapping exists,
851 * therefore users/importers should not hold onto a mapping for undue amounts of
852 * time.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530853 */
854struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
855 enum dma_data_direction direction)
856{
Colin Ian King531beb02017-09-15 00:05:16 +0100857 struct sg_table *sg_table;
Christian Königbb42df42018-07-03 16:42:26 +0200858 int r;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530859
860 might_sleep();
861
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100862 if (WARN_ON(!attach || !attach->dmabuf))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530863 return ERR_PTR(-EINVAL);
864
Christian König15fd5522018-07-03 16:42:26 +0200865 if (dma_buf_attachment_is_dynamic(attach))
866 dma_resv_assert_held(attach->dmabuf->resv);
867
Christian Königf13e1432018-07-03 16:42:26 +0200868 if (attach->sgt) {
869 /*
870 * Two mappings with different directions for the same
871 * attachment are not allowed.
872 */
873 if (attach->dir != direction &&
874 attach->dir != DMA_BIDIRECTIONAL)
875 return ERR_PTR(-EBUSY);
876
877 return attach->sgt;
878 }
879
Christian Königbb42df42018-07-03 16:42:26 +0200880 if (dma_buf_is_dynamic(attach->dmabuf)) {
Christian König15fd5522018-07-03 16:42:26 +0200881 dma_resv_assert_held(attach->dmabuf->resv);
Christian König4981cdb2020-02-19 13:32:43 +0100882 if (!IS_ENABLED(CONFIG_DMABUF_MOVE_NOTIFY)) {
Christian Königbb42df42018-07-03 16:42:26 +0200883 r = dma_buf_pin(attach);
884 if (r)
885 return ERR_PTR(r);
886 }
887 }
Christian König15fd5522018-07-03 16:42:26 +0200888
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100889 sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
Colin Crossfee0c542013-12-20 16:43:50 -0800890 if (!sg_table)
891 sg_table = ERR_PTR(-ENOMEM);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530892
Christian Königbb42df42018-07-03 16:42:26 +0200893 if (IS_ERR(sg_table) && dma_buf_is_dynamic(attach->dmabuf) &&
Christian König4981cdb2020-02-19 13:32:43 +0100894 !IS_ENABLED(CONFIG_DMABUF_MOVE_NOTIFY))
Christian Königbb42df42018-07-03 16:42:26 +0200895 dma_buf_unpin(attach);
896
Christian Königf13e1432018-07-03 16:42:26 +0200897 if (!IS_ERR(sg_table) && attach->dmabuf->ops->cache_sgt_mapping) {
898 attach->sgt = sg_table;
899 attach->dir = direction;
900 }
901
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530902 return sg_table;
903}
904EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
905
906/**
907 * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
908 * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
909 * dma_buf_ops.
910 * @attach: [in] attachment to unmap buffer from
911 * @sg_table: [in] scatterlist info of the buffer to unmap
Sumit Semwal33ea2dc2012-01-27 15:09:27 +0530912 * @direction: [in] direction of DMA transfer
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530913 *
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100914 * This unmaps a DMA mapping for @attached obtained by dma_buf_map_attachment().
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530915 */
916void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
Sumit Semwal33ea2dc2012-01-27 15:09:27 +0530917 struct sg_table *sg_table,
918 enum dma_data_direction direction)
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530919{
Rob Clarkb6fa0cd2012-09-28 09:29:43 +0200920 might_sleep();
921
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100922 if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530923 return;
924
Christian König15fd5522018-07-03 16:42:26 +0200925 if (dma_buf_attachment_is_dynamic(attach))
926 dma_resv_assert_held(attach->dmabuf->resv);
927
Christian Königf13e1432018-07-03 16:42:26 +0200928 if (attach->sgt == sg_table)
929 return;
930
Christian König15fd5522018-07-03 16:42:26 +0200931 if (dma_buf_is_dynamic(attach->dmabuf))
932 dma_resv_assert_held(attach->dmabuf->resv);
933
Christian Königf13e1432018-07-03 16:42:26 +0200934 attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
Christian Königbb42df42018-07-03 16:42:26 +0200935
936 if (dma_buf_is_dynamic(attach->dmabuf) &&
Christian König4981cdb2020-02-19 13:32:43 +0100937 !IS_ENABLED(CONFIG_DMABUF_MOVE_NOTIFY))
Christian Königbb42df42018-07-03 16:42:26 +0200938 dma_buf_unpin(attach);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530939}
940EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
Daniel Vetterfc130202012-03-20 00:02:37 +0100941
Daniel Vetter0959a162016-12-09 19:53:08 +0100942/**
Christian Königbb42df42018-07-03 16:42:26 +0200943 * dma_buf_move_notify - notify attachments that DMA-buf is moving
944 *
945 * @dmabuf: [in] buffer which is moving
946 *
947 * Informs all attachmenst that they need to destroy and recreated all their
948 * mappings.
949 */
950void dma_buf_move_notify(struct dma_buf *dmabuf)
951{
952 struct dma_buf_attachment *attach;
953
954 dma_resv_assert_held(dmabuf->resv);
955
956 list_for_each_entry(attach, &dmabuf->attachments, node)
Christian König4981cdb2020-02-19 13:32:43 +0100957 if (attach->importer_ops)
Christian Königbb42df42018-07-03 16:42:26 +0200958 attach->importer_ops->move_notify(attach);
959}
960EXPORT_SYMBOL_GPL(dma_buf_move_notify);
961
962/**
Daniel Vetter0959a162016-12-09 19:53:08 +0100963 * DOC: cpu access
964 *
965 * There are mutliple reasons for supporting CPU access to a dma buffer object:
966 *
967 * - Fallback operations in the kernel, for example when a device is connected
968 * over USB and the kernel needs to shuffle the data around first before
969 * sending it away. Cache coherency is handled by braketing any transactions
970 * with calls to dma_buf_begin_cpu_access() and dma_buf_end_cpu_access()
971 * access.
972 *
Daniel Vetter7f0de8d2019-11-18 11:35:30 +0100973 * Since for most kernel internal dma-buf accesses need the entire buffer, a
974 * vmap interface is introduced. Note that on very old 32-bit architectures
975 * vmalloc space might be limited and result in vmap calls failing.
Daniel Vetter0959a162016-12-09 19:53:08 +0100976 *
977 * Interfaces::
978 * void \*dma_buf_vmap(struct dma_buf \*dmabuf)
979 * void dma_buf_vunmap(struct dma_buf \*dmabuf, void \*vaddr)
980 *
981 * The vmap call can fail if there is no vmap support in the exporter, or if
982 * it runs out of vmalloc space. Fallback to kmap should be implemented. Note
983 * that the dma-buf layer keeps a reference count for all vmap access and
984 * calls down into the exporter's vmap function only when no vmapping exists,
985 * and only unmaps it once. Protection against concurrent vmap/vunmap calls is
986 * provided by taking the dma_buf->lock mutex.
987 *
988 * - For full compatibility on the importer side with existing userspace
989 * interfaces, which might already support mmap'ing buffers. This is needed in
990 * many processing pipelines (e.g. feeding a software rendered image into a
991 * hardware pipeline, thumbnail creation, snapshots, ...). Also, Android's ION
992 * framework already supported this and for DMA buffer file descriptors to
993 * replace ION buffers mmap support was needed.
994 *
995 * There is no special interfaces, userspace simply calls mmap on the dma-buf
996 * fd. But like for CPU access there's a need to braket the actual access,
997 * which is handled by the ioctl (DMA_BUF_IOCTL_SYNC). Note that
998 * DMA_BUF_IOCTL_SYNC can fail with -EAGAIN or -EINTR, in which case it must
999 * be restarted.
1000 *
1001 * Some systems might need some sort of cache coherency management e.g. when
1002 * CPU and GPU domains are being accessed through dma-buf at the same time.
1003 * To circumvent this problem there are begin/end coherency markers, that
1004 * forward directly to existing dma-buf device drivers vfunc hooks. Userspace
1005 * can make use of those markers through the DMA_BUF_IOCTL_SYNC ioctl. The
1006 * sequence would be used like following:
1007 *
1008 * - mmap dma-buf fd
1009 * - for each drawing/upload cycle in CPU 1. SYNC_START ioctl, 2. read/write
1010 * to mmap area 3. SYNC_END ioctl. This can be repeated as often as you
1011 * want (with the new data being consumed by say the GPU or the scanout
1012 * device)
1013 * - munmap once you don't need the buffer any more
1014 *
1015 * For correctness and optimal performance, it is always required to use
1016 * SYNC_START and SYNC_END before and after, respectively, when accessing the
1017 * mapped address. Userspace cannot rely on coherent access, even when there
1018 * are systems where it just works without calling these ioctls.
1019 *
1020 * - And as a CPU fallback in userspace processing pipelines.
1021 *
1022 * Similar to the motivation for kernel cpu access it is again important that
1023 * the userspace code of a given importing subsystem can use the same
1024 * interfaces with a imported dma-buf buffer object as with a native buffer
1025 * object. This is especially important for drm where the userspace part of
1026 * contemporary OpenGL, X, and other drivers is huge, and reworking them to
1027 * use a different way to mmap a buffer rather invasive.
1028 *
1029 * The assumption in the current dma-buf interfaces is that redirecting the
1030 * initial mmap is all that's needed. A survey of some of the existing
1031 * subsystems shows that no driver seems to do any nefarious thing like
1032 * syncing up with outstanding asynchronous processing on the device or
1033 * allocating special resources at fault time. So hopefully this is good
1034 * enough, since adding interfaces to intercept pagefaults and allow pte
1035 * shootdowns would increase the complexity quite a bit.
1036 *
1037 * Interface::
1038 * int dma_buf_mmap(struct dma_buf \*, struct vm_area_struct \*,
1039 * unsigned long);
1040 *
1041 * If the importing subsystem simply provides a special-purpose mmap call to
1042 * set up a mapping in userspace, calling do_mmap with dma_buf->file will
1043 * equally achieve that for a dma-buf object.
1044 */
1045
Chris Wilsonae4e46b2016-08-15 16:42:18 +01001046static int __dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
1047 enum dma_data_direction direction)
1048{
1049 bool write = (direction == DMA_BIDIRECTIONAL ||
1050 direction == DMA_TO_DEVICE);
Christian König52791ee2019-08-11 10:06:32 +02001051 struct dma_resv *resv = dmabuf->resv;
Chris Wilsonae4e46b2016-08-15 16:42:18 +01001052 long ret;
1053
1054 /* Wait on any implicit rendering fences */
Christian König52791ee2019-08-11 10:06:32 +02001055 ret = dma_resv_wait_timeout_rcu(resv, write, true,
Chris Wilsonae4e46b2016-08-15 16:42:18 +01001056 MAX_SCHEDULE_TIMEOUT);
1057 if (ret < 0)
1058 return ret;
1059
1060 return 0;
1061}
Daniel Vetterfc130202012-03-20 00:02:37 +01001062
1063/**
1064 * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
1065 * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
1066 * preparations. Coherency is only guaranteed in the specified range for the
1067 * specified access direction.
Randy Dunlapefb4df822012-04-17 17:03:30 -07001068 * @dmabuf: [in] buffer to prepare cpu access for.
Daniel Vetterfc130202012-03-20 00:02:37 +01001069 * @direction: [in] length of range for cpu access.
1070 *
Daniel Vetter0959a162016-12-09 19:53:08 +01001071 * After the cpu access is complete the caller should call
1072 * dma_buf_end_cpu_access(). Only when cpu access is braketed by both calls is
1073 * it guaranteed to be coherent with other DMA access.
1074 *
Daniel Vetterfc130202012-03-20 00:02:37 +01001075 * Can return negative error values, returns 0 on success.
1076 */
Tiago Vignatti831e9da2015-12-22 19:36:45 -02001077int dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
Daniel Vetterfc130202012-03-20 00:02:37 +01001078 enum dma_data_direction direction)
1079{
1080 int ret = 0;
1081
1082 if (WARN_ON(!dmabuf))
1083 return -EINVAL;
1084
1085 if (dmabuf->ops->begin_cpu_access)
Tiago Vignatti831e9da2015-12-22 19:36:45 -02001086 ret = dmabuf->ops->begin_cpu_access(dmabuf, direction);
Daniel Vetterfc130202012-03-20 00:02:37 +01001087
Chris Wilsonae4e46b2016-08-15 16:42:18 +01001088 /* Ensure that all fences are waited upon - but we first allow
1089 * the native handler the chance to do so more efficiently if it
1090 * chooses. A double invocation here will be reasonably cheap no-op.
1091 */
1092 if (ret == 0)
1093 ret = __dma_buf_begin_cpu_access(dmabuf, direction);
1094
Daniel Vetterfc130202012-03-20 00:02:37 +01001095 return ret;
1096}
1097EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access);
1098
1099/**
1100 * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
1101 * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
1102 * actions. Coherency is only guaranteed in the specified range for the
1103 * specified access direction.
Randy Dunlapefb4df822012-04-17 17:03:30 -07001104 * @dmabuf: [in] buffer to complete cpu access for.
Daniel Vetterfc130202012-03-20 00:02:37 +01001105 * @direction: [in] length of range for cpu access.
1106 *
Daniel Vetter0959a162016-12-09 19:53:08 +01001107 * This terminates CPU access started with dma_buf_begin_cpu_access().
1108 *
Daniel Vetter87e332d2016-03-21 08:24:22 +01001109 * Can return negative error values, returns 0 on success.
Daniel Vetterfc130202012-03-20 00:02:37 +01001110 */
Chris Wilson18b862d2016-03-18 20:02:39 +00001111int dma_buf_end_cpu_access(struct dma_buf *dmabuf,
1112 enum dma_data_direction direction)
Daniel Vetterfc130202012-03-20 00:02:37 +01001113{
Chris Wilson18b862d2016-03-18 20:02:39 +00001114 int ret = 0;
1115
Daniel Vetterfc130202012-03-20 00:02:37 +01001116 WARN_ON(!dmabuf);
1117
1118 if (dmabuf->ops->end_cpu_access)
Chris Wilson18b862d2016-03-18 20:02:39 +00001119 ret = dmabuf->ops->end_cpu_access(dmabuf, direction);
1120
1121 return ret;
Daniel Vetterfc130202012-03-20 00:02:37 +01001122}
1123EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access);
1124
Daniel Vetter4c785132012-04-24 14:38:52 +05301125
1126/**
1127 * dma_buf_mmap - Setup up a userspace mmap with the given vma
Sumit Semwal12c47272012-05-23 15:27:40 +05301128 * @dmabuf: [in] buffer that should back the vma
Daniel Vetter4c785132012-04-24 14:38:52 +05301129 * @vma: [in] vma for the mmap
1130 * @pgoff: [in] offset in pages where this mmap should start within the
Jagan Teki51366292015-05-21 01:09:31 +05301131 * dma-buf buffer.
Daniel Vetter4c785132012-04-24 14:38:52 +05301132 *
1133 * This function adjusts the passed in vma so that it points at the file of the
Javier Martinez Canillasecf1dba2014-04-10 01:30:05 +02001134 * dma_buf operation. It also adjusts the starting pgoff and does bounds
Daniel Vetter4c785132012-04-24 14:38:52 +05301135 * checking on the size of the vma. Then it calls the exporters mmap function to
1136 * set up the mapping.
1137 *
1138 * Can return negative error values, returns 0 on success.
1139 */
1140int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
1141 unsigned long pgoff)
1142{
John Sheu495c10c2013-02-11 17:50:24 -08001143 struct file *oldfile;
1144 int ret;
1145
Daniel Vetter4c785132012-04-24 14:38:52 +05301146 if (WARN_ON(!dmabuf || !vma))
1147 return -EINVAL;
1148
Andrew F. Davise3a9d6c2019-03-29 11:52:01 -05001149 /* check if buffer supports mmap */
1150 if (!dmabuf->ops->mmap)
1151 return -EINVAL;
1152
Daniel Vetter4c785132012-04-24 14:38:52 +05301153 /* check for offset overflow */
Muhammad Falak R Wanib02da6f2016-05-23 17:08:42 +05301154 if (pgoff + vma_pages(vma) < pgoff)
Daniel Vetter4c785132012-04-24 14:38:52 +05301155 return -EOVERFLOW;
1156
1157 /* check for overflowing the buffer's size */
Muhammad Falak R Wanib02da6f2016-05-23 17:08:42 +05301158 if (pgoff + vma_pages(vma) >
Daniel Vetter4c785132012-04-24 14:38:52 +05301159 dmabuf->size >> PAGE_SHIFT)
1160 return -EINVAL;
1161
1162 /* readjust the vma */
John Sheu495c10c2013-02-11 17:50:24 -08001163 get_file(dmabuf->file);
1164 oldfile = vma->vm_file;
1165 vma->vm_file = dmabuf->file;
Daniel Vetter4c785132012-04-24 14:38:52 +05301166 vma->vm_pgoff = pgoff;
1167
John Sheu495c10c2013-02-11 17:50:24 -08001168 ret = dmabuf->ops->mmap(dmabuf, vma);
1169 if (ret) {
1170 /* restore old parameters on failure */
1171 vma->vm_file = oldfile;
1172 fput(dmabuf->file);
1173 } else {
1174 if (oldfile)
1175 fput(oldfile);
1176 }
1177 return ret;
1178
Daniel Vetter4c785132012-04-24 14:38:52 +05301179}
1180EXPORT_SYMBOL_GPL(dma_buf_mmap);
Dave Airlie98f86c92012-05-20 12:33:56 +05301181
1182/**
Sumit Semwal12c47272012-05-23 15:27:40 +05301183 * dma_buf_vmap - Create virtual mapping for the buffer object into kernel
1184 * address space. Same restrictions as for vmap and friends apply.
1185 * @dmabuf: [in] buffer to vmap
Dave Airlie98f86c92012-05-20 12:33:56 +05301186 *
1187 * This call may fail due to lack of virtual mapping address space.
1188 * These calls are optional in drivers. The intended use for them
1189 * is for mapping objects linear in kernel space for high use objects.
1190 * Please attempt to use kmap/kunmap before thinking about these interfaces.
Colin Crossfee0c542013-12-20 16:43:50 -08001191 *
1192 * Returns NULL on error.
Dave Airlie98f86c92012-05-20 12:33:56 +05301193 */
1194void *dma_buf_vmap(struct dma_buf *dmabuf)
1195{
Daniel Vetterf00b4da2012-12-20 14:14:23 +01001196 void *ptr;
1197
Dave Airlie98f86c92012-05-20 12:33:56 +05301198 if (WARN_ON(!dmabuf))
1199 return NULL;
1200
Daniel Vetterf00b4da2012-12-20 14:14:23 +01001201 if (!dmabuf->ops->vmap)
1202 return NULL;
1203
1204 mutex_lock(&dmabuf->lock);
1205 if (dmabuf->vmapping_counter) {
1206 dmabuf->vmapping_counter++;
1207 BUG_ON(!dmabuf->vmap_ptr);
1208 ptr = dmabuf->vmap_ptr;
1209 goto out_unlock;
1210 }
1211
1212 BUG_ON(dmabuf->vmap_ptr);
1213
1214 ptr = dmabuf->ops->vmap(dmabuf);
Colin Crossfee0c542013-12-20 16:43:50 -08001215 if (WARN_ON_ONCE(IS_ERR(ptr)))
1216 ptr = NULL;
1217 if (!ptr)
Daniel Vetterf00b4da2012-12-20 14:14:23 +01001218 goto out_unlock;
1219
1220 dmabuf->vmap_ptr = ptr;
1221 dmabuf->vmapping_counter = 1;
1222
1223out_unlock:
1224 mutex_unlock(&dmabuf->lock);
1225 return ptr;
Dave Airlie98f86c92012-05-20 12:33:56 +05301226}
1227EXPORT_SYMBOL_GPL(dma_buf_vmap);
1228
1229/**
1230 * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
Sumit Semwal12c47272012-05-23 15:27:40 +05301231 * @dmabuf: [in] buffer to vunmap
Randy Dunlap6e7b4a52012-06-09 15:02:59 -07001232 * @vaddr: [in] vmap to vunmap
Dave Airlie98f86c92012-05-20 12:33:56 +05301233 */
1234void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
1235{
1236 if (WARN_ON(!dmabuf))
1237 return;
1238
Daniel Vetterf00b4da2012-12-20 14:14:23 +01001239 BUG_ON(!dmabuf->vmap_ptr);
1240 BUG_ON(dmabuf->vmapping_counter == 0);
1241 BUG_ON(dmabuf->vmap_ptr != vaddr);
1242
1243 mutex_lock(&dmabuf->lock);
1244 if (--dmabuf->vmapping_counter == 0) {
1245 if (dmabuf->ops->vunmap)
1246 dmabuf->ops->vunmap(dmabuf, vaddr);
1247 dmabuf->vmap_ptr = NULL;
1248 }
1249 mutex_unlock(&dmabuf->lock);
Dave Airlie98f86c92012-05-20 12:33:56 +05301250}
1251EXPORT_SYMBOL_GPL(dma_buf_vunmap);
Sumit Semwalb89e35632013-04-04 11:44:37 +05301252
1253#ifdef CONFIG_DEBUG_FS
Mathias Krauseeb0b9472016-06-19 14:31:29 +02001254static int dma_buf_debug_show(struct seq_file *s, void *unused)
Sumit Semwalb89e35632013-04-04 11:44:37 +05301255{
1256 int ret;
1257 struct dma_buf *buf_obj;
1258 struct dma_buf_attachment *attach_obj;
Christian König52791ee2019-08-11 10:06:32 +02001259 struct dma_resv *robj;
1260 struct dma_resv_list *fobj;
Russell King5eb2c722017-03-31 11:00:42 +01001261 struct dma_fence *fence;
Chris Wilsonb016cd62019-08-14 19:24:01 +01001262 unsigned seq;
Russell King5eb2c722017-03-31 11:00:42 +01001263 int count = 0, attach_count, shared_count, i;
Sumit Semwalb89e35632013-04-04 11:44:37 +05301264 size_t size = 0;
1265
1266 ret = mutex_lock_interruptible(&db_list.lock);
1267
1268 if (ret)
1269 return ret;
1270
Sumit Semwalc0b00a52014-02-03 15:09:12 +05301271 seq_puts(s, "\nDma-buf Objects:\n");
Greg Hackmanned63bb12019-06-13 15:34:06 -07001272 seq_printf(s, "%-8s\t%-8s\t%-8s\t%-8s\texp_name\t%-8s\n",
1273 "size", "flags", "mode", "count", "ino");
Sumit Semwalb89e35632013-04-04 11:44:37 +05301274
1275 list_for_each_entry(buf_obj, &db_list.head, list_node) {
Sumit Semwalb89e35632013-04-04 11:44:37 +05301276
Christian König15fd5522018-07-03 16:42:26 +02001277 ret = dma_resv_lock_interruptible(buf_obj->resv, NULL);
1278 if (ret)
Christian Königf45f57c2019-10-18 16:30:19 +02001279 goto error_unlock;
Sumit Semwalb89e35632013-04-04 11:44:37 +05301280
Greg Hackmannbb2bb902019-06-13 15:34:07 -07001281 seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\t%08lu\t%s\n",
Sumit Semwalc0b00a52014-02-03 15:09:12 +05301282 buf_obj->size,
Sumit Semwalb89e35632013-04-04 11:44:37 +05301283 buf_obj->file->f_flags, buf_obj->file->f_mode,
Al Viroa1f6dba2014-08-20 11:05:50 -04001284 file_count(buf_obj->file),
Greg Hackmanned63bb12019-06-13 15:34:06 -07001285 buf_obj->exp_name,
Greg Hackmannbb2bb902019-06-13 15:34:07 -07001286 file_inode(buf_obj->file)->i_ino,
1287 buf_obj->name ?: "");
Sumit Semwalb89e35632013-04-04 11:44:37 +05301288
Russell King5eb2c722017-03-31 11:00:42 +01001289 robj = buf_obj->resv;
Chris Wilsonb016cd62019-08-14 19:24:01 +01001290 while (true) {
1291 seq = read_seqcount_begin(&robj->seq);
1292 rcu_read_lock();
1293 fobj = rcu_dereference(robj->fence);
1294 shared_count = fobj ? fobj->shared_count : 0;
1295 fence = rcu_dereference(robj->fence_excl);
1296 if (!read_seqcount_retry(&robj->seq, seq))
1297 break;
1298 rcu_read_unlock();
1299 }
Russell King5eb2c722017-03-31 11:00:42 +01001300
1301 if (fence)
1302 seq_printf(s, "\tExclusive fence: %s %s %ssignalled\n",
1303 fence->ops->get_driver_name(fence),
1304 fence->ops->get_timeline_name(fence),
1305 dma_fence_is_signaled(fence) ? "" : "un");
1306 for (i = 0; i < shared_count; i++) {
1307 fence = rcu_dereference(fobj->shared[i]);
1308 if (!dma_fence_get_rcu(fence))
1309 continue;
1310 seq_printf(s, "\tShared fence: %s %s %ssignalled\n",
1311 fence->ops->get_driver_name(fence),
1312 fence->ops->get_timeline_name(fence),
1313 dma_fence_is_signaled(fence) ? "" : "un");
Jérôme Glisse5e383a92018-12-06 11:18:40 -05001314 dma_fence_put(fence);
Russell King5eb2c722017-03-31 11:00:42 +01001315 }
1316 rcu_read_unlock();
1317
Sumit Semwalc0b00a52014-02-03 15:09:12 +05301318 seq_puts(s, "\tAttached Devices:\n");
Sumit Semwalb89e35632013-04-04 11:44:37 +05301319 attach_count = 0;
1320
1321 list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
Markus Elfring9eddb412017-05-08 10:32:44 +02001322 seq_printf(s, "\t%s\n", dev_name(attach_obj->dev));
Sumit Semwalb89e35632013-04-04 11:44:37 +05301323 attach_count++;
1324 }
Christian König15fd5522018-07-03 16:42:26 +02001325 dma_resv_unlock(buf_obj->resv);
Sumit Semwalb89e35632013-04-04 11:44:37 +05301326
Sumit Semwalc0b00a52014-02-03 15:09:12 +05301327 seq_printf(s, "Total %d devices attached\n\n",
Sumit Semwalb89e35632013-04-04 11:44:37 +05301328 attach_count);
1329
1330 count++;
1331 size += buf_obj->size;
Sumit Semwalb89e35632013-04-04 11:44:37 +05301332 }
1333
1334 seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size);
1335
1336 mutex_unlock(&db_list.lock);
1337 return 0;
Christian König15fd5522018-07-03 16:42:26 +02001338
Christian Königf45f57c2019-10-18 16:30:19 +02001339error_unlock:
Christian König15fd5522018-07-03 16:42:26 +02001340 mutex_unlock(&db_list.lock);
1341 return ret;
Sumit Semwalb89e35632013-04-04 11:44:37 +05301342}
1343
Yangtao Li26743052018-11-30 11:11:01 -05001344DEFINE_SHOW_ATTRIBUTE(dma_buf_debug);
Sumit Semwalb89e35632013-04-04 11:44:37 +05301345
1346static struct dentry *dma_buf_debugfs_dir;
1347
1348static int dma_buf_init_debugfs(void)
1349{
Mathias Krausebd3e2202016-06-19 14:31:31 +02001350 struct dentry *d;
Sumit Semwalb89e35632013-04-04 11:44:37 +05301351 int err = 0;
Jagan Teki51366292015-05-21 01:09:31 +05301352
Mathias Krausebd3e2202016-06-19 14:31:31 +02001353 d = debugfs_create_dir("dma_buf", NULL);
1354 if (IS_ERR(d))
1355 return PTR_ERR(d);
Jagan Teki51366292015-05-21 01:09:31 +05301356
Mathias Krausebd3e2202016-06-19 14:31:31 +02001357 dma_buf_debugfs_dir = d;
Sumit Semwalb89e35632013-04-04 11:44:37 +05301358
Mathias Krausebd3e2202016-06-19 14:31:31 +02001359 d = debugfs_create_file("bufinfo", S_IRUGO, dma_buf_debugfs_dir,
1360 NULL, &dma_buf_debug_fops);
1361 if (IS_ERR(d)) {
Sumit Semwalb89e35632013-04-04 11:44:37 +05301362 pr_debug("dma_buf: debugfs: failed to create node bufinfo\n");
Mathias Krauseb7479992016-06-19 14:31:30 +02001363 debugfs_remove_recursive(dma_buf_debugfs_dir);
1364 dma_buf_debugfs_dir = NULL;
Mathias Krausebd3e2202016-06-19 14:31:31 +02001365 err = PTR_ERR(d);
Mathias Krauseb7479992016-06-19 14:31:30 +02001366 }
Sumit Semwalb89e35632013-04-04 11:44:37 +05301367
1368 return err;
1369}
1370
1371static void dma_buf_uninit_debugfs(void)
1372{
Vasyl Gomonovych298b6a82017-11-22 16:22:41 +01001373 debugfs_remove_recursive(dma_buf_debugfs_dir);
Sumit Semwalb89e35632013-04-04 11:44:37 +05301374}
Sumit Semwalb89e35632013-04-04 11:44:37 +05301375#else
1376static inline int dma_buf_init_debugfs(void)
1377{
1378 return 0;
1379}
1380static inline void dma_buf_uninit_debugfs(void)
1381{
1382}
1383#endif
1384
1385static int __init dma_buf_init(void)
1386{
Greg Hackmanned63bb12019-06-13 15:34:06 -07001387 dma_buf_mnt = kern_mount(&dma_buf_fs_type);
1388 if (IS_ERR(dma_buf_mnt))
1389 return PTR_ERR(dma_buf_mnt);
1390
Sumit Semwalb89e35632013-04-04 11:44:37 +05301391 mutex_init(&db_list.lock);
1392 INIT_LIST_HEAD(&db_list.head);
1393 dma_buf_init_debugfs();
1394 return 0;
1395}
1396subsys_initcall(dma_buf_init);
1397
1398static void __exit dma_buf_deinit(void)
1399{
1400 dma_buf_uninit_debugfs();
Greg Hackmanned63bb12019-06-13 15:34:06 -07001401 kern_unmount(dma_buf_mnt);
Sumit Semwalb89e35632013-04-04 11:44:37 +05301402}
1403__exitcall(dma_buf_deinit);