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);
246 goto userptr_fail_pfnvec;
249 buf->pages = frame_vector_pages(vec);
250 if (IS_ERR(buf->pages))
251 goto userptr_fail_sgtable;
252 buf->num_pages = frame_vector_count(vec);
254 if (sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
255 buf->num_pages, buf->offset, size, 0))
256 goto userptr_fail_sgtable;
258 sgt = &buf->sg_table;
260 * No need to sync to the device, this will happen later when the
261 * prepare() memop is called.
263 if (dma_map_sgtable(buf->dev, sgt, buf->dma_dir,
264 DMA_ATTR_SKIP_CPU_SYNC))
265 goto userptr_fail_map;
270 sg_free_table(&buf->sg_table);
271 userptr_fail_sgtable:
272 vb2_destroy_framevec(vec);
275 return ERR_PTR(-ENOMEM);
279 * @put_userptr: inform the allocator that a USERPTR buffer will no longer
282 static void vb2_dma_sg_put_userptr(void *buf_priv)
284 struct vb2_dma_sg_buf *buf = buf_priv;
285 struct sg_table *sgt = &buf->sg_table;
286 int i = buf->num_pages;
288 dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
289 __func__, buf->num_pages);
290 dma_unmap_sgtable(buf->dev, sgt, buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
292 vm_unmap_ram(buf->vaddr, buf->num_pages);
293 sg_free_table(buf->dma_sgt);
294 if (buf->dma_dir == DMA_FROM_DEVICE ||
295 buf->dma_dir == DMA_BIDIRECTIONAL)
297 set_page_dirty_lock(buf->pages[i]);
298 vb2_destroy_framevec(buf->vec);
302 static void *vb2_dma_sg_vaddr(struct vb2_buffer *vb, void *buf_priv)
304 struct vb2_dma_sg_buf *buf = buf_priv;
305 struct iosys_map map;
311 if (buf->db_attach) {
312 ret = dma_buf_vmap(buf->db_attach->dmabuf, &map);
313 buf->vaddr = ret ? NULL : map.vaddr;
315 buf->vaddr = vm_map_ram(buf->pages, buf->num_pages, -1);
319 /* add offset in case userptr is not page-aligned */
320 return buf->vaddr ? buf->vaddr + buf->offset : NULL;
323 static unsigned int vb2_dma_sg_num_users(void *buf_priv)
325 struct vb2_dma_sg_buf *buf = buf_priv;
327 return refcount_read(&buf->refcount);
330 static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
332 struct vb2_dma_sg_buf *buf = buf_priv;
336 printk(KERN_ERR "No memory to map\n");
340 err = vm_map_pages(vma, buf->pages, buf->num_pages);
342 printk(KERN_ERR "Remapping memory, error: %d\n", err);
347 * Use common vm_area operations to track buffer refcount.
349 vma->vm_private_data = &buf->handler;
350 vma->vm_ops = &vb2_common_vm_ops;
352 vma->vm_ops->open(vma);
357 /*********************************************/
358 /* DMABUF ops for exporters */
359 /*********************************************/
361 struct vb2_dma_sg_attachment {
363 enum dma_data_direction dma_dir;
366 static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf *dbuf,
367 struct dma_buf_attachment *dbuf_attach)
369 struct vb2_dma_sg_attachment *attach;
371 struct scatterlist *rd, *wr;
372 struct sg_table *sgt;
373 struct vb2_dma_sg_buf *buf = dbuf->priv;
376 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
381 /* Copy the buf->base_sgt scatter list to the attachment, as we can't
382 * map the same scatter list to multiple attachments at the same time.
384 ret = sg_alloc_table(sgt, buf->dma_sgt->orig_nents, GFP_KERNEL);
390 rd = buf->dma_sgt->sgl;
392 for (i = 0; i < sgt->orig_nents; ++i) {
393 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
398 attach->dma_dir = DMA_NONE;
399 dbuf_attach->priv = attach;
404 static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf,
405 struct dma_buf_attachment *db_attach)
407 struct vb2_dma_sg_attachment *attach = db_attach->priv;
408 struct sg_table *sgt;
415 /* release the scatterlist cache */
416 if (attach->dma_dir != DMA_NONE)
417 dma_unmap_sgtable(db_attach->dev, sgt, attach->dma_dir, 0);
420 db_attach->priv = NULL;
423 static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
424 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
426 struct vb2_dma_sg_attachment *attach = db_attach->priv;
427 /* stealing dmabuf mutex to serialize map/unmap operations */
428 struct mutex *lock = &db_attach->dmabuf->lock;
429 struct sg_table *sgt;
434 /* return previously mapped sg table */
435 if (attach->dma_dir == dma_dir) {
440 /* release any previous cache */
441 if (attach->dma_dir != DMA_NONE) {
442 dma_unmap_sgtable(db_attach->dev, sgt, attach->dma_dir, 0);
443 attach->dma_dir = DMA_NONE;
446 /* mapping to the client with new direction */
447 if (dma_map_sgtable(db_attach->dev, sgt, dma_dir, 0)) {
448 pr_err("failed to map scatterlist\n");
450 return ERR_PTR(-EIO);
453 attach->dma_dir = dma_dir;
460 static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
461 struct sg_table *sgt, enum dma_data_direction dma_dir)
463 /* nothing to be done here */
466 static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf *dbuf)
468 /* drop reference obtained in vb2_dma_sg_get_dmabuf */
469 vb2_dma_sg_put(dbuf->priv);
473 vb2_dma_sg_dmabuf_ops_begin_cpu_access(struct dma_buf *dbuf,
474 enum dma_data_direction direction)
476 struct vb2_dma_sg_buf *buf = dbuf->priv;
477 struct sg_table *sgt = buf->dma_sgt;
479 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
484 vb2_dma_sg_dmabuf_ops_end_cpu_access(struct dma_buf *dbuf,
485 enum dma_data_direction direction)
487 struct vb2_dma_sg_buf *buf = dbuf->priv;
488 struct sg_table *sgt = buf->dma_sgt;
490 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
494 static int vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf *dbuf,
495 struct iosys_map *map)
497 struct vb2_dma_sg_buf *buf = dbuf->priv;
499 iosys_map_set_vaddr(map, buf->vaddr);
504 static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf *dbuf,
505 struct vm_area_struct *vma)
507 return vb2_dma_sg_mmap(dbuf->priv, vma);
510 static const struct dma_buf_ops vb2_dma_sg_dmabuf_ops = {
511 .attach = vb2_dma_sg_dmabuf_ops_attach,
512 .detach = vb2_dma_sg_dmabuf_ops_detach,
513 .map_dma_buf = vb2_dma_sg_dmabuf_ops_map,
514 .unmap_dma_buf = vb2_dma_sg_dmabuf_ops_unmap,
515 .begin_cpu_access = vb2_dma_sg_dmabuf_ops_begin_cpu_access,
516 .end_cpu_access = vb2_dma_sg_dmabuf_ops_end_cpu_access,
517 .vmap = vb2_dma_sg_dmabuf_ops_vmap,
518 .mmap = vb2_dma_sg_dmabuf_ops_mmap,
519 .release = vb2_dma_sg_dmabuf_ops_release,
522 static struct dma_buf *vb2_dma_sg_get_dmabuf(struct vb2_buffer *vb,
526 struct vb2_dma_sg_buf *buf = buf_priv;
527 struct dma_buf *dbuf;
528 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
530 exp_info.ops = &vb2_dma_sg_dmabuf_ops;
531 exp_info.size = buf->size;
532 exp_info.flags = flags;
535 if (WARN_ON(!buf->dma_sgt))
538 dbuf = dma_buf_export(&exp_info);
542 /* dmabuf keeps reference to vb2 buffer */
543 refcount_inc(&buf->refcount);
548 /*********************************************/
549 /* callbacks for DMABUF buffers */
550 /*********************************************/
552 static int vb2_dma_sg_map_dmabuf(void *mem_priv)
554 struct vb2_dma_sg_buf *buf = mem_priv;
555 struct sg_table *sgt;
557 if (WARN_ON(!buf->db_attach)) {
558 pr_err("trying to pin a non attached buffer\n");
562 if (WARN_ON(buf->dma_sgt)) {
563 pr_err("dmabuf buffer is already pinned\n");
567 /* get the associated scatterlist for this buffer */
568 sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
570 pr_err("Error getting dmabuf scatterlist\n");
580 static void vb2_dma_sg_unmap_dmabuf(void *mem_priv)
582 struct vb2_dma_sg_buf *buf = mem_priv;
583 struct sg_table *sgt = buf->dma_sgt;
584 struct iosys_map map = IOSYS_MAP_INIT_VADDR(buf->vaddr);
586 if (WARN_ON(!buf->db_attach)) {
587 pr_err("trying to unpin a not attached buffer\n");
592 pr_err("dmabuf buffer is already unpinned\n");
597 dma_buf_vunmap(buf->db_attach->dmabuf, &map);
600 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
605 static void vb2_dma_sg_detach_dmabuf(void *mem_priv)
607 struct vb2_dma_sg_buf *buf = mem_priv;
609 /* if vb2 works correctly you should never detach mapped buffer */
610 if (WARN_ON(buf->dma_sgt))
611 vb2_dma_sg_unmap_dmabuf(buf);
613 /* detach this attachment */
614 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
618 static void *vb2_dma_sg_attach_dmabuf(struct vb2_buffer *vb, struct device *dev,
619 struct dma_buf *dbuf, unsigned long size)
621 struct vb2_dma_sg_buf *buf;
622 struct dma_buf_attachment *dba;
625 return ERR_PTR(-EINVAL);
627 if (dbuf->size < size)
628 return ERR_PTR(-EFAULT);
630 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
632 return ERR_PTR(-ENOMEM);
635 /* create attachment for the dmabuf with the user device */
636 dba = dma_buf_attach(dbuf, buf->dev);
638 pr_err("failed to attach dmabuf\n");
643 buf->dma_dir = vb->vb2_queue->dma_dir;
645 buf->db_attach = dba;
651 static void *vb2_dma_sg_cookie(struct vb2_buffer *vb, void *buf_priv)
653 struct vb2_dma_sg_buf *buf = buf_priv;
658 const struct vb2_mem_ops vb2_dma_sg_memops = {
659 .alloc = vb2_dma_sg_alloc,
660 .put = vb2_dma_sg_put,
661 .get_userptr = vb2_dma_sg_get_userptr,
662 .put_userptr = vb2_dma_sg_put_userptr,
663 .prepare = vb2_dma_sg_prepare,
664 .finish = vb2_dma_sg_finish,
665 .vaddr = vb2_dma_sg_vaddr,
666 .mmap = vb2_dma_sg_mmap,
667 .num_users = vb2_dma_sg_num_users,
668 .get_dmabuf = vb2_dma_sg_get_dmabuf,
669 .map_dmabuf = vb2_dma_sg_map_dmabuf,
670 .unmap_dmabuf = vb2_dma_sg_unmap_dmabuf,
671 .attach_dmabuf = vb2_dma_sg_attach_dmabuf,
672 .detach_dmabuf = vb2_dma_sg_detach_dmabuf,
673 .cookie = vb2_dma_sg_cookie,
675 EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
677 MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
678 MODULE_AUTHOR("Andrzej Pietrasiewicz");
679 MODULE_LICENSE("GPL");
680 MODULE_IMPORT_NS(DMA_BUF);