blob: eeda13aeaf7d861d297f7aa02f8cf0fe1b74f945 [file] [log] [blame]
Duy Truong790f06d2013-02-13 16:38:12 -08001/* Copyright (c) 2011, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028#include <linux/memory_alloc.h>
29#include <media/videobuf-msm-mem.h>
30#include <media/msm_camera.h>
31#include <mach/memory.h>
32
33#define MAGIC_PMEM 0x0733ac64
34#define MAGIC_CHECK(is, should) \
35 if (unlikely((is) != (should))) { \
36 pr_err("magic mismatch: %x expected %x\n", (is), (should)); \
37 BUG(); \
38 }
39
40#ifdef CONFIG_MSM_CAMERA_DEBUG
41#define D(fmt, args...) printk(KERN_DEBUG "videobuf-msm-mem: " fmt, ##args)
42#else
43#define D(fmt, args...) do {} while (0)
44#endif
45
46static int32_t msm_mem_allocate(const size_t size)
47{
48 int32_t phyaddr;
49 phyaddr = allocate_contiguous_ebi_nomap(size, SZ_4K);
50 return phyaddr;
51}
52
53static int32_t msm_mem_free(const int32_t phyaddr)
54{
55 int32_t rc = 0;
56 free_contiguous_memory_by_paddr(phyaddr);
57 return rc;
58}
59
60static void
61videobuf_vm_open(struct vm_area_struct *vma)
62{
63 struct videobuf_mapping *map = vma->vm_private_data;
64
65 D("vm_open %p [count=%u,vma=%08lx-%08lx]\n",
66 map, map->count, vma->vm_start, vma->vm_end);
67
68 map->count++;
69}
70
71static void videobuf_vm_close(struct vm_area_struct *vma)
72{
73 struct videobuf_mapping *map = vma->vm_private_data;
74 struct videobuf_queue *q = map->q;
75 int i, rc;
76
77 D("vm_close %p [count=%u,vma=%08lx-%08lx]\n",
78 map, map->count, vma->vm_start, vma->vm_end);
79
80 map->count--;
81 if (0 == map->count) {
82 struct videobuf_contig_pmem *mem;
83
84 D("munmap %p q=%p\n", map, q);
85 mutex_lock(&q->vb_lock);
86
87 /* We need first to cancel streams, before unmapping */
88 if (q->streaming)
89 videobuf_queue_cancel(q);
90
91 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
92 if (NULL == q->bufs[i])
93 continue;
94
95 if (q->bufs[i]->map != map)
96 continue;
97
98 mem = q->bufs[i]->priv;
99 if (mem) {
100 /* This callback is called only if kernel has
101 * allocated memory and this memory is mmapped.
102 * In this case, memory should be freed,
103 * in order to do memory unmap.
104 */
105
106 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
107
108 /* vfree is not atomic - can't be
109 called with IRQ's disabled
110 */
111 D("buf[%d] freeing physical %d\n",
112 i, mem->phyaddr);
113
114 rc = msm_mem_free(mem->phyaddr);
115 if (rc < 0)
116 D("%s: Invalid memory location\n",
117 __func__);
118 else {
119 mem->phyaddr = 0;
120 }
121 }
122
123 q->bufs[i]->map = NULL;
124 q->bufs[i]->baddr = 0;
125 }
126
127 kfree(map);
128
129 mutex_unlock(&q->vb_lock);
130
131 /* deallocate the q->bufs[i] structure not a good solution
132 as it will result in unnecessary iterations but right now
133 this looks like the only cleaner way */
134 videobuf_mmap_free(q);
135 }
136}
137
138static const struct vm_operations_struct videobuf_vm_ops = {
139 .open = videobuf_vm_open,
140 .close = videobuf_vm_close,
141};
142
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143static struct videobuf_buffer *__videobuf_alloc(size_t size)
144{
145 struct videobuf_contig_pmem *mem;
146 struct videobuf_buffer *vb;
147
148 vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
149 if (vb) {
150 mem = vb->priv = ((char *)vb) + size;
151 mem->magic = MAGIC_PMEM;
152 }
153
154 return vb;
155}
156
157static void *__videobuf_to_vaddr(struct videobuf_buffer *buf)
158{
159 struct videobuf_contig_pmem *mem = buf->priv;
160
161 BUG_ON(!mem);
162 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
163
164 return mem->vaddr;
165}
166
167static int __videobuf_iolock(struct videobuf_queue *q,
168 struct videobuf_buffer *vb,
169 struct v4l2_framebuffer *fbuf)
170{
171 int rc = 0;
172 struct videobuf_contig_pmem *mem = vb->priv;
173
174 BUG_ON(!mem);
175 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
176
177 switch (vb->memory) {
178 case V4L2_MEMORY_MMAP:
179 D("%s memory method MMAP\n", __func__);
180
181 /* All handling should be done by __videobuf_mmap_mapper() */
182 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700183 case V4L2_MEMORY_OVERLAY:
184 default:
185 pr_err("%s memory method OVERLAY/unknown\n", __func__);
186 rc = -EINVAL;
187 }
188
189 return rc;
190}
191
192static int __videobuf_mmap_mapper(struct videobuf_queue *q,
193 struct videobuf_buffer *buf,
194 struct vm_area_struct *vma)
195{
196 struct videobuf_contig_pmem *mem;
197 struct videobuf_mapping *map;
198 int retval;
199 unsigned long size;
200
201 D("%s\n", __func__);
202
203 /* create mapping + update buffer list */
204 map = kzalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
205 if (!map) {
206 pr_err("%s: kzalloc failed.\n", __func__);
207 return -ENOMEM;
208 }
209
210 buf->map = map;
211 map->q = q;
212
213 buf->baddr = vma->vm_start;
214
215 mem = buf->priv;
216 D("mem = 0x%x\n", (u32)mem);
217 D("buf = 0x%x\n", (u32)buf);
218 BUG_ON(!mem);
219 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
220
221 mem->size = PAGE_ALIGN(buf->bsize);
Kiran Kumar H N5a19c682011-07-23 11:34:34 -0700222 mem->y_off = 0;
223 mem->cbcr_off = (buf->bsize)*2/3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 if (buf->i >= 0 && buf->i <= 3)
225 mem->buffer_type = OUTPUT_TYPE_P;
226 else
227 mem->buffer_type = OUTPUT_TYPE_V;
228
229 buf->bsize = mem->size;
230 mem->phyaddr = msm_mem_allocate(mem->size);
231
Ankit Premrajkaa5ea3a92011-08-04 17:54:17 -0700232 if (!mem->phyaddr) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233 pr_err("%s : pmem memory allocation failed\n", __func__);
234 goto error;
235 }
236
237 /* Try to remap memory */
238 size = vma->vm_end - vma->vm_start;
239 size = (size < mem->size) ? size : mem->size;
240
241 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
242 retval = remap_pfn_range(vma, vma->vm_start,
243 mem->phyaddr >> PAGE_SHIFT,
244 size, vma->vm_page_prot);
245 if (retval) {
246 pr_err("mmap: remap failed with error %d. ", retval);
247 retval = msm_mem_free(mem->phyaddr);
248 if (retval < 0)
249 printk(KERN_ERR "%s: Invalid memory location\n",
250 __func__);
251 else {
252 mem->phyaddr = 0;
253 }
254 goto error;
255 }
256
257 vma->vm_ops = &videobuf_vm_ops;
258 vma->vm_flags |= VM_DONTEXPAND;
259 vma->vm_private_data = map;
260
261 D("mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n",
262 map, q, vma->vm_start, vma->vm_end,
263 (long int)buf->bsize,
264 vma->vm_pgoff, buf->i);
265
266 videobuf_vm_open(vma);
267
268 return 0;
269
270error:
271 kfree(map);
272 return -ENOMEM;
273}
274
275static struct videobuf_qtype_ops qops = {
276 .magic = MAGIC_QTYPE_OPS,
277
278 .alloc_vb = __videobuf_alloc,
279 .iolock = __videobuf_iolock,
280 .mmap_mapper = __videobuf_mmap_mapper,
281 .vaddr = __videobuf_to_vaddr,
282};
283
284void videobuf_queue_pmem_contig_init(struct videobuf_queue *q,
285 const struct videobuf_queue_ops *ops,
286 struct device *dev,
287 spinlock_t *irqlock,
288 enum v4l2_buf_type type,
289 enum v4l2_field field,
290 unsigned int msize,
291 void *priv,
292 struct mutex *ext_lock)
293{
294 videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
295 priv, &qops, ext_lock);
296}
297EXPORT_SYMBOL_GPL(videobuf_queue_pmem_contig_init);
298
299int videobuf_to_pmem_contig(struct videobuf_buffer *buf)
300{
301 struct videobuf_contig_pmem *mem = buf->priv;
302
303 BUG_ON(!mem);
304 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
305
306 return mem->phyaddr;
307}
308EXPORT_SYMBOL_GPL(videobuf_to_pmem_contig);
309
310int videobuf_pmem_contig_free(struct videobuf_queue *q,
311 struct videobuf_buffer *buf)
312{
313 struct videobuf_contig_pmem *mem = buf->priv;
314
315 /* mmapped memory can't be freed here, otherwise mmapped region
316 would be released, while still needed. In this case, the memory
317 release should happen inside videobuf_vm_close().
318 So, it should free memory only if the memory were allocated for
319 read() operation.
320 */
321 if (buf->memory != V4L2_MEMORY_USERPTR)
322 return -EINVAL;
323
324 if (!mem)
325 return -ENOMEM;
326
327 MAGIC_CHECK(mem->magic, MAGIC_PMEM);
328
329 /* handle user space pointer case */
330 if (buf->baddr) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700331 return 0;
332 } else {
333 /* don't support read() method */
334 return -EINVAL;
335 }
336}
337EXPORT_SYMBOL_GPL(videobuf_pmem_contig_free);
338
339MODULE_DESCRIPTION("helper module to manage video4linux PMEM contig buffers");
340MODULE_LICENSE("GPL v2");