x86: extend e820 ealy_res support 32bit
[deliverable/linux.git] / arch / x86 / kernel / e820_64.c
1 /*
2 * Handle the memory map.
3 * The functions here do the job until bootmem takes over.
4 *
5 * Getting sanitize_e820_map() in sync with i386 version by applying change:
6 * - Provisions for empty E820 memory regions (reported by certain BIOSes).
7 * Alex Achenbach <xela@slit.de>, December 2002.
8 * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
9 *
10 */
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/init.h>
14 #include <linux/bootmem.h>
15 #include <linux/ioport.h>
16 #include <linux/string.h>
17 #include <linux/kexec.h>
18 #include <linux/module.h>
19 #include <linux/mm.h>
20 #include <linux/suspend.h>
21 #include <linux/pfn.h>
22
23 #include <asm/pgtable.h>
24 #include <asm/page.h>
25 #include <asm/e820.h>
26 #include <asm/proto.h>
27 #include <asm/setup.h>
28 #include <asm/sections.h>
29 #include <asm/kdebug.h>
30 #include <asm/trampoline.h>
31
32 /*
33 * PFN of last memory page.
34 */
35 unsigned long end_pfn;
36
37 /*
38 * end_pfn only includes RAM, while max_pfn_mapped includes all e820 entries.
39 * The direct mapping extends to max_pfn_mapped, so that we can directly access
40 * apertures, ACPI and other tables without having to play with fixmaps.
41 */
42 unsigned long max_pfn_mapped;
43
44 /*
45 * Last pfn which the user wants to use.
46 */
47 static unsigned long __initdata end_user_pfn = MAXMEM>>PAGE_SHIFT;
48
49 /*
50 * Find the highest page frame number we have available
51 */
52 unsigned long __init e820_end_of_ram(void)
53 {
54 unsigned long end_pfn;
55
56 end_pfn = find_max_pfn_with_active_regions();
57
58 if (end_pfn > max_pfn_mapped)
59 max_pfn_mapped = end_pfn;
60 if (max_pfn_mapped > MAXMEM>>PAGE_SHIFT)
61 max_pfn_mapped = MAXMEM>>PAGE_SHIFT;
62 if (end_pfn > end_user_pfn)
63 end_pfn = end_user_pfn;
64 if (end_pfn > max_pfn_mapped)
65 end_pfn = max_pfn_mapped;
66
67 printk(KERN_INFO "max_pfn_mapped = %lu\n", max_pfn_mapped);
68 return end_pfn;
69 }
70
71 /*
72 * Mark e820 reserved areas as busy for the resource manager.
73 */
74 void __init e820_reserve_resources(void)
75 {
76 int i;
77 struct resource *res;
78
79 res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
80 for (i = 0; i < e820.nr_map; i++) {
81 switch (e820.map[i].type) {
82 case E820_RAM: res->name = "System RAM"; break;
83 case E820_ACPI: res->name = "ACPI Tables"; break;
84 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
85 default: res->name = "reserved";
86 }
87 res->start = e820.map[i].addr;
88 res->end = res->start + e820.map[i].size - 1;
89 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
90 insert_resource(&iomem_resource, res);
91 res++;
92 }
93 }
94
95 /*
96 * Find the ranges of physical addresses that do not correspond to
97 * e820 RAM areas and mark the corresponding pages as nosave for software
98 * suspend and suspend to RAM.
99 *
100 * This function requires the e820 map to be sorted and without any
101 * overlapping entries and assumes the first e820 area to be RAM.
102 */
103 void __init e820_mark_nosave_regions(void)
104 {
105 int i;
106 unsigned long paddr;
107
108 paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
109 for (i = 1; i < e820.nr_map; i++) {
110 struct e820entry *ei = &e820.map[i];
111
112 if (paddr < ei->addr)
113 register_nosave_region(PFN_DOWN(paddr),
114 PFN_UP(ei->addr));
115
116 paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
117 if (ei->type != E820_RAM)
118 register_nosave_region(PFN_UP(ei->addr),
119 PFN_DOWN(paddr));
120
121 if (paddr >= (end_pfn << PAGE_SHIFT))
122 break;
123 }
124 }
125
126 /*
127 * Finds an active region in the address range from start_pfn to end_pfn and
128 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
129 */
130 static int __init e820_find_active_region(const struct e820entry *ei,
131 unsigned long start_pfn,
132 unsigned long end_pfn,
133 unsigned long *ei_startpfn,
134 unsigned long *ei_endpfn)
135 {
136 *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
137 *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
138
139 /* Skip map entries smaller than a page */
140 if (*ei_startpfn >= *ei_endpfn)
141 return 0;
142
143 /* Check if max_pfn_mapped should be updated */
144 if (ei->type != E820_RAM && *ei_endpfn > max_pfn_mapped)
145 max_pfn_mapped = *ei_endpfn;
146
147 /* Skip if map is outside the node */
148 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
149 *ei_startpfn >= end_pfn)
150 return 0;
151
152 /* Check for overlaps */
153 if (*ei_startpfn < start_pfn)
154 *ei_startpfn = start_pfn;
155 if (*ei_endpfn > end_pfn)
156 *ei_endpfn = end_pfn;
157
158 /* Obey end_user_pfn to save on memmap */
159 if (*ei_startpfn >= end_user_pfn)
160 return 0;
161 if (*ei_endpfn > end_user_pfn)
162 *ei_endpfn = end_user_pfn;
163
164 return 1;
165 }
166
167 /* Walk the e820 map and register active regions within a node */
168 void __init
169 e820_register_active_regions(int nid, unsigned long start_pfn,
170 unsigned long end_pfn)
171 {
172 unsigned long ei_startpfn;
173 unsigned long ei_endpfn;
174 int i;
175
176 for (i = 0; i < e820.nr_map; i++)
177 if (e820_find_active_region(&e820.map[i],
178 start_pfn, end_pfn,
179 &ei_startpfn, &ei_endpfn))
180 add_active_range(nid, ei_startpfn, ei_endpfn);
181 }
182
183 /*
184 * Find the hole size (in bytes) in the memory range.
185 * @start: starting address of the memory range to scan
186 * @end: ending address of the memory range to scan
187 */
188 unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
189 {
190 unsigned long start_pfn = start >> PAGE_SHIFT;
191 unsigned long end_pfn = end >> PAGE_SHIFT;
192 unsigned long ei_startpfn, ei_endpfn, ram = 0;
193 int i;
194
195 for (i = 0; i < e820.nr_map; i++) {
196 if (e820_find_active_region(&e820.map[i],
197 start_pfn, end_pfn,
198 &ei_startpfn, &ei_endpfn))
199 ram += ei_endpfn - ei_startpfn;
200 }
201 return end - start - (ram << PAGE_SHIFT);
202 }
203
204 static void early_panic(char *msg)
205 {
206 early_printk(msg);
207 panic(msg);
208 }
209
210 /* We're not void only for x86 32-bit compat */
211 char *__init machine_specific_memory_setup(void)
212 {
213 char *who = "BIOS-e820";
214 int new_nr;
215 /*
216 * Try to copy the BIOS-supplied E820-map.
217 *
218 * Otherwise fake a memory map; one section from 0k->640k,
219 * the next section from 1mb->appropriate_mem_k
220 */
221 new_nr = boot_params.e820_entries;
222 sanitize_e820_map(boot_params.e820_map,
223 ARRAY_SIZE(boot_params.e820_map),
224 &new_nr);
225 boot_params.e820_entries = new_nr;
226 if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
227 early_panic("Cannot find a valid memory map");
228 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
229 e820_print_map(who);
230
231 /* In case someone cares... */
232 return who;
233 }
234
235 static int __init parse_memopt(char *p)
236 {
237 if (!p)
238 return -EINVAL;
239 end_user_pfn = memparse(p, &p);
240 end_user_pfn >>= PAGE_SHIFT;
241 return 0;
242 }
243 early_param("mem", parse_memopt);
244
245 static int userdef __initdata;
246
247 static int __init parse_memmap_opt(char *p)
248 {
249 char *oldp;
250 unsigned long long start_at, mem_size;
251
252 if (!strcmp(p, "exactmap")) {
253 #ifdef CONFIG_CRASH_DUMP
254 /*
255 * If we are doing a crash dump, we still need to know
256 * the real mem size before original memory map is
257 * reset.
258 */
259 e820_register_active_regions(0, 0, -1UL);
260 saved_max_pfn = e820_end_of_ram();
261 remove_all_active_ranges();
262 #endif
263 max_pfn_mapped = 0;
264 e820.nr_map = 0;
265 userdef = 1;
266 return 0;
267 }
268
269 oldp = p;
270 mem_size = memparse(p, &p);
271 if (p == oldp)
272 return -EINVAL;
273
274 userdef = 1;
275 if (*p == '@') {
276 start_at = memparse(p+1, &p);
277 add_memory_region(start_at, mem_size, E820_RAM);
278 } else if (*p == '#') {
279 start_at = memparse(p+1, &p);
280 add_memory_region(start_at, mem_size, E820_ACPI);
281 } else if (*p == '$') {
282 start_at = memparse(p+1, &p);
283 add_memory_region(start_at, mem_size, E820_RESERVED);
284 } else {
285 end_user_pfn = (mem_size >> PAGE_SHIFT);
286 }
287 return *p == '\0' ? 0 : -EINVAL;
288 }
289 early_param("memmap", parse_memmap_opt);
290
291 void __init finish_e820_parsing(void)
292 {
293 if (userdef) {
294 int nr = e820.nr_map;
295
296 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
297 early_panic("Invalid user supplied memory map");
298 e820.nr_map = nr;
299
300 printk(KERN_INFO "user-defined physical RAM map:\n");
301 e820_print_map("user");
302 }
303 }
304
305 int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
306 {
307 int i;
308
309 if (slot < 0 || slot >= e820.nr_map)
310 return -1;
311 for (i = slot; i < e820.nr_map; i++) {
312 if (e820.map[i].type != E820_RAM)
313 continue;
314 break;
315 }
316 if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
317 return -1;
318 *addr = e820.map[i].addr;
319 *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
320 max_pfn << PAGE_SHIFT) - *addr;
321 return i + 1;
322 }
This page took 0.0424099999999999 seconds and 6 git commands to generate.