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
, const struct dma_attrs
*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
);
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
);
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
;
81 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
85 buf
->dma_dir
= dma_dir
;
86 offset
= vaddr
& ~PAGE_MASK
;
88 vec
= vb2_create_framevec(vaddr
, size
, dma_dir
== DMA_FROM_DEVICE
);
90 goto fail_pfnvec_create
;
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
])
103 buf
->vaddr
= (__force
void *)
104 ioremap_nocache(nums
[0] << PAGE_SHIFT
, size
);
106 buf
->vaddr
= vm_map_ram(frame_vector_pages(vec
), n_pages
, -1,
112 buf
->vaddr
+= offset
;
116 vb2_destroy_framevec(vec
);
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
;
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
);
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
]);
140 iounmap((__force
void __iomem
*)buf
->vaddr
);
142 vb2_destroy_framevec(buf
->vec
);
146 static void *vb2_vmalloc_vaddr(void *buf_priv
)
148 struct vb2_vmalloc_buf
*buf
= buf_priv
;
151 pr_err("Address of an unallocated plane requested "
152 "or cannot map user pointer\n");
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
;
171 pr_err("No memory to map\n");
175 ret
= remap_vmalloc_range(vma
, buf
->vaddr
, 0);
177 pr_err("Remapping vmalloc memory, error: %d\n", 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
);
197 #ifdef CONFIG_HAS_DMA
198 /*********************************************/
199 /* DMABUF ops for exporters */
200 /*********************************************/
202 struct vb2_vmalloc_attachment
{
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
;
219 attach
= kzalloc(sizeof(*attach
), GFP_KERNEL
);
224 ret
= sg_alloc_table(sgt
, num_pages
, GFP_KERNEL
);
229 for_each_sg(sgt
->sgl
, sg
, sgt
->nents
, i
) {
230 struct page
*page
= vmalloc_to_page(vaddr
);
237 sg_set_page(sg
, page
, PAGE_SIZE
, 0);
241 attach
->dma_dir
= DMA_NONE
;
242 dbuf_attach
->priv
= attach
;
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
;
257 /* release the scatterlist cache */
258 if (attach
->dma_dir
!= DMA_NONE
)
259 dma_unmap_sg(db_attach
->dev
, sgt
->sgl
, sgt
->orig_nents
,
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
;
277 /* return previously mapped sg table */
278 if (attach
->dma_dir
== dma_dir
) {
283 /* release any previous cache */
284 if (attach
->dma_dir
!= DMA_NONE
) {
285 dma_unmap_sg(db_attach
->dev
, sgt
->sgl
, sgt
->orig_nents
,
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
,
294 pr_err("failed to map scatterlist\n");
296 return ERR_PTR(-EIO
);
299 attach
->dma_dir
= dma_dir
;
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
;
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
;
361 if (WARN_ON(!buf
->vaddr
))
364 dbuf
= dma_buf_export(&exp_info
);
368 /* dmabuf keeps reference to vb2 buffer */
369 atomic_inc(&buf
->refcount
);
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
);
397 static void vb2_vmalloc_detach_dmabuf(void *mem_priv
)
399 struct vb2_vmalloc_buf
*buf
= mem_priv
;
402 dma_buf_vunmap(buf
->dbuf
, buf
->vaddr
);
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
);
417 return ERR_PTR(-ENOMEM
);
420 buf
->dma_dir
= dma_dir
;
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
,
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");