bcm27xx: switch to kernel v6.1
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-5.15 / 950-0072-vc_mem-Add-vc_mem-driver-for-querying-firmware-memor.patch
1 From 47c1d1988df17bf39bbf3a188d78792eaa693068 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
5 memory addresses
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 Signed-off-by: popcornmix <popcornmix@gmail.com>
11
12 BCM270x: Move vc_mem
13
14 Make the vc_mem module available for ARCH_BCM2835 by moving it.
15
16 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
17
18 char: vc_mem: Fix up compat ioctls for 64bit kernel
19
20 compat_ioctl wasn't defined, so 32bit user/64bit kernel
21 always failed.
22 VC_MEM_IOC_MEM_PHYS_ADDR was defined with parameter size
23 unsigned long, so the ioctl cmd changes between sizes.
24
25 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
26
27 char: vc_mem: Fix all coding style issues.
28
29 Cleans up all checkpatch errors in vc_mem.c and vc_mem.h
30 No functional change to the code.
31
32 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
33
34 char: vc_mem: Delete dead code
35
36 There are no error exists once device_create has succeeded, and
37 therefore no need to call device_destroy from vc_mem_init.
38
39 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
40 ---
41 drivers/char/broadcom/Kconfig | 18 ++
42 drivers/char/broadcom/Makefile | 1 +
43 drivers/char/broadcom/vc_mem.c | 373 ++++++++++++++++++++++++++++++++
44 include/linux/broadcom/vc_mem.h | 39 ++++
45 4 files changed, 431 insertions(+)
46 create mode 100644 drivers/char/broadcom/Kconfig
47 create mode 100644 drivers/char/broadcom/Makefile
48 create mode 100644 drivers/char/broadcom/vc_mem.c
49 create mode 100644 include/linux/broadcom/vc_mem.h
50
51 --- /dev/null
52 +++ b/drivers/char/broadcom/Kconfig
53 @@ -0,0 +1,18 @@
54 +#
55 +# Broadcom char driver config
56 +#
57 +
58 +menuconfig BRCM_CHAR_DRIVERS
59 + bool "Broadcom Char Drivers"
60 + help
61 + Broadcom's char drivers
62 +
63 +if BRCM_CHAR_DRIVERS
64 +
65 +config BCM2708_VCMEM
66 + bool "Videocore Memory"
67 + default y
68 + help
69 + Helper for videocore memory access and total size allocation.
70 +
71 +endif
72 --- /dev/null
73 +++ b/drivers/char/broadcom/Makefile
74 @@ -0,0 +1 @@
75 +obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
76 --- /dev/null
77 +++ b/drivers/char/broadcom/vc_mem.c
78 @@ -0,0 +1,373 @@
79 +/*
80 + * Copyright 2010 - 2011 Broadcom Corporation. All rights reserved.
81 + *
82 + * Unless you and Broadcom execute a separate written software license
83 + * agreement governing use of this software, this software is licensed to you
84 + * under the terms of the GNU General Public License version 2, available at
85 + * http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
86 + *
87 + * Notwithstanding the above, under no circumstances may you combine this
88 + * software in any way with any other Broadcom software provided under a
89 + * license other than the GPL, without Broadcom's express prior written
90 + * consent.
91 + */
92 +
93 +#include <linux/kernel.h>
94 +#include <linux/module.h>
95 +#include <linux/fs.h>
96 +#include <linux/device.h>
97 +#include <linux/cdev.h>
98 +#include <linux/mm.h>
99 +#include <linux/slab.h>
100 +#include <linux/debugfs.h>
101 +#include <linux/uaccess.h>
102 +#include <linux/dma-mapping.h>
103 +#include <linux/broadcom/vc_mem.h>
104 +
105 +#define DRIVER_NAME "vc-mem"
106 +
107 +/* Device (/dev) related variables */
108 +static dev_t vc_mem_devnum;
109 +static struct class *vc_mem_class;
110 +static struct cdev vc_mem_cdev;
111 +static int vc_mem_inited;
112 +
113 +#ifdef CONFIG_DEBUG_FS
114 +static struct dentry *vc_mem_debugfs_entry;
115 +#endif
116 +
117 +/*
118 + * Videocore memory addresses and size
119 + *
120 + * Drivers that wish to know the videocore memory addresses and sizes should
121 + * use these variables instead of the MM_IO_BASE and MM_ADDR_IO defines in
122 + * headers. This allows the other drivers to not be tied down to a a certain
123 + * address/size at compile time.
124 + *
125 + * In the future, the goal is to have the videocore memory virtual address and
126 + * size be calculated at boot time rather than at compile time. The decision of
127 + * where the videocore memory resides and its size would be in the hands of the
128 + * bootloader (and/or kernel). When that happens, the values of these variables
129 + * would be calculated and assigned in the init function.
130 + */
131 +/* In the 2835 VC in mapped above ARM, but ARM has full access to VC space */
132 +unsigned long mm_vc_mem_phys_addr;
133 +EXPORT_SYMBOL(mm_vc_mem_phys_addr);
134 +unsigned int mm_vc_mem_size;
135 +EXPORT_SYMBOL(mm_vc_mem_size);
136 +unsigned int mm_vc_mem_base;
137 +EXPORT_SYMBOL(mm_vc_mem_base);
138 +
139 +static uint phys_addr;
140 +static uint mem_size;
141 +static uint mem_base;
142 +
143 +static int
144 +vc_mem_open(struct inode *inode, struct file *file)
145 +{
146 + (void)inode;
147 +
148 + pr_debug("%s: called file = 0x%p\n", __func__, file);
149 +
150 + return 0;
151 +}
152 +
153 +static int
154 +vc_mem_release(struct inode *inode, struct file *file)
155 +{
156 + (void)inode;
157 +
158 + pr_debug("%s: called file = 0x%p\n", __func__, file);
159 +
160 + return 0;
161 +}
162 +
163 +static void
164 +vc_mem_get_size(void)
165 +{
166 +}
167 +
168 +static void
169 +vc_mem_get_base(void)
170 +{
171 +}
172 +
173 +int
174 +vc_mem_get_current_size(void)
175 +{
176 + return mm_vc_mem_size;
177 +}
178 +EXPORT_SYMBOL_GPL(vc_mem_get_current_size);
179 +
180 +static long
181 +vc_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
182 +{
183 + int rc = 0;
184 +
185 + (void) cmd;
186 + (void) arg;
187 +
188 + pr_debug("%s: called file = 0x%p, cmd %08x\n", __func__, file, cmd);
189 +
190 + switch (cmd) {
191 + case VC_MEM_IOC_MEM_PHYS_ADDR:
192 + {
193 + pr_debug("%s: VC_MEM_IOC_MEM_PHYS_ADDR=0x%p\n",
194 + __func__, (void *)mm_vc_mem_phys_addr);
195 +
196 + if (copy_to_user((void *)arg, &mm_vc_mem_phys_addr,
197 + sizeof(mm_vc_mem_phys_addr))) {
198 + rc = -EFAULT;
199 + }
200 + break;
201 + }
202 + case VC_MEM_IOC_MEM_SIZE:
203 + {
204 + /* Get the videocore memory size first */
205 + vc_mem_get_size();
206 +
207 + pr_debug("%s: VC_MEM_IOC_MEM_SIZE=%x\n", __func__,
208 + mm_vc_mem_size);
209 +
210 + if (copy_to_user((void *)arg, &mm_vc_mem_size,
211 + sizeof(mm_vc_mem_size))) {
212 + rc = -EFAULT;
213 + }
214 + break;
215 + }
216 + case VC_MEM_IOC_MEM_BASE:
217 + {
218 + /* Get the videocore memory base */
219 + vc_mem_get_base();
220 +
221 + pr_debug("%s: VC_MEM_IOC_MEM_BASE=%x\n", __func__,
222 + mm_vc_mem_base);
223 +
224 + if (copy_to_user((void *)arg, &mm_vc_mem_base,
225 + sizeof(mm_vc_mem_base))) {
226 + rc = -EFAULT;
227 + }
228 + break;
229 + }
230 + case VC_MEM_IOC_MEM_LOAD:
231 + {
232 + /* Get the videocore memory base */
233 + vc_mem_get_base();
234 +
235 + pr_debug("%s: VC_MEM_IOC_MEM_LOAD=%x\n", __func__,
236 + mm_vc_mem_base);
237 +
238 + if (copy_to_user((void *)arg, &mm_vc_mem_base,
239 + sizeof(mm_vc_mem_base))) {
240 + rc = -EFAULT;
241 + }
242 + break;
243 + }
244 + default:
245 + {
246 + return -ENOTTY;
247 + }
248 + }
249 + pr_debug("%s: file = 0x%p returning %d\n", __func__, file, rc);
250 +
251 + return rc;
252 +}
253 +
254 +#ifdef CONFIG_COMPAT
255 +static long
256 +vc_mem_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
257 +{
258 + int rc = 0;
259 +
260 + switch (cmd) {
261 + case VC_MEM_IOC_MEM_PHYS_ADDR32:
262 + pr_debug("%s: VC_MEM_IOC_MEM_PHYS_ADDR32=0x%p\n",
263 + __func__, (void *)mm_vc_mem_phys_addr);
264 +
265 + /* This isn't correct, but will cover us for now as
266 + * VideoCore is 32bit only.
267 + */
268 + if (copy_to_user((void *)arg, &mm_vc_mem_phys_addr,
269 + sizeof(compat_ulong_t)))
270 + rc = -EFAULT;
271 +
272 + break;
273 +
274 + default:
275 + rc = vc_mem_ioctl(file, cmd, arg);
276 + break;
277 + }
278 +
279 + return rc;
280 +}
281 +#endif
282 +
283 +static int
284 +vc_mem_mmap(struct file *filp, struct vm_area_struct *vma)
285 +{
286 + int rc = 0;
287 + unsigned long length = vma->vm_end - vma->vm_start;
288 + unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
289 +
290 + pr_debug("%s: vm_start = 0x%08lx vm_end = 0x%08lx vm_pgoff = 0x%08lx\n",
291 + __func__, (long)vma->vm_start, (long)vma->vm_end,
292 + (long)vma->vm_pgoff);
293 +
294 + if (offset + length > mm_vc_mem_size) {
295 + pr_err("%s: length %ld is too big\n", __func__, length);
296 + return -EINVAL;
297 + }
298 + /* Do not cache the memory map */
299 + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
300 +
301 + rc = remap_pfn_range(vma, vma->vm_start,
302 + (mm_vc_mem_phys_addr >> PAGE_SHIFT) +
303 + vma->vm_pgoff, length, vma->vm_page_prot);
304 + if (rc)
305 + pr_err("%s: remap_pfn_range failed (rc=%d)\n", __func__, rc);
306 +
307 + return rc;
308 +}
309 +
310 +/* File Operations for the driver. */
311 +static const struct file_operations vc_mem_fops = {
312 + .owner = THIS_MODULE,
313 + .open = vc_mem_open,
314 + .release = vc_mem_release,
315 + .unlocked_ioctl = vc_mem_ioctl,
316 +#ifdef CONFIG_COMPAT
317 + .compat_ioctl = vc_mem_compat_ioctl,
318 +#endif
319 + .mmap = vc_mem_mmap,
320 +};
321 +
322 +#ifdef CONFIG_DEBUG_FS
323 +static void vc_mem_debugfs_deinit(void)
324 +{
325 + debugfs_remove_recursive(vc_mem_debugfs_entry);
326 + vc_mem_debugfs_entry = NULL;
327 +}
328 +
329 +
330 +static int vc_mem_debugfs_init(
331 + struct device *dev)
332 +{
333 + vc_mem_debugfs_entry = debugfs_create_dir(DRIVER_NAME, NULL);
334 + if (!vc_mem_debugfs_entry) {
335 + dev_warn(dev, "could not create debugfs entry\n");
336 + return -EFAULT;
337 + }
338 +
339 + debugfs_create_x32("vc_mem_phys_addr",
340 + 0444,
341 + vc_mem_debugfs_entry,
342 + (u32 *)&mm_vc_mem_phys_addr);
343 + debugfs_create_x32("vc_mem_size",
344 + 0444,
345 + vc_mem_debugfs_entry,
346 + (u32 *)&mm_vc_mem_size);
347 + debugfs_create_x32("vc_mem_base",
348 + 0444,
349 + vc_mem_debugfs_entry,
350 + (u32 *)&mm_vc_mem_base);
351 +
352 + return 0;
353 +}
354 +
355 +#endif /* CONFIG_DEBUG_FS */
356 +
357 +/* Module load/unload functions */
358 +
359 +static int __init
360 +vc_mem_init(void)
361 +{
362 + int rc = -EFAULT;
363 + struct device *dev;
364 +
365 + pr_debug("%s: called\n", __func__);
366 +
367 + mm_vc_mem_phys_addr = phys_addr;
368 + mm_vc_mem_size = mem_size;
369 + mm_vc_mem_base = mem_base;
370 +
371 + vc_mem_get_size();
372 +
373 + pr_info("vc-mem: phys_addr:0x%08lx mem_base=0x%08x mem_size:0x%08x(%u MiB)\n",
374 + mm_vc_mem_phys_addr, mm_vc_mem_base, mm_vc_mem_size,
375 + mm_vc_mem_size / (1024 * 1024));
376 +
377 + rc = alloc_chrdev_region(&vc_mem_devnum, 0, 1, DRIVER_NAME);
378 + if (rc < 0) {
379 + pr_err("%s: alloc_chrdev_region failed (rc=%d)\n",
380 + __func__, rc);
381 + goto out_err;
382 + }
383 +
384 + cdev_init(&vc_mem_cdev, &vc_mem_fops);
385 + rc = cdev_add(&vc_mem_cdev, vc_mem_devnum, 1);
386 + if (rc) {
387 + pr_err("%s: cdev_add failed (rc=%d)\n", __func__, rc);
388 + goto out_unregister;
389 + }
390 +
391 + vc_mem_class = class_create(THIS_MODULE, DRIVER_NAME);
392 + if (IS_ERR(vc_mem_class)) {
393 + rc = PTR_ERR(vc_mem_class);
394 + pr_err("%s: class_create failed (rc=%d)\n", __func__, rc);
395 + goto out_cdev_del;
396 + }
397 +
398 + dev = device_create(vc_mem_class, NULL, vc_mem_devnum, NULL,
399 + DRIVER_NAME);
400 + if (IS_ERR(dev)) {
401 + rc = PTR_ERR(dev);
402 + pr_err("%s: device_create failed (rc=%d)\n", __func__, rc);
403 + goto out_class_destroy;
404 + }
405 +
406 +#ifdef CONFIG_DEBUG_FS
407 + /* don't fail if the debug entries cannot be created */
408 + vc_mem_debugfs_init(dev);
409 +#endif
410 +
411 + vc_mem_inited = 1;
412 + return 0;
413 +
414 +out_class_destroy:
415 + class_destroy(vc_mem_class);
416 + vc_mem_class = NULL;
417 +
418 +out_cdev_del:
419 + cdev_del(&vc_mem_cdev);
420 +
421 +out_unregister:
422 + unregister_chrdev_region(vc_mem_devnum, 1);
423 +
424 +out_err:
425 + return -1;
426 +}
427 +
428 +static void __exit
429 +vc_mem_exit(void)
430 +{
431 + pr_debug("%s: called\n", __func__);
432 +
433 + if (vc_mem_inited) {
434 +#if CONFIG_DEBUG_FS
435 + vc_mem_debugfs_deinit();
436 +#endif
437 + device_destroy(vc_mem_class, vc_mem_devnum);
438 + class_destroy(vc_mem_class);
439 + cdev_del(&vc_mem_cdev);
440 + unregister_chrdev_region(vc_mem_devnum, 1);
441 + }
442 +}
443 +
444 +module_init(vc_mem_init);
445 +module_exit(vc_mem_exit);
446 +MODULE_LICENSE("GPL");
447 +MODULE_AUTHOR("Broadcom Corporation");
448 +
449 +module_param(phys_addr, uint, 0644);
450 +module_param(mem_size, uint, 0644);
451 +module_param(mem_base, uint, 0644);
452 --- /dev/null
453 +++ b/include/linux/broadcom/vc_mem.h
454 @@ -0,0 +1,39 @@
455 +/*
456 + * Copyright 2010 - 2011 Broadcom Corporation. All rights reserved.
457 + *
458 + * Unless you and Broadcom execute a separate written software license
459 + * agreement governing use of this software, this software is licensed to you
460 + * under the terms of the GNU General Public License version 2, available at
461 + * http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
462 + *
463 + * Notwithstanding the above, under no circumstances may you combine this
464 + * software in any way with any other Broadcom software provided under a
465 + * license other than the GPL, without Broadcom's express prior written
466 + * consent.
467 + */
468 +
469 +#ifndef _VC_MEM_H
470 +#define _VC_MEM_H
471 +
472 +#include <linux/ioctl.h>
473 +
474 +#define VC_MEM_IOC_MAGIC 'v'
475 +
476 +#define VC_MEM_IOC_MEM_PHYS_ADDR _IOR(VC_MEM_IOC_MAGIC, 0, unsigned long)
477 +#define VC_MEM_IOC_MEM_SIZE _IOR(VC_MEM_IOC_MAGIC, 1, unsigned int)
478 +#define VC_MEM_IOC_MEM_BASE _IOR(VC_MEM_IOC_MAGIC, 2, unsigned int)
479 +#define VC_MEM_IOC_MEM_LOAD _IOR(VC_MEM_IOC_MAGIC, 3, unsigned int)
480 +
481 +#ifdef __KERNEL__
482 +#define VC_MEM_TO_ARM_ADDR_MASK 0x3FFFFFFF
483 +
484 +extern unsigned long mm_vc_mem_phys_addr;
485 +extern unsigned int mm_vc_mem_size;
486 +extern int vc_mem_get_current_size(void);
487 +#endif
488 +
489 +#ifdef CONFIG_COMPAT
490 +#define VC_MEM_IOC_MEM_PHYS_ADDR32 _IOR(VC_MEM_IOC_MAGIC, 0, compat_ulong_t)
491 +#endif
492 +
493 +#endif /* _VC_MEM_H */