blob: d7652446f7582bee237e3e51a61d269d9914864d [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 N5e08d772011-10-03 10:19:15 -0700127 struct videobuf2_msm_offset *offset,
128 enum videobuf2_buffer_type buffer_type,
129 int path)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700130{
Kiran Kumar H N5e08d772011-10-03 10:19:15 -0700131 if (offset)
132 mem->offset = *offset;
133 else
134 memset(&mem->offset, 0, sizeof(struct videobuf2_msm_offset));
135 mem->buffer_type = buffer_type;
136 mem->path = path;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700137 return 0;
138}
139EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_mmap_get);
140
141/**
142 * videobuf_pmem_contig_user_get() - setup user space memory pointer
143 * @mem: per-buffer private videobuf-contig-pmem data
144 * @vb: video buffer to map
145 *
146 * This function validates and sets up a pointer to user space memory.
147 * Only physically contiguous pfn-mapped memory is accepted.
148 *
149 * Returns 0 if successful.
150 */
151int videobuf2_pmem_contig_user_get(struct videobuf2_contig_pmem *mem,
Kiran Kumar H N5e08d772011-10-03 10:19:15 -0700152 struct videobuf2_msm_offset *offset,
153 enum videobuf2_buffer_type buffer_type,
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700154 uint32_t addr_offset, int path)
155{
156 unsigned long kvstart;
157 unsigned long len;
158 int rc;
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700159 unsigned int flags = 0;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700160
161 if (mem->phyaddr != 0)
162 return 0;
163
164 rc = get_pmem_file((int)mem->vaddr, (unsigned long *)&mem->phyaddr,
165 &kvstart, &len, &mem->file);
166 if (rc < 0) {
167 pr_err("%s: get_pmem_file fd %d error %d\n",
168 __func__, (int)mem->vaddr, rc);
169 return rc;
170 }
Kiran Kumar H N5e08d772011-10-03 10:19:15 -0700171 if (offset)
172 mem->offset = *offset;
173 else
174 memset(&mem->offset, 0, sizeof(struct videobuf2_msm_offset));
175 mem->path = path;
176 mem->buffer_type = buffer_type;
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700177 flags = MSM_SUBSYSTEM_MAP_IOVA;
178 mem->subsys_id = MSM_SUBSYSTEM_CAMERA;
Ankit Premrajka1b9b9a42011-08-05 15:49:42 -0700179 mem->msm_buffer = msm_subsystem_map_buffer(mem->phyaddr, len,
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700180 flags, &(mem->subsys_id), 1);
181 if (IS_ERR((void *)mem->msm_buffer)) {
182 pr_err("%s: msm_subsystem_map_buffer failed\n", __func__);
183 put_pmem_file(mem->file);
184 return PTR_ERR((void *)mem->msm_buffer);
185 }
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700186 mem->mapped_phyaddr = mem->msm_buffer->iova[0] + addr_offset;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700187 return rc;
188}
189EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_user_get);
190
191void videobuf2_pmem_contig_user_put(struct videobuf2_contig_pmem *mem)
192{
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700193 if (msm_subsystem_unmap_buffer(mem->msm_buffer) < 0)
194 D("%s unmapped memory\n", __func__);
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700195 if (mem->is_userptr)
196 put_pmem_file(mem->file);
197 mem->is_userptr = 0;
198 mem->phyaddr = 0;
199 mem->size = 0;
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700200 mem->mapped_phyaddr = 0;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700201}
202EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_user_put);
203
204static void *msm_vb2_mem_ops_get_userptr(void *alloc_ctx, unsigned long vaddr,
205 unsigned long size, int write)
206{
207 struct videobuf2_contig_pmem *mem;
208 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
209 if (!mem)
210 return ERR_PTR(-ENOMEM);
211 mem->magic = MAGIC_PMEM;
212 mem->is_userptr = 1;
213 mem->vaddr = (void *)vaddr;
214 mem->size = size;
215 mem->alloc_ctx = alloc_ctx;
216 return mem;
217}
218static void msm_vb2_mem_ops_put_userptr(void *buf_priv)
219{
220 kfree(buf_priv);
221}
222
223static void *msm_vb2_mem_ops_vaddr(void *buf_priv)
224{
225 struct videobuf2_contig_pmem *mem = buf_priv;
226 return mem->vaddr;
227}
228static void *msm_vb2_mem_ops_cookie(void *buf_priv)
229{
230 return buf_priv;
231}
232static unsigned int msm_vb2_mem_ops_num_users(void *buf_priv)
233{
234 struct videobuf2_contig_pmem *mem = buf_priv;
235 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
236 return mem->count;
237}
238static int msm_vb2_mem_ops_mmap(void *buf_priv, struct vm_area_struct *vma)
239{
240 struct videobuf2_contig_pmem *mem;
241 int retval;
242 unsigned long size;
243 D("%s\n", __func__);
244 mem = buf_priv;
245 D("mem = 0x%x\n", (u32)mem);
246 BUG_ON(!mem);
247 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700248 /* Try to remap memory */
249 size = vma->vm_end - vma->vm_start;
250 size = (size < mem->size) ? size : mem->size;
251 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
252 retval = remap_pfn_range(vma, vma->vm_start,
253 mem->phyaddr >> PAGE_SHIFT,
254 size, vma->vm_page_prot);
255 if (retval) {
256 pr_err("mmap: remap failed with error %d. ", retval);
257 goto error;
258 }
259 mem->vaddr = (void *)vma->vm_start;
260 vma->vm_ops = &videobuf2_vm_ops;
261 vma->vm_flags |= VM_DONTEXPAND;
262 vma->vm_private_data = mem;
263
264 D("mmap %p: %08lx-%08lx (%lx) pgoff %08lx\n",
265 map, vma->vm_start, vma->vm_end,
266 (long int)mem->bsize, vma->vm_pgoff);
267 videobuf2_vm_open(vma);
268 return 0;
269error:
270 return -ENOMEM;
271}
272
273static struct vb2_mem_ops msm_vb2_mem_ops = {
274 .alloc = msm_vb2_mem_ops_alloc,
275 .put = msm_vb2_mem_ops_put,
276 .get_userptr = msm_vb2_mem_ops_get_userptr,
277 .put_userptr = msm_vb2_mem_ops_put_userptr,
278 .vaddr = msm_vb2_mem_ops_vaddr,
279 .cookie = msm_vb2_mem_ops_cookie,
280 .num_users = msm_vb2_mem_ops_num_users,
281 .mmap = msm_vb2_mem_ops_mmap
282};
283
284void videobuf2_queue_pmem_contig_init(struct vb2_queue *q,
285 enum v4l2_buf_type type,
286 const struct vb2_ops *ops,
287 unsigned int size,
288 void *priv)
289{
290 memset(q, 0, sizeof(struct vb2_queue));
291 q->mem_ops = &msm_vb2_mem_ops;
292 q->ops = ops;
293 q->drv_priv = priv;
294 q->type = type;
295 q->io_modes = VB2_MMAP | VB2_USERPTR;
296 q->io_flags = 0;
297 q->buf_struct_size = size;
298 vb2_queue_init(q);
299}
300EXPORT_SYMBOL_GPL(videobuf2_queue_pmem_contig_init);
301
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700302unsigned long videobuf2_to_pmem_contig(struct vb2_buffer *vb,
303 unsigned int plane_no)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700304{
305 struct videobuf2_contig_pmem *mem;
306 mem = vb2_plane_cookie(vb, plane_no);
307 BUG_ON(!mem);
308 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700309 return mem->mapped_phyaddr;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700310}
311EXPORT_SYMBOL_GPL(videobuf2_to_pmem_contig);
312
313MODULE_DESCRIPTION("helper module to manage video4linux PMEM contig buffers");
314MODULE_LICENSE("GPL v2");