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