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;
111 if (WARN_ON(alloc_ctx == NULL))
113 buf = kzalloc(sizeof *buf, GFP_KERNEL);
118 buf->dma_dir = dma_dir;
121 /* size is already page aligned */
122 buf->num_pages = size >> PAGE_SHIFT;
123 buf->dma_sgt = &buf->sg_table;
125 buf->pages = kzalloc(buf->num_pages * sizeof(struct page *),
128 goto fail_pages_array_alloc;
130 ret = vb2_dma_sg_alloc_compacted(buf, gfp_flags);
132 goto fail_pages_alloc;
134 ret = sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
135 buf->num_pages, 0, size, GFP_KERNEL);
137 goto fail_table_alloc;
139 /* Prevent the device from being released while the buffer is used */
140 buf->dev = get_device(conf->dev);
142 sgt = &buf->sg_table;
143 if (dma_map_sg(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir) == 0)
145 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
147 buf->handler.refcount = &buf->refcount;
148 buf->handler.put = vb2_dma_sg_put;
149 buf->handler.arg = buf;
151 atomic_inc(&buf->refcount);
153 dprintk(1, "%s: Allocated buffer of %d pages\n",
154 __func__, buf->num_pages);
158 put_device(buf->dev);
159 sg_free_table(buf->dma_sgt);
161 num_pages = buf->num_pages;
163 __free_page(buf->pages[num_pages]);
166 fail_pages_array_alloc:
171 static void vb2_dma_sg_put(void *buf_priv)
173 struct vb2_dma_sg_buf *buf = buf_priv;
174 struct sg_table *sgt = &buf->sg_table;
175 int i = buf->num_pages;
177 if (atomic_dec_and_test(&buf->refcount)) {
178 dprintk(1, "%s: Freeing buffer of %d pages\n", __func__,
180 dma_unmap_sg(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
182 vm_unmap_ram(buf->vaddr, buf->num_pages);
183 sg_free_table(buf->dma_sgt);
185 __free_page(buf->pages[i]);
187 put_device(buf->dev);
192 static void vb2_dma_sg_prepare(void *buf_priv)
194 struct vb2_dma_sg_buf *buf = buf_priv;
195 struct sg_table *sgt = buf->dma_sgt;
197 /* DMABUF exporter will flush the cache for us */
201 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
204 static void vb2_dma_sg_finish(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_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
216 static inline int vma_is_io(struct vm_area_struct *vma)
218 return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
221 static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr,
223 enum dma_data_direction dma_dir)
225 struct vb2_dma_sg_conf *conf = alloc_ctx;
226 struct vb2_dma_sg_buf *buf;
227 unsigned long first, last;
228 int num_pages_from_user;
229 struct vm_area_struct *vma;
230 struct sg_table *sgt;
232 buf = kzalloc(sizeof *buf, GFP_KERNEL);
237 buf->dev = conf->dev;
238 buf->dma_dir = dma_dir;
239 buf->offset = vaddr & ~PAGE_MASK;
241 buf->dma_sgt = &buf->sg_table;
243 first = (vaddr & PAGE_MASK) >> PAGE_SHIFT;
244 last = ((vaddr + size - 1) & PAGE_MASK) >> PAGE_SHIFT;
245 buf->num_pages = last - first + 1;
247 buf->pages = kzalloc(buf->num_pages * sizeof(struct page *),
250 goto userptr_fail_alloc_pages;
252 vma = find_vma(current->mm, vaddr);
254 dprintk(1, "no vma for address %lu\n", vaddr);
255 goto userptr_fail_find_vma;
258 if (vma->vm_end < vaddr + size) {
259 dprintk(1, "vma at %lu is too small for %lu bytes\n",
261 goto userptr_fail_find_vma;
264 buf->vma = vb2_get_vma(vma);
266 dprintk(1, "failed to copy vma\n");
267 goto userptr_fail_find_vma;
270 if (vma_is_io(buf->vma)) {
271 for (num_pages_from_user = 0;
272 num_pages_from_user < buf->num_pages;
273 ++num_pages_from_user, vaddr += PAGE_SIZE) {
276 if (follow_pfn(vma, vaddr, &pfn)) {
277 dprintk(1, "no page for address %lu\n", vaddr);
280 buf->pages[num_pages_from_user] = pfn_to_page(pfn);
283 num_pages_from_user = get_user_pages(current, current->mm,
286 buf->dma_dir == DMA_FROM_DEVICE,
291 if (num_pages_from_user != buf->num_pages)
292 goto userptr_fail_get_user_pages;
294 if (sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
295 buf->num_pages, buf->offset, size, 0))
296 goto userptr_fail_alloc_table_from_pages;
298 sgt = &buf->sg_table;
299 if (dma_map_sg(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir) == 0)
300 goto userptr_fail_map;
301 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
305 sg_free_table(&buf->sg_table);
306 userptr_fail_alloc_table_from_pages:
307 userptr_fail_get_user_pages:
308 dprintk(1, "get_user_pages requested/got: %d/%d]\n",
309 buf->num_pages, num_pages_from_user);
310 if (!vma_is_io(buf->vma))
311 while (--num_pages_from_user >= 0)
312 put_page(buf->pages[num_pages_from_user]);
313 vb2_put_vma(buf->vma);
314 userptr_fail_find_vma:
316 userptr_fail_alloc_pages:
322 * @put_userptr: inform the allocator that a USERPTR buffer will no longer
325 static void vb2_dma_sg_put_userptr(void *buf_priv)
327 struct vb2_dma_sg_buf *buf = buf_priv;
328 struct sg_table *sgt = &buf->sg_table;
329 int i = buf->num_pages;
331 dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
332 __func__, buf->num_pages);
333 dma_unmap_sg(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
335 vm_unmap_ram(buf->vaddr, buf->num_pages);
336 sg_free_table(buf->dma_sgt);
338 if (buf->dma_dir == DMA_FROM_DEVICE)
339 set_page_dirty_lock(buf->pages[i]);
340 if (!vma_is_io(buf->vma))
341 put_page(buf->pages[i]);
344 vb2_put_vma(buf->vma);
348 static void *vb2_dma_sg_vaddr(void *buf_priv)
350 struct vb2_dma_sg_buf *buf = buf_priv;
356 buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
358 buf->vaddr = vm_map_ram(buf->pages,
359 buf->num_pages, -1, PAGE_KERNEL);
362 /* add offset in case userptr is not page-aligned */
363 return buf->vaddr ? buf->vaddr + buf->offset : NULL;
366 static unsigned int vb2_dma_sg_num_users(void *buf_priv)
368 struct vb2_dma_sg_buf *buf = buf_priv;
370 return atomic_read(&buf->refcount);
373 static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
375 struct vb2_dma_sg_buf *buf = buf_priv;
376 unsigned long uaddr = vma->vm_start;
377 unsigned long usize = vma->vm_end - vma->vm_start;
381 printk(KERN_ERR "No memory to map\n");
388 ret = vm_insert_page(vma, uaddr, buf->pages[i++]);
390 printk(KERN_ERR "Remapping memory, error: %d\n", ret);
400 * Use common vm_area operations to track buffer refcount.
402 vma->vm_private_data = &buf->handler;
403 vma->vm_ops = &vb2_common_vm_ops;
405 vma->vm_ops->open(vma);
410 /*********************************************/
411 /* DMABUF ops for exporters */
412 /*********************************************/
414 struct vb2_dma_sg_attachment {
416 enum dma_data_direction dma_dir;
419 static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
420 struct dma_buf_attachment *dbuf_attach)
422 struct vb2_dma_sg_attachment *attach;
424 struct scatterlist *rd, *wr;
425 struct sg_table *sgt;
426 struct vb2_dma_sg_buf *buf = dbuf->priv;
429 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
434 /* Copy the buf->base_sgt scatter list to the attachment, as we can't
435 * map the same scatter list to multiple attachments at the same time.
437 ret = sg_alloc_table(sgt, buf->dma_sgt->orig_nents, GFP_KERNEL);
443 rd = buf->dma_sgt->sgl;
445 for (i = 0; i < sgt->orig_nents; ++i) {
446 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
451 attach->dma_dir = DMA_NONE;
452 dbuf_attach->priv = attach;
457 static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf,
458 struct dma_buf_attachment *db_attach)
460 struct vb2_dma_sg_attachment *attach = db_attach->priv;
461 struct sg_table *sgt;
468 /* release the scatterlist cache */
469 if (attach->dma_dir != DMA_NONE)
470 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
474 db_attach->priv = NULL;
477 static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
478 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
480 struct vb2_dma_sg_attachment *attach = db_attach->priv;
481 /* stealing dmabuf mutex to serialize map/unmap operations */
482 struct mutex *lock = &db_attach->dmabuf->lock;
483 struct sg_table *sgt;
489 /* return previously mapped sg table */
490 if (attach->dma_dir == dma_dir) {
495 /* release any previous cache */
496 if (attach->dma_dir != DMA_NONE) {
497 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
499 attach->dma_dir = DMA_NONE;
502 /* mapping to the client with new direction */
503 ret = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, dma_dir);
505 pr_err("failed to map scatterlist\n");
507 return ERR_PTR(-EIO);
510 attach->dma_dir = dma_dir;
517 static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
518 struct sg_table *sgt, enum dma_data_direction dma_dir)
520 /* nothing to be done here */
523 static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf *dbuf)
525 /* drop reference obtained in vb2_dma_sg_get_dmabuf */
526 vb2_dma_sg_put(dbuf->priv);
529 static void *vb2_dma_sg_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
531 struct vb2_dma_sg_buf *buf = dbuf->priv;
533 return buf->vaddr ? buf->vaddr + pgnum * PAGE_SIZE : NULL;
536 static void *vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf *dbuf)
538 struct vb2_dma_sg_buf *buf = dbuf->priv;
540 return vb2_dma_sg_vaddr(buf);
543 static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf *dbuf,
544 struct vm_area_struct *vma)
546 return vb2_dma_sg_mmap(dbuf->priv, vma);
549 static struct dma_buf_ops vb2_dma_sg_dmabuf_ops = {
550 .attach = vb2_dma_sg_dmabuf_ops_attach,
551 .detach = vb2_dma_sg_dmabuf_ops_detach,
552 .map_dma_buf = vb2_dma_sg_dmabuf_ops_map,
553 .unmap_dma_buf = vb2_dma_sg_dmabuf_ops_unmap,
554 .kmap = vb2_dma_sg_dmabuf_ops_kmap,
555 .kmap_atomic = vb2_dma_sg_dmabuf_ops_kmap,
556 .vmap = vb2_dma_sg_dmabuf_ops_vmap,
557 .mmap = vb2_dma_sg_dmabuf_ops_mmap,
558 .release = vb2_dma_sg_dmabuf_ops_release,
561 static struct dma_buf *vb2_dma_sg_get_dmabuf(void *buf_priv, unsigned long flags)
563 struct vb2_dma_sg_buf *buf = buf_priv;
564 struct dma_buf *dbuf;
566 if (WARN_ON(!buf->dma_sgt))
569 dbuf = dma_buf_export(buf, &vb2_dma_sg_dmabuf_ops, buf->size, flags, NULL);
573 /* dmabuf keeps reference to vb2 buffer */
574 atomic_inc(&buf->refcount);
579 /*********************************************/
580 /* callbacks for DMABUF buffers */
581 /*********************************************/
583 static int vb2_dma_sg_map_dmabuf(void *mem_priv)
585 struct vb2_dma_sg_buf *buf = mem_priv;
586 struct sg_table *sgt;
588 if (WARN_ON(!buf->db_attach)) {
589 pr_err("trying to pin a non attached buffer\n");
593 if (WARN_ON(buf->dma_sgt)) {
594 pr_err("dmabuf buffer is already pinned\n");
598 /* get the associated scatterlist for this buffer */
599 sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
601 pr_err("Error getting dmabuf scatterlist\n");
611 static void vb2_dma_sg_unmap_dmabuf(void *mem_priv)
613 struct vb2_dma_sg_buf *buf = mem_priv;
614 struct sg_table *sgt = buf->dma_sgt;
616 if (WARN_ON(!buf->db_attach)) {
617 pr_err("trying to unpin a not attached buffer\n");
622 pr_err("dmabuf buffer is already unpinned\n");
627 dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
630 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
635 static void vb2_dma_sg_detach_dmabuf(void *mem_priv)
637 struct vb2_dma_sg_buf *buf = mem_priv;
639 /* if vb2 works correctly you should never detach mapped buffer */
640 if (WARN_ON(buf->dma_sgt))
641 vb2_dma_sg_unmap_dmabuf(buf);
643 /* detach this attachment */
644 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
648 static void *vb2_dma_sg_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
649 unsigned long size, enum dma_data_direction dma_dir)
651 struct vb2_dma_sg_conf *conf = alloc_ctx;
652 struct vb2_dma_sg_buf *buf;
653 struct dma_buf_attachment *dba;
655 if (dbuf->size < size)
656 return ERR_PTR(-EFAULT);
658 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
660 return ERR_PTR(-ENOMEM);
662 buf->dev = conf->dev;
663 /* create attachment for the dmabuf with the user device */
664 dba = dma_buf_attach(dbuf, buf->dev);
666 pr_err("failed to attach dmabuf\n");
671 buf->dma_dir = dma_dir;
673 buf->db_attach = dba;
678 static void *vb2_dma_sg_cookie(void *buf_priv)
680 struct vb2_dma_sg_buf *buf = buf_priv;
685 const struct vb2_mem_ops vb2_dma_sg_memops = {
686 .alloc = vb2_dma_sg_alloc,
687 .put = vb2_dma_sg_put,
688 .get_userptr = vb2_dma_sg_get_userptr,
689 .put_userptr = vb2_dma_sg_put_userptr,
690 .prepare = vb2_dma_sg_prepare,
691 .finish = vb2_dma_sg_finish,
692 .vaddr = vb2_dma_sg_vaddr,
693 .mmap = vb2_dma_sg_mmap,
694 .num_users = vb2_dma_sg_num_users,
695 .get_dmabuf = vb2_dma_sg_get_dmabuf,
696 .map_dmabuf = vb2_dma_sg_map_dmabuf,
697 .unmap_dmabuf = vb2_dma_sg_unmap_dmabuf,
698 .attach_dmabuf = vb2_dma_sg_attach_dmabuf,
699 .detach_dmabuf = vb2_dma_sg_detach_dmabuf,
700 .cookie = vb2_dma_sg_cookie,
702 EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
704 void *vb2_dma_sg_init_ctx(struct device *dev)
706 struct vb2_dma_sg_conf *conf;
708 conf = kzalloc(sizeof(*conf), GFP_KERNEL);
710 return ERR_PTR(-ENOMEM);
716 EXPORT_SYMBOL_GPL(vb2_dma_sg_init_ctx);
718 void vb2_dma_sg_cleanup_ctx(void *alloc_ctx)
720 if (!IS_ERR_OR_NULL(alloc_ctx))
723 EXPORT_SYMBOL_GPL(vb2_dma_sg_cleanup_ctx);
725 MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
726 MODULE_AUTHOR("Andrzej Pietrasiewicz");
727 MODULE_LICENSE("GPL");