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/scatterlist.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/dma-mapping.h>
20 #include <media/videobuf2-v4l2.h>
21 #include <media/videobuf2-dma-contig.h>
22 #include <media/videobuf2-memops.h>
30 struct dma_attrs attrs;
31 enum dma_data_direction dma_dir;
32 struct sg_table *dma_sgt;
33 struct frame_vector *vec;
36 struct vb2_vmarea_handler handler;
38 struct sg_table *sgt_base;
41 struct dma_buf_attachment *db_attach;
44 /*********************************************/
45 /* scatterlist table functions */
46 /*********************************************/
48 static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
50 struct scatterlist *s;
51 dma_addr_t expected = sg_dma_address(sgt->sgl);
53 unsigned long size = 0;
55 for_each_sg(sgt->sgl, s, sgt->nents, i) {
56 if (sg_dma_address(s) != expected)
58 expected = sg_dma_address(s) + sg_dma_len(s);
59 size += sg_dma_len(s);
64 /*********************************************/
65 /* callbacks for all buffers */
66 /*********************************************/
68 static void *vb2_dc_cookie(void *buf_priv)
70 struct vb2_dc_buf *buf = buf_priv;
72 return &buf->dma_addr;
75 static void *vb2_dc_vaddr(void *buf_priv)
77 struct vb2_dc_buf *buf = buf_priv;
79 if (!buf->vaddr && buf->db_attach)
80 buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
85 static unsigned int vb2_dc_num_users(void *buf_priv)
87 struct vb2_dc_buf *buf = buf_priv;
89 return atomic_read(&buf->refcount);
92 static void vb2_dc_prepare(void *buf_priv)
94 struct vb2_dc_buf *buf = buf_priv;
95 struct sg_table *sgt = buf->dma_sgt;
97 /* DMABUF exporter will flush the cache for us */
98 if (!sgt || buf->db_attach)
101 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->orig_nents,
105 static void vb2_dc_finish(void *buf_priv)
107 struct vb2_dc_buf *buf = buf_priv;
108 struct sg_table *sgt = buf->dma_sgt;
110 /* DMABUF exporter will flush the cache for us */
111 if (!sgt || buf->db_attach)
114 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
117 /*********************************************/
118 /* callbacks for MMAP buffers */
119 /*********************************************/
121 static void vb2_dc_put(void *buf_priv)
123 struct vb2_dc_buf *buf = buf_priv;
125 if (!atomic_dec_and_test(&buf->refcount))
129 sg_free_table(buf->sgt_base);
130 kfree(buf->sgt_base);
132 dma_free_attrs(buf->dev, buf->size, buf->cookie, buf->dma_addr,
134 put_device(buf->dev);
138 static void *vb2_dc_alloc(struct device *dev, const struct dma_attrs *attrs,
139 unsigned long size, enum dma_data_direction dma_dir,
142 struct vb2_dc_buf *buf;
144 buf = kzalloc(sizeof *buf, GFP_KERNEL);
146 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 %ld failed\n", size);
155 return ERR_PTR(-ENOMEM);
158 if (!dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, &buf->attrs))
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 atomic_inc(&buf->refcount);
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");
186 * dma_mmap_* uses vm_pgoff as in-buffer offset, but we want to
191 ret = dma_mmap_attrs(buf->dev, vma, buf->cookie,
192 buf->dma_addr, buf->size, &buf->attrs);
195 pr_err("Remapping memory failed, error: %d\n", ret);
199 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
200 vma->vm_private_data = &buf->handler;
201 vma->vm_ops = &vb2_common_vm_ops;
203 vma->vm_ops->open(vma);
205 pr_debug("%s: mapped dma addr 0x%08lx at 0x%08lx, size %ld\n",
206 __func__, (unsigned long)buf->dma_addr, vma->vm_start,
212 /*********************************************/
213 /* DMABUF ops for exporters */
214 /*********************************************/
216 struct vb2_dc_attachment {
218 enum dma_data_direction dma_dir;
221 static int vb2_dc_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
222 struct dma_buf_attachment *dbuf_attach)
224 struct vb2_dc_attachment *attach;
226 struct scatterlist *rd, *wr;
227 struct sg_table *sgt;
228 struct vb2_dc_buf *buf = dbuf->priv;
231 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
236 /* Copy the buf->base_sgt scatter list to the attachment, as we can't
237 * map the same scatter list to multiple attachments at the same time.
239 ret = sg_alloc_table(sgt, buf->sgt_base->orig_nents, GFP_KERNEL);
245 rd = buf->sgt_base->sgl;
247 for (i = 0; i < sgt->orig_nents; ++i) {
248 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
253 attach->dma_dir = DMA_NONE;
254 dbuf_attach->priv = attach;
259 static void vb2_dc_dmabuf_ops_detach(struct dma_buf *dbuf,
260 struct dma_buf_attachment *db_attach)
262 struct vb2_dc_attachment *attach = db_attach->priv;
263 struct sg_table *sgt;
270 /* release the scatterlist cache */
271 if (attach->dma_dir != DMA_NONE)
272 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
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_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
300 attach->dma_dir = DMA_NONE;
303 /* mapping to the client with new direction */
304 sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
307 pr_err("failed to map scatterlist\n");
309 return ERR_PTR(-EIO);
312 attach->dma_dir = dma_dir;
319 static void vb2_dc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
320 struct sg_table *sgt, enum dma_data_direction dma_dir)
322 /* nothing to be done here */
325 static void vb2_dc_dmabuf_ops_release(struct dma_buf *dbuf)
327 /* drop reference obtained in vb2_dc_get_dmabuf */
328 vb2_dc_put(dbuf->priv);
331 static void *vb2_dc_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
333 struct vb2_dc_buf *buf = dbuf->priv;
335 return buf->vaddr ? buf->vaddr + pgnum * PAGE_SIZE : NULL;
338 static void *vb2_dc_dmabuf_ops_vmap(struct dma_buf *dbuf)
340 struct vb2_dc_buf *buf = dbuf->priv;
345 static int vb2_dc_dmabuf_ops_mmap(struct dma_buf *dbuf,
346 struct vm_area_struct *vma)
348 return vb2_dc_mmap(dbuf->priv, vma);
351 static struct dma_buf_ops vb2_dc_dmabuf_ops = {
352 .attach = vb2_dc_dmabuf_ops_attach,
353 .detach = vb2_dc_dmabuf_ops_detach,
354 .map_dma_buf = vb2_dc_dmabuf_ops_map,
355 .unmap_dma_buf = vb2_dc_dmabuf_ops_unmap,
356 .kmap = vb2_dc_dmabuf_ops_kmap,
357 .kmap_atomic = vb2_dc_dmabuf_ops_kmap,
358 .vmap = vb2_dc_dmabuf_ops_vmap,
359 .mmap = vb2_dc_dmabuf_ops_mmap,
360 .release = vb2_dc_dmabuf_ops_release,
363 static struct sg_table *vb2_dc_get_base_sgt(struct vb2_dc_buf *buf)
366 struct sg_table *sgt;
368 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
370 dev_err(buf->dev, "failed to alloc sg table\n");
374 ret = dma_get_sgtable_attrs(buf->dev, sgt, buf->cookie, buf->dma_addr,
375 buf->size, &buf->attrs);
377 dev_err(buf->dev, "failed to get scatterlist from DMA API\n");
385 static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv, unsigned long flags)
387 struct vb2_dc_buf *buf = buf_priv;
388 struct dma_buf *dbuf;
389 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
391 exp_info.ops = &vb2_dc_dmabuf_ops;
392 exp_info.size = buf->size;
393 exp_info.flags = flags;
397 buf->sgt_base = vb2_dc_get_base_sgt(buf);
399 if (WARN_ON(!buf->sgt_base))
402 dbuf = dma_buf_export(&exp_info);
406 /* dmabuf keeps reference to vb2 buffer */
407 atomic_inc(&buf->refcount);
412 /*********************************************/
413 /* callbacks for USERPTR buffers */
414 /*********************************************/
416 static void vb2_dc_put_userptr(void *buf_priv)
418 struct vb2_dc_buf *buf = buf_priv;
419 struct sg_table *sgt = buf->dma_sgt;
424 DEFINE_DMA_ATTRS(attrs);
426 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
428 * No need to sync to CPU, it's already synced to the CPU
429 * since the finish() memop will have been called before this.
431 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
432 buf->dma_dir, &attrs);
433 pages = frame_vector_pages(buf->vec);
434 /* sgt should exist only if vector contains pages... */
435 BUG_ON(IS_ERR(pages));
436 for (i = 0; i < frame_vector_count(buf->vec); i++)
437 set_page_dirty_lock(pages[i]);
441 vb2_destroy_framevec(buf->vec);
446 * For some kind of reserved memory there might be no struct page available,
447 * so all that can be done to support such 'pages' is to try to convert
448 * pfn to dma address or at the last resort just assume that
449 * dma address == physical address (like it has been assumed in earlier version
450 * of videobuf2-dma-contig
453 #ifdef __arch_pfn_to_dma
454 static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
456 return (dma_addr_t)__arch_pfn_to_dma(dev, pfn);
458 #elif defined(__pfn_to_bus)
459 static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
461 return (dma_addr_t)__pfn_to_bus(pfn);
463 #elif defined(__pfn_to_phys)
464 static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
466 return (dma_addr_t)__pfn_to_phys(pfn);
469 static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
471 /* really, we cannot do anything better at this point */
472 return (dma_addr_t)(pfn) << PAGE_SHIFT;
476 static void *vb2_dc_get_userptr(struct device *dev, unsigned long vaddr,
477 unsigned long size, enum dma_data_direction dma_dir)
479 struct vb2_dc_buf *buf;
480 struct frame_vector *vec;
481 unsigned long offset;
484 struct sg_table *sgt;
485 unsigned long contig_size;
486 unsigned long dma_align = dma_get_cache_alignment();
487 DEFINE_DMA_ATTRS(attrs);
489 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
491 /* Only cache aligned DMA transfers are reliable */
492 if (!IS_ALIGNED(vaddr | size, dma_align)) {
493 pr_debug("user data must be aligned to %lu bytes\n", dma_align);
494 return ERR_PTR(-EINVAL);
498 pr_debug("size is zero\n");
499 return ERR_PTR(-EINVAL);
502 buf = kzalloc(sizeof *buf, GFP_KERNEL);
504 return ERR_PTR(-ENOMEM);
507 buf->dma_dir = dma_dir;
509 offset = vaddr & ~PAGE_MASK;
510 vec = vb2_create_framevec(vaddr, size, dma_dir == DMA_FROM_DEVICE);
516 n_pages = frame_vector_count(vec);
517 ret = frame_vector_to_pages(vec);
519 unsigned long *nums = frame_vector_pfns(vec);
522 * Failed to convert to pages... Check the memory is physically
523 * contiguous and use direct mapping
525 for (i = 1; i < n_pages; i++)
526 if (nums[i-1] + 1 != nums[i])
528 buf->dma_addr = vb2_dc_pfn_to_dma(buf->dev, nums[0]);
532 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
534 pr_err("failed to allocate sg table\n");
539 ret = sg_alloc_table_from_pages(sgt, frame_vector_pages(vec), n_pages,
540 offset, size, GFP_KERNEL);
542 pr_err("failed to initialize sg table\n");
547 * No need to sync to the device, this will happen later when the
548 * prepare() memop is called.
550 sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
551 buf->dma_dir, &attrs);
552 if (sgt->nents <= 0) {
553 pr_err("failed to map scatterlist\n");
558 contig_size = vb2_dc_get_contiguous_size(sgt);
559 if (contig_size < size) {
560 pr_err("contiguous mapping is too small %lu/%lu\n",
566 buf->dma_addr = sg_dma_address(sgt->sgl);
574 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
575 buf->dma_dir, &attrs);
584 vb2_destroy_framevec(vec);
592 /*********************************************/
593 /* callbacks for DMABUF buffers */
594 /*********************************************/
596 static int vb2_dc_map_dmabuf(void *mem_priv)
598 struct vb2_dc_buf *buf = mem_priv;
599 struct sg_table *sgt;
600 unsigned long contig_size;
602 if (WARN_ON(!buf->db_attach)) {
603 pr_err("trying to pin a non attached buffer\n");
607 if (WARN_ON(buf->dma_sgt)) {
608 pr_err("dmabuf buffer is already pinned\n");
612 /* get the associated scatterlist for this buffer */
613 sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
615 pr_err("Error getting dmabuf scatterlist\n");
619 /* checking if dmabuf is big enough to store contiguous chunk */
620 contig_size = vb2_dc_get_contiguous_size(sgt);
621 if (contig_size < buf->size) {
622 pr_err("contiguous chunk is too small %lu/%lu b\n",
623 contig_size, buf->size);
624 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
628 buf->dma_addr = sg_dma_address(sgt->sgl);
635 static void vb2_dc_unmap_dmabuf(void *mem_priv)
637 struct vb2_dc_buf *buf = mem_priv;
638 struct sg_table *sgt = buf->dma_sgt;
640 if (WARN_ON(!buf->db_attach)) {
641 pr_err("trying to unpin a not attached buffer\n");
646 pr_err("dmabuf buffer is already unpinned\n");
651 dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
654 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
660 static void vb2_dc_detach_dmabuf(void *mem_priv)
662 struct vb2_dc_buf *buf = mem_priv;
664 /* if vb2 works correctly you should never detach mapped buffer */
665 if (WARN_ON(buf->dma_addr))
666 vb2_dc_unmap_dmabuf(buf);
668 /* detach this attachment */
669 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
673 static void *vb2_dc_attach_dmabuf(struct device *dev, struct dma_buf *dbuf,
674 unsigned long size, enum dma_data_direction dma_dir)
676 struct vb2_dc_buf *buf;
677 struct dma_buf_attachment *dba;
679 if (dbuf->size < size)
680 return ERR_PTR(-EFAULT);
682 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
684 return ERR_PTR(-ENOMEM);
687 /* create attachment for the dmabuf with the user device */
688 dba = dma_buf_attach(dbuf, buf->dev);
690 pr_err("failed to attach dmabuf\n");
695 buf->dma_dir = dma_dir;
697 buf->db_attach = dba;
702 /*********************************************/
703 /* DMA CONTIG exported functions */
704 /*********************************************/
706 const struct vb2_mem_ops vb2_dma_contig_memops = {
707 .alloc = vb2_dc_alloc,
709 .get_dmabuf = vb2_dc_get_dmabuf,
710 .cookie = vb2_dc_cookie,
711 .vaddr = vb2_dc_vaddr,
713 .get_userptr = vb2_dc_get_userptr,
714 .put_userptr = vb2_dc_put_userptr,
715 .prepare = vb2_dc_prepare,
716 .finish = vb2_dc_finish,
717 .map_dmabuf = vb2_dc_map_dmabuf,
718 .unmap_dmabuf = vb2_dc_unmap_dmabuf,
719 .attach_dmabuf = vb2_dc_attach_dmabuf,
720 .detach_dmabuf = vb2_dc_detach_dmabuf,
721 .num_users = vb2_dc_num_users,
723 EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
726 * vb2_dma_contig_set_max_seg_size() - configure DMA max segment size
727 * @dev: device for configuring DMA parameters
728 * @size: size of DMA max segment size to set
730 * To allow mapping the scatter-list into a single chunk in the DMA
731 * address space, the device is required to have the DMA max segment
732 * size parameter set to a value larger than the buffer size. Otherwise,
733 * the DMA-mapping subsystem will split the mapping into max segment
734 * size chunks. This function sets the DMA max segment size
735 * parameter to let DMA-mapping map a buffer as a single chunk in DMA
737 * This code assumes that the DMA-mapping subsystem will merge all
738 * scatterlist segments if this is really possible (for example when
739 * an IOMMU is available and enabled).
740 * Ideally, this parameter should be set by the generic bus code, but it
741 * is left with the default 64KiB value due to historical litmiations in
742 * other subsystems (like limited USB host drivers) and there no good
743 * place to set it to the proper value.
744 * This function should be called from the drivers, which are known to
745 * operate on platforms with IOMMU and provide access to shared buffers
746 * (either USERPTR or DMABUF). This should be done before initializing
749 int vb2_dma_contig_set_max_seg_size(struct device *dev, unsigned int size)
751 if (!dev->dma_parms) {
752 dev->dma_parms = kzalloc(sizeof(dev->dma_parms), GFP_KERNEL);
756 if (dma_get_max_seg_size(dev) < size)
757 return dma_set_max_seg_size(dev, size);
761 EXPORT_SYMBOL_GPL(vb2_dma_contig_set_max_seg_size);
764 * vb2_dma_contig_clear_max_seg_size() - release resources for DMA parameters
765 * @dev: device for configuring DMA parameters
767 * This function releases resources allocated to configure DMA parameters
768 * (see vb2_dma_contig_set_max_seg_size() function). It should be called from
769 * device drivers on driver remove.
771 void vb2_dma_contig_clear_max_seg_size(struct device *dev)
773 kfree(dev->dma_parms);
774 dev->dma_parms = NULL;
776 EXPORT_SYMBOL_GPL(vb2_dma_contig_clear_max_seg_size);
778 MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
779 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
780 MODULE_LICENSE("GPL");