blob: 3cf6681ac80d51277769b4b446768ce8664b19b9 [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 Lud0be6bd2008-06-15 18:58:51 -0700113void __init e820_add_region(u64 start, u64 size, int type)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700114{
115 int x = e820.nr_map;
116
Paul Jacksonc3965bd2008-05-14 08:15:34 -0700117 if (x == ARRAY_SIZE(e820.map)) {
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700118 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
119 return;
120 }
121
122 e820.map[x].addr = start;
123 e820.map[x].size = size;
124 e820.map[x].type = type;
125 e820.nr_map++;
126}
127
128void __init e820_print_map(char *who)
129{
130 int i;
131
132 for (i = 0; i < e820.nr_map; i++) {
133 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
134 (unsigned long long) e820.map[i].addr,
135 (unsigned long long)
136 (e820.map[i].addr + e820.map[i].size));
137 switch (e820.map[i].type) {
138 case E820_RAM:
Yinghai Lu28bb2232008-06-30 16:20:54 -0700139 case E820_RESERVED_KERN:
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700140 printk(KERN_CONT "(usable)\n");
141 break;
142 case E820_RESERVED:
143 printk(KERN_CONT "(reserved)\n");
144 break;
145 case E820_ACPI:
146 printk(KERN_CONT "(ACPI data)\n");
147 break;
148 case E820_NVS:
149 printk(KERN_CONT "(ACPI NVS)\n");
150 break;
Cihula, Joseph671eef82008-08-20 16:43:07 -0700151 case E820_UNUSABLE:
152 printk("(unusable)\n");
153 break;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700154 default:
155 printk(KERN_CONT "type %u\n", e820.map[i].type);
156 break;
157 }
158 }
159}
160
161/*
162 * Sanitize the BIOS e820 map.
163 *
164 * Some e820 responses include overlapping entries. The following
Paul Jackson5b7eb2e2008-05-14 08:15:52 -0700165 * replaces the original e820 map with a new one, removing overlaps,
166 * and resolving conflicting memory types in favor of highest
167 * numbered type.
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700168 *
Paul Jackson5b7eb2e2008-05-14 08:15:52 -0700169 * The input parameter biosmap points to an array of 'struct
170 * e820entry' which on entry has elements in the range [0, *pnr_map)
171 * valid, and which has space for up to max_nr_map entries.
172 * On return, the resulting sanitized e820 map entries will be in
173 * overwritten in the same location, starting at biosmap.
174 *
175 * The integer pointed to by pnr_map must be valid on entry (the
176 * current number of valid entries located at biosmap) and will
177 * be updated on return, with the new number of valid entries
178 * (something no more than max_nr_map.)
179 *
180 * The return value from sanitize_e820_map() is zero if it
181 * successfully 'sanitized' the map entries passed in, and is -1
182 * if it did nothing, which can happen if either of (1) it was
183 * only passed one map entry, or (2) any of the input map entries
184 * were invalid (start + size < start, meaning that the size was
185 * so big the described memory range wrapped around through zero.)
186 *
187 * Visually we're performing the following
188 * (1,2,3,4 = memory types)...
189 *
190 * Sample memory map (w/overlaps):
191 * ____22__________________
192 * ______________________4_
193 * ____1111________________
194 * _44_____________________
195 * 11111111________________
196 * ____________________33__
197 * ___________44___________
198 * __________33333_________
199 * ______________22________
200 * ___________________2222_
201 * _________111111111______
202 * _____________________11_
203 * _________________4______
204 *
205 * Sanitized equivalent (no overlap):
206 * 1_______________________
207 * _44_____________________
208 * ___1____________________
209 * ____22__________________
210 * ______11________________
211 * _________1______________
212 * __________3_____________
213 * ___________44___________
214 * _____________33_________
215 * _______________2________
216 * ________________1_______
217 * _________________4______
218 * ___________________2____
219 * ____________________33__
220 * ______________________4_
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700221 */
Paul Jackson5b7eb2e2008-05-14 08:15:52 -0700222
Paul Jacksonc3965bd2008-05-14 08:15:34 -0700223int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map,
Paul Jackson6e9bcc72008-05-14 08:15:46 -0700224 int *pnr_map)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700225{
226 struct change_member {
227 struct e820entry *pbios; /* pointer to original bios entry */
228 unsigned long long addr; /* address for this change point */
229 };
Paul Jackson157fabf2008-06-22 07:21:57 -0700230 static struct change_member change_point_list[2*E820_X_MAX] __initdata;
231 static struct change_member *change_point[2*E820_X_MAX] __initdata;
232 static struct e820entry *overlap_list[E820_X_MAX] __initdata;
233 static struct e820entry new_bios[E820_X_MAX] __initdata;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700234 struct change_member *change_tmp;
235 unsigned long current_type, last_type;
236 unsigned long long last_addr;
237 int chgidx, still_changing;
238 int overlap_entries;
239 int new_bios_entry;
240 int old_nr, new_nr, chg_nr;
241 int i;
242
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700243 /* if there's only one memory region, don't bother */
244 if (*pnr_map < 2)
245 return -1;
246
247 old_nr = *pnr_map;
Paul Jackson6e9bcc72008-05-14 08:15:46 -0700248 BUG_ON(old_nr > max_nr_map);
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700249
250 /* bail out if we find any unreasonable addresses in bios map */
251 for (i = 0; i < old_nr; i++)
252 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
253 return -1;
254
255 /* create pointers for initial change-point information (for sorting) */
256 for (i = 0; i < 2 * old_nr; i++)
257 change_point[i] = &change_point_list[i];
258
259 /* record all known change-points (starting and ending addresses),
260 omitting those that are for empty memory regions */
261 chgidx = 0;
262 for (i = 0; i < old_nr; i++) {
263 if (biosmap[i].size != 0) {
264 change_point[chgidx]->addr = biosmap[i].addr;
265 change_point[chgidx++]->pbios = &biosmap[i];
266 change_point[chgidx]->addr = biosmap[i].addr +
267 biosmap[i].size;
268 change_point[chgidx++]->pbios = &biosmap[i];
269 }
270 }
271 chg_nr = chgidx;
272
273 /* sort change-point list by memory addresses (low -> high) */
274 still_changing = 1;
275 while (still_changing) {
276 still_changing = 0;
277 for (i = 1; i < chg_nr; i++) {
278 unsigned long long curaddr, lastaddr;
279 unsigned long long curpbaddr, lastpbaddr;
280
281 curaddr = change_point[i]->addr;
282 lastaddr = change_point[i - 1]->addr;
283 curpbaddr = change_point[i]->pbios->addr;
284 lastpbaddr = change_point[i - 1]->pbios->addr;
285
286 /*
287 * swap entries, when:
288 *
289 * curaddr > lastaddr or
290 * curaddr == lastaddr and curaddr == curpbaddr and
291 * lastaddr != lastpbaddr
292 */
293 if (curaddr < lastaddr ||
294 (curaddr == lastaddr && curaddr == curpbaddr &&
295 lastaddr != lastpbaddr)) {
296 change_tmp = change_point[i];
297 change_point[i] = change_point[i-1];
298 change_point[i-1] = change_tmp;
299 still_changing = 1;
300 }
301 }
302 }
303
304 /* create a new bios memory map, removing overlaps */
305 overlap_entries = 0; /* number of entries in the overlap table */
306 new_bios_entry = 0; /* index for creating new bios map entries */
307 last_type = 0; /* start with undefined memory type */
308 last_addr = 0; /* start with 0 as last starting address */
309
310 /* loop through change-points, determining affect on the new bios map */
311 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
312 /* keep track of all overlapping bios entries */
313 if (change_point[chgidx]->addr ==
314 change_point[chgidx]->pbios->addr) {
315 /*
316 * add map entry to overlap list (> 1 entry
317 * implies an overlap)
318 */
319 overlap_list[overlap_entries++] =
320 change_point[chgidx]->pbios;
321 } else {
322 /*
323 * remove entry from list (order independent,
324 * so swap with last)
325 */
326 for (i = 0; i < overlap_entries; i++) {
327 if (overlap_list[i] ==
328 change_point[chgidx]->pbios)
329 overlap_list[i] =
330 overlap_list[overlap_entries-1];
331 }
332 overlap_entries--;
333 }
334 /*
335 * if there are overlapping entries, decide which
336 * "type" to use (larger value takes precedence --
337 * 1=usable, 2,3,4,4+=unusable)
338 */
339 current_type = 0;
340 for (i = 0; i < overlap_entries; i++)
341 if (overlap_list[i]->type > current_type)
342 current_type = overlap_list[i]->type;
343 /*
344 * continue building up new bios map based on this
345 * information
346 */
347 if (current_type != last_type) {
348 if (last_type != 0) {
349 new_bios[new_bios_entry].size =
350 change_point[chgidx]->addr - last_addr;
351 /*
352 * move forward only if the new size
353 * was non-zero
354 */
355 if (new_bios[new_bios_entry].size != 0)
356 /*
357 * no more space left for new
358 * bios entries ?
359 */
Paul Jacksonc3965bd2008-05-14 08:15:34 -0700360 if (++new_bios_entry >= max_nr_map)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700361 break;
362 }
363 if (current_type != 0) {
364 new_bios[new_bios_entry].addr =
365 change_point[chgidx]->addr;
366 new_bios[new_bios_entry].type = current_type;
367 last_addr = change_point[chgidx]->addr;
368 }
369 last_type = current_type;
370 }
371 }
372 /* retain count for new bios entries */
373 new_nr = new_bios_entry;
374
375 /* copy new bios mapping into original location */
376 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
377 *pnr_map = new_nr;
378
379 return 0;
380}
381
Yinghai Ludc8e8122008-07-01 20:02:16 -0700382static int __init __append_e820_map(struct e820entry *biosmap, int nr_map)
Huang, Ying8c5beb52008-06-11 11:33:39 +0800383{
384 while (nr_map) {
385 u64 start = biosmap->addr;
386 u64 size = biosmap->size;
387 u64 end = start + size;
388 u32 type = biosmap->type;
389
390 /* Overflow in 64 bits? Ignore the memory map. */
391 if (start > end)
392 return -1;
393
394 e820_add_region(start, size, type);
395
396 biosmap++;
397 nr_map--;
398 }
399 return 0;
400}
401
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700402/*
403 * Copy the BIOS e820 map into a safe place.
404 *
405 * Sanity-check it while we're at it..
406 *
407 * If we're lucky and live on a modern system, the setup code
408 * will have given us a memory map that we can use to properly
409 * set up memory. If we aren't, we'll fake a memory map.
410 */
Yinghai Ludc8e8122008-07-01 20:02:16 -0700411static int __init append_e820_map(struct e820entry *biosmap, int nr_map)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700412{
413 /* Only one memory region (or negative)? Ignore it */
414 if (nr_map < 2)
415 return -1;
416
Yinghai Ludc8e8122008-07-01 20:02:16 -0700417 return __append_e820_map(biosmap, nr_map);
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700418}
419
Yinghai Lufc9036e2008-07-03 11:39:00 -0700420static u64 __init e820_update_range_map(struct e820map *e820x, u64 start,
421 u64 size, unsigned old_type,
422 unsigned new_type)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700423{
Jan Beulich5c0e6f02009-03-12 13:07:23 +0000424 unsigned int i, x;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700425 u64 real_updated_size = 0;
426
427 BUG_ON(old_type == new_type);
428
Yinghai Lu232b9572008-06-24 14:58:38 -0700429 if (size > (ULLONG_MAX - start))
430 size = ULLONG_MAX - start;
431
Jan Beulich5c0e6f02009-03-12 13:07:23 +0000432 for (i = 0; i < e820x->nr_map; i++) {
Yinghai Lufc9036e2008-07-03 11:39:00 -0700433 struct e820entry *ei = &e820x->map[i];
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700434 u64 final_start, final_end;
435 if (ei->type != old_type)
436 continue;
437 /* totally covered? */
438 if (ei->addr >= start &&
439 (ei->addr + ei->size) <= (start + size)) {
440 ei->type = new_type;
441 real_updated_size += ei->size;
442 continue;
443 }
444 /* partially covered */
445 final_start = max(start, ei->addr);
446 final_end = min(start + size, ei->addr + ei->size);
447 if (final_start >= final_end)
448 continue;
Jan Beulich5c0e6f02009-03-12 13:07:23 +0000449
450 x = e820x->nr_map;
451 if (x == ARRAY_SIZE(e820x->map)) {
452 printk(KERN_ERR "Too many memory map entries!\n");
453 break;
454 }
455 e820x->map[x].addr = final_start;
456 e820x->map[x].size = final_end - final_start;
457 e820x->map[x].type = new_type;
458 e820x->nr_map++;
459
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700460 real_updated_size += final_end - final_start;
Yinghai Lu976dd4d2008-06-24 14:55:32 -0700461
Yinghai Lu976dd4d2008-06-24 14:55:32 -0700462 if (ei->addr < final_start)
463 continue;
464 ei->addr = final_end;
Jan Beulich5c0e6f02009-03-12 13:07:23 +0000465 ei->size -= final_end - final_start;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700466 }
467 return real_updated_size;
468}
469
Yinghai Lufc9036e2008-07-03 11:39:00 -0700470u64 __init e820_update_range(u64 start, u64 size, unsigned old_type,
471 unsigned new_type)
472{
473 return e820_update_range_map(&e820, start, size, old_type, new_type);
474}
475
476static u64 __init e820_update_range_saved(u64 start, u64 size,
477 unsigned old_type, unsigned new_type)
478{
479 return e820_update_range_map(&e820_saved, start, size, old_type,
480 new_type);
481}
482
Yinghai Lu7a1fd982008-06-21 14:48:05 -0700483/* make e820 not cover the range */
484u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type,
485 int checktype)
486{
487 int i;
488 u64 real_removed_size = 0;
489
Yinghai Lu232b9572008-06-24 14:58:38 -0700490 if (size > (ULLONG_MAX - start))
491 size = ULLONG_MAX - start;
492
Yinghai Lu7a1fd982008-06-21 14:48:05 -0700493 for (i = 0; i < e820.nr_map; i++) {
494 struct e820entry *ei = &e820.map[i];
495 u64 final_start, final_end;
496
497 if (checktype && ei->type != old_type)
498 continue;
499 /* totally covered? */
500 if (ei->addr >= start &&
501 (ei->addr + ei->size) <= (start + size)) {
502 real_removed_size += ei->size;
503 memset(ei, 0, sizeof(struct e820entry));
504 continue;
505 }
506 /* partially covered */
507 final_start = max(start, ei->addr);
508 final_end = min(start + size, ei->addr + ei->size);
509 if (final_start >= final_end)
510 continue;
511 real_removed_size += final_end - final_start;
512
513 ei->size -= final_end - final_start;
514 if (ei->addr < final_start)
515 continue;
516 ei->addr = final_end;
517 }
518 return real_removed_size;
519}
520
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700521void __init update_e820(void)
522{
Paul Jackson6e9bcc72008-05-14 08:15:46 -0700523 int nr_map;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700524
525 nr_map = e820.nr_map;
Paul Jacksonc3965bd2008-05-14 08:15:34 -0700526 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map))
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700527 return;
528 e820.nr_map = nr_map;
529 printk(KERN_INFO "modified physical RAM map:\n");
530 e820_print_map("modified");
531}
Yinghai Lufc9036e2008-07-03 11:39:00 -0700532static void __init update_e820_saved(void)
533{
534 int nr_map;
535
536 nr_map = e820_saved.nr_map;
537 if (sanitize_e820_map(e820_saved.map, ARRAY_SIZE(e820_saved.map), &nr_map))
538 return;
539 e820_saved.nr_map = nr_map;
540}
Alok Katariafd6493e2008-06-25 11:02:42 -0700541#define MAX_GAP_END 0x100000000ull
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700542/*
Alok Katariafd6493e2008-06-25 11:02:42 -0700543 * Search for a gap in the e820 memory space from start_addr to end_addr.
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700544 */
Alok Kataria33819592008-06-24 11:48:30 -0700545__init int e820_search_gap(unsigned long *gapstart, unsigned long *gapsize,
Alok Katariafd6493e2008-06-25 11:02:42 -0700546 unsigned long start_addr, unsigned long long end_addr)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700547{
Alok Katariafd6493e2008-06-25 11:02:42 -0700548 unsigned long long last;
Alok Kataria33819592008-06-24 11:48:30 -0700549 int i = e820.nr_map;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700550 int found = 0;
551
Alok Katariafd6493e2008-06-25 11:02:42 -0700552 last = (end_addr && end_addr < MAX_GAP_END) ? end_addr : MAX_GAP_END;
553
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700554 while (--i >= 0) {
555 unsigned long long start = e820.map[i].addr;
556 unsigned long long end = start + e820.map[i].size;
557
Alok Kataria33819592008-06-24 11:48:30 -0700558 if (end < start_addr)
559 continue;
560
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700561 /*
562 * Since "last" is at most 4GB, we know we'll
563 * fit in 32 bits if this condition is true
564 */
565 if (last > end) {
566 unsigned long gap = last - end;
567
Alok Kataria33819592008-06-24 11:48:30 -0700568 if (gap >= *gapsize) {
569 *gapsize = gap;
570 *gapstart = end;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700571 found = 1;
572 }
573 }
574 if (start < last)
575 last = start;
576 }
Alok Kataria33819592008-06-24 11:48:30 -0700577 return found;
578}
579
580/*
581 * Search for the biggest gap in the low 32 bits of the e820
582 * memory space. We pass this space to PCI to assign MMIO resources
583 * for hotplug or unconfigured devices in.
584 * Hopefully the BIOS let enough space left.
585 */
586__init void e820_setup_gap(void)
587{
588 unsigned long gapstart, gapsize, round;
589 int found;
590
591 gapstart = 0x10000000;
592 gapsize = 0x400000;
Alok Katariafd6493e2008-06-25 11:02:42 -0700593 found = e820_search_gap(&gapstart, &gapsize, 0, MAX_GAP_END);
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700594
595#ifdef CONFIG_X86_64
596 if (!found) {
Yinghai Luc987d122008-06-24 22:14:09 -0700597 gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700598 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
599 "address range\n"
600 KERN_ERR "PCI: Unassigned devices with 32bit resource "
601 "registers may break!\n");
602 }
603#endif
604
605 /*
606 * See how much we want to round up: start off with
607 * rounding to the next 1MB area.
608 */
609 round = 0x100000;
610 while ((gapsize >> 4) > round)
611 round += round;
612 /* Fun with two's complement */
613 pci_mem_start = (gapstart + round) & -round;
614
615 printk(KERN_INFO
616 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
617 pci_mem_start, gapstart, gapsize);
618}
619
Huang, Ying8c5beb52008-06-11 11:33:39 +0800620/**
621 * Because of the size limitation of struct boot_params, only first
622 * 128 E820 memory entries are passed to kernel via
623 * boot_params.e820_map, others are passed via SETUP_E820_EXT node of
624 * linked list of struct setup_data, which is parsed here.
625 */
626void __init parse_e820_ext(struct setup_data *sdata, unsigned long pa_data)
627{
628 u32 map_len;
629 int entries;
630 struct e820entry *extmap;
631
632 entries = sdata->len / sizeof(struct e820entry);
633 map_len = sdata->len + sizeof(struct setup_data);
634 if (map_len > PAGE_SIZE)
635 sdata = early_ioremap(pa_data, map_len);
636 extmap = (struct e820entry *)(sdata->data);
Yinghai Ludc8e8122008-07-01 20:02:16 -0700637 __append_e820_map(extmap, entries);
Huang, Ying8c5beb52008-06-11 11:33:39 +0800638 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
639 if (map_len > PAGE_SIZE)
640 early_iounmap(sdata, map_len);
641 printk(KERN_INFO "extended physical RAM map:\n");
642 e820_print_map("extended");
643}
644
Yinghai Lubf62f392008-05-20 20:10:58 -0700645#if defined(CONFIG_X86_64) || \
646 (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION))
647/**
648 * Find the ranges of physical addresses that do not correspond to
649 * e820 RAM areas and mark the corresponding pages as nosave for
650 * hibernation (32 bit) or software suspend and suspend to RAM (64 bit).
651 *
652 * This function requires the e820 map to be sorted and without any
653 * overlapping entries and assumes the first e820 area to be RAM.
654 */
655void __init e820_mark_nosave_regions(unsigned long limit_pfn)
656{
657 int i;
658 unsigned long pfn;
659
660 pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
661 for (i = 1; i < e820.nr_map; i++) {
662 struct e820entry *ei = &e820.map[i];
663
664 if (pfn < PFN_UP(ei->addr))
665 register_nosave_region(pfn, PFN_UP(ei->addr));
666
667 pfn = PFN_DOWN(ei->addr + ei->size);
Yinghai Lu28bb2232008-06-30 16:20:54 -0700668 if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
Yinghai Lubf62f392008-05-20 20:10:58 -0700669 register_nosave_region(PFN_UP(ei->addr), pfn);
670
671 if (pfn >= limit_pfn)
672 break;
673 }
674}
675#endif
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700676
Rafael J. Wysockib69edc72008-10-31 01:02:41 +0100677#ifdef CONFIG_HIBERNATION
678/**
679 * Mark ACPI NVS memory region, so that we can save/restore it during
680 * hibernation and the subsequent resume.
681 */
682static int __init e820_mark_nvs_memory(void)
683{
684 int i;
685
686 for (i = 0; i < e820.nr_map; i++) {
687 struct e820entry *ei = &e820.map[i];
688
689 if (ei->type == E820_NVS)
690 hibernate_nvs_register(ei->addr, ei->size);
691 }
692
693 return 0;
694}
695core_initcall(e820_mark_nvs_memory);
696#endif
697
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700698/*
699 * Early reserved memory areas.
700 */
701#define MAX_EARLY_RES 20
702
703struct early_res {
704 u64 start, end;
705 char name[16];
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700706 char overlap_ok;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700707};
708static struct early_res early_res[MAX_EARLY_RES] __initdata = {
709 { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700710 {}
711};
712
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800713static int __init find_overlapped_early(u64 start, u64 end)
714{
715 int i;
716 struct early_res *r;
717
718 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
719 r = &early_res[i];
720 if (end > r->start && start < r->end)
721 break;
722 }
723
724 return i;
725}
726
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700727/*
728 * Drop the i-th range from the early reservation map,
729 * by copying any higher ranges down one over it, and
730 * clearing what had been the last slot.
731 */
732static void __init drop_range(int i)
733{
734 int j;
735
736 for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++)
737 ;
738
739 memmove(&early_res[i], &early_res[i + 1],
740 (j - 1 - i) * sizeof(struct early_res));
741
742 early_res[j - 1].end = 0;
743}
744
745/*
746 * Split any existing ranges that:
747 * 1) are marked 'overlap_ok', and
748 * 2) overlap with the stated range [start, end)
749 * into whatever portion (if any) of the existing range is entirely
750 * below or entirely above the stated range. Drop the portion
751 * of the existing range that overlaps with the stated range,
752 * which will allow the caller of this routine to then add that
753 * stated range without conflicting with any existing range.
754 */
755static void __init drop_overlaps_that_are_ok(u64 start, u64 end)
756{
757 int i;
758 struct early_res *r;
759 u64 lower_start, lower_end;
760 u64 upper_start, upper_end;
761 char name[16];
762
763 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
764 r = &early_res[i];
765
766 /* Continue past non-overlapping ranges */
767 if (end <= r->start || start >= r->end)
768 continue;
769
770 /*
771 * Leave non-ok overlaps as is; let caller
772 * panic "Overlapping early reservations"
773 * when it hits this overlap.
774 */
775 if (!r->overlap_ok)
776 return;
777
778 /*
779 * We have an ok overlap. We will drop it from the early
780 * reservation map, and add back in any non-overlapping
781 * portions (lower or upper) as separate, overlap_ok,
782 * non-overlapping ranges.
783 */
784
785 /* 1. Note any non-overlapping (lower or upper) ranges. */
786 strncpy(name, r->name, sizeof(name) - 1);
787
788 lower_start = lower_end = 0;
789 upper_start = upper_end = 0;
790 if (r->start < start) {
791 lower_start = r->start;
792 lower_end = start;
793 }
794 if (r->end > end) {
795 upper_start = end;
796 upper_end = r->end;
797 }
798
799 /* 2. Drop the original ok overlapping range */
800 drop_range(i);
801
802 i--; /* resume for-loop on copied down entry */
803
804 /* 3. Add back in any non-overlapping ranges. */
805 if (lower_end)
806 reserve_early_overlap_ok(lower_start, lower_end, name);
807 if (upper_end)
808 reserve_early_overlap_ok(upper_start, upper_end, name);
809 }
810}
811
812static void __init __reserve_early(u64 start, u64 end, char *name,
813 int overlap_ok)
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700814{
815 int i;
816 struct early_res *r;
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800817
818 i = find_overlapped_early(start, end);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700819 if (i >= MAX_EARLY_RES)
820 panic("Too many early reservations");
821 r = &early_res[i];
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800822 if (r->end)
823 panic("Overlapping early reservations "
824 "%llx-%llx %s to %llx-%llx %s\n",
825 start, end - 1, name?name:"", r->start,
826 r->end - 1, r->name);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700827 r->start = start;
828 r->end = end;
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700829 r->overlap_ok = overlap_ok;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700830 if (name)
831 strncpy(r->name, name, sizeof(r->name) - 1);
832}
833
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700834/*
835 * A few early reservtations come here.
836 *
837 * The 'overlap_ok' in the name of this routine does -not- mean it
838 * is ok for these reservations to overlap an earlier reservation.
839 * Rather it means that it is ok for subsequent reservations to
840 * overlap this one.
841 *
842 * Use this entry point to reserve early ranges when you are doing
843 * so out of "Paranoia", reserving perhaps more memory than you need,
844 * just in case, and don't mind a subsequent overlapping reservation
845 * that is known to be needed.
846 *
847 * The drop_overlaps_that_are_ok() call here isn't really needed.
848 * It would be needed if we had two colliding 'overlap_ok'
849 * reservations, so that the second such would not panic on the
850 * overlap with the first. We don't have any such as of this
851 * writing, but might as well tolerate such if it happens in
852 * the future.
853 */
854void __init reserve_early_overlap_ok(u64 start, u64 end, char *name)
855{
856 drop_overlaps_that_are_ok(start, end);
857 __reserve_early(start, end, name, 1);
858}
859
860/*
861 * Most early reservations come here.
862 *
863 * We first have drop_overlaps_that_are_ok() drop any pre-existing
864 * 'overlap_ok' ranges, so that we can then reserve this memory
865 * range without risk of panic'ing on an overlapping overlap_ok
866 * early reservation.
867 */
868void __init reserve_early(u64 start, u64 end, char *name)
869{
Yinghai Lu46cb27f2009-02-24 13:12:43 -0800870 if (start >= end)
871 return;
872
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700873 drop_overlaps_that_are_ok(start, end);
874 __reserve_early(start, end, name, 0);
875}
876
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700877void __init free_early(u64 start, u64 end)
878{
879 struct early_res *r;
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700880 int i;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700881
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800882 i = find_overlapped_early(start, end);
883 r = &early_res[i];
884 if (i >= MAX_EARLY_RES || r->end != end || r->start != start)
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700885 panic("free_early on not reserved area: %llx-%llx!",
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800886 start, end - 1);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700887
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700888 drop_range(i);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700889}
890
891void __init early_res_to_bootmem(u64 start, u64 end)
892{
Yinghai Luab677152008-06-27 15:36:54 -0700893 int i, count;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700894 u64 final_start, final_end;
Yinghai Luab677152008-06-27 15:36:54 -0700895
896 count = 0;
897 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++)
898 count++;
899
Yinghai Lu5f1f2b32008-07-18 16:16:23 -0700900 printk(KERN_INFO "(%d early reservations) ==> bootmem [%010llx - %010llx]\n",
901 count, start, end);
Yinghai Luab677152008-06-27 15:36:54 -0700902 for (i = 0; i < count; i++) {
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700903 struct early_res *r = &early_res[i];
Yinghai Lu4fcc5452008-07-01 20:03:11 -0700904 printk(KERN_INFO " #%d [%010llx - %010llx] %16s", i,
Yinghai Luab677152008-06-27 15:36:54 -0700905 r->start, r->end, r->name);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700906 final_start = max(start, r->start);
907 final_end = min(end, r->end);
Yinghai Luab677152008-06-27 15:36:54 -0700908 if (final_start >= final_end) {
909 printk(KERN_CONT "\n");
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700910 continue;
Yinghai Luab677152008-06-27 15:36:54 -0700911 }
Yinghai Lu4fcc5452008-07-01 20:03:11 -0700912 printk(KERN_CONT " ==> [%010llx - %010llx]\n",
Yinghai Luab677152008-06-27 15:36:54 -0700913 final_start, final_end);
Yinghai Lud2dbf342008-06-13 02:00:56 -0700914 reserve_bootmem_generic(final_start, final_end - final_start,
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700915 BOOTMEM_DEFAULT);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700916 }
917}
918
919/* Check for already reserved areas */
920static inline int __init bad_addr(u64 *addrp, u64 size, u64 align)
921{
922 int i;
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800923 u64 addr = *addrp;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700924 int changed = 0;
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800925 struct early_res *r;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700926again:
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800927 i = find_overlapped_early(addr, addr + size);
928 r = &early_res[i];
929 if (i < MAX_EARLY_RES && r->end) {
930 *addrp = addr = round_up(r->end, align);
931 changed = 1;
932 goto again;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700933 }
934 return changed;
935}
936
937/* Check for already reserved areas */
938static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align)
939{
940 int i;
941 u64 addr = *addrp, last;
942 u64 size = *sizep;
943 int changed = 0;
944again:
945 last = addr + size;
946 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
947 struct early_res *r = &early_res[i];
948 if (last > r->start && addr < r->start) {
949 size = r->start - addr;
950 changed = 1;
951 goto again;
952 }
953 if (last > r->end && addr < r->end) {
954 addr = round_up(r->end, align);
955 size = last - addr;
956 changed = 1;
957 goto again;
958 }
959 if (last <= r->end && addr >= r->start) {
960 (*sizep)++;
961 return 0;
962 }
963 }
964 if (changed) {
965 *addrp = addr;
966 *sizep = size;
967 }
968 return changed;
969}
970
971/*
972 * Find a free area with specified alignment in a specific range.
973 */
974u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align)
975{
976 int i;
977
978 for (i = 0; i < e820.nr_map; i++) {
979 struct e820entry *ei = &e820.map[i];
980 u64 addr, last;
981 u64 ei_last;
982
983 if (ei->type != E820_RAM)
984 continue;
985 addr = round_up(ei->addr, align);
986 ei_last = ei->addr + ei->size;
987 if (addr < start)
988 addr = round_up(start, align);
989 if (addr >= ei_last)
990 continue;
991 while (bad_addr(&addr, size, align) && addr+size <= ei_last)
992 ;
993 last = addr + size;
994 if (last > ei_last)
995 continue;
996 if (last > end)
997 continue;
998 return addr;
999 }
1000 return -1ULL;
1001}
1002
1003/*
1004 * Find next free range after *start
1005 */
1006u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align)
1007{
1008 int i;
1009
1010 for (i = 0; i < e820.nr_map; i++) {
1011 struct e820entry *ei = &e820.map[i];
1012 u64 addr, last;
1013 u64 ei_last;
1014
1015 if (ei->type != E820_RAM)
1016 continue;
1017 addr = round_up(ei->addr, align);
1018 ei_last = ei->addr + ei->size;
1019 if (addr < start)
1020 addr = round_up(start, align);
1021 if (addr >= ei_last)
1022 continue;
1023 *sizep = ei_last - addr;
1024 while (bad_addr_size(&addr, sizep, align) &&
1025 addr + *sizep <= ei_last)
1026 ;
1027 last = addr + *sizep;
1028 if (last > ei_last)
1029 continue;
1030 return addr;
1031 }
Yinghai Lua4c81cf2008-05-18 01:18:57 -07001032
Jan Beulich5c0e6f02009-03-12 13:07:23 +00001033 return -1ULL;
Yinghai Lua4c81cf2008-05-18 01:18:57 -07001034}
Yinghai Lu2944e162008-06-01 13:17:38 -07001035
1036/*
1037 * pre allocated 4k and reserved it in e820
1038 */
1039u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align)
1040{
1041 u64 size = 0;
1042 u64 addr;
1043 u64 start;
1044
1045 start = startt;
Jan Beulich5c0e6f02009-03-12 13:07:23 +00001046 while (size < sizet && (start + 1))
Yinghai Lu2944e162008-06-01 13:17:38 -07001047 start = find_e820_area_size(start, &size, align);
1048
1049 if (size < sizet)
1050 return 0;
1051
Jan Beulich5c0e6f02009-03-12 13:07:23 +00001052#ifdef CONFIG_X86_32
1053 if (start >= MAXMEM)
1054 return 0;
1055 if (start + size > MAXMEM)
1056 size = MAXMEM - start;
1057#endif
1058
Yinghai Lu2944e162008-06-01 13:17:38 -07001059 addr = round_down(start + size - sizet, align);
Jan Beulich5c0e6f02009-03-12 13:07:23 +00001060 if (addr < start)
1061 return 0;
Yinghai Lud0be6bd2008-06-15 18:58:51 -07001062 e820_update_range(addr, sizet, E820_RAM, E820_RESERVED);
Yinghai Lufc9036e2008-07-03 11:39:00 -07001063 e820_update_range_saved(addr, sizet, E820_RAM, E820_RESERVED);
Yinghai Lu2944e162008-06-01 13:17:38 -07001064 printk(KERN_INFO "update e820 for early_reserve_e820\n");
1065 update_e820();
Yinghai Lufc9036e2008-07-03 11:39:00 -07001066 update_e820_saved();
Yinghai Lu2944e162008-06-01 13:17:38 -07001067
1068 return addr;
1069}
1070
Yinghai Luee0c80f2008-06-03 19:34:00 -07001071#ifdef CONFIG_X86_32
1072# ifdef CONFIG_X86_PAE
1073# define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
1074# else
1075# define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
1076# endif
1077#else /* CONFIG_X86_32 */
Yinghai Lubd70e522008-06-04 13:21:29 -07001078# define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
Yinghai Luee0c80f2008-06-03 19:34:00 -07001079#endif
1080
1081/*
Yinghai Luee0c80f2008-06-03 19:34:00 -07001082 * Find the highest page frame number we have available
1083 */
Yinghai Luf361a452008-07-10 20:38:26 -07001084static unsigned long __init e820_end_pfn(unsigned long limit_pfn, unsigned type)
Yinghai Luee0c80f2008-06-03 19:34:00 -07001085{
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001086 int i;
1087 unsigned long last_pfn = 0;
Yinghai Luee0c80f2008-06-03 19:34:00 -07001088 unsigned long max_arch_pfn = MAX_ARCH_PFN;
1089
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001090 for (i = 0; i < e820.nr_map; i++) {
1091 struct e820entry *ei = &e820.map[i];
Yinghai Luf361a452008-07-10 20:38:26 -07001092 unsigned long start_pfn;
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001093 unsigned long end_pfn;
1094
Yinghai Luf361a452008-07-10 20:38:26 -07001095 if (ei->type != type)
Yinghai Luc22d4c12008-07-09 03:01:14 -07001096 continue;
Yinghai Luc22d4c12008-07-09 03:01:14 -07001097
Yinghai Luf361a452008-07-10 20:38:26 -07001098 start_pfn = ei->addr >> PAGE_SHIFT;
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001099 end_pfn = (ei->addr + ei->size) >> PAGE_SHIFT;
Yinghai Luf361a452008-07-10 20:38:26 -07001100
1101 if (start_pfn >= limit_pfn)
1102 continue;
1103 if (end_pfn > limit_pfn) {
1104 last_pfn = limit_pfn;
1105 break;
1106 }
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001107 if (end_pfn > last_pfn)
1108 last_pfn = end_pfn;
1109 }
Yinghai Luee0c80f2008-06-03 19:34:00 -07001110
1111 if (last_pfn > max_arch_pfn)
1112 last_pfn = max_arch_pfn;
Yinghai Luee0c80f2008-06-03 19:34:00 -07001113
Paul Jackson5dab8ec2008-06-25 05:44:40 -07001114 printk(KERN_INFO "last_pfn = %#lx max_arch_pfn = %#lx\n",
Yinghai Luee0c80f2008-06-03 19:34:00 -07001115 last_pfn, max_arch_pfn);
1116 return last_pfn;
1117}
Yinghai Luf361a452008-07-10 20:38:26 -07001118unsigned long __init e820_end_of_ram_pfn(void)
1119{
1120 return e820_end_pfn(MAX_ARCH_PFN, E820_RAM);
1121}
Yinghai Luee0c80f2008-06-03 19:34:00 -07001122
Yinghai Luf361a452008-07-10 20:38:26 -07001123unsigned long __init e820_end_of_low_ram_pfn(void)
1124{
1125 return e820_end_pfn(1UL<<(32 - PAGE_SHIFT), E820_RAM);
1126}
Yinghai Luee0c80f2008-06-03 19:34:00 -07001127/*
1128 * Finds an active region in the address range from start_pfn to last_pfn and
1129 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
1130 */
1131int __init e820_find_active_region(const struct e820entry *ei,
1132 unsigned long start_pfn,
1133 unsigned long last_pfn,
1134 unsigned long *ei_startpfn,
1135 unsigned long *ei_endpfn)
1136{
1137 u64 align = PAGE_SIZE;
1138
1139 *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT;
1140 *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT;
1141
1142 /* Skip map entries smaller than a page */
1143 if (*ei_startpfn >= *ei_endpfn)
1144 return 0;
1145
1146 /* Skip if map is outside the node */
1147 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
1148 *ei_startpfn >= last_pfn)
1149 return 0;
1150
1151 /* Check for overlaps */
1152 if (*ei_startpfn < start_pfn)
1153 *ei_startpfn = start_pfn;
1154 if (*ei_endpfn > last_pfn)
1155 *ei_endpfn = last_pfn;
1156
Yinghai Luee0c80f2008-06-03 19:34:00 -07001157 return 1;
1158}
1159
1160/* Walk the e820 map and register active regions within a node */
1161void __init e820_register_active_regions(int nid, unsigned long start_pfn,
1162 unsigned long last_pfn)
1163{
1164 unsigned long ei_startpfn;
1165 unsigned long ei_endpfn;
1166 int i;
1167
1168 for (i = 0; i < e820.nr_map; i++)
1169 if (e820_find_active_region(&e820.map[i],
1170 start_pfn, last_pfn,
1171 &ei_startpfn, &ei_endpfn))
1172 add_active_range(nid, ei_startpfn, ei_endpfn);
1173}
1174
1175/*
1176 * Find the hole size (in bytes) in the memory range.
1177 * @start: starting address of the memory range to scan
1178 * @end: ending address of the memory range to scan
1179 */
1180u64 __init e820_hole_size(u64 start, u64 end)
1181{
1182 unsigned long start_pfn = start >> PAGE_SHIFT;
1183 unsigned long last_pfn = end >> PAGE_SHIFT;
1184 unsigned long ei_startpfn, ei_endpfn, ram = 0;
1185 int i;
1186
1187 for (i = 0; i < e820.nr_map; i++) {
1188 if (e820_find_active_region(&e820.map[i],
1189 start_pfn, last_pfn,
1190 &ei_startpfn, &ei_endpfn))
1191 ram += ei_endpfn - ei_startpfn;
1192 }
1193 return end - start - ((u64)ram << PAGE_SHIFT);
1194}
Yinghai Luab4a4652008-06-10 12:55:54 -07001195
1196static void early_panic(char *msg)
1197{
1198 early_printk(msg);
1199 panic(msg);
1200}
1201
Yinghai Lu69a77042008-07-10 04:17:00 -07001202static int userdef __initdata;
1203
Yinghai Luab4a4652008-06-10 12:55:54 -07001204/* "mem=nopentium" disables the 4MB page tables. */
1205static int __init parse_memopt(char *p)
1206{
1207 u64 mem_size;
1208
1209 if (!p)
1210 return -EINVAL;
1211
1212#ifdef CONFIG_X86_32
1213 if (!strcmp(p, "nopentium")) {
1214 setup_clear_cpu_cap(X86_FEATURE_PSE);
1215 return 0;
1216 }
1217#endif
1218
Yinghai Lu69a77042008-07-10 04:17:00 -07001219 userdef = 1;
Yinghai Luab4a4652008-06-10 12:55:54 -07001220 mem_size = memparse(p, &p);
Yinghai Lu69a77042008-07-10 04:17:00 -07001221 e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
Bernhard Walle611dfd72008-06-25 21:39:16 +02001222
Yinghai Luab4a4652008-06-10 12:55:54 -07001223 return 0;
1224}
1225early_param("mem", parse_memopt);
1226
Yinghai Luab4a4652008-06-10 12:55:54 -07001227static int __init parse_memmap_opt(char *p)
1228{
1229 char *oldp;
1230 u64 start_at, mem_size;
1231
Cyrill Gorcunova737abd2008-07-05 15:53:39 +04001232 if (!p)
1233 return -EINVAL;
1234
Prarit Bhargavad6be118a2008-09-09 09:56:08 -04001235 if (!strncmp(p, "exactmap", 8)) {
Yinghai Luab4a4652008-06-10 12:55:54 -07001236#ifdef CONFIG_CRASH_DUMP
1237 /*
1238 * If we are doing a crash dump, we still need to know
1239 * the real mem size before original memory map is
1240 * reset.
1241 */
Yinghai Luf361a452008-07-10 20:38:26 -07001242 saved_max_pfn = e820_end_of_ram_pfn();
Yinghai Luab4a4652008-06-10 12:55:54 -07001243#endif
1244 e820.nr_map = 0;
1245 userdef = 1;
1246 return 0;
1247 }
1248
1249 oldp = p;
1250 mem_size = memparse(p, &p);
1251 if (p == oldp)
1252 return -EINVAL;
1253
1254 userdef = 1;
1255 if (*p == '@') {
1256 start_at = memparse(p+1, &p);
Yinghai Lud0be6bd2008-06-15 18:58:51 -07001257 e820_add_region(start_at, mem_size, E820_RAM);
Yinghai Luab4a4652008-06-10 12:55:54 -07001258 } else 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_ACPI);
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_RESERVED);
Yinghai Lu7b479be2008-07-12 22:57:07 -07001264 } else
Yinghai Lu69a77042008-07-10 04:17:00 -07001265 e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
Yinghai Lu7b479be2008-07-12 22:57:07 -07001266
Yinghai Luab4a4652008-06-10 12:55:54 -07001267 return *p == '\0' ? 0 : -EINVAL;
1268}
1269early_param("memmap", parse_memmap_opt);
1270
1271void __init finish_e820_parsing(void)
1272{
1273 if (userdef) {
1274 int nr = e820.nr_map;
1275
1276 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
1277 early_panic("Invalid user supplied memory map");
1278 e820.nr_map = nr;
1279
1280 printk(KERN_INFO "user-defined physical RAM map:\n");
1281 e820_print_map("user");
1282 }
1283}
Yinghai Lu41c094f2008-06-16 13:03:31 -07001284
Bernhard Walle5dfcf142008-06-27 13:12:55 +02001285static inline const char *e820_type_to_string(int e820_type)
1286{
1287 switch (e820_type) {
1288 case E820_RESERVED_KERN:
1289 case E820_RAM: return "System RAM";
1290 case E820_ACPI: return "ACPI Tables";
1291 case E820_NVS: return "ACPI Non-volatile Storage";
Cihula, Joseph671eef82008-08-20 16:43:07 -07001292 case E820_UNUSABLE: return "Unusable memory";
Bernhard Walle5dfcf142008-06-27 13:12:55 +02001293 default: return "reserved";
1294 }
1295}
1296
Yinghai Lu41c094f2008-06-16 13:03:31 -07001297/*
1298 * Mark e820 reserved areas as busy for the resource manager.
1299 */
Ingo Molnara5444d12008-08-29 08:09:23 +02001300static struct resource __initdata *e820_res;
Yinghai Lu41c094f2008-06-16 13:03:31 -07001301void __init e820_reserve_resources(void)
1302{
1303 int i;
Yinghai Lu58f7c982008-08-28 13:52:25 -07001304 struct resource *res;
Ingo Molnara5444d12008-08-29 08:09:23 +02001305 u64 end;
Yinghai Lu41c094f2008-06-16 13:03:31 -07001306
1307 res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
Yinghai Lu58f7c982008-08-28 13:52:25 -07001308 e820_res = res;
Yinghai Lu41c094f2008-06-16 13:03:31 -07001309 for (i = 0; i < e820.nr_map; i++) {
Yinghai Lub4df32f2008-06-28 17:49:59 -07001310 end = e820.map[i].addr + e820.map[i].size - 1;
Jeremy Fitzhardinge8308c542008-09-11 01:31:50 -07001311 if (end != (resource_size_t)end) {
Yinghai Lu41c094f2008-06-16 13:03:31 -07001312 res++;
1313 continue;
1314 }
Bernhard Walle5dfcf142008-06-27 13:12:55 +02001315 res->name = e820_type_to_string(e820.map[i].type);
Yinghai Lub4df32f2008-06-28 17:49:59 -07001316 res->start = e820.map[i].addr;
1317 res->end = end;
1318
Linus Torvalds1f987572008-11-01 10:17:22 -07001319 res->flags = IORESOURCE_MEM;
Ingo Molnara5444d12008-08-29 08:09:23 +02001320
1321 /*
1322 * don't register the region that could be conflicted with
1323 * pci device BAR resource and insert them later in
1324 * pcibios_resource_survey()
1325 */
Linus Torvalds1f987572008-11-01 10:17:22 -07001326 if (e820.map[i].type != E820_RESERVED || res->start < (1ULL<<20)) {
1327 res->flags |= IORESOURCE_BUSY;
Yinghai Lu58f7c982008-08-28 13:52:25 -07001328 insert_resource(&iomem_resource, res);
Linus Torvalds1f987572008-11-01 10:17:22 -07001329 }
Yinghai Lu41c094f2008-06-16 13:03:31 -07001330 res++;
1331 }
Bernhard Walle5dfcf142008-06-27 13:12:55 +02001332
1333 for (i = 0; i < e820_saved.nr_map; i++) {
1334 struct e820entry *entry = &e820_saved.map[i];
1335 firmware_map_add_early(entry->addr,
1336 entry->addr + entry->size - 1,
1337 e820_type_to_string(entry->type));
1338 }
Yinghai Lu41c094f2008-06-16 13:03:31 -07001339}
1340
Yinghai Lu58f7c982008-08-28 13:52:25 -07001341void __init e820_reserve_resources_late(void)
1342{
1343 int i;
1344 struct resource *res;
1345
1346 res = e820_res;
1347 for (i = 0; i < e820.nr_map; i++) {
Ingo Molnara5444d12008-08-29 08:09:23 +02001348 if (!res->parent && res->end)
Linus Torvalds1f987572008-11-01 10:17:22 -07001349 insert_resource_expand_to_fit(&iomem_resource, res);
Yinghai Lu58f7c982008-08-28 13:52:25 -07001350 res++;
1351 }
1352}
1353
Yinghai Lu95a71a42008-06-18 17:27:08 -07001354char *__init default_machine_specific_memory_setup(void)
Yinghai Lu064d25f2008-06-16 19:58:28 -07001355{
1356 char *who = "BIOS-e820";
1357 int new_nr;
1358 /*
1359 * Try to copy the BIOS-supplied E820-map.
1360 *
1361 * Otherwise fake a memory map; one section from 0k->640k,
1362 * the next section from 1mb->appropriate_mem_k
1363 */
1364 new_nr = boot_params.e820_entries;
1365 sanitize_e820_map(boot_params.e820_map,
1366 ARRAY_SIZE(boot_params.e820_map),
1367 &new_nr);
1368 boot_params.e820_entries = new_nr;
Yinghai Ludc8e8122008-07-01 20:02:16 -07001369 if (append_e820_map(boot_params.e820_map, boot_params.e820_entries)
1370 < 0) {
Yinghai Lu95a71a42008-06-18 17:27:08 -07001371 u64 mem_size;
Yinghai Lu41c094f2008-06-16 13:03:31 -07001372
Yinghai Lu064d25f2008-06-16 19:58:28 -07001373 /* compare results from other methods and take the greater */
1374 if (boot_params.alt_mem_k
1375 < boot_params.screen_info.ext_mem_k) {
1376 mem_size = boot_params.screen_info.ext_mem_k;
1377 who = "BIOS-88";
1378 } else {
1379 mem_size = boot_params.alt_mem_k;
1380 who = "BIOS-e801";
1381 }
1382
1383 e820.nr_map = 0;
1384 e820_add_region(0, LOWMEMSIZE(), E820_RAM);
1385 e820_add_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
Yinghai Lu064d25f2008-06-16 19:58:28 -07001386 }
1387
1388 /* In case someone cares... */
1389 return who;
1390}
1391
Yinghai Lu95a71a42008-06-18 17:27:08 -07001392char *__init __attribute__((weak)) machine_specific_memory_setup(void)
1393{
Yinghai Lu3c9cb6d2008-07-19 02:07:25 -07001394 if (x86_quirks->arch_memory_setup) {
1395 char *who = x86_quirks->arch_memory_setup();
Ingo Molnar3b335532008-07-10 17:30:40 +02001396
1397 if (who)
1398 return who;
1399 }
Yinghai Lu95a71a42008-06-18 17:27:08 -07001400 return default_machine_specific_memory_setup();
1401}
1402
Yinghai Lu064d25f2008-06-16 19:58:28 -07001403/* Overridden in paravirt.c if CONFIG_PARAVIRT */
1404char * __init __attribute__((weak)) memory_setup(void)
1405{
1406 return machine_specific_memory_setup();
1407}
1408
1409void __init setup_memory_map(void)
1410{
Yinghai Lu0be15522008-07-03 11:35:37 -07001411 char *who;
1412
1413 who = memory_setup();
1414 memcpy(&e820_saved, &e820, sizeof(struct e820map));
Yinghai Lu064d25f2008-06-16 19:58:28 -07001415 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
Yinghai Lu0be15522008-07-03 11:35:37 -07001416 e820_print_map(who);
Yinghai Lu064d25f2008-06-16 19:58:28 -07001417}