2 * videobuf2-dma-sg.c - dma scatter/gather memory allocator for videobuf2
4 * Copyright (C) 2010 Samsung Electronics
6 * Author: Andrzej Pietrasiewicz <andrzejtp2010@gmail.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/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/vmalloc.h>
21 #include <media/videobuf2-v4l2.h>
22 #include <media/videobuf2-memops.h>
23 #include <media/videobuf2-dma-sg.h>
26 module_param(debug
, int, 0644);
28 #define dprintk(level, fmt, arg...) \
31 printk(KERN_DEBUG "vb2-dma-sg: " fmt, ## arg); \
34 struct vb2_dma_sg_buf
{
38 struct frame_vector
*vec
;
40 enum dma_data_direction dma_dir
;
41 struct sg_table sg_table
;
43 * This will point to sg_table when used with the MMAP or USERPTR
44 * memory model, and to the dma_buf sglist when used with the
45 * DMABUF memory model.
47 struct sg_table
*dma_sgt
;
49 unsigned int num_pages
;
51 struct vb2_vmarea_handler handler
;
53 struct dma_buf_attachment
*db_attach
;
55 struct vb2_buffer
*vb
;
58 static void vb2_dma_sg_put(void *buf_priv
);
60 static int vb2_dma_sg_alloc_compacted(struct vb2_dma_sg_buf
*buf
,
63 unsigned int last_page
= 0;
64 unsigned long size
= buf
->size
;
71 order
= get_order(size
);
72 /* Don't over allocate*/
73 if ((PAGE_SIZE
<< order
) > size
)
78 pages
= alloc_pages(GFP_KERNEL
| __GFP_ZERO
|
79 __GFP_NOWARN
| gfp_flags
, order
);
85 __free_page(buf
->pages
[last_page
]);
91 split_page(pages
, order
);
92 for (i
= 0; i
< (1 << order
); i
++)
93 buf
->pages
[last_page
++] = &pages
[i
];
95 size
-= PAGE_SIZE
<< order
;
101 static void *vb2_dma_sg_alloc(struct vb2_buffer
*vb
, struct device
*dev
,
104 struct vb2_dma_sg_buf
*buf
;
105 struct sg_table
*sgt
;
109 if (WARN_ON(!dev
) || WARN_ON(!size
))
110 return ERR_PTR(-EINVAL
);
112 buf
= kzalloc(sizeof *buf
, GFP_KERNEL
);
114 return ERR_PTR(-ENOMEM
);
117 buf
->dma_dir
= vb
->vb2_queue
->dma_dir
;
120 /* size is already page aligned */
121 buf
->num_pages
= size
>> PAGE_SHIFT
;
122 buf
->dma_sgt
= &buf
->sg_table
;
125 * NOTE: dma-sg allocates memory using the page allocator directly, so
126 * there is no memory consistency guarantee, hence dma-sg ignores DMA
127 * attributes passed from the upper layer.
129 buf
->pages
= kvcalloc(buf
->num_pages
, sizeof(struct page
*), GFP_KERNEL
);
131 goto fail_pages_array_alloc
;
133 ret
= vb2_dma_sg_alloc_compacted(buf
, vb
->vb2_queue
->gfp_flags
);
135 goto fail_pages_alloc
;
137 ret
= sg_alloc_table_from_pages(buf
->dma_sgt
, buf
->pages
,
138 buf
->num_pages
, 0, size
, GFP_KERNEL
);
140 goto fail_table_alloc
;
142 /* Prevent the device from being released while the buffer is used */
143 buf
->dev
= get_device(dev
);
145 sgt
= &buf
->sg_table
;
147 * No need to sync to the device, this will happen later when the
148 * prepare() memop is called.
150 if (dma_map_sgtable(buf
->dev
, sgt
, buf
->dma_dir
,
151 DMA_ATTR_SKIP_CPU_SYNC
))
154 buf
->handler
.refcount
= &buf
->refcount
;
155 buf
->handler
.put
= vb2_dma_sg_put
;
156 buf
->handler
.arg
= buf
;
159 refcount_set(&buf
->refcount
, 1);
161 dprintk(1, "%s: Allocated buffer of %d pages\n",
162 __func__
, buf
->num_pages
);
166 put_device(buf
->dev
);
167 sg_free_table(buf
->dma_sgt
);
169 num_pages
= buf
->num_pages
;
171 __free_page(buf
->pages
[num_pages
]);
174 fail_pages_array_alloc
:
176 return ERR_PTR(-ENOMEM
);
179 static void vb2_dma_sg_put(void *buf_priv
)
181 struct vb2_dma_sg_buf
*buf
= buf_priv
;
182 struct sg_table
*sgt
= &buf
->sg_table
;
183 int i
= buf
->num_pages
;
185 if (refcount_dec_and_test(&buf
->refcount
)) {
186 dprintk(1, "%s: Freeing buffer of %d pages\n", __func__
,
188 dma_unmap_sgtable(buf
->dev
, sgt
, buf
->dma_dir
,
189 DMA_ATTR_SKIP_CPU_SYNC
);
191 vm_unmap_ram(buf
->vaddr
, buf
->num_pages
);
192 sg_free_table(buf
->dma_sgt
);
194 __free_page(buf
->pages
[i
]);
196 put_device(buf
->dev
);
201 static void vb2_dma_sg_prepare(void *buf_priv
)
203 struct vb2_dma_sg_buf
*buf
= buf_priv
;
204 struct sg_table
*sgt
= buf
->dma_sgt
;
206 if (buf
->vb
->skip_cache_sync_on_prepare
)
209 dma_sync_sgtable_for_device(buf
->dev
, sgt
, buf
->dma_dir
);
212 static void vb2_dma_sg_finish(void *buf_priv
)
214 struct vb2_dma_sg_buf
*buf
= buf_priv
;
215 struct sg_table
*sgt
= buf
->dma_sgt
;
217 if (buf
->vb
->skip_cache_sync_on_finish
)
220 dma_sync_sgtable_for_cpu(buf
->dev
, sgt
, buf
->dma_dir
);
223 static void *vb2_dma_sg_get_userptr(struct vb2_buffer
*vb
, struct device
*dev
,
224 unsigned long vaddr
, unsigned long size
)
226 struct vb2_dma_sg_buf
*buf
;
227 struct sg_table
*sgt
;
228 struct frame_vector
*vec
;
231 return ERR_PTR(-EINVAL
);
233 buf
= kzalloc(sizeof *buf
, GFP_KERNEL
);
235 return ERR_PTR(-ENOMEM
);
239 buf
->dma_dir
= vb
->vb2_queue
->dma_dir
;
240 buf
->offset
= vaddr
& ~PAGE_MASK
;
242 buf
->dma_sgt
= &buf
->sg_table
;
244 vec
= vb2_create_framevec(vaddr
, size
,
245 buf
->dma_dir
== DMA_FROM_DEVICE
||
246 buf
->dma_dir
== DMA_BIDIRECTIONAL
);
248 goto userptr_fail_pfnvec
;
251 buf
->pages
= frame_vector_pages(vec
);
252 if (IS_ERR(buf
->pages
))
253 goto userptr_fail_sgtable
;
254 buf
->num_pages
= frame_vector_count(vec
);
256 if (sg_alloc_table_from_pages(buf
->dma_sgt
, buf
->pages
,
257 buf
->num_pages
, buf
->offset
, size
, 0))
258 goto userptr_fail_sgtable
;
260 sgt
= &buf
->sg_table
;
262 * No need to sync to the device, this will happen later when the
263 * prepare() memop is called.
265 if (dma_map_sgtable(buf
->dev
, sgt
, buf
->dma_dir
,
266 DMA_ATTR_SKIP_CPU_SYNC
))
267 goto userptr_fail_map
;
272 sg_free_table(&buf
->sg_table
);
273 userptr_fail_sgtable
:
274 vb2_destroy_framevec(vec
);
277 return ERR_PTR(-ENOMEM
);
281 * @put_userptr: inform the allocator that a USERPTR buffer will no longer
284 static void vb2_dma_sg_put_userptr(void *buf_priv
)
286 struct vb2_dma_sg_buf
*buf
= buf_priv
;
287 struct sg_table
*sgt
= &buf
->sg_table
;
288 int i
= buf
->num_pages
;
290 dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
291 __func__
, buf
->num_pages
);
292 dma_unmap_sgtable(buf
->dev
, sgt
, buf
->dma_dir
, DMA_ATTR_SKIP_CPU_SYNC
);
294 vm_unmap_ram(buf
->vaddr
, buf
->num_pages
);
295 sg_free_table(buf
->dma_sgt
);
296 if (buf
->dma_dir
== DMA_FROM_DEVICE
||
297 buf
->dma_dir
== DMA_BIDIRECTIONAL
)
299 set_page_dirty_lock(buf
->pages
[i
]);
300 vb2_destroy_framevec(buf
->vec
);
304 static void *vb2_dma_sg_vaddr(struct vb2_buffer
*vb
, void *buf_priv
)
306 struct vb2_dma_sg_buf
*buf
= buf_priv
;
307 struct iosys_map map
;
313 if (buf
->db_attach
) {
314 ret
= dma_buf_vmap_unlocked(buf
->db_attach
->dmabuf
, &map
);
315 buf
->vaddr
= ret
? NULL
: map
.vaddr
;
317 buf
->vaddr
= vm_map_ram(buf
->pages
, buf
->num_pages
, -1);
321 /* add offset in case userptr is not page-aligned */
322 return buf
->vaddr
? buf
->vaddr
+ buf
->offset
: NULL
;
325 static unsigned int vb2_dma_sg_num_users(void *buf_priv
)
327 struct vb2_dma_sg_buf
*buf
= buf_priv
;
329 return refcount_read(&buf
->refcount
);
332 static int vb2_dma_sg_mmap(void *buf_priv
, struct vm_area_struct
*vma
)
334 struct vb2_dma_sg_buf
*buf
= buf_priv
;
338 printk(KERN_ERR
"No memory to map\n");
342 err
= vm_map_pages(vma
, buf
->pages
, buf
->num_pages
);
344 printk(KERN_ERR
"Remapping memory, error: %d\n", err
);
349 * Use common vm_area operations to track buffer refcount.
351 vma
->vm_private_data
= &buf
->handler
;
352 vma
->vm_ops
= &vb2_common_vm_ops
;
354 vma
->vm_ops
->open(vma
);
359 /*********************************************/
360 /* DMABUF ops for exporters */
361 /*********************************************/
363 struct vb2_dma_sg_attachment
{
365 enum dma_data_direction dma_dir
;
368 static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf
*dbuf
,
369 struct dma_buf_attachment
*dbuf_attach
)
371 struct vb2_dma_sg_attachment
*attach
;
373 struct scatterlist
*rd
, *wr
;
374 struct sg_table
*sgt
;
375 struct vb2_dma_sg_buf
*buf
= dbuf
->priv
;
378 attach
= kzalloc(sizeof(*attach
), GFP_KERNEL
);
383 /* Copy the buf->base_sgt scatter list to the attachment, as we can't
384 * map the same scatter list to multiple attachments at the same time.
386 ret
= sg_alloc_table(sgt
, buf
->dma_sgt
->orig_nents
, GFP_KERNEL
);
392 rd
= buf
->dma_sgt
->sgl
;
394 for (i
= 0; i
< sgt
->orig_nents
; ++i
) {
395 sg_set_page(wr
, sg_page(rd
), rd
->length
, rd
->offset
);
400 attach
->dma_dir
= DMA_NONE
;
401 dbuf_attach
->priv
= attach
;
406 static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf
*dbuf
,
407 struct dma_buf_attachment
*db_attach
)
409 struct vb2_dma_sg_attachment
*attach
= db_attach
->priv
;
410 struct sg_table
*sgt
;
417 /* release the scatterlist cache */
418 if (attach
->dma_dir
!= DMA_NONE
)
419 dma_unmap_sgtable(db_attach
->dev
, sgt
, attach
->dma_dir
, 0);
422 db_attach
->priv
= NULL
;
425 static struct sg_table
*vb2_dma_sg_dmabuf_ops_map(
426 struct dma_buf_attachment
*db_attach
, enum dma_data_direction dma_dir
)
428 struct vb2_dma_sg_attachment
*attach
= db_attach
->priv
;
429 struct sg_table
*sgt
;
432 /* return previously mapped sg table */
433 if (attach
->dma_dir
== dma_dir
)
436 /* release any previous cache */
437 if (attach
->dma_dir
!= DMA_NONE
) {
438 dma_unmap_sgtable(db_attach
->dev
, sgt
, attach
->dma_dir
, 0);
439 attach
->dma_dir
= DMA_NONE
;
442 /* mapping to the client with new direction */
443 if (dma_map_sgtable(db_attach
->dev
, sgt
, dma_dir
, 0)) {
444 pr_err("failed to map scatterlist\n");
445 return ERR_PTR(-EIO
);
448 attach
->dma_dir
= dma_dir
;
453 static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment
*db_attach
,
454 struct sg_table
*sgt
, enum dma_data_direction dma_dir
)
456 /* nothing to be done here */
459 static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf
*dbuf
)
461 /* drop reference obtained in vb2_dma_sg_get_dmabuf */
462 vb2_dma_sg_put(dbuf
->priv
);
466 vb2_dma_sg_dmabuf_ops_begin_cpu_access(struct dma_buf
*dbuf
,
467 enum dma_data_direction direction
)
469 struct vb2_dma_sg_buf
*buf
= dbuf
->priv
;
470 struct sg_table
*sgt
= buf
->dma_sgt
;
472 dma_sync_sg_for_cpu(buf
->dev
, sgt
->sgl
, sgt
->nents
, buf
->dma_dir
);
477 vb2_dma_sg_dmabuf_ops_end_cpu_access(struct dma_buf
*dbuf
,
478 enum dma_data_direction direction
)
480 struct vb2_dma_sg_buf
*buf
= dbuf
->priv
;
481 struct sg_table
*sgt
= buf
->dma_sgt
;
483 dma_sync_sg_for_device(buf
->dev
, sgt
->sgl
, sgt
->nents
, buf
->dma_dir
);
487 static int vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf
*dbuf
,
488 struct iosys_map
*map
)
490 struct vb2_dma_sg_buf
*buf
;
494 vaddr
= vb2_dma_sg_vaddr(buf
->vb
, buf
);
498 iosys_map_set_vaddr(map
, vaddr
);
503 static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf
*dbuf
,
504 struct vm_area_struct
*vma
)
506 return vb2_dma_sg_mmap(dbuf
->priv
, vma
);
509 static const struct dma_buf_ops vb2_dma_sg_dmabuf_ops
= {
510 .attach
= vb2_dma_sg_dmabuf_ops_attach
,
511 .detach
= vb2_dma_sg_dmabuf_ops_detach
,
512 .map_dma_buf
= vb2_dma_sg_dmabuf_ops_map
,
513 .unmap_dma_buf
= vb2_dma_sg_dmabuf_ops_unmap
,
514 .begin_cpu_access
= vb2_dma_sg_dmabuf_ops_begin_cpu_access
,
515 .end_cpu_access
= vb2_dma_sg_dmabuf_ops_end_cpu_access
,
516 .vmap
= vb2_dma_sg_dmabuf_ops_vmap
,
517 .mmap
= vb2_dma_sg_dmabuf_ops_mmap
,
518 .release
= vb2_dma_sg_dmabuf_ops_release
,
521 static struct dma_buf
*vb2_dma_sg_get_dmabuf(struct vb2_buffer
*vb
,
525 struct vb2_dma_sg_buf
*buf
= buf_priv
;
526 struct dma_buf
*dbuf
;
527 DEFINE_DMA_BUF_EXPORT_INFO(exp_info
);
529 exp_info
.ops
= &vb2_dma_sg_dmabuf_ops
;
530 exp_info
.size
= buf
->size
;
531 exp_info
.flags
= flags
;
534 if (WARN_ON(!buf
->dma_sgt
))
537 dbuf
= dma_buf_export(&exp_info
);
541 /* dmabuf keeps reference to vb2 buffer */
542 refcount_inc(&buf
->refcount
);
547 /*********************************************/
548 /* callbacks for DMABUF buffers */
549 /*********************************************/
551 static int vb2_dma_sg_map_dmabuf(void *mem_priv
)
553 struct vb2_dma_sg_buf
*buf
= mem_priv
;
554 struct sg_table
*sgt
;
556 if (WARN_ON(!buf
->db_attach
)) {
557 pr_err("trying to pin a non attached buffer\n");
561 if (WARN_ON(buf
->dma_sgt
)) {
562 pr_err("dmabuf buffer is already pinned\n");
566 /* get the associated scatterlist for this buffer */
567 sgt
= dma_buf_map_attachment_unlocked(buf
->db_attach
, buf
->dma_dir
);
569 pr_err("Error getting dmabuf scatterlist\n");
579 static void vb2_dma_sg_unmap_dmabuf(void *mem_priv
)
581 struct vb2_dma_sg_buf
*buf
= mem_priv
;
582 struct sg_table
*sgt
= buf
->dma_sgt
;
583 struct iosys_map map
= IOSYS_MAP_INIT_VADDR(buf
->vaddr
);
585 if (WARN_ON(!buf
->db_attach
)) {
586 pr_err("trying to unpin a not attached buffer\n");
591 pr_err("dmabuf buffer is already unpinned\n");
596 dma_buf_vunmap_unlocked(buf
->db_attach
->dmabuf
, &map
);
599 dma_buf_unmap_attachment_unlocked(buf
->db_attach
, sgt
, buf
->dma_dir
);
604 static void vb2_dma_sg_detach_dmabuf(void *mem_priv
)
606 struct vb2_dma_sg_buf
*buf
= mem_priv
;
608 /* if vb2 works correctly you should never detach mapped buffer */
609 if (WARN_ON(buf
->dma_sgt
))
610 vb2_dma_sg_unmap_dmabuf(buf
);
612 /* detach this attachment */
613 dma_buf_detach(buf
->db_attach
->dmabuf
, buf
->db_attach
);
617 static void *vb2_dma_sg_attach_dmabuf(struct vb2_buffer
*vb
, struct device
*dev
,
618 struct dma_buf
*dbuf
, unsigned long size
)
620 struct vb2_dma_sg_buf
*buf
;
621 struct dma_buf_attachment
*dba
;
624 return ERR_PTR(-EINVAL
);
626 if (dbuf
->size
< size
)
627 return ERR_PTR(-EFAULT
);
629 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
631 return ERR_PTR(-ENOMEM
);
634 /* create attachment for the dmabuf with the user device */
635 dba
= dma_buf_attach(dbuf
, buf
->dev
);
637 pr_err("failed to attach dmabuf\n");
642 buf
->dma_dir
= vb
->vb2_queue
->dma_dir
;
644 buf
->db_attach
= dba
;
650 static void *vb2_dma_sg_cookie(struct vb2_buffer
*vb
, void *buf_priv
)
652 struct vb2_dma_sg_buf
*buf
= buf_priv
;
657 const struct vb2_mem_ops vb2_dma_sg_memops
= {
658 .alloc
= vb2_dma_sg_alloc
,
659 .put
= vb2_dma_sg_put
,
660 .get_userptr
= vb2_dma_sg_get_userptr
,
661 .put_userptr
= vb2_dma_sg_put_userptr
,
662 .prepare
= vb2_dma_sg_prepare
,
663 .finish
= vb2_dma_sg_finish
,
664 .vaddr
= vb2_dma_sg_vaddr
,
665 .mmap
= vb2_dma_sg_mmap
,
666 .num_users
= vb2_dma_sg_num_users
,
667 .get_dmabuf
= vb2_dma_sg_get_dmabuf
,
668 .map_dmabuf
= vb2_dma_sg_map_dmabuf
,
669 .unmap_dmabuf
= vb2_dma_sg_unmap_dmabuf
,
670 .attach_dmabuf
= vb2_dma_sg_attach_dmabuf
,
671 .detach_dmabuf
= vb2_dma_sg_detach_dmabuf
,
672 .cookie
= vb2_dma_sg_cookie
,
674 EXPORT_SYMBOL_GPL(vb2_dma_sg_memops
);
676 MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
677 MODULE_AUTHOR("Andrzej Pietrasiewicz");
678 MODULE_LICENSE("GPL");
679 MODULE_IMPORT_NS("DMA_BUF");