blob: 0c34ff49ff4d1f50f0c9a8837f7470e702311fa0 [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 Lu78a8b35b2009-03-12 22:36:01 -0700430 u64 end;
Yinghai Lu773e6732009-03-12 21:35:18 -0700431 unsigned int i;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700432 u64 real_updated_size = 0;
433
434 BUG_ON(old_type == new_type);
435
Yinghai Lu232b9572008-06-24 14:58:38 -0700436 if (size > (ULLONG_MAX - start))
437 size = ULLONG_MAX - start;
438
Yinghai Lu78a8b35b2009-03-12 22:36:01 -0700439 end = start + size;
Jan Beulich5c0e6f02009-03-12 13:07:23 +0000440 for (i = 0; i < e820x->nr_map; i++) {
Yinghai Lufc9036e2008-07-03 11:39:00 -0700441 struct e820entry *ei = &e820x->map[i];
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700442 u64 final_start, final_end;
Yinghai Lu78a8b35b2009-03-12 22:36:01 -0700443 u64 ei_end;
444
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700445 if (ei->type != old_type)
446 continue;
Yinghai Lu78a8b35b2009-03-12 22:36:01 -0700447
448 ei_end = ei->addr + ei->size;
449 /* totally covered by new range? */
450 if (ei->addr >= start && ei_end <= end) {
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700451 ei->type = new_type;
452 real_updated_size += ei->size;
453 continue;
454 }
Yinghai Lu78a8b35b2009-03-12 22:36:01 -0700455
456 /* new range is totally covered? */
457 if (ei->addr < start && ei_end > end) {
458 __e820_add_region(e820x, start, size, new_type);
459 __e820_add_region(e820x, end, ei_end - end, ei->type);
460 ei->size = start - ei->addr;
461 real_updated_size += size;
462 continue;
463 }
464
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700465 /* partially covered */
466 final_start = max(start, ei->addr);
Yinghai Lu78a8b35b2009-03-12 22:36:01 -0700467 final_end = min(end, ei_end);
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700468 if (final_start >= final_end)
469 continue;
Jan Beulich5c0e6f02009-03-12 13:07:23 +0000470
Yinghai Lu773e6732009-03-12 21:35:18 -0700471 __e820_add_region(e820x, final_start, final_end - final_start,
472 new_type);
Jan Beulich5c0e6f02009-03-12 13:07:23 +0000473
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700474 real_updated_size += final_end - final_start;
Yinghai Lu976dd4d2008-06-24 14:55:32 -0700475
Yinghai Lu773e6732009-03-12 21:35:18 -0700476 /*
477 * left range could be head or tail, so need to update
478 * size at first.
479 */
480 ei->size -= final_end - final_start;
Yinghai Lu976dd4d2008-06-24 14:55:32 -0700481 if (ei->addr < final_start)
482 continue;
483 ei->addr = final_end;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700484 }
485 return real_updated_size;
486}
487
Yinghai Lufc9036e2008-07-03 11:39:00 -0700488u64 __init e820_update_range(u64 start, u64 size, unsigned old_type,
489 unsigned new_type)
490{
Yinghai Lu773e6732009-03-12 21:35:18 -0700491 return __e820_update_range(&e820, start, size, old_type, new_type);
Yinghai Lufc9036e2008-07-03 11:39:00 -0700492}
493
494static u64 __init e820_update_range_saved(u64 start, u64 size,
495 unsigned old_type, unsigned new_type)
496{
Yinghai Lu773e6732009-03-12 21:35:18 -0700497 return __e820_update_range(&e820_saved, start, size, old_type,
Yinghai Lufc9036e2008-07-03 11:39:00 -0700498 new_type);
499}
500
Yinghai Lu7a1fd982008-06-21 14:48:05 -0700501/* make e820 not cover the range */
502u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type,
503 int checktype)
504{
505 int i;
506 u64 real_removed_size = 0;
507
Yinghai Lu232b9572008-06-24 14:58:38 -0700508 if (size > (ULLONG_MAX - start))
509 size = ULLONG_MAX - start;
510
Yinghai Lu7a1fd982008-06-21 14:48:05 -0700511 for (i = 0; i < e820.nr_map; i++) {
512 struct e820entry *ei = &e820.map[i];
513 u64 final_start, final_end;
514
515 if (checktype && ei->type != old_type)
516 continue;
517 /* totally covered? */
518 if (ei->addr >= start &&
519 (ei->addr + ei->size) <= (start + size)) {
520 real_removed_size += ei->size;
521 memset(ei, 0, sizeof(struct e820entry));
522 continue;
523 }
524 /* partially covered */
525 final_start = max(start, ei->addr);
526 final_end = min(start + size, ei->addr + ei->size);
527 if (final_start >= final_end)
528 continue;
529 real_removed_size += final_end - final_start;
530
531 ei->size -= final_end - final_start;
532 if (ei->addr < final_start)
533 continue;
534 ei->addr = final_end;
535 }
536 return real_removed_size;
537}
538
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700539void __init update_e820(void)
540{
Paul Jackson6e9bcc72008-05-14 08:15:46 -0700541 int nr_map;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700542
543 nr_map = e820.nr_map;
Paul Jacksonc3965bd2008-05-14 08:15:34 -0700544 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map))
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700545 return;
546 e820.nr_map = nr_map;
547 printk(KERN_INFO "modified physical RAM map:\n");
548 e820_print_map("modified");
549}
Yinghai Lufc9036e2008-07-03 11:39:00 -0700550static void __init update_e820_saved(void)
551{
552 int nr_map;
553
554 nr_map = e820_saved.nr_map;
555 if (sanitize_e820_map(e820_saved.map, ARRAY_SIZE(e820_saved.map), &nr_map))
556 return;
557 e820_saved.nr_map = nr_map;
558}
Alok Katariafd6493e2008-06-25 11:02:42 -0700559#define MAX_GAP_END 0x100000000ull
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700560/*
Alok Katariafd6493e2008-06-25 11:02:42 -0700561 * Search for a gap in the e820 memory space from start_addr to end_addr.
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700562 */
Alok Kataria33819592008-06-24 11:48:30 -0700563__init int e820_search_gap(unsigned long *gapstart, unsigned long *gapsize,
Alok Katariafd6493e2008-06-25 11:02:42 -0700564 unsigned long start_addr, unsigned long long end_addr)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700565{
Alok Katariafd6493e2008-06-25 11:02:42 -0700566 unsigned long long last;
Alok Kataria33819592008-06-24 11:48:30 -0700567 int i = e820.nr_map;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700568 int found = 0;
569
Alok Katariafd6493e2008-06-25 11:02:42 -0700570 last = (end_addr && end_addr < MAX_GAP_END) ? end_addr : MAX_GAP_END;
571
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700572 while (--i >= 0) {
573 unsigned long long start = e820.map[i].addr;
574 unsigned long long end = start + e820.map[i].size;
575
Alok Kataria33819592008-06-24 11:48:30 -0700576 if (end < start_addr)
577 continue;
578
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700579 /*
580 * Since "last" is at most 4GB, we know we'll
581 * fit in 32 bits if this condition is true
582 */
583 if (last > end) {
584 unsigned long gap = last - end;
585
Alok Kataria33819592008-06-24 11:48:30 -0700586 if (gap >= *gapsize) {
587 *gapsize = gap;
588 *gapstart = end;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700589 found = 1;
590 }
591 }
592 if (start < last)
593 last = start;
594 }
Alok Kataria33819592008-06-24 11:48:30 -0700595 return found;
596}
597
598/*
599 * Search for the biggest gap in the low 32 bits of the e820
600 * memory space. We pass this space to PCI to assign MMIO resources
601 * for hotplug or unconfigured devices in.
602 * Hopefully the BIOS let enough space left.
603 */
604__init void e820_setup_gap(void)
605{
606 unsigned long gapstart, gapsize, round;
607 int found;
608
609 gapstart = 0x10000000;
610 gapsize = 0x400000;
Alok Katariafd6493e2008-06-25 11:02:42 -0700611 found = e820_search_gap(&gapstart, &gapsize, 0, MAX_GAP_END);
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700612
613#ifdef CONFIG_X86_64
614 if (!found) {
Yinghai Luc987d122008-06-24 22:14:09 -0700615 gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700616 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
617 "address range\n"
618 KERN_ERR "PCI: Unassigned devices with 32bit resource "
619 "registers may break!\n");
620 }
621#endif
622
623 /*
624 * See how much we want to round up: start off with
625 * rounding to the next 1MB area.
626 */
627 round = 0x100000;
628 while ((gapsize >> 4) > round)
629 round += round;
630 /* Fun with two's complement */
631 pci_mem_start = (gapstart + round) & -round;
632
633 printk(KERN_INFO
634 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
635 pci_mem_start, gapstart, gapsize);
636}
637
Huang, Ying8c5beb52008-06-11 11:33:39 +0800638/**
639 * Because of the size limitation of struct boot_params, only first
640 * 128 E820 memory entries are passed to kernel via
641 * boot_params.e820_map, others are passed via SETUP_E820_EXT node of
642 * linked list of struct setup_data, which is parsed here.
643 */
644void __init parse_e820_ext(struct setup_data *sdata, unsigned long pa_data)
645{
646 u32 map_len;
647 int entries;
648 struct e820entry *extmap;
649
650 entries = sdata->len / sizeof(struct e820entry);
651 map_len = sdata->len + sizeof(struct setup_data);
652 if (map_len > PAGE_SIZE)
653 sdata = early_ioremap(pa_data, map_len);
654 extmap = (struct e820entry *)(sdata->data);
Yinghai Ludc8e8122008-07-01 20:02:16 -0700655 __append_e820_map(extmap, entries);
Huang, Ying8c5beb52008-06-11 11:33:39 +0800656 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
657 if (map_len > PAGE_SIZE)
658 early_iounmap(sdata, map_len);
659 printk(KERN_INFO "extended physical RAM map:\n");
660 e820_print_map("extended");
661}
662
Yinghai Lubf62f392008-05-20 20:10:58 -0700663#if defined(CONFIG_X86_64) || \
664 (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION))
665/**
666 * Find the ranges of physical addresses that do not correspond to
667 * e820 RAM areas and mark the corresponding pages as nosave for
668 * hibernation (32 bit) or software suspend and suspend to RAM (64 bit).
669 *
670 * This function requires the e820 map to be sorted and without any
671 * overlapping entries and assumes the first e820 area to be RAM.
672 */
673void __init e820_mark_nosave_regions(unsigned long limit_pfn)
674{
675 int i;
676 unsigned long pfn;
677
678 pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
679 for (i = 1; i < e820.nr_map; i++) {
680 struct e820entry *ei = &e820.map[i];
681
682 if (pfn < PFN_UP(ei->addr))
683 register_nosave_region(pfn, PFN_UP(ei->addr));
684
685 pfn = PFN_DOWN(ei->addr + ei->size);
Yinghai Lu28bb2232008-06-30 16:20:54 -0700686 if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
Yinghai Lubf62f392008-05-20 20:10:58 -0700687 register_nosave_region(PFN_UP(ei->addr), pfn);
688
689 if (pfn >= limit_pfn)
690 break;
691 }
692}
693#endif
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700694
Rafael J. Wysockib69edc72008-10-31 01:02:41 +0100695#ifdef CONFIG_HIBERNATION
696/**
697 * Mark ACPI NVS memory region, so that we can save/restore it during
698 * hibernation and the subsequent resume.
699 */
700static int __init e820_mark_nvs_memory(void)
701{
702 int i;
703
704 for (i = 0; i < e820.nr_map; i++) {
705 struct e820entry *ei = &e820.map[i];
706
707 if (ei->type == E820_NVS)
708 hibernate_nvs_register(ei->addr, ei->size);
709 }
710
711 return 0;
712}
713core_initcall(e820_mark_nvs_memory);
714#endif
715
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700716/*
717 * Early reserved memory areas.
718 */
719#define MAX_EARLY_RES 20
720
721struct early_res {
722 u64 start, end;
723 char name[16];
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700724 char overlap_ok;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700725};
726static struct early_res early_res[MAX_EARLY_RES] __initdata = {
727 { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700728 {}
729};
730
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800731static int __init find_overlapped_early(u64 start, u64 end)
732{
733 int i;
734 struct early_res *r;
735
736 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
737 r = &early_res[i];
738 if (end > r->start && start < r->end)
739 break;
740 }
741
742 return i;
743}
744
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700745/*
746 * Drop the i-th range from the early reservation map,
747 * by copying any higher ranges down one over it, and
748 * clearing what had been the last slot.
749 */
750static void __init drop_range(int i)
751{
752 int j;
753
754 for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++)
755 ;
756
757 memmove(&early_res[i], &early_res[i + 1],
758 (j - 1 - i) * sizeof(struct early_res));
759
760 early_res[j - 1].end = 0;
761}
762
763/*
764 * Split any existing ranges that:
765 * 1) are marked 'overlap_ok', and
766 * 2) overlap with the stated range [start, end)
767 * into whatever portion (if any) of the existing range is entirely
768 * below or entirely above the stated range. Drop the portion
769 * of the existing range that overlaps with the stated range,
770 * which will allow the caller of this routine to then add that
771 * stated range without conflicting with any existing range.
772 */
773static void __init drop_overlaps_that_are_ok(u64 start, u64 end)
774{
775 int i;
776 struct early_res *r;
777 u64 lower_start, lower_end;
778 u64 upper_start, upper_end;
779 char name[16];
780
781 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
782 r = &early_res[i];
783
784 /* Continue past non-overlapping ranges */
785 if (end <= r->start || start >= r->end)
786 continue;
787
788 /*
789 * Leave non-ok overlaps as is; let caller
790 * panic "Overlapping early reservations"
791 * when it hits this overlap.
792 */
793 if (!r->overlap_ok)
794 return;
795
796 /*
797 * We have an ok overlap. We will drop it from the early
798 * reservation map, and add back in any non-overlapping
799 * portions (lower or upper) as separate, overlap_ok,
800 * non-overlapping ranges.
801 */
802
803 /* 1. Note any non-overlapping (lower or upper) ranges. */
804 strncpy(name, r->name, sizeof(name) - 1);
805
806 lower_start = lower_end = 0;
807 upper_start = upper_end = 0;
808 if (r->start < start) {
809 lower_start = r->start;
810 lower_end = start;
811 }
812 if (r->end > end) {
813 upper_start = end;
814 upper_end = r->end;
815 }
816
817 /* 2. Drop the original ok overlapping range */
818 drop_range(i);
819
820 i--; /* resume for-loop on copied down entry */
821
822 /* 3. Add back in any non-overlapping ranges. */
823 if (lower_end)
824 reserve_early_overlap_ok(lower_start, lower_end, name);
825 if (upper_end)
826 reserve_early_overlap_ok(upper_start, upper_end, name);
827 }
828}
829
830static void __init __reserve_early(u64 start, u64 end, char *name,
831 int overlap_ok)
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700832{
833 int i;
834 struct early_res *r;
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800835
836 i = find_overlapped_early(start, end);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700837 if (i >= MAX_EARLY_RES)
838 panic("Too many early reservations");
839 r = &early_res[i];
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800840 if (r->end)
841 panic("Overlapping early reservations "
842 "%llx-%llx %s to %llx-%llx %s\n",
843 start, end - 1, name?name:"", r->start,
844 r->end - 1, r->name);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700845 r->start = start;
846 r->end = end;
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700847 r->overlap_ok = overlap_ok;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700848 if (name)
849 strncpy(r->name, name, sizeof(r->name) - 1);
850}
851
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700852/*
853 * A few early reservtations come here.
854 *
855 * The 'overlap_ok' in the name of this routine does -not- mean it
856 * is ok for these reservations to overlap an earlier reservation.
857 * Rather it means that it is ok for subsequent reservations to
858 * overlap this one.
859 *
860 * Use this entry point to reserve early ranges when you are doing
861 * so out of "Paranoia", reserving perhaps more memory than you need,
862 * just in case, and don't mind a subsequent overlapping reservation
863 * that is known to be needed.
864 *
865 * The drop_overlaps_that_are_ok() call here isn't really needed.
866 * It would be needed if we had two colliding 'overlap_ok'
867 * reservations, so that the second such would not panic on the
868 * overlap with the first. We don't have any such as of this
869 * writing, but might as well tolerate such if it happens in
870 * the future.
871 */
872void __init reserve_early_overlap_ok(u64 start, u64 end, char *name)
873{
874 drop_overlaps_that_are_ok(start, end);
875 __reserve_early(start, end, name, 1);
876}
877
878/*
879 * Most early reservations come here.
880 *
881 * We first have drop_overlaps_that_are_ok() drop any pre-existing
882 * 'overlap_ok' ranges, so that we can then reserve this memory
883 * range without risk of panic'ing on an overlapping overlap_ok
884 * early reservation.
885 */
886void __init reserve_early(u64 start, u64 end, char *name)
887{
Yinghai Lu46cb27f2009-02-24 13:12:43 -0800888 if (start >= end)
889 return;
890
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700891 drop_overlaps_that_are_ok(start, end);
892 __reserve_early(start, end, name, 0);
893}
894
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700895void __init free_early(u64 start, u64 end)
896{
897 struct early_res *r;
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700898 int i;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700899
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800900 i = find_overlapped_early(start, end);
901 r = &early_res[i];
902 if (i >= MAX_EARLY_RES || r->end != end || r->start != start)
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700903 panic("free_early on not reserved area: %llx-%llx!",
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800904 start, end - 1);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700905
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700906 drop_range(i);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700907}
908
909void __init early_res_to_bootmem(u64 start, u64 end)
910{
Yinghai Luab677152008-06-27 15:36:54 -0700911 int i, count;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700912 u64 final_start, final_end;
Yinghai Luab677152008-06-27 15:36:54 -0700913
914 count = 0;
915 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++)
916 count++;
917
Yinghai Lu5f1f2b32008-07-18 16:16:23 -0700918 printk(KERN_INFO "(%d early reservations) ==> bootmem [%010llx - %010llx]\n",
919 count, start, end);
Yinghai Luab677152008-06-27 15:36:54 -0700920 for (i = 0; i < count; i++) {
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700921 struct early_res *r = &early_res[i];
Yinghai Lu4fcc5452008-07-01 20:03:11 -0700922 printk(KERN_INFO " #%d [%010llx - %010llx] %16s", i,
Yinghai Luab677152008-06-27 15:36:54 -0700923 r->start, r->end, r->name);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700924 final_start = max(start, r->start);
925 final_end = min(end, r->end);
Yinghai Luab677152008-06-27 15:36:54 -0700926 if (final_start >= final_end) {
927 printk(KERN_CONT "\n");
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700928 continue;
Yinghai Luab677152008-06-27 15:36:54 -0700929 }
Yinghai Lu4fcc5452008-07-01 20:03:11 -0700930 printk(KERN_CONT " ==> [%010llx - %010llx]\n",
Yinghai Luab677152008-06-27 15:36:54 -0700931 final_start, final_end);
Yinghai Lud2dbf342008-06-13 02:00:56 -0700932 reserve_bootmem_generic(final_start, final_end - final_start,
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700933 BOOTMEM_DEFAULT);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700934 }
935}
936
937/* Check for already reserved areas */
938static inline int __init bad_addr(u64 *addrp, u64 size, u64 align)
939{
940 int i;
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800941 u64 addr = *addrp;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700942 int changed = 0;
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800943 struct early_res *r;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700944again:
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800945 i = find_overlapped_early(addr, addr + size);
946 r = &early_res[i];
947 if (i < MAX_EARLY_RES && r->end) {
948 *addrp = addr = round_up(r->end, align);
949 changed = 1;
950 goto again;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700951 }
952 return changed;
953}
954
955/* Check for already reserved areas */
956static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align)
957{
958 int i;
959 u64 addr = *addrp, last;
960 u64 size = *sizep;
961 int changed = 0;
962again:
963 last = addr + size;
964 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
965 struct early_res *r = &early_res[i];
966 if (last > r->start && addr < r->start) {
967 size = r->start - addr;
968 changed = 1;
969 goto again;
970 }
971 if (last > r->end && addr < r->end) {
972 addr = round_up(r->end, align);
973 size = last - addr;
974 changed = 1;
975 goto again;
976 }
977 if (last <= r->end && addr >= r->start) {
978 (*sizep)++;
979 return 0;
980 }
981 }
982 if (changed) {
983 *addrp = addr;
984 *sizep = size;
985 }
986 return changed;
987}
988
989/*
990 * Find a free area with specified alignment in a specific range.
991 */
992u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align)
993{
994 int i;
995
996 for (i = 0; i < e820.nr_map; i++) {
997 struct e820entry *ei = &e820.map[i];
998 u64 addr, last;
999 u64 ei_last;
1000
1001 if (ei->type != E820_RAM)
1002 continue;
1003 addr = round_up(ei->addr, align);
1004 ei_last = ei->addr + ei->size;
1005 if (addr < start)
1006 addr = round_up(start, align);
1007 if (addr >= ei_last)
1008 continue;
1009 while (bad_addr(&addr, size, align) && addr+size <= ei_last)
1010 ;
1011 last = addr + size;
1012 if (last > ei_last)
1013 continue;
1014 if (last > end)
1015 continue;
1016 return addr;
1017 }
1018 return -1ULL;
1019}
1020
1021/*
1022 * Find next free range after *start
1023 */
1024u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align)
1025{
1026 int i;
1027
1028 for (i = 0; i < e820.nr_map; i++) {
1029 struct e820entry *ei = &e820.map[i];
1030 u64 addr, last;
1031 u64 ei_last;
1032
1033 if (ei->type != E820_RAM)
1034 continue;
1035 addr = round_up(ei->addr, align);
1036 ei_last = ei->addr + ei->size;
1037 if (addr < start)
1038 addr = round_up(start, align);
1039 if (addr >= ei_last)
1040 continue;
1041 *sizep = ei_last - addr;
1042 while (bad_addr_size(&addr, sizep, align) &&
1043 addr + *sizep <= ei_last)
1044 ;
1045 last = addr + *sizep;
1046 if (last > ei_last)
1047 continue;
1048 return addr;
1049 }
Yinghai Lua4c81cf2008-05-18 01:18:57 -07001050
Jan Beulich5c0e6f02009-03-12 13:07:23 +00001051 return -1ULL;
Yinghai Lua4c81cf2008-05-18 01:18:57 -07001052}
Yinghai Lu2944e162008-06-01 13:17:38 -07001053
1054/*
1055 * pre allocated 4k and reserved it in e820
1056 */
1057u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align)
1058{
1059 u64 size = 0;
1060 u64 addr;
1061 u64 start;
1062
1063 start = startt;
Jan Beulich5c0e6f02009-03-12 13:07:23 +00001064 while (size < sizet && (start + 1))
Yinghai Lu2944e162008-06-01 13:17:38 -07001065 start = find_e820_area_size(start, &size, align);
1066
1067 if (size < sizet)
1068 return 0;
1069
Jan Beulich5c0e6f02009-03-12 13:07:23 +00001070#ifdef CONFIG_X86_32
1071 if (start >= MAXMEM)
1072 return 0;
1073 if (start + size > MAXMEM)
1074 size = MAXMEM - start;
1075#endif
1076
Yinghai Lu2944e162008-06-01 13:17:38 -07001077 addr = round_down(start + size - sizet, align);
Jan Beulich5c0e6f02009-03-12 13:07:23 +00001078 if (addr < start)
1079 return 0;
Yinghai Lud0be6bd2008-06-15 18:58:51 -07001080 e820_update_range(addr, sizet, E820_RAM, E820_RESERVED);
Yinghai Lufc9036e2008-07-03 11:39:00 -07001081 e820_update_range_saved(addr, sizet, E820_RAM, E820_RESERVED);
Yinghai Lu2944e162008-06-01 13:17:38 -07001082 printk(KERN_INFO "update e820 for early_reserve_e820\n");
1083 update_e820();
Yinghai Lufc9036e2008-07-03 11:39:00 -07001084 update_e820_saved();
Yinghai Lu2944e162008-06-01 13:17:38 -07001085
1086 return addr;
1087}
1088
Yinghai Luee0c80f2008-06-03 19:34:00 -07001089#ifdef CONFIG_X86_32
1090# ifdef CONFIG_X86_PAE
1091# define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
1092# else
1093# define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
1094# endif
1095#else /* CONFIG_X86_32 */
Yinghai Lubd70e522008-06-04 13:21:29 -07001096# define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
Yinghai Luee0c80f2008-06-03 19:34:00 -07001097#endif
1098
1099/*
Yinghai Luee0c80f2008-06-03 19:34:00 -07001100 * Find the highest page frame number we have available
1101 */
Yinghai Luf361a452008-07-10 20:38:26 -07001102static unsigned long __init e820_end_pfn(unsigned long limit_pfn, unsigned type)
Yinghai Luee0c80f2008-06-03 19:34:00 -07001103{
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001104 int i;
1105 unsigned long last_pfn = 0;
Yinghai Luee0c80f2008-06-03 19:34:00 -07001106 unsigned long max_arch_pfn = MAX_ARCH_PFN;
1107
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001108 for (i = 0; i < e820.nr_map; i++) {
1109 struct e820entry *ei = &e820.map[i];
Yinghai Luf361a452008-07-10 20:38:26 -07001110 unsigned long start_pfn;
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001111 unsigned long end_pfn;
1112
Yinghai Luf361a452008-07-10 20:38:26 -07001113 if (ei->type != type)
Yinghai Luc22d4c12008-07-09 03:01:14 -07001114 continue;
Yinghai Luc22d4c12008-07-09 03:01:14 -07001115
Yinghai Luf361a452008-07-10 20:38:26 -07001116 start_pfn = ei->addr >> PAGE_SHIFT;
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001117 end_pfn = (ei->addr + ei->size) >> PAGE_SHIFT;
Yinghai Luf361a452008-07-10 20:38:26 -07001118
1119 if (start_pfn >= limit_pfn)
1120 continue;
1121 if (end_pfn > limit_pfn) {
1122 last_pfn = limit_pfn;
1123 break;
1124 }
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001125 if (end_pfn > last_pfn)
1126 last_pfn = end_pfn;
1127 }
Yinghai Luee0c80f2008-06-03 19:34:00 -07001128
1129 if (last_pfn > max_arch_pfn)
1130 last_pfn = max_arch_pfn;
Yinghai Luee0c80f2008-06-03 19:34:00 -07001131
Paul Jackson5dab8ec2008-06-25 05:44:40 -07001132 printk(KERN_INFO "last_pfn = %#lx max_arch_pfn = %#lx\n",
Yinghai Luee0c80f2008-06-03 19:34:00 -07001133 last_pfn, max_arch_pfn);
1134 return last_pfn;
1135}
Yinghai Luf361a452008-07-10 20:38:26 -07001136unsigned long __init e820_end_of_ram_pfn(void)
1137{
1138 return e820_end_pfn(MAX_ARCH_PFN, E820_RAM);
1139}
Yinghai Luee0c80f2008-06-03 19:34:00 -07001140
Yinghai Luf361a452008-07-10 20:38:26 -07001141unsigned long __init e820_end_of_low_ram_pfn(void)
1142{
1143 return e820_end_pfn(1UL<<(32 - PAGE_SHIFT), E820_RAM);
1144}
Yinghai Luee0c80f2008-06-03 19:34:00 -07001145/*
1146 * Finds an active region in the address range from start_pfn to last_pfn and
1147 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
1148 */
1149int __init e820_find_active_region(const struct e820entry *ei,
1150 unsigned long start_pfn,
1151 unsigned long last_pfn,
1152 unsigned long *ei_startpfn,
1153 unsigned long *ei_endpfn)
1154{
1155 u64 align = PAGE_SIZE;
1156
1157 *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT;
1158 *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT;
1159
1160 /* Skip map entries smaller than a page */
1161 if (*ei_startpfn >= *ei_endpfn)
1162 return 0;
1163
1164 /* Skip if map is outside the node */
1165 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
1166 *ei_startpfn >= last_pfn)
1167 return 0;
1168
1169 /* Check for overlaps */
1170 if (*ei_startpfn < start_pfn)
1171 *ei_startpfn = start_pfn;
1172 if (*ei_endpfn > last_pfn)
1173 *ei_endpfn = last_pfn;
1174
Yinghai Luee0c80f2008-06-03 19:34:00 -07001175 return 1;
1176}
1177
1178/* Walk the e820 map and register active regions within a node */
1179void __init e820_register_active_regions(int nid, unsigned long start_pfn,
1180 unsigned long last_pfn)
1181{
1182 unsigned long ei_startpfn;
1183 unsigned long ei_endpfn;
1184 int i;
1185
1186 for (i = 0; i < e820.nr_map; i++)
1187 if (e820_find_active_region(&e820.map[i],
1188 start_pfn, last_pfn,
1189 &ei_startpfn, &ei_endpfn))
1190 add_active_range(nid, ei_startpfn, ei_endpfn);
1191}
1192
1193/*
1194 * Find the hole size (in bytes) in the memory range.
1195 * @start: starting address of the memory range to scan
1196 * @end: ending address of the memory range to scan
1197 */
1198u64 __init e820_hole_size(u64 start, u64 end)
1199{
1200 unsigned long start_pfn = start >> PAGE_SHIFT;
1201 unsigned long last_pfn = end >> PAGE_SHIFT;
1202 unsigned long ei_startpfn, ei_endpfn, ram = 0;
1203 int i;
1204
1205 for (i = 0; i < e820.nr_map; i++) {
1206 if (e820_find_active_region(&e820.map[i],
1207 start_pfn, last_pfn,
1208 &ei_startpfn, &ei_endpfn))
1209 ram += ei_endpfn - ei_startpfn;
1210 }
1211 return end - start - ((u64)ram << PAGE_SHIFT);
1212}
Yinghai Luab4a4652008-06-10 12:55:54 -07001213
1214static void early_panic(char *msg)
1215{
1216 early_printk(msg);
1217 panic(msg);
1218}
1219
Yinghai Lu69a77042008-07-10 04:17:00 -07001220static int userdef __initdata;
1221
Yinghai Luab4a4652008-06-10 12:55:54 -07001222/* "mem=nopentium" disables the 4MB page tables. */
1223static int __init parse_memopt(char *p)
1224{
1225 u64 mem_size;
1226
1227 if (!p)
1228 return -EINVAL;
1229
1230#ifdef CONFIG_X86_32
1231 if (!strcmp(p, "nopentium")) {
1232 setup_clear_cpu_cap(X86_FEATURE_PSE);
1233 return 0;
1234 }
1235#endif
1236
Yinghai Lu69a77042008-07-10 04:17:00 -07001237 userdef = 1;
Yinghai Luab4a4652008-06-10 12:55:54 -07001238 mem_size = memparse(p, &p);
Yinghai Lu69a77042008-07-10 04:17:00 -07001239 e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
Bernhard Walle611dfd72008-06-25 21:39:16 +02001240
Yinghai Luab4a4652008-06-10 12:55:54 -07001241 return 0;
1242}
1243early_param("mem", parse_memopt);
1244
Yinghai Luab4a4652008-06-10 12:55:54 -07001245static int __init parse_memmap_opt(char *p)
1246{
1247 char *oldp;
1248 u64 start_at, mem_size;
1249
Cyrill Gorcunova737abd2008-07-05 15:53:39 +04001250 if (!p)
1251 return -EINVAL;
1252
Prarit Bhargavad6be118a2008-09-09 09:56:08 -04001253 if (!strncmp(p, "exactmap", 8)) {
Yinghai Luab4a4652008-06-10 12:55:54 -07001254#ifdef CONFIG_CRASH_DUMP
1255 /*
1256 * If we are doing a crash dump, we still need to know
1257 * the real mem size before original memory map is
1258 * reset.
1259 */
Yinghai Luf361a452008-07-10 20:38:26 -07001260 saved_max_pfn = e820_end_of_ram_pfn();
Yinghai Luab4a4652008-06-10 12:55:54 -07001261#endif
1262 e820.nr_map = 0;
1263 userdef = 1;
1264 return 0;
1265 }
1266
1267 oldp = p;
1268 mem_size = memparse(p, &p);
1269 if (p == oldp)
1270 return -EINVAL;
1271
1272 userdef = 1;
1273 if (*p == '@') {
1274 start_at = memparse(p+1, &p);
Yinghai Lud0be6bd2008-06-15 18:58:51 -07001275 e820_add_region(start_at, mem_size, E820_RAM);
Yinghai Luab4a4652008-06-10 12:55:54 -07001276 } else if (*p == '#') {
1277 start_at = memparse(p+1, &p);
Yinghai Lud0be6bd2008-06-15 18:58:51 -07001278 e820_add_region(start_at, mem_size, E820_ACPI);
Yinghai Luab4a4652008-06-10 12:55:54 -07001279 } else if (*p == '$') {
1280 start_at = memparse(p+1, &p);
Yinghai Lud0be6bd2008-06-15 18:58:51 -07001281 e820_add_region(start_at, mem_size, E820_RESERVED);
Yinghai Lu7b479be2008-07-12 22:57:07 -07001282 } else
Yinghai Lu69a77042008-07-10 04:17:00 -07001283 e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
Yinghai Lu7b479be2008-07-12 22:57:07 -07001284
Yinghai Luab4a4652008-06-10 12:55:54 -07001285 return *p == '\0' ? 0 : -EINVAL;
1286}
1287early_param("memmap", parse_memmap_opt);
1288
1289void __init finish_e820_parsing(void)
1290{
1291 if (userdef) {
1292 int nr = e820.nr_map;
1293
1294 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
1295 early_panic("Invalid user supplied memory map");
1296 e820.nr_map = nr;
1297
1298 printk(KERN_INFO "user-defined physical RAM map:\n");
1299 e820_print_map("user");
1300 }
1301}
Yinghai Lu41c094f2008-06-16 13:03:31 -07001302
Bernhard Walle5dfcf142008-06-27 13:12:55 +02001303static inline const char *e820_type_to_string(int e820_type)
1304{
1305 switch (e820_type) {
1306 case E820_RESERVED_KERN:
1307 case E820_RAM: return "System RAM";
1308 case E820_ACPI: return "ACPI Tables";
1309 case E820_NVS: return "ACPI Non-volatile Storage";
Cihula, Joseph671eef82008-08-20 16:43:07 -07001310 case E820_UNUSABLE: return "Unusable memory";
Bernhard Walle5dfcf142008-06-27 13:12:55 +02001311 default: return "reserved";
1312 }
1313}
1314
Yinghai Lu41c094f2008-06-16 13:03:31 -07001315/*
1316 * Mark e820 reserved areas as busy for the resource manager.
1317 */
Ingo Molnara5444d12008-08-29 08:09:23 +02001318static struct resource __initdata *e820_res;
Yinghai Lu41c094f2008-06-16 13:03:31 -07001319void __init e820_reserve_resources(void)
1320{
1321 int i;
Yinghai Lu58f7c982008-08-28 13:52:25 -07001322 struct resource *res;
Ingo Molnara5444d12008-08-29 08:09:23 +02001323 u64 end;
Yinghai Lu41c094f2008-06-16 13:03:31 -07001324
1325 res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
Yinghai Lu58f7c982008-08-28 13:52:25 -07001326 e820_res = res;
Yinghai Lu41c094f2008-06-16 13:03:31 -07001327 for (i = 0; i < e820.nr_map; i++) {
Yinghai Lub4df32f2008-06-28 17:49:59 -07001328 end = e820.map[i].addr + e820.map[i].size - 1;
Jeremy Fitzhardinge8308c542008-09-11 01:31:50 -07001329 if (end != (resource_size_t)end) {
Yinghai Lu41c094f2008-06-16 13:03:31 -07001330 res++;
1331 continue;
1332 }
Bernhard Walle5dfcf142008-06-27 13:12:55 +02001333 res->name = e820_type_to_string(e820.map[i].type);
Yinghai Lub4df32f2008-06-28 17:49:59 -07001334 res->start = e820.map[i].addr;
1335 res->end = end;
1336
Linus Torvalds1f987572008-11-01 10:17:22 -07001337 res->flags = IORESOURCE_MEM;
Ingo Molnara5444d12008-08-29 08:09:23 +02001338
1339 /*
1340 * don't register the region that could be conflicted with
1341 * pci device BAR resource and insert them later in
1342 * pcibios_resource_survey()
1343 */
Linus Torvalds1f987572008-11-01 10:17:22 -07001344 if (e820.map[i].type != E820_RESERVED || res->start < (1ULL<<20)) {
1345 res->flags |= IORESOURCE_BUSY;
Yinghai Lu58f7c982008-08-28 13:52:25 -07001346 insert_resource(&iomem_resource, res);
Linus Torvalds1f987572008-11-01 10:17:22 -07001347 }
Yinghai Lu41c094f2008-06-16 13:03:31 -07001348 res++;
1349 }
Bernhard Walle5dfcf142008-06-27 13:12:55 +02001350
1351 for (i = 0; i < e820_saved.nr_map; i++) {
1352 struct e820entry *entry = &e820_saved.map[i];
1353 firmware_map_add_early(entry->addr,
1354 entry->addr + entry->size - 1,
1355 e820_type_to_string(entry->type));
1356 }
Yinghai Lu41c094f2008-06-16 13:03:31 -07001357}
1358
Yinghai Lu58f7c982008-08-28 13:52:25 -07001359void __init e820_reserve_resources_late(void)
1360{
1361 int i;
1362 struct resource *res;
1363
1364 res = e820_res;
1365 for (i = 0; i < e820.nr_map; i++) {
Ingo Molnara5444d12008-08-29 08:09:23 +02001366 if (!res->parent && res->end)
Linus Torvalds1f987572008-11-01 10:17:22 -07001367 insert_resource_expand_to_fit(&iomem_resource, res);
Yinghai Lu58f7c982008-08-28 13:52:25 -07001368 res++;
1369 }
1370}
1371
Yinghai Lu95a71a42008-06-18 17:27:08 -07001372char *__init default_machine_specific_memory_setup(void)
Yinghai Lu064d25f2008-06-16 19:58:28 -07001373{
1374 char *who = "BIOS-e820";
1375 int new_nr;
1376 /*
1377 * Try to copy the BIOS-supplied E820-map.
1378 *
1379 * Otherwise fake a memory map; one section from 0k->640k,
1380 * the next section from 1mb->appropriate_mem_k
1381 */
1382 new_nr = boot_params.e820_entries;
1383 sanitize_e820_map(boot_params.e820_map,
1384 ARRAY_SIZE(boot_params.e820_map),
1385 &new_nr);
1386 boot_params.e820_entries = new_nr;
Yinghai Ludc8e8122008-07-01 20:02:16 -07001387 if (append_e820_map(boot_params.e820_map, boot_params.e820_entries)
1388 < 0) {
Yinghai Lu95a71a42008-06-18 17:27:08 -07001389 u64 mem_size;
Yinghai Lu41c094f2008-06-16 13:03:31 -07001390
Yinghai Lu064d25f2008-06-16 19:58:28 -07001391 /* compare results from other methods and take the greater */
1392 if (boot_params.alt_mem_k
1393 < boot_params.screen_info.ext_mem_k) {
1394 mem_size = boot_params.screen_info.ext_mem_k;
1395 who = "BIOS-88";
1396 } else {
1397 mem_size = boot_params.alt_mem_k;
1398 who = "BIOS-e801";
1399 }
1400
1401 e820.nr_map = 0;
1402 e820_add_region(0, LOWMEMSIZE(), E820_RAM);
1403 e820_add_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
Yinghai Lu064d25f2008-06-16 19:58:28 -07001404 }
1405
1406 /* In case someone cares... */
1407 return who;
1408}
1409
Yinghai Lu95a71a42008-06-18 17:27:08 -07001410char *__init __attribute__((weak)) machine_specific_memory_setup(void)
1411{
Yinghai Lu3c9cb6d2008-07-19 02:07:25 -07001412 if (x86_quirks->arch_memory_setup) {
1413 char *who = x86_quirks->arch_memory_setup();
Ingo Molnar3b335532008-07-10 17:30:40 +02001414
1415 if (who)
1416 return who;
1417 }
Yinghai Lu95a71a42008-06-18 17:27:08 -07001418 return default_machine_specific_memory_setup();
1419}
1420
Yinghai Lu064d25f2008-06-16 19:58:28 -07001421/* Overridden in paravirt.c if CONFIG_PARAVIRT */
1422char * __init __attribute__((weak)) memory_setup(void)
1423{
1424 return machine_specific_memory_setup();
1425}
1426
1427void __init setup_memory_map(void)
1428{
Yinghai Lu0be15522008-07-03 11:35:37 -07001429 char *who;
1430
1431 who = memory_setup();
1432 memcpy(&e820_saved, &e820, sizeof(struct e820map));
Yinghai Lu064d25f2008-06-16 19:58:28 -07001433 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
Yinghai Lu0be15522008-07-03 11:35:37 -07001434 e820_print_map(who);
Yinghai Lu064d25f2008-06-16 19:58:28 -07001435}