blob: fa7e3fd7f5ce610448bd7ce1d3ef7b041d906a1e [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,
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700154 uint32_t addr_offset, int path,
155 struct ion_client *client)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700156{
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700157 unsigned long len;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700158 int rc = 0;
159#ifndef CONFIG_MSM_MULTIMEDIA_USE_ION
160 unsigned long kvstart;
161#endif
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700162 unsigned int flags = 0;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700163 unsigned long paddr = 0;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700164 if (mem->phyaddr != 0)
165 return 0;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700166#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
167 mem->ion_handle = ion_import_fd(client, (int)mem->vaddr);
168 if (IS_ERR_OR_NULL(mem->ion_handle)) {
169 pr_err("%s ION import failed\n", __func__);
170 return PTR_ERR(mem->ion_handle);
171 }
172 rc = ion_phys(client, mem->ion_handle, (ion_phys_addr_t *)&mem->phyaddr,
173 (size_t *)&len);
174#elif CONFIG_ANDROID_PMEM
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700175 rc = get_pmem_file((int)mem->vaddr, (unsigned long *)&mem->phyaddr,
176 &kvstart, &len, &mem->file);
177 if (rc < 0) {
178 pr_err("%s: get_pmem_file fd %d error %d\n",
179 __func__, (int)mem->vaddr, rc);
180 return rc;
181 }
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700182#else
183 paddr = 0;
184 kvstart = 0;
185#endif
Kiran Kumar H N5e08d772011-10-03 10:19:15 -0700186 if (offset)
187 mem->offset = *offset;
188 else
189 memset(&mem->offset, 0, sizeof(struct videobuf2_msm_offset));
190 mem->path = path;
191 mem->buffer_type = buffer_type;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700192 paddr = mem->phyaddr;
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700193 flags = MSM_SUBSYSTEM_MAP_IOVA;
194 mem->subsys_id = MSM_SUBSYSTEM_CAMERA;
Ankit Premrajka1b9b9a42011-08-05 15:49:42 -0700195 mem->msm_buffer = msm_subsystem_map_buffer(mem->phyaddr, len,
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700196 flags, &(mem->subsys_id), 1);
197 if (IS_ERR((void *)mem->msm_buffer)) {
198 pr_err("%s: msm_subsystem_map_buffer failed\n", __func__);
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700199#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
200 ion_free(client, mem->ion_handle);
201#elif CONFIG_ANDROID_PMEM
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700202 put_pmem_file(mem->file);
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700203#endif
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700204 return PTR_ERR((void *)mem->msm_buffer);
205 }
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700206 paddr = mem->msm_buffer->iova[0];
207 mem->mapped_phyaddr = paddr + addr_offset;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700208 return rc;
209}
210EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_user_get);
211
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700212void videobuf2_pmem_contig_user_put(struct videobuf2_contig_pmem *mem,
213 struct ion_client *client)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700214{
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700215 if (msm_subsystem_unmap_buffer(mem->msm_buffer) < 0)
216 D("%s unmapped memory\n", __func__);
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700217 if (mem->is_userptr) {
218#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
219 ion_free(client, mem->ion_handle);
220#elif CONFIG_ANDROID_PMEM
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700221 put_pmem_file(mem->file);
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700222#endif
223 }
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700224 mem->is_userptr = 0;
225 mem->phyaddr = 0;
226 mem->size = 0;
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700227 mem->mapped_phyaddr = 0;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700228}
229EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_user_put);
230
231static void *msm_vb2_mem_ops_get_userptr(void *alloc_ctx, unsigned long vaddr,
232 unsigned long size, int write)
233{
234 struct videobuf2_contig_pmem *mem;
235 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
236 if (!mem)
237 return ERR_PTR(-ENOMEM);
238 mem->magic = MAGIC_PMEM;
239 mem->is_userptr = 1;
240 mem->vaddr = (void *)vaddr;
241 mem->size = size;
242 mem->alloc_ctx = alloc_ctx;
243 return mem;
244}
245static void msm_vb2_mem_ops_put_userptr(void *buf_priv)
246{
247 kfree(buf_priv);
248}
249
250static void *msm_vb2_mem_ops_vaddr(void *buf_priv)
251{
252 struct videobuf2_contig_pmem *mem = buf_priv;
253 return mem->vaddr;
254}
255static void *msm_vb2_mem_ops_cookie(void *buf_priv)
256{
257 return buf_priv;
258}
259static unsigned int msm_vb2_mem_ops_num_users(void *buf_priv)
260{
261 struct videobuf2_contig_pmem *mem = buf_priv;
262 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
263 return mem->count;
264}
265static int msm_vb2_mem_ops_mmap(void *buf_priv, struct vm_area_struct *vma)
266{
267 struct videobuf2_contig_pmem *mem;
268 int retval;
269 unsigned long size;
270 D("%s\n", __func__);
271 mem = buf_priv;
272 D("mem = 0x%x\n", (u32)mem);
273 BUG_ON(!mem);
274 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700275 /* Try to remap memory */
276 size = vma->vm_end - vma->vm_start;
277 size = (size < mem->size) ? size : mem->size;
278 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
279 retval = remap_pfn_range(vma, vma->vm_start,
280 mem->phyaddr >> PAGE_SHIFT,
281 size, vma->vm_page_prot);
282 if (retval) {
283 pr_err("mmap: remap failed with error %d. ", retval);
284 goto error;
285 }
286 mem->vaddr = (void *)vma->vm_start;
287 vma->vm_ops = &videobuf2_vm_ops;
288 vma->vm_flags |= VM_DONTEXPAND;
289 vma->vm_private_data = mem;
290
291 D("mmap %p: %08lx-%08lx (%lx) pgoff %08lx\n",
292 map, vma->vm_start, vma->vm_end,
293 (long int)mem->bsize, vma->vm_pgoff);
294 videobuf2_vm_open(vma);
295 return 0;
296error:
297 return -ENOMEM;
298}
299
300static struct vb2_mem_ops msm_vb2_mem_ops = {
301 .alloc = msm_vb2_mem_ops_alloc,
302 .put = msm_vb2_mem_ops_put,
303 .get_userptr = msm_vb2_mem_ops_get_userptr,
304 .put_userptr = msm_vb2_mem_ops_put_userptr,
305 .vaddr = msm_vb2_mem_ops_vaddr,
306 .cookie = msm_vb2_mem_ops_cookie,
307 .num_users = msm_vb2_mem_ops_num_users,
308 .mmap = msm_vb2_mem_ops_mmap
309};
310
311void videobuf2_queue_pmem_contig_init(struct vb2_queue *q,
312 enum v4l2_buf_type type,
313 const struct vb2_ops *ops,
314 unsigned int size,
315 void *priv)
316{
317 memset(q, 0, sizeof(struct vb2_queue));
318 q->mem_ops = &msm_vb2_mem_ops;
319 q->ops = ops;
320 q->drv_priv = priv;
321 q->type = type;
322 q->io_modes = VB2_MMAP | VB2_USERPTR;
323 q->io_flags = 0;
324 q->buf_struct_size = size;
325 vb2_queue_init(q);
326}
327EXPORT_SYMBOL_GPL(videobuf2_queue_pmem_contig_init);
328
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700329unsigned long videobuf2_to_pmem_contig(struct vb2_buffer *vb,
330 unsigned int plane_no)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700331{
332 struct videobuf2_contig_pmem *mem;
333 mem = vb2_plane_cookie(vb, plane_no);
334 BUG_ON(!mem);
335 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700336 return mem->mapped_phyaddr;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700337}
338EXPORT_SYMBOL_GPL(videobuf2_to_pmem_contig);
339
340MODULE_DESCRIPTION("helper module to manage video4linux PMEM contig buffers");
341MODULE_LICENSE("GPL v2");