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/module.h>
14 #include <linux/scatterlist.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/dma-mapping.h>
19 #include <media/videobuf2-core.h>
20 #include <media/videobuf2-dma-contig.h>
21 #include <media/videobuf2-memops.h>
32 enum dma_data_direction dma_dir;
33 struct sg_table *dma_sgt;
36 struct vb2_vmarea_handler handler;
40 struct vm_area_struct *vma;
43 /*********************************************/
44 /* scatterlist table functions */
45 /*********************************************/
48 static void vb2_dc_sgt_foreach_page(struct sg_table *sgt,
49 void (*cb)(struct page *pg))
51 struct scatterlist *s;
54 for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
55 struct page *page = sg_page(s);
56 unsigned int n_pages = PAGE_ALIGN(s->offset + s->length)
60 for (j = 0; j < n_pages; ++j, ++page)
65 static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
67 struct scatterlist *s;
68 dma_addr_t expected = sg_dma_address(sgt->sgl);
70 unsigned long size = 0;
72 for_each_sg(sgt->sgl, s, sgt->nents, i) {
73 if (sg_dma_address(s) != expected)
75 expected = sg_dma_address(s) + sg_dma_len(s);
76 size += sg_dma_len(s);
81 /*********************************************/
82 /* callbacks for all buffers */
83 /*********************************************/
85 static void *vb2_dc_cookie(void *buf_priv)
87 struct vb2_dc_buf *buf = buf_priv;
89 return &buf->dma_addr;
92 static void *vb2_dc_vaddr(void *buf_priv)
94 struct vb2_dc_buf *buf = buf_priv;
99 static unsigned int vb2_dc_num_users(void *buf_priv)
101 struct vb2_dc_buf *buf = buf_priv;
103 return atomic_read(&buf->refcount);
106 static void vb2_dc_prepare(void *buf_priv)
108 struct vb2_dc_buf *buf = buf_priv;
109 struct sg_table *sgt = buf->dma_sgt;
114 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
117 static void vb2_dc_finish(void *buf_priv)
119 struct vb2_dc_buf *buf = buf_priv;
120 struct sg_table *sgt = buf->dma_sgt;
125 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
128 /*********************************************/
129 /* callbacks for MMAP buffers */
130 /*********************************************/
132 static void vb2_dc_put(void *buf_priv)
134 struct vb2_dc_buf *buf = buf_priv;
136 if (!atomic_dec_and_test(&buf->refcount))
139 dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr);
143 static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size)
145 struct vb2_dc_conf *conf = alloc_ctx;
146 struct device *dev = conf->dev;
147 struct vb2_dc_buf *buf;
149 buf = kzalloc(sizeof *buf, GFP_KERNEL);
151 return ERR_PTR(-ENOMEM);
153 buf->vaddr = dma_alloc_coherent(dev, size, &buf->dma_addr, GFP_KERNEL);
155 dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size);
157 return ERR_PTR(-ENOMEM);
163 buf->handler.refcount = &buf->refcount;
164 buf->handler.put = vb2_dc_put;
165 buf->handler.arg = buf;
167 atomic_inc(&buf->refcount);
172 static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
174 struct vb2_dc_buf *buf = buf_priv;
177 printk(KERN_ERR "No buffer to map\n");
181 return vb2_mmap_pfn_range(vma, buf->dma_addr, buf->size,
182 &vb2_common_vm_ops, &buf->handler);
185 /*********************************************/
186 /* callbacks for USERPTR buffers */
187 /*********************************************/
189 static inline int vma_is_io(struct vm_area_struct *vma)
191 return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
194 static int vb2_dc_get_user_pages(unsigned long start, struct page **pages,
195 int n_pages, struct vm_area_struct *vma, int write)
197 if (vma_is_io(vma)) {
200 for (i = 0; i < n_pages; ++i, start += PAGE_SIZE) {
202 int ret = follow_pfn(vma, start, &pfn);
205 pr_err("no page for address %lu\n", start);
208 pages[i] = pfn_to_page(pfn);
213 n = get_user_pages(current, current->mm, start & PAGE_MASK,
214 n_pages, write, 1, pages, NULL);
215 /* negative error means that no page was pinned */
218 pr_err("got only %d of %d user pages\n", n, n_pages);
220 put_page(pages[--n]);
228 static void vb2_dc_put_dirty_page(struct page *page)
230 set_page_dirty_lock(page);
234 static void vb2_dc_put_userptr(void *buf_priv)
236 struct vb2_dc_buf *buf = buf_priv;
237 struct sg_table *sgt = buf->dma_sgt;
239 dma_unmap_sg(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
240 if (!vma_is_io(buf->vma))
241 vb2_dc_sgt_foreach_page(sgt, vb2_dc_put_dirty_page);
245 vb2_put_vma(buf->vma);
249 static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
250 unsigned long size, int write)
252 struct vb2_dc_conf *conf = alloc_ctx;
253 struct vb2_dc_buf *buf;
256 unsigned long offset;
260 struct vm_area_struct *vma;
261 struct sg_table *sgt;
262 unsigned long contig_size;
264 buf = kzalloc(sizeof *buf, GFP_KERNEL);
266 return ERR_PTR(-ENOMEM);
268 buf->dev = conf->dev;
269 buf->dma_dir = write ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
271 start = vaddr & PAGE_MASK;
272 offset = vaddr & ~PAGE_MASK;
273 end = PAGE_ALIGN(vaddr + size);
274 n_pages = (end - start) >> PAGE_SHIFT;
276 pages = kmalloc(n_pages * sizeof(pages[0]), GFP_KERNEL);
279 pr_err("failed to allocate pages table\n");
283 /* current->mm->mmap_sem is taken by videobuf2 core */
284 vma = find_vma(current->mm, vaddr);
286 pr_err("no vma for address %lu\n", vaddr);
291 if (vma->vm_end < vaddr + size) {
292 pr_err("vma at %lu is too small for %lu bytes\n", vaddr, size);
297 buf->vma = vb2_get_vma(vma);
299 pr_err("failed to copy vma\n");
304 /* extract page list from userspace mapping */
305 ret = vb2_dc_get_user_pages(start, pages, n_pages, vma, write);
307 pr_err("failed to get user pages\n");
311 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
313 pr_err("failed to allocate sg table\n");
315 goto fail_get_user_pages;
318 ret = sg_alloc_table_from_pages(sgt, pages, n_pages,
319 offset, size, GFP_KERNEL);
321 pr_err("failed to initialize sg table\n");
325 /* pages are no longer needed */
329 sgt->nents = dma_map_sg(buf->dev, sgt->sgl, sgt->orig_nents,
331 if (sgt->nents <= 0) {
332 pr_err("failed to map scatterlist\n");
337 contig_size = vb2_dc_get_contiguous_size(sgt);
338 if (contig_size < size) {
339 pr_err("contiguous mapping is too small %lu/%lu\n",
345 buf->dma_addr = sg_dma_address(sgt->sgl);
352 dma_unmap_sg(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
355 if (!vma_is_io(buf->vma))
356 vb2_dc_sgt_foreach_page(sgt, put_page);
363 if (pages && !vma_is_io(buf->vma))
365 put_page(pages[--n_pages]);
368 vb2_put_vma(buf->vma);
371 kfree(pages); /* kfree is NULL-proof */
379 /*********************************************/
380 /* DMA CONTIG exported functions */
381 /*********************************************/
383 const struct vb2_mem_ops vb2_dma_contig_memops = {
384 .alloc = vb2_dc_alloc,
386 .cookie = vb2_dc_cookie,
387 .vaddr = vb2_dc_vaddr,
389 .get_userptr = vb2_dc_get_userptr,
390 .put_userptr = vb2_dc_put_userptr,
391 .prepare = vb2_dc_prepare,
392 .finish = vb2_dc_finish,
393 .num_users = vb2_dc_num_users,
395 EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
397 void *vb2_dma_contig_init_ctx(struct device *dev)
399 struct vb2_dc_conf *conf;
401 conf = kzalloc(sizeof *conf, GFP_KERNEL);
403 return ERR_PTR(-ENOMEM);
409 EXPORT_SYMBOL_GPL(vb2_dma_contig_init_ctx);
411 void vb2_dma_contig_cleanup_ctx(void *alloc_ctx)
415 EXPORT_SYMBOL_GPL(vb2_dma_contig_cleanup_ctx);
417 MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
418 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
419 MODULE_LICENSE("GPL");