blob: b6189a4958c5ec2d40cfd8486e69794ba9be9bee [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
Hans Verkuil861360a2014-12-23 09:46:27 -0300148static void videobuf_dma_init(struct videobuf_dmabuf *dma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300149{
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}
153
Maxim Levitsky99001322007-09-27 20:34:09 -0300154static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma,
155 int direction, unsigned long data, unsigned long size)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300156{
Pawel Osciak7a022642010-03-17 04:01:04 -0300157 unsigned long first, last;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300158 int err, rw = 0;
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100159 unsigned int flags = FOLL_FORCE;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300160
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
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100182 if (rw == READ)
183 flags |= FOLL_WRITE;
184
Pawel Osciak7a022642010-03-17 04:01:04 -0300185 dprintk(1, "init user [0x%lx+0x%lx => %d pages]\n",
186 data, size, dma->nr_pages);
187
Dan Williams53dfce32018-02-23 14:05:54 -0800188 err = get_user_pages_longterm(data & PAGE_MASK, dma->nr_pages,
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100189 flags, dma->pages, NULL);
Maxim Levitsky99001322007-09-27 20:34:09 -0300190
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300191 if (err != dma->nr_pages) {
192 dma->nr_pages = (err >= 0) ? err : 0;
Dan Williams53dfce32018-02-23 14:05:54 -0800193 dprintk(1, "get_user_pages_longterm: err=%d [%d]\n", err,
194 dma->nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300195 return err < 0 ? err : -EINVAL;
196 }
197 return 0;
198}
199
Hans Verkuil861360a2014-12-23 09:46:27 -0300200static int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
Maxim Levitsky99001322007-09-27 20:34:09 -0300201 unsigned long data, unsigned long size)
202{
203 int ret;
Pawel Osciak7a022642010-03-17 04:01:04 -0300204
Maxim Levitsky99001322007-09-27 20:34:09 -0300205 down_read(&current->mm->mmap_sem);
206 ret = videobuf_dma_init_user_locked(dma, direction, data, size);
207 up_read(&current->mm->mmap_sem);
208
209 return ret;
210}
211
Hans Verkuil861360a2014-12-23 09:46:27 -0300212static int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300213 int nr_pages)
214{
James Harper7b4eeed2014-06-12 06:53:38 -0300215 int i;
216
Pawel Osciak7a022642010-03-17 04:01:04 -0300217 dprintk(1, "init kernel [%d pages]\n", nr_pages);
218
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300219 dma->direction = direction;
James Harper7b4eeed2014-06-12 06:53:38 -0300220 dma->vaddr_pages = kcalloc(nr_pages, sizeof(*dma->vaddr_pages),
221 GFP_KERNEL);
222 if (!dma->vaddr_pages)
223 return -ENOMEM;
224
225 dma->dma_addr = kcalloc(nr_pages, sizeof(*dma->dma_addr), GFP_KERNEL);
226 if (!dma->dma_addr) {
227 kfree(dma->vaddr_pages);
228 return -ENOMEM;
229 }
230 for (i = 0; i < nr_pages; i++) {
231 void *addr;
232
233 addr = dma_alloc_coherent(dma->dev, PAGE_SIZE,
234 &(dma->dma_addr[i]), GFP_KERNEL);
235 if (addr == NULL)
236 goto out_free_pages;
237
238 dma->vaddr_pages[i] = virt_to_page(addr);
239 }
240 dma->vaddr = vmap(dma->vaddr_pages, nr_pages, VM_MAP | VM_IOREMAP,
241 PAGE_KERNEL);
Laurent Pinchartbb6dbe72010-05-11 10:36:34 -0300242 if (NULL == dma->vaddr) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300243 dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
James Harper7b4eeed2014-06-12 06:53:38 -0300244 goto out_free_pages;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300245 }
Pawel Osciak7a022642010-03-17 04:01:04 -0300246
247 dprintk(1, "vmalloc is at addr 0x%08lx, size=%d\n",
Laurent Pinchartbb6dbe72010-05-11 10:36:34 -0300248 (unsigned long)dma->vaddr,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300249 nr_pages << PAGE_SHIFT);
Pawel Osciak7a022642010-03-17 04:01:04 -0300250
Laurent Pinchartbb6dbe72010-05-11 10:36:34 -0300251 memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300252 dma->nr_pages = nr_pages;
Pawel Osciak7a022642010-03-17 04:01:04 -0300253
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300254 return 0;
James Harper7b4eeed2014-06-12 06:53:38 -0300255out_free_pages:
256 while (i > 0) {
Dan Carpenter23d30902014-08-05 05:11:13 -0300257 void *addr;
258
James Harper7b4eeed2014-06-12 06:53:38 -0300259 i--;
Dan Carpenter23d30902014-08-05 05:11:13 -0300260 addr = page_address(dma->vaddr_pages[i]);
261 dma_free_coherent(dma->dev, PAGE_SIZE, addr, dma->dma_addr[i]);
James Harper7b4eeed2014-06-12 06:53:38 -0300262 }
263 kfree(dma->dma_addr);
264 dma->dma_addr = NULL;
265 kfree(dma->vaddr_pages);
266 dma->vaddr_pages = NULL;
267
268 return -ENOMEM;
269
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300270}
271
Hans Verkuil861360a2014-12-23 09:46:27 -0300272static int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300273 dma_addr_t addr, int nr_pages)
274{
Pawel Osciak7a022642010-03-17 04:01:04 -0300275 dprintk(1, "init overlay [%d pages @ bus 0x%lx]\n",
276 nr_pages, (unsigned long)addr);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300277 dma->direction = direction;
Pawel Osciak7a022642010-03-17 04:01:04 -0300278
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300279 if (0 == addr)
280 return -EINVAL;
281
282 dma->bus_addr = addr;
283 dma->nr_pages = nr_pages;
Pawel Osciak7a022642010-03-17 04:01:04 -0300284
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300285 return 0;
286}
287
Hans Verkuil861360a2014-12-23 09:46:27 -0300288static int videobuf_dma_map(struct device *dev, struct videobuf_dmabuf *dma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300289{
Pawel Osciak7a022642010-03-17 04:01:04 -0300290 MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300291 BUG_ON(0 == dma->nr_pages);
292
293 if (dma->pages) {
294 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
Hans Verkuil2fc11532010-09-07 06:10:45 -0300295 dma->offset, dma->size);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300296 }
Laurent Pinchartbb6dbe72010-05-11 10:36:34 -0300297 if (dma->vaddr) {
298 dma->sglist = videobuf_vmalloc_to_sg(dma->vaddr,
Pawel Osciak7a022642010-03-17 04:01:04 -0300299 dma->nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300300 }
301 if (dma->bus_addr) {
Cohen David.A1010ed12009-05-11 11:00:20 -0300302 dma->sglist = vmalloc(sizeof(*dma->sglist));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300303 if (NULL != dma->sglist) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300304 dma->sglen = 1;
305 sg_dma_address(&dma->sglist[0]) = dma->bus_addr
306 & PAGE_MASK;
307 dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
308 sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300309 }
310 }
311 if (NULL == dma->sglist) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300312 dprintk(1, "scatterlist is NULL\n");
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300313 return -ENOMEM;
314 }
315 if (!dma->bus_addr) {
Laurent Pinchart95268402010-05-11 10:36:30 -0300316 dma->sglen = dma_map_sg(dev, dma->sglist,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300317 dma->nr_pages, dma->direction);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300318 if (0 == dma->sglen) {
319 printk(KERN_WARNING
Pawel Osciak7a022642010-03-17 04:01:04 -0300320 "%s: videobuf_map_sg failed\n", __func__);
Cohen David.A1010ed12009-05-11 11:00:20 -0300321 vfree(dma->sglist);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300322 dma->sglist = NULL;
323 dma->sglen = 0;
Figo.zhang92051b22009-06-10 23:17:27 -0300324 return -ENOMEM;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300325 }
326 }
Pawel Osciak7a022642010-03-17 04:01:04 -0300327
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300328 return 0;
329}
330
Laurent Pinchart95268402010-05-11 10:36:30 -0300331int videobuf_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300332{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300333 MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
Pawel Osciak7a022642010-03-17 04:01:04 -0300334
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300335 if (!dma->sglen)
336 return 0;
337
Laurent Pinchart95268402010-05-11 10:36:30 -0300338 dma_unmap_sg(dev, dma->sglist, dma->sglen, dma->direction);
Mauro Carvalho Chehab5ddff432007-10-08 11:43:49 -0300339
Cohen David.A1010ed12009-05-11 11:00:20 -0300340 vfree(dma->sglist);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300341 dma->sglist = NULL;
342 dma->sglen = 0;
Pawel Osciak7a022642010-03-17 04:01:04 -0300343
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300344 return 0;
345}
Pawel Osciak7a022642010-03-17 04:01:04 -0300346EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300347
348int videobuf_dma_free(struct videobuf_dmabuf *dma)
349{
Pawel Osciak7a022642010-03-17 04:01:04 -0300350 int i;
351 MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300352 BUG_ON(dma->sglen);
353
354 if (dma->pages) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300355 for (i = 0; i < dma->nr_pages; i++)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300356 put_page(dma->pages[i]);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300357 kfree(dma->pages);
358 dma->pages = NULL;
359 }
360
James Harper7b4eeed2014-06-12 06:53:38 -0300361 if (dma->dma_addr) {
362 for (i = 0; i < dma->nr_pages; i++) {
363 void *addr;
364
365 addr = page_address(dma->vaddr_pages[i]);
366 dma_free_coherent(dma->dev, PAGE_SIZE, addr,
367 dma->dma_addr[i]);
368 }
369 kfree(dma->dma_addr);
370 dma->dma_addr = NULL;
371 kfree(dma->vaddr_pages);
372 dma->vaddr_pages = NULL;
373 vunmap(dma->vaddr);
374 dma->vaddr = NULL;
375 }
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300376
Pawel Osciak7a022642010-03-17 04:01:04 -0300377 if (dma->bus_addr)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300378 dma->bus_addr = 0;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300379 dma->direction = DMA_NONE;
Pawel Osciak7a022642010-03-17 04:01:04 -0300380
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300381 return 0;
382}
Pawel Osciak7a022642010-03-17 04:01:04 -0300383EXPORT_SYMBOL_GPL(videobuf_dma_free);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300384
385/* --------------------------------------------------------------------- */
386
Pawel Osciak7a022642010-03-17 04:01:04 -0300387static void videobuf_vm_open(struct vm_area_struct *vma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300388{
389 struct videobuf_mapping *map = vma->vm_private_data;
390
Pawel Osciak7a022642010-03-17 04:01:04 -0300391 dprintk(2, "vm_open %p [count=%d,vma=%08lx-%08lx]\n", map,
392 map->count, vma->vm_start, vma->vm_end);
393
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300394 map->count++;
395}
396
Pawel Osciak7a022642010-03-17 04:01:04 -0300397static void videobuf_vm_close(struct vm_area_struct *vma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300398{
399 struct videobuf_mapping *map = vma->vm_private_data;
400 struct videobuf_queue *q = map->q;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300401 struct videobuf_dma_sg_memory *mem;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300402 int i;
403
Pawel Osciak7a022642010-03-17 04:01:04 -0300404 dprintk(2, "vm_close %p [count=%d,vma=%08lx-%08lx]\n", map,
405 map->count, vma->vm_start, vma->vm_end);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300406
Hans Verkuilcca36e22014-01-03 08:10:49 -0300407 map->count--;
408 if (0 == map->count) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300409 dprintk(1, "munmap %p q=%p\n", map, q);
Hans Verkuilcca36e22014-01-03 08:10:49 -0300410 videobuf_queue_lock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300411 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
412 if (NULL == q->bufs[i])
413 continue;
Pawel Osciak7a022642010-03-17 04:01:04 -0300414 mem = q->bufs[i]->priv;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300415 if (!mem)
416 continue;
417
Pawel Osciak7a022642010-03-17 04:01:04 -0300418 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300419
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300420 if (q->bufs[i]->map != map)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300421 continue;
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300422 q->bufs[i]->map = NULL;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300423 q->bufs[i]->baddr = 0;
Pawel Osciak7a022642010-03-17 04:01:04 -0300424 q->ops->buf_release(q, q->bufs[i]);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300425 }
Hans Verkuilcca36e22014-01-03 08:10:49 -0300426 videobuf_queue_unlock(q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300427 kfree(map);
428 }
429 return;
430}
431
432/*
433 * Get a anonymous page for the mapping. Make sure we can DMA to that
434 * memory location with 32bit PCI devices (i.e. don't use highmem for
435 * now ...). Bounce buffers don't work very well for the data rates
436 * video capture has.
437 */
Pawel Osciak7a022642010-03-17 04:01:04 -0300438static int videobuf_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300439{
440 struct page *page;
441
Pawel Osciak7a022642010-03-17 04:01:04 -0300442 dprintk(3, "fault: fault @ %08lx [vma %08lx-%08lx]\n",
443 (unsigned long)vmf->virtual_address,
444 vma->vm_start, vma->vm_end);
445
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300446 page = alloc_page(GFP_USER | __GFP_DMA32);
447 if (!page)
Nick Piggin105354a2007-12-07 17:57:38 -0300448 return VM_FAULT_OOM;
Guennadi Liakhovetskic0cd5012009-01-03 18:20:04 -0300449 clear_user_highpage(page, (unsigned long)vmf->virtual_address);
Nick Piggin105354a2007-12-07 17:57:38 -0300450 vmf->page = page;
Pawel Osciak7a022642010-03-17 04:01:04 -0300451
Nick Piggin105354a2007-12-07 17:57:38 -0300452 return 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300453}
454
Pawel Osciak7a022642010-03-17 04:01:04 -0300455static const struct vm_operations_struct videobuf_vm_ops = {
456 .open = videobuf_vm_open,
457 .close = videobuf_vm_close,
458 .fault = videobuf_vm_fault,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300459};
460
461/* ---------------------------------------------------------------------
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300462 * SG handlers for the generic methods
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300463 */
464
465/* Allocated area consists on 3 parts:
466 struct video_buffer
467 struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300468 struct videobuf_dma_sg_memory
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300469 */
470
Pawel Osciak33c38282010-05-11 10:36:28 -0300471static struct videobuf_buffer *__videobuf_alloc_vb(size_t size)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300472{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300473 struct videobuf_dma_sg_memory *mem;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300474 struct videobuf_buffer *vb;
475
Pawel Osciak7a022642010-03-17 04:01:04 -0300476 vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
Pawel Osciakbee527f2010-02-22 13:10:06 -0300477 if (!vb)
478 return vb;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300479
Pawel Osciak7a022642010-03-17 04:01:04 -0300480 mem = vb->priv = ((char *)vb) + size;
481 mem->magic = MAGIC_SG_MEM;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300482
483 videobuf_dma_init(&mem->dma);
484
Pawel Osciak7a022642010-03-17 04:01:04 -0300485 dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
486 __func__, vb, (long)sizeof(*vb), (long)size - sizeof(*vb),
487 mem, (long)sizeof(*mem));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300488
489 return vb;
490}
491
Hans Verkuil037c75e2010-03-28 08:18:37 -0300492static void *__videobuf_to_vaddr(struct videobuf_buffer *buf)
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300493{
494 struct videobuf_dma_sg_memory *mem = buf->priv;
495 BUG_ON(!mem);
496
497 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
498
Laurent Pinchartbb6dbe72010-05-11 10:36:34 -0300499 return mem->dma.vaddr;
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300500}
501
Pawel Osciak7a022642010-03-17 04:01:04 -0300502static int __videobuf_iolock(struct videobuf_queue *q,
503 struct videobuf_buffer *vb,
504 struct v4l2_framebuffer *fbuf)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300505{
Pawel Osciak7a022642010-03-17 04:01:04 -0300506 int err, pages;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300507 dma_addr_t bus;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300508 struct videobuf_dma_sg_memory *mem = vb->priv;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300509 BUG_ON(!mem);
510
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300511 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300512
James Harper7b4eeed2014-06-12 06:53:38 -0300513 if (!mem->dma.dev)
514 mem->dma.dev = q->dev;
515 else
516 WARN_ON(mem->dma.dev != q->dev);
517
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300518 switch (vb->memory) {
519 case V4L2_MEMORY_MMAP:
520 case V4L2_MEMORY_USERPTR:
521 if (0 == vb->baddr) {
522 /* no userspace addr -- kernel bounce buffer */
523 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
Pawel Osciak7a022642010-03-17 04:01:04 -0300524 err = videobuf_dma_init_kernel(&mem->dma,
525 DMA_FROM_DEVICE,
526 pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300527 if (0 != err)
528 return err;
Maxim Levitsky99001322007-09-27 20:34:09 -0300529 } else if (vb->memory == V4L2_MEMORY_USERPTR) {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300530 /* dma directly to userspace */
Pawel Osciak7a022642010-03-17 04:01:04 -0300531 err = videobuf_dma_init_user(&mem->dma,
532 DMA_FROM_DEVICE,
533 vb->baddr, vb->bsize);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300534 if (0 != err)
535 return err;
Maxim Levitsky99001322007-09-27 20:34:09 -0300536 } else {
537 /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP
538 buffers can only be called from videobuf_qbuf
539 we take current->mm->mmap_sem there, to prevent
540 locking inversion, so don't take it here */
541
542 err = videobuf_dma_init_user_locked(&mem->dma,
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300543 DMA_FROM_DEVICE,
Maxim Levitsky99001322007-09-27 20:34:09 -0300544 vb->baddr, vb->bsize);
545 if (0 != err)
546 return err;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300547 }
548 break;
549 case V4L2_MEMORY_OVERLAY:
550 if (NULL == fbuf)
551 return -EINVAL;
552 /* FIXME: need sanity checks for vb->boff */
553 /*
554 * Using a double cast to avoid compiler warnings when
555 * building for PAE. Compiler doesn't like direct casting
556 * of a 32 bit ptr to 64 bit integer.
557 */
558 bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
559 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300560 err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300561 bus, pages);
562 if (0 != err)
563 return err;
564 break;
565 default:
566 BUG();
567 }
Laurent Pinchart95268402010-05-11 10:36:30 -0300568 err = videobuf_dma_map(q->dev, &mem->dma);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300569 if (0 != err)
570 return err;
571
572 return 0;
573}
574
575static int __videobuf_sync(struct videobuf_queue *q,
576 struct videobuf_buffer *buf)
577{
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300578 struct videobuf_dma_sg_memory *mem = buf->priv;
Mauro Carvalho Chehab97f81052010-05-05 16:23:09 -0300579 BUG_ON(!mem || !mem->dma.sglen);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300580
Mauro Carvalho Chehab97f81052010-05-05 16:23:09 -0300581 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
582 MAGIC_CHECK(mem->dma.magic, MAGIC_DMABUF);
583
584 dma_sync_sg_for_cpu(q->dev, mem->dma.sglist,
Arnout Vandecappellefc7f8fd2010-03-17 19:53:04 -0300585 mem->dma.sglen, mem->dma.direction);
Mauro Carvalho Chehab97f81052010-05-05 16:23:09 -0300586
587 return 0;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300588}
589
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300590static int __videobuf_mmap_mapper(struct videobuf_queue *q,
Hans Verkuil0b62b732010-03-28 09:09:05 -0300591 struct videobuf_buffer *buf,
592 struct vm_area_struct *vma)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300593{
Hans Verkuil0b62b732010-03-28 09:09:05 -0300594 struct videobuf_dma_sg_memory *mem = buf->priv;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300595 struct videobuf_mapping *map;
Mauro Carvalho Chehab474675a2010-04-25 11:23:52 -0300596 unsigned int first, last, size = 0, i;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300597 int retval;
598
599 retval = -EINVAL;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300600
Hans Verkuil0b62b732010-03-28 09:09:05 -0300601 BUG_ON(!mem);
602 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
603
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300604 /* look for first buffer to map */
605 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
Hans Verkuil0b62b732010-03-28 09:09:05 -0300606 if (buf == q->bufs[first]) {
607 size = PAGE_ALIGN(q->bufs[first]->bsize);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300608 break;
Hans Verkuil0b62b732010-03-28 09:09:05 -0300609 }
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300610 }
Hans Verkuil0b62b732010-03-28 09:09:05 -0300611
612 /* paranoia, should never happen since buf is always valid. */
Mauro Carvalho Chehab474675a2010-04-25 11:23:52 -0300613 if (!size) {
Pawel Osciak7a022642010-03-17 04:01:04 -0300614 dprintk(1, "mmap app bug: offset invalid [offset=0x%lx]\n",
Hans Verkuil0b62b732010-03-28 09:09:05 -0300615 (vma->vm_pgoff << PAGE_SHIFT));
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300616 goto done;
617 }
618
Hans Verkuil0b62b732010-03-28 09:09:05 -0300619 last = first;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300620
621 /* create mapping + update buffer list */
622 retval = -ENOMEM;
Pawel Osciak7a022642010-03-17 04:01:04 -0300623 map = kmalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300624 if (NULL == map)
625 goto done;
Brandon Philipsce540932008-04-02 18:10:59 -0300626
627 size = 0;
628 for (i = first; i <= last; i++) {
629 if (NULL == q->bufs[i])
630 continue;
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300631 q->bufs[i]->map = map;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300632 q->bufs[i]->baddr = vma->vm_start + size;
Tuukka Toivonen7cbefad2009-07-23 10:56:25 -0300633 size += PAGE_ALIGN(q->bufs[i]->bsize);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300634 }
Brandon Philipsce540932008-04-02 18:10:59 -0300635
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300636 map->count = 1;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300637 map->q = q;
638 vma->vm_ops = &videobuf_vm_ops;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700639 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300640 vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
641 vma->vm_private_data = map;
Pawel Osciak7a022642010-03-17 04:01:04 -0300642 dprintk(1, "mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
643 map, q, vma->vm_start, vma->vm_end, vma->vm_pgoff, first, last);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300644 retval = 0;
645
Pawel Osciak7a022642010-03-17 04:01:04 -0300646done:
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300647 return retval;
648}
649
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300650static struct videobuf_qtype_ops sg_ops = {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300651 .magic = MAGIC_QTYPE_OPS,
652
Pawel Osciak33c38282010-05-11 10:36:28 -0300653 .alloc_vb = __videobuf_alloc_vb,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300654 .iolock = __videobuf_iolock,
655 .sync = __videobuf_sync,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300656 .mmap_mapper = __videobuf_mmap_mapper,
Hans Verkuil037c75e2010-03-28 08:18:37 -0300657 .vaddr = __videobuf_to_vaddr,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300658};
659
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300660void *videobuf_sg_alloc(size_t size)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300661{
662 struct videobuf_queue q;
663
664 /* Required to make generic handler to call __videobuf_alloc */
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300665 q.int_ops = &sg_ops;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300666
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300667 q.msize = size;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300668
Pawel Osciak33c38282010-05-11 10:36:28 -0300669 return videobuf_alloc_vb(&q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300670}
Pawel Osciak7a022642010-03-17 04:01:04 -0300671EXPORT_SYMBOL_GPL(videobuf_sg_alloc);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300672
Pawel Osciak7a022642010-03-17 04:01:04 -0300673void videobuf_queue_sg_init(struct videobuf_queue *q,
Jonathan Corbet38a54f32009-11-17 19:43:41 -0300674 const struct videobuf_queue_ops *ops,
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300675 struct device *dev,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300676 spinlock_t *irqlock,
677 enum v4l2_buf_type type,
678 enum v4l2_field field,
679 unsigned int msize,
Hans Verkuil08bff032010-09-20 17:39:46 -0300680 void *priv,
681 struct mutex *ext_lock)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300682{
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300683 videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
Hans Verkuil08bff032010-09-20 17:39:46 -0300684 priv, &sg_ops, ext_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300685}
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300686EXPORT_SYMBOL_GPL(videobuf_queue_sg_init);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300687