blob: 186195d8e04725ab4c250488c0d5d262e70f9a30 [file] [log] [blame]
Ankit Premrajkad8147432012-01-06 09:45:11 -08001/* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Mingcheng Zhu9812bd32011-07-22 22:57:11 -07002 *
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>
Mingcheng Zhu9812bd32011-07-22 22:57:11 -070032#include <media/videobuf2-core.h>
Laura Abbott5b1e6f12012-05-28 08:13:55 -070033#include <mach/iommu_domains.h>
Mingcheng Zhu9812bd32011-07-22 22:57:11 -070034
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
Ankit Premrajka45942962012-02-15 19:21:25 -080048static unsigned long msm_mem_allocate(struct videobuf2_contig_pmem *mem)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -070049{
Ankit Premrajka45942962012-02-15 19:21:25 -080050 unsigned long phyaddr;
Ankit Premrajkad8147432012-01-06 09:45:11 -080051#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
52 int rc, len;
53 mem->client = msm_ion_client_create(-1, "camera");
54 if (IS_ERR((void *)mem->client)) {
55 pr_err("%s Could not create client\n", __func__);
56 goto client_failed;
57 }
58 mem->ion_handle = ion_alloc(mem->client, mem->size, SZ_4K,
59 (0x1 << ION_CP_MM_HEAP_ID | 0x1 << ION_IOMMU_HEAP_ID));
60 if (IS_ERR((void *)mem->ion_handle)) {
61 pr_err("%s Could not allocate\n", __func__);
62 goto alloc_failed;
63 }
Ankit Premrajka45942962012-02-15 19:21:25 -080064 rc = ion_map_iommu(mem->client, mem->ion_handle,
65 CAMERA_DOMAIN, GEN_POOL, SZ_4K, 0,
66 (unsigned long *)&phyaddr,
67 (unsigned long *)&len, UNCACHED, 0);
Ankit Premrajkad8147432012-01-06 09:45:11 -080068 if (rc < 0) {
69 pr_err("%s Could not get physical address\n", __func__);
70 goto phys_failed;
71 }
72#else
73 phyaddr = allocate_contiguous_ebi_nomap(mem->size, SZ_4K);
74#endif
Mingcheng Zhu9812bd32011-07-22 22:57:11 -070075 return phyaddr;
Ankit Premrajkad8147432012-01-06 09:45:11 -080076#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
77phys_failed:
78 ion_free(mem->client, mem->ion_handle);
79alloc_failed:
80 ion_client_destroy(mem->client);
81client_failed:
82 return 0;
83#endif
Mingcheng Zhu9812bd32011-07-22 22:57:11 -070084}
85
Ankit Premrajkad8147432012-01-06 09:45:11 -080086static int32_t msm_mem_free(struct videobuf2_contig_pmem *mem)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -070087{
88 int32_t rc = 0;
Ankit Premrajkad8147432012-01-06 09:45:11 -080089#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
Ankit Premrajka45942962012-02-15 19:21:25 -080090 ion_unmap_iommu(mem->client, mem->ion_handle, CAMERA_DOMAIN, GEN_POOL);
Ankit Premrajkad8147432012-01-06 09:45:11 -080091 ion_free(mem->client, mem->ion_handle);
92 ion_client_destroy(mem->client);
93#else
94 free_contiguous_memory_by_paddr(mem->phyaddr);
95#endif
Mingcheng Zhu9812bd32011-07-22 22:57:11 -070096 return rc;
97}
98
99static void videobuf2_vm_close(struct vm_area_struct *vma)
100{
101 struct videobuf2_contig_pmem *mem = vma->vm_private_data;
102 D("vm_close %p [count=%u,vma=%08lx-%08lx]\n",
103 mem, mem->count, vma->vm_start, vma->vm_end);
104 mem->count--;
105}
106static void videobuf2_vm_open(struct vm_area_struct *vma)
107{
108 struct videobuf2_contig_pmem *mem = vma->vm_private_data;
109 D("vm_open %p [count=%u,vma=%08lx-%08lx]\n",
110 mem, mem->count, vma->vm_start, vma->vm_end);
111 mem->count++;
112}
113
114static const struct vm_operations_struct videobuf2_vm_ops = {
115 .open = videobuf2_vm_open,
116 .close = videobuf2_vm_close,
117};
118
119static void *msm_vb2_mem_ops_alloc(void *alloc_ctx, unsigned long size)
120{
121 struct videobuf2_contig_pmem *mem;
122 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
123 if (!mem)
124 return ERR_PTR(-ENOMEM);
125
126 mem->magic = MAGIC_PMEM;
127 mem->size = PAGE_ALIGN(size);
128 mem->alloc_ctx = alloc_ctx;
129 mem->is_userptr = 0;
Ankit Premrajkad8147432012-01-06 09:45:11 -0800130 mem->phyaddr = msm_mem_allocate(mem);
Ankit Premrajkaa5ea3a92011-08-04 17:54:17 -0700131 if (!mem->phyaddr) {
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700132 pr_err("%s : pmem memory allocation failed\n", __func__);
133 kfree(mem);
134 return ERR_PTR(-ENOMEM);
135 }
Ankit Premrajka45942962012-02-15 19:21:25 -0800136 mem->mapped_phyaddr = mem->phyaddr;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700137 return mem;
138}
139static void msm_vb2_mem_ops_put(void *buf_priv)
140{
141 struct videobuf2_contig_pmem *mem = buf_priv;
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700142 if (!mem->is_userptr) {
143 D("%s Freeing memory ", __func__);
Ankit Premrajkad8147432012-01-06 09:45:11 -0800144 msm_mem_free(mem);
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700145 }
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700146 kfree(mem);
147}
148int videobuf2_pmem_contig_mmap_get(struct videobuf2_contig_pmem *mem,
Kiran Kumar H N5e08d772011-10-03 10:19:15 -0700149 struct videobuf2_msm_offset *offset,
150 enum videobuf2_buffer_type buffer_type,
151 int path)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700152{
Kiran Kumar H N5e08d772011-10-03 10:19:15 -0700153 if (offset)
154 mem->offset = *offset;
155 else
156 memset(&mem->offset, 0, sizeof(struct videobuf2_msm_offset));
157 mem->buffer_type = buffer_type;
158 mem->path = path;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700159 return 0;
160}
161EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_mmap_get);
162
163/**
164 * videobuf_pmem_contig_user_get() - setup user space memory pointer
165 * @mem: per-buffer private videobuf-contig-pmem data
166 * @vb: video buffer to map
167 *
168 * This function validates and sets up a pointer to user space memory.
169 * Only physically contiguous pfn-mapped memory is accepted.
170 *
171 * Returns 0 if successful.
172 */
173int videobuf2_pmem_contig_user_get(struct videobuf2_contig_pmem *mem,
Kiran Kumar H N5e08d772011-10-03 10:19:15 -0700174 struct videobuf2_msm_offset *offset,
175 enum videobuf2_buffer_type buffer_type,
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700176 uint32_t addr_offset, int path,
177 struct ion_client *client)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700178{
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700179 unsigned long len;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700180 int rc = 0;
181#ifndef CONFIG_MSM_MULTIMEDIA_USE_ION
182 unsigned long kvstart;
183#endif
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700184 unsigned long paddr = 0;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700185 if (mem->phyaddr != 0)
186 return 0;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700187#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
188 mem->ion_handle = ion_import_fd(client, (int)mem->vaddr);
189 if (IS_ERR_OR_NULL(mem->ion_handle)) {
190 pr_err("%s ION import failed\n", __func__);
191 return PTR_ERR(mem->ion_handle);
192 }
Ankit Premrajka45942962012-02-15 19:21:25 -0800193 rc = ion_map_iommu(client, mem->ion_handle, CAMERA_DOMAIN, GEN_POOL,
194 SZ_4K, 0, (unsigned long *)&mem->phyaddr, &len, UNCACHED, 0);
195 if (rc < 0)
196 ion_free(client, mem->ion_handle);
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700197#elif CONFIG_ANDROID_PMEM
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700198 rc = get_pmem_file((int)mem->vaddr, (unsigned long *)&mem->phyaddr,
199 &kvstart, &len, &mem->file);
200 if (rc < 0) {
201 pr_err("%s: get_pmem_file fd %d error %d\n",
202 __func__, (int)mem->vaddr, rc);
203 return rc;
204 }
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700205#else
206 paddr = 0;
207 kvstart = 0;
208#endif
Kiran Kumar H N5e08d772011-10-03 10:19:15 -0700209 if (offset)
210 mem->offset = *offset;
211 else
212 memset(&mem->offset, 0, sizeof(struct videobuf2_msm_offset));
213 mem->path = path;
214 mem->buffer_type = buffer_type;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700215 paddr = mem->phyaddr;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700216 mem->mapped_phyaddr = paddr + addr_offset;
Kevin Chan318d7cb2011-11-29 14:24:26 -0800217 mem->addr_offset = addr_offset;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700218 return rc;
219}
220EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_user_get);
221
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700222void videobuf2_pmem_contig_user_put(struct videobuf2_contig_pmem *mem,
223 struct ion_client *client)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700224{
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700225 if (mem->is_userptr) {
226#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
Ankit Premrajka45942962012-02-15 19:21:25 -0800227 ion_unmap_iommu(client, mem->ion_handle,
228 CAMERA_DOMAIN, GEN_POOL);
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700229 ion_free(client, mem->ion_handle);
230#elif CONFIG_ANDROID_PMEM
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700231 put_pmem_file(mem->file);
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700232#endif
233 }
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700234 mem->is_userptr = 0;
235 mem->phyaddr = 0;
236 mem->size = 0;
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700237 mem->mapped_phyaddr = 0;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700238}
239EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_user_put);
240
241static void *msm_vb2_mem_ops_get_userptr(void *alloc_ctx, unsigned long vaddr,
242 unsigned long size, int write)
243{
244 struct videobuf2_contig_pmem *mem;
245 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
246 if (!mem)
247 return ERR_PTR(-ENOMEM);
248 mem->magic = MAGIC_PMEM;
249 mem->is_userptr = 1;
250 mem->vaddr = (void *)vaddr;
251 mem->size = size;
252 mem->alloc_ctx = alloc_ctx;
253 return mem;
254}
255static void msm_vb2_mem_ops_put_userptr(void *buf_priv)
256{
257 kfree(buf_priv);
258}
259
260static void *msm_vb2_mem_ops_vaddr(void *buf_priv)
261{
262 struct videobuf2_contig_pmem *mem = buf_priv;
263 return mem->vaddr;
264}
265static void *msm_vb2_mem_ops_cookie(void *buf_priv)
266{
267 return buf_priv;
268}
269static unsigned int msm_vb2_mem_ops_num_users(void *buf_priv)
270{
271 struct videobuf2_contig_pmem *mem = buf_priv;
272 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
273 return mem->count;
274}
275static int msm_vb2_mem_ops_mmap(void *buf_priv, struct vm_area_struct *vma)
276{
277 struct videobuf2_contig_pmem *mem;
278 int retval;
279 unsigned long size;
280 D("%s\n", __func__);
281 mem = buf_priv;
282 D("mem = 0x%x\n", (u32)mem);
283 BUG_ON(!mem);
284 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700285 /* Try to remap memory */
286 size = vma->vm_end - vma->vm_start;
287 size = (size < mem->size) ? size : mem->size;
288 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
289 retval = remap_pfn_range(vma, vma->vm_start,
290 mem->phyaddr >> PAGE_SHIFT,
291 size, vma->vm_page_prot);
292 if (retval) {
293 pr_err("mmap: remap failed with error %d. ", retval);
294 goto error;
295 }
296 mem->vaddr = (void *)vma->vm_start;
297 vma->vm_ops = &videobuf2_vm_ops;
298 vma->vm_flags |= VM_DONTEXPAND;
299 vma->vm_private_data = mem;
300
301 D("mmap %p: %08lx-%08lx (%lx) pgoff %08lx\n",
302 map, vma->vm_start, vma->vm_end,
303 (long int)mem->bsize, vma->vm_pgoff);
304 videobuf2_vm_open(vma);
305 return 0;
306error:
307 return -ENOMEM;
308}
309
310static struct vb2_mem_ops msm_vb2_mem_ops = {
311 .alloc = msm_vb2_mem_ops_alloc,
312 .put = msm_vb2_mem_ops_put,
313 .get_userptr = msm_vb2_mem_ops_get_userptr,
314 .put_userptr = msm_vb2_mem_ops_put_userptr,
315 .vaddr = msm_vb2_mem_ops_vaddr,
316 .cookie = msm_vb2_mem_ops_cookie,
317 .num_users = msm_vb2_mem_ops_num_users,
318 .mmap = msm_vb2_mem_ops_mmap
319};
320
321void videobuf2_queue_pmem_contig_init(struct vb2_queue *q,
322 enum v4l2_buf_type type,
323 const struct vb2_ops *ops,
324 unsigned int size,
325 void *priv)
326{
327 memset(q, 0, sizeof(struct vb2_queue));
328 q->mem_ops = &msm_vb2_mem_ops;
329 q->ops = ops;
330 q->drv_priv = priv;
331 q->type = type;
332 q->io_modes = VB2_MMAP | VB2_USERPTR;
333 q->io_flags = 0;
334 q->buf_struct_size = size;
335 vb2_queue_init(q);
336}
337EXPORT_SYMBOL_GPL(videobuf2_queue_pmem_contig_init);
338
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700339unsigned long videobuf2_to_pmem_contig(struct vb2_buffer *vb,
340 unsigned int plane_no)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700341{
342 struct videobuf2_contig_pmem *mem;
343 mem = vb2_plane_cookie(vb, plane_no);
344 BUG_ON(!mem);
345 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700346 return mem->mapped_phyaddr;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700347}
348EXPORT_SYMBOL_GPL(videobuf2_to_pmem_contig);
349
350MODULE_DESCRIPTION("helper module to manage video4linux PMEM contig buffers");
351MODULE_LICENSE("GPL v2");