2 * Framework for buffer objects that can be shared across devices/subsystems.
4 * Copyright(C) 2011 Linaro Limited. All rights reserved.
5 * Author: Sumit Semwal <sumit.semwal@ti.com>
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.
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.
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
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/>.
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>
35 static inline int is_dma_buf_file(struct file *);
37 static int dma_buf_release(struct inode *inode, struct file *file)
39 struct dma_buf *dmabuf;
41 if (!is_dma_buf_file(file))
44 dmabuf = file->private_data;
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);
55 static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
57 struct dma_buf *dmabuf;
59 if (!is_dma_buf_file(file))
62 dmabuf = file->private_data;
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)
69 return dmabuf->ops->mmap(dmabuf, vma);
72 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
73 static void dma_buf_kds_cb_fn(void *param1, void *param2)
75 struct kds_resource_set **rset_ptr = param1;
76 struct kds_resource_set *rset = *rset_ptr;
77 wait_queue_head_t *wait_queue = param2;
80 kds_resource_set_release(&rset);
84 static int dma_buf_kds_check(struct kds_resource *kds,
85 unsigned long exclusive, int *poll_ret)
87 /* Synchronous wait with 0 timeout - poll availability */
88 struct kds_resource_set *rset = kds_waitall(1, &exclusive, &kds, 0);
94 kds_resource_set_release(&rset);
95 *poll_ret = POLLIN | POLLRDNORM;
97 *poll_ret |= POLLOUT | POLLWRNORM;
104 static unsigned int dma_buf_poll(struct file *file,
105 struct poll_table_struct *wait)
107 struct dma_buf *dmabuf;
108 struct kds_resource *kds;
109 unsigned int ret = 0;
111 if (!is_dma_buf_file(file))
114 dmabuf = file->private_data;
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);
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);
131 if (events & POLLOUT) {
132 wq = &dmabuf->wq_exclusive;
135 wq = &dmabuf->wq_shared;
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,
143 if (IS_ERR_VALUE(ret)) {
147 /* Can't allow access until callback */
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,
164 * is_dma_buf_file - Check if struct file* is associated with dma_buf
166 static inline int is_dma_buf_file(struct file *file)
168 return file->f_op == &dma_buf_fops;
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.
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.
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.
186 struct dma_buf *dma_buf_export(void *priv, const struct dma_buf_ops *ops,
187 size_t size, int flags)
189 struct dma_buf *dmabuf;
192 if (WARN_ON(!priv || !ops
194 || !ops->unmap_dma_buf
199 return ERR_PTR(-EINVAL);
202 dmabuf = kzalloc(sizeof(struct dma_buf), GFP_KERNEL);
204 return ERR_PTR(-ENOMEM);
210 file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf, flags);
214 mutex_init(&dmabuf->lock);
215 INIT_LIST_HEAD(&dmabuf->attachments);
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);
226 EXPORT_SYMBOL_GPL(dma_buf_export);
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
234 * On success, returns an associated 'fd'. Else, returns error.
236 int dma_buf_fd(struct dma_buf *dmabuf, int flags)
240 if (!dmabuf || !dmabuf->file)
243 error = get_unused_fd_flags(flags);
248 fd_install(fd, dmabuf->file);
252 EXPORT_SYMBOL_GPL(dma_buf_fd);
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
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
262 struct dma_buf *dma_buf_get(int fd)
269 return ERR_PTR(-EBADF);
271 if (!is_dma_buf_file(file)) {
273 return ERR_PTR(-EINVAL);
276 return file->private_data;
278 EXPORT_SYMBOL_GPL(dma_buf_get);
281 * dma_buf_put - decreases refcount of the buffer
282 * @dmabuf: [in] buffer to reduce refcount of
284 * Uses file's refcounting done implicitly by fput()
286 void dma_buf_put(struct dma_buf *dmabuf)
288 if (WARN_ON(!dmabuf || !dmabuf->file))
293 EXPORT_SYMBOL_GPL(dma_buf_put);
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.
301 * Returns struct dma_buf_attachment * for this attachment; may return negative
305 struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
308 struct dma_buf_attachment *attach;
311 if (WARN_ON(!dmabuf || !dev))
312 return ERR_PTR(-EINVAL);
314 attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL);
316 return ERR_PTR(-ENOMEM);
319 attach->dmabuf = dmabuf;
321 mutex_lock(&dmabuf->lock);
323 if (dmabuf->ops->attach) {
324 ret = dmabuf->ops->attach(dmabuf, dev, attach);
328 list_add(&attach->node, &dmabuf->attachments);
330 mutex_unlock(&dmabuf->lock);
335 mutex_unlock(&dmabuf->lock);
338 EXPORT_SYMBOL_GPL(dma_buf_attach);
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.
347 void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
349 if (WARN_ON(!dmabuf || !attach))
352 mutex_lock(&dmabuf->lock);
353 list_del(&attach->node);
354 if (dmabuf->ops->detach)
355 dmabuf->ops->detach(dmabuf, attach);
357 mutex_unlock(&dmabuf->lock);
360 EXPORT_SYMBOL_GPL(dma_buf_detach);
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
366 * @attach: [in] attachment whose scatterlist is to be returned
367 * @direction: [in] direction of DMA transfer
369 * Returns sg_table containing the scatterlist to be returned; may return NULL
373 struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
374 enum dma_data_direction direction)
376 struct sg_table *sg_table = ERR_PTR(-EINVAL);
380 if (WARN_ON(!attach || !attach->dmabuf))
381 return ERR_PTR(-EINVAL);
383 sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
387 EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
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
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
398 void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
399 struct sg_table *sg_table,
400 enum dma_data_direction direction)
402 if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
405 attach->dmabuf->ops->unmap_dma_buf(attach, sg_table,
408 EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
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.
421 * Can return negative error values, returns 0 on success.
423 int dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
424 enum dma_data_direction direction)
428 if (WARN_ON(!dmabuf))
431 if (dmabuf->ops->begin_cpu_access)
432 ret = dmabuf->ops->begin_cpu_access(dmabuf, start, len, direction);
436 EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access);
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.
448 * This call must always succeed.
450 void dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
451 enum dma_data_direction direction)
455 if (dmabuf->ops->end_cpu_access)
456 dmabuf->ops->end_cpu_access(dmabuf, start, len, direction);
458 EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access);
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.
466 * This call must always succeed, any necessary preparations that might fail
467 * need to be done in begin_cpu_access.
469 void *dma_buf_kmap_atomic(struct dma_buf *dmabuf, unsigned long page_num)
473 return dmabuf->ops->kmap_atomic(dmabuf, page_num);
475 EXPORT_SYMBOL_GPL(dma_buf_kmap_atomic);
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.
483 * This call must always succeed.
485 void dma_buf_kunmap_atomic(struct dma_buf *dmabuf, unsigned long page_num,
490 if (dmabuf->ops->kunmap_atomic)
491 dmabuf->ops->kunmap_atomic(dmabuf, page_num, vaddr);
493 EXPORT_SYMBOL_GPL(dma_buf_kunmap_atomic);
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.
501 * This call must always succeed, any necessary preparations that might fail
502 * need to be done in begin_cpu_access.
504 void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num)
508 return dmabuf->ops->kmap(dmabuf, page_num);
510 EXPORT_SYMBOL_GPL(dma_buf_kmap);
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.
518 * This call must always succeed.
520 void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num,
525 if (dmabuf->ops->kunmap)
526 dmabuf->ops->kunmap(dmabuf, page_num, vaddr);
528 EXPORT_SYMBOL_GPL(dma_buf_kunmap);
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
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.
543 * Can return negative error values, returns 0 on success.
545 int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
548 struct file *oldfile;
551 if (WARN_ON(!dmabuf || !vma))
554 /* check for offset overflow */
555 if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) < pgoff)
558 /* check for overflowing the buffer's size */
559 if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
560 dmabuf->size >> PAGE_SHIFT)
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;
569 ret = dmabuf->ops->mmap(dmabuf, vma);
571 /* restore old parameters on failure */
572 vma->vm_file = oldfile;
580 EXPORT_SYMBOL_GPL(dma_buf_mmap);
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
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.
592 void *dma_buf_vmap(struct dma_buf *dmabuf)
594 if (WARN_ON(!dmabuf))
597 if (dmabuf->ops->vmap)
598 return dmabuf->ops->vmap(dmabuf);
601 EXPORT_SYMBOL_GPL(dma_buf_vmap);
604 * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
605 * @dmabuf: [in] buffer to vunmap
607 void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
609 if (WARN_ON(!dmabuf))
612 if (dmabuf->ops->vunmap)
613 dmabuf->ops->vunmap(dmabuf, vaddr);
615 EXPORT_SYMBOL_GPL(dma_buf_vunmap);