Mingcheng Zhu | 9812bd3 | 2011-07-22 22:57:11 -0700 | [diff] [blame] | 1 | /* 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> |
| 32 | |
| 33 | #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 | |
| 48 | static 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 | |
| 55 | static 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 | |
| 62 | static 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 | } |
| 69 | static 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 | |
| 77 | static const struct vm_operations_struct videobuf2_vm_ops = { |
| 78 | .open = videobuf2_vm_open, |
| 79 | .close = videobuf2_vm_close, |
| 80 | }; |
| 81 | |
| 82 | static void *msm_vb2_mem_ops_alloc(void *alloc_ctx, unsigned long size) |
| 83 | { |
| 84 | struct videobuf2_contig_pmem *mem; |
| 85 | mem = kzalloc(sizeof(*mem), GFP_KERNEL); |
| 86 | if (!mem) |
| 87 | return ERR_PTR(-ENOMEM); |
| 88 | |
| 89 | mem->magic = MAGIC_PMEM; |
| 90 | mem->size = PAGE_ALIGN(size); |
| 91 | mem->alloc_ctx = alloc_ctx; |
| 92 | mem->is_userptr = 0; |
| 93 | mem->phyaddr = msm_mem_allocate(mem->size); |
| 94 | if (IS_ERR((void *)mem->phyaddr)) { |
| 95 | pr_err("%s : pmem memory allocation failed\n", __func__); |
| 96 | kfree(mem); |
| 97 | return ERR_PTR(-ENOMEM); |
| 98 | } |
| 99 | return mem; |
| 100 | } |
| 101 | static void msm_vb2_mem_ops_put(void *buf_priv) |
| 102 | { |
| 103 | struct videobuf2_contig_pmem *mem = buf_priv; |
| 104 | if (!mem->is_userptr) |
| 105 | msm_mem_free(mem->phyaddr); |
| 106 | kfree(mem); |
| 107 | } |
| 108 | int videobuf2_pmem_contig_mmap_get(struct videobuf2_contig_pmem *mem, |
| 109 | uint32_t yoffset, |
| 110 | uint32_t cbcroffset, int path) |
| 111 | { |
| 112 | mem->y_off = yoffset; |
| 113 | mem->cbcr_off = cbcroffset; |
| 114 | mem->buffer_type = path; |
| 115 | return 0; |
| 116 | } |
| 117 | EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_mmap_get); |
| 118 | |
| 119 | /** |
| 120 | * videobuf_pmem_contig_user_get() - setup user space memory pointer |
| 121 | * @mem: per-buffer private videobuf-contig-pmem data |
| 122 | * @vb: video buffer to map |
| 123 | * |
| 124 | * This function validates and sets up a pointer to user space memory. |
| 125 | * Only physically contiguous pfn-mapped memory is accepted. |
| 126 | * |
| 127 | * Returns 0 if successful. |
| 128 | */ |
| 129 | int videobuf2_pmem_contig_user_get(struct videobuf2_contig_pmem *mem, |
| 130 | uint32_t yoffset, uint32_t cbcroffset, |
| 131 | uint32_t addr_offset, int path) |
| 132 | { |
| 133 | unsigned long kvstart; |
| 134 | unsigned long len; |
| 135 | int rc; |
| 136 | |
| 137 | if (mem->phyaddr != 0) |
| 138 | return 0; |
| 139 | |
| 140 | rc = get_pmem_file((int)mem->vaddr, (unsigned long *)&mem->phyaddr, |
| 141 | &kvstart, &len, &mem->file); |
| 142 | if (rc < 0) { |
| 143 | pr_err("%s: get_pmem_file fd %d error %d\n", |
| 144 | __func__, (int)mem->vaddr, rc); |
| 145 | return rc; |
| 146 | } |
| 147 | mem->phyaddr += addr_offset; |
| 148 | mem->y_off = yoffset; |
| 149 | mem->cbcr_off = cbcroffset; |
| 150 | mem->buffer_type = path; |
| 151 | return rc; |
| 152 | } |
| 153 | EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_user_get); |
| 154 | |
| 155 | void videobuf2_pmem_contig_user_put(struct videobuf2_contig_pmem *mem) |
| 156 | { |
| 157 | if (mem->is_userptr) |
| 158 | put_pmem_file(mem->file); |
| 159 | mem->is_userptr = 0; |
| 160 | mem->phyaddr = 0; |
| 161 | mem->size = 0; |
| 162 | } |
| 163 | EXPORT_SYMBOL_GPL(videobuf2_pmem_contig_user_put); |
| 164 | |
| 165 | static void *msm_vb2_mem_ops_get_userptr(void *alloc_ctx, unsigned long vaddr, |
| 166 | unsigned long size, int write) |
| 167 | { |
| 168 | struct videobuf2_contig_pmem *mem; |
| 169 | mem = kzalloc(sizeof(*mem), GFP_KERNEL); |
| 170 | if (!mem) |
| 171 | return ERR_PTR(-ENOMEM); |
| 172 | mem->magic = MAGIC_PMEM; |
| 173 | mem->is_userptr = 1; |
| 174 | mem->vaddr = (void *)vaddr; |
| 175 | mem->size = size; |
| 176 | mem->alloc_ctx = alloc_ctx; |
| 177 | return mem; |
| 178 | } |
| 179 | static void msm_vb2_mem_ops_put_userptr(void *buf_priv) |
| 180 | { |
| 181 | kfree(buf_priv); |
| 182 | } |
| 183 | |
| 184 | static void *msm_vb2_mem_ops_vaddr(void *buf_priv) |
| 185 | { |
| 186 | struct videobuf2_contig_pmem *mem = buf_priv; |
| 187 | return mem->vaddr; |
| 188 | } |
| 189 | static void *msm_vb2_mem_ops_cookie(void *buf_priv) |
| 190 | { |
| 191 | return buf_priv; |
| 192 | } |
| 193 | static unsigned int msm_vb2_mem_ops_num_users(void *buf_priv) |
| 194 | { |
| 195 | struct videobuf2_contig_pmem *mem = buf_priv; |
| 196 | MAGIC_CHECK(mem->magic, MAGIC_PMEM); |
| 197 | return mem->count; |
| 198 | } |
| 199 | static int msm_vb2_mem_ops_mmap(void *buf_priv, struct vm_area_struct *vma) |
| 200 | { |
| 201 | struct videobuf2_contig_pmem *mem; |
| 202 | int retval; |
| 203 | unsigned long size; |
| 204 | D("%s\n", __func__); |
| 205 | mem = buf_priv; |
| 206 | D("mem = 0x%x\n", (u32)mem); |
| 207 | BUG_ON(!mem); |
| 208 | MAGIC_CHECK(mem->magic, MAGIC_PMEM); |
| 209 | |
| 210 | /* Try to remap memory */ |
| 211 | size = vma->vm_end - vma->vm_start; |
| 212 | size = (size < mem->size) ? size : mem->size; |
| 213 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 214 | retval = remap_pfn_range(vma, vma->vm_start, |
| 215 | mem->phyaddr >> PAGE_SHIFT, |
| 216 | size, vma->vm_page_prot); |
| 217 | if (retval) { |
| 218 | pr_err("mmap: remap failed with error %d. ", retval); |
| 219 | goto error; |
| 220 | } |
| 221 | mem->vaddr = (void *)vma->vm_start; |
| 222 | vma->vm_ops = &videobuf2_vm_ops; |
| 223 | vma->vm_flags |= VM_DONTEXPAND; |
| 224 | vma->vm_private_data = mem; |
| 225 | |
| 226 | D("mmap %p: %08lx-%08lx (%lx) pgoff %08lx\n", |
| 227 | map, vma->vm_start, vma->vm_end, |
| 228 | (long int)mem->bsize, vma->vm_pgoff); |
| 229 | videobuf2_vm_open(vma); |
| 230 | return 0; |
| 231 | error: |
| 232 | return -ENOMEM; |
| 233 | } |
| 234 | |
| 235 | static struct vb2_mem_ops msm_vb2_mem_ops = { |
| 236 | .alloc = msm_vb2_mem_ops_alloc, |
| 237 | .put = msm_vb2_mem_ops_put, |
| 238 | .get_userptr = msm_vb2_mem_ops_get_userptr, |
| 239 | .put_userptr = msm_vb2_mem_ops_put_userptr, |
| 240 | .vaddr = msm_vb2_mem_ops_vaddr, |
| 241 | .cookie = msm_vb2_mem_ops_cookie, |
| 242 | .num_users = msm_vb2_mem_ops_num_users, |
| 243 | .mmap = msm_vb2_mem_ops_mmap |
| 244 | }; |
| 245 | |
| 246 | void videobuf2_queue_pmem_contig_init(struct vb2_queue *q, |
| 247 | enum v4l2_buf_type type, |
| 248 | const struct vb2_ops *ops, |
| 249 | unsigned int size, |
| 250 | void *priv) |
| 251 | { |
| 252 | memset(q, 0, sizeof(struct vb2_queue)); |
| 253 | q->mem_ops = &msm_vb2_mem_ops; |
| 254 | q->ops = ops; |
| 255 | q->drv_priv = priv; |
| 256 | q->type = type; |
| 257 | q->io_modes = VB2_MMAP | VB2_USERPTR; |
| 258 | q->io_flags = 0; |
| 259 | q->buf_struct_size = size; |
| 260 | vb2_queue_init(q); |
| 261 | } |
| 262 | EXPORT_SYMBOL_GPL(videobuf2_queue_pmem_contig_init); |
| 263 | |
| 264 | int videobuf2_to_pmem_contig(struct vb2_buffer *vb, unsigned int plane_no) |
| 265 | { |
| 266 | struct videobuf2_contig_pmem *mem; |
| 267 | mem = vb2_plane_cookie(vb, plane_no); |
| 268 | BUG_ON(!mem); |
| 269 | MAGIC_CHECK(mem->magic, MAGIC_PMEM); |
| 270 | return mem->phyaddr; |
| 271 | } |
| 272 | EXPORT_SYMBOL_GPL(videobuf2_to_pmem_contig); |
| 273 | |
| 274 | MODULE_DESCRIPTION("helper module to manage video4linux PMEM contig buffers"); |
| 275 | MODULE_LICENSE("GPL v2"); |