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-core.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_conf {
37 struct vb2_dma_sg_buf {
42 enum dma_data_direction dma_dir;
43 struct sg_table sg_table;
45 * This will point to sg_table when used with the MMAP or USERPTR
46 * memory model, and to the dma_buf sglist when used with the
47 * DMABUF memory model.
49 struct sg_table *dma_sgt;
51 unsigned int num_pages;
53 struct vb2_vmarea_handler handler;
54 struct vm_area_struct *vma;
56 struct dma_buf_attachment *db_attach;
59 static void vb2_dma_sg_put(void *buf_priv);
61 static int vb2_dma_sg_alloc_compacted(struct vb2_dma_sg_buf *buf,
64 unsigned int last_page = 0;
72 order = get_order(size);
73 /* Dont over allocate*/
74 if ((PAGE_SIZE << order) > size)
79 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO |
80 __GFP_NOWARN | gfp_flags, order);
86 __free_page(buf->pages[last_page]);
92 split_page(pages, order);
93 for (i = 0; i < (1 << order); i++)
94 buf->pages[last_page++] = &pages[i];
96 size -= PAGE_SIZE << order;
102 static void *vb2_dma_sg_alloc(void *alloc_ctx, unsigned long size,
103 enum dma_data_direction dma_dir, gfp_t gfp_flags)
105 struct vb2_dma_sg_conf *conf = alloc_ctx;
106 struct vb2_dma_sg_buf *buf;
107 struct sg_table *sgt;
110 DEFINE_DMA_ATTRS(attrs);
112 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
114 if (WARN_ON(alloc_ctx == NULL))
116 buf = kzalloc(sizeof *buf, GFP_KERNEL);
121 buf->dma_dir = dma_dir;
124 /* size is already page aligned */
125 buf->num_pages = size >> PAGE_SHIFT;
126 buf->dma_sgt = &buf->sg_table;
128 buf->pages = kzalloc(buf->num_pages * sizeof(struct page *),
131 goto fail_pages_array_alloc;
133 ret = vb2_dma_sg_alloc_compacted(buf, 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(conf->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_sg_attrs(buf->dev, sgt->sgl, sgt->nents,
151 buf->dma_dir, &attrs) == 0)
154 buf->handler.refcount = &buf->refcount;
155 buf->handler.put = vb2_dma_sg_put;
156 buf->handler.arg = buf;
158 atomic_inc(&buf->refcount);
160 dprintk(1, "%s: Allocated buffer of %d pages\n",
161 __func__, buf->num_pages);
165 put_device(buf->dev);
166 sg_free_table(buf->dma_sgt);
168 num_pages = buf->num_pages;
170 __free_page(buf->pages[num_pages]);
173 fail_pages_array_alloc:
178 static void vb2_dma_sg_put(void *buf_priv)
180 struct vb2_dma_sg_buf *buf = buf_priv;
181 struct sg_table *sgt = &buf->sg_table;
182 int i = buf->num_pages;
184 if (atomic_dec_and_test(&buf->refcount)) {
185 DEFINE_DMA_ATTRS(attrs);
187 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
188 dprintk(1, "%s: Freeing buffer of %d pages\n", __func__,
190 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->nents,
191 buf->dma_dir, &attrs);
193 vm_unmap_ram(buf->vaddr, buf->num_pages);
194 sg_free_table(buf->dma_sgt);
196 __free_page(buf->pages[i]);
198 put_device(buf->dev);
203 static void vb2_dma_sg_prepare(void *buf_priv)
205 struct vb2_dma_sg_buf *buf = buf_priv;
206 struct sg_table *sgt = buf->dma_sgt;
208 /* DMABUF exporter will flush the cache for us */
212 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
215 static void vb2_dma_sg_finish(void *buf_priv)
217 struct vb2_dma_sg_buf *buf = buf_priv;
218 struct sg_table *sgt = buf->dma_sgt;
220 /* DMABUF exporter will flush the cache for us */
224 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
227 static inline int vma_is_io(struct vm_area_struct *vma)
229 return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
232 static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr,
234 enum dma_data_direction dma_dir)
236 struct vb2_dma_sg_conf *conf = alloc_ctx;
237 struct vb2_dma_sg_buf *buf;
238 unsigned long first, last;
239 int num_pages_from_user;
240 struct vm_area_struct *vma;
241 struct sg_table *sgt;
242 DEFINE_DMA_ATTRS(attrs);
244 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
246 buf = kzalloc(sizeof *buf, GFP_KERNEL);
251 buf->dev = conf->dev;
252 buf->dma_dir = dma_dir;
253 buf->offset = vaddr & ~PAGE_MASK;
255 buf->dma_sgt = &buf->sg_table;
257 first = (vaddr & PAGE_MASK) >> PAGE_SHIFT;
258 last = ((vaddr + size - 1) & PAGE_MASK) >> PAGE_SHIFT;
259 buf->num_pages = last - first + 1;
261 buf->pages = kzalloc(buf->num_pages * sizeof(struct page *),
264 goto userptr_fail_alloc_pages;
266 vma = find_vma(current->mm, vaddr);
268 dprintk(1, "no vma for address %lu\n", vaddr);
269 goto userptr_fail_find_vma;
272 if (vma->vm_end < vaddr + size) {
273 dprintk(1, "vma at %lu is too small for %lu bytes\n",
275 goto userptr_fail_find_vma;
278 buf->vma = vb2_get_vma(vma);
280 dprintk(1, "failed to copy vma\n");
281 goto userptr_fail_find_vma;
284 if (vma_is_io(buf->vma)) {
285 for (num_pages_from_user = 0;
286 num_pages_from_user < buf->num_pages;
287 ++num_pages_from_user, vaddr += PAGE_SIZE) {
290 if (follow_pfn(vma, vaddr, &pfn)) {
291 dprintk(1, "no page for address %lu\n", vaddr);
294 buf->pages[num_pages_from_user] = pfn_to_page(pfn);
297 num_pages_from_user = get_user_pages(current, current->mm,
300 buf->dma_dir == DMA_FROM_DEVICE,
305 if (num_pages_from_user != buf->num_pages)
306 goto userptr_fail_get_user_pages;
308 if (sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
309 buf->num_pages, buf->offset, size, 0))
310 goto userptr_fail_alloc_table_from_pages;
312 sgt = &buf->sg_table;
314 * No need to sync to the device, this will happen later when the
315 * prepare() memop is called.
317 if (dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->nents,
318 buf->dma_dir, &attrs) == 0)
319 goto userptr_fail_map;
323 sg_free_table(&buf->sg_table);
324 userptr_fail_alloc_table_from_pages:
325 userptr_fail_get_user_pages:
326 dprintk(1, "get_user_pages requested/got: %d/%d]\n",
327 buf->num_pages, num_pages_from_user);
328 if (!vma_is_io(buf->vma))
329 while (--num_pages_from_user >= 0)
330 put_page(buf->pages[num_pages_from_user]);
331 vb2_put_vma(buf->vma);
332 userptr_fail_find_vma:
334 userptr_fail_alloc_pages:
340 * @put_userptr: inform the allocator that a USERPTR buffer will no longer
343 static void vb2_dma_sg_put_userptr(void *buf_priv)
345 struct vb2_dma_sg_buf *buf = buf_priv;
346 struct sg_table *sgt = &buf->sg_table;
347 int i = buf->num_pages;
348 DEFINE_DMA_ATTRS(attrs);
350 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
352 dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
353 __func__, buf->num_pages);
354 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir, &attrs);
356 vm_unmap_ram(buf->vaddr, buf->num_pages);
357 sg_free_table(buf->dma_sgt);
359 if (buf->dma_dir == DMA_FROM_DEVICE)
360 set_page_dirty_lock(buf->pages[i]);
361 if (!vma_is_io(buf->vma))
362 put_page(buf->pages[i]);
365 vb2_put_vma(buf->vma);
369 static void *vb2_dma_sg_vaddr(void *buf_priv)
371 struct vb2_dma_sg_buf *buf = buf_priv;
377 buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
379 buf->vaddr = vm_map_ram(buf->pages,
380 buf->num_pages, -1, PAGE_KERNEL);
383 /* add offset in case userptr is not page-aligned */
384 return buf->vaddr ? buf->vaddr + buf->offset : NULL;
387 static unsigned int vb2_dma_sg_num_users(void *buf_priv)
389 struct vb2_dma_sg_buf *buf = buf_priv;
391 return atomic_read(&buf->refcount);
394 static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
396 struct vb2_dma_sg_buf *buf = buf_priv;
397 unsigned long uaddr = vma->vm_start;
398 unsigned long usize = vma->vm_end - vma->vm_start;
402 printk(KERN_ERR "No memory to map\n");
409 ret = vm_insert_page(vma, uaddr, buf->pages[i++]);
411 printk(KERN_ERR "Remapping memory, error: %d\n", ret);
421 * Use common vm_area operations to track buffer refcount.
423 vma->vm_private_data = &buf->handler;
424 vma->vm_ops = &vb2_common_vm_ops;
426 vma->vm_ops->open(vma);
431 /*********************************************/
432 /* DMABUF ops for exporters */
433 /*********************************************/
435 struct vb2_dma_sg_attachment {
437 enum dma_data_direction dma_dir;
440 static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
441 struct dma_buf_attachment *dbuf_attach)
443 struct vb2_dma_sg_attachment *attach;
445 struct scatterlist *rd, *wr;
446 struct sg_table *sgt;
447 struct vb2_dma_sg_buf *buf = dbuf->priv;
450 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
455 /* Copy the buf->base_sgt scatter list to the attachment, as we can't
456 * map the same scatter list to multiple attachments at the same time.
458 ret = sg_alloc_table(sgt, buf->dma_sgt->orig_nents, GFP_KERNEL);
464 rd = buf->dma_sgt->sgl;
466 for (i = 0; i < sgt->orig_nents; ++i) {
467 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
472 attach->dma_dir = DMA_NONE;
473 dbuf_attach->priv = attach;
478 static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf,
479 struct dma_buf_attachment *db_attach)
481 struct vb2_dma_sg_attachment *attach = db_attach->priv;
482 struct sg_table *sgt;
489 /* release the scatterlist cache */
490 if (attach->dma_dir != DMA_NONE)
491 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
495 db_attach->priv = NULL;
498 static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
499 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
501 struct vb2_dma_sg_attachment *attach = db_attach->priv;
502 /* stealing dmabuf mutex to serialize map/unmap operations */
503 struct mutex *lock = &db_attach->dmabuf->lock;
504 struct sg_table *sgt;
510 /* return previously mapped sg table */
511 if (attach->dma_dir == dma_dir) {
516 /* release any previous cache */
517 if (attach->dma_dir != DMA_NONE) {
518 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
520 attach->dma_dir = DMA_NONE;
523 /* mapping to the client with new direction */
524 ret = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, dma_dir);
526 pr_err("failed to map scatterlist\n");
528 return ERR_PTR(-EIO);
531 attach->dma_dir = dma_dir;
538 static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
539 struct sg_table *sgt, enum dma_data_direction dma_dir)
541 /* nothing to be done here */
544 static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf *dbuf)
546 /* drop reference obtained in vb2_dma_sg_get_dmabuf */
547 vb2_dma_sg_put(dbuf->priv);
550 static void *vb2_dma_sg_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
552 struct vb2_dma_sg_buf *buf = dbuf->priv;
554 return buf->vaddr ? buf->vaddr + pgnum * PAGE_SIZE : NULL;
557 static void *vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf *dbuf)
559 struct vb2_dma_sg_buf *buf = dbuf->priv;
561 return vb2_dma_sg_vaddr(buf);
564 static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf *dbuf,
565 struct vm_area_struct *vma)
567 return vb2_dma_sg_mmap(dbuf->priv, vma);
570 static struct dma_buf_ops vb2_dma_sg_dmabuf_ops = {
571 .attach = vb2_dma_sg_dmabuf_ops_attach,
572 .detach = vb2_dma_sg_dmabuf_ops_detach,
573 .map_dma_buf = vb2_dma_sg_dmabuf_ops_map,
574 .unmap_dma_buf = vb2_dma_sg_dmabuf_ops_unmap,
575 .kmap = vb2_dma_sg_dmabuf_ops_kmap,
576 .kmap_atomic = vb2_dma_sg_dmabuf_ops_kmap,
577 .vmap = vb2_dma_sg_dmabuf_ops_vmap,
578 .mmap = vb2_dma_sg_dmabuf_ops_mmap,
579 .release = vb2_dma_sg_dmabuf_ops_release,
582 static struct dma_buf *vb2_dma_sg_get_dmabuf(void *buf_priv, unsigned long flags)
584 struct vb2_dma_sg_buf *buf = buf_priv;
585 struct dma_buf *dbuf;
586 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
588 exp_info.ops = &vb2_dma_sg_dmabuf_ops;
589 exp_info.size = buf->size;
590 exp_info.flags = flags;
593 if (WARN_ON(!buf->dma_sgt))
596 dbuf = dma_buf_export(&exp_info);
600 /* dmabuf keeps reference to vb2 buffer */
601 atomic_inc(&buf->refcount);
606 /*********************************************/
607 /* callbacks for DMABUF buffers */
608 /*********************************************/
610 static int vb2_dma_sg_map_dmabuf(void *mem_priv)
612 struct vb2_dma_sg_buf *buf = mem_priv;
613 struct sg_table *sgt;
615 if (WARN_ON(!buf->db_attach)) {
616 pr_err("trying to pin a non attached buffer\n");
620 if (WARN_ON(buf->dma_sgt)) {
621 pr_err("dmabuf buffer is already pinned\n");
625 /* get the associated scatterlist for this buffer */
626 sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
628 pr_err("Error getting dmabuf scatterlist\n");
638 static void vb2_dma_sg_unmap_dmabuf(void *mem_priv)
640 struct vb2_dma_sg_buf *buf = mem_priv;
641 struct sg_table *sgt = buf->dma_sgt;
643 if (WARN_ON(!buf->db_attach)) {
644 pr_err("trying to unpin a not attached buffer\n");
649 pr_err("dmabuf buffer is already unpinned\n");
654 dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
657 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
662 static void vb2_dma_sg_detach_dmabuf(void *mem_priv)
664 struct vb2_dma_sg_buf *buf = mem_priv;
666 /* if vb2 works correctly you should never detach mapped buffer */
667 if (WARN_ON(buf->dma_sgt))
668 vb2_dma_sg_unmap_dmabuf(buf);
670 /* detach this attachment */
671 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
675 static void *vb2_dma_sg_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
676 unsigned long size, enum dma_data_direction dma_dir)
678 struct vb2_dma_sg_conf *conf = alloc_ctx;
679 struct vb2_dma_sg_buf *buf;
680 struct dma_buf_attachment *dba;
682 if (dbuf->size < size)
683 return ERR_PTR(-EFAULT);
685 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
687 return ERR_PTR(-ENOMEM);
689 buf->dev = conf->dev;
690 /* create attachment for the dmabuf with the user device */
691 dba = dma_buf_attach(dbuf, buf->dev);
693 pr_err("failed to attach dmabuf\n");
698 buf->dma_dir = dma_dir;
700 buf->db_attach = dba;
705 static void *vb2_dma_sg_cookie(void *buf_priv)
707 struct vb2_dma_sg_buf *buf = buf_priv;
712 const struct vb2_mem_ops vb2_dma_sg_memops = {
713 .alloc = vb2_dma_sg_alloc,
714 .put = vb2_dma_sg_put,
715 .get_userptr = vb2_dma_sg_get_userptr,
716 .put_userptr = vb2_dma_sg_put_userptr,
717 .prepare = vb2_dma_sg_prepare,
718 .finish = vb2_dma_sg_finish,
719 .vaddr = vb2_dma_sg_vaddr,
720 .mmap = vb2_dma_sg_mmap,
721 .num_users = vb2_dma_sg_num_users,
722 .get_dmabuf = vb2_dma_sg_get_dmabuf,
723 .map_dmabuf = vb2_dma_sg_map_dmabuf,
724 .unmap_dmabuf = vb2_dma_sg_unmap_dmabuf,
725 .attach_dmabuf = vb2_dma_sg_attach_dmabuf,
726 .detach_dmabuf = vb2_dma_sg_detach_dmabuf,
727 .cookie = vb2_dma_sg_cookie,
729 EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
731 void *vb2_dma_sg_init_ctx(struct device *dev)
733 struct vb2_dma_sg_conf *conf;
735 conf = kzalloc(sizeof(*conf), GFP_KERNEL);
737 return ERR_PTR(-ENOMEM);
743 EXPORT_SYMBOL_GPL(vb2_dma_sg_init_ctx);
745 void vb2_dma_sg_cleanup_ctx(void *alloc_ctx)
747 if (!IS_ERR_OR_NULL(alloc_ctx))
750 EXPORT_SYMBOL_GPL(vb2_dma_sg_cleanup_ctx);
752 MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
753 MODULE_AUTHOR("Andrzej Pietrasiewicz");
754 MODULE_LICENSE("GPL");