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/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;
69 order = get_order(size);
70 /* Dont 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;
123 buf->pages = kvmalloc_array(buf->num_pages, sizeof(struct page *),
124 GFP_KERNEL | __GFP_ZERO);
126 goto fail_pages_array_alloc;
128 ret = vb2_dma_sg_alloc_compacted(buf, gfp_flags);
130 goto fail_pages_alloc;
132 ret = sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
133 buf->num_pages, 0, size, GFP_KERNEL);
135 goto fail_table_alloc;
137 /* Prevent the device from being released while the buffer is used */
138 buf->dev = get_device(dev);
140 sgt = &buf->sg_table;
142 * No need to sync to the device, this will happen later when the
143 * prepare() memop is called.
145 sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
146 buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
150 buf->handler.refcount = &buf->refcount;
151 buf->handler.put = vb2_dma_sg_put;
152 buf->handler.arg = buf;
154 refcount_set(&buf->refcount, 1);
156 dprintk(1, "%s: Allocated buffer of %d pages\n",
157 __func__, buf->num_pages);
161 put_device(buf->dev);
162 sg_free_table(buf->dma_sgt);
164 num_pages = buf->num_pages;
166 __free_page(buf->pages[num_pages]);
169 fail_pages_array_alloc:
171 return ERR_PTR(-ENOMEM);
174 static void vb2_dma_sg_put(void *buf_priv)
176 struct vb2_dma_sg_buf *buf = buf_priv;
177 struct sg_table *sgt = &buf->sg_table;
178 int i = buf->num_pages;
180 if (refcount_dec_and_test(&buf->refcount)) {
181 dprintk(1, "%s: Freeing buffer of %d pages\n", __func__,
183 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
184 buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
186 vm_unmap_ram(buf->vaddr, buf->num_pages);
187 sg_free_table(buf->dma_sgt);
189 __free_page(buf->pages[i]);
191 put_device(buf->dev);
196 static void vb2_dma_sg_prepare(void *buf_priv)
198 struct vb2_dma_sg_buf *buf = buf_priv;
199 struct sg_table *sgt = buf->dma_sgt;
201 /* DMABUF exporter will flush the cache for us */
205 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->orig_nents,
209 static void vb2_dma_sg_finish(void *buf_priv)
211 struct vb2_dma_sg_buf *buf = buf_priv;
212 struct sg_table *sgt = buf->dma_sgt;
214 /* DMABUF exporter will flush the cache for us */
218 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
221 static void *vb2_dma_sg_get_userptr(struct device *dev, unsigned long vaddr,
223 enum dma_data_direction dma_dir)
225 struct vb2_dma_sg_buf *buf;
226 struct sg_table *sgt;
227 struct frame_vector *vec;
230 return ERR_PTR(-EINVAL);
232 buf = kzalloc(sizeof *buf, GFP_KERNEL);
234 return ERR_PTR(-ENOMEM);
238 buf->dma_dir = dma_dir;
239 buf->offset = vaddr & ~PAGE_MASK;
241 buf->dma_sgt = &buf->sg_table;
242 vec = vb2_create_framevec(vaddr, size, dma_dir == DMA_FROM_DEVICE ||
243 dma_dir == DMA_BIDIRECTIONAL);
245 goto userptr_fail_pfnvec;
248 buf->pages = frame_vector_pages(vec);
249 if (IS_ERR(buf->pages))
250 goto userptr_fail_sgtable;
251 buf->num_pages = frame_vector_count(vec);
253 if (sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
254 buf->num_pages, buf->offset, size, 0))
255 goto userptr_fail_sgtable;
257 sgt = &buf->sg_table;
259 * No need to sync to the device, this will happen later when the
260 * prepare() memop is called.
262 sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
263 buf->dma_dir, 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_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir,
291 DMA_ATTR_SKIP_CPU_SYNC);
293 vm_unmap_ram(buf->vaddr, buf->num_pages);
294 sg_free_table(buf->dma_sgt);
295 if (buf->dma_dir == DMA_FROM_DEVICE ||
296 buf->dma_dir == DMA_BIDIRECTIONAL)
298 set_page_dirty_lock(buf->pages[i]);
299 vb2_destroy_framevec(buf->vec);
303 static void *vb2_dma_sg_vaddr(void *buf_priv)
305 struct vb2_dma_sg_buf *buf = buf_priv;
311 buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
313 buf->vaddr = vm_map_ram(buf->pages,
314 buf->num_pages, -1, PAGE_KERNEL);
317 /* add offset in case userptr is not page-aligned */
318 return buf->vaddr ? buf->vaddr + buf->offset : NULL;
321 static unsigned int vb2_dma_sg_num_users(void *buf_priv)
323 struct vb2_dma_sg_buf *buf = buf_priv;
325 return refcount_read(&buf->refcount);
328 static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
330 struct vb2_dma_sg_buf *buf = buf_priv;
331 unsigned long uaddr = vma->vm_start;
332 unsigned long usize = vma->vm_end - vma->vm_start;
336 printk(KERN_ERR "No memory to map\n");
343 ret = vm_insert_page(vma, uaddr, buf->pages[i++]);
345 printk(KERN_ERR "Remapping memory, error: %d\n", ret);
355 * Use common vm_area operations to track buffer refcount.
357 vma->vm_private_data = &buf->handler;
358 vma->vm_ops = &vb2_common_vm_ops;
360 vma->vm_ops->open(vma);
365 /*********************************************/
366 /* DMABUF ops for exporters */
367 /*********************************************/
369 struct vb2_dma_sg_attachment {
371 enum dma_data_direction dma_dir;
374 static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
375 struct dma_buf_attachment *dbuf_attach)
377 struct vb2_dma_sg_attachment *attach;
379 struct scatterlist *rd, *wr;
380 struct sg_table *sgt;
381 struct vb2_dma_sg_buf *buf = dbuf->priv;
384 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
389 /* Copy the buf->base_sgt scatter list to the attachment, as we can't
390 * map the same scatter list to multiple attachments at the same time.
392 ret = sg_alloc_table(sgt, buf->dma_sgt->orig_nents, GFP_KERNEL);
398 rd = buf->dma_sgt->sgl;
400 for (i = 0; i < sgt->orig_nents; ++i) {
401 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
406 attach->dma_dir = DMA_NONE;
407 dbuf_attach->priv = attach;
412 static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf,
413 struct dma_buf_attachment *db_attach)
415 struct vb2_dma_sg_attachment *attach = db_attach->priv;
416 struct sg_table *sgt;
423 /* release the scatterlist cache */
424 if (attach->dma_dir != DMA_NONE)
425 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
429 db_attach->priv = NULL;
432 static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
433 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
435 struct vb2_dma_sg_attachment *attach = db_attach->priv;
436 /* stealing dmabuf mutex to serialize map/unmap operations */
437 struct mutex *lock = &db_attach->dmabuf->lock;
438 struct sg_table *sgt;
443 /* return previously mapped sg table */
444 if (attach->dma_dir == dma_dir) {
449 /* release any previous cache */
450 if (attach->dma_dir != DMA_NONE) {
451 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
453 attach->dma_dir = DMA_NONE;
456 /* mapping to the client with new direction */
457 sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
460 pr_err("failed to map scatterlist\n");
462 return ERR_PTR(-EIO);
465 attach->dma_dir = dma_dir;
472 static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
473 struct sg_table *sgt, enum dma_data_direction dma_dir)
475 /* nothing to be done here */
478 static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf *dbuf)
480 /* drop reference obtained in vb2_dma_sg_get_dmabuf */
481 vb2_dma_sg_put(dbuf->priv);
484 static void *vb2_dma_sg_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
486 struct vb2_dma_sg_buf *buf = dbuf->priv;
488 return buf->vaddr ? buf->vaddr + pgnum * PAGE_SIZE : NULL;
491 static void *vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf *dbuf)
493 struct vb2_dma_sg_buf *buf = dbuf->priv;
495 return vb2_dma_sg_vaddr(buf);
498 static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf *dbuf,
499 struct vm_area_struct *vma)
501 return vb2_dma_sg_mmap(dbuf->priv, vma);
504 static const struct dma_buf_ops vb2_dma_sg_dmabuf_ops = {
505 .attach = vb2_dma_sg_dmabuf_ops_attach,
506 .detach = vb2_dma_sg_dmabuf_ops_detach,
507 .map_dma_buf = vb2_dma_sg_dmabuf_ops_map,
508 .unmap_dma_buf = vb2_dma_sg_dmabuf_ops_unmap,
509 .map = vb2_dma_sg_dmabuf_ops_kmap,
510 .map_atomic = vb2_dma_sg_dmabuf_ops_kmap,
511 .vmap = vb2_dma_sg_dmabuf_ops_vmap,
512 .mmap = vb2_dma_sg_dmabuf_ops_mmap,
513 .release = vb2_dma_sg_dmabuf_ops_release,
516 static struct dma_buf *vb2_dma_sg_get_dmabuf(void *buf_priv, unsigned long flags)
518 struct vb2_dma_sg_buf *buf = buf_priv;
519 struct dma_buf *dbuf;
520 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
522 exp_info.ops = &vb2_dma_sg_dmabuf_ops;
523 exp_info.size = buf->size;
524 exp_info.flags = flags;
527 if (WARN_ON(!buf->dma_sgt))
530 dbuf = dma_buf_export(&exp_info);
534 /* dmabuf keeps reference to vb2 buffer */
535 refcount_inc(&buf->refcount);
540 /*********************************************/
541 /* callbacks for DMABUF buffers */
542 /*********************************************/
544 static int vb2_dma_sg_map_dmabuf(void *mem_priv)
546 struct vb2_dma_sg_buf *buf = mem_priv;
547 struct sg_table *sgt;
549 if (WARN_ON(!buf->db_attach)) {
550 pr_err("trying to pin a non attached buffer\n");
554 if (WARN_ON(buf->dma_sgt)) {
555 pr_err("dmabuf buffer is already pinned\n");
559 /* get the associated scatterlist for this buffer */
560 sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
562 pr_err("Error getting dmabuf scatterlist\n");
572 static void vb2_dma_sg_unmap_dmabuf(void *mem_priv)
574 struct vb2_dma_sg_buf *buf = mem_priv;
575 struct sg_table *sgt = buf->dma_sgt;
577 if (WARN_ON(!buf->db_attach)) {
578 pr_err("trying to unpin a not attached buffer\n");
583 pr_err("dmabuf buffer is already unpinned\n");
588 dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
591 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
596 static void vb2_dma_sg_detach_dmabuf(void *mem_priv)
598 struct vb2_dma_sg_buf *buf = mem_priv;
600 /* if vb2 works correctly you should never detach mapped buffer */
601 if (WARN_ON(buf->dma_sgt))
602 vb2_dma_sg_unmap_dmabuf(buf);
604 /* detach this attachment */
605 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
609 static void *vb2_dma_sg_attach_dmabuf(struct device *dev, struct dma_buf *dbuf,
610 unsigned long size, enum dma_data_direction dma_dir)
612 struct vb2_dma_sg_buf *buf;
613 struct dma_buf_attachment *dba;
616 return ERR_PTR(-EINVAL);
618 if (dbuf->size < size)
619 return ERR_PTR(-EFAULT);
621 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
623 return ERR_PTR(-ENOMEM);
626 /* create attachment for the dmabuf with the user device */
627 dba = dma_buf_attach(dbuf, buf->dev);
629 pr_err("failed to attach dmabuf\n");
634 buf->dma_dir = dma_dir;
636 buf->db_attach = dba;
641 static void *vb2_dma_sg_cookie(void *buf_priv)
643 struct vb2_dma_sg_buf *buf = buf_priv;
648 const struct vb2_mem_ops vb2_dma_sg_memops = {
649 .alloc = vb2_dma_sg_alloc,
650 .put = vb2_dma_sg_put,
651 .get_userptr = vb2_dma_sg_get_userptr,
652 .put_userptr = vb2_dma_sg_put_userptr,
653 .prepare = vb2_dma_sg_prepare,
654 .finish = vb2_dma_sg_finish,
655 .vaddr = vb2_dma_sg_vaddr,
656 .mmap = vb2_dma_sg_mmap,
657 .num_users = vb2_dma_sg_num_users,
658 .get_dmabuf = vb2_dma_sg_get_dmabuf,
659 .map_dmabuf = vb2_dma_sg_map_dmabuf,
660 .unmap_dmabuf = vb2_dma_sg_unmap_dmabuf,
661 .attach_dmabuf = vb2_dma_sg_attach_dmabuf,
662 .detach_dmabuf = vb2_dma_sg_detach_dmabuf,
663 .cookie = vb2_dma_sg_cookie,
665 EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
667 MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
668 MODULE_AUTHOR("Andrzej Pietrasiewicz");
669 MODULE_LICENSE("GPL");