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