6a22bdf97025020a318866d6043854bd28169104
[openwrt/staging/jogo.git] /
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
5 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 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
25
26 --- a/drivers/char/broadcom/Kconfig
27 +++ b/drivers/char/broadcom/Kconfig
28 @@ -7,9 +7,19 @@ menuconfig BRCM_CHAR_DRIVERS
29 help
30 Broadcom's char drivers
31
32 +if BRCM_CHAR_DRIVERS
33 +
34 config BCM_VC_CMA
35 bool "Videocore CMA"
36 - depends on CMA && BRCM_CHAR_DRIVERS && BCM2708_VCHIQ
37 + depends on CMA && BCM2708_VCHIQ
38 default n
39 help
40 Helper for videocore CMA access.
41 +
42 +config BCM2708_VCMEM
43 + bool "Videocore Memory"
44 + default y
45 + help
46 + Helper for videocore memory access and total size allocation.
47 +
48 +endif
49 --- a/drivers/char/broadcom/Makefile
50 +++ b/drivers/char/broadcom/Makefile
51 @@ -1 +1,2 @@
52 obj-$(CONFIG_BCM_VC_CMA) += vc_cma/
53 +obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
54 --- /dev/null
55 +++ b/drivers/char/broadcom/vc_mem.c
56 @@ -0,0 +1,422 @@
57 +/*****************************************************************************
58 +* Copyright 2010 - 2011 Broadcom Corporation. All rights reserved.
59 +*
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").
64 +*
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
68 +* consent.
69 +*****************************************************************************/
70 +
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>
82 +
83 +#define DRIVER_NAME "vc-mem"
84 +
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;
90 +
91 +#ifdef CONFIG_DEBUG_FS
92 +static struct dentry *vc_mem_debugfs_entry;
93 +#endif
94 +
95 +/*
96 + * Videocore memory addresses and size
97 + *
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.
102 + *
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.
108 + */
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;
113 +
114 +EXPORT_SYMBOL(mm_vc_mem_phys_addr);
115 +EXPORT_SYMBOL(mm_vc_mem_size);
116 +EXPORT_SYMBOL(mm_vc_mem_base);
117 +
118 +static uint phys_addr = 0;
119 +static uint mem_size = 0;
120 +static uint mem_base = 0;
121 +
122 +
123 +/****************************************************************************
124 +*
125 +* vc_mem_open
126 +*
127 +***************************************************************************/
128 +
129 +static int
130 +vc_mem_open(struct inode *inode, struct file *file)
131 +{
132 + (void) inode;
133 + (void) file;
134 +
135 + pr_debug("%s: called file = 0x%p\n", __func__, file);
136 +
137 + return 0;
138 +}
139 +
140 +/****************************************************************************
141 +*
142 +* vc_mem_release
143 +*
144 +***************************************************************************/
145 +
146 +static int
147 +vc_mem_release(struct inode *inode, struct file *file)
148 +{
149 + (void) inode;
150 + (void) file;
151 +
152 + pr_debug("%s: called file = 0x%p\n", __func__, file);
153 +
154 + return 0;
155 +}
156 +
157 +/****************************************************************************
158 +*
159 +* vc_mem_get_size
160 +*
161 +***************************************************************************/
162 +
163 +static void
164 +vc_mem_get_size(void)
165 +{
166 +}
167 +
168 +/****************************************************************************
169 +*
170 +* vc_mem_get_base
171 +*
172 +***************************************************************************/
173 +
174 +static void
175 +vc_mem_get_base(void)
176 +{
177 +}
178 +
179 +/****************************************************************************
180 +*
181 +* vc_mem_get_current_size
182 +*
183 +***************************************************************************/
184 +
185 +int
186 +vc_mem_get_current_size(void)
187 +{
188 + return mm_vc_mem_size;
189 +}
190 +
191 +EXPORT_SYMBOL_GPL(vc_mem_get_current_size);
192 +
193 +/****************************************************************************
194 +*
195 +* vc_mem_ioctl
196 +*
197 +***************************************************************************/
198 +
199 +static long
200 +vc_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
201 +{
202 + int rc = 0;
203 +
204 + (void) cmd;
205 + (void) arg;
206 +
207 + pr_debug("%s: called file = 0x%p\n", __func__, file);
208 +
209 + switch (cmd) {
210 + case VC_MEM_IOC_MEM_PHYS_ADDR:
211 + {
212 + pr_debug("%s: VC_MEM_IOC_MEM_PHYS_ADDR=0x%p\n",
213 + __func__, (void *) mm_vc_mem_phys_addr);
214 +
215 + if (copy_to_user((void *) arg, &mm_vc_mem_phys_addr,
216 + sizeof (mm_vc_mem_phys_addr)) != 0) {
217 + rc = -EFAULT;
218 + }
219 + break;
220 + }
221 + case VC_MEM_IOC_MEM_SIZE:
222 + {
223 + // Get the videocore memory size first
224 + vc_mem_get_size();
225 +
226 + pr_debug("%s: VC_MEM_IOC_MEM_SIZE=%u\n", __func__,
227 + mm_vc_mem_size);
228 +
229 + if (copy_to_user((void *) arg, &mm_vc_mem_size,
230 + sizeof (mm_vc_mem_size)) != 0) {
231 + rc = -EFAULT;
232 + }
233 + break;
234 + }
235 + case VC_MEM_IOC_MEM_BASE:
236 + {
237 + // Get the videocore memory base
238 + vc_mem_get_base();
239 +
240 + pr_debug("%s: VC_MEM_IOC_MEM_BASE=%u\n", __func__,
241 + mm_vc_mem_base);
242 +
243 + if (copy_to_user((void *) arg, &mm_vc_mem_base,
244 + sizeof (mm_vc_mem_base)) != 0) {
245 + rc = -EFAULT;
246 + }
247 + break;
248 + }
249 + case VC_MEM_IOC_MEM_LOAD:
250 + {
251 + // Get the videocore memory base
252 + vc_mem_get_base();
253 +
254 + pr_debug("%s: VC_MEM_IOC_MEM_LOAD=%u\n", __func__,
255 + mm_vc_mem_base);
256 +
257 + if (copy_to_user((void *) arg, &mm_vc_mem_base,
258 + sizeof (mm_vc_mem_base)) != 0) {
259 + rc = -EFAULT;
260 + }
261 + break;
262 + }
263 + default:
264 + {
265 + return -ENOTTY;
266 + }
267 + }
268 + pr_debug("%s: file = 0x%p returning %d\n", __func__, file, rc);
269 +
270 + return rc;
271 +}
272 +
273 +/****************************************************************************
274 +*
275 +* vc_mem_mmap
276 +*
277 +***************************************************************************/
278 +
279 +static int
280 +vc_mem_mmap(struct file *filp, struct vm_area_struct *vma)
281 +{
282 + int rc = 0;
283 + unsigned long length = vma->vm_end - vma->vm_start;
284 + unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
285 +
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);
289 +
290 + if (offset + length > mm_vc_mem_size) {
291 + pr_err("%s: length %ld is too big\n", __func__, length);
292 + return -EINVAL;
293 + }
294 + // Do not cache the memory map
295 + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
296 +
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);
300 + if (rc != 0) {
301 + pr_err("%s: remap_pfn_range failed (rc=%d)\n", __func__, rc);
302 + }
303 +
304 + return rc;
305 +}
306 +
307 +/****************************************************************************
308 +*
309 +* File Operations for the driver.
310 +*
311 +***************************************************************************/
312 +
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,
319 +};
320 +
321 +#ifdef CONFIG_DEBUG_FS
322 +static void vc_mem_debugfs_deinit(void)
323 +{
324 + debugfs_remove_recursive(vc_mem_debugfs_entry);
325 + vc_mem_debugfs_entry = NULL;
326 +}
327 +
328 +
329 +static int vc_mem_debugfs_init(
330 + struct device *dev)
331 +{
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");
335 + return -EFAULT;
336 + }
337 +
338 + if (!debugfs_create_x32("vc_mem_phys_addr",
339 + 0444,
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",
343 + __func__);
344 + goto fail;
345 + }
346 +
347 + if (!debugfs_create_x32("vc_mem_size",
348 + 0444,
349 + vc_mem_debugfs_entry,
350 + (u32 *)&mm_vc_mem_size)) {
351 + dev_warn(dev, "%s:could not create vc_mem_size entry\n",
352 + __func__);
353 + goto fail;
354 + }
355 +
356 + if (!debugfs_create_x32("vc_mem_base",
357 + 0444,
358 + vc_mem_debugfs_entry,
359 + (u32 *)&mm_vc_mem_base)) {
360 + dev_warn(dev, "%s:could not create vc_mem_base entry\n",
361 + __func__);
362 + goto fail;
363 + }
364 +
365 + return 0;
366 +
367 +fail:
368 + vc_mem_debugfs_deinit();
369 + return -EFAULT;
370 +}
371 +
372 +#endif /* CONFIG_DEBUG_FS */
373 +
374 +
375 +/****************************************************************************
376 +*
377 +* vc_mem_init
378 +*
379 +***************************************************************************/
380 +
381 +static int __init
382 +vc_mem_init(void)
383 +{
384 + int rc = -EFAULT;
385 + struct device *dev;
386 +
387 + pr_debug("%s: called\n", __func__);
388 +
389 + mm_vc_mem_phys_addr = phys_addr;
390 + mm_vc_mem_size = mem_size;
391 + mm_vc_mem_base = mem_base;
392 +
393 + vc_mem_get_size();
394 +
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));
397 +
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",
400 + __func__, rc);
401 + goto out_err;
402 + }
403 +
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;
408 + }
409 +
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);
414 + goto out_cdev_del;
415 + }
416 +
417 + dev = device_create(vc_mem_class, NULL, vc_mem_devnum, NULL,
418 + DRIVER_NAME);
419 + if (IS_ERR(dev)) {
420 + rc = PTR_ERR(dev);
421 + pr_err("%s: device_create failed (rc=%d)\n", __func__, rc);
422 + goto out_class_destroy;
423 + }
424 +
425 +#ifdef CONFIG_DEBUG_FS
426 + /* don't fail if the debug entries cannot be created */
427 + vc_mem_debugfs_init(dev);
428 +#endif
429 +
430 + vc_mem_inited = 1;
431 + return 0;
432 +
433 + device_destroy(vc_mem_class, vc_mem_devnum);
434 +
435 + out_class_destroy:
436 + class_destroy(vc_mem_class);
437 + vc_mem_class = NULL;
438 +
439 + out_cdev_del:
440 + cdev_del(&vc_mem_cdev);
441 +
442 + out_unregister:
443 + unregister_chrdev_region(vc_mem_devnum, 1);
444 +
445 + out_err:
446 + return -1;
447 +}
448 +
449 +/****************************************************************************
450 +*
451 +* vc_mem_exit
452 +*
453 +***************************************************************************/
454 +
455 +static void __exit
456 +vc_mem_exit(void)
457 +{
458 + pr_debug("%s: called\n", __func__);
459 +
460 + if (vc_mem_inited) {
461 +#if CONFIG_DEBUG_FS
462 + vc_mem_debugfs_deinit();
463 +#endif
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);
468 + }
469 +}
470 +
471 +module_init(vc_mem_init);
472 +module_exit(vc_mem_exit);
473 +MODULE_LICENSE("GPL");
474 +MODULE_AUTHOR("Broadcom Corporation");
475 +
476 +module_param(phys_addr, uint, 0644);
477 +module_param(mem_size, uint, 0644);
478 +module_param(mem_base, uint, 0644);
479 --- /dev/null
480 +++ b/include/linux/broadcom/vc_mem.h
481 @@ -0,0 +1,35 @@
482 +/*****************************************************************************
483 +* Copyright 2010 - 2011 Broadcom Corporation. All rights reserved.
484 +*
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").
489 +*
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
493 +* consent.
494 +*****************************************************************************/
495 +
496 +#ifndef _VC_MEM_H
497 +#define _VC_MEM_H
498 +
499 +#include <linux/ioctl.h>
500 +
501 +#define VC_MEM_IOC_MAGIC 'v'
502 +
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 )
507 +
508 +#if defined( __KERNEL__ )
509 +#define VC_MEM_TO_ARM_ADDR_MASK 0x3FFFFFFF
510 +
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 );
514 +#endif
515 +
516 +#endif /* _VC_MEM_H */