blob: 8dbb522e31f930d92a08b3e7f087a5314116a872 [file] [log] [blame]
Duy Truong790f06d2013-02-13 16:38:12 -08001/* Copyright (c) 2011-2012, The Linux Foundation. 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>
Laura Abbott17b7dbe2013-03-09 15:59:25 -080027
Mingcheng Zhu9812bd32011-07-22 22:57:11 -070028#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,
Mitchel Humpherys76b9b4d2012-09-17 14:33:22 -070067 (unsigned long *)&len, 0, 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{
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700180 int rc = 0;
Laura Abbott17b7dbe2013-03-09 15:59:25 -0800181#ifdef CONFIG_MSM_MULTIMEDIA_USE_ION
182 unsigned long len;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700183#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;
Laura Abbott17b7dbe2013-03-09 15:59:25 -0800187#if defined(CONFIG_MSM_MULTIMEDIA_USE_ION)
Laura Abbottb14ed962012-01-30 14:18:08 -0800188 mem->ion_handle = ion_import_dma_buf(client, (int)mem->vaddr);
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700189 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 Premrajka79b9f222012-07-02 12:38:34 -0700193 rc = ion_map_iommu(client, mem->ion_handle, domain_num, 0,
Mitchel Humpherys76b9b4d2012-09-17 14:33:22 -0700194 SZ_4K, 0, (unsigned long *)&mem->phyaddr, &len, 0, 0);
Ankit Premrajka45942962012-02-15 19:21:25 -0800195 if (rc < 0)
196 ion_free(client, mem->ion_handle);
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700197#else
198 paddr = 0;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700199#endif
Kiran Kumar H N5e08d772011-10-03 10:19:15 -0700200 if (offset)
201 mem->offset = *offset;
202 else
203 memset(&mem->offset, 0, sizeof(struct videobuf2_msm_offset));
204 mem->path = path;
205 mem->buffer_type = buffer_type;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700206 paddr = mem->phyaddr;
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700207 mem->mapped_phyaddr = paddr + addr_offset;
Kevin Chan318d7cb2011-11-29 14:24:26 -0800208 mem->addr_offset = addr_offset;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700209 return rc;
210}
211EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_user_get);
212
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700213void videobuf2_pmem_contig_user_put(struct videobuf2_contig_pmem *mem,
Ankit Premrajka79b9f222012-07-02 12:38:34 -0700214 struct ion_client *client, int domain_num)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700215{
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700216 if (mem->is_userptr) {
Laura Abbott17b7dbe2013-03-09 15:59:25 -0800217#if defined(CONFIG_MSM_MULTIMEDIA_USE_ION)
Ankit Premrajka45942962012-02-15 19:21:25 -0800218 ion_unmap_iommu(client, mem->ion_handle,
Ankit Premrajka79b9f222012-07-02 12:38:34 -0700219 domain_num, 0);
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700220 ion_free(client, mem->ion_handle);
Ankit Premrajka748a70a2011-11-01 08:22:04 -0700221#endif
222 }
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700223 mem->is_userptr = 0;
224 mem->phyaddr = 0;
225 mem->size = 0;
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700226 mem->mapped_phyaddr = 0;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700227}
228EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_user_put);
229
230static void *msm_vb2_mem_ops_get_userptr(void *alloc_ctx, unsigned long vaddr,
231 unsigned long size, int write)
232{
233 struct videobuf2_contig_pmem *mem;
234 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
235 if (!mem)
236 return ERR_PTR(-ENOMEM);
237 mem->magic = MAGIC_PMEM;
238 mem->is_userptr = 1;
239 mem->vaddr = (void *)vaddr;
240 mem->size = size;
241 mem->alloc_ctx = alloc_ctx;
242 return mem;
243}
244static void msm_vb2_mem_ops_put_userptr(void *buf_priv)
245{
246 kfree(buf_priv);
247}
248
249static void *msm_vb2_mem_ops_vaddr(void *buf_priv)
250{
251 struct videobuf2_contig_pmem *mem = buf_priv;
252 return mem->vaddr;
253}
254static void *msm_vb2_mem_ops_cookie(void *buf_priv)
255{
256 return buf_priv;
257}
258static unsigned int msm_vb2_mem_ops_num_users(void *buf_priv)
259{
260 struct videobuf2_contig_pmem *mem = buf_priv;
261 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
262 return mem->count;
263}
264static int msm_vb2_mem_ops_mmap(void *buf_priv, struct vm_area_struct *vma)
265{
266 struct videobuf2_contig_pmem *mem;
267 int retval;
268 unsigned long size;
269 D("%s\n", __func__);
270 mem = buf_priv;
271 D("mem = 0x%x\n", (u32)mem);
272 BUG_ON(!mem);
273 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700274 /* Try to remap memory */
275 size = vma->vm_end - vma->vm_start;
276 size = (size < mem->size) ? size : mem->size;
277 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
278 retval = remap_pfn_range(vma, vma->vm_start,
279 mem->phyaddr >> PAGE_SHIFT,
280 size, vma->vm_page_prot);
281 if (retval) {
282 pr_err("mmap: remap failed with error %d. ", retval);
283 goto error;
284 }
285 mem->vaddr = (void *)vma->vm_start;
286 vma->vm_ops = &videobuf2_vm_ops;
287 vma->vm_flags |= VM_DONTEXPAND;
288 vma->vm_private_data = mem;
289
290 D("mmap %p: %08lx-%08lx (%lx) pgoff %08lx\n",
291 map, vma->vm_start, vma->vm_end,
292 (long int)mem->bsize, vma->vm_pgoff);
293 videobuf2_vm_open(vma);
294 return 0;
295error:
296 return -ENOMEM;
297}
298
299static struct vb2_mem_ops msm_vb2_mem_ops = {
300 .alloc = msm_vb2_mem_ops_alloc,
301 .put = msm_vb2_mem_ops_put,
302 .get_userptr = msm_vb2_mem_ops_get_userptr,
303 .put_userptr = msm_vb2_mem_ops_put_userptr,
304 .vaddr = msm_vb2_mem_ops_vaddr,
305 .cookie = msm_vb2_mem_ops_cookie,
306 .num_users = msm_vb2_mem_ops_num_users,
307 .mmap = msm_vb2_mem_ops_mmap
308};
309
310void videobuf2_queue_pmem_contig_init(struct vb2_queue *q,
311 enum v4l2_buf_type type,
312 const struct vb2_ops *ops,
313 unsigned int size,
314 void *priv)
315{
316 memset(q, 0, sizeof(struct vb2_queue));
317 q->mem_ops = &msm_vb2_mem_ops;
318 q->ops = ops;
319 q->drv_priv = priv;
320 q->type = type;
321 q->io_modes = VB2_MMAP | VB2_USERPTR;
322 q->io_flags = 0;
323 q->buf_struct_size = size;
324 vb2_queue_init(q);
325}
326EXPORT_SYMBOL_GPL(videobuf2_queue_pmem_contig_init);
327
Ankit Premrajkac6864b82011-07-15 11:43:41 -0700328unsigned long videobuf2_to_pmem_contig(struct vb2_buffer *vb,
329 unsigned int plane_no)
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700330{
331 struct videobuf2_contig_pmem *mem;
332 mem = vb2_plane_cookie(vb, plane_no);
333 BUG_ON(!mem);
334 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
Kiran Kumar H Nceea7622011-08-23 14:01:03 -0700335 return mem->mapped_phyaddr;
Mingcheng Zhu9812bd32011-07-22 22:57:11 -0700336}
337EXPORT_SYMBOL_GPL(videobuf2_to_pmem_contig);
338
339MODULE_DESCRIPTION("helper module to manage video4linux PMEM contig buffers");
340MODULE_LICENSE("GPL v2");