Merge tag 'iommu-fixes-v4.2' of git://git.kernel.org/pub/scm/linux/kernel/git/joro...
[deliverable/linux.git] / drivers / media / v4l2-core / videobuf2-dma-contig.c
CommitLineData
1a758d4e
PO
1/*
2 * videobuf2-dma-contig.c - DMA contig memory allocator for videobuf2
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 *
95072084 6 * Author: Pawel Osciak <pawel@osciak.com>
1a758d4e
PO
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation.
11 */
12
8c417d03 13#include <linux/dma-buf.h>
1a758d4e 14#include <linux/module.h>
e15dab75
TS
15#include <linux/scatterlist.h>
16#include <linux/sched.h>
1a758d4e
PO
17#include <linux/slab.h>
18#include <linux/dma-mapping.h>
19
20#include <media/videobuf2-core.h>
d0df3c38 21#include <media/videobuf2-dma-contig.h>
1a758d4e
PO
22#include <media/videobuf2-memops.h>
23
24struct vb2_dc_conf {
25 struct device *dev;
26};
27
28struct vb2_dc_buf {
72f86bff 29 struct device *dev;
1a758d4e 30 void *vaddr;
1a758d4e 31 unsigned long size;
40d8b766 32 dma_addr_t dma_addr;
e15dab75
TS
33 enum dma_data_direction dma_dir;
34 struct sg_table *dma_sgt;
40d8b766
LP
35
36 /* MMAP related */
1a758d4e 37 struct vb2_vmarea_handler handler;
40d8b766 38 atomic_t refcount;
9ef2cbeb 39 struct sg_table *sgt_base;
40d8b766
LP
40
41 /* USERPTR related */
42 struct vm_area_struct *vma;
8c417d03
SS
43
44 /* DMABUF related */
45 struct dma_buf_attachment *db_attach;
1a758d4e
PO
46};
47
e15dab75
TS
48/*********************************************/
49/* scatterlist table functions */
50/*********************************************/
51
52
53static void vb2_dc_sgt_foreach_page(struct sg_table *sgt,
54 void (*cb)(struct page *pg))
55{
56 struct scatterlist *s;
57 unsigned int i;
58
59 for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
60 struct page *page = sg_page(s);
61 unsigned int n_pages = PAGE_ALIGN(s->offset + s->length)
62 >> PAGE_SHIFT;
63 unsigned int j;
64
65 for (j = 0; j < n_pages; ++j, ++page)
66 cb(page);
67 }
68}
69
70static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
71{
72 struct scatterlist *s;
73 dma_addr_t expected = sg_dma_address(sgt->sgl);
74 unsigned int i;
75 unsigned long size = 0;
76
77 for_each_sg(sgt->sgl, s, sgt->nents, i) {
78 if (sg_dma_address(s) != expected)
79 break;
80 expected = sg_dma_address(s) + sg_dma_len(s);
81 size += sg_dma_len(s);
82 }
83 return size;
84}
85
40d8b766
LP
86/*********************************************/
87/* callbacks for all buffers */
88/*********************************************/
89
90static void *vb2_dc_cookie(void *buf_priv)
91{
92 struct vb2_dc_buf *buf = buf_priv;
93
94 return &buf->dma_addr;
95}
96
97static void *vb2_dc_vaddr(void *buf_priv)
98{
99 struct vb2_dc_buf *buf = buf_priv;
100
6bbd4fec
PZ
101 if (!buf->vaddr && buf->db_attach)
102 buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
103
40d8b766
LP
104 return buf->vaddr;
105}
106
107static unsigned int vb2_dc_num_users(void *buf_priv)
108{
109 struct vb2_dc_buf *buf = buf_priv;
110
111 return atomic_read(&buf->refcount);
112}
113
199d101e
MS
114static void vb2_dc_prepare(void *buf_priv)
115{
116 struct vb2_dc_buf *buf = buf_priv;
117 struct sg_table *sgt = buf->dma_sgt;
118
8c417d03
SS
119 /* DMABUF exporter will flush the cache for us */
120 if (!sgt || buf->db_attach)
199d101e
MS
121 return;
122
123 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
124}
125
126static void vb2_dc_finish(void *buf_priv)
127{
128 struct vb2_dc_buf *buf = buf_priv;
129 struct sg_table *sgt = buf->dma_sgt;
130
8c417d03
SS
131 /* DMABUF exporter will flush the cache for us */
132 if (!sgt || buf->db_attach)
199d101e
MS
133 return;
134
135 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
136}
137
40d8b766
LP
138/*********************************************/
139/* callbacks for MMAP buffers */
140/*********************************************/
141
142static void vb2_dc_put(void *buf_priv)
143{
144 struct vb2_dc_buf *buf = buf_priv;
145
146 if (!atomic_dec_and_test(&buf->refcount))
147 return;
148
9ef2cbeb
TS
149 if (buf->sgt_base) {
150 sg_free_table(buf->sgt_base);
151 kfree(buf->sgt_base);
152 }
40d8b766 153 dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr);
67a5d0ce 154 put_device(buf->dev);
40d8b766
LP
155 kfree(buf);
156}
1a758d4e 157
d935c57e
HV
158static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size,
159 enum dma_data_direction dma_dir, gfp_t gfp_flags)
1a758d4e
PO
160{
161 struct vb2_dc_conf *conf = alloc_ctx;
72f86bff 162 struct device *dev = conf->dev;
1a758d4e
PO
163 struct vb2_dc_buf *buf;
164
165 buf = kzalloc(sizeof *buf, GFP_KERNEL);
166 if (!buf)
167 return ERR_PTR(-ENOMEM);
168
b6ba2057
HV
169 buf->vaddr = dma_alloc_coherent(dev, size, &buf->dma_addr,
170 GFP_KERNEL | gfp_flags);
1a758d4e 171 if (!buf->vaddr) {
72f86bff 172 dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size);
1a758d4e
PO
173 kfree(buf);
174 return ERR_PTR(-ENOMEM);
175 }
176
67a5d0ce
TS
177 /* Prevent the device from being released while the buffer is used */
178 buf->dev = get_device(dev);
1a758d4e 179 buf->size = size;
d935c57e 180 buf->dma_dir = dma_dir;
1a758d4e
PO
181
182 buf->handler.refcount = &buf->refcount;
f7f129ce 183 buf->handler.put = vb2_dc_put;
1a758d4e
PO
184 buf->handler.arg = buf;
185
186 atomic_inc(&buf->refcount);
187
188 return buf;
189}
190
f7f129ce 191static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
1a758d4e
PO
192{
193 struct vb2_dc_buf *buf = buf_priv;
c60520fa 194 int ret;
1a758d4e
PO
195
196 if (!buf) {
197 printk(KERN_ERR "No buffer to map\n");
198 return -EINVAL;
199 }
200
c60520fa
MS
201 /*
202 * dma_mmap_* uses vm_pgoff as in-buffer offset, but we want to
203 * map whole buffer
204 */
205 vma->vm_pgoff = 0;
206
207 ret = dma_mmap_coherent(buf->dev, vma, buf->vaddr,
208 buf->dma_addr, buf->size);
209
210 if (ret) {
211 pr_err("Remapping memory failed, error: %d\n", ret);
212 return ret;
213 }
214
215 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
216 vma->vm_private_data = &buf->handler;
217 vma->vm_ops = &vb2_common_vm_ops;
218
219 vma->vm_ops->open(vma);
220
221 pr_debug("%s: mapped dma addr 0x%08lx at 0x%08lx, size %ld\n",
222 __func__, (unsigned long)buf->dma_addr, vma->vm_start,
223 buf->size);
224
225 return 0;
1a758d4e
PO
226}
227
9ef2cbeb
TS
228/*********************************************/
229/* DMABUF ops for exporters */
230/*********************************************/
231
232struct vb2_dc_attachment {
233 struct sg_table sgt;
cd474037 234 enum dma_data_direction dma_dir;
9ef2cbeb
TS
235};
236
237static int vb2_dc_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
238 struct dma_buf_attachment *dbuf_attach)
239{
240 struct vb2_dc_attachment *attach;
241 unsigned int i;
242 struct scatterlist *rd, *wr;
243 struct sg_table *sgt;
244 struct vb2_dc_buf *buf = dbuf->priv;
245 int ret;
246
247 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
248 if (!attach)
249 return -ENOMEM;
250
251 sgt = &attach->sgt;
252 /* Copy the buf->base_sgt scatter list to the attachment, as we can't
253 * map the same scatter list to multiple attachments at the same time.
254 */
255 ret = sg_alloc_table(sgt, buf->sgt_base->orig_nents, GFP_KERNEL);
256 if (ret) {
257 kfree(attach);
258 return -ENOMEM;
259 }
260
261 rd = buf->sgt_base->sgl;
262 wr = sgt->sgl;
263 for (i = 0; i < sgt->orig_nents; ++i) {
264 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
265 rd = sg_next(rd);
266 wr = sg_next(wr);
267 }
268
cd474037 269 attach->dma_dir = DMA_NONE;
9ef2cbeb
TS
270 dbuf_attach->priv = attach;
271
272 return 0;
273}
274
275static void vb2_dc_dmabuf_ops_detach(struct dma_buf *dbuf,
276 struct dma_buf_attachment *db_attach)
277{
278 struct vb2_dc_attachment *attach = db_attach->priv;
279 struct sg_table *sgt;
280
281 if (!attach)
282 return;
283
284 sgt = &attach->sgt;
285
286 /* release the scatterlist cache */
cd474037 287 if (attach->dma_dir != DMA_NONE)
9ef2cbeb 288 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
cd474037 289 attach->dma_dir);
9ef2cbeb
TS
290 sg_free_table(sgt);
291 kfree(attach);
292 db_attach->priv = NULL;
293}
294
295static struct sg_table *vb2_dc_dmabuf_ops_map(
cd474037 296 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
9ef2cbeb
TS
297{
298 struct vb2_dc_attachment *attach = db_attach->priv;
299 /* stealing dmabuf mutex to serialize map/unmap operations */
300 struct mutex *lock = &db_attach->dmabuf->lock;
301 struct sg_table *sgt;
9ef2cbeb
TS
302
303 mutex_lock(lock);
304
305 sgt = &attach->sgt;
306 /* return previously mapped sg table */
cd474037 307 if (attach->dma_dir == dma_dir) {
9ef2cbeb
TS
308 mutex_unlock(lock);
309 return sgt;
310 }
311
312 /* release any previous cache */
cd474037 313 if (attach->dma_dir != DMA_NONE) {
9ef2cbeb 314 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
cd474037
HV
315 attach->dma_dir);
316 attach->dma_dir = DMA_NONE;
9ef2cbeb
TS
317 }
318
319 /* mapping to the client with new direction */
60a47192
RR
320 sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
321 dma_dir);
322 if (!sgt->nents) {
9ef2cbeb
TS
323 pr_err("failed to map scatterlist\n");
324 mutex_unlock(lock);
325 return ERR_PTR(-EIO);
326 }
327
cd474037 328 attach->dma_dir = dma_dir;
9ef2cbeb
TS
329
330 mutex_unlock(lock);
331
332 return sgt;
333}
334
335static void vb2_dc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
cd474037 336 struct sg_table *sgt, enum dma_data_direction dma_dir)
9ef2cbeb
TS
337{
338 /* nothing to be done here */
339}
340
341static void vb2_dc_dmabuf_ops_release(struct dma_buf *dbuf)
342{
343 /* drop reference obtained in vb2_dc_get_dmabuf */
344 vb2_dc_put(dbuf->priv);
345}
346
347static void *vb2_dc_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
348{
349 struct vb2_dc_buf *buf = dbuf->priv;
350
351 return buf->vaddr + pgnum * PAGE_SIZE;
352}
353
354static void *vb2_dc_dmabuf_ops_vmap(struct dma_buf *dbuf)
355{
356 struct vb2_dc_buf *buf = dbuf->priv;
357
358 return buf->vaddr;
359}
360
361static int vb2_dc_dmabuf_ops_mmap(struct dma_buf *dbuf,
362 struct vm_area_struct *vma)
363{
364 return vb2_dc_mmap(dbuf->priv, vma);
365}
366
367static struct dma_buf_ops vb2_dc_dmabuf_ops = {
368 .attach = vb2_dc_dmabuf_ops_attach,
369 .detach = vb2_dc_dmabuf_ops_detach,
370 .map_dma_buf = vb2_dc_dmabuf_ops_map,
371 .unmap_dma_buf = vb2_dc_dmabuf_ops_unmap,
372 .kmap = vb2_dc_dmabuf_ops_kmap,
373 .kmap_atomic = vb2_dc_dmabuf_ops_kmap,
374 .vmap = vb2_dc_dmabuf_ops_vmap,
375 .mmap = vb2_dc_dmabuf_ops_mmap,
376 .release = vb2_dc_dmabuf_ops_release,
377};
378
379static struct sg_table *vb2_dc_get_base_sgt(struct vb2_dc_buf *buf)
380{
381 int ret;
382 struct sg_table *sgt;
383
384 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
385 if (!sgt) {
386 dev_err(buf->dev, "failed to alloc sg table\n");
387 return NULL;
388 }
389
390 ret = dma_get_sgtable(buf->dev, sgt, buf->vaddr, buf->dma_addr,
391 buf->size);
392 if (ret < 0) {
393 dev_err(buf->dev, "failed to get scatterlist from DMA API\n");
394 kfree(sgt);
395 return NULL;
396 }
397
398 return sgt;
399}
400
c1b96a23 401static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv, unsigned long flags)
9ef2cbeb
TS
402{
403 struct vb2_dc_buf *buf = buf_priv;
404 struct dma_buf *dbuf;
d8fbe341
SS
405 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
406
407 exp_info.ops = &vb2_dc_dmabuf_ops;
408 exp_info.size = buf->size;
409 exp_info.flags = flags;
410 exp_info.priv = buf;
9ef2cbeb
TS
411
412 if (!buf->sgt_base)
413 buf->sgt_base = vb2_dc_get_base_sgt(buf);
414
415 if (WARN_ON(!buf->sgt_base))
416 return NULL;
417
d8fbe341 418 dbuf = dma_buf_export(&exp_info);
9ef2cbeb
TS
419 if (IS_ERR(dbuf))
420 return NULL;
421
422 /* dmabuf keeps reference to vb2 buffer */
423 atomic_inc(&buf->refcount);
424
425 return dbuf;
426}
427
40d8b766
LP
428/*********************************************/
429/* callbacks for USERPTR buffers */
430/*********************************************/
431
e15dab75
TS
432static inline int vma_is_io(struct vm_area_struct *vma)
433{
434 return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
435}
436
774d2301
MS
437static int vb2_dc_get_user_pfn(unsigned long start, int n_pages,
438 struct vm_area_struct *vma, unsigned long *res)
439{
440 unsigned long pfn, start_pfn, prev_pfn;
441 unsigned int i;
442 int ret;
443
444 if (!vma_is_io(vma))
445 return -EFAULT;
446
447 ret = follow_pfn(vma, start, &pfn);
448 if (ret)
449 return ret;
450
451 start_pfn = pfn;
452 start += PAGE_SIZE;
453
454 for (i = 1; i < n_pages; ++i, start += PAGE_SIZE) {
455 prev_pfn = pfn;
456 ret = follow_pfn(vma, start, &pfn);
457
458 if (ret) {
459 pr_err("no page for address %lu\n", start);
460 return ret;
461 }
462 if (pfn != prev_pfn + 1)
463 return -EINVAL;
464 }
465
466 *res = start_pfn;
467 return 0;
468}
469
e15dab75 470static int vb2_dc_get_user_pages(unsigned long start, struct page **pages,
cd474037
HV
471 int n_pages, struct vm_area_struct *vma,
472 enum dma_data_direction dma_dir)
e15dab75
TS
473{
474 if (vma_is_io(vma)) {
475 unsigned int i;
476
477 for (i = 0; i < n_pages; ++i, start += PAGE_SIZE) {
478 unsigned long pfn;
479 int ret = follow_pfn(vma, start, &pfn);
480
774d2301
MS
481 if (!pfn_valid(pfn))
482 return -EINVAL;
483
e15dab75
TS
484 if (ret) {
485 pr_err("no page for address %lu\n", start);
486 return ret;
487 }
488 pages[i] = pfn_to_page(pfn);
489 }
490 } else {
491 int n;
492
493 n = get_user_pages(current, current->mm, start & PAGE_MASK,
cd474037 494 n_pages, dma_dir == DMA_FROM_DEVICE, 1, pages, NULL);
e15dab75
TS
495 /* negative error means that no page was pinned */
496 n = max(n, 0);
497 if (n != n_pages) {
498 pr_err("got only %d of %d user pages\n", n, n_pages);
499 while (n)
500 put_page(pages[--n]);
501 return -EFAULT;
502 }
503 }
504
505 return 0;
506}
507
508static void vb2_dc_put_dirty_page(struct page *page)
509{
510 set_page_dirty_lock(page);
511 put_page(page);
512}
513
514static void vb2_dc_put_userptr(void *buf_priv)
515{
516 struct vb2_dc_buf *buf = buf_priv;
517 struct sg_table *sgt = buf->dma_sgt;
518
774d2301 519 if (sgt) {
251a79f8
HV
520 DEFINE_DMA_ATTRS(attrs);
521
522 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
523 /*
524 * No need to sync to CPU, it's already synced to the CPU
525 * since the finish() memop will have been called before this.
526 */
527 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
528 buf->dma_dir, &attrs);
774d2301
MS
529 if (!vma_is_io(buf->vma))
530 vb2_dc_sgt_foreach_page(sgt, vb2_dc_put_dirty_page);
e15dab75 531
774d2301
MS
532 sg_free_table(sgt);
533 kfree(sgt);
534 }
e15dab75
TS
535 vb2_put_vma(buf->vma);
536 kfree(buf);
537}
538
774d2301
MS
539/*
540 * For some kind of reserved memory there might be no struct page available,
541 * so all that can be done to support such 'pages' is to try to convert
542 * pfn to dma address or at the last resort just assume that
543 * dma address == physical address (like it has been assumed in earlier version
544 * of videobuf2-dma-contig
545 */
546
547#ifdef __arch_pfn_to_dma
548static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
549{
550 return (dma_addr_t)__arch_pfn_to_dma(dev, pfn);
551}
552#elif defined(__pfn_to_bus)
553static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
554{
555 return (dma_addr_t)__pfn_to_bus(pfn);
556}
557#elif defined(__pfn_to_phys)
558static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
559{
560 return (dma_addr_t)__pfn_to_phys(pfn);
561}
562#else
563static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
564{
565 /* really, we cannot do anything better at this point */
566 return (dma_addr_t)(pfn) << PAGE_SHIFT;
567}
568#endif
569
f7f129ce 570static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
cd474037 571 unsigned long size, enum dma_data_direction dma_dir)
1a758d4e 572{
e15dab75 573 struct vb2_dc_conf *conf = alloc_ctx;
1a758d4e 574 struct vb2_dc_buf *buf;
e15dab75
TS
575 unsigned long start;
576 unsigned long end;
577 unsigned long offset;
578 struct page **pages;
579 int n_pages;
580 int ret = 0;
1a758d4e 581 struct vm_area_struct *vma;
e15dab75
TS
582 struct sg_table *sgt;
583 unsigned long contig_size;
d81e870d 584 unsigned long dma_align = dma_get_cache_alignment();
251a79f8
HV
585 DEFINE_DMA_ATTRS(attrs);
586
587 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
d81e870d
MS
588
589 /* Only cache aligned DMA transfers are reliable */
590 if (!IS_ALIGNED(vaddr | size, dma_align)) {
591 pr_debug("user data must be aligned to %lu bytes\n", dma_align);
592 return ERR_PTR(-EINVAL);
593 }
594
595 if (!size) {
596 pr_debug("size is zero\n");
597 return ERR_PTR(-EINVAL);
598 }
1a758d4e
PO
599
600 buf = kzalloc(sizeof *buf, GFP_KERNEL);
601 if (!buf)
602 return ERR_PTR(-ENOMEM);
603
e15dab75 604 buf->dev = conf->dev;
cd474037 605 buf->dma_dir = dma_dir;
e15dab75
TS
606
607 start = vaddr & PAGE_MASK;
608 offset = vaddr & ~PAGE_MASK;
609 end = PAGE_ALIGN(vaddr + size);
610 n_pages = (end - start) >> PAGE_SHIFT;
611
612 pages = kmalloc(n_pages * sizeof(pages[0]), GFP_KERNEL);
613 if (!pages) {
614 ret = -ENOMEM;
615 pr_err("failed to allocate pages table\n");
616 goto fail_buf;
617 }
618
619 /* current->mm->mmap_sem is taken by videobuf2 core */
620 vma = find_vma(current->mm, vaddr);
621 if (!vma) {
622 pr_err("no vma for address %lu\n", vaddr);
623 ret = -EFAULT;
624 goto fail_pages;
625 }
626
627 if (vma->vm_end < vaddr + size) {
628 pr_err("vma at %lu is too small for %lu bytes\n", vaddr, size);
629 ret = -EFAULT;
630 goto fail_pages;
631 }
632
633 buf->vma = vb2_get_vma(vma);
634 if (!buf->vma) {
635 pr_err("failed to copy vma\n");
636 ret = -ENOMEM;
637 goto fail_pages;
638 }
639
640 /* extract page list from userspace mapping */
4879785e 641 ret = vb2_dc_get_user_pages(start, pages, n_pages, vma, dma_dir);
1a758d4e 642 if (ret) {
774d2301
MS
643 unsigned long pfn;
644 if (vb2_dc_get_user_pfn(start, n_pages, vma, &pfn) == 0) {
645 buf->dma_addr = vb2_dc_pfn_to_dma(buf->dev, pfn);
646 buf->size = size;
647 kfree(pages);
648 return buf;
649 }
650
e15dab75
TS
651 pr_err("failed to get user pages\n");
652 goto fail_vma;
653 }
654
655 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
656 if (!sgt) {
657 pr_err("failed to allocate sg table\n");
658 ret = -ENOMEM;
659 goto fail_get_user_pages;
660 }
661
662 ret = sg_alloc_table_from_pages(sgt, pages, n_pages,
663 offset, size, GFP_KERNEL);
664 if (ret) {
665 pr_err("failed to initialize sg table\n");
666 goto fail_sgt;
667 }
668
669 /* pages are no longer needed */
670 kfree(pages);
671 pages = NULL;
672
251a79f8
HV
673 /*
674 * No need to sync to the device, this will happen later when the
675 * prepare() memop is called.
676 */
677 sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
678 buf->dma_dir, &attrs);
e15dab75
TS
679 if (sgt->nents <= 0) {
680 pr_err("failed to map scatterlist\n");
681 ret = -EIO;
682 goto fail_sgt_init;
683 }
684
685 contig_size = vb2_dc_get_contiguous_size(sgt);
686 if (contig_size < size) {
687 pr_err("contiguous mapping is too small %lu/%lu\n",
688 contig_size, size);
689 ret = -EFAULT;
690 goto fail_map_sg;
1a758d4e
PO
691 }
692
e15dab75 693 buf->dma_addr = sg_dma_address(sgt->sgl);
1a758d4e 694 buf->size = size;
e15dab75 695 buf->dma_sgt = sgt;
1a758d4e
PO
696
697 return buf;
1a758d4e 698
e15dab75 699fail_map_sg:
251a79f8
HV
700 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
701 buf->dma_dir, &attrs);
1a758d4e 702
e15dab75
TS
703fail_sgt_init:
704 if (!vma_is_io(buf->vma))
705 vb2_dc_sgt_foreach_page(sgt, put_page);
706 sg_free_table(sgt);
707
708fail_sgt:
709 kfree(sgt);
1a758d4e 710
e15dab75
TS
711fail_get_user_pages:
712 if (pages && !vma_is_io(buf->vma))
713 while (n_pages)
714 put_page(pages[--n_pages]);
715
716fail_vma:
1a758d4e 717 vb2_put_vma(buf->vma);
e15dab75
TS
718
719fail_pages:
720 kfree(pages); /* kfree is NULL-proof */
721
722fail_buf:
1a758d4e 723 kfree(buf);
e15dab75
TS
724
725 return ERR_PTR(ret);
1a758d4e
PO
726}
727
8c417d03
SS
728/*********************************************/
729/* callbacks for DMABUF buffers */
730/*********************************************/
731
732static int vb2_dc_map_dmabuf(void *mem_priv)
733{
734 struct vb2_dc_buf *buf = mem_priv;
735 struct sg_table *sgt;
736 unsigned long contig_size;
737
738 if (WARN_ON(!buf->db_attach)) {
739 pr_err("trying to pin a non attached buffer\n");
740 return -EINVAL;
741 }
742
743 if (WARN_ON(buf->dma_sgt)) {
744 pr_err("dmabuf buffer is already pinned\n");
745 return 0;
746 }
747
748 /* get the associated scatterlist for this buffer */
749 sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
fee0c54e 750 if (IS_ERR(sgt)) {
8c417d03
SS
751 pr_err("Error getting dmabuf scatterlist\n");
752 return -EINVAL;
753 }
754
755 /* checking if dmabuf is big enough to store contiguous chunk */
756 contig_size = vb2_dc_get_contiguous_size(sgt);
757 if (contig_size < buf->size) {
758 pr_err("contiguous chunk is too small %lu/%lu b\n",
759 contig_size, buf->size);
760 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
761 return -EFAULT;
762 }
763
764 buf->dma_addr = sg_dma_address(sgt->sgl);
765 buf->dma_sgt = sgt;
6bbd4fec 766 buf->vaddr = NULL;
8c417d03
SS
767
768 return 0;
769}
770
771static void vb2_dc_unmap_dmabuf(void *mem_priv)
772{
773 struct vb2_dc_buf *buf = mem_priv;
774 struct sg_table *sgt = buf->dma_sgt;
775
776 if (WARN_ON(!buf->db_attach)) {
777 pr_err("trying to unpin a not attached buffer\n");
778 return;
779 }
780
781 if (WARN_ON(!sgt)) {
782 pr_err("dmabuf buffer is already unpinned\n");
783 return;
784 }
785
6bbd4fec
PZ
786 if (buf->vaddr) {
787 dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
788 buf->vaddr = NULL;
789 }
8c417d03
SS
790 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
791
792 buf->dma_addr = 0;
793 buf->dma_sgt = NULL;
794}
795
796static void vb2_dc_detach_dmabuf(void *mem_priv)
797{
798 struct vb2_dc_buf *buf = mem_priv;
799
800 /* if vb2 works correctly you should never detach mapped buffer */
801 if (WARN_ON(buf->dma_addr))
802 vb2_dc_unmap_dmabuf(buf);
803
804 /* detach this attachment */
805 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
806 kfree(buf);
807}
808
809static void *vb2_dc_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
cd474037 810 unsigned long size, enum dma_data_direction dma_dir)
8c417d03
SS
811{
812 struct vb2_dc_conf *conf = alloc_ctx;
813 struct vb2_dc_buf *buf;
814 struct dma_buf_attachment *dba;
815
816 if (dbuf->size < size)
817 return ERR_PTR(-EFAULT);
818
819 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
820 if (!buf)
821 return ERR_PTR(-ENOMEM);
822
823 buf->dev = conf->dev;
824 /* create attachment for the dmabuf with the user device */
825 dba = dma_buf_attach(dbuf, buf->dev);
826 if (IS_ERR(dba)) {
827 pr_err("failed to attach dmabuf\n");
828 kfree(buf);
829 return dba;
830 }
831
cd474037 832 buf->dma_dir = dma_dir;
8c417d03
SS
833 buf->size = size;
834 buf->db_attach = dba;
835
836 return buf;
837}
838
40d8b766
LP
839/*********************************************/
840/* DMA CONTIG exported functions */
841/*********************************************/
842
1a758d4e 843const struct vb2_mem_ops vb2_dma_contig_memops = {
f7f129ce
LP
844 .alloc = vb2_dc_alloc,
845 .put = vb2_dc_put,
9ef2cbeb 846 .get_dmabuf = vb2_dc_get_dmabuf,
f7f129ce
LP
847 .cookie = vb2_dc_cookie,
848 .vaddr = vb2_dc_vaddr,
849 .mmap = vb2_dc_mmap,
850 .get_userptr = vb2_dc_get_userptr,
851 .put_userptr = vb2_dc_put_userptr,
199d101e
MS
852 .prepare = vb2_dc_prepare,
853 .finish = vb2_dc_finish,
8c417d03
SS
854 .map_dmabuf = vb2_dc_map_dmabuf,
855 .unmap_dmabuf = vb2_dc_unmap_dmabuf,
856 .attach_dmabuf = vb2_dc_attach_dmabuf,
857 .detach_dmabuf = vb2_dc_detach_dmabuf,
f7f129ce 858 .num_users = vb2_dc_num_users,
1a758d4e
PO
859};
860EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
861
862void *vb2_dma_contig_init_ctx(struct device *dev)
863{
864 struct vb2_dc_conf *conf;
865
866 conf = kzalloc(sizeof *conf, GFP_KERNEL);
867 if (!conf)
868 return ERR_PTR(-ENOMEM);
869
870 conf->dev = dev;
871
872 return conf;
873}
874EXPORT_SYMBOL_GPL(vb2_dma_contig_init_ctx);
875
876void vb2_dma_contig_cleanup_ctx(void *alloc_ctx)
877{
e5ae8fa7
HV
878 if (!IS_ERR_OR_NULL(alloc_ctx))
879 kfree(alloc_ctx);
1a758d4e
PO
880}
881EXPORT_SYMBOL_GPL(vb2_dma_contig_cleanup_ctx);
882
883MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
95072084 884MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
1a758d4e 885MODULE_LICENSE("GPL");
This page took 0.309019 seconds and 5 git commands to generate.