• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * 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.
5  *
6  * PROM library functions for acquiring/using memory descriptors given to
7  * us from the YAMON.
8  *
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>
13  */
14 #include <linux/init.h>
15 #include <linux/bootmem.h>
16 #include <linux/string.h>
17 
18 #include <asm/bootinfo.h>
19 #include <asm/maar.h>
20 #include <asm/sections.h>
21 #include <asm/fw/fw.h>
22 
23 static fw_memblock_t mdesc[FW_MAX_MEMBLOCKS];
24 
25 /* determined physical memory size, not overridden by command line args	 */
26 unsigned long physical_memsize = 0L;
27 
fw_getmdesc(int eva)28 fw_memblock_t * __init fw_getmdesc(int eva)
29 {
30 	char *memsize_str, *ememsize_str = NULL, *ptr;
31 	unsigned long memsize = 0, ememsize = 0;
32 	static char cmdline[COMMAND_LINE_SIZE] __initdata;
33 	int tmp;
34 
35 	/* otherwise look in the environment */
36 
37 	memsize_str = fw_getenv("memsize");
38 	if (memsize_str) {
39 		tmp = kstrtoul(memsize_str, 0, &memsize);
40 		if (tmp)
41 			pr_warn("Failed to read the 'memsize' env variable.\n");
42 	}
43 	if (eva) {
44 	/* Look for ememsize for EVA */
45 		ememsize_str = fw_getenv("ememsize");
46 		if (ememsize_str) {
47 			tmp = kstrtoul(ememsize_str, 0, &ememsize);
48 			if (tmp)
49 				pr_warn("Failed to read the 'ememsize' env variable.\n");
50 		}
51 	}
52 	if (!memsize && !ememsize) {
53 		pr_warn("memsize not set in YAMON, set to default (32Mb)\n");
54 		physical_memsize = 0x02000000;
55 	} else {
56 		if (memsize > (256 << 20)) { /* memsize should be capped to 256M */
57 			pr_warn("Unsupported memsize value (0x%lx) detected! "
58 				"Using 0x10000000 (256M) instead\n",
59 				memsize);
60 			memsize = 256 << 20;
61 		}
62 		/* If ememsize is set, then set physical_memsize to that */
63 		physical_memsize = ememsize ? : memsize;
64 	}
65 
66 #ifdef CONFIG_CPU_BIG_ENDIAN
67 	/* SOC-it swaps, or perhaps doesn't swap, when DMA'ing the last
68 	   word of physical memory */
69 	physical_memsize -= PAGE_SIZE;
70 #endif
71 
72 	/* Check the command line for a memsize directive that overrides
73 	   the physical/default amount */
74 	strcpy(cmdline, arcs_cmdline);
75 	ptr = strstr(cmdline, "memsize=");
76 	if (ptr && (ptr != cmdline) && (*(ptr - 1) != ' '))
77 		ptr = strstr(ptr, " memsize=");
78 	/* And now look for ememsize */
79 	if (eva) {
80 		ptr = strstr(cmdline, "ememsize=");
81 		if (ptr && (ptr != cmdline) && (*(ptr - 1) != ' '))
82 			ptr = strstr(ptr, " ememsize=");
83 	}
84 
85 	if (ptr)
86 		memsize = memparse(ptr + 8 + (eva ? 1 : 0), &ptr);
87 	else
88 		memsize = physical_memsize;
89 
90 	/* Last 64K for HIGHMEM arithmetics */
91 	if (memsize > 0x7fff0000)
92 		memsize = 0x7fff0000;
93 
94 	memset(mdesc, 0, sizeof(mdesc));
95 
96 	mdesc[0].type = fw_dontuse;
97 	mdesc[0].base = PHYS_OFFSET;
98 	mdesc[0].size = 0x00001000;
99 
100 	mdesc[1].type = fw_code;
101 	mdesc[1].base = mdesc[0].base + 0x00001000UL;
102 	mdesc[1].size = 0x000ef000;
103 
104 	/*
105 	 * The area 0x000f0000-0x000fffff is allocated for BIOS memory by the
106 	 * south bridge and PCI access always forwarded to the ISA Bus and
107 	 * BIOSCS# is always generated.
108 	 * This mean that this area can't be used as DMA memory for PCI
109 	 * devices.
110 	 */
111 	mdesc[2].type = fw_dontuse;
112 	mdesc[2].base = mdesc[0].base + 0x000f0000UL;
113 	mdesc[2].size = 0x00010000;
114 
115 	mdesc[3].type = fw_dontuse;
116 	mdesc[3].base = mdesc[0].base + 0x00100000UL;
117 	mdesc[3].size = CPHYSADDR(PFN_ALIGN((unsigned long)&_end)) -
118 		0x00100000UL;
119 
120 	mdesc[4].type = fw_free;
121 	mdesc[4].base = mdesc[0].base + CPHYSADDR(PFN_ALIGN(&_end));
122 	mdesc[4].size = memsize - CPHYSADDR(mdesc[4].base);
123 
124 	return &mdesc[0];
125 }
126 
free_init_pages_eva_malta(void * begin,void * end)127 static void free_init_pages_eva_malta(void *begin, void *end)
128 {
129 	free_init_pages("unused kernel", __pa_symbol((unsigned long *)begin),
130 			__pa_symbol((unsigned long *)end));
131 }
132 
fw_memtype_classify(unsigned int type)133 static int __init fw_memtype_classify(unsigned int type)
134 {
135 	switch (type) {
136 	case fw_free:
137 		return BOOT_MEM_RAM;
138 	case fw_code:
139 		return BOOT_MEM_ROM_DATA;
140 	default:
141 		return BOOT_MEM_RESERVED;
142 	}
143 }
144 
fw_meminit(void)145 void __init fw_meminit(void)
146 {
147 	fw_memblock_t *p;
148 
149 	p = fw_getmdesc(config_enabled(CONFIG_EVA));
150 	free_init_pages_eva = (config_enabled(CONFIG_EVA) ?
151 			       free_init_pages_eva_malta : NULL);
152 
153 	while (p->size) {
154 		long type;
155 		unsigned long base, size;
156 
157 		type = fw_memtype_classify(p->type);
158 		base = p->base;
159 		size = p->size;
160 
161 		add_memory_region(base, size, type);
162 		p++;
163 	}
164 }
165 
prom_free_prom_memory(void)166 void __init prom_free_prom_memory(void)
167 {
168 	unsigned long addr;
169 	int i;
170 
171 	for (i = 0; i < boot_mem_map.nr_map; i++) {
172 		if (boot_mem_map.map[i].type != BOOT_MEM_ROM_DATA)
173 			continue;
174 
175 		addr = boot_mem_map.map[i].addr;
176 		free_init_pages("YAMON memory",
177 				addr, addr + boot_mem_map.map[i].size);
178 	}
179 }
180 
platform_maar_init(unsigned num_pairs)181 unsigned platform_maar_init(unsigned num_pairs)
182 {
183 	phys_addr_t mem_end = (physical_memsize & ~0xffffull) - 1;
184 	struct maar_config cfg[] = {
185 		/* DRAM preceding I/O */
186 		{ 0x00000000, 0x0fffffff, MIPS_MAAR_S },
187 
188 		/* DRAM following I/O */
189 		{ 0x20000000, mem_end, MIPS_MAAR_S },
190 
191 		/* DRAM alias in upper half of physical */
192 		{ 0x80000000, 0x80000000 + mem_end, MIPS_MAAR_S },
193 	};
194 	unsigned i, num_cfg = ARRAY_SIZE(cfg);
195 
196 	/* If DRAM fits before I/O, drop the region following it */
197 	if (physical_memsize <= 0x10000000) {
198 		num_cfg--;
199 		for (i = 1; i < num_cfg; i++)
200 			cfg[i] = cfg[i + 1];
201 	}
202 
203 	return maar_config(cfg, num_cfg, num_pairs);
204 }
205