blob: 95b81c18b6bce844a194b6239a7548a7f7f71d8b [file] [log] [blame]
Yinghai Lub79cd8f2008-05-11 00:30:15 -07001/*
2 * Handle the memory map.
3 * The functions here do the job until bootmem takes over.
4 *
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 *
10 */
11#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>
17#include <linux/kexec.h>
18#include <linux/module.h>
19#include <linux/mm.h>
20#include <linux/pfn.h>
Yinghai Lubf62f392008-05-20 20:10:58 -070021#include <linux/suspend.h>
Bernhard Walle5dfcf142008-06-27 13:12:55 +020022#include <linux/firmware-map.h>
Yinghai Lub79cd8f2008-05-11 00:30:15 -070023
24#include <asm/pgtable.h>
25#include <asm/page.h>
26#include <asm/e820.h>
Yinghai Lua4c81cf2008-05-18 01:18:57 -070027#include <asm/proto.h>
Yinghai Lub79cd8f2008-05-11 00:30:15 -070028#include <asm/setup.h>
Yinghai Lua4c81cf2008-05-18 01:18:57 -070029#include <asm/trampoline.h>
Yinghai Lub79cd8f2008-05-11 00:30:15 -070030
Bernhard Walle5dfcf142008-06-27 13:12:55 +020031/*
32 * The e820 map is the map that gets modified e.g. with command line parameters
33 * and that is also registered with modifications in the kernel resource tree
34 * with the iomem_resource as parent.
35 *
36 * The e820_saved is directly saved after the BIOS-provided memory map is
37 * copied. It doesn't get modified afterwards. It's registered for the
38 * /sys/firmware/memmap interface.
39 *
40 * That memory map is not modified and is used as base for kexec. The kexec'd
41 * kernel should get the same memory map as the firmware provides. Then the
42 * user can e.g. boot the original kernel with mem=1G while still booting the
43 * next kernel with full memory.
44 */
Yinghai Lub79cd8f2008-05-11 00:30:15 -070045struct e820map e820;
Bernhard Walle5dfcf142008-06-27 13:12:55 +020046struct e820map e820_saved;
Yinghai Lub79cd8f2008-05-11 00:30:15 -070047
48/* For PCI or other memory-mapped resources */
49unsigned long pci_mem_start = 0xaeedbabe;
50#ifdef CONFIG_PCI
51EXPORT_SYMBOL(pci_mem_start);
52#endif
53
54/*
55 * This function checks if any part of the range <start,end> is mapped
56 * with type.
57 */
58int
59e820_any_mapped(u64 start, u64 end, unsigned type)
60{
61 int i;
62
63 for (i = 0; i < e820.nr_map; i++) {
64 struct e820entry *ei = &e820.map[i];
65
66 if (type && ei->type != type)
67 continue;
68 if (ei->addr >= end || ei->addr + ei->size <= start)
69 continue;
70 return 1;
71 }
72 return 0;
73}
74EXPORT_SYMBOL_GPL(e820_any_mapped);
75
76/*
77 * This function checks if the entire range <start,end> is mapped with type.
78 *
79 * Note: this function only works correct if the e820 table is sorted and
80 * not-overlapping, which is the case
81 */
82int __init e820_all_mapped(u64 start, u64 end, unsigned type)
83{
84 int i;
85
86 for (i = 0; i < e820.nr_map; i++) {
87 struct e820entry *ei = &e820.map[i];
88
89 if (type && ei->type != type)
90 continue;
91 /* is the region (part) in overlap with the current region ?*/
92 if (ei->addr >= end || ei->addr + ei->size <= start)
93 continue;
94
95 /* if the region is at the beginning of <start,end> we move
96 * start to the end of the region since it's ok until there
97 */
98 if (ei->addr <= start)
99 start = ei->addr + ei->size;
100 /*
101 * if start is now at or beyond end, we're done, full
102 * coverage
103 */
104 if (start >= end)
105 return 1;
106 }
107 return 0;
108}
109
110/*
111 * Add a memory region to the kernel e820 map.
112 */
Yinghai Lu773e6732009-03-12 21:35:18 -0700113static void __init __e820_add_region(struct e820map *e820x, u64 start, u64 size,
114 int type)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700115{
Yinghai Lu773e6732009-03-12 21:35:18 -0700116 int x = e820x->nr_map;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700117
Yinghai Lu773e6732009-03-12 21:35:18 -0700118 if (x == ARRAY_SIZE(e820x->map)) {
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700119 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
120 return;
121 }
122
Yinghai Lu773e6732009-03-12 21:35:18 -0700123 e820x->map[x].addr = start;
124 e820x->map[x].size = size;
125 e820x->map[x].type = type;
126 e820x->nr_map++;
127}
128
129void __init e820_add_region(u64 start, u64 size, int type)
130{
131 __e820_add_region(&e820, start, size, type);
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700132}
133
134void __init e820_print_map(char *who)
135{
136 int i;
137
138 for (i = 0; i < e820.nr_map; i++) {
139 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
140 (unsigned long long) e820.map[i].addr,
141 (unsigned long long)
142 (e820.map[i].addr + e820.map[i].size));
143 switch (e820.map[i].type) {
144 case E820_RAM:
Yinghai Lu28bb2232008-06-30 16:20:54 -0700145 case E820_RESERVED_KERN:
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700146 printk(KERN_CONT "(usable)\n");
147 break;
148 case E820_RESERVED:
149 printk(KERN_CONT "(reserved)\n");
150 break;
151 case E820_ACPI:
152 printk(KERN_CONT "(ACPI data)\n");
153 break;
154 case E820_NVS:
155 printk(KERN_CONT "(ACPI NVS)\n");
156 break;
Cihula, Joseph671eef82008-08-20 16:43:07 -0700157 case E820_UNUSABLE:
158 printk("(unusable)\n");
159 break;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700160 default:
161 printk(KERN_CONT "type %u\n", e820.map[i].type);
162 break;
163 }
164 }
165}
166
167/*
168 * Sanitize the BIOS e820 map.
169 *
170 * Some e820 responses include overlapping entries. The following
Paul Jackson5b7eb2e2008-05-14 08:15:52 -0700171 * replaces the original e820 map with a new one, removing overlaps,
172 * and resolving conflicting memory types in favor of highest
173 * numbered type.
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700174 *
Paul Jackson5b7eb2e2008-05-14 08:15:52 -0700175 * The input parameter biosmap points to an array of 'struct
176 * e820entry' which on entry has elements in the range [0, *pnr_map)
177 * valid, and which has space for up to max_nr_map entries.
178 * On return, the resulting sanitized e820 map entries will be in
179 * overwritten in the same location, starting at biosmap.
180 *
181 * The integer pointed to by pnr_map must be valid on entry (the
182 * current number of valid entries located at biosmap) and will
183 * be updated on return, with the new number of valid entries
184 * (something no more than max_nr_map.)
185 *
186 * The return value from sanitize_e820_map() is zero if it
187 * successfully 'sanitized' the map entries passed in, and is -1
188 * if it did nothing, which can happen if either of (1) it was
189 * only passed one map entry, or (2) any of the input map entries
190 * were invalid (start + size < start, meaning that the size was
191 * so big the described memory range wrapped around through zero.)
192 *
193 * Visually we're performing the following
194 * (1,2,3,4 = memory types)...
195 *
196 * Sample memory map (w/overlaps):
197 * ____22__________________
198 * ______________________4_
199 * ____1111________________
200 * _44_____________________
201 * 11111111________________
202 * ____________________33__
203 * ___________44___________
204 * __________33333_________
205 * ______________22________
206 * ___________________2222_
207 * _________111111111______
208 * _____________________11_
209 * _________________4______
210 *
211 * Sanitized equivalent (no overlap):
212 * 1_______________________
213 * _44_____________________
214 * ___1____________________
215 * ____22__________________
216 * ______11________________
217 * _________1______________
218 * __________3_____________
219 * ___________44___________
220 * _____________33_________
221 * _______________2________
222 * ________________1_______
223 * _________________4______
224 * ___________________2____
225 * ____________________33__
226 * ______________________4_
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700227 */
Paul Jackson5b7eb2e2008-05-14 08:15:52 -0700228
Paul Jacksonc3965bd2008-05-14 08:15:34 -0700229int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map,
Paul Jackson6e9bcc72008-05-14 08:15:46 -0700230 int *pnr_map)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700231{
232 struct change_member {
233 struct e820entry *pbios; /* pointer to original bios entry */
234 unsigned long long addr; /* address for this change point */
235 };
Paul Jackson157fabf2008-06-22 07:21:57 -0700236 static struct change_member change_point_list[2*E820_X_MAX] __initdata;
237 static struct change_member *change_point[2*E820_X_MAX] __initdata;
238 static struct e820entry *overlap_list[E820_X_MAX] __initdata;
239 static struct e820entry new_bios[E820_X_MAX] __initdata;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700240 struct change_member *change_tmp;
241 unsigned long current_type, last_type;
242 unsigned long long last_addr;
243 int chgidx, still_changing;
244 int overlap_entries;
245 int new_bios_entry;
246 int old_nr, new_nr, chg_nr;
247 int i;
248
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700249 /* if there's only one memory region, don't bother */
250 if (*pnr_map < 2)
251 return -1;
252
253 old_nr = *pnr_map;
Paul Jackson6e9bcc72008-05-14 08:15:46 -0700254 BUG_ON(old_nr > max_nr_map);
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700255
256 /* bail out if we find any unreasonable addresses in bios map */
257 for (i = 0; i < old_nr; i++)
258 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
259 return -1;
260
261 /* create pointers for initial change-point information (for sorting) */
262 for (i = 0; i < 2 * old_nr; i++)
263 change_point[i] = &change_point_list[i];
264
265 /* record all known change-points (starting and ending addresses),
266 omitting those that are for empty memory regions */
267 chgidx = 0;
268 for (i = 0; i < old_nr; i++) {
269 if (biosmap[i].size != 0) {
270 change_point[chgidx]->addr = biosmap[i].addr;
271 change_point[chgidx++]->pbios = &biosmap[i];
272 change_point[chgidx]->addr = biosmap[i].addr +
273 biosmap[i].size;
274 change_point[chgidx++]->pbios = &biosmap[i];
275 }
276 }
277 chg_nr = chgidx;
278
279 /* sort change-point list by memory addresses (low -> high) */
280 still_changing = 1;
281 while (still_changing) {
282 still_changing = 0;
283 for (i = 1; i < chg_nr; i++) {
284 unsigned long long curaddr, lastaddr;
285 unsigned long long curpbaddr, lastpbaddr;
286
287 curaddr = change_point[i]->addr;
288 lastaddr = change_point[i - 1]->addr;
289 curpbaddr = change_point[i]->pbios->addr;
290 lastpbaddr = change_point[i - 1]->pbios->addr;
291
292 /*
293 * swap entries, when:
294 *
295 * curaddr > lastaddr or
296 * curaddr == lastaddr and curaddr == curpbaddr and
297 * lastaddr != lastpbaddr
298 */
299 if (curaddr < lastaddr ||
300 (curaddr == lastaddr && curaddr == curpbaddr &&
301 lastaddr != lastpbaddr)) {
302 change_tmp = change_point[i];
303 change_point[i] = change_point[i-1];
304 change_point[i-1] = change_tmp;
305 still_changing = 1;
306 }
307 }
308 }
309
310 /* create a new bios memory map, removing overlaps */
311 overlap_entries = 0; /* number of entries in the overlap table */
312 new_bios_entry = 0; /* index for creating new bios map entries */
313 last_type = 0; /* start with undefined memory type */
314 last_addr = 0; /* start with 0 as last starting address */
315
316 /* loop through change-points, determining affect on the new bios map */
317 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
318 /* keep track of all overlapping bios entries */
319 if (change_point[chgidx]->addr ==
320 change_point[chgidx]->pbios->addr) {
321 /*
322 * add map entry to overlap list (> 1 entry
323 * implies an overlap)
324 */
325 overlap_list[overlap_entries++] =
326 change_point[chgidx]->pbios;
327 } else {
328 /*
329 * remove entry from list (order independent,
330 * so swap with last)
331 */
332 for (i = 0; i < overlap_entries; i++) {
333 if (overlap_list[i] ==
334 change_point[chgidx]->pbios)
335 overlap_list[i] =
336 overlap_list[overlap_entries-1];
337 }
338 overlap_entries--;
339 }
340 /*
341 * if there are overlapping entries, decide which
342 * "type" to use (larger value takes precedence --
343 * 1=usable, 2,3,4,4+=unusable)
344 */
345 current_type = 0;
346 for (i = 0; i < overlap_entries; i++)
347 if (overlap_list[i]->type > current_type)
348 current_type = overlap_list[i]->type;
349 /*
350 * continue building up new bios map based on this
351 * information
352 */
353 if (current_type != last_type) {
354 if (last_type != 0) {
355 new_bios[new_bios_entry].size =
356 change_point[chgidx]->addr - last_addr;
357 /*
358 * move forward only if the new size
359 * was non-zero
360 */
361 if (new_bios[new_bios_entry].size != 0)
362 /*
363 * no more space left for new
364 * bios entries ?
365 */
Paul Jacksonc3965bd2008-05-14 08:15:34 -0700366 if (++new_bios_entry >= max_nr_map)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700367 break;
368 }
369 if (current_type != 0) {
370 new_bios[new_bios_entry].addr =
371 change_point[chgidx]->addr;
372 new_bios[new_bios_entry].type = current_type;
373 last_addr = change_point[chgidx]->addr;
374 }
375 last_type = current_type;
376 }
377 }
378 /* retain count for new bios entries */
379 new_nr = new_bios_entry;
380
381 /* copy new bios mapping into original location */
382 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
383 *pnr_map = new_nr;
384
385 return 0;
386}
387
Yinghai Ludc8e8122008-07-01 20:02:16 -0700388static int __init __append_e820_map(struct e820entry *biosmap, int nr_map)
Huang, Ying8c5beb52008-06-11 11:33:39 +0800389{
390 while (nr_map) {
391 u64 start = biosmap->addr;
392 u64 size = biosmap->size;
393 u64 end = start + size;
394 u32 type = biosmap->type;
395
396 /* Overflow in 64 bits? Ignore the memory map. */
397 if (start > end)
398 return -1;
399
400 e820_add_region(start, size, type);
401
402 biosmap++;
403 nr_map--;
404 }
405 return 0;
406}
407
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700408/*
409 * Copy the BIOS e820 map into a safe place.
410 *
411 * Sanity-check it while we're at it..
412 *
413 * If we're lucky and live on a modern system, the setup code
414 * will have given us a memory map that we can use to properly
415 * set up memory. If we aren't, we'll fake a memory map.
416 */
Yinghai Ludc8e8122008-07-01 20:02:16 -0700417static int __init append_e820_map(struct e820entry *biosmap, int nr_map)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700418{
419 /* Only one memory region (or negative)? Ignore it */
420 if (nr_map < 2)
421 return -1;
422
Yinghai Ludc8e8122008-07-01 20:02:16 -0700423 return __append_e820_map(biosmap, nr_map);
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700424}
425
Yinghai Lu773e6732009-03-12 21:35:18 -0700426static u64 __init __e820_update_range(struct e820map *e820x, u64 start,
Yinghai Lufc9036e2008-07-03 11:39:00 -0700427 u64 size, unsigned old_type,
428 unsigned new_type)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700429{
Yinghai Lu773e6732009-03-12 21:35:18 -0700430 unsigned int i;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700431 u64 real_updated_size = 0;
432
433 BUG_ON(old_type == new_type);
434
Yinghai Lu232b9572008-06-24 14:58:38 -0700435 if (size > (ULLONG_MAX - start))
436 size = ULLONG_MAX - start;
437
Jan Beulich5c0e6f02009-03-12 13:07:23 +0000438 for (i = 0; i < e820x->nr_map; i++) {
Yinghai Lufc9036e2008-07-03 11:39:00 -0700439 struct e820entry *ei = &e820x->map[i];
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700440 u64 final_start, final_end;
441 if (ei->type != old_type)
442 continue;
443 /* totally covered? */
444 if (ei->addr >= start &&
445 (ei->addr + ei->size) <= (start + size)) {
446 ei->type = new_type;
447 real_updated_size += ei->size;
448 continue;
449 }
450 /* partially covered */
451 final_start = max(start, ei->addr);
452 final_end = min(start + size, ei->addr + ei->size);
453 if (final_start >= final_end)
454 continue;
Jan Beulich5c0e6f02009-03-12 13:07:23 +0000455
Yinghai Lu773e6732009-03-12 21:35:18 -0700456 __e820_add_region(e820x, final_start, final_end - final_start,
457 new_type);
Jan Beulich5c0e6f02009-03-12 13:07:23 +0000458
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700459 real_updated_size += final_end - final_start;
Yinghai Lu976dd4d2008-06-24 14:55:32 -0700460
Yinghai Lu773e6732009-03-12 21:35:18 -0700461 /*
462 * left range could be head or tail, so need to update
463 * size at first.
464 */
465 ei->size -= final_end - final_start;
Yinghai Lu976dd4d2008-06-24 14:55:32 -0700466 if (ei->addr < final_start)
467 continue;
468 ei->addr = final_end;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700469 }
470 return real_updated_size;
471}
472
Yinghai Lufc9036e2008-07-03 11:39:00 -0700473u64 __init e820_update_range(u64 start, u64 size, unsigned old_type,
474 unsigned new_type)
475{
Yinghai Lu773e6732009-03-12 21:35:18 -0700476 return __e820_update_range(&e820, start, size, old_type, new_type);
Yinghai Lufc9036e2008-07-03 11:39:00 -0700477}
478
479static u64 __init e820_update_range_saved(u64 start, u64 size,
480 unsigned old_type, unsigned new_type)
481{
Yinghai Lu773e6732009-03-12 21:35:18 -0700482 return __e820_update_range(&e820_saved, start, size, old_type,
Yinghai Lufc9036e2008-07-03 11:39:00 -0700483 new_type);
484}
485
Yinghai Lu7a1fd982008-06-21 14:48:05 -0700486/* make e820 not cover the range */
487u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type,
488 int checktype)
489{
490 int i;
491 u64 real_removed_size = 0;
492
Yinghai Lu232b9572008-06-24 14:58:38 -0700493 if (size > (ULLONG_MAX - start))
494 size = ULLONG_MAX - start;
495
Yinghai Lu7a1fd982008-06-21 14:48:05 -0700496 for (i = 0; i < e820.nr_map; i++) {
497 struct e820entry *ei = &e820.map[i];
498 u64 final_start, final_end;
499
500 if (checktype && ei->type != old_type)
501 continue;
502 /* totally covered? */
503 if (ei->addr >= start &&
504 (ei->addr + ei->size) <= (start + size)) {
505 real_removed_size += ei->size;
506 memset(ei, 0, sizeof(struct e820entry));
507 continue;
508 }
509 /* partially covered */
510 final_start = max(start, ei->addr);
511 final_end = min(start + size, ei->addr + ei->size);
512 if (final_start >= final_end)
513 continue;
514 real_removed_size += final_end - final_start;
515
516 ei->size -= final_end - final_start;
517 if (ei->addr < final_start)
518 continue;
519 ei->addr = final_end;
520 }
521 return real_removed_size;
522}
523
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700524void __init update_e820(void)
525{
Paul Jackson6e9bcc72008-05-14 08:15:46 -0700526 int nr_map;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700527
528 nr_map = e820.nr_map;
Paul Jacksonc3965bd2008-05-14 08:15:34 -0700529 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map))
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700530 return;
531 e820.nr_map = nr_map;
532 printk(KERN_INFO "modified physical RAM map:\n");
533 e820_print_map("modified");
534}
Yinghai Lufc9036e2008-07-03 11:39:00 -0700535static void __init update_e820_saved(void)
536{
537 int nr_map;
538
539 nr_map = e820_saved.nr_map;
540 if (sanitize_e820_map(e820_saved.map, ARRAY_SIZE(e820_saved.map), &nr_map))
541 return;
542 e820_saved.nr_map = nr_map;
543}
Alok Katariafd6493e2008-06-25 11:02:42 -0700544#define MAX_GAP_END 0x100000000ull
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700545/*
Alok Katariafd6493e2008-06-25 11:02:42 -0700546 * Search for a gap in the e820 memory space from start_addr to end_addr.
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700547 */
Alok Kataria33819592008-06-24 11:48:30 -0700548__init int e820_search_gap(unsigned long *gapstart, unsigned long *gapsize,
Alok Katariafd6493e2008-06-25 11:02:42 -0700549 unsigned long start_addr, unsigned long long end_addr)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700550{
Alok Katariafd6493e2008-06-25 11:02:42 -0700551 unsigned long long last;
Alok Kataria33819592008-06-24 11:48:30 -0700552 int i = e820.nr_map;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700553 int found = 0;
554
Alok Katariafd6493e2008-06-25 11:02:42 -0700555 last = (end_addr && end_addr < MAX_GAP_END) ? end_addr : MAX_GAP_END;
556
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700557 while (--i >= 0) {
558 unsigned long long start = e820.map[i].addr;
559 unsigned long long end = start + e820.map[i].size;
560
Alok Kataria33819592008-06-24 11:48:30 -0700561 if (end < start_addr)
562 continue;
563
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700564 /*
565 * Since "last" is at most 4GB, we know we'll
566 * fit in 32 bits if this condition is true
567 */
568 if (last > end) {
569 unsigned long gap = last - end;
570
Alok Kataria33819592008-06-24 11:48:30 -0700571 if (gap >= *gapsize) {
572 *gapsize = gap;
573 *gapstart = end;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700574 found = 1;
575 }
576 }
577 if (start < last)
578 last = start;
579 }
Alok Kataria33819592008-06-24 11:48:30 -0700580 return found;
581}
582
583/*
584 * Search for the biggest gap in the low 32 bits of the e820
585 * memory space. We pass this space to PCI to assign MMIO resources
586 * for hotplug or unconfigured devices in.
587 * Hopefully the BIOS let enough space left.
588 */
589__init void e820_setup_gap(void)
590{
591 unsigned long gapstart, gapsize, round;
592 int found;
593
594 gapstart = 0x10000000;
595 gapsize = 0x400000;
Alok Katariafd6493e2008-06-25 11:02:42 -0700596 found = e820_search_gap(&gapstart, &gapsize, 0, MAX_GAP_END);
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700597
598#ifdef CONFIG_X86_64
599 if (!found) {
Yinghai Luc987d122008-06-24 22:14:09 -0700600 gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700601 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
602 "address range\n"
603 KERN_ERR "PCI: Unassigned devices with 32bit resource "
604 "registers may break!\n");
605 }
606#endif
607
608 /*
609 * See how much we want to round up: start off with
610 * rounding to the next 1MB area.
611 */
612 round = 0x100000;
613 while ((gapsize >> 4) > round)
614 round += round;
615 /* Fun with two's complement */
616 pci_mem_start = (gapstart + round) & -round;
617
618 printk(KERN_INFO
619 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
620 pci_mem_start, gapstart, gapsize);
621}
622
Huang, Ying8c5beb52008-06-11 11:33:39 +0800623/**
624 * Because of the size limitation of struct boot_params, only first
625 * 128 E820 memory entries are passed to kernel via
626 * boot_params.e820_map, others are passed via SETUP_E820_EXT node of
627 * linked list of struct setup_data, which is parsed here.
628 */
629void __init parse_e820_ext(struct setup_data *sdata, unsigned long pa_data)
630{
631 u32 map_len;
632 int entries;
633 struct e820entry *extmap;
634
635 entries = sdata->len / sizeof(struct e820entry);
636 map_len = sdata->len + sizeof(struct setup_data);
637 if (map_len > PAGE_SIZE)
638 sdata = early_ioremap(pa_data, map_len);
639 extmap = (struct e820entry *)(sdata->data);
Yinghai Ludc8e8122008-07-01 20:02:16 -0700640 __append_e820_map(extmap, entries);
Huang, Ying8c5beb52008-06-11 11:33:39 +0800641 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
642 if (map_len > PAGE_SIZE)
643 early_iounmap(sdata, map_len);
644 printk(KERN_INFO "extended physical RAM map:\n");
645 e820_print_map("extended");
646}
647
Yinghai Lubf62f392008-05-20 20:10:58 -0700648#if defined(CONFIG_X86_64) || \
649 (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION))
650/**
651 * Find the ranges of physical addresses that do not correspond to
652 * e820 RAM areas and mark the corresponding pages as nosave for
653 * hibernation (32 bit) or software suspend and suspend to RAM (64 bit).
654 *
655 * This function requires the e820 map to be sorted and without any
656 * overlapping entries and assumes the first e820 area to be RAM.
657 */
658void __init e820_mark_nosave_regions(unsigned long limit_pfn)
659{
660 int i;
661 unsigned long pfn;
662
663 pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
664 for (i = 1; i < e820.nr_map; i++) {
665 struct e820entry *ei = &e820.map[i];
666
667 if (pfn < PFN_UP(ei->addr))
668 register_nosave_region(pfn, PFN_UP(ei->addr));
669
670 pfn = PFN_DOWN(ei->addr + ei->size);
Yinghai Lu28bb2232008-06-30 16:20:54 -0700671 if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
Yinghai Lubf62f392008-05-20 20:10:58 -0700672 register_nosave_region(PFN_UP(ei->addr), pfn);
673
674 if (pfn >= limit_pfn)
675 break;
676 }
677}
678#endif
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700679
Rafael J. Wysockib69edc72008-10-31 01:02:41 +0100680#ifdef CONFIG_HIBERNATION
681/**
682 * Mark ACPI NVS memory region, so that we can save/restore it during
683 * hibernation and the subsequent resume.
684 */
685static int __init e820_mark_nvs_memory(void)
686{
687 int i;
688
689 for (i = 0; i < e820.nr_map; i++) {
690 struct e820entry *ei = &e820.map[i];
691
692 if (ei->type == E820_NVS)
693 hibernate_nvs_register(ei->addr, ei->size);
694 }
695
696 return 0;
697}
698core_initcall(e820_mark_nvs_memory);
699#endif
700
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700701/*
702 * Early reserved memory areas.
703 */
704#define MAX_EARLY_RES 20
705
706struct early_res {
707 u64 start, end;
708 char name[16];
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700709 char overlap_ok;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700710};
711static struct early_res early_res[MAX_EARLY_RES] __initdata = {
712 { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700713 {}
714};
715
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800716static int __init find_overlapped_early(u64 start, u64 end)
717{
718 int i;
719 struct early_res *r;
720
721 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
722 r = &early_res[i];
723 if (end > r->start && start < r->end)
724 break;
725 }
726
727 return i;
728}
729
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700730/*
731 * Drop the i-th range from the early reservation map,
732 * by copying any higher ranges down one over it, and
733 * clearing what had been the last slot.
734 */
735static void __init drop_range(int i)
736{
737 int j;
738
739 for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++)
740 ;
741
742 memmove(&early_res[i], &early_res[i + 1],
743 (j - 1 - i) * sizeof(struct early_res));
744
745 early_res[j - 1].end = 0;
746}
747
748/*
749 * Split any existing ranges that:
750 * 1) are marked 'overlap_ok', and
751 * 2) overlap with the stated range [start, end)
752 * into whatever portion (if any) of the existing range is entirely
753 * below or entirely above the stated range. Drop the portion
754 * of the existing range that overlaps with the stated range,
755 * which will allow the caller of this routine to then add that
756 * stated range without conflicting with any existing range.
757 */
758static void __init drop_overlaps_that_are_ok(u64 start, u64 end)
759{
760 int i;
761 struct early_res *r;
762 u64 lower_start, lower_end;
763 u64 upper_start, upper_end;
764 char name[16];
765
766 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
767 r = &early_res[i];
768
769 /* Continue past non-overlapping ranges */
770 if (end <= r->start || start >= r->end)
771 continue;
772
773 /*
774 * Leave non-ok overlaps as is; let caller
775 * panic "Overlapping early reservations"
776 * when it hits this overlap.
777 */
778 if (!r->overlap_ok)
779 return;
780
781 /*
782 * We have an ok overlap. We will drop it from the early
783 * reservation map, and add back in any non-overlapping
784 * portions (lower or upper) as separate, overlap_ok,
785 * non-overlapping ranges.
786 */
787
788 /* 1. Note any non-overlapping (lower or upper) ranges. */
789 strncpy(name, r->name, sizeof(name) - 1);
790
791 lower_start = lower_end = 0;
792 upper_start = upper_end = 0;
793 if (r->start < start) {
794 lower_start = r->start;
795 lower_end = start;
796 }
797 if (r->end > end) {
798 upper_start = end;
799 upper_end = r->end;
800 }
801
802 /* 2. Drop the original ok overlapping range */
803 drop_range(i);
804
805 i--; /* resume for-loop on copied down entry */
806
807 /* 3. Add back in any non-overlapping ranges. */
808 if (lower_end)
809 reserve_early_overlap_ok(lower_start, lower_end, name);
810 if (upper_end)
811 reserve_early_overlap_ok(upper_start, upper_end, name);
812 }
813}
814
815static void __init __reserve_early(u64 start, u64 end, char *name,
816 int overlap_ok)
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700817{
818 int i;
819 struct early_res *r;
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800820
821 i = find_overlapped_early(start, end);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700822 if (i >= MAX_EARLY_RES)
823 panic("Too many early reservations");
824 r = &early_res[i];
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800825 if (r->end)
826 panic("Overlapping early reservations "
827 "%llx-%llx %s to %llx-%llx %s\n",
828 start, end - 1, name?name:"", r->start,
829 r->end - 1, r->name);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700830 r->start = start;
831 r->end = end;
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700832 r->overlap_ok = overlap_ok;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700833 if (name)
834 strncpy(r->name, name, sizeof(r->name) - 1);
835}
836
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700837/*
838 * A few early reservtations come here.
839 *
840 * The 'overlap_ok' in the name of this routine does -not- mean it
841 * is ok for these reservations to overlap an earlier reservation.
842 * Rather it means that it is ok for subsequent reservations to
843 * overlap this one.
844 *
845 * Use this entry point to reserve early ranges when you are doing
846 * so out of "Paranoia", reserving perhaps more memory than you need,
847 * just in case, and don't mind a subsequent overlapping reservation
848 * that is known to be needed.
849 *
850 * The drop_overlaps_that_are_ok() call here isn't really needed.
851 * It would be needed if we had two colliding 'overlap_ok'
852 * reservations, so that the second such would not panic on the
853 * overlap with the first. We don't have any such as of this
854 * writing, but might as well tolerate such if it happens in
855 * the future.
856 */
857void __init reserve_early_overlap_ok(u64 start, u64 end, char *name)
858{
859 drop_overlaps_that_are_ok(start, end);
860 __reserve_early(start, end, name, 1);
861}
862
863/*
864 * Most early reservations come here.
865 *
866 * We first have drop_overlaps_that_are_ok() drop any pre-existing
867 * 'overlap_ok' ranges, so that we can then reserve this memory
868 * range without risk of panic'ing on an overlapping overlap_ok
869 * early reservation.
870 */
871void __init reserve_early(u64 start, u64 end, char *name)
872{
Yinghai Lu46cb27f2009-02-24 13:12:43 -0800873 if (start >= end)
874 return;
875
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700876 drop_overlaps_that_are_ok(start, end);
877 __reserve_early(start, end, name, 0);
878}
879
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700880void __init free_early(u64 start, u64 end)
881{
882 struct early_res *r;
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700883 int i;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700884
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800885 i = find_overlapped_early(start, end);
886 r = &early_res[i];
887 if (i >= MAX_EARLY_RES || r->end != end || r->start != start)
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700888 panic("free_early on not reserved area: %llx-%llx!",
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800889 start, end - 1);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700890
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700891 drop_range(i);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700892}
893
894void __init early_res_to_bootmem(u64 start, u64 end)
895{
Yinghai Luab677152008-06-27 15:36:54 -0700896 int i, count;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700897 u64 final_start, final_end;
Yinghai Luab677152008-06-27 15:36:54 -0700898
899 count = 0;
900 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++)
901 count++;
902
Yinghai Lu5f1f2b32008-07-18 16:16:23 -0700903 printk(KERN_INFO "(%d early reservations) ==> bootmem [%010llx - %010llx]\n",
904 count, start, end);
Yinghai Luab677152008-06-27 15:36:54 -0700905 for (i = 0; i < count; i++) {
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700906 struct early_res *r = &early_res[i];
Yinghai Lu4fcc5452008-07-01 20:03:11 -0700907 printk(KERN_INFO " #%d [%010llx - %010llx] %16s", i,
Yinghai Luab677152008-06-27 15:36:54 -0700908 r->start, r->end, r->name);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700909 final_start = max(start, r->start);
910 final_end = min(end, r->end);
Yinghai Luab677152008-06-27 15:36:54 -0700911 if (final_start >= final_end) {
912 printk(KERN_CONT "\n");
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700913 continue;
Yinghai Luab677152008-06-27 15:36:54 -0700914 }
Yinghai Lu4fcc5452008-07-01 20:03:11 -0700915 printk(KERN_CONT " ==> [%010llx - %010llx]\n",
Yinghai Luab677152008-06-27 15:36:54 -0700916 final_start, final_end);
Yinghai Lud2dbf342008-06-13 02:00:56 -0700917 reserve_bootmem_generic(final_start, final_end - final_start,
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700918 BOOTMEM_DEFAULT);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700919 }
920}
921
922/* Check for already reserved areas */
923static inline int __init bad_addr(u64 *addrp, u64 size, u64 align)
924{
925 int i;
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800926 u64 addr = *addrp;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700927 int changed = 0;
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800928 struct early_res *r;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700929again:
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800930 i = find_overlapped_early(addr, addr + size);
931 r = &early_res[i];
932 if (i < MAX_EARLY_RES && r->end) {
933 *addrp = addr = round_up(r->end, align);
934 changed = 1;
935 goto again;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700936 }
937 return changed;
938}
939
940/* Check for already reserved areas */
941static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align)
942{
943 int i;
944 u64 addr = *addrp, last;
945 u64 size = *sizep;
946 int changed = 0;
947again:
948 last = addr + size;
949 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
950 struct early_res *r = &early_res[i];
951 if (last > r->start && addr < r->start) {
952 size = r->start - addr;
953 changed = 1;
954 goto again;
955 }
956 if (last > r->end && addr < r->end) {
957 addr = round_up(r->end, align);
958 size = last - addr;
959 changed = 1;
960 goto again;
961 }
962 if (last <= r->end && addr >= r->start) {
963 (*sizep)++;
964 return 0;
965 }
966 }
967 if (changed) {
968 *addrp = addr;
969 *sizep = size;
970 }
971 return changed;
972}
973
974/*
975 * Find a free area with specified alignment in a specific range.
976 */
977u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align)
978{
979 int i;
980
981 for (i = 0; i < e820.nr_map; i++) {
982 struct e820entry *ei = &e820.map[i];
983 u64 addr, last;
984 u64 ei_last;
985
986 if (ei->type != E820_RAM)
987 continue;
988 addr = round_up(ei->addr, align);
989 ei_last = ei->addr + ei->size;
990 if (addr < start)
991 addr = round_up(start, align);
992 if (addr >= ei_last)
993 continue;
994 while (bad_addr(&addr, size, align) && addr+size <= ei_last)
995 ;
996 last = addr + size;
997 if (last > ei_last)
998 continue;
999 if (last > end)
1000 continue;
1001 return addr;
1002 }
1003 return -1ULL;
1004}
1005
1006/*
1007 * Find next free range after *start
1008 */
1009u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align)
1010{
1011 int i;
1012
1013 for (i = 0; i < e820.nr_map; i++) {
1014 struct e820entry *ei = &e820.map[i];
1015 u64 addr, last;
1016 u64 ei_last;
1017
1018 if (ei->type != E820_RAM)
1019 continue;
1020 addr = round_up(ei->addr, align);
1021 ei_last = ei->addr + ei->size;
1022 if (addr < start)
1023 addr = round_up(start, align);
1024 if (addr >= ei_last)
1025 continue;
1026 *sizep = ei_last - addr;
1027 while (bad_addr_size(&addr, sizep, align) &&
1028 addr + *sizep <= ei_last)
1029 ;
1030 last = addr + *sizep;
1031 if (last > ei_last)
1032 continue;
1033 return addr;
1034 }
Yinghai Lua4c81cf2008-05-18 01:18:57 -07001035
Jan Beulich5c0e6f02009-03-12 13:07:23 +00001036 return -1ULL;
Yinghai Lua4c81cf2008-05-18 01:18:57 -07001037}
Yinghai Lu2944e162008-06-01 13:17:38 -07001038
1039/*
1040 * pre allocated 4k and reserved it in e820
1041 */
1042u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align)
1043{
1044 u64 size = 0;
1045 u64 addr;
1046 u64 start;
1047
1048 start = startt;
Jan Beulich5c0e6f02009-03-12 13:07:23 +00001049 while (size < sizet && (start + 1))
Yinghai Lu2944e162008-06-01 13:17:38 -07001050 start = find_e820_area_size(start, &size, align);
1051
1052 if (size < sizet)
1053 return 0;
1054
Jan Beulich5c0e6f02009-03-12 13:07:23 +00001055#ifdef CONFIG_X86_32
1056 if (start >= MAXMEM)
1057 return 0;
1058 if (start + size > MAXMEM)
1059 size = MAXMEM - start;
1060#endif
1061
Yinghai Lu2944e162008-06-01 13:17:38 -07001062 addr = round_down(start + size - sizet, align);
Jan Beulich5c0e6f02009-03-12 13:07:23 +00001063 if (addr < start)
1064 return 0;
Yinghai Lud0be6bd2008-06-15 18:58:51 -07001065 e820_update_range(addr, sizet, E820_RAM, E820_RESERVED);
Yinghai Lufc9036e2008-07-03 11:39:00 -07001066 e820_update_range_saved(addr, sizet, E820_RAM, E820_RESERVED);
Yinghai Lu2944e162008-06-01 13:17:38 -07001067 printk(KERN_INFO "update e820 for early_reserve_e820\n");
1068 update_e820();
Yinghai Lufc9036e2008-07-03 11:39:00 -07001069 update_e820_saved();
Yinghai Lu2944e162008-06-01 13:17:38 -07001070
1071 return addr;
1072}
1073
Yinghai Luee0c80f2008-06-03 19:34:00 -07001074#ifdef CONFIG_X86_32
1075# ifdef CONFIG_X86_PAE
1076# define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
1077# else
1078# define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
1079# endif
1080#else /* CONFIG_X86_32 */
Yinghai Lubd70e522008-06-04 13:21:29 -07001081# define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
Yinghai Luee0c80f2008-06-03 19:34:00 -07001082#endif
1083
1084/*
Yinghai Luee0c80f2008-06-03 19:34:00 -07001085 * Find the highest page frame number we have available
1086 */
Yinghai Luf361a452008-07-10 20:38:26 -07001087static unsigned long __init e820_end_pfn(unsigned long limit_pfn, unsigned type)
Yinghai Luee0c80f2008-06-03 19:34:00 -07001088{
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001089 int i;
1090 unsigned long last_pfn = 0;
Yinghai Luee0c80f2008-06-03 19:34:00 -07001091 unsigned long max_arch_pfn = MAX_ARCH_PFN;
1092
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001093 for (i = 0; i < e820.nr_map; i++) {
1094 struct e820entry *ei = &e820.map[i];
Yinghai Luf361a452008-07-10 20:38:26 -07001095 unsigned long start_pfn;
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001096 unsigned long end_pfn;
1097
Yinghai Luf361a452008-07-10 20:38:26 -07001098 if (ei->type != type)
Yinghai Luc22d4c12008-07-09 03:01:14 -07001099 continue;
Yinghai Luc22d4c12008-07-09 03:01:14 -07001100
Yinghai Luf361a452008-07-10 20:38:26 -07001101 start_pfn = ei->addr >> PAGE_SHIFT;
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001102 end_pfn = (ei->addr + ei->size) >> PAGE_SHIFT;
Yinghai Luf361a452008-07-10 20:38:26 -07001103
1104 if (start_pfn >= limit_pfn)
1105 continue;
1106 if (end_pfn > limit_pfn) {
1107 last_pfn = limit_pfn;
1108 break;
1109 }
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001110 if (end_pfn > last_pfn)
1111 last_pfn = end_pfn;
1112 }
Yinghai Luee0c80f2008-06-03 19:34:00 -07001113
1114 if (last_pfn > max_arch_pfn)
1115 last_pfn = max_arch_pfn;
Yinghai Luee0c80f2008-06-03 19:34:00 -07001116
Paul Jackson5dab8ec2008-06-25 05:44:40 -07001117 printk(KERN_INFO "last_pfn = %#lx max_arch_pfn = %#lx\n",
Yinghai Luee0c80f2008-06-03 19:34:00 -07001118 last_pfn, max_arch_pfn);
1119 return last_pfn;
1120}
Yinghai Luf361a452008-07-10 20:38:26 -07001121unsigned long __init e820_end_of_ram_pfn(void)
1122{
1123 return e820_end_pfn(MAX_ARCH_PFN, E820_RAM);
1124}
Yinghai Luee0c80f2008-06-03 19:34:00 -07001125
Yinghai Luf361a452008-07-10 20:38:26 -07001126unsigned long __init e820_end_of_low_ram_pfn(void)
1127{
1128 return e820_end_pfn(1UL<<(32 - PAGE_SHIFT), E820_RAM);
1129}
Yinghai Luee0c80f2008-06-03 19:34:00 -07001130/*
1131 * Finds an active region in the address range from start_pfn to last_pfn and
1132 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
1133 */
1134int __init e820_find_active_region(const struct e820entry *ei,
1135 unsigned long start_pfn,
1136 unsigned long last_pfn,
1137 unsigned long *ei_startpfn,
1138 unsigned long *ei_endpfn)
1139{
1140 u64 align = PAGE_SIZE;
1141
1142 *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT;
1143 *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT;
1144
1145 /* Skip map entries smaller than a page */
1146 if (*ei_startpfn >= *ei_endpfn)
1147 return 0;
1148
1149 /* Skip if map is outside the node */
1150 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
1151 *ei_startpfn >= last_pfn)
1152 return 0;
1153
1154 /* Check for overlaps */
1155 if (*ei_startpfn < start_pfn)
1156 *ei_startpfn = start_pfn;
1157 if (*ei_endpfn > last_pfn)
1158 *ei_endpfn = last_pfn;
1159
Yinghai Luee0c80f2008-06-03 19:34:00 -07001160 return 1;
1161}
1162
1163/* Walk the e820 map and register active regions within a node */
1164void __init e820_register_active_regions(int nid, unsigned long start_pfn,
1165 unsigned long last_pfn)
1166{
1167 unsigned long ei_startpfn;
1168 unsigned long ei_endpfn;
1169 int i;
1170
1171 for (i = 0; i < e820.nr_map; i++)
1172 if (e820_find_active_region(&e820.map[i],
1173 start_pfn, last_pfn,
1174 &ei_startpfn, &ei_endpfn))
1175 add_active_range(nid, ei_startpfn, ei_endpfn);
1176}
1177
1178/*
1179 * Find the hole size (in bytes) in the memory range.
1180 * @start: starting address of the memory range to scan
1181 * @end: ending address of the memory range to scan
1182 */
1183u64 __init e820_hole_size(u64 start, u64 end)
1184{
1185 unsigned long start_pfn = start >> PAGE_SHIFT;
1186 unsigned long last_pfn = end >> PAGE_SHIFT;
1187 unsigned long ei_startpfn, ei_endpfn, ram = 0;
1188 int i;
1189
1190 for (i = 0; i < e820.nr_map; i++) {
1191 if (e820_find_active_region(&e820.map[i],
1192 start_pfn, last_pfn,
1193 &ei_startpfn, &ei_endpfn))
1194 ram += ei_endpfn - ei_startpfn;
1195 }
1196 return end - start - ((u64)ram << PAGE_SHIFT);
1197}
Yinghai Luab4a4652008-06-10 12:55:54 -07001198
1199static void early_panic(char *msg)
1200{
1201 early_printk(msg);
1202 panic(msg);
1203}
1204
Yinghai Lu69a77042008-07-10 04:17:00 -07001205static int userdef __initdata;
1206
Yinghai Luab4a4652008-06-10 12:55:54 -07001207/* "mem=nopentium" disables the 4MB page tables. */
1208static int __init parse_memopt(char *p)
1209{
1210 u64 mem_size;
1211
1212 if (!p)
1213 return -EINVAL;
1214
1215#ifdef CONFIG_X86_32
1216 if (!strcmp(p, "nopentium")) {
1217 setup_clear_cpu_cap(X86_FEATURE_PSE);
1218 return 0;
1219 }
1220#endif
1221
Yinghai Lu69a77042008-07-10 04:17:00 -07001222 userdef = 1;
Yinghai Luab4a4652008-06-10 12:55:54 -07001223 mem_size = memparse(p, &p);
Yinghai Lu69a77042008-07-10 04:17:00 -07001224 e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
Bernhard Walle611dfd72008-06-25 21:39:16 +02001225
Yinghai Luab4a4652008-06-10 12:55:54 -07001226 return 0;
1227}
1228early_param("mem", parse_memopt);
1229
Yinghai Luab4a4652008-06-10 12:55:54 -07001230static int __init parse_memmap_opt(char *p)
1231{
1232 char *oldp;
1233 u64 start_at, mem_size;
1234
Cyrill Gorcunova737abd2008-07-05 15:53:39 +04001235 if (!p)
1236 return -EINVAL;
1237
Prarit Bhargavad6be118a2008-09-09 09:56:08 -04001238 if (!strncmp(p, "exactmap", 8)) {
Yinghai Luab4a4652008-06-10 12:55:54 -07001239#ifdef CONFIG_CRASH_DUMP
1240 /*
1241 * If we are doing a crash dump, we still need to know
1242 * the real mem size before original memory map is
1243 * reset.
1244 */
Yinghai Luf361a452008-07-10 20:38:26 -07001245 saved_max_pfn = e820_end_of_ram_pfn();
Yinghai Luab4a4652008-06-10 12:55:54 -07001246#endif
1247 e820.nr_map = 0;
1248 userdef = 1;
1249 return 0;
1250 }
1251
1252 oldp = p;
1253 mem_size = memparse(p, &p);
1254 if (p == oldp)
1255 return -EINVAL;
1256
1257 userdef = 1;
1258 if (*p == '@') {
1259 start_at = memparse(p+1, &p);
Yinghai Lud0be6bd2008-06-15 18:58:51 -07001260 e820_add_region(start_at, mem_size, E820_RAM);
Yinghai Luab4a4652008-06-10 12:55:54 -07001261 } else if (*p == '#') {
1262 start_at = memparse(p+1, &p);
Yinghai Lud0be6bd2008-06-15 18:58:51 -07001263 e820_add_region(start_at, mem_size, E820_ACPI);
Yinghai Luab4a4652008-06-10 12:55:54 -07001264 } else if (*p == '$') {
1265 start_at = memparse(p+1, &p);
Yinghai Lud0be6bd2008-06-15 18:58:51 -07001266 e820_add_region(start_at, mem_size, E820_RESERVED);
Yinghai Lu7b479be2008-07-12 22:57:07 -07001267 } else
Yinghai Lu69a77042008-07-10 04:17:00 -07001268 e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
Yinghai Lu7b479be2008-07-12 22:57:07 -07001269
Yinghai Luab4a4652008-06-10 12:55:54 -07001270 return *p == '\0' ? 0 : -EINVAL;
1271}
1272early_param("memmap", parse_memmap_opt);
1273
1274void __init finish_e820_parsing(void)
1275{
1276 if (userdef) {
1277 int nr = e820.nr_map;
1278
1279 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
1280 early_panic("Invalid user supplied memory map");
1281 e820.nr_map = nr;
1282
1283 printk(KERN_INFO "user-defined physical RAM map:\n");
1284 e820_print_map("user");
1285 }
1286}
Yinghai Lu41c094f2008-06-16 13:03:31 -07001287
Bernhard Walle5dfcf142008-06-27 13:12:55 +02001288static inline const char *e820_type_to_string(int e820_type)
1289{
1290 switch (e820_type) {
1291 case E820_RESERVED_KERN:
1292 case E820_RAM: return "System RAM";
1293 case E820_ACPI: return "ACPI Tables";
1294 case E820_NVS: return "ACPI Non-volatile Storage";
Cihula, Joseph671eef82008-08-20 16:43:07 -07001295 case E820_UNUSABLE: return "Unusable memory";
Bernhard Walle5dfcf142008-06-27 13:12:55 +02001296 default: return "reserved";
1297 }
1298}
1299
Yinghai Lu41c094f2008-06-16 13:03:31 -07001300/*
1301 * Mark e820 reserved areas as busy for the resource manager.
1302 */
Ingo Molnara5444d12008-08-29 08:09:23 +02001303static struct resource __initdata *e820_res;
Yinghai Lu41c094f2008-06-16 13:03:31 -07001304void __init e820_reserve_resources(void)
1305{
1306 int i;
Yinghai Lu58f7c982008-08-28 13:52:25 -07001307 struct resource *res;
Ingo Molnara5444d12008-08-29 08:09:23 +02001308 u64 end;
Yinghai Lu41c094f2008-06-16 13:03:31 -07001309
1310 res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
Yinghai Lu58f7c982008-08-28 13:52:25 -07001311 e820_res = res;
Yinghai Lu41c094f2008-06-16 13:03:31 -07001312 for (i = 0; i < e820.nr_map; i++) {
Yinghai Lub4df32f2008-06-28 17:49:59 -07001313 end = e820.map[i].addr + e820.map[i].size - 1;
Jeremy Fitzhardinge8308c542008-09-11 01:31:50 -07001314 if (end != (resource_size_t)end) {
Yinghai Lu41c094f2008-06-16 13:03:31 -07001315 res++;
1316 continue;
1317 }
Bernhard Walle5dfcf142008-06-27 13:12:55 +02001318 res->name = e820_type_to_string(e820.map[i].type);
Yinghai Lub4df32f2008-06-28 17:49:59 -07001319 res->start = e820.map[i].addr;
1320 res->end = end;
1321
Linus Torvalds1f987572008-11-01 10:17:22 -07001322 res->flags = IORESOURCE_MEM;
Ingo Molnara5444d12008-08-29 08:09:23 +02001323
1324 /*
1325 * don't register the region that could be conflicted with
1326 * pci device BAR resource and insert them later in
1327 * pcibios_resource_survey()
1328 */
Linus Torvalds1f987572008-11-01 10:17:22 -07001329 if (e820.map[i].type != E820_RESERVED || res->start < (1ULL<<20)) {
1330 res->flags |= IORESOURCE_BUSY;
Yinghai Lu58f7c982008-08-28 13:52:25 -07001331 insert_resource(&iomem_resource, res);
Linus Torvalds1f987572008-11-01 10:17:22 -07001332 }
Yinghai Lu41c094f2008-06-16 13:03:31 -07001333 res++;
1334 }
Bernhard Walle5dfcf142008-06-27 13:12:55 +02001335
1336 for (i = 0; i < e820_saved.nr_map; i++) {
1337 struct e820entry *entry = &e820_saved.map[i];
1338 firmware_map_add_early(entry->addr,
1339 entry->addr + entry->size - 1,
1340 e820_type_to_string(entry->type));
1341 }
Yinghai Lu41c094f2008-06-16 13:03:31 -07001342}
1343
Yinghai Lu58f7c982008-08-28 13:52:25 -07001344void __init e820_reserve_resources_late(void)
1345{
1346 int i;
1347 struct resource *res;
1348
1349 res = e820_res;
1350 for (i = 0; i < e820.nr_map; i++) {
Ingo Molnara5444d12008-08-29 08:09:23 +02001351 if (!res->parent && res->end)
Linus Torvalds1f987572008-11-01 10:17:22 -07001352 insert_resource_expand_to_fit(&iomem_resource, res);
Yinghai Lu58f7c982008-08-28 13:52:25 -07001353 res++;
1354 }
1355}
1356
Yinghai Lu95a71a42008-06-18 17:27:08 -07001357char *__init default_machine_specific_memory_setup(void)
Yinghai Lu064d25f2008-06-16 19:58:28 -07001358{
1359 char *who = "BIOS-e820";
1360 int new_nr;
1361 /*
1362 * Try to copy the BIOS-supplied E820-map.
1363 *
1364 * Otherwise fake a memory map; one section from 0k->640k,
1365 * the next section from 1mb->appropriate_mem_k
1366 */
1367 new_nr = boot_params.e820_entries;
1368 sanitize_e820_map(boot_params.e820_map,
1369 ARRAY_SIZE(boot_params.e820_map),
1370 &new_nr);
1371 boot_params.e820_entries = new_nr;
Yinghai Ludc8e8122008-07-01 20:02:16 -07001372 if (append_e820_map(boot_params.e820_map, boot_params.e820_entries)
1373 < 0) {
Yinghai Lu95a71a42008-06-18 17:27:08 -07001374 u64 mem_size;
Yinghai Lu41c094f2008-06-16 13:03:31 -07001375
Yinghai Lu064d25f2008-06-16 19:58:28 -07001376 /* compare results from other methods and take the greater */
1377 if (boot_params.alt_mem_k
1378 < boot_params.screen_info.ext_mem_k) {
1379 mem_size = boot_params.screen_info.ext_mem_k;
1380 who = "BIOS-88";
1381 } else {
1382 mem_size = boot_params.alt_mem_k;
1383 who = "BIOS-e801";
1384 }
1385
1386 e820.nr_map = 0;
1387 e820_add_region(0, LOWMEMSIZE(), E820_RAM);
1388 e820_add_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
Yinghai Lu064d25f2008-06-16 19:58:28 -07001389 }
1390
1391 /* In case someone cares... */
1392 return who;
1393}
1394
Yinghai Lu95a71a42008-06-18 17:27:08 -07001395char *__init __attribute__((weak)) machine_specific_memory_setup(void)
1396{
Yinghai Lu3c9cb6d2008-07-19 02:07:25 -07001397 if (x86_quirks->arch_memory_setup) {
1398 char *who = x86_quirks->arch_memory_setup();
Ingo Molnar3b335532008-07-10 17:30:40 +02001399
1400 if (who)
1401 return who;
1402 }
Yinghai Lu95a71a42008-06-18 17:27:08 -07001403 return default_machine_specific_memory_setup();
1404}
1405
Yinghai Lu064d25f2008-06-16 19:58:28 -07001406/* Overridden in paravirt.c if CONFIG_PARAVIRT */
1407char * __init __attribute__((weak)) memory_setup(void)
1408{
1409 return machine_specific_memory_setup();
1410}
1411
1412void __init setup_memory_map(void)
1413{
Yinghai Lu0be15522008-07-03 11:35:37 -07001414 char *who;
1415
1416 who = memory_setup();
1417 memcpy(&e820_saved, &e820, sizeof(struct e820map));
Yinghai Lu064d25f2008-06-16 19:58:28 -07001418 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
Yinghai Lu0be15522008-07-03 11:35:37 -07001419 e820_print_map(who);
Yinghai Lu064d25f2008-06-16 19:58:28 -07001420}