blob: 37b935b184d63f9298400bec24a2145378e56957 [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>
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
Ankit Premrajkad8147432012-01-06 09:45:11 -080048static int32_t msm_mem_allocate(struct videobuf2_contig_pmem *mem)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -070049{
50 int32_t 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 }
64 rc = ion_phys(mem->client, mem->ion_handle, (ion_phys_addr_t *)&phyaddr,
65 (size_t *)&len);
66 if (rc < 0) {
67 pr_err("%s Could not get physical address\n", __func__);
68 goto phys_failed;
69 }
70#else
71 phyaddr = allocate_contiguous_ebi_nomap(mem->size, SZ_4K);
72#endif
Mingcheng Zhu9812bd32011-07-22 22:57:11 -070073 return phyaddr;
Ankit Premrajkad8147432012-01-06 09:45:11 -080074#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
75phys_failed:
76 ion_free(mem->client, mem->ion_handle);
77alloc_failed:
78 ion_client_destroy(mem->client);
79client_failed:
80 return 0;
81#endif
Mingcheng Zhu9812bd32011-07-22 22:57:11 -070082}
83
Ankit Premrajkad8147432012-01-06 09:45:11 -080084static int32_t msm_mem_free(struct videobuf2_contig_pmem *mem)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -070085{
86 int32_t rc = 0;
Ankit Premrajkad8147432012-01-06 09:45:11 -080087#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
88 ion_free(mem->client, mem->ion_handle);
89 ion_client_destroy(mem->client);
90#else
91 free_contiguous_memory_by_paddr(mem->phyaddr);
92#endif
Mingcheng Zhu9812bd32011-07-22 22:57:11 -070093 return rc;
94}
95
96static void videobuf2_vm_close(struct vm_area_struct *vma)
97{
98 struct videobuf2_contig_pmem *mem = vma->vm_private_data;
99 D("vm_close %p [count=%u,vma=%08lx-%08lx]\n",
100 mem, mem->count, vma->vm_start, vma->vm_end);
101 mem->count--;
102}
103static void videobuf2_vm_open(struct vm_area_struct *vma)
104{
105 struct videobuf2_contig_pmem *mem = vma->vm_private_data;
106 D("vm_open %p [count=%u,vma=%08lx-%08lx]\n",
107 mem, mem->count, vma->vm_start, vma->vm_end);
108 mem->count++;
109}
110
111static const struct vm_operations_struct videobuf2_vm_ops = {
112 .open = videobuf2_vm_open,
113 .close = videobuf2_vm_close,
114};
115
116static void *msm_vb2_mem_ops_alloc(void *alloc_ctx, unsigned long size)
117{
118 struct videobuf2_contig_pmem *mem;
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700119 unsigned int flags = 0;
120 long rc;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700121 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
122 if (!mem)
123 return ERR_PTR(-ENOMEM);
124
125 mem->magic = MAGIC_PMEM;
126 mem->size = PAGE_ALIGN(size);
127 mem->alloc_ctx = alloc_ctx;
128 mem->is_userptr = 0;
Ankit Premrajkad8147432012-01-06 09:45:11 -0800129 mem->phyaddr = msm_mem_allocate(mem);
Ankit Premrajkaa5ea3a92011-08-04 17:54:17 -0700130 if (!mem->phyaddr) {
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700131 pr_err("%s : pmem memory allocation failed\n", __func__);
132 kfree(mem);
133 return ERR_PTR(-ENOMEM);
134 }
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700135 flags = MSM_SUBSYSTEM_MAP_IOVA;
136 mem->subsys_id = MSM_SUBSYSTEM_CAMERA;
137 mem->msm_buffer = msm_subsystem_map_buffer(mem->phyaddr, mem->size,
138 flags, &(mem->subsys_id), 1);
139 if (IS_ERR((void *)mem->msm_buffer)) {
140 pr_err("%s: msm_subsystem_map_buffer failed\n", __func__);
141 rc = PTR_ERR((void *)mem->msm_buffer);
Ankit Premrajkad8147432012-01-06 09:45:11 -0800142 msm_mem_free(mem);
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700143 kfree(mem);
144 return ERR_PTR(-ENOMEM);
145 }
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700146 mem->mapped_phyaddr = mem->msm_buffer->iova[0];
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700147 return mem;
148}
149static void msm_vb2_mem_ops_put(void *buf_priv)
150{
151 struct videobuf2_contig_pmem *mem = buf_priv;
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700152 if (!mem->is_userptr) {
153 D("%s Freeing memory ", __func__);
154 if (msm_subsystem_unmap_buffer(mem->msm_buffer) < 0)
155 D("%s unmapped memory\n", __func__);
Ankit Premrajkad8147432012-01-06 09:45:11 -0800156 msm_mem_free(mem);
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700157 }
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700158 kfree(mem);
159}
160int videobuf2_pmem_contig_mmap_get(struct videobuf2_contig_pmem *mem,
Kiran Kumar H N5e08d772011-10-03 10:19:15 -0700161 struct videobuf2_msm_offset *offset,
162 enum videobuf2_buffer_type buffer_type,
163 int path)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700164{
Kiran Kumar H N5e08d772011-10-03 10:19:15 -0700165 if (offset)
166 mem->offset = *offset;
167 else
168 memset(&mem->offset, 0, sizeof(struct videobuf2_msm_offset));
169 mem->buffer_type = buffer_type;
170 mem->path = path;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700171 return 0;
172}
173EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_mmap_get);
174
175/**
176 * videobuf_pmem_contig_user_get() - setup user space memory pointer
177 * @mem: per-buffer private videobuf-contig-pmem data
178 * @vb: video buffer to map
179 *
180 * This function validates and sets up a pointer to user space memory.
181 * Only physically contiguous pfn-mapped memory is accepted.
182 *
183 * Returns 0 if successful.
184 */
185int videobuf2_pmem_contig_user_get(struct videobuf2_contig_pmem *mem,
Kiran Kumar H N5e08d772011-10-03 10:19:15 -0700186 struct videobuf2_msm_offset *offset,
187 enum videobuf2_buffer_type buffer_type,
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700188 uint32_t addr_offset, int path,
189 struct ion_client *client)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700190{
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700191 unsigned long len;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700192 int rc = 0;
193#ifndef CONFIG_MSM_MULTIMEDIA_USE_ION
194 unsigned long kvstart;
195#endif
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700196 unsigned int flags = 0;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700197 unsigned long paddr = 0;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700198 if (mem->phyaddr != 0)
199 return 0;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700200#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
201 mem->ion_handle = ion_import_fd(client, (int)mem->vaddr);
202 if (IS_ERR_OR_NULL(mem->ion_handle)) {
203 pr_err("%s ION import failed\n", __func__);
204 return PTR_ERR(mem->ion_handle);
205 }
206 rc = ion_phys(client, mem->ion_handle, (ion_phys_addr_t *)&mem->phyaddr,
207 (size_t *)&len);
208#elif CONFIG_ANDROID_PMEM
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700209 rc = get_pmem_file((int)mem->vaddr, (unsigned long *)&mem->phyaddr,
210 &kvstart, &len, &mem->file);
211 if (rc < 0) {
212 pr_err("%s: get_pmem_file fd %d error %d\n",
213 __func__, (int)mem->vaddr, rc);
214 return rc;
215 }
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700216#else
217 paddr = 0;
218 kvstart = 0;
219#endif
Kiran Kumar H N5e08d772011-10-03 10:19:15 -0700220 if (offset)
221 mem->offset = *offset;
222 else
223 memset(&mem->offset, 0, sizeof(struct videobuf2_msm_offset));
224 mem->path = path;
225 mem->buffer_type = buffer_type;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700226 paddr = mem->phyaddr;
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700227 flags = MSM_SUBSYSTEM_MAP_IOVA;
228 mem->subsys_id = MSM_SUBSYSTEM_CAMERA;
Ankit Premrajka1b9b9a42011-08-05 15:49:42 -0700229 mem->msm_buffer = msm_subsystem_map_buffer(mem->phyaddr, len,
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700230 flags, &(mem->subsys_id), 1);
231 if (IS_ERR((void *)mem->msm_buffer)) {
232 pr_err("%s: msm_subsystem_map_buffer failed\n", __func__);
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700233#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
234 ion_free(client, mem->ion_handle);
235#elif CONFIG_ANDROID_PMEM
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700236 put_pmem_file(mem->file);
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700237#endif
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700238 return PTR_ERR((void *)mem->msm_buffer);
239 }
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700240 paddr = mem->msm_buffer->iova[0];
241 mem->mapped_phyaddr = paddr + addr_offset;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700242 return rc;
243}
244EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_user_get);
245
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700246void videobuf2_pmem_contig_user_put(struct videobuf2_contig_pmem *mem,
247 struct ion_client *client)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700248{
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700249 if (msm_subsystem_unmap_buffer(mem->msm_buffer) < 0)
250 D("%s unmapped memory\n", __func__);
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700251 if (mem->is_userptr) {
252#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
253 ion_free(client, mem->ion_handle);
254#elif CONFIG_ANDROID_PMEM
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700255 put_pmem_file(mem->file);
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700256#endif
257 }
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700258 mem->is_userptr = 0;
259 mem->phyaddr = 0;
260 mem->size = 0;
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700261 mem->mapped_phyaddr = 0;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700262}
263EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_user_put);
264
265static void *msm_vb2_mem_ops_get_userptr(void *alloc_ctx, unsigned long vaddr,
266 unsigned long size, int write)
267{
268 struct videobuf2_contig_pmem *mem;
269 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
270 if (!mem)
271 return ERR_PTR(-ENOMEM);
272 mem->magic = MAGIC_PMEM;
273 mem->is_userptr = 1;
274 mem->vaddr = (void *)vaddr;
275 mem->size = size;
276 mem->alloc_ctx = alloc_ctx;
277 return mem;
278}
279static void msm_vb2_mem_ops_put_userptr(void *buf_priv)
280{
281 kfree(buf_priv);
282}
283
284static void *msm_vb2_mem_ops_vaddr(void *buf_priv)
285{
286 struct videobuf2_contig_pmem *mem = buf_priv;
287 return mem->vaddr;
288}
289static void *msm_vb2_mem_ops_cookie(void *buf_priv)
290{
291 return buf_priv;
292}
293static unsigned int msm_vb2_mem_ops_num_users(void *buf_priv)
294{
295 struct videobuf2_contig_pmem *mem = buf_priv;
296 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
297 return mem->count;
298}
299static int msm_vb2_mem_ops_mmap(void *buf_priv, struct vm_area_struct *vma)
300{
301 struct videobuf2_contig_pmem *mem;
302 int retval;
303 unsigned long size;
304 D("%s\n", __func__);
305 mem = buf_priv;
306 D("mem = 0x%x\n", (u32)mem);
307 BUG_ON(!mem);
308 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700309 /* Try to remap memory */
310 size = vma->vm_end - vma->vm_start;
311 size = (size < mem->size) ? size : mem->size;
312 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
313 retval = remap_pfn_range(vma, vma->vm_start,
314 mem->phyaddr >> PAGE_SHIFT,
315 size, vma->vm_page_prot);
316 if (retval) {
317 pr_err("mmap: remap failed with error %d. ", retval);
318 goto error;
319 }
320 mem->vaddr = (void *)vma->vm_start;
321 vma->vm_ops = &videobuf2_vm_ops;
322 vma->vm_flags |= VM_DONTEXPAND;
323 vma->vm_private_data = mem;
324
325 D("mmap %p: %08lx-%08lx (%lx) pgoff %08lx\n",
326 map, vma->vm_start, vma->vm_end,
327 (long int)mem->bsize, vma->vm_pgoff);
328 videobuf2_vm_open(vma);
329 return 0;
330error:
331 return -ENOMEM;
332}
333
334static struct vb2_mem_ops msm_vb2_mem_ops = {
335 .alloc = msm_vb2_mem_ops_alloc,
336 .put = msm_vb2_mem_ops_put,
337 .get_userptr = msm_vb2_mem_ops_get_userptr,
338 .put_userptr = msm_vb2_mem_ops_put_userptr,
339 .vaddr = msm_vb2_mem_ops_vaddr,
340 .cookie = msm_vb2_mem_ops_cookie,
341 .num_users = msm_vb2_mem_ops_num_users,
342 .mmap = msm_vb2_mem_ops_mmap
343};
344
345void videobuf2_queue_pmem_contig_init(struct vb2_queue *q,
346 enum v4l2_buf_type type,
347 const struct vb2_ops *ops,
348 unsigned int size,
349 void *priv)
350{
351 memset(q, 0, sizeof(struct vb2_queue));
352 q->mem_ops = &msm_vb2_mem_ops;
353 q->ops = ops;
354 q->drv_priv = priv;
355 q->type = type;
356 q->io_modes = VB2_MMAP | VB2_USERPTR;
357 q->io_flags = 0;
358 q->buf_struct_size = size;
359 vb2_queue_init(q);
360}
361EXPORT_SYMBOL_GPL(videobuf2_queue_pmem_contig_init);
362
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700363unsigned long videobuf2_to_pmem_contig(struct vb2_buffer *vb,
364 unsigned int plane_no)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700365{
366 struct videobuf2_contig_pmem *mem;
367 mem = vb2_plane_cookie(vb, plane_no);
368 BUG_ON(!mem);
369 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700370 return mem->mapped_phyaddr;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700371}
372EXPORT_SYMBOL_GPL(videobuf2_to_pmem_contig);
373
374MODULE_DESCRIPTION("helper module to manage video4linux PMEM contig buffers");
375MODULE_LICENSE("GPL v2");