blob: 9db674ccdc68c11b2751d290f786a6cf56d6a6d9 [file] [log] [blame]
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001/*
Guennadi Liakhovetski07051352008-04-22 14:42:13 -03002 * helper functions for SG DMA video4linux capture buffers
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03003 *
Magnus Damm5d6aaf52008-07-16 21:27:49 -03004 * The functions expect the hardware being able to scatter gather
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03005 * (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 Dobriyand43c36d2009-10-07 17:09:06 +040024#include <linux/sched.h>
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030025#include <linux/slab.h>
26#include <linux/interrupt.h>
27
Guennadi Liakhovetski07051352008-04-22 14:42:13 -030028#include <linux/dma-mapping.h>
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030029#include <linux/vmalloc.h>
30#include <linux/pagemap.h>
Ralf Baechle11763602007-10-23 20:42:11 +020031#include <linux/scatterlist.h>
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030032#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 Osciak7a022642010-03-17 04:01:04 -030040#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 Chehab7a7d9a82007-08-23 16:26:14 -030046
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030047static int debug;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030048module_param(debug, int, 0644);
49
Guennadi Liakhovetski07051352008-04-22 14:42:13 -030050MODULE_DESCRIPTION("helper module to manage video4linux dma sg buffers");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030051MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
52MODULE_LICENSE("GPL");
53
Pawel Osciak7a022642010-03-17 04:01:04 -030054#define dprintk(level, fmt, arg...) \
55 if (debug >= level) \
56 printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030057
58/* --------------------------------------------------------------------- */
59
Laurent Pinchart71817722010-05-11 10:36:32 -030060/*
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 */
65static struct scatterlist *videobuf_vmalloc_to_sg(unsigned char *virt,
66 int nr_pages)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030067{
68 struct scatterlist *sglist;
69 struct page *pg;
70 int i;
71
Joe Perches101b25b2010-11-05 00:07:39 -030072 sglist = vzalloc(nr_pages * sizeof(*sglist));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030073 if (NULL == sglist)
74 return NULL;
Jens Axboe45711f12007-10-22 21:19:53 +020075 sg_init_table(sglist, nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030076 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 Axboe642f1492007-10-24 11:20:47 +020081 sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030082 }
83 return sglist;
84
Pawel Osciak7a022642010-03-17 04:01:04 -030085err:
Cohen David.A1010ed12009-05-11 11:00:20 -030086 vfree(sglist);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030087 return NULL;
88}
89
Laurent Pinchart71817722010-05-11 10:36:32 -030090/*
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 */
95static struct scatterlist *videobuf_pages_to_sg(struct page **pages,
Hans Verkuil2fc11532010-09-07 06:10:45 -030096 int nr_pages, int offset, size_t size)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030097{
98 struct scatterlist *sglist;
Christophe Jailleta47cacb2008-07-04 06:33:22 -030099 int i;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300100
101 if (NULL == pages[0])
102 return NULL;
Cohen David.A1010ed12009-05-11 11:00:20 -0300103 sglist = vmalloc(nr_pages * sizeof(*sglist));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300104 if (NULL == sglist)
105 return NULL;
Jens Axboe45711f12007-10-22 21:19:53 +0200106 sg_init_table(sglist, nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300107
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300108 if (PageHighMem(pages[0]))
109 /* DMA to highmem pages might not work */
110 goto highmem;
Newson Edouard45f239a2011-04-19 11:54:54 -0300111 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 Chehab7a7d9a82007-08-23 16:26:14 -0300114 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 Chehab1bede752010-10-07 09:31:33 -0300119 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 Chehab7a7d9a82007-08-23 16:26:14 -0300121 }
122 return sglist;
123
Pawel Osciak7a022642010-03-17 04:01:04 -0300124nopage:
125 dprintk(2, "sgl: oops - no page\n");
Cohen David.A1010ed12009-05-11 11:00:20 -0300126 vfree(sglist);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300127 return NULL;
128
Pawel Osciak7a022642010-03-17 04:01:04 -0300129highmem:
130 dprintk(2, "sgl: oops - highmem page\n");
Cohen David.A1010ed12009-05-11 11:00:20 -0300131 vfree(sglist);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300132 return NULL;
133}
134
135/* --------------------------------------------------------------------- */
136
Pawel Osciak7a022642010-03-17 04:01:04 -0300137struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300138{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300139 struct videobuf_dma_sg_memory *mem = buf->priv;
140 BUG_ON(!mem);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300141
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300142 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300143
144 return &mem->dma;
145}
Pawel Osciak7a022642010-03-17 04:01:04 -0300146EXPORT_SYMBOL_GPL(videobuf_to_dma);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300147
148void videobuf_dma_init(struct videobuf_dmabuf *dma)
149{
Pawel Osciak7a022642010-03-17 04:01:04 -0300150 memset(dma, 0, sizeof(*dma));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300151 dma->magic = MAGIC_DMABUF;
152}
Pawel Osciak7a022642010-03-17 04:01:04 -0300153EXPORT_SYMBOL_GPL(videobuf_dma_init);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300154
Maxim Levitsky99001322007-09-27 20:34:09 -0300155static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma,
156 int direction, unsigned long data, unsigned long size)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300157{
Pawel Osciak7a022642010-03-17 04:01:04 -0300158 unsigned long first, last;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300159 int err, rw = 0;
160
161 dma->direction = direction;
162 switch (dma->direction) {
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300163 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 Chehab7a7d9a82007-08-23 16:26:14 -0300171 }
172
173 first = (data & PAGE_MASK) >> PAGE_SHIFT;
174 last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
Hans Verkuil2fc11532010-09-07 06:10:45 -0300175 dma->offset = data & ~PAGE_MASK;
176 dma->size = size;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300177 dma->nr_pages = last-first+1;
Pawel Osciak7a022642010-03-17 04:01:04 -0300178 dma->pages = kmalloc(dma->nr_pages * sizeof(struct page *), GFP_KERNEL);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300179 if (NULL == dma->pages)
180 return -ENOMEM;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300181
Pawel Osciak7a022642010-03-17 04:01:04 -0300182 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 Chehab7a7d9a82007-08-23 16:26:14 -0300186 data & PAGE_MASK, dma->nr_pages,
187 rw == READ, 1, /* force */
188 dma->pages, NULL);
Maxim Levitsky99001322007-09-27 20:34:09 -0300189
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300190 if (err != dma->nr_pages) {
191 dma->nr_pages = (err >= 0) ? err : 0;
Pawel Osciak7a022642010-03-17 04:01:04 -0300192 dprintk(1, "get_user_pages: err=%d [%d]\n", err, dma->nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300193 return err < 0 ? err : -EINVAL;
194 }
195 return 0;
196}
197
Maxim Levitsky99001322007-09-27 20:34:09 -0300198int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
199 unsigned long data, unsigned long size)
200{
201 int ret;
Pawel Osciak7a022642010-03-17 04:01:04 -0300202
Maxim Levitsky99001322007-09-27 20:34:09 -0300203 down_read(&current->mm->mmap_sem);
204 ret = videobuf_dma_init_user_locked(dma, direction, data, size);
205 up_read(&current->mm->mmap_sem);
206
207 return ret;
208}
Pawel Osciak7a022642010-03-17 04:01:04 -0300209EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
Maxim Levitsky99001322007-09-27 20:34:09 -0300210
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300211int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
212 int nr_pages)
213{
Pawel Osciak7a022642010-03-17 04:01:04 -0300214 dprintk(1, "init kernel [%d pages]\n", nr_pages);
215
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300216 dma->direction = direction;
Laurent Pinchartbb6dbe72010-05-11 10:36:34 -0300217 dma->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
218 if (NULL == dma->vaddr) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300219 dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300220 return -ENOMEM;
221 }
Pawel Osciak7a022642010-03-17 04:01:04 -0300222
223 dprintk(1, "vmalloc is at addr 0x%08lx, size=%d\n",
Laurent Pinchartbb6dbe72010-05-11 10:36:34 -0300224 (unsigned long)dma->vaddr,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300225 nr_pages << PAGE_SHIFT);
Pawel Osciak7a022642010-03-17 04:01:04 -0300226
Laurent Pinchartbb6dbe72010-05-11 10:36:34 -0300227 memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300228 dma->nr_pages = nr_pages;
Pawel Osciak7a022642010-03-17 04:01:04 -0300229
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300230 return 0;
231}
Pawel Osciak7a022642010-03-17 04:01:04 -0300232EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300233
234int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
235 dma_addr_t addr, int nr_pages)
236{
Pawel Osciak7a022642010-03-17 04:01:04 -0300237 dprintk(1, "init overlay [%d pages @ bus 0x%lx]\n",
238 nr_pages, (unsigned long)addr);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300239 dma->direction = direction;
Pawel Osciak7a022642010-03-17 04:01:04 -0300240
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300241 if (0 == addr)
242 return -EINVAL;
243
244 dma->bus_addr = addr;
245 dma->nr_pages = nr_pages;
Pawel Osciak7a022642010-03-17 04:01:04 -0300246
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300247 return 0;
248}
Pawel Osciak7a022642010-03-17 04:01:04 -0300249EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300250
Laurent Pinchart95268402010-05-11 10:36:30 -0300251int videobuf_dma_map(struct device *dev, struct videobuf_dmabuf *dma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300252{
Pawel Osciak7a022642010-03-17 04:01:04 -0300253 MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300254 BUG_ON(0 == dma->nr_pages);
255
256 if (dma->pages) {
257 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
Hans Verkuil2fc11532010-09-07 06:10:45 -0300258 dma->offset, dma->size);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300259 }
Laurent Pinchartbb6dbe72010-05-11 10:36:34 -0300260 if (dma->vaddr) {
261 dma->sglist = videobuf_vmalloc_to_sg(dma->vaddr,
Pawel Osciak7a022642010-03-17 04:01:04 -0300262 dma->nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300263 }
264 if (dma->bus_addr) {
Cohen David.A1010ed12009-05-11 11:00:20 -0300265 dma->sglist = vmalloc(sizeof(*dma->sglist));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300266 if (NULL != dma->sglist) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300267 dma->sglen = 1;
268 sg_dma_address(&dma->sglist[0]) = dma->bus_addr
269 & PAGE_MASK;
270 dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
271 sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300272 }
273 }
274 if (NULL == dma->sglist) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300275 dprintk(1, "scatterlist is NULL\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300276 return -ENOMEM;
277 }
278 if (!dma->bus_addr) {
Laurent Pinchart95268402010-05-11 10:36:30 -0300279 dma->sglen = dma_map_sg(dev, dma->sglist,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300280 dma->nr_pages, dma->direction);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300281 if (0 == dma->sglen) {
282 printk(KERN_WARNING
Pawel Osciak7a022642010-03-17 04:01:04 -0300283 "%s: videobuf_map_sg failed\n", __func__);
Cohen David.A1010ed12009-05-11 11:00:20 -0300284 vfree(dma->sglist);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300285 dma->sglist = NULL;
286 dma->sglen = 0;
Figo.zhang92051b22009-06-10 23:17:27 -0300287 return -ENOMEM;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300288 }
289 }
Pawel Osciak7a022642010-03-17 04:01:04 -0300290
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300291 return 0;
292}
Pawel Osciak7a022642010-03-17 04:01:04 -0300293EXPORT_SYMBOL_GPL(videobuf_dma_map);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300294
Laurent Pinchart95268402010-05-11 10:36:30 -0300295int videobuf_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300296{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300297 MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
Pawel Osciak7a022642010-03-17 04:01:04 -0300298
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300299 if (!dma->sglen)
300 return 0;
301
Laurent Pinchart95268402010-05-11 10:36:30 -0300302 dma_unmap_sg(dev, dma->sglist, dma->sglen, dma->direction);
Mauro Carvalho Chehab5ddff432007-10-08 11:43:49 -0300303
Cohen David.A1010ed12009-05-11 11:00:20 -0300304 vfree(dma->sglist);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300305 dma->sglist = NULL;
306 dma->sglen = 0;
Pawel Osciak7a022642010-03-17 04:01:04 -0300307
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300308 return 0;
309}
Pawel Osciak7a022642010-03-17 04:01:04 -0300310EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300311
312int videobuf_dma_free(struct videobuf_dmabuf *dma)
313{
Pawel Osciak7a022642010-03-17 04:01:04 -0300314 int i;
315 MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300316 BUG_ON(dma->sglen);
317
318 if (dma->pages) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300319 for (i = 0; i < dma->nr_pages; i++)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300320 page_cache_release(dma->pages[i]);
321 kfree(dma->pages);
322 dma->pages = NULL;
323 }
324
Laurent Pinchartbb6dbe72010-05-11 10:36:34 -0300325 vfree(dma->vaddr);
326 dma->vaddr = NULL;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300327
Pawel Osciak7a022642010-03-17 04:01:04 -0300328 if (dma->bus_addr)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300329 dma->bus_addr = 0;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300330 dma->direction = DMA_NONE;
Pawel Osciak7a022642010-03-17 04:01:04 -0300331
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300332 return 0;
333}
Pawel Osciak7a022642010-03-17 04:01:04 -0300334EXPORT_SYMBOL_GPL(videobuf_dma_free);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300335
336/* --------------------------------------------------------------------- */
337
Pawel Osciak7a022642010-03-17 04:01:04 -0300338static void videobuf_vm_open(struct vm_area_struct *vma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300339{
340 struct videobuf_mapping *map = vma->vm_private_data;
Al Viroa242f422013-05-09 15:03:33 -0300341 struct videobuf_queue *q = map->q;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300342
Pawel Osciak7a022642010-03-17 04:01:04 -0300343 dprintk(2, "vm_open %p [count=%d,vma=%08lx-%08lx]\n", map,
344 map->count, vma->vm_start, vma->vm_end);
345
Al Viroa242f422013-05-09 15:03:33 -0300346 videobuf_queue_lock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300347 map->count++;
Al Viroa242f422013-05-09 15:03:33 -0300348 videobuf_queue_unlock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300349}
350
Pawel Osciak7a022642010-03-17 04:01:04 -0300351static void videobuf_vm_close(struct vm_area_struct *vma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300352{
353 struct videobuf_mapping *map = vma->vm_private_data;
354 struct videobuf_queue *q = map->q;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300355 struct videobuf_dma_sg_memory *mem;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300356 int i;
357
Pawel Osciak7a022642010-03-17 04:01:04 -0300358 dprintk(2, "vm_close %p [count=%d,vma=%08lx-%08lx]\n", map,
359 map->count, vma->vm_start, vma->vm_end);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300360
Al Viroa242f422013-05-09 15:03:33 -0300361 videobuf_queue_lock(q);
362 if (!--map->count) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300363 dprintk(1, "munmap %p q=%p\n", map, q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300364 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
365 if (NULL == q->bufs[i])
366 continue;
Pawel Osciak7a022642010-03-17 04:01:04 -0300367 mem = q->bufs[i]->priv;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300368 if (!mem)
369 continue;
370
Pawel Osciak7a022642010-03-17 04:01:04 -0300371 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300372
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300373 if (q->bufs[i]->map != map)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300374 continue;
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300375 q->bufs[i]->map = NULL;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300376 q->bufs[i]->baddr = 0;
Pawel Osciak7a022642010-03-17 04:01:04 -0300377 q->ops->buf_release(q, q->bufs[i]);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300378 }
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300379 kfree(map);
380 }
Al Viroa242f422013-05-09 15:03:33 -0300381 videobuf_queue_unlock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300382 return;
383}
384
385/*
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
389 * video capture has.
390 */
Pawel Osciak7a022642010-03-17 04:01:04 -0300391static int videobuf_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300392{
393 struct page *page;
394
Pawel Osciak7a022642010-03-17 04:01:04 -0300395 dprintk(3, "fault: fault @ %08lx [vma %08lx-%08lx]\n",
396 (unsigned long)vmf->virtual_address,
397 vma->vm_start, vma->vm_end);
398
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300399 page = alloc_page(GFP_USER | __GFP_DMA32);
400 if (!page)
Nick Piggin105354a2007-12-07 17:57:38 -0300401 return VM_FAULT_OOM;
Guennadi Liakhovetskic0cd5012009-01-03 18:20:04 -0300402 clear_user_highpage(page, (unsigned long)vmf->virtual_address);
Nick Piggin105354a2007-12-07 17:57:38 -0300403 vmf->page = page;
Pawel Osciak7a022642010-03-17 04:01:04 -0300404
Nick Piggin105354a2007-12-07 17:57:38 -0300405 return 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300406}
407
Pawel Osciak7a022642010-03-17 04:01:04 -0300408static const struct vm_operations_struct videobuf_vm_ops = {
409 .open = videobuf_vm_open,
410 .close = videobuf_vm_close,
411 .fault = videobuf_vm_fault,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300412};
413
414/* ---------------------------------------------------------------------
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300415 * SG handlers for the generic methods
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300416 */
417
418/* Allocated area consists on 3 parts:
419 struct video_buffer
420 struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300421 struct videobuf_dma_sg_memory
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300422 */
423
Pawel Osciak33c38282010-05-11 10:36:28 -0300424static struct videobuf_buffer *__videobuf_alloc_vb(size_t size)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300425{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300426 struct videobuf_dma_sg_memory *mem;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300427 struct videobuf_buffer *vb;
428
Pawel Osciak7a022642010-03-17 04:01:04 -0300429 vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
Pawel Osciakbee527f2010-02-22 13:10:06 -0300430 if (!vb)
431 return vb;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300432
Pawel Osciak7a022642010-03-17 04:01:04 -0300433 mem = vb->priv = ((char *)vb) + size;
434 mem->magic = MAGIC_SG_MEM;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300435
436 videobuf_dma_init(&mem->dma);
437
Pawel Osciak7a022642010-03-17 04:01:04 -0300438 dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
439 __func__, vb, (long)sizeof(*vb), (long)size - sizeof(*vb),
440 mem, (long)sizeof(*mem));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300441
442 return vb;
443}
444
Hans Verkuil037c75e2010-03-28 08:18:37 -0300445static void *__videobuf_to_vaddr(struct videobuf_buffer *buf)
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300446{
447 struct videobuf_dma_sg_memory *mem = buf->priv;
448 BUG_ON(!mem);
449
450 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
451
Laurent Pinchartbb6dbe72010-05-11 10:36:34 -0300452 return mem->dma.vaddr;
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300453}
454
Pawel Osciak7a022642010-03-17 04:01:04 -0300455static int __videobuf_iolock(struct videobuf_queue *q,
456 struct videobuf_buffer *vb,
457 struct v4l2_framebuffer *fbuf)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300458{
Pawel Osciak7a022642010-03-17 04:01:04 -0300459 int err, pages;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300460 dma_addr_t bus;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300461 struct videobuf_dma_sg_memory *mem = vb->priv;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300462 BUG_ON(!mem);
463
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300464 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300465
466 switch (vb->memory) {
467 case V4L2_MEMORY_MMAP:
468 case V4L2_MEMORY_USERPTR:
469 if (0 == vb->baddr) {
470 /* no userspace addr -- kernel bounce buffer */
471 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
Pawel Osciak7a022642010-03-17 04:01:04 -0300472 err = videobuf_dma_init_kernel(&mem->dma,
473 DMA_FROM_DEVICE,
474 pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300475 if (0 != err)
476 return err;
Maxim Levitsky99001322007-09-27 20:34:09 -0300477 } else if (vb->memory == V4L2_MEMORY_USERPTR) {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300478 /* dma directly to userspace */
Pawel Osciak7a022642010-03-17 04:01:04 -0300479 err = videobuf_dma_init_user(&mem->dma,
480 DMA_FROM_DEVICE,
481 vb->baddr, vb->bsize);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300482 if (0 != err)
483 return err;
Maxim Levitsky99001322007-09-27 20:34:09 -0300484 } else {
485 /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP
486 buffers can only be called from videobuf_qbuf
487 we take current->mm->mmap_sem there, to prevent
488 locking inversion, so don't take it here */
489
490 err = videobuf_dma_init_user_locked(&mem->dma,
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300491 DMA_FROM_DEVICE,
Maxim Levitsky99001322007-09-27 20:34:09 -0300492 vb->baddr, vb->bsize);
493 if (0 != err)
494 return err;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300495 }
496 break;
497 case V4L2_MEMORY_OVERLAY:
498 if (NULL == fbuf)
499 return -EINVAL;
500 /* FIXME: need sanity checks for vb->boff */
501 /*
502 * Using a double cast to avoid compiler warnings when
503 * building for PAE. Compiler doesn't like direct casting
504 * of a 32 bit ptr to 64 bit integer.
505 */
506 bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
507 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300508 err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300509 bus, pages);
510 if (0 != err)
511 return err;
512 break;
513 default:
514 BUG();
515 }
Laurent Pinchart95268402010-05-11 10:36:30 -0300516 err = videobuf_dma_map(q->dev, &mem->dma);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300517 if (0 != err)
518 return err;
519
520 return 0;
521}
522
523static int __videobuf_sync(struct videobuf_queue *q,
524 struct videobuf_buffer *buf)
525{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300526 struct videobuf_dma_sg_memory *mem = buf->priv;
Mauro Carvalho Chehab97f81052010-05-05 16:23:09 -0300527 BUG_ON(!mem || !mem->dma.sglen);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300528
Mauro Carvalho Chehab97f81052010-05-05 16:23:09 -0300529 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
530 MAGIC_CHECK(mem->dma.magic, MAGIC_DMABUF);
531
532 dma_sync_sg_for_cpu(q->dev, mem->dma.sglist,
Arnout Vandecappellefc7f8fd2010-03-17 19:53:04 -0300533 mem->dma.sglen, mem->dma.direction);
Mauro Carvalho Chehab97f81052010-05-05 16:23:09 -0300534
535 return 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300536}
537
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300538static int __videobuf_mmap_mapper(struct videobuf_queue *q,
Hans Verkuil0b62b732010-03-28 09:09:05 -0300539 struct videobuf_buffer *buf,
540 struct vm_area_struct *vma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300541{
Hans Verkuil0b62b732010-03-28 09:09:05 -0300542 struct videobuf_dma_sg_memory *mem = buf->priv;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300543 struct videobuf_mapping *map;
Mauro Carvalho Chehab474675a2010-04-25 11:23:52 -0300544 unsigned int first, last, size = 0, i;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300545 int retval;
546
547 retval = -EINVAL;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300548
Hans Verkuil0b62b732010-03-28 09:09:05 -0300549 BUG_ON(!mem);
550 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
551
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300552 /* look for first buffer to map */
553 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
Hans Verkuil0b62b732010-03-28 09:09:05 -0300554 if (buf == q->bufs[first]) {
555 size = PAGE_ALIGN(q->bufs[first]->bsize);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300556 break;
Hans Verkuil0b62b732010-03-28 09:09:05 -0300557 }
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300558 }
Hans Verkuil0b62b732010-03-28 09:09:05 -0300559
560 /* paranoia, should never happen since buf is always valid. */
Mauro Carvalho Chehab474675a2010-04-25 11:23:52 -0300561 if (!size) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300562 dprintk(1, "mmap app bug: offset invalid [offset=0x%lx]\n",
Hans Verkuil0b62b732010-03-28 09:09:05 -0300563 (vma->vm_pgoff << PAGE_SHIFT));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300564 goto done;
565 }
566
Hans Verkuil0b62b732010-03-28 09:09:05 -0300567 last = first;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300568
569 /* create mapping + update buffer list */
570 retval = -ENOMEM;
Pawel Osciak7a022642010-03-17 04:01:04 -0300571 map = kmalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300572 if (NULL == map)
573 goto done;
Brandon Philipsce540932008-04-02 18:10:59 -0300574
575 size = 0;
576 for (i = first; i <= last; i++) {
577 if (NULL == q->bufs[i])
578 continue;
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300579 q->bufs[i]->map = map;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300580 q->bufs[i]->baddr = vma->vm_start + size;
Tuukka Toivonen7cbefad2009-07-23 10:56:25 -0300581 size += PAGE_ALIGN(q->bufs[i]->bsize);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300582 }
Brandon Philipsce540932008-04-02 18:10:59 -0300583
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300584 map->count = 1;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300585 map->q = q;
586 vma->vm_ops = &videobuf_vm_ops;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700587 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300588 vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
589 vma->vm_private_data = map;
Pawel Osciak7a022642010-03-17 04:01:04 -0300590 dprintk(1, "mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
591 map, q, vma->vm_start, vma->vm_end, vma->vm_pgoff, first, last);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300592 retval = 0;
593
Pawel Osciak7a022642010-03-17 04:01:04 -0300594done:
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300595 return retval;
596}
597
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300598static struct videobuf_qtype_ops sg_ops = {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300599 .magic = MAGIC_QTYPE_OPS,
600
Pawel Osciak33c38282010-05-11 10:36:28 -0300601 .alloc_vb = __videobuf_alloc_vb,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300602 .iolock = __videobuf_iolock,
603 .sync = __videobuf_sync,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300604 .mmap_mapper = __videobuf_mmap_mapper,
Hans Verkuil037c75e2010-03-28 08:18:37 -0300605 .vaddr = __videobuf_to_vaddr,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300606};
607
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300608void *videobuf_sg_alloc(size_t size)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300609{
610 struct videobuf_queue q;
611
612 /* Required to make generic handler to call __videobuf_alloc */
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300613 q.int_ops = &sg_ops;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300614
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300615 q.msize = size;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300616
Pawel Osciak33c38282010-05-11 10:36:28 -0300617 return videobuf_alloc_vb(&q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300618}
Pawel Osciak7a022642010-03-17 04:01:04 -0300619EXPORT_SYMBOL_GPL(videobuf_sg_alloc);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300620
Pawel Osciak7a022642010-03-17 04:01:04 -0300621void videobuf_queue_sg_init(struct videobuf_queue *q,
Jonathan Corbet38a54f32009-11-17 19:43:41 -0300622 const struct videobuf_queue_ops *ops,
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300623 struct device *dev,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300624 spinlock_t *irqlock,
625 enum v4l2_buf_type type,
626 enum v4l2_field field,
627 unsigned int msize,
Hans Verkuil08bff032010-09-20 17:39:46 -0300628 void *priv,
629 struct mutex *ext_lock)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300630{
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300631 videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
Hans Verkuil08bff032010-09-20 17:39:46 -0300632 priv, &sg_ops, ext_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300633}
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300634EXPORT_SYMBOL_GPL(videobuf_queue_sg_init);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300635