media: videobuf-dma-sg: Fix dma_{sync,unmap}_sg() calls
[linux/fpc-iii.git] / drivers / media / v4l2-core / videobuf-dma-sg.c
blob87b16773f326f94fe42cf7f00ea709801809163f
1 /*
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/mm.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>
32 #include <asm/page.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", \
43 is, should); \
44 BUG(); \
47 static int debug;
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...) \
55 if (debug >= level) \
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,
66 int nr_pages)
68 struct scatterlist *sglist;
69 struct page *pg;
70 int i;
72 sglist = vzalloc(nr_pages * sizeof(*sglist));
73 if (NULL == sglist)
74 return NULL;
75 sg_init_table(sglist, nr_pages);
76 for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
77 pg = vmalloc_to_page(virt);
78 if (NULL == pg)
79 goto err;
80 BUG_ON(PageHighMem(pg));
81 sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
83 return sglist;
85 err:
86 vfree(sglist);
87 return NULL;
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;
99 int i;
101 if (NULL == pages[0])
102 return NULL;
103 sglist = vmalloc(nr_pages * sizeof(*sglist));
104 if (NULL == sglist)
105 return NULL;
106 sg_init_table(sglist, nr_pages);
108 if (PageHighMem(pages[0]))
109 /* DMA to highmem pages might not work */
110 goto highmem;
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])
116 goto nopage;
117 if (PageHighMem(pages[i]))
118 goto highmem;
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);
122 return sglist;
124 nopage:
125 dprintk(2, "sgl: oops - no page\n");
126 vfree(sglist);
127 return NULL;
129 highmem:
130 dprintk(2, "sgl: oops - highmem page\n");
131 vfree(sglist);
132 return NULL;
135 /* --------------------------------------------------------------------- */
137 struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf)
139 struct videobuf_dma_sg_memory *mem = buf->priv;
140 BUG_ON(!mem);
142 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
144 return &mem->dma;
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;
158 int err, rw = 0;
159 unsigned int flags = FOLL_FORCE;
161 dma->direction = direction;
162 switch (dma->direction) {
163 case DMA_FROM_DEVICE:
164 rw = READ;
165 break;
166 case DMA_TO_DEVICE:
167 rw = WRITE;
168 break;
169 default:
170 BUG();
173 first = (data & PAGE_MASK) >> PAGE_SHIFT;
174 last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
175 dma->offset = data & ~PAGE_MASK;
176 dma->size = size;
177 dma->nr_pages = last-first+1;
178 dma->pages = kmalloc(dma->nr_pages * sizeof(struct page *), GFP_KERNEL);
179 if (NULL == dma->pages)
180 return -ENOMEM;
182 if (rw == READ)
183 flags |= FOLL_WRITE;
185 dprintk(1, "init user [0x%lx+0x%lx => %d pages]\n",
186 data, size, dma->nr_pages);
188 err = get_user_pages_longterm(data & PAGE_MASK, dma->nr_pages,
189 flags, dma->pages, NULL);
191 if (err != dma->nr_pages) {
192 dma->nr_pages = (err >= 0) ? err : 0;
193 dprintk(1, "get_user_pages_longterm: err=%d [%d]\n", err,
194 dma->nr_pages);
195 return err < 0 ? err : -EINVAL;
197 return 0;
200 static int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
201 unsigned long data, unsigned long size)
203 int ret;
205 down_read(&current->mm->mmap_sem);
206 ret = videobuf_dma_init_user_locked(dma, direction, data, size);
207 up_read(&current->mm->mmap_sem);
209 return ret;
212 static int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
213 int nr_pages)
215 int i;
217 dprintk(1, "init kernel [%d pages]\n", nr_pages);
219 dma->direction = direction;
220 dma->vaddr_pages = kcalloc(nr_pages, sizeof(*dma->vaddr_pages),
221 GFP_KERNEL);
222 if (!dma->vaddr_pages)
223 return -ENOMEM;
225 dma->dma_addr = kcalloc(nr_pages, sizeof(*dma->dma_addr), GFP_KERNEL);
226 if (!dma->dma_addr) {
227 kfree(dma->vaddr_pages);
228 return -ENOMEM;
230 for (i = 0; i < nr_pages; i++) {
231 void *addr;
233 addr = dma_alloc_coherent(dma->dev, PAGE_SIZE,
234 &(dma->dma_addr[i]), GFP_KERNEL);
235 if (addr == NULL)
236 goto out_free_pages;
238 dma->vaddr_pages[i] = virt_to_page(addr);
240 dma->vaddr = vmap(dma->vaddr_pages, nr_pages, VM_MAP | VM_IOREMAP,
241 PAGE_KERNEL);
242 if (NULL == dma->vaddr) {
243 dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
244 goto out_free_pages;
247 dprintk(1, "vmalloc is at addr %p, size=%d\n",
248 dma->vaddr, nr_pages << PAGE_SHIFT);
250 memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT);
251 dma->nr_pages = nr_pages;
253 return 0;
254 out_free_pages:
255 while (i > 0) {
256 void *addr;
258 i--;
259 addr = page_address(dma->vaddr_pages[i]);
260 dma_free_coherent(dma->dev, PAGE_SIZE, addr, dma->dma_addr[i]);
262 kfree(dma->dma_addr);
263 dma->dma_addr = NULL;
264 kfree(dma->vaddr_pages);
265 dma->vaddr_pages = NULL;
267 return -ENOMEM;
271 static int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
272 dma_addr_t addr, int nr_pages)
274 dprintk(1, "init overlay [%d pages @ bus 0x%lx]\n",
275 nr_pages, (unsigned long)addr);
276 dma->direction = direction;
278 if (0 == addr)
279 return -EINVAL;
281 dma->bus_addr = addr;
282 dma->nr_pages = nr_pages;
284 return 0;
287 static int videobuf_dma_map(struct device *dev, struct videobuf_dmabuf *dma)
289 MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
290 BUG_ON(0 == dma->nr_pages);
292 if (dma->pages) {
293 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
294 dma->offset, dma->size);
296 if (dma->vaddr) {
297 dma->sglist = videobuf_vmalloc_to_sg(dma->vaddr,
298 dma->nr_pages);
300 if (dma->bus_addr) {
301 dma->sglist = vmalloc(sizeof(*dma->sglist));
302 if (NULL != dma->sglist) {
303 dma->sglen = 1;
304 sg_dma_address(&dma->sglist[0]) = dma->bus_addr
305 & PAGE_MASK;
306 dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
307 sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
310 if (NULL == dma->sglist) {
311 dprintk(1, "scatterlist is NULL\n");
312 return -ENOMEM;
314 if (!dma->bus_addr) {
315 dma->sglen = dma_map_sg(dev, dma->sglist,
316 dma->nr_pages, dma->direction);
317 if (0 == dma->sglen) {
318 printk(KERN_WARNING
319 "%s: videobuf_map_sg failed\n", __func__);
320 vfree(dma->sglist);
321 dma->sglist = NULL;
322 dma->sglen = 0;
323 return -ENOMEM;
327 return 0;
330 int videobuf_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma)
332 MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
334 if (!dma->sglen)
335 return 0;
337 dma_unmap_sg(dev, dma->sglist, dma->nr_pages, dma->direction);
339 vfree(dma->sglist);
340 dma->sglist = NULL;
341 dma->sglen = 0;
343 return 0;
345 EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
347 int videobuf_dma_free(struct videobuf_dmabuf *dma)
349 int i;
350 MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
351 BUG_ON(dma->sglen);
353 if (dma->pages) {
354 for (i = 0; i < dma->nr_pages; i++)
355 put_page(dma->pages[i]);
356 kfree(dma->pages);
357 dma->pages = NULL;
360 if (dma->dma_addr) {
361 for (i = 0; i < dma->nr_pages; i++) {
362 void *addr;
364 addr = page_address(dma->vaddr_pages[i]);
365 dma_free_coherent(dma->dev, PAGE_SIZE, addr,
366 dma->dma_addr[i]);
368 kfree(dma->dma_addr);
369 dma->dma_addr = NULL;
370 kfree(dma->vaddr_pages);
371 dma->vaddr_pages = NULL;
372 vunmap(dma->vaddr);
373 dma->vaddr = NULL;
376 if (dma->bus_addr)
377 dma->bus_addr = 0;
378 dma->direction = DMA_NONE;
380 return 0;
382 EXPORT_SYMBOL_GPL(videobuf_dma_free);
384 /* --------------------------------------------------------------------- */
386 static void videobuf_vm_open(struct vm_area_struct *vma)
388 struct videobuf_mapping *map = vma->vm_private_data;
390 dprintk(2, "vm_open %p [count=%d,vma=%08lx-%08lx]\n", map,
391 map->count, vma->vm_start, vma->vm_end);
393 map->count++;
396 static void videobuf_vm_close(struct vm_area_struct *vma)
398 struct videobuf_mapping *map = vma->vm_private_data;
399 struct videobuf_queue *q = map->q;
400 struct videobuf_dma_sg_memory *mem;
401 int i;
403 dprintk(2, "vm_close %p [count=%d,vma=%08lx-%08lx]\n", map,
404 map->count, vma->vm_start, vma->vm_end);
406 map->count--;
407 if (0 == map->count) {
408 dprintk(1, "munmap %p q=%p\n", map, q);
409 videobuf_queue_lock(q);
410 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
411 if (NULL == q->bufs[i])
412 continue;
413 mem = q->bufs[i]->priv;
414 if (!mem)
415 continue;
417 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
419 if (q->bufs[i]->map != map)
420 continue;
421 q->bufs[i]->map = NULL;
422 q->bufs[i]->baddr = 0;
423 q->ops->buf_release(q, q->bufs[i]);
425 videobuf_queue_unlock(q);
426 kfree(map);
428 return;
432 * Get a anonymous page for the mapping. Make sure we can DMA to that
433 * memory location with 32bit PCI devices (i.e. don't use highmem for
434 * now ...). Bounce buffers don't work very well for the data rates
435 * video capture has.
437 static vm_fault_t videobuf_vm_fault(struct vm_fault *vmf)
439 struct vm_area_struct *vma = vmf->vma;
440 struct page *page;
442 dprintk(3, "fault: fault @ %08lx [vma %08lx-%08lx]\n",
443 vmf->address, vma->vm_start, vma->vm_end);
445 page = alloc_page(GFP_USER | __GFP_DMA32);
446 if (!page)
447 return VM_FAULT_OOM;
448 clear_user_highpage(page, vmf->address);
449 vmf->page = page;
451 return 0;
454 static const struct vm_operations_struct videobuf_vm_ops = {
455 .open = videobuf_vm_open,
456 .close = videobuf_vm_close,
457 .fault = videobuf_vm_fault,
460 /* ---------------------------------------------------------------------
461 * SG handlers for the generic methods
464 /* Allocated area consists on 3 parts:
465 struct video_buffer
466 struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
467 struct videobuf_dma_sg_memory
470 static struct videobuf_buffer *__videobuf_alloc_vb(size_t size)
472 struct videobuf_dma_sg_memory *mem;
473 struct videobuf_buffer *vb;
475 vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
476 if (!vb)
477 return vb;
479 mem = vb->priv = ((char *)vb) + size;
480 mem->magic = MAGIC_SG_MEM;
482 videobuf_dma_init(&mem->dma);
484 dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
485 __func__, vb, (long)sizeof(*vb), (long)size - sizeof(*vb),
486 mem, (long)sizeof(*mem));
488 return vb;
491 static void *__videobuf_to_vaddr(struct videobuf_buffer *buf)
493 struct videobuf_dma_sg_memory *mem = buf->priv;
494 BUG_ON(!mem);
496 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
498 return mem->dma.vaddr;
501 static int __videobuf_iolock(struct videobuf_queue *q,
502 struct videobuf_buffer *vb,
503 struct v4l2_framebuffer *fbuf)
505 int err, pages;
506 dma_addr_t bus;
507 struct videobuf_dma_sg_memory *mem = vb->priv;
508 BUG_ON(!mem);
510 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
512 if (!mem->dma.dev)
513 mem->dma.dev = q->dev;
514 else
515 WARN_ON(mem->dma.dev != q->dev);
517 switch (vb->memory) {
518 case V4L2_MEMORY_MMAP:
519 case V4L2_MEMORY_USERPTR:
520 if (0 == vb->baddr) {
521 /* no userspace addr -- kernel bounce buffer */
522 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
523 err = videobuf_dma_init_kernel(&mem->dma,
524 DMA_FROM_DEVICE,
525 pages);
526 if (0 != err)
527 return err;
528 } else if (vb->memory == V4L2_MEMORY_USERPTR) {
529 /* dma directly to userspace */
530 err = videobuf_dma_init_user(&mem->dma,
531 DMA_FROM_DEVICE,
532 vb->baddr, vb->bsize);
533 if (0 != err)
534 return err;
535 } else {
536 /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP
537 buffers can only be called from videobuf_qbuf
538 we take current->mm->mmap_sem there, to prevent
539 locking inversion, so don't take it here */
541 err = videobuf_dma_init_user_locked(&mem->dma,
542 DMA_FROM_DEVICE,
543 vb->baddr, vb->bsize);
544 if (0 != err)
545 return err;
547 break;
548 case V4L2_MEMORY_OVERLAY:
549 if (NULL == fbuf)
550 return -EINVAL;
551 /* FIXME: need sanity checks for vb->boff */
553 * Using a double cast to avoid compiler warnings when
554 * building for PAE. Compiler doesn't like direct casting
555 * of a 32 bit ptr to 64 bit integer.
557 bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
558 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
559 err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE,
560 bus, pages);
561 if (0 != err)
562 return err;
563 break;
564 default:
565 BUG();
567 err = videobuf_dma_map(q->dev, &mem->dma);
568 if (0 != err)
569 return err;
571 return 0;
574 static int __videobuf_sync(struct videobuf_queue *q,
575 struct videobuf_buffer *buf)
577 struct videobuf_dma_sg_memory *mem = buf->priv;
578 BUG_ON(!mem || !mem->dma.sglen);
580 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
581 MAGIC_CHECK(mem->dma.magic, MAGIC_DMABUF);
583 dma_sync_sg_for_cpu(q->dev, mem->dma.sglist,
584 mem->dma.nr_pages, mem->dma.direction);
586 return 0;
589 static int __videobuf_mmap_mapper(struct videobuf_queue *q,
590 struct videobuf_buffer *buf,
591 struct vm_area_struct *vma)
593 struct videobuf_dma_sg_memory *mem = buf->priv;
594 struct videobuf_mapping *map;
595 unsigned int first, last, size = 0, i;
596 int retval;
598 retval = -EINVAL;
600 BUG_ON(!mem);
601 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
603 /* look for first buffer to map */
604 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
605 if (buf == q->bufs[first]) {
606 size = PAGE_ALIGN(q->bufs[first]->bsize);
607 break;
611 /* paranoia, should never happen since buf is always valid. */
612 if (!size) {
613 dprintk(1, "mmap app bug: offset invalid [offset=0x%lx]\n",
614 (vma->vm_pgoff << PAGE_SHIFT));
615 goto done;
618 last = first;
620 /* create mapping + update buffer list */
621 retval = -ENOMEM;
622 map = kmalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
623 if (NULL == map)
624 goto done;
626 size = 0;
627 for (i = first; i <= last; i++) {
628 if (NULL == q->bufs[i])
629 continue;
630 q->bufs[i]->map = map;
631 q->bufs[i]->baddr = vma->vm_start + size;
632 size += PAGE_ALIGN(q->bufs[i]->bsize);
635 map->count = 1;
636 map->q = q;
637 vma->vm_ops = &videobuf_vm_ops;
638 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
639 vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
640 vma->vm_private_data = map;
641 dprintk(1, "mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
642 map, q, vma->vm_start, vma->vm_end, vma->vm_pgoff, first, last);
643 retval = 0;
645 done:
646 return retval;
649 static struct videobuf_qtype_ops sg_ops = {
650 .magic = MAGIC_QTYPE_OPS,
652 .alloc_vb = __videobuf_alloc_vb,
653 .iolock = __videobuf_iolock,
654 .sync = __videobuf_sync,
655 .mmap_mapper = __videobuf_mmap_mapper,
656 .vaddr = __videobuf_to_vaddr,
659 void *videobuf_sg_alloc(size_t size)
661 struct videobuf_queue q;
663 /* Required to make generic handler to call __videobuf_alloc */
664 q.int_ops = &sg_ops;
666 q.msize = size;
668 return videobuf_alloc_vb(&q);
670 EXPORT_SYMBOL_GPL(videobuf_sg_alloc);
672 void videobuf_queue_sg_init(struct videobuf_queue *q,
673 const struct videobuf_queue_ops *ops,
674 struct device *dev,
675 spinlock_t *irqlock,
676 enum v4l2_buf_type type,
677 enum v4l2_field field,
678 unsigned int msize,
679 void *priv,
680 struct mutex *ext_lock)
682 videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
683 priv, &sg_ops, ext_lock);
685 EXPORT_SYMBOL_GPL(videobuf_queue_sg_init);