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.
14 #include <linux/module.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
{
26 struct frame_vector
*vec
;
27 enum dma_data_direction dma_dir
;
30 struct vb2_vmarea_handler handler
;
34 static void vb2_vmalloc_put(void *buf_priv
);
36 static void *vb2_vmalloc_alloc(struct device
*dev
, unsigned long attrs
,
37 unsigned long size
, enum dma_data_direction dma_dir
,
40 struct vb2_vmalloc_buf
*buf
;
42 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
| gfp_flags
);
44 return ERR_PTR(-ENOMEM
);
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
;
54 pr_debug("vmalloc of size %ld failed\n", buf
->size
);
56 return ERR_PTR(-ENOMEM
);
59 atomic_inc(&buf
->refcount
);
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
)) {
73 static void *vb2_vmalloc_get_userptr(struct device
*dev
, unsigned long vaddr
,
75 enum dma_data_direction dma_dir
)
77 struct vb2_vmalloc_buf
*buf
;
78 struct frame_vector
*vec
;
79 int n_pages
, offset
, i
;
82 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
84 return ERR_PTR(-ENOMEM
);
86 buf
->dma_dir
= dma_dir
;
87 offset
= vaddr
& ~PAGE_MASK
;
89 vec
= vb2_create_framevec(vaddr
, size
, dma_dir
== DMA_FROM_DEVICE
);
92 goto fail_pfnvec_create
;
95 n_pages
= frame_vector_count(vec
);
96 if (frame_vector_to_pages(vec
) < 0) {
97 unsigned long *nums
= frame_vector_pfns(vec
);
100 * We cannot get page pointers for these pfns. Check memory is
101 * physically contiguous and use direct mapping.
103 for (i
= 1; i
< n_pages
; i
++)
104 if (nums
[i
-1] + 1 != nums
[i
])
106 buf
->vaddr
= (__force
void *)
107 ioremap_nocache(nums
[0] << PAGE_SHIFT
, size
);
109 buf
->vaddr
= vm_map_ram(frame_vector_pages(vec
), n_pages
, -1,
115 buf
->vaddr
+= offset
;
119 vb2_destroy_framevec(vec
);
126 static void vb2_vmalloc_put_userptr(void *buf_priv
)
128 struct vb2_vmalloc_buf
*buf
= buf_priv
;
129 unsigned long vaddr
= (unsigned long)buf
->vaddr
& PAGE_MASK
;
132 unsigned int n_pages
;
134 if (!buf
->vec
->is_pfns
) {
135 n_pages
= frame_vector_count(buf
->vec
);
136 pages
= frame_vector_pages(buf
->vec
);
138 vm_unmap_ram((void *)vaddr
, n_pages
);
139 if (buf
->dma_dir
== DMA_FROM_DEVICE
)
140 for (i
= 0; i
< n_pages
; i
++)
141 set_page_dirty_lock(pages
[i
]);
143 iounmap((__force
void __iomem
*)buf
->vaddr
);
145 vb2_destroy_framevec(buf
->vec
);
149 static void *vb2_vmalloc_vaddr(void *buf_priv
)
151 struct vb2_vmalloc_buf
*buf
= buf_priv
;
154 pr_err("Address of an unallocated plane requested or cannot map user pointer\n");
161 static unsigned int vb2_vmalloc_num_users(void *buf_priv
)
163 struct vb2_vmalloc_buf
*buf
= buf_priv
;
164 return atomic_read(&buf
->refcount
);
167 static int vb2_vmalloc_mmap(void *buf_priv
, struct vm_area_struct
*vma
)
169 struct vb2_vmalloc_buf
*buf
= buf_priv
;
173 pr_err("No memory to map\n");
177 ret
= remap_vmalloc_range(vma
, buf
->vaddr
, 0);
179 pr_err("Remapping vmalloc memory, error: %d\n", ret
);
184 * Make sure that vm_areas for 2 buffers won't be merged together
186 vma
->vm_flags
|= VM_DONTEXPAND
;
189 * Use common vm_area operations to track buffer refcount.
191 vma
->vm_private_data
= &buf
->handler
;
192 vma
->vm_ops
= &vb2_common_vm_ops
;
194 vma
->vm_ops
->open(vma
);
199 #ifdef CONFIG_HAS_DMA
200 /*********************************************/
201 /* DMABUF ops for exporters */
202 /*********************************************/
204 struct vb2_vmalloc_attachment
{
206 enum dma_data_direction dma_dir
;
209 static int vb2_vmalloc_dmabuf_ops_attach(struct dma_buf
*dbuf
, struct device
*dev
,
210 struct dma_buf_attachment
*dbuf_attach
)
212 struct vb2_vmalloc_attachment
*attach
;
213 struct vb2_vmalloc_buf
*buf
= dbuf
->priv
;
214 int num_pages
= PAGE_ALIGN(buf
->size
) / PAGE_SIZE
;
215 struct sg_table
*sgt
;
216 struct scatterlist
*sg
;
217 void *vaddr
= buf
->vaddr
;
221 attach
= kzalloc(sizeof(*attach
), GFP_KERNEL
);
226 ret
= sg_alloc_table(sgt
, num_pages
, GFP_KERNEL
);
231 for_each_sg(sgt
->sgl
, sg
, sgt
->nents
, i
) {
232 struct page
*page
= vmalloc_to_page(vaddr
);
239 sg_set_page(sg
, page
, PAGE_SIZE
, 0);
243 attach
->dma_dir
= DMA_NONE
;
244 dbuf_attach
->priv
= attach
;
248 static void vb2_vmalloc_dmabuf_ops_detach(struct dma_buf
*dbuf
,
249 struct dma_buf_attachment
*db_attach
)
251 struct vb2_vmalloc_attachment
*attach
= db_attach
->priv
;
252 struct sg_table
*sgt
;
259 /* release the scatterlist cache */
260 if (attach
->dma_dir
!= DMA_NONE
)
261 dma_unmap_sg(db_attach
->dev
, sgt
->sgl
, sgt
->orig_nents
,
265 db_attach
->priv
= NULL
;
268 static struct sg_table
*vb2_vmalloc_dmabuf_ops_map(
269 struct dma_buf_attachment
*db_attach
, enum dma_data_direction dma_dir
)
271 struct vb2_vmalloc_attachment
*attach
= db_attach
->priv
;
272 /* stealing dmabuf mutex to serialize map/unmap operations */
273 struct mutex
*lock
= &db_attach
->dmabuf
->lock
;
274 struct sg_table
*sgt
;
279 /* return previously mapped sg table */
280 if (attach
->dma_dir
== dma_dir
) {
285 /* release any previous cache */
286 if (attach
->dma_dir
!= DMA_NONE
) {
287 dma_unmap_sg(db_attach
->dev
, sgt
->sgl
, sgt
->orig_nents
,
289 attach
->dma_dir
= DMA_NONE
;
292 /* mapping to the client with new direction */
293 sgt
->nents
= dma_map_sg(db_attach
->dev
, sgt
->sgl
, sgt
->orig_nents
,
296 pr_err("failed to map scatterlist\n");
298 return ERR_PTR(-EIO
);
301 attach
->dma_dir
= dma_dir
;
308 static void vb2_vmalloc_dmabuf_ops_unmap(struct dma_buf_attachment
*db_attach
,
309 struct sg_table
*sgt
, enum dma_data_direction dma_dir
)
311 /* nothing to be done here */
314 static void vb2_vmalloc_dmabuf_ops_release(struct dma_buf
*dbuf
)
316 /* drop reference obtained in vb2_vmalloc_get_dmabuf */
317 vb2_vmalloc_put(dbuf
->priv
);
320 static void *vb2_vmalloc_dmabuf_ops_kmap(struct dma_buf
*dbuf
, unsigned long pgnum
)
322 struct vb2_vmalloc_buf
*buf
= dbuf
->priv
;
324 return buf
->vaddr
+ pgnum
* PAGE_SIZE
;
327 static void *vb2_vmalloc_dmabuf_ops_vmap(struct dma_buf
*dbuf
)
329 struct vb2_vmalloc_buf
*buf
= dbuf
->priv
;
334 static int vb2_vmalloc_dmabuf_ops_mmap(struct dma_buf
*dbuf
,
335 struct vm_area_struct
*vma
)
337 return vb2_vmalloc_mmap(dbuf
->priv
, vma
);
340 static struct dma_buf_ops vb2_vmalloc_dmabuf_ops
= {
341 .attach
= vb2_vmalloc_dmabuf_ops_attach
,
342 .detach
= vb2_vmalloc_dmabuf_ops_detach
,
343 .map_dma_buf
= vb2_vmalloc_dmabuf_ops_map
,
344 .unmap_dma_buf
= vb2_vmalloc_dmabuf_ops_unmap
,
345 .kmap
= vb2_vmalloc_dmabuf_ops_kmap
,
346 .kmap_atomic
= vb2_vmalloc_dmabuf_ops_kmap
,
347 .vmap
= vb2_vmalloc_dmabuf_ops_vmap
,
348 .mmap
= vb2_vmalloc_dmabuf_ops_mmap
,
349 .release
= vb2_vmalloc_dmabuf_ops_release
,
352 static struct dma_buf
*vb2_vmalloc_get_dmabuf(void *buf_priv
, unsigned long flags
)
354 struct vb2_vmalloc_buf
*buf
= buf_priv
;
355 struct dma_buf
*dbuf
;
356 DEFINE_DMA_BUF_EXPORT_INFO(exp_info
);
358 exp_info
.ops
= &vb2_vmalloc_dmabuf_ops
;
359 exp_info
.size
= buf
->size
;
360 exp_info
.flags
= flags
;
363 if (WARN_ON(!buf
->vaddr
))
366 dbuf
= dma_buf_export(&exp_info
);
370 /* dmabuf keeps reference to vb2 buffer */
371 atomic_inc(&buf
->refcount
);
375 #endif /* CONFIG_HAS_DMA */
378 /*********************************************/
379 /* callbacks for DMABUF buffers */
380 /*********************************************/
382 static int vb2_vmalloc_map_dmabuf(void *mem_priv
)
384 struct vb2_vmalloc_buf
*buf
= mem_priv
;
386 buf
->vaddr
= dma_buf_vmap(buf
->dbuf
);
388 return buf
->vaddr
? 0 : -EFAULT
;
391 static void vb2_vmalloc_unmap_dmabuf(void *mem_priv
)
393 struct vb2_vmalloc_buf
*buf
= mem_priv
;
395 dma_buf_vunmap(buf
->dbuf
, buf
->vaddr
);
399 static void vb2_vmalloc_detach_dmabuf(void *mem_priv
)
401 struct vb2_vmalloc_buf
*buf
= mem_priv
;
404 dma_buf_vunmap(buf
->dbuf
, buf
->vaddr
);
409 static void *vb2_vmalloc_attach_dmabuf(struct device
*dev
, struct dma_buf
*dbuf
,
410 unsigned long size
, enum dma_data_direction dma_dir
)
412 struct vb2_vmalloc_buf
*buf
;
414 if (dbuf
->size
< size
)
415 return ERR_PTR(-EFAULT
);
417 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
419 return ERR_PTR(-ENOMEM
);
422 buf
->dma_dir
= dma_dir
;
429 const struct vb2_mem_ops vb2_vmalloc_memops
= {
430 .alloc
= vb2_vmalloc_alloc
,
431 .put
= vb2_vmalloc_put
,
432 .get_userptr
= vb2_vmalloc_get_userptr
,
433 .put_userptr
= vb2_vmalloc_put_userptr
,
434 #ifdef CONFIG_HAS_DMA
435 .get_dmabuf
= vb2_vmalloc_get_dmabuf
,
437 .map_dmabuf
= vb2_vmalloc_map_dmabuf
,
438 .unmap_dmabuf
= vb2_vmalloc_unmap_dmabuf
,
439 .attach_dmabuf
= vb2_vmalloc_attach_dmabuf
,
440 .detach_dmabuf
= vb2_vmalloc_detach_dmabuf
,
441 .vaddr
= vb2_vmalloc_vaddr
,
442 .mmap
= vb2_vmalloc_mmap
,
443 .num_users
= vb2_vmalloc_num_users
,
445 EXPORT_SYMBOL_GPL(vb2_vmalloc_memops
);
447 MODULE_DESCRIPTION("vmalloc memory handling routines for videobuf2");
448 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
449 MODULE_LICENSE("GPL");