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
, unsigned long dma_attrs
,
99 unsigned long size
, enum dma_data_direction dma_dir
,
102 struct vb2_dma_sg_buf
*buf
;
103 struct sg_table
*sgt
;
107 if (WARN_ON(dev
== NULL
))
109 buf
= kzalloc(sizeof *buf
, GFP_KERNEL
);
114 buf
->dma_dir
= dma_dir
;
117 /* size is already page aligned */
118 buf
->num_pages
= size
>> PAGE_SHIFT
;
119 buf
->dma_sgt
= &buf
->sg_table
;
121 buf
->pages
= kzalloc(buf
->num_pages
* sizeof(struct page
*),
124 goto fail_pages_array_alloc
;
126 ret
= vb2_dma_sg_alloc_compacted(buf
, gfp_flags
);
128 goto fail_pages_alloc
;
130 ret
= sg_alloc_table_from_pages(buf
->dma_sgt
, buf
->pages
,
131 buf
->num_pages
, 0, size
, GFP_KERNEL
);
133 goto fail_table_alloc
;
135 /* Prevent the device from being released while the buffer is used */
136 buf
->dev
= get_device(dev
);
138 sgt
= &buf
->sg_table
;
140 * No need to sync to the device, this will happen later when the
141 * prepare() memop is called.
143 sgt
->nents
= dma_map_sg_attrs(buf
->dev
, sgt
->sgl
, sgt
->orig_nents
,
144 buf
->dma_dir
, DMA_ATTR_SKIP_CPU_SYNC
);
148 buf
->handler
.refcount
= &buf
->refcount
;
149 buf
->handler
.put
= vb2_dma_sg_put
;
150 buf
->handler
.arg
= buf
;
152 atomic_inc(&buf
->refcount
);
154 dprintk(1, "%s: Allocated buffer of %d pages\n",
155 __func__
, buf
->num_pages
);
159 put_device(buf
->dev
);
160 sg_free_table(buf
->dma_sgt
);
162 num_pages
= buf
->num_pages
;
164 __free_page(buf
->pages
[num_pages
]);
167 fail_pages_array_alloc
:
172 static void vb2_dma_sg_put(void *buf_priv
)
174 struct vb2_dma_sg_buf
*buf
= buf_priv
;
175 struct sg_table
*sgt
= &buf
->sg_table
;
176 int i
= buf
->num_pages
;
178 if (atomic_dec_and_test(&buf
->refcount
)) {
179 dprintk(1, "%s: Freeing buffer of %d pages\n", __func__
,
181 dma_unmap_sg_attrs(buf
->dev
, sgt
->sgl
, sgt
->orig_nents
,
182 buf
->dma_dir
, DMA_ATTR_SKIP_CPU_SYNC
);
184 vm_unmap_ram(buf
->vaddr
, buf
->num_pages
);
185 sg_free_table(buf
->dma_sgt
);
187 __free_page(buf
->pages
[i
]);
189 put_device(buf
->dev
);
194 static void vb2_dma_sg_prepare(void *buf_priv
)
196 struct vb2_dma_sg_buf
*buf
= buf_priv
;
197 struct sg_table
*sgt
= buf
->dma_sgt
;
199 /* DMABUF exporter will flush the cache for us */
203 dma_sync_sg_for_device(buf
->dev
, sgt
->sgl
, sgt
->orig_nents
,
207 static void vb2_dma_sg_finish(void *buf_priv
)
209 struct vb2_dma_sg_buf
*buf
= buf_priv
;
210 struct sg_table
*sgt
= buf
->dma_sgt
;
212 /* DMABUF exporter will flush the cache for us */
216 dma_sync_sg_for_cpu(buf
->dev
, sgt
->sgl
, sgt
->orig_nents
, buf
->dma_dir
);
219 static void *vb2_dma_sg_get_userptr(struct device
*dev
, unsigned long vaddr
,
221 enum dma_data_direction dma_dir
)
223 struct vb2_dma_sg_buf
*buf
;
224 struct sg_table
*sgt
;
225 struct frame_vector
*vec
;
227 buf
= kzalloc(sizeof *buf
, GFP_KERNEL
);
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
, buf
->dma_dir
== DMA_FROM_DEVICE
);
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 sgt
->nents
= dma_map_sg_attrs(buf
->dev
, sgt
->sgl
, sgt
->orig_nents
,
257 buf
->dma_dir
, DMA_ATTR_SKIP_CPU_SYNC
);
259 goto userptr_fail_map
;
264 sg_free_table(&buf
->sg_table
);
265 userptr_fail_sgtable
:
266 vb2_destroy_framevec(vec
);
273 * @put_userptr: inform the allocator that a USERPTR buffer will no longer
276 static void vb2_dma_sg_put_userptr(void *buf_priv
)
278 struct vb2_dma_sg_buf
*buf
= buf_priv
;
279 struct sg_table
*sgt
= &buf
->sg_table
;
280 int i
= buf
->num_pages
;
282 dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
283 __func__
, buf
->num_pages
);
284 dma_unmap_sg_attrs(buf
->dev
, sgt
->sgl
, sgt
->orig_nents
, buf
->dma_dir
,
285 DMA_ATTR_SKIP_CPU_SYNC
);
287 vm_unmap_ram(buf
->vaddr
, buf
->num_pages
);
288 sg_free_table(buf
->dma_sgt
);
290 if (buf
->dma_dir
== DMA_FROM_DEVICE
)
291 set_page_dirty_lock(buf
->pages
[i
]);
293 vb2_destroy_framevec(buf
->vec
);
297 static void *vb2_dma_sg_vaddr(void *buf_priv
)
299 struct vb2_dma_sg_buf
*buf
= buf_priv
;
305 buf
->vaddr
= dma_buf_vmap(buf
->db_attach
->dmabuf
);
307 buf
->vaddr
= vm_map_ram(buf
->pages
,
308 buf
->num_pages
, -1, PAGE_KERNEL
);
311 /* add offset in case userptr is not page-aligned */
312 return buf
->vaddr
? buf
->vaddr
+ buf
->offset
: NULL
;
315 static unsigned int vb2_dma_sg_num_users(void *buf_priv
)
317 struct vb2_dma_sg_buf
*buf
= buf_priv
;
319 return atomic_read(&buf
->refcount
);
322 static int vb2_dma_sg_mmap(void *buf_priv
, struct vm_area_struct
*vma
)
324 struct vb2_dma_sg_buf
*buf
= buf_priv
;
325 unsigned long uaddr
= vma
->vm_start
;
326 unsigned long usize
= vma
->vm_end
- vma
->vm_start
;
330 printk(KERN_ERR
"No memory to map\n");
337 ret
= vm_insert_page(vma
, uaddr
, buf
->pages
[i
++]);
339 printk(KERN_ERR
"Remapping memory, error: %d\n", ret
);
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
, struct device
*dev
,
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_sg(db_attach
->dev
, sgt
->sgl
, sgt
->orig_nents
,
423 db_attach
->priv
= NULL
;
426 static struct sg_table
*vb2_dma_sg_dmabuf_ops_map(
427 struct dma_buf_attachment
*db_attach
, enum dma_data_direction dma_dir
)
429 struct vb2_dma_sg_attachment
*attach
= db_attach
->priv
;
430 /* stealing dmabuf mutex to serialize map/unmap operations */
431 struct mutex
*lock
= &db_attach
->dmabuf
->lock
;
432 struct sg_table
*sgt
;
437 /* return previously mapped sg table */
438 if (attach
->dma_dir
== dma_dir
) {
443 /* release any previous cache */
444 if (attach
->dma_dir
!= DMA_NONE
) {
445 dma_unmap_sg(db_attach
->dev
, sgt
->sgl
, sgt
->orig_nents
,
447 attach
->dma_dir
= DMA_NONE
;
450 /* mapping to the client with new direction */
451 sgt
->nents
= dma_map_sg(db_attach
->dev
, sgt
->sgl
, sgt
->orig_nents
,
454 pr_err("failed to map scatterlist\n");
456 return ERR_PTR(-EIO
);
459 attach
->dma_dir
= dma_dir
;
466 static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment
*db_attach
,
467 struct sg_table
*sgt
, enum dma_data_direction dma_dir
)
469 /* nothing to be done here */
472 static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf
*dbuf
)
474 /* drop reference obtained in vb2_dma_sg_get_dmabuf */
475 vb2_dma_sg_put(dbuf
->priv
);
478 static void *vb2_dma_sg_dmabuf_ops_kmap(struct dma_buf
*dbuf
, unsigned long pgnum
)
480 struct vb2_dma_sg_buf
*buf
= dbuf
->priv
;
482 return buf
->vaddr
? buf
->vaddr
+ pgnum
* PAGE_SIZE
: NULL
;
485 static void *vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf
*dbuf
)
487 struct vb2_dma_sg_buf
*buf
= dbuf
->priv
;
489 return vb2_dma_sg_vaddr(buf
);
492 static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf
*dbuf
,
493 struct vm_area_struct
*vma
)
495 return vb2_dma_sg_mmap(dbuf
->priv
, vma
);
498 static struct dma_buf_ops vb2_dma_sg_dmabuf_ops
= {
499 .attach
= vb2_dma_sg_dmabuf_ops_attach
,
500 .detach
= vb2_dma_sg_dmabuf_ops_detach
,
501 .map_dma_buf
= vb2_dma_sg_dmabuf_ops_map
,
502 .unmap_dma_buf
= vb2_dma_sg_dmabuf_ops_unmap
,
503 .kmap
= vb2_dma_sg_dmabuf_ops_kmap
,
504 .kmap_atomic
= vb2_dma_sg_dmabuf_ops_kmap
,
505 .vmap
= vb2_dma_sg_dmabuf_ops_vmap
,
506 .mmap
= vb2_dma_sg_dmabuf_ops_mmap
,
507 .release
= vb2_dma_sg_dmabuf_ops_release
,
510 static struct dma_buf
*vb2_dma_sg_get_dmabuf(void *buf_priv
, unsigned long flags
)
512 struct vb2_dma_sg_buf
*buf
= buf_priv
;
513 struct dma_buf
*dbuf
;
514 DEFINE_DMA_BUF_EXPORT_INFO(exp_info
);
516 exp_info
.ops
= &vb2_dma_sg_dmabuf_ops
;
517 exp_info
.size
= buf
->size
;
518 exp_info
.flags
= flags
;
521 if (WARN_ON(!buf
->dma_sgt
))
524 dbuf
= dma_buf_export(&exp_info
);
528 /* dmabuf keeps reference to vb2 buffer */
529 atomic_inc(&buf
->refcount
);
534 /*********************************************/
535 /* callbacks for DMABUF buffers */
536 /*********************************************/
538 static int vb2_dma_sg_map_dmabuf(void *mem_priv
)
540 struct vb2_dma_sg_buf
*buf
= mem_priv
;
541 struct sg_table
*sgt
;
543 if (WARN_ON(!buf
->db_attach
)) {
544 pr_err("trying to pin a non attached buffer\n");
548 if (WARN_ON(buf
->dma_sgt
)) {
549 pr_err("dmabuf buffer is already pinned\n");
553 /* get the associated scatterlist for this buffer */
554 sgt
= dma_buf_map_attachment(buf
->db_attach
, buf
->dma_dir
);
556 pr_err("Error getting dmabuf scatterlist\n");
566 static void vb2_dma_sg_unmap_dmabuf(void *mem_priv
)
568 struct vb2_dma_sg_buf
*buf
= mem_priv
;
569 struct sg_table
*sgt
= buf
->dma_sgt
;
571 if (WARN_ON(!buf
->db_attach
)) {
572 pr_err("trying to unpin a not attached buffer\n");
577 pr_err("dmabuf buffer is already unpinned\n");
582 dma_buf_vunmap(buf
->db_attach
->dmabuf
, buf
->vaddr
);
585 dma_buf_unmap_attachment(buf
->db_attach
, sgt
, buf
->dma_dir
);
590 static void vb2_dma_sg_detach_dmabuf(void *mem_priv
)
592 struct vb2_dma_sg_buf
*buf
= mem_priv
;
594 /* if vb2 works correctly you should never detach mapped buffer */
595 if (WARN_ON(buf
->dma_sgt
))
596 vb2_dma_sg_unmap_dmabuf(buf
);
598 /* detach this attachment */
599 dma_buf_detach(buf
->db_attach
->dmabuf
, buf
->db_attach
);
603 static void *vb2_dma_sg_attach_dmabuf(struct device
*dev
, struct dma_buf
*dbuf
,
604 unsigned long size
, enum dma_data_direction dma_dir
)
606 struct vb2_dma_sg_buf
*buf
;
607 struct dma_buf_attachment
*dba
;
609 if (dbuf
->size
< size
)
610 return ERR_PTR(-EFAULT
);
612 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
614 return ERR_PTR(-ENOMEM
);
617 /* create attachment for the dmabuf with the user device */
618 dba
= dma_buf_attach(dbuf
, buf
->dev
);
620 pr_err("failed to attach dmabuf\n");
625 buf
->dma_dir
= dma_dir
;
627 buf
->db_attach
= dba
;
632 static void *vb2_dma_sg_cookie(void *buf_priv
)
634 struct vb2_dma_sg_buf
*buf
= buf_priv
;
639 const struct vb2_mem_ops vb2_dma_sg_memops
= {
640 .alloc
= vb2_dma_sg_alloc
,
641 .put
= vb2_dma_sg_put
,
642 .get_userptr
= vb2_dma_sg_get_userptr
,
643 .put_userptr
= vb2_dma_sg_put_userptr
,
644 .prepare
= vb2_dma_sg_prepare
,
645 .finish
= vb2_dma_sg_finish
,
646 .vaddr
= vb2_dma_sg_vaddr
,
647 .mmap
= vb2_dma_sg_mmap
,
648 .num_users
= vb2_dma_sg_num_users
,
649 .get_dmabuf
= vb2_dma_sg_get_dmabuf
,
650 .map_dmabuf
= vb2_dma_sg_map_dmabuf
,
651 .unmap_dmabuf
= vb2_dma_sg_unmap_dmabuf
,
652 .attach_dmabuf
= vb2_dma_sg_attach_dmabuf
,
653 .detach_dmabuf
= vb2_dma_sg_detach_dmabuf
,
654 .cookie
= vb2_dma_sg_cookie
,
656 EXPORT_SYMBOL_GPL(vb2_dma_sg_memops
);
658 MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
659 MODULE_AUTHOR("Andrzej Pietrasiewicz");
660 MODULE_LICENSE("GPL");