2 * videobuf2-dma-sg.c - dma scatter/gather memory allocator for videobuf2
4 * Copyright (C) 2010 Samsung Electronics
6 * Author: Andrzej Pietrasiewicz <andrzejtp2010@gmail.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>
15 #include <linux/refcount.h>
16 #include <linux/scatterlist.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
21 #include <media/videobuf2-v4l2.h>
22 #include <media/videobuf2-memops.h>
23 #include <media/videobuf2-dma-sg.h>
26 module_param(debug, int, 0644);
28 #define dprintk(level, fmt, arg...) \
31 printk(KERN_DEBUG "vb2-dma-sg: " fmt, ## arg); \
34 struct vb2_dma_sg_buf {
38 struct frame_vector *vec;
40 enum dma_data_direction dma_dir;
41 struct sg_table sg_table;
43 * This will point to sg_table when used with the MMAP or USERPTR
44 * memory model, and to the dma_buf sglist when used with the
45 * DMABUF memory model.
47 struct sg_table *dma_sgt;
49 unsigned int num_pages;
51 struct vb2_vmarea_handler handler;
53 struct dma_buf_attachment *db_attach;
56 static void vb2_dma_sg_put(void *buf_priv);
58 static int vb2_dma_sg_alloc_compacted(struct vb2_dma_sg_buf *buf,
61 unsigned int last_page = 0;
62 unsigned long size = buf->size;
69 order = get_order(size);
70 /* Don't over allocate*/
71 if ((PAGE_SIZE << order) > size)
76 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO |
77 __GFP_NOWARN | gfp_flags, order);
83 __free_page(buf->pages[last_page]);
89 split_page(pages, order);
90 for (i = 0; i < (1 << order); i++)
91 buf->pages[last_page++] = &pages[i];
93 size -= PAGE_SIZE << order;
99 static void *vb2_dma_sg_alloc(struct device *dev, unsigned long dma_attrs,
100 unsigned long size, enum dma_data_direction dma_dir,
103 struct vb2_dma_sg_buf *buf;
104 struct sg_table *sgt;
109 return ERR_PTR(-EINVAL);
111 buf = kzalloc(sizeof *buf, GFP_KERNEL);
113 return ERR_PTR(-ENOMEM);
116 buf->dma_dir = dma_dir;
119 /* size is already page aligned */
120 buf->num_pages = size >> PAGE_SHIFT;
121 buf->dma_sgt = &buf->sg_table;
123 buf->pages = kvmalloc_array(buf->num_pages, sizeof(struct page *),
124 GFP_KERNEL | __GFP_ZERO);
126 goto fail_pages_array_alloc;
128 ret = vb2_dma_sg_alloc_compacted(buf, gfp_flags);
130 goto fail_pages_alloc;
132 ret = sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
133 buf->num_pages, 0, size, GFP_KERNEL);
135 goto fail_table_alloc;
137 /* Prevent the device from being released while the buffer is used */
138 buf->dev = get_device(dev);
140 sgt = &buf->sg_table;
142 * No need to sync to the device, this will happen later when the
143 * prepare() memop is called.
145 sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
146 buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
150 buf->handler.refcount = &buf->refcount;
151 buf->handler.put = vb2_dma_sg_put;
152 buf->handler.arg = buf;
154 refcount_set(&buf->refcount, 1);
156 dprintk(1, "%s: Allocated buffer of %d pages\n",
157 __func__, buf->num_pages);
161 put_device(buf->dev);
162 sg_free_table(buf->dma_sgt);
164 num_pages = buf->num_pages;
166 __free_page(buf->pages[num_pages]);
169 fail_pages_array_alloc:
171 return ERR_PTR(-ENOMEM);
174 static void vb2_dma_sg_put(void *buf_priv)
176 struct vb2_dma_sg_buf *buf = buf_priv;
177 struct sg_table *sgt = &buf->sg_table;
178 int i = buf->num_pages;
180 if (refcount_dec_and_test(&buf->refcount)) {
181 dprintk(1, "%s: Freeing buffer of %d pages\n", __func__,
183 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
184 buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
186 vm_unmap_ram(buf->vaddr, buf->num_pages);
187 sg_free_table(buf->dma_sgt);
189 __free_page(buf->pages[i]);
191 put_device(buf->dev);
196 static void vb2_dma_sg_prepare(void *buf_priv)
198 struct vb2_dma_sg_buf *buf = buf_priv;
199 struct sg_table *sgt = buf->dma_sgt;
201 /* DMABUF exporter will flush the cache for us */
205 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->orig_nents,
209 static void vb2_dma_sg_finish(void *buf_priv)
211 struct vb2_dma_sg_buf *buf = buf_priv;
212 struct sg_table *sgt = buf->dma_sgt;
214 /* DMABUF exporter will flush the cache for us */
218 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
221 static void *vb2_dma_sg_get_userptr(struct device *dev, unsigned long vaddr,
223 enum dma_data_direction dma_dir)
225 struct vb2_dma_sg_buf *buf;
226 struct sg_table *sgt;
227 struct frame_vector *vec;
230 return ERR_PTR(-EINVAL);
232 buf = kzalloc(sizeof *buf, GFP_KERNEL);
234 return ERR_PTR(-ENOMEM);
238 buf->dma_dir = dma_dir;
239 buf->offset = vaddr & ~PAGE_MASK;
241 buf->dma_sgt = &buf->sg_table;
242 vec = vb2_create_framevec(vaddr, size);
244 goto userptr_fail_pfnvec;
247 buf->pages = frame_vector_pages(vec);
248 if (IS_ERR(buf->pages))
249 goto userptr_fail_sgtable;
250 buf->num_pages = frame_vector_count(vec);
252 if (sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
253 buf->num_pages, buf->offset, size, 0))
254 goto userptr_fail_sgtable;
256 sgt = &buf->sg_table;
258 * No need to sync to the device, this will happen later when the
259 * prepare() memop is called.
261 sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
262 buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
264 goto userptr_fail_map;
269 sg_free_table(&buf->sg_table);
270 userptr_fail_sgtable:
271 vb2_destroy_framevec(vec);
274 return ERR_PTR(-ENOMEM);
278 * @put_userptr: inform the allocator that a USERPTR buffer will no longer
281 static void vb2_dma_sg_put_userptr(void *buf_priv)
283 struct vb2_dma_sg_buf *buf = buf_priv;
284 struct sg_table *sgt = &buf->sg_table;
285 int i = buf->num_pages;
287 dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
288 __func__, buf->num_pages);
289 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir,
290 DMA_ATTR_SKIP_CPU_SYNC);
292 vm_unmap_ram(buf->vaddr, buf->num_pages);
293 sg_free_table(buf->dma_sgt);
294 if (buf->dma_dir == DMA_FROM_DEVICE ||
295 buf->dma_dir == DMA_BIDIRECTIONAL)
297 set_page_dirty_lock(buf->pages[i]);
298 vb2_destroy_framevec(buf->vec);
302 static void *vb2_dma_sg_vaddr(void *buf_priv)
304 struct vb2_dma_sg_buf *buf = buf_priv;
310 buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
312 buf->vaddr = vm_map_ram(buf->pages,
313 buf->num_pages, -1, PAGE_KERNEL);
316 /* add offset in case userptr is not page-aligned */
317 return buf->vaddr ? buf->vaddr + buf->offset : NULL;
320 static unsigned int vb2_dma_sg_num_users(void *buf_priv)
322 struct vb2_dma_sg_buf *buf = buf_priv;
324 return refcount_read(&buf->refcount);
327 static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
329 struct vb2_dma_sg_buf *buf = buf_priv;
333 printk(KERN_ERR "No memory to map\n");
337 err = vm_map_pages(vma, buf->pages, buf->num_pages);
339 printk(KERN_ERR "Remapping memory, error: %d\n", err);
344 * Use common vm_area operations to track buffer refcount.
346 vma->vm_private_data = &buf->handler;
347 vma->vm_ops = &vb2_common_vm_ops;
349 vma->vm_ops->open(vma);
354 /*********************************************/
355 /* DMABUF ops for exporters */
356 /*********************************************/
358 struct vb2_dma_sg_attachment {
360 enum dma_data_direction dma_dir;
363 static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf *dbuf,
364 struct dma_buf_attachment *dbuf_attach)
366 struct vb2_dma_sg_attachment *attach;
368 struct scatterlist *rd, *wr;
369 struct sg_table *sgt;
370 struct vb2_dma_sg_buf *buf = dbuf->priv;
373 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
378 /* Copy the buf->base_sgt scatter list to the attachment, as we can't
379 * map the same scatter list to multiple attachments at the same time.
381 ret = sg_alloc_table(sgt, buf->dma_sgt->orig_nents, GFP_KERNEL);
387 rd = buf->dma_sgt->sgl;
389 for (i = 0; i < sgt->orig_nents; ++i) {
390 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
395 attach->dma_dir = DMA_NONE;
396 dbuf_attach->priv = attach;
401 static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf,
402 struct dma_buf_attachment *db_attach)
404 struct vb2_dma_sg_attachment *attach = db_attach->priv;
405 struct sg_table *sgt;
412 /* release the scatterlist cache */
413 if (attach->dma_dir != DMA_NONE)
414 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
418 db_attach->priv = NULL;
421 static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
422 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
424 struct vb2_dma_sg_attachment *attach = db_attach->priv;
425 /* stealing dmabuf mutex to serialize map/unmap operations */
426 struct mutex *lock = &db_attach->dmabuf->lock;
427 struct sg_table *sgt;
432 /* return previously mapped sg table */
433 if (attach->dma_dir == dma_dir) {
438 /* release any previous cache */
439 if (attach->dma_dir != DMA_NONE) {
440 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
442 attach->dma_dir = DMA_NONE;
445 /* mapping to the client with new direction */
446 sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
449 pr_err("failed to map scatterlist\n");
451 return ERR_PTR(-EIO);
454 attach->dma_dir = dma_dir;
461 static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
462 struct sg_table *sgt, enum dma_data_direction dma_dir)
464 /* nothing to be done here */
467 static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf *dbuf)
469 /* drop reference obtained in vb2_dma_sg_get_dmabuf */
470 vb2_dma_sg_put(dbuf->priv);
473 static void *vb2_dma_sg_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
475 struct vb2_dma_sg_buf *buf = dbuf->priv;
477 return buf->vaddr ? buf->vaddr + pgnum * PAGE_SIZE : NULL;
480 static void *vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf *dbuf)
482 struct vb2_dma_sg_buf *buf = dbuf->priv;
484 return vb2_dma_sg_vaddr(buf);
487 static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf *dbuf,
488 struct vm_area_struct *vma)
490 return vb2_dma_sg_mmap(dbuf->priv, vma);
493 static const struct dma_buf_ops vb2_dma_sg_dmabuf_ops = {
494 .attach = vb2_dma_sg_dmabuf_ops_attach,
495 .detach = vb2_dma_sg_dmabuf_ops_detach,
496 .map_dma_buf = vb2_dma_sg_dmabuf_ops_map,
497 .unmap_dma_buf = vb2_dma_sg_dmabuf_ops_unmap,
498 .map = vb2_dma_sg_dmabuf_ops_kmap,
499 .vmap = vb2_dma_sg_dmabuf_ops_vmap,
500 .mmap = vb2_dma_sg_dmabuf_ops_mmap,
501 .release = vb2_dma_sg_dmabuf_ops_release,
504 static struct dma_buf *vb2_dma_sg_get_dmabuf(void *buf_priv, unsigned long flags)
506 struct vb2_dma_sg_buf *buf = buf_priv;
507 struct dma_buf *dbuf;
508 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
510 exp_info.ops = &vb2_dma_sg_dmabuf_ops;
511 exp_info.size = buf->size;
512 exp_info.flags = flags;
515 if (WARN_ON(!buf->dma_sgt))
518 dbuf = dma_buf_export(&exp_info);
522 /* dmabuf keeps reference to vb2 buffer */
523 refcount_inc(&buf->refcount);
528 /*********************************************/
529 /* callbacks for DMABUF buffers */
530 /*********************************************/
532 static int vb2_dma_sg_map_dmabuf(void *mem_priv)
534 struct vb2_dma_sg_buf *buf = mem_priv;
535 struct sg_table *sgt;
537 if (WARN_ON(!buf->db_attach)) {
538 pr_err("trying to pin a non attached buffer\n");
542 if (WARN_ON(buf->dma_sgt)) {
543 pr_err("dmabuf buffer is already pinned\n");
547 /* get the associated scatterlist for this buffer */
548 sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
550 pr_err("Error getting dmabuf scatterlist\n");
560 static void vb2_dma_sg_unmap_dmabuf(void *mem_priv)
562 struct vb2_dma_sg_buf *buf = mem_priv;
563 struct sg_table *sgt = buf->dma_sgt;
565 if (WARN_ON(!buf->db_attach)) {
566 pr_err("trying to unpin a not attached buffer\n");
571 pr_err("dmabuf buffer is already unpinned\n");
576 dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
579 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
584 static void vb2_dma_sg_detach_dmabuf(void *mem_priv)
586 struct vb2_dma_sg_buf *buf = mem_priv;
588 /* if vb2 works correctly you should never detach mapped buffer */
589 if (WARN_ON(buf->dma_sgt))
590 vb2_dma_sg_unmap_dmabuf(buf);
592 /* detach this attachment */
593 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
597 static void *vb2_dma_sg_attach_dmabuf(struct device *dev, struct dma_buf *dbuf,
598 unsigned long size, enum dma_data_direction dma_dir)
600 struct vb2_dma_sg_buf *buf;
601 struct dma_buf_attachment *dba;
604 return ERR_PTR(-EINVAL);
606 if (dbuf->size < size)
607 return ERR_PTR(-EFAULT);
609 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
611 return ERR_PTR(-ENOMEM);
614 /* create attachment for the dmabuf with the user device */
615 dba = dma_buf_attach(dbuf, buf->dev);
617 pr_err("failed to attach dmabuf\n");
622 buf->dma_dir = dma_dir;
624 buf->db_attach = dba;
629 static void *vb2_dma_sg_cookie(void *buf_priv)
631 struct vb2_dma_sg_buf *buf = buf_priv;
636 const struct vb2_mem_ops vb2_dma_sg_memops = {
637 .alloc = vb2_dma_sg_alloc,
638 .put = vb2_dma_sg_put,
639 .get_userptr = vb2_dma_sg_get_userptr,
640 .put_userptr = vb2_dma_sg_put_userptr,
641 .prepare = vb2_dma_sg_prepare,
642 .finish = vb2_dma_sg_finish,
643 .vaddr = vb2_dma_sg_vaddr,
644 .mmap = vb2_dma_sg_mmap,
645 .num_users = vb2_dma_sg_num_users,
646 .get_dmabuf = vb2_dma_sg_get_dmabuf,
647 .map_dmabuf = vb2_dma_sg_map_dmabuf,
648 .unmap_dmabuf = vb2_dma_sg_unmap_dmabuf,
649 .attach_dmabuf = vb2_dma_sg_attach_dmabuf,
650 .detach_dmabuf = vb2_dma_sg_detach_dmabuf,
651 .cookie = vb2_dma_sg_cookie,
653 EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
655 MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
656 MODULE_AUTHOR("Andrzej Pietrasiewicz");
657 MODULE_LICENSE("GPL");