xref: /OK3568_Linux_fs/kernel/drivers/dma-buf/dma-buf.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Framework for buffer objects that can be shared across devices/subsystems.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright(C) 2011 Linaro Limited. All rights reserved.
6*4882a593Smuzhiyun  * Author: Sumit Semwal <sumit.semwal@ti.com>
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Many thanks to linaro-mm-sig list, and specially
9*4882a593Smuzhiyun  * Arnd Bergmann <arnd@arndb.de>, Rob Clark <rob@ti.com> and
10*4882a593Smuzhiyun  * Daniel Vetter <daniel@ffwll.ch> for their support in creation and
11*4882a593Smuzhiyun  * refining of this idea.
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/fs.h>
15*4882a593Smuzhiyun #include <linux/slab.h>
16*4882a593Smuzhiyun #include <linux/dma-buf.h>
17*4882a593Smuzhiyun #include <linux/dma-fence.h>
18*4882a593Smuzhiyun #include <linux/anon_inodes.h>
19*4882a593Smuzhiyun #include <linux/export.h>
20*4882a593Smuzhiyun #include <linux/debugfs.h>
21*4882a593Smuzhiyun #include <linux/module.h>
22*4882a593Smuzhiyun #include <linux/seq_file.h>
23*4882a593Smuzhiyun #include <linux/poll.h>
24*4882a593Smuzhiyun #include <linux/dma-resv.h>
25*4882a593Smuzhiyun #include <linux/mm.h>
26*4882a593Smuzhiyun #include <linux/mount.h>
27*4882a593Smuzhiyun #include <linux/pseudo_fs.h>
28*4882a593Smuzhiyun #include <linux/sched/task.h>
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #include <uapi/linux/dma-buf.h>
31*4882a593Smuzhiyun #include <uapi/linux/magic.h>
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #include "dma-buf-sysfs-stats.h"
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun struct dma_buf_list {
36*4882a593Smuzhiyun 	struct list_head head;
37*4882a593Smuzhiyun 	struct mutex lock;
38*4882a593Smuzhiyun };
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun static struct dma_buf_list db_list;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /*
43*4882a593Smuzhiyun  * This function helps in traversing the db_list and calls the
44*4882a593Smuzhiyun  * callback function which can extract required info out of each
45*4882a593Smuzhiyun  * dmabuf.
46*4882a593Smuzhiyun  */
get_each_dmabuf(int (* callback)(const struct dma_buf * dmabuf,void * private),void * private)47*4882a593Smuzhiyun int get_each_dmabuf(int (*callback)(const struct dma_buf *dmabuf,
48*4882a593Smuzhiyun 		    void *private), void *private)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun 	struct dma_buf *buf;
51*4882a593Smuzhiyun 	int ret = mutex_lock_interruptible(&db_list.lock);
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	if (ret)
54*4882a593Smuzhiyun 		return ret;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	list_for_each_entry(buf, &db_list.head, list_node) {
57*4882a593Smuzhiyun 		ret = callback(buf, private);
58*4882a593Smuzhiyun 		if (ret)
59*4882a593Smuzhiyun 			break;
60*4882a593Smuzhiyun 	}
61*4882a593Smuzhiyun 	mutex_unlock(&db_list.lock);
62*4882a593Smuzhiyun 	return ret;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(get_each_dmabuf);
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_RK_DMABUF_DEBUG)
67*4882a593Smuzhiyun static size_t db_total_size;
68*4882a593Smuzhiyun static size_t db_peak_size;
69*4882a593Smuzhiyun 
dma_buf_reset_peak_size(void)70*4882a593Smuzhiyun void dma_buf_reset_peak_size(void)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	mutex_lock(&db_list.lock);
73*4882a593Smuzhiyun 	db_peak_size = 0;
74*4882a593Smuzhiyun 	mutex_unlock(&db_list.lock);
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_reset_peak_size);
77*4882a593Smuzhiyun 
dma_buf_get_peak_size(void)78*4882a593Smuzhiyun size_t dma_buf_get_peak_size(void)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	size_t sz;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	mutex_lock(&db_list.lock);
83*4882a593Smuzhiyun 	sz = db_peak_size;
84*4882a593Smuzhiyun 	mutex_unlock(&db_list.lock);
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	return sz;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_get_peak_size);
89*4882a593Smuzhiyun 
dma_buf_get_total_size(void)90*4882a593Smuzhiyun size_t dma_buf_get_total_size(void)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	size_t sz;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	mutex_lock(&db_list.lock);
95*4882a593Smuzhiyun 	sz = db_total_size;
96*4882a593Smuzhiyun 	mutex_unlock(&db_list.lock);
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	return sz;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_get_total_size);
101*4882a593Smuzhiyun #endif
102*4882a593Smuzhiyun 
dmabuffs_dname(struct dentry * dentry,char * buffer,int buflen)103*4882a593Smuzhiyun static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun 	struct dma_buf *dmabuf;
106*4882a593Smuzhiyun 	char name[DMA_BUF_NAME_LEN];
107*4882a593Smuzhiyun 	size_t ret = 0;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	dmabuf = dentry->d_fsdata;
110*4882a593Smuzhiyun 	spin_lock(&dmabuf->name_lock);
111*4882a593Smuzhiyun 	if (dmabuf->name)
112*4882a593Smuzhiyun 		ret = strlcpy(name, dmabuf->name, DMA_BUF_NAME_LEN);
113*4882a593Smuzhiyun 	spin_unlock(&dmabuf->name_lock);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	return dynamic_dname(dentry, buffer, buflen, "/%s:%s",
116*4882a593Smuzhiyun 			     dentry->d_name.name, ret > 0 ? name : "");
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
dma_buf_release(struct dentry * dentry)119*4882a593Smuzhiyun static void dma_buf_release(struct dentry *dentry)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun 	struct dma_buf *dmabuf;
122*4882a593Smuzhiyun #ifdef CONFIG_DMABUF_CACHE
123*4882a593Smuzhiyun 	int dtor_ret = 0;
124*4882a593Smuzhiyun #endif
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	dmabuf = dentry->d_fsdata;
127*4882a593Smuzhiyun 	if (unlikely(!dmabuf))
128*4882a593Smuzhiyun 		return;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	BUG_ON(dmabuf->vmapping_counter);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	/*
133*4882a593Smuzhiyun 	 * Any fences that a dma-buf poll can wait on should be signaled
134*4882a593Smuzhiyun 	 * before releasing dma-buf. This is the responsibility of each
135*4882a593Smuzhiyun 	 * driver that uses the reservation objects.
136*4882a593Smuzhiyun 	 *
137*4882a593Smuzhiyun 	 * If you hit this BUG() it means someone dropped their ref to the
138*4882a593Smuzhiyun 	 * dma-buf while still having pending operation to the buffer.
139*4882a593Smuzhiyun 	 */
140*4882a593Smuzhiyun 	BUG_ON(dmabuf->cb_shared.active || dmabuf->cb_excl.active);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	dma_buf_stats_teardown(dmabuf);
143*4882a593Smuzhiyun #ifdef CONFIG_DMABUF_CACHE
144*4882a593Smuzhiyun 	if (dmabuf->dtor)
145*4882a593Smuzhiyun 		dtor_ret = dmabuf->dtor(dmabuf, dmabuf->dtor_data);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	if (!dtor_ret)
148*4882a593Smuzhiyun #endif
149*4882a593Smuzhiyun 		dmabuf->ops->release(dmabuf);
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	if (dmabuf->resv == (struct dma_resv *)&dmabuf[1])
152*4882a593Smuzhiyun 		dma_resv_fini(dmabuf->resv);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	WARN_ON(!list_empty(&dmabuf->attachments));
155*4882a593Smuzhiyun 	module_put(dmabuf->owner);
156*4882a593Smuzhiyun 	kfree(dmabuf->name);
157*4882a593Smuzhiyun 	kfree(dmabuf);
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun 
dma_buf_file_release(struct inode * inode,struct file * file)160*4882a593Smuzhiyun static int dma_buf_file_release(struct inode *inode, struct file *file)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun 	struct dma_buf *dmabuf;
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	if (!is_dma_buf_file(file))
165*4882a593Smuzhiyun 		return -EINVAL;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	dmabuf = file->private_data;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	mutex_lock(&db_list.lock);
170*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_RK_DMABUF_DEBUG)
171*4882a593Smuzhiyun 	db_total_size -= dmabuf->size;
172*4882a593Smuzhiyun #endif
173*4882a593Smuzhiyun 	list_del(&dmabuf->list_node);
174*4882a593Smuzhiyun 	mutex_unlock(&db_list.lock);
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	return 0;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun static const struct dentry_operations dma_buf_dentry_ops = {
180*4882a593Smuzhiyun 	.d_dname = dmabuffs_dname,
181*4882a593Smuzhiyun 	.d_release = dma_buf_release,
182*4882a593Smuzhiyun };
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun static struct vfsmount *dma_buf_mnt;
185*4882a593Smuzhiyun 
dma_buf_fs_init_context(struct fs_context * fc)186*4882a593Smuzhiyun static int dma_buf_fs_init_context(struct fs_context *fc)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun 	struct pseudo_fs_context *ctx;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	ctx = init_pseudo(fc, DMA_BUF_MAGIC);
191*4882a593Smuzhiyun 	if (!ctx)
192*4882a593Smuzhiyun 		return -ENOMEM;
193*4882a593Smuzhiyun 	ctx->dops = &dma_buf_dentry_ops;
194*4882a593Smuzhiyun 	return 0;
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun static struct file_system_type dma_buf_fs_type = {
198*4882a593Smuzhiyun 	.name = "dmabuf",
199*4882a593Smuzhiyun 	.init_fs_context = dma_buf_fs_init_context,
200*4882a593Smuzhiyun 	.kill_sb = kill_anon_super,
201*4882a593Smuzhiyun };
202*4882a593Smuzhiyun 
dma_buf_mmap_internal(struct file * file,struct vm_area_struct * vma)203*4882a593Smuzhiyun static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun 	struct dma_buf *dmabuf;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	if (!is_dma_buf_file(file))
208*4882a593Smuzhiyun 		return -EINVAL;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	dmabuf = file->private_data;
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	/* check if buffer supports mmap */
213*4882a593Smuzhiyun 	if (!dmabuf->ops->mmap)
214*4882a593Smuzhiyun 		return -EINVAL;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	/* check for overflowing the buffer's size */
217*4882a593Smuzhiyun 	if (vma->vm_pgoff + vma_pages(vma) >
218*4882a593Smuzhiyun 	    dmabuf->size >> PAGE_SHIFT)
219*4882a593Smuzhiyun 		return -EINVAL;
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	return dmabuf->ops->mmap(dmabuf, vma);
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun 
dma_buf_llseek(struct file * file,loff_t offset,int whence)224*4882a593Smuzhiyun static loff_t dma_buf_llseek(struct file *file, loff_t offset, int whence)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun 	struct dma_buf *dmabuf;
227*4882a593Smuzhiyun 	loff_t base;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	if (!is_dma_buf_file(file))
230*4882a593Smuzhiyun 		return -EBADF;
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	dmabuf = file->private_data;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	/* only support discovering the end of the buffer,
235*4882a593Smuzhiyun 	   but also allow SEEK_SET to maintain the idiomatic
236*4882a593Smuzhiyun 	   SEEK_END(0), SEEK_CUR(0) pattern */
237*4882a593Smuzhiyun 	if (whence == SEEK_END)
238*4882a593Smuzhiyun 		base = dmabuf->size;
239*4882a593Smuzhiyun 	else if (whence == SEEK_SET)
240*4882a593Smuzhiyun 		base = 0;
241*4882a593Smuzhiyun 	else
242*4882a593Smuzhiyun 		return -EINVAL;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	if (offset != 0)
245*4882a593Smuzhiyun 		return -EINVAL;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	return base + offset;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun /**
251*4882a593Smuzhiyun  * DOC: implicit fence polling
252*4882a593Smuzhiyun  *
253*4882a593Smuzhiyun  * To support cross-device and cross-driver synchronization of buffer access
254*4882a593Smuzhiyun  * implicit fences (represented internally in the kernel with &struct dma_fence)
255*4882a593Smuzhiyun  * can be attached to a &dma_buf. The glue for that and a few related things are
256*4882a593Smuzhiyun  * provided in the &dma_resv structure.
257*4882a593Smuzhiyun  *
258*4882a593Smuzhiyun  * Userspace can query the state of these implicitly tracked fences using poll()
259*4882a593Smuzhiyun  * and related system calls:
260*4882a593Smuzhiyun  *
261*4882a593Smuzhiyun  * - Checking for EPOLLIN, i.e. read access, can be use to query the state of the
262*4882a593Smuzhiyun  *   most recent write or exclusive fence.
263*4882a593Smuzhiyun  *
264*4882a593Smuzhiyun  * - Checking for EPOLLOUT, i.e. write access, can be used to query the state of
265*4882a593Smuzhiyun  *   all attached fences, shared and exclusive ones.
266*4882a593Smuzhiyun  *
267*4882a593Smuzhiyun  * Note that this only signals the completion of the respective fences, i.e. the
268*4882a593Smuzhiyun  * DMA transfers are complete. Cache flushing and any other necessary
269*4882a593Smuzhiyun  * preparations before CPU access can begin still need to happen.
270*4882a593Smuzhiyun  */
271*4882a593Smuzhiyun 
dma_buf_poll_cb(struct dma_fence * fence,struct dma_fence_cb * cb)272*4882a593Smuzhiyun static void dma_buf_poll_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun 	struct dma_buf_poll_cb_t *dcb = (struct dma_buf_poll_cb_t *)cb;
275*4882a593Smuzhiyun 	unsigned long flags;
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	spin_lock_irqsave(&dcb->poll->lock, flags);
278*4882a593Smuzhiyun 	wake_up_locked_poll(dcb->poll, dcb->active);
279*4882a593Smuzhiyun 	dcb->active = 0;
280*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dcb->poll->lock, flags);
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun 
dma_buf_poll(struct file * file,poll_table * poll)283*4882a593Smuzhiyun static __poll_t dma_buf_poll(struct file *file, poll_table *poll)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun 	struct dma_buf *dmabuf;
286*4882a593Smuzhiyun 	struct dma_resv *resv;
287*4882a593Smuzhiyun 	struct dma_resv_list *fobj;
288*4882a593Smuzhiyun 	struct dma_fence *fence_excl;
289*4882a593Smuzhiyun 	__poll_t events;
290*4882a593Smuzhiyun 	unsigned shared_count, seq;
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	dmabuf = file->private_data;
293*4882a593Smuzhiyun 	if (!dmabuf || !dmabuf->resv)
294*4882a593Smuzhiyun 		return EPOLLERR;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	resv = dmabuf->resv;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	poll_wait(file, &dmabuf->poll, poll);
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	events = poll_requested_events(poll) & (EPOLLIN | EPOLLOUT);
301*4882a593Smuzhiyun 	if (!events)
302*4882a593Smuzhiyun 		return 0;
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun retry:
305*4882a593Smuzhiyun 	seq = read_seqcount_begin(&resv->seq);
306*4882a593Smuzhiyun 	rcu_read_lock();
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	fobj = rcu_dereference(resv->fence);
309*4882a593Smuzhiyun 	if (fobj)
310*4882a593Smuzhiyun 		shared_count = fobj->shared_count;
311*4882a593Smuzhiyun 	else
312*4882a593Smuzhiyun 		shared_count = 0;
313*4882a593Smuzhiyun 	fence_excl = rcu_dereference(resv->fence_excl);
314*4882a593Smuzhiyun 	if (read_seqcount_retry(&resv->seq, seq)) {
315*4882a593Smuzhiyun 		rcu_read_unlock();
316*4882a593Smuzhiyun 		goto retry;
317*4882a593Smuzhiyun 	}
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	if (fence_excl && (!(events & EPOLLOUT) || shared_count == 0)) {
320*4882a593Smuzhiyun 		struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_excl;
321*4882a593Smuzhiyun 		__poll_t pevents = EPOLLIN;
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 		if (shared_count == 0)
324*4882a593Smuzhiyun 			pevents |= EPOLLOUT;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 		spin_lock_irq(&dmabuf->poll.lock);
327*4882a593Smuzhiyun 		if (dcb->active) {
328*4882a593Smuzhiyun 			dcb->active |= pevents;
329*4882a593Smuzhiyun 			events &= ~pevents;
330*4882a593Smuzhiyun 		} else
331*4882a593Smuzhiyun 			dcb->active = pevents;
332*4882a593Smuzhiyun 		spin_unlock_irq(&dmabuf->poll.lock);
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 		if (events & pevents) {
335*4882a593Smuzhiyun 			if (!dma_fence_get_rcu(fence_excl)) {
336*4882a593Smuzhiyun 				/* force a recheck */
337*4882a593Smuzhiyun 				events &= ~pevents;
338*4882a593Smuzhiyun 				dma_buf_poll_cb(NULL, &dcb->cb);
339*4882a593Smuzhiyun 			} else if (!dma_fence_add_callback(fence_excl, &dcb->cb,
340*4882a593Smuzhiyun 							   dma_buf_poll_cb)) {
341*4882a593Smuzhiyun 				events &= ~pevents;
342*4882a593Smuzhiyun 				dma_fence_put(fence_excl);
343*4882a593Smuzhiyun 			} else {
344*4882a593Smuzhiyun 				/*
345*4882a593Smuzhiyun 				 * No callback queued, wake up any additional
346*4882a593Smuzhiyun 				 * waiters.
347*4882a593Smuzhiyun 				 */
348*4882a593Smuzhiyun 				dma_fence_put(fence_excl);
349*4882a593Smuzhiyun 				dma_buf_poll_cb(NULL, &dcb->cb);
350*4882a593Smuzhiyun 			}
351*4882a593Smuzhiyun 		}
352*4882a593Smuzhiyun 	}
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	if ((events & EPOLLOUT) && shared_count > 0) {
355*4882a593Smuzhiyun 		struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_shared;
356*4882a593Smuzhiyun 		int i;
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 		/* Only queue a new callback if no event has fired yet */
359*4882a593Smuzhiyun 		spin_lock_irq(&dmabuf->poll.lock);
360*4882a593Smuzhiyun 		if (dcb->active)
361*4882a593Smuzhiyun 			events &= ~EPOLLOUT;
362*4882a593Smuzhiyun 		else
363*4882a593Smuzhiyun 			dcb->active = EPOLLOUT;
364*4882a593Smuzhiyun 		spin_unlock_irq(&dmabuf->poll.lock);
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 		if (!(events & EPOLLOUT))
367*4882a593Smuzhiyun 			goto out;
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 		for (i = 0; i < shared_count; ++i) {
370*4882a593Smuzhiyun 			struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 			if (!dma_fence_get_rcu(fence)) {
373*4882a593Smuzhiyun 				/*
374*4882a593Smuzhiyun 				 * fence refcount dropped to zero, this means
375*4882a593Smuzhiyun 				 * that fobj has been freed
376*4882a593Smuzhiyun 				 *
377*4882a593Smuzhiyun 				 * call dma_buf_poll_cb and force a recheck!
378*4882a593Smuzhiyun 				 */
379*4882a593Smuzhiyun 				events &= ~EPOLLOUT;
380*4882a593Smuzhiyun 				dma_buf_poll_cb(NULL, &dcb->cb);
381*4882a593Smuzhiyun 				break;
382*4882a593Smuzhiyun 			}
383*4882a593Smuzhiyun 			if (!dma_fence_add_callback(fence, &dcb->cb,
384*4882a593Smuzhiyun 						    dma_buf_poll_cb)) {
385*4882a593Smuzhiyun 				dma_fence_put(fence);
386*4882a593Smuzhiyun 				events &= ~EPOLLOUT;
387*4882a593Smuzhiyun 				break;
388*4882a593Smuzhiyun 			}
389*4882a593Smuzhiyun 			dma_fence_put(fence);
390*4882a593Smuzhiyun 		}
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 		/* No callback queued, wake up any additional waiters. */
393*4882a593Smuzhiyun 		if (i == shared_count)
394*4882a593Smuzhiyun 			dma_buf_poll_cb(NULL, &dcb->cb);
395*4882a593Smuzhiyun 	}
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun out:
398*4882a593Smuzhiyun 	rcu_read_unlock();
399*4882a593Smuzhiyun 	return events;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun 
_dma_buf_set_name(struct dma_buf * dmabuf,const char * name)402*4882a593Smuzhiyun static long _dma_buf_set_name(struct dma_buf *dmabuf, const char *name)
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun 	spin_lock(&dmabuf->name_lock);
405*4882a593Smuzhiyun 	kfree(dmabuf->name);
406*4882a593Smuzhiyun 	dmabuf->name = name;
407*4882a593Smuzhiyun 	spin_unlock(&dmabuf->name_lock);
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	return 0;
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun /**
413*4882a593Smuzhiyun  * dma_buf_set_name - Set a name to a specific dma_buf to track the usage.
414*4882a593Smuzhiyun  * It could support changing the name of the dma-buf if the same piece of
415*4882a593Smuzhiyun  * memory is used for multiple purpose between different devices.
416*4882a593Smuzhiyun  *
417*4882a593Smuzhiyun  * @dmabuf: [in]     dmabuf buffer that will be renamed.
418*4882a593Smuzhiyun  * @buf:    [in]     A piece of userspace memory that contains the name of
419*4882a593Smuzhiyun  *                   the dma-buf.
420*4882a593Smuzhiyun  *
421*4882a593Smuzhiyun  * Returns 0 on success. If the dma-buf buffer is already attached to
422*4882a593Smuzhiyun  * devices, return -EBUSY.
423*4882a593Smuzhiyun  *
424*4882a593Smuzhiyun  */
dma_buf_set_name(struct dma_buf * dmabuf,const char * name)425*4882a593Smuzhiyun long dma_buf_set_name(struct dma_buf *dmabuf, const char *name)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun 	long ret = 0;
428*4882a593Smuzhiyun 	char *buf = kstrndup(name, DMA_BUF_NAME_LEN, GFP_KERNEL);
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	if (!buf)
431*4882a593Smuzhiyun 		return -ENOMEM;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	ret = _dma_buf_set_name(dmabuf, buf);
434*4882a593Smuzhiyun 	if (ret)
435*4882a593Smuzhiyun 		kfree(buf);
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	return ret;
438*4882a593Smuzhiyun }
439*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_set_name);
440*4882a593Smuzhiyun 
dma_buf_set_name_user(struct dma_buf * dmabuf,const char __user * buf)441*4882a593Smuzhiyun static long dma_buf_set_name_user(struct dma_buf *dmabuf, const char __user *buf)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun 	char *name = strndup_user(buf, DMA_BUF_NAME_LEN);
444*4882a593Smuzhiyun 	long ret = 0;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	if (IS_ERR(name))
447*4882a593Smuzhiyun 		return PTR_ERR(name);
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	ret = _dma_buf_set_name(dmabuf, name);
450*4882a593Smuzhiyun 	if (ret)
451*4882a593Smuzhiyun 		kfree(name);
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	return ret;
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun 
dma_buf_ioctl(struct file * file,unsigned int cmd,unsigned long arg)456*4882a593Smuzhiyun static long dma_buf_ioctl(struct file *file,
457*4882a593Smuzhiyun 			  unsigned int cmd, unsigned long arg)
458*4882a593Smuzhiyun {
459*4882a593Smuzhiyun 	struct dma_buf *dmabuf;
460*4882a593Smuzhiyun 	struct dma_buf_sync sync;
461*4882a593Smuzhiyun 	struct dma_buf_sync_partial sync_p;
462*4882a593Smuzhiyun 	enum dma_data_direction direction;
463*4882a593Smuzhiyun 	int ret;
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	dmabuf = file->private_data;
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	switch (cmd) {
468*4882a593Smuzhiyun 	case DMA_BUF_IOCTL_SYNC:
469*4882a593Smuzhiyun 		if (copy_from_user(&sync, (void __user *) arg, sizeof(sync)))
470*4882a593Smuzhiyun 			return -EFAULT;
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 		if (sync.flags & ~DMA_BUF_SYNC_VALID_FLAGS_MASK)
473*4882a593Smuzhiyun 			return -EINVAL;
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 		switch (sync.flags & DMA_BUF_SYNC_RW) {
476*4882a593Smuzhiyun 		case DMA_BUF_SYNC_READ:
477*4882a593Smuzhiyun 			direction = DMA_FROM_DEVICE;
478*4882a593Smuzhiyun 			break;
479*4882a593Smuzhiyun 		case DMA_BUF_SYNC_WRITE:
480*4882a593Smuzhiyun 			direction = DMA_TO_DEVICE;
481*4882a593Smuzhiyun 			break;
482*4882a593Smuzhiyun 		case DMA_BUF_SYNC_RW:
483*4882a593Smuzhiyun 			direction = DMA_BIDIRECTIONAL;
484*4882a593Smuzhiyun 			break;
485*4882a593Smuzhiyun 		default:
486*4882a593Smuzhiyun 			return -EINVAL;
487*4882a593Smuzhiyun 		}
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 		if (sync.flags & DMA_BUF_SYNC_END)
490*4882a593Smuzhiyun 			ret = dma_buf_end_cpu_access(dmabuf, direction);
491*4882a593Smuzhiyun 		else
492*4882a593Smuzhiyun 			ret = dma_buf_begin_cpu_access(dmabuf, direction);
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 		return ret;
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 	case DMA_BUF_SET_NAME_A:
497*4882a593Smuzhiyun 	case DMA_BUF_SET_NAME_B:
498*4882a593Smuzhiyun 		return dma_buf_set_name_user(dmabuf, (const char __user *)arg);
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	case DMA_BUF_IOCTL_SYNC_PARTIAL:
501*4882a593Smuzhiyun 		if (copy_from_user(&sync_p, (void __user *) arg, sizeof(sync_p)))
502*4882a593Smuzhiyun 			return -EFAULT;
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 		if (sync_p.len == 0)
505*4882a593Smuzhiyun 			return 0;
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 		if (sync_p.len > dmabuf->size || sync_p.offset > dmabuf->size - sync_p.len)
508*4882a593Smuzhiyun 			return -EINVAL;
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 		if (sync_p.flags & ~DMA_BUF_SYNC_VALID_FLAGS_MASK)
511*4882a593Smuzhiyun 			return -EINVAL;
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 		switch (sync_p.flags & DMA_BUF_SYNC_RW) {
514*4882a593Smuzhiyun 		case DMA_BUF_SYNC_READ:
515*4882a593Smuzhiyun 			direction = DMA_FROM_DEVICE;
516*4882a593Smuzhiyun 			break;
517*4882a593Smuzhiyun 		case DMA_BUF_SYNC_WRITE:
518*4882a593Smuzhiyun 			direction = DMA_TO_DEVICE;
519*4882a593Smuzhiyun 			break;
520*4882a593Smuzhiyun 		case DMA_BUF_SYNC_RW:
521*4882a593Smuzhiyun 			direction = DMA_BIDIRECTIONAL;
522*4882a593Smuzhiyun 			break;
523*4882a593Smuzhiyun 		default:
524*4882a593Smuzhiyun 			return -EINVAL;
525*4882a593Smuzhiyun 		}
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 		if (sync_p.flags & DMA_BUF_SYNC_END)
528*4882a593Smuzhiyun 			ret = dma_buf_end_cpu_access_partial(dmabuf, direction,
529*4882a593Smuzhiyun 							     sync_p.offset,
530*4882a593Smuzhiyun 							     sync_p.len);
531*4882a593Smuzhiyun 		else
532*4882a593Smuzhiyun 			ret = dma_buf_begin_cpu_access_partial(dmabuf, direction,
533*4882a593Smuzhiyun 							       sync_p.offset,
534*4882a593Smuzhiyun 							       sync_p.len);
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 		return ret;
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	default:
539*4882a593Smuzhiyun 		return -ENOTTY;
540*4882a593Smuzhiyun 	}
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun 
dma_buf_show_fdinfo(struct seq_file * m,struct file * file)543*4882a593Smuzhiyun static void dma_buf_show_fdinfo(struct seq_file *m, struct file *file)
544*4882a593Smuzhiyun {
545*4882a593Smuzhiyun 	struct dma_buf *dmabuf = file->private_data;
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	seq_printf(m, "size:\t%zu\n", dmabuf->size);
548*4882a593Smuzhiyun 	/* Don't count the temporary reference taken inside procfs seq_show */
549*4882a593Smuzhiyun 	seq_printf(m, "count:\t%ld\n", file_count(dmabuf->file) - 1);
550*4882a593Smuzhiyun 	seq_printf(m, "exp_name:\t%s\n", dmabuf->exp_name);
551*4882a593Smuzhiyun 	spin_lock(&dmabuf->name_lock);
552*4882a593Smuzhiyun 	if (dmabuf->name)
553*4882a593Smuzhiyun 		seq_printf(m, "name:\t%s\n", dmabuf->name);
554*4882a593Smuzhiyun 	spin_unlock(&dmabuf->name_lock);
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun static const struct file_operations dma_buf_fops = {
558*4882a593Smuzhiyun 	.release	= dma_buf_file_release,
559*4882a593Smuzhiyun 	.mmap		= dma_buf_mmap_internal,
560*4882a593Smuzhiyun 	.llseek		= dma_buf_llseek,
561*4882a593Smuzhiyun 	.poll		= dma_buf_poll,
562*4882a593Smuzhiyun 	.unlocked_ioctl	= dma_buf_ioctl,
563*4882a593Smuzhiyun 	.compat_ioctl	= compat_ptr_ioctl,
564*4882a593Smuzhiyun 	.show_fdinfo	= dma_buf_show_fdinfo,
565*4882a593Smuzhiyun };
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun /*
568*4882a593Smuzhiyun  * is_dma_buf_file - Check if struct file* is associated with dma_buf
569*4882a593Smuzhiyun  */
is_dma_buf_file(struct file * file)570*4882a593Smuzhiyun int is_dma_buf_file(struct file *file)
571*4882a593Smuzhiyun {
572*4882a593Smuzhiyun 	return file->f_op == &dma_buf_fops;
573*4882a593Smuzhiyun }
574*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(is_dma_buf_file);
575*4882a593Smuzhiyun 
dma_buf_getfile(struct dma_buf * dmabuf,int flags)576*4882a593Smuzhiyun static struct file *dma_buf_getfile(struct dma_buf *dmabuf, int flags)
577*4882a593Smuzhiyun {
578*4882a593Smuzhiyun 	static atomic64_t dmabuf_inode = ATOMIC64_INIT(0);
579*4882a593Smuzhiyun 	struct file *file;
580*4882a593Smuzhiyun 	struct inode *inode = alloc_anon_inode(dma_buf_mnt->mnt_sb);
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	if (IS_ERR(inode))
583*4882a593Smuzhiyun 		return ERR_CAST(inode);
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	inode->i_size = dmabuf->size;
586*4882a593Smuzhiyun 	inode_set_bytes(inode, dmabuf->size);
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	/*
589*4882a593Smuzhiyun 	 * The ->i_ino acquired from get_next_ino() is not unique thus
590*4882a593Smuzhiyun 	 * not suitable for using it as dentry name by dmabuf stats.
591*4882a593Smuzhiyun 	 * Override ->i_ino with the unique and dmabuffs specific
592*4882a593Smuzhiyun 	 * value.
593*4882a593Smuzhiyun 	 */
594*4882a593Smuzhiyun 	inode->i_ino = atomic64_add_return(1, &dmabuf_inode);
595*4882a593Smuzhiyun 	file = alloc_file_pseudo(inode, dma_buf_mnt, "dmabuf",
596*4882a593Smuzhiyun 				 flags, &dma_buf_fops);
597*4882a593Smuzhiyun 	if (IS_ERR(file))
598*4882a593Smuzhiyun 		goto err_alloc_file;
599*4882a593Smuzhiyun 	file->f_flags = flags & (O_ACCMODE | O_NONBLOCK);
600*4882a593Smuzhiyun 	file->private_data = dmabuf;
601*4882a593Smuzhiyun 	file->f_path.dentry->d_fsdata = dmabuf;
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 	return file;
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun err_alloc_file:
606*4882a593Smuzhiyun 	iput(inode);
607*4882a593Smuzhiyun 	return file;
608*4882a593Smuzhiyun }
609*4882a593Smuzhiyun 
dma_buf_set_default_name(struct dma_buf * dmabuf)610*4882a593Smuzhiyun static void dma_buf_set_default_name(struct dma_buf *dmabuf)
611*4882a593Smuzhiyun {
612*4882a593Smuzhiyun 	char task_comm[TASK_COMM_LEN];
613*4882a593Smuzhiyun 	char *name;
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	get_task_comm(task_comm, current->group_leader);
616*4882a593Smuzhiyun 	name = kasprintf(GFP_KERNEL, "%d-%s", current->tgid, task_comm);
617*4882a593Smuzhiyun 	dma_buf_set_name(dmabuf, name);
618*4882a593Smuzhiyun 	kfree(name);
619*4882a593Smuzhiyun }
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun /**
622*4882a593Smuzhiyun  * DOC: dma buf device access
623*4882a593Smuzhiyun  *
624*4882a593Smuzhiyun  * For device DMA access to a shared DMA buffer the usual sequence of operations
625*4882a593Smuzhiyun  * is fairly simple:
626*4882a593Smuzhiyun  *
627*4882a593Smuzhiyun  * 1. The exporter defines his exporter instance using
628*4882a593Smuzhiyun  *    DEFINE_DMA_BUF_EXPORT_INFO() and calls dma_buf_export() to wrap a private
629*4882a593Smuzhiyun  *    buffer object into a &dma_buf. It then exports that &dma_buf to userspace
630*4882a593Smuzhiyun  *    as a file descriptor by calling dma_buf_fd().
631*4882a593Smuzhiyun  *
632*4882a593Smuzhiyun  * 2. Userspace passes this file-descriptors to all drivers it wants this buffer
633*4882a593Smuzhiyun  *    to share with: First the filedescriptor is converted to a &dma_buf using
634*4882a593Smuzhiyun  *    dma_buf_get(). Then the buffer is attached to the device using
635*4882a593Smuzhiyun  *    dma_buf_attach().
636*4882a593Smuzhiyun  *
637*4882a593Smuzhiyun  *    Up to this stage the exporter is still free to migrate or reallocate the
638*4882a593Smuzhiyun  *    backing storage.
639*4882a593Smuzhiyun  *
640*4882a593Smuzhiyun  * 3. Once the buffer is attached to all devices userspace can initiate DMA
641*4882a593Smuzhiyun  *    access to the shared buffer. In the kernel this is done by calling
642*4882a593Smuzhiyun  *    dma_buf_map_attachment() and dma_buf_unmap_attachment().
643*4882a593Smuzhiyun  *
644*4882a593Smuzhiyun  * 4. Once a driver is done with a shared buffer it needs to call
645*4882a593Smuzhiyun  *    dma_buf_detach() (after cleaning up any mappings) and then release the
646*4882a593Smuzhiyun  *    reference acquired with dma_buf_get by calling dma_buf_put().
647*4882a593Smuzhiyun  *
648*4882a593Smuzhiyun  * For the detailed semantics exporters are expected to implement see
649*4882a593Smuzhiyun  * &dma_buf_ops.
650*4882a593Smuzhiyun  */
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun /**
653*4882a593Smuzhiyun  * dma_buf_export - Creates a new dma_buf, and associates an anon file
654*4882a593Smuzhiyun  * with this buffer, so it can be exported.
655*4882a593Smuzhiyun  * Also connect the allocator specific data and ops to the buffer.
656*4882a593Smuzhiyun  * Additionally, provide a name string for exporter; useful in debugging.
657*4882a593Smuzhiyun  *
658*4882a593Smuzhiyun  * @exp_info:	[in]	holds all the export related information provided
659*4882a593Smuzhiyun  *			by the exporter. see &struct dma_buf_export_info
660*4882a593Smuzhiyun  *			for further details.
661*4882a593Smuzhiyun  *
662*4882a593Smuzhiyun  * Returns, on success, a newly created dma_buf object, which wraps the
663*4882a593Smuzhiyun  * supplied private data and operations for dma_buf_ops. On either missing
664*4882a593Smuzhiyun  * ops, or error in allocating struct dma_buf, will return negative error.
665*4882a593Smuzhiyun  *
666*4882a593Smuzhiyun  * For most cases the easiest way to create @exp_info is through the
667*4882a593Smuzhiyun  * %DEFINE_DMA_BUF_EXPORT_INFO macro.
668*4882a593Smuzhiyun  */
dma_buf_export(const struct dma_buf_export_info * exp_info)669*4882a593Smuzhiyun struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
670*4882a593Smuzhiyun {
671*4882a593Smuzhiyun 	struct dma_buf *dmabuf;
672*4882a593Smuzhiyun 	struct dma_resv *resv = exp_info->resv;
673*4882a593Smuzhiyun 	struct file *file;
674*4882a593Smuzhiyun 	size_t alloc_size = sizeof(struct dma_buf);
675*4882a593Smuzhiyun 	int ret;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	if (!exp_info->resv)
678*4882a593Smuzhiyun 		alloc_size += sizeof(struct dma_resv);
679*4882a593Smuzhiyun 	else
680*4882a593Smuzhiyun 		/* prevent &dma_buf[1] == dma_buf->resv */
681*4882a593Smuzhiyun 		alloc_size += 1;
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 	if (WARN_ON(!exp_info->priv
684*4882a593Smuzhiyun 			  || !exp_info->ops
685*4882a593Smuzhiyun 			  || !exp_info->ops->map_dma_buf
686*4882a593Smuzhiyun 			  || !exp_info->ops->unmap_dma_buf
687*4882a593Smuzhiyun 			  || !exp_info->ops->release)) {
688*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
689*4882a593Smuzhiyun 	}
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	if (WARN_ON(exp_info->ops->cache_sgt_mapping &&
692*4882a593Smuzhiyun 		    (exp_info->ops->pin || exp_info->ops->unpin)))
693*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	if (WARN_ON(!exp_info->ops->pin != !exp_info->ops->unpin))
696*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	if (!try_module_get(exp_info->owner))
699*4882a593Smuzhiyun 		return ERR_PTR(-ENOENT);
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 	dmabuf = kzalloc(alloc_size, GFP_KERNEL);
702*4882a593Smuzhiyun 	if (!dmabuf) {
703*4882a593Smuzhiyun 		ret = -ENOMEM;
704*4882a593Smuzhiyun 		goto err_module;
705*4882a593Smuzhiyun 	}
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 	dmabuf->priv = exp_info->priv;
708*4882a593Smuzhiyun 	dmabuf->ops = exp_info->ops;
709*4882a593Smuzhiyun 	dmabuf->size = exp_info->size;
710*4882a593Smuzhiyun 	dmabuf->exp_name = exp_info->exp_name;
711*4882a593Smuzhiyun 	dmabuf->owner = exp_info->owner;
712*4882a593Smuzhiyun 	spin_lock_init(&dmabuf->name_lock);
713*4882a593Smuzhiyun #ifdef CONFIG_DMABUF_CACHE
714*4882a593Smuzhiyun 	mutex_init(&dmabuf->cache_lock);
715*4882a593Smuzhiyun #endif
716*4882a593Smuzhiyun 	init_waitqueue_head(&dmabuf->poll);
717*4882a593Smuzhiyun 	dmabuf->cb_excl.poll = dmabuf->cb_shared.poll = &dmabuf->poll;
718*4882a593Smuzhiyun 	dmabuf->cb_excl.active = dmabuf->cb_shared.active = 0;
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun 	if (!resv) {
721*4882a593Smuzhiyun 		resv = (struct dma_resv *)&dmabuf[1];
722*4882a593Smuzhiyun 		dma_resv_init(resv);
723*4882a593Smuzhiyun 	}
724*4882a593Smuzhiyun 	dmabuf->resv = resv;
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 	file = dma_buf_getfile(dmabuf, exp_info->flags);
727*4882a593Smuzhiyun 	if (IS_ERR(file)) {
728*4882a593Smuzhiyun 		ret = PTR_ERR(file);
729*4882a593Smuzhiyun 		goto err_dmabuf;
730*4882a593Smuzhiyun 	}
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun 	file->f_mode |= FMODE_LSEEK;
733*4882a593Smuzhiyun 	dmabuf->file = file;
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	mutex_init(&dmabuf->lock);
736*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dmabuf->attachments);
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 	mutex_lock(&db_list.lock);
739*4882a593Smuzhiyun 	list_add(&dmabuf->list_node, &db_list.head);
740*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_RK_DMABUF_DEBUG)
741*4882a593Smuzhiyun 	db_total_size += dmabuf->size;
742*4882a593Smuzhiyun 	db_peak_size = max(db_total_size, db_peak_size);
743*4882a593Smuzhiyun #endif
744*4882a593Smuzhiyun 	mutex_unlock(&db_list.lock);
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	ret = dma_buf_stats_setup(dmabuf);
747*4882a593Smuzhiyun 	if (ret)
748*4882a593Smuzhiyun 		goto err_sysfs;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_RK_DMABUF_DEBUG))
751*4882a593Smuzhiyun 		dma_buf_set_default_name(dmabuf);
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	return dmabuf;
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun err_sysfs:
756*4882a593Smuzhiyun 	/*
757*4882a593Smuzhiyun 	 * Set file->f_path.dentry->d_fsdata to NULL so that when
758*4882a593Smuzhiyun 	 * dma_buf_release() gets invoked by dentry_ops, it exits
759*4882a593Smuzhiyun 	 * early before calling the release() dma_buf op.
760*4882a593Smuzhiyun 	 */
761*4882a593Smuzhiyun 	file->f_path.dentry->d_fsdata = NULL;
762*4882a593Smuzhiyun 	fput(file);
763*4882a593Smuzhiyun err_dmabuf:
764*4882a593Smuzhiyun 	kfree(dmabuf);
765*4882a593Smuzhiyun err_module:
766*4882a593Smuzhiyun 	module_put(exp_info->owner);
767*4882a593Smuzhiyun 	return ERR_PTR(ret);
768*4882a593Smuzhiyun }
769*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_export);
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun /**
772*4882a593Smuzhiyun  * dma_buf_fd - returns a file descriptor for the given dma_buf
773*4882a593Smuzhiyun  * @dmabuf:	[in]	pointer to dma_buf for which fd is required.
774*4882a593Smuzhiyun  * @flags:      [in]    flags to give to fd
775*4882a593Smuzhiyun  *
776*4882a593Smuzhiyun  * On success, returns an associated 'fd'. Else, returns error.
777*4882a593Smuzhiyun  */
dma_buf_fd(struct dma_buf * dmabuf,int flags)778*4882a593Smuzhiyun int dma_buf_fd(struct dma_buf *dmabuf, int flags)
779*4882a593Smuzhiyun {
780*4882a593Smuzhiyun 	int fd;
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	if (!dmabuf || !dmabuf->file)
783*4882a593Smuzhiyun 		return -EINVAL;
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 	fd = get_unused_fd_flags(flags);
786*4882a593Smuzhiyun 	if (fd < 0)
787*4882a593Smuzhiyun 		return fd;
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	fd_install(fd, dmabuf->file);
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	return fd;
792*4882a593Smuzhiyun }
793*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_fd);
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun /**
796*4882a593Smuzhiyun  * dma_buf_get - returns the dma_buf structure related to an fd
797*4882a593Smuzhiyun  * @fd:	[in]	fd associated with the dma_buf to be returned
798*4882a593Smuzhiyun  *
799*4882a593Smuzhiyun  * On success, returns the dma_buf structure associated with an fd; uses
800*4882a593Smuzhiyun  * file's refcounting done by fget to increase refcount. returns ERR_PTR
801*4882a593Smuzhiyun  * otherwise.
802*4882a593Smuzhiyun  */
dma_buf_get(int fd)803*4882a593Smuzhiyun struct dma_buf *dma_buf_get(int fd)
804*4882a593Smuzhiyun {
805*4882a593Smuzhiyun 	struct file *file;
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 	file = fget(fd);
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 	if (!file)
810*4882a593Smuzhiyun 		return ERR_PTR(-EBADF);
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	if (!is_dma_buf_file(file)) {
813*4882a593Smuzhiyun 		fput(file);
814*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
815*4882a593Smuzhiyun 	}
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	return file->private_data;
818*4882a593Smuzhiyun }
819*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_get);
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun /**
822*4882a593Smuzhiyun  * dma_buf_put - decreases refcount of the buffer
823*4882a593Smuzhiyun  * @dmabuf:	[in]	buffer to reduce refcount of
824*4882a593Smuzhiyun  *
825*4882a593Smuzhiyun  * Uses file's refcounting done implicitly by fput().
826*4882a593Smuzhiyun  *
827*4882a593Smuzhiyun  * If, as a result of this call, the refcount becomes 0, the 'release' file
828*4882a593Smuzhiyun  * operation related to this fd is called. It calls &dma_buf_ops.release vfunc
829*4882a593Smuzhiyun  * in turn, and frees the memory allocated for dmabuf when exported.
830*4882a593Smuzhiyun  */
dma_buf_put(struct dma_buf * dmabuf)831*4882a593Smuzhiyun void dma_buf_put(struct dma_buf *dmabuf)
832*4882a593Smuzhiyun {
833*4882a593Smuzhiyun 	if (WARN_ON(!dmabuf || !dmabuf->file))
834*4882a593Smuzhiyun 		return;
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun 	fput(dmabuf->file);
837*4882a593Smuzhiyun }
838*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_put);
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun /**
841*4882a593Smuzhiyun  * dma_buf_dynamic_attach - Add the device to dma_buf's attachments list; optionally,
842*4882a593Smuzhiyun  * calls attach() of dma_buf_ops to allow device-specific attach functionality
843*4882a593Smuzhiyun  * @dmabuf:		[in]	buffer to attach device to.
844*4882a593Smuzhiyun  * @dev:		[in]	device to be attached.
845*4882a593Smuzhiyun  * @importer_ops:	[in]	importer operations for the attachment
846*4882a593Smuzhiyun  * @importer_priv:	[in]	importer private pointer for the attachment
847*4882a593Smuzhiyun  *
848*4882a593Smuzhiyun  * Returns struct dma_buf_attachment pointer for this attachment. Attachments
849*4882a593Smuzhiyun  * must be cleaned up by calling dma_buf_detach().
850*4882a593Smuzhiyun  *
851*4882a593Smuzhiyun  * Returns:
852*4882a593Smuzhiyun  *
853*4882a593Smuzhiyun  * A pointer to newly created &dma_buf_attachment on success, or a negative
854*4882a593Smuzhiyun  * error code wrapped into a pointer on failure.
855*4882a593Smuzhiyun  *
856*4882a593Smuzhiyun  * Note that this can fail if the backing storage of @dmabuf is in a place not
857*4882a593Smuzhiyun  * accessible to @dev, and cannot be moved to a more suitable place. This is
858*4882a593Smuzhiyun  * indicated with the error code -EBUSY.
859*4882a593Smuzhiyun  */
860*4882a593Smuzhiyun struct dma_buf_attachment *
dma_buf_dynamic_attach(struct dma_buf * dmabuf,struct device * dev,const struct dma_buf_attach_ops * importer_ops,void * importer_priv)861*4882a593Smuzhiyun dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
862*4882a593Smuzhiyun 		       const struct dma_buf_attach_ops *importer_ops,
863*4882a593Smuzhiyun 		       void *importer_priv)
864*4882a593Smuzhiyun {
865*4882a593Smuzhiyun 	struct dma_buf_attachment *attach;
866*4882a593Smuzhiyun 	int ret;
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun 	if (WARN_ON(!dmabuf || !dev))
869*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	if (WARN_ON(importer_ops && !importer_ops->move_notify))
872*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun 	attach = kzalloc(sizeof(*attach), GFP_KERNEL);
875*4882a593Smuzhiyun 	if (!attach)
876*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 	attach->dev = dev;
879*4882a593Smuzhiyun 	attach->dmabuf = dmabuf;
880*4882a593Smuzhiyun 	if (importer_ops)
881*4882a593Smuzhiyun 		attach->peer2peer = importer_ops->allow_peer2peer;
882*4882a593Smuzhiyun 	attach->importer_ops = importer_ops;
883*4882a593Smuzhiyun 	attach->importer_priv = importer_priv;
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 	if (dmabuf->ops->attach) {
886*4882a593Smuzhiyun 		ret = dmabuf->ops->attach(dmabuf, attach);
887*4882a593Smuzhiyun 		if (ret)
888*4882a593Smuzhiyun 			goto err_attach;
889*4882a593Smuzhiyun 	}
890*4882a593Smuzhiyun 	dma_resv_lock(dmabuf->resv, NULL);
891*4882a593Smuzhiyun 	list_add(&attach->node, &dmabuf->attachments);
892*4882a593Smuzhiyun 	dma_resv_unlock(dmabuf->resv);
893*4882a593Smuzhiyun 
894*4882a593Smuzhiyun 	/* When either the importer or the exporter can't handle dynamic
895*4882a593Smuzhiyun 	 * mappings we cache the mapping here to avoid issues with the
896*4882a593Smuzhiyun 	 * reservation object lock.
897*4882a593Smuzhiyun 	 */
898*4882a593Smuzhiyun 	if (dma_buf_attachment_is_dynamic(attach) !=
899*4882a593Smuzhiyun 	    dma_buf_is_dynamic(dmabuf)) {
900*4882a593Smuzhiyun 		struct sg_table *sgt;
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 		if (dma_buf_is_dynamic(attach->dmabuf)) {
903*4882a593Smuzhiyun 			dma_resv_lock(attach->dmabuf->resv, NULL);
904*4882a593Smuzhiyun 			ret = dma_buf_pin(attach);
905*4882a593Smuzhiyun 			if (ret)
906*4882a593Smuzhiyun 				goto err_unlock;
907*4882a593Smuzhiyun 		}
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 		sgt = dmabuf->ops->map_dma_buf(attach, DMA_BIDIRECTIONAL);
910*4882a593Smuzhiyun 		if (!sgt)
911*4882a593Smuzhiyun 			sgt = ERR_PTR(-ENOMEM);
912*4882a593Smuzhiyun 		if (IS_ERR(sgt)) {
913*4882a593Smuzhiyun 			ret = PTR_ERR(sgt);
914*4882a593Smuzhiyun 			goto err_unpin;
915*4882a593Smuzhiyun 		}
916*4882a593Smuzhiyun 		if (dma_buf_is_dynamic(attach->dmabuf))
917*4882a593Smuzhiyun 			dma_resv_unlock(attach->dmabuf->resv);
918*4882a593Smuzhiyun 		attach->sgt = sgt;
919*4882a593Smuzhiyun 		attach->dir = DMA_BIDIRECTIONAL;
920*4882a593Smuzhiyun 	}
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	return attach;
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun err_attach:
925*4882a593Smuzhiyun 	kfree(attach);
926*4882a593Smuzhiyun 	return ERR_PTR(ret);
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun err_unpin:
929*4882a593Smuzhiyun 	if (dma_buf_is_dynamic(attach->dmabuf))
930*4882a593Smuzhiyun 		dma_buf_unpin(attach);
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun err_unlock:
933*4882a593Smuzhiyun 	if (dma_buf_is_dynamic(attach->dmabuf))
934*4882a593Smuzhiyun 		dma_resv_unlock(attach->dmabuf->resv);
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	dma_buf_detach(dmabuf, attach);
937*4882a593Smuzhiyun 	return ERR_PTR(ret);
938*4882a593Smuzhiyun }
939*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_dynamic_attach);
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun /**
942*4882a593Smuzhiyun  * dma_buf_attach - Wrapper for dma_buf_dynamic_attach
943*4882a593Smuzhiyun  * @dmabuf:	[in]	buffer to attach device to.
944*4882a593Smuzhiyun  * @dev:	[in]	device to be attached.
945*4882a593Smuzhiyun  *
946*4882a593Smuzhiyun  * Wrapper to call dma_buf_dynamic_attach() for drivers which still use a static
947*4882a593Smuzhiyun  * mapping.
948*4882a593Smuzhiyun  */
dma_buf_attach(struct dma_buf * dmabuf,struct device * dev)949*4882a593Smuzhiyun struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
950*4882a593Smuzhiyun 					  struct device *dev)
951*4882a593Smuzhiyun {
952*4882a593Smuzhiyun 	return dma_buf_dynamic_attach(dmabuf, dev, NULL, NULL);
953*4882a593Smuzhiyun }
954*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_attach);
955*4882a593Smuzhiyun 
956*4882a593Smuzhiyun /**
957*4882a593Smuzhiyun  * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
958*4882a593Smuzhiyun  * optionally calls detach() of dma_buf_ops for device-specific detach
959*4882a593Smuzhiyun  * @dmabuf:	[in]	buffer to detach from.
960*4882a593Smuzhiyun  * @attach:	[in]	attachment to be detached; is free'd after this call.
961*4882a593Smuzhiyun  *
962*4882a593Smuzhiyun  * Clean up a device attachment obtained by calling dma_buf_attach().
963*4882a593Smuzhiyun  */
dma_buf_detach(struct dma_buf * dmabuf,struct dma_buf_attachment * attach)964*4882a593Smuzhiyun void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
965*4882a593Smuzhiyun {
966*4882a593Smuzhiyun 	if (WARN_ON(!dmabuf || !attach))
967*4882a593Smuzhiyun 		return;
968*4882a593Smuzhiyun 
969*4882a593Smuzhiyun 	if (attach->sgt) {
970*4882a593Smuzhiyun 		if (dma_buf_is_dynamic(attach->dmabuf))
971*4882a593Smuzhiyun 			dma_resv_lock(attach->dmabuf->resv, NULL);
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 		dmabuf->ops->unmap_dma_buf(attach, attach->sgt, attach->dir);
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun 		if (dma_buf_is_dynamic(attach->dmabuf)) {
976*4882a593Smuzhiyun 			dma_buf_unpin(attach);
977*4882a593Smuzhiyun 			dma_resv_unlock(attach->dmabuf->resv);
978*4882a593Smuzhiyun 		}
979*4882a593Smuzhiyun 	}
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun 	dma_resv_lock(dmabuf->resv, NULL);
982*4882a593Smuzhiyun 	list_del(&attach->node);
983*4882a593Smuzhiyun 	dma_resv_unlock(dmabuf->resv);
984*4882a593Smuzhiyun 	if (dmabuf->ops->detach)
985*4882a593Smuzhiyun 		dmabuf->ops->detach(dmabuf, attach);
986*4882a593Smuzhiyun 
987*4882a593Smuzhiyun 	kfree(attach);
988*4882a593Smuzhiyun }
989*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_detach);
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun /**
992*4882a593Smuzhiyun  * dma_buf_pin - Lock down the DMA-buf
993*4882a593Smuzhiyun  *
994*4882a593Smuzhiyun  * @attach:	[in]	attachment which should be pinned
995*4882a593Smuzhiyun  *
996*4882a593Smuzhiyun  * Returns:
997*4882a593Smuzhiyun  * 0 on success, negative error code on failure.
998*4882a593Smuzhiyun  */
dma_buf_pin(struct dma_buf_attachment * attach)999*4882a593Smuzhiyun int dma_buf_pin(struct dma_buf_attachment *attach)
1000*4882a593Smuzhiyun {
1001*4882a593Smuzhiyun 	struct dma_buf *dmabuf = attach->dmabuf;
1002*4882a593Smuzhiyun 	int ret = 0;
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun 	dma_resv_assert_held(dmabuf->resv);
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun 	if (dmabuf->ops->pin)
1007*4882a593Smuzhiyun 		ret = dmabuf->ops->pin(attach);
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun 	return ret;
1010*4882a593Smuzhiyun }
1011*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_pin);
1012*4882a593Smuzhiyun 
1013*4882a593Smuzhiyun /**
1014*4882a593Smuzhiyun  * dma_buf_unpin - Remove lock from DMA-buf
1015*4882a593Smuzhiyun  *
1016*4882a593Smuzhiyun  * @attach:	[in]	attachment which should be unpinned
1017*4882a593Smuzhiyun  */
dma_buf_unpin(struct dma_buf_attachment * attach)1018*4882a593Smuzhiyun void dma_buf_unpin(struct dma_buf_attachment *attach)
1019*4882a593Smuzhiyun {
1020*4882a593Smuzhiyun 	struct dma_buf *dmabuf = attach->dmabuf;
1021*4882a593Smuzhiyun 
1022*4882a593Smuzhiyun 	dma_resv_assert_held(dmabuf->resv);
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun 	if (dmabuf->ops->unpin)
1025*4882a593Smuzhiyun 		dmabuf->ops->unpin(attach);
1026*4882a593Smuzhiyun }
1027*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_unpin);
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun /**
1030*4882a593Smuzhiyun  * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
1031*4882a593Smuzhiyun  * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
1032*4882a593Smuzhiyun  * dma_buf_ops.
1033*4882a593Smuzhiyun  * @attach:	[in]	attachment whose scatterlist is to be returned
1034*4882a593Smuzhiyun  * @direction:	[in]	direction of DMA transfer
1035*4882a593Smuzhiyun  *
1036*4882a593Smuzhiyun  * Returns sg_table containing the scatterlist to be returned; returns ERR_PTR
1037*4882a593Smuzhiyun  * on error. May return -EINTR if it is interrupted by a signal.
1038*4882a593Smuzhiyun  *
1039*4882a593Smuzhiyun  * A mapping must be unmapped by using dma_buf_unmap_attachment(). Note that
1040*4882a593Smuzhiyun  * the underlying backing storage is pinned for as long as a mapping exists,
1041*4882a593Smuzhiyun  * therefore users/importers should not hold onto a mapping for undue amounts of
1042*4882a593Smuzhiyun  * time.
1043*4882a593Smuzhiyun  */
dma_buf_map_attachment(struct dma_buf_attachment * attach,enum dma_data_direction direction)1044*4882a593Smuzhiyun struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
1045*4882a593Smuzhiyun 					enum dma_data_direction direction)
1046*4882a593Smuzhiyun {
1047*4882a593Smuzhiyun 	struct sg_table *sg_table;
1048*4882a593Smuzhiyun 	int r;
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun 	might_sleep();
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun 	if (WARN_ON(!attach || !attach->dmabuf))
1053*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
1054*4882a593Smuzhiyun 
1055*4882a593Smuzhiyun 	if (dma_buf_attachment_is_dynamic(attach))
1056*4882a593Smuzhiyun 		dma_resv_assert_held(attach->dmabuf->resv);
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun 	if (attach->sgt) {
1059*4882a593Smuzhiyun 		/*
1060*4882a593Smuzhiyun 		 * Two mappings with different directions for the same
1061*4882a593Smuzhiyun 		 * attachment are not allowed.
1062*4882a593Smuzhiyun 		 */
1063*4882a593Smuzhiyun 		if (attach->dir != direction &&
1064*4882a593Smuzhiyun 		    attach->dir != DMA_BIDIRECTIONAL)
1065*4882a593Smuzhiyun 			return ERR_PTR(-EBUSY);
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 		return attach->sgt;
1068*4882a593Smuzhiyun 	}
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun 	if (dma_buf_is_dynamic(attach->dmabuf)) {
1071*4882a593Smuzhiyun 		dma_resv_assert_held(attach->dmabuf->resv);
1072*4882a593Smuzhiyun 		if (!IS_ENABLED(CONFIG_DMABUF_MOVE_NOTIFY)) {
1073*4882a593Smuzhiyun 			r = dma_buf_pin(attach);
1074*4882a593Smuzhiyun 			if (r)
1075*4882a593Smuzhiyun 				return ERR_PTR(r);
1076*4882a593Smuzhiyun 		}
1077*4882a593Smuzhiyun 	}
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 	sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
1080*4882a593Smuzhiyun 	if (!sg_table)
1081*4882a593Smuzhiyun 		sg_table = ERR_PTR(-ENOMEM);
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun 	if (IS_ERR(sg_table) && dma_buf_is_dynamic(attach->dmabuf) &&
1084*4882a593Smuzhiyun 	     !IS_ENABLED(CONFIG_DMABUF_MOVE_NOTIFY))
1085*4882a593Smuzhiyun 		dma_buf_unpin(attach);
1086*4882a593Smuzhiyun 
1087*4882a593Smuzhiyun 	if (!IS_ERR(sg_table) && attach->dmabuf->ops->cache_sgt_mapping) {
1088*4882a593Smuzhiyun 		attach->sgt = sg_table;
1089*4882a593Smuzhiyun 		attach->dir = direction;
1090*4882a593Smuzhiyun 	}
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 	return sg_table;
1093*4882a593Smuzhiyun }
1094*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
1095*4882a593Smuzhiyun 
1096*4882a593Smuzhiyun /**
1097*4882a593Smuzhiyun  * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
1098*4882a593Smuzhiyun  * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
1099*4882a593Smuzhiyun  * dma_buf_ops.
1100*4882a593Smuzhiyun  * @attach:	[in]	attachment to unmap buffer from
1101*4882a593Smuzhiyun  * @sg_table:	[in]	scatterlist info of the buffer to unmap
1102*4882a593Smuzhiyun  * @direction:  [in]    direction of DMA transfer
1103*4882a593Smuzhiyun  *
1104*4882a593Smuzhiyun  * This unmaps a DMA mapping for @attached obtained by dma_buf_map_attachment().
1105*4882a593Smuzhiyun  */
dma_buf_unmap_attachment(struct dma_buf_attachment * attach,struct sg_table * sg_table,enum dma_data_direction direction)1106*4882a593Smuzhiyun void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
1107*4882a593Smuzhiyun 				struct sg_table *sg_table,
1108*4882a593Smuzhiyun 				enum dma_data_direction direction)
1109*4882a593Smuzhiyun {
1110*4882a593Smuzhiyun 	might_sleep();
1111*4882a593Smuzhiyun 
1112*4882a593Smuzhiyun 	if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
1113*4882a593Smuzhiyun 		return;
1114*4882a593Smuzhiyun 
1115*4882a593Smuzhiyun 	if (dma_buf_attachment_is_dynamic(attach))
1116*4882a593Smuzhiyun 		dma_resv_assert_held(attach->dmabuf->resv);
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun 	if (attach->sgt == sg_table)
1119*4882a593Smuzhiyun 		return;
1120*4882a593Smuzhiyun 
1121*4882a593Smuzhiyun 	if (dma_buf_is_dynamic(attach->dmabuf))
1122*4882a593Smuzhiyun 		dma_resv_assert_held(attach->dmabuf->resv);
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun 	attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
1125*4882a593Smuzhiyun 
1126*4882a593Smuzhiyun 	if (dma_buf_is_dynamic(attach->dmabuf) &&
1127*4882a593Smuzhiyun 	    !IS_ENABLED(CONFIG_DMABUF_MOVE_NOTIFY))
1128*4882a593Smuzhiyun 		dma_buf_unpin(attach);
1129*4882a593Smuzhiyun }
1130*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun /**
1133*4882a593Smuzhiyun  * dma_buf_move_notify - notify attachments that DMA-buf is moving
1134*4882a593Smuzhiyun  *
1135*4882a593Smuzhiyun  * @dmabuf:	[in]	buffer which is moving
1136*4882a593Smuzhiyun  *
1137*4882a593Smuzhiyun  * Informs all attachmenst that they need to destroy and recreated all their
1138*4882a593Smuzhiyun  * mappings.
1139*4882a593Smuzhiyun  */
dma_buf_move_notify(struct dma_buf * dmabuf)1140*4882a593Smuzhiyun void dma_buf_move_notify(struct dma_buf *dmabuf)
1141*4882a593Smuzhiyun {
1142*4882a593Smuzhiyun 	struct dma_buf_attachment *attach;
1143*4882a593Smuzhiyun 
1144*4882a593Smuzhiyun 	dma_resv_assert_held(dmabuf->resv);
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 	list_for_each_entry(attach, &dmabuf->attachments, node)
1147*4882a593Smuzhiyun 		if (attach->importer_ops)
1148*4882a593Smuzhiyun 			attach->importer_ops->move_notify(attach);
1149*4882a593Smuzhiyun }
1150*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_move_notify);
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun /**
1153*4882a593Smuzhiyun  * DOC: cpu access
1154*4882a593Smuzhiyun  *
1155*4882a593Smuzhiyun  * There are mutliple reasons for supporting CPU access to a dma buffer object:
1156*4882a593Smuzhiyun  *
1157*4882a593Smuzhiyun  * - Fallback operations in the kernel, for example when a device is connected
1158*4882a593Smuzhiyun  *   over USB and the kernel needs to shuffle the data around first before
1159*4882a593Smuzhiyun  *   sending it away. Cache coherency is handled by braketing any transactions
1160*4882a593Smuzhiyun  *   with calls to dma_buf_begin_cpu_access() and dma_buf_end_cpu_access()
1161*4882a593Smuzhiyun  *   access.
1162*4882a593Smuzhiyun  *
1163*4882a593Smuzhiyun  *   Since for most kernel internal dma-buf accesses need the entire buffer, a
1164*4882a593Smuzhiyun  *   vmap interface is introduced. Note that on very old 32-bit architectures
1165*4882a593Smuzhiyun  *   vmalloc space might be limited and result in vmap calls failing.
1166*4882a593Smuzhiyun  *
1167*4882a593Smuzhiyun  *   Interfaces::
1168*4882a593Smuzhiyun  *      void \*dma_buf_vmap(struct dma_buf \*dmabuf)
1169*4882a593Smuzhiyun  *      void dma_buf_vunmap(struct dma_buf \*dmabuf, void \*vaddr)
1170*4882a593Smuzhiyun  *
1171*4882a593Smuzhiyun  *   The vmap call can fail if there is no vmap support in the exporter, or if
1172*4882a593Smuzhiyun  *   it runs out of vmalloc space. Fallback to kmap should be implemented. Note
1173*4882a593Smuzhiyun  *   that the dma-buf layer keeps a reference count for all vmap access and
1174*4882a593Smuzhiyun  *   calls down into the exporter's vmap function only when no vmapping exists,
1175*4882a593Smuzhiyun  *   and only unmaps it once. Protection against concurrent vmap/vunmap calls is
1176*4882a593Smuzhiyun  *   provided by taking the dma_buf->lock mutex.
1177*4882a593Smuzhiyun  *
1178*4882a593Smuzhiyun  * - For full compatibility on the importer side with existing userspace
1179*4882a593Smuzhiyun  *   interfaces, which might already support mmap'ing buffers. This is needed in
1180*4882a593Smuzhiyun  *   many processing pipelines (e.g. feeding a software rendered image into a
1181*4882a593Smuzhiyun  *   hardware pipeline, thumbnail creation, snapshots, ...). Also, Android's ION
1182*4882a593Smuzhiyun  *   framework already supported this and for DMA buffer file descriptors to
1183*4882a593Smuzhiyun  *   replace ION buffers mmap support was needed.
1184*4882a593Smuzhiyun  *
1185*4882a593Smuzhiyun  *   There is no special interfaces, userspace simply calls mmap on the dma-buf
1186*4882a593Smuzhiyun  *   fd. But like for CPU access there's a need to braket the actual access,
1187*4882a593Smuzhiyun  *   which is handled by the ioctl (DMA_BUF_IOCTL_SYNC). Note that
1188*4882a593Smuzhiyun  *   DMA_BUF_IOCTL_SYNC can fail with -EAGAIN or -EINTR, in which case it must
1189*4882a593Smuzhiyun  *   be restarted.
1190*4882a593Smuzhiyun  *
1191*4882a593Smuzhiyun  *   Some systems might need some sort of cache coherency management e.g. when
1192*4882a593Smuzhiyun  *   CPU and GPU domains are being accessed through dma-buf at the same time.
1193*4882a593Smuzhiyun  *   To circumvent this problem there are begin/end coherency markers, that
1194*4882a593Smuzhiyun  *   forward directly to existing dma-buf device drivers vfunc hooks. Userspace
1195*4882a593Smuzhiyun  *   can make use of those markers through the DMA_BUF_IOCTL_SYNC ioctl. The
1196*4882a593Smuzhiyun  *   sequence would be used like following:
1197*4882a593Smuzhiyun  *
1198*4882a593Smuzhiyun  *     - mmap dma-buf fd
1199*4882a593Smuzhiyun  *     - for each drawing/upload cycle in CPU 1. SYNC_START ioctl, 2. read/write
1200*4882a593Smuzhiyun  *       to mmap area 3. SYNC_END ioctl. This can be repeated as often as you
1201*4882a593Smuzhiyun  *       want (with the new data being consumed by say the GPU or the scanout
1202*4882a593Smuzhiyun  *       device)
1203*4882a593Smuzhiyun  *     - munmap once you don't need the buffer any more
1204*4882a593Smuzhiyun  *
1205*4882a593Smuzhiyun  *    For correctness and optimal performance, it is always required to use
1206*4882a593Smuzhiyun  *    SYNC_START and SYNC_END before and after, respectively, when accessing the
1207*4882a593Smuzhiyun  *    mapped address. Userspace cannot rely on coherent access, even when there
1208*4882a593Smuzhiyun  *    are systems where it just works without calling these ioctls.
1209*4882a593Smuzhiyun  *
1210*4882a593Smuzhiyun  * - And as a CPU fallback in userspace processing pipelines.
1211*4882a593Smuzhiyun  *
1212*4882a593Smuzhiyun  *   Similar to the motivation for kernel cpu access it is again important that
1213*4882a593Smuzhiyun  *   the userspace code of a given importing subsystem can use the same
1214*4882a593Smuzhiyun  *   interfaces with a imported dma-buf buffer object as with a native buffer
1215*4882a593Smuzhiyun  *   object. This is especially important for drm where the userspace part of
1216*4882a593Smuzhiyun  *   contemporary OpenGL, X, and other drivers is huge, and reworking them to
1217*4882a593Smuzhiyun  *   use a different way to mmap a buffer rather invasive.
1218*4882a593Smuzhiyun  *
1219*4882a593Smuzhiyun  *   The assumption in the current dma-buf interfaces is that redirecting the
1220*4882a593Smuzhiyun  *   initial mmap is all that's needed. A survey of some of the existing
1221*4882a593Smuzhiyun  *   subsystems shows that no driver seems to do any nefarious thing like
1222*4882a593Smuzhiyun  *   syncing up with outstanding asynchronous processing on the device or
1223*4882a593Smuzhiyun  *   allocating special resources at fault time. So hopefully this is good
1224*4882a593Smuzhiyun  *   enough, since adding interfaces to intercept pagefaults and allow pte
1225*4882a593Smuzhiyun  *   shootdowns would increase the complexity quite a bit.
1226*4882a593Smuzhiyun  *
1227*4882a593Smuzhiyun  *   Interface::
1228*4882a593Smuzhiyun  *      int dma_buf_mmap(struct dma_buf \*, struct vm_area_struct \*,
1229*4882a593Smuzhiyun  *		       unsigned long);
1230*4882a593Smuzhiyun  *
1231*4882a593Smuzhiyun  *   If the importing subsystem simply provides a special-purpose mmap call to
1232*4882a593Smuzhiyun  *   set up a mapping in userspace, calling do_mmap with dma_buf->file will
1233*4882a593Smuzhiyun  *   equally achieve that for a dma-buf object.
1234*4882a593Smuzhiyun  */
1235*4882a593Smuzhiyun 
__dma_buf_begin_cpu_access(struct dma_buf * dmabuf,enum dma_data_direction direction)1236*4882a593Smuzhiyun static int __dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
1237*4882a593Smuzhiyun 				      enum dma_data_direction direction)
1238*4882a593Smuzhiyun {
1239*4882a593Smuzhiyun 	bool write = (direction == DMA_BIDIRECTIONAL ||
1240*4882a593Smuzhiyun 		      direction == DMA_TO_DEVICE);
1241*4882a593Smuzhiyun 	struct dma_resv *resv = dmabuf->resv;
1242*4882a593Smuzhiyun 	long ret;
1243*4882a593Smuzhiyun 
1244*4882a593Smuzhiyun 	/* Wait on any implicit rendering fences */
1245*4882a593Smuzhiyun 	ret = dma_resv_wait_timeout_rcu(resv, write, true,
1246*4882a593Smuzhiyun 						  MAX_SCHEDULE_TIMEOUT);
1247*4882a593Smuzhiyun 	if (ret < 0)
1248*4882a593Smuzhiyun 		return ret;
1249*4882a593Smuzhiyun 
1250*4882a593Smuzhiyun 	return 0;
1251*4882a593Smuzhiyun }
1252*4882a593Smuzhiyun 
1253*4882a593Smuzhiyun /**
1254*4882a593Smuzhiyun  * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
1255*4882a593Smuzhiyun  * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
1256*4882a593Smuzhiyun  * preparations. Coherency is only guaranteed in the specified range for the
1257*4882a593Smuzhiyun  * specified access direction.
1258*4882a593Smuzhiyun  * @dmabuf:	[in]	buffer to prepare cpu access for.
1259*4882a593Smuzhiyun  * @direction:	[in]	length of range for cpu access.
1260*4882a593Smuzhiyun  *
1261*4882a593Smuzhiyun  * After the cpu access is complete the caller should call
1262*4882a593Smuzhiyun  * dma_buf_end_cpu_access(). Only when cpu access is braketed by both calls is
1263*4882a593Smuzhiyun  * it guaranteed to be coherent with other DMA access.
1264*4882a593Smuzhiyun  *
1265*4882a593Smuzhiyun  * Can return negative error values, returns 0 on success.
1266*4882a593Smuzhiyun  */
dma_buf_begin_cpu_access(struct dma_buf * dmabuf,enum dma_data_direction direction)1267*4882a593Smuzhiyun int dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
1268*4882a593Smuzhiyun 			     enum dma_data_direction direction)
1269*4882a593Smuzhiyun {
1270*4882a593Smuzhiyun 	int ret = 0;
1271*4882a593Smuzhiyun 
1272*4882a593Smuzhiyun 	if (WARN_ON(!dmabuf))
1273*4882a593Smuzhiyun 		return -EINVAL;
1274*4882a593Smuzhiyun 
1275*4882a593Smuzhiyun 	if (dmabuf->ops->begin_cpu_access)
1276*4882a593Smuzhiyun 		ret = dmabuf->ops->begin_cpu_access(dmabuf, direction);
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun 	/* Ensure that all fences are waited upon - but we first allow
1279*4882a593Smuzhiyun 	 * the native handler the chance to do so more efficiently if it
1280*4882a593Smuzhiyun 	 * chooses. A double invocation here will be reasonably cheap no-op.
1281*4882a593Smuzhiyun 	 */
1282*4882a593Smuzhiyun 	if (ret == 0)
1283*4882a593Smuzhiyun 		ret = __dma_buf_begin_cpu_access(dmabuf, direction);
1284*4882a593Smuzhiyun 
1285*4882a593Smuzhiyun 	return ret;
1286*4882a593Smuzhiyun }
1287*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access);
1288*4882a593Smuzhiyun 
dma_buf_begin_cpu_access_partial(struct dma_buf * dmabuf,enum dma_data_direction direction,unsigned int offset,unsigned int len)1289*4882a593Smuzhiyun int dma_buf_begin_cpu_access_partial(struct dma_buf *dmabuf,
1290*4882a593Smuzhiyun 				     enum dma_data_direction direction,
1291*4882a593Smuzhiyun 				     unsigned int offset, unsigned int len)
1292*4882a593Smuzhiyun {
1293*4882a593Smuzhiyun 	int ret = 0;
1294*4882a593Smuzhiyun 
1295*4882a593Smuzhiyun 	if (WARN_ON(!dmabuf))
1296*4882a593Smuzhiyun 		return -EINVAL;
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun 	if (dmabuf->ops->begin_cpu_access_partial)
1299*4882a593Smuzhiyun 		ret = dmabuf->ops->begin_cpu_access_partial(dmabuf, direction,
1300*4882a593Smuzhiyun 							    offset, len);
1301*4882a593Smuzhiyun 
1302*4882a593Smuzhiyun 	/* Ensure that all fences are waited upon - but we first allow
1303*4882a593Smuzhiyun 	 * the native handler the chance to do so more efficiently if it
1304*4882a593Smuzhiyun 	 * chooses. A double invocation here will be reasonably cheap no-op.
1305*4882a593Smuzhiyun 	 */
1306*4882a593Smuzhiyun 	if (ret == 0)
1307*4882a593Smuzhiyun 		ret = __dma_buf_begin_cpu_access(dmabuf, direction);
1308*4882a593Smuzhiyun 
1309*4882a593Smuzhiyun 	return ret;
1310*4882a593Smuzhiyun }
1311*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access_partial);
1312*4882a593Smuzhiyun 
1313*4882a593Smuzhiyun /**
1314*4882a593Smuzhiyun  * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
1315*4882a593Smuzhiyun  * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
1316*4882a593Smuzhiyun  * actions. Coherency is only guaranteed in the specified range for the
1317*4882a593Smuzhiyun  * specified access direction.
1318*4882a593Smuzhiyun  * @dmabuf:	[in]	buffer to complete cpu access for.
1319*4882a593Smuzhiyun  * @direction:	[in]	length of range for cpu access.
1320*4882a593Smuzhiyun  *
1321*4882a593Smuzhiyun  * This terminates CPU access started with dma_buf_begin_cpu_access().
1322*4882a593Smuzhiyun  *
1323*4882a593Smuzhiyun  * Can return negative error values, returns 0 on success.
1324*4882a593Smuzhiyun  */
dma_buf_end_cpu_access(struct dma_buf * dmabuf,enum dma_data_direction direction)1325*4882a593Smuzhiyun int dma_buf_end_cpu_access(struct dma_buf *dmabuf,
1326*4882a593Smuzhiyun 			   enum dma_data_direction direction)
1327*4882a593Smuzhiyun {
1328*4882a593Smuzhiyun 	int ret = 0;
1329*4882a593Smuzhiyun 
1330*4882a593Smuzhiyun 	WARN_ON(!dmabuf);
1331*4882a593Smuzhiyun 
1332*4882a593Smuzhiyun 	if (dmabuf->ops->end_cpu_access)
1333*4882a593Smuzhiyun 		ret = dmabuf->ops->end_cpu_access(dmabuf, direction);
1334*4882a593Smuzhiyun 
1335*4882a593Smuzhiyun 	return ret;
1336*4882a593Smuzhiyun }
1337*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access);
1338*4882a593Smuzhiyun 
dma_buf_end_cpu_access_partial(struct dma_buf * dmabuf,enum dma_data_direction direction,unsigned int offset,unsigned int len)1339*4882a593Smuzhiyun int dma_buf_end_cpu_access_partial(struct dma_buf *dmabuf,
1340*4882a593Smuzhiyun 				   enum dma_data_direction direction,
1341*4882a593Smuzhiyun 				   unsigned int offset, unsigned int len)
1342*4882a593Smuzhiyun {
1343*4882a593Smuzhiyun 	int ret = 0;
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun 	WARN_ON(!dmabuf);
1346*4882a593Smuzhiyun 
1347*4882a593Smuzhiyun 	if (dmabuf->ops->end_cpu_access_partial)
1348*4882a593Smuzhiyun 		ret = dmabuf->ops->end_cpu_access_partial(dmabuf, direction,
1349*4882a593Smuzhiyun 							  offset, len);
1350*4882a593Smuzhiyun 
1351*4882a593Smuzhiyun 	return ret;
1352*4882a593Smuzhiyun }
1353*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access_partial);
1354*4882a593Smuzhiyun 
1355*4882a593Smuzhiyun /**
1356*4882a593Smuzhiyun  * dma_buf_mmap - Setup up a userspace mmap with the given vma
1357*4882a593Smuzhiyun  * @dmabuf:	[in]	buffer that should back the vma
1358*4882a593Smuzhiyun  * @vma:	[in]	vma for the mmap
1359*4882a593Smuzhiyun  * @pgoff:	[in]	offset in pages where this mmap should start within the
1360*4882a593Smuzhiyun  *			dma-buf buffer.
1361*4882a593Smuzhiyun  *
1362*4882a593Smuzhiyun  * This function adjusts the passed in vma so that it points at the file of the
1363*4882a593Smuzhiyun  * dma_buf operation. It also adjusts the starting pgoff and does bounds
1364*4882a593Smuzhiyun  * checking on the size of the vma. Then it calls the exporters mmap function to
1365*4882a593Smuzhiyun  * set up the mapping.
1366*4882a593Smuzhiyun  *
1367*4882a593Smuzhiyun  * Can return negative error values, returns 0 on success.
1368*4882a593Smuzhiyun  */
dma_buf_mmap(struct dma_buf * dmabuf,struct vm_area_struct * vma,unsigned long pgoff)1369*4882a593Smuzhiyun int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
1370*4882a593Smuzhiyun 		 unsigned long pgoff)
1371*4882a593Smuzhiyun {
1372*4882a593Smuzhiyun 	struct file *oldfile;
1373*4882a593Smuzhiyun 	int ret;
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun 	if (WARN_ON(!dmabuf || !vma))
1376*4882a593Smuzhiyun 		return -EINVAL;
1377*4882a593Smuzhiyun 
1378*4882a593Smuzhiyun 	/* check if buffer supports mmap */
1379*4882a593Smuzhiyun 	if (!dmabuf->ops->mmap)
1380*4882a593Smuzhiyun 		return -EINVAL;
1381*4882a593Smuzhiyun 
1382*4882a593Smuzhiyun 	/* check for offset overflow */
1383*4882a593Smuzhiyun 	if (pgoff + vma_pages(vma) < pgoff)
1384*4882a593Smuzhiyun 		return -EOVERFLOW;
1385*4882a593Smuzhiyun 
1386*4882a593Smuzhiyun 	/* check for overflowing the buffer's size */
1387*4882a593Smuzhiyun 	if (pgoff + vma_pages(vma) >
1388*4882a593Smuzhiyun 	    dmabuf->size >> PAGE_SHIFT)
1389*4882a593Smuzhiyun 		return -EINVAL;
1390*4882a593Smuzhiyun 
1391*4882a593Smuzhiyun 	/* readjust the vma */
1392*4882a593Smuzhiyun 	get_file(dmabuf->file);
1393*4882a593Smuzhiyun 	oldfile = vma->vm_file;
1394*4882a593Smuzhiyun 	vma->vm_file = dmabuf->file;
1395*4882a593Smuzhiyun 	vma->vm_pgoff = pgoff;
1396*4882a593Smuzhiyun 
1397*4882a593Smuzhiyun 	ret = dmabuf->ops->mmap(dmabuf, vma);
1398*4882a593Smuzhiyun 	if (ret) {
1399*4882a593Smuzhiyun 		/* restore old parameters on failure */
1400*4882a593Smuzhiyun 		vma->vm_file = oldfile;
1401*4882a593Smuzhiyun 		fput(dmabuf->file);
1402*4882a593Smuzhiyun 	} else {
1403*4882a593Smuzhiyun 		if (oldfile)
1404*4882a593Smuzhiyun 			fput(oldfile);
1405*4882a593Smuzhiyun 	}
1406*4882a593Smuzhiyun 	return ret;
1407*4882a593Smuzhiyun 
1408*4882a593Smuzhiyun }
1409*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_mmap);
1410*4882a593Smuzhiyun 
1411*4882a593Smuzhiyun /**
1412*4882a593Smuzhiyun  * dma_buf_vmap - Create virtual mapping for the buffer object into kernel
1413*4882a593Smuzhiyun  * address space. Same restrictions as for vmap and friends apply.
1414*4882a593Smuzhiyun  * @dmabuf:	[in]	buffer to vmap
1415*4882a593Smuzhiyun  *
1416*4882a593Smuzhiyun  * This call may fail due to lack of virtual mapping address space.
1417*4882a593Smuzhiyun  * These calls are optional in drivers. The intended use for them
1418*4882a593Smuzhiyun  * is for mapping objects linear in kernel space for high use objects.
1419*4882a593Smuzhiyun  * Please attempt to use kmap/kunmap before thinking about these interfaces.
1420*4882a593Smuzhiyun  *
1421*4882a593Smuzhiyun  * Returns NULL on error.
1422*4882a593Smuzhiyun  */
dma_buf_vmap(struct dma_buf * dmabuf)1423*4882a593Smuzhiyun void *dma_buf_vmap(struct dma_buf *dmabuf)
1424*4882a593Smuzhiyun {
1425*4882a593Smuzhiyun 	void *ptr;
1426*4882a593Smuzhiyun 
1427*4882a593Smuzhiyun 	if (WARN_ON(!dmabuf))
1428*4882a593Smuzhiyun 		return NULL;
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun 	if (!dmabuf->ops->vmap)
1431*4882a593Smuzhiyun 		return NULL;
1432*4882a593Smuzhiyun 
1433*4882a593Smuzhiyun 	mutex_lock(&dmabuf->lock);
1434*4882a593Smuzhiyun 	if (dmabuf->vmapping_counter) {
1435*4882a593Smuzhiyun 		dmabuf->vmapping_counter++;
1436*4882a593Smuzhiyun 		BUG_ON(!dmabuf->vmap_ptr);
1437*4882a593Smuzhiyun 		ptr = dmabuf->vmap_ptr;
1438*4882a593Smuzhiyun 		goto out_unlock;
1439*4882a593Smuzhiyun 	}
1440*4882a593Smuzhiyun 
1441*4882a593Smuzhiyun 	BUG_ON(dmabuf->vmap_ptr);
1442*4882a593Smuzhiyun 
1443*4882a593Smuzhiyun 	ptr = dmabuf->ops->vmap(dmabuf);
1444*4882a593Smuzhiyun 	if (WARN_ON_ONCE(IS_ERR(ptr)))
1445*4882a593Smuzhiyun 		ptr = NULL;
1446*4882a593Smuzhiyun 	if (!ptr)
1447*4882a593Smuzhiyun 		goto out_unlock;
1448*4882a593Smuzhiyun 
1449*4882a593Smuzhiyun 	dmabuf->vmap_ptr = ptr;
1450*4882a593Smuzhiyun 	dmabuf->vmapping_counter = 1;
1451*4882a593Smuzhiyun 
1452*4882a593Smuzhiyun out_unlock:
1453*4882a593Smuzhiyun 	mutex_unlock(&dmabuf->lock);
1454*4882a593Smuzhiyun 	return ptr;
1455*4882a593Smuzhiyun }
1456*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_vmap);
1457*4882a593Smuzhiyun 
1458*4882a593Smuzhiyun /**
1459*4882a593Smuzhiyun  * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
1460*4882a593Smuzhiyun  * @dmabuf:	[in]	buffer to vunmap
1461*4882a593Smuzhiyun  * @vaddr:	[in]	vmap to vunmap
1462*4882a593Smuzhiyun  */
dma_buf_vunmap(struct dma_buf * dmabuf,void * vaddr)1463*4882a593Smuzhiyun void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
1464*4882a593Smuzhiyun {
1465*4882a593Smuzhiyun 	if (WARN_ON(!dmabuf))
1466*4882a593Smuzhiyun 		return;
1467*4882a593Smuzhiyun 
1468*4882a593Smuzhiyun 	BUG_ON(!dmabuf->vmap_ptr);
1469*4882a593Smuzhiyun 	BUG_ON(dmabuf->vmapping_counter == 0);
1470*4882a593Smuzhiyun 	BUG_ON(dmabuf->vmap_ptr != vaddr);
1471*4882a593Smuzhiyun 
1472*4882a593Smuzhiyun 	mutex_lock(&dmabuf->lock);
1473*4882a593Smuzhiyun 	if (--dmabuf->vmapping_counter == 0) {
1474*4882a593Smuzhiyun 		if (dmabuf->ops->vunmap)
1475*4882a593Smuzhiyun 			dmabuf->ops->vunmap(dmabuf, vaddr);
1476*4882a593Smuzhiyun 		dmabuf->vmap_ptr = NULL;
1477*4882a593Smuzhiyun 	}
1478*4882a593Smuzhiyun 	mutex_unlock(&dmabuf->lock);
1479*4882a593Smuzhiyun }
1480*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_vunmap);
1481*4882a593Smuzhiyun 
dma_buf_get_flags(struct dma_buf * dmabuf,unsigned long * flags)1482*4882a593Smuzhiyun int dma_buf_get_flags(struct dma_buf *dmabuf, unsigned long *flags)
1483*4882a593Smuzhiyun {
1484*4882a593Smuzhiyun 	int ret = 0;
1485*4882a593Smuzhiyun 
1486*4882a593Smuzhiyun 	if (WARN_ON(!dmabuf) || !flags)
1487*4882a593Smuzhiyun 		return -EINVAL;
1488*4882a593Smuzhiyun 
1489*4882a593Smuzhiyun 	if (dmabuf->ops->get_flags)
1490*4882a593Smuzhiyun 		ret = dmabuf->ops->get_flags(dmabuf, flags);
1491*4882a593Smuzhiyun 
1492*4882a593Smuzhiyun 	return ret;
1493*4882a593Smuzhiyun }
1494*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_get_flags);
1495*4882a593Smuzhiyun 
dma_buf_get_uuid(struct dma_buf * dmabuf,uuid_t * uuid)1496*4882a593Smuzhiyun int dma_buf_get_uuid(struct dma_buf *dmabuf, uuid_t *uuid)
1497*4882a593Smuzhiyun {
1498*4882a593Smuzhiyun 	if (WARN_ON(!dmabuf) || !uuid)
1499*4882a593Smuzhiyun 		return -EINVAL;
1500*4882a593Smuzhiyun 
1501*4882a593Smuzhiyun 	if (!dmabuf->ops->get_uuid)
1502*4882a593Smuzhiyun 		return -ENODEV;
1503*4882a593Smuzhiyun 
1504*4882a593Smuzhiyun 	return dmabuf->ops->get_uuid(dmabuf, uuid);
1505*4882a593Smuzhiyun }
1506*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dma_buf_get_uuid);
1507*4882a593Smuzhiyun 
1508*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
dma_buf_debug_show(struct seq_file * s,void * unused)1509*4882a593Smuzhiyun static int dma_buf_debug_show(struct seq_file *s, void *unused)
1510*4882a593Smuzhiyun {
1511*4882a593Smuzhiyun 	int ret;
1512*4882a593Smuzhiyun 	struct dma_buf *buf_obj;
1513*4882a593Smuzhiyun 	struct dma_buf_attachment *attach_obj;
1514*4882a593Smuzhiyun 	struct dma_resv *robj;
1515*4882a593Smuzhiyun 	struct dma_resv_list *fobj;
1516*4882a593Smuzhiyun 	struct dma_fence *fence;
1517*4882a593Smuzhiyun 	unsigned seq;
1518*4882a593Smuzhiyun 	int count = 0, attach_count, shared_count, i;
1519*4882a593Smuzhiyun 	size_t size = 0;
1520*4882a593Smuzhiyun 
1521*4882a593Smuzhiyun 	ret = mutex_lock_interruptible(&db_list.lock);
1522*4882a593Smuzhiyun 
1523*4882a593Smuzhiyun 	if (ret)
1524*4882a593Smuzhiyun 		return ret;
1525*4882a593Smuzhiyun 
1526*4882a593Smuzhiyun 	seq_puts(s, "\nDma-buf Objects:\n");
1527*4882a593Smuzhiyun 	seq_printf(s, "%-8s\t%-8s\t%-8s\t%-8s\texp_name\t%-8s\n",
1528*4882a593Smuzhiyun 		   "size", "flags", "mode", "count", "ino");
1529*4882a593Smuzhiyun 
1530*4882a593Smuzhiyun 	list_for_each_entry(buf_obj, &db_list.head, list_node) {
1531*4882a593Smuzhiyun 
1532*4882a593Smuzhiyun 		ret = dma_resv_lock_interruptible(buf_obj->resv, NULL);
1533*4882a593Smuzhiyun 		if (ret)
1534*4882a593Smuzhiyun 			goto error_unlock;
1535*4882a593Smuzhiyun 
1536*4882a593Smuzhiyun 		spin_lock(&buf_obj->name_lock);
1537*4882a593Smuzhiyun 		seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\t%08lu\t%s\n",
1538*4882a593Smuzhiyun 				buf_obj->size,
1539*4882a593Smuzhiyun 				buf_obj->file->f_flags, buf_obj->file->f_mode,
1540*4882a593Smuzhiyun 				file_count(buf_obj->file),
1541*4882a593Smuzhiyun 				buf_obj->exp_name,
1542*4882a593Smuzhiyun 				file_inode(buf_obj->file)->i_ino,
1543*4882a593Smuzhiyun 				buf_obj->name ?: "");
1544*4882a593Smuzhiyun 		spin_unlock(&buf_obj->name_lock);
1545*4882a593Smuzhiyun 
1546*4882a593Smuzhiyun 		robj = buf_obj->resv;
1547*4882a593Smuzhiyun 		while (true) {
1548*4882a593Smuzhiyun 			seq = read_seqcount_begin(&robj->seq);
1549*4882a593Smuzhiyun 			rcu_read_lock();
1550*4882a593Smuzhiyun 			fobj = rcu_dereference(robj->fence);
1551*4882a593Smuzhiyun 			shared_count = fobj ? fobj->shared_count : 0;
1552*4882a593Smuzhiyun 			fence = rcu_dereference(robj->fence_excl);
1553*4882a593Smuzhiyun 			if (!read_seqcount_retry(&robj->seq, seq))
1554*4882a593Smuzhiyun 				break;
1555*4882a593Smuzhiyun 			rcu_read_unlock();
1556*4882a593Smuzhiyun 		}
1557*4882a593Smuzhiyun 
1558*4882a593Smuzhiyun 		if (fence)
1559*4882a593Smuzhiyun 			seq_printf(s, "\tExclusive fence: %s %s %ssignalled\n",
1560*4882a593Smuzhiyun 				   fence->ops->get_driver_name(fence),
1561*4882a593Smuzhiyun 				   fence->ops->get_timeline_name(fence),
1562*4882a593Smuzhiyun 				   dma_fence_is_signaled(fence) ? "" : "un");
1563*4882a593Smuzhiyun 		for (i = 0; i < shared_count; i++) {
1564*4882a593Smuzhiyun 			fence = rcu_dereference(fobj->shared[i]);
1565*4882a593Smuzhiyun 			if (!dma_fence_get_rcu(fence))
1566*4882a593Smuzhiyun 				continue;
1567*4882a593Smuzhiyun 			seq_printf(s, "\tShared fence: %s %s %ssignalled\n",
1568*4882a593Smuzhiyun 				   fence->ops->get_driver_name(fence),
1569*4882a593Smuzhiyun 				   fence->ops->get_timeline_name(fence),
1570*4882a593Smuzhiyun 				   dma_fence_is_signaled(fence) ? "" : "un");
1571*4882a593Smuzhiyun 			dma_fence_put(fence);
1572*4882a593Smuzhiyun 		}
1573*4882a593Smuzhiyun 		rcu_read_unlock();
1574*4882a593Smuzhiyun 
1575*4882a593Smuzhiyun 		seq_puts(s, "\tAttached Devices:\n");
1576*4882a593Smuzhiyun 		attach_count = 0;
1577*4882a593Smuzhiyun 
1578*4882a593Smuzhiyun 		list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
1579*4882a593Smuzhiyun 			seq_printf(s, "\t%s\n", dev_name(attach_obj->dev));
1580*4882a593Smuzhiyun 			attach_count++;
1581*4882a593Smuzhiyun 		}
1582*4882a593Smuzhiyun 		dma_resv_unlock(buf_obj->resv);
1583*4882a593Smuzhiyun 
1584*4882a593Smuzhiyun 		seq_printf(s, "Total %d devices attached\n\n",
1585*4882a593Smuzhiyun 				attach_count);
1586*4882a593Smuzhiyun 
1587*4882a593Smuzhiyun 		count++;
1588*4882a593Smuzhiyun 		size += buf_obj->size;
1589*4882a593Smuzhiyun 	}
1590*4882a593Smuzhiyun 
1591*4882a593Smuzhiyun 	seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size);
1592*4882a593Smuzhiyun 
1593*4882a593Smuzhiyun 	mutex_unlock(&db_list.lock);
1594*4882a593Smuzhiyun 	return 0;
1595*4882a593Smuzhiyun 
1596*4882a593Smuzhiyun error_unlock:
1597*4882a593Smuzhiyun 	mutex_unlock(&db_list.lock);
1598*4882a593Smuzhiyun 	return ret;
1599*4882a593Smuzhiyun }
1600*4882a593Smuzhiyun 
1601*4882a593Smuzhiyun DEFINE_SHOW_ATTRIBUTE(dma_buf_debug);
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun static struct dentry *dma_buf_debugfs_dir;
1604*4882a593Smuzhiyun 
dma_buf_init_debugfs(void)1605*4882a593Smuzhiyun static int dma_buf_init_debugfs(void)
1606*4882a593Smuzhiyun {
1607*4882a593Smuzhiyun 	struct dentry *d;
1608*4882a593Smuzhiyun 	int err = 0;
1609*4882a593Smuzhiyun 
1610*4882a593Smuzhiyun 	d = debugfs_create_dir("dma_buf", NULL);
1611*4882a593Smuzhiyun 	if (IS_ERR(d))
1612*4882a593Smuzhiyun 		return PTR_ERR(d);
1613*4882a593Smuzhiyun 
1614*4882a593Smuzhiyun 	dma_buf_debugfs_dir = d;
1615*4882a593Smuzhiyun 
1616*4882a593Smuzhiyun 	d = debugfs_create_file("bufinfo", S_IRUGO, dma_buf_debugfs_dir,
1617*4882a593Smuzhiyun 				NULL, &dma_buf_debug_fops);
1618*4882a593Smuzhiyun 	if (IS_ERR(d)) {
1619*4882a593Smuzhiyun 		pr_debug("dma_buf: debugfs: failed to create node bufinfo\n");
1620*4882a593Smuzhiyun 		debugfs_remove_recursive(dma_buf_debugfs_dir);
1621*4882a593Smuzhiyun 		dma_buf_debugfs_dir = NULL;
1622*4882a593Smuzhiyun 		err = PTR_ERR(d);
1623*4882a593Smuzhiyun 	}
1624*4882a593Smuzhiyun 
1625*4882a593Smuzhiyun 	return err;
1626*4882a593Smuzhiyun }
1627*4882a593Smuzhiyun 
dma_buf_uninit_debugfs(void)1628*4882a593Smuzhiyun static void dma_buf_uninit_debugfs(void)
1629*4882a593Smuzhiyun {
1630*4882a593Smuzhiyun 	debugfs_remove_recursive(dma_buf_debugfs_dir);
1631*4882a593Smuzhiyun }
1632*4882a593Smuzhiyun #else
dma_buf_init_debugfs(void)1633*4882a593Smuzhiyun static inline int dma_buf_init_debugfs(void)
1634*4882a593Smuzhiyun {
1635*4882a593Smuzhiyun 	return 0;
1636*4882a593Smuzhiyun }
dma_buf_uninit_debugfs(void)1637*4882a593Smuzhiyun static inline void dma_buf_uninit_debugfs(void)
1638*4882a593Smuzhiyun {
1639*4882a593Smuzhiyun }
1640*4882a593Smuzhiyun #endif
1641*4882a593Smuzhiyun 
dma_buf_init(void)1642*4882a593Smuzhiyun static int __init dma_buf_init(void)
1643*4882a593Smuzhiyun {
1644*4882a593Smuzhiyun 	int ret;
1645*4882a593Smuzhiyun 
1646*4882a593Smuzhiyun 	ret = dma_buf_init_sysfs_statistics();
1647*4882a593Smuzhiyun 	if (ret)
1648*4882a593Smuzhiyun 		return ret;
1649*4882a593Smuzhiyun 
1650*4882a593Smuzhiyun 	dma_buf_mnt = kern_mount(&dma_buf_fs_type);
1651*4882a593Smuzhiyun 	if (IS_ERR(dma_buf_mnt))
1652*4882a593Smuzhiyun 		return PTR_ERR(dma_buf_mnt);
1653*4882a593Smuzhiyun 
1654*4882a593Smuzhiyun 	mutex_init(&db_list.lock);
1655*4882a593Smuzhiyun 	INIT_LIST_HEAD(&db_list.head);
1656*4882a593Smuzhiyun 	dma_buf_init_debugfs();
1657*4882a593Smuzhiyun 	return 0;
1658*4882a593Smuzhiyun }
1659*4882a593Smuzhiyun subsys_initcall(dma_buf_init);
1660*4882a593Smuzhiyun 
dma_buf_deinit(void)1661*4882a593Smuzhiyun static void __exit dma_buf_deinit(void)
1662*4882a593Smuzhiyun {
1663*4882a593Smuzhiyun 	dma_buf_uninit_debugfs();
1664*4882a593Smuzhiyun 	kern_unmount(dma_buf_mnt);
1665*4882a593Smuzhiyun 	dma_buf_uninit_sysfs_statistics();
1666*4882a593Smuzhiyun }
1667*4882a593Smuzhiyun __exitcall(dma_buf_deinit);
1668