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 /* --------------------------------------------------------------------- */
60 struct scatterlist
*videobuf_vmalloc_to_sg(unsigned char *virt
, int nr_pages
)
62 struct scatterlist
*sglist
;
66 sglist
= vmalloc(nr_pages
* sizeof(*sglist
));
69 memset(sglist
, 0, nr_pages
* sizeof(*sglist
));
70 sg_init_table(sglist
, nr_pages
);
71 for (i
= 0; i
< nr_pages
; i
++, virt
+= PAGE_SIZE
) {
72 pg
= vmalloc_to_page(virt
);
75 BUG_ON(PageHighMem(pg
));
76 sg_set_page(&sglist
[i
], pg
, PAGE_SIZE
, 0);
84 EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg
);
86 struct scatterlist
*videobuf_pages_to_sg(struct page
**pages
, int nr_pages
,
89 struct scatterlist
*sglist
;
94 sglist
= vmalloc(nr_pages
* sizeof(*sglist
));
97 sg_init_table(sglist
, nr_pages
);
99 if (PageHighMem(pages
[0]))
100 /* DMA to highmem pages might not work */
102 sg_set_page(&sglist
[0], pages
[0], PAGE_SIZE
- offset
, offset
);
103 for (i
= 1; i
< nr_pages
; i
++) {
104 if (NULL
== pages
[i
])
106 if (PageHighMem(pages
[i
]))
108 sg_set_page(&sglist
[i
], pages
[i
], PAGE_SIZE
, 0);
113 dprintk(2, "sgl: oops - no page\n");
118 dprintk(2, "sgl: oops - highmem page\n");
123 /* --------------------------------------------------------------------- */
125 struct videobuf_dmabuf
*videobuf_to_dma(struct videobuf_buffer
*buf
)
127 struct videobuf_dma_sg_memory
*mem
= buf
->priv
;
130 MAGIC_CHECK(mem
->magic
, MAGIC_SG_MEM
);
134 EXPORT_SYMBOL_GPL(videobuf_to_dma
);
136 void videobuf_dma_init(struct videobuf_dmabuf
*dma
)
138 memset(dma
, 0, sizeof(*dma
));
139 dma
->magic
= MAGIC_DMABUF
;
141 EXPORT_SYMBOL_GPL(videobuf_dma_init
);
143 static int videobuf_dma_init_user_locked(struct videobuf_dmabuf
*dma
,
144 int direction
, unsigned long data
, unsigned long size
)
146 unsigned long first
, last
;
149 dma
->direction
= direction
;
150 switch (dma
->direction
) {
151 case DMA_FROM_DEVICE
:
161 first
= (data
& PAGE_MASK
) >> PAGE_SHIFT
;
162 last
= ((data
+size
-1) & PAGE_MASK
) >> PAGE_SHIFT
;
163 dma
->offset
= data
& ~PAGE_MASK
;
164 dma
->nr_pages
= last
-first
+1;
165 dma
->pages
= kmalloc(dma
->nr_pages
* sizeof(struct page
*), GFP_KERNEL
);
166 if (NULL
== dma
->pages
)
169 dprintk(1, "init user [0x%lx+0x%lx => %d pages]\n",
170 data
, size
, dma
->nr_pages
);
172 err
= get_user_pages(current
, current
->mm
,
173 data
& PAGE_MASK
, dma
->nr_pages
,
174 rw
== READ
, 1, /* force */
177 if (err
!= dma
->nr_pages
) {
178 dma
->nr_pages
= (err
>= 0) ? err
: 0;
179 dprintk(1, "get_user_pages: err=%d [%d]\n", err
, dma
->nr_pages
);
180 return err
< 0 ? err
: -EINVAL
;
185 int videobuf_dma_init_user(struct videobuf_dmabuf
*dma
, int direction
,
186 unsigned long data
, unsigned long size
)
190 down_read(¤t
->mm
->mmap_sem
);
191 ret
= videobuf_dma_init_user_locked(dma
, direction
, data
, size
);
192 up_read(¤t
->mm
->mmap_sem
);
196 EXPORT_SYMBOL_GPL(videobuf_dma_init_user
);
198 int videobuf_dma_init_kernel(struct videobuf_dmabuf
*dma
, int direction
,
201 dprintk(1, "init kernel [%d pages]\n", nr_pages
);
203 dma
->direction
= direction
;
204 dma
->vmalloc
= vmalloc_32(nr_pages
<< PAGE_SHIFT
);
205 if (NULL
== dma
->vmalloc
) {
206 dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages
);
210 dprintk(1, "vmalloc is at addr 0x%08lx, size=%d\n",
211 (unsigned long)dma
->vmalloc
,
212 nr_pages
<< PAGE_SHIFT
);
214 memset(dma
->vmalloc
, 0, nr_pages
<< PAGE_SHIFT
);
215 dma
->nr_pages
= nr_pages
;
219 EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel
);
221 int videobuf_dma_init_overlay(struct videobuf_dmabuf
*dma
, int direction
,
222 dma_addr_t addr
, int nr_pages
)
224 dprintk(1, "init overlay [%d pages @ bus 0x%lx]\n",
225 nr_pages
, (unsigned long)addr
);
226 dma
->direction
= direction
;
231 dma
->bus_addr
= addr
;
232 dma
->nr_pages
= nr_pages
;
236 EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay
);
238 int videobuf_dma_map(struct videobuf_queue
*q
, struct videobuf_dmabuf
*dma
)
240 MAGIC_CHECK(dma
->magic
, MAGIC_DMABUF
);
241 BUG_ON(0 == dma
->nr_pages
);
244 dma
->sglist
= videobuf_pages_to_sg(dma
->pages
, dma
->nr_pages
,
248 dma
->sglist
= videobuf_vmalloc_to_sg(dma
->vmalloc
,
252 dma
->sglist
= vmalloc(sizeof(*dma
->sglist
));
253 if (NULL
!= dma
->sglist
) {
255 sg_dma_address(&dma
->sglist
[0]) = dma
->bus_addr
257 dma
->sglist
[0].offset
= dma
->bus_addr
& ~PAGE_MASK
;
258 sg_dma_len(&dma
->sglist
[0]) = dma
->nr_pages
* PAGE_SIZE
;
261 if (NULL
== dma
->sglist
) {
262 dprintk(1, "scatterlist is NULL\n");
265 if (!dma
->bus_addr
) {
266 dma
->sglen
= dma_map_sg(q
->dev
, dma
->sglist
,
267 dma
->nr_pages
, dma
->direction
);
268 if (0 == dma
->sglen
) {
270 "%s: videobuf_map_sg failed\n", __func__
);
280 EXPORT_SYMBOL_GPL(videobuf_dma_map
);
282 int videobuf_dma_sync(struct videobuf_queue
*q
, struct videobuf_dmabuf
*dma
)
284 MAGIC_CHECK(dma
->magic
, MAGIC_DMABUF
);
287 dma_sync_sg_for_cpu(q
->dev
, dma
->sglist
, dma
->nr_pages
, dma
->direction
);
291 EXPORT_SYMBOL_GPL(videobuf_dma_sync
);
293 int videobuf_dma_unmap(struct videobuf_queue
*q
, struct videobuf_dmabuf
*dma
)
295 MAGIC_CHECK(dma
->magic
, MAGIC_DMABUF
);
300 dma_unmap_sg(q
->dev
, dma
->sglist
, dma
->nr_pages
, dma
->direction
);
308 EXPORT_SYMBOL_GPL(videobuf_dma_unmap
);
310 int videobuf_dma_free(struct videobuf_dmabuf
*dma
)
313 MAGIC_CHECK(dma
->magic
, MAGIC_DMABUF
);
317 for (i
= 0; i
< dma
->nr_pages
; i
++)
318 page_cache_release(dma
->pages
[i
]);
328 dma
->direction
= DMA_NONE
;
332 EXPORT_SYMBOL_GPL(videobuf_dma_free
);
334 /* --------------------------------------------------------------------- */
336 int videobuf_sg_dma_map(struct device
*dev
, struct videobuf_dmabuf
*dma
)
338 struct videobuf_queue q
;
342 return videobuf_dma_map(&q
, dma
);
344 EXPORT_SYMBOL_GPL(videobuf_sg_dma_map
);
346 int videobuf_sg_dma_unmap(struct device
*dev
, struct videobuf_dmabuf
*dma
)
348 struct videobuf_queue q
;
352 return videobuf_dma_unmap(&q
, dma
);
354 EXPORT_SYMBOL_GPL(videobuf_sg_dma_unmap
);
356 /* --------------------------------------------------------------------- */
358 static void videobuf_vm_open(struct vm_area_struct
*vma
)
360 struct videobuf_mapping
*map
= vma
->vm_private_data
;
362 dprintk(2, "vm_open %p [count=%d,vma=%08lx-%08lx]\n", map
,
363 map
->count
, vma
->vm_start
, vma
->vm_end
);
368 static void videobuf_vm_close(struct vm_area_struct
*vma
)
370 struct videobuf_mapping
*map
= vma
->vm_private_data
;
371 struct videobuf_queue
*q
= map
->q
;
372 struct videobuf_dma_sg_memory
*mem
;
375 dprintk(2, "vm_close %p [count=%d,vma=%08lx-%08lx]\n", map
,
376 map
->count
, vma
->vm_start
, vma
->vm_end
);
379 if (0 == map
->count
) {
380 dprintk(1, "munmap %p q=%p\n", map
, q
);
381 mutex_lock(&q
->vb_lock
);
382 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
383 if (NULL
== q
->bufs
[i
])
385 mem
= q
->bufs
[i
]->priv
;
389 MAGIC_CHECK(mem
->magic
, MAGIC_SG_MEM
);
391 if (q
->bufs
[i
]->map
!= map
)
393 q
->bufs
[i
]->map
= NULL
;
394 q
->bufs
[i
]->baddr
= 0;
395 q
->ops
->buf_release(q
, q
->bufs
[i
]);
397 mutex_unlock(&q
->vb_lock
);
404 * Get a anonymous page for the mapping. Make sure we can DMA to that
405 * memory location with 32bit PCI devices (i.e. don't use highmem for
406 * now ...). Bounce buffers don't work very well for the data rates
409 static int videobuf_vm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
413 dprintk(3, "fault: fault @ %08lx [vma %08lx-%08lx]\n",
414 (unsigned long)vmf
->virtual_address
,
415 vma
->vm_start
, vma
->vm_end
);
417 page
= alloc_page(GFP_USER
| __GFP_DMA32
);
420 clear_user_highpage(page
, (unsigned long)vmf
->virtual_address
);
426 static const struct vm_operations_struct videobuf_vm_ops
= {
427 .open
= videobuf_vm_open
,
428 .close
= videobuf_vm_close
,
429 .fault
= videobuf_vm_fault
,
432 /* ---------------------------------------------------------------------
433 * SG handlers for the generic methods
436 /* Allocated area consists on 3 parts:
438 struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
439 struct videobuf_dma_sg_memory
442 static struct videobuf_buffer
*__videobuf_alloc(size_t size
)
444 struct videobuf_dma_sg_memory
*mem
;
445 struct videobuf_buffer
*vb
;
447 vb
= kzalloc(size
+ sizeof(*mem
), GFP_KERNEL
);
451 mem
= vb
->priv
= ((char *)vb
) + size
;
452 mem
->magic
= MAGIC_SG_MEM
;
454 videobuf_dma_init(&mem
->dma
);
456 dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
457 __func__
, vb
, (long)sizeof(*vb
), (long)size
- sizeof(*vb
),
458 mem
, (long)sizeof(*mem
));
463 static void *__videobuf_to_vmalloc(struct videobuf_buffer
*buf
)
465 struct videobuf_dma_sg_memory
*mem
= buf
->priv
;
468 MAGIC_CHECK(mem
->magic
, MAGIC_SG_MEM
);
470 return mem
->dma
.vmalloc
;
473 static int __videobuf_iolock(struct videobuf_queue
*q
,
474 struct videobuf_buffer
*vb
,
475 struct v4l2_framebuffer
*fbuf
)
479 struct videobuf_dma_sg_memory
*mem
= vb
->priv
;
482 MAGIC_CHECK(mem
->magic
, MAGIC_SG_MEM
);
484 switch (vb
->memory
) {
485 case V4L2_MEMORY_MMAP
:
486 case V4L2_MEMORY_USERPTR
:
487 if (0 == vb
->baddr
) {
488 /* no userspace addr -- kernel bounce buffer */
489 pages
= PAGE_ALIGN(vb
->size
) >> PAGE_SHIFT
;
490 err
= videobuf_dma_init_kernel(&mem
->dma
,
495 } else if (vb
->memory
== V4L2_MEMORY_USERPTR
) {
496 /* dma directly to userspace */
497 err
= videobuf_dma_init_user(&mem
->dma
,
499 vb
->baddr
, vb
->bsize
);
503 /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP
504 buffers can only be called from videobuf_qbuf
505 we take current->mm->mmap_sem there, to prevent
506 locking inversion, so don't take it here */
508 err
= videobuf_dma_init_user_locked(&mem
->dma
,
510 vb
->baddr
, vb
->bsize
);
515 case V4L2_MEMORY_OVERLAY
:
518 /* FIXME: need sanity checks for vb->boff */
520 * Using a double cast to avoid compiler warnings when
521 * building for PAE. Compiler doesn't like direct casting
522 * of a 32 bit ptr to 64 bit integer.
524 bus
= (dma_addr_t
)(unsigned long)fbuf
->base
+ vb
->boff
;
525 pages
= PAGE_ALIGN(vb
->size
) >> PAGE_SHIFT
;
526 err
= videobuf_dma_init_overlay(&mem
->dma
, DMA_FROM_DEVICE
,
534 err
= videobuf_dma_map(q
, &mem
->dma
);
541 static int __videobuf_sync(struct videobuf_queue
*q
,
542 struct videobuf_buffer
*buf
)
544 struct videobuf_dma_sg_memory
*mem
= buf
->priv
;
546 MAGIC_CHECK(mem
->magic
, MAGIC_SG_MEM
);
548 return videobuf_dma_sync(q
, &mem
->dma
);
551 static int __videobuf_mmap_mapper(struct videobuf_queue
*q
,
552 struct vm_area_struct
*vma
)
554 struct videobuf_dma_sg_memory
*mem
;
555 struct videobuf_mapping
*map
;
556 unsigned int first
, last
, size
, i
;
560 if (!(vma
->vm_flags
& VM_WRITE
)) {
561 dprintk(1, "mmap app bug: PROT_WRITE please\n");
564 if (!(vma
->vm_flags
& VM_SHARED
)) {
565 dprintk(1, "mmap app bug: MAP_SHARED please\n");
569 /* This function maintains backwards compatibility with V4L1 and will
570 * map more than one buffer if the vma length is equal to the combined
571 * size of multiple buffers than it will map them together. See
572 * VIDIOCGMBUF in the v4l spec
574 * TODO: Allow drivers to specify if they support this mode
577 /* look for first buffer to map */
578 for (first
= 0; first
< VIDEO_MAX_FRAME
; first
++) {
579 if (NULL
== q
->bufs
[first
])
581 mem
= q
->bufs
[first
]->priv
;
583 MAGIC_CHECK(mem
->magic
, MAGIC_SG_MEM
);
585 if (V4L2_MEMORY_MMAP
!= q
->bufs
[first
]->memory
)
587 if (q
->bufs
[first
]->boff
== (vma
->vm_pgoff
<< PAGE_SHIFT
))
590 if (VIDEO_MAX_FRAME
== first
) {
591 dprintk(1, "mmap app bug: offset invalid [offset=0x%lx]\n",
592 (vma
->vm_pgoff
<< PAGE_SHIFT
));
596 /* look for last buffer to map */
597 for (size
= 0, last
= first
; last
< VIDEO_MAX_FRAME
; last
++) {
598 if (NULL
== q
->bufs
[last
])
600 if (V4L2_MEMORY_MMAP
!= q
->bufs
[last
]->memory
)
602 if (q
->bufs
[last
]->map
) {
606 size
+= PAGE_ALIGN(q
->bufs
[last
]->bsize
);
607 if (size
== (vma
->vm_end
- vma
->vm_start
))
610 if (VIDEO_MAX_FRAME
== last
) {
611 dprintk(1, "mmap app bug: size invalid [size=0x%lx]\n",
612 (vma
->vm_end
- vma
->vm_start
));
616 /* create mapping + update buffer list */
618 map
= kmalloc(sizeof(struct videobuf_mapping
), GFP_KERNEL
);
623 for (i
= first
; i
<= last
; i
++) {
624 if (NULL
== q
->bufs
[i
])
626 q
->bufs
[i
]->map
= map
;
627 q
->bufs
[i
]->baddr
= vma
->vm_start
+ size
;
628 size
+= PAGE_ALIGN(q
->bufs
[i
]->bsize
);
632 map
->start
= vma
->vm_start
;
633 map
->end
= vma
->vm_end
;
635 vma
->vm_ops
= &videobuf_vm_ops
;
636 vma
->vm_flags
|= VM_DONTEXPAND
| VM_RESERVED
;
637 vma
->vm_flags
&= ~VM_IO
; /* using shared anonymous pages */
638 vma
->vm_private_data
= map
;
639 dprintk(1, "mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
640 map
, q
, vma
->vm_start
, vma
->vm_end
, vma
->vm_pgoff
, first
, last
);
647 static int __videobuf_copy_to_user(struct videobuf_queue
*q
,
648 char __user
*data
, size_t count
,
651 struct videobuf_dma_sg_memory
*mem
= q
->read_buf
->priv
;
653 MAGIC_CHECK(mem
->magic
, MAGIC_SG_MEM
);
655 /* copy to userspace */
656 if (count
> q
->read_buf
->size
- q
->read_off
)
657 count
= q
->read_buf
->size
- q
->read_off
;
659 if (copy_to_user(data
, mem
->dma
.vmalloc
+q
->read_off
, count
))
665 static int __videobuf_copy_stream(struct videobuf_queue
*q
,
666 char __user
*data
, size_t count
, size_t pos
,
667 int vbihack
, int nonblocking
)
670 struct videobuf_dma_sg_memory
*mem
= q
->read_buf
->priv
;
672 MAGIC_CHECK(mem
->magic
, MAGIC_SG_MEM
);
675 /* dirty, undocumented hack -- pass the frame counter
676 * within the last four bytes of each vbi data block.
677 * We need that one to maintain backward compatibility
678 * to all vbi decoding software out there ... */
679 fc
= (unsigned int *)mem
->dma
.vmalloc
;
680 fc
+= (q
->read_buf
->size
>> 2) - 1;
681 *fc
= q
->read_buf
->field_count
>> 1;
682 dprintk(1, "vbihack: %d\n", *fc
);
685 /* copy stuff using the common method */
686 count
= __videobuf_copy_to_user(q
, data
, count
, nonblocking
);
688 if ((count
== -EFAULT
) && (0 == pos
))
694 static struct videobuf_qtype_ops sg_ops
= {
695 .magic
= MAGIC_QTYPE_OPS
,
697 .alloc
= __videobuf_alloc
,
698 .iolock
= __videobuf_iolock
,
699 .sync
= __videobuf_sync
,
700 .mmap_mapper
= __videobuf_mmap_mapper
,
701 .video_copy_to_user
= __videobuf_copy_to_user
,
702 .copy_stream
= __videobuf_copy_stream
,
703 .vmalloc
= __videobuf_to_vmalloc
,
706 void *videobuf_sg_alloc(size_t size
)
708 struct videobuf_queue q
;
710 /* Required to make generic handler to call __videobuf_alloc */
715 return videobuf_alloc(&q
);
717 EXPORT_SYMBOL_GPL(videobuf_sg_alloc
);
719 void videobuf_queue_sg_init(struct videobuf_queue
*q
,
720 const struct videobuf_queue_ops
*ops
,
723 enum v4l2_buf_type type
,
724 enum v4l2_field field
,
728 videobuf_queue_core_init(q
, ops
, dev
, irqlock
, type
, field
, msize
,
731 EXPORT_SYMBOL_GPL(videobuf_queue_sg_init
);