blob: 74debe18067643451168dac0f1cb7ed0f23341e6 [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);
Ankit Premrajkaa5ea3a92011-08-04 17:54:17 -070096 if (!mem->phyaddr) {
Mingcheng Zhu9812bd32011-07-22 22:57:11 -070097 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 }
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700112 mem->mapped_phyaddr = mem->msm_buffer->iova[0];
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700113 return mem;
114}
115static void msm_vb2_mem_ops_put(void *buf_priv)
116{
117 struct videobuf2_contig_pmem *mem = buf_priv;
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700118 if (!mem->is_userptr) {
119 D("%s Freeing memory ", __func__);
120 if (msm_subsystem_unmap_buffer(mem->msm_buffer) < 0)
121 D("%s unmapped memory\n", __func__);
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700122 msm_mem_free(mem->phyaddr);
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700123 }
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700124 kfree(mem);
125}
126int videobuf2_pmem_contig_mmap_get(struct videobuf2_contig_pmem *mem,
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700127 uint32_t offset, int path)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700128{
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700129 mem->offset = offset;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700130 mem->buffer_type = path;
131 return 0;
132}
133EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_mmap_get);
134
135/**
136 * videobuf_pmem_contig_user_get() - setup user space memory pointer
137 * @mem: per-buffer private videobuf-contig-pmem data
138 * @vb: video buffer to map
139 *
140 * This function validates and sets up a pointer to user space memory.
141 * Only physically contiguous pfn-mapped memory is accepted.
142 *
143 * Returns 0 if successful.
144 */
145int videobuf2_pmem_contig_user_get(struct videobuf2_contig_pmem *mem,
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700146 uint32_t offset,
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700147 uint32_t addr_offset, int path)
148{
149 unsigned long kvstart;
150 unsigned long len;
151 int rc;
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700152 unsigned int flags = 0;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700153
154 if (mem->phyaddr != 0)
155 return 0;
156
157 rc = get_pmem_file((int)mem->vaddr, (unsigned long *)&mem->phyaddr,
158 &kvstart, &len, &mem->file);
159 if (rc < 0) {
160 pr_err("%s: get_pmem_file fd %d error %d\n",
161 __func__, (int)mem->vaddr, rc);
162 return rc;
163 }
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700164 mem->offset = offset;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700165 mem->buffer_type = path;
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700166 flags = MSM_SUBSYSTEM_MAP_IOVA;
167 mem->subsys_id = MSM_SUBSYSTEM_CAMERA;
Ankit Premrajka1b9b9a42011-08-05 15:49:42 -0700168 mem->msm_buffer = msm_subsystem_map_buffer(mem->phyaddr, len,
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700169 flags, &(mem->subsys_id), 1);
170 if (IS_ERR((void *)mem->msm_buffer)) {
171 pr_err("%s: msm_subsystem_map_buffer failed\n", __func__);
172 put_pmem_file(mem->file);
173 return PTR_ERR((void *)mem->msm_buffer);
174 }
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700175 mem->mapped_phyaddr = mem->msm_buffer->iova[0] + addr_offset;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700176 return rc;
177}
178EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_user_get);
179
180void videobuf2_pmem_contig_user_put(struct videobuf2_contig_pmem *mem)
181{
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700182 if (msm_subsystem_unmap_buffer(mem->msm_buffer) < 0)
183 D("%s unmapped memory\n", __func__);
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700184 if (mem->is_userptr)
185 put_pmem_file(mem->file);
186 mem->is_userptr = 0;
187 mem->phyaddr = 0;
188 mem->size = 0;
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700189 mem->mapped_phyaddr = 0;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700190}
191EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_user_put);
192
193static void *msm_vb2_mem_ops_get_userptr(void *alloc_ctx, unsigned long vaddr,
194 unsigned long size, int write)
195{
196 struct videobuf2_contig_pmem *mem;
197 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
198 if (!mem)
199 return ERR_PTR(-ENOMEM);
200 mem->magic = MAGIC_PMEM;
201 mem->is_userptr = 1;
202 mem->vaddr = (void *)vaddr;
203 mem->size = size;
204 mem->alloc_ctx = alloc_ctx;
205 return mem;
206}
207static void msm_vb2_mem_ops_put_userptr(void *buf_priv)
208{
209 kfree(buf_priv);
210}
211
212static void *msm_vb2_mem_ops_vaddr(void *buf_priv)
213{
214 struct videobuf2_contig_pmem *mem = buf_priv;
215 return mem->vaddr;
216}
217static void *msm_vb2_mem_ops_cookie(void *buf_priv)
218{
219 return buf_priv;
220}
221static unsigned int msm_vb2_mem_ops_num_users(void *buf_priv)
222{
223 struct videobuf2_contig_pmem *mem = buf_priv;
224 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
225 return mem->count;
226}
227static int msm_vb2_mem_ops_mmap(void *buf_priv, struct vm_area_struct *vma)
228{
229 struct videobuf2_contig_pmem *mem;
230 int retval;
231 unsigned long size;
232 D("%s\n", __func__);
233 mem = buf_priv;
234 D("mem = 0x%x\n", (u32)mem);
235 BUG_ON(!mem);
236 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700237 /* Try to remap memory */
238 size = vma->vm_end - vma->vm_start;
239 size = (size < mem->size) ? size : mem->size;
240 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
241 retval = remap_pfn_range(vma, vma->vm_start,
242 mem->phyaddr >> PAGE_SHIFT,
243 size, vma->vm_page_prot);
244 if (retval) {
245 pr_err("mmap: remap failed with error %d. ", retval);
246 goto error;
247 }
248 mem->vaddr = (void *)vma->vm_start;
249 vma->vm_ops = &videobuf2_vm_ops;
250 vma->vm_flags |= VM_DONTEXPAND;
251 vma->vm_private_data = mem;
252
253 D("mmap %p: %08lx-%08lx (%lx) pgoff %08lx\n",
254 map, vma->vm_start, vma->vm_end,
255 (long int)mem->bsize, vma->vm_pgoff);
256 videobuf2_vm_open(vma);
257 return 0;
258error:
259 return -ENOMEM;
260}
261
262static struct vb2_mem_ops msm_vb2_mem_ops = {
263 .alloc = msm_vb2_mem_ops_alloc,
264 .put = msm_vb2_mem_ops_put,
265 .get_userptr = msm_vb2_mem_ops_get_userptr,
266 .put_userptr = msm_vb2_mem_ops_put_userptr,
267 .vaddr = msm_vb2_mem_ops_vaddr,
268 .cookie = msm_vb2_mem_ops_cookie,
269 .num_users = msm_vb2_mem_ops_num_users,
270 .mmap = msm_vb2_mem_ops_mmap
271};
272
273void videobuf2_queue_pmem_contig_init(struct vb2_queue *q,
274 enum v4l2_buf_type type,
275 const struct vb2_ops *ops,
276 unsigned int size,
277 void *priv)
278{
279 memset(q, 0, sizeof(struct vb2_queue));
280 q->mem_ops = &msm_vb2_mem_ops;
281 q->ops = ops;
282 q->drv_priv = priv;
283 q->type = type;
284 q->io_modes = VB2_MMAP | VB2_USERPTR;
285 q->io_flags = 0;
286 q->buf_struct_size = size;
287 vb2_queue_init(q);
288}
289EXPORT_SYMBOL_GPL(videobuf2_queue_pmem_contig_init);
290
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700291unsigned long videobuf2_to_pmem_contig(struct vb2_buffer *vb,
292 unsigned int plane_no)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700293{
294 struct videobuf2_contig_pmem *mem;
295 mem = vb2_plane_cookie(vb, plane_no);
296 BUG_ON(!mem);
297 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700298 return mem->mapped_phyaddr;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700299}
300EXPORT_SYMBOL_GPL(videobuf2_to_pmem_contig);
301
302MODULE_DESCRIPTION("helper module to manage video4linux PMEM contig buffers");
303MODULE_LICENSE("GPL v2");