2 * videobuf2-dma-sg.c - dma scatter/gather memory allocator for videobuf2
4 * Copyright (C) 2010 Samsung Electronics
6 * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.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/scatterlist.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
20 #include <media/videobuf2-v4l2.h>
21 #include <media/videobuf2-memops.h>
22 #include <media/videobuf2-dma-sg.h>
25 module_param(debug
, int, 0644);
27 #define dprintk(level, fmt, arg...) \
30 printk(KERN_DEBUG "vb2-dma-sg: " fmt, ## arg); \
33 struct vb2_dma_sg_buf
{
37 struct frame_vector
*vec
;
39 enum dma_data_direction dma_dir
;
40 struct sg_table sg_table
;
42 * This will point to sg_table when used with the MMAP or USERPTR
43 * memory model, and to the dma_buf sglist when used with the
44 * DMABUF memory model.
46 struct sg_table
*dma_sgt
;
48 unsigned int num_pages
;
50 struct vb2_vmarea_handler handler
;
52 struct dma_buf_attachment
*db_attach
;
55 static void vb2_dma_sg_put(void *buf_priv
);
57 static int vb2_dma_sg_alloc_compacted(struct vb2_dma_sg_buf
*buf
,
60 unsigned int last_page
= 0;
68 order
= get_order(size
);
69 /* Dont over allocate*/
70 if ((PAGE_SIZE
<< order
) > size
)
75 pages
= alloc_pages(GFP_KERNEL
| __GFP_ZERO
|
76 __GFP_NOWARN
| gfp_flags
, order
);
82 __free_page(buf
->pages
[last_page
]);
88 split_page(pages
, order
);
89 for (i
= 0; i
< (1 << order
); i
++)
90 buf
->pages
[last_page
++] = &pages
[i
];
92 size
-= PAGE_SIZE
<< order
;
98 static void *vb2_dma_sg_alloc(struct device
*dev
, const struct dma_attrs
*dma_attrs
,
99 unsigned long size
, enum dma_data_direction dma_dir
,
102 struct vb2_dma_sg_buf
*buf
;
103 struct sg_table
*sgt
;
106 DEFINE_DMA_ATTRS(attrs
);
108 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC
, &attrs
);
110 if (WARN_ON(dev
== NULL
))
112 buf
= kzalloc(sizeof *buf
, GFP_KERNEL
);
117 buf
->dma_dir
= dma_dir
;
120 /* size is already page aligned */
121 buf
->num_pages
= size
>> PAGE_SHIFT
;
122 buf
->dma_sgt
= &buf
->sg_table
;
124 buf
->pages
= kzalloc(buf
->num_pages
* sizeof(struct page
*),
127 goto fail_pages_array_alloc
;
129 ret
= vb2_dma_sg_alloc_compacted(buf
, gfp_flags
);
131 goto fail_pages_alloc
;
133 ret
= sg_alloc_table_from_pages(buf
->dma_sgt
, buf
->pages
,
134 buf
->num_pages
, 0, size
, GFP_KERNEL
);
136 goto fail_table_alloc
;
138 /* Prevent the device from being released while the buffer is used */
139 buf
->dev
= get_device(dev
);
141 sgt
= &buf
->sg_table
;
143 * No need to sync to the device, this will happen later when the
144 * prepare() memop is called.
146 sgt
->nents
= dma_map_sg_attrs(buf
->dev
, sgt
->sgl
, sgt
->orig_nents
,
147 buf
->dma_dir
, &attrs
);
151 buf
->handler
.refcount
= &buf
->refcount
;
152 buf
->handler
.put
= vb2_dma_sg_put
;
153 buf
->handler
.arg
= buf
;
155 atomic_inc(&buf
->refcount
);
157 dprintk(1, "%s: Allocated buffer of %d pages\n",
158 __func__
, buf
->num_pages
);
162 put_device(buf
->dev
);
163 sg_free_table(buf
->dma_sgt
);
165 num_pages
= buf
->num_pages
;
167 __free_page(buf
->pages
[num_pages
]);
170 fail_pages_array_alloc
:
175 static void vb2_dma_sg_put(void *buf_priv
)
177 struct vb2_dma_sg_buf
*buf
= buf_priv
;
178 struct sg_table
*sgt
= &buf
->sg_table
;
179 int i
= buf
->num_pages
;
181 if (atomic_dec_and_test(&buf
->refcount
)) {
182 DEFINE_DMA_ATTRS(attrs
);
184 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC
, &attrs
);
185 dprintk(1, "%s: Freeing buffer of %d pages\n", __func__
,
187 dma_unmap_sg_attrs(buf
->dev
, sgt
->sgl
, sgt
->orig_nents
,
188 buf
->dma_dir
, &attrs
);
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 /* DMABUF exporter will flush the cache for us */
209 dma_sync_sg_for_device(buf
->dev
, sgt
->sgl
, sgt
->orig_nents
,
213 static void vb2_dma_sg_finish(void *buf_priv
)
215 struct vb2_dma_sg_buf
*buf
= buf_priv
;
216 struct sg_table
*sgt
= buf
->dma_sgt
;
218 /* DMABUF exporter will flush the cache for us */
222 dma_sync_sg_for_cpu(buf
->dev
, sgt
->sgl
, sgt
->orig_nents
, buf
->dma_dir
);
225 static void *vb2_dma_sg_get_userptr(struct device
*dev
, unsigned long vaddr
,
227 enum dma_data_direction dma_dir
)
229 struct vb2_dma_sg_buf
*buf
;
230 struct sg_table
*sgt
;
231 DEFINE_DMA_ATTRS(attrs
);
232 struct frame_vector
*vec
;
234 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC
, &attrs
);
235 buf
= kzalloc(sizeof *buf
, GFP_KERNEL
);
241 buf
->dma_dir
= dma_dir
;
242 buf
->offset
= vaddr
& ~PAGE_MASK
;
244 buf
->dma_sgt
= &buf
->sg_table
;
245 vec
= vb2_create_framevec(vaddr
, size
, buf
->dma_dir
== DMA_FROM_DEVICE
);
247 goto userptr_fail_pfnvec
;
250 buf
->pages
= frame_vector_pages(vec
);
251 if (IS_ERR(buf
->pages
))
252 goto userptr_fail_sgtable
;
253 buf
->num_pages
= frame_vector_count(vec
);
255 if (sg_alloc_table_from_pages(buf
->dma_sgt
, buf
->pages
,
256 buf
->num_pages
, buf
->offset
, size
, 0))
257 goto userptr_fail_sgtable
;
259 sgt
= &buf
->sg_table
;
261 * No need to sync to the device, this will happen later when the
262 * prepare() memop is called.
264 sgt
->nents
= dma_map_sg_attrs(buf
->dev
, sgt
->sgl
, sgt
->orig_nents
,
265 buf
->dma_dir
, &attrs
);
267 goto userptr_fail_map
;
272 sg_free_table(&buf
->sg_table
);
273 userptr_fail_sgtable
:
274 vb2_destroy_framevec(vec
);
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
;
289 DEFINE_DMA_ATTRS(attrs
);
291 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC
, &attrs
);
293 dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
294 __func__
, buf
->num_pages
);
295 dma_unmap_sg_attrs(buf
->dev
, sgt
->sgl
, sgt
->orig_nents
, buf
->dma_dir
,
298 vm_unmap_ram(buf
->vaddr
, buf
->num_pages
);
299 sg_free_table(buf
->dma_sgt
);
301 if (buf
->dma_dir
== DMA_FROM_DEVICE
)
302 set_page_dirty_lock(buf
->pages
[i
]);
304 vb2_destroy_framevec(buf
->vec
);
308 static void *vb2_dma_sg_vaddr(void *buf_priv
)
310 struct vb2_dma_sg_buf
*buf
= buf_priv
;
316 buf
->vaddr
= dma_buf_vmap(buf
->db_attach
->dmabuf
);
318 buf
->vaddr
= vm_map_ram(buf
->pages
,
319 buf
->num_pages
, -1, PAGE_KERNEL
);
322 /* add offset in case userptr is not page-aligned */
323 return buf
->vaddr
? buf
->vaddr
+ buf
->offset
: NULL
;
326 static unsigned int vb2_dma_sg_num_users(void *buf_priv
)
328 struct vb2_dma_sg_buf
*buf
= buf_priv
;
330 return atomic_read(&buf
->refcount
);
333 static int vb2_dma_sg_mmap(void *buf_priv
, struct vm_area_struct
*vma
)
335 struct vb2_dma_sg_buf
*buf
= buf_priv
;
336 unsigned long uaddr
= vma
->vm_start
;
337 unsigned long usize
= vma
->vm_end
- vma
->vm_start
;
341 printk(KERN_ERR
"No memory to map\n");
348 ret
= vm_insert_page(vma
, uaddr
, buf
->pages
[i
++]);
350 printk(KERN_ERR
"Remapping memory, error: %d\n", ret
);
360 * Use common vm_area operations to track buffer refcount.
362 vma
->vm_private_data
= &buf
->handler
;
363 vma
->vm_ops
= &vb2_common_vm_ops
;
365 vma
->vm_ops
->open(vma
);
370 /*********************************************/
371 /* DMABUF ops for exporters */
372 /*********************************************/
374 struct vb2_dma_sg_attachment
{
376 enum dma_data_direction dma_dir
;
379 static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf
*dbuf
, struct device
*dev
,
380 struct dma_buf_attachment
*dbuf_attach
)
382 struct vb2_dma_sg_attachment
*attach
;
384 struct scatterlist
*rd
, *wr
;
385 struct sg_table
*sgt
;
386 struct vb2_dma_sg_buf
*buf
= dbuf
->priv
;
389 attach
= kzalloc(sizeof(*attach
), GFP_KERNEL
);
394 /* Copy the buf->base_sgt scatter list to the attachment, as we can't
395 * map the same scatter list to multiple attachments at the same time.
397 ret
= sg_alloc_table(sgt
, buf
->dma_sgt
->orig_nents
, GFP_KERNEL
);
403 rd
= buf
->dma_sgt
->sgl
;
405 for (i
= 0; i
< sgt
->orig_nents
; ++i
) {
406 sg_set_page(wr
, sg_page(rd
), rd
->length
, rd
->offset
);
411 attach
->dma_dir
= DMA_NONE
;
412 dbuf_attach
->priv
= attach
;
417 static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf
*dbuf
,
418 struct dma_buf_attachment
*db_attach
)
420 struct vb2_dma_sg_attachment
*attach
= db_attach
->priv
;
421 struct sg_table
*sgt
;
428 /* release the scatterlist cache */
429 if (attach
->dma_dir
!= DMA_NONE
)
430 dma_unmap_sg(db_attach
->dev
, sgt
->sgl
, sgt
->orig_nents
,
434 db_attach
->priv
= NULL
;
437 static struct sg_table
*vb2_dma_sg_dmabuf_ops_map(
438 struct dma_buf_attachment
*db_attach
, enum dma_data_direction dma_dir
)
440 struct vb2_dma_sg_attachment
*attach
= db_attach
->priv
;
441 /* stealing dmabuf mutex to serialize map/unmap operations */
442 struct mutex
*lock
= &db_attach
->dmabuf
->lock
;
443 struct sg_table
*sgt
;
448 /* return previously mapped sg table */
449 if (attach
->dma_dir
== dma_dir
) {
454 /* release any previous cache */
455 if (attach
->dma_dir
!= DMA_NONE
) {
456 dma_unmap_sg(db_attach
->dev
, sgt
->sgl
, sgt
->orig_nents
,
458 attach
->dma_dir
= DMA_NONE
;
461 /* mapping to the client with new direction */
462 sgt
->nents
= dma_map_sg(db_attach
->dev
, sgt
->sgl
, sgt
->orig_nents
,
465 pr_err("failed to map scatterlist\n");
467 return ERR_PTR(-EIO
);
470 attach
->dma_dir
= dma_dir
;
477 static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment
*db_attach
,
478 struct sg_table
*sgt
, enum dma_data_direction dma_dir
)
480 /* nothing to be done here */
483 static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf
*dbuf
)
485 /* drop reference obtained in vb2_dma_sg_get_dmabuf */
486 vb2_dma_sg_put(dbuf
->priv
);
489 static void *vb2_dma_sg_dmabuf_ops_kmap(struct dma_buf
*dbuf
, unsigned long pgnum
)
491 struct vb2_dma_sg_buf
*buf
= dbuf
->priv
;
493 return buf
->vaddr
? buf
->vaddr
+ pgnum
* PAGE_SIZE
: NULL
;
496 static void *vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf
*dbuf
)
498 struct vb2_dma_sg_buf
*buf
= dbuf
->priv
;
500 return vb2_dma_sg_vaddr(buf
);
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 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 .kmap
= vb2_dma_sg_dmabuf_ops_kmap
,
515 .kmap_atomic
= vb2_dma_sg_dmabuf_ops_kmap
,
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(void *buf_priv
, unsigned long flags
)
523 struct vb2_dma_sg_buf
*buf
= buf_priv
;
524 struct dma_buf
*dbuf
;
525 DEFINE_DMA_BUF_EXPORT_INFO(exp_info
);
527 exp_info
.ops
= &vb2_dma_sg_dmabuf_ops
;
528 exp_info
.size
= buf
->size
;
529 exp_info
.flags
= flags
;
532 if (WARN_ON(!buf
->dma_sgt
))
535 dbuf
= dma_buf_export(&exp_info
);
539 /* dmabuf keeps reference to vb2 buffer */
540 atomic_inc(&buf
->refcount
);
545 /*********************************************/
546 /* callbacks for DMABUF buffers */
547 /*********************************************/
549 static int vb2_dma_sg_map_dmabuf(void *mem_priv
)
551 struct vb2_dma_sg_buf
*buf
= mem_priv
;
552 struct sg_table
*sgt
;
554 if (WARN_ON(!buf
->db_attach
)) {
555 pr_err("trying to pin a non attached buffer\n");
559 if (WARN_ON(buf
->dma_sgt
)) {
560 pr_err("dmabuf buffer is already pinned\n");
564 /* get the associated scatterlist for this buffer */
565 sgt
= dma_buf_map_attachment(buf
->db_attach
, buf
->dma_dir
);
567 pr_err("Error getting dmabuf scatterlist\n");
577 static void vb2_dma_sg_unmap_dmabuf(void *mem_priv
)
579 struct vb2_dma_sg_buf
*buf
= mem_priv
;
580 struct sg_table
*sgt
= buf
->dma_sgt
;
582 if (WARN_ON(!buf
->db_attach
)) {
583 pr_err("trying to unpin a not attached buffer\n");
588 pr_err("dmabuf buffer is already unpinned\n");
593 dma_buf_vunmap(buf
->db_attach
->dmabuf
, buf
->vaddr
);
596 dma_buf_unmap_attachment(buf
->db_attach
, sgt
, buf
->dma_dir
);
601 static void vb2_dma_sg_detach_dmabuf(void *mem_priv
)
603 struct vb2_dma_sg_buf
*buf
= mem_priv
;
605 /* if vb2 works correctly you should never detach mapped buffer */
606 if (WARN_ON(buf
->dma_sgt
))
607 vb2_dma_sg_unmap_dmabuf(buf
);
609 /* detach this attachment */
610 dma_buf_detach(buf
->db_attach
->dmabuf
, buf
->db_attach
);
614 static void *vb2_dma_sg_attach_dmabuf(struct device
*dev
, struct dma_buf
*dbuf
,
615 unsigned long size
, enum dma_data_direction dma_dir
)
617 struct vb2_dma_sg_buf
*buf
;
618 struct dma_buf_attachment
*dba
;
620 if (dbuf
->size
< size
)
621 return ERR_PTR(-EFAULT
);
623 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
625 return ERR_PTR(-ENOMEM
);
628 /* create attachment for the dmabuf with the user device */
629 dba
= dma_buf_attach(dbuf
, buf
->dev
);
631 pr_err("failed to attach dmabuf\n");
636 buf
->dma_dir
= dma_dir
;
638 buf
->db_attach
= dba
;
643 static void *vb2_dma_sg_cookie(void *buf_priv
)
645 struct vb2_dma_sg_buf
*buf
= buf_priv
;
650 const struct vb2_mem_ops vb2_dma_sg_memops
= {
651 .alloc
= vb2_dma_sg_alloc
,
652 .put
= vb2_dma_sg_put
,
653 .get_userptr
= vb2_dma_sg_get_userptr
,
654 .put_userptr
= vb2_dma_sg_put_userptr
,
655 .prepare
= vb2_dma_sg_prepare
,
656 .finish
= vb2_dma_sg_finish
,
657 .vaddr
= vb2_dma_sg_vaddr
,
658 .mmap
= vb2_dma_sg_mmap
,
659 .num_users
= vb2_dma_sg_num_users
,
660 .get_dmabuf
= vb2_dma_sg_get_dmabuf
,
661 .map_dmabuf
= vb2_dma_sg_map_dmabuf
,
662 .unmap_dmabuf
= vb2_dma_sg_unmap_dmabuf
,
663 .attach_dmabuf
= vb2_dma_sg_attach_dmabuf
,
664 .detach_dmabuf
= vb2_dma_sg_detach_dmabuf
,
665 .cookie
= vb2_dma_sg_cookie
,
667 EXPORT_SYMBOL_GPL(vb2_dma_sg_memops
);
669 MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
670 MODULE_AUTHOR("Andrzej Pietrasiewicz");
671 MODULE_LICENSE("GPL");