blob: 0c35dee0a2150b4ca284a03dd3637cad6eb77b86 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Steven J. Hill49bffbd2013-03-25 15:05:40 -05002 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * PROM library functions for acquiring/using memory descriptors given to
7 * us from the YAMON.
Steven J. Hill49bffbd2013-03-25 15:05:40 -05008 *
9 * Copyright (C) 1999,2000,2012 MIPS Technologies, Inc.
10 * All rights reserved.
11 * Authors: Carsten Langgaard <carstenl@mips.com>
12 * Steven J. Hill <sjhill@mips.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/bootmem.h>
Ralf Baechlee01402b2005-07-14 15:57:16 +000016#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <asm/bootinfo.h>
Paul Burton3a551e22014-07-14 12:37:39 +010019#include <asm/maar.h>
Ralf Baechle9c1f1252006-04-03 10:17:21 +010020#include <asm/sections.h>
Steven J. Hillb431f092013-03-25 14:47:05 -050021#include <asm/fw/fw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Steven J. Hillb431f092013-03-25 14:47:05 -050023static fw_memblock_t mdesc[FW_MAX_MEMBLOCKS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Ralf Baechle70342282013-01-22 12:59:30 +010025/* determined physical memory size, not overridden by command line args */
Ralf Baechlee1a4e462006-07-03 17:02:35 +010026unsigned long physical_memsize = 0L;
27
Markos Chandrase6ca4e52014-01-15 13:55:07 +000028fw_memblock_t * __init fw_getmdesc(int eva)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Markos Chandrasacd8bc12014-05-23 13:31:32 +010030 char *memsize_str, *ememsize_str = NULL, *ptr;
Ralf Baechlef8647b52014-06-04 22:53:02 +020031 unsigned long memsize = 0, ememsize = 0;
Dmitri Vorobiev7580c9c2009-10-13 23:43:24 +030032 static char cmdline[COMMAND_LINE_SIZE] __initdata;
Steven J. Hill49bffbd2013-03-25 15:05:40 -050033 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Ralf Baechlee1a4e462006-07-03 17:02:35 +010035 /* otherwise look in the environment */
Markos Chandrase6ca4e52014-01-15 13:55:07 +000036
Steven J. Hillb431f092013-03-25 14:47:05 -050037 memsize_str = fw_getenv("memsize");
Markos Chandrase6ca4e52014-01-15 13:55:07 +000038 if (memsize_str)
39 tmp = kstrtol(memsize_str, 0, &memsize);
40 if (eva) {
41 /* Look for ememsize for EVA */
42 ememsize_str = fw_getenv("ememsize");
43 if (ememsize_str)
44 tmp = kstrtol(ememsize_str, 0, &ememsize);
45 }
46 if (!memsize && !ememsize) {
Steven J. Hill49bffbd2013-03-25 15:05:40 -050047 pr_warn("memsize not set in YAMON, set to default (32Mb)\n");
Ralf Baechlee1a4e462006-07-03 17:02:35 +010048 physical_memsize = 0x02000000;
49 } else {
Markos Chandrase6ca4e52014-01-15 13:55:07 +000050 /* If ememsize is set, then set physical_memsize to that */
51 physical_memsize = ememsize ? : memsize;
Ralf Baechlee1a4e462006-07-03 17:02:35 +010052 }
53
54#ifdef CONFIG_CPU_BIG_ENDIAN
55 /* SOC-it swaps, or perhaps doesn't swap, when DMA'ing the last
56 word of physical memory */
57 physical_memsize -= PAGE_SIZE;
58#endif
59
60 /* Check the command line for a memsize directive that overrides
61 the physical/default amount */
Ralf Baechlee01402b2005-07-14 15:57:16 +000062 strcpy(cmdline, arcs_cmdline);
63 ptr = strstr(cmdline, "memsize=");
64 if (ptr && (ptr != cmdline) && (*(ptr - 1) != ' '))
65 ptr = strstr(ptr, " memsize=");
Markos Chandrase6ca4e52014-01-15 13:55:07 +000066 /* And now look for ememsize */
67 if (eva) {
68 ptr = strstr(cmdline, "ememsize=");
69 if (ptr && (ptr != cmdline) && (*(ptr - 1) != ' '))
70 ptr = strstr(ptr, " ememsize=");
71 }
Ralf Baechlee01402b2005-07-14 15:57:16 +000072
Ralf Baechlee1a4e462006-07-03 17:02:35 +010073 if (ptr)
Markos Chandrase6ca4e52014-01-15 13:55:07 +000074 memsize = memparse(ptr + 8 + (eva ? 1 : 0), &ptr);
Ralf Baechlee1a4e462006-07-03 17:02:35 +010075 else
76 memsize = physical_memsize;
Elizabeth Oldham73499682006-06-06 10:57:09 +010077
Markos Chandrase6ca4e52014-01-15 13:55:07 +000078 /* Last 64K for HIGHMEM arithmetics */
79 if (memsize > 0x7fff0000)
80 memsize = 0x7fff0000;
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 memset(mdesc, 0, sizeof(mdesc));
83
Steven J. Hillb431f092013-03-25 14:47:05 -050084 mdesc[0].type = fw_dontuse;
Markos Chandras3bdd8e62014-01-15 14:13:05 +000085 mdesc[0].base = PHYS_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 mdesc[0].size = 0x00001000;
87
Steven J. Hillb431f092013-03-25 14:47:05 -050088 mdesc[1].type = fw_code;
Markos Chandras3bdd8e62014-01-15 14:13:05 +000089 mdesc[1].base = mdesc[0].base + 0x00001000UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 mdesc[1].size = 0x000ef000;
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 /*
93 * The area 0x000f0000-0x000fffff is allocated for BIOS memory by the
94 * south bridge and PCI access always forwarded to the ISA Bus and
95 * BIOSCS# is always generated.
96 * This mean that this area can't be used as DMA memory for PCI
97 * devices.
98 */
Steven J. Hillb431f092013-03-25 14:47:05 -050099 mdesc[2].type = fw_dontuse;
Markos Chandras3bdd8e62014-01-15 14:13:05 +0000100 mdesc[2].base = mdesc[0].base + 0x000f0000UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 mdesc[2].size = 0x00010000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Steven J. Hillb431f092013-03-25 14:47:05 -0500103 mdesc[3].type = fw_dontuse;
Markos Chandras3bdd8e62014-01-15 14:13:05 +0000104 mdesc[3].base = mdesc[0].base + 0x00100000UL;
Steven J. Hill49bffbd2013-03-25 15:05:40 -0500105 mdesc[3].size = CPHYSADDR(PFN_ALIGN((unsigned long)&_end)) -
Markos Chandras3bdd8e62014-01-15 14:13:05 +0000106 0x00100000UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Steven J. Hillb431f092013-03-25 14:47:05 -0500108 mdesc[4].type = fw_free;
Markos Chandras3bdd8e62014-01-15 14:13:05 +0000109 mdesc[4].base = mdesc[0].base + CPHYSADDR(PFN_ALIGN(&_end));
110 mdesc[4].size = memsize - CPHYSADDR(mdesc[4].base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 return &mdesc[0];
113}
114
Markos Chandrasd1965c02014-01-15 14:07:57 +0000115static void free_init_pages_eva_malta(void *begin, void *end)
116{
117 free_init_pages("unused kernel", __pa_symbol((unsigned long *)begin),
118 __pa_symbol((unsigned long *)end));
119}
120
Steven J. Hillb431f092013-03-25 14:47:05 -0500121static int __init fw_memtype_classify(unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 switch (type) {
Steven J. Hillb431f092013-03-25 14:47:05 -0500124 case fw_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return BOOT_MEM_RAM;
Steven J. Hillb431f092013-03-25 14:47:05 -0500126 case fw_code:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return BOOT_MEM_ROM_DATA;
128 default:
129 return BOOT_MEM_RESERVED;
130 }
131}
132
Steven J. Hillb431f092013-03-25 14:47:05 -0500133void __init fw_meminit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Steven J. Hillb431f092013-03-25 14:47:05 -0500135 fw_memblock_t *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Markos Chandrase6ca4e52014-01-15 13:55:07 +0000137 p = fw_getmdesc(config_enabled(CONFIG_EVA));
Markos Chandrasd1965c02014-01-15 14:07:57 +0000138 free_init_pages_eva = (config_enabled(CONFIG_EVA) ?
139 free_init_pages_eva_malta : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 while (p->size) {
142 long type;
143 unsigned long base, size;
144
Steven J. Hillb431f092013-03-25 14:47:05 -0500145 type = fw_memtype_classify(p->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 base = p->base;
147 size = p->size;
148
149 add_memory_region(base, size, type);
Ralf Baechle70342282013-01-22 12:59:30 +0100150 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152}
153
Atsushi Nemotoc44e8d52006-12-30 00:43:59 +0900154void __init prom_free_prom_memory(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 unsigned long addr;
157 int i;
158
159 for (i = 0; i < boot_mem_map.nr_map; i++) {
160 if (boot_mem_map.map[i].type != BOOT_MEM_ROM_DATA)
161 continue;
162
Atsushi Nemotoc44e8d52006-12-30 00:43:59 +0900163 addr = boot_mem_map.map[i].addr;
Steven J. Hill49bffbd2013-03-25 15:05:40 -0500164 free_init_pages("YAMON memory",
Atsushi Nemotoc44e8d52006-12-30 00:43:59 +0900165 addr, addr + boot_mem_map.map[i].size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
Paul Burton3a551e22014-07-14 12:37:39 +0100168
169unsigned platform_maar_init(unsigned num_pairs)
170{
171 phys_addr_t mem_end = (physical_memsize & ~0xffffull) - 1;
172 struct maar_config cfg[] = {
173 /* DRAM preceding I/O */
174 { 0x00000000, 0x0fffffff, MIPS_MAAR_S },
175
176 /* DRAM following I/O */
177 { 0x20000000, mem_end, MIPS_MAAR_S },
178
179 /* DRAM alias in upper half of physical */
180 { 0x80000000, 0x80000000 + mem_end, MIPS_MAAR_S },
181 };
182 unsigned i, num_cfg = ARRAY_SIZE(cfg);
183
184 /* If DRAM fits before I/O, drop the region following it */
185 if (physical_memsize <= 0x10000000) {
186 num_cfg--;
187 for (i = 1; i < num_cfg; i++)
188 cfg[i] = cfg[i + 1];
189 }
190
191 return maar_config(cfg, num_cfg, num_pairs);
192}