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/refcount.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
21 #include <media/videobuf2-v4l2.h>
22 #include <media/videobuf2-vmalloc.h>
23 #include <media/videobuf2-memops.h>
25 struct vb2_vmalloc_buf
{
27 struct frame_vector
*vec
;
28 enum dma_data_direction dma_dir
;
31 struct vb2_vmarea_handler handler
;
35 static void vb2_vmalloc_put(void *buf_priv
);
37 static void *vb2_vmalloc_alloc(struct device
*dev
, unsigned long attrs
,
38 unsigned long size
, enum dma_data_direction dma_dir
,
41 struct vb2_vmalloc_buf
*buf
;
43 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
| gfp_flags
);
45 return ERR_PTR(-ENOMEM
);
48 buf
->vaddr
= vmalloc_user(buf
->size
);
50 pr_debug("vmalloc of size %ld failed\n", buf
->size
);
52 return ERR_PTR(-ENOMEM
);
55 buf
->dma_dir
= dma_dir
;
56 buf
->handler
.refcount
= &buf
->refcount
;
57 buf
->handler
.put
= vb2_vmalloc_put
;
58 buf
->handler
.arg
= buf
;
60 refcount_set(&buf
->refcount
, 1);
64 static void vb2_vmalloc_put(void *buf_priv
)
66 struct vb2_vmalloc_buf
*buf
= buf_priv
;
68 if (refcount_dec_and_test(&buf
->refcount
)) {
74 static void *vb2_vmalloc_get_userptr(struct device
*dev
, unsigned long vaddr
,
76 enum dma_data_direction dma_dir
)
78 struct vb2_vmalloc_buf
*buf
;
79 struct frame_vector
*vec
;
80 int n_pages
, offset
, i
;
83 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
85 return ERR_PTR(-ENOMEM
);
87 buf
->dma_dir
= dma_dir
;
88 offset
= vaddr
& ~PAGE_MASK
;
90 vec
= vb2_create_framevec(vaddr
, size
);
93 goto fail_pfnvec_create
;
96 n_pages
= frame_vector_count(vec
);
97 if (frame_vector_to_pages(vec
) < 0) {
98 unsigned long *nums
= frame_vector_pfns(vec
);
101 * We cannot get page pointers for these pfns. Check memory is
102 * physically contiguous and use direct mapping.
104 for (i
= 1; i
< n_pages
; i
++)
105 if (nums
[i
-1] + 1 != nums
[i
])
107 buf
->vaddr
= (__force
void *)
108 ioremap(__pfn_to_phys(nums
[0]), size
+ offset
);
110 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 buf
->dma_dir
== DMA_BIDIRECTIONAL
)
141 for (i
= 0; i
< n_pages
; i
++)
142 set_page_dirty_lock(pages
[i
]);
144 iounmap((__force
void __iomem
*)buf
->vaddr
);
146 vb2_destroy_framevec(buf
->vec
);
150 static void *vb2_vmalloc_vaddr(void *buf_priv
)
152 struct vb2_vmalloc_buf
*buf
= buf_priv
;
155 pr_err("Address of an unallocated plane requested or cannot map user pointer\n");
162 static unsigned int vb2_vmalloc_num_users(void *buf_priv
)
164 struct vb2_vmalloc_buf
*buf
= buf_priv
;
165 return refcount_read(&buf
->refcount
);
168 static int vb2_vmalloc_mmap(void *buf_priv
, struct vm_area_struct
*vma
)
170 struct vb2_vmalloc_buf
*buf
= buf_priv
;
174 pr_err("No memory to map\n");
178 ret
= remap_vmalloc_range(vma
, buf
->vaddr
, 0);
180 pr_err("Remapping vmalloc memory, error: %d\n", ret
);
185 * Make sure that vm_areas for 2 buffers won't be merged together
187 vma
->vm_flags
|= VM_DONTEXPAND
;
190 * Use common vm_area operations to track buffer refcount.
192 vma
->vm_private_data
= &buf
->handler
;
193 vma
->vm_ops
= &vb2_common_vm_ops
;
195 vma
->vm_ops
->open(vma
);
200 #ifdef CONFIG_HAS_DMA
201 /*********************************************/
202 /* DMABUF ops for exporters */
203 /*********************************************/
205 struct vb2_vmalloc_attachment
{
207 enum dma_data_direction dma_dir
;
210 static int vb2_vmalloc_dmabuf_ops_attach(struct dma_buf
*dbuf
,
211 struct dma_buf_attachment
*dbuf_attach
)
213 struct vb2_vmalloc_attachment
*attach
;
214 struct vb2_vmalloc_buf
*buf
= dbuf
->priv
;
215 int num_pages
= PAGE_ALIGN(buf
->size
) / PAGE_SIZE
;
216 struct sg_table
*sgt
;
217 struct scatterlist
*sg
;
218 void *vaddr
= buf
->vaddr
;
222 attach
= kzalloc(sizeof(*attach
), GFP_KERNEL
);
227 ret
= sg_alloc_table(sgt
, num_pages
, GFP_KERNEL
);
232 for_each_sgtable_sg(sgt
, sg
, i
) {
233 struct page
*page
= vmalloc_to_page(vaddr
);
240 sg_set_page(sg
, page
, PAGE_SIZE
, 0);
244 attach
->dma_dir
= DMA_NONE
;
245 dbuf_attach
->priv
= attach
;
249 static void vb2_vmalloc_dmabuf_ops_detach(struct dma_buf
*dbuf
,
250 struct dma_buf_attachment
*db_attach
)
252 struct vb2_vmalloc_attachment
*attach
= db_attach
->priv
;
253 struct sg_table
*sgt
;
260 /* release the scatterlist cache */
261 if (attach
->dma_dir
!= DMA_NONE
)
262 dma_unmap_sgtable(db_attach
->dev
, sgt
, attach
->dma_dir
, 0);
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_sgtable(db_attach
->dev
, sgt
, attach
->dma_dir
, 0);
288 attach
->dma_dir
= DMA_NONE
;
291 /* mapping to the client with new direction */
292 if (dma_map_sgtable(db_attach
->dev
, sgt
, dma_dir
, 0)) {
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 int vb2_vmalloc_dmabuf_ops_vmap(struct dma_buf
*dbuf
, struct dma_buf_map
*map
)
319 struct vb2_vmalloc_buf
*buf
= dbuf
->priv
;
321 dma_buf_map_set_vaddr(map
, buf
->vaddr
);
326 static int vb2_vmalloc_dmabuf_ops_mmap(struct dma_buf
*dbuf
,
327 struct vm_area_struct
*vma
)
329 return vb2_vmalloc_mmap(dbuf
->priv
, vma
);
332 static const struct dma_buf_ops vb2_vmalloc_dmabuf_ops
= {
333 .attach
= vb2_vmalloc_dmabuf_ops_attach
,
334 .detach
= vb2_vmalloc_dmabuf_ops_detach
,
335 .map_dma_buf
= vb2_vmalloc_dmabuf_ops_map
,
336 .unmap_dma_buf
= vb2_vmalloc_dmabuf_ops_unmap
,
337 .vmap
= vb2_vmalloc_dmabuf_ops_vmap
,
338 .mmap
= vb2_vmalloc_dmabuf_ops_mmap
,
339 .release
= vb2_vmalloc_dmabuf_ops_release
,
342 static struct dma_buf
*vb2_vmalloc_get_dmabuf(void *buf_priv
, unsigned long flags
)
344 struct vb2_vmalloc_buf
*buf
= buf_priv
;
345 struct dma_buf
*dbuf
;
346 DEFINE_DMA_BUF_EXPORT_INFO(exp_info
);
348 exp_info
.ops
= &vb2_vmalloc_dmabuf_ops
;
349 exp_info
.size
= buf
->size
;
350 exp_info
.flags
= flags
;
353 if (WARN_ON(!buf
->vaddr
))
356 dbuf
= dma_buf_export(&exp_info
);
360 /* dmabuf keeps reference to vb2 buffer */
361 refcount_inc(&buf
->refcount
);
365 #endif /* CONFIG_HAS_DMA */
368 /*********************************************/
369 /* callbacks for DMABUF buffers */
370 /*********************************************/
372 static int vb2_vmalloc_map_dmabuf(void *mem_priv
)
374 struct vb2_vmalloc_buf
*buf
= mem_priv
;
375 struct dma_buf_map map
;
378 ret
= dma_buf_vmap(buf
->dbuf
, &map
);
381 buf
->vaddr
= map
.vaddr
;
386 static void vb2_vmalloc_unmap_dmabuf(void *mem_priv
)
388 struct vb2_vmalloc_buf
*buf
= mem_priv
;
389 struct dma_buf_map map
= DMA_BUF_MAP_INIT_VADDR(buf
->vaddr
);
391 dma_buf_vunmap(buf
->dbuf
, &map
);
395 static void vb2_vmalloc_detach_dmabuf(void *mem_priv
)
397 struct vb2_vmalloc_buf
*buf
= mem_priv
;
398 struct dma_buf_map map
= DMA_BUF_MAP_INIT_VADDR(buf
->vaddr
);
401 dma_buf_vunmap(buf
->dbuf
, &map
);
406 static void *vb2_vmalloc_attach_dmabuf(struct device
*dev
, 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");