Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / media / v4l2-core / videobuf2-dma-sg.c
blobbd82d709ee8299967632f677d59e6412a451badc
1 /*
2 * videobuf2-dma-sg.c - dma scatter/gather memory allocator for videobuf2
4 * Copyright (C) 2010 Samsung Electronics
6 * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
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.
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>
20 #include <media/videobuf2-v4l2.h>
21 #include <media/videobuf2-memops.h>
22 #include <media/videobuf2-dma-sg.h>
24 static int debug;
25 module_param(debug, int, 0644);
27 #define dprintk(level, fmt, arg...) \
28 do { \
29 if (debug >= level) \
30 printk(KERN_DEBUG "vb2-dma-sg: " fmt, ## arg); \
31 } while (0)
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;
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.
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;
52 struct dma_buf_attachment *db_attach;
55 static void vb2_dma_sg_put(void *buf_priv);
57 static int vb2_dma_sg_alloc_compacted(struct vb2_dma_sg_buf *buf,
58 gfp_t gfp_flags)
60 unsigned int last_page = 0;
61 int size = buf->size;
63 while (size > 0) {
64 struct page *pages;
65 int order;
66 int i;
68 order = get_order(size);
69 /* Dont over allocate*/
70 if ((PAGE_SIZE << order) > size)
71 order--;
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;
80 if (order == 0) {
81 while (last_page--)
82 __free_page(buf->pages[last_page]);
83 return -ENOMEM;
85 order--;
88 split_page(pages, order);
89 for (i = 0; i < (1 << order); i++)
90 buf->pages[last_page++] = &pages[i];
92 size -= PAGE_SIZE << order;
95 return 0;
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)
102 struct vb2_dma_sg_buf *buf;
103 struct sg_table *sgt;
104 int ret;
105 int num_pages;
107 if (WARN_ON(dev == NULL))
108 return NULL;
109 buf = kzalloc(sizeof *buf, GFP_KERNEL);
110 if (!buf)
111 return NULL;
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;
121 buf->pages = kzalloc(buf->num_pages * sizeof(struct page *),
122 GFP_KERNEL);
123 if (!buf->pages)
124 goto fail_pages_array_alloc;
126 ret = vb2_dma_sg_alloc_compacted(buf, gfp_flags);
127 if (ret)
128 goto fail_pages_alloc;
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;
135 /* Prevent the device from being released while the buffer is used */
136 buf->dev = get_device(dev);
138 sgt = &buf->sg_table;
140 * No need to sync to the device, this will happen later when the
141 * prepare() memop is called.
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;
148 buf->handler.refcount = &buf->refcount;
149 buf->handler.put = vb2_dma_sg_put;
150 buf->handler.arg = buf;
152 atomic_inc(&buf->refcount);
154 dprintk(1, "%s: Allocated buffer of %d pages\n",
155 __func__, buf->num_pages);
156 return buf;
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;
172 static void vb2_dma_sg_put(void *buf_priv)
174 struct vb2_dma_sg_buf *buf = buf_priv;
175 struct sg_table *sgt = &buf->sg_table;
176 int i = buf->num_pages;
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);
194 static void vb2_dma_sg_prepare(void *buf_priv)
196 struct vb2_dma_sg_buf *buf = buf_priv;
197 struct sg_table *sgt = buf->dma_sgt;
199 /* DMABUF exporter will flush the cache for us */
200 if (buf->db_attach)
201 return;
203 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->orig_nents,
204 buf->dma_dir);
207 static void vb2_dma_sg_finish(void *buf_priv)
209 struct vb2_dma_sg_buf *buf = buf_priv;
210 struct sg_table *sgt = buf->dma_sgt;
212 /* DMABUF exporter will flush the cache for us */
213 if (buf->db_attach)
214 return;
216 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
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)
223 struct vb2_dma_sg_buf *buf;
224 struct sg_table *sgt;
225 struct frame_vector *vec;
227 buf = kzalloc(sizeof *buf, GFP_KERNEL);
228 if (!buf)
229 return NULL;
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;
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);
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;
251 sgt = &buf->sg_table;
253 * No need to sync to the device, this will happen later when the
254 * prepare() memop is called.
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;
261 return buf;
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;
273 * @put_userptr: inform the allocator that a USERPTR buffer will no longer
274 * be used
276 static void vb2_dma_sg_put_userptr(void *buf_priv)
278 struct vb2_dma_sg_buf *buf = buf_priv;
279 struct sg_table *sgt = &buf->sg_table;
280 int i = buf->num_pages;
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]);
293 vb2_destroy_framevec(buf->vec);
294 kfree(buf);
297 static void *vb2_dma_sg_vaddr(void *buf_priv)
299 struct vb2_dma_sg_buf *buf = buf_priv;
301 BUG_ON(!buf);
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);
311 /* add offset in case userptr is not page-aligned */
312 return buf->vaddr ? buf->vaddr + buf->offset : NULL;
315 static unsigned int vb2_dma_sg_num_users(void *buf_priv)
317 struct vb2_dma_sg_buf *buf = buf_priv;
319 return atomic_read(&buf->refcount);
322 static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
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;
329 if (!buf) {
330 printk(KERN_ERR "No memory to map\n");
331 return -EINVAL;
334 do {
335 int ret;
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;
343 uaddr += PAGE_SIZE;
344 usize -= PAGE_SIZE;
345 } while (usize > 0);
349 * Use common vm_area operations to track buffer refcount.
351 vma->vm_private_data = &buf->handler;
352 vma->vm_ops = &vb2_common_vm_ops;
354 vma->vm_ops->open(vma);
356 return 0;
359 /*********************************************/
360 /* DMABUF ops for exporters */
361 /*********************************************/
363 struct vb2_dma_sg_attachment {
364 struct sg_table sgt;
365 enum dma_data_direction dma_dir;
368 static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
369 struct dma_buf_attachment *dbuf_attach)
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;
378 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
379 if (!attach)
380 return -ENOMEM;
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.
386 ret = sg_alloc_table(sgt, buf->dma_sgt->orig_nents, GFP_KERNEL);
387 if (ret) {
388 kfree(attach);
389 return -ENOMEM;
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);
400 attach->dma_dir = DMA_NONE;
401 dbuf_attach->priv = attach;
403 return 0;
406 static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf,
407 struct dma_buf_attachment *db_attach)
409 struct vb2_dma_sg_attachment *attach = db_attach->priv;
410 struct sg_table *sgt;
412 if (!attach)
413 return;
415 sgt = &attach->sgt;
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;
426 static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
427 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
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;
434 mutex_lock(lock);
436 sgt = &attach->sgt;
437 /* return previously mapped sg table */
438 if (attach->dma_dir == dma_dir) {
439 mutex_unlock(lock);
440 return sgt;
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;
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);
459 attach->dma_dir = dma_dir;
461 mutex_unlock(lock);
463 return sgt;
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)
469 /* nothing to be done here */
472 static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf *dbuf)
474 /* drop reference obtained in vb2_dma_sg_get_dmabuf */
475 vb2_dma_sg_put(dbuf->priv);
478 static void *vb2_dma_sg_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
480 struct vb2_dma_sg_buf *buf = dbuf->priv;
482 return buf->vaddr ? buf->vaddr + pgnum * PAGE_SIZE : NULL;
485 static void *vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf *dbuf)
487 struct vb2_dma_sg_buf *buf = dbuf->priv;
489 return vb2_dma_sg_vaddr(buf);
492 static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf *dbuf,
493 struct vm_area_struct *vma)
495 return vb2_dma_sg_mmap(dbuf->priv, vma);
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,
510 static struct dma_buf *vb2_dma_sg_get_dmabuf(void *buf_priv, unsigned long flags)
512 struct vb2_dma_sg_buf *buf = buf_priv;
513 struct dma_buf *dbuf;
514 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
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;
521 if (WARN_ON(!buf->dma_sgt))
522 return NULL;
524 dbuf = dma_buf_export(&exp_info);
525 if (IS_ERR(dbuf))
526 return NULL;
528 /* dmabuf keeps reference to vb2 buffer */
529 atomic_inc(&buf->refcount);
531 return dbuf;
534 /*********************************************/
535 /* callbacks for DMABUF buffers */
536 /*********************************************/
538 static int vb2_dma_sg_map_dmabuf(void *mem_priv)
540 struct vb2_dma_sg_buf *buf = mem_priv;
541 struct sg_table *sgt;
543 if (WARN_ON(!buf->db_attach)) {
544 pr_err("trying to pin a non attached buffer\n");
545 return -EINVAL;
548 if (WARN_ON(buf->dma_sgt)) {
549 pr_err("dmabuf buffer is already pinned\n");
550 return 0;
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;
560 buf->dma_sgt = sgt;
561 buf->vaddr = NULL;
563 return 0;
566 static void vb2_dma_sg_unmap_dmabuf(void *mem_priv)
568 struct vb2_dma_sg_buf *buf = mem_priv;
569 struct sg_table *sgt = buf->dma_sgt;
571 if (WARN_ON(!buf->db_attach)) {
572 pr_err("trying to unpin a not attached buffer\n");
573 return;
576 if (WARN_ON(!sgt)) {
577 pr_err("dmabuf buffer is already unpinned\n");
578 return;
581 if (buf->vaddr) {
582 dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
583 buf->vaddr = NULL;
585 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
587 buf->dma_sgt = NULL;
590 static void vb2_dma_sg_detach_dmabuf(void *mem_priv)
592 struct vb2_dma_sg_buf *buf = mem_priv;
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);
598 /* detach this attachment */
599 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
600 kfree(buf);
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)
606 struct vb2_dma_sg_buf *buf;
607 struct dma_buf_attachment *dba;
609 if (dbuf->size < size)
610 return ERR_PTR(-EFAULT);
612 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
613 if (!buf)
614 return ERR_PTR(-ENOMEM);
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;
625 buf->dma_dir = dma_dir;
626 buf->size = size;
627 buf->db_attach = dba;
629 return buf;
632 static void *vb2_dma_sg_cookie(void *buf_priv)
634 struct vb2_dma_sg_buf *buf = buf_priv;
636 return buf->dma_sgt;
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,
656 EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
658 MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
659 MODULE_AUTHOR("Andrzej Pietrasiewicz");
660 MODULE_LICENSE("GPL");