2 * videobuf2-dma-contig.c - DMA contig memory allocator for videobuf2
4 * Copyright (C) 2010 Samsung Electronics
6 * Author: Pawel Osciak <pawel@osciak.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation.
13 #include <linux/dma-buf.h>
14 #include <linux/module.h>
15 #include <linux/refcount.h>
16 #include <linux/scatterlist.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/dma-mapping.h>
21 #include <media/videobuf2-v4l2.h>
22 #include <media/videobuf2-dma-contig.h>
23 #include <media/videobuf2-memops.h>
32 enum dma_data_direction dma_dir;
33 struct sg_table *dma_sgt;
34 struct frame_vector *vec;
37 struct vb2_vmarea_handler handler;
39 struct sg_table *sgt_base;
42 struct dma_buf_attachment *db_attach;
45 /*********************************************/
46 /* scatterlist table functions */
47 /*********************************************/
49 static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
51 struct scatterlist *s;
52 dma_addr_t expected = sg_dma_address(sgt->sgl);
54 unsigned long size = 0;
56 for_each_sgtable_dma_sg(sgt, s, i) {
57 if (sg_dma_address(s) != expected)
59 expected += sg_dma_len(s);
60 size += sg_dma_len(s);
65 /*********************************************/
66 /* callbacks for all buffers */
67 /*********************************************/
69 static void *vb2_dc_cookie(void *buf_priv)
71 struct vb2_dc_buf *buf = buf_priv;
73 return &buf->dma_addr;
76 static void *vb2_dc_vaddr(void *buf_priv)
78 struct vb2_dc_buf *buf = buf_priv;
80 if (!buf->vaddr && buf->db_attach)
81 buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
86 static unsigned int vb2_dc_num_users(void *buf_priv)
88 struct vb2_dc_buf *buf = buf_priv;
90 return refcount_read(&buf->refcount);
93 static void vb2_dc_prepare(void *buf_priv)
95 struct vb2_dc_buf *buf = buf_priv;
96 struct sg_table *sgt = buf->dma_sgt;
101 dma_sync_sgtable_for_device(buf->dev, sgt, buf->dma_dir);
104 static void vb2_dc_finish(void *buf_priv)
106 struct vb2_dc_buf *buf = buf_priv;
107 struct sg_table *sgt = buf->dma_sgt;
112 dma_sync_sgtable_for_cpu(buf->dev, sgt, buf->dma_dir);
115 /*********************************************/
116 /* callbacks for MMAP buffers */
117 /*********************************************/
119 static void vb2_dc_put(void *buf_priv)
121 struct vb2_dc_buf *buf = buf_priv;
123 if (!refcount_dec_and_test(&buf->refcount))
127 sg_free_table(buf->sgt_base);
128 kfree(buf->sgt_base);
130 dma_free_attrs(buf->dev, buf->size, buf->cookie, buf->dma_addr,
132 put_device(buf->dev);
136 static void *vb2_dc_alloc(struct device *dev, unsigned long attrs,
137 unsigned long size, enum dma_data_direction dma_dir,
140 struct vb2_dc_buf *buf;
143 return ERR_PTR(-EINVAL);
145 buf = kzalloc(sizeof *buf, GFP_KERNEL);
147 return ERR_PTR(-ENOMEM);
150 buf->cookie = dma_alloc_attrs(dev, size, &buf->dma_addr,
151 GFP_KERNEL | gfp_flags, buf->attrs);
153 dev_err(dev, "dma_alloc_coherent of size %lu failed\n", size);
155 return ERR_PTR(-ENOMEM);
158 if ((buf->attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0)
159 buf->vaddr = buf->cookie;
161 /* Prevent the device from being released while the buffer is used */
162 buf->dev = get_device(dev);
164 buf->dma_dir = dma_dir;
166 buf->handler.refcount = &buf->refcount;
167 buf->handler.put = vb2_dc_put;
168 buf->handler.arg = buf;
170 refcount_set(&buf->refcount, 1);
175 static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
177 struct vb2_dc_buf *buf = buf_priv;
181 printk(KERN_ERR "No buffer to map\n");
185 ret = dma_mmap_attrs(buf->dev, vma, buf->cookie,
186 buf->dma_addr, buf->size, buf->attrs);
189 pr_err("Remapping memory failed, error: %d\n", ret);
193 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
194 vma->vm_private_data = &buf->handler;
195 vma->vm_ops = &vb2_common_vm_ops;
197 vma->vm_ops->open(vma);
199 pr_debug("%s: mapped dma addr 0x%08lx at 0x%08lx, size %lu\n",
200 __func__, (unsigned long)buf->dma_addr, vma->vm_start,
206 /*********************************************/
207 /* DMABUF ops for exporters */
208 /*********************************************/
210 struct vb2_dc_attachment {
212 enum dma_data_direction dma_dir;
215 static int vb2_dc_dmabuf_ops_attach(struct dma_buf *dbuf,
216 struct dma_buf_attachment *dbuf_attach)
218 struct vb2_dc_attachment *attach;
220 struct scatterlist *rd, *wr;
221 struct sg_table *sgt;
222 struct vb2_dc_buf *buf = dbuf->priv;
225 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
230 /* Copy the buf->base_sgt scatter list to the attachment, as we can't
231 * map the same scatter list to multiple attachments at the same time.
233 ret = sg_alloc_table(sgt, buf->sgt_base->orig_nents, GFP_KERNEL);
239 rd = buf->sgt_base->sgl;
241 for (i = 0; i < sgt->orig_nents; ++i) {
242 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
247 attach->dma_dir = DMA_NONE;
248 dbuf_attach->priv = attach;
253 static void vb2_dc_dmabuf_ops_detach(struct dma_buf *dbuf,
254 struct dma_buf_attachment *db_attach)
256 struct vb2_dc_attachment *attach = db_attach->priv;
257 struct sg_table *sgt;
264 /* release the scatterlist cache */
265 if (attach->dma_dir != DMA_NONE)
267 * Cache sync can be skipped here, as the vb2_dc memory is
268 * allocated from device coherent memory, which means the
269 * memory locations do not require any explicit cache
270 * maintenance prior or after being used by the device.
272 dma_unmap_sgtable(db_attach->dev, sgt, attach->dma_dir,
273 DMA_ATTR_SKIP_CPU_SYNC);
276 db_attach->priv = NULL;
279 static struct sg_table *vb2_dc_dmabuf_ops_map(
280 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
282 struct vb2_dc_attachment *attach = db_attach->priv;
283 /* stealing dmabuf mutex to serialize map/unmap operations */
284 struct mutex *lock = &db_attach->dmabuf->lock;
285 struct sg_table *sgt;
290 /* return previously mapped sg table */
291 if (attach->dma_dir == dma_dir) {
296 /* release any previous cache */
297 if (attach->dma_dir != DMA_NONE) {
298 dma_unmap_sgtable(db_attach->dev, sgt, attach->dma_dir,
299 DMA_ATTR_SKIP_CPU_SYNC);
300 attach->dma_dir = DMA_NONE;
304 * mapping to the client with new direction, no cache sync
305 * required see comment in vb2_dc_dmabuf_ops_detach()
307 if (dma_map_sgtable(db_attach->dev, sgt, dma_dir,
308 DMA_ATTR_SKIP_CPU_SYNC)) {
309 pr_err("failed to map scatterlist\n");
311 return ERR_PTR(-EIO);
314 attach->dma_dir = dma_dir;
321 static void vb2_dc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
322 struct sg_table *sgt, enum dma_data_direction dma_dir)
324 /* nothing to be done here */
327 static void vb2_dc_dmabuf_ops_release(struct dma_buf *dbuf)
329 /* drop reference obtained in vb2_dc_get_dmabuf */
330 vb2_dc_put(dbuf->priv);
334 vb2_dc_dmabuf_ops_begin_cpu_access(struct dma_buf *dbuf,
335 enum dma_data_direction direction)
341 vb2_dc_dmabuf_ops_end_cpu_access(struct dma_buf *dbuf,
342 enum dma_data_direction direction)
347 static void *vb2_dc_dmabuf_ops_vmap(struct dma_buf *dbuf)
349 struct vb2_dc_buf *buf = dbuf->priv;
354 static int vb2_dc_dmabuf_ops_mmap(struct dma_buf *dbuf,
355 struct vm_area_struct *vma)
357 return vb2_dc_mmap(dbuf->priv, vma);
360 static const struct dma_buf_ops vb2_dc_dmabuf_ops = {
361 .attach = vb2_dc_dmabuf_ops_attach,
362 .detach = vb2_dc_dmabuf_ops_detach,
363 .map_dma_buf = vb2_dc_dmabuf_ops_map,
364 .unmap_dma_buf = vb2_dc_dmabuf_ops_unmap,
365 .begin_cpu_access = vb2_dc_dmabuf_ops_begin_cpu_access,
366 .end_cpu_access = vb2_dc_dmabuf_ops_end_cpu_access,
367 .vmap = vb2_dc_dmabuf_ops_vmap,
368 .mmap = vb2_dc_dmabuf_ops_mmap,
369 .release = vb2_dc_dmabuf_ops_release,
372 static struct sg_table *vb2_dc_get_base_sgt(struct vb2_dc_buf *buf)
375 struct sg_table *sgt;
377 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
379 dev_err(buf->dev, "failed to alloc sg table\n");
383 ret = dma_get_sgtable_attrs(buf->dev, sgt, buf->cookie, buf->dma_addr,
384 buf->size, buf->attrs);
386 dev_err(buf->dev, "failed to get scatterlist from DMA API\n");
394 static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv, unsigned long flags)
396 struct vb2_dc_buf *buf = buf_priv;
397 struct dma_buf *dbuf;
398 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
400 exp_info.ops = &vb2_dc_dmabuf_ops;
401 exp_info.size = buf->size;
402 exp_info.flags = flags;
406 buf->sgt_base = vb2_dc_get_base_sgt(buf);
408 if (WARN_ON(!buf->sgt_base))
411 dbuf = dma_buf_export(&exp_info);
415 /* dmabuf keeps reference to vb2 buffer */
416 refcount_inc(&buf->refcount);
421 /*********************************************/
422 /* callbacks for USERPTR buffers */
423 /*********************************************/
425 static void vb2_dc_put_userptr(void *buf_priv)
427 struct vb2_dc_buf *buf = buf_priv;
428 struct sg_table *sgt = buf->dma_sgt;
434 * No need to sync to CPU, it's already synced to the CPU
435 * since the finish() memop will have been called before this.
437 dma_unmap_sgtable(buf->dev, sgt, buf->dma_dir,
438 DMA_ATTR_SKIP_CPU_SYNC);
439 pages = frame_vector_pages(buf->vec);
440 /* sgt should exist only if vector contains pages... */
441 BUG_ON(IS_ERR(pages));
442 if (buf->dma_dir == DMA_FROM_DEVICE ||
443 buf->dma_dir == DMA_BIDIRECTIONAL)
444 for (i = 0; i < frame_vector_count(buf->vec); i++)
445 set_page_dirty_lock(pages[i]);
449 dma_unmap_resource(buf->dev, buf->dma_addr, buf->size,
452 vb2_destroy_framevec(buf->vec);
456 static void *vb2_dc_get_userptr(struct device *dev, unsigned long vaddr,
457 unsigned long size, enum dma_data_direction dma_dir)
459 struct vb2_dc_buf *buf;
460 struct frame_vector *vec;
464 struct sg_table *sgt;
465 unsigned long contig_size;
466 unsigned long dma_align = dma_get_cache_alignment();
468 /* Only cache aligned DMA transfers are reliable */
469 if (!IS_ALIGNED(vaddr | size, dma_align)) {
470 pr_debug("user data must be aligned to %lu bytes\n", dma_align);
471 return ERR_PTR(-EINVAL);
475 pr_debug("size is zero\n");
476 return ERR_PTR(-EINVAL);
480 return ERR_PTR(-EINVAL);
482 buf = kzalloc(sizeof *buf, GFP_KERNEL);
484 return ERR_PTR(-ENOMEM);
487 buf->dma_dir = dma_dir;
489 offset = lower_32_bits(offset_in_page(vaddr));
490 vec = vb2_create_framevec(vaddr, size);
496 n_pages = frame_vector_count(vec);
497 ret = frame_vector_to_pages(vec);
499 unsigned long *nums = frame_vector_pfns(vec);
502 * Failed to convert to pages... Check the memory is physically
503 * contiguous and use direct mapping
505 for (i = 1; i < n_pages; i++)
506 if (nums[i-1] + 1 != nums[i])
508 buf->dma_addr = dma_map_resource(buf->dev,
509 __pfn_to_phys(nums[0]), size, buf->dma_dir, 0);
510 if (dma_mapping_error(buf->dev, buf->dma_addr)) {
517 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
519 pr_err("failed to allocate sg table\n");
524 ret = sg_alloc_table_from_pages(sgt, frame_vector_pages(vec), n_pages,
525 offset, size, GFP_KERNEL);
527 pr_err("failed to initialize sg table\n");
532 * No need to sync to the device, this will happen later when the
533 * prepare() memop is called.
535 if (dma_map_sgtable(buf->dev, sgt, buf->dma_dir,
536 DMA_ATTR_SKIP_CPU_SYNC)) {
537 pr_err("failed to map scatterlist\n");
542 contig_size = vb2_dc_get_contiguous_size(sgt);
543 if (contig_size < size) {
544 pr_err("contiguous mapping is too small %lu/%lu\n",
550 buf->dma_addr = sg_dma_address(sgt->sgl);
558 dma_unmap_sgtable(buf->dev, sgt, buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
567 vb2_destroy_framevec(vec);
575 /*********************************************/
576 /* callbacks for DMABUF buffers */
577 /*********************************************/
579 static int vb2_dc_map_dmabuf(void *mem_priv)
581 struct vb2_dc_buf *buf = mem_priv;
582 struct sg_table *sgt;
583 unsigned long contig_size;
585 if (WARN_ON(!buf->db_attach)) {
586 pr_err("trying to pin a non attached buffer\n");
590 if (WARN_ON(buf->dma_sgt)) {
591 pr_err("dmabuf buffer is already pinned\n");
595 /* get the associated scatterlist for this buffer */
596 sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
598 pr_err("Error getting dmabuf scatterlist\n");
602 /* checking if dmabuf is big enough to store contiguous chunk */
603 contig_size = vb2_dc_get_contiguous_size(sgt);
604 if (contig_size < buf->size) {
605 pr_err("contiguous chunk is too small %lu/%lu\n",
606 contig_size, buf->size);
607 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
611 buf->dma_addr = sg_dma_address(sgt->sgl);
618 static void vb2_dc_unmap_dmabuf(void *mem_priv)
620 struct vb2_dc_buf *buf = mem_priv;
621 struct sg_table *sgt = buf->dma_sgt;
623 if (WARN_ON(!buf->db_attach)) {
624 pr_err("trying to unpin a not attached buffer\n");
629 pr_err("dmabuf buffer is already unpinned\n");
634 dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
637 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
643 static void vb2_dc_detach_dmabuf(void *mem_priv)
645 struct vb2_dc_buf *buf = mem_priv;
647 /* if vb2 works correctly you should never detach mapped buffer */
648 if (WARN_ON(buf->dma_addr))
649 vb2_dc_unmap_dmabuf(buf);
651 /* detach this attachment */
652 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
656 static void *vb2_dc_attach_dmabuf(struct device *dev, struct dma_buf *dbuf,
657 unsigned long size, enum dma_data_direction dma_dir)
659 struct vb2_dc_buf *buf;
660 struct dma_buf_attachment *dba;
662 if (dbuf->size < size)
663 return ERR_PTR(-EFAULT);
666 return ERR_PTR(-EINVAL);
668 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
670 return ERR_PTR(-ENOMEM);
673 /* create attachment for the dmabuf with the user device */
674 dba = dma_buf_attach(dbuf, buf->dev);
676 pr_err("failed to attach dmabuf\n");
681 buf->dma_dir = dma_dir;
683 buf->db_attach = dba;
688 /*********************************************/
689 /* DMA CONTIG exported functions */
690 /*********************************************/
692 const struct vb2_mem_ops vb2_dma_contig_memops = {
693 .alloc = vb2_dc_alloc,
695 .get_dmabuf = vb2_dc_get_dmabuf,
696 .cookie = vb2_dc_cookie,
697 .vaddr = vb2_dc_vaddr,
699 .get_userptr = vb2_dc_get_userptr,
700 .put_userptr = vb2_dc_put_userptr,
701 .prepare = vb2_dc_prepare,
702 .finish = vb2_dc_finish,
703 .map_dmabuf = vb2_dc_map_dmabuf,
704 .unmap_dmabuf = vb2_dc_unmap_dmabuf,
705 .attach_dmabuf = vb2_dc_attach_dmabuf,
706 .detach_dmabuf = vb2_dc_detach_dmabuf,
707 .num_users = vb2_dc_num_users,
709 EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
712 * vb2_dma_contig_set_max_seg_size() - configure DMA max segment size
713 * @dev: device for configuring DMA parameters
714 * @size: size of DMA max segment size to set
716 * To allow mapping the scatter-list into a single chunk in the DMA
717 * address space, the device is required to have the DMA max segment
718 * size parameter set to a value larger than the buffer size. Otherwise,
719 * the DMA-mapping subsystem will split the mapping into max segment
720 * size chunks. This function sets the DMA max segment size
721 * parameter to let DMA-mapping map a buffer as a single chunk in DMA
723 * This code assumes that the DMA-mapping subsystem will merge all
724 * scatterlist segments if this is really possible (for example when
725 * an IOMMU is available and enabled).
726 * Ideally, this parameter should be set by the generic bus code, but it
727 * is left with the default 64KiB value due to historical litmiations in
728 * other subsystems (like limited USB host drivers) and there no good
729 * place to set it to the proper value.
730 * This function should be called from the drivers, which are known to
731 * operate on platforms with IOMMU and provide access to shared buffers
732 * (either USERPTR or DMABUF). This should be done before initializing
735 int vb2_dma_contig_set_max_seg_size(struct device *dev, unsigned int size)
737 if (!dev->dma_parms) {
738 dev_err(dev, "Failed to set max_seg_size: dma_parms is NULL\n");
741 if (dma_get_max_seg_size(dev) < size)
742 return dma_set_max_seg_size(dev, size);
746 EXPORT_SYMBOL_GPL(vb2_dma_contig_set_max_seg_size);
748 MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
749 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
750 MODULE_LICENSE("GPL");