This source file includes following definitions.
- videobuf_vmalloc_to_sg
- videobuf_pages_to_sg
- videobuf_to_dma
- videobuf_dma_init
- videobuf_dma_init_user_locked
- videobuf_dma_init_user
- videobuf_dma_init_kernel
- videobuf_dma_init_overlay
- videobuf_dma_map
- videobuf_dma_unmap
- videobuf_dma_free
- videobuf_vm_open
- videobuf_vm_close
- videobuf_vm_fault
- __videobuf_alloc_vb
- __videobuf_to_vaddr
- __videobuf_iolock
- __videobuf_sync
- __videobuf_mmap_mapper
- videobuf_sg_alloc
- videobuf_queue_sg_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/sched/mm.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24
25 #include <linux/dma-mapping.h>
26 #include <linux/vmalloc.h>
27 #include <linux/pagemap.h>
28 #include <linux/scatterlist.h>
29 #include <asm/page.h>
30 #include <asm/pgtable.h>
31
32 #include <media/videobuf-dma-sg.h>
33
34 #define MAGIC_DMABUF 0x19721112
35 #define MAGIC_SG_MEM 0x17890714
36
37 #define MAGIC_CHECK(is, should) \
38 if (unlikely((is) != (should))) { \
39 printk(KERN_ERR "magic mismatch: %x (expected %x)\n", \
40 is, should); \
41 BUG(); \
42 }
43
44 static int debug;
45 module_param(debug, int, 0644);
46
47 MODULE_DESCRIPTION("helper module to manage video4linux dma sg buffers");
48 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@kernel.org>");
49 MODULE_LICENSE("GPL");
50
51 #define dprintk(level, fmt, arg...) \
52 if (debug >= level) \
53 printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg)
54
55
56
57
58
59
60
61
62 static struct scatterlist *videobuf_vmalloc_to_sg(unsigned char *virt,
63 int nr_pages)
64 {
65 struct scatterlist *sglist;
66 struct page *pg;
67 int i;
68
69 sglist = vzalloc(array_size(nr_pages, sizeof(*sglist)));
70 if (NULL == sglist)
71 return NULL;
72 sg_init_table(sglist, nr_pages);
73 for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
74 pg = vmalloc_to_page(virt);
75 if (NULL == pg)
76 goto err;
77 BUG_ON(PageHighMem(pg));
78 sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
79 }
80 return sglist;
81
82 err:
83 vfree(sglist);
84 return NULL;
85 }
86
87
88
89
90
91
92 static struct scatterlist *videobuf_pages_to_sg(struct page **pages,
93 int nr_pages, int offset, size_t size)
94 {
95 struct scatterlist *sglist;
96 int i;
97
98 if (NULL == pages[0])
99 return NULL;
100 sglist = vmalloc(array_size(nr_pages, sizeof(*sglist)));
101 if (NULL == sglist)
102 return NULL;
103 sg_init_table(sglist, nr_pages);
104
105 if (PageHighMem(pages[0]))
106
107 goto highmem;
108 sg_set_page(&sglist[0], pages[0],
109 min_t(size_t, PAGE_SIZE - offset, size), offset);
110 size -= min_t(size_t, PAGE_SIZE - offset, size);
111 for (i = 1; i < nr_pages; i++) {
112 if (NULL == pages[i])
113 goto nopage;
114 if (PageHighMem(pages[i]))
115 goto highmem;
116 sg_set_page(&sglist[i], pages[i], min_t(size_t, PAGE_SIZE, size), 0);
117 size -= min_t(size_t, PAGE_SIZE, size);
118 }
119 return sglist;
120
121 nopage:
122 dprintk(2, "sgl: oops - no page\n");
123 vfree(sglist);
124 return NULL;
125
126 highmem:
127 dprintk(2, "sgl: oops - highmem page\n");
128 vfree(sglist);
129 return NULL;
130 }
131
132
133
134 struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf)
135 {
136 struct videobuf_dma_sg_memory *mem = buf->priv;
137 BUG_ON(!mem);
138
139 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
140
141 return &mem->dma;
142 }
143 EXPORT_SYMBOL_GPL(videobuf_to_dma);
144
145 static void videobuf_dma_init(struct videobuf_dmabuf *dma)
146 {
147 memset(dma, 0, sizeof(*dma));
148 dma->magic = MAGIC_DMABUF;
149 }
150
151 static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma,
152 int direction, unsigned long data, unsigned long size)
153 {
154 unsigned long first, last;
155 int err, rw = 0;
156 unsigned int flags = FOLL_FORCE;
157
158 dma->direction = direction;
159 switch (dma->direction) {
160 case DMA_FROM_DEVICE:
161 rw = READ;
162 break;
163 case DMA_TO_DEVICE:
164 rw = WRITE;
165 break;
166 default:
167 BUG();
168 }
169
170 first = (data & PAGE_MASK) >> PAGE_SHIFT;
171 last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
172 dma->offset = data & ~PAGE_MASK;
173 dma->size = size;
174 dma->nr_pages = last-first+1;
175 dma->pages = kmalloc_array(dma->nr_pages, sizeof(struct page *),
176 GFP_KERNEL);
177 if (NULL == dma->pages)
178 return -ENOMEM;
179
180 if (rw == READ)
181 flags |= FOLL_WRITE;
182
183 dprintk(1, "init user [0x%lx+0x%lx => %d pages]\n",
184 data, size, dma->nr_pages);
185
186 err = get_user_pages(data & PAGE_MASK, dma->nr_pages,
187 flags | FOLL_LONGTERM, 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,
192 dma->nr_pages);
193 return err < 0 ? err : -EINVAL;
194 }
195 return 0;
196 }
197
198 static int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
199 unsigned long data, unsigned long size)
200 {
201 int ret;
202
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 }
209
210 static int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
211 int nr_pages)
212 {
213 int i;
214
215 dprintk(1, "init kernel [%d pages]\n", nr_pages);
216
217 dma->direction = direction;
218 dma->vaddr_pages = kcalloc(nr_pages, sizeof(*dma->vaddr_pages),
219 GFP_KERNEL);
220 if (!dma->vaddr_pages)
221 return -ENOMEM;
222
223 dma->dma_addr = kcalloc(nr_pages, sizeof(*dma->dma_addr), GFP_KERNEL);
224 if (!dma->dma_addr) {
225 kfree(dma->vaddr_pages);
226 return -ENOMEM;
227 }
228 for (i = 0; i < nr_pages; i++) {
229 void *addr;
230
231 addr = dma_alloc_coherent(dma->dev, PAGE_SIZE,
232 &(dma->dma_addr[i]), GFP_KERNEL);
233 if (addr == NULL)
234 goto out_free_pages;
235
236 dma->vaddr_pages[i] = virt_to_page(addr);
237 }
238 dma->vaddr = vmap(dma->vaddr_pages, nr_pages, VM_MAP | VM_IOREMAP,
239 PAGE_KERNEL);
240 if (NULL == dma->vaddr) {
241 dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
242 goto out_free_pages;
243 }
244
245 dprintk(1, "vmalloc is at addr %p, size=%d\n",
246 dma->vaddr, nr_pages << PAGE_SHIFT);
247
248 memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT);
249 dma->nr_pages = nr_pages;
250
251 return 0;
252 out_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
269 static 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
285 static 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
328 int 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->nr_pages, dma->direction);
336
337 vfree(dma->sglist);
338 dma->sglist = NULL;
339 dma->sglen = 0;
340
341 return 0;
342 }
343 EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
344
345 int 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 if (dma->direction == DMA_FROM_DEVICE)
354 set_page_dirty_lock(dma->pages[i]);
355 put_page(dma->pages[i]);
356 }
357 kfree(dma->pages);
358 dma->pages = NULL;
359 }
360
361 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 }
376
377 if (dma->bus_addr)
378 dma->bus_addr = 0;
379 dma->direction = DMA_NONE;
380
381 return 0;
382 }
383 EXPORT_SYMBOL_GPL(videobuf_dma_free);
384
385
386
387 static void videobuf_vm_open(struct vm_area_struct *vma)
388 {
389 struct videobuf_mapping *map = vma->vm_private_data;
390
391 dprintk(2, "vm_open %p [count=%d,vma=%08lx-%08lx]\n", map,
392 map->count, vma->vm_start, vma->vm_end);
393
394 map->count++;
395 }
396
397 static void videobuf_vm_close(struct vm_area_struct *vma)
398 {
399 struct videobuf_mapping *map = vma->vm_private_data;
400 struct videobuf_queue *q = map->q;
401 struct videobuf_dma_sg_memory *mem;
402 int i;
403
404 dprintk(2, "vm_close %p [count=%d,vma=%08lx-%08lx]\n", map,
405 map->count, vma->vm_start, vma->vm_end);
406
407 map->count--;
408 if (0 == map->count) {
409 dprintk(1, "munmap %p q=%p\n", map, q);
410 videobuf_queue_lock(q);
411 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
412 if (NULL == q->bufs[i])
413 continue;
414 mem = q->bufs[i]->priv;
415 if (!mem)
416 continue;
417
418 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
419
420 if (q->bufs[i]->map != map)
421 continue;
422 q->bufs[i]->map = NULL;
423 q->bufs[i]->baddr = 0;
424 q->ops->buf_release(q, q->bufs[i]);
425 }
426 videobuf_queue_unlock(q);
427 kfree(map);
428 }
429 return;
430 }
431
432
433
434
435
436
437
438 static vm_fault_t videobuf_vm_fault(struct vm_fault *vmf)
439 {
440 struct vm_area_struct *vma = vmf->vma;
441 struct page *page;
442
443 dprintk(3, "fault: fault @ %08lx [vma %08lx-%08lx]\n",
444 vmf->address, vma->vm_start, vma->vm_end);
445
446 page = alloc_page(GFP_USER | __GFP_DMA32);
447 if (!page)
448 return VM_FAULT_OOM;
449 clear_user_highpage(page, vmf->address);
450 vmf->page = page;
451
452 return 0;
453 }
454
455 static const struct vm_operations_struct videobuf_vm_ops = {
456 .open = videobuf_vm_open,
457 .close = videobuf_vm_close,
458 .fault = videobuf_vm_fault,
459 };
460
461
462
463
464
465
466
467
468
469
470
471 static struct videobuf_buffer *__videobuf_alloc_vb(size_t size)
472 {
473 struct videobuf_dma_sg_memory *mem;
474 struct videobuf_buffer *vb;
475
476 vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
477 if (!vb)
478 return vb;
479
480 mem = vb->priv = ((char *)vb) + size;
481 mem->magic = MAGIC_SG_MEM;
482
483 videobuf_dma_init(&mem->dma);
484
485 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));
488
489 return vb;
490 }
491
492 static void *__videobuf_to_vaddr(struct videobuf_buffer *buf)
493 {
494 struct videobuf_dma_sg_memory *mem = buf->priv;
495 BUG_ON(!mem);
496
497 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
498
499 return mem->dma.vaddr;
500 }
501
502 static int __videobuf_iolock(struct videobuf_queue *q,
503 struct videobuf_buffer *vb,
504 struct v4l2_framebuffer *fbuf)
505 {
506 int err, pages;
507 dma_addr_t bus;
508 struct videobuf_dma_sg_memory *mem = vb->priv;
509 BUG_ON(!mem);
510
511 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
512
513 if (!mem->dma.dev)
514 mem->dma.dev = q->dev;
515 else
516 WARN_ON(mem->dma.dev != q->dev);
517
518 switch (vb->memory) {
519 case V4L2_MEMORY_MMAP:
520 case V4L2_MEMORY_USERPTR:
521 if (0 == vb->baddr) {
522
523 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
524 err = videobuf_dma_init_kernel(&mem->dma,
525 DMA_FROM_DEVICE,
526 pages);
527 if (0 != err)
528 return err;
529 } else if (vb->memory == V4L2_MEMORY_USERPTR) {
530
531 err = videobuf_dma_init_user(&mem->dma,
532 DMA_FROM_DEVICE,
533 vb->baddr, vb->bsize);
534 if (0 != err)
535 return err;
536 } else {
537
538
539
540
541
542 err = videobuf_dma_init_user_locked(&mem->dma,
543 DMA_FROM_DEVICE,
544 vb->baddr, vb->bsize);
545 if (0 != err)
546 return err;
547 }
548 break;
549 case V4L2_MEMORY_OVERLAY:
550 if (NULL == fbuf)
551 return -EINVAL;
552
553
554
555
556
557
558 bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
559 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
560 err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE,
561 bus, pages);
562 if (0 != err)
563 return err;
564 break;
565 default:
566 BUG();
567 }
568 err = videobuf_dma_map(q->dev, &mem->dma);
569 if (0 != err)
570 return err;
571
572 return 0;
573 }
574
575 static int __videobuf_sync(struct videobuf_queue *q,
576 struct videobuf_buffer *buf)
577 {
578 struct videobuf_dma_sg_memory *mem = buf->priv;
579 BUG_ON(!mem || !mem->dma.sglen);
580
581 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,
585 mem->dma.nr_pages, mem->dma.direction);
586
587 return 0;
588 }
589
590 static int __videobuf_mmap_mapper(struct videobuf_queue *q,
591 struct videobuf_buffer *buf,
592 struct vm_area_struct *vma)
593 {
594 struct videobuf_dma_sg_memory *mem = buf->priv;
595 struct videobuf_mapping *map;
596 unsigned int first, last, size = 0, i;
597 int retval;
598
599 retval = -EINVAL;
600
601 BUG_ON(!mem);
602 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
603
604
605 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
606 if (buf == q->bufs[first]) {
607 size = PAGE_ALIGN(q->bufs[first]->bsize);
608 break;
609 }
610 }
611
612
613 if (!size) {
614 dprintk(1, "mmap app bug: offset invalid [offset=0x%lx]\n",
615 (vma->vm_pgoff << PAGE_SHIFT));
616 goto done;
617 }
618
619 last = first;
620
621
622 retval = -ENOMEM;
623 map = kmalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
624 if (NULL == map)
625 goto done;
626
627 size = 0;
628 for (i = first; i <= last; i++) {
629 if (NULL == q->bufs[i])
630 continue;
631 q->bufs[i]->map = map;
632 q->bufs[i]->baddr = vma->vm_start + size;
633 size += PAGE_ALIGN(q->bufs[i]->bsize);
634 }
635
636 map->count = 1;
637 map->q = q;
638 vma->vm_ops = &videobuf_vm_ops;
639 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
640 vma->vm_flags &= ~VM_IO;
641 vma->vm_private_data = map;
642 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);
644 retval = 0;
645
646 done:
647 return retval;
648 }
649
650 static struct videobuf_qtype_ops sg_ops = {
651 .magic = MAGIC_QTYPE_OPS,
652
653 .alloc_vb = __videobuf_alloc_vb,
654 .iolock = __videobuf_iolock,
655 .sync = __videobuf_sync,
656 .mmap_mapper = __videobuf_mmap_mapper,
657 .vaddr = __videobuf_to_vaddr,
658 };
659
660 void *videobuf_sg_alloc(size_t size)
661 {
662 struct videobuf_queue q;
663
664
665 q.int_ops = &sg_ops;
666
667 q.msize = size;
668
669 return videobuf_alloc_vb(&q);
670 }
671 EXPORT_SYMBOL_GPL(videobuf_sg_alloc);
672
673 void videobuf_queue_sg_init(struct videobuf_queue *q,
674 const struct videobuf_queue_ops *ops,
675 struct device *dev,
676 spinlock_t *irqlock,
677 enum v4l2_buf_type type,
678 enum v4l2_field field,
679 unsigned int msize,
680 void *priv,
681 struct mutex *ext_lock)
682 {
683 videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
684 priv, &sg_ops, ext_lock);
685 }
686 EXPORT_SYMBOL_GPL(videobuf_queue_sg_init);
687