brcm2708: update linux 4.4 patches to latest version
[openwrt.git] / target / linux / brcm2708 / patches-4.4 / 0038-vc_mem-Add-vc_mem-driver.patch
1 From 2cabfc660c80dd79f24a8323b3913b5060ebf750 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 038/170] 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              | 422 +++++++++++++++++++++++++++
22  include/linux/broadcom/vc_mem.h             |  35 +++
23  6 files changed, 469 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,422 @@
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/broadcom/vc_mem.h>
557 +
558 +#define DRIVER_NAME  "vc-mem"
559 +
560 +// Device (/dev) related variables
561 +static dev_t vc_mem_devnum = 0;
562 +static struct class *vc_mem_class = NULL;
563 +static struct cdev vc_mem_cdev;
564 +static int vc_mem_inited = 0;
565 +
566 +#ifdef CONFIG_DEBUG_FS
567 +static struct dentry *vc_mem_debugfs_entry;
568 +#endif
569 +
570 +/*
571 + * Videocore memory addresses and size
572 + *
573 + * Drivers that wish to know the videocore memory addresses and sizes should
574 + * use these variables instead of the MM_IO_BASE and MM_ADDR_IO defines in
575 + * headers. This allows the other drivers to not be tied down to a a certain
576 + * address/size at compile time.
577 + *
578 + * In the future, the goal is to have the videocore memory virtual address and
579 + * size be calculated at boot time rather than at compile time. The decision of
580 + * where the videocore memory resides and its size would be in the hands of the
581 + * bootloader (and/or kernel). When that happens, the values of these variables
582 + * would be calculated and assigned in the init function.
583 + */
584 +// in the 2835 VC in mapped above ARM, but ARM has full access to VC space
585 +unsigned long mm_vc_mem_phys_addr = 0x00000000;
586 +unsigned int mm_vc_mem_size = 0;
587 +unsigned int mm_vc_mem_base = 0;
588 +
589 +EXPORT_SYMBOL(mm_vc_mem_phys_addr);
590 +EXPORT_SYMBOL(mm_vc_mem_size);
591 +EXPORT_SYMBOL(mm_vc_mem_base);
592 +
593 +static uint phys_addr = 0;
594 +static uint mem_size = 0;
595 +static uint mem_base = 0;
596 +
597 +
598 +/****************************************************************************
599 +*
600 +*   vc_mem_open
601 +*
602 +***************************************************************************/
603 +
604 +static int
605 +vc_mem_open(struct inode *inode, struct file *file)
606 +{
607 +       (void) inode;
608 +       (void) file;
609 +
610 +       pr_debug("%s: called file = 0x%p\n", __func__, file);
611 +
612 +       return 0;
613 +}
614 +
615 +/****************************************************************************
616 +*
617 +*   vc_mem_release
618 +*
619 +***************************************************************************/
620 +
621 +static int
622 +vc_mem_release(struct inode *inode, struct file *file)
623 +{
624 +       (void) inode;
625 +       (void) file;
626 +
627 +       pr_debug("%s: called file = 0x%p\n", __func__, file);
628 +
629 +       return 0;
630 +}
631 +
632 +/****************************************************************************
633 +*
634 +*   vc_mem_get_size
635 +*
636 +***************************************************************************/
637 +
638 +static void
639 +vc_mem_get_size(void)
640 +{
641 +}
642 +
643 +/****************************************************************************
644 +*
645 +*   vc_mem_get_base
646 +*
647 +***************************************************************************/
648 +
649 +static void
650 +vc_mem_get_base(void)
651 +{
652 +}
653 +
654 +/****************************************************************************
655 +*
656 +*   vc_mem_get_current_size
657 +*
658 +***************************************************************************/
659 +
660 +int
661 +vc_mem_get_current_size(void)
662 +{
663 +       return mm_vc_mem_size;
664 +}
665 +
666 +EXPORT_SYMBOL_GPL(vc_mem_get_current_size);
667 +
668 +/****************************************************************************
669 +*
670 +*   vc_mem_ioctl
671 +*
672 +***************************************************************************/
673 +
674 +static long
675 +vc_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
676 +{
677 +       int rc = 0;
678 +
679 +       (void) cmd;
680 +       (void) arg;
681 +
682 +       pr_debug("%s: called file = 0x%p\n", __func__, file);
683 +
684 +       switch (cmd) {
685 +       case VC_MEM_IOC_MEM_PHYS_ADDR:
686 +               {
687 +                       pr_debug("%s: VC_MEM_IOC_MEM_PHYS_ADDR=0x%p\n",
688 +                               __func__, (void *) mm_vc_mem_phys_addr);
689 +
690 +                       if (copy_to_user((void *) arg, &mm_vc_mem_phys_addr,
691 +                                        sizeof (mm_vc_mem_phys_addr)) != 0) {
692 +                               rc = -EFAULT;
693 +                       }
694 +                       break;
695 +               }
696 +       case VC_MEM_IOC_MEM_SIZE:
697 +               {
698 +                       // Get the videocore memory size first
699 +                       vc_mem_get_size();
700 +
701 +                       pr_debug("%s: VC_MEM_IOC_MEM_SIZE=%u\n", __func__,
702 +                               mm_vc_mem_size);
703 +
704 +                       if (copy_to_user((void *) arg, &mm_vc_mem_size,
705 +                                        sizeof (mm_vc_mem_size)) != 0) {
706 +                               rc = -EFAULT;
707 +                       }
708 +                       break;
709 +               }
710 +       case VC_MEM_IOC_MEM_BASE:
711 +               {
712 +                       // Get the videocore memory base
713 +                       vc_mem_get_base();
714 +
715 +                       pr_debug("%s: VC_MEM_IOC_MEM_BASE=%u\n", __func__,
716 +                               mm_vc_mem_base);
717 +
718 +                       if (copy_to_user((void *) arg, &mm_vc_mem_base,
719 +                                        sizeof (mm_vc_mem_base)) != 0) {
720 +                               rc = -EFAULT;
721 +                       }
722 +                       break;
723 +               }
724 +       case VC_MEM_IOC_MEM_LOAD:
725 +               {
726 +                       // Get the videocore memory base
727 +                       vc_mem_get_base();
728 +
729 +                       pr_debug("%s: VC_MEM_IOC_MEM_LOAD=%u\n", __func__,
730 +                               mm_vc_mem_base);
731 +
732 +                       if (copy_to_user((void *) arg, &mm_vc_mem_base,
733 +                                        sizeof (mm_vc_mem_base)) != 0) {
734 +                               rc = -EFAULT;
735 +                       }
736 +                       break;
737 +               }
738 +       default:
739 +               {
740 +                       return -ENOTTY;
741 +               }
742 +       }
743 +       pr_debug("%s: file = 0x%p returning %d\n", __func__, file, rc);
744 +
745 +       return rc;
746 +}
747 +
748 +/****************************************************************************
749 +*
750 +*   vc_mem_mmap
751 +*
752 +***************************************************************************/
753 +
754 +static int
755 +vc_mem_mmap(struct file *filp, struct vm_area_struct *vma)
756 +{
757 +       int rc = 0;
758 +       unsigned long length = vma->vm_end - vma->vm_start;
759 +       unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
760 +
761 +       pr_debug("%s: vm_start = 0x%08lx vm_end = 0x%08lx vm_pgoff = 0x%08lx\n",
762 +               __func__, (long) vma->vm_start, (long) vma->vm_end,
763 +               (long) vma->vm_pgoff);
764 +
765 +       if (offset + length > mm_vc_mem_size) {
766 +               pr_err("%s: length %ld is too big\n", __func__, length);
767 +               return -EINVAL;
768 +       }
769 +       // Do not cache the memory map
770 +       vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
771 +
772 +       rc = remap_pfn_range(vma, vma->vm_start,
773 +                            (mm_vc_mem_phys_addr >> PAGE_SHIFT) +
774 +                            vma->vm_pgoff, length, vma->vm_page_prot);
775 +       if (rc != 0) {
776 +               pr_err("%s: remap_pfn_range failed (rc=%d)\n", __func__, rc);
777 +       }
778 +
779 +       return rc;
780 +}
781 +
782 +/****************************************************************************
783 +*
784 +*   File Operations for the driver.
785 +*
786 +***************************************************************************/
787 +
788 +static const struct file_operations vc_mem_fops = {
789 +       .owner = THIS_MODULE,
790 +       .open = vc_mem_open,
791 +       .release = vc_mem_release,
792 +       .unlocked_ioctl = vc_mem_ioctl,
793 +       .mmap = vc_mem_mmap,
794 +};
795 +
796 +#ifdef CONFIG_DEBUG_FS
797 +static void vc_mem_debugfs_deinit(void)
798 +{
799 +       debugfs_remove_recursive(vc_mem_debugfs_entry);
800 +       vc_mem_debugfs_entry = NULL;
801 +}
802 +
803 +
804 +static int vc_mem_debugfs_init(
805 +       struct device *dev)
806 +{
807 +       vc_mem_debugfs_entry = debugfs_create_dir(DRIVER_NAME, NULL);
808 +       if (!vc_mem_debugfs_entry) {
809 +               dev_warn(dev, "could not create debugfs entry\n");
810 +               return -EFAULT;
811 +       }
812 +
813 +       if (!debugfs_create_x32("vc_mem_phys_addr",
814 +                               0444,
815 +                               vc_mem_debugfs_entry,
816 +                               (u32 *)&mm_vc_mem_phys_addr)) {
817 +               dev_warn(dev, "%s:could not create vc_mem_phys entry\n",
818 +                       __func__);
819 +               goto fail;
820 +       }
821 +
822 +       if (!debugfs_create_x32("vc_mem_size",
823 +                               0444,
824 +                               vc_mem_debugfs_entry,
825 +                               (u32 *)&mm_vc_mem_size)) {
826 +               dev_warn(dev, "%s:could not create vc_mem_size entry\n",
827 +                       __func__);
828 +               goto fail;
829 +       }
830 +
831 +       if (!debugfs_create_x32("vc_mem_base",
832 +                               0444,
833 +                               vc_mem_debugfs_entry,
834 +                               (u32 *)&mm_vc_mem_base)) {
835 +               dev_warn(dev, "%s:could not create vc_mem_base entry\n",
836 +                        __func__);
837 +               goto fail;
838 +       }
839 +
840 +       return 0;
841 +
842 +fail:
843 +       vc_mem_debugfs_deinit();
844 +       return -EFAULT;
845 +}
846 +
847 +#endif /* CONFIG_DEBUG_FS */
848 +
849 +
850 +/****************************************************************************
851 +*
852 +*   vc_mem_init
853 +*
854 +***************************************************************************/
855 +
856 +static int __init
857 +vc_mem_init(void)
858 +{
859 +       int rc = -EFAULT;
860 +       struct device *dev;
861 +
862 +       pr_debug("%s: called\n", __func__);
863 +
864 +       mm_vc_mem_phys_addr = phys_addr;
865 +       mm_vc_mem_size = mem_size;
866 +       mm_vc_mem_base = mem_base;
867 +
868 +       vc_mem_get_size();
869 +
870 +       pr_info("vc-mem: phys_addr:0x%08lx mem_base=0x%08x mem_size:0x%08x(%u MiB)\n",
871 +               mm_vc_mem_phys_addr, mm_vc_mem_base, mm_vc_mem_size, mm_vc_mem_size / (1024 * 1024));
872 +
873 +       if ((rc = alloc_chrdev_region(&vc_mem_devnum, 0, 1, DRIVER_NAME)) < 0) {
874 +               pr_err("%s: alloc_chrdev_region failed (rc=%d)\n",
875 +                      __func__, rc);
876 +               goto out_err;
877 +       }
878 +
879 +       cdev_init(&vc_mem_cdev, &vc_mem_fops);
880 +       if ((rc = cdev_add(&vc_mem_cdev, vc_mem_devnum, 1)) != 0) {
881 +               pr_err("%s: cdev_add failed (rc=%d)\n", __func__, rc);
882 +               goto out_unregister;
883 +       }
884 +
885 +       vc_mem_class = class_create(THIS_MODULE, DRIVER_NAME);
886 +       if (IS_ERR(vc_mem_class)) {
887 +               rc = PTR_ERR(vc_mem_class);
888 +               pr_err("%s: class_create failed (rc=%d)\n", __func__, rc);
889 +               goto out_cdev_del;
890 +       }
891 +
892 +       dev = device_create(vc_mem_class, NULL, vc_mem_devnum, NULL,
893 +                           DRIVER_NAME);
894 +       if (IS_ERR(dev)) {
895 +               rc = PTR_ERR(dev);
896 +               pr_err("%s: device_create failed (rc=%d)\n", __func__, rc);
897 +               goto out_class_destroy;
898 +       }
899 +
900 +#ifdef CONFIG_DEBUG_FS
901 +       /* don't fail if the debug entries cannot be created */
902 +       vc_mem_debugfs_init(dev);
903 +#endif
904 +
905 +       vc_mem_inited = 1;
906 +       return 0;
907 +
908 +       device_destroy(vc_mem_class, vc_mem_devnum);
909 +
910 +      out_class_destroy:
911 +       class_destroy(vc_mem_class);
912 +       vc_mem_class = NULL;
913 +
914 +      out_cdev_del:
915 +       cdev_del(&vc_mem_cdev);
916 +
917 +      out_unregister:
918 +       unregister_chrdev_region(vc_mem_devnum, 1);
919 +
920 +      out_err:
921 +       return -1;
922 +}
923 +
924 +/****************************************************************************
925 +*
926 +*   vc_mem_exit
927 +*
928 +***************************************************************************/
929 +
930 +static void __exit
931 +vc_mem_exit(void)
932 +{
933 +       pr_debug("%s: called\n", __func__);
934 +
935 +       if (vc_mem_inited) {
936 +#if CONFIG_DEBUG_FS
937 +               vc_mem_debugfs_deinit();
938 +#endif
939 +               device_destroy(vc_mem_class, vc_mem_devnum);
940 +               class_destroy(vc_mem_class);
941 +               cdev_del(&vc_mem_cdev);
942 +               unregister_chrdev_region(vc_mem_devnum, 1);
943 +       }
944 +}
945 +
946 +module_init(vc_mem_init);
947 +module_exit(vc_mem_exit);
948 +MODULE_LICENSE("GPL");
949 +MODULE_AUTHOR("Broadcom Corporation");
950 +
951 +module_param(phys_addr, uint, 0644);
952 +module_param(mem_size, uint, 0644);
953 +module_param(mem_base, uint, 0644);
954 --- /dev/null
955 +++ b/include/linux/broadcom/vc_mem.h
956 @@ -0,0 +1,35 @@
957 +/*****************************************************************************
958 +* Copyright 2010 - 2011 Broadcom Corporation.  All rights reserved.
959 +*
960 +* Unless you and Broadcom execute a separate written software license
961 +* agreement governing use of this software, this software is licensed to you
962 +* under the terms of the GNU General Public License version 2, available at
963 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
964 +*
965 +* Notwithstanding the above, under no circumstances may you combine this
966 +* software in any way with any other Broadcom software provided under a
967 +* license other than the GPL, without Broadcom's express prior written
968 +* consent.
969 +*****************************************************************************/
970 +
971 +#ifndef _VC_MEM_H
972 +#define _VC_MEM_H
973 +
974 +#include <linux/ioctl.h>
975 +
976 +#define VC_MEM_IOC_MAGIC  'v'
977 +
978 +#define VC_MEM_IOC_MEM_PHYS_ADDR    _IOR( VC_MEM_IOC_MAGIC, 0, unsigned long )
979 +#define VC_MEM_IOC_MEM_SIZE         _IOR( VC_MEM_IOC_MAGIC, 1, unsigned int )
980 +#define VC_MEM_IOC_MEM_BASE         _IOR( VC_MEM_IOC_MAGIC, 2, unsigned int )
981 +#define VC_MEM_IOC_MEM_LOAD         _IOR( VC_MEM_IOC_MAGIC, 3, unsigned int )
982 +
983 +#if defined( __KERNEL__ )
984 +#define VC_MEM_TO_ARM_ADDR_MASK 0x3FFFFFFF
985 +
986 +extern unsigned long mm_vc_mem_phys_addr;
987 +extern unsigned int  mm_vc_mem_size;
988 +extern int vc_mem_get_current_size( void );
989 +#endif
990 +
991 +#endif  /* _VC_MEM_H */