Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1 | /* |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 2 | * helper functions for SG DMA video4linux capture buffers |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 3 | * |
Magnus Damm | 5d6aaf5 | 2008-07-16 21:27:49 -0300 | [diff] [blame] | 4 | * The functions expect the hardware being able to scatter gather |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 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> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 24 | #include <linux/sched.h> |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 25 | #include <linux/slab.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 28 | #include <linux/dma-mapping.h> |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 29 | #include <linux/vmalloc.h> |
| 30 | #include <linux/pagemap.h> |
Ralf Baechle | 1176360 | 2007-10-23 20:42:11 +0200 | [diff] [blame] | 31 | #include <linux/scatterlist.h> |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 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 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 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 | } |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 46 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 47 | static int debug; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 48 | module_param(debug, int, 0644); |
| 49 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 50 | MODULE_DESCRIPTION("helper module to manage video4linux dma sg buffers"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 51 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>"); |
| 52 | MODULE_LICENSE("GPL"); |
| 53 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 54 | #define dprintk(level, fmt, arg...) \ |
| 55 | if (debug >= level) \ |
| 56 | printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 57 | |
| 58 | /* --------------------------------------------------------------------- */ |
| 59 | |
Laurent Pinchart | 7181772 | 2010-05-11 10:36:32 -0300 | [diff] [blame] | 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) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 67 | { |
| 68 | struct scatterlist *sglist; |
| 69 | struct page *pg; |
| 70 | int i; |
| 71 | |
Joe Perches | 101b25b | 2010-11-05 00:07:39 -0300 | [diff] [blame] | 72 | sglist = vzalloc(nr_pages * sizeof(*sglist)); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 73 | if (NULL == sglist) |
| 74 | return NULL; |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 75 | sg_init_table(sglist, nr_pages); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 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)); |
Jens Axboe | 642f149 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 81 | sg_set_page(&sglist[i], pg, PAGE_SIZE, 0); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 82 | } |
| 83 | return sglist; |
| 84 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 85 | err: |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 86 | vfree(sglist); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 87 | return NULL; |
| 88 | } |
| 89 | |
Laurent Pinchart | 7181772 | 2010-05-11 10:36:32 -0300 | [diff] [blame] | 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, |
Hans Verkuil | 2fc1153 | 2010-09-07 06:10:45 -0300 | [diff] [blame] | 96 | int nr_pages, int offset, size_t size) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 97 | { |
| 98 | struct scatterlist *sglist; |
Christophe Jaillet | a47cacb | 2008-07-04 06:33:22 -0300 | [diff] [blame] | 99 | int i; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 100 | |
| 101 | if (NULL == pages[0]) |
| 102 | return NULL; |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 103 | sglist = vmalloc(nr_pages * sizeof(*sglist)); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 104 | if (NULL == sglist) |
| 105 | return NULL; |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 106 | sg_init_table(sglist, nr_pages); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 107 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 108 | if (PageHighMem(pages[0])) |
| 109 | /* DMA to highmem pages might not work */ |
| 110 | goto highmem; |
Newson Edouard | 45f239a | 2011-04-19 11:54:54 -0300 | [diff] [blame] | 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); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 114 | for (i = 1; i < nr_pages; i++) { |
| 115 | if (NULL == pages[i]) |
| 116 | goto nopage; |
| 117 | if (PageHighMem(pages[i])) |
| 118 | goto highmem; |
Mauro Carvalho Chehab | 1bede75 | 2010-10-07 09:31:33 -0300 | [diff] [blame] | 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); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 121 | } |
| 122 | return sglist; |
| 123 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 124 | nopage: |
| 125 | dprintk(2, "sgl: oops - no page\n"); |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 126 | vfree(sglist); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 127 | return NULL; |
| 128 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 129 | highmem: |
| 130 | dprintk(2, "sgl: oops - highmem page\n"); |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 131 | vfree(sglist); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 132 | return NULL; |
| 133 | } |
| 134 | |
| 135 | /* --------------------------------------------------------------------- */ |
| 136 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 137 | struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 138 | { |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 139 | struct videobuf_dma_sg_memory *mem = buf->priv; |
| 140 | BUG_ON(!mem); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 141 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 142 | MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 143 | |
| 144 | return &mem->dma; |
| 145 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 146 | EXPORT_SYMBOL_GPL(videobuf_to_dma); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 147 | |
| 148 | void videobuf_dma_init(struct videobuf_dmabuf *dma) |
| 149 | { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 150 | memset(dma, 0, sizeof(*dma)); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 151 | dma->magic = MAGIC_DMABUF; |
| 152 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 153 | EXPORT_SYMBOL_GPL(videobuf_dma_init); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 154 | |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 155 | static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma, |
| 156 | int direction, unsigned long data, unsigned long size) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 157 | { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 158 | unsigned long first, last; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 159 | int err, rw = 0; |
| 160 | |
| 161 | dma->direction = direction; |
| 162 | switch (dma->direction) { |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 163 | case DMA_FROM_DEVICE: |
| 164 | rw = READ; |
| 165 | break; |
| 166 | case DMA_TO_DEVICE: |
| 167 | rw = WRITE; |
| 168 | break; |
| 169 | default: |
| 170 | BUG(); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | first = (data & PAGE_MASK) >> PAGE_SHIFT; |
| 174 | last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT; |
Hans Verkuil | 2fc1153 | 2010-09-07 06:10:45 -0300 | [diff] [blame] | 175 | dma->offset = data & ~PAGE_MASK; |
| 176 | dma->size = size; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 177 | dma->nr_pages = last-first+1; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 178 | dma->pages = kmalloc(dma->nr_pages * sizeof(struct page *), GFP_KERNEL); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 179 | if (NULL == dma->pages) |
| 180 | return -ENOMEM; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 181 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 182 | dprintk(1, "init user [0x%lx+0x%lx => %d pages]\n", |
| 183 | data, size, dma->nr_pages); |
| 184 | |
| 185 | err = get_user_pages(current, current->mm, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 186 | data & PAGE_MASK, dma->nr_pages, |
| 187 | rw == READ, 1, /* force */ |
| 188 | dma->pages, NULL); |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 189 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 190 | if (err != dma->nr_pages) { |
| 191 | dma->nr_pages = (err >= 0) ? err : 0; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 192 | dprintk(1, "get_user_pages: err=%d [%d]\n", err, dma->nr_pages); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 193 | return err < 0 ? err : -EINVAL; |
| 194 | } |
| 195 | return 0; |
| 196 | } |
| 197 | |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 198 | int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction, |
| 199 | unsigned long data, unsigned long size) |
| 200 | { |
| 201 | int ret; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 202 | |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 203 | down_read(¤t->mm->mmap_sem); |
| 204 | ret = videobuf_dma_init_user_locked(dma, direction, data, size); |
| 205 | up_read(¤t->mm->mmap_sem); |
| 206 | |
| 207 | return ret; |
| 208 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 209 | EXPORT_SYMBOL_GPL(videobuf_dma_init_user); |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 210 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 211 | int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction, |
| 212 | int nr_pages) |
| 213 | { |
James Harper | 7b4eeed | 2014-06-12 06:53:38 -0300 | [diff] [blame] | 214 | int i; |
| 215 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 216 | dprintk(1, "init kernel [%d pages]\n", nr_pages); |
| 217 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 218 | dma->direction = direction; |
James Harper | 7b4eeed | 2014-06-12 06:53:38 -0300 | [diff] [blame] | 219 | dma->vaddr_pages = kcalloc(nr_pages, sizeof(*dma->vaddr_pages), |
| 220 | GFP_KERNEL); |
| 221 | if (!dma->vaddr_pages) |
| 222 | return -ENOMEM; |
| 223 | |
| 224 | dma->dma_addr = kcalloc(nr_pages, sizeof(*dma->dma_addr), GFP_KERNEL); |
| 225 | if (!dma->dma_addr) { |
| 226 | kfree(dma->vaddr_pages); |
| 227 | return -ENOMEM; |
| 228 | } |
| 229 | for (i = 0; i < nr_pages; i++) { |
| 230 | void *addr; |
| 231 | |
| 232 | addr = dma_alloc_coherent(dma->dev, PAGE_SIZE, |
| 233 | &(dma->dma_addr[i]), GFP_KERNEL); |
| 234 | if (addr == NULL) |
| 235 | goto out_free_pages; |
| 236 | |
| 237 | dma->vaddr_pages[i] = virt_to_page(addr); |
| 238 | } |
| 239 | dma->vaddr = vmap(dma->vaddr_pages, nr_pages, VM_MAP | VM_IOREMAP, |
| 240 | PAGE_KERNEL); |
Laurent Pinchart | bb6dbe7 | 2010-05-11 10:36:34 -0300 | [diff] [blame] | 241 | if (NULL == dma->vaddr) { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 242 | dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages); |
James Harper | 7b4eeed | 2014-06-12 06:53:38 -0300 | [diff] [blame] | 243 | goto out_free_pages; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 244 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 245 | |
| 246 | dprintk(1, "vmalloc is at addr 0x%08lx, size=%d\n", |
Laurent Pinchart | bb6dbe7 | 2010-05-11 10:36:34 -0300 | [diff] [blame] | 247 | (unsigned long)dma->vaddr, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 248 | nr_pages << PAGE_SHIFT); |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 249 | |
Laurent Pinchart | bb6dbe7 | 2010-05-11 10:36:34 -0300 | [diff] [blame] | 250 | memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 251 | dma->nr_pages = nr_pages; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 252 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 253 | return 0; |
James Harper | 7b4eeed | 2014-06-12 06:53:38 -0300 | [diff] [blame] | 254 | out_free_pages: |
| 255 | while (i > 0) { |
Dan Carpenter | 23d3090 | 2014-08-05 05:11:13 -0300 | [diff] [blame] | 256 | void *addr; |
| 257 | |
James Harper | 7b4eeed | 2014-06-12 06:53:38 -0300 | [diff] [blame] | 258 | i--; |
Dan Carpenter | 23d3090 | 2014-08-05 05:11:13 -0300 | [diff] [blame] | 259 | addr = page_address(dma->vaddr_pages[i]); |
| 260 | dma_free_coherent(dma->dev, PAGE_SIZE, addr, dma->dma_addr[i]); |
James Harper | 7b4eeed | 2014-06-12 06:53:38 -0300 | [diff] [blame] | 261 | } |
| 262 | kfree(dma->dma_addr); |
| 263 | dma->dma_addr = NULL; |
| 264 | kfree(dma->vaddr_pages); |
| 265 | dma->vaddr_pages = NULL; |
| 266 | |
| 267 | return -ENOMEM; |
| 268 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 269 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 270 | EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 271 | |
| 272 | int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction, |
| 273 | dma_addr_t addr, int nr_pages) |
| 274 | { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 275 | dprintk(1, "init overlay [%d pages @ bus 0x%lx]\n", |
| 276 | nr_pages, (unsigned long)addr); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 277 | dma->direction = direction; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 278 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 279 | if (0 == addr) |
| 280 | return -EINVAL; |
| 281 | |
| 282 | dma->bus_addr = addr; |
| 283 | dma->nr_pages = nr_pages; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 284 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 285 | return 0; |
| 286 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 287 | EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 288 | |
Laurent Pinchart | 9526840 | 2010-05-11 10:36:30 -0300 | [diff] [blame] | 289 | int videobuf_dma_map(struct device *dev, struct videobuf_dmabuf *dma) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 290 | { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 291 | MAGIC_CHECK(dma->magic, MAGIC_DMABUF); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 292 | BUG_ON(0 == dma->nr_pages); |
| 293 | |
| 294 | if (dma->pages) { |
| 295 | dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages, |
Hans Verkuil | 2fc1153 | 2010-09-07 06:10:45 -0300 | [diff] [blame] | 296 | dma->offset, dma->size); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 297 | } |
Laurent Pinchart | bb6dbe7 | 2010-05-11 10:36:34 -0300 | [diff] [blame] | 298 | if (dma->vaddr) { |
| 299 | dma->sglist = videobuf_vmalloc_to_sg(dma->vaddr, |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 300 | dma->nr_pages); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 301 | } |
| 302 | if (dma->bus_addr) { |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 303 | dma->sglist = vmalloc(sizeof(*dma->sglist)); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 304 | if (NULL != dma->sglist) { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 305 | dma->sglen = 1; |
| 306 | sg_dma_address(&dma->sglist[0]) = dma->bus_addr |
| 307 | & PAGE_MASK; |
| 308 | dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK; |
| 309 | sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | if (NULL == dma->sglist) { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 313 | dprintk(1, "scatterlist is NULL\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 314 | return -ENOMEM; |
| 315 | } |
| 316 | if (!dma->bus_addr) { |
Laurent Pinchart | 9526840 | 2010-05-11 10:36:30 -0300 | [diff] [blame] | 317 | dma->sglen = dma_map_sg(dev, dma->sglist, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 318 | dma->nr_pages, dma->direction); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 319 | if (0 == dma->sglen) { |
| 320 | printk(KERN_WARNING |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 321 | "%s: videobuf_map_sg failed\n", __func__); |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 322 | vfree(dma->sglist); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 323 | dma->sglist = NULL; |
| 324 | dma->sglen = 0; |
Figo.zhang | 92051b2 | 2009-06-10 23:17:27 -0300 | [diff] [blame] | 325 | return -ENOMEM; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 326 | } |
| 327 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 328 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 329 | return 0; |
| 330 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 331 | EXPORT_SYMBOL_GPL(videobuf_dma_map); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 332 | |
Laurent Pinchart | 9526840 | 2010-05-11 10:36:30 -0300 | [diff] [blame] | 333 | int videobuf_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 334 | { |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 335 | MAGIC_CHECK(dma->magic, MAGIC_DMABUF); |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 336 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 337 | if (!dma->sglen) |
| 338 | return 0; |
| 339 | |
Laurent Pinchart | 9526840 | 2010-05-11 10:36:30 -0300 | [diff] [blame] | 340 | dma_unmap_sg(dev, dma->sglist, dma->sglen, dma->direction); |
Mauro Carvalho Chehab | 5ddff43 | 2007-10-08 11:43:49 -0300 | [diff] [blame] | 341 | |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 342 | vfree(dma->sglist); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 343 | dma->sglist = NULL; |
| 344 | dma->sglen = 0; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 345 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 346 | return 0; |
| 347 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 348 | EXPORT_SYMBOL_GPL(videobuf_dma_unmap); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 349 | |
| 350 | int videobuf_dma_free(struct videobuf_dmabuf *dma) |
| 351 | { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 352 | int i; |
| 353 | MAGIC_CHECK(dma->magic, MAGIC_DMABUF); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 354 | BUG_ON(dma->sglen); |
| 355 | |
| 356 | if (dma->pages) { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 357 | for (i = 0; i < dma->nr_pages; i++) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 358 | page_cache_release(dma->pages[i]); |
| 359 | kfree(dma->pages); |
| 360 | dma->pages = NULL; |
| 361 | } |
| 362 | |
James Harper | 7b4eeed | 2014-06-12 06:53:38 -0300 | [diff] [blame] | 363 | if (dma->dma_addr) { |
| 364 | for (i = 0; i < dma->nr_pages; i++) { |
| 365 | void *addr; |
| 366 | |
| 367 | addr = page_address(dma->vaddr_pages[i]); |
| 368 | dma_free_coherent(dma->dev, PAGE_SIZE, addr, |
| 369 | dma->dma_addr[i]); |
| 370 | } |
| 371 | kfree(dma->dma_addr); |
| 372 | dma->dma_addr = NULL; |
| 373 | kfree(dma->vaddr_pages); |
| 374 | dma->vaddr_pages = NULL; |
| 375 | vunmap(dma->vaddr); |
| 376 | dma->vaddr = NULL; |
| 377 | } |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 378 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 379 | if (dma->bus_addr) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 380 | dma->bus_addr = 0; |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 381 | dma->direction = DMA_NONE; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 382 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 383 | return 0; |
| 384 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 385 | EXPORT_SYMBOL_GPL(videobuf_dma_free); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 386 | |
| 387 | /* --------------------------------------------------------------------- */ |
| 388 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 389 | static void videobuf_vm_open(struct vm_area_struct *vma) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 390 | { |
| 391 | struct videobuf_mapping *map = vma->vm_private_data; |
| 392 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 393 | dprintk(2, "vm_open %p [count=%d,vma=%08lx-%08lx]\n", map, |
| 394 | map->count, vma->vm_start, vma->vm_end); |
| 395 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 396 | map->count++; |
| 397 | } |
| 398 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 399 | static void videobuf_vm_close(struct vm_area_struct *vma) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 400 | { |
| 401 | struct videobuf_mapping *map = vma->vm_private_data; |
| 402 | struct videobuf_queue *q = map->q; |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 403 | struct videobuf_dma_sg_memory *mem; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 404 | int i; |
| 405 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 406 | dprintk(2, "vm_close %p [count=%d,vma=%08lx-%08lx]\n", map, |
| 407 | map->count, vma->vm_start, vma->vm_end); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 408 | |
Hans Verkuil | cca36e2 | 2014-01-03 08:10:49 -0300 | [diff] [blame] | 409 | map->count--; |
| 410 | if (0 == map->count) { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 411 | dprintk(1, "munmap %p q=%p\n", map, q); |
Hans Verkuil | cca36e2 | 2014-01-03 08:10:49 -0300 | [diff] [blame] | 412 | videobuf_queue_lock(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 413 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
| 414 | if (NULL == q->bufs[i]) |
| 415 | continue; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 416 | mem = q->bufs[i]->priv; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 417 | if (!mem) |
| 418 | continue; |
| 419 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 420 | MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 421 | |
Mauro Carvalho Chehab | 851c0c9 | 2007-09-27 18:25:44 -0300 | [diff] [blame] | 422 | if (q->bufs[i]->map != map) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 423 | continue; |
Mauro Carvalho Chehab | 851c0c9 | 2007-09-27 18:25:44 -0300 | [diff] [blame] | 424 | q->bufs[i]->map = NULL; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 425 | q->bufs[i]->baddr = 0; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 426 | q->ops->buf_release(q, q->bufs[i]); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 427 | } |
Hans Verkuil | cca36e2 | 2014-01-03 08:10:49 -0300 | [diff] [blame] | 428 | videobuf_queue_unlock(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 429 | kfree(map); |
| 430 | } |
| 431 | return; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Get a anonymous page for the mapping. Make sure we can DMA to that |
| 436 | * memory location with 32bit PCI devices (i.e. don't use highmem for |
| 437 | * now ...). Bounce buffers don't work very well for the data rates |
| 438 | * video capture has. |
| 439 | */ |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 440 | static int videobuf_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 441 | { |
| 442 | struct page *page; |
| 443 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 444 | dprintk(3, "fault: fault @ %08lx [vma %08lx-%08lx]\n", |
| 445 | (unsigned long)vmf->virtual_address, |
| 446 | vma->vm_start, vma->vm_end); |
| 447 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 448 | page = alloc_page(GFP_USER | __GFP_DMA32); |
| 449 | if (!page) |
Nick Piggin | 105354a | 2007-12-07 17:57:38 -0300 | [diff] [blame] | 450 | return VM_FAULT_OOM; |
Guennadi Liakhovetski | c0cd501 | 2009-01-03 18:20:04 -0300 | [diff] [blame] | 451 | clear_user_highpage(page, (unsigned long)vmf->virtual_address); |
Nick Piggin | 105354a | 2007-12-07 17:57:38 -0300 | [diff] [blame] | 452 | vmf->page = page; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 453 | |
Nick Piggin | 105354a | 2007-12-07 17:57:38 -0300 | [diff] [blame] | 454 | return 0; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 455 | } |
| 456 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 457 | static const struct vm_operations_struct videobuf_vm_ops = { |
| 458 | .open = videobuf_vm_open, |
| 459 | .close = videobuf_vm_close, |
| 460 | .fault = videobuf_vm_fault, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 461 | }; |
| 462 | |
| 463 | /* --------------------------------------------------------------------- |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 464 | * SG handlers for the generic methods |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 465 | */ |
| 466 | |
| 467 | /* Allocated area consists on 3 parts: |
| 468 | struct video_buffer |
| 469 | struct <driver>_buffer (cx88_buffer, saa7134_buf, ...) |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 470 | struct videobuf_dma_sg_memory |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 471 | */ |
| 472 | |
Pawel Osciak | 33c3828 | 2010-05-11 10:36:28 -0300 | [diff] [blame] | 473 | static struct videobuf_buffer *__videobuf_alloc_vb(size_t size) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 474 | { |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 475 | struct videobuf_dma_sg_memory *mem; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 476 | struct videobuf_buffer *vb; |
| 477 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 478 | vb = kzalloc(size + sizeof(*mem), GFP_KERNEL); |
Pawel Osciak | bee527f | 2010-02-22 13:10:06 -0300 | [diff] [blame] | 479 | if (!vb) |
| 480 | return vb; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 481 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 482 | mem = vb->priv = ((char *)vb) + size; |
| 483 | mem->magic = MAGIC_SG_MEM; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 484 | |
| 485 | videobuf_dma_init(&mem->dma); |
| 486 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 487 | dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n", |
| 488 | __func__, vb, (long)sizeof(*vb), (long)size - sizeof(*vb), |
| 489 | mem, (long)sizeof(*mem)); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 490 | |
| 491 | return vb; |
| 492 | } |
| 493 | |
Hans Verkuil | 037c75e | 2010-03-28 08:18:37 -0300 | [diff] [blame] | 494 | static void *__videobuf_to_vaddr(struct videobuf_buffer *buf) |
Mauro Carvalho Chehab | 59d3448 | 2008-04-13 15:10:00 -0300 | [diff] [blame] | 495 | { |
| 496 | struct videobuf_dma_sg_memory *mem = buf->priv; |
| 497 | BUG_ON(!mem); |
| 498 | |
| 499 | MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); |
| 500 | |
Laurent Pinchart | bb6dbe7 | 2010-05-11 10:36:34 -0300 | [diff] [blame] | 501 | return mem->dma.vaddr; |
Mauro Carvalho Chehab | 59d3448 | 2008-04-13 15:10:00 -0300 | [diff] [blame] | 502 | } |
| 503 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 504 | static int __videobuf_iolock(struct videobuf_queue *q, |
| 505 | struct videobuf_buffer *vb, |
| 506 | struct v4l2_framebuffer *fbuf) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 507 | { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 508 | int err, pages; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 509 | dma_addr_t bus; |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 510 | struct videobuf_dma_sg_memory *mem = vb->priv; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 511 | BUG_ON(!mem); |
| 512 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 513 | MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 514 | |
James Harper | 7b4eeed | 2014-06-12 06:53:38 -0300 | [diff] [blame] | 515 | if (!mem->dma.dev) |
| 516 | mem->dma.dev = q->dev; |
| 517 | else |
| 518 | WARN_ON(mem->dma.dev != q->dev); |
| 519 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 520 | switch (vb->memory) { |
| 521 | case V4L2_MEMORY_MMAP: |
| 522 | case V4L2_MEMORY_USERPTR: |
| 523 | if (0 == vb->baddr) { |
| 524 | /* no userspace addr -- kernel bounce buffer */ |
| 525 | pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 526 | err = videobuf_dma_init_kernel(&mem->dma, |
| 527 | DMA_FROM_DEVICE, |
| 528 | pages); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 529 | if (0 != err) |
| 530 | return err; |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 531 | } else if (vb->memory == V4L2_MEMORY_USERPTR) { |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 532 | /* dma directly to userspace */ |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 533 | err = videobuf_dma_init_user(&mem->dma, |
| 534 | DMA_FROM_DEVICE, |
| 535 | vb->baddr, vb->bsize); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 536 | if (0 != err) |
| 537 | return err; |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 538 | } else { |
| 539 | /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP |
| 540 | buffers can only be called from videobuf_qbuf |
| 541 | we take current->mm->mmap_sem there, to prevent |
| 542 | locking inversion, so don't take it here */ |
| 543 | |
| 544 | err = videobuf_dma_init_user_locked(&mem->dma, |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 545 | DMA_FROM_DEVICE, |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 546 | vb->baddr, vb->bsize); |
| 547 | if (0 != err) |
| 548 | return err; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 549 | } |
| 550 | break; |
| 551 | case V4L2_MEMORY_OVERLAY: |
| 552 | if (NULL == fbuf) |
| 553 | return -EINVAL; |
| 554 | /* FIXME: need sanity checks for vb->boff */ |
| 555 | /* |
| 556 | * Using a double cast to avoid compiler warnings when |
| 557 | * building for PAE. Compiler doesn't like direct casting |
| 558 | * of a 32 bit ptr to 64 bit integer. |
| 559 | */ |
| 560 | bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff; |
| 561 | pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT; |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 562 | err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 563 | bus, pages); |
| 564 | if (0 != err) |
| 565 | return err; |
| 566 | break; |
| 567 | default: |
| 568 | BUG(); |
| 569 | } |
Laurent Pinchart | 9526840 | 2010-05-11 10:36:30 -0300 | [diff] [blame] | 570 | err = videobuf_dma_map(q->dev, &mem->dma); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 571 | if (0 != err) |
| 572 | return err; |
| 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | static int __videobuf_sync(struct videobuf_queue *q, |
| 578 | struct videobuf_buffer *buf) |
| 579 | { |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 580 | struct videobuf_dma_sg_memory *mem = buf->priv; |
Mauro Carvalho Chehab | 97f8105 | 2010-05-05 16:23:09 -0300 | [diff] [blame] | 581 | BUG_ON(!mem || !mem->dma.sglen); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 582 | |
Mauro Carvalho Chehab | 97f8105 | 2010-05-05 16:23:09 -0300 | [diff] [blame] | 583 | MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); |
| 584 | MAGIC_CHECK(mem->dma.magic, MAGIC_DMABUF); |
| 585 | |
| 586 | dma_sync_sg_for_cpu(q->dev, mem->dma.sglist, |
Arnout Vandecappelle | fc7f8fd | 2010-03-17 19:53:04 -0300 | [diff] [blame] | 587 | mem->dma.sglen, mem->dma.direction); |
Mauro Carvalho Chehab | 97f8105 | 2010-05-05 16:23:09 -0300 | [diff] [blame] | 588 | |
| 589 | return 0; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 590 | } |
| 591 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 592 | static int __videobuf_mmap_mapper(struct videobuf_queue *q, |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 593 | struct videobuf_buffer *buf, |
| 594 | struct vm_area_struct *vma) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 595 | { |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 596 | struct videobuf_dma_sg_memory *mem = buf->priv; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 597 | struct videobuf_mapping *map; |
Mauro Carvalho Chehab | 474675a | 2010-04-25 11:23:52 -0300 | [diff] [blame] | 598 | unsigned int first, last, size = 0, i; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 599 | int retval; |
| 600 | |
| 601 | retval = -EINVAL; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 602 | |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 603 | BUG_ON(!mem); |
| 604 | MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); |
| 605 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 606 | /* look for first buffer to map */ |
| 607 | for (first = 0; first < VIDEO_MAX_FRAME; first++) { |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 608 | if (buf == q->bufs[first]) { |
| 609 | size = PAGE_ALIGN(q->bufs[first]->bsize); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 610 | break; |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 611 | } |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 612 | } |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 613 | |
| 614 | /* paranoia, should never happen since buf is always valid. */ |
Mauro Carvalho Chehab | 474675a | 2010-04-25 11:23:52 -0300 | [diff] [blame] | 615 | if (!size) { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 616 | dprintk(1, "mmap app bug: offset invalid [offset=0x%lx]\n", |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 617 | (vma->vm_pgoff << PAGE_SHIFT)); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 618 | goto done; |
| 619 | } |
| 620 | |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 621 | last = first; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 622 | |
| 623 | /* create mapping + update buffer list */ |
| 624 | retval = -ENOMEM; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 625 | map = kmalloc(sizeof(struct videobuf_mapping), GFP_KERNEL); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 626 | if (NULL == map) |
| 627 | goto done; |
Brandon Philips | ce54093 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 628 | |
| 629 | size = 0; |
| 630 | for (i = first; i <= last; i++) { |
| 631 | if (NULL == q->bufs[i]) |
| 632 | continue; |
Mauro Carvalho Chehab | 851c0c9 | 2007-09-27 18:25:44 -0300 | [diff] [blame] | 633 | q->bufs[i]->map = map; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 634 | q->bufs[i]->baddr = vma->vm_start + size; |
Tuukka Toivonen | 7cbefad | 2009-07-23 10:56:25 -0300 | [diff] [blame] | 635 | size += PAGE_ALIGN(q->bufs[i]->bsize); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 636 | } |
Brandon Philips | ce54093 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 637 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 638 | map->count = 1; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 639 | map->q = q; |
| 640 | vma->vm_ops = &videobuf_vm_ops; |
Konstantin Khlebnikov | 314e51b | 2012-10-08 16:29:02 -0700 | [diff] [blame] | 641 | vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 642 | vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */ |
| 643 | vma->vm_private_data = map; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 644 | dprintk(1, "mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n", |
| 645 | map, q, vma->vm_start, vma->vm_end, vma->vm_pgoff, first, last); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 646 | retval = 0; |
| 647 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 648 | done: |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 649 | return retval; |
| 650 | } |
| 651 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 652 | static struct videobuf_qtype_ops sg_ops = { |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 653 | .magic = MAGIC_QTYPE_OPS, |
| 654 | |
Pawel Osciak | 33c3828 | 2010-05-11 10:36:28 -0300 | [diff] [blame] | 655 | .alloc_vb = __videobuf_alloc_vb, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 656 | .iolock = __videobuf_iolock, |
| 657 | .sync = __videobuf_sync, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 658 | .mmap_mapper = __videobuf_mmap_mapper, |
Hans Verkuil | 037c75e | 2010-03-28 08:18:37 -0300 | [diff] [blame] | 659 | .vaddr = __videobuf_to_vaddr, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 660 | }; |
| 661 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 662 | void *videobuf_sg_alloc(size_t size) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 663 | { |
| 664 | struct videobuf_queue q; |
| 665 | |
| 666 | /* Required to make generic handler to call __videobuf_alloc */ |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 667 | q.int_ops = &sg_ops; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 668 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 669 | q.msize = size; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 670 | |
Pawel Osciak | 33c3828 | 2010-05-11 10:36:28 -0300 | [diff] [blame] | 671 | return videobuf_alloc_vb(&q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 672 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 673 | EXPORT_SYMBOL_GPL(videobuf_sg_alloc); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 674 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 675 | void videobuf_queue_sg_init(struct videobuf_queue *q, |
Jonathan Corbet | 38a54f3 | 2009-11-17 19:43:41 -0300 | [diff] [blame] | 676 | const struct videobuf_queue_ops *ops, |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 677 | struct device *dev, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 678 | spinlock_t *irqlock, |
| 679 | enum v4l2_buf_type type, |
| 680 | enum v4l2_field field, |
| 681 | unsigned int msize, |
Hans Verkuil | 08bff03 | 2010-09-20 17:39:46 -0300 | [diff] [blame] | 682 | void *priv, |
| 683 | struct mutex *ext_lock) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 684 | { |
Mauro Carvalho Chehab | d4cae5a | 2007-10-08 12:20:02 -0300 | [diff] [blame] | 685 | videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize, |
Hans Verkuil | 08bff03 | 2010-09-20 17:39:46 -0300 | [diff] [blame] | 686 | priv, &sg_ops, ext_lock); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 687 | } |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 688 | EXPORT_SYMBOL_GPL(videobuf_queue_sg_init); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 689 | |