2 * Copyright 2010 - 2011 Broadcom Corporation. All rights reserved.
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").
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
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/device.h>
19 #include <linux/cdev.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>
27 #define DRIVER_NAME "vc-mem"
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;
35 #ifdef CONFIG_DEBUG_FS
36 static struct dentry *vc_mem_debugfs_entry;
40 * Videocore memory addresses and size
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.
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.
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);
61 static uint phys_addr;
66 vc_mem_open(struct inode *inode, struct file *file)
70 pr_debug("%s: called file = 0x%p\n", __func__, file);
76 vc_mem_release(struct inode *inode, struct file *file)
80 pr_debug("%s: called file = 0x%p\n", __func__, file);
96 vc_mem_get_current_size(void)
98 return mm_vc_mem_size;
100 EXPORT_SYMBOL_GPL(vc_mem_get_current_size);
103 vc_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
110 pr_debug("%s: called file = 0x%p, cmd %08x\n", __func__, file, cmd);
113 case VC_MEM_IOC_MEM_PHYS_ADDR:
115 pr_debug("%s: VC_MEM_IOC_MEM_PHYS_ADDR=0x%p\n",
116 __func__, (void *)mm_vc_mem_phys_addr);
118 if (copy_to_user((void *)arg, &mm_vc_mem_phys_addr,
119 sizeof(mm_vc_mem_phys_addr))) {
124 case VC_MEM_IOC_MEM_SIZE:
126 /* Get the videocore memory size first */
129 pr_debug("%s: VC_MEM_IOC_MEM_SIZE=%x\n", __func__,
132 if (copy_to_user((void *)arg, &mm_vc_mem_size,
133 sizeof(mm_vc_mem_size))) {
138 case VC_MEM_IOC_MEM_BASE:
140 /* Get the videocore memory base */
143 pr_debug("%s: VC_MEM_IOC_MEM_BASE=%x\n", __func__,
146 if (copy_to_user((void *)arg, &mm_vc_mem_base,
147 sizeof(mm_vc_mem_base))) {
152 case VC_MEM_IOC_MEM_LOAD:
154 /* Get the videocore memory base */
157 pr_debug("%s: VC_MEM_IOC_MEM_LOAD=%x\n", __func__,
160 if (copy_to_user((void *)arg, &mm_vc_mem_base,
161 sizeof(mm_vc_mem_base))) {
171 pr_debug("%s: file = 0x%p returning %d\n", __func__, file, rc);
178 vc_mem_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
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);
187 /* This isn't correct, but will cover us for now as
188 * VideoCore is 32bit only.
190 if (copy_to_user((void *)arg, &mm_vc_mem_phys_addr,
191 sizeof(compat_ulong_t)))
197 rc = vc_mem_ioctl(file, cmd, arg);
206 vc_mem_mmap(struct file *filp, struct vm_area_struct *vma)
209 unsigned long length = vma->vm_end - vma->vm_start;
210 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
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);
216 if (offset + length > mm_vc_mem_size) {
217 pr_err("%s: length %ld is too big\n", __func__, length);
220 /* Do not cache the memory map */
221 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
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);
227 pr_err("%s: remap_pfn_range failed (rc=%d)\n", __func__, rc);
232 /* File Operations for the driver. */
233 static const struct file_operations vc_mem_fops = {
234 .owner = THIS_MODULE,
236 .release = vc_mem_release,
237 .unlocked_ioctl = vc_mem_ioctl,
239 .compat_ioctl = vc_mem_compat_ioctl,
244 #ifdef CONFIG_DEBUG_FS
245 static void vc_mem_debugfs_deinit(void)
247 debugfs_remove_recursive(vc_mem_debugfs_entry);
248 vc_mem_debugfs_entry = NULL;
252 static int vc_mem_debugfs_init(
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");
261 debugfs_create_x32("vc_mem_phys_addr",
263 vc_mem_debugfs_entry,
264 (u32 *)&mm_vc_mem_phys_addr);
265 debugfs_create_x32("vc_mem_size",
267 vc_mem_debugfs_entry,
268 (u32 *)&mm_vc_mem_size);
269 debugfs_create_x32("vc_mem_base",
271 vc_mem_debugfs_entry,
272 (u32 *)&mm_vc_mem_base);
277 #endif /* CONFIG_DEBUG_FS */
279 /* Module load/unload functions */
287 pr_debug("%s: called\n", __func__);
289 mm_vc_mem_phys_addr = phys_addr;
290 mm_vc_mem_size = mem_size;
291 mm_vc_mem_base = mem_base;
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));
299 rc = alloc_chrdev_region(&vc_mem_devnum, 0, 1, DRIVER_NAME);
301 pr_err("%s: alloc_chrdev_region failed (rc=%d)\n",
306 cdev_init(&vc_mem_cdev, &vc_mem_fops);
307 rc = cdev_add(&vc_mem_cdev, vc_mem_devnum, 1);
309 pr_err("%s: cdev_add failed (rc=%d)\n", __func__, rc);
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);
320 dev = device_create(vc_mem_class, NULL, vc_mem_devnum, NULL,
324 pr_err("%s: device_create failed (rc=%d)\n", __func__, rc);
325 goto out_class_destroy;
328 #ifdef CONFIG_DEBUG_FS
329 /* don't fail if the debug entries cannot be created */
330 vc_mem_debugfs_init(dev);
337 class_destroy(vc_mem_class);
341 cdev_del(&vc_mem_cdev);
344 unregister_chrdev_region(vc_mem_devnum, 1);
353 pr_debug("%s: called\n", __func__);
357 vc_mem_debugfs_deinit();
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);
366 module_init(vc_mem_init);
367 module_exit(vc_mem_exit);
368 MODULE_LICENSE("GPL");
369 MODULE_AUTHOR("Broadcom Corporation");
371 module_param(phys_addr, uint, 0644);
372 module_param(mem_size, uint, 0644);
373 module_param(mem_base, uint, 0644);