brcm2708: add linux 4.1 support
[openwrt.git] / target / linux / brcm2708 / patches-4.1 / 0013-vc_mem-Add-vc_mem-driver.patch
1 From 8909329c134abdf242dc345a4528e98e86c85c33 Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Wed, 17 Jun 2015 16:07:06 +0100
4 Subject: [PATCH 013/121] vc_mem: Add vc_mem driver
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Signed-off-by: popcornmix <popcornmix@gmail.com>
10
11 BCM270x: Move vc_mem
12
13 Make the vc_mem module available for ARCH_BCM2835 by moving it.
14
15 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
16 ---
17  arch/arm/mach-bcm2709/include/mach/vc_mem.h |  35 ---
18  arch/arm/mach-bcm2709/vc_mem.c              | 431 ----------------------------
19  drivers/char/broadcom/Kconfig               |  12 +-
20  drivers/char/broadcom/Makefile              |   1 +
21  drivers/char/broadcom/vc_mem.c              | 423 +++++++++++++++++++++++++++
22  include/linux/broadcom/vc_mem.h             |  35 +++
23  6 files changed, 470 insertions(+), 467 deletions(-)
24  delete mode 100644 arch/arm/mach-bcm2709/include/mach/vc_mem.h
25  delete mode 100644 arch/arm/mach-bcm2709/vc_mem.c
26  create mode 100644 drivers/char/broadcom/vc_mem.c
27  create mode 100644 include/linux/broadcom/vc_mem.h
28
29 --- a/arch/arm/mach-bcm2709/include/mach/vc_mem.h
30 +++ /dev/null
31 @@ -1,35 +0,0 @@
32 -/*****************************************************************************
33 -* Copyright 2010 - 2011 Broadcom Corporation.  All rights reserved.
34 -*
35 -* Unless you and Broadcom execute a separate written software license
36 -* agreement governing use of this software, this software is licensed to you
37 -* under the terms of the GNU General Public License version 2, available at
38 -* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
39 -*
40 -* Notwithstanding the above, under no circumstances may you combine this
41 -* software in any way with any other Broadcom software provided under a
42 -* license other than the GPL, without Broadcom's express prior written
43 -* consent.
44 -*****************************************************************************/
45 -
46 -#if !defined( VC_MEM_H )
47 -#define VC_MEM_H
48 -
49 -#include <linux/ioctl.h>
50 -
51 -#define VC_MEM_IOC_MAGIC  'v'
52 -
53 -#define VC_MEM_IOC_MEM_PHYS_ADDR    _IOR( VC_MEM_IOC_MAGIC, 0, unsigned long )
54 -#define VC_MEM_IOC_MEM_SIZE         _IOR( VC_MEM_IOC_MAGIC, 1, unsigned int )
55 -#define VC_MEM_IOC_MEM_BASE         _IOR( VC_MEM_IOC_MAGIC, 2, unsigned int )
56 -#define VC_MEM_IOC_MEM_LOAD         _IOR( VC_MEM_IOC_MAGIC, 3, unsigned int )
57 -
58 -#if defined( __KERNEL__ )
59 -#define VC_MEM_TO_ARM_ADDR_MASK 0x3FFFFFFF
60 -
61 -extern unsigned long mm_vc_mem_phys_addr;
62 -extern unsigned int  mm_vc_mem_size;
63 -extern int vc_mem_get_current_size( void );
64 -#endif
65 -
66 -#endif  /* VC_MEM_H */
67 --- a/arch/arm/mach-bcm2709/vc_mem.c
68 +++ /dev/null
69 @@ -1,431 +0,0 @@
70 -/*****************************************************************************
71 -* Copyright 2010 - 2011 Broadcom Corporation.  All rights reserved.
72 -*
73 -* Unless you and Broadcom execute a separate written software license
74 -* agreement governing use of this software, this software is licensed to you
75 -* under the terms of the GNU General Public License version 2, available at
76 -* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
77 -*
78 -* Notwithstanding the above, under no circumstances may you combine this
79 -* software in any way with any other Broadcom software provided under a
80 -* license other than the GPL, without Broadcom's express prior written
81 -* consent.
82 -*****************************************************************************/
83 -
84 -#include <linux/kernel.h>
85 -#include <linux/module.h>
86 -#include <linux/fs.h>
87 -#include <linux/device.h>
88 -#include <linux/cdev.h>
89 -#include <linux/mm.h>
90 -#include <linux/slab.h>
91 -#include <linux/debugfs.h>
92 -#include <asm/uaccess.h>
93 -#include <linux/dma-mapping.h>
94 -#include <linux/platform_data/mailbox-bcm2708.h>
95 -
96 -#ifdef CONFIG_ARCH_KONA
97 -#include <chal/chal_ipc.h>
98 -#elif defined(CONFIG_ARCH_BCM2708) || defined(CONFIG_ARCH_BCM2709)
99 -#else
100 -#include <csp/chal_ipc.h>
101 -#endif
102 -
103 -#include "mach/vc_mem.h"
104 -
105 -#define DRIVER_NAME  "vc-mem"
106 -
107 -// Device (/dev) related variables
108 -static dev_t vc_mem_devnum = 0;
109 -static struct class *vc_mem_class = NULL;
110 -static struct cdev vc_mem_cdev;
111 -static int vc_mem_inited = 0;
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 = 0x00000000;
133 -unsigned int mm_vc_mem_size = 0;
134 -unsigned int mm_vc_mem_base = 0;
135 -
136 -EXPORT_SYMBOL(mm_vc_mem_phys_addr);
137 -EXPORT_SYMBOL(mm_vc_mem_size);
138 -EXPORT_SYMBOL(mm_vc_mem_base);
139 -
140 -static uint phys_addr = 0;
141 -static uint mem_size = 0;
142 -static uint mem_base = 0;
143 -
144 -
145 -/****************************************************************************
146 -*
147 -*   vc_mem_open
148 -*
149 -***************************************************************************/
150 -
151 -static int
152 -vc_mem_open(struct inode *inode, struct file *file)
153 -{
154 -       (void) inode;
155 -       (void) file;
156 -
157 -       pr_debug("%s: called file = 0x%p\n", __func__, file);
158 -
159 -       return 0;
160 -}
161 -
162 -/****************************************************************************
163 -*
164 -*   vc_mem_release
165 -*
166 -***************************************************************************/
167 -
168 -static int
169 -vc_mem_release(struct inode *inode, struct file *file)
170 -{
171 -       (void) inode;
172 -       (void) file;
173 -
174 -       pr_debug("%s: called file = 0x%p\n", __func__, file);
175 -
176 -       return 0;
177 -}
178 -
179 -/****************************************************************************
180 -*
181 -*   vc_mem_get_size
182 -*
183 -***************************************************************************/
184 -
185 -static void
186 -vc_mem_get_size(void)
187 -{
188 -}
189 -
190 -/****************************************************************************
191 -*
192 -*   vc_mem_get_base
193 -*
194 -***************************************************************************/
195 -
196 -static void
197 -vc_mem_get_base(void)
198 -{
199 -}
200 -
201 -/****************************************************************************
202 -*
203 -*   vc_mem_get_current_size
204 -*
205 -***************************************************************************/
206 -
207 -int
208 -vc_mem_get_current_size(void)
209 -{
210 -       return mm_vc_mem_size;
211 -}
212 -
213 -EXPORT_SYMBOL_GPL(vc_mem_get_current_size);
214 -
215 -/****************************************************************************
216 -*
217 -*   vc_mem_ioctl
218 -*
219 -***************************************************************************/
220 -
221 -static long
222 -vc_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
223 -{
224 -       int rc = 0;
225 -
226 -       (void) cmd;
227 -       (void) arg;
228 -
229 -       pr_debug("%s: called file = 0x%p\n", __func__, file);
230 -
231 -       switch (cmd) {
232 -       case VC_MEM_IOC_MEM_PHYS_ADDR:
233 -               {
234 -                       pr_debug("%s: VC_MEM_IOC_MEM_PHYS_ADDR=0x%p\n",
235 -                               __func__, (void *) mm_vc_mem_phys_addr);
236 -
237 -                       if (copy_to_user((void *) arg, &mm_vc_mem_phys_addr,
238 -                                        sizeof (mm_vc_mem_phys_addr)) != 0) {
239 -                               rc = -EFAULT;
240 -                       }
241 -                       break;
242 -               }
243 -       case VC_MEM_IOC_MEM_SIZE:
244 -               {
245 -                       // Get the videocore memory size first
246 -                       vc_mem_get_size();
247 -
248 -                       pr_debug("%s: VC_MEM_IOC_MEM_SIZE=%u\n", __func__,
249 -                               mm_vc_mem_size);
250 -
251 -                       if (copy_to_user((void *) arg, &mm_vc_mem_size,
252 -                                        sizeof (mm_vc_mem_size)) != 0) {
253 -                               rc = -EFAULT;
254 -                       }
255 -                       break;
256 -               }
257 -       case VC_MEM_IOC_MEM_BASE:
258 -               {
259 -                       // Get the videocore memory base
260 -                       vc_mem_get_base();
261 -
262 -                       pr_debug("%s: VC_MEM_IOC_MEM_BASE=%u\n", __func__,
263 -                               mm_vc_mem_base);
264 -
265 -                       if (copy_to_user((void *) arg, &mm_vc_mem_base,
266 -                                        sizeof (mm_vc_mem_base)) != 0) {
267 -                               rc = -EFAULT;
268 -                       }
269 -                       break;
270 -               }
271 -       case VC_MEM_IOC_MEM_LOAD:
272 -               {
273 -                       // Get the videocore memory base
274 -                       vc_mem_get_base();
275 -
276 -                       pr_debug("%s: VC_MEM_IOC_MEM_LOAD=%u\n", __func__,
277 -                               mm_vc_mem_base);
278 -
279 -                       if (copy_to_user((void *) arg, &mm_vc_mem_base,
280 -                                        sizeof (mm_vc_mem_base)) != 0) {
281 -                               rc = -EFAULT;
282 -                       }
283 -                       break;
284 -               }
285 -       default:
286 -               {
287 -                       return -ENOTTY;
288 -               }
289 -       }
290 -       pr_debug("%s: file = 0x%p returning %d\n", __func__, file, rc);
291 -
292 -       return rc;
293 -}
294 -
295 -/****************************************************************************
296 -*
297 -*   vc_mem_mmap
298 -*
299 -***************************************************************************/
300 -
301 -static int
302 -vc_mem_mmap(struct file *filp, struct vm_area_struct *vma)
303 -{
304 -       int rc = 0;
305 -       unsigned long length = vma->vm_end - vma->vm_start;
306 -       unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
307 -
308 -       pr_debug("%s: vm_start = 0x%08lx vm_end = 0x%08lx vm_pgoff = 0x%08lx\n",
309 -               __func__, (long) vma->vm_start, (long) vma->vm_end,
310 -               (long) vma->vm_pgoff);
311 -
312 -       if (offset + length > mm_vc_mem_size) {
313 -               pr_err("%s: length %ld is too big\n", __func__, length);
314 -               return -EINVAL;
315 -       }
316 -       // Do not cache the memory map
317 -       vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
318 -
319 -       rc = remap_pfn_range(vma, vma->vm_start,
320 -                            (mm_vc_mem_phys_addr >> PAGE_SHIFT) +
321 -                            vma->vm_pgoff, length, vma->vm_page_prot);
322 -       if (rc != 0) {
323 -               pr_err("%s: remap_pfn_range failed (rc=%d)\n", __func__, rc);
324 -       }
325 -
326 -       return rc;
327 -}
328 -
329 -/****************************************************************************
330 -*
331 -*   File Operations for the driver.
332 -*
333 -***************************************************************************/
334 -
335 -static const struct file_operations vc_mem_fops = {
336 -       .owner = THIS_MODULE,
337 -       .open = vc_mem_open,
338 -       .release = vc_mem_release,
339 -       .unlocked_ioctl = vc_mem_ioctl,
340 -       .mmap = vc_mem_mmap,
341 -};
342 -
343 -#ifdef CONFIG_DEBUG_FS
344 -static void vc_mem_debugfs_deinit(void)
345 -{
346 -       debugfs_remove_recursive(vc_mem_debugfs_entry);
347 -       vc_mem_debugfs_entry = NULL;
348 -}
349 -
350 -
351 -static int vc_mem_debugfs_init(
352 -       struct device *dev)
353 -{
354 -       vc_mem_debugfs_entry = debugfs_create_dir(DRIVER_NAME, NULL);
355 -       if (!vc_mem_debugfs_entry) {
356 -               dev_warn(dev, "could not create debugfs entry\n");
357 -               return -EFAULT;
358 -       }
359 -
360 -       if (!debugfs_create_x32("vc_mem_phys_addr",
361 -                               0444,
362 -                               vc_mem_debugfs_entry,
363 -                               (u32 *)&mm_vc_mem_phys_addr)) {
364 -               dev_warn(dev, "%s:could not create vc_mem_phys entry\n",
365 -                       __func__);
366 -               goto fail;
367 -       }
368 -
369 -       if (!debugfs_create_x32("vc_mem_size",
370 -                               0444,
371 -                               vc_mem_debugfs_entry,
372 -                               (u32 *)&mm_vc_mem_size)) {
373 -               dev_warn(dev, "%s:could not create vc_mem_size entry\n",
374 -                       __func__);
375 -               goto fail;
376 -       }
377 -
378 -       if (!debugfs_create_x32("vc_mem_base",
379 -                               0444,
380 -                               vc_mem_debugfs_entry,
381 -                               (u32 *)&mm_vc_mem_base)) {
382 -               dev_warn(dev, "%s:could not create vc_mem_base entry\n",
383 -                        __func__);
384 -               goto fail;
385 -       }
386 -
387 -       return 0;
388 -
389 -fail:
390 -       vc_mem_debugfs_deinit();
391 -       return -EFAULT;
392 -}
393 -
394 -#endif /* CONFIG_DEBUG_FS */
395 -
396 -
397 -/****************************************************************************
398 -*
399 -*   vc_mem_init
400 -*
401 -***************************************************************************/
402 -
403 -static int __init
404 -vc_mem_init(void)
405 -{
406 -       int rc = -EFAULT;
407 -       struct device *dev;
408 -
409 -       pr_debug("%s: called\n", __func__);
410 -
411 -       mm_vc_mem_phys_addr = phys_addr;
412 -       mm_vc_mem_size = mem_size;
413 -       mm_vc_mem_base = mem_base;
414 -
415 -       vc_mem_get_size();
416 -
417 -       pr_info("vc-mem: phys_addr:0x%08lx mem_base=0x%08x mem_size:0x%08x(%u MiB)\n",
418 -               mm_vc_mem_phys_addr, mm_vc_mem_base, mm_vc_mem_size, mm_vc_mem_size / (1024 * 1024));
419 -
420 -       if ((rc = alloc_chrdev_region(&vc_mem_devnum, 0, 1, DRIVER_NAME)) < 0) {
421 -               pr_err("%s: alloc_chrdev_region failed (rc=%d)\n",
422 -                      __func__, rc);
423 -               goto out_err;
424 -       }
425 -
426 -       cdev_init(&vc_mem_cdev, &vc_mem_fops);
427 -       if ((rc = cdev_add(&vc_mem_cdev, vc_mem_devnum, 1)) != 0) {
428 -               pr_err("%s: cdev_add failed (rc=%d)\n", __func__, rc);
429 -               goto out_unregister;
430 -       }
431 -
432 -       vc_mem_class = class_create(THIS_MODULE, DRIVER_NAME);
433 -       if (IS_ERR(vc_mem_class)) {
434 -               rc = PTR_ERR(vc_mem_class);
435 -               pr_err("%s: class_create failed (rc=%d)\n", __func__, rc);
436 -               goto out_cdev_del;
437 -       }
438 -
439 -       dev = device_create(vc_mem_class, NULL, vc_mem_devnum, NULL,
440 -                           DRIVER_NAME);
441 -       if (IS_ERR(dev)) {
442 -               rc = PTR_ERR(dev);
443 -               pr_err("%s: device_create failed (rc=%d)\n", __func__, rc);
444 -               goto out_class_destroy;
445 -       }
446 -
447 -#ifdef CONFIG_DEBUG_FS
448 -       /* don't fail if the debug entries cannot be created */
449 -       vc_mem_debugfs_init(dev);
450 -#endif
451 -
452 -       vc_mem_inited = 1;
453 -       return 0;
454 -
455 -       device_destroy(vc_mem_class, vc_mem_devnum);
456 -
457 -      out_class_destroy:
458 -       class_destroy(vc_mem_class);
459 -       vc_mem_class = NULL;
460 -
461 -      out_cdev_del:
462 -       cdev_del(&vc_mem_cdev);
463 -
464 -      out_unregister:
465 -       unregister_chrdev_region(vc_mem_devnum, 1);
466 -
467 -      out_err:
468 -       return -1;
469 -}
470 -
471 -/****************************************************************************
472 -*
473 -*   vc_mem_exit
474 -*
475 -***************************************************************************/
476 -
477 -static void __exit
478 -vc_mem_exit(void)
479 -{
480 -       pr_debug("%s: called\n", __func__);
481 -
482 -       if (vc_mem_inited) {
483 -#if CONFIG_DEBUG_FS
484 -               vc_mem_debugfs_deinit();
485 -#endif
486 -               device_destroy(vc_mem_class, vc_mem_devnum);
487 -               class_destroy(vc_mem_class);
488 -               cdev_del(&vc_mem_cdev);
489 -               unregister_chrdev_region(vc_mem_devnum, 1);
490 -       }
491 -}
492 -
493 -module_init(vc_mem_init);
494 -module_exit(vc_mem_exit);
495 -MODULE_LICENSE("GPL");
496 -MODULE_AUTHOR("Broadcom Corporation");
497 -
498 -module_param(phys_addr, uint, 0644);
499 -module_param(mem_size, uint, 0644);
500 -module_param(mem_base, uint, 0644);
501 --- a/drivers/char/broadcom/Kconfig
502 +++ b/drivers/char/broadcom/Kconfig
503 @@ -7,9 +7,19 @@ menuconfig BRCM_CHAR_DRIVERS
504         help
505           Broadcom's char drivers
506  
507 +if BRCM_CHAR_DRIVERS
508 +
509  config BCM_VC_CMA
510         bool "Videocore CMA"
511 -       depends on CMA && BRCM_CHAR_DRIVERS && BCM2708_VCHIQ
512 +       depends on CMA && BCM2708_VCHIQ
513         default n
514          help
515            Helper for videocore CMA access.
516 +
517 +config BCM2708_VCMEM
518 +       bool "Videocore Memory"
519 +        default y
520 +        help
521 +          Helper for videocore memory access and total size allocation.
522 +
523 +endif
524 --- a/drivers/char/broadcom/Makefile
525 +++ b/drivers/char/broadcom/Makefile
526 @@ -1 +1,2 @@
527  obj-$(CONFIG_BCM_VC_CMA)       += vc_cma/
528 +obj-$(CONFIG_BCM2708_VCMEM)    += vc_mem.o
529 --- /dev/null
530 +++ b/drivers/char/broadcom/vc_mem.c
531 @@ -0,0 +1,423 @@
532 +/*****************************************************************************
533 +* Copyright 2010 - 2011 Broadcom Corporation.  All rights reserved.
534 +*
535 +* Unless you and Broadcom execute a separate written software license
536 +* agreement governing use of this software, this software is licensed to you
537 +* under the terms of the GNU General Public License version 2, available at
538 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
539 +*
540 +* Notwithstanding the above, under no circumstances may you combine this
541 +* software in any way with any other Broadcom software provided under a
542 +* license other than the GPL, without Broadcom's express prior written
543 +* consent.
544 +*****************************************************************************/
545 +
546 +#include <linux/kernel.h>
547 +#include <linux/module.h>
548 +#include <linux/fs.h>
549 +#include <linux/device.h>
550 +#include <linux/cdev.h>
551 +#include <linux/mm.h>
552 +#include <linux/slab.h>
553 +#include <linux/debugfs.h>
554 +#include <asm/uaccess.h>
555 +#include <linux/dma-mapping.h>
556 +#include <linux/platform_data/mailbox-bcm2708.h>
557 +#include <linux/broadcom/vc_mem.h>
558 +
559 +#define DRIVER_NAME  "vc-mem"
560 +
561 +// Device (/dev) related variables
562 +static dev_t vc_mem_devnum = 0;
563 +static struct class *vc_mem_class = NULL;
564 +static struct cdev vc_mem_cdev;
565 +static int vc_mem_inited = 0;
566 +
567 +#ifdef CONFIG_DEBUG_FS
568 +static struct dentry *vc_mem_debugfs_entry;
569 +#endif
570 +
571 +/*
572 + * Videocore memory addresses and size
573 + *
574 + * Drivers that wish to know the videocore memory addresses and sizes should
575 + * use these variables instead of the MM_IO_BASE and MM_ADDR_IO defines in
576 + * headers. This allows the other drivers to not be tied down to a a certain
577 + * address/size at compile time.
578 + *
579 + * In the future, the goal is to have the videocore memory virtual address and
580 + * size be calculated at boot time rather than at compile time. The decision of
581 + * where the videocore memory resides and its size would be in the hands of the
582 + * bootloader (and/or kernel). When that happens, the values of these variables
583 + * would be calculated and assigned in the init function.
584 + */
585 +// in the 2835 VC in mapped above ARM, but ARM has full access to VC space
586 +unsigned long mm_vc_mem_phys_addr = 0x00000000;
587 +unsigned int mm_vc_mem_size = 0;
588 +unsigned int mm_vc_mem_base = 0;
589 +
590 +EXPORT_SYMBOL(mm_vc_mem_phys_addr);
591 +EXPORT_SYMBOL(mm_vc_mem_size);
592 +EXPORT_SYMBOL(mm_vc_mem_base);
593 +
594 +static uint phys_addr = 0;
595 +static uint mem_size = 0;
596 +static uint mem_base = 0;
597 +
598 +
599 +/****************************************************************************
600 +*
601 +*   vc_mem_open
602 +*
603 +***************************************************************************/
604 +
605 +static int
606 +vc_mem_open(struct inode *inode, struct file *file)
607 +{
608 +       (void) inode;
609 +       (void) file;
610 +
611 +       pr_debug("%s: called file = 0x%p\n", __func__, file);
612 +
613 +       return 0;
614 +}
615 +
616 +/****************************************************************************
617 +*
618 +*   vc_mem_release
619 +*
620 +***************************************************************************/
621 +
622 +static int
623 +vc_mem_release(struct inode *inode, struct file *file)
624 +{
625 +       (void) inode;
626 +       (void) file;
627 +
628 +       pr_debug("%s: called file = 0x%p\n", __func__, file);
629 +
630 +       return 0;
631 +}
632 +
633 +/****************************************************************************
634 +*
635 +*   vc_mem_get_size
636 +*
637 +***************************************************************************/
638 +
639 +static void
640 +vc_mem_get_size(void)
641 +{
642 +}
643 +
644 +/****************************************************************************
645 +*
646 +*   vc_mem_get_base
647 +*
648 +***************************************************************************/
649 +
650 +static void
651 +vc_mem_get_base(void)
652 +{
653 +}
654 +
655 +/****************************************************************************
656 +*
657 +*   vc_mem_get_current_size
658 +*
659 +***************************************************************************/
660 +
661 +int
662 +vc_mem_get_current_size(void)
663 +{
664 +       return mm_vc_mem_size;
665 +}
666 +
667 +EXPORT_SYMBOL_GPL(vc_mem_get_current_size);
668 +
669 +/****************************************************************************
670 +*
671 +*   vc_mem_ioctl
672 +*
673 +***************************************************************************/
674 +
675 +static long
676 +vc_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
677 +{
678 +       int rc = 0;
679 +
680 +       (void) cmd;
681 +       (void) arg;
682 +
683 +       pr_debug("%s: called file = 0x%p\n", __func__, file);
684 +
685 +       switch (cmd) {
686 +       case VC_MEM_IOC_MEM_PHYS_ADDR:
687 +               {
688 +                       pr_debug("%s: VC_MEM_IOC_MEM_PHYS_ADDR=0x%p\n",
689 +                               __func__, (void *) mm_vc_mem_phys_addr);
690 +
691 +                       if (copy_to_user((void *) arg, &mm_vc_mem_phys_addr,
692 +                                        sizeof (mm_vc_mem_phys_addr)) != 0) {
693 +                               rc = -EFAULT;
694 +                       }
695 +                       break;
696 +               }
697 +       case VC_MEM_IOC_MEM_SIZE:
698 +               {
699 +                       // Get the videocore memory size first
700 +                       vc_mem_get_size();
701 +
702 +                       pr_debug("%s: VC_MEM_IOC_MEM_SIZE=%u\n", __func__,
703 +                               mm_vc_mem_size);
704 +
705 +                       if (copy_to_user((void *) arg, &mm_vc_mem_size,
706 +                                        sizeof (mm_vc_mem_size)) != 0) {
707 +                               rc = -EFAULT;
708 +                       }
709 +                       break;
710 +               }
711 +       case VC_MEM_IOC_MEM_BASE:
712 +               {
713 +                       // Get the videocore memory base
714 +                       vc_mem_get_base();
715 +
716 +                       pr_debug("%s: VC_MEM_IOC_MEM_BASE=%u\n", __func__,
717 +                               mm_vc_mem_base);
718 +
719 +                       if (copy_to_user((void *) arg, &mm_vc_mem_base,
720 +                                        sizeof (mm_vc_mem_base)) != 0) {
721 +                               rc = -EFAULT;
722 +                       }
723 +                       break;
724 +               }
725 +       case VC_MEM_IOC_MEM_LOAD:
726 +               {
727 +                       // Get the videocore memory base
728 +                       vc_mem_get_base();
729 +
730 +                       pr_debug("%s: VC_MEM_IOC_MEM_LOAD=%u\n", __func__,
731 +                               mm_vc_mem_base);
732 +
733 +                       if (copy_to_user((void *) arg, &mm_vc_mem_base,
734 +                                        sizeof (mm_vc_mem_base)) != 0) {
735 +                               rc = -EFAULT;
736 +                       }
737 +                       break;
738 +               }
739 +       default:
740 +               {
741 +                       return -ENOTTY;
742 +               }
743 +       }
744 +       pr_debug("%s: file = 0x%p returning %d\n", __func__, file, rc);
745 +
746 +       return rc;
747 +}
748 +
749 +/****************************************************************************
750 +*
751 +*   vc_mem_mmap
752 +*
753 +***************************************************************************/
754 +
755 +static int
756 +vc_mem_mmap(struct file *filp, struct vm_area_struct *vma)
757 +{
758 +       int rc = 0;
759 +       unsigned long length = vma->vm_end - vma->vm_start;
760 +       unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
761 +
762 +       pr_debug("%s: vm_start = 0x%08lx vm_end = 0x%08lx vm_pgoff = 0x%08lx\n",
763 +               __func__, (long) vma->vm_start, (long) vma->vm_end,
764 +               (long) vma->vm_pgoff);
765 +
766 +       if (offset + length > mm_vc_mem_size) {
767 +               pr_err("%s: length %ld is too big\n", __func__, length);
768 +               return -EINVAL;
769 +       }
770 +       // Do not cache the memory map
771 +       vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
772 +
773 +       rc = remap_pfn_range(vma, vma->vm_start,
774 +                            (mm_vc_mem_phys_addr >> PAGE_SHIFT) +
775 +                            vma->vm_pgoff, length, vma->vm_page_prot);
776 +       if (rc != 0) {
777 +               pr_err("%s: remap_pfn_range failed (rc=%d)\n", __func__, rc);
778 +       }
779 +
780 +       return rc;
781 +}
782 +
783 +/****************************************************************************
784 +*
785 +*   File Operations for the driver.
786 +*
787 +***************************************************************************/
788 +
789 +static const struct file_operations vc_mem_fops = {
790 +       .owner = THIS_MODULE,
791 +       .open = vc_mem_open,
792 +       .release = vc_mem_release,
793 +       .unlocked_ioctl = vc_mem_ioctl,
794 +       .mmap = vc_mem_mmap,
795 +};
796 +
797 +#ifdef CONFIG_DEBUG_FS
798 +static void vc_mem_debugfs_deinit(void)
799 +{
800 +       debugfs_remove_recursive(vc_mem_debugfs_entry);
801 +       vc_mem_debugfs_entry = NULL;
802 +}
803 +
804 +
805 +static int vc_mem_debugfs_init(
806 +       struct device *dev)
807 +{
808 +       vc_mem_debugfs_entry = debugfs_create_dir(DRIVER_NAME, NULL);
809 +       if (!vc_mem_debugfs_entry) {
810 +               dev_warn(dev, "could not create debugfs entry\n");
811 +               return -EFAULT;
812 +       }
813 +
814 +       if (!debugfs_create_x32("vc_mem_phys_addr",
815 +                               0444,
816 +                               vc_mem_debugfs_entry,
817 +                               (u32 *)&mm_vc_mem_phys_addr)) {
818 +               dev_warn(dev, "%s:could not create vc_mem_phys entry\n",
819 +                       __func__);
820 +               goto fail;
821 +       }
822 +
823 +       if (!debugfs_create_x32("vc_mem_size",
824 +                               0444,
825 +                               vc_mem_debugfs_entry,
826 +                               (u32 *)&mm_vc_mem_size)) {
827 +               dev_warn(dev, "%s:could not create vc_mem_size entry\n",
828 +                       __func__);
829 +               goto fail;
830 +       }
831 +
832 +       if (!debugfs_create_x32("vc_mem_base",
833 +                               0444,
834 +                               vc_mem_debugfs_entry,
835 +                               (u32 *)&mm_vc_mem_base)) {
836 +               dev_warn(dev, "%s:could not create vc_mem_base entry\n",
837 +                        __func__);
838 +               goto fail;
839 +       }
840 +
841 +       return 0;
842 +
843 +fail:
844 +       vc_mem_debugfs_deinit();
845 +       return -EFAULT;
846 +}
847 +
848 +#endif /* CONFIG_DEBUG_FS */
849 +
850 +
851 +/****************************************************************************
852 +*
853 +*   vc_mem_init
854 +*
855 +***************************************************************************/
856 +
857 +static int __init
858 +vc_mem_init(void)
859 +{
860 +       int rc = -EFAULT;
861 +       struct device *dev;
862 +
863 +       pr_debug("%s: called\n", __func__);
864 +
865 +       mm_vc_mem_phys_addr = phys_addr;
866 +       mm_vc_mem_size = mem_size;
867 +       mm_vc_mem_base = mem_base;
868 +
869 +       vc_mem_get_size();
870 +
871 +       pr_info("vc-mem: phys_addr:0x%08lx mem_base=0x%08x mem_size:0x%08x(%u MiB)\n",
872 +               mm_vc_mem_phys_addr, mm_vc_mem_base, mm_vc_mem_size, mm_vc_mem_size / (1024 * 1024));
873 +
874 +       if ((rc = alloc_chrdev_region(&vc_mem_devnum, 0, 1, DRIVER_NAME)) < 0) {
875 +               pr_err("%s: alloc_chrdev_region failed (rc=%d)\n",
876 +                      __func__, rc);
877 +               goto out_err;
878 +       }
879 +
880 +       cdev_init(&vc_mem_cdev, &vc_mem_fops);
881 +       if ((rc = cdev_add(&vc_mem_cdev, vc_mem_devnum, 1)) != 0) {
882 +               pr_err("%s: cdev_add failed (rc=%d)\n", __func__, rc);
883 +               goto out_unregister;
884 +       }
885 +
886 +       vc_mem_class = class_create(THIS_MODULE, DRIVER_NAME);
887 +       if (IS_ERR(vc_mem_class)) {
888 +               rc = PTR_ERR(vc_mem_class);
889 +               pr_err("%s: class_create failed (rc=%d)\n", __func__, rc);
890 +               goto out_cdev_del;
891 +       }
892 +
893 +       dev = device_create(vc_mem_class, NULL, vc_mem_devnum, NULL,
894 +                           DRIVER_NAME);
895 +       if (IS_ERR(dev)) {
896 +               rc = PTR_ERR(dev);
897 +               pr_err("%s: device_create failed (rc=%d)\n", __func__, rc);
898 +               goto out_class_destroy;
899 +       }
900 +
901 +#ifdef CONFIG_DEBUG_FS
902 +       /* don't fail if the debug entries cannot be created */
903 +       vc_mem_debugfs_init(dev);
904 +#endif
905 +
906 +       vc_mem_inited = 1;
907 +       return 0;
908 +
909 +       device_destroy(vc_mem_class, vc_mem_devnum);
910 +
911 +      out_class_destroy:
912 +       class_destroy(vc_mem_class);
913 +       vc_mem_class = NULL;
914 +
915 +      out_cdev_del:
916 +       cdev_del(&vc_mem_cdev);
917 +
918 +      out_unregister:
919 +       unregister_chrdev_region(vc_mem_devnum, 1);
920 +
921 +      out_err:
922 +       return -1;
923 +}
924 +
925 +/****************************************************************************
926 +*
927 +*   vc_mem_exit
928 +*
929 +***************************************************************************/
930 +
931 +static void __exit
932 +vc_mem_exit(void)
933 +{
934 +       pr_debug("%s: called\n", __func__);
935 +
936 +       if (vc_mem_inited) {
937 +#if CONFIG_DEBUG_FS
938 +               vc_mem_debugfs_deinit();
939 +#endif
940 +               device_destroy(vc_mem_class, vc_mem_devnum);
941 +               class_destroy(vc_mem_class);
942 +               cdev_del(&vc_mem_cdev);
943 +               unregister_chrdev_region(vc_mem_devnum, 1);
944 +       }
945 +}
946 +
947 +module_init(vc_mem_init);
948 +module_exit(vc_mem_exit);
949 +MODULE_LICENSE("GPL");
950 +MODULE_AUTHOR("Broadcom Corporation");
951 +
952 +module_param(phys_addr, uint, 0644);
953 +module_param(mem_size, uint, 0644);
954 +module_param(mem_base, uint, 0644);
955 --- /dev/null
956 +++ b/include/linux/broadcom/vc_mem.h
957 @@ -0,0 +1,35 @@
958 +/*****************************************************************************
959 +* Copyright 2010 - 2011 Broadcom Corporation.  All rights reserved.
960 +*
961 +* Unless you and Broadcom execute a separate written software license
962 +* agreement governing use of this software, this software is licensed to you
963 +* under the terms of the GNU General Public License version 2, available at
964 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
965 +*
966 +* Notwithstanding the above, under no circumstances may you combine this
967 +* software in any way with any other Broadcom software provided under a
968 +* license other than the GPL, without Broadcom's express prior written
969 +* consent.
970 +*****************************************************************************/
971 +
972 +#ifndef _VC_MEM_H
973 +#define _VC_MEM_H
974 +
975 +#include <linux/ioctl.h>
976 +
977 +#define VC_MEM_IOC_MAGIC  'v'
978 +
979 +#define VC_MEM_IOC_MEM_PHYS_ADDR    _IOR( VC_MEM_IOC_MAGIC, 0, unsigned long )
980 +#define VC_MEM_IOC_MEM_SIZE         _IOR( VC_MEM_IOC_MAGIC, 1, unsigned int )
981 +#define VC_MEM_IOC_MEM_BASE         _IOR( VC_MEM_IOC_MAGIC, 2, unsigned int )
982 +#define VC_MEM_IOC_MEM_LOAD         _IOR( VC_MEM_IOC_MAGIC, 3, unsigned int )
983 +
984 +#if defined( __KERNEL__ )
985 +#define VC_MEM_TO_ARM_ADDR_MASK 0x3FFFFFFF
986 +
987 +extern unsigned long mm_vc_mem_phys_addr;
988 +extern unsigned int  mm_vc_mem_size;
989 +extern int vc_mem_get_current_size( void );
990 +#endif
991 +
992 +#endif  /* _VC_MEM_H */