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;
101 if (!buf->vaddr && buf->db_attach)
102 buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
107 static unsigned int vb2_dc_num_users(void *buf_priv)
109 struct vb2_dc_buf *buf = buf_priv;
111 return atomic_read(&buf->refcount);
114 static void vb2_dc_prepare(void *buf_priv)
116 struct vb2_dc_buf *buf = buf_priv;
117 struct sg_table *sgt = buf->dma_sgt;
119 /* DMABUF exporter will flush the cache for us */
120 if (!sgt || buf->db_attach)
123 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
126 static void vb2_dc_finish(void *buf_priv)
128 struct vb2_dc_buf *buf = buf_priv;
129 struct sg_table *sgt = buf->dma_sgt;
131 /* DMABUF exporter will flush the cache for us */
132 if (!sgt || buf->db_attach)
135 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
138 /*********************************************/
139 /* callbacks for MMAP buffers */
140 /*********************************************/
142 static void vb2_dc_put(void *buf_priv)
144 struct vb2_dc_buf *buf = buf_priv;
146 if (!atomic_dec_and_test(&buf->refcount))
150 sg_free_table(buf->sgt_base);
151 kfree(buf->sgt_base);
153 dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr);
154 put_device(buf->dev);
158 static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size,
159 enum dma_data_direction dma_dir, gfp_t gfp_flags)
161 struct vb2_dc_conf *conf = alloc_ctx;
162 struct device *dev = conf->dev;
163 struct vb2_dc_buf *buf;
165 buf = kzalloc(sizeof *buf, GFP_KERNEL);
167 return ERR_PTR(-ENOMEM);
169 buf->vaddr = dma_alloc_coherent(dev, size, &buf->dma_addr,
170 GFP_KERNEL | gfp_flags);
172 dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size);
174 return ERR_PTR(-ENOMEM);
177 /* Prevent the device from being released while the buffer is used */
178 buf->dev = get_device(dev);
180 buf->dma_dir = dma_dir;
182 buf->handler.refcount = &buf->refcount;
183 buf->handler.put = vb2_dc_put;
184 buf->handler.arg = buf;
186 atomic_inc(&buf->refcount);
191 static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
193 struct vb2_dc_buf *buf = buf_priv;
197 printk(KERN_ERR "No buffer to map\n");
202 * dma_mmap_* uses vm_pgoff as in-buffer offset, but we want to
207 ret = dma_mmap_coherent(buf->dev, vma, buf->vaddr,
208 buf->dma_addr, buf->size);
211 pr_err("Remapping memory failed, error: %d\n", ret);
215 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
216 vma->vm_private_data = &buf->handler;
217 vma->vm_ops = &vb2_common_vm_ops;
219 vma->vm_ops->open(vma);
221 pr_debug("%s: mapped dma addr 0x%08lx at 0x%08lx, size %ld\n",
222 __func__, (unsigned long)buf->dma_addr, vma->vm_start,
228 /*********************************************/
229 /* DMABUF ops for exporters */
230 /*********************************************/
232 struct vb2_dc_attachment {
234 enum dma_data_direction dma_dir;
237 static int vb2_dc_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
238 struct dma_buf_attachment *dbuf_attach)
240 struct vb2_dc_attachment *attach;
242 struct scatterlist *rd, *wr;
243 struct sg_table *sgt;
244 struct vb2_dc_buf *buf = dbuf->priv;
247 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
252 /* Copy the buf->base_sgt scatter list to the attachment, as we can't
253 * map the same scatter list to multiple attachments at the same time.
255 ret = sg_alloc_table(sgt, buf->sgt_base->orig_nents, GFP_KERNEL);
261 rd = buf->sgt_base->sgl;
263 for (i = 0; i < sgt->orig_nents; ++i) {
264 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
269 attach->dma_dir = DMA_NONE;
270 dbuf_attach->priv = attach;
275 static void vb2_dc_dmabuf_ops_detach(struct dma_buf *dbuf,
276 struct dma_buf_attachment *db_attach)
278 struct vb2_dc_attachment *attach = db_attach->priv;
279 struct sg_table *sgt;
286 /* release the scatterlist cache */
287 if (attach->dma_dir != DMA_NONE)
288 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
292 db_attach->priv = NULL;
295 static struct sg_table *vb2_dc_dmabuf_ops_map(
296 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
298 struct vb2_dc_attachment *attach = db_attach->priv;
299 /* stealing dmabuf mutex to serialize map/unmap operations */
300 struct mutex *lock = &db_attach->dmabuf->lock;
301 struct sg_table *sgt;
307 /* return previously mapped sg table */
308 if (attach->dma_dir == dma_dir) {
313 /* release any previous cache */
314 if (attach->dma_dir != DMA_NONE) {
315 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
317 attach->dma_dir = DMA_NONE;
320 /* mapping to the client with new direction */
321 ret = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, dma_dir);
323 pr_err("failed to map scatterlist\n");
325 return ERR_PTR(-EIO);
328 attach->dma_dir = dma_dir;
335 static void vb2_dc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
336 struct sg_table *sgt, enum dma_data_direction dma_dir)
338 /* nothing to be done here */
341 static void vb2_dc_dmabuf_ops_release(struct dma_buf *dbuf)
343 /* drop reference obtained in vb2_dc_get_dmabuf */
344 vb2_dc_put(dbuf->priv);
347 static void *vb2_dc_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
349 struct vb2_dc_buf *buf = dbuf->priv;
351 return buf->vaddr + pgnum * PAGE_SIZE;
354 static void *vb2_dc_dmabuf_ops_vmap(struct dma_buf *dbuf)
356 struct vb2_dc_buf *buf = dbuf->priv;
361 static int vb2_dc_dmabuf_ops_mmap(struct dma_buf *dbuf,
362 struct vm_area_struct *vma)
364 return vb2_dc_mmap(dbuf->priv, vma);
367 static struct dma_buf_ops vb2_dc_dmabuf_ops = {
368 .attach = vb2_dc_dmabuf_ops_attach,
369 .detach = vb2_dc_dmabuf_ops_detach,
370 .map_dma_buf = vb2_dc_dmabuf_ops_map,
371 .unmap_dma_buf = vb2_dc_dmabuf_ops_unmap,
372 .kmap = vb2_dc_dmabuf_ops_kmap,
373 .kmap_atomic = vb2_dc_dmabuf_ops_kmap,
374 .vmap = vb2_dc_dmabuf_ops_vmap,
375 .mmap = vb2_dc_dmabuf_ops_mmap,
376 .release = vb2_dc_dmabuf_ops_release,
379 static struct sg_table *vb2_dc_get_base_sgt(struct vb2_dc_buf *buf)
382 struct sg_table *sgt;
384 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
386 dev_err(buf->dev, "failed to alloc sg table\n");
390 ret = dma_get_sgtable(buf->dev, sgt, buf->vaddr, buf->dma_addr,
393 dev_err(buf->dev, "failed to get scatterlist from DMA API\n");
401 static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv, unsigned long flags)
403 struct vb2_dc_buf *buf = buf_priv;
404 struct dma_buf *dbuf;
407 buf->sgt_base = vb2_dc_get_base_sgt(buf);
409 if (WARN_ON(!buf->sgt_base))
412 dbuf = dma_buf_export(buf, &vb2_dc_dmabuf_ops, buf->size, flags, NULL);
416 /* dmabuf keeps reference to vb2 buffer */
417 atomic_inc(&buf->refcount);
422 /*********************************************/
423 /* callbacks for USERPTR buffers */
424 /*********************************************/
426 static inline int vma_is_io(struct vm_area_struct *vma)
428 return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
431 static int vb2_dc_get_user_pfn(unsigned long start, int n_pages,
432 struct vm_area_struct *vma, unsigned long *res)
434 unsigned long pfn, start_pfn, prev_pfn;
441 ret = follow_pfn(vma, start, &pfn);
448 for (i = 1; i < n_pages; ++i, start += PAGE_SIZE) {
450 ret = follow_pfn(vma, start, &pfn);
453 pr_err("no page for address %lu\n", start);
456 if (pfn != prev_pfn + 1)
464 static int vb2_dc_get_user_pages(unsigned long start, struct page **pages,
465 int n_pages, struct vm_area_struct *vma,
466 enum dma_data_direction dma_dir)
468 if (vma_is_io(vma)) {
471 for (i = 0; i < n_pages; ++i, start += PAGE_SIZE) {
473 int ret = follow_pfn(vma, start, &pfn);
479 pr_err("no page for address %lu\n", start);
482 pages[i] = pfn_to_page(pfn);
487 n = get_user_pages(current, current->mm, start & PAGE_MASK,
488 n_pages, dma_dir == DMA_FROM_DEVICE, 1, pages, NULL);
489 /* negative error means that no page was pinned */
492 pr_err("got only %d of %d user pages\n", n, n_pages);
494 put_page(pages[--n]);
502 static void vb2_dc_put_dirty_page(struct page *page)
504 set_page_dirty_lock(page);
508 static void vb2_dc_put_userptr(void *buf_priv)
510 struct vb2_dc_buf *buf = buf_priv;
511 struct sg_table *sgt = buf->dma_sgt;
514 DEFINE_DMA_ATTRS(attrs);
516 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
518 * No need to sync to CPU, it's already synced to the CPU
519 * since the finish() memop will have been called before this.
521 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
522 buf->dma_dir, &attrs);
523 if (!vma_is_io(buf->vma))
524 vb2_dc_sgt_foreach_page(sgt, vb2_dc_put_dirty_page);
529 vb2_put_vma(buf->vma);
534 * For some kind of reserved memory there might be no struct page available,
535 * so all that can be done to support such 'pages' is to try to convert
536 * pfn to dma address or at the last resort just assume that
537 * dma address == physical address (like it has been assumed in earlier version
538 * of videobuf2-dma-contig
541 #ifdef __arch_pfn_to_dma
542 static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
544 return (dma_addr_t)__arch_pfn_to_dma(dev, pfn);
546 #elif defined(__pfn_to_bus)
547 static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
549 return (dma_addr_t)__pfn_to_bus(pfn);
551 #elif defined(__pfn_to_phys)
552 static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
554 return (dma_addr_t)__pfn_to_phys(pfn);
557 static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
559 /* really, we cannot do anything better at this point */
560 return (dma_addr_t)(pfn) << PAGE_SHIFT;
564 static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
565 unsigned long size, enum dma_data_direction dma_dir)
567 struct vb2_dc_conf *conf = alloc_ctx;
568 struct vb2_dc_buf *buf;
571 unsigned long offset;
575 struct vm_area_struct *vma;
576 struct sg_table *sgt;
577 unsigned long contig_size;
578 unsigned long dma_align = dma_get_cache_alignment();
579 DEFINE_DMA_ATTRS(attrs);
581 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
583 /* Only cache aligned DMA transfers are reliable */
584 if (!IS_ALIGNED(vaddr | size, dma_align)) {
585 pr_debug("user data must be aligned to %lu bytes\n", dma_align);
586 return ERR_PTR(-EINVAL);
590 pr_debug("size is zero\n");
591 return ERR_PTR(-EINVAL);
594 buf = kzalloc(sizeof *buf, GFP_KERNEL);
596 return ERR_PTR(-ENOMEM);
598 buf->dev = conf->dev;
599 buf->dma_dir = dma_dir;
601 start = vaddr & PAGE_MASK;
602 offset = vaddr & ~PAGE_MASK;
603 end = PAGE_ALIGN(vaddr + size);
604 n_pages = (end - start) >> PAGE_SHIFT;
606 pages = kmalloc(n_pages * sizeof(pages[0]), GFP_KERNEL);
609 pr_err("failed to allocate pages table\n");
613 /* current->mm->mmap_sem is taken by videobuf2 core */
614 vma = find_vma(current->mm, vaddr);
616 pr_err("no vma for address %lu\n", vaddr);
621 if (vma->vm_end < vaddr + size) {
622 pr_err("vma at %lu is too small for %lu bytes\n", vaddr, size);
627 buf->vma = vb2_get_vma(vma);
629 pr_err("failed to copy vma\n");
634 /* extract page list from userspace mapping */
635 ret = vb2_dc_get_user_pages(start, pages, n_pages, vma,
636 dma_dir == DMA_FROM_DEVICE);
639 if (vb2_dc_get_user_pfn(start, n_pages, vma, &pfn) == 0) {
640 buf->dma_addr = vb2_dc_pfn_to_dma(buf->dev, pfn);
646 pr_err("failed to get user pages\n");
650 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
652 pr_err("failed to allocate sg table\n");
654 goto fail_get_user_pages;
657 ret = sg_alloc_table_from_pages(sgt, pages, n_pages,
658 offset, size, GFP_KERNEL);
660 pr_err("failed to initialize sg table\n");
664 /* pages are no longer needed */
669 * No need to sync to the device, this will happen later when the
670 * prepare() memop is called.
672 sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
673 buf->dma_dir, &attrs);
674 if (sgt->nents <= 0) {
675 pr_err("failed to map scatterlist\n");
680 contig_size = vb2_dc_get_contiguous_size(sgt);
681 if (contig_size < size) {
682 pr_err("contiguous mapping is too small %lu/%lu\n",
688 buf->dma_addr = sg_dma_address(sgt->sgl);
695 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
696 buf->dma_dir, &attrs);
699 if (!vma_is_io(buf->vma))
700 vb2_dc_sgt_foreach_page(sgt, put_page);
707 if (pages && !vma_is_io(buf->vma))
709 put_page(pages[--n_pages]);
712 vb2_put_vma(buf->vma);
715 kfree(pages); /* kfree is NULL-proof */
723 /*********************************************/
724 /* callbacks for DMABUF buffers */
725 /*********************************************/
727 static int vb2_dc_map_dmabuf(void *mem_priv)
729 struct vb2_dc_buf *buf = mem_priv;
730 struct sg_table *sgt;
731 unsigned long contig_size;
733 if (WARN_ON(!buf->db_attach)) {
734 pr_err("trying to pin a non attached buffer\n");
738 if (WARN_ON(buf->dma_sgt)) {
739 pr_err("dmabuf buffer is already pinned\n");
743 /* get the associated scatterlist for this buffer */
744 sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
746 pr_err("Error getting dmabuf scatterlist\n");
750 /* checking if dmabuf is big enough to store contiguous chunk */
751 contig_size = vb2_dc_get_contiguous_size(sgt);
752 if (contig_size < buf->size) {
753 pr_err("contiguous chunk is too small %lu/%lu b\n",
754 contig_size, buf->size);
755 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
759 buf->dma_addr = sg_dma_address(sgt->sgl);
766 static void vb2_dc_unmap_dmabuf(void *mem_priv)
768 struct vb2_dc_buf *buf = mem_priv;
769 struct sg_table *sgt = buf->dma_sgt;
771 if (WARN_ON(!buf->db_attach)) {
772 pr_err("trying to unpin a not attached buffer\n");
777 pr_err("dmabuf buffer is already unpinned\n");
782 dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
785 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
791 static void vb2_dc_detach_dmabuf(void *mem_priv)
793 struct vb2_dc_buf *buf = mem_priv;
795 /* if vb2 works correctly you should never detach mapped buffer */
796 if (WARN_ON(buf->dma_addr))
797 vb2_dc_unmap_dmabuf(buf);
799 /* detach this attachment */
800 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
804 static void *vb2_dc_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
805 unsigned long size, enum dma_data_direction dma_dir)
807 struct vb2_dc_conf *conf = alloc_ctx;
808 struct vb2_dc_buf *buf;
809 struct dma_buf_attachment *dba;
811 if (dbuf->size < size)
812 return ERR_PTR(-EFAULT);
814 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
816 return ERR_PTR(-ENOMEM);
818 buf->dev = conf->dev;
819 /* create attachment for the dmabuf with the user device */
820 dba = dma_buf_attach(dbuf, buf->dev);
822 pr_err("failed to attach dmabuf\n");
827 buf->dma_dir = dma_dir;
829 buf->db_attach = dba;
834 /*********************************************/
835 /* DMA CONTIG exported functions */
836 /*********************************************/
838 const struct vb2_mem_ops vb2_dma_contig_memops = {
839 .alloc = vb2_dc_alloc,
841 .get_dmabuf = vb2_dc_get_dmabuf,
842 .cookie = vb2_dc_cookie,
843 .vaddr = vb2_dc_vaddr,
845 .get_userptr = vb2_dc_get_userptr,
846 .put_userptr = vb2_dc_put_userptr,
847 .prepare = vb2_dc_prepare,
848 .finish = vb2_dc_finish,
849 .map_dmabuf = vb2_dc_map_dmabuf,
850 .unmap_dmabuf = vb2_dc_unmap_dmabuf,
851 .attach_dmabuf = vb2_dc_attach_dmabuf,
852 .detach_dmabuf = vb2_dc_detach_dmabuf,
853 .num_users = vb2_dc_num_users,
855 EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
857 void *vb2_dma_contig_init_ctx(struct device *dev)
859 struct vb2_dc_conf *conf;
861 conf = kzalloc(sizeof *conf, GFP_KERNEL);
863 return ERR_PTR(-ENOMEM);
869 EXPORT_SYMBOL_GPL(vb2_dma_contig_init_ctx);
871 void vb2_dma_contig_cleanup_ctx(void *alloc_ctx)
873 if (!IS_ERR_OR_NULL(alloc_ctx))
876 EXPORT_SYMBOL_GPL(vb2_dma_contig_cleanup_ctx);
878 MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
879 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
880 MODULE_LICENSE("GPL");