pvclock: introduce seqcount-like API
[linux/fpc-iii.git] / drivers / media / v4l2-core / videobuf2-dma-sg.c
bloba39db8a6db7a592bcdc51c3697e6a098792c35c9
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, const struct dma_attrs *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;
106 DEFINE_DMA_ATTRS(attrs);
108 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
110 if (WARN_ON(dev == NULL))
111 return NULL;
112 buf = kzalloc(sizeof *buf, GFP_KERNEL);
113 if (!buf)
114 return NULL;
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;
124 buf->pages = kzalloc(buf->num_pages * sizeof(struct page *),
125 GFP_KERNEL);
126 if (!buf->pages)
127 goto fail_pages_array_alloc;
129 ret = vb2_dma_sg_alloc_compacted(buf, gfp_flags);
130 if (ret)
131 goto fail_pages_alloc;
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;
138 /* Prevent the device from being released while the buffer is used */
139 buf->dev = get_device(dev);
141 sgt = &buf->sg_table;
143 * No need to sync to the device, this will happen later when the
144 * prepare() memop is called.
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;
151 buf->handler.refcount = &buf->refcount;
152 buf->handler.put = vb2_dma_sg_put;
153 buf->handler.arg = buf;
155 atomic_inc(&buf->refcount);
157 dprintk(1, "%s: Allocated buffer of %d pages\n",
158 __func__, buf->num_pages);
159 return buf;
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;
175 static void vb2_dma_sg_put(void *buf_priv)
177 struct vb2_dma_sg_buf *buf = buf_priv;
178 struct sg_table *sgt = &buf->sg_table;
179 int i = buf->num_pages;
181 if (atomic_dec_and_test(&buf->refcount)) {
182 DEFINE_DMA_ATTRS(attrs);
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);
200 static void vb2_dma_sg_prepare(void *buf_priv)
202 struct vb2_dma_sg_buf *buf = buf_priv;
203 struct sg_table *sgt = buf->dma_sgt;
205 /* DMABUF exporter will flush the cache for us */
206 if (buf->db_attach)
207 return;
209 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->orig_nents,
210 buf->dma_dir);
213 static void vb2_dma_sg_finish(void *buf_priv)
215 struct vb2_dma_sg_buf *buf = buf_priv;
216 struct sg_table *sgt = buf->dma_sgt;
218 /* DMABUF exporter will flush the cache for us */
219 if (buf->db_attach)
220 return;
222 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
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)
229 struct vb2_dma_sg_buf *buf;
230 struct sg_table *sgt;
231 DEFINE_DMA_ATTRS(attrs);
232 struct frame_vector *vec;
234 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
235 buf = kzalloc(sizeof *buf, GFP_KERNEL);
236 if (!buf)
237 return NULL;
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;
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);
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;
259 sgt = &buf->sg_table;
261 * No need to sync to the device, this will happen later when the
262 * prepare() memop is called.
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;
269 return buf;
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;
281 * @put_userptr: inform the allocator that a USERPTR buffer will no longer
282 * be used
284 static void vb2_dma_sg_put_userptr(void *buf_priv)
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);
291 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
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]);
304 vb2_destroy_framevec(buf->vec);
305 kfree(buf);
308 static void *vb2_dma_sg_vaddr(void *buf_priv)
310 struct vb2_dma_sg_buf *buf = buf_priv;
312 BUG_ON(!buf);
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);
322 /* add offset in case userptr is not page-aligned */
323 return buf->vaddr ? buf->vaddr + buf->offset : NULL;
326 static unsigned int vb2_dma_sg_num_users(void *buf_priv)
328 struct vb2_dma_sg_buf *buf = buf_priv;
330 return atomic_read(&buf->refcount);
333 static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
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;
340 if (!buf) {
341 printk(KERN_ERR "No memory to map\n");
342 return -EINVAL;
345 do {
346 int ret;
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;
354 uaddr += PAGE_SIZE;
355 usize -= PAGE_SIZE;
356 } while (usize > 0);
360 * Use common vm_area operations to track buffer refcount.
362 vma->vm_private_data = &buf->handler;
363 vma->vm_ops = &vb2_common_vm_ops;
365 vma->vm_ops->open(vma);
367 return 0;
370 /*********************************************/
371 /* DMABUF ops for exporters */
372 /*********************************************/
374 struct vb2_dma_sg_attachment {
375 struct sg_table sgt;
376 enum dma_data_direction dma_dir;
379 static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
380 struct dma_buf_attachment *dbuf_attach)
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;
389 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
390 if (!attach)
391 return -ENOMEM;
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.
397 ret = sg_alloc_table(sgt, buf->dma_sgt->orig_nents, GFP_KERNEL);
398 if (ret) {
399 kfree(attach);
400 return -ENOMEM;
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);
411 attach->dma_dir = DMA_NONE;
412 dbuf_attach->priv = attach;
414 return 0;
417 static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf,
418 struct dma_buf_attachment *db_attach)
420 struct vb2_dma_sg_attachment *attach = db_attach->priv;
421 struct sg_table *sgt;
423 if (!attach)
424 return;
426 sgt = &attach->sgt;
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;
437 static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
438 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
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;
445 mutex_lock(lock);
447 sgt = &attach->sgt;
448 /* return previously mapped sg table */
449 if (attach->dma_dir == dma_dir) {
450 mutex_unlock(lock);
451 return sgt;
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;
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);
470 attach->dma_dir = dma_dir;
472 mutex_unlock(lock);
474 return sgt;
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)
480 /* nothing to be done here */
483 static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf *dbuf)
485 /* drop reference obtained in vb2_dma_sg_get_dmabuf */
486 vb2_dma_sg_put(dbuf->priv);
489 static void *vb2_dma_sg_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
491 struct vb2_dma_sg_buf *buf = dbuf->priv;
493 return buf->vaddr ? buf->vaddr + pgnum * PAGE_SIZE : NULL;
496 static void *vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf *dbuf)
498 struct vb2_dma_sg_buf *buf = dbuf->priv;
500 return vb2_dma_sg_vaddr(buf);
503 static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf *dbuf,
504 struct vm_area_struct *vma)
506 return vb2_dma_sg_mmap(dbuf->priv, vma);
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,
521 static struct dma_buf *vb2_dma_sg_get_dmabuf(void *buf_priv, unsigned long flags)
523 struct vb2_dma_sg_buf *buf = buf_priv;
524 struct dma_buf *dbuf;
525 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
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;
532 if (WARN_ON(!buf->dma_sgt))
533 return NULL;
535 dbuf = dma_buf_export(&exp_info);
536 if (IS_ERR(dbuf))
537 return NULL;
539 /* dmabuf keeps reference to vb2 buffer */
540 atomic_inc(&buf->refcount);
542 return dbuf;
545 /*********************************************/
546 /* callbacks for DMABUF buffers */
547 /*********************************************/
549 static int vb2_dma_sg_map_dmabuf(void *mem_priv)
551 struct vb2_dma_sg_buf *buf = mem_priv;
552 struct sg_table *sgt;
554 if (WARN_ON(!buf->db_attach)) {
555 pr_err("trying to pin a non attached buffer\n");
556 return -EINVAL;
559 if (WARN_ON(buf->dma_sgt)) {
560 pr_err("dmabuf buffer is already pinned\n");
561 return 0;
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;
571 buf->dma_sgt = sgt;
572 buf->vaddr = NULL;
574 return 0;
577 static void vb2_dma_sg_unmap_dmabuf(void *mem_priv)
579 struct vb2_dma_sg_buf *buf = mem_priv;
580 struct sg_table *sgt = buf->dma_sgt;
582 if (WARN_ON(!buf->db_attach)) {
583 pr_err("trying to unpin a not attached buffer\n");
584 return;
587 if (WARN_ON(!sgt)) {
588 pr_err("dmabuf buffer is already unpinned\n");
589 return;
592 if (buf->vaddr) {
593 dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
594 buf->vaddr = NULL;
596 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
598 buf->dma_sgt = NULL;
601 static void vb2_dma_sg_detach_dmabuf(void *mem_priv)
603 struct vb2_dma_sg_buf *buf = mem_priv;
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);
609 /* detach this attachment */
610 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
611 kfree(buf);
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)
617 struct vb2_dma_sg_buf *buf;
618 struct dma_buf_attachment *dba;
620 if (dbuf->size < size)
621 return ERR_PTR(-EFAULT);
623 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
624 if (!buf)
625 return ERR_PTR(-ENOMEM);
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;
636 buf->dma_dir = dma_dir;
637 buf->size = size;
638 buf->db_attach = dba;
640 return buf;
643 static void *vb2_dma_sg_cookie(void *buf_priv)
645 struct vb2_dma_sg_buf *buf = buf_priv;
647 return buf->dma_sgt;
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,
667 EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
669 MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
670 MODULE_AUTHOR("Andrzej Pietrasiewicz");
671 MODULE_LICENSE("GPL");