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