blob: 4509757844eb14a370fbb219837f4361ceea027d [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
Yinghai Lu272b9ca2008-03-20 23:58:33 -0700117/* Check for already reserved areas */
118static inline int
119bad_addr_size(unsigned long *addrp, unsigned long *sizep, unsigned long align)
120{
121 int i;
122 unsigned long addr = *addrp, last;
123 unsigned long size = *sizep;
124 int changed = 0;
125again:
126 last = addr + size;
127 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
128 struct early_res *r = &early_res[i];
129 if (last > r->start && addr < r->start) {
130 size = r->start - addr;
131 changed = 1;
132 goto again;
133 }
134 if (last > r->end && addr < r->end) {
135 addr = round_up(r->end, align);
136 size = last - addr;
137 changed = 1;
138 goto again;
139 }
140 if (last <= r->end && addr >= r->start) {
141 (*sizep)++;
142 return 0;
143 }
144 }
145 if (changed) {
146 *addrp = addr;
147 *sizep = size;
148 }
149 return changed;
150}
Arjan van de Ven95222362006-04-07 19:49:27 +0200151/*
152 * This function checks if any part of the range <start,end> is mapped
153 * with type.
154 */
Jan Beulichb92e9fa2007-05-02 19:27:11 +0200155int
Arjan van de Veneee5a9f2006-04-07 19:49:24 +0200156e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100157{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 int i;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100159
160 for (i = 0; i < e820.nr_map; i++) {
161 struct e820entry *ei = &e820.map[i];
162
163 if (type && ei->type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 continue;
Eric W. Biederman48c8b112005-09-06 15:16:20 -0700165 if (ei->addr >= end || ei->addr + ei->size <= start)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100166 continue;
167 return 1;
168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return 0;
170}
Jan Beulichb92e9fa2007-05-02 19:27:11 +0200171EXPORT_SYMBOL_GPL(e820_any_mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvalds79e453d2006-09-19 08:15:22 -0700173/*
174 * This function checks if the entire range <start,end> is mapped with type.
175 *
176 * Note: this function only works correct if the e820 table is sorted and
177 * not-overlapping, which is the case
178 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100179int __init e820_all_mapped(unsigned long start, unsigned long end,
180 unsigned type)
Linus Torvalds79e453d2006-09-19 08:15:22 -0700181{
182 int i;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100183
Linus Torvalds79e453d2006-09-19 08:15:22 -0700184 for (i = 0; i < e820.nr_map; i++) {
185 struct e820entry *ei = &e820.map[i];
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100186
Linus Torvalds79e453d2006-09-19 08:15:22 -0700187 if (type && ei->type != type)
188 continue;
189 /* is the region (part) in overlap with the current region ?*/
190 if (ei->addr >= end || ei->addr + ei->size <= start)
191 continue;
192
193 /* if the region is at the beginning of <start,end> we move
194 * start to the end of the region since it's ok until there
195 */
196 if (ei->addr <= start)
197 start = ei->addr + ei->size;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100198 /*
199 * if start is now at or beyond end, we're done, full
200 * coverage
201 */
Linus Torvalds79e453d2006-09-19 08:15:22 -0700202 if (start >= end)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100203 return 1;
Linus Torvalds79e453d2006-09-19 08:15:22 -0700204 }
205 return 0;
206}
207
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100208/*
Yinghai Lu24a5da72008-02-01 17:49:41 +0100209 * Find a free area with specified alignment in a specific range.
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100210 */
211unsigned long __init find_e820_area(unsigned long start, unsigned long end,
Yinghai Lu48c508b2008-04-17 17:40:45 +0200212 unsigned long size, unsigned long align)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100213{
214 int i;
215
216 for (i = 0; i < e820.nr_map; i++) {
217 struct e820entry *ei = &e820.map[i];
Yinghai Lu48c508b2008-04-17 17:40:45 +0200218 unsigned long addr, last;
219 unsigned long ei_last;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100220
221 if (ei->type != E820_RAM)
222 continue;
Yinghai Lu48c508b2008-04-17 17:40:45 +0200223 addr = round_up(ei->addr, align);
224 ei_last = ei->addr + ei->size;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100225 if (addr < start)
Yinghai Lu48c508b2008-04-17 17:40:45 +0200226 addr = round_up(start, align);
Yinghai Lu272b9ca2008-03-20 23:58:33 -0700227 if (addr >= ei_last)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100228 continue;
Yinghai Lu48c508b2008-04-17 17:40:45 +0200229 while (bad_addr(&addr, size, align) && addr+size <= ei_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 ;
Yinghai Lu24a5da72008-02-01 17:49:41 +0100231 last = addr + size;
Yinghai Lu48c508b2008-04-17 17:40:45 +0200232 if (last > ei_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 continue;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100234 if (last > end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 continue;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100236 return addr;
237 }
238 return -1UL;
239}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241/*
Yinghai Lu272b9ca2008-03-20 23:58:33 -0700242 * Find next free range after *start
243 */
244unsigned long __init find_e820_area_size(unsigned long start, unsigned long *sizep, unsigned long align)
245{
246 int i;
247
248 for (i = 0; i < e820.nr_map; i++) {
249 struct e820entry *ei = &e820.map[i];
250 unsigned long addr, last;
251 unsigned long ei_last;
252
253 if (ei->type != E820_RAM)
254 continue;
255 addr = round_up(ei->addr, align);
256 ei_last = ei->addr + ei->size;
257// printk(KERN_DEBUG "find_e820_area_size : e820 %d [%llx, %lx]\n", i, ei->addr, ei_last);
258 if (addr < start)
259 addr = round_up(start, align);
260// printk(KERN_DEBUG "find_e820_area_size : 0 [%lx, %lx]\n", addr, ei_last);
261 if (addr >= ei_last)
262 continue;
263 *sizep = ei_last - addr;
264 while (bad_addr_size(&addr, sizep, align) && addr+ *sizep <= ei_last)
265 ;
266 last = addr + *sizep;
267// printk(KERN_DEBUG "find_e820_area_size : 1 [%lx, %lx]\n", addr, last);
268 if (last > ei_last)
269 continue;
270 return addr;
271 }
272 return -1UL;
273
274}
275/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 * Find the highest page frame number we have available
277 */
278unsigned long __init e820_end_of_ram(void)
279{
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100280 unsigned long end_pfn;
281
Mel Gorman5cb248a2006-09-27 01:49:52 -0700282 end_pfn = find_max_pfn_with_active_regions();
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100283
284 if (end_pfn > end_pfn_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 end_pfn_map = end_pfn;
286 if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
287 end_pfn_map = MAXMEM>>PAGE_SHIFT;
288 if (end_pfn > end_user_pfn)
289 end_pfn = end_user_pfn;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100290 if (end_pfn > end_pfn_map)
291 end_pfn = end_pfn_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100293 printk(KERN_INFO "end_pfn_map = %lu\n", end_pfn_map);
294 return end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Andi Kleen485761b2005-08-26 18:34:10 -0700297/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * Mark e820 reserved areas as busy for the resource manager.
299 */
Yinghai Lu3def3d62008-02-22 17:07:16 -0800300void __init e820_reserve_resources(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 int i;
303 for (i = 0; i < e820.nr_map; i++) {
304 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 res = alloc_bootmem_low(sizeof(struct resource));
306 switch (e820.map[i].type) {
307 case E820_RAM: res->name = "System RAM"; break;
308 case E820_ACPI: res->name = "ACPI Tables"; break;
309 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
310 default: res->name = "reserved";
311 }
312 res->start = e820.map[i].addr;
313 res->end = res->start + e820.map[i].size - 1;
314 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Yinghai Lu3def3d62008-02-22 17:07:16 -0800315 insert_resource(&iomem_resource, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317}
318
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -0700319/*
320 * Find the ranges of physical addresses that do not correspond to
321 * e820 RAM areas and mark the corresponding pages as nosave for software
322 * suspend and suspend to RAM.
323 *
324 * This function requires the e820 map to be sorted and without any
325 * overlapping entries and assumes the first e820 area to be RAM.
326 */
327void __init e820_mark_nosave_regions(void)
328{
329 int i;
330 unsigned long paddr;
331
332 paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
333 for (i = 1; i < e820.nr_map; i++) {
334 struct e820entry *ei = &e820.map[i];
335
336 if (paddr < ei->addr)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700337 register_nosave_region(PFN_DOWN(paddr),
338 PFN_UP(ei->addr));
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -0700339
340 paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
341 if (ei->type != E820_RAM)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700342 register_nosave_region(PFN_UP(ei->addr),
343 PFN_DOWN(paddr));
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -0700344
345 if (paddr >= (end_pfn << PAGE_SHIFT))
346 break;
347 }
348}
349
David Rientjes3af044e2007-07-21 17:10:31 +0200350/*
351 * Finds an active region in the address range from start_pfn to end_pfn and
352 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
353 */
354static int __init e820_find_active_region(const struct e820entry *ei,
355 unsigned long start_pfn,
356 unsigned long end_pfn,
357 unsigned long *ei_startpfn,
358 unsigned long *ei_endpfn)
359{
360 *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
361 *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
362
363 /* Skip map entries smaller than a page */
364 if (*ei_startpfn >= *ei_endpfn)
365 return 0;
366
367 /* Check if end_pfn_map should be updated */
368 if (ei->type != E820_RAM && *ei_endpfn > end_pfn_map)
369 end_pfn_map = *ei_endpfn;
370
371 /* Skip if map is outside the node */
372 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
373 *ei_startpfn >= end_pfn)
374 return 0;
375
376 /* Check for overlaps */
377 if (*ei_startpfn < start_pfn)
378 *ei_startpfn = start_pfn;
379 if (*ei_endpfn > end_pfn)
380 *ei_endpfn = end_pfn;
381
382 /* Obey end_user_pfn to save on memmap */
383 if (*ei_startpfn >= end_user_pfn)
384 return 0;
385 if (*ei_endpfn > end_user_pfn)
386 *ei_endpfn = end_user_pfn;
387
388 return 1;
389}
390
Mel Gorman5cb248a2006-09-27 01:49:52 -0700391/* Walk the e820 map and register active regions within a node */
392void __init
393e820_register_active_regions(int nid, unsigned long start_pfn,
394 unsigned long end_pfn)
395{
David Rientjes3af044e2007-07-21 17:10:31 +0200396 unsigned long ei_startpfn;
397 unsigned long ei_endpfn;
Mel Gorman5cb248a2006-09-27 01:49:52 -0700398 int i;
Mel Gorman5cb248a2006-09-27 01:49:52 -0700399
David Rientjes3af044e2007-07-21 17:10:31 +0200400 for (i = 0; i < e820.nr_map; i++)
401 if (e820_find_active_region(&e820.map[i],
402 start_pfn, end_pfn,
403 &ei_startpfn, &ei_endpfn))
404 add_active_range(nid, ei_startpfn, ei_endpfn);
Mel Gorman5cb248a2006-09-27 01:49:52 -0700405}
406
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100407/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 * Add a memory region to the kernel e820 map.
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100409 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410void __init add_memory_region(unsigned long start, unsigned long size, int type)
411{
412 int x = e820.nr_map;
413
414 if (x == E820MAX) {
415 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
416 return;
417 }
418
419 e820.map[x].addr = start;
420 e820.map[x].size = size;
421 e820.map[x].type = type;
422 e820.nr_map++;
423}
424
David Rientjesa7e96622007-07-21 17:11:29 +0200425/*
426 * Find the hole size (in bytes) in the memory range.
427 * @start: starting address of the memory range to scan
428 * @end: ending address of the memory range to scan
429 */
430unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
431{
432 unsigned long start_pfn = start >> PAGE_SHIFT;
433 unsigned long end_pfn = end >> PAGE_SHIFT;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100434 unsigned long ei_startpfn, ei_endpfn, ram = 0;
David Rientjesa7e96622007-07-21 17:11:29 +0200435 int i;
436
437 for (i = 0; i < e820.nr_map; i++) {
438 if (e820_find_active_region(&e820.map[i],
439 start_pfn, end_pfn,
440 &ei_startpfn, &ei_endpfn))
441 ram += ei_endpfn - ei_startpfn;
442 }
443 return end - start - (ram << PAGE_SHIFT);
444}
445
Adrian Bunk013d23e2008-01-30 13:30:30 +0100446static void __init e820_print_map(char *who)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 int i;
449
450 for (i = 0; i < e820.nr_map; i++) {
Dan Aloni5a3ece72007-07-21 17:11:37 +0200451 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100452 (unsigned long long) e820.map[i].addr,
453 (unsigned long long)
454 (e820.map[i].addr + e820.map[i].size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 switch (e820.map[i].type) {
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100456 case E820_RAM:
457 printk(KERN_CONT "(usable)\n");
458 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 case E820_RESERVED:
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100460 printk(KERN_CONT "(reserved)\n");
461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 case E820_ACPI:
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100463 printk(KERN_CONT "(ACPI data)\n");
464 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 case E820_NVS:
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100466 printk(KERN_CONT "(ACPI NVS)\n");
467 break;
468 default:
469 printk(KERN_CONT "type %u\n", e820.map[i].type);
470 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472 }
473}
474
475/*
476 * Sanitize the BIOS e820 map.
477 *
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100478 * Some e820 responses include overlapping entries. The following
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 * replaces the original e820 map with a new one, removing overlaps.
480 *
481 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100482static int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 struct change_member {
485 struct e820entry *pbios; /* pointer to original bios entry */
486 unsigned long long addr; /* address for this change point */
487 };
488 static struct change_member change_point_list[2*E820MAX] __initdata;
489 static struct change_member *change_point[2*E820MAX] __initdata;
490 static struct e820entry *overlap_list[E820MAX] __initdata;
491 static struct e820entry new_bios[E820MAX] __initdata;
492 struct change_member *change_tmp;
493 unsigned long current_type, last_type;
494 unsigned long long last_addr;
495 int chgidx, still_changing;
496 int overlap_entries;
497 int new_bios_entry;
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700498 int old_nr, new_nr, chg_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 int i;
500
501 /*
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100502 Visually we're performing the following
503 (1,2,3,4 = memory types)...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 Sample memory map (w/overlaps):
506 ____22__________________
507 ______________________4_
508 ____1111________________
509 _44_____________________
510 11111111________________
511 ____________________33__
512 ___________44___________
513 __________33333_________
514 ______________22________
515 ___________________2222_
516 _________111111111______
517 _____________________11_
518 _________________4______
519
520 Sanitized equivalent (no overlap):
521 1_______________________
522 _44_____________________
523 ___1____________________
524 ____22__________________
525 ______11________________
526 _________1______________
527 __________3_____________
528 ___________44___________
529 _____________33_________
530 _______________2________
531 ________________1_______
532 _________________4______
533 ___________________2____
534 ____________________33__
535 ______________________4_
536 */
537
538 /* if there's only one memory region, don't bother */
539 if (*pnr_map < 2)
540 return -1;
541
542 old_nr = *pnr_map;
543
544 /* bail out if we find any unreasonable addresses in bios map */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100545 for (i = 0; i < old_nr; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
547 return -1;
548
549 /* create pointers for initial change-point information (for sorting) */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100550 for (i = 0; i < 2 * old_nr; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 change_point[i] = &change_point_list[i];
552
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700553 /* record all known change-points (starting and ending addresses),
554 omitting those that are for empty memory regions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 chgidx = 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100556 for (i = 0; i < old_nr; i++) {
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700557 if (biosmap[i].size != 0) {
558 change_point[chgidx]->addr = biosmap[i].addr;
559 change_point[chgidx++]->pbios = &biosmap[i];
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100560 change_point[chgidx]->addr = biosmap[i].addr +
561 biosmap[i].size;
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700562 change_point[chgidx++]->pbios = &biosmap[i];
563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700565 chg_nr = chgidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 /* sort change-point list by memory addresses (low -> high) */
568 still_changing = 1;
569 while (still_changing) {
570 still_changing = 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100571 for (i = 1; i < chg_nr; i++) {
572 unsigned long long curaddr, lastaddr;
573 unsigned long long curpbaddr, lastpbaddr;
574
575 curaddr = change_point[i]->addr;
576 lastaddr = change_point[i - 1]->addr;
577 curpbaddr = change_point[i]->pbios->addr;
578 lastpbaddr = change_point[i - 1]->pbios->addr;
579
580 /*
581 * swap entries, when:
582 *
583 * curaddr > lastaddr or
584 * curaddr == lastaddr and curaddr == curpbaddr and
585 * lastaddr != lastpbaddr
586 */
587 if (curaddr < lastaddr ||
588 (curaddr == lastaddr && curaddr == curpbaddr &&
589 lastaddr != lastpbaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 change_tmp = change_point[i];
591 change_point[i] = change_point[i-1];
592 change_point[i-1] = change_tmp;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100593 still_changing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595 }
596 }
597
598 /* create a new bios memory map, removing overlaps */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100599 overlap_entries = 0; /* number of entries in the overlap table */
600 new_bios_entry = 0; /* index for creating new bios map entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 last_type = 0; /* start with undefined memory type */
602 last_addr = 0; /* start with 0 as last starting address */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /* loop through change-points, determining affect on the new bios map */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100605 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /* keep track of all overlapping bios entries */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100607 if (change_point[chgidx]->addr ==
608 change_point[chgidx]->pbios->addr) {
609 /*
610 * add map entry to overlap list (> 1 entry
611 * implies an overlap)
612 */
613 overlap_list[overlap_entries++] =
614 change_point[chgidx]->pbios;
615 } else {
616 /*
617 * remove entry from list (order independent,
618 * so swap with last)
619 */
620 for (i = 0; i < overlap_entries; i++) {
621 if (overlap_list[i] ==
622 change_point[chgidx]->pbios)
623 overlap_list[i] =
624 overlap_list[overlap_entries-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626 overlap_entries--;
627 }
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100628 /*
629 * if there are overlapping entries, decide which
630 * "type" to use (larger value takes precedence --
631 * 1=usable, 2,3,4,4+=unusable)
632 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 current_type = 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100634 for (i = 0; i < overlap_entries; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (overlap_list[i]->type > current_type)
636 current_type = overlap_list[i]->type;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100637 /*
638 * continue building up new bios map based on this
639 * information
640 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (current_type != last_type) {
642 if (last_type != 0) {
643 new_bios[new_bios_entry].size =
644 change_point[chgidx]->addr - last_addr;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100645 /*
646 * move forward only if the new size
647 * was non-zero
648 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (new_bios[new_bios_entry].size != 0)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100650 /*
651 * no more space left for new
652 * bios entries ?
653 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (++new_bios_entry >= E820MAX)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657 if (current_type != 0) {
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100658 new_bios[new_bios_entry].addr =
659 change_point[chgidx]->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 new_bios[new_bios_entry].type = current_type;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100661 last_addr = change_point[chgidx]->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663 last_type = current_type;
664 }
665 }
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100666 /* retain count for new bios entries */
667 new_nr = new_bios_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 /* copy new bios mapping into original location */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100670 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 *pnr_map = new_nr;
672
673 return 0;
674}
675
676/*
677 * Copy the BIOS e820 map into a safe place.
678 *
679 * Sanity-check it while we're at it..
680 *
681 * If we're lucky and live on a modern system, the setup code
682 * will have given us a memory map that we can use to properly
683 * set up memory. If we aren't, we'll fake a memory map.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100685static int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 /* Only one memory region (or negative)? Ignore it */
688 if (nr_map < 2)
689 return -1;
690
691 do {
Alexander van Heukelum320a6b22008-03-01 17:12:43 +0100692 u64 start = biosmap->addr;
693 u64 size = biosmap->size;
694 u64 end = start + size;
695 u32 type = biosmap->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 /* Overflow in 64 bits? Ignore the memory map. */
698 if (start > end)
699 return -1;
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 add_memory_region(start, size, type);
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100702 } while (biosmap++, --nr_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return 0;
704}
705
Adrian Bunk013d23e2008-01-30 13:30:30 +0100706static void early_panic(char *msg)
Andi Kleen8380aab2006-09-26 10:52:37 +0200707{
708 early_printk(msg);
709 panic(msg);
710}
711
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100712/* We're not void only for x86 32-bit compat */
713char * __init machine_specific_memory_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100715 char *who = "BIOS-e820";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /*
717 * Try to copy the BIOS-supplied E820-map.
718 *
719 * Otherwise fake a memory map; one section from 0k->640k,
720 * the next section from 1mb->appropriate_mem_k
721 */
H. Peter Anvin30c82642007-10-15 17:13:22 -0700722 sanitize_e820_map(boot_params.e820_map, &boot_params.e820_entries);
723 if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
Andi Kleen8380aab2006-09-26 10:52:37 +0200724 early_panic("Cannot find a valid memory map");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100726 e820_print_map(who);
727
728 /* In case someone cares... */
729 return who;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200732static int __init parse_memopt(char *p)
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800733{
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200734 if (!p)
735 return -EINVAL;
736 end_user_pfn = memparse(p, &p);
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100737 end_user_pfn >>= PAGE_SHIFT;
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200738 return 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100739}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200740early_param("mem", parse_memopt);
741
742static int userdef __initdata;
743
744static int __init parse_memmap_opt(char *p)
745{
746 char *oldp;
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800747 unsigned long long start_at, mem_size;
748
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200749 if (!strcmp(p, "exactmap")) {
750#ifdef CONFIG_CRASH_DUMP
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100751 /*
752 * If we are doing a crash dump, we still need to know
753 * the real mem size before original memory map is
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200754 * reset.
755 */
Magnus Damm15803a42006-11-14 16:57:46 +0100756 e820_register_active_regions(0, 0, -1UL);
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200757 saved_max_pfn = e820_end_of_ram();
Magnus Damm15803a42006-11-14 16:57:46 +0100758 remove_all_active_ranges();
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200759#endif
760 end_pfn_map = 0;
761 e820.nr_map = 0;
762 userdef = 1;
763 return 0;
764 }
765
766 oldp = p;
767 mem_size = memparse(p, &p);
768 if (p == oldp)
769 return -EINVAL;
Vladimir Bereznikerb3ca74a2008-01-30 13:30:46 +0100770
771 userdef = 1;
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800772 if (*p == '@') {
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200773 start_at = memparse(p+1, &p);
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800774 add_memory_region(start_at, mem_size, E820_RAM);
775 } else if (*p == '#') {
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200776 start_at = memparse(p+1, &p);
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800777 add_memory_region(start_at, mem_size, E820_ACPI);
778 } else if (*p == '$') {
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200779 start_at = memparse(p+1, &p);
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800780 add_memory_region(start_at, mem_size, E820_RESERVED);
781 } else {
782 end_user_pfn = (mem_size >> PAGE_SHIFT);
783 }
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200784 return *p == '\0' ? 0 : -EINVAL;
785}
786early_param("memmap", parse_memmap_opt);
787
Sam Ravnborg43999d92007-03-16 21:07:36 +0100788void __init finish_e820_parsing(void)
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200789{
790 if (userdef) {
Vladimir Bereznikerb3ca74a2008-01-30 13:30:46 +0100791 char nr = e820.nr_map;
792
793 if (sanitize_e820_map(e820.map, &nr) < 0)
794 early_panic("Invalid user supplied memory map");
795 e820.nr_map = nr;
796
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200797 printk(KERN_INFO "user-defined physical RAM map:\n");
798 e820_print_map("user");
799 }
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800800}
801
Yinghai Lu5dca6a12008-03-18 16:44:19 -0700802void __init update_memory_range(u64 start, u64 size, unsigned old_type,
803 unsigned new_type)
804{
805 int i;
806
807 BUG_ON(old_type == new_type);
808
809 for (i = 0; i < e820.nr_map; i++) {
810 struct e820entry *ei = &e820.map[i];
811 u64 final_start, final_end;
812 if (ei->type != old_type)
813 continue;
814 /* totally covered? */
815 if (ei->addr >= start && ei->size <= size) {
816 ei->type = new_type;
817 continue;
818 }
819 /* partially covered */
820 final_start = max(start, ei->addr);
821 final_end = min(start + size, ei->addr + ei->size);
822 if (final_start >= final_end)
823 continue;
824 add_memory_region(final_start, final_end - final_start,
825 new_type);
826 }
827}
828
Yinghai Luaaf23042008-01-30 13:33:09 +0100829void __init update_e820(void)
830{
831 u8 nr_map;
832
833 nr_map = e820.nr_map;
834 if (sanitize_e820_map(e820.map, &nr_map))
835 return;
836 e820.nr_map = nr_map;
837 printk(KERN_INFO "modified physical RAM map:\n");
838 e820_print_map("modified");
839}
840
Andi Kleena1e97782005-04-16 15:25:12 -0700841unsigned long pci_mem_start = 0xaeedbabe;
Andi Kleen2ee60e172006-06-26 13:59:44 +0200842EXPORT_SYMBOL(pci_mem_start);
Andi Kleena1e97782005-04-16 15:25:12 -0700843
844/*
845 * Search for the biggest gap in the low 32 bits of the e820
846 * memory space. We pass this space to PCI to assign MMIO resources
847 * for hotplug or unconfigured devices in.
848 * Hopefully the BIOS let enough space left.
849 */
850__init void e820_setup_gap(void)
851{
Daniel Ritzf0eca962005-09-09 00:57:14 +0200852 unsigned long gapstart, gapsize, round;
Andi Kleena1e97782005-04-16 15:25:12 -0700853 unsigned long last;
854 int i;
855 int found = 0;
856
857 last = 0x100000000ull;
858 gapstart = 0x10000000;
859 gapsize = 0x400000;
860 i = e820.nr_map;
861 while (--i >= 0) {
862 unsigned long long start = e820.map[i].addr;
863 unsigned long long end = start + e820.map[i].size;
864
865 /*
866 * Since "last" is at most 4GB, we know we'll
867 * fit in 32 bits if this condition is true
868 */
869 if (last > end) {
870 unsigned long gap = last - end;
871
872 if (gap > gapsize) {
873 gapsize = gap;
874 gapstart = end;
875 found = 1;
876 }
877 }
878 if (start < last)
879 last = start;
880 }
881
882 if (!found) {
883 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100884 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
885 "address range\n"
886 KERN_ERR "PCI: Unassigned devices with 32bit resource "
887 "registers may break!\n");
Andi Kleena1e97782005-04-16 15:25:12 -0700888 }
889
890 /*
Daniel Ritzf0eca962005-09-09 00:57:14 +0200891 * See how much we want to round up: start off with
892 * rounding to the next 1MB area.
Andi Kleena1e97782005-04-16 15:25:12 -0700893 */
Daniel Ritzf0eca962005-09-09 00:57:14 +0200894 round = 0x100000;
895 while ((gapsize >> 4) > round)
896 round += round;
897 /* Fun with two's complement */
898 pci_mem_start = (gapstart + round) & -round;
Andi Kleena1e97782005-04-16 15:25:12 -0700899
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100900 printk(KERN_INFO
901 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
902 pci_mem_start, gapstart, gapsize);
Andi Kleena1e97782005-04-16 15:25:12 -0700903}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -0700904
905int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
906{
907 int i;
908
909 if (slot < 0 || slot >= e820.nr_map)
910 return -1;
911 for (i = slot; i < e820.nr_map; i++) {
912 if (e820.map[i].type != E820_RAM)
913 continue;
914 break;
915 }
916 if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
917 return -1;
918 *addr = e820.map[i].addr;
919 *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
920 max_pfn << PAGE_SHIFT) - *addr;
921 return i + 1;
922}