2 * helper functions for PCI DMA video4linux capture buffers
4 * The functions expect the hardware being able to scatter gatter
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/slab.h>
25 #include <linux/interrupt.h>
27 #include <linux/pci.h>
28 #include <linux/vmalloc.h>
29 #include <linux/pagemap.h>
31 #include <asm/pgtable.h>
33 #include <media/videobuf-dma-sg.h>
35 #define MAGIC_DMABUF 0x19721112
36 #define MAGIC_SG_MEM 0x17890714
38 #define MAGIC_CHECK(is,should) if (unlikely((is) != (should))) \
39 { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); }
42 module_param(debug, int, 0644);
44 MODULE_DESCRIPTION("helper module to manage video4linux pci dma sg buffers");
45 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
46 MODULE_LICENSE("GPL");
48 #define dprintk(level, fmt, arg...) if (debug >= level) \
49 printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg)
51 /* --------------------------------------------------------------------- */
54 videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages)
56 struct scatterlist *sglist;
60 sglist = kcalloc(nr_pages, sizeof(struct scatterlist), GFP_KERNEL);
63 for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
64 pg = vmalloc_to_page(virt);
67 BUG_ON(PageHighMem(pg));
69 sglist[i].length = PAGE_SIZE;
79 videobuf_pages_to_sg(struct page **pages, int nr_pages, int offset)
81 struct scatterlist *sglist;
86 sglist = kcalloc(nr_pages, sizeof(*sglist), GFP_KERNEL);
92 if (PageHighMem(pages[0]))
93 /* DMA to highmem pages might not work */
95 sglist[0].page = pages[0];
96 sglist[0].offset = offset;
97 sglist[0].length = PAGE_SIZE - offset;
98 for (i = 1; i < nr_pages; i++) {
101 if (PageHighMem(pages[i]))
103 sglist[i].page = pages[i];
104 sglist[i].length = PAGE_SIZE;
109 dprintk(2,"sgl: oops - no page\n");
114 dprintk(2,"sgl: oops - highmem page\n");
119 /* --------------------------------------------------------------------- */
121 struct videobuf_dmabuf *videobuf_to_dma (struct videobuf_buffer *buf)
123 struct videbuf_pci_sg_memory *mem=buf->priv;
126 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
131 void videobuf_dma_init(struct videobuf_dmabuf *dma)
133 memset(dma,0,sizeof(*dma));
134 dma->magic = MAGIC_DMABUF;
137 int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
138 unsigned long data, unsigned long size)
140 unsigned long first,last;
143 dma->direction = direction;
144 switch (dma->direction) {
145 case PCI_DMA_FROMDEVICE: rw = READ; break;
146 case PCI_DMA_TODEVICE: rw = WRITE; break;
150 first = (data & PAGE_MASK) >> PAGE_SHIFT;
151 last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
152 dma->offset = data & ~PAGE_MASK;
153 dma->nr_pages = last-first+1;
154 dma->pages = kmalloc(dma->nr_pages * sizeof(struct page*),
156 if (NULL == dma->pages)
158 dprintk(1,"init user [0x%lx+0x%lx => %d pages]\n",
159 data,size,dma->nr_pages);
161 dma->varea = (void *) data;
163 down_read(¤t->mm->mmap_sem);
164 err = get_user_pages(current,current->mm,
165 data & PAGE_MASK, dma->nr_pages,
166 rw == READ, 1, /* force */
168 up_read(¤t->mm->mmap_sem);
169 if (err != dma->nr_pages) {
170 dma->nr_pages = (err >= 0) ? err : 0;
171 dprintk(1,"get_user_pages: err=%d [%d]\n",err,dma->nr_pages);
172 return err < 0 ? err : -EINVAL;
177 int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
180 dprintk(1,"init kernel [%d pages]\n",nr_pages);
181 dma->direction = direction;
182 dma->vmalloc = vmalloc_32(nr_pages << PAGE_SHIFT);
183 if (NULL == dma->vmalloc) {
184 dprintk(1,"vmalloc_32(%d pages) failed\n",nr_pages);
187 dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n",
188 (unsigned long)dma->vmalloc,
189 nr_pages << PAGE_SHIFT);
190 memset(dma->vmalloc,0,nr_pages << PAGE_SHIFT);
191 dma->nr_pages = nr_pages;
195 int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
196 dma_addr_t addr, int nr_pages)
198 dprintk(1,"init overlay [%d pages @ bus 0x%lx]\n",
199 nr_pages,(unsigned long)addr);
200 dma->direction = direction;
204 dma->bus_addr = addr;
205 dma->nr_pages = nr_pages;
209 int videobuf_dma_map(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
212 struct videobuf_dma_sg_ops *ops=q->priv_ops;
214 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
215 BUG_ON(0 == dma->nr_pages);
218 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
222 dma->sglist = videobuf_vmalloc_to_sg
223 (dma->vmalloc,dma->nr_pages);
226 dma->sglist = kmalloc(sizeof(struct scatterlist), GFP_KERNEL);
227 if (NULL != dma->sglist) {
229 sg_dma_address(&dma->sglist[0]) = dma->bus_addr & PAGE_MASK;
230 dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
231 sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
234 if (NULL == dma->sglist) {
235 dprintk(1,"scatterlist is NULL\n");
238 if (!dma->bus_addr) {
239 if (ops && ops->vb_map_sg) {
240 dma->sglen = ops->vb_map_sg(dev,dma->sglist,
241 dma->nr_pages, dma->direction);
243 if (0 == dma->sglen) {
245 "%s: videobuf_map_sg failed\n",__FUNCTION__);
255 int videobuf_dma_sync(struct videobuf_queue *q,struct videobuf_dmabuf *dma)
258 struct videobuf_dma_sg_ops *ops=q->priv_ops;
260 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
263 if (!dma->bus_addr && ops && ops->vb_dma_sync_sg)
264 ops->vb_dma_sync_sg(dev,dma->sglist,dma->nr_pages,
270 int videobuf_dma_unmap(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
273 struct videobuf_dma_sg_ops *ops=q->priv_ops;
275 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
279 if (!dma->bus_addr && ops && ops->vb_unmap_sg)
280 ops->vb_unmap_sg(dev,dma->sglist,dma->nr_pages,
288 int videobuf_dma_free(struct videobuf_dmabuf *dma)
290 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
295 for (i=0; i < dma->nr_pages; i++)
296 page_cache_release(dma->pages[i]);
308 dma->direction = PCI_DMA_NONE;
312 /* --------------------------------------------------------------------- */
314 int videobuf_pci_dma_map(struct pci_dev *pci,struct videobuf_dmabuf *dma)
316 struct videobuf_queue q;
317 struct videobuf_dma_sg_ops qops;
320 qops.vb_map_sg=(vb_map_sg_t *)pci_map_sg;
321 qops.vb_unmap_sg=(vb_map_sg_t *)pci_unmap_sg;
324 return (videobuf_dma_map(&q,dma));
327 int videobuf_pci_dma_unmap(struct pci_dev *pci,struct videobuf_dmabuf *dma)
329 struct videobuf_queue q;
330 struct videobuf_dma_sg_ops qops;
333 qops.vb_map_sg=(vb_map_sg_t *)pci_map_sg;
334 qops.vb_unmap_sg=(vb_map_sg_t *)pci_unmap_sg;
337 return (videobuf_dma_unmap(&q,dma));
340 /* --------------------------------------------------------------------- */
343 videobuf_vm_open(struct vm_area_struct *vma)
345 struct videobuf_mapping *map = vma->vm_private_data;
347 dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map,
348 map->count,vma->vm_start,vma->vm_end);
353 videobuf_vm_close(struct vm_area_struct *vma)
355 struct videobuf_mapping *map = vma->vm_private_data;
356 struct videobuf_queue *q = map->q;
357 struct videbuf_pci_sg_memory *mem;
360 dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
361 map->count,vma->vm_start,vma->vm_end);
364 if (0 == map->count) {
365 dprintk(1,"munmap %p q=%p\n",map,q);
366 mutex_lock(&q->lock);
367 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
368 if (NULL == q->bufs[i])
370 mem=q->bufs[i]->priv;
375 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
380 q->bufs[i]->baddr = 0;
381 q->ops->buf_release(q,q->bufs[i]);
383 mutex_unlock(&q->lock);
390 * Get a anonymous page for the mapping. Make sure we can DMA to that
391 * memory location with 32bit PCI devices (i.e. don't use highmem for
392 * now ...). Bounce buffers don't work very well for the data rates
396 videobuf_vm_nopage(struct vm_area_struct *vma, unsigned long vaddr,
401 dprintk(3,"nopage: fault @ %08lx [vma %08lx-%08lx]\n",
402 vaddr,vma->vm_start,vma->vm_end);
403 if (vaddr > vma->vm_end)
404 return NOPAGE_SIGBUS;
405 page = alloc_page(GFP_USER | __GFP_DMA32);
408 clear_user_page(page_address(page), vaddr, page);
410 *type = VM_FAULT_MINOR;
414 static struct vm_operations_struct videobuf_vm_ops =
416 .open = videobuf_vm_open,
417 .close = videobuf_vm_close,
418 .nopage = videobuf_vm_nopage,
421 /* ---------------------------------------------------------------------
422 * PCI handlers for the generic methods
425 /* Allocated area consists on 3 parts:
427 struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
428 struct videobuf_pci_sg_memory
431 static void *__videobuf_alloc(size_t size)
433 struct videbuf_pci_sg_memory *mem;
434 struct videobuf_buffer *vb;
436 vb = kzalloc(size+sizeof(*mem),GFP_KERNEL);
438 mem = vb->priv = ((char *)vb)+size;
439 mem->magic=MAGIC_SG_MEM;
441 videobuf_dma_init(&mem->dma);
443 dprintk(1,"%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
444 __FUNCTION__,vb,(long)sizeof(*vb),(long)size-sizeof(*vb),
445 mem,(long)sizeof(*mem));
450 static int __videobuf_iolock (struct videobuf_queue* q,
451 struct videobuf_buffer *vb,
452 struct v4l2_framebuffer *fbuf)
456 struct videbuf_pci_sg_memory *mem=vb->priv;
459 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
461 switch (vb->memory) {
462 case V4L2_MEMORY_MMAP:
463 case V4L2_MEMORY_USERPTR:
464 if (0 == vb->baddr) {
465 /* no userspace addr -- kernel bounce buffer */
466 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
467 err = videobuf_dma_init_kernel( &mem->dma,
473 /* dma directly to userspace */
474 err = videobuf_dma_init_user( &mem->dma,
476 vb->baddr,vb->bsize );
481 case V4L2_MEMORY_OVERLAY:
484 /* FIXME: need sanity checks for vb->boff */
486 * Using a double cast to avoid compiler warnings when
487 * building for PAE. Compiler doesn't like direct casting
488 * of a 32 bit ptr to 64 bit integer.
490 bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
491 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
492 err = videobuf_dma_init_overlay(&mem->dma,PCI_DMA_FROMDEVICE,
500 err = videobuf_dma_map(q,&mem->dma);
507 static int __videobuf_sync(struct videobuf_queue *q,
508 struct videobuf_buffer *buf)
510 struct videbuf_pci_sg_memory *mem=buf->priv;
512 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
514 return videobuf_dma_sync(q,&mem->dma);
517 static int __videobuf_mmap_free(struct videobuf_queue *q)
521 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
523 struct videbuf_pci_sg_memory *mem=q->bufs[i]->priv;
532 static int __videobuf_mmap_mapper(struct videobuf_queue *q,
533 struct vm_area_struct *vma)
535 struct videbuf_pci_sg_memory *mem;
536 struct videobuf_mapping *map;
537 unsigned int first,last,size,i;
541 if (!(vma->vm_flags & VM_WRITE)) {
542 dprintk(1,"mmap app bug: PROT_WRITE please\n");
545 if (!(vma->vm_flags & VM_SHARED)) {
546 dprintk(1,"mmap app bug: MAP_SHARED please\n");
550 /* look for first buffer to map */
551 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
552 if (NULL == q->bufs[first])
554 mem=q->bufs[first]->priv;
556 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
558 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
560 if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
563 if (VIDEO_MAX_FRAME == first) {
564 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
565 (vma->vm_pgoff << PAGE_SHIFT));
569 /* look for last buffer to map */
570 for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
571 if (NULL == q->bufs[last])
573 if (V4L2_MEMORY_MMAP != q->bufs[last]->memory)
575 mem=q->bufs[last]->priv;
580 size += q->bufs[last]->bsize;
581 if (size == (vma->vm_end - vma->vm_start))
584 if (VIDEO_MAX_FRAME == last) {
585 dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n",
586 (vma->vm_end - vma->vm_start));
590 /* create mapping + update buffer list */
592 map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
595 for (size = 0, i = first; i <= last; size += q->bufs[i++]->bsize) {
596 mem=q->bufs[i]->priv;
598 q->bufs[i]->baddr = vma->vm_start + size;
601 map->start = vma->vm_start;
602 map->end = vma->vm_end;
604 vma->vm_ops = &videobuf_vm_ops;
605 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
606 vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
607 vma->vm_private_data = map;
608 dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
609 map,q,vma->vm_start,vma->vm_end,vma->vm_pgoff,first,last);
616 static int __videobuf_is_mmapped (struct videobuf_buffer *buf)
618 struct videbuf_pci_sg_memory *mem=buf->priv;
621 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
623 return (mem->map)?1:0;
626 static int __videobuf_copy_to_user ( struct videobuf_queue *q,
627 char __user *data, size_t count,
630 struct videbuf_pci_sg_memory *mem=q->read_buf->priv;
632 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
634 /* copy to userspace */
635 if (count > q->read_buf->size - q->read_off)
636 count = q->read_buf->size - q->read_off;
638 if (copy_to_user(data, mem->dma.vmalloc+q->read_off, count))
644 static int __videobuf_copy_stream ( struct videobuf_queue *q,
645 char __user *data, size_t count, size_t pos,
646 int vbihack, int nonblocking )
649 struct videbuf_pci_sg_memory *mem=q->read_buf->priv;
651 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
654 /* dirty, undocumented hack -- pass the frame counter
655 * within the last four bytes of each vbi data block.
656 * We need that one to maintain backward compatibility
657 * to all vbi decoding software out there ... */
658 fc = (unsigned int*)mem->dma.vmalloc;
659 fc += (q->read_buf->size>>2) -1;
660 *fc = q->read_buf->field_count >> 1;
661 dprintk(1,"vbihack: %d\n",*fc);
664 /* copy stuff using the common method */
665 count = __videobuf_copy_to_user (q,data,count,nonblocking);
667 if ( (count==-EFAULT) && (0 == pos) )
673 static struct videobuf_qtype_ops pci_ops = {
674 .magic = MAGIC_QTYPE_OPS,
676 .alloc = __videobuf_alloc,
677 .iolock = __videobuf_iolock,
678 .sync = __videobuf_sync,
679 .mmap_free = __videobuf_mmap_free,
680 .mmap_mapper = __videobuf_mmap_mapper,
681 .is_mmapped = __videobuf_is_mmapped,
682 .copy_to_user = __videobuf_copy_to_user,
683 .copy_stream = __videobuf_copy_stream,
686 void *videobuf_pci_alloc (size_t size)
688 struct videobuf_queue q;
690 /* Required to make generic handler to call __videobuf_alloc */
695 return videobuf_alloc (&q);
698 void videobuf_queue_pci_init(struct videobuf_queue* q,
699 struct videobuf_queue_ops *ops,
702 enum v4l2_buf_type type,
703 enum v4l2_field field,
707 struct videobuf_dma_sg_ops *priv_ops;
709 videobuf_queue_init(q, ops, dev, irqlock, type, field, msize, priv);
712 /* FIXME: the code bellow should be removed after having a proper
713 * memory allocation method for vivi and tm6000
715 q->priv_ops= kzalloc(sizeof(struct videobuf_dma_sg_ops), GFP_KERNEL);
716 BUG_ON (!q->priv_ops);
718 priv_ops=q->priv_ops;
720 /* Sets default methods for handling Scatter Gather mapping */
721 priv_ops->vb_map_sg=(vb_map_sg_t *)pci_map_sg;
722 priv_ops->vb_unmap_sg=(vb_map_sg_t *)pci_unmap_sg;
723 priv_ops->vb_dma_sync_sg=(vb_map_sg_t *)pci_dma_sync_sg_for_cpu;
726 void videobuf_set_pci_ops (struct videobuf_queue* q,
727 struct videobuf_dma_sg_ops *ops)
736 /* If not specified, defaults to PCI map sg */
738 ops->vb_map_sg=(vb_map_sg_t *)pci_map_sg;
740 if (!ops->vb_dma_sync_sg)
741 ops->vb_dma_sync_sg=(vb_map_sg_t *)pci_dma_sync_sg_for_cpu;
742 if (!ops->vb_unmap_sg)
743 ops->vb_unmap_sg=(vb_map_sg_t *)pci_unmap_sg;
747 /* --------------------------------------------------------------------- */
749 EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg);
751 EXPORT_SYMBOL_GPL(videobuf_to_dma);
752 EXPORT_SYMBOL_GPL(videobuf_dma_init);
753 EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
754 EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
755 EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
756 EXPORT_SYMBOL_GPL(videobuf_dma_map);
757 EXPORT_SYMBOL_GPL(videobuf_dma_sync);
758 EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
759 EXPORT_SYMBOL_GPL(videobuf_dma_free);
761 EXPORT_SYMBOL_GPL(videobuf_pci_dma_map);
762 EXPORT_SYMBOL_GPL(videobuf_pci_dma_unmap);
763 EXPORT_SYMBOL_GPL(videobuf_pci_alloc);
765 EXPORT_SYMBOL_GPL(videobuf_queue_pci_init);
766 EXPORT_SYMBOL_GPL(videobuf_set_pci_ops);