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