]> Pileus Git - ~andy/linux/blob - drivers/media/v4l2-core/videobuf2-dma-contig.c
[media] v4l: vb2-dma-contig: add support for dma_buf importing
[~andy/linux] / 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
40         /* USERPTR related */
41         struct vm_area_struct           *vma;
42
43         /* DMABUF related */
44         struct dma_buf_attachment       *db_attach;
45 };
46
47 /*********************************************/
48 /*        scatterlist table functions        */
49 /*********************************************/
50
51
52 static void vb2_dc_sgt_foreach_page(struct sg_table *sgt,
53         void (*cb)(struct page *pg))
54 {
55         struct scatterlist *s;
56         unsigned int i;
57
58         for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
59                 struct page *page = sg_page(s);
60                 unsigned int n_pages = PAGE_ALIGN(s->offset + s->length)
61                         >> PAGE_SHIFT;
62                 unsigned int j;
63
64                 for (j = 0; j < n_pages; ++j, ++page)
65                         cb(page);
66         }
67 }
68
69 static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
70 {
71         struct scatterlist *s;
72         dma_addr_t expected = sg_dma_address(sgt->sgl);
73         unsigned int i;
74         unsigned long size = 0;
75
76         for_each_sg(sgt->sgl, s, sgt->nents, i) {
77                 if (sg_dma_address(s) != expected)
78                         break;
79                 expected = sg_dma_address(s) + sg_dma_len(s);
80                 size += sg_dma_len(s);
81         }
82         return size;
83 }
84
85 /*********************************************/
86 /*         callbacks for all buffers         */
87 /*********************************************/
88
89 static void *vb2_dc_cookie(void *buf_priv)
90 {
91         struct vb2_dc_buf *buf = buf_priv;
92
93         return &buf->dma_addr;
94 }
95
96 static void *vb2_dc_vaddr(void *buf_priv)
97 {
98         struct vb2_dc_buf *buf = buf_priv;
99
100         return buf->vaddr;
101 }
102
103 static unsigned int vb2_dc_num_users(void *buf_priv)
104 {
105         struct vb2_dc_buf *buf = buf_priv;
106
107         return atomic_read(&buf->refcount);
108 }
109
110 static void vb2_dc_prepare(void *buf_priv)
111 {
112         struct vb2_dc_buf *buf = buf_priv;
113         struct sg_table *sgt = buf->dma_sgt;
114
115         /* DMABUF exporter will flush the cache for us */
116         if (!sgt || buf->db_attach)
117                 return;
118
119         dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
120 }
121
122 static void vb2_dc_finish(void *buf_priv)
123 {
124         struct vb2_dc_buf *buf = buf_priv;
125         struct sg_table *sgt = buf->dma_sgt;
126
127         /* DMABUF exporter will flush the cache for us */
128         if (!sgt || buf->db_attach)
129                 return;
130
131         dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
132 }
133
134 /*********************************************/
135 /*        callbacks for MMAP buffers         */
136 /*********************************************/
137
138 static void vb2_dc_put(void *buf_priv)
139 {
140         struct vb2_dc_buf *buf = buf_priv;
141
142         if (!atomic_dec_and_test(&buf->refcount))
143                 return;
144
145         dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr);
146         kfree(buf);
147 }
148
149 static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size)
150 {
151         struct vb2_dc_conf *conf = alloc_ctx;
152         struct device *dev = conf->dev;
153         struct vb2_dc_buf *buf;
154
155         buf = kzalloc(sizeof *buf, GFP_KERNEL);
156         if (!buf)
157                 return ERR_PTR(-ENOMEM);
158
159         buf->vaddr = dma_alloc_coherent(dev, size, &buf->dma_addr, GFP_KERNEL);
160         if (!buf->vaddr) {
161                 dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size);
162                 kfree(buf);
163                 return ERR_PTR(-ENOMEM);
164         }
165
166         buf->dev = dev;
167         buf->size = size;
168
169         buf->handler.refcount = &buf->refcount;
170         buf->handler.put = vb2_dc_put;
171         buf->handler.arg = buf;
172
173         atomic_inc(&buf->refcount);
174
175         return buf;
176 }
177
178 static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
179 {
180         struct vb2_dc_buf *buf = buf_priv;
181
182         if (!buf) {
183                 printk(KERN_ERR "No buffer to map\n");
184                 return -EINVAL;
185         }
186
187         return vb2_mmap_pfn_range(vma, buf->dma_addr, buf->size,
188                                   &vb2_common_vm_ops, &buf->handler);
189 }
190
191 /*********************************************/
192 /*       callbacks for USERPTR buffers       */
193 /*********************************************/
194
195 static inline int vma_is_io(struct vm_area_struct *vma)
196 {
197         return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
198 }
199
200 static int vb2_dc_get_user_pages(unsigned long start, struct page **pages,
201         int n_pages, struct vm_area_struct *vma, int write)
202 {
203         if (vma_is_io(vma)) {
204                 unsigned int i;
205
206                 for (i = 0; i < n_pages; ++i, start += PAGE_SIZE) {
207                         unsigned long pfn;
208                         int ret = follow_pfn(vma, start, &pfn);
209
210                         if (ret) {
211                                 pr_err("no page for address %lu\n", start);
212                                 return ret;
213                         }
214                         pages[i] = pfn_to_page(pfn);
215                 }
216         } else {
217                 int n;
218
219                 n = get_user_pages(current, current->mm, start & PAGE_MASK,
220                         n_pages, write, 1, pages, NULL);
221                 /* negative error means that no page was pinned */
222                 n = max(n, 0);
223                 if (n != n_pages) {
224                         pr_err("got only %d of %d user pages\n", n, n_pages);
225                         while (n)
226                                 put_page(pages[--n]);
227                         return -EFAULT;
228                 }
229         }
230
231         return 0;
232 }
233
234 static void vb2_dc_put_dirty_page(struct page *page)
235 {
236         set_page_dirty_lock(page);
237         put_page(page);
238 }
239
240 static void vb2_dc_put_userptr(void *buf_priv)
241 {
242         struct vb2_dc_buf *buf = buf_priv;
243         struct sg_table *sgt = buf->dma_sgt;
244
245         dma_unmap_sg(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
246         if (!vma_is_io(buf->vma))
247                 vb2_dc_sgt_foreach_page(sgt, vb2_dc_put_dirty_page);
248
249         sg_free_table(sgt);
250         kfree(sgt);
251         vb2_put_vma(buf->vma);
252         kfree(buf);
253 }
254
255 static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
256         unsigned long size, int write)
257 {
258         struct vb2_dc_conf *conf = alloc_ctx;
259         struct vb2_dc_buf *buf;
260         unsigned long start;
261         unsigned long end;
262         unsigned long offset;
263         struct page **pages;
264         int n_pages;
265         int ret = 0;
266         struct vm_area_struct *vma;
267         struct sg_table *sgt;
268         unsigned long contig_size;
269
270         buf = kzalloc(sizeof *buf, GFP_KERNEL);
271         if (!buf)
272                 return ERR_PTR(-ENOMEM);
273
274         buf->dev = conf->dev;
275         buf->dma_dir = write ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
276
277         start = vaddr & PAGE_MASK;
278         offset = vaddr & ~PAGE_MASK;
279         end = PAGE_ALIGN(vaddr + size);
280         n_pages = (end - start) >> PAGE_SHIFT;
281
282         pages = kmalloc(n_pages * sizeof(pages[0]), GFP_KERNEL);
283         if (!pages) {
284                 ret = -ENOMEM;
285                 pr_err("failed to allocate pages table\n");
286                 goto fail_buf;
287         }
288
289         /* current->mm->mmap_sem is taken by videobuf2 core */
290         vma = find_vma(current->mm, vaddr);
291         if (!vma) {
292                 pr_err("no vma for address %lu\n", vaddr);
293                 ret = -EFAULT;
294                 goto fail_pages;
295         }
296
297         if (vma->vm_end < vaddr + size) {
298                 pr_err("vma at %lu is too small for %lu bytes\n", vaddr, size);
299                 ret = -EFAULT;
300                 goto fail_pages;
301         }
302
303         buf->vma = vb2_get_vma(vma);
304         if (!buf->vma) {
305                 pr_err("failed to copy vma\n");
306                 ret = -ENOMEM;
307                 goto fail_pages;
308         }
309
310         /* extract page list from userspace mapping */
311         ret = vb2_dc_get_user_pages(start, pages, n_pages, vma, write);
312         if (ret) {
313                 pr_err("failed to get user pages\n");
314                 goto fail_vma;
315         }
316
317         sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
318         if (!sgt) {
319                 pr_err("failed to allocate sg table\n");
320                 ret = -ENOMEM;
321                 goto fail_get_user_pages;
322         }
323
324         ret = sg_alloc_table_from_pages(sgt, pages, n_pages,
325                 offset, size, GFP_KERNEL);
326         if (ret) {
327                 pr_err("failed to initialize sg table\n");
328                 goto fail_sgt;
329         }
330
331         /* pages are no longer needed */
332         kfree(pages);
333         pages = NULL;
334
335         sgt->nents = dma_map_sg(buf->dev, sgt->sgl, sgt->orig_nents,
336                 buf->dma_dir);
337         if (sgt->nents <= 0) {
338                 pr_err("failed to map scatterlist\n");
339                 ret = -EIO;
340                 goto fail_sgt_init;
341         }
342
343         contig_size = vb2_dc_get_contiguous_size(sgt);
344         if (contig_size < size) {
345                 pr_err("contiguous mapping is too small %lu/%lu\n",
346                         contig_size, size);
347                 ret = -EFAULT;
348                 goto fail_map_sg;
349         }
350
351         buf->dma_addr = sg_dma_address(sgt->sgl);
352         buf->size = size;
353         buf->dma_sgt = sgt;
354
355         return buf;
356
357 fail_map_sg:
358         dma_unmap_sg(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
359
360 fail_sgt_init:
361         if (!vma_is_io(buf->vma))
362                 vb2_dc_sgt_foreach_page(sgt, put_page);
363         sg_free_table(sgt);
364
365 fail_sgt:
366         kfree(sgt);
367
368 fail_get_user_pages:
369         if (pages && !vma_is_io(buf->vma))
370                 while (n_pages)
371                         put_page(pages[--n_pages]);
372
373 fail_vma:
374         vb2_put_vma(buf->vma);
375
376 fail_pages:
377         kfree(pages); /* kfree is NULL-proof */
378
379 fail_buf:
380         kfree(buf);
381
382         return ERR_PTR(ret);
383 }
384
385 /*********************************************/
386 /*       callbacks for DMABUF buffers        */
387 /*********************************************/
388
389 static int vb2_dc_map_dmabuf(void *mem_priv)
390 {
391         struct vb2_dc_buf *buf = mem_priv;
392         struct sg_table *sgt;
393         unsigned long contig_size;
394
395         if (WARN_ON(!buf->db_attach)) {
396                 pr_err("trying to pin a non attached buffer\n");
397                 return -EINVAL;
398         }
399
400         if (WARN_ON(buf->dma_sgt)) {
401                 pr_err("dmabuf buffer is already pinned\n");
402                 return 0;
403         }
404
405         /* get the associated scatterlist for this buffer */
406         sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
407         if (IS_ERR_OR_NULL(sgt)) {
408                 pr_err("Error getting dmabuf scatterlist\n");
409                 return -EINVAL;
410         }
411
412         /* checking if dmabuf is big enough to store contiguous chunk */
413         contig_size = vb2_dc_get_contiguous_size(sgt);
414         if (contig_size < buf->size) {
415                 pr_err("contiguous chunk is too small %lu/%lu b\n",
416                         contig_size, buf->size);
417                 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
418                 return -EFAULT;
419         }
420
421         buf->dma_addr = sg_dma_address(sgt->sgl);
422         buf->dma_sgt = sgt;
423
424         return 0;
425 }
426
427 static void vb2_dc_unmap_dmabuf(void *mem_priv)
428 {
429         struct vb2_dc_buf *buf = mem_priv;
430         struct sg_table *sgt = buf->dma_sgt;
431
432         if (WARN_ON(!buf->db_attach)) {
433                 pr_err("trying to unpin a not attached buffer\n");
434                 return;
435         }
436
437         if (WARN_ON(!sgt)) {
438                 pr_err("dmabuf buffer is already unpinned\n");
439                 return;
440         }
441
442         dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
443
444         buf->dma_addr = 0;
445         buf->dma_sgt = NULL;
446 }
447
448 static void vb2_dc_detach_dmabuf(void *mem_priv)
449 {
450         struct vb2_dc_buf *buf = mem_priv;
451
452         /* if vb2 works correctly you should never detach mapped buffer */
453         if (WARN_ON(buf->dma_addr))
454                 vb2_dc_unmap_dmabuf(buf);
455
456         /* detach this attachment */
457         dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
458         kfree(buf);
459 }
460
461 static void *vb2_dc_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
462         unsigned long size, int write)
463 {
464         struct vb2_dc_conf *conf = alloc_ctx;
465         struct vb2_dc_buf *buf;
466         struct dma_buf_attachment *dba;
467
468         if (dbuf->size < size)
469                 return ERR_PTR(-EFAULT);
470
471         buf = kzalloc(sizeof(*buf), GFP_KERNEL);
472         if (!buf)
473                 return ERR_PTR(-ENOMEM);
474
475         buf->dev = conf->dev;
476         /* create attachment for the dmabuf with the user device */
477         dba = dma_buf_attach(dbuf, buf->dev);
478         if (IS_ERR(dba)) {
479                 pr_err("failed to attach dmabuf\n");
480                 kfree(buf);
481                 return dba;
482         }
483
484         buf->dma_dir = write ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
485         buf->size = size;
486         buf->db_attach = dba;
487
488         return buf;
489 }
490
491 /*********************************************/
492 /*       DMA CONTIG exported functions       */
493 /*********************************************/
494
495 const struct vb2_mem_ops vb2_dma_contig_memops = {
496         .alloc          = vb2_dc_alloc,
497         .put            = vb2_dc_put,
498         .cookie         = vb2_dc_cookie,
499         .vaddr          = vb2_dc_vaddr,
500         .mmap           = vb2_dc_mmap,
501         .get_userptr    = vb2_dc_get_userptr,
502         .put_userptr    = vb2_dc_put_userptr,
503         .prepare        = vb2_dc_prepare,
504         .finish         = vb2_dc_finish,
505         .map_dmabuf     = vb2_dc_map_dmabuf,
506         .unmap_dmabuf   = vb2_dc_unmap_dmabuf,
507         .attach_dmabuf  = vb2_dc_attach_dmabuf,
508         .detach_dmabuf  = vb2_dc_detach_dmabuf,
509         .num_users      = vb2_dc_num_users,
510 };
511 EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
512
513 void *vb2_dma_contig_init_ctx(struct device *dev)
514 {
515         struct vb2_dc_conf *conf;
516
517         conf = kzalloc(sizeof *conf, GFP_KERNEL);
518         if (!conf)
519                 return ERR_PTR(-ENOMEM);
520
521         conf->dev = dev;
522
523         return conf;
524 }
525 EXPORT_SYMBOL_GPL(vb2_dma_contig_init_ctx);
526
527 void vb2_dma_contig_cleanup_ctx(void *alloc_ctx)
528 {
529         kfree(alloc_ctx);
530 }
531 EXPORT_SYMBOL_GPL(vb2_dma_contig_cleanup_ctx);
532
533 MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
534 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
535 MODULE_LICENSE("GPL");