blob: 4159056c68146fc45c33e8a7a29e60f421f7bff0 [file] [log] [blame]
Mingcheng Zhu9812bd32011-07-22 22:57:11 -07001/* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
2 *
3 * Based on videobuf-dma-contig.c,
4 * (c) 2008 Magnus Damm
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 and
8 * only version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * helper functions for physically contiguous pmem capture buffers
16 * The functions support contiguous memory allocations using pmem
17 * kernel API.
18 */
19
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/mm.h>
23#include <linux/slab.h>
24#include <linux/pagemap.h>
25#include <linux/sched.h>
26#include <linux/io.h>
27#include <linux/android_pmem.h>
28#include <linux/memory_alloc.h>
29#include <media/videobuf2-msm-mem.h>
30#include <media/msm_camera.h>
31#include <mach/memory.h>
Ankit Premrajkac6864b82011-07-15 11:43:41 -070032#include <mach/msm_subsystem_map.h>
Mingcheng Zhu9812bd32011-07-22 22:57:11 -070033#include <media/videobuf2-core.h>
34
35#define MAGIC_PMEM 0x0733ac64
36#define MAGIC_CHECK(is, should) \
37 if (unlikely((is) != (should))) { \
38 pr_err("magic mismatch: %x expected %x\n", (is), (should)); \
39 BUG(); \
40 }
41
42#ifdef CONFIG_MSM_CAMERA_DEBUG
43#define D(fmt, args...) pr_debug("videobuf-msm-mem: " fmt, ##args)
44#else
45#define D(fmt, args...) do {} while (0)
46#endif
47
48static int32_t msm_mem_allocate(const size_t size)
49{
50 int32_t phyaddr;
51 phyaddr = allocate_contiguous_ebi_nomap(size, SZ_4K);
52 return phyaddr;
53}
54
55static int32_t msm_mem_free(const int32_t phyaddr)
56{
57 int32_t rc = 0;
58 free_contiguous_memory_by_paddr(phyaddr);
59 return rc;
60}
61
62static void videobuf2_vm_close(struct vm_area_struct *vma)
63{
64 struct videobuf2_contig_pmem *mem = vma->vm_private_data;
65 D("vm_close %p [count=%u,vma=%08lx-%08lx]\n",
66 mem, mem->count, vma->vm_start, vma->vm_end);
67 mem->count--;
68}
69static void videobuf2_vm_open(struct vm_area_struct *vma)
70{
71 struct videobuf2_contig_pmem *mem = vma->vm_private_data;
72 D("vm_open %p [count=%u,vma=%08lx-%08lx]\n",
73 mem, mem->count, vma->vm_start, vma->vm_end);
74 mem->count++;
75}
76
77static const struct vm_operations_struct videobuf2_vm_ops = {
78 .open = videobuf2_vm_open,
79 .close = videobuf2_vm_close,
80};
81
82static void *msm_vb2_mem_ops_alloc(void *alloc_ctx, unsigned long size)
83{
84 struct videobuf2_contig_pmem *mem;
Ankit Premrajkac6864b82011-07-15 11:43:41 -070085 unsigned int flags = 0;
86 long rc;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -070087 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
88 if (!mem)
89 return ERR_PTR(-ENOMEM);
90
91 mem->magic = MAGIC_PMEM;
92 mem->size = PAGE_ALIGN(size);
93 mem->alloc_ctx = alloc_ctx;
94 mem->is_userptr = 0;
95 mem->phyaddr = msm_mem_allocate(mem->size);
96 if (IS_ERR((void *)mem->phyaddr)) {
97 pr_err("%s : pmem memory allocation failed\n", __func__);
98 kfree(mem);
99 return ERR_PTR(-ENOMEM);
100 }
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700101 flags = MSM_SUBSYSTEM_MAP_IOVA;
102 mem->subsys_id = MSM_SUBSYSTEM_CAMERA;
103 mem->msm_buffer = msm_subsystem_map_buffer(mem->phyaddr, mem->size,
104 flags, &(mem->subsys_id), 1);
105 if (IS_ERR((void *)mem->msm_buffer)) {
106 pr_err("%s: msm_subsystem_map_buffer failed\n", __func__);
107 rc = PTR_ERR((void *)mem->msm_buffer);
108 msm_mem_free(mem->phyaddr);
109 kfree(mem);
110 return ERR_PTR(-ENOMEM);
111 }
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700112 return mem;
113}
114static void msm_vb2_mem_ops_put(void *buf_priv)
115{
116 struct videobuf2_contig_pmem *mem = buf_priv;
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700117 if (!mem->is_userptr) {
118 D("%s Freeing memory ", __func__);
119 if (msm_subsystem_unmap_buffer(mem->msm_buffer) < 0)
120 D("%s unmapped memory\n", __func__);
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700121 msm_mem_free(mem->phyaddr);
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700122 }
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700123 kfree(mem);
124}
125int videobuf2_pmem_contig_mmap_get(struct videobuf2_contig_pmem *mem,
126 uint32_t yoffset,
127 uint32_t cbcroffset, int path)
128{
129 mem->y_off = yoffset;
130 mem->cbcr_off = cbcroffset;
131 mem->buffer_type = path;
132 return 0;
133}
134EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_mmap_get);
135
136/**
137 * videobuf_pmem_contig_user_get() - setup user space memory pointer
138 * @mem: per-buffer private videobuf-contig-pmem data
139 * @vb: video buffer to map
140 *
141 * This function validates and sets up a pointer to user space memory.
142 * Only physically contiguous pfn-mapped memory is accepted.
143 *
144 * Returns 0 if successful.
145 */
146int videobuf2_pmem_contig_user_get(struct videobuf2_contig_pmem *mem,
147 uint32_t yoffset, uint32_t cbcroffset,
148 uint32_t addr_offset, int path)
149{
150 unsigned long kvstart;
151 unsigned long len;
152 int rc;
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700153 unsigned int flags = 0;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700154
155 if (mem->phyaddr != 0)
156 return 0;
157
158 rc = get_pmem_file((int)mem->vaddr, (unsigned long *)&mem->phyaddr,
159 &kvstart, &len, &mem->file);
160 if (rc < 0) {
161 pr_err("%s: get_pmem_file fd %d error %d\n",
162 __func__, (int)mem->vaddr, rc);
163 return rc;
164 }
165 mem->phyaddr += addr_offset;
166 mem->y_off = yoffset;
167 mem->cbcr_off = cbcroffset;
168 mem->buffer_type = path;
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700169 flags = MSM_SUBSYSTEM_MAP_IOVA;
170 mem->subsys_id = MSM_SUBSYSTEM_CAMERA;
Ankit Premrajka1b9b9a42011-08-05 15:49:42 -0700171 mem->msm_buffer = msm_subsystem_map_buffer(mem->phyaddr, len,
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700172 flags, &(mem->subsys_id), 1);
173 if (IS_ERR((void *)mem->msm_buffer)) {
174 pr_err("%s: msm_subsystem_map_buffer failed\n", __func__);
175 put_pmem_file(mem->file);
176 return PTR_ERR((void *)mem->msm_buffer);
177 }
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700178 return rc;
179}
180EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_user_get);
181
182void videobuf2_pmem_contig_user_put(struct videobuf2_contig_pmem *mem)
183{
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700184 if (msm_subsystem_unmap_buffer(mem->msm_buffer) < 0)
185 D("%s unmapped memory\n", __func__);
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700186 if (mem->is_userptr)
187 put_pmem_file(mem->file);
188 mem->is_userptr = 0;
189 mem->phyaddr = 0;
190 mem->size = 0;
191}
192EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_user_put);
193
194static void *msm_vb2_mem_ops_get_userptr(void *alloc_ctx, unsigned long vaddr,
195 unsigned long size, int write)
196{
197 struct videobuf2_contig_pmem *mem;
198 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
199 if (!mem)
200 return ERR_PTR(-ENOMEM);
201 mem->magic = MAGIC_PMEM;
202 mem->is_userptr = 1;
203 mem->vaddr = (void *)vaddr;
204 mem->size = size;
205 mem->alloc_ctx = alloc_ctx;
206 return mem;
207}
208static void msm_vb2_mem_ops_put_userptr(void *buf_priv)
209{
210 kfree(buf_priv);
211}
212
213static void *msm_vb2_mem_ops_vaddr(void *buf_priv)
214{
215 struct videobuf2_contig_pmem *mem = buf_priv;
216 return mem->vaddr;
217}
218static void *msm_vb2_mem_ops_cookie(void *buf_priv)
219{
220 return buf_priv;
221}
222static unsigned int msm_vb2_mem_ops_num_users(void *buf_priv)
223{
224 struct videobuf2_contig_pmem *mem = buf_priv;
225 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
226 return mem->count;
227}
228static int msm_vb2_mem_ops_mmap(void *buf_priv, struct vm_area_struct *vma)
229{
230 struct videobuf2_contig_pmem *mem;
231 int retval;
232 unsigned long size;
233 D("%s\n", __func__);
234 mem = buf_priv;
235 D("mem = 0x%x\n", (u32)mem);
236 BUG_ON(!mem);
237 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700238 /* Try to remap memory */
239 size = vma->vm_end - vma->vm_start;
240 size = (size < mem->size) ? size : mem->size;
241 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
242 retval = remap_pfn_range(vma, vma->vm_start,
243 mem->phyaddr >> PAGE_SHIFT,
244 size, vma->vm_page_prot);
245 if (retval) {
246 pr_err("mmap: remap failed with error %d. ", retval);
247 goto error;
248 }
249 mem->vaddr = (void *)vma->vm_start;
250 vma->vm_ops = &videobuf2_vm_ops;
251 vma->vm_flags |= VM_DONTEXPAND;
252 vma->vm_private_data = mem;
253
254 D("mmap %p: %08lx-%08lx (%lx) pgoff %08lx\n",
255 map, vma->vm_start, vma->vm_end,
256 (long int)mem->bsize, vma->vm_pgoff);
257 videobuf2_vm_open(vma);
258 return 0;
259error:
260 return -ENOMEM;
261}
262
263static struct vb2_mem_ops msm_vb2_mem_ops = {
264 .alloc = msm_vb2_mem_ops_alloc,
265 .put = msm_vb2_mem_ops_put,
266 .get_userptr = msm_vb2_mem_ops_get_userptr,
267 .put_userptr = msm_vb2_mem_ops_put_userptr,
268 .vaddr = msm_vb2_mem_ops_vaddr,
269 .cookie = msm_vb2_mem_ops_cookie,
270 .num_users = msm_vb2_mem_ops_num_users,
271 .mmap = msm_vb2_mem_ops_mmap
272};
273
274void videobuf2_queue_pmem_contig_init(struct vb2_queue *q,
275 enum v4l2_buf_type type,
276 const struct vb2_ops *ops,
277 unsigned int size,
278 void *priv)
279{
280 memset(q, 0, sizeof(struct vb2_queue));
281 q->mem_ops = &msm_vb2_mem_ops;
282 q->ops = ops;
283 q->drv_priv = priv;
284 q->type = type;
285 q->io_modes = VB2_MMAP | VB2_USERPTR;
286 q->io_flags = 0;
287 q->buf_struct_size = size;
288 vb2_queue_init(q);
289}
290EXPORT_SYMBOL_GPL(videobuf2_queue_pmem_contig_init);
291
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700292unsigned long videobuf2_to_pmem_contig(struct vb2_buffer *vb,
293 unsigned int plane_no)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700294{
295 struct videobuf2_contig_pmem *mem;
296 mem = vb2_plane_cookie(vb, plane_no);
297 BUG_ON(!mem);
298 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700299 return mem->msm_buffer->iova[0];
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700300}
301EXPORT_SYMBOL_GPL(videobuf2_to_pmem_contig);
302
303MODULE_DESCRIPTION("helper module to manage video4linux PMEM contig buffers");
304MODULE_LICENSE("GPL v2");