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