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;
108 return ERR_PTR(-EINVAL);
110 buf = kzalloc(sizeof *buf, GFP_KERNEL);
112 return ERR_PTR(-ENOMEM);
115 buf->dma_dir = dma_dir;
118 /* size is already page aligned */
119 buf->num_pages = size >> PAGE_SHIFT;
120 buf->dma_sgt = &buf->sg_table;
122 buf->pages = kzalloc(buf->num_pages * sizeof(struct page *),
125 goto fail_pages_array_alloc;
127 ret = vb2_dma_sg_alloc_compacted(buf, gfp_flags);
129 goto fail_pages_alloc;
131 ret = sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
132 buf->num_pages, 0, size, GFP_KERNEL);
134 goto fail_table_alloc;
136 /* Prevent the device from being released while the buffer is used */
137 buf->dev = get_device(dev);
139 sgt = &buf->sg_table;
141 * No need to sync to the device, this will happen later when the
142 * prepare() memop is called.
144 sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
145 buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
149 buf->handler.refcount = &buf->refcount;
150 buf->handler.put = vb2_dma_sg_put;
151 buf->handler.arg = buf;
153 atomic_inc(&buf->refcount);
155 dprintk(1, "%s: Allocated buffer of %d pages\n",
156 __func__, buf->num_pages);
160 put_device(buf->dev);
161 sg_free_table(buf->dma_sgt);
163 num_pages = buf->num_pages;
165 __free_page(buf->pages[num_pages]);
168 fail_pages_array_alloc:
170 return ERR_PTR(-ENOMEM);
173 static void vb2_dma_sg_put(void *buf_priv)
175 struct vb2_dma_sg_buf *buf = buf_priv;
176 struct sg_table *sgt = &buf->sg_table;
177 int i = buf->num_pages;
179 if (atomic_dec_and_test(&buf->refcount)) {
180 dprintk(1, "%s: Freeing buffer of %d pages\n", __func__,
182 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
183 buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
185 vm_unmap_ram(buf->vaddr, buf->num_pages);
186 sg_free_table(buf->dma_sgt);
188 __free_page(buf->pages[i]);
190 put_device(buf->dev);
195 static void vb2_dma_sg_prepare(void *buf_priv)
197 struct vb2_dma_sg_buf *buf = buf_priv;
198 struct sg_table *sgt = buf->dma_sgt;
200 /* DMABUF exporter will flush the cache for us */
204 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->orig_nents,
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 /* DMABUF exporter will flush the cache for us */
217 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
220 static void *vb2_dma_sg_get_userptr(struct device *dev, unsigned long vaddr,
222 enum dma_data_direction dma_dir)
224 struct vb2_dma_sg_buf *buf;
225 struct sg_table *sgt;
226 struct frame_vector *vec;
229 return ERR_PTR(-EINVAL);
231 buf = kzalloc(sizeof *buf, GFP_KERNEL);
233 return ERR_PTR(-ENOMEM);
237 buf->dma_dir = dma_dir;
238 buf->offset = vaddr & ~PAGE_MASK;
240 buf->dma_sgt = &buf->sg_table;
241 vec = vb2_create_framevec(vaddr, size, buf->dma_dir == DMA_FROM_DEVICE);
243 goto userptr_fail_pfnvec;
246 buf->pages = frame_vector_pages(vec);
247 if (IS_ERR(buf->pages))
248 goto userptr_fail_sgtable;
249 buf->num_pages = frame_vector_count(vec);
251 if (sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
252 buf->num_pages, buf->offset, size, 0))
253 goto userptr_fail_sgtable;
255 sgt = &buf->sg_table;
257 * No need to sync to the device, this will happen later when the
258 * prepare() memop is called.
260 sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
261 buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
263 goto userptr_fail_map;
268 sg_free_table(&buf->sg_table);
269 userptr_fail_sgtable:
270 vb2_destroy_framevec(vec);
273 return ERR_PTR(-ENOMEM);
277 * @put_userptr: inform the allocator that a USERPTR buffer will no longer
280 static void vb2_dma_sg_put_userptr(void *buf_priv)
282 struct vb2_dma_sg_buf *buf = buf_priv;
283 struct sg_table *sgt = &buf->sg_table;
284 int i = buf->num_pages;
286 dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
287 __func__, buf->num_pages);
288 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir,
289 DMA_ATTR_SKIP_CPU_SYNC);
291 vm_unmap_ram(buf->vaddr, buf->num_pages);
292 sg_free_table(buf->dma_sgt);
294 if (buf->dma_dir == DMA_FROM_DEVICE)
295 set_page_dirty_lock(buf->pages[i]);
297 vb2_destroy_framevec(buf->vec);
301 static void *vb2_dma_sg_vaddr(void *buf_priv)
303 struct vb2_dma_sg_buf *buf = buf_priv;
309 buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
311 buf->vaddr = vm_map_ram(buf->pages,
312 buf->num_pages, -1, PAGE_KERNEL);
315 /* add offset in case userptr is not page-aligned */
316 return buf->vaddr ? buf->vaddr + buf->offset : NULL;
319 static unsigned int vb2_dma_sg_num_users(void *buf_priv)
321 struct vb2_dma_sg_buf *buf = buf_priv;
323 return atomic_read(&buf->refcount);
326 static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
328 struct vb2_dma_sg_buf *buf = buf_priv;
329 unsigned long uaddr = vma->vm_start;
330 unsigned long usize = vma->vm_end - vma->vm_start;
334 printk(KERN_ERR "No memory to map\n");
341 ret = vm_insert_page(vma, uaddr, buf->pages[i++]);
343 printk(KERN_ERR "Remapping memory, error: %d\n", ret);
353 * Use common vm_area operations to track buffer refcount.
355 vma->vm_private_data = &buf->handler;
356 vma->vm_ops = &vb2_common_vm_ops;
358 vma->vm_ops->open(vma);
363 /*********************************************/
364 /* DMABUF ops for exporters */
365 /*********************************************/
367 struct vb2_dma_sg_attachment {
369 enum dma_data_direction dma_dir;
372 static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
373 struct dma_buf_attachment *dbuf_attach)
375 struct vb2_dma_sg_attachment *attach;
377 struct scatterlist *rd, *wr;
378 struct sg_table *sgt;
379 struct vb2_dma_sg_buf *buf = dbuf->priv;
382 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
387 /* Copy the buf->base_sgt scatter list to the attachment, as we can't
388 * map the same scatter list to multiple attachments at the same time.
390 ret = sg_alloc_table(sgt, buf->dma_sgt->orig_nents, GFP_KERNEL);
396 rd = buf->dma_sgt->sgl;
398 for (i = 0; i < sgt->orig_nents; ++i) {
399 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
404 attach->dma_dir = DMA_NONE;
405 dbuf_attach->priv = attach;
410 static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf,
411 struct dma_buf_attachment *db_attach)
413 struct vb2_dma_sg_attachment *attach = db_attach->priv;
414 struct sg_table *sgt;
421 /* release the scatterlist cache */
422 if (attach->dma_dir != DMA_NONE)
423 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
427 db_attach->priv = NULL;
430 static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
431 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
433 struct vb2_dma_sg_attachment *attach = db_attach->priv;
434 /* stealing dmabuf mutex to serialize map/unmap operations */
435 struct mutex *lock = &db_attach->dmabuf->lock;
436 struct sg_table *sgt;
441 /* return previously mapped sg table */
442 if (attach->dma_dir == dma_dir) {
447 /* release any previous cache */
448 if (attach->dma_dir != DMA_NONE) {
449 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
451 attach->dma_dir = DMA_NONE;
454 /* mapping to the client with new direction */
455 sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
458 pr_err("failed to map scatterlist\n");
460 return ERR_PTR(-EIO);
463 attach->dma_dir = dma_dir;
470 static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
471 struct sg_table *sgt, enum dma_data_direction dma_dir)
473 /* nothing to be done here */
476 static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf *dbuf)
478 /* drop reference obtained in vb2_dma_sg_get_dmabuf */
479 vb2_dma_sg_put(dbuf->priv);
482 static void *vb2_dma_sg_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
484 struct vb2_dma_sg_buf *buf = dbuf->priv;
486 return buf->vaddr ? buf->vaddr + pgnum * PAGE_SIZE : NULL;
489 static void *vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf *dbuf)
491 struct vb2_dma_sg_buf *buf = dbuf->priv;
493 return vb2_dma_sg_vaddr(buf);
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 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 .kmap = vb2_dma_sg_dmabuf_ops_kmap,
508 .kmap_atomic = vb2_dma_sg_dmabuf_ops_kmap,
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 atomic_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;
575 if (WARN_ON(!buf->db_attach)) {
576 pr_err("trying to unpin a not attached buffer\n");
581 pr_err("dmabuf buffer is already unpinned\n");
586 dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
589 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
594 static void vb2_dma_sg_detach_dmabuf(void *mem_priv)
596 struct vb2_dma_sg_buf *buf = mem_priv;
598 /* if vb2 works correctly you should never detach mapped buffer */
599 if (WARN_ON(buf->dma_sgt))
600 vb2_dma_sg_unmap_dmabuf(buf);
602 /* detach this attachment */
603 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
607 static void *vb2_dma_sg_attach_dmabuf(struct device *dev, struct dma_buf *dbuf,
608 unsigned long size, enum dma_data_direction dma_dir)
610 struct vb2_dma_sg_buf *buf;
611 struct dma_buf_attachment *dba;
614 return ERR_PTR(-EINVAL);
616 if (dbuf->size < size)
617 return ERR_PTR(-EFAULT);
619 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
621 return ERR_PTR(-ENOMEM);
624 /* create attachment for the dmabuf with the user device */
625 dba = dma_buf_attach(dbuf, buf->dev);
627 pr_err("failed to attach dmabuf\n");
632 buf->dma_dir = dma_dir;
634 buf->db_attach = dba;
639 static void *vb2_dma_sg_cookie(void *buf_priv)
641 struct vb2_dma_sg_buf *buf = buf_priv;
646 const struct vb2_mem_ops vb2_dma_sg_memops = {
647 .alloc = vb2_dma_sg_alloc,
648 .put = vb2_dma_sg_put,
649 .get_userptr = vb2_dma_sg_get_userptr,
650 .put_userptr = vb2_dma_sg_put_userptr,
651 .prepare = vb2_dma_sg_prepare,
652 .finish = vb2_dma_sg_finish,
653 .vaddr = vb2_dma_sg_vaddr,
654 .mmap = vb2_dma_sg_mmap,
655 .num_users = vb2_dma_sg_num_users,
656 .get_dmabuf = vb2_dma_sg_get_dmabuf,
657 .map_dmabuf = vb2_dma_sg_map_dmabuf,
658 .unmap_dmabuf = vb2_dma_sg_unmap_dmabuf,
659 .attach_dmabuf = vb2_dma_sg_attach_dmabuf,
660 .detach_dmabuf = vb2_dma_sg_detach_dmabuf,
661 .cookie = vb2_dma_sg_cookie,
663 EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
665 MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
666 MODULE_AUTHOR("Andrzej Pietrasiewicz");
667 MODULE_LICENSE("GPL");