blob: 9f65b4cc323c49cf39d61242f62394865c94decb [file] [log] [blame]
Thomas Gleixner2f36fa12008-01-30 13:30:12 +01001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Handle the memory map.
3 * The functions here do the job until bootmem takes over.
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -07004 *
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#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>
Eric W. Biederman5f5609d2005-06-25 14:58:04 -070017#include <linux/kexec.h>
Andrew Mortonb9491ac2005-09-16 19:27:54 -070018#include <linux/module.h>
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -070019#include <linux/mm.h>
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -070020#include <linux/suspend.h>
21#include <linux/pfn.h>
Andrew Mortonb9491ac2005-09-16 19:27:54 -070022
Andrew Morton1a91023a2006-07-10 04:43:49 -070023#include <asm/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/page.h>
25#include <asm/e820.h>
26#include <asm/proto.h>
H. Peter Anvin30c82642007-10-15 17:13:22 -070027#include <asm/setup.h>
Andi Kleen2bc04142005-11-05 17:25:53 +010028#include <asm/sections.h>
Thomas Gleixner718fc132008-01-30 13:30:17 +010029#include <asm/kdebug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Jan Beulichb92e9fa2007-05-02 19:27:11 +020031struct e820map e820;
Andi Kleen3bd4d182006-09-26 10:52:33 +020032
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010033/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * PFN of last memory page.
35 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010036unsigned long end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010038/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
40 * The direct mapping extends to end_pfn_map, so that we can directly access
41 * apertures, ACPI and other tables without having to play with fixmaps.
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010042 */
43unsigned long end_pfn_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010045/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * Last pfn which the user wants to use.
47 */
Jan Beulichcaff0712006-09-26 10:52:31 +020048static unsigned long __initdata end_user_pfn = MAXMEM>>PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Andi Kleen75175272008-01-30 13:33:17 +010050/*
51 * Early reserved memory areas.
52 */
53#define MAX_EARLY_RES 20
54
55struct early_res {
56 unsigned long start, end;
Yinghai Lu25eff8d2008-02-01 17:49:41 +010057 char name[16];
Andi Kleen75175272008-01-30 13:33:17 +010058};
59static struct early_res early_res[MAX_EARLY_RES] __initdata = {
Yinghai Lu25eff8d2008-02-01 17:49:41 +010060 { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
Andi Kleen75175272008-01-30 13:33:17 +010061#ifdef CONFIG_SMP
Yinghai Lu25eff8d2008-02-01 17:49:41 +010062 { SMP_TRAMPOLINE_BASE, SMP_TRAMPOLINE_BASE + 2*PAGE_SIZE, "SMP_TRAMPOLINE" },
Andi Kleen75175272008-01-30 13:33:17 +010063#endif
64 {}
65};
66
Yinghai Lu25eff8d2008-02-01 17:49:41 +010067void __init reserve_early(unsigned long start, unsigned long end, char *name)
Andi Kleen75175272008-01-30 13:33:17 +010068{
69 int i;
70 struct early_res *r;
71 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
72 r = &early_res[i];
73 if (end > r->start && start < r->end)
Yinghai Lu25eff8d2008-02-01 17:49:41 +010074 panic("Overlapping early reservations %lx-%lx %s to %lx-%lx %s\n",
75 start, end - 1, name?name:"", r->start, r->end - 1, r->name);
Andi Kleen75175272008-01-30 13:33:17 +010076 }
77 if (i >= MAX_EARLY_RES)
78 panic("Too many early reservations");
79 r = &early_res[i];
80 r->start = start;
81 r->end = end;
Yinghai Lu25eff8d2008-02-01 17:49:41 +010082 if (name)
83 strncpy(r->name, name, sizeof(r->name) - 1);
Andi Kleen75175272008-01-30 13:33:17 +010084}
85
86void __init early_res_to_bootmem(void)
87{
88 int i;
89 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
90 struct early_res *r = &early_res[i];
Yinghai Lu25eff8d2008-02-01 17:49:41 +010091 printk(KERN_INFO "early res: %d [%lx-%lx] %s\n", i,
92 r->start, r->end - 1, r->name);
Andi Kleen75175272008-01-30 13:33:17 +010093 reserve_bootmem_generic(r->start, r->end - r->start);
94 }
95}
96
97/* Check for already reserved areas */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static inline int bad_addr(unsigned long *addrp, unsigned long size)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010099{
Andi Kleen75175272008-01-30 13:33:17 +0100100 int i;
101 unsigned long addr = *addrp, last;
102 int changed = 0;
103again:
104 last = addr + size;
105 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
106 struct early_res *r = &early_res[i];
107 if (last >= r->start && addr < r->end) {
108 *addrp = addr = r->end;
109 changed = 1;
110 goto again;
H. Peter Anvin30c82642007-10-15 17:13:22 -0700111 }
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100112 }
Andi Kleen75175272008-01-30 13:33:17 +0100113 return changed;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100114}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Arjan van de Ven95222362006-04-07 19:49:27 +0200116/*
117 * This function checks if any part of the range <start,end> is mapped
118 * with type.
119 */
Jan Beulichb92e9fa2007-05-02 19:27:11 +0200120int
Arjan van de Veneee5a9f2006-04-07 19:49:24 +0200121e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100122{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int i;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100124
125 for (i = 0; i < e820.nr_map; i++) {
126 struct e820entry *ei = &e820.map[i];
127
128 if (type && ei->type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 continue;
Eric W. Biederman48c8b112005-09-06 15:16:20 -0700130 if (ei->addr >= end || ei->addr + ei->size <= start)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100131 continue;
132 return 1;
133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return 0;
135}
Jan Beulichb92e9fa2007-05-02 19:27:11 +0200136EXPORT_SYMBOL_GPL(e820_any_mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Linus Torvalds79e453d2006-09-19 08:15:22 -0700138/*
139 * This function checks if the entire range <start,end> is mapped with type.
140 *
141 * Note: this function only works correct if the e820 table is sorted and
142 * not-overlapping, which is the case
143 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100144int __init e820_all_mapped(unsigned long start, unsigned long end,
145 unsigned type)
Linus Torvalds79e453d2006-09-19 08:15:22 -0700146{
147 int i;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100148
Linus Torvalds79e453d2006-09-19 08:15:22 -0700149 for (i = 0; i < e820.nr_map; i++) {
150 struct e820entry *ei = &e820.map[i];
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100151
Linus Torvalds79e453d2006-09-19 08:15:22 -0700152 if (type && ei->type != type)
153 continue;
154 /* is the region (part) in overlap with the current region ?*/
155 if (ei->addr >= end || ei->addr + ei->size <= start)
156 continue;
157
158 /* if the region is at the beginning of <start,end> we move
159 * start to the end of the region since it's ok until there
160 */
161 if (ei->addr <= start)
162 start = ei->addr + ei->size;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100163 /*
164 * if start is now at or beyond end, we're done, full
165 * coverage
166 */
Linus Torvalds79e453d2006-09-19 08:15:22 -0700167 if (start >= end)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100168 return 1;
Linus Torvalds79e453d2006-09-19 08:15:22 -0700169 }
170 return 0;
171}
172
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100173/*
Yinghai Lu24a5da72008-02-01 17:49:41 +0100174 * Find a free area with specified alignment in a specific range.
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100175 */
176unsigned long __init find_e820_area(unsigned long start, unsigned long end,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100177 unsigned size, unsigned long align)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100178{
179 int i;
Yinghai Lu24a5da72008-02-01 17:49:41 +0100180 unsigned long mask = ~(align - 1);
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100181
182 for (i = 0; i < e820.nr_map; i++) {
183 struct e820entry *ei = &e820.map[i];
184 unsigned long addr = ei->addr, last;
185
186 if (ei->type != E820_RAM)
187 continue;
188 if (addr < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 addr = start;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100190 if (addr > ei->addr + ei->size)
191 continue;
Robert Hentosh7ca97c62006-05-30 22:48:00 +0200192 while (bad_addr(&addr, size) && addr+size <= ei->addr+ei->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 ;
Yinghai Lu24a5da72008-02-01 17:49:41 +0100194 addr = (addr + align - 1) & mask;
195 last = addr + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (last > ei->addr + ei->size)
197 continue;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100198 if (last > end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 continue;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100200 return addr;
201 }
202 return -1UL;
203}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/*
206 * Find the highest page frame number we have available
207 */
208unsigned long __init e820_end_of_ram(void)
209{
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100210 unsigned long end_pfn;
211
Mel Gorman5cb248a2006-09-27 01:49:52 -0700212 end_pfn = find_max_pfn_with_active_regions();
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100213
214 if (end_pfn > end_pfn_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 end_pfn_map = end_pfn;
216 if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
217 end_pfn_map = MAXMEM>>PAGE_SHIFT;
218 if (end_pfn > end_user_pfn)
219 end_pfn = end_user_pfn;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100220 if (end_pfn > end_pfn_map)
221 end_pfn = end_pfn_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100223 printk(KERN_INFO "end_pfn_map = %lu\n", end_pfn_map);
224 return end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Andi Kleen485761b2005-08-26 18:34:10 -0700227/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 * Mark e820 reserved areas as busy for the resource manager.
229 */
Bernhard Wallec9cce832008-01-30 13:30:32 +0100230void __init e820_reserve_resources(struct resource *code_resource,
231 struct resource *data_resource, struct resource *bss_resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 int i;
234 for (i = 0; i < e820.nr_map; i++) {
235 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 res = alloc_bootmem_low(sizeof(struct resource));
237 switch (e820.map[i].type) {
238 case E820_RAM: res->name = "System RAM"; break;
239 case E820_ACPI: res->name = "ACPI Tables"; break;
240 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
241 default: res->name = "reserved";
242 }
243 res->start = e820.map[i].addr;
244 res->end = res->start + e820.map[i].size - 1;
245 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
246 request_resource(&iomem_resource, res);
247 if (e820.map[i].type == E820_RAM) {
248 /*
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100249 * We don't know which RAM region contains kernel data,
250 * so we try it repeatedly and let the resource manager
251 * test it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 */
Bernhard Wallec9cce832008-01-30 13:30:32 +0100253 request_resource(res, code_resource);
254 request_resource(res, data_resource);
255 request_resource(res, bss_resource);
Eric W. Biederman5f5609d2005-06-25 14:58:04 -0700256#ifdef CONFIG_KEXEC
Bernhard Walle5c3391f2007-10-18 23:40:59 -0700257 if (crashk_res.start != crashk_res.end)
258 request_resource(res, &crashk_res);
Eric W. Biederman5f5609d2005-06-25 14:58:04 -0700259#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 }
262}
263
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -0700264/*
265 * Find the ranges of physical addresses that do not correspond to
266 * e820 RAM areas and mark the corresponding pages as nosave for software
267 * suspend and suspend to RAM.
268 *
269 * This function requires the e820 map to be sorted and without any
270 * overlapping entries and assumes the first e820 area to be RAM.
271 */
272void __init e820_mark_nosave_regions(void)
273{
274 int i;
275 unsigned long paddr;
276
277 paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
278 for (i = 1; i < e820.nr_map; i++) {
279 struct e820entry *ei = &e820.map[i];
280
281 if (paddr < ei->addr)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700282 register_nosave_region(PFN_DOWN(paddr),
283 PFN_UP(ei->addr));
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -0700284
285 paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
286 if (ei->type != E820_RAM)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700287 register_nosave_region(PFN_UP(ei->addr),
288 PFN_DOWN(paddr));
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -0700289
290 if (paddr >= (end_pfn << PAGE_SHIFT))
291 break;
292 }
293}
294
David Rientjes3af044e2007-07-21 17:10:31 +0200295/*
296 * Finds an active region in the address range from start_pfn to end_pfn and
297 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
298 */
299static int __init e820_find_active_region(const struct e820entry *ei,
300 unsigned long start_pfn,
301 unsigned long end_pfn,
302 unsigned long *ei_startpfn,
303 unsigned long *ei_endpfn)
304{
305 *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
306 *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
307
308 /* Skip map entries smaller than a page */
309 if (*ei_startpfn >= *ei_endpfn)
310 return 0;
311
312 /* Check if end_pfn_map should be updated */
313 if (ei->type != E820_RAM && *ei_endpfn > end_pfn_map)
314 end_pfn_map = *ei_endpfn;
315
316 /* Skip if map is outside the node */
317 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
318 *ei_startpfn >= end_pfn)
319 return 0;
320
321 /* Check for overlaps */
322 if (*ei_startpfn < start_pfn)
323 *ei_startpfn = start_pfn;
324 if (*ei_endpfn > end_pfn)
325 *ei_endpfn = end_pfn;
326
327 /* Obey end_user_pfn to save on memmap */
328 if (*ei_startpfn >= end_user_pfn)
329 return 0;
330 if (*ei_endpfn > end_user_pfn)
331 *ei_endpfn = end_user_pfn;
332
333 return 1;
334}
335
Mel Gorman5cb248a2006-09-27 01:49:52 -0700336/* Walk the e820 map and register active regions within a node */
337void __init
338e820_register_active_regions(int nid, unsigned long start_pfn,
339 unsigned long end_pfn)
340{
David Rientjes3af044e2007-07-21 17:10:31 +0200341 unsigned long ei_startpfn;
342 unsigned long ei_endpfn;
Mel Gorman5cb248a2006-09-27 01:49:52 -0700343 int i;
Mel Gorman5cb248a2006-09-27 01:49:52 -0700344
David Rientjes3af044e2007-07-21 17:10:31 +0200345 for (i = 0; i < e820.nr_map; i++)
346 if (e820_find_active_region(&e820.map[i],
347 start_pfn, end_pfn,
348 &ei_startpfn, &ei_endpfn))
349 add_active_range(nid, ei_startpfn, ei_endpfn);
Mel Gorman5cb248a2006-09-27 01:49:52 -0700350}
351
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100352/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * Add a memory region to the kernel e820 map.
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100354 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355void __init add_memory_region(unsigned long start, unsigned long size, int type)
356{
357 int x = e820.nr_map;
358
359 if (x == E820MAX) {
360 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
361 return;
362 }
363
364 e820.map[x].addr = start;
365 e820.map[x].size = size;
366 e820.map[x].type = type;
367 e820.nr_map++;
368}
369
David Rientjesa7e96622007-07-21 17:11:29 +0200370/*
371 * Find the hole size (in bytes) in the memory range.
372 * @start: starting address of the memory range to scan
373 * @end: ending address of the memory range to scan
374 */
375unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
376{
377 unsigned long start_pfn = start >> PAGE_SHIFT;
378 unsigned long end_pfn = end >> PAGE_SHIFT;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100379 unsigned long ei_startpfn, ei_endpfn, ram = 0;
David Rientjesa7e96622007-07-21 17:11:29 +0200380 int i;
381
382 for (i = 0; i < e820.nr_map; i++) {
383 if (e820_find_active_region(&e820.map[i],
384 start_pfn, end_pfn,
385 &ei_startpfn, &ei_endpfn))
386 ram += ei_endpfn - ei_startpfn;
387 }
388 return end - start - (ram << PAGE_SHIFT);
389}
390
Adrian Bunk013d23e2008-01-30 13:30:30 +0100391static void __init e820_print_map(char *who)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 int i;
394
395 for (i = 0; i < e820.nr_map; i++) {
Dan Aloni5a3ece72007-07-21 17:11:37 +0200396 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100397 (unsigned long long) e820.map[i].addr,
398 (unsigned long long)
399 (e820.map[i].addr + e820.map[i].size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 switch (e820.map[i].type) {
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100401 case E820_RAM:
402 printk(KERN_CONT "(usable)\n");
403 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 case E820_RESERVED:
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100405 printk(KERN_CONT "(reserved)\n");
406 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 case E820_ACPI:
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100408 printk(KERN_CONT "(ACPI data)\n");
409 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 case E820_NVS:
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100411 printk(KERN_CONT "(ACPI NVS)\n");
412 break;
413 default:
414 printk(KERN_CONT "type %u\n", e820.map[i].type);
415 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 }
418}
419
420/*
421 * Sanitize the BIOS e820 map.
422 *
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100423 * Some e820 responses include overlapping entries. The following
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * replaces the original e820 map with a new one, removing overlaps.
425 *
426 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100427static int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 struct change_member {
430 struct e820entry *pbios; /* pointer to original bios entry */
431 unsigned long long addr; /* address for this change point */
432 };
433 static struct change_member change_point_list[2*E820MAX] __initdata;
434 static struct change_member *change_point[2*E820MAX] __initdata;
435 static struct e820entry *overlap_list[E820MAX] __initdata;
436 static struct e820entry new_bios[E820MAX] __initdata;
437 struct change_member *change_tmp;
438 unsigned long current_type, last_type;
439 unsigned long long last_addr;
440 int chgidx, still_changing;
441 int overlap_entries;
442 int new_bios_entry;
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700443 int old_nr, new_nr, chg_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 int i;
445
446 /*
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100447 Visually we're performing the following
448 (1,2,3,4 = memory types)...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 Sample memory map (w/overlaps):
451 ____22__________________
452 ______________________4_
453 ____1111________________
454 _44_____________________
455 11111111________________
456 ____________________33__
457 ___________44___________
458 __________33333_________
459 ______________22________
460 ___________________2222_
461 _________111111111______
462 _____________________11_
463 _________________4______
464
465 Sanitized equivalent (no overlap):
466 1_______________________
467 _44_____________________
468 ___1____________________
469 ____22__________________
470 ______11________________
471 _________1______________
472 __________3_____________
473 ___________44___________
474 _____________33_________
475 _______________2________
476 ________________1_______
477 _________________4______
478 ___________________2____
479 ____________________33__
480 ______________________4_
481 */
482
483 /* if there's only one memory region, don't bother */
484 if (*pnr_map < 2)
485 return -1;
486
487 old_nr = *pnr_map;
488
489 /* bail out if we find any unreasonable addresses in bios map */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100490 for (i = 0; i < old_nr; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
492 return -1;
493
494 /* create pointers for initial change-point information (for sorting) */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100495 for (i = 0; i < 2 * old_nr; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 change_point[i] = &change_point_list[i];
497
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700498 /* record all known change-points (starting and ending addresses),
499 omitting those that are for empty memory regions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 chgidx = 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100501 for (i = 0; i < old_nr; i++) {
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700502 if (biosmap[i].size != 0) {
503 change_point[chgidx]->addr = biosmap[i].addr;
504 change_point[chgidx++]->pbios = &biosmap[i];
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100505 change_point[chgidx]->addr = biosmap[i].addr +
506 biosmap[i].size;
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700507 change_point[chgidx++]->pbios = &biosmap[i];
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700510 chg_nr = chgidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 /* sort change-point list by memory addresses (low -> high) */
513 still_changing = 1;
514 while (still_changing) {
515 still_changing = 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100516 for (i = 1; i < chg_nr; i++) {
517 unsigned long long curaddr, lastaddr;
518 unsigned long long curpbaddr, lastpbaddr;
519
520 curaddr = change_point[i]->addr;
521 lastaddr = change_point[i - 1]->addr;
522 curpbaddr = change_point[i]->pbios->addr;
523 lastpbaddr = change_point[i - 1]->pbios->addr;
524
525 /*
526 * swap entries, when:
527 *
528 * curaddr > lastaddr or
529 * curaddr == lastaddr and curaddr == curpbaddr and
530 * lastaddr != lastpbaddr
531 */
532 if (curaddr < lastaddr ||
533 (curaddr == lastaddr && curaddr == curpbaddr &&
534 lastaddr != lastpbaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 change_tmp = change_point[i];
536 change_point[i] = change_point[i-1];
537 change_point[i-1] = change_tmp;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100538 still_changing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540 }
541 }
542
543 /* create a new bios memory map, removing overlaps */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100544 overlap_entries = 0; /* number of entries in the overlap table */
545 new_bios_entry = 0; /* index for creating new bios map entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 last_type = 0; /* start with undefined memory type */
547 last_addr = 0; /* start with 0 as last starting address */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /* loop through change-points, determining affect on the new bios map */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100550 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* keep track of all overlapping bios entries */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100552 if (change_point[chgidx]->addr ==
553 change_point[chgidx]->pbios->addr) {
554 /*
555 * add map entry to overlap list (> 1 entry
556 * implies an overlap)
557 */
558 overlap_list[overlap_entries++] =
559 change_point[chgidx]->pbios;
560 } else {
561 /*
562 * remove entry from list (order independent,
563 * so swap with last)
564 */
565 for (i = 0; i < overlap_entries; i++) {
566 if (overlap_list[i] ==
567 change_point[chgidx]->pbios)
568 overlap_list[i] =
569 overlap_list[overlap_entries-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571 overlap_entries--;
572 }
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100573 /*
574 * if there are overlapping entries, decide which
575 * "type" to use (larger value takes precedence --
576 * 1=usable, 2,3,4,4+=unusable)
577 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 current_type = 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100579 for (i = 0; i < overlap_entries; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (overlap_list[i]->type > current_type)
581 current_type = overlap_list[i]->type;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100582 /*
583 * continue building up new bios map based on this
584 * information
585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (current_type != last_type) {
587 if (last_type != 0) {
588 new_bios[new_bios_entry].size =
589 change_point[chgidx]->addr - last_addr;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100590 /*
591 * move forward only if the new size
592 * was non-zero
593 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (new_bios[new_bios_entry].size != 0)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100595 /*
596 * no more space left for new
597 * bios entries ?
598 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (++new_bios_entry >= E820MAX)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100600 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602 if (current_type != 0) {
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100603 new_bios[new_bios_entry].addr =
604 change_point[chgidx]->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 new_bios[new_bios_entry].type = current_type;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100606 last_addr = change_point[chgidx]->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608 last_type = current_type;
609 }
610 }
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100611 /* retain count for new bios entries */
612 new_nr = new_bios_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 /* copy new bios mapping into original location */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100615 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 *pnr_map = new_nr;
617
618 return 0;
619}
620
621/*
622 * Copy the BIOS e820 map into a safe place.
623 *
624 * Sanity-check it while we're at it..
625 *
626 * If we're lucky and live on a modern system, the setup code
627 * will have given us a memory map that we can use to properly
628 * set up memory. If we aren't, we'll fake a memory map.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100630static int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 /* Only one memory region (or negative)? Ignore it */
633 if (nr_map < 2)
634 return -1;
635
636 do {
637 unsigned long start = biosmap->addr;
638 unsigned long size = biosmap->size;
639 unsigned long end = start + size;
640 unsigned long type = biosmap->type;
641
642 /* Overflow in 64 bits? Ignore the memory map. */
643 if (start > end)
644 return -1;
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 add_memory_region(start, size, type);
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100647 } while (biosmap++, --nr_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return 0;
649}
650
Adrian Bunk013d23e2008-01-30 13:30:30 +0100651static void early_panic(char *msg)
Andi Kleen8380aab2006-09-26 10:52:37 +0200652{
653 early_printk(msg);
654 panic(msg);
655}
656
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100657/* We're not void only for x86 32-bit compat */
658char * __init machine_specific_memory_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100660 char *who = "BIOS-e820";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /*
662 * Try to copy the BIOS-supplied E820-map.
663 *
664 * Otherwise fake a memory map; one section from 0k->640k,
665 * the next section from 1mb->appropriate_mem_k
666 */
H. Peter Anvin30c82642007-10-15 17:13:22 -0700667 sanitize_e820_map(boot_params.e820_map, &boot_params.e820_entries);
668 if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
Andi Kleen8380aab2006-09-26 10:52:37 +0200669 early_panic("Cannot find a valid memory map");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100671 e820_print_map(who);
672
673 /* In case someone cares... */
674 return who;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200677static int __init parse_memopt(char *p)
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800678{
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200679 if (!p)
680 return -EINVAL;
681 end_user_pfn = memparse(p, &p);
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100682 end_user_pfn >>= PAGE_SHIFT;
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200683 return 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100684}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200685early_param("mem", parse_memopt);
686
687static int userdef __initdata;
688
689static int __init parse_memmap_opt(char *p)
690{
691 char *oldp;
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800692 unsigned long long start_at, mem_size;
693
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200694 if (!strcmp(p, "exactmap")) {
695#ifdef CONFIG_CRASH_DUMP
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100696 /*
697 * If we are doing a crash dump, we still need to know
698 * the real mem size before original memory map is
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200699 * reset.
700 */
Magnus Damm15803a42006-11-14 16:57:46 +0100701 e820_register_active_regions(0, 0, -1UL);
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200702 saved_max_pfn = e820_end_of_ram();
Magnus Damm15803a42006-11-14 16:57:46 +0100703 remove_all_active_ranges();
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200704#endif
705 end_pfn_map = 0;
706 e820.nr_map = 0;
707 userdef = 1;
708 return 0;
709 }
710
711 oldp = p;
712 mem_size = memparse(p, &p);
713 if (p == oldp)
714 return -EINVAL;
Vladimir Bereznikerb3ca74a2008-01-30 13:30:46 +0100715
716 userdef = 1;
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800717 if (*p == '@') {
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200718 start_at = memparse(p+1, &p);
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800719 add_memory_region(start_at, mem_size, E820_RAM);
720 } else if (*p == '#') {
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200721 start_at = memparse(p+1, &p);
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800722 add_memory_region(start_at, mem_size, E820_ACPI);
723 } else if (*p == '$') {
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200724 start_at = memparse(p+1, &p);
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800725 add_memory_region(start_at, mem_size, E820_RESERVED);
726 } else {
727 end_user_pfn = (mem_size >> PAGE_SHIFT);
728 }
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200729 return *p == '\0' ? 0 : -EINVAL;
730}
731early_param("memmap", parse_memmap_opt);
732
Sam Ravnborg43999d92007-03-16 21:07:36 +0100733void __init finish_e820_parsing(void)
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200734{
735 if (userdef) {
Vladimir Bereznikerb3ca74a2008-01-30 13:30:46 +0100736 char nr = e820.nr_map;
737
738 if (sanitize_e820_map(e820.map, &nr) < 0)
739 early_panic("Invalid user supplied memory map");
740 e820.nr_map = nr;
741
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200742 printk(KERN_INFO "user-defined physical RAM map:\n");
743 e820_print_map("user");
744 }
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800745}
746
Yinghai Luaaf23042008-01-30 13:33:09 +0100747void __init update_e820(void)
748{
749 u8 nr_map;
750
751 nr_map = e820.nr_map;
752 if (sanitize_e820_map(e820.map, &nr_map))
753 return;
754 e820.nr_map = nr_map;
755 printk(KERN_INFO "modified physical RAM map:\n");
756 e820_print_map("modified");
757}
758
Andi Kleena1e97782005-04-16 15:25:12 -0700759unsigned long pci_mem_start = 0xaeedbabe;
Andi Kleen2ee60e172006-06-26 13:59:44 +0200760EXPORT_SYMBOL(pci_mem_start);
Andi Kleena1e97782005-04-16 15:25:12 -0700761
762/*
763 * Search for the biggest gap in the low 32 bits of the e820
764 * memory space. We pass this space to PCI to assign MMIO resources
765 * for hotplug or unconfigured devices in.
766 * Hopefully the BIOS let enough space left.
767 */
768__init void e820_setup_gap(void)
769{
Daniel Ritzf0eca962005-09-09 00:57:14 +0200770 unsigned long gapstart, gapsize, round;
Andi Kleena1e97782005-04-16 15:25:12 -0700771 unsigned long last;
772 int i;
773 int found = 0;
774
775 last = 0x100000000ull;
776 gapstart = 0x10000000;
777 gapsize = 0x400000;
778 i = e820.nr_map;
779 while (--i >= 0) {
780 unsigned long long start = e820.map[i].addr;
781 unsigned long long end = start + e820.map[i].size;
782
783 /*
784 * Since "last" is at most 4GB, we know we'll
785 * fit in 32 bits if this condition is true
786 */
787 if (last > end) {
788 unsigned long gap = last - end;
789
790 if (gap > gapsize) {
791 gapsize = gap;
792 gapstart = end;
793 found = 1;
794 }
795 }
796 if (start < last)
797 last = start;
798 }
799
800 if (!found) {
801 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100802 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
803 "address range\n"
804 KERN_ERR "PCI: Unassigned devices with 32bit resource "
805 "registers may break!\n");
Andi Kleena1e97782005-04-16 15:25:12 -0700806 }
807
808 /*
Daniel Ritzf0eca962005-09-09 00:57:14 +0200809 * See how much we want to round up: start off with
810 * rounding to the next 1MB area.
Andi Kleena1e97782005-04-16 15:25:12 -0700811 */
Daniel Ritzf0eca962005-09-09 00:57:14 +0200812 round = 0x100000;
813 while ((gapsize >> 4) > round)
814 round += round;
815 /* Fun with two's complement */
816 pci_mem_start = (gapstart + round) & -round;
Andi Kleena1e97782005-04-16 15:25:12 -0700817
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100818 printk(KERN_INFO
819 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
820 pci_mem_start, gapstart, gapsize);
Andi Kleena1e97782005-04-16 15:25:12 -0700821}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -0700822
823int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
824{
825 int i;
826
827 if (slot < 0 || slot >= e820.nr_map)
828 return -1;
829 for (i = slot; i < e820.nr_map; i++) {
830 if (e820.map[i].type != E820_RAM)
831 continue;
832 break;
833 }
834 if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
835 return -1;
836 *addr = e820.map[i].addr;
837 *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
838 max_pfn << PAGE_SHIFT) - *addr;
839 return i + 1;
840}