dma-buf: add ref counting for module as exporter
[linux-2.6-block.git] / drivers / dma-buf / dma-buf.c
CommitLineData
d15bd7ee
SS
1/*
2 * Framework for buffer objects that can be shared across devices/subsystems.
3 *
4 * Copyright(C) 2011 Linaro Limited. All rights reserved.
5 * Author: Sumit Semwal <sumit.semwal@ti.com>
6 *
7 * Many thanks to linaro-mm-sig list, and specially
8 * Arnd Bergmann <arnd@arndb.de>, Rob Clark <rob@ti.com> and
9 * Daniel Vetter <daniel@ffwll.ch> for their support in creation and
10 * refining of this idea.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published by
14 * the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * this program. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25#include <linux/fs.h>
26#include <linux/slab.h>
27#include <linux/dma-buf.h>
3aac4502 28#include <linux/fence.h>
d15bd7ee
SS
29#include <linux/anon_inodes.h>
30#include <linux/export.h>
b89e3563 31#include <linux/debugfs.h>
9abdffe2 32#include <linux/module.h>
b89e3563 33#include <linux/seq_file.h>
9b495a58 34#include <linux/poll.h>
3aac4502 35#include <linux/reservation.h>
d15bd7ee
SS
36
37static inline int is_dma_buf_file(struct file *);
38
b89e3563
SS
39struct dma_buf_list {
40 struct list_head head;
41 struct mutex lock;
42};
43
44static struct dma_buf_list db_list;
45
d15bd7ee
SS
46static int dma_buf_release(struct inode *inode, struct file *file)
47{
48 struct dma_buf *dmabuf;
49
50 if (!is_dma_buf_file(file))
51 return -EINVAL;
52
53 dmabuf = file->private_data;
54
f00b4dad
DV
55 BUG_ON(dmabuf->vmapping_counter);
56
9b495a58
ML
57 /*
58 * Any fences that a dma-buf poll can wait on should be signaled
59 * before releasing dma-buf. This is the responsibility of each
60 * driver that uses the reservation objects.
61 *
62 * If you hit this BUG() it means someone dropped their ref to the
63 * dma-buf while still having pending operation to the buffer.
64 */
65 BUG_ON(dmabuf->cb_shared.active || dmabuf->cb_excl.active);
66
d15bd7ee 67 dmabuf->ops->release(dmabuf);
b89e3563
SS
68
69 mutex_lock(&db_list.lock);
70 list_del(&dmabuf->list_node);
71 mutex_unlock(&db_list.lock);
72
3aac4502
ML
73 if (dmabuf->resv == (struct reservation_object *)&dmabuf[1])
74 reservation_object_fini(dmabuf->resv);
75
9abdffe2 76 module_put(dmabuf->owner);
d15bd7ee
SS
77 kfree(dmabuf);
78 return 0;
79}
80
4c78513e
DV
81static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
82{
83 struct dma_buf *dmabuf;
84
85 if (!is_dma_buf_file(file))
86 return -EINVAL;
87
88 dmabuf = file->private_data;
89
90 /* check for overflowing the buffer's size */
91 if (vma->vm_pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
92 dmabuf->size >> PAGE_SHIFT)
93 return -EINVAL;
94
95 return dmabuf->ops->mmap(dmabuf, vma);
96}
97
19e8697b
CJHR
98static loff_t dma_buf_llseek(struct file *file, loff_t offset, int whence)
99{
100 struct dma_buf *dmabuf;
101 loff_t base;
102
103 if (!is_dma_buf_file(file))
104 return -EBADF;
105
106 dmabuf = file->private_data;
107
108 /* only support discovering the end of the buffer,
109 but also allow SEEK_SET to maintain the idiomatic
110 SEEK_END(0), SEEK_CUR(0) pattern */
111 if (whence == SEEK_END)
112 base = dmabuf->size;
113 else if (whence == SEEK_SET)
114 base = 0;
115 else
116 return -EINVAL;
117
118 if (offset != 0)
119 return -EINVAL;
120
121 return base + offset;
122}
123
9b495a58
ML
124static void dma_buf_poll_cb(struct fence *fence, struct fence_cb *cb)
125{
126 struct dma_buf_poll_cb_t *dcb = (struct dma_buf_poll_cb_t *)cb;
127 unsigned long flags;
128
129 spin_lock_irqsave(&dcb->poll->lock, flags);
130 wake_up_locked_poll(dcb->poll, dcb->active);
131 dcb->active = 0;
132 spin_unlock_irqrestore(&dcb->poll->lock, flags);
133}
134
135static unsigned int dma_buf_poll(struct file *file, poll_table *poll)
136{
137 struct dma_buf *dmabuf;
138 struct reservation_object *resv;
04a5faa8
ML
139 struct reservation_object_list *fobj;
140 struct fence *fence_excl;
9b495a58 141 unsigned long events;
3c3b177a 142 unsigned shared_count, seq;
9b495a58
ML
143
144 dmabuf = file->private_data;
145 if (!dmabuf || !dmabuf->resv)
146 return POLLERR;
147
148 resv = dmabuf->resv;
149
150 poll_wait(file, &dmabuf->poll, poll);
151
152 events = poll_requested_events(poll) & (POLLIN | POLLOUT);
153 if (!events)
154 return 0;
155
3c3b177a
ML
156retry:
157 seq = read_seqcount_begin(&resv->seq);
158 rcu_read_lock();
9b495a58 159
3c3b177a
ML
160 fobj = rcu_dereference(resv->fence);
161 if (fobj)
162 shared_count = fobj->shared_count;
163 else
164 shared_count = 0;
165 fence_excl = rcu_dereference(resv->fence_excl);
166 if (read_seqcount_retry(&resv->seq, seq)) {
167 rcu_read_unlock();
168 goto retry;
169 }
04a5faa8
ML
170
171 if (fence_excl && (!(events & POLLOUT) || shared_count == 0)) {
9b495a58
ML
172 struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_excl;
173 unsigned long pevents = POLLIN;
174
04a5faa8 175 if (shared_count == 0)
9b495a58
ML
176 pevents |= POLLOUT;
177
178 spin_lock_irq(&dmabuf->poll.lock);
179 if (dcb->active) {
180 dcb->active |= pevents;
181 events &= ~pevents;
182 } else
183 dcb->active = pevents;
184 spin_unlock_irq(&dmabuf->poll.lock);
185
186 if (events & pevents) {
3c3b177a
ML
187 if (!fence_get_rcu(fence_excl)) {
188 /* force a recheck */
189 events &= ~pevents;
190 dma_buf_poll_cb(NULL, &dcb->cb);
191 } else if (!fence_add_callback(fence_excl, &dcb->cb,
04a5faa8 192 dma_buf_poll_cb)) {
9b495a58 193 events &= ~pevents;
3c3b177a 194 fence_put(fence_excl);
04a5faa8 195 } else {
9b495a58
ML
196 /*
197 * No callback queued, wake up any additional
198 * waiters.
199 */
3c3b177a 200 fence_put(fence_excl);
9b495a58 201 dma_buf_poll_cb(NULL, &dcb->cb);
04a5faa8 202 }
9b495a58
ML
203 }
204 }
205
04a5faa8 206 if ((events & POLLOUT) && shared_count > 0) {
9b495a58
ML
207 struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_shared;
208 int i;
209
210 /* Only queue a new callback if no event has fired yet */
211 spin_lock_irq(&dmabuf->poll.lock);
212 if (dcb->active)
213 events &= ~POLLOUT;
214 else
215 dcb->active = POLLOUT;
216 spin_unlock_irq(&dmabuf->poll.lock);
217
218 if (!(events & POLLOUT))
219 goto out;
220
04a5faa8 221 for (i = 0; i < shared_count; ++i) {
3c3b177a 222 struct fence *fence = rcu_dereference(fobj->shared[i]);
04a5faa8 223
3c3b177a
ML
224 if (!fence_get_rcu(fence)) {
225 /*
226 * fence refcount dropped to zero, this means
227 * that fobj has been freed
228 *
229 * call dma_buf_poll_cb and force a recheck!
230 */
231 events &= ~POLLOUT;
232 dma_buf_poll_cb(NULL, &dcb->cb);
233 break;
234 }
04a5faa8
ML
235 if (!fence_add_callback(fence, &dcb->cb,
236 dma_buf_poll_cb)) {
3c3b177a 237 fence_put(fence);
9b495a58
ML
238 events &= ~POLLOUT;
239 break;
240 }
3c3b177a 241 fence_put(fence);
04a5faa8 242 }
9b495a58
ML
243
244 /* No callback queued, wake up any additional waiters. */
04a5faa8 245 if (i == shared_count)
9b495a58
ML
246 dma_buf_poll_cb(NULL, &dcb->cb);
247 }
248
249out:
3c3b177a 250 rcu_read_unlock();
9b495a58
ML
251 return events;
252}
253
d15bd7ee
SS
254static const struct file_operations dma_buf_fops = {
255 .release = dma_buf_release,
4c78513e 256 .mmap = dma_buf_mmap_internal,
19e8697b 257 .llseek = dma_buf_llseek,
9b495a58 258 .poll = dma_buf_poll,
d15bd7ee
SS
259};
260
261/*
262 * is_dma_buf_file - Check if struct file* is associated with dma_buf
263 */
264static inline int is_dma_buf_file(struct file *file)
265{
266 return file->f_op == &dma_buf_fops;
267}
268
269/**
d8fbe341 270 * dma_buf_export - Creates a new dma_buf, and associates an anon file
d15bd7ee
SS
271 * with this buffer, so it can be exported.
272 * Also connect the allocator specific data and ops to the buffer.
78df9695 273 * Additionally, provide a name string for exporter; useful in debugging.
d15bd7ee 274 *
d8fbe341
SS
275 * @exp_info: [in] holds all the export related information provided
276 * by the exporter. see struct dma_buf_export_info
277 * for further details.
d15bd7ee
SS
278 *
279 * Returns, on success, a newly created dma_buf object, which wraps the
280 * supplied private data and operations for dma_buf_ops. On either missing
281 * ops, or error in allocating struct dma_buf, will return negative error.
282 *
283 */
d8fbe341 284struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
d15bd7ee
SS
285{
286 struct dma_buf *dmabuf;
d8fbe341 287 struct reservation_object *resv = exp_info->resv;
d15bd7ee 288 struct file *file;
3aac4502 289 size_t alloc_size = sizeof(struct dma_buf);
d8fbe341 290 if (!exp_info->resv)
3aac4502
ML
291 alloc_size += sizeof(struct reservation_object);
292 else
293 /* prevent &dma_buf[1] == dma_buf->resv */
294 alloc_size += 1;
d15bd7ee 295
d8fbe341
SS
296 if (WARN_ON(!exp_info->priv
297 || !exp_info->ops
298 || !exp_info->ops->map_dma_buf
299 || !exp_info->ops->unmap_dma_buf
300 || !exp_info->ops->release
301 || !exp_info->ops->kmap_atomic
302 || !exp_info->ops->kmap
303 || !exp_info->ops->mmap)) {
d15bd7ee
SS
304 return ERR_PTR(-EINVAL);
305 }
306
9abdffe2
SS
307 if (!try_module_get(exp_info->owner))
308 return ERR_PTR(-ENOENT);
309
3aac4502 310 dmabuf = kzalloc(alloc_size, GFP_KERNEL);
9abdffe2
SS
311 if (!dmabuf) {
312 module_put(exp_info->owner);
d15bd7ee 313 return ERR_PTR(-ENOMEM);
9abdffe2 314 }
d15bd7ee 315
d8fbe341
SS
316 dmabuf->priv = exp_info->priv;
317 dmabuf->ops = exp_info->ops;
318 dmabuf->size = exp_info->size;
319 dmabuf->exp_name = exp_info->exp_name;
9abdffe2 320 dmabuf->owner = exp_info->owner;
9b495a58
ML
321 init_waitqueue_head(&dmabuf->poll);
322 dmabuf->cb_excl.poll = dmabuf->cb_shared.poll = &dmabuf->poll;
323 dmabuf->cb_excl.active = dmabuf->cb_shared.active = 0;
324
3aac4502
ML
325 if (!resv) {
326 resv = (struct reservation_object *)&dmabuf[1];
327 reservation_object_init(resv);
328 }
329 dmabuf->resv = resv;
d15bd7ee 330
d8fbe341
SS
331 file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf,
332 exp_info->flags);
9022e24e
TT
333 if (IS_ERR(file)) {
334 kfree(dmabuf);
335 return ERR_CAST(file);
336 }
19e8697b
CJHR
337
338 file->f_mode |= FMODE_LSEEK;
d15bd7ee
SS
339 dmabuf->file = file;
340
341 mutex_init(&dmabuf->lock);
342 INIT_LIST_HEAD(&dmabuf->attachments);
343
b89e3563
SS
344 mutex_lock(&db_list.lock);
345 list_add(&dmabuf->list_node, &db_list.head);
346 mutex_unlock(&db_list.lock);
347
d15bd7ee
SS
348 return dmabuf;
349}
d8fbe341 350EXPORT_SYMBOL_GPL(dma_buf_export);
d15bd7ee
SS
351
352/**
353 * dma_buf_fd - returns a file descriptor for the given dma_buf
354 * @dmabuf: [in] pointer to dma_buf for which fd is required.
55c1c4ca 355 * @flags: [in] flags to give to fd
d15bd7ee
SS
356 *
357 * On success, returns an associated 'fd'. Else, returns error.
358 */
55c1c4ca 359int dma_buf_fd(struct dma_buf *dmabuf, int flags)
d15bd7ee 360{
f5e097f0 361 int fd;
d15bd7ee
SS
362
363 if (!dmabuf || !dmabuf->file)
364 return -EINVAL;
365
f5e097f0
BP
366 fd = get_unused_fd_flags(flags);
367 if (fd < 0)
368 return fd;
d15bd7ee
SS
369
370 fd_install(fd, dmabuf->file);
371
372 return fd;
373}
374EXPORT_SYMBOL_GPL(dma_buf_fd);
375
376/**
377 * dma_buf_get - returns the dma_buf structure related to an fd
378 * @fd: [in] fd associated with the dma_buf to be returned
379 *
380 * On success, returns the dma_buf structure associated with an fd; uses
381 * file's refcounting done by fget to increase refcount. returns ERR_PTR
382 * otherwise.
383 */
384struct dma_buf *dma_buf_get(int fd)
385{
386 struct file *file;
387
388 file = fget(fd);
389
390 if (!file)
391 return ERR_PTR(-EBADF);
392
393 if (!is_dma_buf_file(file)) {
394 fput(file);
395 return ERR_PTR(-EINVAL);
396 }
397
398 return file->private_data;
399}
400EXPORT_SYMBOL_GPL(dma_buf_get);
401
402/**
403 * dma_buf_put - decreases refcount of the buffer
404 * @dmabuf: [in] buffer to reduce refcount of
405 *
406 * Uses file's refcounting done implicitly by fput()
407 */
408void dma_buf_put(struct dma_buf *dmabuf)
409{
410 if (WARN_ON(!dmabuf || !dmabuf->file))
411 return;
412
413 fput(dmabuf->file);
414}
415EXPORT_SYMBOL_GPL(dma_buf_put);
416
417/**
418 * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
419 * calls attach() of dma_buf_ops to allow device-specific attach functionality
420 * @dmabuf: [in] buffer to attach device to.
421 * @dev: [in] device to be attached.
422 *
fee0c54e
CC
423 * Returns struct dma_buf_attachment * for this attachment; returns ERR_PTR on
424 * error.
d15bd7ee
SS
425 */
426struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
427 struct device *dev)
428{
429 struct dma_buf_attachment *attach;
430 int ret;
431
d1aa06a1 432 if (WARN_ON(!dmabuf || !dev))
d15bd7ee
SS
433 return ERR_PTR(-EINVAL);
434
435 attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL);
436 if (attach == NULL)
a9fbc3b7 437 return ERR_PTR(-ENOMEM);
d15bd7ee 438
d15bd7ee
SS
439 attach->dev = dev;
440 attach->dmabuf = dmabuf;
2ed9201b
LP
441
442 mutex_lock(&dmabuf->lock);
443
d15bd7ee
SS
444 if (dmabuf->ops->attach) {
445 ret = dmabuf->ops->attach(dmabuf, dev, attach);
446 if (ret)
447 goto err_attach;
448 }
449 list_add(&attach->node, &dmabuf->attachments);
450
451 mutex_unlock(&dmabuf->lock);
452 return attach;
453
d15bd7ee
SS
454err_attach:
455 kfree(attach);
456 mutex_unlock(&dmabuf->lock);
457 return ERR_PTR(ret);
458}
459EXPORT_SYMBOL_GPL(dma_buf_attach);
460
461/**
462 * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
463 * optionally calls detach() of dma_buf_ops for device-specific detach
464 * @dmabuf: [in] buffer to detach from.
465 * @attach: [in] attachment to be detached; is free'd after this call.
466 *
467 */
468void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
469{
d1aa06a1 470 if (WARN_ON(!dmabuf || !attach))
d15bd7ee
SS
471 return;
472
473 mutex_lock(&dmabuf->lock);
474 list_del(&attach->node);
475 if (dmabuf->ops->detach)
476 dmabuf->ops->detach(dmabuf, attach);
477
478 mutex_unlock(&dmabuf->lock);
479 kfree(attach);
480}
481EXPORT_SYMBOL_GPL(dma_buf_detach);
482
483/**
484 * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
485 * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
486 * dma_buf_ops.
487 * @attach: [in] attachment whose scatterlist is to be returned
488 * @direction: [in] direction of DMA transfer
489 *
fee0c54e
CC
490 * Returns sg_table containing the scatterlist to be returned; returns ERR_PTR
491 * on error.
d15bd7ee
SS
492 */
493struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
494 enum dma_data_direction direction)
495{
496 struct sg_table *sg_table = ERR_PTR(-EINVAL);
497
498 might_sleep();
499
d1aa06a1 500 if (WARN_ON(!attach || !attach->dmabuf))
d15bd7ee
SS
501 return ERR_PTR(-EINVAL);
502
d1aa06a1 503 sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
fee0c54e
CC
504 if (!sg_table)
505 sg_table = ERR_PTR(-ENOMEM);
d15bd7ee
SS
506
507 return sg_table;
508}
509EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
510
511/**
512 * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
513 * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
514 * dma_buf_ops.
515 * @attach: [in] attachment to unmap buffer from
516 * @sg_table: [in] scatterlist info of the buffer to unmap
33ea2dcb 517 * @direction: [in] direction of DMA transfer
d15bd7ee
SS
518 *
519 */
520void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
33ea2dcb
SS
521 struct sg_table *sg_table,
522 enum dma_data_direction direction)
d15bd7ee 523{
b6fa0cd6
RC
524 might_sleep();
525
d1aa06a1 526 if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
d15bd7ee
SS
527 return;
528
33ea2dcb
SS
529 attach->dmabuf->ops->unmap_dma_buf(attach, sg_table,
530 direction);
d15bd7ee
SS
531}
532EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
fc13020e
DV
533
534
535/**
536 * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
537 * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
538 * preparations. Coherency is only guaranteed in the specified range for the
539 * specified access direction.
efb4df82 540 * @dmabuf: [in] buffer to prepare cpu access for.
fc13020e
DV
541 * @start: [in] start of range for cpu access.
542 * @len: [in] length of range for cpu access.
543 * @direction: [in] length of range for cpu access.
544 *
545 * Can return negative error values, returns 0 on success.
546 */
547int dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
548 enum dma_data_direction direction)
549{
550 int ret = 0;
551
552 if (WARN_ON(!dmabuf))
553 return -EINVAL;
554
555 if (dmabuf->ops->begin_cpu_access)
556 ret = dmabuf->ops->begin_cpu_access(dmabuf, start, len, direction);
557
558 return ret;
559}
560EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access);
561
562/**
563 * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
564 * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
565 * actions. Coherency is only guaranteed in the specified range for the
566 * specified access direction.
efb4df82 567 * @dmabuf: [in] buffer to complete cpu access for.
fc13020e
DV
568 * @start: [in] start of range for cpu access.
569 * @len: [in] length of range for cpu access.
570 * @direction: [in] length of range for cpu access.
571 *
572 * This call must always succeed.
573 */
574void dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
575 enum dma_data_direction direction)
576{
577 WARN_ON(!dmabuf);
578
579 if (dmabuf->ops->end_cpu_access)
580 dmabuf->ops->end_cpu_access(dmabuf, start, len, direction);
581}
582EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access);
583
584/**
585 * dma_buf_kmap_atomic - Map a page of the buffer object into kernel address
586 * space. The same restrictions as for kmap_atomic and friends apply.
efb4df82 587 * @dmabuf: [in] buffer to map page from.
fc13020e
DV
588 * @page_num: [in] page in PAGE_SIZE units to map.
589 *
590 * This call must always succeed, any necessary preparations that might fail
591 * need to be done in begin_cpu_access.
592 */
593void *dma_buf_kmap_atomic(struct dma_buf *dmabuf, unsigned long page_num)
594{
595 WARN_ON(!dmabuf);
596
597 return dmabuf->ops->kmap_atomic(dmabuf, page_num);
598}
599EXPORT_SYMBOL_GPL(dma_buf_kmap_atomic);
600
601/**
602 * dma_buf_kunmap_atomic - Unmap a page obtained by dma_buf_kmap_atomic.
efb4df82 603 * @dmabuf: [in] buffer to unmap page from.
fc13020e
DV
604 * @page_num: [in] page in PAGE_SIZE units to unmap.
605 * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap_atomic.
606 *
607 * This call must always succeed.
608 */
609void dma_buf_kunmap_atomic(struct dma_buf *dmabuf, unsigned long page_num,
610 void *vaddr)
611{
612 WARN_ON(!dmabuf);
613
614 if (dmabuf->ops->kunmap_atomic)
615 dmabuf->ops->kunmap_atomic(dmabuf, page_num, vaddr);
616}
617EXPORT_SYMBOL_GPL(dma_buf_kunmap_atomic);
618
619/**
620 * dma_buf_kmap - Map a page of the buffer object into kernel address space. The
621 * same restrictions as for kmap and friends apply.
efb4df82 622 * @dmabuf: [in] buffer to map page from.
fc13020e
DV
623 * @page_num: [in] page in PAGE_SIZE units to map.
624 *
625 * This call must always succeed, any necessary preparations that might fail
626 * need to be done in begin_cpu_access.
627 */
628void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num)
629{
630 WARN_ON(!dmabuf);
631
632 return dmabuf->ops->kmap(dmabuf, page_num);
633}
634EXPORT_SYMBOL_GPL(dma_buf_kmap);
635
636/**
637 * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap.
efb4df82 638 * @dmabuf: [in] buffer to unmap page from.
fc13020e
DV
639 * @page_num: [in] page in PAGE_SIZE units to unmap.
640 * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap.
641 *
642 * This call must always succeed.
643 */
644void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num,
645 void *vaddr)
646{
647 WARN_ON(!dmabuf);
648
649 if (dmabuf->ops->kunmap)
650 dmabuf->ops->kunmap(dmabuf, page_num, vaddr);
651}
652EXPORT_SYMBOL_GPL(dma_buf_kunmap);
4c78513e
DV
653
654
655/**
656 * dma_buf_mmap - Setup up a userspace mmap with the given vma
12c4727e 657 * @dmabuf: [in] buffer that should back the vma
4c78513e
DV
658 * @vma: [in] vma for the mmap
659 * @pgoff: [in] offset in pages where this mmap should start within the
660 * dma-buf buffer.
661 *
662 * This function adjusts the passed in vma so that it points at the file of the
ecf1dbac 663 * dma_buf operation. It also adjusts the starting pgoff and does bounds
4c78513e
DV
664 * checking on the size of the vma. Then it calls the exporters mmap function to
665 * set up the mapping.
666 *
667 * Can return negative error values, returns 0 on success.
668 */
669int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
670 unsigned long pgoff)
671{
495c10cc
JS
672 struct file *oldfile;
673 int ret;
674
4c78513e
DV
675 if (WARN_ON(!dmabuf || !vma))
676 return -EINVAL;
677
678 /* check for offset overflow */
679 if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) < pgoff)
680 return -EOVERFLOW;
681
682 /* check for overflowing the buffer's size */
683 if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
684 dmabuf->size >> PAGE_SHIFT)
685 return -EINVAL;
686
687 /* readjust the vma */
495c10cc
JS
688 get_file(dmabuf->file);
689 oldfile = vma->vm_file;
690 vma->vm_file = dmabuf->file;
4c78513e
DV
691 vma->vm_pgoff = pgoff;
692
495c10cc
JS
693 ret = dmabuf->ops->mmap(dmabuf, vma);
694 if (ret) {
695 /* restore old parameters on failure */
696 vma->vm_file = oldfile;
697 fput(dmabuf->file);
698 } else {
699 if (oldfile)
700 fput(oldfile);
701 }
702 return ret;
703
4c78513e
DV
704}
705EXPORT_SYMBOL_GPL(dma_buf_mmap);
98f86c9e
DA
706
707/**
12c4727e
SS
708 * dma_buf_vmap - Create virtual mapping for the buffer object into kernel
709 * address space. Same restrictions as for vmap and friends apply.
710 * @dmabuf: [in] buffer to vmap
98f86c9e
DA
711 *
712 * This call may fail due to lack of virtual mapping address space.
713 * These calls are optional in drivers. The intended use for them
714 * is for mapping objects linear in kernel space for high use objects.
715 * Please attempt to use kmap/kunmap before thinking about these interfaces.
fee0c54e
CC
716 *
717 * Returns NULL on error.
98f86c9e
DA
718 */
719void *dma_buf_vmap(struct dma_buf *dmabuf)
720{
f00b4dad
DV
721 void *ptr;
722
98f86c9e
DA
723 if (WARN_ON(!dmabuf))
724 return NULL;
725
f00b4dad
DV
726 if (!dmabuf->ops->vmap)
727 return NULL;
728
729 mutex_lock(&dmabuf->lock);
730 if (dmabuf->vmapping_counter) {
731 dmabuf->vmapping_counter++;
732 BUG_ON(!dmabuf->vmap_ptr);
733 ptr = dmabuf->vmap_ptr;
734 goto out_unlock;
735 }
736
737 BUG_ON(dmabuf->vmap_ptr);
738
739 ptr = dmabuf->ops->vmap(dmabuf);
fee0c54e
CC
740 if (WARN_ON_ONCE(IS_ERR(ptr)))
741 ptr = NULL;
742 if (!ptr)
f00b4dad
DV
743 goto out_unlock;
744
745 dmabuf->vmap_ptr = ptr;
746 dmabuf->vmapping_counter = 1;
747
748out_unlock:
749 mutex_unlock(&dmabuf->lock);
750 return ptr;
98f86c9e
DA
751}
752EXPORT_SYMBOL_GPL(dma_buf_vmap);
753
754/**
755 * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
12c4727e 756 * @dmabuf: [in] buffer to vunmap
6e7b4a59 757 * @vaddr: [in] vmap to vunmap
98f86c9e
DA
758 */
759void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
760{
761 if (WARN_ON(!dmabuf))
762 return;
763
f00b4dad
DV
764 BUG_ON(!dmabuf->vmap_ptr);
765 BUG_ON(dmabuf->vmapping_counter == 0);
766 BUG_ON(dmabuf->vmap_ptr != vaddr);
767
768 mutex_lock(&dmabuf->lock);
769 if (--dmabuf->vmapping_counter == 0) {
770 if (dmabuf->ops->vunmap)
771 dmabuf->ops->vunmap(dmabuf, vaddr);
772 dmabuf->vmap_ptr = NULL;
773 }
774 mutex_unlock(&dmabuf->lock);
98f86c9e
DA
775}
776EXPORT_SYMBOL_GPL(dma_buf_vunmap);
b89e3563
SS
777
778#ifdef CONFIG_DEBUG_FS
779static int dma_buf_describe(struct seq_file *s)
780{
781 int ret;
782 struct dma_buf *buf_obj;
783 struct dma_buf_attachment *attach_obj;
784 int count = 0, attach_count;
785 size_t size = 0;
786
787 ret = mutex_lock_interruptible(&db_list.lock);
788
789 if (ret)
790 return ret;
791
c0b00a52
SS
792 seq_puts(s, "\nDma-buf Objects:\n");
793 seq_puts(s, "size\tflags\tmode\tcount\texp_name\n");
b89e3563
SS
794
795 list_for_each_entry(buf_obj, &db_list.head, list_node) {
796 ret = mutex_lock_interruptible(&buf_obj->lock);
797
798 if (ret) {
c0b00a52
SS
799 seq_puts(s,
800 "\tERROR locking buffer object: skipping\n");
b89e3563
SS
801 continue;
802 }
803
c0b00a52
SS
804 seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\n",
805 buf_obj->size,
b89e3563 806 buf_obj->file->f_flags, buf_obj->file->f_mode,
a1f6dbac 807 file_count(buf_obj->file),
c0b00a52 808 buf_obj->exp_name);
b89e3563 809
c0b00a52 810 seq_puts(s, "\tAttached Devices:\n");
b89e3563
SS
811 attach_count = 0;
812
813 list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
c0b00a52 814 seq_puts(s, "\t");
b89e3563 815
c0b00a52 816 seq_printf(s, "%s\n", dev_name(attach_obj->dev));
b89e3563
SS
817 attach_count++;
818 }
819
c0b00a52 820 seq_printf(s, "Total %d devices attached\n\n",
b89e3563
SS
821 attach_count);
822
823 count++;
824 size += buf_obj->size;
825 mutex_unlock(&buf_obj->lock);
826 }
827
828 seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size);
829
830 mutex_unlock(&db_list.lock);
831 return 0;
832}
833
834static int dma_buf_show(struct seq_file *s, void *unused)
835{
836 void (*func)(struct seq_file *) = s->private;
837 func(s);
838 return 0;
839}
840
841static int dma_buf_debug_open(struct inode *inode, struct file *file)
842{
843 return single_open(file, dma_buf_show, inode->i_private);
844}
845
846static const struct file_operations dma_buf_debug_fops = {
847 .open = dma_buf_debug_open,
848 .read = seq_read,
849 .llseek = seq_lseek,
850 .release = single_release,
851};
852
853static struct dentry *dma_buf_debugfs_dir;
854
855static int dma_buf_init_debugfs(void)
856{
857 int err = 0;
858 dma_buf_debugfs_dir = debugfs_create_dir("dma_buf", NULL);
859 if (IS_ERR(dma_buf_debugfs_dir)) {
860 err = PTR_ERR(dma_buf_debugfs_dir);
861 dma_buf_debugfs_dir = NULL;
862 return err;
863 }
864
865 err = dma_buf_debugfs_create_file("bufinfo", dma_buf_describe);
866
867 if (err)
868 pr_debug("dma_buf: debugfs: failed to create node bufinfo\n");
869
870 return err;
871}
872
873static void dma_buf_uninit_debugfs(void)
874{
875 if (dma_buf_debugfs_dir)
876 debugfs_remove_recursive(dma_buf_debugfs_dir);
877}
878
879int dma_buf_debugfs_create_file(const char *name,
880 int (*write)(struct seq_file *))
881{
882 struct dentry *d;
883
884 d = debugfs_create_file(name, S_IRUGO, dma_buf_debugfs_dir,
885 write, &dma_buf_debug_fops);
886
28ce4201 887 return PTR_ERR_OR_ZERO(d);
b89e3563
SS
888}
889#else
890static inline int dma_buf_init_debugfs(void)
891{
892 return 0;
893}
894static inline void dma_buf_uninit_debugfs(void)
895{
896}
897#endif
898
899static int __init dma_buf_init(void)
900{
901 mutex_init(&db_list.lock);
902 INIT_LIST_HEAD(&db_list.head);
903 dma_buf_init_debugfs();
904 return 0;
905}
906subsys_initcall(dma_buf_init);
907
908static void __exit dma_buf_deinit(void)
909{
910 dma_buf_uninit_debugfs();
911}
912__exitcall(dma_buf_deinit);