CHROMIUM: dma-buf/kds: allow KDS to be compiled out if dma-buf is enabled
[cascardo/linux.git] / drivers / base / 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/anon_inodes.h>
29 #include <linux/export.h>
30 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
31 #include <linux/poll.h>
32 #include <linux/sched.h>
33 #endif
34
35 static inline int is_dma_buf_file(struct file *);
36
37 static int dma_buf_release(struct inode *inode, struct file *file)
38 {
39         struct dma_buf *dmabuf;
40
41         if (!is_dma_buf_file(file))
42                 return -EINVAL;
43
44         dmabuf = file->private_data;
45
46         dmabuf->ops->release(dmabuf);
47 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
48         kds_callback_term(&dmabuf->kds_cb);
49         kds_resource_term(&dmabuf->kds);
50 #endif
51         kfree(dmabuf);
52         return 0;
53 }
54
55 static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
56 {
57         struct dma_buf *dmabuf;
58
59         if (!is_dma_buf_file(file))
60                 return -EINVAL;
61
62         dmabuf = file->private_data;
63
64         /* check for overflowing the buffer's size */
65         if (vma->vm_pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
66             dmabuf->size >> PAGE_SHIFT)
67                 return -EINVAL;
68
69         return dmabuf->ops->mmap(dmabuf, vma);
70 }
71
72 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
73 static void dma_buf_kds_cb_fn(void *param1, void *param2)
74 {
75         struct kds_resource_set **rset_ptr = param1;
76         struct kds_resource_set *rset = *rset_ptr;
77         wait_queue_head_t *wait_queue = param2;
78
79         kfree(rset_ptr);
80         kds_resource_set_release(&rset);
81         wake_up(wait_queue);
82 }
83
84 static int dma_buf_kds_check(struct kds_resource *kds,
85                 unsigned long exclusive, int *poll_ret)
86 {
87         /* Synchronous wait with 0 timeout - poll availability */
88         struct kds_resource_set *rset = kds_waitall(1, &exclusive, &kds, 0);
89
90         if (IS_ERR(rset))
91                 return POLLERR;
92
93         if (rset) {
94                 kds_resource_set_release(&rset);
95                 *poll_ret = POLLIN | POLLRDNORM;
96                 if (exclusive)
97                         *poll_ret |=  POLLOUT | POLLWRNORM;
98                 return 1;
99         } else {
100                 return 0;
101         }
102 }
103
104 static unsigned int dma_buf_poll(struct file *file,
105                 struct poll_table_struct *wait)
106 {
107         struct dma_buf *dmabuf;
108         struct kds_resource *kds;
109         unsigned int ret = 0;
110
111         if (!is_dma_buf_file(file))
112                 return POLLERR;
113
114         dmabuf = file->private_data;
115         kds    = &dmabuf->kds;
116
117         if (poll_does_not_wait(wait)) {
118                 /* Check for exclusive access (superset of shared) first */
119                 if (!dma_buf_kds_check(kds, 1ul, &ret))
120                         dma_buf_kds_check(kds, 0ul, &ret);
121         } else {
122                 int events = poll_requested_events(wait);
123                 unsigned long exclusive;
124                 wait_queue_head_t *wq;
125                 struct kds_resource_set **rset_ptr =
126                                 kmalloc(sizeof(*rset_ptr), GFP_KERNEL);
127
128                 if (!rset_ptr)
129                         return POLL_ERR;
130
131                 if (events & POLLOUT) {
132                         wq = &dmabuf->wq_exclusive;
133                         exclusive = 1;
134                 } else {
135                         wq = &dmabuf->wq_shared;
136                         exclusive = 0;
137                 }
138                 poll_wait(file, wq, wait);
139                 ret = kds_async_waitall(rset_ptr, KDS_FLAG_LOCKED_WAIT,
140                                 &dmabuf->kds_cb, rset_ptr, wq, 1, &exclusive,
141                                 &kds);
142
143                 if (IS_ERR_VALUE(ret)) {
144                         ret = POLL_ERR;
145                         kfree(rset_ptr);
146                 } else {
147                         /* Can't allow access until callback */
148                         ret = 0;
149                 }
150         }
151         return ret;
152 }
153 #endif
154
155 static const struct file_operations dma_buf_fops = {
156         .release        = dma_buf_release,
157         .mmap           = dma_buf_mmap_internal,
158 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
159         .poll           = dma_buf_poll,
160 #endif
161 };
162
163 /*
164  * is_dma_buf_file - Check if struct file* is associated with dma_buf
165  */
166 static inline int is_dma_buf_file(struct file *file)
167 {
168         return file->f_op == &dma_buf_fops;
169 }
170
171 /**
172  * dma_buf_export - Creates a new dma_buf, and associates an anon file
173  * with this buffer, so it can be exported.
174  * Also connect the allocator specific data and ops to the buffer.
175  *
176  * @priv:       [in]    Attach private data of allocator to this buffer
177  * @ops:        [in]    Attach allocator-defined dma buf ops to the new buffer.
178  * @size:       [in]    Size of the buffer
179  * @flags:      [in]    mode flags for the file.
180  *
181  * Returns, on success, a newly created dma_buf object, which wraps the
182  * supplied private data and operations for dma_buf_ops. On either missing
183  * ops, or error in allocating struct dma_buf, will return negative error.
184  *
185  */
186 struct dma_buf *dma_buf_export(void *priv, const struct dma_buf_ops *ops,
187                                 size_t size, int flags)
188 {
189         struct dma_buf *dmabuf;
190         struct file *file;
191
192         if (WARN_ON(!priv || !ops
193                           || !ops->map_dma_buf
194                           || !ops->unmap_dma_buf
195                           || !ops->release
196                           || !ops->kmap_atomic
197                           || !ops->kmap
198                           || !ops->mmap)) {
199                 return ERR_PTR(-EINVAL);
200         }
201
202         dmabuf = kzalloc(sizeof(struct dma_buf), GFP_KERNEL);
203         if (dmabuf == NULL)
204                 return ERR_PTR(-ENOMEM);
205
206         dmabuf->priv = priv;
207         dmabuf->ops = ops;
208         dmabuf->size = size;
209
210         file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf, flags);
211
212         dmabuf->file = file;
213
214         mutex_init(&dmabuf->lock);
215         INIT_LIST_HEAD(&dmabuf->attachments);
216
217 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
218         init_waitqueue_head(&dmabuf->wq_exclusive);
219         init_waitqueue_head(&dmabuf->wq_shared);
220         kds_resource_init(&dmabuf->kds);
221         kds_callback_init(&dmabuf->kds_cb, 1, dma_buf_kds_cb_fn);
222 #endif
223
224         return dmabuf;
225 }
226 EXPORT_SYMBOL_GPL(dma_buf_export);
227
228
229 /**
230  * dma_buf_fd - returns a file descriptor for the given dma_buf
231  * @dmabuf:     [in]    pointer to dma_buf for which fd is required.
232  * @flags:      [in]    flags to give to fd
233  *
234  * On success, returns an associated 'fd'. Else, returns error.
235  */
236 int dma_buf_fd(struct dma_buf *dmabuf, int flags)
237 {
238         int error, fd;
239
240         if (!dmabuf || !dmabuf->file)
241                 return -EINVAL;
242
243         error = get_unused_fd_flags(flags);
244         if (error < 0)
245                 return error;
246         fd = error;
247
248         fd_install(fd, dmabuf->file);
249
250         return fd;
251 }
252 EXPORT_SYMBOL_GPL(dma_buf_fd);
253
254 /**
255  * dma_buf_get - returns the dma_buf structure related to an fd
256  * @fd: [in]    fd associated with the dma_buf to be returned
257  *
258  * On success, returns the dma_buf structure associated with an fd; uses
259  * file's refcounting done by fget to increase refcount. returns ERR_PTR
260  * otherwise.
261  */
262 struct dma_buf *dma_buf_get(int fd)
263 {
264         struct file *file;
265
266         file = fget(fd);
267
268         if (!file)
269                 return ERR_PTR(-EBADF);
270
271         if (!is_dma_buf_file(file)) {
272                 fput(file);
273                 return ERR_PTR(-EINVAL);
274         }
275
276         return file->private_data;
277 }
278 EXPORT_SYMBOL_GPL(dma_buf_get);
279
280 /**
281  * dma_buf_put - decreases refcount of the buffer
282  * @dmabuf:     [in]    buffer to reduce refcount of
283  *
284  * Uses file's refcounting done implicitly by fput()
285  */
286 void dma_buf_put(struct dma_buf *dmabuf)
287 {
288         if (WARN_ON(!dmabuf || !dmabuf->file))
289                 return;
290
291         fput(dmabuf->file);
292 }
293 EXPORT_SYMBOL_GPL(dma_buf_put);
294
295 /**
296  * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
297  * calls attach() of dma_buf_ops to allow device-specific attach functionality
298  * @dmabuf:     [in]    buffer to attach device to.
299  * @dev:        [in]    device to be attached.
300  *
301  * Returns struct dma_buf_attachment * for this attachment; may return negative
302  * error codes.
303  *
304  */
305 struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
306                                           struct device *dev)
307 {
308         struct dma_buf_attachment *attach;
309         int ret;
310
311         if (WARN_ON(!dmabuf || !dev))
312                 return ERR_PTR(-EINVAL);
313
314         attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL);
315         if (attach == NULL)
316                 return ERR_PTR(-ENOMEM);
317
318         attach->dev = dev;
319         attach->dmabuf = dmabuf;
320
321         mutex_lock(&dmabuf->lock);
322
323         if (dmabuf->ops->attach) {
324                 ret = dmabuf->ops->attach(dmabuf, dev, attach);
325                 if (ret)
326                         goto err_attach;
327         }
328         list_add(&attach->node, &dmabuf->attachments);
329
330         mutex_unlock(&dmabuf->lock);
331         return attach;
332
333 err_attach:
334         kfree(attach);
335         mutex_unlock(&dmabuf->lock);
336         return ERR_PTR(ret);
337 }
338 EXPORT_SYMBOL_GPL(dma_buf_attach);
339
340 /**
341  * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
342  * optionally calls detach() of dma_buf_ops for device-specific detach
343  * @dmabuf:     [in]    buffer to detach from.
344  * @attach:     [in]    attachment to be detached; is free'd after this call.
345  *
346  */
347 void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
348 {
349         if (WARN_ON(!dmabuf || !attach))
350                 return;
351
352         mutex_lock(&dmabuf->lock);
353         list_del(&attach->node);
354         if (dmabuf->ops->detach)
355                 dmabuf->ops->detach(dmabuf, attach);
356
357         mutex_unlock(&dmabuf->lock);
358         kfree(attach);
359 }
360 EXPORT_SYMBOL_GPL(dma_buf_detach);
361
362 /**
363  * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
364  * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
365  * dma_buf_ops.
366  * @attach:     [in]    attachment whose scatterlist is to be returned
367  * @direction:  [in]    direction of DMA transfer
368  *
369  * Returns sg_table containing the scatterlist to be returned; may return NULL
370  * or ERR_PTR.
371  *
372  */
373 struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
374                                         enum dma_data_direction direction)
375 {
376         struct sg_table *sg_table = ERR_PTR(-EINVAL);
377
378         might_sleep();
379
380         if (WARN_ON(!attach || !attach->dmabuf))
381                 return ERR_PTR(-EINVAL);
382
383         sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
384
385         return sg_table;
386 }
387 EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
388
389 /**
390  * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
391  * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
392  * dma_buf_ops.
393  * @attach:     [in]    attachment to unmap buffer from
394  * @sg_table:   [in]    scatterlist info of the buffer to unmap
395  * @direction:  [in]    direction of DMA transfer
396  *
397  */
398 void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
399                                 struct sg_table *sg_table,
400                                 enum dma_data_direction direction)
401 {
402         if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
403                 return;
404
405         attach->dmabuf->ops->unmap_dma_buf(attach, sg_table,
406                                                 direction);
407 }
408 EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
409
410
411 /**
412  * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
413  * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
414  * preparations. Coherency is only guaranteed in the specified range for the
415  * specified access direction.
416  * @dma_buf:    [in]    buffer to prepare cpu access for.
417  * @start:      [in]    start of range for cpu access.
418  * @len:        [in]    length of range for cpu access.
419  * @direction:  [in]    length of range for cpu access.
420  *
421  * Can return negative error values, returns 0 on success.
422  */
423 int dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
424                              enum dma_data_direction direction)
425 {
426         int ret = 0;
427
428         if (WARN_ON(!dmabuf))
429                 return -EINVAL;
430
431         if (dmabuf->ops->begin_cpu_access)
432                 ret = dmabuf->ops->begin_cpu_access(dmabuf, start, len, direction);
433
434         return ret;
435 }
436 EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access);
437
438 /**
439  * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
440  * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
441  * actions. Coherency is only guaranteed in the specified range for the
442  * specified access direction.
443  * @dma_buf:    [in]    buffer to complete cpu access for.
444  * @start:      [in]    start of range for cpu access.
445  * @len:        [in]    length of range for cpu access.
446  * @direction:  [in]    length of range for cpu access.
447  *
448  * This call must always succeed.
449  */
450 void dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
451                             enum dma_data_direction direction)
452 {
453         WARN_ON(!dmabuf);
454
455         if (dmabuf->ops->end_cpu_access)
456                 dmabuf->ops->end_cpu_access(dmabuf, start, len, direction);
457 }
458 EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access);
459
460 /**
461  * dma_buf_kmap_atomic - Map a page of the buffer object into kernel address
462  * space. The same restrictions as for kmap_atomic and friends apply.
463  * @dma_buf:    [in]    buffer to map page from.
464  * @page_num:   [in]    page in PAGE_SIZE units to map.
465  *
466  * This call must always succeed, any necessary preparations that might fail
467  * need to be done in begin_cpu_access.
468  */
469 void *dma_buf_kmap_atomic(struct dma_buf *dmabuf, unsigned long page_num)
470 {
471         WARN_ON(!dmabuf);
472
473         return dmabuf->ops->kmap_atomic(dmabuf, page_num);
474 }
475 EXPORT_SYMBOL_GPL(dma_buf_kmap_atomic);
476
477 /**
478  * dma_buf_kunmap_atomic - Unmap a page obtained by dma_buf_kmap_atomic.
479  * @dma_buf:    [in]    buffer to unmap page from.
480  * @page_num:   [in]    page in PAGE_SIZE units to unmap.
481  * @vaddr:      [in]    kernel space pointer obtained from dma_buf_kmap_atomic.
482  *
483  * This call must always succeed.
484  */
485 void dma_buf_kunmap_atomic(struct dma_buf *dmabuf, unsigned long page_num,
486                            void *vaddr)
487 {
488         WARN_ON(!dmabuf);
489
490         if (dmabuf->ops->kunmap_atomic)
491                 dmabuf->ops->kunmap_atomic(dmabuf, page_num, vaddr);
492 }
493 EXPORT_SYMBOL_GPL(dma_buf_kunmap_atomic);
494
495 /**
496  * dma_buf_kmap - Map a page of the buffer object into kernel address space. The
497  * same restrictions as for kmap and friends apply.
498  * @dma_buf:    [in]    buffer to map page from.
499  * @page_num:   [in]    page in PAGE_SIZE units to map.
500  *
501  * This call must always succeed, any necessary preparations that might fail
502  * need to be done in begin_cpu_access.
503  */
504 void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num)
505 {
506         WARN_ON(!dmabuf);
507
508         return dmabuf->ops->kmap(dmabuf, page_num);
509 }
510 EXPORT_SYMBOL_GPL(dma_buf_kmap);
511
512 /**
513  * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap.
514  * @dma_buf:    [in]    buffer to unmap page from.
515  * @page_num:   [in]    page in PAGE_SIZE units to unmap.
516  * @vaddr:      [in]    kernel space pointer obtained from dma_buf_kmap.
517  *
518  * This call must always succeed.
519  */
520 void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num,
521                     void *vaddr)
522 {
523         WARN_ON(!dmabuf);
524
525         if (dmabuf->ops->kunmap)
526                 dmabuf->ops->kunmap(dmabuf, page_num, vaddr);
527 }
528 EXPORT_SYMBOL_GPL(dma_buf_kunmap);
529
530
531 /**
532  * dma_buf_mmap - Setup up a userspace mmap with the given vma
533  * @dmabuf:     [in]    buffer that should back the vma
534  * @vma:        [in]    vma for the mmap
535  * @pgoff:      [in]    offset in pages where this mmap should start within the
536  *                      dma-buf buffer.
537  *
538  * This function adjusts the passed in vma so that it points at the file of the
539  * dma_buf operation. It alsog adjusts the starting pgoff and does bounds
540  * checking on the size of the vma. Then it calls the exporters mmap function to
541  * set up the mapping.
542  *
543  * Can return negative error values, returns 0 on success.
544  */
545 int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
546                  unsigned long pgoff)
547 {
548         struct file *oldfile;
549         int ret;
550
551         if (WARN_ON(!dmabuf || !vma))
552                 return -EINVAL;
553
554         /* check for offset overflow */
555         if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) < pgoff)
556                 return -EOVERFLOW;
557
558         /* check for overflowing the buffer's size */
559         if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
560             dmabuf->size >> PAGE_SHIFT)
561                 return -EINVAL;
562
563         /* readjust the vma */
564         get_file(dmabuf->file);
565         oldfile = vma->vm_file;
566         vma->vm_file = dmabuf->file;
567         vma->vm_pgoff = pgoff;
568
569         ret = dmabuf->ops->mmap(dmabuf, vma);
570         if (ret) {
571                 /* restore old parameters on failure */
572                 vma->vm_file = oldfile;
573                 fput(dmabuf->file);
574         } else {
575                 if (oldfile)
576                         fput(oldfile);
577         }
578         return ret;
579 }
580 EXPORT_SYMBOL_GPL(dma_buf_mmap);
581
582 /**
583  * dma_buf_vmap - Create virtual mapping for the buffer object into kernel
584  * address space. Same restrictions as for vmap and friends apply.
585  * @dmabuf:     [in]    buffer to vmap
586  *
587  * This call may fail due to lack of virtual mapping address space.
588  * These calls are optional in drivers. The intended use for them
589  * is for mapping objects linear in kernel space for high use objects.
590  * Please attempt to use kmap/kunmap before thinking about these interfaces.
591  */
592 void *dma_buf_vmap(struct dma_buf *dmabuf)
593 {
594         if (WARN_ON(!dmabuf))
595                 return NULL;
596
597         if (dmabuf->ops->vmap)
598                 return dmabuf->ops->vmap(dmabuf);
599         return NULL;
600 }
601 EXPORT_SYMBOL_GPL(dma_buf_vmap);
602
603 /**
604  * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
605  * @dmabuf:     [in]    buffer to vunmap
606  */
607 void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
608 {
609         if (WARN_ON(!dmabuf))
610                 return;
611
612         if (dmabuf->ops->vunmap)
613                 dmabuf->ops->vunmap(dmabuf, vaddr);
614 }
615 EXPORT_SYMBOL_GPL(dma_buf_vunmap);