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