blob: d8faa460597fb2d261a9bcba6327308b1a3d0bbb [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,
Hanumant Singh7d72bad2012-08-29 18:39:44 -070059 (0x1 << ION_CP_MM_HEAP_ID | 0x1 << ION_IOMMU_HEAP_ID), 0);
Ankit Premrajkad8147432012-01-06 09:45:11 -080060 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,
Ankit Premrajka79b9f222012-07-02 12:38:34 -070065 -1, 0, SZ_4K, 0,
Ankit Premrajka45942962012-02-15 19:21:25 -080066 (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 Premrajka79b9f222012-07-02 12:38:34 -070090 ion_unmap_iommu(mem->client, mem->ion_handle, -1, 0);
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,
Ankit Premrajka79b9f222012-07-02 12:38:34 -0700177 struct ion_client *client,
178 int domain_num)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700179{
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700180 unsigned long len;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700181 int rc = 0;
182#ifndef CONFIG_MSM_MULTIMEDIA_USE_ION
183 unsigned long kvstart;
184#endif
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700185 unsigned long paddr = 0;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700186 if (mem->phyaddr != 0)
187 return 0;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700188#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
Laura Abbottb14ed962012-01-30 14:18:08 -0800189 mem->ion_handle = ion_import_dma_buf(client, (int)mem->vaddr);
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700190 if (IS_ERR_OR_NULL(mem->ion_handle)) {
191 pr_err("%s ION import failed\n", __func__);
192 return PTR_ERR(mem->ion_handle);
193 }
Ankit Premrajka79b9f222012-07-02 12:38:34 -0700194 rc = ion_map_iommu(client, mem->ion_handle, domain_num, 0,
Ankit Premrajka45942962012-02-15 19:21:25 -0800195 SZ_4K, 0, (unsigned long *)&mem->phyaddr, &len, UNCACHED, 0);
196 if (rc < 0)
197 ion_free(client, mem->ion_handle);
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700198#elif CONFIG_ANDROID_PMEM
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700199 rc = get_pmem_file((int)mem->vaddr, (unsigned long *)&mem->phyaddr,
200 &kvstart, &len, &mem->file);
201 if (rc < 0) {
202 pr_err("%s: get_pmem_file fd %d error %d\n",
203 __func__, (int)mem->vaddr, rc);
204 return rc;
205 }
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700206#else
207 paddr = 0;
208 kvstart = 0;
209#endif
Kiran Kumar H N5e08d772011-10-03 10:19:15 -0700210 if (offset)
211 mem->offset = *offset;
212 else
213 memset(&mem->offset, 0, sizeof(struct videobuf2_msm_offset));
214 mem->path = path;
215 mem->buffer_type = buffer_type;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700216 paddr = mem->phyaddr;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700217 mem->mapped_phyaddr = paddr + addr_offset;
Kevin Chan318d7cb2011-11-29 14:24:26 -0800218 mem->addr_offset = addr_offset;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700219 return rc;
220}
221EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_user_get);
222
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700223void videobuf2_pmem_contig_user_put(struct videobuf2_contig_pmem *mem,
Ankit Premrajka79b9f222012-07-02 12:38:34 -0700224 struct ion_client *client, int domain_num)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700225{
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700226 if (mem->is_userptr) {
227#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
Ankit Premrajka45942962012-02-15 19:21:25 -0800228 ion_unmap_iommu(client, mem->ion_handle,
Ankit Premrajka79b9f222012-07-02 12:38:34 -0700229 domain_num, 0);
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700230 ion_free(client, mem->ion_handle);
231#elif CONFIG_ANDROID_PMEM
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700232 put_pmem_file(mem->file);
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700233#endif
234 }
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700235 mem->is_userptr = 0;
236 mem->phyaddr = 0;
237 mem->size = 0;
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700238 mem->mapped_phyaddr = 0;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700239}
240EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_user_put);
241
242static void *msm_vb2_mem_ops_get_userptr(void *alloc_ctx, unsigned long vaddr,
243 unsigned long size, int write)
244{
245 struct videobuf2_contig_pmem *mem;
246 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
247 if (!mem)
248 return ERR_PTR(-ENOMEM);
249 mem->magic = MAGIC_PMEM;
250 mem->is_userptr = 1;
251 mem->vaddr = (void *)vaddr;
252 mem->size = size;
253 mem->alloc_ctx = alloc_ctx;
254 return mem;
255}
256static void msm_vb2_mem_ops_put_userptr(void *buf_priv)
257{
258 kfree(buf_priv);
259}
260
261static void *msm_vb2_mem_ops_vaddr(void *buf_priv)
262{
263 struct videobuf2_contig_pmem *mem = buf_priv;
264 return mem->vaddr;
265}
266static void *msm_vb2_mem_ops_cookie(void *buf_priv)
267{
268 return buf_priv;
269}
270static unsigned int msm_vb2_mem_ops_num_users(void *buf_priv)
271{
272 struct videobuf2_contig_pmem *mem = buf_priv;
273 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
274 return mem->count;
275}
276static int msm_vb2_mem_ops_mmap(void *buf_priv, struct vm_area_struct *vma)
277{
278 struct videobuf2_contig_pmem *mem;
279 int retval;
280 unsigned long size;
281 D("%s\n", __func__);
282 mem = buf_priv;
283 D("mem = 0x%x\n", (u32)mem);
284 BUG_ON(!mem);
285 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700286 /* Try to remap memory */
287 size = vma->vm_end - vma->vm_start;
288 size = (size < mem->size) ? size : mem->size;
289 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
290 retval = remap_pfn_range(vma, vma->vm_start,
291 mem->phyaddr >> PAGE_SHIFT,
292 size, vma->vm_page_prot);
293 if (retval) {
294 pr_err("mmap: remap failed with error %d. ", retval);
295 goto error;
296 }
297 mem->vaddr = (void *)vma->vm_start;
298 vma->vm_ops = &videobuf2_vm_ops;
299 vma->vm_flags |= VM_DONTEXPAND;
300 vma->vm_private_data = mem;
301
302 D("mmap %p: %08lx-%08lx (%lx) pgoff %08lx\n",
303 map, vma->vm_start, vma->vm_end,
304 (long int)mem->bsize, vma->vm_pgoff);
305 videobuf2_vm_open(vma);
306 return 0;
307error:
308 return -ENOMEM;
309}
310
311static struct vb2_mem_ops msm_vb2_mem_ops = {
312 .alloc = msm_vb2_mem_ops_alloc,
313 .put = msm_vb2_mem_ops_put,
314 .get_userptr = msm_vb2_mem_ops_get_userptr,
315 .put_userptr = msm_vb2_mem_ops_put_userptr,
316 .vaddr = msm_vb2_mem_ops_vaddr,
317 .cookie = msm_vb2_mem_ops_cookie,
318 .num_users = msm_vb2_mem_ops_num_users,
319 .mmap = msm_vb2_mem_ops_mmap
320};
321
322void videobuf2_queue_pmem_contig_init(struct vb2_queue *q,
323 enum v4l2_buf_type type,
324 const struct vb2_ops *ops,
325 unsigned int size,
326 void *priv)
327{
328 memset(q, 0, sizeof(struct vb2_queue));
329 q->mem_ops = &msm_vb2_mem_ops;
330 q->ops = ops;
331 q->drv_priv = priv;
332 q->type = type;
333 q->io_modes = VB2_MMAP | VB2_USERPTR;
334 q->io_flags = 0;
335 q->buf_struct_size = size;
336 vb2_queue_init(q);
337}
338EXPORT_SYMBOL_GPL(videobuf2_queue_pmem_contig_init);
339
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700340unsigned long videobuf2_to_pmem_contig(struct vb2_buffer *vb,
341 unsigned int plane_no)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700342{
343 struct videobuf2_contig_pmem *mem;
344 mem = vb2_plane_cookie(vb, plane_no);
345 BUG_ON(!mem);
346 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700347 return mem->mapped_phyaddr;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700348}
349EXPORT_SYMBOL_GPL(videobuf2_to_pmem_contig);
350
351MODULE_DESCRIPTION("helper module to manage video4linux PMEM contig buffers");
352MODULE_LICENSE("GPL v2");