2 * videobuf2-cma.c - CMA memory allocator for videobuf2
4 * Copyright (C) 2010 Samsung Electronics
6 * Author: Pawel Osciak <p.osciak@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/slab.h>
15 #include <linux/cma.h>
17 #include <linux/sched.h>
18 #include <linux/file.h>
20 #include <media/videobuf2-core.h>
21 #include <media/videobuf2-memops.h>
28 unsigned long alignment;
32 struct vb2_cma_conf *conf;
35 struct vm_area_struct *vma;
37 struct vb2_vmarea_handler handler;
40 static void vb2_cma_put(void *buf_priv);
42 static void *vb2_cma_alloc(void *alloc_ctx, unsigned long size)
44 struct vb2_cma_conf *conf = alloc_ctx;
45 struct vb2_cma_buf *buf;
47 buf = kzalloc(sizeof *buf, GFP_KERNEL);
49 return ERR_PTR(-ENOMEM);
51 buf->paddr = cma_alloc(conf->dev, conf->type, size, conf->alignment);
52 if (IS_ERR((void *)buf->paddr)) {
53 printk(KERN_ERR "cma_alloc of size %ld failed\n", size);
55 return ERR_PTR(-ENOMEM);
61 buf->handler.refcount = &buf->refcount;
62 buf->handler.put = vb2_cma_put;
63 buf->handler.arg = buf;
65 atomic_inc(&buf->refcount);
70 static void vb2_cma_put(void *buf_priv)
72 struct vb2_cma_buf *buf = buf_priv;
74 if (atomic_dec_and_test(&buf->refcount)) {
80 static void *vb2_cma_cookie(void *buf_priv)
82 struct vb2_cma_buf *buf = buf_priv;
84 return (void *)buf->paddr;
87 static unsigned int vb2_cma_num_users(void *buf_priv)
89 struct vb2_cma_buf *buf = buf_priv;
91 return atomic_read(&buf->refcount);
94 static int vb2_cma_mmap(void *buf_priv, struct vm_area_struct *vma)
96 struct vb2_cma_buf *buf = buf_priv;
99 printk(KERN_ERR "No buffer to map\n");
103 return vb2_mmap_pfn_range(vma, buf->paddr, buf->size,
104 &vb2_common_vm_ops, &buf->handler);
107 static void *vb2_cma_get_userptr(void *alloc_ctx, unsigned long vaddr,
108 unsigned long size, int write)
110 struct vb2_cma_buf *buf;
111 struct vm_area_struct *vma;
112 dma_addr_t paddr = 0;
115 buf = kzalloc(sizeof *buf, GFP_KERNEL);
117 return ERR_PTR(-ENOMEM);
119 ret = vb2_get_contig_userptr(vaddr, size, &vma, &paddr);
121 printk(KERN_ERR "Failed acquiring VMA for vaddr 0x%08lx\n",
134 static void vb2_cma_put_userptr(void *mem_priv)
136 struct vb2_cma_buf *buf = mem_priv;
141 vb2_put_vma(buf->vma);
145 static void *vb2_cma_vaddr(void *mem_priv)
147 struct vb2_cma_buf *buf = mem_priv;
151 return phys_to_virt(buf->paddr);
154 const struct vb2_mem_ops vb2_cma_memops = {
155 .alloc = vb2_cma_alloc,
157 .cookie = vb2_cma_cookie,
158 .mmap = vb2_cma_mmap,
159 .get_userptr = vb2_cma_get_userptr,
160 .put_userptr = vb2_cma_put_userptr,
161 .num_users = vb2_cma_num_users,
162 .vaddr = vb2_cma_vaddr,
164 EXPORT_SYMBOL_GPL(vb2_cma_memops);
166 void *vb2_cma_init(struct device *dev, const char *type,
167 unsigned long alignment)
169 struct vb2_cma_conf *conf;
171 conf = kzalloc(sizeof *conf, GFP_KERNEL);
173 return ERR_PTR(-ENOMEM);
177 conf->alignment = alignment;
181 EXPORT_SYMBOL_GPL(vb2_cma_init);
183 void vb2_cma_cleanup(void *conf)
187 EXPORT_SYMBOL_GPL(vb2_cma_cleanup);
189 void **vb2_cma_init_multi(struct device *dev,
190 unsigned int num_planes,
192 unsigned long alignments[])
194 struct vb2_cma_conf *cma_conf;
198 alloc_ctxes = kzalloc((sizeof *alloc_ctxes + sizeof *cma_conf)
199 * num_planes, GFP_KERNEL);
201 return ERR_PTR(-ENOMEM);
203 cma_conf = (void *)(alloc_ctxes + num_planes);
205 for (i = 0; i < num_planes; ++i, ++cma_conf) {
206 alloc_ctxes[i] = cma_conf;
208 cma_conf->type = types[i];
209 cma_conf->alignment = alignments[i];
214 EXPORT_SYMBOL_GPL(vb2_cma_init_multi);
216 void vb2_cma_cleanup_multi(void **alloc_ctxes)
220 EXPORT_SYMBOL_GPL(vb2_cma_cleanup_multi);
222 MODULE_DESCRIPTION("CMA allocator handling routines for videobuf2");
223 MODULE_AUTHOR("Pawel Osciak");
224 MODULE_LICENSE("GPL");