1 // SPDX-License-Identifier: GPL-2.0-only
3 * helper functions for vmalloc video4linux capture buffers
5 * The functions expect the hardware being able to scatter gather
6 * (i.e. the buffers are not linear in physical memory, but fragmented
7 * into PAGE_SIZE chunks). They also assume the driver does not need
8 * to touch the video data.
10 * (c) 2007 Mauro Carvalho Chehab <mchehab@kernel.org>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/slab.h>
17 #include <linux/interrupt.h>
18 #include <linux/pgtable.h>
20 #include <linux/pci.h>
21 #include <linux/vmalloc.h>
22 #include <linux/pagemap.h>
25 #include <media/videobuf-vmalloc.h>
27 #define MAGIC_DMABUF 0x17760309
28 #define MAGIC_VMAL_MEM 0x18221223
30 #define MAGIC_CHECK(is, should) \
31 if (unlikely((is) != (should))) { \
32 printk(KERN_ERR "magic mismatch: %x (expected %x)\n", \
38 module_param(debug
, int, 0644);
40 MODULE_DESCRIPTION("helper module to manage video4linux vmalloc buffers");
41 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@kernel.org>");
42 MODULE_LICENSE("GPL");
44 #define dprintk(level, fmt, arg...) \
46 printk(KERN_DEBUG "vbuf-vmalloc: " fmt , ## arg)
49 /***************************************************************************/
51 static void videobuf_vm_open(struct vm_area_struct
*vma
)
53 struct videobuf_mapping
*map
= vma
->vm_private_data
;
55 dprintk(2, "vm_open %p [count=%u,vma=%08lx-%08lx]\n", map
,
56 map
->count
, vma
->vm_start
, vma
->vm_end
);
61 static void videobuf_vm_close(struct vm_area_struct
*vma
)
63 struct videobuf_mapping
*map
= vma
->vm_private_data
;
64 struct videobuf_queue
*q
= map
->q
;
67 dprintk(2, "vm_close %p [count=%u,vma=%08lx-%08lx]\n", map
,
68 map
->count
, vma
->vm_start
, vma
->vm_end
);
71 if (0 == map
->count
) {
72 struct videobuf_vmalloc_memory
*mem
;
74 dprintk(1, "munmap %p q=%p\n", map
, q
);
75 videobuf_queue_lock(q
);
77 /* We need first to cancel streams, before unmapping */
79 videobuf_queue_cancel(q
);
81 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
82 if (NULL
== q
->bufs
[i
])
85 if (q
->bufs
[i
]->map
!= map
)
88 mem
= q
->bufs
[i
]->priv
;
90 /* This callback is called only if kernel has
91 allocated memory and this memory is mmapped.
92 In this case, memory should be freed,
93 in order to do memory unmap.
96 MAGIC_CHECK(mem
->magic
, MAGIC_VMAL_MEM
);
98 /* vfree is not atomic - can't be
99 called with IRQ's disabled
101 dprintk(1, "%s: buf[%d] freeing (%p)\n",
102 __func__
, i
, mem
->vaddr
);
108 q
->bufs
[i
]->map
= NULL
;
109 q
->bufs
[i
]->baddr
= 0;
114 videobuf_queue_unlock(q
);
120 static const struct vm_operations_struct videobuf_vm_ops
= {
121 .open
= videobuf_vm_open
,
122 .close
= videobuf_vm_close
,
125 /* ---------------------------------------------------------------------
126 * vmalloc handlers for the generic methods
129 /* Allocated area consists on 3 parts:
131 struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
132 struct videobuf_dma_sg_memory
135 static struct videobuf_buffer
*__videobuf_alloc_vb(size_t size
)
137 struct videobuf_vmalloc_memory
*mem
;
138 struct videobuf_buffer
*vb
;
140 vb
= kzalloc(size
+ sizeof(*mem
), GFP_KERNEL
);
144 mem
= vb
->priv
= ((char *)vb
) + size
;
145 mem
->magic
= MAGIC_VMAL_MEM
;
147 dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
148 __func__
, vb
, (long)sizeof(*vb
), (long)size
- sizeof(*vb
),
149 mem
, (long)sizeof(*mem
));
154 static int __videobuf_iolock(struct videobuf_queue
*q
,
155 struct videobuf_buffer
*vb
,
156 struct v4l2_framebuffer
*fbuf
)
158 struct videobuf_vmalloc_memory
*mem
= vb
->priv
;
163 MAGIC_CHECK(mem
->magic
, MAGIC_VMAL_MEM
);
165 switch (vb
->memory
) {
166 case V4L2_MEMORY_MMAP
:
167 dprintk(1, "%s memory method MMAP\n", __func__
);
169 /* All handling should be done by __videobuf_mmap_mapper() */
171 printk(KERN_ERR
"memory is not allocated/mmapped.\n");
175 case V4L2_MEMORY_USERPTR
:
176 pages
= PAGE_ALIGN(vb
->size
);
178 dprintk(1, "%s memory method USERPTR\n", __func__
);
181 printk(KERN_ERR
"USERPTR is currently not supported\n");
185 /* The only USERPTR currently supported is the one needed for
189 mem
->vaddr
= vmalloc_user(pages
);
191 printk(KERN_ERR
"vmalloc (%d pages) failed\n", pages
);
194 dprintk(1, "vmalloc is at addr %p (%d pages)\n",
197 case V4L2_MEMORY_OVERLAY
:
199 dprintk(1, "%s memory method OVERLAY/unknown\n", __func__
);
201 /* Currently, doesn't support V4L2_MEMORY_OVERLAY */
202 printk(KERN_ERR
"Memory method currently unsupported.\n");
209 static int __videobuf_mmap_mapper(struct videobuf_queue
*q
,
210 struct videobuf_buffer
*buf
,
211 struct vm_area_struct
*vma
)
213 struct videobuf_vmalloc_memory
*mem
;
214 struct videobuf_mapping
*map
;
217 dprintk(1, "%s\n", __func__
);
219 /* create mapping + update buffer list */
220 map
= kzalloc(sizeof(struct videobuf_mapping
), GFP_KERNEL
);
227 buf
->baddr
= vma
->vm_start
;
231 MAGIC_CHECK(mem
->magic
, MAGIC_VMAL_MEM
);
233 pages
= PAGE_ALIGN(vma
->vm_end
- vma
->vm_start
);
234 mem
->vaddr
= vmalloc_user(pages
);
236 printk(KERN_ERR
"vmalloc (%d pages) failed\n", pages
);
239 dprintk(1, "vmalloc is at addr %p (%d pages)\n", mem
->vaddr
, pages
);
241 /* Try to remap memory */
242 retval
= remap_vmalloc_range(vma
, mem
->vaddr
, 0);
244 printk(KERN_ERR
"mmap: remap failed with error %d. ", retval
);
249 vma
->vm_ops
= &videobuf_vm_ops
;
250 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
251 vma
->vm_private_data
= map
;
253 dprintk(1, "mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n",
254 map
, q
, vma
->vm_start
, vma
->vm_end
,
255 (long int)buf
->bsize
,
256 vma
->vm_pgoff
, buf
->i
);
258 videobuf_vm_open(vma
);
268 static struct videobuf_qtype_ops qops
= {
269 .magic
= MAGIC_QTYPE_OPS
,
271 .alloc_vb
= __videobuf_alloc_vb
,
272 .iolock
= __videobuf_iolock
,
273 .mmap_mapper
= __videobuf_mmap_mapper
,
274 .vaddr
= videobuf_to_vmalloc
,
277 void videobuf_queue_vmalloc_init(struct videobuf_queue
*q
,
278 const struct videobuf_queue_ops
*ops
,
281 enum v4l2_buf_type type
,
282 enum v4l2_field field
,
285 struct mutex
*ext_lock
)
287 videobuf_queue_core_init(q
, ops
, dev
, irqlock
, type
, field
, msize
,
288 priv
, &qops
, ext_lock
);
290 EXPORT_SYMBOL_GPL(videobuf_queue_vmalloc_init
);
292 void *videobuf_to_vmalloc(struct videobuf_buffer
*buf
)
294 struct videobuf_vmalloc_memory
*mem
= buf
->priv
;
296 MAGIC_CHECK(mem
->magic
, MAGIC_VMAL_MEM
);
300 EXPORT_SYMBOL_GPL(videobuf_to_vmalloc
);
302 void videobuf_vmalloc_free(struct videobuf_buffer
*buf
)
304 struct videobuf_vmalloc_memory
*mem
= buf
->priv
;
306 /* mmapped memory can't be freed here, otherwise mmapped region
307 would be released, while still needed. In this case, the memory
308 release should happen inside videobuf_vm_close().
309 So, it should free memory only if the memory were allocated for
312 if ((buf
->memory
!= V4L2_MEMORY_USERPTR
) || buf
->baddr
)
318 MAGIC_CHECK(mem
->magic
, MAGIC_VMAL_MEM
);
325 EXPORT_SYMBOL_GPL(videobuf_vmalloc_free
);