1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/module.h>
14#include <linux/mm.h>
15#include <linux/refcount.h>
16#include <linux/scatterlist.h>
17#include <linux/sched.h>
18#include <linux/slab.h>
19#include <linux/vmalloc.h>
20
21#include <media/videobuf2-v4l2.h>
22#include <media/videobuf2-memops.h>
23#include <media/videobuf2-dma-sg.h>
24
25static int debug;
26module_param(debug, int, 0644);
27
28#define dprintk(level, fmt, arg...) \
29 do { \
30 if (debug >= level) \
31 printk(KERN_DEBUG "vb2-dma-sg: " fmt, ## arg); \
32 } while (0)
33
34struct vb2_dma_sg_buf {
35 struct device *dev;
36 void *vaddr;
37 struct page **pages;
38 struct frame_vector *vec;
39 int offset;
40 enum dma_data_direction dma_dir;
41 struct sg_table sg_table;
42
43
44
45
46
47 struct sg_table *dma_sgt;
48 size_t size;
49 unsigned int num_pages;
50 refcount_t refcount;
51 struct vb2_vmarea_handler handler;
52
53 struct dma_buf_attachment *db_attach;
54};
55
56static void vb2_dma_sg_put(void *buf_priv);
57
58static int vb2_dma_sg_alloc_compacted(struct vb2_dma_sg_buf *buf,
59 gfp_t gfp_flags)
60{
61 unsigned int last_page = 0;
62 int size = buf->size;
63
64 while (size > 0) {
65 struct page *pages;
66 int order;
67 int i;
68
69 order = get_order(size);
70
71 if ((PAGE_SIZE << order) > size)
72 order--;
73
74 pages = NULL;
75 while (!pages) {
76 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO |
77 __GFP_NOWARN | gfp_flags, order);
78 if (pages)
79 break;
80
81 if (order == 0) {
82 while (last_page--)
83 __free_page(buf->pages[last_page]);
84 return -ENOMEM;
85 }
86 order--;
87 }
88
89 split_page(pages, order);
90 for (i = 0; i < (1 << order); i++)
91 buf->pages[last_page++] = &pages[i];
92
93 size -= PAGE_SIZE << order;
94 }
95
96 return 0;
97}
98
99static void *vb2_dma_sg_alloc(struct device *dev, unsigned long dma_attrs,
100 unsigned long size, enum dma_data_direction dma_dir,
101 gfp_t gfp_flags)
102{
103 struct vb2_dma_sg_buf *buf;
104 struct sg_table *sgt;
105 int ret;
106 int num_pages;
107
108 if (WARN_ON(!dev))
109 return ERR_PTR(-EINVAL);
110
111 buf = kzalloc(sizeof *buf, GFP_KERNEL);
112 if (!buf)
113 return ERR_PTR(-ENOMEM);
114
115 buf->vaddr = NULL;
116 buf->dma_dir = dma_dir;
117 buf->offset = 0;
118 buf->size = size;
119
120 buf->num_pages = size >> PAGE_SHIFT;
121 buf->dma_sgt = &buf->sg_table;
122
123 buf->pages = kvmalloc_array(buf->num_pages, sizeof(struct page *),
124 GFP_KERNEL | __GFP_ZERO);
125 if (!buf->pages)
126 goto fail_pages_array_alloc;
127
128 ret = vb2_dma_sg_alloc_compacted(buf, gfp_flags);
129 if (ret)
130 goto fail_pages_alloc;
131
132 ret = sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
133 buf->num_pages, 0, size, GFP_KERNEL);
134 if (ret)
135 goto fail_table_alloc;
136
137
138 buf->dev = get_device(dev);
139
140 sgt = &buf->sg_table;
141
142
143
144
145 sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
146 buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
147 if (!sgt->nents)
148 goto fail_map;
149
150 buf->handler.refcount = &buf->refcount;
151 buf->handler.put = vb2_dma_sg_put;
152 buf->handler.arg = buf;
153
154 refcount_set(&buf->refcount, 1);
155
156 dprintk(1, "%s: Allocated buffer of %d pages\n",
157 __func__, buf->num_pages);
158 return buf;
159
160fail_map:
161 put_device(buf->dev);
162 sg_free_table(buf->dma_sgt);
163fail_table_alloc:
164 num_pages = buf->num_pages;
165 while (num_pages--)
166 __free_page(buf->pages[num_pages]);
167fail_pages_alloc:
168 kvfree(buf->pages);
169fail_pages_array_alloc:
170 kfree(buf);
171 return ERR_PTR(-ENOMEM);
172}
173
174static void vb2_dma_sg_put(void *buf_priv)
175{
176 struct vb2_dma_sg_buf *buf = buf_priv;
177 struct sg_table *sgt = &buf->sg_table;
178 int i = buf->num_pages;
179
180 if (refcount_dec_and_test(&buf->refcount)) {
181 dprintk(1, "%s: Freeing buffer of %d pages\n", __func__,
182 buf->num_pages);
183 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
184 buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
185 if (buf->vaddr)
186 vm_unmap_ram(buf->vaddr, buf->num_pages);
187 sg_free_table(buf->dma_sgt);
188 while (--i >= 0)
189 __free_page(buf->pages[i]);
190 kvfree(buf->pages);
191 put_device(buf->dev);
192 kfree(buf);
193 }
194}
195
196static void vb2_dma_sg_prepare(void *buf_priv)
197{
198 struct vb2_dma_sg_buf *buf = buf_priv;
199 struct sg_table *sgt = buf->dma_sgt;
200
201
202 if (buf->db_attach)
203 return;
204
205 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->orig_nents,
206 buf->dma_dir);
207}
208
209static void vb2_dma_sg_finish(void *buf_priv)
210{
211 struct vb2_dma_sg_buf *buf = buf_priv;
212 struct sg_table *sgt = buf->dma_sgt;
213
214
215 if (buf->db_attach)
216 return;
217
218 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
219}
220
221static void *vb2_dma_sg_get_userptr(struct device *dev, unsigned long vaddr,
222 unsigned long size,
223 enum dma_data_direction dma_dir)
224{
225 struct vb2_dma_sg_buf *buf;
226 struct sg_table *sgt;
227 struct frame_vector *vec;
228
229 if (WARN_ON(!dev))
230 return ERR_PTR(-EINVAL);
231
232 buf = kzalloc(sizeof *buf, GFP_KERNEL);
233 if (!buf)
234 return ERR_PTR(-ENOMEM);
235
236 buf->vaddr = NULL;
237 buf->dev = dev;
238 buf->dma_dir = dma_dir;
239 buf->offset = vaddr & ~PAGE_MASK;
240 buf->size = size;
241 buf->dma_sgt = &buf->sg_table;
242 vec = vb2_create_framevec(vaddr, size, dma_dir == DMA_FROM_DEVICE ||
243 dma_dir == DMA_BIDIRECTIONAL);
244 if (IS_ERR(vec))
245 goto userptr_fail_pfnvec;
246 buf->vec = vec;
247
248 buf->pages = frame_vector_pages(vec);
249 if (IS_ERR(buf->pages))
250 goto userptr_fail_sgtable;
251 buf->num_pages = frame_vector_count(vec);
252
253 if (sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
254 buf->num_pages, buf->offset, size, 0))
255 goto userptr_fail_sgtable;
256
257 sgt = &buf->sg_table;
258
259
260
261
262 sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
263 buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
264 if (!sgt->nents)
265 goto userptr_fail_map;
266
267 return buf;
268
269userptr_fail_map:
270 sg_free_table(&buf->sg_table);
271userptr_fail_sgtable:
272 vb2_destroy_framevec(vec);
273userptr_fail_pfnvec:
274 kfree(buf);
275 return ERR_PTR(-ENOMEM);
276}
277
278
279
280
281
282static void vb2_dma_sg_put_userptr(void *buf_priv)
283{
284 struct vb2_dma_sg_buf *buf = buf_priv;
285 struct sg_table *sgt = &buf->sg_table;
286 int i = buf->num_pages;
287
288 dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
289 __func__, buf->num_pages);
290 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir,
291 DMA_ATTR_SKIP_CPU_SYNC);
292 if (buf->vaddr)
293 vm_unmap_ram(buf->vaddr, buf->num_pages);
294 sg_free_table(buf->dma_sgt);
295 if (buf->dma_dir == DMA_FROM_DEVICE ||
296 buf->dma_dir == DMA_BIDIRECTIONAL)
297 while (--i >= 0)
298 set_page_dirty_lock(buf->pages[i]);
299 vb2_destroy_framevec(buf->vec);
300 kfree(buf);
301}
302
303static void *vb2_dma_sg_vaddr(void *buf_priv)
304{
305 struct vb2_dma_sg_buf *buf = buf_priv;
306
307 BUG_ON(!buf);
308
309 if (!buf->vaddr) {
310 if (buf->db_attach)
311 buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
312 else
313 buf->vaddr = vm_map_ram(buf->pages,
314 buf->num_pages, -1, PAGE_KERNEL);
315 }
316
317
318 return buf->vaddr ? buf->vaddr + buf->offset : NULL;
319}
320
321static unsigned int vb2_dma_sg_num_users(void *buf_priv)
322{
323 struct vb2_dma_sg_buf *buf = buf_priv;
324
325 return refcount_read(&buf->refcount);
326}
327
328static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
329{
330 struct vb2_dma_sg_buf *buf = buf_priv;
331 unsigned long uaddr = vma->vm_start;
332 unsigned long usize = vma->vm_end - vma->vm_start;
333 int i = 0;
334
335 if (!buf) {
336 printk(KERN_ERR "No memory to map\n");
337 return -EINVAL;
338 }
339
340 do {
341 int ret;
342
343 ret = vm_insert_page(vma, uaddr, buf->pages[i++]);
344 if (ret) {
345 printk(KERN_ERR "Remapping memory, error: %d\n", ret);
346 return ret;
347 }
348
349 uaddr += PAGE_SIZE;
350 usize -= PAGE_SIZE;
351 } while (usize > 0);
352
353
354
355
356
357 vma->vm_private_data = &buf->handler;
358 vma->vm_ops = &vb2_common_vm_ops;
359
360 vma->vm_ops->open(vma);
361
362 return 0;
363}
364
365
366
367
368
369struct vb2_dma_sg_attachment {
370 struct sg_table sgt;
371 enum dma_data_direction dma_dir;
372};
373
374static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf *dbuf,
375 struct dma_buf_attachment *dbuf_attach)
376{
377 struct vb2_dma_sg_attachment *attach;
378 unsigned int i;
379 struct scatterlist *rd, *wr;
380 struct sg_table *sgt;
381 struct vb2_dma_sg_buf *buf = dbuf->priv;
382 int ret;
383
384 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
385 if (!attach)
386 return -ENOMEM;
387
388 sgt = &attach->sgt;
389
390
391
392 ret = sg_alloc_table(sgt, buf->dma_sgt->orig_nents, GFP_KERNEL);
393 if (ret) {
394 kfree(attach);
395 return -ENOMEM;
396 }
397
398 rd = buf->dma_sgt->sgl;
399 wr = sgt->sgl;
400 for (i = 0; i < sgt->orig_nents; ++i) {
401 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
402 rd = sg_next(rd);
403 wr = sg_next(wr);
404 }
405
406 attach->dma_dir = DMA_NONE;
407 dbuf_attach->priv = attach;
408
409 return 0;
410}
411
412static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf,
413 struct dma_buf_attachment *db_attach)
414{
415 struct vb2_dma_sg_attachment *attach = db_attach->priv;
416 struct sg_table *sgt;
417
418 if (!attach)
419 return;
420
421 sgt = &attach->sgt;
422
423
424 if (attach->dma_dir != DMA_NONE)
425 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
426 attach->dma_dir);
427 sg_free_table(sgt);
428 kfree(attach);
429 db_attach->priv = NULL;
430}
431
432static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
433 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
434{
435 struct vb2_dma_sg_attachment *attach = db_attach->priv;
436
437 struct mutex *lock = &db_attach->dmabuf->lock;
438 struct sg_table *sgt;
439
440 mutex_lock(lock);
441
442 sgt = &attach->sgt;
443
444 if (attach->dma_dir == dma_dir) {
445 mutex_unlock(lock);
446 return sgt;
447 }
448
449
450 if (attach->dma_dir != DMA_NONE) {
451 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
452 attach->dma_dir);
453 attach->dma_dir = DMA_NONE;
454 }
455
456
457 sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
458 dma_dir);
459 if (!sgt->nents) {
460 pr_err("failed to map scatterlist\n");
461 mutex_unlock(lock);
462 return ERR_PTR(-EIO);
463 }
464
465 attach->dma_dir = dma_dir;
466
467 mutex_unlock(lock);
468
469 return sgt;
470}
471
472static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
473 struct sg_table *sgt, enum dma_data_direction dma_dir)
474{
475
476}
477
478static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf *dbuf)
479{
480
481 vb2_dma_sg_put(dbuf->priv);
482}
483
484static void *vb2_dma_sg_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
485{
486 struct vb2_dma_sg_buf *buf = dbuf->priv;
487
488 return buf->vaddr ? buf->vaddr + pgnum * PAGE_SIZE : NULL;
489}
490
491static void *vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf *dbuf)
492{
493 struct vb2_dma_sg_buf *buf = dbuf->priv;
494
495 return vb2_dma_sg_vaddr(buf);
496}
497
498static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf *dbuf,
499 struct vm_area_struct *vma)
500{
501 return vb2_dma_sg_mmap(dbuf->priv, vma);
502}
503
504static const struct dma_buf_ops vb2_dma_sg_dmabuf_ops = {
505 .attach = vb2_dma_sg_dmabuf_ops_attach,
506 .detach = vb2_dma_sg_dmabuf_ops_detach,
507 .map_dma_buf = vb2_dma_sg_dmabuf_ops_map,
508 .unmap_dma_buf = vb2_dma_sg_dmabuf_ops_unmap,
509 .map = vb2_dma_sg_dmabuf_ops_kmap,
510 .vmap = vb2_dma_sg_dmabuf_ops_vmap,
511 .mmap = vb2_dma_sg_dmabuf_ops_mmap,
512 .release = vb2_dma_sg_dmabuf_ops_release,
513};
514
515static struct dma_buf *vb2_dma_sg_get_dmabuf(void *buf_priv, unsigned long flags)
516{
517 struct vb2_dma_sg_buf *buf = buf_priv;
518 struct dma_buf *dbuf;
519 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
520
521 exp_info.ops = &vb2_dma_sg_dmabuf_ops;
522 exp_info.size = buf->size;
523 exp_info.flags = flags;
524 exp_info.priv = buf;
525
526 if (WARN_ON(!buf->dma_sgt))
527 return NULL;
528
529 dbuf = dma_buf_export(&exp_info);
530 if (IS_ERR(dbuf))
531 return NULL;
532
533
534 refcount_inc(&buf->refcount);
535
536 return dbuf;
537}
538
539
540
541
542
543static int vb2_dma_sg_map_dmabuf(void *mem_priv)
544{
545 struct vb2_dma_sg_buf *buf = mem_priv;
546 struct sg_table *sgt;
547
548 if (WARN_ON(!buf->db_attach)) {
549 pr_err("trying to pin a non attached buffer\n");
550 return -EINVAL;
551 }
552
553 if (WARN_ON(buf->dma_sgt)) {
554 pr_err("dmabuf buffer is already pinned\n");
555 return 0;
556 }
557
558
559 sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
560 if (IS_ERR(sgt)) {
561 pr_err("Error getting dmabuf scatterlist\n");
562 return -EINVAL;
563 }
564
565 buf->dma_sgt = sgt;
566 buf->vaddr = NULL;
567
568 return 0;
569}
570
571static void vb2_dma_sg_unmap_dmabuf(void *mem_priv)
572{
573 struct vb2_dma_sg_buf *buf = mem_priv;
574 struct sg_table *sgt = buf->dma_sgt;
575
576 if (WARN_ON(!buf->db_attach)) {
577 pr_err("trying to unpin a not attached buffer\n");
578 return;
579 }
580
581 if (WARN_ON(!sgt)) {
582 pr_err("dmabuf buffer is already unpinned\n");
583 return;
584 }
585
586 if (buf->vaddr) {
587 dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
588 buf->vaddr = NULL;
589 }
590 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
591
592 buf->dma_sgt = NULL;
593}
594
595static void vb2_dma_sg_detach_dmabuf(void *mem_priv)
596{
597 struct vb2_dma_sg_buf *buf = mem_priv;
598
599
600 if (WARN_ON(buf->dma_sgt))
601 vb2_dma_sg_unmap_dmabuf(buf);
602
603
604 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
605 kfree(buf);
606}
607
608static void *vb2_dma_sg_attach_dmabuf(struct device *dev, struct dma_buf *dbuf,
609 unsigned long size, enum dma_data_direction dma_dir)
610{
611 struct vb2_dma_sg_buf *buf;
612 struct dma_buf_attachment *dba;
613
614 if (WARN_ON(!dev))
615 return ERR_PTR(-EINVAL);
616
617 if (dbuf->size < size)
618 return ERR_PTR(-EFAULT);
619
620 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
621 if (!buf)
622 return ERR_PTR(-ENOMEM);
623
624 buf->dev = dev;
625
626 dba = dma_buf_attach(dbuf, buf->dev);
627 if (IS_ERR(dba)) {
628 pr_err("failed to attach dmabuf\n");
629 kfree(buf);
630 return dba;
631 }
632
633 buf->dma_dir = dma_dir;
634 buf->size = size;
635 buf->db_attach = dba;
636
637 return buf;
638}
639
640static void *vb2_dma_sg_cookie(void *buf_priv)
641{
642 struct vb2_dma_sg_buf *buf = buf_priv;
643
644 return buf->dma_sgt;
645}
646
647const struct vb2_mem_ops vb2_dma_sg_memops = {
648 .alloc = vb2_dma_sg_alloc,
649 .put = vb2_dma_sg_put,
650 .get_userptr = vb2_dma_sg_get_userptr,
651 .put_userptr = vb2_dma_sg_put_userptr,
652 .prepare = vb2_dma_sg_prepare,
653 .finish = vb2_dma_sg_finish,
654 .vaddr = vb2_dma_sg_vaddr,
655 .mmap = vb2_dma_sg_mmap,
656 .num_users = vb2_dma_sg_num_users,
657 .get_dmabuf = vb2_dma_sg_get_dmabuf,
658 .map_dmabuf = vb2_dma_sg_map_dmabuf,
659 .unmap_dmabuf = vb2_dma_sg_unmap_dmabuf,
660 .attach_dmabuf = vb2_dma_sg_attach_dmabuf,
661 .detach_dmabuf = vb2_dma_sg_detach_dmabuf,
662 .cookie = vb2_dma_sg_cookie,
663};
664EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
665
666MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
667MODULE_AUTHOR("Andrzej Pietrasiewicz");
668MODULE_LICENSE("GPL");
669