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(void *alloc_ctx
, unsigned long size
,
37 enum dma_data_direction dma_dir
, gfp_t gfp_flags
)
39 struct vb2_vmalloc_buf
*buf
;
41 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
| gfp_flags
);
46 buf
->vaddr
= vmalloc_user(buf
->size
);
47 buf
->dma_dir
= dma_dir
;
48 buf
->handler
.refcount
= &buf
->refcount
;
49 buf
->handler
.put
= vb2_vmalloc_put
;
50 buf
->handler
.arg
= buf
;
53 pr_debug("vmalloc of size %ld failed\n", buf
->size
);
58 atomic_inc(&buf
->refcount
);
62 static void vb2_vmalloc_put(void *buf_priv
)
64 struct vb2_vmalloc_buf
*buf
= buf_priv
;
66 if (atomic_dec_and_test(&buf
->refcount
)) {
72 static void *vb2_vmalloc_get_userptr(void *alloc_ctx
, unsigned long vaddr
,
74 enum dma_data_direction dma_dir
)
76 struct vb2_vmalloc_buf
*buf
;
77 struct frame_vector
*vec
;
78 int n_pages
, offset
, i
;
80 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
84 buf
->dma_dir
= dma_dir
;
85 offset
= vaddr
& ~PAGE_MASK
;
87 vec
= vb2_create_framevec(vaddr
, size
, dma_dir
== DMA_FROM_DEVICE
);
89 goto fail_pfnvec_create
;
91 n_pages
= frame_vector_count(vec
);
92 if (frame_vector_to_pages(vec
) < 0) {
93 unsigned long *nums
= frame_vector_pfns(vec
);
96 * We cannot get page pointers for these pfns. Check memory is
97 * physically contiguous and use direct mapping.
99 for (i
= 1; i
< n_pages
; i
++)
100 if (nums
[i
-1] + 1 != nums
[i
])
102 buf
->vaddr
= (__force
void *)
103 ioremap_nocache(nums
[0] << PAGE_SHIFT
, size
);
105 buf
->vaddr
= vm_map_ram(frame_vector_pages(vec
), n_pages
, -1,
111 buf
->vaddr
+= offset
;
115 vb2_destroy_framevec(vec
);
122 static void vb2_vmalloc_put_userptr(void *buf_priv
)
124 struct vb2_vmalloc_buf
*buf
= buf_priv
;
125 unsigned long vaddr
= (unsigned long)buf
->vaddr
& PAGE_MASK
;
128 unsigned int n_pages
;
130 if (!buf
->vec
->is_pfns
) {
131 n_pages
= frame_vector_count(buf
->vec
);
132 pages
= frame_vector_pages(buf
->vec
);
134 vm_unmap_ram((void *)vaddr
, n_pages
);
135 if (buf
->dma_dir
== DMA_FROM_DEVICE
)
136 for (i
= 0; i
< n_pages
; i
++)
137 set_page_dirty_lock(pages
[i
]);
139 iounmap((__force
void __iomem
*)buf
->vaddr
);
141 vb2_destroy_framevec(buf
->vec
);
145 static void *vb2_vmalloc_vaddr(void *buf_priv
)
147 struct vb2_vmalloc_buf
*buf
= buf_priv
;
150 pr_err("Address of an unallocated plane requested "
151 "or cannot map user pointer\n");
158 static unsigned int vb2_vmalloc_num_users(void *buf_priv
)
160 struct vb2_vmalloc_buf
*buf
= buf_priv
;
161 return atomic_read(&buf
->refcount
);
164 static int vb2_vmalloc_mmap(void *buf_priv
, struct vm_area_struct
*vma
)
166 struct vb2_vmalloc_buf
*buf
= buf_priv
;
170 pr_err("No memory to map\n");
174 ret
= remap_vmalloc_range(vma
, buf
->vaddr
, 0);
176 pr_err("Remapping vmalloc memory, error: %d\n", ret
);
181 * Make sure that vm_areas for 2 buffers won't be merged together
183 vma
->vm_flags
|= VM_DONTEXPAND
;
186 * Use common vm_area operations to track buffer refcount.
188 vma
->vm_private_data
= &buf
->handler
;
189 vma
->vm_ops
= &vb2_common_vm_ops
;
191 vma
->vm_ops
->open(vma
);
196 #ifdef CONFIG_HAS_DMA
197 /*********************************************/
198 /* DMABUF ops for exporters */
199 /*********************************************/
201 struct vb2_vmalloc_attachment
{
203 enum dma_data_direction dma_dir
;
206 static int vb2_vmalloc_dmabuf_ops_attach(struct dma_buf
*dbuf
, struct device
*dev
,
207 struct dma_buf_attachment
*dbuf_attach
)
209 struct vb2_vmalloc_attachment
*attach
;
210 struct vb2_vmalloc_buf
*buf
= dbuf
->priv
;
211 int num_pages
= PAGE_ALIGN(buf
->size
) / PAGE_SIZE
;
212 struct sg_table
*sgt
;
213 struct scatterlist
*sg
;
214 void *vaddr
= buf
->vaddr
;
218 attach
= kzalloc(sizeof(*attach
), GFP_KERNEL
);
223 ret
= sg_alloc_table(sgt
, num_pages
, GFP_KERNEL
);
228 for_each_sg(sgt
->sgl
, sg
, sgt
->nents
, i
) {
229 struct page
*page
= vmalloc_to_page(vaddr
);
236 sg_set_page(sg
, page
, PAGE_SIZE
, 0);
240 attach
->dma_dir
= DMA_NONE
;
241 dbuf_attach
->priv
= attach
;
245 static void vb2_vmalloc_dmabuf_ops_detach(struct dma_buf
*dbuf
,
246 struct dma_buf_attachment
*db_attach
)
248 struct vb2_vmalloc_attachment
*attach
= db_attach
->priv
;
249 struct sg_table
*sgt
;
256 /* release the scatterlist cache */
257 if (attach
->dma_dir
!= DMA_NONE
)
258 dma_unmap_sg(db_attach
->dev
, sgt
->sgl
, sgt
->orig_nents
,
262 db_attach
->priv
= NULL
;
265 static struct sg_table
*vb2_vmalloc_dmabuf_ops_map(
266 struct dma_buf_attachment
*db_attach
, enum dma_data_direction dma_dir
)
268 struct vb2_vmalloc_attachment
*attach
= db_attach
->priv
;
269 /* stealing dmabuf mutex to serialize map/unmap operations */
270 struct mutex
*lock
= &db_attach
->dmabuf
->lock
;
271 struct sg_table
*sgt
;
276 /* return previously mapped sg table */
277 if (attach
->dma_dir
== dma_dir
) {
282 /* release any previous cache */
283 if (attach
->dma_dir
!= DMA_NONE
) {
284 dma_unmap_sg(db_attach
->dev
, sgt
->sgl
, sgt
->orig_nents
,
286 attach
->dma_dir
= DMA_NONE
;
289 /* mapping to the client with new direction */
290 sgt
->nents
= dma_map_sg(db_attach
->dev
, sgt
->sgl
, sgt
->orig_nents
,
293 pr_err("failed to map scatterlist\n");
295 return ERR_PTR(-EIO
);
298 attach
->dma_dir
= dma_dir
;
305 static void vb2_vmalloc_dmabuf_ops_unmap(struct dma_buf_attachment
*db_attach
,
306 struct sg_table
*sgt
, enum dma_data_direction dma_dir
)
308 /* nothing to be done here */
311 static void vb2_vmalloc_dmabuf_ops_release(struct dma_buf
*dbuf
)
313 /* drop reference obtained in vb2_vmalloc_get_dmabuf */
314 vb2_vmalloc_put(dbuf
->priv
);
317 static void *vb2_vmalloc_dmabuf_ops_kmap(struct dma_buf
*dbuf
, unsigned long pgnum
)
319 struct vb2_vmalloc_buf
*buf
= dbuf
->priv
;
321 return buf
->vaddr
+ pgnum
* PAGE_SIZE
;
324 static void *vb2_vmalloc_dmabuf_ops_vmap(struct dma_buf
*dbuf
)
326 struct vb2_vmalloc_buf
*buf
= dbuf
->priv
;
331 static int vb2_vmalloc_dmabuf_ops_mmap(struct dma_buf
*dbuf
,
332 struct vm_area_struct
*vma
)
334 return vb2_vmalloc_mmap(dbuf
->priv
, vma
);
337 static struct dma_buf_ops vb2_vmalloc_dmabuf_ops
= {
338 .attach
= vb2_vmalloc_dmabuf_ops_attach
,
339 .detach
= vb2_vmalloc_dmabuf_ops_detach
,
340 .map_dma_buf
= vb2_vmalloc_dmabuf_ops_map
,
341 .unmap_dma_buf
= vb2_vmalloc_dmabuf_ops_unmap
,
342 .kmap
= vb2_vmalloc_dmabuf_ops_kmap
,
343 .kmap_atomic
= vb2_vmalloc_dmabuf_ops_kmap
,
344 .vmap
= vb2_vmalloc_dmabuf_ops_vmap
,
345 .mmap
= vb2_vmalloc_dmabuf_ops_mmap
,
346 .release
= vb2_vmalloc_dmabuf_ops_release
,
349 static struct dma_buf
*vb2_vmalloc_get_dmabuf(void *buf_priv
, unsigned long flags
)
351 struct vb2_vmalloc_buf
*buf
= buf_priv
;
352 struct dma_buf
*dbuf
;
353 DEFINE_DMA_BUF_EXPORT_INFO(exp_info
);
355 exp_info
.ops
= &vb2_vmalloc_dmabuf_ops
;
356 exp_info
.size
= buf
->size
;
357 exp_info
.flags
= flags
;
360 if (WARN_ON(!buf
->vaddr
))
363 dbuf
= dma_buf_export(&exp_info
);
367 /* dmabuf keeps reference to vb2 buffer */
368 atomic_inc(&buf
->refcount
);
372 #endif /* CONFIG_HAS_DMA */
375 /*********************************************/
376 /* callbacks for DMABUF buffers */
377 /*********************************************/
379 static int vb2_vmalloc_map_dmabuf(void *mem_priv
)
381 struct vb2_vmalloc_buf
*buf
= mem_priv
;
383 buf
->vaddr
= dma_buf_vmap(buf
->dbuf
);
385 return buf
->vaddr
? 0 : -EFAULT
;
388 static void vb2_vmalloc_unmap_dmabuf(void *mem_priv
)
390 struct vb2_vmalloc_buf
*buf
= mem_priv
;
392 dma_buf_vunmap(buf
->dbuf
, buf
->vaddr
);
396 static void vb2_vmalloc_detach_dmabuf(void *mem_priv
)
398 struct vb2_vmalloc_buf
*buf
= mem_priv
;
401 dma_buf_vunmap(buf
->dbuf
, buf
->vaddr
);
406 static void *vb2_vmalloc_attach_dmabuf(void *alloc_ctx
, struct dma_buf
*dbuf
,
407 unsigned long size
, enum dma_data_direction dma_dir
)
409 struct vb2_vmalloc_buf
*buf
;
411 if (dbuf
->size
< size
)
412 return ERR_PTR(-EFAULT
);
414 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
416 return ERR_PTR(-ENOMEM
);
419 buf
->dma_dir
= dma_dir
;
426 const struct vb2_mem_ops vb2_vmalloc_memops
= {
427 .alloc
= vb2_vmalloc_alloc
,
428 .put
= vb2_vmalloc_put
,
429 .get_userptr
= vb2_vmalloc_get_userptr
,
430 .put_userptr
= vb2_vmalloc_put_userptr
,
431 #ifdef CONFIG_HAS_DMA
432 .get_dmabuf
= vb2_vmalloc_get_dmabuf
,
434 .map_dmabuf
= vb2_vmalloc_map_dmabuf
,
435 .unmap_dmabuf
= vb2_vmalloc_unmap_dmabuf
,
436 .attach_dmabuf
= vb2_vmalloc_attach_dmabuf
,
437 .detach_dmabuf
= vb2_vmalloc_detach_dmabuf
,
438 .vaddr
= vb2_vmalloc_vaddr
,
439 .mmap
= vb2_vmalloc_mmap
,
440 .num_users
= vb2_vmalloc_num_users
,
442 EXPORT_SYMBOL_GPL(vb2_vmalloc_memops
);
444 MODULE_DESCRIPTION("vmalloc memory handling routines for videobuf2");
445 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
446 MODULE_LICENSE("GPL");