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