Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/sage/ceph...
[pandora-kernel.git] / drivers / media / video / videobuf-dma-sg.c
1 /*
2  * helper functions for SG DMA video4linux capture buffers
3  *
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.
8  *
9  * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
10  *
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
15  *
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
19  */
20
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>
27
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>
34
35 #include <media/videobuf-dma-sg.h>
36
37 #define MAGIC_DMABUF 0x19721112
38 #define MAGIC_SG_MEM 0x17890714
39
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();                                                  \
45         }
46
47 static int debug;
48 module_param(debug, int, 0644);
49
50 MODULE_DESCRIPTION("helper module to manage video4linux dma sg buffers");
51 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
52 MODULE_LICENSE("GPL");
53
54 #define dprintk(level, fmt, arg...)                                     \
55         if (debug >= level)                                             \
56                 printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg)
57
58 /* --------------------------------------------------------------------- */
59
60 /*
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.
64  */
65 static struct scatterlist *videobuf_vmalloc_to_sg(unsigned char *virt,
66                                                   int nr_pages)
67 {
68         struct scatterlist *sglist;
69         struct page *pg;
70         int i;
71
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);
82         }
83         return sglist;
84
85 err:
86         vfree(sglist);
87         return NULL;
88 }
89
90 /*
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.
94  */
95 static struct scatterlist *videobuf_pages_to_sg(struct page **pages,
96                                         int nr_pages, int offset, size_t size)
97 {
98         struct scatterlist *sglist;
99         int i;
100
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);
107
108         if (PageHighMem(pages[0]))
109                 /* DMA to highmem pages might not work */
110                 goto highmem;
111         sg_set_page(&sglist[0], pages[0], PAGE_SIZE - offset, offset);
112         size -= PAGE_SIZE - offset;
113         for (i = 1; i < nr_pages; i++) {
114                 if (NULL == pages[i])
115                         goto nopage;
116                 if (PageHighMem(pages[i]))
117                         goto highmem;
118                 sg_set_page(&sglist[i], pages[i], min_t(size_t, PAGE_SIZE, size), 0);
119                 size -= min_t(size_t, PAGE_SIZE, size);
120         }
121         return sglist;
122
123 nopage:
124         dprintk(2, "sgl: oops - no page\n");
125         vfree(sglist);
126         return NULL;
127
128 highmem:
129         dprintk(2, "sgl: oops - highmem page\n");
130         vfree(sglist);
131         return NULL;
132 }
133
134 /* --------------------------------------------------------------------- */
135
136 struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf)
137 {
138         struct videobuf_dma_sg_memory *mem = buf->priv;
139         BUG_ON(!mem);
140
141         MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
142
143         return &mem->dma;
144 }
145 EXPORT_SYMBOL_GPL(videobuf_to_dma);
146
147 void videobuf_dma_init(struct videobuf_dmabuf *dma)
148 {
149         memset(dma, 0, sizeof(*dma));
150         dma->magic = MAGIC_DMABUF;
151 }
152 EXPORT_SYMBOL_GPL(videobuf_dma_init);
153
154 static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma,
155                         int direction, unsigned long data, unsigned long size)
156 {
157         unsigned long first, last;
158         int err, rw = 0;
159
160         dma->direction = direction;
161         switch (dma->direction) {
162         case DMA_FROM_DEVICE:
163                 rw = READ;
164                 break;
165         case DMA_TO_DEVICE:
166                 rw = WRITE;
167                 break;
168         default:
169                 BUG();
170         }
171
172         first = (data          & PAGE_MASK) >> PAGE_SHIFT;
173         last  = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
174         dma->offset = data & ~PAGE_MASK;
175         dma->size = size;
176         dma->nr_pages = last-first+1;
177         dma->pages = kmalloc(dma->nr_pages * sizeof(struct page *), GFP_KERNEL);
178         if (NULL == dma->pages)
179                 return -ENOMEM;
180
181         dprintk(1, "init user [0x%lx+0x%lx => %d pages]\n",
182                 data, size, dma->nr_pages);
183
184         err = get_user_pages(current, current->mm,
185                              data & PAGE_MASK, dma->nr_pages,
186                              rw == READ, 1, /* force */
187                              dma->pages, NULL);
188
189         if (err != dma->nr_pages) {
190                 dma->nr_pages = (err >= 0) ? err : 0;
191                 dprintk(1, "get_user_pages: err=%d [%d]\n", err, dma->nr_pages);
192                 return err < 0 ? err : -EINVAL;
193         }
194         return 0;
195 }
196
197 int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
198                            unsigned long data, unsigned long size)
199 {
200         int ret;
201
202         down_read(&current->mm->mmap_sem);
203         ret = videobuf_dma_init_user_locked(dma, direction, data, size);
204         up_read(&current->mm->mmap_sem);
205
206         return ret;
207 }
208 EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
209
210 int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
211                              int nr_pages)
212 {
213         dprintk(1, "init kernel [%d pages]\n", nr_pages);
214
215         dma->direction = direction;
216         dma->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
217         if (NULL == dma->vaddr) {
218                 dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
219                 return -ENOMEM;
220         }
221
222         dprintk(1, "vmalloc is at addr 0x%08lx, size=%d\n",
223                                 (unsigned long)dma->vaddr,
224                                 nr_pages << PAGE_SHIFT);
225
226         memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT);
227         dma->nr_pages = nr_pages;
228
229         return 0;
230 }
231 EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
232
233 int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
234                               dma_addr_t addr, int nr_pages)
235 {
236         dprintk(1, "init overlay [%d pages @ bus 0x%lx]\n",
237                 nr_pages, (unsigned long)addr);
238         dma->direction = direction;
239
240         if (0 == addr)
241                 return -EINVAL;
242
243         dma->bus_addr = addr;
244         dma->nr_pages = nr_pages;
245
246         return 0;
247 }
248 EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
249
250 int videobuf_dma_map(struct device *dev, struct videobuf_dmabuf *dma)
251 {
252         MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
253         BUG_ON(0 == dma->nr_pages);
254
255         if (dma->pages) {
256                 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
257                                                    dma->offset, dma->size);
258         }
259         if (dma->vaddr) {
260                 dma->sglist = videobuf_vmalloc_to_sg(dma->vaddr,
261                                                      dma->nr_pages);
262         }
263         if (dma->bus_addr) {
264                 dma->sglist = vmalloc(sizeof(*dma->sglist));
265                 if (NULL != dma->sglist) {
266                         dma->sglen = 1;
267                         sg_dma_address(&dma->sglist[0]) = dma->bus_addr
268                                                         & PAGE_MASK;
269                         dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
270                         sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
271                 }
272         }
273         if (NULL == dma->sglist) {
274                 dprintk(1, "scatterlist is NULL\n");
275                 return -ENOMEM;
276         }
277         if (!dma->bus_addr) {
278                 dma->sglen = dma_map_sg(dev, dma->sglist,
279                                         dma->nr_pages, dma->direction);
280                 if (0 == dma->sglen) {
281                         printk(KERN_WARNING
282                                "%s: videobuf_map_sg failed\n", __func__);
283                         vfree(dma->sglist);
284                         dma->sglist = NULL;
285                         dma->sglen = 0;
286                         return -ENOMEM;
287                 }
288         }
289
290         return 0;
291 }
292 EXPORT_SYMBOL_GPL(videobuf_dma_map);
293
294 int videobuf_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma)
295 {
296         MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
297
298         if (!dma->sglen)
299                 return 0;
300
301         dma_unmap_sg(dev, dma->sglist, dma->sglen, dma->direction);
302
303         vfree(dma->sglist);
304         dma->sglist = NULL;
305         dma->sglen = 0;
306
307         return 0;
308 }
309 EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
310
311 int videobuf_dma_free(struct videobuf_dmabuf *dma)
312 {
313         int i;
314         MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
315         BUG_ON(dma->sglen);
316
317         if (dma->pages) {
318                 for (i = 0; i < dma->nr_pages; i++)
319                         page_cache_release(dma->pages[i]);
320                 kfree(dma->pages);
321                 dma->pages = NULL;
322         }
323
324         vfree(dma->vaddr);
325         dma->vaddr = NULL;
326
327         if (dma->bus_addr)
328                 dma->bus_addr = 0;
329         dma->direction = DMA_NONE;
330
331         return 0;
332 }
333 EXPORT_SYMBOL_GPL(videobuf_dma_free);
334
335 /* --------------------------------------------------------------------- */
336
337 static void videobuf_vm_open(struct vm_area_struct *vma)
338 {
339         struct videobuf_mapping *map = vma->vm_private_data;
340
341         dprintk(2, "vm_open %p [count=%d,vma=%08lx-%08lx]\n", map,
342                 map->count, vma->vm_start, vma->vm_end);
343
344         map->count++;
345 }
346
347 static void videobuf_vm_close(struct vm_area_struct *vma)
348 {
349         struct videobuf_mapping *map = vma->vm_private_data;
350         struct videobuf_queue *q = map->q;
351         struct videobuf_dma_sg_memory *mem;
352         int i;
353
354         dprintk(2, "vm_close %p [count=%d,vma=%08lx-%08lx]\n", map,
355                 map->count, vma->vm_start, vma->vm_end);
356
357         map->count--;
358         if (0 == map->count) {
359                 dprintk(1, "munmap %p q=%p\n", map, q);
360                 videobuf_queue_lock(q);
361                 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
362                         if (NULL == q->bufs[i])
363                                 continue;
364                         mem = q->bufs[i]->priv;
365                         if (!mem)
366                                 continue;
367
368                         MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
369
370                         if (q->bufs[i]->map != map)
371                                 continue;
372                         q->bufs[i]->map   = NULL;
373                         q->bufs[i]->baddr = 0;
374                         q->ops->buf_release(q, q->bufs[i]);
375                 }
376                 videobuf_queue_unlock(q);
377                 kfree(map);
378         }
379         return;
380 }
381
382 /*
383  * Get a anonymous page for the mapping.  Make sure we can DMA to that
384  * memory location with 32bit PCI devices (i.e. don't use highmem for
385  * now ...).  Bounce buffers don't work very well for the data rates
386  * video capture has.
387  */
388 static int videobuf_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
389 {
390         struct page *page;
391
392         dprintk(3, "fault: fault @ %08lx [vma %08lx-%08lx]\n",
393                 (unsigned long)vmf->virtual_address,
394                 vma->vm_start, vma->vm_end);
395
396         page = alloc_page(GFP_USER | __GFP_DMA32);
397         if (!page)
398                 return VM_FAULT_OOM;
399         clear_user_highpage(page, (unsigned long)vmf->virtual_address);
400         vmf->page = page;
401
402         return 0;
403 }
404
405 static const struct vm_operations_struct videobuf_vm_ops = {
406         .open   = videobuf_vm_open,
407         .close  = videobuf_vm_close,
408         .fault  = videobuf_vm_fault,
409 };
410
411 /* ---------------------------------------------------------------------
412  * SG handlers for the generic methods
413  */
414
415 /* Allocated area consists on 3 parts:
416         struct video_buffer
417         struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
418         struct videobuf_dma_sg_memory
419  */
420
421 static struct videobuf_buffer *__videobuf_alloc_vb(size_t size)
422 {
423         struct videobuf_dma_sg_memory *mem;
424         struct videobuf_buffer *vb;
425
426         vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
427         if (!vb)
428                 return vb;
429
430         mem = vb->priv = ((char *)vb) + size;
431         mem->magic = MAGIC_SG_MEM;
432
433         videobuf_dma_init(&mem->dma);
434
435         dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
436                 __func__, vb, (long)sizeof(*vb), (long)size - sizeof(*vb),
437                 mem, (long)sizeof(*mem));
438
439         return vb;
440 }
441
442 static void *__videobuf_to_vaddr(struct videobuf_buffer *buf)
443 {
444         struct videobuf_dma_sg_memory *mem = buf->priv;
445         BUG_ON(!mem);
446
447         MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
448
449         return mem->dma.vaddr;
450 }
451
452 static int __videobuf_iolock(struct videobuf_queue *q,
453                              struct videobuf_buffer *vb,
454                              struct v4l2_framebuffer *fbuf)
455 {
456         int err, pages;
457         dma_addr_t bus;
458         struct videobuf_dma_sg_memory *mem = vb->priv;
459         BUG_ON(!mem);
460
461         MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
462
463         switch (vb->memory) {
464         case V4L2_MEMORY_MMAP:
465         case V4L2_MEMORY_USERPTR:
466                 if (0 == vb->baddr) {
467                         /* no userspace addr -- kernel bounce buffer */
468                         pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
469                         err = videobuf_dma_init_kernel(&mem->dma,
470                                                        DMA_FROM_DEVICE,
471                                                        pages);
472                         if (0 != err)
473                                 return err;
474                 } else if (vb->memory == V4L2_MEMORY_USERPTR) {
475                         /* dma directly to userspace */
476                         err = videobuf_dma_init_user(&mem->dma,
477                                                      DMA_FROM_DEVICE,
478                                                      vb->baddr, vb->bsize);
479                         if (0 != err)
480                                 return err;
481                 } else {
482                         /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP
483                         buffers can only be called from videobuf_qbuf
484                         we take current->mm->mmap_sem there, to prevent
485                         locking inversion, so don't take it here */
486
487                         err = videobuf_dma_init_user_locked(&mem->dma,
488                                                       DMA_FROM_DEVICE,
489                                                       vb->baddr, vb->bsize);
490                         if (0 != err)
491                                 return err;
492                 }
493                 break;
494         case V4L2_MEMORY_OVERLAY:
495                 if (NULL == fbuf)
496                         return -EINVAL;
497                 /* FIXME: need sanity checks for vb->boff */
498                 /*
499                  * Using a double cast to avoid compiler warnings when
500                  * building for PAE. Compiler doesn't like direct casting
501                  * of a 32 bit ptr to 64 bit integer.
502                  */
503                 bus   = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
504                 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
505                 err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE,
506                                                 bus, pages);
507                 if (0 != err)
508                         return err;
509                 break;
510         default:
511                 BUG();
512         }
513         err = videobuf_dma_map(q->dev, &mem->dma);
514         if (0 != err)
515                 return err;
516
517         return 0;
518 }
519
520 static int __videobuf_sync(struct videobuf_queue *q,
521                            struct videobuf_buffer *buf)
522 {
523         struct videobuf_dma_sg_memory *mem = buf->priv;
524         BUG_ON(!mem || !mem->dma.sglen);
525
526         MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
527         MAGIC_CHECK(mem->dma.magic, MAGIC_DMABUF);
528
529         dma_sync_sg_for_cpu(q->dev, mem->dma.sglist,
530                             mem->dma.sglen, mem->dma.direction);
531
532         return 0;
533 }
534
535 static int __videobuf_mmap_mapper(struct videobuf_queue *q,
536                                   struct videobuf_buffer *buf,
537                                   struct vm_area_struct *vma)
538 {
539         struct videobuf_dma_sg_memory *mem = buf->priv;
540         struct videobuf_mapping *map;
541         unsigned int first, last, size = 0, i;
542         int retval;
543
544         retval = -EINVAL;
545
546         BUG_ON(!mem);
547         MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
548
549         /* look for first buffer to map */
550         for (first = 0; first < VIDEO_MAX_FRAME; first++) {
551                 if (buf == q->bufs[first]) {
552                         size = PAGE_ALIGN(q->bufs[first]->bsize);
553                         break;
554                 }
555         }
556
557         /* paranoia, should never happen since buf is always valid. */
558         if (!size) {
559                 dprintk(1, "mmap app bug: offset invalid [offset=0x%lx]\n",
560                                 (vma->vm_pgoff << PAGE_SHIFT));
561                 goto done;
562         }
563
564         last = first;
565
566         /* create mapping + update buffer list */
567         retval = -ENOMEM;
568         map = kmalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
569         if (NULL == map)
570                 goto done;
571
572         size = 0;
573         for (i = first; i <= last; i++) {
574                 if (NULL == q->bufs[i])
575                         continue;
576                 q->bufs[i]->map   = map;
577                 q->bufs[i]->baddr = vma->vm_start + size;
578                 size += PAGE_ALIGN(q->bufs[i]->bsize);
579         }
580
581         map->count    = 1;
582         map->q        = q;
583         vma->vm_ops   = &videobuf_vm_ops;
584         vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
585         vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
586         vma->vm_private_data = map;
587         dprintk(1, "mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
588                 map, q, vma->vm_start, vma->vm_end, vma->vm_pgoff, first, last);
589         retval = 0;
590
591 done:
592         return retval;
593 }
594
595 static struct videobuf_qtype_ops sg_ops = {
596         .magic        = MAGIC_QTYPE_OPS,
597
598         .alloc_vb     = __videobuf_alloc_vb,
599         .iolock       = __videobuf_iolock,
600         .sync         = __videobuf_sync,
601         .mmap_mapper  = __videobuf_mmap_mapper,
602         .vaddr        = __videobuf_to_vaddr,
603 };
604
605 void *videobuf_sg_alloc(size_t size)
606 {
607         struct videobuf_queue q;
608
609         /* Required to make generic handler to call __videobuf_alloc */
610         q.int_ops = &sg_ops;
611
612         q.msize = size;
613
614         return videobuf_alloc_vb(&q);
615 }
616 EXPORT_SYMBOL_GPL(videobuf_sg_alloc);
617
618 void videobuf_queue_sg_init(struct videobuf_queue *q,
619                          const struct videobuf_queue_ops *ops,
620                          struct device *dev,
621                          spinlock_t *irqlock,
622                          enum v4l2_buf_type type,
623                          enum v4l2_field field,
624                          unsigned int msize,
625                          void *priv,
626                          struct mutex *ext_lock)
627 {
628         videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
629                                  priv, &sg_ops, ext_lock);
630 }
631 EXPORT_SYMBOL_GPL(videobuf_queue_sg_init);
632