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
;
56 static void vb2_dma_sg_put(void *buf_priv
);
58 static int vb2_dma_sg_alloc_compacted(struct vb2_dma_sg_buf
*buf
,
61 unsigned int last_page
= 0;
62 unsigned long size
= buf
->size
;
69 order
= get_order(size
);
70 /* Don't over allocate*/
71 if ((PAGE_SIZE
<< order
) > size
)
76 pages
= alloc_pages(GFP_KERNEL
| __GFP_ZERO
|
77 __GFP_NOWARN
| gfp_flags
, order
);
83 __free_page(buf
->pages
[last_page
]);
89 split_page(pages
, order
);
90 for (i
= 0; i
< (1 << order
); i
++)
91 buf
->pages
[last_page
++] = &pages
[i
];
93 size
-= PAGE_SIZE
<< order
;
99 static void *vb2_dma_sg_alloc(struct device
*dev
, unsigned long dma_attrs
,
100 unsigned long size
, enum dma_data_direction dma_dir
,
103 struct vb2_dma_sg_buf
*buf
;
104 struct sg_table
*sgt
;
109 return ERR_PTR(-EINVAL
);
111 buf
= kzalloc(sizeof *buf
, GFP_KERNEL
);
113 return ERR_PTR(-ENOMEM
);
116 buf
->dma_dir
= dma_dir
;
119 /* size is already page aligned */
120 buf
->num_pages
= size
>> PAGE_SHIFT
;
121 buf
->dma_sgt
= &buf
->sg_table
;
124 * NOTE: dma-sg allocates memory using the page allocator directly, so
125 * there is no memory consistency guarantee, hence dma-sg ignores DMA
126 * attributes passed from the upper layer.
128 buf
->pages
= kvmalloc_array(buf
->num_pages
, sizeof(struct page
*),
129 GFP_KERNEL
| __GFP_ZERO
);
131 goto fail_pages_array_alloc
;
133 ret
= vb2_dma_sg_alloc_compacted(buf
, 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
;
158 refcount_set(&buf
->refcount
, 1);
160 dprintk(1, "%s: Allocated buffer of %d pages\n",
161 __func__
, buf
->num_pages
);
165 put_device(buf
->dev
);
166 sg_free_table(buf
->dma_sgt
);
168 num_pages
= buf
->num_pages
;
170 __free_page(buf
->pages
[num_pages
]);
173 fail_pages_array_alloc
:
175 return ERR_PTR(-ENOMEM
);
178 static void vb2_dma_sg_put(void *buf_priv
)
180 struct vb2_dma_sg_buf
*buf
= buf_priv
;
181 struct sg_table
*sgt
= &buf
->sg_table
;
182 int i
= buf
->num_pages
;
184 if (refcount_dec_and_test(&buf
->refcount
)) {
185 dprintk(1, "%s: Freeing buffer of %d pages\n", __func__
,
187 dma_unmap_sgtable(buf
->dev
, sgt
, buf
->dma_dir
,
188 DMA_ATTR_SKIP_CPU_SYNC
);
190 vm_unmap_ram(buf
->vaddr
, buf
->num_pages
);
191 sg_free_table(buf
->dma_sgt
);
193 __free_page(buf
->pages
[i
]);
195 put_device(buf
->dev
);
200 static void vb2_dma_sg_prepare(void *buf_priv
)
202 struct vb2_dma_sg_buf
*buf
= buf_priv
;
203 struct sg_table
*sgt
= buf
->dma_sgt
;
205 dma_sync_sgtable_for_device(buf
->dev
, sgt
, buf
->dma_dir
);
208 static void vb2_dma_sg_finish(void *buf_priv
)
210 struct vb2_dma_sg_buf
*buf
= buf_priv
;
211 struct sg_table
*sgt
= buf
->dma_sgt
;
213 dma_sync_sgtable_for_cpu(buf
->dev
, sgt
, buf
->dma_dir
);
216 static void *vb2_dma_sg_get_userptr(struct device
*dev
, unsigned long vaddr
,
218 enum dma_data_direction dma_dir
)
220 struct vb2_dma_sg_buf
*buf
;
221 struct sg_table
*sgt
;
222 struct frame_vector
*vec
;
225 return ERR_PTR(-EINVAL
);
227 buf
= kzalloc(sizeof *buf
, GFP_KERNEL
);
229 return ERR_PTR(-ENOMEM
);
233 buf
->dma_dir
= dma_dir
;
234 buf
->offset
= vaddr
& ~PAGE_MASK
;
236 buf
->dma_sgt
= &buf
->sg_table
;
237 vec
= vb2_create_framevec(vaddr
, size
);
239 goto userptr_fail_pfnvec
;
242 buf
->pages
= frame_vector_pages(vec
);
243 if (IS_ERR(buf
->pages
))
244 goto userptr_fail_sgtable
;
245 buf
->num_pages
= frame_vector_count(vec
);
247 if (sg_alloc_table_from_pages(buf
->dma_sgt
, buf
->pages
,
248 buf
->num_pages
, buf
->offset
, size
, 0))
249 goto userptr_fail_sgtable
;
251 sgt
= &buf
->sg_table
;
253 * No need to sync to the device, this will happen later when the
254 * prepare() memop is called.
256 if (dma_map_sgtable(buf
->dev
, sgt
, buf
->dma_dir
,
257 DMA_ATTR_SKIP_CPU_SYNC
))
258 goto userptr_fail_map
;
263 sg_free_table(&buf
->sg_table
);
264 userptr_fail_sgtable
:
265 vb2_destroy_framevec(vec
);
268 return ERR_PTR(-ENOMEM
);
272 * @put_userptr: inform the allocator that a USERPTR buffer will no longer
275 static void vb2_dma_sg_put_userptr(void *buf_priv
)
277 struct vb2_dma_sg_buf
*buf
= buf_priv
;
278 struct sg_table
*sgt
= &buf
->sg_table
;
279 int i
= buf
->num_pages
;
281 dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
282 __func__
, buf
->num_pages
);
283 dma_unmap_sgtable(buf
->dev
, sgt
, buf
->dma_dir
, DMA_ATTR_SKIP_CPU_SYNC
);
285 vm_unmap_ram(buf
->vaddr
, buf
->num_pages
);
286 sg_free_table(buf
->dma_sgt
);
287 if (buf
->dma_dir
== DMA_FROM_DEVICE
||
288 buf
->dma_dir
== DMA_BIDIRECTIONAL
)
290 set_page_dirty_lock(buf
->pages
[i
]);
291 vb2_destroy_framevec(buf
->vec
);
295 static void *vb2_dma_sg_vaddr(void *buf_priv
)
297 struct vb2_dma_sg_buf
*buf
= buf_priv
;
298 struct dma_buf_map map
;
304 if (buf
->db_attach
) {
305 ret
= dma_buf_vmap(buf
->db_attach
->dmabuf
, &map
);
306 buf
->vaddr
= ret
? NULL
: map
.vaddr
;
308 buf
->vaddr
= vm_map_ram(buf
->pages
, buf
->num_pages
, -1);
312 /* add offset in case userptr is not page-aligned */
313 return buf
->vaddr
? buf
->vaddr
+ buf
->offset
: NULL
;
316 static unsigned int vb2_dma_sg_num_users(void *buf_priv
)
318 struct vb2_dma_sg_buf
*buf
= buf_priv
;
320 return refcount_read(&buf
->refcount
);
323 static int vb2_dma_sg_mmap(void *buf_priv
, struct vm_area_struct
*vma
)
325 struct vb2_dma_sg_buf
*buf
= buf_priv
;
329 printk(KERN_ERR
"No memory to map\n");
333 err
= vm_map_pages(vma
, buf
->pages
, buf
->num_pages
);
335 printk(KERN_ERR
"Remapping memory, error: %d\n", err
);
340 * Use common vm_area operations to track buffer refcount.
342 vma
->vm_private_data
= &buf
->handler
;
343 vma
->vm_ops
= &vb2_common_vm_ops
;
345 vma
->vm_ops
->open(vma
);
350 /*********************************************/
351 /* DMABUF ops for exporters */
352 /*********************************************/
354 struct vb2_dma_sg_attachment
{
356 enum dma_data_direction dma_dir
;
359 static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf
*dbuf
,
360 struct dma_buf_attachment
*dbuf_attach
)
362 struct vb2_dma_sg_attachment
*attach
;
364 struct scatterlist
*rd
, *wr
;
365 struct sg_table
*sgt
;
366 struct vb2_dma_sg_buf
*buf
= dbuf
->priv
;
369 attach
= kzalloc(sizeof(*attach
), GFP_KERNEL
);
374 /* Copy the buf->base_sgt scatter list to the attachment, as we can't
375 * map the same scatter list to multiple attachments at the same time.
377 ret
= sg_alloc_table(sgt
, buf
->dma_sgt
->orig_nents
, GFP_KERNEL
);
383 rd
= buf
->dma_sgt
->sgl
;
385 for (i
= 0; i
< sgt
->orig_nents
; ++i
) {
386 sg_set_page(wr
, sg_page(rd
), rd
->length
, rd
->offset
);
391 attach
->dma_dir
= DMA_NONE
;
392 dbuf_attach
->priv
= attach
;
397 static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf
*dbuf
,
398 struct dma_buf_attachment
*db_attach
)
400 struct vb2_dma_sg_attachment
*attach
= db_attach
->priv
;
401 struct sg_table
*sgt
;
408 /* release the scatterlist cache */
409 if (attach
->dma_dir
!= DMA_NONE
)
410 dma_unmap_sgtable(db_attach
->dev
, sgt
, attach
->dma_dir
, 0);
413 db_attach
->priv
= NULL
;
416 static struct sg_table
*vb2_dma_sg_dmabuf_ops_map(
417 struct dma_buf_attachment
*db_attach
, enum dma_data_direction dma_dir
)
419 struct vb2_dma_sg_attachment
*attach
= db_attach
->priv
;
420 /* stealing dmabuf mutex to serialize map/unmap operations */
421 struct mutex
*lock
= &db_attach
->dmabuf
->lock
;
422 struct sg_table
*sgt
;
427 /* return previously mapped sg table */
428 if (attach
->dma_dir
== dma_dir
) {
433 /* release any previous cache */
434 if (attach
->dma_dir
!= DMA_NONE
) {
435 dma_unmap_sgtable(db_attach
->dev
, sgt
, attach
->dma_dir
, 0);
436 attach
->dma_dir
= DMA_NONE
;
439 /* mapping to the client with new direction */
440 if (dma_map_sgtable(db_attach
->dev
, sgt
, dma_dir
, 0)) {
441 pr_err("failed to map scatterlist\n");
443 return ERR_PTR(-EIO
);
446 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
, struct dma_buf_map
*map
)
489 struct vb2_dma_sg_buf
*buf
= dbuf
->priv
;
491 dma_buf_map_set_vaddr(map
, buf
->vaddr
);
496 static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf
*dbuf
,
497 struct vm_area_struct
*vma
)
499 return vb2_dma_sg_mmap(dbuf
->priv
, vma
);
502 static const struct dma_buf_ops vb2_dma_sg_dmabuf_ops
= {
503 .attach
= vb2_dma_sg_dmabuf_ops_attach
,
504 .detach
= vb2_dma_sg_dmabuf_ops_detach
,
505 .map_dma_buf
= vb2_dma_sg_dmabuf_ops_map
,
506 .unmap_dma_buf
= vb2_dma_sg_dmabuf_ops_unmap
,
507 .begin_cpu_access
= vb2_dma_sg_dmabuf_ops_begin_cpu_access
,
508 .end_cpu_access
= vb2_dma_sg_dmabuf_ops_end_cpu_access
,
509 .vmap
= vb2_dma_sg_dmabuf_ops_vmap
,
510 .mmap
= vb2_dma_sg_dmabuf_ops_mmap
,
511 .release
= vb2_dma_sg_dmabuf_ops_release
,
514 static struct dma_buf
*vb2_dma_sg_get_dmabuf(void *buf_priv
, unsigned long flags
)
516 struct vb2_dma_sg_buf
*buf
= buf_priv
;
517 struct dma_buf
*dbuf
;
518 DEFINE_DMA_BUF_EXPORT_INFO(exp_info
);
520 exp_info
.ops
= &vb2_dma_sg_dmabuf_ops
;
521 exp_info
.size
= buf
->size
;
522 exp_info
.flags
= flags
;
525 if (WARN_ON(!buf
->dma_sgt
))
528 dbuf
= dma_buf_export(&exp_info
);
532 /* dmabuf keeps reference to vb2 buffer */
533 refcount_inc(&buf
->refcount
);
538 /*********************************************/
539 /* callbacks for DMABUF buffers */
540 /*********************************************/
542 static int vb2_dma_sg_map_dmabuf(void *mem_priv
)
544 struct vb2_dma_sg_buf
*buf
= mem_priv
;
545 struct sg_table
*sgt
;
547 if (WARN_ON(!buf
->db_attach
)) {
548 pr_err("trying to pin a non attached buffer\n");
552 if (WARN_ON(buf
->dma_sgt
)) {
553 pr_err("dmabuf buffer is already pinned\n");
557 /* get the associated scatterlist for this buffer */
558 sgt
= dma_buf_map_attachment(buf
->db_attach
, buf
->dma_dir
);
560 pr_err("Error getting dmabuf scatterlist\n");
570 static void vb2_dma_sg_unmap_dmabuf(void *mem_priv
)
572 struct vb2_dma_sg_buf
*buf
= mem_priv
;
573 struct sg_table
*sgt
= buf
->dma_sgt
;
574 struct dma_buf_map map
= DMA_BUF_MAP_INIT_VADDR(buf
->vaddr
);
576 if (WARN_ON(!buf
->db_attach
)) {
577 pr_err("trying to unpin a not attached buffer\n");
582 pr_err("dmabuf buffer is already unpinned\n");
587 dma_buf_vunmap(buf
->db_attach
->dmabuf
, &map
);
590 dma_buf_unmap_attachment(buf
->db_attach
, sgt
, buf
->dma_dir
);
595 static void vb2_dma_sg_detach_dmabuf(void *mem_priv
)
597 struct vb2_dma_sg_buf
*buf
= mem_priv
;
599 /* if vb2 works correctly you should never detach mapped buffer */
600 if (WARN_ON(buf
->dma_sgt
))
601 vb2_dma_sg_unmap_dmabuf(buf
);
603 /* detach this attachment */
604 dma_buf_detach(buf
->db_attach
->dmabuf
, buf
->db_attach
);
608 static void *vb2_dma_sg_attach_dmabuf(struct device
*dev
, struct dma_buf
*dbuf
,
609 unsigned long size
, enum dma_data_direction dma_dir
)
611 struct vb2_dma_sg_buf
*buf
;
612 struct dma_buf_attachment
*dba
;
615 return ERR_PTR(-EINVAL
);
617 if (dbuf
->size
< size
)
618 return ERR_PTR(-EFAULT
);
620 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
622 return ERR_PTR(-ENOMEM
);
625 /* create attachment for the dmabuf with the user device */
626 dba
= dma_buf_attach(dbuf
, buf
->dev
);
628 pr_err("failed to attach dmabuf\n");
633 buf
->dma_dir
= dma_dir
;
635 buf
->db_attach
= dba
;
640 static void *vb2_dma_sg_cookie(void *buf_priv
)
642 struct vb2_dma_sg_buf
*buf
= buf_priv
;
647 const struct vb2_mem_ops vb2_dma_sg_memops
= {
648 .alloc
= vb2_dma_sg_alloc
,
649 .put
= vb2_dma_sg_put
,
650 .get_userptr
= vb2_dma_sg_get_userptr
,
651 .put_userptr
= vb2_dma_sg_put_userptr
,
652 .prepare
= vb2_dma_sg_prepare
,
653 .finish
= vb2_dma_sg_finish
,
654 .vaddr
= vb2_dma_sg_vaddr
,
655 .mmap
= vb2_dma_sg_mmap
,
656 .num_users
= vb2_dma_sg_num_users
,
657 .get_dmabuf
= vb2_dma_sg_get_dmabuf
,
658 .map_dmabuf
= vb2_dma_sg_map_dmabuf
,
659 .unmap_dmabuf
= vb2_dma_sg_unmap_dmabuf
,
660 .attach_dmabuf
= vb2_dma_sg_attach_dmabuf
,
661 .detach_dmabuf
= vb2_dma_sg_detach_dmabuf
,
662 .cookie
= vb2_dma_sg_cookie
,
664 EXPORT_SYMBOL_GPL(vb2_dma_sg_memops
);
666 MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
667 MODULE_AUTHOR("Andrzej Pietrasiewicz");
668 MODULE_LICENSE("GPL");