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;
41 struct vm_area_struct *vma;
44 struct dma_buf_attachment *db_attach;
47 /*********************************************/
48 /* scatterlist table functions */
49 /*********************************************/
52 static void vb2_dc_sgt_foreach_page(struct sg_table *sgt,
53 void (*cb)(struct page *pg))
55 struct scatterlist *s;
58 for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
59 struct page *page = sg_page(s);
60 unsigned int n_pages = PAGE_ALIGN(s->offset + s->length)
64 for (j = 0; j < n_pages; ++j, ++page)
69 static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
71 struct scatterlist *s;
72 dma_addr_t expected = sg_dma_address(sgt->sgl);
74 unsigned long size = 0;
76 for_each_sg(sgt->sgl, s, sgt->nents, i) {
77 if (sg_dma_address(s) != expected)
79 expected = sg_dma_address(s) + sg_dma_len(s);
80 size += sg_dma_len(s);
85 /*********************************************/
86 /* callbacks for all buffers */
87 /*********************************************/
89 static void *vb2_dc_cookie(void *buf_priv)
91 struct vb2_dc_buf *buf = buf_priv;
93 return &buf->dma_addr;
96 static void *vb2_dc_vaddr(void *buf_priv)
98 struct vb2_dc_buf *buf = buf_priv;
103 static unsigned int vb2_dc_num_users(void *buf_priv)
105 struct vb2_dc_buf *buf = buf_priv;
107 return atomic_read(&buf->refcount);
110 static void vb2_dc_prepare(void *buf_priv)
112 struct vb2_dc_buf *buf = buf_priv;
113 struct sg_table *sgt = buf->dma_sgt;
115 /* DMABUF exporter will flush the cache for us */
116 if (!sgt || buf->db_attach)
119 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
122 static void vb2_dc_finish(void *buf_priv)
124 struct vb2_dc_buf *buf = buf_priv;
125 struct sg_table *sgt = buf->dma_sgt;
127 /* DMABUF exporter will flush the cache for us */
128 if (!sgt || buf->db_attach)
131 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
134 /*********************************************/
135 /* callbacks for MMAP buffers */
136 /*********************************************/
138 static void vb2_dc_put(void *buf_priv)
140 struct vb2_dc_buf *buf = buf_priv;
142 if (!atomic_dec_and_test(&buf->refcount))
145 dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr);
149 static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size)
151 struct vb2_dc_conf *conf = alloc_ctx;
152 struct device *dev = conf->dev;
153 struct vb2_dc_buf *buf;
155 buf = kzalloc(sizeof *buf, GFP_KERNEL);
157 return ERR_PTR(-ENOMEM);
159 buf->vaddr = dma_alloc_coherent(dev, size, &buf->dma_addr, GFP_KERNEL);
161 dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size);
163 return ERR_PTR(-ENOMEM);
169 buf->handler.refcount = &buf->refcount;
170 buf->handler.put = vb2_dc_put;
171 buf->handler.arg = buf;
173 atomic_inc(&buf->refcount);
178 static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
180 struct vb2_dc_buf *buf = buf_priv;
184 printk(KERN_ERR "No buffer to map\n");
189 * dma_mmap_* uses vm_pgoff as in-buffer offset, but we want to
194 ret = dma_mmap_coherent(buf->dev, vma, buf->vaddr,
195 buf->dma_addr, buf->size);
198 pr_err("Remapping memory failed, error: %d\n", ret);
202 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
203 vma->vm_private_data = &buf->handler;
204 vma->vm_ops = &vb2_common_vm_ops;
206 vma->vm_ops->open(vma);
208 pr_debug("%s: mapped dma addr 0x%08lx at 0x%08lx, size %ld\n",
209 __func__, (unsigned long)buf->dma_addr, vma->vm_start,
215 /*********************************************/
216 /* callbacks for USERPTR buffers */
217 /*********************************************/
219 static inline int vma_is_io(struct vm_area_struct *vma)
221 return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
224 static int vb2_dc_get_user_pages(unsigned long start, struct page **pages,
225 int n_pages, struct vm_area_struct *vma, int write)
227 if (vma_is_io(vma)) {
230 for (i = 0; i < n_pages; ++i, start += PAGE_SIZE) {
232 int ret = follow_pfn(vma, start, &pfn);
235 pr_err("no page for address %lu\n", start);
238 pages[i] = pfn_to_page(pfn);
243 n = get_user_pages(current, current->mm, start & PAGE_MASK,
244 n_pages, write, 1, pages, NULL);
245 /* negative error means that no page was pinned */
248 pr_err("got only %d of %d user pages\n", n, n_pages);
250 put_page(pages[--n]);
258 static void vb2_dc_put_dirty_page(struct page *page)
260 set_page_dirty_lock(page);
264 static void vb2_dc_put_userptr(void *buf_priv)
266 struct vb2_dc_buf *buf = buf_priv;
267 struct sg_table *sgt = buf->dma_sgt;
269 dma_unmap_sg(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
270 if (!vma_is_io(buf->vma))
271 vb2_dc_sgt_foreach_page(sgt, vb2_dc_put_dirty_page);
275 vb2_put_vma(buf->vma);
279 static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
280 unsigned long size, int write)
282 struct vb2_dc_conf *conf = alloc_ctx;
283 struct vb2_dc_buf *buf;
286 unsigned long offset;
290 struct vm_area_struct *vma;
291 struct sg_table *sgt;
292 unsigned long contig_size;
294 buf = kzalloc(sizeof *buf, GFP_KERNEL);
296 return ERR_PTR(-ENOMEM);
298 buf->dev = conf->dev;
299 buf->dma_dir = write ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
301 start = vaddr & PAGE_MASK;
302 offset = vaddr & ~PAGE_MASK;
303 end = PAGE_ALIGN(vaddr + size);
304 n_pages = (end - start) >> PAGE_SHIFT;
306 pages = kmalloc(n_pages * sizeof(pages[0]), GFP_KERNEL);
309 pr_err("failed to allocate pages table\n");
313 /* current->mm->mmap_sem is taken by videobuf2 core */
314 vma = find_vma(current->mm, vaddr);
316 pr_err("no vma for address %lu\n", vaddr);
321 if (vma->vm_end < vaddr + size) {
322 pr_err("vma at %lu is too small for %lu bytes\n", vaddr, size);
327 buf->vma = vb2_get_vma(vma);
329 pr_err("failed to copy vma\n");
334 /* extract page list from userspace mapping */
335 ret = vb2_dc_get_user_pages(start, pages, n_pages, vma, write);
337 pr_err("failed to get user pages\n");
341 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
343 pr_err("failed to allocate sg table\n");
345 goto fail_get_user_pages;
348 ret = sg_alloc_table_from_pages(sgt, pages, n_pages,
349 offset, size, GFP_KERNEL);
351 pr_err("failed to initialize sg table\n");
355 /* pages are no longer needed */
359 sgt->nents = dma_map_sg(buf->dev, sgt->sgl, sgt->orig_nents,
361 if (sgt->nents <= 0) {
362 pr_err("failed to map scatterlist\n");
367 contig_size = vb2_dc_get_contiguous_size(sgt);
368 if (contig_size < size) {
369 pr_err("contiguous mapping is too small %lu/%lu\n",
375 buf->dma_addr = sg_dma_address(sgt->sgl);
382 dma_unmap_sg(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
385 if (!vma_is_io(buf->vma))
386 vb2_dc_sgt_foreach_page(sgt, put_page);
393 if (pages && !vma_is_io(buf->vma))
395 put_page(pages[--n_pages]);
398 vb2_put_vma(buf->vma);
401 kfree(pages); /* kfree is NULL-proof */
409 /*********************************************/
410 /* callbacks for DMABUF buffers */
411 /*********************************************/
413 static int vb2_dc_map_dmabuf(void *mem_priv)
415 struct vb2_dc_buf *buf = mem_priv;
416 struct sg_table *sgt;
417 unsigned long contig_size;
419 if (WARN_ON(!buf->db_attach)) {
420 pr_err("trying to pin a non attached buffer\n");
424 if (WARN_ON(buf->dma_sgt)) {
425 pr_err("dmabuf buffer is already pinned\n");
429 /* get the associated scatterlist for this buffer */
430 sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
431 if (IS_ERR_OR_NULL(sgt)) {
432 pr_err("Error getting dmabuf scatterlist\n");
436 /* checking if dmabuf is big enough to store contiguous chunk */
437 contig_size = vb2_dc_get_contiguous_size(sgt);
438 if (contig_size < buf->size) {
439 pr_err("contiguous chunk is too small %lu/%lu b\n",
440 contig_size, buf->size);
441 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
445 buf->dma_addr = sg_dma_address(sgt->sgl);
451 static void vb2_dc_unmap_dmabuf(void *mem_priv)
453 struct vb2_dc_buf *buf = mem_priv;
454 struct sg_table *sgt = buf->dma_sgt;
456 if (WARN_ON(!buf->db_attach)) {
457 pr_err("trying to unpin a not attached buffer\n");
462 pr_err("dmabuf buffer is already unpinned\n");
466 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
472 static void vb2_dc_detach_dmabuf(void *mem_priv)
474 struct vb2_dc_buf *buf = mem_priv;
476 /* if vb2 works correctly you should never detach mapped buffer */
477 if (WARN_ON(buf->dma_addr))
478 vb2_dc_unmap_dmabuf(buf);
480 /* detach this attachment */
481 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
485 static void *vb2_dc_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
486 unsigned long size, int write)
488 struct vb2_dc_conf *conf = alloc_ctx;
489 struct vb2_dc_buf *buf;
490 struct dma_buf_attachment *dba;
492 if (dbuf->size < size)
493 return ERR_PTR(-EFAULT);
495 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
497 return ERR_PTR(-ENOMEM);
499 buf->dev = conf->dev;
500 /* create attachment for the dmabuf with the user device */
501 dba = dma_buf_attach(dbuf, buf->dev);
503 pr_err("failed to attach dmabuf\n");
508 buf->dma_dir = write ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
510 buf->db_attach = dba;
515 /*********************************************/
516 /* DMA CONTIG exported functions */
517 /*********************************************/
519 const struct vb2_mem_ops vb2_dma_contig_memops = {
520 .alloc = vb2_dc_alloc,
522 .cookie = vb2_dc_cookie,
523 .vaddr = vb2_dc_vaddr,
525 .get_userptr = vb2_dc_get_userptr,
526 .put_userptr = vb2_dc_put_userptr,
527 .prepare = vb2_dc_prepare,
528 .finish = vb2_dc_finish,
529 .map_dmabuf = vb2_dc_map_dmabuf,
530 .unmap_dmabuf = vb2_dc_unmap_dmabuf,
531 .attach_dmabuf = vb2_dc_attach_dmabuf,
532 .detach_dmabuf = vb2_dc_detach_dmabuf,
533 .num_users = vb2_dc_num_users,
535 EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
537 void *vb2_dma_contig_init_ctx(struct device *dev)
539 struct vb2_dc_conf *conf;
541 conf = kzalloc(sizeof *conf, GFP_KERNEL);
543 return ERR_PTR(-ENOMEM);
549 EXPORT_SYMBOL_GPL(vb2_dma_contig_init_ctx);
551 void vb2_dma_contig_cleanup_ctx(void *alloc_ctx)
555 EXPORT_SYMBOL_GPL(vb2_dma_contig_cleanup_ctx);
557 MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
558 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
559 MODULE_LICENSE("GPL");