pvclock: introduce seqcount-like API
[linux/fpc-iii.git] / drivers / media / v4l2-core / videobuf2-vmalloc.c
blob7e8a07ed8d825fd6d508fe9da9df3802461543c3
1 /*
2 * videobuf2-vmalloc.c - vmalloc 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/io.h>
14 #include <linux/module.h>
15 #include <linux/mm.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
20 #include <media/videobuf2-v4l2.h>
21 #include <media/videobuf2-vmalloc.h>
22 #include <media/videobuf2-memops.h>
24 struct vb2_vmalloc_buf {
25 void *vaddr;
26 struct frame_vector *vec;
27 enum dma_data_direction dma_dir;
28 unsigned long size;
29 atomic_t refcount;
30 struct vb2_vmarea_handler handler;
31 struct dma_buf *dbuf;
34 static void vb2_vmalloc_put(void *buf_priv);
36 static void *vb2_vmalloc_alloc(struct device *dev, const struct dma_attrs *attrs,
37 unsigned long size, enum dma_data_direction dma_dir,
38 gfp_t gfp_flags)
40 struct vb2_vmalloc_buf *buf;
42 buf = kzalloc(sizeof(*buf), GFP_KERNEL | gfp_flags);
43 if (!buf)
44 return NULL;
46 buf->size = size;
47 buf->vaddr = vmalloc_user(buf->size);
48 buf->dma_dir = dma_dir;
49 buf->handler.refcount = &buf->refcount;
50 buf->handler.put = vb2_vmalloc_put;
51 buf->handler.arg = buf;
53 if (!buf->vaddr) {
54 pr_debug("vmalloc of size %ld failed\n", buf->size);
55 kfree(buf);
56 return NULL;
59 atomic_inc(&buf->refcount);
60 return buf;
63 static void vb2_vmalloc_put(void *buf_priv)
65 struct vb2_vmalloc_buf *buf = buf_priv;
67 if (atomic_dec_and_test(&buf->refcount)) {
68 vfree(buf->vaddr);
69 kfree(buf);
73 static void *vb2_vmalloc_get_userptr(struct device *dev, unsigned long vaddr,
74 unsigned long size,
75 enum dma_data_direction dma_dir)
77 struct vb2_vmalloc_buf *buf;
78 struct frame_vector *vec;
79 int n_pages, offset, i;
81 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
82 if (!buf)
83 return NULL;
85 buf->dma_dir = dma_dir;
86 offset = vaddr & ~PAGE_MASK;
87 buf->size = size;
88 vec = vb2_create_framevec(vaddr, size, dma_dir == DMA_FROM_DEVICE);
89 if (IS_ERR(vec))
90 goto fail_pfnvec_create;
91 buf->vec = vec;
92 n_pages = frame_vector_count(vec);
93 if (frame_vector_to_pages(vec) < 0) {
94 unsigned long *nums = frame_vector_pfns(vec);
97 * We cannot get page pointers for these pfns. Check memory is
98 * physically contiguous and use direct mapping.
100 for (i = 1; i < n_pages; i++)
101 if (nums[i-1] + 1 != nums[i])
102 goto fail_map;
103 buf->vaddr = (__force void *)
104 ioremap_nocache(nums[0] << PAGE_SHIFT, size);
105 } else {
106 buf->vaddr = vm_map_ram(frame_vector_pages(vec), n_pages, -1,
107 PAGE_KERNEL);
110 if (!buf->vaddr)
111 goto fail_map;
112 buf->vaddr += offset;
113 return buf;
115 fail_map:
116 vb2_destroy_framevec(vec);
117 fail_pfnvec_create:
118 kfree(buf);
120 return NULL;
123 static void vb2_vmalloc_put_userptr(void *buf_priv)
125 struct vb2_vmalloc_buf *buf = buf_priv;
126 unsigned long vaddr = (unsigned long)buf->vaddr & PAGE_MASK;
127 unsigned int i;
128 struct page **pages;
129 unsigned int n_pages;
131 if (!buf->vec->is_pfns) {
132 n_pages = frame_vector_count(buf->vec);
133 pages = frame_vector_pages(buf->vec);
134 if (vaddr)
135 vm_unmap_ram((void *)vaddr, n_pages);
136 if (buf->dma_dir == DMA_FROM_DEVICE)
137 for (i = 0; i < n_pages; i++)
138 set_page_dirty_lock(pages[i]);
139 } else {
140 iounmap((__force void __iomem *)buf->vaddr);
142 vb2_destroy_framevec(buf->vec);
143 kfree(buf);
146 static void *vb2_vmalloc_vaddr(void *buf_priv)
148 struct vb2_vmalloc_buf *buf = buf_priv;
150 if (!buf->vaddr) {
151 pr_err("Address of an unallocated plane requested "
152 "or cannot map user pointer\n");
153 return NULL;
156 return buf->vaddr;
159 static unsigned int vb2_vmalloc_num_users(void *buf_priv)
161 struct vb2_vmalloc_buf *buf = buf_priv;
162 return atomic_read(&buf->refcount);
165 static int vb2_vmalloc_mmap(void *buf_priv, struct vm_area_struct *vma)
167 struct vb2_vmalloc_buf *buf = buf_priv;
168 int ret;
170 if (!buf) {
171 pr_err("No memory to map\n");
172 return -EINVAL;
175 ret = remap_vmalloc_range(vma, buf->vaddr, 0);
176 if (ret) {
177 pr_err("Remapping vmalloc memory, error: %d\n", ret);
178 return ret;
182 * Make sure that vm_areas for 2 buffers won't be merged together
184 vma->vm_flags |= VM_DONTEXPAND;
187 * Use common vm_area operations to track buffer refcount.
189 vma->vm_private_data = &buf->handler;
190 vma->vm_ops = &vb2_common_vm_ops;
192 vma->vm_ops->open(vma);
194 return 0;
197 #ifdef CONFIG_HAS_DMA
198 /*********************************************/
199 /* DMABUF ops for exporters */
200 /*********************************************/
202 struct vb2_vmalloc_attachment {
203 struct sg_table sgt;
204 enum dma_data_direction dma_dir;
207 static int vb2_vmalloc_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
208 struct dma_buf_attachment *dbuf_attach)
210 struct vb2_vmalloc_attachment *attach;
211 struct vb2_vmalloc_buf *buf = dbuf->priv;
212 int num_pages = PAGE_ALIGN(buf->size) / PAGE_SIZE;
213 struct sg_table *sgt;
214 struct scatterlist *sg;
215 void *vaddr = buf->vaddr;
216 int ret;
217 int i;
219 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
220 if (!attach)
221 return -ENOMEM;
223 sgt = &attach->sgt;
224 ret = sg_alloc_table(sgt, num_pages, GFP_KERNEL);
225 if (ret) {
226 kfree(attach);
227 return ret;
229 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
230 struct page *page = vmalloc_to_page(vaddr);
232 if (!page) {
233 sg_free_table(sgt);
234 kfree(attach);
235 return -ENOMEM;
237 sg_set_page(sg, page, PAGE_SIZE, 0);
238 vaddr += PAGE_SIZE;
241 attach->dma_dir = DMA_NONE;
242 dbuf_attach->priv = attach;
243 return 0;
246 static void vb2_vmalloc_dmabuf_ops_detach(struct dma_buf *dbuf,
247 struct dma_buf_attachment *db_attach)
249 struct vb2_vmalloc_attachment *attach = db_attach->priv;
250 struct sg_table *sgt;
252 if (!attach)
253 return;
255 sgt = &attach->sgt;
257 /* release the scatterlist cache */
258 if (attach->dma_dir != DMA_NONE)
259 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
260 attach->dma_dir);
261 sg_free_table(sgt);
262 kfree(attach);
263 db_attach->priv = NULL;
266 static struct sg_table *vb2_vmalloc_dmabuf_ops_map(
267 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
269 struct vb2_vmalloc_attachment *attach = db_attach->priv;
270 /* stealing dmabuf mutex to serialize map/unmap operations */
271 struct mutex *lock = &db_attach->dmabuf->lock;
272 struct sg_table *sgt;
274 mutex_lock(lock);
276 sgt = &attach->sgt;
277 /* return previously mapped sg table */
278 if (attach->dma_dir == dma_dir) {
279 mutex_unlock(lock);
280 return sgt;
283 /* release any previous cache */
284 if (attach->dma_dir != DMA_NONE) {
285 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
286 attach->dma_dir);
287 attach->dma_dir = DMA_NONE;
290 /* mapping to the client with new direction */
291 sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
292 dma_dir);
293 if (!sgt->nents) {
294 pr_err("failed to map scatterlist\n");
295 mutex_unlock(lock);
296 return ERR_PTR(-EIO);
299 attach->dma_dir = dma_dir;
301 mutex_unlock(lock);
303 return sgt;
306 static void vb2_vmalloc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
307 struct sg_table *sgt, enum dma_data_direction dma_dir)
309 /* nothing to be done here */
312 static void vb2_vmalloc_dmabuf_ops_release(struct dma_buf *dbuf)
314 /* drop reference obtained in vb2_vmalloc_get_dmabuf */
315 vb2_vmalloc_put(dbuf->priv);
318 static void *vb2_vmalloc_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
320 struct vb2_vmalloc_buf *buf = dbuf->priv;
322 return buf->vaddr + pgnum * PAGE_SIZE;
325 static void *vb2_vmalloc_dmabuf_ops_vmap(struct dma_buf *dbuf)
327 struct vb2_vmalloc_buf *buf = dbuf->priv;
329 return buf->vaddr;
332 static int vb2_vmalloc_dmabuf_ops_mmap(struct dma_buf *dbuf,
333 struct vm_area_struct *vma)
335 return vb2_vmalloc_mmap(dbuf->priv, vma);
338 static struct dma_buf_ops vb2_vmalloc_dmabuf_ops = {
339 .attach = vb2_vmalloc_dmabuf_ops_attach,
340 .detach = vb2_vmalloc_dmabuf_ops_detach,
341 .map_dma_buf = vb2_vmalloc_dmabuf_ops_map,
342 .unmap_dma_buf = vb2_vmalloc_dmabuf_ops_unmap,
343 .kmap = vb2_vmalloc_dmabuf_ops_kmap,
344 .kmap_atomic = vb2_vmalloc_dmabuf_ops_kmap,
345 .vmap = vb2_vmalloc_dmabuf_ops_vmap,
346 .mmap = vb2_vmalloc_dmabuf_ops_mmap,
347 .release = vb2_vmalloc_dmabuf_ops_release,
350 static struct dma_buf *vb2_vmalloc_get_dmabuf(void *buf_priv, unsigned long flags)
352 struct vb2_vmalloc_buf *buf = buf_priv;
353 struct dma_buf *dbuf;
354 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
356 exp_info.ops = &vb2_vmalloc_dmabuf_ops;
357 exp_info.size = buf->size;
358 exp_info.flags = flags;
359 exp_info.priv = buf;
361 if (WARN_ON(!buf->vaddr))
362 return NULL;
364 dbuf = dma_buf_export(&exp_info);
365 if (IS_ERR(dbuf))
366 return NULL;
368 /* dmabuf keeps reference to vb2 buffer */
369 atomic_inc(&buf->refcount);
371 return dbuf;
373 #endif /* CONFIG_HAS_DMA */
376 /*********************************************/
377 /* callbacks for DMABUF buffers */
378 /*********************************************/
380 static int vb2_vmalloc_map_dmabuf(void *mem_priv)
382 struct vb2_vmalloc_buf *buf = mem_priv;
384 buf->vaddr = dma_buf_vmap(buf->dbuf);
386 return buf->vaddr ? 0 : -EFAULT;
389 static void vb2_vmalloc_unmap_dmabuf(void *mem_priv)
391 struct vb2_vmalloc_buf *buf = mem_priv;
393 dma_buf_vunmap(buf->dbuf, buf->vaddr);
394 buf->vaddr = NULL;
397 static void vb2_vmalloc_detach_dmabuf(void *mem_priv)
399 struct vb2_vmalloc_buf *buf = mem_priv;
401 if (buf->vaddr)
402 dma_buf_vunmap(buf->dbuf, buf->vaddr);
404 kfree(buf);
407 static void *vb2_vmalloc_attach_dmabuf(struct device *dev, struct dma_buf *dbuf,
408 unsigned long size, enum dma_data_direction dma_dir)
410 struct vb2_vmalloc_buf *buf;
412 if (dbuf->size < size)
413 return ERR_PTR(-EFAULT);
415 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
416 if (!buf)
417 return ERR_PTR(-ENOMEM);
419 buf->dbuf = dbuf;
420 buf->dma_dir = dma_dir;
421 buf->size = size;
423 return buf;
427 const struct vb2_mem_ops vb2_vmalloc_memops = {
428 .alloc = vb2_vmalloc_alloc,
429 .put = vb2_vmalloc_put,
430 .get_userptr = vb2_vmalloc_get_userptr,
431 .put_userptr = vb2_vmalloc_put_userptr,
432 #ifdef CONFIG_HAS_DMA
433 .get_dmabuf = vb2_vmalloc_get_dmabuf,
434 #endif
435 .map_dmabuf = vb2_vmalloc_map_dmabuf,
436 .unmap_dmabuf = vb2_vmalloc_unmap_dmabuf,
437 .attach_dmabuf = vb2_vmalloc_attach_dmabuf,
438 .detach_dmabuf = vb2_vmalloc_detach_dmabuf,
439 .vaddr = vb2_vmalloc_vaddr,
440 .mmap = vb2_vmalloc_mmap,
441 .num_users = vb2_vmalloc_num_users,
443 EXPORT_SYMBOL_GPL(vb2_vmalloc_memops);
445 MODULE_DESCRIPTION("vmalloc memory handling routines for videobuf2");
446 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
447 MODULE_LICENSE("GPL");