2 * videobuf2-dma-contig.c - DMA contig memory allocator for videobuf2
4 * Copyright (C) 2010 Samsung Electronics
6 * Author: Pawel Osciak <pawel@osciak.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/dma-buf.h>
14 #include <linux/module.h>
15 #include <linux/scatterlist.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/dma-mapping.h>
20 #include <media/videobuf2-core.h>
21 #include <media/videobuf2-dma-contig.h>
22 #include <media/videobuf2-memops.h>
33 enum dma_data_direction dma_dir;
34 struct sg_table *dma_sgt;
37 struct vb2_vmarea_handler handler;
39 struct sg_table *sgt_base;
42 struct vm_area_struct *vma;
45 struct dma_buf_attachment *db_attach;
48 /*********************************************/
49 /* scatterlist table functions */
50 /*********************************************/
53 static void vb2_dc_sgt_foreach_page(struct sg_table *sgt,
54 void (*cb)(struct page *pg))
56 struct scatterlist *s;
59 for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
60 struct page *page = sg_page(s);
61 unsigned int n_pages = PAGE_ALIGN(s->offset + s->length)
65 for (j = 0; j < n_pages; ++j, ++page)
70 static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
72 struct scatterlist *s;
73 dma_addr_t expected = sg_dma_address(sgt->sgl);
75 unsigned long size = 0;
77 for_each_sg(sgt->sgl, s, sgt->nents, i) {
78 if (sg_dma_address(s) != expected)
80 expected = sg_dma_address(s) + sg_dma_len(s);
81 size += sg_dma_len(s);
86 /*********************************************/
87 /* callbacks for all buffers */
88 /*********************************************/
90 static void *vb2_dc_cookie(void *buf_priv)
92 struct vb2_dc_buf *buf = buf_priv;
94 return &buf->dma_addr;
97 static void *vb2_dc_vaddr(void *buf_priv)
99 struct vb2_dc_buf *buf = buf_priv;
104 static unsigned int vb2_dc_num_users(void *buf_priv)
106 struct vb2_dc_buf *buf = buf_priv;
108 return atomic_read(&buf->refcount);
111 static void vb2_dc_prepare(void *buf_priv)
113 struct vb2_dc_buf *buf = buf_priv;
114 struct sg_table *sgt = buf->dma_sgt;
116 /* DMABUF exporter will flush the cache for us */
117 if (!sgt || buf->db_attach)
120 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
123 static void vb2_dc_finish(void *buf_priv)
125 struct vb2_dc_buf *buf = buf_priv;
126 struct sg_table *sgt = buf->dma_sgt;
128 /* DMABUF exporter will flush the cache for us */
129 if (!sgt || buf->db_attach)
132 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
135 /*********************************************/
136 /* callbacks for MMAP buffers */
137 /*********************************************/
139 static void vb2_dc_put(void *buf_priv)
141 struct vb2_dc_buf *buf = buf_priv;
143 if (!atomic_dec_and_test(&buf->refcount))
147 sg_free_table(buf->sgt_base);
148 kfree(buf->sgt_base);
150 dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr);
151 put_device(buf->dev);
155 static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size)
157 struct vb2_dc_conf *conf = alloc_ctx;
158 struct device *dev = conf->dev;
159 struct vb2_dc_buf *buf;
161 buf = kzalloc(sizeof *buf, GFP_KERNEL);
163 return ERR_PTR(-ENOMEM);
165 buf->vaddr = dma_alloc_coherent(dev, size, &buf->dma_addr, GFP_KERNEL);
167 dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size);
169 return ERR_PTR(-ENOMEM);
172 /* Prevent the device from being released while the buffer is used */
173 buf->dev = get_device(dev);
176 buf->handler.refcount = &buf->refcount;
177 buf->handler.put = vb2_dc_put;
178 buf->handler.arg = buf;
180 atomic_inc(&buf->refcount);
185 static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
187 struct vb2_dc_buf *buf = buf_priv;
191 printk(KERN_ERR "No buffer to map\n");
196 * dma_mmap_* uses vm_pgoff as in-buffer offset, but we want to
201 ret = dma_mmap_coherent(buf->dev, vma, buf->vaddr,
202 buf->dma_addr, buf->size);
205 pr_err("Remapping memory failed, error: %d\n", ret);
209 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
210 vma->vm_private_data = &buf->handler;
211 vma->vm_ops = &vb2_common_vm_ops;
213 vma->vm_ops->open(vma);
215 pr_debug("%s: mapped dma addr 0x%08lx at 0x%08lx, size %ld\n",
216 __func__, (unsigned long)buf->dma_addr, vma->vm_start,
222 /*********************************************/
223 /* DMABUF ops for exporters */
224 /*********************************************/
226 struct vb2_dc_attachment {
228 enum dma_data_direction dir;
231 static int vb2_dc_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
232 struct dma_buf_attachment *dbuf_attach)
234 struct vb2_dc_attachment *attach;
236 struct scatterlist *rd, *wr;
237 struct sg_table *sgt;
238 struct vb2_dc_buf *buf = dbuf->priv;
241 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
246 /* Copy the buf->base_sgt scatter list to the attachment, as we can't
247 * map the same scatter list to multiple attachments at the same time.
249 ret = sg_alloc_table(sgt, buf->sgt_base->orig_nents, GFP_KERNEL);
255 rd = buf->sgt_base->sgl;
257 for (i = 0; i < sgt->orig_nents; ++i) {
258 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
263 attach->dir = DMA_NONE;
264 dbuf_attach->priv = attach;
269 static void vb2_dc_dmabuf_ops_detach(struct dma_buf *dbuf,
270 struct dma_buf_attachment *db_attach)
272 struct vb2_dc_attachment *attach = db_attach->priv;
273 struct sg_table *sgt;
280 /* release the scatterlist cache */
281 if (attach->dir != DMA_NONE)
282 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
286 db_attach->priv = NULL;
289 static struct sg_table *vb2_dc_dmabuf_ops_map(
290 struct dma_buf_attachment *db_attach, enum dma_data_direction dir)
292 struct vb2_dc_attachment *attach = db_attach->priv;
293 /* stealing dmabuf mutex to serialize map/unmap operations */
294 struct mutex *lock = &db_attach->dmabuf->lock;
295 struct sg_table *sgt;
301 /* return previously mapped sg table */
302 if (attach->dir == dir) {
307 /* release any previous cache */
308 if (attach->dir != DMA_NONE) {
309 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
311 attach->dir = DMA_NONE;
314 /* mapping to the client with new direction */
315 ret = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, dir);
317 pr_err("failed to map scatterlist\n");
319 return ERR_PTR(-EIO);
329 static void vb2_dc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
330 struct sg_table *sgt, enum dma_data_direction dir)
332 /* nothing to be done here */
335 static void vb2_dc_dmabuf_ops_release(struct dma_buf *dbuf)
337 /* drop reference obtained in vb2_dc_get_dmabuf */
338 vb2_dc_put(dbuf->priv);
341 static void *vb2_dc_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
343 struct vb2_dc_buf *buf = dbuf->priv;
345 return buf->vaddr + pgnum * PAGE_SIZE;
348 static void *vb2_dc_dmabuf_ops_vmap(struct dma_buf *dbuf)
350 struct vb2_dc_buf *buf = dbuf->priv;
355 static int vb2_dc_dmabuf_ops_mmap(struct dma_buf *dbuf,
356 struct vm_area_struct *vma)
358 return vb2_dc_mmap(dbuf->priv, vma);
361 static struct dma_buf_ops vb2_dc_dmabuf_ops = {
362 .attach = vb2_dc_dmabuf_ops_attach,
363 .detach = vb2_dc_dmabuf_ops_detach,
364 .map_dma_buf = vb2_dc_dmabuf_ops_map,
365 .unmap_dma_buf = vb2_dc_dmabuf_ops_unmap,
366 .kmap = vb2_dc_dmabuf_ops_kmap,
367 .kmap_atomic = vb2_dc_dmabuf_ops_kmap,
368 .vmap = vb2_dc_dmabuf_ops_vmap,
369 .mmap = vb2_dc_dmabuf_ops_mmap,
370 .release = vb2_dc_dmabuf_ops_release,
373 static struct sg_table *vb2_dc_get_base_sgt(struct vb2_dc_buf *buf)
376 struct sg_table *sgt;
378 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
380 dev_err(buf->dev, "failed to alloc sg table\n");
384 ret = dma_get_sgtable(buf->dev, sgt, buf->vaddr, buf->dma_addr,
387 dev_err(buf->dev, "failed to get scatterlist from DMA API\n");
395 static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv)
397 struct vb2_dc_buf *buf = buf_priv;
398 struct dma_buf *dbuf;
401 buf->sgt_base = vb2_dc_get_base_sgt(buf);
403 if (WARN_ON(!buf->sgt_base))
406 dbuf = dma_buf_export(buf, &vb2_dc_dmabuf_ops, buf->size, 0);
410 /* dmabuf keeps reference to vb2 buffer */
411 atomic_inc(&buf->refcount);
416 /*********************************************/
417 /* callbacks for USERPTR buffers */
418 /*********************************************/
420 static inline int vma_is_io(struct vm_area_struct *vma)
422 return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
425 static int vb2_dc_get_user_pages(unsigned long start, struct page **pages,
426 int n_pages, struct vm_area_struct *vma, int write)
428 if (vma_is_io(vma)) {
431 for (i = 0; i < n_pages; ++i, start += PAGE_SIZE) {
433 int ret = follow_pfn(vma, start, &pfn);
436 pr_err("no page for address %lu\n", start);
439 pages[i] = pfn_to_page(pfn);
444 n = get_user_pages(current, current->mm, start & PAGE_MASK,
445 n_pages, write, 1, pages, NULL);
446 /* negative error means that no page was pinned */
449 pr_err("got only %d of %d user pages\n", n, n_pages);
451 put_page(pages[--n]);
459 static void vb2_dc_put_dirty_page(struct page *page)
461 set_page_dirty_lock(page);
465 static void vb2_dc_put_userptr(void *buf_priv)
467 struct vb2_dc_buf *buf = buf_priv;
468 struct sg_table *sgt = buf->dma_sgt;
470 dma_unmap_sg(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
471 if (!vma_is_io(buf->vma))
472 vb2_dc_sgt_foreach_page(sgt, vb2_dc_put_dirty_page);
476 vb2_put_vma(buf->vma);
480 static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
481 unsigned long size, int write)
483 struct vb2_dc_conf *conf = alloc_ctx;
484 struct vb2_dc_buf *buf;
487 unsigned long offset;
491 struct vm_area_struct *vma;
492 struct sg_table *sgt;
493 unsigned long contig_size;
495 buf = kzalloc(sizeof *buf, GFP_KERNEL);
497 return ERR_PTR(-ENOMEM);
499 buf->dev = conf->dev;
500 buf->dma_dir = write ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
502 start = vaddr & PAGE_MASK;
503 offset = vaddr & ~PAGE_MASK;
504 end = PAGE_ALIGN(vaddr + size);
505 n_pages = (end - start) >> PAGE_SHIFT;
507 pages = kmalloc(n_pages * sizeof(pages[0]), GFP_KERNEL);
510 pr_err("failed to allocate pages table\n");
514 /* current->mm->mmap_sem is taken by videobuf2 core */
515 vma = find_vma(current->mm, vaddr);
517 pr_err("no vma for address %lu\n", vaddr);
522 if (vma->vm_end < vaddr + size) {
523 pr_err("vma at %lu is too small for %lu bytes\n", vaddr, size);
528 buf->vma = vb2_get_vma(vma);
530 pr_err("failed to copy vma\n");
535 /* extract page list from userspace mapping */
536 ret = vb2_dc_get_user_pages(start, pages, n_pages, vma, write);
538 pr_err("failed to get user pages\n");
542 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
544 pr_err("failed to allocate sg table\n");
546 goto fail_get_user_pages;
549 ret = sg_alloc_table_from_pages(sgt, pages, n_pages,
550 offset, size, GFP_KERNEL);
552 pr_err("failed to initialize sg table\n");
556 /* pages are no longer needed */
560 sgt->nents = dma_map_sg(buf->dev, sgt->sgl, sgt->orig_nents,
562 if (sgt->nents <= 0) {
563 pr_err("failed to map scatterlist\n");
568 contig_size = vb2_dc_get_contiguous_size(sgt);
569 if (contig_size < size) {
570 pr_err("contiguous mapping is too small %lu/%lu\n",
576 buf->dma_addr = sg_dma_address(sgt->sgl);
583 dma_unmap_sg(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
586 if (!vma_is_io(buf->vma))
587 vb2_dc_sgt_foreach_page(sgt, put_page);
594 if (pages && !vma_is_io(buf->vma))
596 put_page(pages[--n_pages]);
599 vb2_put_vma(buf->vma);
602 kfree(pages); /* kfree is NULL-proof */
610 /*********************************************/
611 /* callbacks for DMABUF buffers */
612 /*********************************************/
614 static int vb2_dc_map_dmabuf(void *mem_priv)
616 struct vb2_dc_buf *buf = mem_priv;
617 struct sg_table *sgt;
618 unsigned long contig_size;
620 if (WARN_ON(!buf->db_attach)) {
621 pr_err("trying to pin a non attached buffer\n");
625 if (WARN_ON(buf->dma_sgt)) {
626 pr_err("dmabuf buffer is already pinned\n");
630 /* get the associated scatterlist for this buffer */
631 sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
632 if (IS_ERR_OR_NULL(sgt)) {
633 pr_err("Error getting dmabuf scatterlist\n");
637 /* checking if dmabuf is big enough to store contiguous chunk */
638 contig_size = vb2_dc_get_contiguous_size(sgt);
639 if (contig_size < buf->size) {
640 pr_err("contiguous chunk is too small %lu/%lu b\n",
641 contig_size, buf->size);
642 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
646 buf->dma_addr = sg_dma_address(sgt->sgl);
652 static void vb2_dc_unmap_dmabuf(void *mem_priv)
654 struct vb2_dc_buf *buf = mem_priv;
655 struct sg_table *sgt = buf->dma_sgt;
657 if (WARN_ON(!buf->db_attach)) {
658 pr_err("trying to unpin a not attached buffer\n");
663 pr_err("dmabuf buffer is already unpinned\n");
667 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
673 static void vb2_dc_detach_dmabuf(void *mem_priv)
675 struct vb2_dc_buf *buf = mem_priv;
677 /* if vb2 works correctly you should never detach mapped buffer */
678 if (WARN_ON(buf->dma_addr))
679 vb2_dc_unmap_dmabuf(buf);
681 /* detach this attachment */
682 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
686 static void *vb2_dc_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
687 unsigned long size, int write)
689 struct vb2_dc_conf *conf = alloc_ctx;
690 struct vb2_dc_buf *buf;
691 struct dma_buf_attachment *dba;
693 if (dbuf->size < size)
694 return ERR_PTR(-EFAULT);
696 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
698 return ERR_PTR(-ENOMEM);
700 buf->dev = conf->dev;
701 /* create attachment for the dmabuf with the user device */
702 dba = dma_buf_attach(dbuf, buf->dev);
704 pr_err("failed to attach dmabuf\n");
709 buf->dma_dir = write ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
711 buf->db_attach = dba;
716 /*********************************************/
717 /* DMA CONTIG exported functions */
718 /*********************************************/
720 const struct vb2_mem_ops vb2_dma_contig_memops = {
721 .alloc = vb2_dc_alloc,
723 .get_dmabuf = vb2_dc_get_dmabuf,
724 .cookie = vb2_dc_cookie,
725 .vaddr = vb2_dc_vaddr,
727 .get_userptr = vb2_dc_get_userptr,
728 .put_userptr = vb2_dc_put_userptr,
729 .prepare = vb2_dc_prepare,
730 .finish = vb2_dc_finish,
731 .map_dmabuf = vb2_dc_map_dmabuf,
732 .unmap_dmabuf = vb2_dc_unmap_dmabuf,
733 .attach_dmabuf = vb2_dc_attach_dmabuf,
734 .detach_dmabuf = vb2_dc_detach_dmabuf,
735 .num_users = vb2_dc_num_users,
737 EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
739 void *vb2_dma_contig_init_ctx(struct device *dev)
741 struct vb2_dc_conf *conf;
743 conf = kzalloc(sizeof *conf, GFP_KERNEL);
745 return ERR_PTR(-ENOMEM);
751 EXPORT_SYMBOL_GPL(vb2_dma_contig_init_ctx);
753 void vb2_dma_contig_cleanup_ctx(void *alloc_ctx)
757 EXPORT_SYMBOL_GPL(vb2_dma_contig_cleanup_ctx);
759 MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
760 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
761 MODULE_LICENSE("GPL");