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
;
79 struct dma_buf_map map
;
82 if (!buf
->vaddr
&& buf
->db_attach
) {
83 ret
= dma_buf_vmap(buf
->db_attach
->dmabuf
, &map
);
84 buf
->vaddr
= ret
? NULL
: map
.vaddr
;
90 static unsigned int vb2_dc_num_users(void *buf_priv
)
92 struct vb2_dc_buf
*buf
= buf_priv
;
94 return refcount_read(&buf
->refcount
);
97 static void vb2_dc_prepare(void *buf_priv
)
99 struct vb2_dc_buf
*buf
= buf_priv
;
100 struct sg_table
*sgt
= buf
->dma_sgt
;
105 dma_sync_sgtable_for_device(buf
->dev
, sgt
, buf
->dma_dir
);
108 static void vb2_dc_finish(void *buf_priv
)
110 struct vb2_dc_buf
*buf
= buf_priv
;
111 struct sg_table
*sgt
= buf
->dma_sgt
;
116 dma_sync_sgtable_for_cpu(buf
->dev
, sgt
, buf
->dma_dir
);
119 /*********************************************/
120 /* callbacks for MMAP buffers */
121 /*********************************************/
123 static void vb2_dc_put(void *buf_priv
)
125 struct vb2_dc_buf
*buf
= buf_priv
;
127 if (!refcount_dec_and_test(&buf
->refcount
))
131 sg_free_table(buf
->sgt_base
);
132 kfree(buf
->sgt_base
);
134 dma_free_attrs(buf
->dev
, buf
->size
, buf
->cookie
, buf
->dma_addr
,
136 put_device(buf
->dev
);
140 static void *vb2_dc_alloc(struct device
*dev
, unsigned long attrs
,
141 unsigned long size
, enum dma_data_direction dma_dir
,
144 struct vb2_dc_buf
*buf
;
147 return ERR_PTR(-EINVAL
);
149 buf
= kzalloc(sizeof *buf
, GFP_KERNEL
);
151 return ERR_PTR(-ENOMEM
);
154 buf
->cookie
= dma_alloc_attrs(dev
, size
, &buf
->dma_addr
,
155 GFP_KERNEL
| gfp_flags
, buf
->attrs
);
157 dev_err(dev
, "dma_alloc_coherent of size %ld failed\n", size
);
159 return ERR_PTR(-ENOMEM
);
162 if ((buf
->attrs
& DMA_ATTR_NO_KERNEL_MAPPING
) == 0)
163 buf
->vaddr
= buf
->cookie
;
165 /* Prevent the device from being released while the buffer is used */
166 buf
->dev
= get_device(dev
);
168 buf
->dma_dir
= dma_dir
;
170 buf
->handler
.refcount
= &buf
->refcount
;
171 buf
->handler
.put
= vb2_dc_put
;
172 buf
->handler
.arg
= buf
;
174 refcount_set(&buf
->refcount
, 1);
179 static int vb2_dc_mmap(void *buf_priv
, struct vm_area_struct
*vma
)
181 struct vb2_dc_buf
*buf
= buf_priv
;
185 printk(KERN_ERR
"No buffer to map\n");
189 ret
= dma_mmap_attrs(buf
->dev
, vma
, buf
->cookie
,
190 buf
->dma_addr
, buf
->size
, buf
->attrs
);
193 pr_err("Remapping memory failed, error: %d\n", ret
);
197 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
198 vma
->vm_private_data
= &buf
->handler
;
199 vma
->vm_ops
= &vb2_common_vm_ops
;
201 vma
->vm_ops
->open(vma
);
203 pr_debug("%s: mapped dma addr 0x%08lx at 0x%08lx, size %ld\n",
204 __func__
, (unsigned long)buf
->dma_addr
, vma
->vm_start
,
210 /*********************************************/
211 /* DMABUF ops for exporters */
212 /*********************************************/
214 struct vb2_dc_attachment
{
216 enum dma_data_direction dma_dir
;
219 static int vb2_dc_dmabuf_ops_attach(struct dma_buf
*dbuf
,
220 struct dma_buf_attachment
*dbuf_attach
)
222 struct vb2_dc_attachment
*attach
;
224 struct scatterlist
*rd
, *wr
;
225 struct sg_table
*sgt
;
226 struct vb2_dc_buf
*buf
= dbuf
->priv
;
229 attach
= kzalloc(sizeof(*attach
), GFP_KERNEL
);
234 /* Copy the buf->base_sgt scatter list to the attachment, as we can't
235 * map the same scatter list to multiple attachments at the same time.
237 ret
= sg_alloc_table(sgt
, buf
->sgt_base
->orig_nents
, GFP_KERNEL
);
243 rd
= buf
->sgt_base
->sgl
;
245 for (i
= 0; i
< sgt
->orig_nents
; ++i
) {
246 sg_set_page(wr
, sg_page(rd
), rd
->length
, rd
->offset
);
251 attach
->dma_dir
= DMA_NONE
;
252 dbuf_attach
->priv
= attach
;
257 static void vb2_dc_dmabuf_ops_detach(struct dma_buf
*dbuf
,
258 struct dma_buf_attachment
*db_attach
)
260 struct vb2_dc_attachment
*attach
= db_attach
->priv
;
261 struct sg_table
*sgt
;
268 /* release the scatterlist cache */
269 if (attach
->dma_dir
!= DMA_NONE
)
271 * Cache sync can be skipped here, as the vb2_dc memory is
272 * allocated from device coherent memory, which means the
273 * memory locations do not require any explicit cache
274 * maintenance prior or after being used by the device.
276 dma_unmap_sgtable(db_attach
->dev
, sgt
, attach
->dma_dir
,
277 DMA_ATTR_SKIP_CPU_SYNC
);
280 db_attach
->priv
= NULL
;
283 static struct sg_table
*vb2_dc_dmabuf_ops_map(
284 struct dma_buf_attachment
*db_attach
, enum dma_data_direction dma_dir
)
286 struct vb2_dc_attachment
*attach
= db_attach
->priv
;
287 /* stealing dmabuf mutex to serialize map/unmap operations */
288 struct mutex
*lock
= &db_attach
->dmabuf
->lock
;
289 struct sg_table
*sgt
;
294 /* return previously mapped sg table */
295 if (attach
->dma_dir
== dma_dir
) {
300 /* release any previous cache */
301 if (attach
->dma_dir
!= DMA_NONE
) {
302 dma_unmap_sgtable(db_attach
->dev
, sgt
, attach
->dma_dir
,
303 DMA_ATTR_SKIP_CPU_SYNC
);
304 attach
->dma_dir
= DMA_NONE
;
308 * mapping to the client with new direction, no cache sync
309 * required see comment in vb2_dc_dmabuf_ops_detach()
311 if (dma_map_sgtable(db_attach
->dev
, sgt
, dma_dir
,
312 DMA_ATTR_SKIP_CPU_SYNC
)) {
313 pr_err("failed to map scatterlist\n");
315 return ERR_PTR(-EIO
);
318 attach
->dma_dir
= dma_dir
;
325 static void vb2_dc_dmabuf_ops_unmap(struct dma_buf_attachment
*db_attach
,
326 struct sg_table
*sgt
, enum dma_data_direction dma_dir
)
328 /* nothing to be done here */
331 static void vb2_dc_dmabuf_ops_release(struct dma_buf
*dbuf
)
333 /* drop reference obtained in vb2_dc_get_dmabuf */
334 vb2_dc_put(dbuf
->priv
);
338 vb2_dc_dmabuf_ops_begin_cpu_access(struct dma_buf
*dbuf
,
339 enum dma_data_direction direction
)
345 vb2_dc_dmabuf_ops_end_cpu_access(struct dma_buf
*dbuf
,
346 enum dma_data_direction direction
)
351 static int vb2_dc_dmabuf_ops_vmap(struct dma_buf
*dbuf
, struct dma_buf_map
*map
)
353 struct vb2_dc_buf
*buf
= dbuf
->priv
;
355 dma_buf_map_set_vaddr(map
, buf
->vaddr
);
360 static int vb2_dc_dmabuf_ops_mmap(struct dma_buf
*dbuf
,
361 struct vm_area_struct
*vma
)
363 return vb2_dc_mmap(dbuf
->priv
, vma
);
366 static const struct dma_buf_ops vb2_dc_dmabuf_ops
= {
367 .attach
= vb2_dc_dmabuf_ops_attach
,
368 .detach
= vb2_dc_dmabuf_ops_detach
,
369 .map_dma_buf
= vb2_dc_dmabuf_ops_map
,
370 .unmap_dma_buf
= vb2_dc_dmabuf_ops_unmap
,
371 .begin_cpu_access
= vb2_dc_dmabuf_ops_begin_cpu_access
,
372 .end_cpu_access
= vb2_dc_dmabuf_ops_end_cpu_access
,
373 .vmap
= vb2_dc_dmabuf_ops_vmap
,
374 .mmap
= vb2_dc_dmabuf_ops_mmap
,
375 .release
= vb2_dc_dmabuf_ops_release
,
378 static struct sg_table
*vb2_dc_get_base_sgt(struct vb2_dc_buf
*buf
)
381 struct sg_table
*sgt
;
383 sgt
= kmalloc(sizeof(*sgt
), GFP_KERNEL
);
385 dev_err(buf
->dev
, "failed to alloc sg table\n");
389 ret
= dma_get_sgtable_attrs(buf
->dev
, sgt
, buf
->cookie
, buf
->dma_addr
,
390 buf
->size
, buf
->attrs
);
392 dev_err(buf
->dev
, "failed to get scatterlist from DMA API\n");
400 static struct dma_buf
*vb2_dc_get_dmabuf(void *buf_priv
, unsigned long flags
)
402 struct vb2_dc_buf
*buf
= buf_priv
;
403 struct dma_buf
*dbuf
;
404 DEFINE_DMA_BUF_EXPORT_INFO(exp_info
);
406 exp_info
.ops
= &vb2_dc_dmabuf_ops
;
407 exp_info
.size
= buf
->size
;
408 exp_info
.flags
= flags
;
412 buf
->sgt_base
= vb2_dc_get_base_sgt(buf
);
414 if (WARN_ON(!buf
->sgt_base
))
417 dbuf
= dma_buf_export(&exp_info
);
421 /* dmabuf keeps reference to vb2 buffer */
422 refcount_inc(&buf
->refcount
);
427 /*********************************************/
428 /* callbacks for USERPTR buffers */
429 /*********************************************/
431 static void vb2_dc_put_userptr(void *buf_priv
)
433 struct vb2_dc_buf
*buf
= buf_priv
;
434 struct sg_table
*sgt
= buf
->dma_sgt
;
440 * No need to sync to CPU, it's already synced to the CPU
441 * since the finish() memop will have been called before this.
443 dma_unmap_sgtable(buf
->dev
, sgt
, buf
->dma_dir
,
444 DMA_ATTR_SKIP_CPU_SYNC
);
445 pages
= frame_vector_pages(buf
->vec
);
446 /* sgt should exist only if vector contains pages... */
447 BUG_ON(IS_ERR(pages
));
448 if (buf
->dma_dir
== DMA_FROM_DEVICE
||
449 buf
->dma_dir
== DMA_BIDIRECTIONAL
)
450 for (i
= 0; i
< frame_vector_count(buf
->vec
); i
++)
451 set_page_dirty_lock(pages
[i
]);
455 dma_unmap_resource(buf
->dev
, buf
->dma_addr
, buf
->size
,
458 vb2_destroy_framevec(buf
->vec
);
462 static void *vb2_dc_get_userptr(struct device
*dev
, unsigned long vaddr
,
463 unsigned long size
, enum dma_data_direction dma_dir
)
465 struct vb2_dc_buf
*buf
;
466 struct frame_vector
*vec
;
470 struct sg_table
*sgt
;
471 unsigned long contig_size
;
472 unsigned long dma_align
= dma_get_cache_alignment();
474 /* Only cache aligned DMA transfers are reliable */
475 if (!IS_ALIGNED(vaddr
| size
, dma_align
)) {
476 pr_debug("user data must be aligned to %lu bytes\n", dma_align
);
477 return ERR_PTR(-EINVAL
);
481 pr_debug("size is zero\n");
482 return ERR_PTR(-EINVAL
);
486 return ERR_PTR(-EINVAL
);
488 buf
= kzalloc(sizeof *buf
, GFP_KERNEL
);
490 return ERR_PTR(-ENOMEM
);
493 buf
->dma_dir
= dma_dir
;
495 offset
= lower_32_bits(offset_in_page(vaddr
));
496 vec
= vb2_create_framevec(vaddr
, size
);
502 n_pages
= frame_vector_count(vec
);
503 ret
= frame_vector_to_pages(vec
);
505 unsigned long *nums
= frame_vector_pfns(vec
);
508 * Failed to convert to pages... Check the memory is physically
509 * contiguous and use direct mapping
511 for (i
= 1; i
< n_pages
; i
++)
512 if (nums
[i
-1] + 1 != nums
[i
])
514 buf
->dma_addr
= dma_map_resource(buf
->dev
,
515 __pfn_to_phys(nums
[0]), size
, buf
->dma_dir
, 0);
516 if (dma_mapping_error(buf
->dev
, buf
->dma_addr
)) {
523 sgt
= kzalloc(sizeof(*sgt
), GFP_KERNEL
);
525 pr_err("failed to allocate sg table\n");
530 ret
= sg_alloc_table_from_pages(sgt
, frame_vector_pages(vec
), n_pages
,
531 offset
, size
, GFP_KERNEL
);
533 pr_err("failed to initialize sg table\n");
538 * No need to sync to the device, this will happen later when the
539 * prepare() memop is called.
541 if (dma_map_sgtable(buf
->dev
, sgt
, buf
->dma_dir
,
542 DMA_ATTR_SKIP_CPU_SYNC
)) {
543 pr_err("failed to map scatterlist\n");
548 contig_size
= vb2_dc_get_contiguous_size(sgt
);
549 if (contig_size
< size
) {
550 pr_err("contiguous mapping is too small %lu/%lu\n",
556 buf
->dma_addr
= sg_dma_address(sgt
->sgl
);
564 dma_unmap_sgtable(buf
->dev
, sgt
, buf
->dma_dir
, DMA_ATTR_SKIP_CPU_SYNC
);
573 vb2_destroy_framevec(vec
);
581 /*********************************************/
582 /* callbacks for DMABUF buffers */
583 /*********************************************/
585 static int vb2_dc_map_dmabuf(void *mem_priv
)
587 struct vb2_dc_buf
*buf
= mem_priv
;
588 struct sg_table
*sgt
;
589 unsigned long contig_size
;
591 if (WARN_ON(!buf
->db_attach
)) {
592 pr_err("trying to pin a non attached buffer\n");
596 if (WARN_ON(buf
->dma_sgt
)) {
597 pr_err("dmabuf buffer is already pinned\n");
601 /* get the associated scatterlist for this buffer */
602 sgt
= dma_buf_map_attachment(buf
->db_attach
, buf
->dma_dir
);
604 pr_err("Error getting dmabuf scatterlist\n");
608 /* checking if dmabuf is big enough to store contiguous chunk */
609 contig_size
= vb2_dc_get_contiguous_size(sgt
);
610 if (contig_size
< buf
->size
) {
611 pr_err("contiguous chunk is too small %lu/%lu\n",
612 contig_size
, buf
->size
);
613 dma_buf_unmap_attachment(buf
->db_attach
, sgt
, buf
->dma_dir
);
617 buf
->dma_addr
= sg_dma_address(sgt
->sgl
);
624 static void vb2_dc_unmap_dmabuf(void *mem_priv
)
626 struct vb2_dc_buf
*buf
= mem_priv
;
627 struct sg_table
*sgt
= buf
->dma_sgt
;
628 struct dma_buf_map map
= DMA_BUF_MAP_INIT_VADDR(buf
->vaddr
);
630 if (WARN_ON(!buf
->db_attach
)) {
631 pr_err("trying to unpin a not attached buffer\n");
636 pr_err("dmabuf buffer is already unpinned\n");
641 dma_buf_vunmap(buf
->db_attach
->dmabuf
, &map
);
644 dma_buf_unmap_attachment(buf
->db_attach
, sgt
, buf
->dma_dir
);
650 static void vb2_dc_detach_dmabuf(void *mem_priv
)
652 struct vb2_dc_buf
*buf
= mem_priv
;
654 /* if vb2 works correctly you should never detach mapped buffer */
655 if (WARN_ON(buf
->dma_addr
))
656 vb2_dc_unmap_dmabuf(buf
);
658 /* detach this attachment */
659 dma_buf_detach(buf
->db_attach
->dmabuf
, buf
->db_attach
);
663 static void *vb2_dc_attach_dmabuf(struct device
*dev
, struct dma_buf
*dbuf
,
664 unsigned long size
, enum dma_data_direction dma_dir
)
666 struct vb2_dc_buf
*buf
;
667 struct dma_buf_attachment
*dba
;
669 if (dbuf
->size
< size
)
670 return ERR_PTR(-EFAULT
);
673 return ERR_PTR(-EINVAL
);
675 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
677 return ERR_PTR(-ENOMEM
);
680 /* create attachment for the dmabuf with the user device */
681 dba
= dma_buf_attach(dbuf
, buf
->dev
);
683 pr_err("failed to attach dmabuf\n");
688 buf
->dma_dir
= dma_dir
;
690 buf
->db_attach
= dba
;
695 /*********************************************/
696 /* DMA CONTIG exported functions */
697 /*********************************************/
699 const struct vb2_mem_ops vb2_dma_contig_memops
= {
700 .alloc
= vb2_dc_alloc
,
702 .get_dmabuf
= vb2_dc_get_dmabuf
,
703 .cookie
= vb2_dc_cookie
,
704 .vaddr
= vb2_dc_vaddr
,
706 .get_userptr
= vb2_dc_get_userptr
,
707 .put_userptr
= vb2_dc_put_userptr
,
708 .prepare
= vb2_dc_prepare
,
709 .finish
= vb2_dc_finish
,
710 .map_dmabuf
= vb2_dc_map_dmabuf
,
711 .unmap_dmabuf
= vb2_dc_unmap_dmabuf
,
712 .attach_dmabuf
= vb2_dc_attach_dmabuf
,
713 .detach_dmabuf
= vb2_dc_detach_dmabuf
,
714 .num_users
= vb2_dc_num_users
,
716 EXPORT_SYMBOL_GPL(vb2_dma_contig_memops
);
719 * vb2_dma_contig_set_max_seg_size() - configure DMA max segment size
720 * @dev: device for configuring DMA parameters
721 * @size: size of DMA max segment size to set
723 * To allow mapping the scatter-list into a single chunk in the DMA
724 * address space, the device is required to have the DMA max segment
725 * size parameter set to a value larger than the buffer size. Otherwise,
726 * the DMA-mapping subsystem will split the mapping into max segment
727 * size chunks. This function sets the DMA max segment size
728 * parameter to let DMA-mapping map a buffer as a single chunk in DMA
730 * This code assumes that the DMA-mapping subsystem will merge all
731 * scatterlist segments if this is really possible (for example when
732 * an IOMMU is available and enabled).
733 * Ideally, this parameter should be set by the generic bus code, but it
734 * is left with the default 64KiB value due to historical litmiations in
735 * other subsystems (like limited USB host drivers) and there no good
736 * place to set it to the proper value.
737 * This function should be called from the drivers, which are known to
738 * operate on platforms with IOMMU and provide access to shared buffers
739 * (either USERPTR or DMABUF). This should be done before initializing
742 int vb2_dma_contig_set_max_seg_size(struct device
*dev
, unsigned int size
)
744 if (!dev
->dma_parms
) {
745 dev_err(dev
, "Failed to set max_seg_size: dma_parms is NULL\n");
748 if (dma_get_max_seg_size(dev
) < size
)
749 return dma_set_max_seg_size(dev
, size
);
753 EXPORT_SYMBOL_GPL(vb2_dma_contig_set_max_seg_size
);
755 MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
756 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
757 MODULE_LICENSE("GPL");