2 * Handle the memory map.
3 * The functions here do the job until bootmem takes over.
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>
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>
20 #include <linux/suspend.h>
21 #include <linux/pfn.h>
23 #include <asm/pgtable.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>
33 * PFN of last memory page.
35 unsigned long end_pfn;
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.
42 unsigned long max_pfn_mapped;
45 * Last pfn which the user wants to use.
47 static unsigned long __initdata end_user_pfn = MAXMEM>>PAGE_SHIFT;
50 * Early reserved memory areas.
52 #define MAX_EARLY_RES 20
55 unsigned long start, end;
58 static struct early_res early_res[MAX_EARLY_RES] __initdata = {
59 { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
60 #ifdef CONFIG_X86_TRAMPOLINE
61 { TRAMPOLINE_BASE, TRAMPOLINE_BASE + 2 * PAGE_SIZE, "TRAMPOLINE" },
66 void __init reserve_early(unsigned long start, unsigned long end, char *name)
70 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
72 if (end > r->start && start < r->end)
73 panic("Overlapping early reservations %lx-%lx %s to %lx-%lx %s\n",
74 start, end - 1, name?name:"", r->start, r->end - 1, r->name);
76 if (i >= MAX_EARLY_RES)
77 panic("Too many early reservations");
82 strncpy(r->name, name, sizeof(r->name) - 1);
85 void __init free_early(unsigned long start, unsigned long end)
90 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
92 if (start == r->start && end == r->end)
95 if (i >= MAX_EARLY_RES || !early_res[i].end)
96 panic("free_early on not reserved area: %lx-%lx!", start, end);
98 for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++)
101 memmove(&early_res[i], &early_res[i + 1],
102 (j - 1 - i) * sizeof(struct early_res));
104 early_res[j - 1].end = 0;
107 void __init early_res_to_bootmem(unsigned long start, unsigned long end)
110 unsigned long final_start, final_end;
111 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
112 struct early_res *r = &early_res[i];
113 final_start = max(start, r->start);
114 final_end = min(end, r->end);
115 if (final_start >= final_end)
117 printk(KERN_INFO " early res: %d [%lx-%lx] %s\n", i,
118 final_start, final_end - 1, r->name);
119 reserve_bootmem_generic(final_start, final_end - final_start);
123 /* Check for already reserved areas */
124 static inline int __init
125 bad_addr(unsigned long *addrp, unsigned long size, unsigned long align)
128 unsigned long addr = *addrp, last;
132 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
133 struct early_res *r = &early_res[i];
134 if (last >= r->start && addr < r->end) {
135 *addrp = addr = round_up(r->end, align);
143 /* Check for already reserved areas */
144 static inline int __init
145 bad_addr_size(unsigned long *addrp, unsigned long *sizep, unsigned long align)
148 unsigned long addr = *addrp, last;
149 unsigned long size = *sizep;
153 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
154 struct early_res *r = &early_res[i];
155 if (last > r->start && addr < r->start) {
156 size = r->start - addr;
160 if (last > r->end && addr < r->end) {
161 addr = round_up(r->end, align);
166 if (last <= r->end && addr >= r->start) {
179 * Find a free area with specified alignment in a specific range.
181 unsigned long __init find_e820_area(unsigned long start, unsigned long end,
182 unsigned long size, unsigned long align)
186 for (i = 0; i < e820.nr_map; i++) {
187 struct e820entry *ei = &e820.map[i];
188 unsigned long addr, last;
189 unsigned long ei_last;
191 if (ei->type != E820_RAM)
193 addr = round_up(ei->addr, align);
194 ei_last = ei->addr + ei->size;
196 addr = round_up(start, align);
199 while (bad_addr(&addr, size, align) && addr+size <= ei_last)
212 * Find next free range after *start
214 unsigned long __init find_e820_area_size(unsigned long start,
215 unsigned long *sizep,
220 for (i = 0; i < e820.nr_map; i++) {
221 struct e820entry *ei = &e820.map[i];
222 unsigned long addr, last;
223 unsigned long ei_last;
225 if (ei->type != E820_RAM)
227 addr = round_up(ei->addr, align);
228 ei_last = ei->addr + ei->size;
230 addr = round_up(start, align);
233 *sizep = ei_last - addr;
234 while (bad_addr_size(&addr, sizep, align) &&
235 addr + *sizep <= ei_last)
237 last = addr + *sizep;
246 * Find the highest page frame number we have available
248 unsigned long __init e820_end_of_ram(void)
250 unsigned long end_pfn;
252 end_pfn = find_max_pfn_with_active_regions();
254 if (end_pfn > max_pfn_mapped)
255 max_pfn_mapped = end_pfn;
256 if (max_pfn_mapped > MAXMEM>>PAGE_SHIFT)
257 max_pfn_mapped = MAXMEM>>PAGE_SHIFT;
258 if (end_pfn > end_user_pfn)
259 end_pfn = end_user_pfn;
260 if (end_pfn > max_pfn_mapped)
261 end_pfn = max_pfn_mapped;
263 printk(KERN_INFO "max_pfn_mapped = %lu\n", max_pfn_mapped);
268 * Mark e820 reserved areas as busy for the resource manager.
270 void __init e820_reserve_resources(void)
273 struct resource *res;
275 res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
276 for (i = 0; i < e820.nr_map; i++) {
277 switch (e820.map[i].type) {
278 case E820_RAM: res->name = "System RAM"; break;
279 case E820_ACPI: res->name = "ACPI Tables"; break;
280 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
281 default: res->name = "reserved";
283 res->start = e820.map[i].addr;
284 res->end = res->start + e820.map[i].size - 1;
285 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
286 insert_resource(&iomem_resource, res);
292 * Find the ranges of physical addresses that do not correspond to
293 * e820 RAM areas and mark the corresponding pages as nosave for software
294 * suspend and suspend to RAM.
296 * This function requires the e820 map to be sorted and without any
297 * overlapping entries and assumes the first e820 area to be RAM.
299 void __init e820_mark_nosave_regions(void)
304 paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
305 for (i = 1; i < e820.nr_map; i++) {
306 struct e820entry *ei = &e820.map[i];
308 if (paddr < ei->addr)
309 register_nosave_region(PFN_DOWN(paddr),
312 paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
313 if (ei->type != E820_RAM)
314 register_nosave_region(PFN_UP(ei->addr),
317 if (paddr >= (end_pfn << PAGE_SHIFT))
323 * Finds an active region in the address range from start_pfn to end_pfn and
324 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
326 static int __init e820_find_active_region(const struct e820entry *ei,
327 unsigned long start_pfn,
328 unsigned long end_pfn,
329 unsigned long *ei_startpfn,
330 unsigned long *ei_endpfn)
332 *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
333 *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
335 /* Skip map entries smaller than a page */
336 if (*ei_startpfn >= *ei_endpfn)
339 /* Check if max_pfn_mapped should be updated */
340 if (ei->type != E820_RAM && *ei_endpfn > max_pfn_mapped)
341 max_pfn_mapped = *ei_endpfn;
343 /* Skip if map is outside the node */
344 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
345 *ei_startpfn >= end_pfn)
348 /* Check for overlaps */
349 if (*ei_startpfn < start_pfn)
350 *ei_startpfn = start_pfn;
351 if (*ei_endpfn > end_pfn)
352 *ei_endpfn = end_pfn;
354 /* Obey end_user_pfn to save on memmap */
355 if (*ei_startpfn >= end_user_pfn)
357 if (*ei_endpfn > end_user_pfn)
358 *ei_endpfn = end_user_pfn;
363 /* Walk the e820 map and register active regions within a node */
365 e820_register_active_regions(int nid, unsigned long start_pfn,
366 unsigned long end_pfn)
368 unsigned long ei_startpfn;
369 unsigned long ei_endpfn;
372 for (i = 0; i < e820.nr_map; i++)
373 if (e820_find_active_region(&e820.map[i],
375 &ei_startpfn, &ei_endpfn))
376 add_active_range(nid, ei_startpfn, ei_endpfn);
380 * Find the hole size (in bytes) in the memory range.
381 * @start: starting address of the memory range to scan
382 * @end: ending address of the memory range to scan
384 unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
386 unsigned long start_pfn = start >> PAGE_SHIFT;
387 unsigned long end_pfn = end >> PAGE_SHIFT;
388 unsigned long ei_startpfn, ei_endpfn, ram = 0;
391 for (i = 0; i < e820.nr_map; i++) {
392 if (e820_find_active_region(&e820.map[i],
394 &ei_startpfn, &ei_endpfn))
395 ram += ei_endpfn - ei_startpfn;
397 return end - start - (ram << PAGE_SHIFT);
400 static void early_panic(char *msg)
406 /* We're not void only for x86 32-bit compat */
407 char *__init machine_specific_memory_setup(void)
409 char *who = "BIOS-e820";
411 * Try to copy the BIOS-supplied E820-map.
413 * Otherwise fake a memory map; one section from 0k->640k,
414 * the next section from 1mb->appropriate_mem_k
416 sanitize_e820_map(boot_params.e820_map,
417 ARRAY_SIZE(boot_params.e820_map),
418 &boot_params.e820_entries);
419 if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
420 early_panic("Cannot find a valid memory map");
421 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
424 /* In case someone cares... */
428 static int __init parse_memopt(char *p)
432 end_user_pfn = memparse(p, &p);
433 end_user_pfn >>= PAGE_SHIFT;
436 early_param("mem", parse_memopt);
438 static int userdef __initdata;
440 static int __init parse_memmap_opt(char *p)
443 unsigned long long start_at, mem_size;
445 if (!strcmp(p, "exactmap")) {
446 #ifdef CONFIG_CRASH_DUMP
448 * If we are doing a crash dump, we still need to know
449 * the real mem size before original memory map is
452 e820_register_active_regions(0, 0, -1UL);
453 saved_max_pfn = e820_end_of_ram();
454 remove_all_active_ranges();
463 mem_size = memparse(p, &p);
469 start_at = memparse(p+1, &p);
470 add_memory_region(start_at, mem_size, E820_RAM);
471 } else if (*p == '#') {
472 start_at = memparse(p+1, &p);
473 add_memory_region(start_at, mem_size, E820_ACPI);
474 } else if (*p == '$') {
475 start_at = memparse(p+1, &p);
476 add_memory_region(start_at, mem_size, E820_RESERVED);
478 end_user_pfn = (mem_size >> PAGE_SHIFT);
480 return *p == '\0' ? 0 : -EINVAL;
482 early_param("memmap", parse_memmap_opt);
484 void __init finish_e820_parsing(void)
487 char nr = e820.nr_map;
489 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
490 early_panic("Invalid user supplied memory map");
493 printk(KERN_INFO "user-defined physical RAM map:\n");
494 e820_print_map("user");
498 int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
502 if (slot < 0 || slot >= e820.nr_map)
504 for (i = slot; i < e820.nr_map; i++) {
505 if (e820.map[i].type != E820_RAM)
509 if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
511 *addr = e820.map[i].addr;
512 *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
513 max_pfn << PAGE_SHIFT) - *addr;