Merge tag 'hsi-for-4.1' of git://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-hsi
[sfrench/cifs-2.6.git] / drivers / media / v4l2-core / videobuf2-dma-contig.c
1 /*
2  * videobuf2-dma-contig.c - DMA contig memory allocator for videobuf2
3  *
4  * Copyright (C) 2010 Samsung Electronics
5  *
6  * Author: Pawel Osciak <pawel@osciak.com>
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
13 #include <linux/dma-buf.h>
14 #include <linux/module.h>
15 #include <linux/scatterlist.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/dma-mapping.h>
19
20 #include <media/videobuf2-core.h>
21 #include <media/videobuf2-dma-contig.h>
22 #include <media/videobuf2-memops.h>
23
24 struct vb2_dc_conf {
25         struct device           *dev;
26 };
27
28 struct vb2_dc_buf {
29         struct device                   *dev;
30         void                            *vaddr;
31         unsigned long                   size;
32         dma_addr_t                      dma_addr;
33         enum dma_data_direction         dma_dir;
34         struct sg_table                 *dma_sgt;
35
36         /* MMAP related */
37         struct vb2_vmarea_handler       handler;
38         atomic_t                        refcount;
39         struct sg_table                 *sgt_base;
40
41         /* USERPTR related */
42         struct vm_area_struct           *vma;
43
44         /* DMABUF related */
45         struct dma_buf_attachment       *db_attach;
46 };
47
48 /*********************************************/
49 /*        scatterlist table functions        */
50 /*********************************************/
51
52
53 static 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
70 static 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
86 /*********************************************/
87 /*         callbacks for all buffers         */
88 /*********************************************/
89
90 static void *vb2_dc_cookie(void *buf_priv)
91 {
92         struct vb2_dc_buf *buf = buf_priv;
93
94         return &buf->dma_addr;
95 }
96
97 static void *vb2_dc_vaddr(void *buf_priv)
98 {
99         struct vb2_dc_buf *buf = buf_priv;
100
101         if (!buf->vaddr && buf->db_attach)
102                 buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
103
104         return buf->vaddr;
105 }
106
107 static 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
114 static 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
119         /* DMABUF exporter will flush the cache for us */
120         if (!sgt || buf->db_attach)
121                 return;
122
123         dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
124 }
125
126 static 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
131         /* DMABUF exporter will flush the cache for us */
132         if (!sgt || buf->db_attach)
133                 return;
134
135         dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
136 }
137
138 /*********************************************/
139 /*        callbacks for MMAP buffers         */
140 /*********************************************/
141
142 static 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
149         if (buf->sgt_base) {
150                 sg_free_table(buf->sgt_base);
151                 kfree(buf->sgt_base);
152         }
153         dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr);
154         put_device(buf->dev);
155         kfree(buf);
156 }
157
158 static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size,
159                           enum dma_data_direction dma_dir, gfp_t gfp_flags)
160 {
161         struct vb2_dc_conf *conf = alloc_ctx;
162         struct device *dev = conf->dev;
163         struct vb2_dc_buf *buf;
164
165         buf = kzalloc(sizeof *buf, GFP_KERNEL);
166         if (!buf)
167                 return ERR_PTR(-ENOMEM);
168
169         buf->vaddr = dma_alloc_coherent(dev, size, &buf->dma_addr,
170                                                 GFP_KERNEL | gfp_flags);
171         if (!buf->vaddr) {
172                 dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size);
173                 kfree(buf);
174                 return ERR_PTR(-ENOMEM);
175         }
176
177         /* Prevent the device from being released while the buffer is used */
178         buf->dev = get_device(dev);
179         buf->size = size;
180         buf->dma_dir = dma_dir;
181
182         buf->handler.refcount = &buf->refcount;
183         buf->handler.put = vb2_dc_put;
184         buf->handler.arg = buf;
185
186         atomic_inc(&buf->refcount);
187
188         return buf;
189 }
190
191 static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
192 {
193         struct vb2_dc_buf *buf = buf_priv;
194         int ret;
195
196         if (!buf) {
197                 printk(KERN_ERR "No buffer to map\n");
198                 return -EINVAL;
199         }
200
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;
226 }
227
228 /*********************************************/
229 /*         DMABUF ops for exporters          */
230 /*********************************************/
231
232 struct vb2_dc_attachment {
233         struct sg_table sgt;
234         enum dma_data_direction dma_dir;
235 };
236
237 static 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
269         attach->dma_dir = DMA_NONE;
270         dbuf_attach->priv = attach;
271
272         return 0;
273 }
274
275 static 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 */
287         if (attach->dma_dir != DMA_NONE)
288                 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
289                         attach->dma_dir);
290         sg_free_table(sgt);
291         kfree(attach);
292         db_attach->priv = NULL;
293 }
294
295 static struct sg_table *vb2_dc_dmabuf_ops_map(
296         struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
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;
302         int ret;
303
304         mutex_lock(lock);
305
306         sgt = &attach->sgt;
307         /* return previously mapped sg table */
308         if (attach->dma_dir == dma_dir) {
309                 mutex_unlock(lock);
310                 return sgt;
311         }
312
313         /* release any previous cache */
314         if (attach->dma_dir != DMA_NONE) {
315                 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
316                         attach->dma_dir);
317                 attach->dma_dir = DMA_NONE;
318         }
319
320         /* mapping to the client with new direction */
321         ret = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, dma_dir);
322         if (ret <= 0) {
323                 pr_err("failed to map scatterlist\n");
324                 mutex_unlock(lock);
325                 return ERR_PTR(-EIO);
326         }
327
328         attach->dma_dir = dma_dir;
329
330         mutex_unlock(lock);
331
332         return sgt;
333 }
334
335 static void vb2_dc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
336         struct sg_table *sgt, enum dma_data_direction dma_dir)
337 {
338         /* nothing to be done here */
339 }
340
341 static 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
347 static 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
354 static 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
361 static 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
367 static 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
379 static 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
401 static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv, unsigned long flags)
402 {
403         struct vb2_dc_buf *buf = buf_priv;
404         struct dma_buf *dbuf;
405
406         if (!buf->sgt_base)
407                 buf->sgt_base = vb2_dc_get_base_sgt(buf);
408
409         if (WARN_ON(!buf->sgt_base))
410                 return NULL;
411
412         dbuf = dma_buf_export(buf, &vb2_dc_dmabuf_ops, buf->size, flags, NULL);
413         if (IS_ERR(dbuf))
414                 return NULL;
415
416         /* dmabuf keeps reference to vb2 buffer */
417         atomic_inc(&buf->refcount);
418
419         return dbuf;
420 }
421
422 /*********************************************/
423 /*       callbacks for USERPTR buffers       */
424 /*********************************************/
425
426 static inline int vma_is_io(struct vm_area_struct *vma)
427 {
428         return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
429 }
430
431 static int vb2_dc_get_user_pfn(unsigned long start, int n_pages,
432         struct vm_area_struct *vma, unsigned long *res)
433 {
434         unsigned long pfn, start_pfn, prev_pfn;
435         unsigned int i;
436         int ret;
437
438         if (!vma_is_io(vma))
439                 return -EFAULT;
440
441         ret = follow_pfn(vma, start, &pfn);
442         if (ret)
443                 return ret;
444
445         start_pfn = pfn;
446         start += PAGE_SIZE;
447
448         for (i = 1; i < n_pages; ++i, start += PAGE_SIZE) {
449                 prev_pfn = pfn;
450                 ret = follow_pfn(vma, start, &pfn);
451
452                 if (ret) {
453                         pr_err("no page for address %lu\n", start);
454                         return ret;
455                 }
456                 if (pfn != prev_pfn + 1)
457                         return -EINVAL;
458         }
459
460         *res = start_pfn;
461         return 0;
462 }
463
464 static int vb2_dc_get_user_pages(unsigned long start, struct page **pages,
465         int n_pages, struct vm_area_struct *vma,
466         enum dma_data_direction dma_dir)
467 {
468         if (vma_is_io(vma)) {
469                 unsigned int i;
470
471                 for (i = 0; i < n_pages; ++i, start += PAGE_SIZE) {
472                         unsigned long pfn;
473                         int ret = follow_pfn(vma, start, &pfn);
474
475                         if (!pfn_valid(pfn))
476                                 return -EINVAL;
477
478                         if (ret) {
479                                 pr_err("no page for address %lu\n", start);
480                                 return ret;
481                         }
482                         pages[i] = pfn_to_page(pfn);
483                 }
484         } else {
485                 int n;
486
487                 n = get_user_pages(current, current->mm, start & PAGE_MASK,
488                         n_pages, dma_dir == DMA_FROM_DEVICE, 1, pages, NULL);
489                 /* negative error means that no page was pinned */
490                 n = max(n, 0);
491                 if (n != n_pages) {
492                         pr_err("got only %d of %d user pages\n", n, n_pages);
493                         while (n)
494                                 put_page(pages[--n]);
495                         return -EFAULT;
496                 }
497         }
498
499         return 0;
500 }
501
502 static void vb2_dc_put_dirty_page(struct page *page)
503 {
504         set_page_dirty_lock(page);
505         put_page(page);
506 }
507
508 static void vb2_dc_put_userptr(void *buf_priv)
509 {
510         struct vb2_dc_buf *buf = buf_priv;
511         struct sg_table *sgt = buf->dma_sgt;
512
513         if (sgt) {
514                 DEFINE_DMA_ATTRS(attrs);
515
516                 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
517                 /*
518                  * No need to sync to CPU, it's already synced to the CPU
519                  * since the finish() memop will have been called before this.
520                  */
521                 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
522                                    buf->dma_dir, &attrs);
523                 if (!vma_is_io(buf->vma))
524                         vb2_dc_sgt_foreach_page(sgt, vb2_dc_put_dirty_page);
525
526                 sg_free_table(sgt);
527                 kfree(sgt);
528         }
529         vb2_put_vma(buf->vma);
530         kfree(buf);
531 }
532
533 /*
534  * For some kind of reserved memory there might be no struct page available,
535  * so all that can be done to support such 'pages' is to try to convert
536  * pfn to dma address or at the last resort just assume that
537  * dma address == physical address (like it has been assumed in earlier version
538  * of videobuf2-dma-contig
539  */
540
541 #ifdef __arch_pfn_to_dma
542 static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
543 {
544         return (dma_addr_t)__arch_pfn_to_dma(dev, pfn);
545 }
546 #elif defined(__pfn_to_bus)
547 static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
548 {
549         return (dma_addr_t)__pfn_to_bus(pfn);
550 }
551 #elif defined(__pfn_to_phys)
552 static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
553 {
554         return (dma_addr_t)__pfn_to_phys(pfn);
555 }
556 #else
557 static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
558 {
559         /* really, we cannot do anything better at this point */
560         return (dma_addr_t)(pfn) << PAGE_SHIFT;
561 }
562 #endif
563
564 static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
565         unsigned long size, enum dma_data_direction dma_dir)
566 {
567         struct vb2_dc_conf *conf = alloc_ctx;
568         struct vb2_dc_buf *buf;
569         unsigned long start;
570         unsigned long end;
571         unsigned long offset;
572         struct page **pages;
573         int n_pages;
574         int ret = 0;
575         struct vm_area_struct *vma;
576         struct sg_table *sgt;
577         unsigned long contig_size;
578         unsigned long dma_align = dma_get_cache_alignment();
579         DEFINE_DMA_ATTRS(attrs);
580
581         dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
582
583         /* Only cache aligned DMA transfers are reliable */
584         if (!IS_ALIGNED(vaddr | size, dma_align)) {
585                 pr_debug("user data must be aligned to %lu bytes\n", dma_align);
586                 return ERR_PTR(-EINVAL);
587         }
588
589         if (!size) {
590                 pr_debug("size is zero\n");
591                 return ERR_PTR(-EINVAL);
592         }
593
594         buf = kzalloc(sizeof *buf, GFP_KERNEL);
595         if (!buf)
596                 return ERR_PTR(-ENOMEM);
597
598         buf->dev = conf->dev;
599         buf->dma_dir = dma_dir;
600
601         start = vaddr & PAGE_MASK;
602         offset = vaddr & ~PAGE_MASK;
603         end = PAGE_ALIGN(vaddr + size);
604         n_pages = (end - start) >> PAGE_SHIFT;
605
606         pages = kmalloc(n_pages * sizeof(pages[0]), GFP_KERNEL);
607         if (!pages) {
608                 ret = -ENOMEM;
609                 pr_err("failed to allocate pages table\n");
610                 goto fail_buf;
611         }
612
613         /* current->mm->mmap_sem is taken by videobuf2 core */
614         vma = find_vma(current->mm, vaddr);
615         if (!vma) {
616                 pr_err("no vma for address %lu\n", vaddr);
617                 ret = -EFAULT;
618                 goto fail_pages;
619         }
620
621         if (vma->vm_end < vaddr + size) {
622                 pr_err("vma at %lu is too small for %lu bytes\n", vaddr, size);
623                 ret = -EFAULT;
624                 goto fail_pages;
625         }
626
627         buf->vma = vb2_get_vma(vma);
628         if (!buf->vma) {
629                 pr_err("failed to copy vma\n");
630                 ret = -ENOMEM;
631                 goto fail_pages;
632         }
633
634         /* extract page list from userspace mapping */
635         ret = vb2_dc_get_user_pages(start, pages, n_pages, vma, dma_dir);
636         if (ret) {
637                 unsigned long pfn;
638                 if (vb2_dc_get_user_pfn(start, n_pages, vma, &pfn) == 0) {
639                         buf->dma_addr = vb2_dc_pfn_to_dma(buf->dev, pfn);
640                         buf->size = size;
641                         kfree(pages);
642                         return buf;
643                 }
644
645                 pr_err("failed to get user pages\n");
646                 goto fail_vma;
647         }
648
649         sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
650         if (!sgt) {
651                 pr_err("failed to allocate sg table\n");
652                 ret = -ENOMEM;
653                 goto fail_get_user_pages;
654         }
655
656         ret = sg_alloc_table_from_pages(sgt, pages, n_pages,
657                 offset, size, GFP_KERNEL);
658         if (ret) {
659                 pr_err("failed to initialize sg table\n");
660                 goto fail_sgt;
661         }
662
663         /* pages are no longer needed */
664         kfree(pages);
665         pages = NULL;
666
667         /*
668          * No need to sync to the device, this will happen later when the
669          * prepare() memop is called.
670          */
671         sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
672                                       buf->dma_dir, &attrs);
673         if (sgt->nents <= 0) {
674                 pr_err("failed to map scatterlist\n");
675                 ret = -EIO;
676                 goto fail_sgt_init;
677         }
678
679         contig_size = vb2_dc_get_contiguous_size(sgt);
680         if (contig_size < size) {
681                 pr_err("contiguous mapping is too small %lu/%lu\n",
682                         contig_size, size);
683                 ret = -EFAULT;
684                 goto fail_map_sg;
685         }
686
687         buf->dma_addr = sg_dma_address(sgt->sgl);
688         buf->size = size;
689         buf->dma_sgt = sgt;
690
691         return buf;
692
693 fail_map_sg:
694         dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
695                            buf->dma_dir, &attrs);
696
697 fail_sgt_init:
698         if (!vma_is_io(buf->vma))
699                 vb2_dc_sgt_foreach_page(sgt, put_page);
700         sg_free_table(sgt);
701
702 fail_sgt:
703         kfree(sgt);
704
705 fail_get_user_pages:
706         if (pages && !vma_is_io(buf->vma))
707                 while (n_pages)
708                         put_page(pages[--n_pages]);
709
710 fail_vma:
711         vb2_put_vma(buf->vma);
712
713 fail_pages:
714         kfree(pages); /* kfree is NULL-proof */
715
716 fail_buf:
717         kfree(buf);
718
719         return ERR_PTR(ret);
720 }
721
722 /*********************************************/
723 /*       callbacks for DMABUF buffers        */
724 /*********************************************/
725
726 static int vb2_dc_map_dmabuf(void *mem_priv)
727 {
728         struct vb2_dc_buf *buf = mem_priv;
729         struct sg_table *sgt;
730         unsigned long contig_size;
731
732         if (WARN_ON(!buf->db_attach)) {
733                 pr_err("trying to pin a non attached buffer\n");
734                 return -EINVAL;
735         }
736
737         if (WARN_ON(buf->dma_sgt)) {
738                 pr_err("dmabuf buffer is already pinned\n");
739                 return 0;
740         }
741
742         /* get the associated scatterlist for this buffer */
743         sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
744         if (IS_ERR(sgt)) {
745                 pr_err("Error getting dmabuf scatterlist\n");
746                 return -EINVAL;
747         }
748
749         /* checking if dmabuf is big enough to store contiguous chunk */
750         contig_size = vb2_dc_get_contiguous_size(sgt);
751         if (contig_size < buf->size) {
752                 pr_err("contiguous chunk is too small %lu/%lu b\n",
753                         contig_size, buf->size);
754                 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
755                 return -EFAULT;
756         }
757
758         buf->dma_addr = sg_dma_address(sgt->sgl);
759         buf->dma_sgt = sgt;
760         buf->vaddr = NULL;
761
762         return 0;
763 }
764
765 static void vb2_dc_unmap_dmabuf(void *mem_priv)
766 {
767         struct vb2_dc_buf *buf = mem_priv;
768         struct sg_table *sgt = buf->dma_sgt;
769
770         if (WARN_ON(!buf->db_attach)) {
771                 pr_err("trying to unpin a not attached buffer\n");
772                 return;
773         }
774
775         if (WARN_ON(!sgt)) {
776                 pr_err("dmabuf buffer is already unpinned\n");
777                 return;
778         }
779
780         if (buf->vaddr) {
781                 dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
782                 buf->vaddr = NULL;
783         }
784         dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
785
786         buf->dma_addr = 0;
787         buf->dma_sgt = NULL;
788 }
789
790 static void vb2_dc_detach_dmabuf(void *mem_priv)
791 {
792         struct vb2_dc_buf *buf = mem_priv;
793
794         /* if vb2 works correctly you should never detach mapped buffer */
795         if (WARN_ON(buf->dma_addr))
796                 vb2_dc_unmap_dmabuf(buf);
797
798         /* detach this attachment */
799         dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
800         kfree(buf);
801 }
802
803 static void *vb2_dc_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
804         unsigned long size, enum dma_data_direction dma_dir)
805 {
806         struct vb2_dc_conf *conf = alloc_ctx;
807         struct vb2_dc_buf *buf;
808         struct dma_buf_attachment *dba;
809
810         if (dbuf->size < size)
811                 return ERR_PTR(-EFAULT);
812
813         buf = kzalloc(sizeof(*buf), GFP_KERNEL);
814         if (!buf)
815                 return ERR_PTR(-ENOMEM);
816
817         buf->dev = conf->dev;
818         /* create attachment for the dmabuf with the user device */
819         dba = dma_buf_attach(dbuf, buf->dev);
820         if (IS_ERR(dba)) {
821                 pr_err("failed to attach dmabuf\n");
822                 kfree(buf);
823                 return dba;
824         }
825
826         buf->dma_dir = dma_dir;
827         buf->size = size;
828         buf->db_attach = dba;
829
830         return buf;
831 }
832
833 /*********************************************/
834 /*       DMA CONTIG exported functions       */
835 /*********************************************/
836
837 const struct vb2_mem_ops vb2_dma_contig_memops = {
838         .alloc          = vb2_dc_alloc,
839         .put            = vb2_dc_put,
840         .get_dmabuf     = vb2_dc_get_dmabuf,
841         .cookie         = vb2_dc_cookie,
842         .vaddr          = vb2_dc_vaddr,
843         .mmap           = vb2_dc_mmap,
844         .get_userptr    = vb2_dc_get_userptr,
845         .put_userptr    = vb2_dc_put_userptr,
846         .prepare        = vb2_dc_prepare,
847         .finish         = vb2_dc_finish,
848         .map_dmabuf     = vb2_dc_map_dmabuf,
849         .unmap_dmabuf   = vb2_dc_unmap_dmabuf,
850         .attach_dmabuf  = vb2_dc_attach_dmabuf,
851         .detach_dmabuf  = vb2_dc_detach_dmabuf,
852         .num_users      = vb2_dc_num_users,
853 };
854 EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
855
856 void *vb2_dma_contig_init_ctx(struct device *dev)
857 {
858         struct vb2_dc_conf *conf;
859
860         conf = kzalloc(sizeof *conf, GFP_KERNEL);
861         if (!conf)
862                 return ERR_PTR(-ENOMEM);
863
864         conf->dev = dev;
865
866         return conf;
867 }
868 EXPORT_SYMBOL_GPL(vb2_dma_contig_init_ctx);
869
870 void vb2_dma_contig_cleanup_ctx(void *alloc_ctx)
871 {
872         if (!IS_ERR_OR_NULL(alloc_ctx))
873                 kfree(alloc_ctx);
874 }
875 EXPORT_SYMBOL_GPL(vb2_dma_contig_cleanup_ctx);
876
877 MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
878 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
879 MODULE_LICENSE("GPL");