1 From 94c0e75bc85ad2a034c501b6d3640b880b9c3bb7 Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Fri, 28 Oct 2016 15:36:43 +0100
4 Subject: [PATCH] vc_mem: Add vc_mem driver for querying firmware memory
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
10 Signed-off-by: popcornmix <popcornmix@gmail.com>
14 Make the vc_mem module available for ARCH_BCM2835 by moving it.
16 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
18 drivers/char/broadcom/Kconfig | 12 +-
19 drivers/char/broadcom/Makefile | 1 +
20 drivers/char/broadcom/vc_mem.c | 422 ++++++++++++++++++++++++++++++++++++++++
21 include/linux/broadcom/vc_mem.h | 35 ++++
22 4 files changed, 469 insertions(+), 1 deletion(-)
23 create mode 100644 drivers/char/broadcom/vc_mem.c
24 create mode 100644 include/linux/broadcom/vc_mem.h
26 --- a/drivers/char/broadcom/Kconfig
27 +++ b/drivers/char/broadcom/Kconfig
28 @@ -7,9 +7,19 @@ menuconfig BRCM_CHAR_DRIVERS
30 Broadcom's char drivers
36 - depends on CMA && BRCM_CHAR_DRIVERS && BCM2708_VCHIQ
37 + depends on CMA && BCM2708_VCHIQ
40 Helper for videocore CMA access.
43 + bool "Videocore Memory"
46 + Helper for videocore memory access and total size allocation.
49 --- a/drivers/char/broadcom/Makefile
50 +++ b/drivers/char/broadcom/Makefile
52 obj-$(CONFIG_BCM_VC_CMA) += vc_cma/
53 +obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
55 +++ b/drivers/char/broadcom/vc_mem.c
57 +/*****************************************************************************
58 +* Copyright 2010 - 2011 Broadcom Corporation. All rights reserved.
60 +* Unless you and Broadcom execute a separate written software license
61 +* agreement governing use of this software, this software is licensed to you
62 +* under the terms of the GNU General Public License version 2, available at
63 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
65 +* Notwithstanding the above, under no circumstances may you combine this
66 +* software in any way with any other Broadcom software provided under a
67 +* license other than the GPL, without Broadcom's express prior written
69 +*****************************************************************************/
71 +#include <linux/kernel.h>
72 +#include <linux/module.h>
73 +#include <linux/fs.h>
74 +#include <linux/device.h>
75 +#include <linux/cdev.h>
76 +#include <linux/mm.h>
77 +#include <linux/slab.h>
78 +#include <linux/debugfs.h>
79 +#include <asm/uaccess.h>
80 +#include <linux/dma-mapping.h>
81 +#include <linux/broadcom/vc_mem.h>
83 +#define DRIVER_NAME "vc-mem"
85 +// Device (/dev) related variables
86 +static dev_t vc_mem_devnum = 0;
87 +static struct class *vc_mem_class = NULL;
88 +static struct cdev vc_mem_cdev;
89 +static int vc_mem_inited = 0;
91 +#ifdef CONFIG_DEBUG_FS
92 +static struct dentry *vc_mem_debugfs_entry;
96 + * Videocore memory addresses and size
98 + * Drivers that wish to know the videocore memory addresses and sizes should
99 + * use these variables instead of the MM_IO_BASE and MM_ADDR_IO defines in
100 + * headers. This allows the other drivers to not be tied down to a a certain
101 + * address/size at compile time.
103 + * In the future, the goal is to have the videocore memory virtual address and
104 + * size be calculated at boot time rather than at compile time. The decision of
105 + * where the videocore memory resides and its size would be in the hands of the
106 + * bootloader (and/or kernel). When that happens, the values of these variables
107 + * would be calculated and assigned in the init function.
109 +// in the 2835 VC in mapped above ARM, but ARM has full access to VC space
110 +unsigned long mm_vc_mem_phys_addr = 0x00000000;
111 +unsigned int mm_vc_mem_size = 0;
112 +unsigned int mm_vc_mem_base = 0;
114 +EXPORT_SYMBOL(mm_vc_mem_phys_addr);
115 +EXPORT_SYMBOL(mm_vc_mem_size);
116 +EXPORT_SYMBOL(mm_vc_mem_base);
118 +static uint phys_addr = 0;
119 +static uint mem_size = 0;
120 +static uint mem_base = 0;
123 +/****************************************************************************
127 +***************************************************************************/
130 +vc_mem_open(struct inode *inode, struct file *file)
135 + pr_debug("%s: called file = 0x%p\n", __func__, file);
140 +/****************************************************************************
144 +***************************************************************************/
147 +vc_mem_release(struct inode *inode, struct file *file)
152 + pr_debug("%s: called file = 0x%p\n", __func__, file);
157 +/****************************************************************************
161 +***************************************************************************/
164 +vc_mem_get_size(void)
168 +/****************************************************************************
172 +***************************************************************************/
175 +vc_mem_get_base(void)
179 +/****************************************************************************
181 +* vc_mem_get_current_size
183 +***************************************************************************/
186 +vc_mem_get_current_size(void)
188 + return mm_vc_mem_size;
191 +EXPORT_SYMBOL_GPL(vc_mem_get_current_size);
193 +/****************************************************************************
197 +***************************************************************************/
200 +vc_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
207 + pr_debug("%s: called file = 0x%p\n", __func__, file);
210 + case VC_MEM_IOC_MEM_PHYS_ADDR:
212 + pr_debug("%s: VC_MEM_IOC_MEM_PHYS_ADDR=0x%p\n",
213 + __func__, (void *) mm_vc_mem_phys_addr);
215 + if (copy_to_user((void *) arg, &mm_vc_mem_phys_addr,
216 + sizeof (mm_vc_mem_phys_addr)) != 0) {
221 + case VC_MEM_IOC_MEM_SIZE:
223 + // Get the videocore memory size first
226 + pr_debug("%s: VC_MEM_IOC_MEM_SIZE=%u\n", __func__,
229 + if (copy_to_user((void *) arg, &mm_vc_mem_size,
230 + sizeof (mm_vc_mem_size)) != 0) {
235 + case VC_MEM_IOC_MEM_BASE:
237 + // Get the videocore memory base
240 + pr_debug("%s: VC_MEM_IOC_MEM_BASE=%u\n", __func__,
243 + if (copy_to_user((void *) arg, &mm_vc_mem_base,
244 + sizeof (mm_vc_mem_base)) != 0) {
249 + case VC_MEM_IOC_MEM_LOAD:
251 + // Get the videocore memory base
254 + pr_debug("%s: VC_MEM_IOC_MEM_LOAD=%u\n", __func__,
257 + if (copy_to_user((void *) arg, &mm_vc_mem_base,
258 + sizeof (mm_vc_mem_base)) != 0) {
268 + pr_debug("%s: file = 0x%p returning %d\n", __func__, file, rc);
273 +/****************************************************************************
277 +***************************************************************************/
280 +vc_mem_mmap(struct file *filp, struct vm_area_struct *vma)
283 + unsigned long length = vma->vm_end - vma->vm_start;
284 + unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
286 + pr_debug("%s: vm_start = 0x%08lx vm_end = 0x%08lx vm_pgoff = 0x%08lx\n",
287 + __func__, (long) vma->vm_start, (long) vma->vm_end,
288 + (long) vma->vm_pgoff);
290 + if (offset + length > mm_vc_mem_size) {
291 + pr_err("%s: length %ld is too big\n", __func__, length);
294 + // Do not cache the memory map
295 + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
297 + rc = remap_pfn_range(vma, vma->vm_start,
298 + (mm_vc_mem_phys_addr >> PAGE_SHIFT) +
299 + vma->vm_pgoff, length, vma->vm_page_prot);
301 + pr_err("%s: remap_pfn_range failed (rc=%d)\n", __func__, rc);
307 +/****************************************************************************
309 +* File Operations for the driver.
311 +***************************************************************************/
313 +static const struct file_operations vc_mem_fops = {
314 + .owner = THIS_MODULE,
315 + .open = vc_mem_open,
316 + .release = vc_mem_release,
317 + .unlocked_ioctl = vc_mem_ioctl,
318 + .mmap = vc_mem_mmap,
321 +#ifdef CONFIG_DEBUG_FS
322 +static void vc_mem_debugfs_deinit(void)
324 + debugfs_remove_recursive(vc_mem_debugfs_entry);
325 + vc_mem_debugfs_entry = NULL;
329 +static int vc_mem_debugfs_init(
330 + struct device *dev)
332 + vc_mem_debugfs_entry = debugfs_create_dir(DRIVER_NAME, NULL);
333 + if (!vc_mem_debugfs_entry) {
334 + dev_warn(dev, "could not create debugfs entry\n");
338 + if (!debugfs_create_x32("vc_mem_phys_addr",
340 + vc_mem_debugfs_entry,
341 + (u32 *)&mm_vc_mem_phys_addr)) {
342 + dev_warn(dev, "%s:could not create vc_mem_phys entry\n",
347 + if (!debugfs_create_x32("vc_mem_size",
349 + vc_mem_debugfs_entry,
350 + (u32 *)&mm_vc_mem_size)) {
351 + dev_warn(dev, "%s:could not create vc_mem_size entry\n",
356 + if (!debugfs_create_x32("vc_mem_base",
358 + vc_mem_debugfs_entry,
359 + (u32 *)&mm_vc_mem_base)) {
360 + dev_warn(dev, "%s:could not create vc_mem_base entry\n",
368 + vc_mem_debugfs_deinit();
372 +#endif /* CONFIG_DEBUG_FS */
375 +/****************************************************************************
379 +***************************************************************************/
385 + struct device *dev;
387 + pr_debug("%s: called\n", __func__);
389 + mm_vc_mem_phys_addr = phys_addr;
390 + mm_vc_mem_size = mem_size;
391 + mm_vc_mem_base = mem_base;
395 + pr_info("vc-mem: phys_addr:0x%08lx mem_base=0x%08x mem_size:0x%08x(%u MiB)\n",
396 + mm_vc_mem_phys_addr, mm_vc_mem_base, mm_vc_mem_size, mm_vc_mem_size / (1024 * 1024));
398 + if ((rc = alloc_chrdev_region(&vc_mem_devnum, 0, 1, DRIVER_NAME)) < 0) {
399 + pr_err("%s: alloc_chrdev_region failed (rc=%d)\n",
404 + cdev_init(&vc_mem_cdev, &vc_mem_fops);
405 + if ((rc = cdev_add(&vc_mem_cdev, vc_mem_devnum, 1)) != 0) {
406 + pr_err("%s: cdev_add failed (rc=%d)\n", __func__, rc);
407 + goto out_unregister;
410 + vc_mem_class = class_create(THIS_MODULE, DRIVER_NAME);
411 + if (IS_ERR(vc_mem_class)) {
412 + rc = PTR_ERR(vc_mem_class);
413 + pr_err("%s: class_create failed (rc=%d)\n", __func__, rc);
417 + dev = device_create(vc_mem_class, NULL, vc_mem_devnum, NULL,
421 + pr_err("%s: device_create failed (rc=%d)\n", __func__, rc);
422 + goto out_class_destroy;
425 +#ifdef CONFIG_DEBUG_FS
426 + /* don't fail if the debug entries cannot be created */
427 + vc_mem_debugfs_init(dev);
433 + device_destroy(vc_mem_class, vc_mem_devnum);
436 + class_destroy(vc_mem_class);
437 + vc_mem_class = NULL;
440 + cdev_del(&vc_mem_cdev);
443 + unregister_chrdev_region(vc_mem_devnum, 1);
449 +/****************************************************************************
453 +***************************************************************************/
458 + pr_debug("%s: called\n", __func__);
460 + if (vc_mem_inited) {
462 + vc_mem_debugfs_deinit();
464 + device_destroy(vc_mem_class, vc_mem_devnum);
465 + class_destroy(vc_mem_class);
466 + cdev_del(&vc_mem_cdev);
467 + unregister_chrdev_region(vc_mem_devnum, 1);
471 +module_init(vc_mem_init);
472 +module_exit(vc_mem_exit);
473 +MODULE_LICENSE("GPL");
474 +MODULE_AUTHOR("Broadcom Corporation");
476 +module_param(phys_addr, uint, 0644);
477 +module_param(mem_size, uint, 0644);
478 +module_param(mem_base, uint, 0644);
480 +++ b/include/linux/broadcom/vc_mem.h
482 +/*****************************************************************************
483 +* Copyright 2010 - 2011 Broadcom Corporation. All rights reserved.
485 +* Unless you and Broadcom execute a separate written software license
486 +* agreement governing use of this software, this software is licensed to you
487 +* under the terms of the GNU General Public License version 2, available at
488 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
490 +* Notwithstanding the above, under no circumstances may you combine this
491 +* software in any way with any other Broadcom software provided under a
492 +* license other than the GPL, without Broadcom's express prior written
494 +*****************************************************************************/
499 +#include <linux/ioctl.h>
501 +#define VC_MEM_IOC_MAGIC 'v'
503 +#define VC_MEM_IOC_MEM_PHYS_ADDR _IOR( VC_MEM_IOC_MAGIC, 0, unsigned long )
504 +#define VC_MEM_IOC_MEM_SIZE _IOR( VC_MEM_IOC_MAGIC, 1, unsigned int )
505 +#define VC_MEM_IOC_MEM_BASE _IOR( VC_MEM_IOC_MAGIC, 2, unsigned int )
506 +#define VC_MEM_IOC_MEM_LOAD _IOR( VC_MEM_IOC_MAGIC, 3, unsigned int )
508 +#if defined( __KERNEL__ )
509 +#define VC_MEM_TO_ARM_ADDR_MASK 0x3FFFFFFF
511 +extern unsigned long mm_vc_mem_phys_addr;
512 +extern unsigned int mm_vc_mem_size;
513 +extern int vc_mem_get_current_size( void );
516 +#endif /* _VC_MEM_H */