blob: 13884474d1588f7a086154d3b06dbd09ff28e881 [file] [log] [blame]
Sumit Semwald15bd7e2011-12-26 14:53:15 +05301/*
2 * Framework for buffer objects that can be shared across devices/subsystems.
3 *
4 * Copyright(C) 2011 Linaro Limited. All rights reserved.
5 * Author: Sumit Semwal <sumit.semwal@ti.com>
6 *
7 * Many thanks to linaro-mm-sig list, and specially
8 * Arnd Bergmann <arnd@arndb.de>, Rob Clark <rob@ti.com> and
9 * Daniel Vetter <daniel@ffwll.ch> for their support in creation and
10 * refining of this idea.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published by
14 * the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * this program. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25#include <linux/fs.h>
26#include <linux/slab.h>
27#include <linux/dma-buf.h>
Chris Wilsonf54d1862016-10-25 13:00:45 +010028#include <linux/dma-fence.h>
Sumit Semwald15bd7e2011-12-26 14:53:15 +053029#include <linux/anon_inodes.h>
30#include <linux/export.h>
Sumit Semwalb89e35632013-04-04 11:44:37 +053031#include <linux/debugfs.h>
Sumit Semwal9abdffe2015-05-05 14:56:15 +053032#include <linux/module.h>
Sumit Semwalb89e35632013-04-04 11:44:37 +053033#include <linux/seq_file.h>
Maarten Lankhorst9b495a52014-07-01 12:57:43 +020034#include <linux/poll.h>
Maarten Lankhorst3aac4502014-07-01 12:57:26 +020035#include <linux/reservation.h>
Muhammad Falak R Wanib02da6f2016-05-23 17:08:42 +053036#include <linux/mm.h>
Sumit Semwald15bd7e2011-12-26 14:53:15 +053037
Daniel Vetterc11e3912016-02-11 20:04:51 -020038#include <uapi/linux/dma-buf.h>
39
Sumit Semwald15bd7e2011-12-26 14:53:15 +053040static inline int is_dma_buf_file(struct file *);
41
Sumit Semwalb89e35632013-04-04 11:44:37 +053042struct dma_buf_list {
43 struct list_head head;
44 struct mutex lock;
45};
46
47static struct dma_buf_list db_list;
48
Sumit Semwald15bd7e2011-12-26 14:53:15 +053049static int dma_buf_release(struct inode *inode, struct file *file)
50{
51 struct dma_buf *dmabuf;
52
53 if (!is_dma_buf_file(file))
54 return -EINVAL;
55
56 dmabuf = file->private_data;
57
Daniel Vetterf00b4da2012-12-20 14:14:23 +010058 BUG_ON(dmabuf->vmapping_counter);
59
Maarten Lankhorst9b495a52014-07-01 12:57:43 +020060 /*
61 * Any fences that a dma-buf poll can wait on should be signaled
62 * before releasing dma-buf. This is the responsibility of each
63 * driver that uses the reservation objects.
64 *
65 * If you hit this BUG() it means someone dropped their ref to the
66 * dma-buf while still having pending operation to the buffer.
67 */
68 BUG_ON(dmabuf->cb_shared.active || dmabuf->cb_excl.active);
69
Sumit Semwald15bd7e2011-12-26 14:53:15 +053070 dmabuf->ops->release(dmabuf);
Sumit Semwalb89e35632013-04-04 11:44:37 +053071
72 mutex_lock(&db_list.lock);
73 list_del(&dmabuf->list_node);
74 mutex_unlock(&db_list.lock);
75
Maarten Lankhorst3aac4502014-07-01 12:57:26 +020076 if (dmabuf->resv == (struct reservation_object *)&dmabuf[1])
77 reservation_object_fini(dmabuf->resv);
78
Sumit Semwal9abdffe2015-05-05 14:56:15 +053079 module_put(dmabuf->owner);
Sumit Semwald15bd7e2011-12-26 14:53:15 +053080 kfree(dmabuf);
81 return 0;
82}
83
Daniel Vetter4c785132012-04-24 14:38:52 +053084static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
85{
86 struct dma_buf *dmabuf;
87
88 if (!is_dma_buf_file(file))
89 return -EINVAL;
90
91 dmabuf = file->private_data;
92
93 /* check for overflowing the buffer's size */
Muhammad Falak R Wanib02da6f2016-05-23 17:08:42 +053094 if (vma->vm_pgoff + vma_pages(vma) >
Daniel Vetter4c785132012-04-24 14:38:52 +053095 dmabuf->size >> PAGE_SHIFT)
96 return -EINVAL;
97
98 return dmabuf->ops->mmap(dmabuf, vma);
99}
100
Christopher James Halse Rogers19e86972013-09-10 11:36:45 +0530101static loff_t dma_buf_llseek(struct file *file, loff_t offset, int whence)
102{
103 struct dma_buf *dmabuf;
104 loff_t base;
105
106 if (!is_dma_buf_file(file))
107 return -EBADF;
108
109 dmabuf = file->private_data;
110
111 /* only support discovering the end of the buffer,
112 but also allow SEEK_SET to maintain the idiomatic
113 SEEK_END(0), SEEK_CUR(0) pattern */
114 if (whence == SEEK_END)
115 base = dmabuf->size;
116 else if (whence == SEEK_SET)
117 base = 0;
118 else
119 return -EINVAL;
120
121 if (offset != 0)
122 return -EINVAL;
123
124 return base + offset;
125}
126
Daniel Vettere7e21c72016-12-09 22:50:55 +0100127/**
128 * DOC: fence polling
129 *
130 * To support cross-device and cross-driver synchronization of buffer access
Daniel Vetterf641d3b2016-12-29 21:48:24 +0100131 * implicit fences (represented internally in the kernel with &struct fence) can
Daniel Vettere7e21c72016-12-09 22:50:55 +0100132 * be attached to a &dma_buf. The glue for that and a few related things are
133 * provided in the &reservation_object structure.
134 *
135 * Userspace can query the state of these implicitly tracked fences using poll()
136 * and related system calls:
137 *
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800138 * - Checking for EPOLLIN, i.e. read access, can be use to query the state of the
Daniel Vettere7e21c72016-12-09 22:50:55 +0100139 * most recent write or exclusive fence.
140 *
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800141 * - Checking for EPOLLOUT, i.e. write access, can be used to query the state of
Daniel Vettere7e21c72016-12-09 22:50:55 +0100142 * all attached fences, shared and exclusive ones.
143 *
144 * Note that this only signals the completion of the respective fences, i.e. the
145 * DMA transfers are complete. Cache flushing and any other necessary
146 * preparations before CPU access can begin still need to happen.
147 */
148
Chris Wilsonf54d1862016-10-25 13:00:45 +0100149static void dma_buf_poll_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200150{
151 struct dma_buf_poll_cb_t *dcb = (struct dma_buf_poll_cb_t *)cb;
152 unsigned long flags;
153
154 spin_lock_irqsave(&dcb->poll->lock, flags);
155 wake_up_locked_poll(dcb->poll, dcb->active);
156 dcb->active = 0;
157 spin_unlock_irqrestore(&dcb->poll->lock, flags);
158}
159
Al Viroafc9a422017-07-03 06:39:46 -0400160static __poll_t dma_buf_poll(struct file *file, poll_table *poll)
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200161{
162 struct dma_buf *dmabuf;
163 struct reservation_object *resv;
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200164 struct reservation_object_list *fobj;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100165 struct dma_fence *fence_excl;
Al Viro01699432017-07-03 03:14:15 -0400166 __poll_t events;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200167 unsigned shared_count, seq;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200168
169 dmabuf = file->private_data;
170 if (!dmabuf || !dmabuf->resv)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800171 return EPOLLERR;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200172
173 resv = dmabuf->resv;
174
175 poll_wait(file, &dmabuf->poll, poll);
176
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800177 events = poll_requested_events(poll) & (EPOLLIN | EPOLLOUT);
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200178 if (!events)
179 return 0;
180
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200181retry:
182 seq = read_seqcount_begin(&resv->seq);
183 rcu_read_lock();
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200184
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200185 fobj = rcu_dereference(resv->fence);
186 if (fobj)
187 shared_count = fobj->shared_count;
188 else
189 shared_count = 0;
190 fence_excl = rcu_dereference(resv->fence_excl);
191 if (read_seqcount_retry(&resv->seq, seq)) {
192 rcu_read_unlock();
193 goto retry;
194 }
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200195
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800196 if (fence_excl && (!(events & EPOLLOUT) || shared_count == 0)) {
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200197 struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_excl;
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800198 __poll_t pevents = EPOLLIN;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200199
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200200 if (shared_count == 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800201 pevents |= EPOLLOUT;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200202
203 spin_lock_irq(&dmabuf->poll.lock);
204 if (dcb->active) {
205 dcb->active |= pevents;
206 events &= ~pevents;
207 } else
208 dcb->active = pevents;
209 spin_unlock_irq(&dmabuf->poll.lock);
210
211 if (events & pevents) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100212 if (!dma_fence_get_rcu(fence_excl)) {
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200213 /* force a recheck */
214 events &= ~pevents;
215 dma_buf_poll_cb(NULL, &dcb->cb);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100216 } else if (!dma_fence_add_callback(fence_excl, &dcb->cb,
217 dma_buf_poll_cb)) {
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200218 events &= ~pevents;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100219 dma_fence_put(fence_excl);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200220 } else {
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200221 /*
222 * No callback queued, wake up any additional
223 * waiters.
224 */
Chris Wilsonf54d1862016-10-25 13:00:45 +0100225 dma_fence_put(fence_excl);
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200226 dma_buf_poll_cb(NULL, &dcb->cb);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200227 }
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200228 }
229 }
230
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800231 if ((events & EPOLLOUT) && shared_count > 0) {
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200232 struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_shared;
233 int i;
234
235 /* Only queue a new callback if no event has fired yet */
236 spin_lock_irq(&dmabuf->poll.lock);
237 if (dcb->active)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800238 events &= ~EPOLLOUT;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200239 else
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800240 dcb->active = EPOLLOUT;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200241 spin_unlock_irq(&dmabuf->poll.lock);
242
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800243 if (!(events & EPOLLOUT))
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200244 goto out;
245
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200246 for (i = 0; i < shared_count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100247 struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200248
Chris Wilsonf54d1862016-10-25 13:00:45 +0100249 if (!dma_fence_get_rcu(fence)) {
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200250 /*
251 * fence refcount dropped to zero, this means
252 * that fobj has been freed
253 *
254 * call dma_buf_poll_cb and force a recheck!
255 */
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800256 events &= ~EPOLLOUT;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200257 dma_buf_poll_cb(NULL, &dcb->cb);
258 break;
259 }
Chris Wilsonf54d1862016-10-25 13:00:45 +0100260 if (!dma_fence_add_callback(fence, &dcb->cb,
261 dma_buf_poll_cb)) {
262 dma_fence_put(fence);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800263 events &= ~EPOLLOUT;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200264 break;
265 }
Chris Wilsonf54d1862016-10-25 13:00:45 +0100266 dma_fence_put(fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200267 }
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200268
269 /* No callback queued, wake up any additional waiters. */
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200270 if (i == shared_count)
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200271 dma_buf_poll_cb(NULL, &dcb->cb);
272 }
273
274out:
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200275 rcu_read_unlock();
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200276 return events;
277}
278
Daniel Vetterc11e3912016-02-11 20:04:51 -0200279static long dma_buf_ioctl(struct file *file,
280 unsigned int cmd, unsigned long arg)
281{
282 struct dma_buf *dmabuf;
283 struct dma_buf_sync sync;
284 enum dma_data_direction direction;
Chris Wilson18b862d2016-03-18 20:02:39 +0000285 int ret;
Daniel Vetterc11e3912016-02-11 20:04:51 -0200286
287 dmabuf = file->private_data;
288
289 switch (cmd) {
290 case DMA_BUF_IOCTL_SYNC:
291 if (copy_from_user(&sync, (void __user *) arg, sizeof(sync)))
292 return -EFAULT;
293
294 if (sync.flags & ~DMA_BUF_SYNC_VALID_FLAGS_MASK)
295 return -EINVAL;
296
297 switch (sync.flags & DMA_BUF_SYNC_RW) {
298 case DMA_BUF_SYNC_READ:
299 direction = DMA_FROM_DEVICE;
300 break;
301 case DMA_BUF_SYNC_WRITE:
302 direction = DMA_TO_DEVICE;
303 break;
304 case DMA_BUF_SYNC_RW:
305 direction = DMA_BIDIRECTIONAL;
306 break;
307 default:
308 return -EINVAL;
309 }
310
311 if (sync.flags & DMA_BUF_SYNC_END)
Chris Wilson18b862d2016-03-18 20:02:39 +0000312 ret = dma_buf_end_cpu_access(dmabuf, direction);
Daniel Vetterc11e3912016-02-11 20:04:51 -0200313 else
Chris Wilson18b862d2016-03-18 20:02:39 +0000314 ret = dma_buf_begin_cpu_access(dmabuf, direction);
Daniel Vetterc11e3912016-02-11 20:04:51 -0200315
Chris Wilson18b862d2016-03-18 20:02:39 +0000316 return ret;
Daniel Vetterc11e3912016-02-11 20:04:51 -0200317 default:
318 return -ENOTTY;
319 }
320}
321
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530322static const struct file_operations dma_buf_fops = {
323 .release = dma_buf_release,
Daniel Vetter4c785132012-04-24 14:38:52 +0530324 .mmap = dma_buf_mmap_internal,
Christopher James Halse Rogers19e86972013-09-10 11:36:45 +0530325 .llseek = dma_buf_llseek,
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200326 .poll = dma_buf_poll,
Daniel Vetterc11e3912016-02-11 20:04:51 -0200327 .unlocked_ioctl = dma_buf_ioctl,
Marek Szyprowski888022c2017-02-21 14:21:01 +0100328#ifdef CONFIG_COMPAT
329 .compat_ioctl = dma_buf_ioctl,
330#endif
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530331};
332
333/*
334 * is_dma_buf_file - Check if struct file* is associated with dma_buf
335 */
336static inline int is_dma_buf_file(struct file *file)
337{
338 return file->f_op == &dma_buf_fops;
339}
340
341/**
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100342 * DOC: dma buf device access
343 *
344 * For device DMA access to a shared DMA buffer the usual sequence of operations
345 * is fairly simple:
346 *
347 * 1. The exporter defines his exporter instance using
348 * DEFINE_DMA_BUF_EXPORT_INFO() and calls dma_buf_export() to wrap a private
349 * buffer object into a &dma_buf. It then exports that &dma_buf to userspace
350 * as a file descriptor by calling dma_buf_fd().
351 *
352 * 2. Userspace passes this file-descriptors to all drivers it wants this buffer
353 * to share with: First the filedescriptor is converted to a &dma_buf using
Liviu Dudauc1387822017-11-01 14:06:30 +0000354 * dma_buf_get(). Then the buffer is attached to the device using
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100355 * dma_buf_attach().
356 *
357 * Up to this stage the exporter is still free to migrate or reallocate the
358 * backing storage.
359 *
Liviu Dudauc1387822017-11-01 14:06:30 +0000360 * 3. Once the buffer is attached to all devices userspace can initiate DMA
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100361 * access to the shared buffer. In the kernel this is done by calling
362 * dma_buf_map_attachment() and dma_buf_unmap_attachment().
363 *
364 * 4. Once a driver is done with a shared buffer it needs to call
365 * dma_buf_detach() (after cleaning up any mappings) and then release the
366 * reference acquired with dma_buf_get by calling dma_buf_put().
367 *
368 * For the detailed semantics exporters are expected to implement see
369 * &dma_buf_ops.
370 */
371
372/**
Sumit Semwald8fbe342015-01-23 12:53:43 +0530373 * dma_buf_export - Creates a new dma_buf, and associates an anon file
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530374 * with this buffer, so it can be exported.
375 * Also connect the allocator specific data and ops to the buffer.
Sumit Semwal78df9692013-03-22 18:22:16 +0530376 * Additionally, provide a name string for exporter; useful in debugging.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530377 *
Sumit Semwald8fbe342015-01-23 12:53:43 +0530378 * @exp_info: [in] holds all the export related information provided
Daniel Vetterf641d3b2016-12-29 21:48:24 +0100379 * by the exporter. see &struct dma_buf_export_info
Sumit Semwald8fbe342015-01-23 12:53:43 +0530380 * for further details.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530381 *
382 * Returns, on success, a newly created dma_buf object, which wraps the
383 * supplied private data and operations for dma_buf_ops. On either missing
384 * ops, or error in allocating struct dma_buf, will return negative error.
385 *
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100386 * For most cases the easiest way to create @exp_info is through the
387 * %DEFINE_DMA_BUF_EXPORT_INFO macro.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530388 */
Sumit Semwald8fbe342015-01-23 12:53:43 +0530389struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530390{
391 struct dma_buf *dmabuf;
Sumit Semwald8fbe342015-01-23 12:53:43 +0530392 struct reservation_object *resv = exp_info->resv;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530393 struct file *file;
Maarten Lankhorst3aac4502014-07-01 12:57:26 +0200394 size_t alloc_size = sizeof(struct dma_buf);
Chris Wilsona026df42016-07-18 12:16:22 +0100395 int ret;
Jagan Teki51366292015-05-21 01:09:31 +0530396
Sumit Semwald8fbe342015-01-23 12:53:43 +0530397 if (!exp_info->resv)
Maarten Lankhorst3aac4502014-07-01 12:57:26 +0200398 alloc_size += sizeof(struct reservation_object);
399 else
400 /* prevent &dma_buf[1] == dma_buf->resv */
401 alloc_size += 1;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530402
Sumit Semwald8fbe342015-01-23 12:53:43 +0530403 if (WARN_ON(!exp_info->priv
404 || !exp_info->ops
405 || !exp_info->ops->map_dma_buf
406 || !exp_info->ops->unmap_dma_buf
407 || !exp_info->ops->release
Logan Gunthorpef9b67f02017-04-19 13:36:10 -0600408 || !exp_info->ops->map
Sumit Semwald8fbe342015-01-23 12:53:43 +0530409 || !exp_info->ops->mmap)) {
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530410 return ERR_PTR(-EINVAL);
411 }
412
Sumit Semwal9abdffe2015-05-05 14:56:15 +0530413 if (!try_module_get(exp_info->owner))
414 return ERR_PTR(-ENOENT);
415
Maarten Lankhorst3aac4502014-07-01 12:57:26 +0200416 dmabuf = kzalloc(alloc_size, GFP_KERNEL);
Sumit Semwal9abdffe2015-05-05 14:56:15 +0530417 if (!dmabuf) {
Chris Wilsona026df42016-07-18 12:16:22 +0100418 ret = -ENOMEM;
419 goto err_module;
Sumit Semwal9abdffe2015-05-05 14:56:15 +0530420 }
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530421
Sumit Semwald8fbe342015-01-23 12:53:43 +0530422 dmabuf->priv = exp_info->priv;
423 dmabuf->ops = exp_info->ops;
424 dmabuf->size = exp_info->size;
425 dmabuf->exp_name = exp_info->exp_name;
Sumit Semwal9abdffe2015-05-05 14:56:15 +0530426 dmabuf->owner = exp_info->owner;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200427 init_waitqueue_head(&dmabuf->poll);
428 dmabuf->cb_excl.poll = dmabuf->cb_shared.poll = &dmabuf->poll;
429 dmabuf->cb_excl.active = dmabuf->cb_shared.active = 0;
430
Maarten Lankhorst3aac4502014-07-01 12:57:26 +0200431 if (!resv) {
432 resv = (struct reservation_object *)&dmabuf[1];
433 reservation_object_init(resv);
434 }
435 dmabuf->resv = resv;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530436
Sumit Semwald8fbe342015-01-23 12:53:43 +0530437 file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf,
438 exp_info->flags);
Tuomas Tynkkynen9022e242013-08-27 16:30:38 +0300439 if (IS_ERR(file)) {
Chris Wilsona026df42016-07-18 12:16:22 +0100440 ret = PTR_ERR(file);
441 goto err_dmabuf;
Tuomas Tynkkynen9022e242013-08-27 16:30:38 +0300442 }
Christopher James Halse Rogers19e86972013-09-10 11:36:45 +0530443
444 file->f_mode |= FMODE_LSEEK;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530445 dmabuf->file = file;
446
447 mutex_init(&dmabuf->lock);
448 INIT_LIST_HEAD(&dmabuf->attachments);
449
Sumit Semwalb89e35632013-04-04 11:44:37 +0530450 mutex_lock(&db_list.lock);
451 list_add(&dmabuf->list_node, &db_list.head);
452 mutex_unlock(&db_list.lock);
453
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530454 return dmabuf;
Chris Wilsona026df42016-07-18 12:16:22 +0100455
456err_dmabuf:
457 kfree(dmabuf);
458err_module:
459 module_put(exp_info->owner);
460 return ERR_PTR(ret);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530461}
Sumit Semwald8fbe342015-01-23 12:53:43 +0530462EXPORT_SYMBOL_GPL(dma_buf_export);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530463
464/**
465 * dma_buf_fd - returns a file descriptor for the given dma_buf
466 * @dmabuf: [in] pointer to dma_buf for which fd is required.
Dave Airlie55c1c4c2012-03-16 10:34:02 +0000467 * @flags: [in] flags to give to fd
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530468 *
469 * On success, returns an associated 'fd'. Else, returns error.
470 */
Dave Airlie55c1c4c2012-03-16 10:34:02 +0000471int dma_buf_fd(struct dma_buf *dmabuf, int flags)
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530472{
Borislav Petkovf5e097f2012-12-11 16:05:26 +0100473 int fd;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530474
475 if (!dmabuf || !dmabuf->file)
476 return -EINVAL;
477
Borislav Petkovf5e097f2012-12-11 16:05:26 +0100478 fd = get_unused_fd_flags(flags);
479 if (fd < 0)
480 return fd;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530481
482 fd_install(fd, dmabuf->file);
483
484 return fd;
485}
486EXPORT_SYMBOL_GPL(dma_buf_fd);
487
488/**
489 * dma_buf_get - returns the dma_buf structure related to an fd
490 * @fd: [in] fd associated with the dma_buf to be returned
491 *
492 * On success, returns the dma_buf structure associated with an fd; uses
493 * file's refcounting done by fget to increase refcount. returns ERR_PTR
494 * otherwise.
495 */
496struct dma_buf *dma_buf_get(int fd)
497{
498 struct file *file;
499
500 file = fget(fd);
501
502 if (!file)
503 return ERR_PTR(-EBADF);
504
505 if (!is_dma_buf_file(file)) {
506 fput(file);
507 return ERR_PTR(-EINVAL);
508 }
509
510 return file->private_data;
511}
512EXPORT_SYMBOL_GPL(dma_buf_get);
513
514/**
515 * dma_buf_put - decreases refcount of the buffer
516 * @dmabuf: [in] buffer to reduce refcount of
517 *
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100518 * Uses file's refcounting done implicitly by fput().
519 *
520 * If, as a result of this call, the refcount becomes 0, the 'release' file
Daniel Vettere9b4d7b2016-12-29 21:48:25 +0100521 * operation related to this fd is called. It calls &dma_buf_ops.release vfunc
522 * in turn, and frees the memory allocated for dmabuf when exported.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530523 */
524void dma_buf_put(struct dma_buf *dmabuf)
525{
526 if (WARN_ON(!dmabuf || !dmabuf->file))
527 return;
528
529 fput(dmabuf->file);
530}
531EXPORT_SYMBOL_GPL(dma_buf_put);
532
533/**
534 * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
535 * calls attach() of dma_buf_ops to allow device-specific attach functionality
536 * @dmabuf: [in] buffer to attach device to.
537 * @dev: [in] device to be attached.
538 *
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100539 * Returns struct dma_buf_attachment pointer for this attachment. Attachments
540 * must be cleaned up by calling dma_buf_detach().
541 *
542 * Returns:
543 *
544 * A pointer to newly created &dma_buf_attachment on success, or a negative
545 * error code wrapped into a pointer on failure.
546 *
547 * Note that this can fail if the backing storage of @dmabuf is in a place not
548 * accessible to @dev, and cannot be moved to a more suitable place. This is
549 * indicated with the error code -EBUSY.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530550 */
551struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
552 struct device *dev)
553{
554 struct dma_buf_attachment *attach;
555 int ret;
556
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100557 if (WARN_ON(!dmabuf || !dev))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530558 return ERR_PTR(-EINVAL);
559
Markus Elfringdb7942b2017-05-08 10:50:09 +0200560 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
Markus Elfring34d84ec2017-05-08 10:54:17 +0200561 if (!attach)
Laurent Pincharta9fbc3b2012-01-26 12:27:24 +0100562 return ERR_PTR(-ENOMEM);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530563
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530564 attach->dev = dev;
565 attach->dmabuf = dmabuf;
Laurent Pinchart2ed92012012-01-26 12:27:25 +0100566
567 mutex_lock(&dmabuf->lock);
568
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530569 if (dmabuf->ops->attach) {
Christian Königa19741e2018-05-28 11:47:52 +0200570 ret = dmabuf->ops->attach(dmabuf, attach);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530571 if (ret)
572 goto err_attach;
573 }
574 list_add(&attach->node, &dmabuf->attachments);
575
576 mutex_unlock(&dmabuf->lock);
577 return attach;
578
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530579err_attach:
580 kfree(attach);
581 mutex_unlock(&dmabuf->lock);
582 return ERR_PTR(ret);
583}
584EXPORT_SYMBOL_GPL(dma_buf_attach);
585
586/**
587 * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
588 * optionally calls detach() of dma_buf_ops for device-specific detach
589 * @dmabuf: [in] buffer to detach from.
590 * @attach: [in] attachment to be detached; is free'd after this call.
591 *
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100592 * Clean up a device attachment obtained by calling dma_buf_attach().
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530593 */
594void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
595{
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100596 if (WARN_ON(!dmabuf || !attach))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530597 return;
598
599 mutex_lock(&dmabuf->lock);
600 list_del(&attach->node);
601 if (dmabuf->ops->detach)
602 dmabuf->ops->detach(dmabuf, attach);
603
604 mutex_unlock(&dmabuf->lock);
605 kfree(attach);
606}
607EXPORT_SYMBOL_GPL(dma_buf_detach);
608
609/**
610 * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
611 * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
612 * dma_buf_ops.
613 * @attach: [in] attachment whose scatterlist is to be returned
614 * @direction: [in] direction of DMA transfer
615 *
Colin Crossfee0c542013-12-20 16:43:50 -0800616 * Returns sg_table containing the scatterlist to be returned; returns ERR_PTR
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100617 * on error. May return -EINTR if it is interrupted by a signal.
618 *
Liviu Dudauc1387822017-11-01 14:06:30 +0000619 * A mapping must be unmapped by using dma_buf_unmap_attachment(). Note that
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100620 * the underlying backing storage is pinned for as long as a mapping exists,
621 * therefore users/importers should not hold onto a mapping for undue amounts of
622 * time.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530623 */
624struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
625 enum dma_data_direction direction)
626{
Colin Ian King531beb02017-09-15 00:05:16 +0100627 struct sg_table *sg_table;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530628
629 might_sleep();
630
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100631 if (WARN_ON(!attach || !attach->dmabuf))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530632 return ERR_PTR(-EINVAL);
633
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100634 sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
Colin Crossfee0c542013-12-20 16:43:50 -0800635 if (!sg_table)
636 sg_table = ERR_PTR(-ENOMEM);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530637
638 return sg_table;
639}
640EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
641
642/**
643 * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
644 * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
645 * dma_buf_ops.
646 * @attach: [in] attachment to unmap buffer from
647 * @sg_table: [in] scatterlist info of the buffer to unmap
Sumit Semwal33ea2dc2012-01-27 15:09:27 +0530648 * @direction: [in] direction of DMA transfer
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530649 *
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100650 * This unmaps a DMA mapping for @attached obtained by dma_buf_map_attachment().
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530651 */
652void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
Sumit Semwal33ea2dc2012-01-27 15:09:27 +0530653 struct sg_table *sg_table,
654 enum dma_data_direction direction)
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530655{
Rob Clarkb6fa0cd2012-09-28 09:29:43 +0200656 might_sleep();
657
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100658 if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530659 return;
660
Sumit Semwal33ea2dc2012-01-27 15:09:27 +0530661 attach->dmabuf->ops->unmap_dma_buf(attach, sg_table,
662 direction);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530663}
664EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
Daniel Vetterfc130202012-03-20 00:02:37 +0100665
Daniel Vetter0959a162016-12-09 19:53:08 +0100666/**
667 * DOC: cpu access
668 *
669 * There are mutliple reasons for supporting CPU access to a dma buffer object:
670 *
671 * - Fallback operations in the kernel, for example when a device is connected
672 * over USB and the kernel needs to shuffle the data around first before
673 * sending it away. Cache coherency is handled by braketing any transactions
674 * with calls to dma_buf_begin_cpu_access() and dma_buf_end_cpu_access()
675 * access.
676 *
677 * To support dma_buf objects residing in highmem cpu access is page-based
678 * using an api similar to kmap. Accessing a dma_buf is done in aligned chunks
679 * of PAGE_SIZE size. Before accessing a chunk it needs to be mapped, which
680 * returns a pointer in kernel virtual address space. Afterwards the chunk
681 * needs to be unmapped again. There is no limit on how often a given chunk
682 * can be mapped and unmapped, i.e. the importer does not need to call
683 * begin_cpu_access again before mapping the same chunk again.
684 *
685 * Interfaces::
686 * void \*dma_buf_kmap(struct dma_buf \*, unsigned long);
687 * void dma_buf_kunmap(struct dma_buf \*, unsigned long, void \*);
688 *
Christian Königf664a52692018-05-28 13:34:01 +0200689 * Implementing the functions is optional for exporters and for importers all
690 * the restrictions of using kmap apply.
Daniel Vetter0959a162016-12-09 19:53:08 +0100691 *
692 * dma_buf kmap calls outside of the range specified in begin_cpu_access are
693 * undefined. If the range is not PAGE_SIZE aligned, kmap needs to succeed on
694 * the partial chunks at the beginning and end but may return stale or bogus
695 * data outside of the range (in these partial chunks).
696 *
Daniel Vetter0959a162016-12-09 19:53:08 +0100697 * For some cases the overhead of kmap can be too high, a vmap interface
698 * is introduced. This interface should be used very carefully, as vmalloc
699 * space is a limited resources on many architectures.
700 *
701 * Interfaces::
702 * void \*dma_buf_vmap(struct dma_buf \*dmabuf)
703 * void dma_buf_vunmap(struct dma_buf \*dmabuf, void \*vaddr)
704 *
705 * The vmap call can fail if there is no vmap support in the exporter, or if
706 * it runs out of vmalloc space. Fallback to kmap should be implemented. Note
707 * that the dma-buf layer keeps a reference count for all vmap access and
708 * calls down into the exporter's vmap function only when no vmapping exists,
709 * and only unmaps it once. Protection against concurrent vmap/vunmap calls is
710 * provided by taking the dma_buf->lock mutex.
711 *
712 * - For full compatibility on the importer side with existing userspace
713 * interfaces, which might already support mmap'ing buffers. This is needed in
714 * many processing pipelines (e.g. feeding a software rendered image into a
715 * hardware pipeline, thumbnail creation, snapshots, ...). Also, Android's ION
716 * framework already supported this and for DMA buffer file descriptors to
717 * replace ION buffers mmap support was needed.
718 *
719 * There is no special interfaces, userspace simply calls mmap on the dma-buf
720 * fd. But like for CPU access there's a need to braket the actual access,
721 * which is handled by the ioctl (DMA_BUF_IOCTL_SYNC). Note that
722 * DMA_BUF_IOCTL_SYNC can fail with -EAGAIN or -EINTR, in which case it must
723 * be restarted.
724 *
725 * Some systems might need some sort of cache coherency management e.g. when
726 * CPU and GPU domains are being accessed through dma-buf at the same time.
727 * To circumvent this problem there are begin/end coherency markers, that
728 * forward directly to existing dma-buf device drivers vfunc hooks. Userspace
729 * can make use of those markers through the DMA_BUF_IOCTL_SYNC ioctl. The
730 * sequence would be used like following:
731 *
732 * - mmap dma-buf fd
733 * - for each drawing/upload cycle in CPU 1. SYNC_START ioctl, 2. read/write
734 * to mmap area 3. SYNC_END ioctl. This can be repeated as often as you
735 * want (with the new data being consumed by say the GPU or the scanout
736 * device)
737 * - munmap once you don't need the buffer any more
738 *
739 * For correctness and optimal performance, it is always required to use
740 * SYNC_START and SYNC_END before and after, respectively, when accessing the
741 * mapped address. Userspace cannot rely on coherent access, even when there
742 * are systems where it just works without calling these ioctls.
743 *
744 * - And as a CPU fallback in userspace processing pipelines.
745 *
746 * Similar to the motivation for kernel cpu access it is again important that
747 * the userspace code of a given importing subsystem can use the same
748 * interfaces with a imported dma-buf buffer object as with a native buffer
749 * object. This is especially important for drm where the userspace part of
750 * contemporary OpenGL, X, and other drivers is huge, and reworking them to
751 * use a different way to mmap a buffer rather invasive.
752 *
753 * The assumption in the current dma-buf interfaces is that redirecting the
754 * initial mmap is all that's needed. A survey of some of the existing
755 * subsystems shows that no driver seems to do any nefarious thing like
756 * syncing up with outstanding asynchronous processing on the device or
757 * allocating special resources at fault time. So hopefully this is good
758 * enough, since adding interfaces to intercept pagefaults and allow pte
759 * shootdowns would increase the complexity quite a bit.
760 *
761 * Interface::
762 * int dma_buf_mmap(struct dma_buf \*, struct vm_area_struct \*,
763 * unsigned long);
764 *
765 * If the importing subsystem simply provides a special-purpose mmap call to
766 * set up a mapping in userspace, calling do_mmap with dma_buf->file will
767 * equally achieve that for a dma-buf object.
768 */
769
Chris Wilsonae4e46b2016-08-15 16:42:18 +0100770static int __dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
771 enum dma_data_direction direction)
772{
773 bool write = (direction == DMA_BIDIRECTIONAL ||
774 direction == DMA_TO_DEVICE);
775 struct reservation_object *resv = dmabuf->resv;
776 long ret;
777
778 /* Wait on any implicit rendering fences */
779 ret = reservation_object_wait_timeout_rcu(resv, write, true,
780 MAX_SCHEDULE_TIMEOUT);
781 if (ret < 0)
782 return ret;
783
784 return 0;
785}
Daniel Vetterfc130202012-03-20 00:02:37 +0100786
787/**
788 * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
789 * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
790 * preparations. Coherency is only guaranteed in the specified range for the
791 * specified access direction.
Randy Dunlapefb4df822012-04-17 17:03:30 -0700792 * @dmabuf: [in] buffer to prepare cpu access for.
Daniel Vetterfc130202012-03-20 00:02:37 +0100793 * @direction: [in] length of range for cpu access.
794 *
Daniel Vetter0959a162016-12-09 19:53:08 +0100795 * After the cpu access is complete the caller should call
796 * dma_buf_end_cpu_access(). Only when cpu access is braketed by both calls is
797 * it guaranteed to be coherent with other DMA access.
798 *
Daniel Vetterfc130202012-03-20 00:02:37 +0100799 * Can return negative error values, returns 0 on success.
800 */
Tiago Vignatti831e9da2015-12-22 19:36:45 -0200801int dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
Daniel Vetterfc130202012-03-20 00:02:37 +0100802 enum dma_data_direction direction)
803{
804 int ret = 0;
805
806 if (WARN_ON(!dmabuf))
807 return -EINVAL;
808
809 if (dmabuf->ops->begin_cpu_access)
Tiago Vignatti831e9da2015-12-22 19:36:45 -0200810 ret = dmabuf->ops->begin_cpu_access(dmabuf, direction);
Daniel Vetterfc130202012-03-20 00:02:37 +0100811
Chris Wilsonae4e46b2016-08-15 16:42:18 +0100812 /* Ensure that all fences are waited upon - but we first allow
813 * the native handler the chance to do so more efficiently if it
814 * chooses. A double invocation here will be reasonably cheap no-op.
815 */
816 if (ret == 0)
817 ret = __dma_buf_begin_cpu_access(dmabuf, direction);
818
Daniel Vetterfc130202012-03-20 00:02:37 +0100819 return ret;
820}
821EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access);
822
823/**
824 * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
825 * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
826 * actions. Coherency is only guaranteed in the specified range for the
827 * specified access direction.
Randy Dunlapefb4df822012-04-17 17:03:30 -0700828 * @dmabuf: [in] buffer to complete cpu access for.
Daniel Vetterfc130202012-03-20 00:02:37 +0100829 * @direction: [in] length of range for cpu access.
830 *
Daniel Vetter0959a162016-12-09 19:53:08 +0100831 * This terminates CPU access started with dma_buf_begin_cpu_access().
832 *
Daniel Vetter87e332d2016-03-21 08:24:22 +0100833 * Can return negative error values, returns 0 on success.
Daniel Vetterfc130202012-03-20 00:02:37 +0100834 */
Chris Wilson18b862d2016-03-18 20:02:39 +0000835int dma_buf_end_cpu_access(struct dma_buf *dmabuf,
836 enum dma_data_direction direction)
Daniel Vetterfc130202012-03-20 00:02:37 +0100837{
Chris Wilson18b862d2016-03-18 20:02:39 +0000838 int ret = 0;
839
Daniel Vetterfc130202012-03-20 00:02:37 +0100840 WARN_ON(!dmabuf);
841
842 if (dmabuf->ops->end_cpu_access)
Chris Wilson18b862d2016-03-18 20:02:39 +0000843 ret = dmabuf->ops->end_cpu_access(dmabuf, direction);
844
845 return ret;
Daniel Vetterfc130202012-03-20 00:02:37 +0100846}
847EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access);
848
849/**
Daniel Vetterfc130202012-03-20 00:02:37 +0100850 * dma_buf_kmap - Map a page of the buffer object into kernel address space. The
851 * same restrictions as for kmap and friends apply.
Randy Dunlapefb4df822012-04-17 17:03:30 -0700852 * @dmabuf: [in] buffer to map page from.
Daniel Vetterfc130202012-03-20 00:02:37 +0100853 * @page_num: [in] page in PAGE_SIZE units to map.
854 *
855 * This call must always succeed, any necessary preparations that might fail
856 * need to be done in begin_cpu_access.
857 */
858void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num)
859{
860 WARN_ON(!dmabuf);
861
Gerd Hoffmann09ea0df2018-05-29 15:59:18 +0200862 if (!dmabuf->ops->map)
863 return NULL;
Logan Gunthorpef9b67f02017-04-19 13:36:10 -0600864 return dmabuf->ops->map(dmabuf, page_num);
Daniel Vetterfc130202012-03-20 00:02:37 +0100865}
866EXPORT_SYMBOL_GPL(dma_buf_kmap);
867
868/**
869 * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap.
Randy Dunlapefb4df822012-04-17 17:03:30 -0700870 * @dmabuf: [in] buffer to unmap page from.
Daniel Vetterfc130202012-03-20 00:02:37 +0100871 * @page_num: [in] page in PAGE_SIZE units to unmap.
872 * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap.
873 *
874 * This call must always succeed.
875 */
876void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num,
877 void *vaddr)
878{
879 WARN_ON(!dmabuf);
880
Logan Gunthorpef9b67f02017-04-19 13:36:10 -0600881 if (dmabuf->ops->unmap)
882 dmabuf->ops->unmap(dmabuf, page_num, vaddr);
Daniel Vetterfc130202012-03-20 00:02:37 +0100883}
884EXPORT_SYMBOL_GPL(dma_buf_kunmap);
Daniel Vetter4c785132012-04-24 14:38:52 +0530885
886
887/**
888 * dma_buf_mmap - Setup up a userspace mmap with the given vma
Sumit Semwal12c47272012-05-23 15:27:40 +0530889 * @dmabuf: [in] buffer that should back the vma
Daniel Vetter4c785132012-04-24 14:38:52 +0530890 * @vma: [in] vma for the mmap
891 * @pgoff: [in] offset in pages where this mmap should start within the
Jagan Teki51366292015-05-21 01:09:31 +0530892 * dma-buf buffer.
Daniel Vetter4c785132012-04-24 14:38:52 +0530893 *
894 * This function adjusts the passed in vma so that it points at the file of the
Javier Martinez Canillasecf1dba2014-04-10 01:30:05 +0200895 * dma_buf operation. It also adjusts the starting pgoff and does bounds
Daniel Vetter4c785132012-04-24 14:38:52 +0530896 * checking on the size of the vma. Then it calls the exporters mmap function to
897 * set up the mapping.
898 *
899 * Can return negative error values, returns 0 on success.
900 */
901int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
902 unsigned long pgoff)
903{
John Sheu495c10c2013-02-11 17:50:24 -0800904 struct file *oldfile;
905 int ret;
906
Daniel Vetter4c785132012-04-24 14:38:52 +0530907 if (WARN_ON(!dmabuf || !vma))
908 return -EINVAL;
909
910 /* check for offset overflow */
Muhammad Falak R Wanib02da6f2016-05-23 17:08:42 +0530911 if (pgoff + vma_pages(vma) < pgoff)
Daniel Vetter4c785132012-04-24 14:38:52 +0530912 return -EOVERFLOW;
913
914 /* check for overflowing the buffer's size */
Muhammad Falak R Wanib02da6f2016-05-23 17:08:42 +0530915 if (pgoff + vma_pages(vma) >
Daniel Vetter4c785132012-04-24 14:38:52 +0530916 dmabuf->size >> PAGE_SHIFT)
917 return -EINVAL;
918
919 /* readjust the vma */
John Sheu495c10c2013-02-11 17:50:24 -0800920 get_file(dmabuf->file);
921 oldfile = vma->vm_file;
922 vma->vm_file = dmabuf->file;
Daniel Vetter4c785132012-04-24 14:38:52 +0530923 vma->vm_pgoff = pgoff;
924
John Sheu495c10c2013-02-11 17:50:24 -0800925 ret = dmabuf->ops->mmap(dmabuf, vma);
926 if (ret) {
927 /* restore old parameters on failure */
928 vma->vm_file = oldfile;
929 fput(dmabuf->file);
930 } else {
931 if (oldfile)
932 fput(oldfile);
933 }
934 return ret;
935
Daniel Vetter4c785132012-04-24 14:38:52 +0530936}
937EXPORT_SYMBOL_GPL(dma_buf_mmap);
Dave Airlie98f86c92012-05-20 12:33:56 +0530938
939/**
Sumit Semwal12c47272012-05-23 15:27:40 +0530940 * dma_buf_vmap - Create virtual mapping for the buffer object into kernel
941 * address space. Same restrictions as for vmap and friends apply.
942 * @dmabuf: [in] buffer to vmap
Dave Airlie98f86c92012-05-20 12:33:56 +0530943 *
944 * This call may fail due to lack of virtual mapping address space.
945 * These calls are optional in drivers. The intended use for them
946 * is for mapping objects linear in kernel space for high use objects.
947 * Please attempt to use kmap/kunmap before thinking about these interfaces.
Colin Crossfee0c542013-12-20 16:43:50 -0800948 *
949 * Returns NULL on error.
Dave Airlie98f86c92012-05-20 12:33:56 +0530950 */
951void *dma_buf_vmap(struct dma_buf *dmabuf)
952{
Daniel Vetterf00b4da2012-12-20 14:14:23 +0100953 void *ptr;
954
Dave Airlie98f86c92012-05-20 12:33:56 +0530955 if (WARN_ON(!dmabuf))
956 return NULL;
957
Daniel Vetterf00b4da2012-12-20 14:14:23 +0100958 if (!dmabuf->ops->vmap)
959 return NULL;
960
961 mutex_lock(&dmabuf->lock);
962 if (dmabuf->vmapping_counter) {
963 dmabuf->vmapping_counter++;
964 BUG_ON(!dmabuf->vmap_ptr);
965 ptr = dmabuf->vmap_ptr;
966 goto out_unlock;
967 }
968
969 BUG_ON(dmabuf->vmap_ptr);
970
971 ptr = dmabuf->ops->vmap(dmabuf);
Colin Crossfee0c542013-12-20 16:43:50 -0800972 if (WARN_ON_ONCE(IS_ERR(ptr)))
973 ptr = NULL;
974 if (!ptr)
Daniel Vetterf00b4da2012-12-20 14:14:23 +0100975 goto out_unlock;
976
977 dmabuf->vmap_ptr = ptr;
978 dmabuf->vmapping_counter = 1;
979
980out_unlock:
981 mutex_unlock(&dmabuf->lock);
982 return ptr;
Dave Airlie98f86c92012-05-20 12:33:56 +0530983}
984EXPORT_SYMBOL_GPL(dma_buf_vmap);
985
986/**
987 * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
Sumit Semwal12c47272012-05-23 15:27:40 +0530988 * @dmabuf: [in] buffer to vunmap
Randy Dunlap6e7b4a52012-06-09 15:02:59 -0700989 * @vaddr: [in] vmap to vunmap
Dave Airlie98f86c92012-05-20 12:33:56 +0530990 */
991void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
992{
993 if (WARN_ON(!dmabuf))
994 return;
995
Daniel Vetterf00b4da2012-12-20 14:14:23 +0100996 BUG_ON(!dmabuf->vmap_ptr);
997 BUG_ON(dmabuf->vmapping_counter == 0);
998 BUG_ON(dmabuf->vmap_ptr != vaddr);
999
1000 mutex_lock(&dmabuf->lock);
1001 if (--dmabuf->vmapping_counter == 0) {
1002 if (dmabuf->ops->vunmap)
1003 dmabuf->ops->vunmap(dmabuf, vaddr);
1004 dmabuf->vmap_ptr = NULL;
1005 }
1006 mutex_unlock(&dmabuf->lock);
Dave Airlie98f86c92012-05-20 12:33:56 +05301007}
1008EXPORT_SYMBOL_GPL(dma_buf_vunmap);
Sumit Semwalb89e35632013-04-04 11:44:37 +05301009
1010#ifdef CONFIG_DEBUG_FS
Mathias Krauseeb0b9472016-06-19 14:31:29 +02001011static int dma_buf_debug_show(struct seq_file *s, void *unused)
Sumit Semwalb89e35632013-04-04 11:44:37 +05301012{
1013 int ret;
1014 struct dma_buf *buf_obj;
1015 struct dma_buf_attachment *attach_obj;
Russell King5eb2c722017-03-31 11:00:42 +01001016 struct reservation_object *robj;
1017 struct reservation_object_list *fobj;
1018 struct dma_fence *fence;
1019 unsigned seq;
1020 int count = 0, attach_count, shared_count, i;
Sumit Semwalb89e35632013-04-04 11:44:37 +05301021 size_t size = 0;
1022
1023 ret = mutex_lock_interruptible(&db_list.lock);
1024
1025 if (ret)
1026 return ret;
1027
Sumit Semwalc0b00a52014-02-03 15:09:12 +05301028 seq_puts(s, "\nDma-buf Objects:\n");
Russell Kingda6c8f52017-03-31 11:03:20 +01001029 seq_printf(s, "%-8s\t%-8s\t%-8s\t%-8s\texp_name\n",
1030 "size", "flags", "mode", "count");
Sumit Semwalb89e35632013-04-04 11:44:37 +05301031
1032 list_for_each_entry(buf_obj, &db_list.head, list_node) {
1033 ret = mutex_lock_interruptible(&buf_obj->lock);
1034
1035 if (ret) {
Sumit Semwalc0b00a52014-02-03 15:09:12 +05301036 seq_puts(s,
1037 "\tERROR locking buffer object: skipping\n");
Sumit Semwalb89e35632013-04-04 11:44:37 +05301038 continue;
1039 }
1040
Sumit Semwalc0b00a52014-02-03 15:09:12 +05301041 seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\n",
1042 buf_obj->size,
Sumit Semwalb89e35632013-04-04 11:44:37 +05301043 buf_obj->file->f_flags, buf_obj->file->f_mode,
Al Viroa1f6dba2014-08-20 11:05:50 -04001044 file_count(buf_obj->file),
Sumit Semwalc0b00a52014-02-03 15:09:12 +05301045 buf_obj->exp_name);
Sumit Semwalb89e35632013-04-04 11:44:37 +05301046
Russell King5eb2c722017-03-31 11:00:42 +01001047 robj = buf_obj->resv;
1048 while (true) {
1049 seq = read_seqcount_begin(&robj->seq);
1050 rcu_read_lock();
1051 fobj = rcu_dereference(robj->fence);
1052 shared_count = fobj ? fobj->shared_count : 0;
1053 fence = rcu_dereference(robj->fence_excl);
1054 if (!read_seqcount_retry(&robj->seq, seq))
1055 break;
1056 rcu_read_unlock();
1057 }
1058
1059 if (fence)
1060 seq_printf(s, "\tExclusive fence: %s %s %ssignalled\n",
1061 fence->ops->get_driver_name(fence),
1062 fence->ops->get_timeline_name(fence),
1063 dma_fence_is_signaled(fence) ? "" : "un");
1064 for (i = 0; i < shared_count; i++) {
1065 fence = rcu_dereference(fobj->shared[i]);
1066 if (!dma_fence_get_rcu(fence))
1067 continue;
1068 seq_printf(s, "\tShared fence: %s %s %ssignalled\n",
1069 fence->ops->get_driver_name(fence),
1070 fence->ops->get_timeline_name(fence),
1071 dma_fence_is_signaled(fence) ? "" : "un");
1072 }
1073 rcu_read_unlock();
1074
Sumit Semwalc0b00a52014-02-03 15:09:12 +05301075 seq_puts(s, "\tAttached Devices:\n");
Sumit Semwalb89e35632013-04-04 11:44:37 +05301076 attach_count = 0;
1077
1078 list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
Markus Elfring9eddb412017-05-08 10:32:44 +02001079 seq_printf(s, "\t%s\n", dev_name(attach_obj->dev));
Sumit Semwalb89e35632013-04-04 11:44:37 +05301080 attach_count++;
1081 }
1082
Sumit Semwalc0b00a52014-02-03 15:09:12 +05301083 seq_printf(s, "Total %d devices attached\n\n",
Sumit Semwalb89e35632013-04-04 11:44:37 +05301084 attach_count);
1085
1086 count++;
1087 size += buf_obj->size;
1088 mutex_unlock(&buf_obj->lock);
1089 }
1090
1091 seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size);
1092
1093 mutex_unlock(&db_list.lock);
1094 return 0;
1095}
1096
Sumit Semwalb89e35632013-04-04 11:44:37 +05301097static int dma_buf_debug_open(struct inode *inode, struct file *file)
1098{
Mathias Krauseeb0b9472016-06-19 14:31:29 +02001099 return single_open(file, dma_buf_debug_show, NULL);
Sumit Semwalb89e35632013-04-04 11:44:37 +05301100}
1101
1102static const struct file_operations dma_buf_debug_fops = {
1103 .open = dma_buf_debug_open,
1104 .read = seq_read,
1105 .llseek = seq_lseek,
1106 .release = single_release,
1107};
1108
1109static struct dentry *dma_buf_debugfs_dir;
1110
1111static int dma_buf_init_debugfs(void)
1112{
Mathias Krausebd3e2202016-06-19 14:31:31 +02001113 struct dentry *d;
Sumit Semwalb89e35632013-04-04 11:44:37 +05301114 int err = 0;
Jagan Teki51366292015-05-21 01:09:31 +05301115
Mathias Krausebd3e2202016-06-19 14:31:31 +02001116 d = debugfs_create_dir("dma_buf", NULL);
1117 if (IS_ERR(d))
1118 return PTR_ERR(d);
Jagan Teki51366292015-05-21 01:09:31 +05301119
Mathias Krausebd3e2202016-06-19 14:31:31 +02001120 dma_buf_debugfs_dir = d;
Sumit Semwalb89e35632013-04-04 11:44:37 +05301121
Mathias Krausebd3e2202016-06-19 14:31:31 +02001122 d = debugfs_create_file("bufinfo", S_IRUGO, dma_buf_debugfs_dir,
1123 NULL, &dma_buf_debug_fops);
1124 if (IS_ERR(d)) {
Sumit Semwalb89e35632013-04-04 11:44:37 +05301125 pr_debug("dma_buf: debugfs: failed to create node bufinfo\n");
Mathias Krauseb7479992016-06-19 14:31:30 +02001126 debugfs_remove_recursive(dma_buf_debugfs_dir);
1127 dma_buf_debugfs_dir = NULL;
Mathias Krausebd3e2202016-06-19 14:31:31 +02001128 err = PTR_ERR(d);
Mathias Krauseb7479992016-06-19 14:31:30 +02001129 }
Sumit Semwalb89e35632013-04-04 11:44:37 +05301130
1131 return err;
1132}
1133
1134static void dma_buf_uninit_debugfs(void)
1135{
Vasyl Gomonovych298b6a82017-11-22 16:22:41 +01001136 debugfs_remove_recursive(dma_buf_debugfs_dir);
Sumit Semwalb89e35632013-04-04 11:44:37 +05301137}
Sumit Semwalb89e35632013-04-04 11:44:37 +05301138#else
1139static inline int dma_buf_init_debugfs(void)
1140{
1141 return 0;
1142}
1143static inline void dma_buf_uninit_debugfs(void)
1144{
1145}
1146#endif
1147
1148static int __init dma_buf_init(void)
1149{
1150 mutex_init(&db_list.lock);
1151 INIT_LIST_HEAD(&db_list.head);
1152 dma_buf_init_debugfs();
1153 return 0;
1154}
1155subsys_initcall(dma_buf_init);
1156
1157static void __exit dma_buf_deinit(void)
1158{
1159 dma_buf_uninit_debugfs();
1160}
1161__exitcall(dma_buf_deinit);