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 sg_init_table(sglist, nr_pages);
64 for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
65 pg = vmalloc_to_page(virt);
68 BUG_ON(PageHighMem(pg));
69 sg_set_page(&sglist[i], pg);
70 sglist[i].length = PAGE_SIZE;
80 videobuf_pages_to_sg(struct page **pages, int nr_pages, int offset)
82 struct scatterlist *sglist;
87 sglist = kcalloc(nr_pages, sizeof(*sglist), GFP_KERNEL);
90 sg_init_table(sglist, nr_pages);
94 if (PageHighMem(pages[0]))
95 /* DMA to highmem pages might not work */
97 sg_set_page(&sglist[0], pages[0]);
98 sglist[0].offset = offset;
99 sglist[0].length = PAGE_SIZE - offset;
100 for (i = 1; i < nr_pages; i++) {
101 if (NULL == pages[i])
103 if (PageHighMem(pages[i]))
105 sg_set_page(&sglist[i], pages[i]);
106 sglist[i].length = PAGE_SIZE;
111 dprintk(2,"sgl: oops - no page\n");
116 dprintk(2,"sgl: oops - highmem page\n");
121 /* --------------------------------------------------------------------- */
123 struct videobuf_dmabuf *videobuf_to_dma (struct videobuf_buffer *buf)
125 struct videbuf_pci_sg_memory *mem=buf->priv;
128 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
133 void videobuf_dma_init(struct videobuf_dmabuf *dma)
135 memset(dma,0,sizeof(*dma));
136 dma->magic = MAGIC_DMABUF;
139 static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma,
140 int direction, unsigned long data, unsigned long size)
142 unsigned long first,last;
145 dma->direction = direction;
146 switch (dma->direction) {
147 case PCI_DMA_FROMDEVICE: rw = READ; break;
148 case PCI_DMA_TODEVICE: rw = WRITE; break;
152 first = (data & PAGE_MASK) >> PAGE_SHIFT;
153 last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
154 dma->offset = data & ~PAGE_MASK;
155 dma->nr_pages = last-first+1;
156 dma->pages = kmalloc(dma->nr_pages * sizeof(struct page*),
158 if (NULL == dma->pages)
160 dprintk(1,"init user [0x%lx+0x%lx => %d pages]\n",
161 data,size,dma->nr_pages);
163 dma->varea = (void *) data;
166 err = get_user_pages(current,current->mm,
167 data & PAGE_MASK, dma->nr_pages,
168 rw == READ, 1, /* force */
171 if (err != dma->nr_pages) {
172 dma->nr_pages = (err >= 0) ? err : 0;
173 dprintk(1,"get_user_pages: err=%d [%d]\n",err,dma->nr_pages);
174 return err < 0 ? err : -EINVAL;
179 int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
180 unsigned long data, unsigned long size)
183 down_read(¤t->mm->mmap_sem);
184 ret = videobuf_dma_init_user_locked(dma, direction, data, size);
185 up_read(¤t->mm->mmap_sem);
190 int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
193 dprintk(1,"init kernel [%d pages]\n",nr_pages);
194 dma->direction = direction;
195 dma->vmalloc = vmalloc_32(nr_pages << PAGE_SHIFT);
196 if (NULL == dma->vmalloc) {
197 dprintk(1,"vmalloc_32(%d pages) failed\n",nr_pages);
200 dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n",
201 (unsigned long)dma->vmalloc,
202 nr_pages << PAGE_SHIFT);
203 memset(dma->vmalloc,0,nr_pages << PAGE_SHIFT);
204 dma->nr_pages = nr_pages;
208 int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
209 dma_addr_t addr, int nr_pages)
211 dprintk(1,"init overlay [%d pages @ bus 0x%lx]\n",
212 nr_pages,(unsigned long)addr);
213 dma->direction = direction;
217 dma->bus_addr = addr;
218 dma->nr_pages = nr_pages;
222 int videobuf_dma_map(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
226 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
227 BUG_ON(0 == dma->nr_pages);
230 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
234 dma->sglist = videobuf_vmalloc_to_sg
235 (dma->vmalloc,dma->nr_pages);
238 dma->sglist = kmalloc(sizeof(struct scatterlist), GFP_KERNEL);
239 if (NULL != dma->sglist) {
241 sg_dma_address(&dma->sglist[0]) = dma->bus_addr & PAGE_MASK;
242 dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
243 sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
246 if (NULL == dma->sglist) {
247 dprintk(1,"scatterlist is NULL\n");
250 if (!dma->bus_addr) {
251 dma->sglen = pci_map_sg(dev,dma->sglist,
252 dma->nr_pages, dma->direction);
253 if (0 == dma->sglen) {
255 "%s: videobuf_map_sg failed\n",__FUNCTION__);
265 int videobuf_dma_sync(struct videobuf_queue *q,struct videobuf_dmabuf *dma)
269 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
272 pci_dma_sync_sg_for_cpu (dev,dma->sglist,dma->nr_pages,dma->direction);
276 int videobuf_dma_unmap(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
280 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
284 pci_unmap_sg (dev,dma->sglist,dma->nr_pages,dma->direction);
292 int videobuf_dma_free(struct videobuf_dmabuf *dma)
294 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
299 for (i=0; i < dma->nr_pages; i++)
300 page_cache_release(dma->pages[i]);
312 dma->direction = PCI_DMA_NONE;
316 /* --------------------------------------------------------------------- */
318 int videobuf_pci_dma_map(struct pci_dev *pci,struct videobuf_dmabuf *dma)
320 struct videobuf_queue q;
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;
333 return (videobuf_dma_unmap(&q,dma));
336 /* --------------------------------------------------------------------- */
339 videobuf_vm_open(struct vm_area_struct *vma)
341 struct videobuf_mapping *map = vma->vm_private_data;
343 dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map,
344 map->count,vma->vm_start,vma->vm_end);
349 videobuf_vm_close(struct vm_area_struct *vma)
351 struct videobuf_mapping *map = vma->vm_private_data;
352 struct videobuf_queue *q = map->q;
353 struct videbuf_pci_sg_memory *mem;
356 dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
357 map->count,vma->vm_start,vma->vm_end);
360 if (0 == map->count) {
361 dprintk(1,"munmap %p q=%p\n",map,q);
362 mutex_lock(&q->lock);
363 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
364 if (NULL == q->bufs[i])
366 mem=q->bufs[i]->priv;
371 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
373 if (q->bufs[i]->map != map)
375 q->bufs[i]->map = NULL;
376 q->bufs[i]->baddr = 0;
377 q->ops->buf_release(q,q->bufs[i]);
379 mutex_unlock(&q->lock);
386 * Get a anonymous page for the mapping. Make sure we can DMA to that
387 * memory location with 32bit PCI devices (i.e. don't use highmem for
388 * now ...). Bounce buffers don't work very well for the data rates
392 videobuf_vm_nopage(struct vm_area_struct *vma, unsigned long vaddr,
397 dprintk(3,"nopage: fault @ %08lx [vma %08lx-%08lx]\n",
398 vaddr,vma->vm_start,vma->vm_end);
399 if (vaddr > vma->vm_end)
400 return NOPAGE_SIGBUS;
401 page = alloc_page(GFP_USER | __GFP_DMA32);
404 clear_user_page(page_address(page), vaddr, page);
406 *type = VM_FAULT_MINOR;
410 static struct vm_operations_struct videobuf_vm_ops =
412 .open = videobuf_vm_open,
413 .close = videobuf_vm_close,
414 .nopage = videobuf_vm_nopage,
417 /* ---------------------------------------------------------------------
418 * PCI handlers for the generic methods
421 /* Allocated area consists on 3 parts:
423 struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
424 struct videobuf_pci_sg_memory
427 static void *__videobuf_alloc(size_t size)
429 struct videbuf_pci_sg_memory *mem;
430 struct videobuf_buffer *vb;
432 vb = kzalloc(size+sizeof(*mem),GFP_KERNEL);
434 mem = vb->priv = ((char *)vb)+size;
435 mem->magic=MAGIC_SG_MEM;
437 videobuf_dma_init(&mem->dma);
439 dprintk(1,"%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
440 __FUNCTION__,vb,(long)sizeof(*vb),(long)size-sizeof(*vb),
441 mem,(long)sizeof(*mem));
446 static int __videobuf_iolock (struct videobuf_queue* q,
447 struct videobuf_buffer *vb,
448 struct v4l2_framebuffer *fbuf)
452 struct videbuf_pci_sg_memory *mem=vb->priv;
455 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
457 switch (vb->memory) {
458 case V4L2_MEMORY_MMAP:
459 case V4L2_MEMORY_USERPTR:
460 if (0 == vb->baddr) {
461 /* no userspace addr -- kernel bounce buffer */
462 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
463 err = videobuf_dma_init_kernel( &mem->dma,
468 } else if (vb->memory == V4L2_MEMORY_USERPTR) {
469 /* dma directly to userspace */
470 err = videobuf_dma_init_user( &mem->dma,
472 vb->baddr,vb->bsize );
476 /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP
477 buffers can only be called from videobuf_qbuf
478 we take current->mm->mmap_sem there, to prevent
479 locking inversion, so don't take it here */
481 err = videobuf_dma_init_user_locked(&mem->dma,
483 vb->baddr, vb->bsize);
488 case V4L2_MEMORY_OVERLAY:
491 /* FIXME: need sanity checks for vb->boff */
493 * Using a double cast to avoid compiler warnings when
494 * building for PAE. Compiler doesn't like direct casting
495 * of a 32 bit ptr to 64 bit integer.
497 bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
498 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
499 err = videobuf_dma_init_overlay(&mem->dma,PCI_DMA_FROMDEVICE,
507 err = videobuf_dma_map(q,&mem->dma);
514 static int __videobuf_sync(struct videobuf_queue *q,
515 struct videobuf_buffer *buf)
517 struct videbuf_pci_sg_memory *mem=buf->priv;
519 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
521 return videobuf_dma_sync(q,&mem->dma);
524 static int __videobuf_mmap_free(struct videobuf_queue *q)
528 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
538 static int __videobuf_mmap_mapper(struct videobuf_queue *q,
539 struct vm_area_struct *vma)
541 struct videbuf_pci_sg_memory *mem;
542 struct videobuf_mapping *map;
543 unsigned int first,last,size,i;
547 if (!(vma->vm_flags & VM_WRITE)) {
548 dprintk(1,"mmap app bug: PROT_WRITE please\n");
551 if (!(vma->vm_flags & VM_SHARED)) {
552 dprintk(1,"mmap app bug: MAP_SHARED please\n");
556 /* look for first buffer to map */
557 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
558 if (NULL == q->bufs[first])
560 mem=q->bufs[first]->priv;
562 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
564 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
566 if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
569 if (VIDEO_MAX_FRAME == first) {
570 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
571 (vma->vm_pgoff << PAGE_SHIFT));
575 /* look for last buffer to map */
576 for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
577 if (NULL == q->bufs[last])
579 if (V4L2_MEMORY_MMAP != q->bufs[last]->memory)
581 if (q->bufs[last]->map) {
585 size += q->bufs[last]->bsize;
586 if (size == (vma->vm_end - vma->vm_start))
589 if (VIDEO_MAX_FRAME == last) {
590 dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n",
591 (vma->vm_end - vma->vm_start));
595 /* create mapping + update buffer list */
597 map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
600 for (size = 0, i = first; i <= last; size += q->bufs[i++]->bsize) {
601 q->bufs[i]->map = map;
602 q->bufs[i]->baddr = vma->vm_start + size;
605 map->start = vma->vm_start;
606 map->end = vma->vm_end;
608 vma->vm_ops = &videobuf_vm_ops;
609 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
610 vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
611 vma->vm_private_data = map;
612 dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
613 map,q,vma->vm_start,vma->vm_end,vma->vm_pgoff,first,last);
620 static int __videobuf_copy_to_user ( struct videobuf_queue *q,
621 char __user *data, size_t count,
624 struct videbuf_pci_sg_memory *mem=q->read_buf->priv;
626 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
628 /* copy to userspace */
629 if (count > q->read_buf->size - q->read_off)
630 count = q->read_buf->size - q->read_off;
632 if (copy_to_user(data, mem->dma.vmalloc+q->read_off, count))
638 static int __videobuf_copy_stream ( struct videobuf_queue *q,
639 char __user *data, size_t count, size_t pos,
640 int vbihack, int nonblocking )
643 struct videbuf_pci_sg_memory *mem=q->read_buf->priv;
645 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
648 /* dirty, undocumented hack -- pass the frame counter
649 * within the last four bytes of each vbi data block.
650 * We need that one to maintain backward compatibility
651 * to all vbi decoding software out there ... */
652 fc = (unsigned int*)mem->dma.vmalloc;
653 fc += (q->read_buf->size>>2) -1;
654 *fc = q->read_buf->field_count >> 1;
655 dprintk(1,"vbihack: %d\n",*fc);
658 /* copy stuff using the common method */
659 count = __videobuf_copy_to_user (q,data,count,nonblocking);
661 if ( (count==-EFAULT) && (0 == pos) )
667 static struct videobuf_qtype_ops pci_ops = {
668 .magic = MAGIC_QTYPE_OPS,
670 .alloc = __videobuf_alloc,
671 .iolock = __videobuf_iolock,
672 .sync = __videobuf_sync,
673 .mmap_free = __videobuf_mmap_free,
674 .mmap_mapper = __videobuf_mmap_mapper,
675 .video_copy_to_user = __videobuf_copy_to_user,
676 .copy_stream = __videobuf_copy_stream,
679 void *videobuf_pci_alloc (size_t size)
681 struct videobuf_queue q;
683 /* Required to make generic handler to call __videobuf_alloc */
688 return videobuf_alloc (&q);
691 void videobuf_queue_pci_init(struct videobuf_queue* q,
692 struct videobuf_queue_ops *ops,
695 enum v4l2_buf_type type,
696 enum v4l2_field field,
700 videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
704 /* --------------------------------------------------------------------- */
706 EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg);
708 EXPORT_SYMBOL_GPL(videobuf_to_dma);
709 EXPORT_SYMBOL_GPL(videobuf_dma_init);
710 EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
711 EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
712 EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
713 EXPORT_SYMBOL_GPL(videobuf_dma_map);
714 EXPORT_SYMBOL_GPL(videobuf_dma_sync);
715 EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
716 EXPORT_SYMBOL_GPL(videobuf_dma_free);
718 EXPORT_SYMBOL_GPL(videobuf_pci_dma_map);
719 EXPORT_SYMBOL_GPL(videobuf_pci_dma_unmap);
720 EXPORT_SYMBOL_GPL(videobuf_pci_alloc);
722 EXPORT_SYMBOL_GPL(videobuf_queue_pci_init);