|
| 1 | +/* |
| 2 | + * Copyright 2010 - 2011 Broadcom Corporation. All rights reserved. |
| 3 | + * |
| 4 | + * Unless you and Broadcom execute a separate written software license |
| 5 | + * agreement governing use of this software, this software is licensed to you |
| 6 | + * under the terms of the GNU General Public License version 2, available at |
| 7 | + * http://www.broadcom.com/licenses/GPLv2.php (the "GPL"). |
| 8 | + * |
| 9 | + * Notwithstanding the above, under no circumstances may you combine this |
| 10 | + * software in any way with any other Broadcom software provided under a |
| 11 | + * license other than the GPL, without Broadcom's express prior written |
| 12 | + * consent. |
| 13 | + */ |
| 14 | + |
| 15 | +#include <linux/kernel.h> |
| 16 | +#include <linux/module.h> |
| 17 | +#include <linux/fs.h> |
| 18 | +#include <linux/device.h> |
| 19 | +#include <linux/cdev.h> |
| 20 | +#include <linux/mm.h> |
| 21 | +#include <linux/slab.h> |
| 22 | +#include <linux/debugfs.h> |
| 23 | +#include <linux/uaccess.h> |
| 24 | +#include <linux/dma-mapping.h> |
| 25 | +#include <linux/broadcom/vc_mem.h> |
| 26 | + |
| 27 | +#define DRIVER_NAME "vc-mem" |
| 28 | + |
| 29 | +/* Device (/dev) related variables */ |
| 30 | +static dev_t vc_mem_devnum; |
| 31 | +static struct class *vc_mem_class; |
| 32 | +static struct cdev vc_mem_cdev; |
| 33 | +static int vc_mem_inited; |
| 34 | + |
| 35 | +#ifdef CONFIG_DEBUG_FS |
| 36 | +static struct dentry *vc_mem_debugfs_entry; |
| 37 | +#endif |
| 38 | + |
| 39 | +/* |
| 40 | + * Videocore memory addresses and size |
| 41 | + * |
| 42 | + * Drivers that wish to know the videocore memory addresses and sizes should |
| 43 | + * use these variables instead of the MM_IO_BASE and MM_ADDR_IO defines in |
| 44 | + * headers. This allows the other drivers to not be tied down to a a certain |
| 45 | + * address/size at compile time. |
| 46 | + * |
| 47 | + * In the future, the goal is to have the videocore memory virtual address and |
| 48 | + * size be calculated at boot time rather than at compile time. The decision of |
| 49 | + * where the videocore memory resides and its size would be in the hands of the |
| 50 | + * bootloader (and/or kernel). When that happens, the values of these variables |
| 51 | + * would be calculated and assigned in the init function. |
| 52 | + */ |
| 53 | +/* In the 2835 VC in mapped above ARM, but ARM has full access to VC space */ |
| 54 | +unsigned long mm_vc_mem_phys_addr; |
| 55 | +EXPORT_SYMBOL(mm_vc_mem_phys_addr); |
| 56 | +unsigned int mm_vc_mem_size; |
| 57 | +EXPORT_SYMBOL(mm_vc_mem_size); |
| 58 | +unsigned int mm_vc_mem_base; |
| 59 | +EXPORT_SYMBOL(mm_vc_mem_base); |
| 60 | + |
| 61 | +static uint phys_addr; |
| 62 | +static uint mem_size; |
| 63 | +static uint mem_base; |
| 64 | + |
| 65 | +static int |
| 66 | +vc_mem_open(struct inode *inode, struct file *file) |
| 67 | +{ |
| 68 | + (void)inode; |
| 69 | + |
| 70 | + pr_debug("%s: called file = 0x%p\n", __func__, file); |
| 71 | + |
| 72 | + return 0; |
| 73 | +} |
| 74 | + |
| 75 | +static int |
| 76 | +vc_mem_release(struct inode *inode, struct file *file) |
| 77 | +{ |
| 78 | + (void)inode; |
| 79 | + |
| 80 | + pr_debug("%s: called file = 0x%p\n", __func__, file); |
| 81 | + |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
| 85 | +static void |
| 86 | +vc_mem_get_size(void) |
| 87 | +{ |
| 88 | +} |
| 89 | + |
| 90 | +static void |
| 91 | +vc_mem_get_base(void) |
| 92 | +{ |
| 93 | +} |
| 94 | + |
| 95 | +int |
| 96 | +vc_mem_get_current_size(void) |
| 97 | +{ |
| 98 | + return mm_vc_mem_size; |
| 99 | +} |
| 100 | +EXPORT_SYMBOL_GPL(vc_mem_get_current_size); |
| 101 | + |
| 102 | +static long |
| 103 | +vc_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 104 | +{ |
| 105 | + int rc = 0; |
| 106 | + |
| 107 | + (void) cmd; |
| 108 | + (void) arg; |
| 109 | + |
| 110 | + pr_debug("%s: called file = 0x%p, cmd %08x\n", __func__, file, cmd); |
| 111 | + |
| 112 | + switch (cmd) { |
| 113 | + case VC_MEM_IOC_MEM_PHYS_ADDR: |
| 114 | + { |
| 115 | + pr_debug("%s: VC_MEM_IOC_MEM_PHYS_ADDR=0x%p\n", |
| 116 | + __func__, (void *)mm_vc_mem_phys_addr); |
| 117 | + |
| 118 | + if (copy_to_user((void *)arg, &mm_vc_mem_phys_addr, |
| 119 | + sizeof(mm_vc_mem_phys_addr))) { |
| 120 | + rc = -EFAULT; |
| 121 | + } |
| 122 | + break; |
| 123 | + } |
| 124 | + case VC_MEM_IOC_MEM_SIZE: |
| 125 | + { |
| 126 | + /* Get the videocore memory size first */ |
| 127 | + vc_mem_get_size(); |
| 128 | + |
| 129 | + pr_debug("%s: VC_MEM_IOC_MEM_SIZE=%x\n", __func__, |
| 130 | + mm_vc_mem_size); |
| 131 | + |
| 132 | + if (copy_to_user((void *)arg, &mm_vc_mem_size, |
| 133 | + sizeof(mm_vc_mem_size))) { |
| 134 | + rc = -EFAULT; |
| 135 | + } |
| 136 | + break; |
| 137 | + } |
| 138 | + case VC_MEM_IOC_MEM_BASE: |
| 139 | + { |
| 140 | + /* Get the videocore memory base */ |
| 141 | + vc_mem_get_base(); |
| 142 | + |
| 143 | + pr_debug("%s: VC_MEM_IOC_MEM_BASE=%x\n", __func__, |
| 144 | + mm_vc_mem_base); |
| 145 | + |
| 146 | + if (copy_to_user((void *)arg, &mm_vc_mem_base, |
| 147 | + sizeof(mm_vc_mem_base))) { |
| 148 | + rc = -EFAULT; |
| 149 | + } |
| 150 | + break; |
| 151 | + } |
| 152 | + case VC_MEM_IOC_MEM_LOAD: |
| 153 | + { |
| 154 | + /* Get the videocore memory base */ |
| 155 | + vc_mem_get_base(); |
| 156 | + |
| 157 | + pr_debug("%s: VC_MEM_IOC_MEM_LOAD=%x\n", __func__, |
| 158 | + mm_vc_mem_base); |
| 159 | + |
| 160 | + if (copy_to_user((void *)arg, &mm_vc_mem_base, |
| 161 | + sizeof(mm_vc_mem_base))) { |
| 162 | + rc = -EFAULT; |
| 163 | + } |
| 164 | + break; |
| 165 | + } |
| 166 | + default: |
| 167 | + { |
| 168 | + return -ENOTTY; |
| 169 | + } |
| 170 | + } |
| 171 | + pr_debug("%s: file = 0x%p returning %d\n", __func__, file, rc); |
| 172 | + |
| 173 | + return rc; |
| 174 | +} |
| 175 | + |
| 176 | +#ifdef CONFIG_COMPAT |
| 177 | +static long |
| 178 | +vc_mem_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 179 | +{ |
| 180 | + int rc = 0; |
| 181 | + |
| 182 | + switch (cmd) { |
| 183 | + case VC_MEM_IOC_MEM_PHYS_ADDR32: |
| 184 | + pr_debug("%s: VC_MEM_IOC_MEM_PHYS_ADDR32=0x%p\n", |
| 185 | + __func__, (void *)mm_vc_mem_phys_addr); |
| 186 | + |
| 187 | + /* This isn't correct, but will cover us for now as |
| 188 | + * VideoCore is 32bit only. |
| 189 | + */ |
| 190 | + if (copy_to_user((void *)arg, &mm_vc_mem_phys_addr, |
| 191 | + sizeof(compat_ulong_t))) |
| 192 | + rc = -EFAULT; |
| 193 | + |
| 194 | + break; |
| 195 | + |
| 196 | + default: |
| 197 | + rc = vc_mem_ioctl(file, cmd, arg); |
| 198 | + break; |
| 199 | + } |
| 200 | + |
| 201 | + return rc; |
| 202 | +} |
| 203 | +#endif |
| 204 | + |
| 205 | +static int |
| 206 | +vc_mem_mmap(struct file *filp, struct vm_area_struct *vma) |
| 207 | +{ |
| 208 | + int rc = 0; |
| 209 | + unsigned long length = vma->vm_end - vma->vm_start; |
| 210 | + unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; |
| 211 | + |
| 212 | + pr_debug("%s: vm_start = 0x%08lx vm_end = 0x%08lx vm_pgoff = 0x%08lx\n", |
| 213 | + __func__, (long)vma->vm_start, (long)vma->vm_end, |
| 214 | + (long)vma->vm_pgoff); |
| 215 | + |
| 216 | + if (offset + length > mm_vc_mem_size) { |
| 217 | + pr_err("%s: length %ld is too big\n", __func__, length); |
| 218 | + return -EINVAL; |
| 219 | + } |
| 220 | + /* Do not cache the memory map */ |
| 221 | + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 222 | + |
| 223 | + rc = remap_pfn_range(vma, vma->vm_start, |
| 224 | + (mm_vc_mem_phys_addr >> PAGE_SHIFT) + |
| 225 | + vma->vm_pgoff, length, vma->vm_page_prot); |
| 226 | + if (rc) |
| 227 | + pr_err("%s: remap_pfn_range failed (rc=%d)\n", __func__, rc); |
| 228 | + |
| 229 | + return rc; |
| 230 | +} |
| 231 | + |
| 232 | +/* File Operations for the driver. */ |
| 233 | +static const struct file_operations vc_mem_fops = { |
| 234 | + .owner = THIS_MODULE, |
| 235 | + .open = vc_mem_open, |
| 236 | + .release = vc_mem_release, |
| 237 | + .unlocked_ioctl = vc_mem_ioctl, |
| 238 | +#ifdef CONFIG_COMPAT |
| 239 | + .compat_ioctl = vc_mem_compat_ioctl, |
| 240 | +#endif |
| 241 | + .mmap = vc_mem_mmap, |
| 242 | +}; |
| 243 | + |
| 244 | +#ifdef CONFIG_DEBUG_FS |
| 245 | +static void vc_mem_debugfs_deinit(void) |
| 246 | +{ |
| 247 | + debugfs_remove_recursive(vc_mem_debugfs_entry); |
| 248 | + vc_mem_debugfs_entry = NULL; |
| 249 | +} |
| 250 | + |
| 251 | + |
| 252 | +static int vc_mem_debugfs_init( |
| 253 | + struct device *dev) |
| 254 | +{ |
| 255 | + vc_mem_debugfs_entry = debugfs_create_dir(DRIVER_NAME, NULL); |
| 256 | + if (!vc_mem_debugfs_entry) { |
| 257 | + dev_warn(dev, "could not create debugfs entry\n"); |
| 258 | + return -EFAULT; |
| 259 | + } |
| 260 | + |
| 261 | + debugfs_create_x32("vc_mem_phys_addr", |
| 262 | + 0444, |
| 263 | + vc_mem_debugfs_entry, |
| 264 | + (u32 *)&mm_vc_mem_phys_addr); |
| 265 | + debugfs_create_x32("vc_mem_size", |
| 266 | + 0444, |
| 267 | + vc_mem_debugfs_entry, |
| 268 | + (u32 *)&mm_vc_mem_size); |
| 269 | + debugfs_create_x32("vc_mem_base", |
| 270 | + 0444, |
| 271 | + vc_mem_debugfs_entry, |
| 272 | + (u32 *)&mm_vc_mem_base); |
| 273 | + |
| 274 | + return 0; |
| 275 | +} |
| 276 | + |
| 277 | +#endif /* CONFIG_DEBUG_FS */ |
| 278 | + |
| 279 | +/* Module load/unload functions */ |
| 280 | + |
| 281 | +static int __init |
| 282 | +vc_mem_init(void) |
| 283 | +{ |
| 284 | + int rc = -EFAULT; |
| 285 | + struct device *dev; |
| 286 | + |
| 287 | + pr_debug("%s: called\n", __func__); |
| 288 | + |
| 289 | + mm_vc_mem_phys_addr = phys_addr; |
| 290 | + mm_vc_mem_size = mem_size; |
| 291 | + mm_vc_mem_base = mem_base; |
| 292 | + |
| 293 | + vc_mem_get_size(); |
| 294 | + |
| 295 | + pr_info("vc-mem: phys_addr:0x%08lx mem_base=0x%08x mem_size:0x%08x(%u MiB)\n", |
| 296 | + mm_vc_mem_phys_addr, mm_vc_mem_base, mm_vc_mem_size, |
| 297 | + mm_vc_mem_size / (1024 * 1024)); |
| 298 | + |
| 299 | + rc = alloc_chrdev_region(&vc_mem_devnum, 0, 1, DRIVER_NAME); |
| 300 | + if (rc < 0) { |
| 301 | + pr_err("%s: alloc_chrdev_region failed (rc=%d)\n", |
| 302 | + __func__, rc); |
| 303 | + goto out_err; |
| 304 | + } |
| 305 | + |
| 306 | + cdev_init(&vc_mem_cdev, &vc_mem_fops); |
| 307 | + rc = cdev_add(&vc_mem_cdev, vc_mem_devnum, 1); |
| 308 | + if (rc) { |
| 309 | + pr_err("%s: cdev_add failed (rc=%d)\n", __func__, rc); |
| 310 | + goto out_unregister; |
| 311 | + } |
| 312 | + |
| 313 | + vc_mem_class = class_create(THIS_MODULE, DRIVER_NAME); |
| 314 | + if (IS_ERR(vc_mem_class)) { |
| 315 | + rc = PTR_ERR(vc_mem_class); |
| 316 | + pr_err("%s: class_create failed (rc=%d)\n", __func__, rc); |
| 317 | + goto out_cdev_del; |
| 318 | + } |
| 319 | + |
| 320 | + dev = device_create(vc_mem_class, NULL, vc_mem_devnum, NULL, |
| 321 | + DRIVER_NAME); |
| 322 | + if (IS_ERR(dev)) { |
| 323 | + rc = PTR_ERR(dev); |
| 324 | + pr_err("%s: device_create failed (rc=%d)\n", __func__, rc); |
| 325 | + goto out_class_destroy; |
| 326 | + } |
| 327 | + |
| 328 | +#ifdef CONFIG_DEBUG_FS |
| 329 | + /* don't fail if the debug entries cannot be created */ |
| 330 | + vc_mem_debugfs_init(dev); |
| 331 | +#endif |
| 332 | + |
| 333 | + vc_mem_inited = 1; |
| 334 | + return 0; |
| 335 | + |
| 336 | +out_class_destroy: |
| 337 | + class_destroy(vc_mem_class); |
| 338 | + vc_mem_class = NULL; |
| 339 | + |
| 340 | +out_cdev_del: |
| 341 | + cdev_del(&vc_mem_cdev); |
| 342 | + |
| 343 | +out_unregister: |
| 344 | + unregister_chrdev_region(vc_mem_devnum, 1); |
| 345 | + |
| 346 | +out_err: |
| 347 | + return -1; |
| 348 | +} |
| 349 | + |
| 350 | +static void __exit |
| 351 | +vc_mem_exit(void) |
| 352 | +{ |
| 353 | + pr_debug("%s: called\n", __func__); |
| 354 | + |
| 355 | + if (vc_mem_inited) { |
| 356 | +#if CONFIG_DEBUG_FS |
| 357 | + vc_mem_debugfs_deinit(); |
| 358 | +#endif |
| 359 | + device_destroy(vc_mem_class, vc_mem_devnum); |
| 360 | + class_destroy(vc_mem_class); |
| 361 | + cdev_del(&vc_mem_cdev); |
| 362 | + unregister_chrdev_region(vc_mem_devnum, 1); |
| 363 | + } |
| 364 | +} |
| 365 | + |
| 366 | +module_init(vc_mem_init); |
| 367 | +module_exit(vc_mem_exit); |
| 368 | +MODULE_LICENSE("GPL"); |
| 369 | +MODULE_AUTHOR("Broadcom Corporation"); |
| 370 | + |
| 371 | +module_param(phys_addr, uint, 0644); |
| 372 | +module_param(mem_size, uint, 0644); |
| 373 | +module_param(mem_base, uint, 0644); |
0 commit comments