blob: e510cfd5bb7129146e349833a00300e42d930f0e [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;
Andi Kleenf3591ff2005-09-13 11:35:28 +020037EXPORT_SYMBOL(end_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010039/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
41 * The direct mapping extends to end_pfn_map, so that we can directly access
42 * apertures, ACPI and other tables without having to play with fixmaps.
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010043 */
44unsigned long end_pfn_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010046/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * Last pfn which the user wants to use.
48 */
Jan Beulichcaff0712006-09-26 10:52:31 +020049static unsigned long __initdata end_user_pfn = MAXMEM>>PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Bernhard Walle00bf4092007-10-21 16:42:01 -070051extern struct resource code_resource, data_resource, bss_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010053/* Check for some hardcoded bad areas that early boot is not allowed to touch */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static inline int bad_addr(unsigned long *addrp, unsigned long size)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010055{
56 unsigned long addr = *addrp, last = addr + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 /* various gunk below that needed for SMP startup */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010059 if (addr < 0x8000) {
Vivek Goyal73bb8912006-10-21 18:37:01 +020060 *addrp = PAGE_ALIGN(0x8000);
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010061 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
63
64 /* direct mapping tables of the kernel */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010065 if (last >= table_start<<PAGE_SHIFT && addr < table_end<<PAGE_SHIFT) {
Vivek Goyal73bb8912006-10-21 18:37:01 +020066 *addrp = PAGE_ALIGN(table_end << PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return 1;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010070 /* initrd */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#ifdef CONFIG_BLK_DEV_INITRD
H. Peter Anvin30c82642007-10-15 17:13:22 -070072 if (boot_params.hdr.type_of_loader && boot_params.hdr.ramdisk_image) {
73 unsigned long ramdisk_image = boot_params.hdr.ramdisk_image;
74 unsigned long ramdisk_size = boot_params.hdr.ramdisk_size;
75 unsigned long ramdisk_end = ramdisk_image+ramdisk_size;
76
77 if (last >= ramdisk_image && addr < ramdisk_end) {
78 *addrp = PAGE_ALIGN(ramdisk_end);
79 return 1;
80 }
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#endif
Andi Kleendbf92722006-09-26 10:52:36 +020083 /* kernel code */
Vivek Goyal73bb8912006-10-21 18:37:01 +020084 if (last >= __pa_symbol(&_text) && addr < __pa_symbol(&_end)) {
85 *addrp = PAGE_ALIGN(__pa_symbol(&_end));
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return 1;
87 }
Andi Kleenac71d122006-05-08 15:17:28 +020088
89 if (last >= ebda_addr && addr < ebda_addr + ebda_size) {
Vivek Goyal73bb8912006-10-21 18:37:01 +020090 *addrp = PAGE_ALIGN(ebda_addr + ebda_size);
Andi Kleenac71d122006-05-08 15:17:28 +020091 return 1;
92 }
93
Amul Shah076422d2007-02-13 13:26:19 +010094#ifdef CONFIG_NUMA
95 /* NUMA memory to node map */
96 if (last >= nodemap_addr && addr < nodemap_addr + nodemap_size) {
97 *addrp = nodemap_addr + nodemap_size;
98 return 1;
99 }
100#endif
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100101 /* XXX ramdisk image here? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100103}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Arjan van de Ven95222362006-04-07 19:49:27 +0200105/*
106 * This function checks if any part of the range <start,end> is mapped
107 * with type.
108 */
Jan Beulichb92e9fa2007-05-02 19:27:11 +0200109int
Arjan van de Veneee5a9f2006-04-07 19:49:24 +0200110e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100111{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int i;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100113
114 for (i = 0; i < e820.nr_map; i++) {
115 struct e820entry *ei = &e820.map[i];
116
117 if (type && ei->type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 continue;
Eric W. Biederman48c8b112005-09-06 15:16:20 -0700119 if (ei->addr >= end || ei->addr + ei->size <= start)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100120 continue;
121 return 1;
122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return 0;
124}
Jan Beulichb92e9fa2007-05-02 19:27:11 +0200125EXPORT_SYMBOL_GPL(e820_any_mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvalds79e453d2006-09-19 08:15:22 -0700127/*
128 * This function checks if the entire range <start,end> is mapped with type.
129 *
130 * Note: this function only works correct if the e820 table is sorted and
131 * not-overlapping, which is the case
132 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100133int __init e820_all_mapped(unsigned long start, unsigned long end,
134 unsigned type)
Linus Torvalds79e453d2006-09-19 08:15:22 -0700135{
136 int i;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100137
Linus Torvalds79e453d2006-09-19 08:15:22 -0700138 for (i = 0; i < e820.nr_map; i++) {
139 struct e820entry *ei = &e820.map[i];
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100140
Linus Torvalds79e453d2006-09-19 08:15:22 -0700141 if (type && ei->type != type)
142 continue;
143 /* is the region (part) in overlap with the current region ?*/
144 if (ei->addr >= end || ei->addr + ei->size <= start)
145 continue;
146
147 /* if the region is at the beginning of <start,end> we move
148 * start to the end of the region since it's ok until there
149 */
150 if (ei->addr <= start)
151 start = ei->addr + ei->size;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100152 /*
153 * if start is now at or beyond end, we're done, full
154 * coverage
155 */
Linus Torvalds79e453d2006-09-19 08:15:22 -0700156 if (start >= end)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100157 return 1;
Linus Torvalds79e453d2006-09-19 08:15:22 -0700158 }
159 return 0;
160}
161
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100162/*
163 * Find a free area in a specific range.
164 */
165unsigned long __init find_e820_area(unsigned long start, unsigned long end,
166 unsigned size)
167{
168 int i;
169
170 for (i = 0; i < e820.nr_map; i++) {
171 struct e820entry *ei = &e820.map[i];
172 unsigned long addr = ei->addr, last;
173
174 if (ei->type != E820_RAM)
175 continue;
176 if (addr < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 addr = start;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100178 if (addr > ei->addr + ei->size)
179 continue;
Robert Hentosh7ca97c62006-05-30 22:48:00 +0200180 while (bad_addr(&addr, size) && addr+size <= ei->addr+ei->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 ;
Vivek Goyal73bb8912006-10-21 18:37:01 +0200182 last = PAGE_ALIGN(addr) + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (last > ei->addr + ei->size)
184 continue;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100185 if (last > end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 continue;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100187 return addr;
188 }
189 return -1UL;
190}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*
193 * Find the highest page frame number we have available
194 */
195unsigned long __init e820_end_of_ram(void)
196{
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100197 unsigned long end_pfn;
198
Mel Gorman5cb248a2006-09-27 01:49:52 -0700199 end_pfn = find_max_pfn_with_active_regions();
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100200
201 if (end_pfn > end_pfn_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 end_pfn_map = end_pfn;
203 if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
204 end_pfn_map = MAXMEM>>PAGE_SHIFT;
205 if (end_pfn > end_user_pfn)
206 end_pfn = end_user_pfn;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100207 if (end_pfn > end_pfn_map)
208 end_pfn = end_pfn_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100210 printk(KERN_INFO "end_pfn_map = %lu\n", end_pfn_map);
211 return end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Andi Kleen485761b2005-08-26 18:34:10 -0700214/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * Mark e820 reserved areas as busy for the resource manager.
216 */
217void __init e820_reserve_resources(void)
218{
219 int i;
220 for (i = 0; i < e820.nr_map; i++) {
221 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 res = alloc_bootmem_low(sizeof(struct resource));
223 switch (e820.map[i].type) {
224 case E820_RAM: res->name = "System RAM"; break;
225 case E820_ACPI: res->name = "ACPI Tables"; break;
226 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
227 default: res->name = "reserved";
228 }
229 res->start = e820.map[i].addr;
230 res->end = res->start + e820.map[i].size - 1;
231 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
232 request_resource(&iomem_resource, res);
233 if (e820.map[i].type == E820_RAM) {
234 /*
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100235 * We don't know which RAM region contains kernel data,
236 * so we try it repeatedly and let the resource manager
237 * test it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
239 request_resource(res, &code_resource);
240 request_resource(res, &data_resource);
Bernhard Walle00bf4092007-10-21 16:42:01 -0700241 request_resource(res, &bss_resource);
Eric W. Biederman5f5609d2005-06-25 14:58:04 -0700242#ifdef CONFIG_KEXEC
Bernhard Walle5c3391f2007-10-18 23:40:59 -0700243 if (crashk_res.start != crashk_res.end)
244 request_resource(res, &crashk_res);
Eric W. Biederman5f5609d2005-06-25 14:58:04 -0700245#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247 }
248}
249
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -0700250/*
251 * Find the ranges of physical addresses that do not correspond to
252 * e820 RAM areas and mark the corresponding pages as nosave for software
253 * suspend and suspend to RAM.
254 *
255 * This function requires the e820 map to be sorted and without any
256 * overlapping entries and assumes the first e820 area to be RAM.
257 */
258void __init e820_mark_nosave_regions(void)
259{
260 int i;
261 unsigned long paddr;
262
263 paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
264 for (i = 1; i < e820.nr_map; i++) {
265 struct e820entry *ei = &e820.map[i];
266
267 if (paddr < ei->addr)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700268 register_nosave_region(PFN_DOWN(paddr),
269 PFN_UP(ei->addr));
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -0700270
271 paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
272 if (ei->type != E820_RAM)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700273 register_nosave_region(PFN_UP(ei->addr),
274 PFN_DOWN(paddr));
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -0700275
276 if (paddr >= (end_pfn << PAGE_SHIFT))
277 break;
278 }
279}
280
David Rientjes3af044e2007-07-21 17:10:31 +0200281/*
282 * Finds an active region in the address range from start_pfn to end_pfn and
283 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
284 */
285static int __init e820_find_active_region(const struct e820entry *ei,
286 unsigned long start_pfn,
287 unsigned long end_pfn,
288 unsigned long *ei_startpfn,
289 unsigned long *ei_endpfn)
290{
291 *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
292 *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
293
294 /* Skip map entries smaller than a page */
295 if (*ei_startpfn >= *ei_endpfn)
296 return 0;
297
298 /* Check if end_pfn_map should be updated */
299 if (ei->type != E820_RAM && *ei_endpfn > end_pfn_map)
300 end_pfn_map = *ei_endpfn;
301
302 /* Skip if map is outside the node */
303 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
304 *ei_startpfn >= end_pfn)
305 return 0;
306
307 /* Check for overlaps */
308 if (*ei_startpfn < start_pfn)
309 *ei_startpfn = start_pfn;
310 if (*ei_endpfn > end_pfn)
311 *ei_endpfn = end_pfn;
312
313 /* Obey end_user_pfn to save on memmap */
314 if (*ei_startpfn >= end_user_pfn)
315 return 0;
316 if (*ei_endpfn > end_user_pfn)
317 *ei_endpfn = end_user_pfn;
318
319 return 1;
320}
321
Mel Gorman5cb248a2006-09-27 01:49:52 -0700322/* Walk the e820 map and register active regions within a node */
323void __init
324e820_register_active_regions(int nid, unsigned long start_pfn,
325 unsigned long end_pfn)
326{
David Rientjes3af044e2007-07-21 17:10:31 +0200327 unsigned long ei_startpfn;
328 unsigned long ei_endpfn;
Mel Gorman5cb248a2006-09-27 01:49:52 -0700329 int i;
Mel Gorman5cb248a2006-09-27 01:49:52 -0700330
David Rientjes3af044e2007-07-21 17:10:31 +0200331 for (i = 0; i < e820.nr_map; i++)
332 if (e820_find_active_region(&e820.map[i],
333 start_pfn, end_pfn,
334 &ei_startpfn, &ei_endpfn))
335 add_active_range(nid, ei_startpfn, ei_endpfn);
Mel Gorman5cb248a2006-09-27 01:49:52 -0700336}
337
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100338/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * Add a memory region to the kernel e820 map.
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100340 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341void __init add_memory_region(unsigned long start, unsigned long size, int type)
342{
343 int x = e820.nr_map;
344
345 if (x == E820MAX) {
346 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
347 return;
348 }
349
350 e820.map[x].addr = start;
351 e820.map[x].size = size;
352 e820.map[x].type = type;
353 e820.nr_map++;
354}
355
David Rientjesa7e96622007-07-21 17:11:29 +0200356/*
357 * Find the hole size (in bytes) in the memory range.
358 * @start: starting address of the memory range to scan
359 * @end: ending address of the memory range to scan
360 */
361unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
362{
363 unsigned long start_pfn = start >> PAGE_SHIFT;
364 unsigned long end_pfn = end >> PAGE_SHIFT;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100365 unsigned long ei_startpfn, ei_endpfn, ram = 0;
David Rientjesa7e96622007-07-21 17:11:29 +0200366 int i;
367
368 for (i = 0; i < e820.nr_map; i++) {
369 if (e820_find_active_region(&e820.map[i],
370 start_pfn, end_pfn,
371 &ei_startpfn, &ei_endpfn))
372 ram += ei_endpfn - ei_startpfn;
373 }
374 return end - start - (ram << PAGE_SHIFT);
375}
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377void __init e820_print_map(char *who)
378{
379 int i;
380
381 for (i = 0; i < e820.nr_map; i++) {
Dan Aloni5a3ece72007-07-21 17:11:37 +0200382 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100383 (unsigned long long) e820.map[i].addr,
384 (unsigned long long)
385 (e820.map[i].addr + e820.map[i].size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 switch (e820.map[i].type) {
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100387 case E820_RAM:
388 printk(KERN_CONT "(usable)\n");
389 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 case E820_RESERVED:
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100391 printk(KERN_CONT "(reserved)\n");
392 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 case E820_ACPI:
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100394 printk(KERN_CONT "(ACPI data)\n");
395 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 case E820_NVS:
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100397 printk(KERN_CONT "(ACPI NVS)\n");
398 break;
399 default:
400 printk(KERN_CONT "type %u\n", e820.map[i].type);
401 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403 }
404}
405
406/*
407 * Sanitize the BIOS e820 map.
408 *
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100409 * Some e820 responses include overlapping entries. The following
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 * replaces the original e820 map with a new one, removing overlaps.
411 *
412 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100413static int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 struct change_member {
416 struct e820entry *pbios; /* pointer to original bios entry */
417 unsigned long long addr; /* address for this change point */
418 };
419 static struct change_member change_point_list[2*E820MAX] __initdata;
420 static struct change_member *change_point[2*E820MAX] __initdata;
421 static struct e820entry *overlap_list[E820MAX] __initdata;
422 static struct e820entry new_bios[E820MAX] __initdata;
423 struct change_member *change_tmp;
424 unsigned long current_type, last_type;
425 unsigned long long last_addr;
426 int chgidx, still_changing;
427 int overlap_entries;
428 int new_bios_entry;
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700429 int old_nr, new_nr, chg_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 int i;
431
432 /*
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100433 Visually we're performing the following
434 (1,2,3,4 = memory types)...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 Sample memory map (w/overlaps):
437 ____22__________________
438 ______________________4_
439 ____1111________________
440 _44_____________________
441 11111111________________
442 ____________________33__
443 ___________44___________
444 __________33333_________
445 ______________22________
446 ___________________2222_
447 _________111111111______
448 _____________________11_
449 _________________4______
450
451 Sanitized equivalent (no overlap):
452 1_______________________
453 _44_____________________
454 ___1____________________
455 ____22__________________
456 ______11________________
457 _________1______________
458 __________3_____________
459 ___________44___________
460 _____________33_________
461 _______________2________
462 ________________1_______
463 _________________4______
464 ___________________2____
465 ____________________33__
466 ______________________4_
467 */
468
469 /* if there's only one memory region, don't bother */
470 if (*pnr_map < 2)
471 return -1;
472
473 old_nr = *pnr_map;
474
475 /* bail out if we find any unreasonable addresses in bios map */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100476 for (i = 0; i < old_nr; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
478 return -1;
479
480 /* create pointers for initial change-point information (for sorting) */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100481 for (i = 0; i < 2 * old_nr; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 change_point[i] = &change_point_list[i];
483
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700484 /* record all known change-points (starting and ending addresses),
485 omitting those that are for empty memory regions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 chgidx = 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100487 for (i = 0; i < old_nr; i++) {
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700488 if (biosmap[i].size != 0) {
489 change_point[chgidx]->addr = biosmap[i].addr;
490 change_point[chgidx++]->pbios = &biosmap[i];
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100491 change_point[chgidx]->addr = biosmap[i].addr +
492 biosmap[i].size;
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700493 change_point[chgidx++]->pbios = &biosmap[i];
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700496 chg_nr = chgidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /* sort change-point list by memory addresses (low -> high) */
499 still_changing = 1;
500 while (still_changing) {
501 still_changing = 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100502 for (i = 1; i < chg_nr; i++) {
503 unsigned long long curaddr, lastaddr;
504 unsigned long long curpbaddr, lastpbaddr;
505
506 curaddr = change_point[i]->addr;
507 lastaddr = change_point[i - 1]->addr;
508 curpbaddr = change_point[i]->pbios->addr;
509 lastpbaddr = change_point[i - 1]->pbios->addr;
510
511 /*
512 * swap entries, when:
513 *
514 * curaddr > lastaddr or
515 * curaddr == lastaddr and curaddr == curpbaddr and
516 * lastaddr != lastpbaddr
517 */
518 if (curaddr < lastaddr ||
519 (curaddr == lastaddr && curaddr == curpbaddr &&
520 lastaddr != lastpbaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 change_tmp = change_point[i];
522 change_point[i] = change_point[i-1];
523 change_point[i-1] = change_tmp;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100524 still_changing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526 }
527 }
528
529 /* create a new bios memory map, removing overlaps */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100530 overlap_entries = 0; /* number of entries in the overlap table */
531 new_bios_entry = 0; /* index for creating new bios map entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 last_type = 0; /* start with undefined memory type */
533 last_addr = 0; /* start with 0 as last starting address */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* loop through change-points, determining affect on the new bios map */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100536 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* keep track of all overlapping bios entries */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100538 if (change_point[chgidx]->addr ==
539 change_point[chgidx]->pbios->addr) {
540 /*
541 * add map entry to overlap list (> 1 entry
542 * implies an overlap)
543 */
544 overlap_list[overlap_entries++] =
545 change_point[chgidx]->pbios;
546 } else {
547 /*
548 * remove entry from list (order independent,
549 * so swap with last)
550 */
551 for (i = 0; i < overlap_entries; i++) {
552 if (overlap_list[i] ==
553 change_point[chgidx]->pbios)
554 overlap_list[i] =
555 overlap_list[overlap_entries-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557 overlap_entries--;
558 }
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100559 /*
560 * if there are overlapping entries, decide which
561 * "type" to use (larger value takes precedence --
562 * 1=usable, 2,3,4,4+=unusable)
563 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 current_type = 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100565 for (i = 0; i < overlap_entries; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (overlap_list[i]->type > current_type)
567 current_type = overlap_list[i]->type;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100568 /*
569 * continue building up new bios map based on this
570 * information
571 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (current_type != last_type) {
573 if (last_type != 0) {
574 new_bios[new_bios_entry].size =
575 change_point[chgidx]->addr - last_addr;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100576 /*
577 * move forward only if the new size
578 * was non-zero
579 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (new_bios[new_bios_entry].size != 0)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100581 /*
582 * no more space left for new
583 * bios entries ?
584 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (++new_bios_entry >= E820MAX)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100586 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588 if (current_type != 0) {
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100589 new_bios[new_bios_entry].addr =
590 change_point[chgidx]->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 new_bios[new_bios_entry].type = current_type;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100592 last_addr = change_point[chgidx]->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 last_type = current_type;
595 }
596 }
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100597 /* retain count for new bios entries */
598 new_nr = new_bios_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 /* copy new bios mapping into original location */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100601 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 *pnr_map = new_nr;
603
604 return 0;
605}
606
607/*
608 * Copy the BIOS e820 map into a safe place.
609 *
610 * Sanity-check it while we're at it..
611 *
612 * If we're lucky and live on a modern system, the setup code
613 * will have given us a memory map that we can use to properly
614 * set up memory. If we aren't, we'll fake a memory map.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100616static int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 /* Only one memory region (or negative)? Ignore it */
619 if (nr_map < 2)
620 return -1;
621
622 do {
623 unsigned long start = biosmap->addr;
624 unsigned long size = biosmap->size;
625 unsigned long end = start + size;
626 unsigned long type = biosmap->type;
627
628 /* Overflow in 64 bits? Ignore the memory map. */
629 if (start > end)
630 return -1;
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 add_memory_region(start, size, type);
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100633 } while (biosmap++, --nr_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return 0;
635}
636
Andi Kleen8380aab2006-09-26 10:52:37 +0200637void early_panic(char *msg)
638{
639 early_printk(msg);
640 panic(msg);
641}
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643void __init setup_memory_region(void)
644{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /*
646 * Try to copy the BIOS-supplied E820-map.
647 *
648 * Otherwise fake a memory map; one section from 0k->640k,
649 * the next section from 1mb->appropriate_mem_k
650 */
H. Peter Anvin30c82642007-10-15 17:13:22 -0700651 sanitize_e820_map(boot_params.e820_map, &boot_params.e820_entries);
652 if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
Andi Kleen8380aab2006-09-26 10:52:37 +0200653 early_panic("Cannot find a valid memory map");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
Andi Kleen8380aab2006-09-26 10:52:37 +0200655 e820_print_map("BIOS-e820");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200658static int __init parse_memopt(char *p)
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800659{
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200660 if (!p)
661 return -EINVAL;
662 end_user_pfn = memparse(p, &p);
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100663 end_user_pfn >>= PAGE_SHIFT;
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200664 return 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100665}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200666early_param("mem", parse_memopt);
667
668static int userdef __initdata;
669
670static int __init parse_memmap_opt(char *p)
671{
672 char *oldp;
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800673 unsigned long long start_at, mem_size;
674
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200675 if (!strcmp(p, "exactmap")) {
676#ifdef CONFIG_CRASH_DUMP
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100677 /*
678 * If we are doing a crash dump, we still need to know
679 * the real mem size before original memory map is
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200680 * reset.
681 */
Magnus Damm15803a42006-11-14 16:57:46 +0100682 e820_register_active_regions(0, 0, -1UL);
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200683 saved_max_pfn = e820_end_of_ram();
Magnus Damm15803a42006-11-14 16:57:46 +0100684 remove_all_active_ranges();
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200685#endif
686 end_pfn_map = 0;
687 e820.nr_map = 0;
688 userdef = 1;
689 return 0;
690 }
691
692 oldp = p;
693 mem_size = memparse(p, &p);
694 if (p == oldp)
695 return -EINVAL;
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800696 if (*p == '@') {
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200697 start_at = memparse(p+1, &p);
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800698 add_memory_region(start_at, mem_size, E820_RAM);
699 } else if (*p == '#') {
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200700 start_at = memparse(p+1, &p);
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800701 add_memory_region(start_at, mem_size, E820_ACPI);
702 } else if (*p == '$') {
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200703 start_at = memparse(p+1, &p);
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800704 add_memory_region(start_at, mem_size, E820_RESERVED);
705 } else {
706 end_user_pfn = (mem_size >> PAGE_SHIFT);
707 }
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200708 return *p == '\0' ? 0 : -EINVAL;
709}
710early_param("memmap", parse_memmap_opt);
711
Sam Ravnborg43999d92007-03-16 21:07:36 +0100712void __init finish_e820_parsing(void)
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200713{
714 if (userdef) {
715 printk(KERN_INFO "user-defined physical RAM map:\n");
716 e820_print_map("user");
717 }
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800718}
719
Andi Kleena1e97782005-04-16 15:25:12 -0700720unsigned long pci_mem_start = 0xaeedbabe;
Andi Kleen2ee60e172006-06-26 13:59:44 +0200721EXPORT_SYMBOL(pci_mem_start);
Andi Kleena1e97782005-04-16 15:25:12 -0700722
723/*
724 * Search for the biggest gap in the low 32 bits of the e820
725 * memory space. We pass this space to PCI to assign MMIO resources
726 * for hotplug or unconfigured devices in.
727 * Hopefully the BIOS let enough space left.
728 */
729__init void e820_setup_gap(void)
730{
Daniel Ritzf0eca962005-09-09 00:57:14 +0200731 unsigned long gapstart, gapsize, round;
Andi Kleena1e97782005-04-16 15:25:12 -0700732 unsigned long last;
733 int i;
734 int found = 0;
735
736 last = 0x100000000ull;
737 gapstart = 0x10000000;
738 gapsize = 0x400000;
739 i = e820.nr_map;
740 while (--i >= 0) {
741 unsigned long long start = e820.map[i].addr;
742 unsigned long long end = start + e820.map[i].size;
743
744 /*
745 * Since "last" is at most 4GB, we know we'll
746 * fit in 32 bits if this condition is true
747 */
748 if (last > end) {
749 unsigned long gap = last - end;
750
751 if (gap > gapsize) {
752 gapsize = gap;
753 gapstart = end;
754 found = 1;
755 }
756 }
757 if (start < last)
758 last = start;
759 }
760
761 if (!found) {
762 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100763 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
764 "address range\n"
765 KERN_ERR "PCI: Unassigned devices with 32bit resource "
766 "registers may break!\n");
Andi Kleena1e97782005-04-16 15:25:12 -0700767 }
768
769 /*
Daniel Ritzf0eca962005-09-09 00:57:14 +0200770 * See how much we want to round up: start off with
771 * rounding to the next 1MB area.
Andi Kleena1e97782005-04-16 15:25:12 -0700772 */
Daniel Ritzf0eca962005-09-09 00:57:14 +0200773 round = 0x100000;
774 while ((gapsize >> 4) > round)
775 round += round;
776 /* Fun with two's complement */
777 pci_mem_start = (gapstart + round) & -round;
Andi Kleena1e97782005-04-16 15:25:12 -0700778
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100779 printk(KERN_INFO
780 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
781 pci_mem_start, gapstart, gapsize);
Andi Kleena1e97782005-04-16 15:25:12 -0700782}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -0700783
784int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
785{
786 int i;
787
788 if (slot < 0 || slot >= e820.nr_map)
789 return -1;
790 for (i = slot; i < e820.nr_map; i++) {
791 if (e820.map[i].type != E820_RAM)
792 continue;
793 break;
794 }
795 if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
796 return -1;
797 *addr = e820.map[i].addr;
798 *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
799 max_pfn << PAGE_SHIFT) - *addr;
800 return i + 1;
801}