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