blob: d41cd2f01733e2844c5ab5bb3f5023e280085c59 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Jan Beulichb92e9fa2007-05-02 19:27:11 +020030struct e820map e820;
Andi Kleen3bd4d182006-09-26 10:52:33 +020031
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010032/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * PFN of last memory page.
34 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010035unsigned long end_pfn;
Andi Kleenf3591ff2005-09-13 11:35:28 +020036EXPORT_SYMBOL(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
Bernhard Walle00bf4092007-10-21 16:42:01 -070050extern struct resource code_resource, data_resource, bss_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010052/* Check for some hardcoded bad areas that early boot is not allowed to touch */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static inline int bad_addr(unsigned long *addrp, unsigned long size)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010054{
55 unsigned long addr = *addrp, last = addr + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 /* various gunk below that needed for SMP startup */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010058 if (addr < 0x8000) {
Vivek Goyal73bb8912006-10-21 18:37:01 +020059 *addrp = PAGE_ALIGN(0x8000);
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010060 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 }
62
63 /* direct mapping tables of the kernel */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010064 if (last >= table_start<<PAGE_SHIFT && addr < table_end<<PAGE_SHIFT) {
Vivek Goyal73bb8912006-10-21 18:37:01 +020065 *addrp = PAGE_ALIGN(table_end << PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return 1;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010069 /* initrd */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#ifdef CONFIG_BLK_DEV_INITRD
H. Peter Anvin30c82642007-10-15 17:13:22 -070071 if (boot_params.hdr.type_of_loader && boot_params.hdr.ramdisk_image) {
72 unsigned long ramdisk_image = boot_params.hdr.ramdisk_image;
73 unsigned long ramdisk_size = boot_params.hdr.ramdisk_size;
74 unsigned long ramdisk_end = ramdisk_image+ramdisk_size;
75
76 if (last >= ramdisk_image && addr < ramdisk_end) {
77 *addrp = PAGE_ALIGN(ramdisk_end);
78 return 1;
79 }
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#endif
Andi Kleendbf92722006-09-26 10:52:36 +020082 /* kernel code */
Vivek Goyal73bb8912006-10-21 18:37:01 +020083 if (last >= __pa_symbol(&_text) && addr < __pa_symbol(&_end)) {
84 *addrp = PAGE_ALIGN(__pa_symbol(&_end));
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return 1;
86 }
Andi Kleenac71d122006-05-08 15:17:28 +020087
88 if (last >= ebda_addr && addr < ebda_addr + ebda_size) {
Vivek Goyal73bb8912006-10-21 18:37:01 +020089 *addrp = PAGE_ALIGN(ebda_addr + ebda_size);
Andi Kleenac71d122006-05-08 15:17:28 +020090 return 1;
91 }
92
Amul Shah076422d2007-02-13 13:26:19 +010093#ifdef CONFIG_NUMA
94 /* NUMA memory to node map */
95 if (last >= nodemap_addr && addr < nodemap_addr + nodemap_size) {
96 *addrp = nodemap_addr + nodemap_size;
97 return 1;
98 }
99#endif
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100100 /* XXX ramdisk image here? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100102}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Arjan van de Ven95222362006-04-07 19:49:27 +0200104/*
105 * This function checks if any part of the range <start,end> is mapped
106 * with type.
107 */
Jan Beulichb92e9fa2007-05-02 19:27:11 +0200108int
Arjan van de Veneee5a9f2006-04-07 19:49:24 +0200109e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100110{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 int i;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100112
113 for (i = 0; i < e820.nr_map; i++) {
114 struct e820entry *ei = &e820.map[i];
115
116 if (type && ei->type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 continue;
Eric W. Biederman48c8b112005-09-06 15:16:20 -0700118 if (ei->addr >= end || ei->addr + ei->size <= start)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100119 continue;
120 return 1;
121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return 0;
123}
Jan Beulichb92e9fa2007-05-02 19:27:11 +0200124EXPORT_SYMBOL_GPL(e820_any_mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Linus Torvalds79e453d2006-09-19 08:15:22 -0700126/*
127 * This function checks if the entire range <start,end> is mapped with type.
128 *
129 * Note: this function only works correct if the e820 table is sorted and
130 * not-overlapping, which is the case
131 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100132int __init e820_all_mapped(unsigned long start, unsigned long end,
133 unsigned type)
Linus Torvalds79e453d2006-09-19 08:15:22 -0700134{
135 int i;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100136
Linus Torvalds79e453d2006-09-19 08:15:22 -0700137 for (i = 0; i < e820.nr_map; i++) {
138 struct e820entry *ei = &e820.map[i];
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100139
Linus Torvalds79e453d2006-09-19 08:15:22 -0700140 if (type && ei->type != type)
141 continue;
142 /* is the region (part) in overlap with the current region ?*/
143 if (ei->addr >= end || ei->addr + ei->size <= start)
144 continue;
145
146 /* if the region is at the beginning of <start,end> we move
147 * start to the end of the region since it's ok until there
148 */
149 if (ei->addr <= start)
150 start = ei->addr + ei->size;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100151 /*
152 * if start is now at or beyond end, we're done, full
153 * coverage
154 */
Linus Torvalds79e453d2006-09-19 08:15:22 -0700155 if (start >= end)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100156 return 1;
Linus Torvalds79e453d2006-09-19 08:15:22 -0700157 }
158 return 0;
159}
160
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100161/*
162 * Find a free area in a specific range.
163 */
164unsigned long __init find_e820_area(unsigned long start, unsigned long end,
165 unsigned size)
166{
167 int i;
168
169 for (i = 0; i < e820.nr_map; i++) {
170 struct e820entry *ei = &e820.map[i];
171 unsigned long addr = ei->addr, last;
172
173 if (ei->type != E820_RAM)
174 continue;
175 if (addr < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 addr = start;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100177 if (addr > ei->addr + ei->size)
178 continue;
Robert Hentosh7ca97c62006-05-30 22:48:00 +0200179 while (bad_addr(&addr, size) && addr+size <= ei->addr+ei->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 ;
Vivek Goyal73bb8912006-10-21 18:37:01 +0200181 last = PAGE_ALIGN(addr) + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (last > ei->addr + ei->size)
183 continue;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100184 if (last > end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 continue;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100186 return addr;
187 }
188 return -1UL;
189}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
192 * Find the highest page frame number we have available
193 */
194unsigned long __init e820_end_of_ram(void)
195{
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100196 unsigned long end_pfn;
197
Mel Gorman5cb248a2006-09-27 01:49:52 -0700198 end_pfn = find_max_pfn_with_active_regions();
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100199
200 if (end_pfn > end_pfn_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 end_pfn_map = end_pfn;
202 if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
203 end_pfn_map = MAXMEM>>PAGE_SHIFT;
204 if (end_pfn > end_user_pfn)
205 end_pfn = end_user_pfn;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100206 if (end_pfn > end_pfn_map)
207 end_pfn = end_pfn_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100209 printk(KERN_INFO "end_pfn_map = %lu\n", end_pfn_map);
210 return end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Andi Kleen485761b2005-08-26 18:34:10 -0700213/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * Mark e820 reserved areas as busy for the resource manager.
215 */
216void __init e820_reserve_resources(void)
217{
218 int i;
219 for (i = 0; i < e820.nr_map; i++) {
220 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 res = alloc_bootmem_low(sizeof(struct resource));
222 switch (e820.map[i].type) {
223 case E820_RAM: res->name = "System RAM"; break;
224 case E820_ACPI: res->name = "ACPI Tables"; break;
225 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
226 default: res->name = "reserved";
227 }
228 res->start = e820.map[i].addr;
229 res->end = res->start + e820.map[i].size - 1;
230 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
231 request_resource(&iomem_resource, res);
232 if (e820.map[i].type == E820_RAM) {
233 /*
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100234 * We don't know which RAM region contains kernel data,
235 * so we try it repeatedly and let the resource manager
236 * test it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 */
238 request_resource(res, &code_resource);
239 request_resource(res, &data_resource);
Bernhard Walle00bf4092007-10-21 16:42:01 -0700240 request_resource(res, &bss_resource);
Eric W. Biederman5f5609d2005-06-25 14:58:04 -0700241#ifdef CONFIG_KEXEC
Bernhard Walle5c3391f2007-10-18 23:40:59 -0700242 if (crashk_res.start != crashk_res.end)
243 request_resource(res, &crashk_res);
Eric W. Biederman5f5609d2005-06-25 14:58:04 -0700244#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246 }
247}
248
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -0700249/*
250 * Find the ranges of physical addresses that do not correspond to
251 * e820 RAM areas and mark the corresponding pages as nosave for software
252 * suspend and suspend to RAM.
253 *
254 * This function requires the e820 map to be sorted and without any
255 * overlapping entries and assumes the first e820 area to be RAM.
256 */
257void __init e820_mark_nosave_regions(void)
258{
259 int i;
260 unsigned long paddr;
261
262 paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
263 for (i = 1; i < e820.nr_map; i++) {
264 struct e820entry *ei = &e820.map[i];
265
266 if (paddr < ei->addr)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700267 register_nosave_region(PFN_DOWN(paddr),
268 PFN_UP(ei->addr));
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -0700269
270 paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
271 if (ei->type != E820_RAM)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700272 register_nosave_region(PFN_UP(ei->addr),
273 PFN_DOWN(paddr));
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -0700274
275 if (paddr >= (end_pfn << PAGE_SHIFT))
276 break;
277 }
278}
279
David Rientjes3af044e2007-07-21 17:10:31 +0200280/*
281 * Finds an active region in the address range from start_pfn to end_pfn and
282 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
283 */
284static int __init e820_find_active_region(const struct e820entry *ei,
285 unsigned long start_pfn,
286 unsigned long end_pfn,
287 unsigned long *ei_startpfn,
288 unsigned long *ei_endpfn)
289{
290 *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
291 *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
292
293 /* Skip map entries smaller than a page */
294 if (*ei_startpfn >= *ei_endpfn)
295 return 0;
296
297 /* Check if end_pfn_map should be updated */
298 if (ei->type != E820_RAM && *ei_endpfn > end_pfn_map)
299 end_pfn_map = *ei_endpfn;
300
301 /* Skip if map is outside the node */
302 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
303 *ei_startpfn >= end_pfn)
304 return 0;
305
306 /* Check for overlaps */
307 if (*ei_startpfn < start_pfn)
308 *ei_startpfn = start_pfn;
309 if (*ei_endpfn > end_pfn)
310 *ei_endpfn = end_pfn;
311
312 /* Obey end_user_pfn to save on memmap */
313 if (*ei_startpfn >= end_user_pfn)
314 return 0;
315 if (*ei_endpfn > end_user_pfn)
316 *ei_endpfn = end_user_pfn;
317
318 return 1;
319}
320
Mel Gorman5cb248a2006-09-27 01:49:52 -0700321/* Walk the e820 map and register active regions within a node */
322void __init
323e820_register_active_regions(int nid, unsigned long start_pfn,
324 unsigned long end_pfn)
325{
David Rientjes3af044e2007-07-21 17:10:31 +0200326 unsigned long ei_startpfn;
327 unsigned long ei_endpfn;
Mel Gorman5cb248a2006-09-27 01:49:52 -0700328 int i;
Mel Gorman5cb248a2006-09-27 01:49:52 -0700329
David Rientjes3af044e2007-07-21 17:10:31 +0200330 for (i = 0; i < e820.nr_map; i++)
331 if (e820_find_active_region(&e820.map[i],
332 start_pfn, end_pfn,
333 &ei_startpfn, &ei_endpfn))
334 add_active_range(nid, ei_startpfn, ei_endpfn);
Mel Gorman5cb248a2006-09-27 01:49:52 -0700335}
336
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100337/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 * Add a memory region to the kernel e820 map.
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100339 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340void __init add_memory_region(unsigned long start, unsigned long size, int type)
341{
342 int x = e820.nr_map;
343
344 if (x == E820MAX) {
345 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
346 return;
347 }
348
349 e820.map[x].addr = start;
350 e820.map[x].size = size;
351 e820.map[x].type = type;
352 e820.nr_map++;
353}
354
David Rientjesa7e96622007-07-21 17:11:29 +0200355/*
356 * Find the hole size (in bytes) in the memory range.
357 * @start: starting address of the memory range to scan
358 * @end: ending address of the memory range to scan
359 */
360unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
361{
362 unsigned long start_pfn = start >> PAGE_SHIFT;
363 unsigned long end_pfn = end >> PAGE_SHIFT;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100364 unsigned long ei_startpfn, ei_endpfn, ram = 0;
David Rientjesa7e96622007-07-21 17:11:29 +0200365 int i;
366
367 for (i = 0; i < e820.nr_map; i++) {
368 if (e820_find_active_region(&e820.map[i],
369 start_pfn, end_pfn,
370 &ei_startpfn, &ei_endpfn))
371 ram += ei_endpfn - ei_startpfn;
372 }
373 return end - start - (ram << PAGE_SHIFT);
374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376void __init e820_print_map(char *who)
377{
378 int i;
379
380 for (i = 0; i < e820.nr_map; i++) {
Dan Aloni5a3ece72007-07-21 17:11:37 +0200381 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100382 (unsigned long long) e820.map[i].addr,
383 (unsigned long long)
384 (e820.map[i].addr + e820.map[i].size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 switch (e820.map[i].type) {
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100386 case E820_RAM:
387 printk(KERN_CONT "(usable)\n");
388 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 case E820_RESERVED:
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100390 printk(KERN_CONT "(reserved)\n");
391 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 case E820_ACPI:
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100393 printk(KERN_CONT "(ACPI data)\n");
394 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 case E820_NVS:
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100396 printk(KERN_CONT "(ACPI NVS)\n");
397 break;
398 default:
399 printk(KERN_CONT "type %u\n", e820.map[i].type);
400 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 }
403}
404
405/*
406 * Sanitize the BIOS e820 map.
407 *
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100408 * Some e820 responses include overlapping entries. The following
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 * replaces the original e820 map with a new one, removing overlaps.
410 *
411 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100412static int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 struct change_member {
415 struct e820entry *pbios; /* pointer to original bios entry */
416 unsigned long long addr; /* address for this change point */
417 };
418 static struct change_member change_point_list[2*E820MAX] __initdata;
419 static struct change_member *change_point[2*E820MAX] __initdata;
420 static struct e820entry *overlap_list[E820MAX] __initdata;
421 static struct e820entry new_bios[E820MAX] __initdata;
422 struct change_member *change_tmp;
423 unsigned long current_type, last_type;
424 unsigned long long last_addr;
425 int chgidx, still_changing;
426 int overlap_entries;
427 int new_bios_entry;
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700428 int old_nr, new_nr, chg_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 int i;
430
431 /*
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100432 Visually we're performing the following
433 (1,2,3,4 = memory types)...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 Sample memory map (w/overlaps):
436 ____22__________________
437 ______________________4_
438 ____1111________________
439 _44_____________________
440 11111111________________
441 ____________________33__
442 ___________44___________
443 __________33333_________
444 ______________22________
445 ___________________2222_
446 _________111111111______
447 _____________________11_
448 _________________4______
449
450 Sanitized equivalent (no overlap):
451 1_______________________
452 _44_____________________
453 ___1____________________
454 ____22__________________
455 ______11________________
456 _________1______________
457 __________3_____________
458 ___________44___________
459 _____________33_________
460 _______________2________
461 ________________1_______
462 _________________4______
463 ___________________2____
464 ____________________33__
465 ______________________4_
466 */
467
468 /* if there's only one memory region, don't bother */
469 if (*pnr_map < 2)
470 return -1;
471
472 old_nr = *pnr_map;
473
474 /* bail out if we find any unreasonable addresses in bios map */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100475 for (i = 0; i < old_nr; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
477 return -1;
478
479 /* create pointers for initial change-point information (for sorting) */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100480 for (i = 0; i < 2 * old_nr; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 change_point[i] = &change_point_list[i];
482
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700483 /* record all known change-points (starting and ending addresses),
484 omitting those that are for empty memory regions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 chgidx = 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100486 for (i = 0; i < old_nr; i++) {
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700487 if (biosmap[i].size != 0) {
488 change_point[chgidx]->addr = biosmap[i].addr;
489 change_point[chgidx++]->pbios = &biosmap[i];
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100490 change_point[chgidx]->addr = biosmap[i].addr +
491 biosmap[i].size;
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700492 change_point[chgidx++]->pbios = &biosmap[i];
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700495 chg_nr = chgidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 /* sort change-point list by memory addresses (low -> high) */
498 still_changing = 1;
499 while (still_changing) {
500 still_changing = 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100501 for (i = 1; i < chg_nr; i++) {
502 unsigned long long curaddr, lastaddr;
503 unsigned long long curpbaddr, lastpbaddr;
504
505 curaddr = change_point[i]->addr;
506 lastaddr = change_point[i - 1]->addr;
507 curpbaddr = change_point[i]->pbios->addr;
508 lastpbaddr = change_point[i - 1]->pbios->addr;
509
510 /*
511 * swap entries, when:
512 *
513 * curaddr > lastaddr or
514 * curaddr == lastaddr and curaddr == curpbaddr and
515 * lastaddr != lastpbaddr
516 */
517 if (curaddr < lastaddr ||
518 (curaddr == lastaddr && curaddr == curpbaddr &&
519 lastaddr != lastpbaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 change_tmp = change_point[i];
521 change_point[i] = change_point[i-1];
522 change_point[i-1] = change_tmp;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100523 still_changing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525 }
526 }
527
528 /* create a new bios memory map, removing overlaps */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100529 overlap_entries = 0; /* number of entries in the overlap table */
530 new_bios_entry = 0; /* index for creating new bios map entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 last_type = 0; /* start with undefined memory type */
532 last_addr = 0; /* start with 0 as last starting address */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /* loop through change-points, determining affect on the new bios map */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100535 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /* keep track of all overlapping bios entries */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100537 if (change_point[chgidx]->addr ==
538 change_point[chgidx]->pbios->addr) {
539 /*
540 * add map entry to overlap list (> 1 entry
541 * implies an overlap)
542 */
543 overlap_list[overlap_entries++] =
544 change_point[chgidx]->pbios;
545 } else {
546 /*
547 * remove entry from list (order independent,
548 * so swap with last)
549 */
550 for (i = 0; i < overlap_entries; i++) {
551 if (overlap_list[i] ==
552 change_point[chgidx]->pbios)
553 overlap_list[i] =
554 overlap_list[overlap_entries-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556 overlap_entries--;
557 }
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100558 /*
559 * if there are overlapping entries, decide which
560 * "type" to use (larger value takes precedence --
561 * 1=usable, 2,3,4,4+=unusable)
562 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 current_type = 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100564 for (i = 0; i < overlap_entries; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (overlap_list[i]->type > current_type)
566 current_type = overlap_list[i]->type;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100567 /*
568 * continue building up new bios map based on this
569 * information
570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (current_type != last_type) {
572 if (last_type != 0) {
573 new_bios[new_bios_entry].size =
574 change_point[chgidx]->addr - last_addr;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100575 /*
576 * move forward only if the new size
577 * was non-zero
578 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (new_bios[new_bios_entry].size != 0)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100580 /*
581 * no more space left for new
582 * bios entries ?
583 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (++new_bios_entry >= E820MAX)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100585 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587 if (current_type != 0) {
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100588 new_bios[new_bios_entry].addr =
589 change_point[chgidx]->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 new_bios[new_bios_entry].type = current_type;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100591 last_addr = change_point[chgidx]->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593 last_type = current_type;
594 }
595 }
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100596 /* retain count for new bios entries */
597 new_nr = new_bios_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 /* copy new bios mapping into original location */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100600 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 *pnr_map = new_nr;
602
603 return 0;
604}
605
606/*
607 * Copy the BIOS e820 map into a safe place.
608 *
609 * Sanity-check it while we're at it..
610 *
611 * If we're lucky and live on a modern system, the setup code
612 * will have given us a memory map that we can use to properly
613 * set up memory. If we aren't, we'll fake a memory map.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100615static int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 /* Only one memory region (or negative)? Ignore it */
618 if (nr_map < 2)
619 return -1;
620
621 do {
622 unsigned long start = biosmap->addr;
623 unsigned long size = biosmap->size;
624 unsigned long end = start + size;
625 unsigned long type = biosmap->type;
626
627 /* Overflow in 64 bits? Ignore the memory map. */
628 if (start > end)
629 return -1;
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 add_memory_region(start, size, type);
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100632 } while (biosmap++, --nr_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return 0;
634}
635
Andi Kleen8380aab2006-09-26 10:52:37 +0200636void early_panic(char *msg)
637{
638 early_printk(msg);
639 panic(msg);
640}
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642void __init setup_memory_region(void)
643{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /*
645 * Try to copy the BIOS-supplied E820-map.
646 *
647 * Otherwise fake a memory map; one section from 0k->640k,
648 * the next section from 1mb->appropriate_mem_k
649 */
H. Peter Anvin30c82642007-10-15 17:13:22 -0700650 sanitize_e820_map(boot_params.e820_map, &boot_params.e820_entries);
651 if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
Andi Kleen8380aab2006-09-26 10:52:37 +0200652 early_panic("Cannot find a valid memory map");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
Andi Kleen8380aab2006-09-26 10:52:37 +0200654 e820_print_map("BIOS-e820");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200657static int __init parse_memopt(char *p)
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800658{
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200659 if (!p)
660 return -EINVAL;
661 end_user_pfn = memparse(p, &p);
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100662 end_user_pfn >>= PAGE_SHIFT;
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200663 return 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100664}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200665early_param("mem", parse_memopt);
666
667static int userdef __initdata;
668
669static int __init parse_memmap_opt(char *p)
670{
671 char *oldp;
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800672 unsigned long long start_at, mem_size;
673
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200674 if (!strcmp(p, "exactmap")) {
675#ifdef CONFIG_CRASH_DUMP
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100676 /*
677 * If we are doing a crash dump, we still need to know
678 * the real mem size before original memory map is
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200679 * reset.
680 */
Magnus Damm15803a42006-11-14 16:57:46 +0100681 e820_register_active_regions(0, 0, -1UL);
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200682 saved_max_pfn = e820_end_of_ram();
Magnus Damm15803a42006-11-14 16:57:46 +0100683 remove_all_active_ranges();
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200684#endif
685 end_pfn_map = 0;
686 e820.nr_map = 0;
687 userdef = 1;
688 return 0;
689 }
690
691 oldp = p;
692 mem_size = memparse(p, &p);
693 if (p == oldp)
694 return -EINVAL;
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800695 if (*p == '@') {
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200696 start_at = memparse(p+1, &p);
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800697 add_memory_region(start_at, mem_size, E820_RAM);
698 } else if (*p == '#') {
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200699 start_at = memparse(p+1, &p);
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800700 add_memory_region(start_at, mem_size, E820_ACPI);
701 } else if (*p == '$') {
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200702 start_at = memparse(p+1, &p);
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800703 add_memory_region(start_at, mem_size, E820_RESERVED);
704 } else {
705 end_user_pfn = (mem_size >> PAGE_SHIFT);
706 }
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200707 return *p == '\0' ? 0 : -EINVAL;
708}
709early_param("memmap", parse_memmap_opt);
710
Sam Ravnborg43999d92007-03-16 21:07:36 +0100711void __init finish_e820_parsing(void)
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200712{
713 if (userdef) {
714 printk(KERN_INFO "user-defined physical RAM map:\n");
715 e820_print_map("user");
716 }
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800717}
718
Andi Kleena1e97782005-04-16 15:25:12 -0700719unsigned long pci_mem_start = 0xaeedbabe;
Andi Kleen2ee60e172006-06-26 13:59:44 +0200720EXPORT_SYMBOL(pci_mem_start);
Andi Kleena1e97782005-04-16 15:25:12 -0700721
722/*
723 * Search for the biggest gap in the low 32 bits of the e820
724 * memory space. We pass this space to PCI to assign MMIO resources
725 * for hotplug or unconfigured devices in.
726 * Hopefully the BIOS let enough space left.
727 */
728__init void e820_setup_gap(void)
729{
Daniel Ritzf0eca962005-09-09 00:57:14 +0200730 unsigned long gapstart, gapsize, round;
Andi Kleena1e97782005-04-16 15:25:12 -0700731 unsigned long last;
732 int i;
733 int found = 0;
734
735 last = 0x100000000ull;
736 gapstart = 0x10000000;
737 gapsize = 0x400000;
738 i = e820.nr_map;
739 while (--i >= 0) {
740 unsigned long long start = e820.map[i].addr;
741 unsigned long long end = start + e820.map[i].size;
742
743 /*
744 * Since "last" is at most 4GB, we know we'll
745 * fit in 32 bits if this condition is true
746 */
747 if (last > end) {
748 unsigned long gap = last - end;
749
750 if (gap > gapsize) {
751 gapsize = gap;
752 gapstart = end;
753 found = 1;
754 }
755 }
756 if (start < last)
757 last = start;
758 }
759
760 if (!found) {
761 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100762 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
763 "address range\n"
764 KERN_ERR "PCI: Unassigned devices with 32bit resource "
765 "registers may break!\n");
Andi Kleena1e97782005-04-16 15:25:12 -0700766 }
767
768 /*
Daniel Ritzf0eca962005-09-09 00:57:14 +0200769 * See how much we want to round up: start off with
770 * rounding to the next 1MB area.
Andi Kleena1e97782005-04-16 15:25:12 -0700771 */
Daniel Ritzf0eca962005-09-09 00:57:14 +0200772 round = 0x100000;
773 while ((gapsize >> 4) > round)
774 round += round;
775 /* Fun with two's complement */
776 pci_mem_start = (gapstart + round) & -round;
Andi Kleena1e97782005-04-16 15:25:12 -0700777
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100778 printk(KERN_INFO
779 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
780 pci_mem_start, gapstart, gapsize);
Andi Kleena1e97782005-04-16 15:25:12 -0700781}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -0700782
783int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
784{
785 int i;
786
787 if (slot < 0 || slot >= e820.nr_map)
788 return -1;
789 for (i = slot; i < e820.nr_map; i++) {
790 if (e820.map[i].type != E820_RAM)
791 continue;
792 break;
793 }
794 if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
795 return -1;
796 *addr = e820.map[i].addr;
797 *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
798 max_pfn << PAGE_SHIFT) - *addr;
799 return i + 1;
800}