2 * helper functions for SG DMA video4linux capture buffers
4 * The functions expect the hardware being able to scatter gather
5 * (i.e. the buffers are not linear in physical memory, but fragmented
6 * into PAGE_SIZE chunks). They also assume the driver does not need
7 * to touch the video data.
9 * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
11 * Highly based on video-buf written originally by:
12 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
13 * (c) 2006 Mauro Carvalho Chehab, <mchehab@infradead.org>
14 * (c) 2006 Ted Walther and John Sokol
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/sched.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/vmalloc.h>
30 #include <linux/pagemap.h>
31 #include <linux/scatterlist.h>
33 #include <asm/pgtable.h>
35 #include <media/videobuf-dma-sg.h>
37 #define MAGIC_DMABUF 0x19721112
38 #define MAGIC_SG_MEM 0x17890714
40 #define MAGIC_CHECK(is, should) \
41 if (unlikely((is) != (should))) { \
42 printk(KERN_ERR "magic mismatch: %x (expected %x)\n", \
48 module_param(debug
, int, 0644);
50 MODULE_DESCRIPTION("helper module to manage video4linux dma sg buffers");
51 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
52 MODULE_LICENSE("GPL");
54 #define dprintk(level, fmt, arg...) \
56 printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg)
58 /* --------------------------------------------------------------------- */
61 * Return a scatterlist for some page-aligned vmalloc()'ed memory
62 * block (NULL on errors). Memory for the scatterlist is allocated
63 * using kmalloc. The caller must free the memory.
65 static struct scatterlist
*videobuf_vmalloc_to_sg(unsigned char *virt
,
68 struct scatterlist
*sglist
;
72 sglist
= vzalloc(nr_pages
* sizeof(*sglist
));
75 sg_init_table(sglist
, nr_pages
);
76 for (i
= 0; i
< nr_pages
; i
++, virt
+= PAGE_SIZE
) {
77 pg
= vmalloc_to_page(virt
);
80 BUG_ON(PageHighMem(pg
));
81 sg_set_page(&sglist
[i
], pg
, PAGE_SIZE
, 0);
91 * Return a scatterlist for a an array of userpages (NULL on errors).
92 * Memory for the scatterlist is allocated using kmalloc. The caller
93 * must free the memory.
95 static struct scatterlist
*videobuf_pages_to_sg(struct page
**pages
,
96 int nr_pages
, int offset
, size_t size
)
98 struct scatterlist
*sglist
;
101 if (NULL
== pages
[0])
103 sglist
= vmalloc(nr_pages
* sizeof(*sglist
));
106 sg_init_table(sglist
, nr_pages
);
108 if (PageHighMem(pages
[0]))
109 /* DMA to highmem pages might not work */
111 sg_set_page(&sglist
[0], pages
[0],
112 min_t(size_t, PAGE_SIZE
- offset
, size
), offset
);
113 size
-= min_t(size_t, PAGE_SIZE
- offset
, size
);
114 for (i
= 1; i
< nr_pages
; i
++) {
115 if (NULL
== pages
[i
])
117 if (PageHighMem(pages
[i
]))
119 sg_set_page(&sglist
[i
], pages
[i
], min_t(size_t, PAGE_SIZE
, size
), 0);
120 size
-= min_t(size_t, PAGE_SIZE
, size
);
125 dprintk(2, "sgl: oops - no page\n");
130 dprintk(2, "sgl: oops - highmem page\n");
135 /* --------------------------------------------------------------------- */
137 struct videobuf_dmabuf
*videobuf_to_dma(struct videobuf_buffer
*buf
)
139 struct videobuf_dma_sg_memory
*mem
= buf
->priv
;
142 MAGIC_CHECK(mem
->magic
, MAGIC_SG_MEM
);
146 EXPORT_SYMBOL_GPL(videobuf_to_dma
);
148 static void videobuf_dma_init(struct videobuf_dmabuf
*dma
)
150 memset(dma
, 0, sizeof(*dma
));
151 dma
->magic
= MAGIC_DMABUF
;
154 static int videobuf_dma_init_user_locked(struct videobuf_dmabuf
*dma
,
155 int direction
, unsigned long data
, unsigned long size
)
157 unsigned long first
, last
;
160 dma
->direction
= direction
;
161 switch (dma
->direction
) {
162 case DMA_FROM_DEVICE
:
172 first
= (data
& PAGE_MASK
) >> PAGE_SHIFT
;
173 last
= ((data
+size
-1) & PAGE_MASK
) >> PAGE_SHIFT
;
174 dma
->offset
= data
& ~PAGE_MASK
;
176 dma
->nr_pages
= last
-first
+1;
177 dma
->pages
= kmalloc(dma
->nr_pages
* sizeof(struct page
*), GFP_KERNEL
);
178 if (NULL
== dma
->pages
)
181 dprintk(1, "init user [0x%lx+0x%lx => %d pages]\n",
182 data
, size
, dma
->nr_pages
);
184 err
= get_user_pages(data
& PAGE_MASK
, dma
->nr_pages
,
185 rw
== READ
, 1, /* force */
188 if (err
!= dma
->nr_pages
) {
189 dma
->nr_pages
= (err
>= 0) ? err
: 0;
190 dprintk(1, "get_user_pages: err=%d [%d]\n", err
, dma
->nr_pages
);
191 return err
< 0 ? err
: -EINVAL
;
196 static int videobuf_dma_init_user(struct videobuf_dmabuf
*dma
, int direction
,
197 unsigned long data
, unsigned long size
)
201 down_read(¤t
->mm
->mmap_sem
);
202 ret
= videobuf_dma_init_user_locked(dma
, direction
, data
, size
);
203 up_read(¤t
->mm
->mmap_sem
);
208 static int videobuf_dma_init_kernel(struct videobuf_dmabuf
*dma
, int direction
,
213 dprintk(1, "init kernel [%d pages]\n", nr_pages
);
215 dma
->direction
= direction
;
216 dma
->vaddr_pages
= kcalloc(nr_pages
, sizeof(*dma
->vaddr_pages
),
218 if (!dma
->vaddr_pages
)
221 dma
->dma_addr
= kcalloc(nr_pages
, sizeof(*dma
->dma_addr
), GFP_KERNEL
);
222 if (!dma
->dma_addr
) {
223 kfree(dma
->vaddr_pages
);
226 for (i
= 0; i
< nr_pages
; i
++) {
229 addr
= dma_alloc_coherent(dma
->dev
, PAGE_SIZE
,
230 &(dma
->dma_addr
[i
]), GFP_KERNEL
);
234 dma
->vaddr_pages
[i
] = virt_to_page(addr
);
236 dma
->vaddr
= vmap(dma
->vaddr_pages
, nr_pages
, VM_MAP
| VM_IOREMAP
,
238 if (NULL
== dma
->vaddr
) {
239 dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages
);
243 dprintk(1, "vmalloc is at addr 0x%08lx, size=%d\n",
244 (unsigned long)dma
->vaddr
,
245 nr_pages
<< PAGE_SHIFT
);
247 memset(dma
->vaddr
, 0, nr_pages
<< PAGE_SHIFT
);
248 dma
->nr_pages
= nr_pages
;
256 addr
= page_address(dma
->vaddr_pages
[i
]);
257 dma_free_coherent(dma
->dev
, PAGE_SIZE
, addr
, dma
->dma_addr
[i
]);
259 kfree(dma
->dma_addr
);
260 dma
->dma_addr
= NULL
;
261 kfree(dma
->vaddr_pages
);
262 dma
->vaddr_pages
= NULL
;
268 static int videobuf_dma_init_overlay(struct videobuf_dmabuf
*dma
, int direction
,
269 dma_addr_t addr
, int nr_pages
)
271 dprintk(1, "init overlay [%d pages @ bus 0x%lx]\n",
272 nr_pages
, (unsigned long)addr
);
273 dma
->direction
= direction
;
278 dma
->bus_addr
= addr
;
279 dma
->nr_pages
= nr_pages
;
284 static int videobuf_dma_map(struct device
*dev
, struct videobuf_dmabuf
*dma
)
286 MAGIC_CHECK(dma
->magic
, MAGIC_DMABUF
);
287 BUG_ON(0 == dma
->nr_pages
);
290 dma
->sglist
= videobuf_pages_to_sg(dma
->pages
, dma
->nr_pages
,
291 dma
->offset
, dma
->size
);
294 dma
->sglist
= videobuf_vmalloc_to_sg(dma
->vaddr
,
298 dma
->sglist
= vmalloc(sizeof(*dma
->sglist
));
299 if (NULL
!= dma
->sglist
) {
301 sg_dma_address(&dma
->sglist
[0]) = dma
->bus_addr
303 dma
->sglist
[0].offset
= dma
->bus_addr
& ~PAGE_MASK
;
304 sg_dma_len(&dma
->sglist
[0]) = dma
->nr_pages
* PAGE_SIZE
;
307 if (NULL
== dma
->sglist
) {
308 dprintk(1, "scatterlist is NULL\n");
311 if (!dma
->bus_addr
) {
312 dma
->sglen
= dma_map_sg(dev
, dma
->sglist
,
313 dma
->nr_pages
, dma
->direction
);
314 if (0 == dma
->sglen
) {
316 "%s: videobuf_map_sg failed\n", __func__
);
327 int videobuf_dma_unmap(struct device
*dev
, struct videobuf_dmabuf
*dma
)
329 MAGIC_CHECK(dma
->magic
, MAGIC_DMABUF
);
334 dma_unmap_sg(dev
, dma
->sglist
, dma
->sglen
, dma
->direction
);
342 EXPORT_SYMBOL_GPL(videobuf_dma_unmap
);
344 int videobuf_dma_free(struct videobuf_dmabuf
*dma
)
347 MAGIC_CHECK(dma
->magic
, MAGIC_DMABUF
);
351 for (i
= 0; i
< dma
->nr_pages
; i
++)
352 put_page(dma
->pages
[i
]);
358 for (i
= 0; i
< dma
->nr_pages
; i
++) {
361 addr
= page_address(dma
->vaddr_pages
[i
]);
362 dma_free_coherent(dma
->dev
, PAGE_SIZE
, addr
,
365 kfree(dma
->dma_addr
);
366 dma
->dma_addr
= NULL
;
367 kfree(dma
->vaddr_pages
);
368 dma
->vaddr_pages
= NULL
;
375 dma
->direction
= DMA_NONE
;
379 EXPORT_SYMBOL_GPL(videobuf_dma_free
);
381 /* --------------------------------------------------------------------- */
383 static void videobuf_vm_open(struct vm_area_struct
*vma
)
385 struct videobuf_mapping
*map
= vma
->vm_private_data
;
387 dprintk(2, "vm_open %p [count=%d,vma=%08lx-%08lx]\n", map
,
388 map
->count
, vma
->vm_start
, vma
->vm_end
);
393 static void videobuf_vm_close(struct vm_area_struct
*vma
)
395 struct videobuf_mapping
*map
= vma
->vm_private_data
;
396 struct videobuf_queue
*q
= map
->q
;
397 struct videobuf_dma_sg_memory
*mem
;
400 dprintk(2, "vm_close %p [count=%d,vma=%08lx-%08lx]\n", map
,
401 map
->count
, vma
->vm_start
, vma
->vm_end
);
404 if (0 == map
->count
) {
405 dprintk(1, "munmap %p q=%p\n", map
, q
);
406 videobuf_queue_lock(q
);
407 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
408 if (NULL
== q
->bufs
[i
])
410 mem
= q
->bufs
[i
]->priv
;
414 MAGIC_CHECK(mem
->magic
, MAGIC_SG_MEM
);
416 if (q
->bufs
[i
]->map
!= map
)
418 q
->bufs
[i
]->map
= NULL
;
419 q
->bufs
[i
]->baddr
= 0;
420 q
->ops
->buf_release(q
, q
->bufs
[i
]);
422 videobuf_queue_unlock(q
);
429 * Get a anonymous page for the mapping. Make sure we can DMA to that
430 * memory location with 32bit PCI devices (i.e. don't use highmem for
431 * now ...). Bounce buffers don't work very well for the data rates
434 static int videobuf_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
438 dprintk(3, "fault: fault @ %08lx [vma %08lx-%08lx]\n",
439 (unsigned long)vmf
->virtual_address
,
440 vma
->vm_start
, vma
->vm_end
);
442 page
= alloc_page(GFP_USER
| __GFP_DMA32
);
445 clear_user_highpage(page
, (unsigned long)vmf
->virtual_address
);
451 static const struct vm_operations_struct videobuf_vm_ops
= {
452 .open
= videobuf_vm_open
,
453 .close
= videobuf_vm_close
,
454 .fault
= videobuf_vm_fault
,
457 /* ---------------------------------------------------------------------
458 * SG handlers for the generic methods
461 /* Allocated area consists on 3 parts:
463 struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
464 struct videobuf_dma_sg_memory
467 static struct videobuf_buffer
*__videobuf_alloc_vb(size_t size
)
469 struct videobuf_dma_sg_memory
*mem
;
470 struct videobuf_buffer
*vb
;
472 vb
= kzalloc(size
+ sizeof(*mem
), GFP_KERNEL
);
476 mem
= vb
->priv
= ((char *)vb
) + size
;
477 mem
->magic
= MAGIC_SG_MEM
;
479 videobuf_dma_init(&mem
->dma
);
481 dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
482 __func__
, vb
, (long)sizeof(*vb
), (long)size
- sizeof(*vb
),
483 mem
, (long)sizeof(*mem
));
488 static void *__videobuf_to_vaddr(struct videobuf_buffer
*buf
)
490 struct videobuf_dma_sg_memory
*mem
= buf
->priv
;
493 MAGIC_CHECK(mem
->magic
, MAGIC_SG_MEM
);
495 return mem
->dma
.vaddr
;
498 static int __videobuf_iolock(struct videobuf_queue
*q
,
499 struct videobuf_buffer
*vb
,
500 struct v4l2_framebuffer
*fbuf
)
504 struct videobuf_dma_sg_memory
*mem
= vb
->priv
;
507 MAGIC_CHECK(mem
->magic
, MAGIC_SG_MEM
);
510 mem
->dma
.dev
= q
->dev
;
512 WARN_ON(mem
->dma
.dev
!= q
->dev
);
514 switch (vb
->memory
) {
515 case V4L2_MEMORY_MMAP
:
516 case V4L2_MEMORY_USERPTR
:
517 if (0 == vb
->baddr
) {
518 /* no userspace addr -- kernel bounce buffer */
519 pages
= PAGE_ALIGN(vb
->size
) >> PAGE_SHIFT
;
520 err
= videobuf_dma_init_kernel(&mem
->dma
,
525 } else if (vb
->memory
== V4L2_MEMORY_USERPTR
) {
526 /* dma directly to userspace */
527 err
= videobuf_dma_init_user(&mem
->dma
,
529 vb
->baddr
, vb
->bsize
);
533 /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP
534 buffers can only be called from videobuf_qbuf
535 we take current->mm->mmap_sem there, to prevent
536 locking inversion, so don't take it here */
538 err
= videobuf_dma_init_user_locked(&mem
->dma
,
540 vb
->baddr
, vb
->bsize
);
545 case V4L2_MEMORY_OVERLAY
:
548 /* FIXME: need sanity checks for vb->boff */
550 * Using a double cast to avoid compiler warnings when
551 * building for PAE. Compiler doesn't like direct casting
552 * of a 32 bit ptr to 64 bit integer.
554 bus
= (dma_addr_t
)(unsigned long)fbuf
->base
+ vb
->boff
;
555 pages
= PAGE_ALIGN(vb
->size
) >> PAGE_SHIFT
;
556 err
= videobuf_dma_init_overlay(&mem
->dma
, DMA_FROM_DEVICE
,
564 err
= videobuf_dma_map(q
->dev
, &mem
->dma
);
571 static int __videobuf_sync(struct videobuf_queue
*q
,
572 struct videobuf_buffer
*buf
)
574 struct videobuf_dma_sg_memory
*mem
= buf
->priv
;
575 BUG_ON(!mem
|| !mem
->dma
.sglen
);
577 MAGIC_CHECK(mem
->magic
, MAGIC_SG_MEM
);
578 MAGIC_CHECK(mem
->dma
.magic
, MAGIC_DMABUF
);
580 dma_sync_sg_for_cpu(q
->dev
, mem
->dma
.sglist
,
581 mem
->dma
.sglen
, mem
->dma
.direction
);
586 static int __videobuf_mmap_mapper(struct videobuf_queue
*q
,
587 struct videobuf_buffer
*buf
,
588 struct vm_area_struct
*vma
)
590 struct videobuf_dma_sg_memory
*mem
= buf
->priv
;
591 struct videobuf_mapping
*map
;
592 unsigned int first
, last
, size
= 0, i
;
598 MAGIC_CHECK(mem
->magic
, MAGIC_SG_MEM
);
600 /* look for first buffer to map */
601 for (first
= 0; first
< VIDEO_MAX_FRAME
; first
++) {
602 if (buf
== q
->bufs
[first
]) {
603 size
= PAGE_ALIGN(q
->bufs
[first
]->bsize
);
608 /* paranoia, should never happen since buf is always valid. */
610 dprintk(1, "mmap app bug: offset invalid [offset=0x%lx]\n",
611 (vma
->vm_pgoff
<< PAGE_SHIFT
));
617 /* create mapping + update buffer list */
619 map
= kmalloc(sizeof(struct videobuf_mapping
), GFP_KERNEL
);
624 for (i
= first
; i
<= last
; i
++) {
625 if (NULL
== q
->bufs
[i
])
627 q
->bufs
[i
]->map
= map
;
628 q
->bufs
[i
]->baddr
= vma
->vm_start
+ size
;
629 size
+= PAGE_ALIGN(q
->bufs
[i
]->bsize
);
634 vma
->vm_ops
= &videobuf_vm_ops
;
635 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
636 vma
->vm_flags
&= ~VM_IO
; /* using shared anonymous pages */
637 vma
->vm_private_data
= map
;
638 dprintk(1, "mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
639 map
, q
, vma
->vm_start
, vma
->vm_end
, vma
->vm_pgoff
, first
, last
);
646 static struct videobuf_qtype_ops sg_ops
= {
647 .magic
= MAGIC_QTYPE_OPS
,
649 .alloc_vb
= __videobuf_alloc_vb
,
650 .iolock
= __videobuf_iolock
,
651 .sync
= __videobuf_sync
,
652 .mmap_mapper
= __videobuf_mmap_mapper
,
653 .vaddr
= __videobuf_to_vaddr
,
656 void *videobuf_sg_alloc(size_t size
)
658 struct videobuf_queue q
;
660 /* Required to make generic handler to call __videobuf_alloc */
665 return videobuf_alloc_vb(&q
);
667 EXPORT_SYMBOL_GPL(videobuf_sg_alloc
);
669 void videobuf_queue_sg_init(struct videobuf_queue
*q
,
670 const struct videobuf_queue_ops
*ops
,
673 enum v4l2_buf_type type
,
674 enum v4l2_field field
,
677 struct mutex
*ext_lock
)
679 videobuf_queue_core_init(q
, ops
, dev
, irqlock
, type
, field
, msize
,
680 priv
, &sg_ops
, ext_lock
);
682 EXPORT_SYMBOL_GPL(videobuf_queue_sg_init
);