blob: a8694a35352b4664643e37a40de92c1e43320804 [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 */
Yinghai Lu48c508b2008-04-17 17:40:45 +020098static inline int
99bad_addr(unsigned long *addrp, unsigned long size, unsigned long align)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100100{
Andi Kleen75175272008-01-30 13:33:17 +0100101 int i;
102 unsigned long addr = *addrp, last;
103 int changed = 0;
104again:
105 last = addr + size;
106 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
107 struct early_res *r = &early_res[i];
108 if (last >= r->start && addr < r->end) {
Yinghai Lu48c508b2008-04-17 17:40:45 +0200109 *addrp = addr = round_up(r->end, align);
Andi Kleen75175272008-01-30 13:33:17 +0100110 changed = 1;
111 goto again;
H. Peter Anvin30c82642007-10-15 17:13:22 -0700112 }
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100113 }
Andi Kleen75175272008-01-30 13:33:17 +0100114 return changed;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100115}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Arjan van de Ven95222362006-04-07 19:49:27 +0200117/*
118 * This function checks if any part of the range <start,end> is mapped
119 * with type.
120 */
Jan Beulichb92e9fa2007-05-02 19:27:11 +0200121int
Arjan van de Veneee5a9f2006-04-07 19:49:24 +0200122e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100123{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int i;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100125
126 for (i = 0; i < e820.nr_map; i++) {
127 struct e820entry *ei = &e820.map[i];
128
129 if (type && ei->type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 continue;
Eric W. Biederman48c8b112005-09-06 15:16:20 -0700131 if (ei->addr >= end || ei->addr + ei->size <= start)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100132 continue;
133 return 1;
134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return 0;
136}
Jan Beulichb92e9fa2007-05-02 19:27:11 +0200137EXPORT_SYMBOL_GPL(e820_any_mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Linus Torvalds79e453d2006-09-19 08:15:22 -0700139/*
140 * This function checks if the entire range <start,end> is mapped with type.
141 *
142 * Note: this function only works correct if the e820 table is sorted and
143 * not-overlapping, which is the case
144 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100145int __init e820_all_mapped(unsigned long start, unsigned long end,
146 unsigned type)
Linus Torvalds79e453d2006-09-19 08:15:22 -0700147{
148 int i;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100149
Linus Torvalds79e453d2006-09-19 08:15:22 -0700150 for (i = 0; i < e820.nr_map; i++) {
151 struct e820entry *ei = &e820.map[i];
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100152
Linus Torvalds79e453d2006-09-19 08:15:22 -0700153 if (type && ei->type != type)
154 continue;
155 /* is the region (part) in overlap with the current region ?*/
156 if (ei->addr >= end || ei->addr + ei->size <= start)
157 continue;
158
159 /* if the region is at the beginning of <start,end> we move
160 * start to the end of the region since it's ok until there
161 */
162 if (ei->addr <= start)
163 start = ei->addr + ei->size;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100164 /*
165 * if start is now at or beyond end, we're done, full
166 * coverage
167 */
Linus Torvalds79e453d2006-09-19 08:15:22 -0700168 if (start >= end)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100169 return 1;
Linus Torvalds79e453d2006-09-19 08:15:22 -0700170 }
171 return 0;
172}
173
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100174/*
Yinghai Lu24a5da72008-02-01 17:49:41 +0100175 * Find a free area with specified alignment in a specific range.
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100176 */
177unsigned long __init find_e820_area(unsigned long start, unsigned long end,
Yinghai Lu48c508b2008-04-17 17:40:45 +0200178 unsigned long size, unsigned long align)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100179{
180 int i;
181
182 for (i = 0; i < e820.nr_map; i++) {
183 struct e820entry *ei = &e820.map[i];
Yinghai Lu48c508b2008-04-17 17:40:45 +0200184 unsigned long addr, last;
185 unsigned long ei_last;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100186
187 if (ei->type != E820_RAM)
188 continue;
Yinghai Lu48c508b2008-04-17 17:40:45 +0200189 addr = round_up(ei->addr, align);
190 ei_last = ei->addr + ei->size;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100191 if (addr < start)
Yinghai Lu48c508b2008-04-17 17:40:45 +0200192 addr = round_up(start, align);
193 if (addr > ei_last)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100194 continue;
Yinghai Lu48c508b2008-04-17 17:40:45 +0200195 while (bad_addr(&addr, size, align) && addr+size <= ei_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 ;
Yinghai Lu24a5da72008-02-01 17:49:41 +0100197 last = addr + size;
Yinghai Lu48c508b2008-04-17 17:40:45 +0200198 if (last > ei_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 continue;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100200 if (last > end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 continue;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100202 return addr;
203 }
204 return -1UL;
205}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/*
208 * Find the highest page frame number we have available
209 */
210unsigned long __init e820_end_of_ram(void)
211{
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100212 unsigned long end_pfn;
213
Mel Gorman5cb248a2006-09-27 01:49:52 -0700214 end_pfn = find_max_pfn_with_active_regions();
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100215
216 if (end_pfn > end_pfn_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 end_pfn_map = end_pfn;
218 if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
219 end_pfn_map = MAXMEM>>PAGE_SHIFT;
220 if (end_pfn > end_user_pfn)
221 end_pfn = end_user_pfn;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100222 if (end_pfn > end_pfn_map)
223 end_pfn = end_pfn_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100225 printk(KERN_INFO "end_pfn_map = %lu\n", end_pfn_map);
226 return end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Andi Kleen485761b2005-08-26 18:34:10 -0700229/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * Mark e820 reserved areas as busy for the resource manager.
231 */
Bernhard Wallec9cce832008-01-30 13:30:32 +0100232void __init e820_reserve_resources(struct resource *code_resource,
233 struct resource *data_resource, struct resource *bss_resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 int i;
236 for (i = 0; i < e820.nr_map; i++) {
237 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 res = alloc_bootmem_low(sizeof(struct resource));
239 switch (e820.map[i].type) {
240 case E820_RAM: res->name = "System RAM"; break;
241 case E820_ACPI: res->name = "ACPI Tables"; break;
242 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
243 default: res->name = "reserved";
244 }
245 res->start = e820.map[i].addr;
246 res->end = res->start + e820.map[i].size - 1;
247 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
248 request_resource(&iomem_resource, res);
249 if (e820.map[i].type == E820_RAM) {
250 /*
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100251 * We don't know which RAM region contains kernel data,
252 * so we try it repeatedly and let the resource manager
253 * test it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 */
Bernhard Wallec9cce832008-01-30 13:30:32 +0100255 request_resource(res, code_resource);
256 request_resource(res, data_resource);
257 request_resource(res, bss_resource);
Eric W. Biederman5f5609d2005-06-25 14:58:04 -0700258#ifdef CONFIG_KEXEC
Bernhard Walle5c3391f2007-10-18 23:40:59 -0700259 if (crashk_res.start != crashk_res.end)
260 request_resource(res, &crashk_res);
Eric W. Biederman5f5609d2005-06-25 14:58:04 -0700261#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263 }
264}
265
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -0700266/*
267 * Find the ranges of physical addresses that do not correspond to
268 * e820 RAM areas and mark the corresponding pages as nosave for software
269 * suspend and suspend to RAM.
270 *
271 * This function requires the e820 map to be sorted and without any
272 * overlapping entries and assumes the first e820 area to be RAM.
273 */
274void __init e820_mark_nosave_regions(void)
275{
276 int i;
277 unsigned long paddr;
278
279 paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
280 for (i = 1; i < e820.nr_map; i++) {
281 struct e820entry *ei = &e820.map[i];
282
283 if (paddr < ei->addr)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700284 register_nosave_region(PFN_DOWN(paddr),
285 PFN_UP(ei->addr));
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -0700286
287 paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
288 if (ei->type != E820_RAM)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700289 register_nosave_region(PFN_UP(ei->addr),
290 PFN_DOWN(paddr));
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -0700291
292 if (paddr >= (end_pfn << PAGE_SHIFT))
293 break;
294 }
295}
296
David Rientjes3af044e2007-07-21 17:10:31 +0200297/*
298 * Finds an active region in the address range from start_pfn to end_pfn and
299 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
300 */
301static int __init e820_find_active_region(const struct e820entry *ei,
302 unsigned long start_pfn,
303 unsigned long end_pfn,
304 unsigned long *ei_startpfn,
305 unsigned long *ei_endpfn)
306{
307 *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
308 *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
309
310 /* Skip map entries smaller than a page */
311 if (*ei_startpfn >= *ei_endpfn)
312 return 0;
313
314 /* Check if end_pfn_map should be updated */
315 if (ei->type != E820_RAM && *ei_endpfn > end_pfn_map)
316 end_pfn_map = *ei_endpfn;
317
318 /* Skip if map is outside the node */
319 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
320 *ei_startpfn >= end_pfn)
321 return 0;
322
323 /* Check for overlaps */
324 if (*ei_startpfn < start_pfn)
325 *ei_startpfn = start_pfn;
326 if (*ei_endpfn > end_pfn)
327 *ei_endpfn = end_pfn;
328
329 /* Obey end_user_pfn to save on memmap */
330 if (*ei_startpfn >= end_user_pfn)
331 return 0;
332 if (*ei_endpfn > end_user_pfn)
333 *ei_endpfn = end_user_pfn;
334
335 return 1;
336}
337
Mel Gorman5cb248a2006-09-27 01:49:52 -0700338/* Walk the e820 map and register active regions within a node */
339void __init
340e820_register_active_regions(int nid, unsigned long start_pfn,
341 unsigned long end_pfn)
342{
David Rientjes3af044e2007-07-21 17:10:31 +0200343 unsigned long ei_startpfn;
344 unsigned long ei_endpfn;
Mel Gorman5cb248a2006-09-27 01:49:52 -0700345 int i;
Mel Gorman5cb248a2006-09-27 01:49:52 -0700346
David Rientjes3af044e2007-07-21 17:10:31 +0200347 for (i = 0; i < e820.nr_map; i++)
348 if (e820_find_active_region(&e820.map[i],
349 start_pfn, end_pfn,
350 &ei_startpfn, &ei_endpfn))
351 add_active_range(nid, ei_startpfn, ei_endpfn);
Mel Gorman5cb248a2006-09-27 01:49:52 -0700352}
353
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100354/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 * Add a memory region to the kernel e820 map.
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357void __init add_memory_region(unsigned long start, unsigned long size, int type)
358{
359 int x = e820.nr_map;
360
361 if (x == E820MAX) {
362 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
363 return;
364 }
365
366 e820.map[x].addr = start;
367 e820.map[x].size = size;
368 e820.map[x].type = type;
369 e820.nr_map++;
370}
371
David Rientjesa7e96622007-07-21 17:11:29 +0200372/*
373 * Find the hole size (in bytes) in the memory range.
374 * @start: starting address of the memory range to scan
375 * @end: ending address of the memory range to scan
376 */
377unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
378{
379 unsigned long start_pfn = start >> PAGE_SHIFT;
380 unsigned long end_pfn = end >> PAGE_SHIFT;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100381 unsigned long ei_startpfn, ei_endpfn, ram = 0;
David Rientjesa7e96622007-07-21 17:11:29 +0200382 int i;
383
384 for (i = 0; i < e820.nr_map; i++) {
385 if (e820_find_active_region(&e820.map[i],
386 start_pfn, end_pfn,
387 &ei_startpfn, &ei_endpfn))
388 ram += ei_endpfn - ei_startpfn;
389 }
390 return end - start - (ram << PAGE_SHIFT);
391}
392
Adrian Bunk013d23e2008-01-30 13:30:30 +0100393static void __init e820_print_map(char *who)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 int i;
396
397 for (i = 0; i < e820.nr_map; i++) {
Dan Aloni5a3ece72007-07-21 17:11:37 +0200398 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100399 (unsigned long long) e820.map[i].addr,
400 (unsigned long long)
401 (e820.map[i].addr + e820.map[i].size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 switch (e820.map[i].type) {
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100403 case E820_RAM:
404 printk(KERN_CONT "(usable)\n");
405 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 case E820_RESERVED:
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100407 printk(KERN_CONT "(reserved)\n");
408 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 case E820_ACPI:
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100410 printk(KERN_CONT "(ACPI data)\n");
411 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 case E820_NVS:
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100413 printk(KERN_CONT "(ACPI NVS)\n");
414 break;
415 default:
416 printk(KERN_CONT "type %u\n", e820.map[i].type);
417 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419 }
420}
421
422/*
423 * Sanitize the BIOS e820 map.
424 *
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100425 * Some e820 responses include overlapping entries. The following
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * replaces the original e820 map with a new one, removing overlaps.
427 *
428 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100429static int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct change_member {
432 struct e820entry *pbios; /* pointer to original bios entry */
433 unsigned long long addr; /* address for this change point */
434 };
435 static struct change_member change_point_list[2*E820MAX] __initdata;
436 static struct change_member *change_point[2*E820MAX] __initdata;
437 static struct e820entry *overlap_list[E820MAX] __initdata;
438 static struct e820entry new_bios[E820MAX] __initdata;
439 struct change_member *change_tmp;
440 unsigned long current_type, last_type;
441 unsigned long long last_addr;
442 int chgidx, still_changing;
443 int overlap_entries;
444 int new_bios_entry;
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700445 int old_nr, new_nr, chg_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 int i;
447
448 /*
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100449 Visually we're performing the following
450 (1,2,3,4 = memory types)...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 Sample memory map (w/overlaps):
453 ____22__________________
454 ______________________4_
455 ____1111________________
456 _44_____________________
457 11111111________________
458 ____________________33__
459 ___________44___________
460 __________33333_________
461 ______________22________
462 ___________________2222_
463 _________111111111______
464 _____________________11_
465 _________________4______
466
467 Sanitized equivalent (no overlap):
468 1_______________________
469 _44_____________________
470 ___1____________________
471 ____22__________________
472 ______11________________
473 _________1______________
474 __________3_____________
475 ___________44___________
476 _____________33_________
477 _______________2________
478 ________________1_______
479 _________________4______
480 ___________________2____
481 ____________________33__
482 ______________________4_
483 */
484
485 /* if there's only one memory region, don't bother */
486 if (*pnr_map < 2)
487 return -1;
488
489 old_nr = *pnr_map;
490
491 /* bail out if we find any unreasonable addresses in bios map */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100492 for (i = 0; i < old_nr; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
494 return -1;
495
496 /* create pointers for initial change-point information (for sorting) */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100497 for (i = 0; i < 2 * old_nr; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 change_point[i] = &change_point_list[i];
499
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700500 /* record all known change-points (starting and ending addresses),
501 omitting those that are for empty memory regions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 chgidx = 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100503 for (i = 0; i < old_nr; i++) {
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700504 if (biosmap[i].size != 0) {
505 change_point[chgidx]->addr = biosmap[i].addr;
506 change_point[chgidx++]->pbios = &biosmap[i];
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100507 change_point[chgidx]->addr = biosmap[i].addr +
508 biosmap[i].size;
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700509 change_point[chgidx++]->pbios = &biosmap[i];
510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700512 chg_nr = chgidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 /* sort change-point list by memory addresses (low -> high) */
515 still_changing = 1;
516 while (still_changing) {
517 still_changing = 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100518 for (i = 1; i < chg_nr; i++) {
519 unsigned long long curaddr, lastaddr;
520 unsigned long long curpbaddr, lastpbaddr;
521
522 curaddr = change_point[i]->addr;
523 lastaddr = change_point[i - 1]->addr;
524 curpbaddr = change_point[i]->pbios->addr;
525 lastpbaddr = change_point[i - 1]->pbios->addr;
526
527 /*
528 * swap entries, when:
529 *
530 * curaddr > lastaddr or
531 * curaddr == lastaddr and curaddr == curpbaddr and
532 * lastaddr != lastpbaddr
533 */
534 if (curaddr < lastaddr ||
535 (curaddr == lastaddr && curaddr == curpbaddr &&
536 lastaddr != lastpbaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 change_tmp = change_point[i];
538 change_point[i] = change_point[i-1];
539 change_point[i-1] = change_tmp;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100540 still_changing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542 }
543 }
544
545 /* create a new bios memory map, removing overlaps */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100546 overlap_entries = 0; /* number of entries in the overlap table */
547 new_bios_entry = 0; /* index for creating new bios map entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 last_type = 0; /* start with undefined memory type */
549 last_addr = 0; /* start with 0 as last starting address */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* loop through change-points, determining affect on the new bios map */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100552 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* keep track of all overlapping bios entries */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100554 if (change_point[chgidx]->addr ==
555 change_point[chgidx]->pbios->addr) {
556 /*
557 * add map entry to overlap list (> 1 entry
558 * implies an overlap)
559 */
560 overlap_list[overlap_entries++] =
561 change_point[chgidx]->pbios;
562 } else {
563 /*
564 * remove entry from list (order independent,
565 * so swap with last)
566 */
567 for (i = 0; i < overlap_entries; i++) {
568 if (overlap_list[i] ==
569 change_point[chgidx]->pbios)
570 overlap_list[i] =
571 overlap_list[overlap_entries-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573 overlap_entries--;
574 }
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100575 /*
576 * if there are overlapping entries, decide which
577 * "type" to use (larger value takes precedence --
578 * 1=usable, 2,3,4,4+=unusable)
579 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 current_type = 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100581 for (i = 0; i < overlap_entries; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (overlap_list[i]->type > current_type)
583 current_type = overlap_list[i]->type;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100584 /*
585 * continue building up new bios map based on this
586 * information
587 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (current_type != last_type) {
589 if (last_type != 0) {
590 new_bios[new_bios_entry].size =
591 change_point[chgidx]->addr - last_addr;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100592 /*
593 * move forward only if the new size
594 * was non-zero
595 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (new_bios[new_bios_entry].size != 0)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100597 /*
598 * no more space left for new
599 * bios entries ?
600 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (++new_bios_entry >= E820MAX)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100602 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604 if (current_type != 0) {
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100605 new_bios[new_bios_entry].addr =
606 change_point[chgidx]->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 new_bios[new_bios_entry].type = current_type;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100608 last_addr = change_point[chgidx]->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610 last_type = current_type;
611 }
612 }
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100613 /* retain count for new bios entries */
614 new_nr = new_bios_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 /* copy new bios mapping into original location */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100617 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 *pnr_map = new_nr;
619
620 return 0;
621}
622
623/*
624 * Copy the BIOS e820 map into a safe place.
625 *
626 * Sanity-check it while we're at it..
627 *
628 * If we're lucky and live on a modern system, the setup code
629 * will have given us a memory map that we can use to properly
630 * set up memory. If we aren't, we'll fake a memory map.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100632static int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 /* Only one memory region (or negative)? Ignore it */
635 if (nr_map < 2)
636 return -1;
637
638 do {
639 unsigned long start = biosmap->addr;
640 unsigned long size = biosmap->size;
641 unsigned long end = start + size;
642 unsigned long type = biosmap->type;
643
644 /* Overflow in 64 bits? Ignore the memory map. */
645 if (start > end)
646 return -1;
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 add_memory_region(start, size, type);
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100649 } while (biosmap++, --nr_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return 0;
651}
652
Adrian Bunk013d23e2008-01-30 13:30:30 +0100653static void early_panic(char *msg)
Andi Kleen8380aab2006-09-26 10:52:37 +0200654{
655 early_printk(msg);
656 panic(msg);
657}
658
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100659/* We're not void only for x86 32-bit compat */
660char * __init machine_specific_memory_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100662 char *who = "BIOS-e820";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /*
664 * Try to copy the BIOS-supplied E820-map.
665 *
666 * Otherwise fake a memory map; one section from 0k->640k,
667 * the next section from 1mb->appropriate_mem_k
668 */
H. Peter Anvin30c82642007-10-15 17:13:22 -0700669 sanitize_e820_map(boot_params.e820_map, &boot_params.e820_entries);
670 if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
Andi Kleen8380aab2006-09-26 10:52:37 +0200671 early_panic("Cannot find a valid memory map");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100673 e820_print_map(who);
674
675 /* In case someone cares... */
676 return who;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200679static int __init parse_memopt(char *p)
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800680{
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200681 if (!p)
682 return -EINVAL;
683 end_user_pfn = memparse(p, &p);
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100684 end_user_pfn >>= PAGE_SHIFT;
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200685 return 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100686}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200687early_param("mem", parse_memopt);
688
689static int userdef __initdata;
690
691static int __init parse_memmap_opt(char *p)
692{
693 char *oldp;
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800694 unsigned long long start_at, mem_size;
695
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200696 if (!strcmp(p, "exactmap")) {
697#ifdef CONFIG_CRASH_DUMP
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100698 /*
699 * If we are doing a crash dump, we still need to know
700 * the real mem size before original memory map is
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200701 * reset.
702 */
Magnus Damm15803a42006-11-14 16:57:46 +0100703 e820_register_active_regions(0, 0, -1UL);
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200704 saved_max_pfn = e820_end_of_ram();
Magnus Damm15803a42006-11-14 16:57:46 +0100705 remove_all_active_ranges();
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200706#endif
707 end_pfn_map = 0;
708 e820.nr_map = 0;
709 userdef = 1;
710 return 0;
711 }
712
713 oldp = p;
714 mem_size = memparse(p, &p);
715 if (p == oldp)
716 return -EINVAL;
Vladimir Bereznikerb3ca74a2008-01-30 13:30:46 +0100717
718 userdef = 1;
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800719 if (*p == '@') {
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200720 start_at = memparse(p+1, &p);
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800721 add_memory_region(start_at, mem_size, E820_RAM);
722 } else if (*p == '#') {
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200723 start_at = memparse(p+1, &p);
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800724 add_memory_region(start_at, mem_size, E820_ACPI);
725 } else if (*p == '$') {
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200726 start_at = memparse(p+1, &p);
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800727 add_memory_region(start_at, mem_size, E820_RESERVED);
728 } else {
729 end_user_pfn = (mem_size >> PAGE_SHIFT);
730 }
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200731 return *p == '\0' ? 0 : -EINVAL;
732}
733early_param("memmap", parse_memmap_opt);
734
Sam Ravnborg43999d92007-03-16 21:07:36 +0100735void __init finish_e820_parsing(void)
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200736{
737 if (userdef) {
Vladimir Bereznikerb3ca74a2008-01-30 13:30:46 +0100738 char nr = e820.nr_map;
739
740 if (sanitize_e820_map(e820.map, &nr) < 0)
741 early_panic("Invalid user supplied memory map");
742 e820.nr_map = nr;
743
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200744 printk(KERN_INFO "user-defined physical RAM map:\n");
745 e820_print_map("user");
746 }
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800747}
748
Yinghai Lu5dca6a12008-03-18 16:44:19 -0700749void __init update_memory_range(u64 start, u64 size, unsigned old_type,
750 unsigned new_type)
751{
752 int i;
753
754 BUG_ON(old_type == new_type);
755
756 for (i = 0; i < e820.nr_map; i++) {
757 struct e820entry *ei = &e820.map[i];
758 u64 final_start, final_end;
759 if (ei->type != old_type)
760 continue;
761 /* totally covered? */
762 if (ei->addr >= start && ei->size <= size) {
763 ei->type = new_type;
764 continue;
765 }
766 /* partially covered */
767 final_start = max(start, ei->addr);
768 final_end = min(start + size, ei->addr + ei->size);
769 if (final_start >= final_end)
770 continue;
771 add_memory_region(final_start, final_end - final_start,
772 new_type);
773 }
774}
775
Yinghai Luaaf23042008-01-30 13:33:09 +0100776void __init update_e820(void)
777{
778 u8 nr_map;
779
780 nr_map = e820.nr_map;
781 if (sanitize_e820_map(e820.map, &nr_map))
782 return;
783 e820.nr_map = nr_map;
784 printk(KERN_INFO "modified physical RAM map:\n");
785 e820_print_map("modified");
786}
787
Andi Kleena1e97782005-04-16 15:25:12 -0700788unsigned long pci_mem_start = 0xaeedbabe;
Andi Kleen2ee60e172006-06-26 13:59:44 +0200789EXPORT_SYMBOL(pci_mem_start);
Andi Kleena1e97782005-04-16 15:25:12 -0700790
791/*
792 * Search for the biggest gap in the low 32 bits of the e820
793 * memory space. We pass this space to PCI to assign MMIO resources
794 * for hotplug or unconfigured devices in.
795 * Hopefully the BIOS let enough space left.
796 */
797__init void e820_setup_gap(void)
798{
Daniel Ritzf0eca962005-09-09 00:57:14 +0200799 unsigned long gapstart, gapsize, round;
Andi Kleena1e97782005-04-16 15:25:12 -0700800 unsigned long last;
801 int i;
802 int found = 0;
803
804 last = 0x100000000ull;
805 gapstart = 0x10000000;
806 gapsize = 0x400000;
807 i = e820.nr_map;
808 while (--i >= 0) {
809 unsigned long long start = e820.map[i].addr;
810 unsigned long long end = start + e820.map[i].size;
811
812 /*
813 * Since "last" is at most 4GB, we know we'll
814 * fit in 32 bits if this condition is true
815 */
816 if (last > end) {
817 unsigned long gap = last - end;
818
819 if (gap > gapsize) {
820 gapsize = gap;
821 gapstart = end;
822 found = 1;
823 }
824 }
825 if (start < last)
826 last = start;
827 }
828
829 if (!found) {
830 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100831 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
832 "address range\n"
833 KERN_ERR "PCI: Unassigned devices with 32bit resource "
834 "registers may break!\n");
Andi Kleena1e97782005-04-16 15:25:12 -0700835 }
836
837 /*
Daniel Ritzf0eca962005-09-09 00:57:14 +0200838 * See how much we want to round up: start off with
839 * rounding to the next 1MB area.
Andi Kleena1e97782005-04-16 15:25:12 -0700840 */
Daniel Ritzf0eca962005-09-09 00:57:14 +0200841 round = 0x100000;
842 while ((gapsize >> 4) > round)
843 round += round;
844 /* Fun with two's complement */
845 pci_mem_start = (gapstart + round) & -round;
Andi Kleena1e97782005-04-16 15:25:12 -0700846
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100847 printk(KERN_INFO
848 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
849 pci_mem_start, gapstart, gapsize);
Andi Kleena1e97782005-04-16 15:25:12 -0700850}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -0700851
852int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
853{
854 int i;
855
856 if (slot < 0 || slot >= e820.nr_map)
857 return -1;
858 for (i = slot; i < e820.nr_map; i++) {
859 if (e820.map[i].type != E820_RAM)
860 continue;
861 break;
862 }
863 if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
864 return -1;
865 *addr = e820.map[i].addr;
866 *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
867 max_pfn << PAGE_SHIFT) - *addr;
868 return i + 1;
869}