blob: 718f832a5c71e69cb8ae6436edfb77c5a8b654ba [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 Semwalb89e3562013-04-04 11:44:37 +053031#include <linux/debugfs.h>
Sumit Semwal9abdffe2015-05-05 14:56:15 +053032#include <linux/module.h>
Sumit Semwalb89e3562013-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 Semwalb89e3562013-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 Semwalb89e3562013-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 *
138 * - Checking for POLLIN, i.e. read access, can be use to query the state of the
139 * most recent write or exclusive fence.
140 *
141 * - Checking for POLLOUT, i.e. write access, can be used to query the state of
142 * 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
160static unsigned int dma_buf_poll(struct file *file, poll_table *poll)
161{
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;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200166 unsigned long 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)
171 return POLLERR;
172
173 resv = dmabuf->resv;
174
175 poll_wait(file, &dmabuf->poll, poll);
176
177 events = poll_requested_events(poll) & (POLLIN | POLLOUT);
178 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
196 if (fence_excl && (!(events & POLLOUT) || shared_count == 0)) {
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200197 struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_excl;
198 unsigned long pevents = POLLIN;
199
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200200 if (shared_count == 0)
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200201 pevents |= POLLOUT;
202
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
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200231 if ((events & POLLOUT) && 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)
238 events &= ~POLLOUT;
239 else
240 dcb->active = POLLOUT;
241 spin_unlock_irq(&dmabuf->poll.lock);
242
243 if (!(events & POLLOUT))
244 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 */
256 events &= ~POLLOUT;
257 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);
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200263 events &= ~POLLOUT;
264 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,
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530328};
329
330/*
331 * is_dma_buf_file - Check if struct file* is associated with dma_buf
332 */
333static inline int is_dma_buf_file(struct file *file)
334{
335 return file->f_op == &dma_buf_fops;
336}
337
338/**
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100339 * DOC: dma buf device access
340 *
341 * For device DMA access to a shared DMA buffer the usual sequence of operations
342 * is fairly simple:
343 *
344 * 1. The exporter defines his exporter instance using
345 * DEFINE_DMA_BUF_EXPORT_INFO() and calls dma_buf_export() to wrap a private
346 * buffer object into a &dma_buf. It then exports that &dma_buf to userspace
347 * as a file descriptor by calling dma_buf_fd().
348 *
349 * 2. Userspace passes this file-descriptors to all drivers it wants this buffer
350 * to share with: First the filedescriptor is converted to a &dma_buf using
351 * dma_buf_get(). The the buffer is attached to the device using
352 * dma_buf_attach().
353 *
354 * Up to this stage the exporter is still free to migrate or reallocate the
355 * backing storage.
356 *
357 * 3. Once the buffer is attached to all devices userspace can inniate DMA
358 * access to the shared buffer. In the kernel this is done by calling
359 * dma_buf_map_attachment() and dma_buf_unmap_attachment().
360 *
361 * 4. Once a driver is done with a shared buffer it needs to call
362 * dma_buf_detach() (after cleaning up any mappings) and then release the
363 * reference acquired with dma_buf_get by calling dma_buf_put().
364 *
365 * For the detailed semantics exporters are expected to implement see
366 * &dma_buf_ops.
367 */
368
369/**
Sumit Semwald8fbe342015-01-23 12:53:43 +0530370 * dma_buf_export - Creates a new dma_buf, and associates an anon file
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530371 * with this buffer, so it can be exported.
372 * Also connect the allocator specific data and ops to the buffer.
Sumit Semwal78df9692013-03-22 18:22:16 +0530373 * Additionally, provide a name string for exporter; useful in debugging.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530374 *
Sumit Semwald8fbe342015-01-23 12:53:43 +0530375 * @exp_info: [in] holds all the export related information provided
Daniel Vetterf641d3b2016-12-29 21:48:24 +0100376 * by the exporter. see &struct dma_buf_export_info
Sumit Semwald8fbe342015-01-23 12:53:43 +0530377 * for further details.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530378 *
379 * Returns, on success, a newly created dma_buf object, which wraps the
380 * supplied private data and operations for dma_buf_ops. On either missing
381 * ops, or error in allocating struct dma_buf, will return negative error.
382 *
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100383 * For most cases the easiest way to create @exp_info is through the
384 * %DEFINE_DMA_BUF_EXPORT_INFO macro.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530385 */
Sumit Semwald8fbe342015-01-23 12:53:43 +0530386struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530387{
388 struct dma_buf *dmabuf;
Sumit Semwald8fbe342015-01-23 12:53:43 +0530389 struct reservation_object *resv = exp_info->resv;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530390 struct file *file;
Maarten Lankhorst3aac4502014-07-01 12:57:26 +0200391 size_t alloc_size = sizeof(struct dma_buf);
Chris Wilsona026df42016-07-18 12:16:22 +0100392 int ret;
Jagan Teki51366292015-05-21 01:09:31 +0530393
Sumit Semwald8fbe342015-01-23 12:53:43 +0530394 if (!exp_info->resv)
Maarten Lankhorst3aac4502014-07-01 12:57:26 +0200395 alloc_size += sizeof(struct reservation_object);
396 else
397 /* prevent &dma_buf[1] == dma_buf->resv */
398 alloc_size += 1;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530399
Sumit Semwald8fbe342015-01-23 12:53:43 +0530400 if (WARN_ON(!exp_info->priv
401 || !exp_info->ops
402 || !exp_info->ops->map_dma_buf
403 || !exp_info->ops->unmap_dma_buf
404 || !exp_info->ops->release
405 || !exp_info->ops->kmap_atomic
406 || !exp_info->ops->kmap
407 || !exp_info->ops->mmap)) {
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530408 return ERR_PTR(-EINVAL);
409 }
410
Sumit Semwal9abdffe2015-05-05 14:56:15 +0530411 if (!try_module_get(exp_info->owner))
412 return ERR_PTR(-ENOENT);
413
Maarten Lankhorst3aac4502014-07-01 12:57:26 +0200414 dmabuf = kzalloc(alloc_size, GFP_KERNEL);
Sumit Semwal9abdffe2015-05-05 14:56:15 +0530415 if (!dmabuf) {
Chris Wilsona026df42016-07-18 12:16:22 +0100416 ret = -ENOMEM;
417 goto err_module;
Sumit Semwal9abdffe2015-05-05 14:56:15 +0530418 }
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530419
Sumit Semwald8fbe342015-01-23 12:53:43 +0530420 dmabuf->priv = exp_info->priv;
421 dmabuf->ops = exp_info->ops;
422 dmabuf->size = exp_info->size;
423 dmabuf->exp_name = exp_info->exp_name;
Sumit Semwal9abdffe2015-05-05 14:56:15 +0530424 dmabuf->owner = exp_info->owner;
Maarten Lankhorst9b495a52014-07-01 12:57:43 +0200425 init_waitqueue_head(&dmabuf->poll);
426 dmabuf->cb_excl.poll = dmabuf->cb_shared.poll = &dmabuf->poll;
427 dmabuf->cb_excl.active = dmabuf->cb_shared.active = 0;
428
Maarten Lankhorst3aac4502014-07-01 12:57:26 +0200429 if (!resv) {
430 resv = (struct reservation_object *)&dmabuf[1];
431 reservation_object_init(resv);
432 }
433 dmabuf->resv = resv;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530434
Sumit Semwald8fbe342015-01-23 12:53:43 +0530435 file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf,
436 exp_info->flags);
Tuomas Tynkkynen9022e242013-08-27 16:30:38 +0300437 if (IS_ERR(file)) {
Chris Wilsona026df42016-07-18 12:16:22 +0100438 ret = PTR_ERR(file);
439 goto err_dmabuf;
Tuomas Tynkkynen9022e242013-08-27 16:30:38 +0300440 }
Christopher James Halse Rogers19e86972013-09-10 11:36:45 +0530441
442 file->f_mode |= FMODE_LSEEK;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530443 dmabuf->file = file;
444
445 mutex_init(&dmabuf->lock);
446 INIT_LIST_HEAD(&dmabuf->attachments);
447
Sumit Semwalb89e3562013-04-04 11:44:37 +0530448 mutex_lock(&db_list.lock);
449 list_add(&dmabuf->list_node, &db_list.head);
450 mutex_unlock(&db_list.lock);
451
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530452 return dmabuf;
Chris Wilsona026df42016-07-18 12:16:22 +0100453
454err_dmabuf:
455 kfree(dmabuf);
456err_module:
457 module_put(exp_info->owner);
458 return ERR_PTR(ret);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530459}
Sumit Semwald8fbe342015-01-23 12:53:43 +0530460EXPORT_SYMBOL_GPL(dma_buf_export);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530461
462/**
463 * dma_buf_fd - returns a file descriptor for the given dma_buf
464 * @dmabuf: [in] pointer to dma_buf for which fd is required.
Dave Airlie55c1c4c2012-03-16 10:34:02 +0000465 * @flags: [in] flags to give to fd
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530466 *
467 * On success, returns an associated 'fd'. Else, returns error.
468 */
Dave Airlie55c1c4c2012-03-16 10:34:02 +0000469int dma_buf_fd(struct dma_buf *dmabuf, int flags)
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530470{
Borislav Petkovf5e097f2012-12-11 16:05:26 +0100471 int fd;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530472
473 if (!dmabuf || !dmabuf->file)
474 return -EINVAL;
475
Borislav Petkovf5e097f2012-12-11 16:05:26 +0100476 fd = get_unused_fd_flags(flags);
477 if (fd < 0)
478 return fd;
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530479
480 fd_install(fd, dmabuf->file);
481
482 return fd;
483}
484EXPORT_SYMBOL_GPL(dma_buf_fd);
485
486/**
487 * dma_buf_get - returns the dma_buf structure related to an fd
488 * @fd: [in] fd associated with the dma_buf to be returned
489 *
490 * On success, returns the dma_buf structure associated with an fd; uses
491 * file's refcounting done by fget to increase refcount. returns ERR_PTR
492 * otherwise.
493 */
494struct dma_buf *dma_buf_get(int fd)
495{
496 struct file *file;
497
498 file = fget(fd);
499
500 if (!file)
501 return ERR_PTR(-EBADF);
502
503 if (!is_dma_buf_file(file)) {
504 fput(file);
505 return ERR_PTR(-EINVAL);
506 }
507
508 return file->private_data;
509}
510EXPORT_SYMBOL_GPL(dma_buf_get);
511
512/**
513 * dma_buf_put - decreases refcount of the buffer
514 * @dmabuf: [in] buffer to reduce refcount of
515 *
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100516 * Uses file's refcounting done implicitly by fput().
517 *
518 * If, as a result of this call, the refcount becomes 0, the 'release' file
Daniel Vettere9b4d7b2016-12-29 21:48:25 +0100519 * operation related to this fd is called. It calls &dma_buf_ops.release vfunc
520 * in turn, and frees the memory allocated for dmabuf when exported.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530521 */
522void dma_buf_put(struct dma_buf *dmabuf)
523{
524 if (WARN_ON(!dmabuf || !dmabuf->file))
525 return;
526
527 fput(dmabuf->file);
528}
529EXPORT_SYMBOL_GPL(dma_buf_put);
530
531/**
532 * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
533 * calls attach() of dma_buf_ops to allow device-specific attach functionality
534 * @dmabuf: [in] buffer to attach device to.
535 * @dev: [in] device to be attached.
536 *
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100537 * Returns struct dma_buf_attachment pointer for this attachment. Attachments
538 * must be cleaned up by calling dma_buf_detach().
539 *
540 * Returns:
541 *
542 * A pointer to newly created &dma_buf_attachment on success, or a negative
543 * error code wrapped into a pointer on failure.
544 *
545 * Note that this can fail if the backing storage of @dmabuf is in a place not
546 * accessible to @dev, and cannot be moved to a more suitable place. This is
547 * indicated with the error code -EBUSY.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530548 */
549struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
550 struct device *dev)
551{
552 struct dma_buf_attachment *attach;
553 int ret;
554
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100555 if (WARN_ON(!dmabuf || !dev))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530556 return ERR_PTR(-EINVAL);
557
558 attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL);
559 if (attach == NULL)
Laurent Pincharta9fbc3b2012-01-26 12:27:24 +0100560 return ERR_PTR(-ENOMEM);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530561
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530562 attach->dev = dev;
563 attach->dmabuf = dmabuf;
Laurent Pinchart2ed92012012-01-26 12:27:25 +0100564
565 mutex_lock(&dmabuf->lock);
566
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530567 if (dmabuf->ops->attach) {
568 ret = dmabuf->ops->attach(dmabuf, dev, attach);
569 if (ret)
570 goto err_attach;
571 }
572 list_add(&attach->node, &dmabuf->attachments);
573
574 mutex_unlock(&dmabuf->lock);
575 return attach;
576
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530577err_attach:
578 kfree(attach);
579 mutex_unlock(&dmabuf->lock);
580 return ERR_PTR(ret);
581}
582EXPORT_SYMBOL_GPL(dma_buf_attach);
583
584/**
585 * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
586 * optionally calls detach() of dma_buf_ops for device-specific detach
587 * @dmabuf: [in] buffer to detach from.
588 * @attach: [in] attachment to be detached; is free'd after this call.
589 *
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100590 * Clean up a device attachment obtained by calling dma_buf_attach().
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530591 */
592void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
593{
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100594 if (WARN_ON(!dmabuf || !attach))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530595 return;
596
597 mutex_lock(&dmabuf->lock);
598 list_del(&attach->node);
599 if (dmabuf->ops->detach)
600 dmabuf->ops->detach(dmabuf, attach);
601
602 mutex_unlock(&dmabuf->lock);
603 kfree(attach);
604}
605EXPORT_SYMBOL_GPL(dma_buf_detach);
606
607/**
608 * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
609 * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
610 * dma_buf_ops.
611 * @attach: [in] attachment whose scatterlist is to be returned
612 * @direction: [in] direction of DMA transfer
613 *
Colin Crossfee0c542013-12-20 16:43:50 -0800614 * Returns sg_table containing the scatterlist to be returned; returns ERR_PTR
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100615 * on error. May return -EINTR if it is interrupted by a signal.
616 *
617 * A mapping must be unmapped again using dma_buf_map_attachment(). Note that
618 * the underlying backing storage is pinned for as long as a mapping exists,
619 * therefore users/importers should not hold onto a mapping for undue amounts of
620 * time.
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530621 */
622struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
623 enum dma_data_direction direction)
624{
625 struct sg_table *sg_table = ERR_PTR(-EINVAL);
626
627 might_sleep();
628
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100629 if (WARN_ON(!attach || !attach->dmabuf))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530630 return ERR_PTR(-EINVAL);
631
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100632 sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
Colin Crossfee0c542013-12-20 16:43:50 -0800633 if (!sg_table)
634 sg_table = ERR_PTR(-ENOMEM);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530635
636 return sg_table;
637}
638EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
639
640/**
641 * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
642 * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
643 * dma_buf_ops.
644 * @attach: [in] attachment to unmap buffer from
645 * @sg_table: [in] scatterlist info of the buffer to unmap
Sumit Semwal33ea2dc2012-01-27 15:09:27 +0530646 * @direction: [in] direction of DMA transfer
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530647 *
Daniel Vetter2904a8c2016-12-09 19:53:07 +0100648 * This unmaps a DMA mapping for @attached obtained by dma_buf_map_attachment().
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530649 */
650void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
Sumit Semwal33ea2dc2012-01-27 15:09:27 +0530651 struct sg_table *sg_table,
652 enum dma_data_direction direction)
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530653{
Rob Clarkb6fa0cd2012-09-28 09:29:43 +0200654 might_sleep();
655
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100656 if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530657 return;
658
Sumit Semwal33ea2dc2012-01-27 15:09:27 +0530659 attach->dmabuf->ops->unmap_dma_buf(attach, sg_table,
660 direction);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530661}
662EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
Daniel Vetterfc130202012-03-20 00:02:37 +0100663
Daniel Vetter0959a162016-12-09 19:53:08 +0100664/**
665 * DOC: cpu access
666 *
667 * There are mutliple reasons for supporting CPU access to a dma buffer object:
668 *
669 * - Fallback operations in the kernel, for example when a device is connected
670 * over USB and the kernel needs to shuffle the data around first before
671 * sending it away. Cache coherency is handled by braketing any transactions
672 * with calls to dma_buf_begin_cpu_access() and dma_buf_end_cpu_access()
673 * access.
674 *
675 * To support dma_buf objects residing in highmem cpu access is page-based
676 * using an api similar to kmap. Accessing a dma_buf is done in aligned chunks
677 * of PAGE_SIZE size. Before accessing a chunk it needs to be mapped, which
678 * returns a pointer in kernel virtual address space. Afterwards the chunk
679 * needs to be unmapped again. There is no limit on how often a given chunk
680 * can be mapped and unmapped, i.e. the importer does not need to call
681 * begin_cpu_access again before mapping the same chunk again.
682 *
683 * Interfaces::
684 * void \*dma_buf_kmap(struct dma_buf \*, unsigned long);
685 * void dma_buf_kunmap(struct dma_buf \*, unsigned long, void \*);
686 *
687 * There are also atomic variants of these interfaces. Like for kmap they
688 * facilitate non-blocking fast-paths. Neither the importer nor the exporter
689 * (in the callback) is allowed to block when using these.
690 *
691 * Interfaces::
692 * void \*dma_buf_kmap_atomic(struct dma_buf \*, unsigned long);
693 * void dma_buf_kunmap_atomic(struct dma_buf \*, unsigned long, void \*);
694 *
695 * For importers all the restrictions of using kmap apply, like the limited
696 * supply of kmap_atomic slots. Hence an importer shall only hold onto at
697 * max 2 atomic dma_buf kmaps at the same time (in any given process context).
698 *
699 * dma_buf kmap calls outside of the range specified in begin_cpu_access are
700 * undefined. If the range is not PAGE_SIZE aligned, kmap needs to succeed on
701 * the partial chunks at the beginning and end but may return stale or bogus
702 * data outside of the range (in these partial chunks).
703 *
704 * Note that these calls need to always succeed. The exporter needs to
705 * complete any preparations that might fail in begin_cpu_access.
706 *
707 * For some cases the overhead of kmap can be too high, a vmap interface
708 * is introduced. This interface should be used very carefully, as vmalloc
709 * space is a limited resources on many architectures.
710 *
711 * Interfaces::
712 * void \*dma_buf_vmap(struct dma_buf \*dmabuf)
713 * void dma_buf_vunmap(struct dma_buf \*dmabuf, void \*vaddr)
714 *
715 * The vmap call can fail if there is no vmap support in the exporter, or if
716 * it runs out of vmalloc space. Fallback to kmap should be implemented. Note
717 * that the dma-buf layer keeps a reference count for all vmap access and
718 * calls down into the exporter's vmap function only when no vmapping exists,
719 * and only unmaps it once. Protection against concurrent vmap/vunmap calls is
720 * provided by taking the dma_buf->lock mutex.
721 *
722 * - For full compatibility on the importer side with existing userspace
723 * interfaces, which might already support mmap'ing buffers. This is needed in
724 * many processing pipelines (e.g. feeding a software rendered image into a
725 * hardware pipeline, thumbnail creation, snapshots, ...). Also, Android's ION
726 * framework already supported this and for DMA buffer file descriptors to
727 * replace ION buffers mmap support was needed.
728 *
729 * There is no special interfaces, userspace simply calls mmap on the dma-buf
730 * fd. But like for CPU access there's a need to braket the actual access,
731 * which is handled by the ioctl (DMA_BUF_IOCTL_SYNC). Note that
732 * DMA_BUF_IOCTL_SYNC can fail with -EAGAIN or -EINTR, in which case it must
733 * be restarted.
734 *
735 * Some systems might need some sort of cache coherency management e.g. when
736 * CPU and GPU domains are being accessed through dma-buf at the same time.
737 * To circumvent this problem there are begin/end coherency markers, that
738 * forward directly to existing dma-buf device drivers vfunc hooks. Userspace
739 * can make use of those markers through the DMA_BUF_IOCTL_SYNC ioctl. The
740 * sequence would be used like following:
741 *
742 * - mmap dma-buf fd
743 * - for each drawing/upload cycle in CPU 1. SYNC_START ioctl, 2. read/write
744 * to mmap area 3. SYNC_END ioctl. This can be repeated as often as you
745 * want (with the new data being consumed by say the GPU or the scanout
746 * device)
747 * - munmap once you don't need the buffer any more
748 *
749 * For correctness and optimal performance, it is always required to use
750 * SYNC_START and SYNC_END before and after, respectively, when accessing the
751 * mapped address. Userspace cannot rely on coherent access, even when there
752 * are systems where it just works without calling these ioctls.
753 *
754 * - And as a CPU fallback in userspace processing pipelines.
755 *
756 * Similar to the motivation for kernel cpu access it is again important that
757 * the userspace code of a given importing subsystem can use the same
758 * interfaces with a imported dma-buf buffer object as with a native buffer
759 * object. This is especially important for drm where the userspace part of
760 * contemporary OpenGL, X, and other drivers is huge, and reworking them to
761 * use a different way to mmap a buffer rather invasive.
762 *
763 * The assumption in the current dma-buf interfaces is that redirecting the
764 * initial mmap is all that's needed. A survey of some of the existing
765 * subsystems shows that no driver seems to do any nefarious thing like
766 * syncing up with outstanding asynchronous processing on the device or
767 * allocating special resources at fault time. So hopefully this is good
768 * enough, since adding interfaces to intercept pagefaults and allow pte
769 * shootdowns would increase the complexity quite a bit.
770 *
771 * Interface::
772 * int dma_buf_mmap(struct dma_buf \*, struct vm_area_struct \*,
773 * unsigned long);
774 *
775 * If the importing subsystem simply provides a special-purpose mmap call to
776 * set up a mapping in userspace, calling do_mmap with dma_buf->file will
777 * equally achieve that for a dma-buf object.
778 */
779
Chris Wilsonae4e46b2016-08-15 16:42:18 +0100780static int __dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
781 enum dma_data_direction direction)
782{
783 bool write = (direction == DMA_BIDIRECTIONAL ||
784 direction == DMA_TO_DEVICE);
785 struct reservation_object *resv = dmabuf->resv;
786 long ret;
787
788 /* Wait on any implicit rendering fences */
789 ret = reservation_object_wait_timeout_rcu(resv, write, true,
790 MAX_SCHEDULE_TIMEOUT);
791 if (ret < 0)
792 return ret;
793
794 return 0;
795}
Daniel Vetterfc130202012-03-20 00:02:37 +0100796
797/**
798 * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
799 * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
800 * preparations. Coherency is only guaranteed in the specified range for the
801 * specified access direction.
Randy Dunlapefb4df822012-04-17 17:03:30 -0700802 * @dmabuf: [in] buffer to prepare cpu access for.
Daniel Vetterfc130202012-03-20 00:02:37 +0100803 * @direction: [in] length of range for cpu access.
804 *
Daniel Vetter0959a162016-12-09 19:53:08 +0100805 * After the cpu access is complete the caller should call
806 * dma_buf_end_cpu_access(). Only when cpu access is braketed by both calls is
807 * it guaranteed to be coherent with other DMA access.
808 *
Daniel Vetterfc130202012-03-20 00:02:37 +0100809 * Can return negative error values, returns 0 on success.
810 */
Tiago Vignatti831e9da2015-12-22 19:36:45 -0200811int dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
Daniel Vetterfc130202012-03-20 00:02:37 +0100812 enum dma_data_direction direction)
813{
814 int ret = 0;
815
816 if (WARN_ON(!dmabuf))
817 return -EINVAL;
818
819 if (dmabuf->ops->begin_cpu_access)
Tiago Vignatti831e9da2015-12-22 19:36:45 -0200820 ret = dmabuf->ops->begin_cpu_access(dmabuf, direction);
Daniel Vetterfc130202012-03-20 00:02:37 +0100821
Chris Wilsonae4e46b2016-08-15 16:42:18 +0100822 /* Ensure that all fences are waited upon - but we first allow
823 * the native handler the chance to do so more efficiently if it
824 * chooses. A double invocation here will be reasonably cheap no-op.
825 */
826 if (ret == 0)
827 ret = __dma_buf_begin_cpu_access(dmabuf, direction);
828
Daniel Vetterfc130202012-03-20 00:02:37 +0100829 return ret;
830}
831EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access);
832
833/**
834 * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
835 * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
836 * actions. Coherency is only guaranteed in the specified range for the
837 * specified access direction.
Randy Dunlapefb4df822012-04-17 17:03:30 -0700838 * @dmabuf: [in] buffer to complete cpu access for.
Daniel Vetterfc130202012-03-20 00:02:37 +0100839 * @direction: [in] length of range for cpu access.
840 *
Daniel Vetter0959a162016-12-09 19:53:08 +0100841 * This terminates CPU access started with dma_buf_begin_cpu_access().
842 *
Daniel Vetter87e332d2016-03-21 08:24:22 +0100843 * Can return negative error values, returns 0 on success.
Daniel Vetterfc130202012-03-20 00:02:37 +0100844 */
Chris Wilson18b862d2016-03-18 20:02:39 +0000845int dma_buf_end_cpu_access(struct dma_buf *dmabuf,
846 enum dma_data_direction direction)
Daniel Vetterfc130202012-03-20 00:02:37 +0100847{
Chris Wilson18b862d2016-03-18 20:02:39 +0000848 int ret = 0;
849
Daniel Vetterfc130202012-03-20 00:02:37 +0100850 WARN_ON(!dmabuf);
851
852 if (dmabuf->ops->end_cpu_access)
Chris Wilson18b862d2016-03-18 20:02:39 +0000853 ret = dmabuf->ops->end_cpu_access(dmabuf, direction);
854
855 return ret;
Daniel Vetterfc130202012-03-20 00:02:37 +0100856}
857EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access);
858
859/**
860 * dma_buf_kmap_atomic - Map a page of the buffer object into kernel address
861 * space. The same restrictions as for kmap_atomic and friends apply.
Randy Dunlapefb4df822012-04-17 17:03:30 -0700862 * @dmabuf: [in] buffer to map page from.
Daniel Vetterfc130202012-03-20 00:02:37 +0100863 * @page_num: [in] page in PAGE_SIZE units to map.
864 *
865 * This call must always succeed, any necessary preparations that might fail
866 * need to be done in begin_cpu_access.
867 */
868void *dma_buf_kmap_atomic(struct dma_buf *dmabuf, unsigned long page_num)
869{
870 WARN_ON(!dmabuf);
871
872 return dmabuf->ops->kmap_atomic(dmabuf, page_num);
873}
874EXPORT_SYMBOL_GPL(dma_buf_kmap_atomic);
875
876/**
877 * dma_buf_kunmap_atomic - Unmap a page obtained by dma_buf_kmap_atomic.
Randy Dunlapefb4df822012-04-17 17:03:30 -0700878 * @dmabuf: [in] buffer to unmap page from.
Daniel Vetterfc130202012-03-20 00:02:37 +0100879 * @page_num: [in] page in PAGE_SIZE units to unmap.
880 * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap_atomic.
881 *
882 * This call must always succeed.
883 */
884void dma_buf_kunmap_atomic(struct dma_buf *dmabuf, unsigned long page_num,
885 void *vaddr)
886{
887 WARN_ON(!dmabuf);
888
889 if (dmabuf->ops->kunmap_atomic)
890 dmabuf->ops->kunmap_atomic(dmabuf, page_num, vaddr);
891}
892EXPORT_SYMBOL_GPL(dma_buf_kunmap_atomic);
893
894/**
895 * dma_buf_kmap - Map a page of the buffer object into kernel address space. The
896 * same restrictions as for kmap and friends apply.
Randy Dunlapefb4df822012-04-17 17:03:30 -0700897 * @dmabuf: [in] buffer to map page from.
Daniel Vetterfc130202012-03-20 00:02:37 +0100898 * @page_num: [in] page in PAGE_SIZE units to map.
899 *
900 * This call must always succeed, any necessary preparations that might fail
901 * need to be done in begin_cpu_access.
902 */
903void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num)
904{
905 WARN_ON(!dmabuf);
906
907 return dmabuf->ops->kmap(dmabuf, page_num);
908}
909EXPORT_SYMBOL_GPL(dma_buf_kmap);
910
911/**
912 * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap.
Randy Dunlapefb4df822012-04-17 17:03:30 -0700913 * @dmabuf: [in] buffer to unmap page from.
Daniel Vetterfc130202012-03-20 00:02:37 +0100914 * @page_num: [in] page in PAGE_SIZE units to unmap.
915 * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap.
916 *
917 * This call must always succeed.
918 */
919void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num,
920 void *vaddr)
921{
922 WARN_ON(!dmabuf);
923
924 if (dmabuf->ops->kunmap)
925 dmabuf->ops->kunmap(dmabuf, page_num, vaddr);
926}
927EXPORT_SYMBOL_GPL(dma_buf_kunmap);
Daniel Vetter4c785132012-04-24 14:38:52 +0530928
929
930/**
931 * dma_buf_mmap - Setup up a userspace mmap with the given vma
Sumit Semwal12c47272012-05-23 15:27:40 +0530932 * @dmabuf: [in] buffer that should back the vma
Daniel Vetter4c785132012-04-24 14:38:52 +0530933 * @vma: [in] vma for the mmap
934 * @pgoff: [in] offset in pages where this mmap should start within the
Jagan Teki51366292015-05-21 01:09:31 +0530935 * dma-buf buffer.
Daniel Vetter4c785132012-04-24 14:38:52 +0530936 *
937 * This function adjusts the passed in vma so that it points at the file of the
Javier Martinez Canillasecf1dba2014-04-10 01:30:05 +0200938 * dma_buf operation. It also adjusts the starting pgoff and does bounds
Daniel Vetter4c785132012-04-24 14:38:52 +0530939 * checking on the size of the vma. Then it calls the exporters mmap function to
940 * set up the mapping.
941 *
942 * Can return negative error values, returns 0 on success.
943 */
944int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
945 unsigned long pgoff)
946{
John Sheu495c10c2013-02-11 17:50:24 -0800947 struct file *oldfile;
948 int ret;
949
Daniel Vetter4c785132012-04-24 14:38:52 +0530950 if (WARN_ON(!dmabuf || !vma))
951 return -EINVAL;
952
953 /* check for offset overflow */
Muhammad Falak R Wanib02da6f2016-05-23 17:08:42 +0530954 if (pgoff + vma_pages(vma) < pgoff)
Daniel Vetter4c785132012-04-24 14:38:52 +0530955 return -EOVERFLOW;
956
957 /* check for overflowing the buffer's size */
Muhammad Falak R Wanib02da6f2016-05-23 17:08:42 +0530958 if (pgoff + vma_pages(vma) >
Daniel Vetter4c785132012-04-24 14:38:52 +0530959 dmabuf->size >> PAGE_SHIFT)
960 return -EINVAL;
961
962 /* readjust the vma */
John Sheu495c10c2013-02-11 17:50:24 -0800963 get_file(dmabuf->file);
964 oldfile = vma->vm_file;
965 vma->vm_file = dmabuf->file;
Daniel Vetter4c785132012-04-24 14:38:52 +0530966 vma->vm_pgoff = pgoff;
967
John Sheu495c10c2013-02-11 17:50:24 -0800968 ret = dmabuf->ops->mmap(dmabuf, vma);
969 if (ret) {
970 /* restore old parameters on failure */
971 vma->vm_file = oldfile;
972 fput(dmabuf->file);
973 } else {
974 if (oldfile)
975 fput(oldfile);
976 }
977 return ret;
978
Daniel Vetter4c785132012-04-24 14:38:52 +0530979}
980EXPORT_SYMBOL_GPL(dma_buf_mmap);
Dave Airlie98f86c92012-05-20 12:33:56 +0530981
982/**
Sumit Semwal12c47272012-05-23 15:27:40 +0530983 * dma_buf_vmap - Create virtual mapping for the buffer object into kernel
984 * address space. Same restrictions as for vmap and friends apply.
985 * @dmabuf: [in] buffer to vmap
Dave Airlie98f86c92012-05-20 12:33:56 +0530986 *
987 * This call may fail due to lack of virtual mapping address space.
988 * These calls are optional in drivers. The intended use for them
989 * is for mapping objects linear in kernel space for high use objects.
990 * Please attempt to use kmap/kunmap before thinking about these interfaces.
Colin Crossfee0c542013-12-20 16:43:50 -0800991 *
992 * Returns NULL on error.
Dave Airlie98f86c92012-05-20 12:33:56 +0530993 */
994void *dma_buf_vmap(struct dma_buf *dmabuf)
995{
Daniel Vetterf00b4da2012-12-20 14:14:23 +0100996 void *ptr;
997
Dave Airlie98f86c92012-05-20 12:33:56 +0530998 if (WARN_ON(!dmabuf))
999 return NULL;
1000
Daniel Vetterf00b4da2012-12-20 14:14:23 +01001001 if (!dmabuf->ops->vmap)
1002 return NULL;
1003
1004 mutex_lock(&dmabuf->lock);
1005 if (dmabuf->vmapping_counter) {
1006 dmabuf->vmapping_counter++;
1007 BUG_ON(!dmabuf->vmap_ptr);
1008 ptr = dmabuf->vmap_ptr;
1009 goto out_unlock;
1010 }
1011
1012 BUG_ON(dmabuf->vmap_ptr);
1013
1014 ptr = dmabuf->ops->vmap(dmabuf);
Colin Crossfee0c542013-12-20 16:43:50 -08001015 if (WARN_ON_ONCE(IS_ERR(ptr)))
1016 ptr = NULL;
1017 if (!ptr)
Daniel Vetterf00b4da2012-12-20 14:14:23 +01001018 goto out_unlock;
1019
1020 dmabuf->vmap_ptr = ptr;
1021 dmabuf->vmapping_counter = 1;
1022
1023out_unlock:
1024 mutex_unlock(&dmabuf->lock);
1025 return ptr;
Dave Airlie98f86c92012-05-20 12:33:56 +05301026}
1027EXPORT_SYMBOL_GPL(dma_buf_vmap);
1028
1029/**
1030 * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
Sumit Semwal12c47272012-05-23 15:27:40 +05301031 * @dmabuf: [in] buffer to vunmap
Randy Dunlap6e7b4a52012-06-09 15:02:59 -07001032 * @vaddr: [in] vmap to vunmap
Dave Airlie98f86c92012-05-20 12:33:56 +05301033 */
1034void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
1035{
1036 if (WARN_ON(!dmabuf))
1037 return;
1038
Daniel Vetterf00b4da2012-12-20 14:14:23 +01001039 BUG_ON(!dmabuf->vmap_ptr);
1040 BUG_ON(dmabuf->vmapping_counter == 0);
1041 BUG_ON(dmabuf->vmap_ptr != vaddr);
1042
1043 mutex_lock(&dmabuf->lock);
1044 if (--dmabuf->vmapping_counter == 0) {
1045 if (dmabuf->ops->vunmap)
1046 dmabuf->ops->vunmap(dmabuf, vaddr);
1047 dmabuf->vmap_ptr = NULL;
1048 }
1049 mutex_unlock(&dmabuf->lock);
Dave Airlie98f86c92012-05-20 12:33:56 +05301050}
1051EXPORT_SYMBOL_GPL(dma_buf_vunmap);
Sumit Semwalb89e3562013-04-04 11:44:37 +05301052
1053#ifdef CONFIG_DEBUG_FS
Mathias Krauseeb0b9472016-06-19 14:31:29 +02001054static int dma_buf_debug_show(struct seq_file *s, void *unused)
Sumit Semwalb89e3562013-04-04 11:44:37 +05301055{
1056 int ret;
1057 struct dma_buf *buf_obj;
1058 struct dma_buf_attachment *attach_obj;
1059 int count = 0, attach_count;
1060 size_t size = 0;
1061
1062 ret = mutex_lock_interruptible(&db_list.lock);
1063
1064 if (ret)
1065 return ret;
1066
Sumit Semwalc0b00a52014-02-03 15:09:12 +05301067 seq_puts(s, "\nDma-buf Objects:\n");
1068 seq_puts(s, "size\tflags\tmode\tcount\texp_name\n");
Sumit Semwalb89e3562013-04-04 11:44:37 +05301069
1070 list_for_each_entry(buf_obj, &db_list.head, list_node) {
1071 ret = mutex_lock_interruptible(&buf_obj->lock);
1072
1073 if (ret) {
Sumit Semwalc0b00a52014-02-03 15:09:12 +05301074 seq_puts(s,
1075 "\tERROR locking buffer object: skipping\n");
Sumit Semwalb89e3562013-04-04 11:44:37 +05301076 continue;
1077 }
1078
Sumit Semwalc0b00a52014-02-03 15:09:12 +05301079 seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\n",
1080 buf_obj->size,
Sumit Semwalb89e3562013-04-04 11:44:37 +05301081 buf_obj->file->f_flags, buf_obj->file->f_mode,
Al Viroa1f6dba2014-08-20 11:05:50 -04001082 file_count(buf_obj->file),
Sumit Semwalc0b00a52014-02-03 15:09:12 +05301083 buf_obj->exp_name);
Sumit Semwalb89e3562013-04-04 11:44:37 +05301084
Sumit Semwalc0b00a52014-02-03 15:09:12 +05301085 seq_puts(s, "\tAttached Devices:\n");
Sumit Semwalb89e3562013-04-04 11:44:37 +05301086 attach_count = 0;
1087
1088 list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
Sumit Semwalc0b00a52014-02-03 15:09:12 +05301089 seq_puts(s, "\t");
Sumit Semwalb89e3562013-04-04 11:44:37 +05301090
Sumit Semwalc0b00a52014-02-03 15:09:12 +05301091 seq_printf(s, "%s\n", dev_name(attach_obj->dev));
Sumit Semwalb89e3562013-04-04 11:44:37 +05301092 attach_count++;
1093 }
1094
Sumit Semwalc0b00a52014-02-03 15:09:12 +05301095 seq_printf(s, "Total %d devices attached\n\n",
Sumit Semwalb89e3562013-04-04 11:44:37 +05301096 attach_count);
1097
1098 count++;
1099 size += buf_obj->size;
1100 mutex_unlock(&buf_obj->lock);
1101 }
1102
1103 seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size);
1104
1105 mutex_unlock(&db_list.lock);
1106 return 0;
1107}
1108
Sumit Semwalb89e3562013-04-04 11:44:37 +05301109static int dma_buf_debug_open(struct inode *inode, struct file *file)
1110{
Mathias Krauseeb0b9472016-06-19 14:31:29 +02001111 return single_open(file, dma_buf_debug_show, NULL);
Sumit Semwalb89e3562013-04-04 11:44:37 +05301112}
1113
1114static const struct file_operations dma_buf_debug_fops = {
1115 .open = dma_buf_debug_open,
1116 .read = seq_read,
1117 .llseek = seq_lseek,
1118 .release = single_release,
1119};
1120
1121static struct dentry *dma_buf_debugfs_dir;
1122
1123static int dma_buf_init_debugfs(void)
1124{
Mathias Krausebd3e2202016-06-19 14:31:31 +02001125 struct dentry *d;
Sumit Semwalb89e3562013-04-04 11:44:37 +05301126 int err = 0;
Jagan Teki51366292015-05-21 01:09:31 +05301127
Mathias Krausebd3e2202016-06-19 14:31:31 +02001128 d = debugfs_create_dir("dma_buf", NULL);
1129 if (IS_ERR(d))
1130 return PTR_ERR(d);
Jagan Teki51366292015-05-21 01:09:31 +05301131
Mathias Krausebd3e2202016-06-19 14:31:31 +02001132 dma_buf_debugfs_dir = d;
Sumit Semwalb89e3562013-04-04 11:44:37 +05301133
Mathias Krausebd3e2202016-06-19 14:31:31 +02001134 d = debugfs_create_file("bufinfo", S_IRUGO, dma_buf_debugfs_dir,
1135 NULL, &dma_buf_debug_fops);
1136 if (IS_ERR(d)) {
Sumit Semwalb89e3562013-04-04 11:44:37 +05301137 pr_debug("dma_buf: debugfs: failed to create node bufinfo\n");
Mathias Krauseb7479992016-06-19 14:31:30 +02001138 debugfs_remove_recursive(dma_buf_debugfs_dir);
1139 dma_buf_debugfs_dir = NULL;
Mathias Krausebd3e2202016-06-19 14:31:31 +02001140 err = PTR_ERR(d);
Mathias Krauseb7479992016-06-19 14:31:30 +02001141 }
Sumit Semwalb89e3562013-04-04 11:44:37 +05301142
1143 return err;
1144}
1145
1146static void dma_buf_uninit_debugfs(void)
1147{
1148 if (dma_buf_debugfs_dir)
1149 debugfs_remove_recursive(dma_buf_debugfs_dir);
1150}
Sumit Semwalb89e3562013-04-04 11:44:37 +05301151#else
1152static inline int dma_buf_init_debugfs(void)
1153{
1154 return 0;
1155}
1156static inline void dma_buf_uninit_debugfs(void)
1157{
1158}
1159#endif
1160
1161static int __init dma_buf_init(void)
1162{
1163 mutex_init(&db_list.lock);
1164 INIT_LIST_HEAD(&db_list.head);
1165 dma_buf_init_debugfs();
1166 return 0;
1167}
1168subsys_initcall(dma_buf_init);
1169
1170static void __exit dma_buf_deinit(void)
1171{
1172 dma_buf_uninit_debugfs();
1173}
1174__exitcall(dma_buf_deinit);