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 sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
151 buf->dma_dir, &attrs);
155 buf->handler.refcount = &buf->refcount;
156 buf->handler.put = vb2_dma_sg_put;
157 buf->handler.arg = buf;
159 atomic_inc(&buf->refcount);
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:
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 (atomic_dec_and_test(&buf->refcount)) {
186 DEFINE_DMA_ATTRS(attrs);
188 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
189 dprintk(1, "%s: Freeing buffer of %d pages\n", __func__,
191 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
192 buf->dma_dir, &attrs);
194 vm_unmap_ram(buf->vaddr, buf->num_pages);
195 sg_free_table(buf->dma_sgt);
197 __free_page(buf->pages[i]);
199 put_device(buf->dev);
204 static void vb2_dma_sg_prepare(void *buf_priv)
206 struct vb2_dma_sg_buf *buf = buf_priv;
207 struct sg_table *sgt = buf->dma_sgt;
209 /* DMABUF exporter will flush the cache for us */
213 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
216 static void vb2_dma_sg_finish(void *buf_priv)
218 struct vb2_dma_sg_buf *buf = buf_priv;
219 struct sg_table *sgt = buf->dma_sgt;
221 /* DMABUF exporter will flush the cache for us */
225 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
228 static inline int vma_is_io(struct vm_area_struct *vma)
230 return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
233 static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr,
235 enum dma_data_direction dma_dir)
237 struct vb2_dma_sg_conf *conf = alloc_ctx;
238 struct vb2_dma_sg_buf *buf;
239 unsigned long first, last;
240 int num_pages_from_user;
241 struct vm_area_struct *vma;
242 struct sg_table *sgt;
243 DEFINE_DMA_ATTRS(attrs);
245 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
247 buf = kzalloc(sizeof *buf, GFP_KERNEL);
252 buf->dev = conf->dev;
253 buf->dma_dir = dma_dir;
254 buf->offset = vaddr & ~PAGE_MASK;
256 buf->dma_sgt = &buf->sg_table;
258 first = (vaddr & PAGE_MASK) >> PAGE_SHIFT;
259 last = ((vaddr + size - 1) & PAGE_MASK) >> PAGE_SHIFT;
260 buf->num_pages = last - first + 1;
262 buf->pages = kzalloc(buf->num_pages * sizeof(struct page *),
265 goto userptr_fail_alloc_pages;
267 vma = find_vma(current->mm, vaddr);
269 dprintk(1, "no vma for address %lu\n", vaddr);
270 goto userptr_fail_find_vma;
273 if (vma->vm_end < vaddr + size) {
274 dprintk(1, "vma at %lu is too small for %lu bytes\n",
276 goto userptr_fail_find_vma;
279 buf->vma = vb2_get_vma(vma);
281 dprintk(1, "failed to copy vma\n");
282 goto userptr_fail_find_vma;
285 if (vma_is_io(buf->vma)) {
286 for (num_pages_from_user = 0;
287 num_pages_from_user < buf->num_pages;
288 ++num_pages_from_user, vaddr += PAGE_SIZE) {
291 if (follow_pfn(vma, vaddr, &pfn)) {
292 dprintk(1, "no page for address %lu\n", vaddr);
295 buf->pages[num_pages_from_user] = pfn_to_page(pfn);
298 num_pages_from_user = get_user_pages(current, current->mm,
301 buf->dma_dir == DMA_FROM_DEVICE,
306 if (num_pages_from_user != buf->num_pages)
307 goto userptr_fail_get_user_pages;
309 if (sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
310 buf->num_pages, buf->offset, size, 0))
311 goto userptr_fail_alloc_table_from_pages;
313 sgt = &buf->sg_table;
315 * No need to sync to the device, this will happen later when the
316 * prepare() memop is called.
318 sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
319 buf->dma_dir, &attrs);
321 goto userptr_fail_map;
326 sg_free_table(&buf->sg_table);
327 userptr_fail_alloc_table_from_pages:
328 userptr_fail_get_user_pages:
329 dprintk(1, "get_user_pages requested/got: %d/%d]\n",
330 buf->num_pages, num_pages_from_user);
331 if (!vma_is_io(buf->vma))
332 while (--num_pages_from_user >= 0)
333 put_page(buf->pages[num_pages_from_user]);
334 vb2_put_vma(buf->vma);
335 userptr_fail_find_vma:
337 userptr_fail_alloc_pages:
343 * @put_userptr: inform the allocator that a USERPTR buffer will no longer
346 static void vb2_dma_sg_put_userptr(void *buf_priv)
348 struct vb2_dma_sg_buf *buf = buf_priv;
349 struct sg_table *sgt = &buf->sg_table;
350 int i = buf->num_pages;
351 DEFINE_DMA_ATTRS(attrs);
353 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
355 dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
356 __func__, buf->num_pages);
357 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir,
360 vm_unmap_ram(buf->vaddr, buf->num_pages);
361 sg_free_table(buf->dma_sgt);
363 if (buf->dma_dir == DMA_FROM_DEVICE)
364 set_page_dirty_lock(buf->pages[i]);
365 if (!vma_is_io(buf->vma))
366 put_page(buf->pages[i]);
369 vb2_put_vma(buf->vma);
373 static void *vb2_dma_sg_vaddr(void *buf_priv)
375 struct vb2_dma_sg_buf *buf = buf_priv;
381 buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
383 buf->vaddr = vm_map_ram(buf->pages,
384 buf->num_pages, -1, PAGE_KERNEL);
387 /* add offset in case userptr is not page-aligned */
388 return buf->vaddr ? buf->vaddr + buf->offset : NULL;
391 static unsigned int vb2_dma_sg_num_users(void *buf_priv)
393 struct vb2_dma_sg_buf *buf = buf_priv;
395 return atomic_read(&buf->refcount);
398 static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
400 struct vb2_dma_sg_buf *buf = buf_priv;
401 unsigned long uaddr = vma->vm_start;
402 unsigned long usize = vma->vm_end - vma->vm_start;
406 printk(KERN_ERR "No memory to map\n");
413 ret = vm_insert_page(vma, uaddr, buf->pages[i++]);
415 printk(KERN_ERR "Remapping memory, error: %d\n", ret);
425 * Use common vm_area operations to track buffer refcount.
427 vma->vm_private_data = &buf->handler;
428 vma->vm_ops = &vb2_common_vm_ops;
430 vma->vm_ops->open(vma);
435 /*********************************************/
436 /* DMABUF ops for exporters */
437 /*********************************************/
439 struct vb2_dma_sg_attachment {
441 enum dma_data_direction dma_dir;
444 static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
445 struct dma_buf_attachment *dbuf_attach)
447 struct vb2_dma_sg_attachment *attach;
449 struct scatterlist *rd, *wr;
450 struct sg_table *sgt;
451 struct vb2_dma_sg_buf *buf = dbuf->priv;
454 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
459 /* Copy the buf->base_sgt scatter list to the attachment, as we can't
460 * map the same scatter list to multiple attachments at the same time.
462 ret = sg_alloc_table(sgt, buf->dma_sgt->orig_nents, GFP_KERNEL);
468 rd = buf->dma_sgt->sgl;
470 for (i = 0; i < sgt->orig_nents; ++i) {
471 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
476 attach->dma_dir = DMA_NONE;
477 dbuf_attach->priv = attach;
482 static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf,
483 struct dma_buf_attachment *db_attach)
485 struct vb2_dma_sg_attachment *attach = db_attach->priv;
486 struct sg_table *sgt;
493 /* release the scatterlist cache */
494 if (attach->dma_dir != DMA_NONE)
495 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
499 db_attach->priv = NULL;
502 static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
503 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
505 struct vb2_dma_sg_attachment *attach = db_attach->priv;
506 /* stealing dmabuf mutex to serialize map/unmap operations */
507 struct mutex *lock = &db_attach->dmabuf->lock;
508 struct sg_table *sgt;
513 /* return previously mapped sg table */
514 if (attach->dma_dir == dma_dir) {
519 /* release any previous cache */
520 if (attach->dma_dir != DMA_NONE) {
521 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
523 attach->dma_dir = DMA_NONE;
526 /* mapping to the client with new direction */
527 sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
530 pr_err("failed to map scatterlist\n");
532 return ERR_PTR(-EIO);
535 attach->dma_dir = dma_dir;
542 static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
543 struct sg_table *sgt, enum dma_data_direction dma_dir)
545 /* nothing to be done here */
548 static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf *dbuf)
550 /* drop reference obtained in vb2_dma_sg_get_dmabuf */
551 vb2_dma_sg_put(dbuf->priv);
554 static void *vb2_dma_sg_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
556 struct vb2_dma_sg_buf *buf = dbuf->priv;
558 return buf->vaddr ? buf->vaddr + pgnum * PAGE_SIZE : NULL;
561 static void *vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf *dbuf)
563 struct vb2_dma_sg_buf *buf = dbuf->priv;
565 return vb2_dma_sg_vaddr(buf);
568 static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf *dbuf,
569 struct vm_area_struct *vma)
571 return vb2_dma_sg_mmap(dbuf->priv, vma);
574 static struct dma_buf_ops vb2_dma_sg_dmabuf_ops = {
575 .attach = vb2_dma_sg_dmabuf_ops_attach,
576 .detach = vb2_dma_sg_dmabuf_ops_detach,
577 .map_dma_buf = vb2_dma_sg_dmabuf_ops_map,
578 .unmap_dma_buf = vb2_dma_sg_dmabuf_ops_unmap,
579 .kmap = vb2_dma_sg_dmabuf_ops_kmap,
580 .kmap_atomic = vb2_dma_sg_dmabuf_ops_kmap,
581 .vmap = vb2_dma_sg_dmabuf_ops_vmap,
582 .mmap = vb2_dma_sg_dmabuf_ops_mmap,
583 .release = vb2_dma_sg_dmabuf_ops_release,
586 static struct dma_buf *vb2_dma_sg_get_dmabuf(void *buf_priv, unsigned long flags)
588 struct vb2_dma_sg_buf *buf = buf_priv;
589 struct dma_buf *dbuf;
590 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
592 exp_info.ops = &vb2_dma_sg_dmabuf_ops;
593 exp_info.size = buf->size;
594 exp_info.flags = flags;
597 if (WARN_ON(!buf->dma_sgt))
600 dbuf = dma_buf_export(&exp_info);
604 /* dmabuf keeps reference to vb2 buffer */
605 atomic_inc(&buf->refcount);
610 /*********************************************/
611 /* callbacks for DMABUF buffers */
612 /*********************************************/
614 static int vb2_dma_sg_map_dmabuf(void *mem_priv)
616 struct vb2_dma_sg_buf *buf = mem_priv;
617 struct sg_table *sgt;
619 if (WARN_ON(!buf->db_attach)) {
620 pr_err("trying to pin a non attached buffer\n");
624 if (WARN_ON(buf->dma_sgt)) {
625 pr_err("dmabuf buffer is already pinned\n");
629 /* get the associated scatterlist for this buffer */
630 sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
632 pr_err("Error getting dmabuf scatterlist\n");
642 static void vb2_dma_sg_unmap_dmabuf(void *mem_priv)
644 struct vb2_dma_sg_buf *buf = mem_priv;
645 struct sg_table *sgt = buf->dma_sgt;
647 if (WARN_ON(!buf->db_attach)) {
648 pr_err("trying to unpin a not attached buffer\n");
653 pr_err("dmabuf buffer is already unpinned\n");
658 dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
661 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
666 static void vb2_dma_sg_detach_dmabuf(void *mem_priv)
668 struct vb2_dma_sg_buf *buf = mem_priv;
670 /* if vb2 works correctly you should never detach mapped buffer */
671 if (WARN_ON(buf->dma_sgt))
672 vb2_dma_sg_unmap_dmabuf(buf);
674 /* detach this attachment */
675 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
679 static void *vb2_dma_sg_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
680 unsigned long size, enum dma_data_direction dma_dir)
682 struct vb2_dma_sg_conf *conf = alloc_ctx;
683 struct vb2_dma_sg_buf *buf;
684 struct dma_buf_attachment *dba;
686 if (dbuf->size < size)
687 return ERR_PTR(-EFAULT);
689 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
691 return ERR_PTR(-ENOMEM);
693 buf->dev = conf->dev;
694 /* create attachment for the dmabuf with the user device */
695 dba = dma_buf_attach(dbuf, buf->dev);
697 pr_err("failed to attach dmabuf\n");
702 buf->dma_dir = dma_dir;
704 buf->db_attach = dba;
709 static void *vb2_dma_sg_cookie(void *buf_priv)
711 struct vb2_dma_sg_buf *buf = buf_priv;
716 const struct vb2_mem_ops vb2_dma_sg_memops = {
717 .alloc = vb2_dma_sg_alloc,
718 .put = vb2_dma_sg_put,
719 .get_userptr = vb2_dma_sg_get_userptr,
720 .put_userptr = vb2_dma_sg_put_userptr,
721 .prepare = vb2_dma_sg_prepare,
722 .finish = vb2_dma_sg_finish,
723 .vaddr = vb2_dma_sg_vaddr,
724 .mmap = vb2_dma_sg_mmap,
725 .num_users = vb2_dma_sg_num_users,
726 .get_dmabuf = vb2_dma_sg_get_dmabuf,
727 .map_dmabuf = vb2_dma_sg_map_dmabuf,
728 .unmap_dmabuf = vb2_dma_sg_unmap_dmabuf,
729 .attach_dmabuf = vb2_dma_sg_attach_dmabuf,
730 .detach_dmabuf = vb2_dma_sg_detach_dmabuf,
731 .cookie = vb2_dma_sg_cookie,
733 EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
735 void *vb2_dma_sg_init_ctx(struct device *dev)
737 struct vb2_dma_sg_conf *conf;
739 conf = kzalloc(sizeof(*conf), GFP_KERNEL);
741 return ERR_PTR(-ENOMEM);
747 EXPORT_SYMBOL_GPL(vb2_dma_sg_init_ctx);
749 void vb2_dma_sg_cleanup_ctx(void *alloc_ctx)
751 if (!IS_ERR_OR_NULL(alloc_ctx))
754 EXPORT_SYMBOL_GPL(vb2_dma_sg_cleanup_ctx);
756 MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
757 MODULE_AUTHOR("Andrzej Pietrasiewicz");
758 MODULE_LICENSE("GPL");