blob: 230687ba5ba5fd0feb6a2fcdfffa9520eccc33b7 [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
Cyrill Gorcunov5051fd62009-08-24 21:53:37 +0400118 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
Yinghai Luc61cf4c2009-03-15 00:59:19 -0700134static void __init e820_print_type(u32 type)
135{
136 switch (type) {
137 case E820_RAM:
138 case E820_RESERVED_KERN:
139 printk(KERN_CONT "(usable)");
140 break;
141 case E820_RESERVED:
142 printk(KERN_CONT "(reserved)");
143 break;
144 case E820_ACPI:
145 printk(KERN_CONT "(ACPI data)");
146 break;
147 case E820_NVS:
148 printk(KERN_CONT "(ACPI NVS)");
149 break;
150 case E820_UNUSABLE:
151 printk(KERN_CONT "(unusable)");
152 break;
153 default:
154 printk(KERN_CONT "type %u", type);
155 break;
156 }
157}
158
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700159void __init e820_print_map(char *who)
160{
161 int i;
162
163 for (i = 0; i < e820.nr_map; i++) {
164 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
165 (unsigned long long) e820.map[i].addr,
166 (unsigned long long)
167 (e820.map[i].addr + e820.map[i].size));
Yinghai Luc61cf4c2009-03-15 00:59:19 -0700168 e820_print_type(e820.map[i].type);
169 printk(KERN_CONT "\n");
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700170 }
171}
172
173/*
174 * Sanitize the BIOS e820 map.
175 *
176 * Some e820 responses include overlapping entries. The following
Paul Jackson5b7eb2e2008-05-14 08:15:52 -0700177 * replaces the original e820 map with a new one, removing overlaps,
178 * and resolving conflicting memory types in favor of highest
179 * numbered type.
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700180 *
Paul Jackson5b7eb2e2008-05-14 08:15:52 -0700181 * The input parameter biosmap points to an array of 'struct
182 * e820entry' which on entry has elements in the range [0, *pnr_map)
183 * valid, and which has space for up to max_nr_map entries.
184 * On return, the resulting sanitized e820 map entries will be in
185 * overwritten in the same location, starting at biosmap.
186 *
187 * The integer pointed to by pnr_map must be valid on entry (the
188 * current number of valid entries located at biosmap) and will
189 * be updated on return, with the new number of valid entries
190 * (something no more than max_nr_map.)
191 *
192 * The return value from sanitize_e820_map() is zero if it
193 * successfully 'sanitized' the map entries passed in, and is -1
194 * if it did nothing, which can happen if either of (1) it was
195 * only passed one map entry, or (2) any of the input map entries
196 * were invalid (start + size < start, meaning that the size was
197 * so big the described memory range wrapped around through zero.)
198 *
199 * Visually we're performing the following
200 * (1,2,3,4 = memory types)...
201 *
202 * Sample memory map (w/overlaps):
203 * ____22__________________
204 * ______________________4_
205 * ____1111________________
206 * _44_____________________
207 * 11111111________________
208 * ____________________33__
209 * ___________44___________
210 * __________33333_________
211 * ______________22________
212 * ___________________2222_
213 * _________111111111______
214 * _____________________11_
215 * _________________4______
216 *
217 * Sanitized equivalent (no overlap):
218 * 1_______________________
219 * _44_____________________
220 * ___1____________________
221 * ____22__________________
222 * ______11________________
223 * _________1______________
224 * __________3_____________
225 * ___________44___________
226 * _____________33_________
227 * _______________2________
228 * ________________1_______
229 * _________________4______
230 * ___________________2____
231 * ____________________33__
232 * ______________________4_
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700233 */
Paul Jackson5b7eb2e2008-05-14 08:15:52 -0700234
Paul Jacksonc3965bd2008-05-14 08:15:34 -0700235int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map,
Jaswinder Singh Rajputba639032009-03-23 02:13:01 +0530236 u32 *pnr_map)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700237{
238 struct change_member {
239 struct e820entry *pbios; /* pointer to original bios entry */
240 unsigned long long addr; /* address for this change point */
241 };
Paul Jackson157fabf2008-06-22 07:21:57 -0700242 static struct change_member change_point_list[2*E820_X_MAX] __initdata;
243 static struct change_member *change_point[2*E820_X_MAX] __initdata;
244 static struct e820entry *overlap_list[E820_X_MAX] __initdata;
245 static struct e820entry new_bios[E820_X_MAX] __initdata;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700246 struct change_member *change_tmp;
247 unsigned long current_type, last_type;
248 unsigned long long last_addr;
249 int chgidx, still_changing;
250 int overlap_entries;
251 int new_bios_entry;
252 int old_nr, new_nr, chg_nr;
253 int i;
254
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700255 /* if there's only one memory region, don't bother */
256 if (*pnr_map < 2)
257 return -1;
258
259 old_nr = *pnr_map;
Paul Jackson6e9bcc72008-05-14 08:15:46 -0700260 BUG_ON(old_nr > max_nr_map);
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700261
262 /* bail out if we find any unreasonable addresses in bios map */
263 for (i = 0; i < old_nr; i++)
264 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
265 return -1;
266
267 /* create pointers for initial change-point information (for sorting) */
268 for (i = 0; i < 2 * old_nr; i++)
269 change_point[i] = &change_point_list[i];
270
271 /* record all known change-points (starting and ending addresses),
272 omitting those that are for empty memory regions */
273 chgidx = 0;
274 for (i = 0; i < old_nr; i++) {
275 if (biosmap[i].size != 0) {
276 change_point[chgidx]->addr = biosmap[i].addr;
277 change_point[chgidx++]->pbios = &biosmap[i];
278 change_point[chgidx]->addr = biosmap[i].addr +
279 biosmap[i].size;
280 change_point[chgidx++]->pbios = &biosmap[i];
281 }
282 }
283 chg_nr = chgidx;
284
285 /* sort change-point list by memory addresses (low -> high) */
286 still_changing = 1;
287 while (still_changing) {
288 still_changing = 0;
289 for (i = 1; i < chg_nr; i++) {
290 unsigned long long curaddr, lastaddr;
291 unsigned long long curpbaddr, lastpbaddr;
292
293 curaddr = change_point[i]->addr;
294 lastaddr = change_point[i - 1]->addr;
295 curpbaddr = change_point[i]->pbios->addr;
296 lastpbaddr = change_point[i - 1]->pbios->addr;
297
298 /*
299 * swap entries, when:
300 *
301 * curaddr > lastaddr or
302 * curaddr == lastaddr and curaddr == curpbaddr and
303 * lastaddr != lastpbaddr
304 */
305 if (curaddr < lastaddr ||
306 (curaddr == lastaddr && curaddr == curpbaddr &&
307 lastaddr != lastpbaddr)) {
308 change_tmp = change_point[i];
309 change_point[i] = change_point[i-1];
310 change_point[i-1] = change_tmp;
311 still_changing = 1;
312 }
313 }
314 }
315
316 /* create a new bios memory map, removing overlaps */
317 overlap_entries = 0; /* number of entries in the overlap table */
318 new_bios_entry = 0; /* index for creating new bios map entries */
319 last_type = 0; /* start with undefined memory type */
320 last_addr = 0; /* start with 0 as last starting address */
321
322 /* loop through change-points, determining affect on the new bios map */
323 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
324 /* keep track of all overlapping bios entries */
325 if (change_point[chgidx]->addr ==
326 change_point[chgidx]->pbios->addr) {
327 /*
328 * add map entry to overlap list (> 1 entry
329 * implies an overlap)
330 */
331 overlap_list[overlap_entries++] =
332 change_point[chgidx]->pbios;
333 } else {
334 /*
335 * remove entry from list (order independent,
336 * so swap with last)
337 */
338 for (i = 0; i < overlap_entries; i++) {
339 if (overlap_list[i] ==
340 change_point[chgidx]->pbios)
341 overlap_list[i] =
342 overlap_list[overlap_entries-1];
343 }
344 overlap_entries--;
345 }
346 /*
347 * if there are overlapping entries, decide which
348 * "type" to use (larger value takes precedence --
349 * 1=usable, 2,3,4,4+=unusable)
350 */
351 current_type = 0;
352 for (i = 0; i < overlap_entries; i++)
353 if (overlap_list[i]->type > current_type)
354 current_type = overlap_list[i]->type;
355 /*
356 * continue building up new bios map based on this
357 * information
358 */
359 if (current_type != last_type) {
360 if (last_type != 0) {
361 new_bios[new_bios_entry].size =
362 change_point[chgidx]->addr - last_addr;
363 /*
364 * move forward only if the new size
365 * was non-zero
366 */
367 if (new_bios[new_bios_entry].size != 0)
368 /*
369 * no more space left for new
370 * bios entries ?
371 */
Paul Jacksonc3965bd2008-05-14 08:15:34 -0700372 if (++new_bios_entry >= max_nr_map)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700373 break;
374 }
375 if (current_type != 0) {
376 new_bios[new_bios_entry].addr =
377 change_point[chgidx]->addr;
378 new_bios[new_bios_entry].type = current_type;
379 last_addr = change_point[chgidx]->addr;
380 }
381 last_type = current_type;
382 }
383 }
384 /* retain count for new bios entries */
385 new_nr = new_bios_entry;
386
387 /* copy new bios mapping into original location */
388 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
389 *pnr_map = new_nr;
390
391 return 0;
392}
393
Yinghai Ludc8e8122008-07-01 20:02:16 -0700394static int __init __append_e820_map(struct e820entry *biosmap, int nr_map)
Huang, Ying8c5beb52008-06-11 11:33:39 +0800395{
396 while (nr_map) {
397 u64 start = biosmap->addr;
398 u64 size = biosmap->size;
399 u64 end = start + size;
400 u32 type = biosmap->type;
401
402 /* Overflow in 64 bits? Ignore the memory map. */
403 if (start > end)
404 return -1;
405
406 e820_add_region(start, size, type);
407
408 biosmap++;
409 nr_map--;
410 }
411 return 0;
412}
413
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700414/*
415 * Copy the BIOS e820 map into a safe place.
416 *
417 * Sanity-check it while we're at it..
418 *
419 * If we're lucky and live on a modern system, the setup code
420 * will have given us a memory map that we can use to properly
421 * set up memory. If we aren't, we'll fake a memory map.
422 */
Yinghai Ludc8e8122008-07-01 20:02:16 -0700423static int __init append_e820_map(struct e820entry *biosmap, int nr_map)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700424{
425 /* Only one memory region (or negative)? Ignore it */
426 if (nr_map < 2)
427 return -1;
428
Yinghai Ludc8e8122008-07-01 20:02:16 -0700429 return __append_e820_map(biosmap, nr_map);
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700430}
431
Yinghai Lu773e6732009-03-12 21:35:18 -0700432static u64 __init __e820_update_range(struct e820map *e820x, u64 start,
Yinghai Lufc9036e2008-07-03 11:39:00 -0700433 u64 size, unsigned old_type,
434 unsigned new_type)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700435{
Yinghai Lu78a8b35b2009-03-12 22:36:01 -0700436 u64 end;
Yinghai Lu773e6732009-03-12 21:35:18 -0700437 unsigned int i;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700438 u64 real_updated_size = 0;
439
440 BUG_ON(old_type == new_type);
441
Yinghai Lu232b9572008-06-24 14:58:38 -0700442 if (size > (ULLONG_MAX - start))
443 size = ULLONG_MAX - start;
444
Yinghai Lu78a8b35b2009-03-12 22:36:01 -0700445 end = start + size;
Yinghai Luc61cf4c2009-03-15 00:59:19 -0700446 printk(KERN_DEBUG "e820 update range: %016Lx - %016Lx ",
447 (unsigned long long) start,
448 (unsigned long long) end);
449 e820_print_type(old_type);
450 printk(KERN_CONT " ==> ");
451 e820_print_type(new_type);
452 printk(KERN_CONT "\n");
453
Jan Beulich5c0e6f02009-03-12 13:07:23 +0000454 for (i = 0; i < e820x->nr_map; i++) {
Yinghai Lufc9036e2008-07-03 11:39:00 -0700455 struct e820entry *ei = &e820x->map[i];
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700456 u64 final_start, final_end;
Yinghai Lu78a8b35b2009-03-12 22:36:01 -0700457 u64 ei_end;
458
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700459 if (ei->type != old_type)
460 continue;
Yinghai Lu78a8b35b2009-03-12 22:36:01 -0700461
462 ei_end = ei->addr + ei->size;
463 /* totally covered by new range? */
464 if (ei->addr >= start && ei_end <= end) {
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700465 ei->type = new_type;
466 real_updated_size += ei->size;
467 continue;
468 }
Yinghai Lu78a8b35b2009-03-12 22:36:01 -0700469
470 /* new range is totally covered? */
471 if (ei->addr < start && ei_end > end) {
472 __e820_add_region(e820x, start, size, new_type);
473 __e820_add_region(e820x, end, ei_end - end, ei->type);
474 ei->size = start - ei->addr;
475 real_updated_size += size;
476 continue;
477 }
478
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700479 /* partially covered */
480 final_start = max(start, ei->addr);
Yinghai Lu78a8b35b2009-03-12 22:36:01 -0700481 final_end = min(end, ei_end);
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700482 if (final_start >= final_end)
483 continue;
Jan Beulich5c0e6f02009-03-12 13:07:23 +0000484
Yinghai Lu773e6732009-03-12 21:35:18 -0700485 __e820_add_region(e820x, final_start, final_end - final_start,
486 new_type);
Jan Beulich5c0e6f02009-03-12 13:07:23 +0000487
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700488 real_updated_size += final_end - final_start;
Yinghai Lu976dd4d2008-06-24 14:55:32 -0700489
Yinghai Lu773e6732009-03-12 21:35:18 -0700490 /*
491 * left range could be head or tail, so need to update
492 * size at first.
493 */
494 ei->size -= final_end - final_start;
Yinghai Lu976dd4d2008-06-24 14:55:32 -0700495 if (ei->addr < final_start)
496 continue;
497 ei->addr = final_end;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700498 }
499 return real_updated_size;
500}
501
Yinghai Lufc9036e2008-07-03 11:39:00 -0700502u64 __init e820_update_range(u64 start, u64 size, unsigned old_type,
503 unsigned new_type)
504{
Yinghai Lu773e6732009-03-12 21:35:18 -0700505 return __e820_update_range(&e820, start, size, old_type, new_type);
Yinghai Lufc9036e2008-07-03 11:39:00 -0700506}
507
508static u64 __init e820_update_range_saved(u64 start, u64 size,
509 unsigned old_type, unsigned new_type)
510{
Yinghai Lu773e6732009-03-12 21:35:18 -0700511 return __e820_update_range(&e820_saved, start, size, old_type,
Yinghai Lufc9036e2008-07-03 11:39:00 -0700512 new_type);
513}
514
Yinghai Lu7a1fd982008-06-21 14:48:05 -0700515/* make e820 not cover the range */
516u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type,
517 int checktype)
518{
519 int i;
Yinghai Lu1b5576e2010-01-22 11:21:04 +0800520 u64 end;
Yinghai Lu7a1fd982008-06-21 14:48:05 -0700521 u64 real_removed_size = 0;
522
Yinghai Lu232b9572008-06-24 14:58:38 -0700523 if (size > (ULLONG_MAX - start))
524 size = ULLONG_MAX - start;
525
Yinghai Lu1b5576e2010-01-22 11:21:04 +0800526 end = start + size;
527 printk(KERN_DEBUG "e820 remove range: %016Lx - %016Lx ",
528 (unsigned long long) start,
529 (unsigned long long) end);
530 e820_print_type(old_type);
531 printk(KERN_CONT "\n");
532
Yinghai Lu7a1fd982008-06-21 14:48:05 -0700533 for (i = 0; i < e820.nr_map; i++) {
534 struct e820entry *ei = &e820.map[i];
535 u64 final_start, final_end;
536
537 if (checktype && ei->type != old_type)
538 continue;
539 /* totally covered? */
540 if (ei->addr >= start &&
541 (ei->addr + ei->size) <= (start + size)) {
542 real_removed_size += ei->size;
543 memset(ei, 0, sizeof(struct e820entry));
544 continue;
545 }
546 /* partially covered */
547 final_start = max(start, ei->addr);
548 final_end = min(start + size, ei->addr + ei->size);
549 if (final_start >= final_end)
550 continue;
551 real_removed_size += final_end - final_start;
552
553 ei->size -= final_end - final_start;
554 if (ei->addr < final_start)
555 continue;
556 ei->addr = final_end;
557 }
558 return real_removed_size;
559}
560
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700561void __init update_e820(void)
562{
Jaswinder Singh Rajputba639032009-03-23 02:13:01 +0530563 u32 nr_map;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700564
565 nr_map = e820.nr_map;
Paul Jacksonc3965bd2008-05-14 08:15:34 -0700566 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map))
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700567 return;
568 e820.nr_map = nr_map;
569 printk(KERN_INFO "modified physical RAM map:\n");
570 e820_print_map("modified");
571}
Yinghai Lufc9036e2008-07-03 11:39:00 -0700572static void __init update_e820_saved(void)
573{
Jaswinder Singh Rajputba639032009-03-23 02:13:01 +0530574 u32 nr_map;
Yinghai Lufc9036e2008-07-03 11:39:00 -0700575
576 nr_map = e820_saved.nr_map;
577 if (sanitize_e820_map(e820_saved.map, ARRAY_SIZE(e820_saved.map), &nr_map))
578 return;
579 e820_saved.nr_map = nr_map;
580}
Alok Katariafd6493e2008-06-25 11:02:42 -0700581#define MAX_GAP_END 0x100000000ull
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700582/*
Alok Katariafd6493e2008-06-25 11:02:42 -0700583 * Search for a gap in the e820 memory space from start_addr to end_addr.
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700584 */
Alok Kataria33819592008-06-24 11:48:30 -0700585__init int e820_search_gap(unsigned long *gapstart, unsigned long *gapsize,
Alok Katariafd6493e2008-06-25 11:02:42 -0700586 unsigned long start_addr, unsigned long long end_addr)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700587{
Alok Katariafd6493e2008-06-25 11:02:42 -0700588 unsigned long long last;
Alok Kataria33819592008-06-24 11:48:30 -0700589 int i = e820.nr_map;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700590 int found = 0;
591
Alok Katariafd6493e2008-06-25 11:02:42 -0700592 last = (end_addr && end_addr < MAX_GAP_END) ? end_addr : MAX_GAP_END;
593
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700594 while (--i >= 0) {
595 unsigned long long start = e820.map[i].addr;
596 unsigned long long end = start + e820.map[i].size;
597
Alok Kataria33819592008-06-24 11:48:30 -0700598 if (end < start_addr)
599 continue;
600
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700601 /*
602 * Since "last" is at most 4GB, we know we'll
603 * fit in 32 bits if this condition is true
604 */
605 if (last > end) {
606 unsigned long gap = last - end;
607
Alok Kataria33819592008-06-24 11:48:30 -0700608 if (gap >= *gapsize) {
609 *gapsize = gap;
610 *gapstart = end;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700611 found = 1;
612 }
613 }
614 if (start < last)
615 last = start;
616 }
Alok Kataria33819592008-06-24 11:48:30 -0700617 return found;
618}
619
620/*
621 * Search for the biggest gap in the low 32 bits of the e820
622 * memory space. We pass this space to PCI to assign MMIO resources
623 * for hotplug or unconfigured devices in.
624 * Hopefully the BIOS let enough space left.
625 */
626__init void e820_setup_gap(void)
627{
Yinghai Lu5d423cc2009-05-06 08:07:52 -0700628 unsigned long gapstart, gapsize;
Alok Kataria33819592008-06-24 11:48:30 -0700629 int found;
630
631 gapstart = 0x10000000;
632 gapsize = 0x400000;
Alok Katariafd6493e2008-06-25 11:02:42 -0700633 found = e820_search_gap(&gapstart, &gapsize, 0, MAX_GAP_END);
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700634
635#ifdef CONFIG_X86_64
636 if (!found) {
Yinghai Luc987d122008-06-24 22:14:09 -0700637 gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024;
Joe Perchesad361c92009-07-06 13:05:40 -0700638 printk(KERN_ERR
639 "PCI: Warning: Cannot find a gap in the 32bit address range\n"
640 "PCI: Unassigned devices with 32bit resource registers may break!\n");
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700641 }
642#endif
643
644 /*
Yinghai Lu5d423cc2009-05-06 08:07:52 -0700645 * e820_reserve_resources_late protect stolen RAM already
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700646 */
Yinghai Lu5d423cc2009-05-06 08:07:52 -0700647 pci_mem_start = gapstart;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700648
649 printk(KERN_INFO
650 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
651 pci_mem_start, gapstart, gapsize);
652}
653
Huang, Ying8c5beb52008-06-11 11:33:39 +0800654/**
655 * Because of the size limitation of struct boot_params, only first
656 * 128 E820 memory entries are passed to kernel via
657 * boot_params.e820_map, others are passed via SETUP_E820_EXT node of
658 * linked list of struct setup_data, which is parsed here.
659 */
660void __init parse_e820_ext(struct setup_data *sdata, unsigned long pa_data)
661{
662 u32 map_len;
663 int entries;
664 struct e820entry *extmap;
665
666 entries = sdata->len / sizeof(struct e820entry);
667 map_len = sdata->len + sizeof(struct setup_data);
668 if (map_len > PAGE_SIZE)
669 sdata = early_ioremap(pa_data, map_len);
670 extmap = (struct e820entry *)(sdata->data);
Yinghai Ludc8e8122008-07-01 20:02:16 -0700671 __append_e820_map(extmap, entries);
Huang, Ying8c5beb52008-06-11 11:33:39 +0800672 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
673 if (map_len > PAGE_SIZE)
674 early_iounmap(sdata, map_len);
675 printk(KERN_INFO "extended physical RAM map:\n");
676 e820_print_map("extended");
677}
678
Yinghai Lubf62f392008-05-20 20:10:58 -0700679#if defined(CONFIG_X86_64) || \
680 (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION))
681/**
682 * Find the ranges of physical addresses that do not correspond to
683 * e820 RAM areas and mark the corresponding pages as nosave for
684 * hibernation (32 bit) or software suspend and suspend to RAM (64 bit).
685 *
686 * This function requires the e820 map to be sorted and without any
687 * overlapping entries and assumes the first e820 area to be RAM.
688 */
689void __init e820_mark_nosave_regions(unsigned long limit_pfn)
690{
691 int i;
692 unsigned long pfn;
693
694 pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
695 for (i = 1; i < e820.nr_map; i++) {
696 struct e820entry *ei = &e820.map[i];
697
698 if (pfn < PFN_UP(ei->addr))
699 register_nosave_region(pfn, PFN_UP(ei->addr));
700
701 pfn = PFN_DOWN(ei->addr + ei->size);
Yinghai Lu28bb2232008-06-30 16:20:54 -0700702 if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
Yinghai Lubf62f392008-05-20 20:10:58 -0700703 register_nosave_region(PFN_UP(ei->addr), pfn);
704
705 if (pfn >= limit_pfn)
706 break;
707 }
708}
709#endif
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700710
Rafael J. Wysockib69edc72008-10-31 01:02:41 +0100711#ifdef CONFIG_HIBERNATION
712/**
713 * Mark ACPI NVS memory region, so that we can save/restore it during
714 * hibernation and the subsequent resume.
715 */
716static int __init e820_mark_nvs_memory(void)
717{
718 int i;
719
720 for (i = 0; i < e820.nr_map; i++) {
721 struct e820entry *ei = &e820.map[i];
722
723 if (ei->type == E820_NVS)
724 hibernate_nvs_register(ei->addr, ei->size);
725 }
726
727 return 0;
728}
729core_initcall(e820_mark_nvs_memory);
730#endif
731
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700732/*
733 * Early reserved memory areas.
734 */
735#define MAX_EARLY_RES 20
736
737struct early_res {
738 u64 start, end;
739 char name[16];
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700740 char overlap_ok;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700741};
742static struct early_res early_res[MAX_EARLY_RES] __initdata = {
743 { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700744 {}
745};
746
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800747static int __init find_overlapped_early(u64 start, u64 end)
748{
749 int i;
750 struct early_res *r;
751
752 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
753 r = &early_res[i];
754 if (end > r->start && start < r->end)
755 break;
756 }
757
758 return i;
759}
760
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700761/*
762 * Drop the i-th range from the early reservation map,
763 * by copying any higher ranges down one over it, and
764 * clearing what had been the last slot.
765 */
766static void __init drop_range(int i)
767{
768 int j;
769
770 for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++)
771 ;
772
773 memmove(&early_res[i], &early_res[i + 1],
774 (j - 1 - i) * sizeof(struct early_res));
775
776 early_res[j - 1].end = 0;
777}
778
779/*
780 * Split any existing ranges that:
781 * 1) are marked 'overlap_ok', and
782 * 2) overlap with the stated range [start, end)
783 * into whatever portion (if any) of the existing range is entirely
784 * below or entirely above the stated range. Drop the portion
785 * of the existing range that overlaps with the stated range,
786 * which will allow the caller of this routine to then add that
787 * stated range without conflicting with any existing range.
788 */
789static void __init drop_overlaps_that_are_ok(u64 start, u64 end)
790{
791 int i;
792 struct early_res *r;
793 u64 lower_start, lower_end;
794 u64 upper_start, upper_end;
795 char name[16];
796
797 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
798 r = &early_res[i];
799
800 /* Continue past non-overlapping ranges */
801 if (end <= r->start || start >= r->end)
802 continue;
803
804 /*
805 * Leave non-ok overlaps as is; let caller
806 * panic "Overlapping early reservations"
807 * when it hits this overlap.
808 */
809 if (!r->overlap_ok)
810 return;
811
812 /*
813 * We have an ok overlap. We will drop it from the early
814 * reservation map, and add back in any non-overlapping
815 * portions (lower or upper) as separate, overlap_ok,
816 * non-overlapping ranges.
817 */
818
819 /* 1. Note any non-overlapping (lower or upper) ranges. */
820 strncpy(name, r->name, sizeof(name) - 1);
821
822 lower_start = lower_end = 0;
823 upper_start = upper_end = 0;
824 if (r->start < start) {
825 lower_start = r->start;
826 lower_end = start;
827 }
828 if (r->end > end) {
829 upper_start = end;
830 upper_end = r->end;
831 }
832
833 /* 2. Drop the original ok overlapping range */
834 drop_range(i);
835
836 i--; /* resume for-loop on copied down entry */
837
838 /* 3. Add back in any non-overlapping ranges. */
839 if (lower_end)
840 reserve_early_overlap_ok(lower_start, lower_end, name);
841 if (upper_end)
842 reserve_early_overlap_ok(upper_start, upper_end, name);
843 }
844}
845
846static void __init __reserve_early(u64 start, u64 end, char *name,
847 int overlap_ok)
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700848{
849 int i;
850 struct early_res *r;
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800851
852 i = find_overlapped_early(start, end);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700853 if (i >= MAX_EARLY_RES)
854 panic("Too many early reservations");
855 r = &early_res[i];
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800856 if (r->end)
857 panic("Overlapping early reservations "
858 "%llx-%llx %s to %llx-%llx %s\n",
859 start, end - 1, name?name:"", r->start,
860 r->end - 1, r->name);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700861 r->start = start;
862 r->end = end;
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700863 r->overlap_ok = overlap_ok;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700864 if (name)
865 strncpy(r->name, name, sizeof(r->name) - 1);
866}
867
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700868/*
869 * A few early reservtations come here.
870 *
871 * The 'overlap_ok' in the name of this routine does -not- mean it
872 * is ok for these reservations to overlap an earlier reservation.
873 * Rather it means that it is ok for subsequent reservations to
874 * overlap this one.
875 *
876 * Use this entry point to reserve early ranges when you are doing
877 * so out of "Paranoia", reserving perhaps more memory than you need,
878 * just in case, and don't mind a subsequent overlapping reservation
879 * that is known to be needed.
880 *
881 * The drop_overlaps_that_are_ok() call here isn't really needed.
882 * It would be needed if we had two colliding 'overlap_ok'
883 * reservations, so that the second such would not panic on the
884 * overlap with the first. We don't have any such as of this
885 * writing, but might as well tolerate such if it happens in
886 * the future.
887 */
888void __init reserve_early_overlap_ok(u64 start, u64 end, char *name)
889{
890 drop_overlaps_that_are_ok(start, end);
891 __reserve_early(start, end, name, 1);
892}
893
894/*
895 * Most early reservations come here.
896 *
897 * We first have drop_overlaps_that_are_ok() drop any pre-existing
898 * 'overlap_ok' ranges, so that we can then reserve this memory
899 * range without risk of panic'ing on an overlapping overlap_ok
900 * early reservation.
901 */
902void __init reserve_early(u64 start, u64 end, char *name)
903{
Yinghai Lu46cb27f2009-02-24 13:12:43 -0800904 if (start >= end)
905 return;
906
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700907 drop_overlaps_that_are_ok(start, end);
908 __reserve_early(start, end, name, 0);
909}
910
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700911void __init free_early(u64 start, u64 end)
912{
913 struct early_res *r;
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700914 int i;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700915
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800916 i = find_overlapped_early(start, end);
917 r = &early_res[i];
918 if (i >= MAX_EARLY_RES || r->end != end || r->start != start)
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700919 panic("free_early on not reserved area: %llx-%llx!",
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800920 start, end - 1);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700921
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700922 drop_range(i);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700923}
924
925void __init early_res_to_bootmem(u64 start, u64 end)
926{
Yinghai Luab677152008-06-27 15:36:54 -0700927 int i, count;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700928 u64 final_start, final_end;
Yinghai Luab677152008-06-27 15:36:54 -0700929
930 count = 0;
931 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++)
932 count++;
933
Yinghai Lu5f1f2b32008-07-18 16:16:23 -0700934 printk(KERN_INFO "(%d early reservations) ==> bootmem [%010llx - %010llx]\n",
935 count, start, end);
Yinghai Luab677152008-06-27 15:36:54 -0700936 for (i = 0; i < count; i++) {
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700937 struct early_res *r = &early_res[i];
Yinghai Lu4fcc5452008-07-01 20:03:11 -0700938 printk(KERN_INFO " #%d [%010llx - %010llx] %16s", i,
Yinghai Luab677152008-06-27 15:36:54 -0700939 r->start, r->end, r->name);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700940 final_start = max(start, r->start);
941 final_end = min(end, r->end);
Yinghai Luab677152008-06-27 15:36:54 -0700942 if (final_start >= final_end) {
943 printk(KERN_CONT "\n");
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700944 continue;
Yinghai Luab677152008-06-27 15:36:54 -0700945 }
Yinghai Lu4fcc5452008-07-01 20:03:11 -0700946 printk(KERN_CONT " ==> [%010llx - %010llx]\n",
Yinghai Luab677152008-06-27 15:36:54 -0700947 final_start, final_end);
Yinghai Lud2dbf342008-06-13 02:00:56 -0700948 reserve_bootmem_generic(final_start, final_end - final_start,
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700949 BOOTMEM_DEFAULT);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700950 }
951}
952
953/* Check for already reserved areas */
954static inline int __init bad_addr(u64 *addrp, u64 size, u64 align)
955{
956 int i;
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800957 u64 addr = *addrp;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700958 int changed = 0;
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800959 struct early_res *r;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700960again:
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800961 i = find_overlapped_early(addr, addr + size);
962 r = &early_res[i];
963 if (i < MAX_EARLY_RES && r->end) {
964 *addrp = addr = round_up(r->end, align);
965 changed = 1;
966 goto again;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700967 }
968 return changed;
969}
970
971/* Check for already reserved areas */
972static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align)
973{
974 int i;
975 u64 addr = *addrp, last;
976 u64 size = *sizep;
977 int changed = 0;
978again:
979 last = addr + size;
980 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
981 struct early_res *r = &early_res[i];
982 if (last > r->start && addr < r->start) {
983 size = r->start - addr;
984 changed = 1;
985 goto again;
986 }
987 if (last > r->end && addr < r->end) {
988 addr = round_up(r->end, align);
989 size = last - addr;
990 changed = 1;
991 goto again;
992 }
993 if (last <= r->end && addr >= r->start) {
994 (*sizep)++;
995 return 0;
996 }
997 }
998 if (changed) {
999 *addrp = addr;
1000 *sizep = size;
1001 }
1002 return changed;
1003}
1004
1005/*
1006 * Find a free area with specified alignment in a specific range.
1007 */
1008u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align)
1009{
1010 int i;
1011
1012 for (i = 0; i < e820.nr_map; i++) {
1013 struct e820entry *ei = &e820.map[i];
1014 u64 addr, last;
1015 u64 ei_last;
1016
1017 if (ei->type != E820_RAM)
1018 continue;
1019 addr = round_up(ei->addr, align);
1020 ei_last = ei->addr + ei->size;
1021 if (addr < start)
1022 addr = round_up(start, align);
1023 if (addr >= ei_last)
1024 continue;
1025 while (bad_addr(&addr, size, align) && addr+size <= ei_last)
1026 ;
1027 last = addr + size;
1028 if (last > ei_last)
1029 continue;
1030 if (last > end)
1031 continue;
1032 return addr;
1033 }
1034 return -1ULL;
1035}
1036
1037/*
1038 * Find next free range after *start
1039 */
1040u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align)
1041{
1042 int i;
1043
1044 for (i = 0; i < e820.nr_map; i++) {
1045 struct e820entry *ei = &e820.map[i];
1046 u64 addr, last;
1047 u64 ei_last;
1048
1049 if (ei->type != E820_RAM)
1050 continue;
1051 addr = round_up(ei->addr, align);
1052 ei_last = ei->addr + ei->size;
1053 if (addr < start)
1054 addr = round_up(start, align);
1055 if (addr >= ei_last)
1056 continue;
1057 *sizep = ei_last - addr;
1058 while (bad_addr_size(&addr, sizep, align) &&
1059 addr + *sizep <= ei_last)
1060 ;
1061 last = addr + *sizep;
1062 if (last > ei_last)
1063 continue;
1064 return addr;
1065 }
Yinghai Lua4c81cf2008-05-18 01:18:57 -07001066
Jan Beulich5c0e6f02009-03-12 13:07:23 +00001067 return -1ULL;
Yinghai Lua4c81cf2008-05-18 01:18:57 -07001068}
Yinghai Lu2944e162008-06-01 13:17:38 -07001069
1070/*
1071 * pre allocated 4k and reserved it in e820
1072 */
1073u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align)
1074{
1075 u64 size = 0;
1076 u64 addr;
1077 u64 start;
1078
Jan Beulich61438762009-05-06 13:02:19 +01001079 for (start = startt; ; start += size) {
Yinghai Lu2944e162008-06-01 13:17:38 -07001080 start = find_e820_area_size(start, &size, align);
Jan Beulich61438762009-05-06 13:02:19 +01001081 if (!(start + 1))
1082 return 0;
1083 if (size >= sizet)
1084 break;
1085 }
Yinghai Lu2944e162008-06-01 13:17:38 -07001086
Jan Beulich5c0e6f02009-03-12 13:07:23 +00001087#ifdef CONFIG_X86_32
1088 if (start >= MAXMEM)
1089 return 0;
1090 if (start + size > MAXMEM)
1091 size = MAXMEM - start;
1092#endif
1093
Yinghai Lu2944e162008-06-01 13:17:38 -07001094 addr = round_down(start + size - sizet, align);
Jan Beulich5c0e6f02009-03-12 13:07:23 +00001095 if (addr < start)
1096 return 0;
Yinghai Lud0be6bd2008-06-15 18:58:51 -07001097 e820_update_range(addr, sizet, E820_RAM, E820_RESERVED);
Yinghai Lufc9036e2008-07-03 11:39:00 -07001098 e820_update_range_saved(addr, sizet, E820_RAM, E820_RESERVED);
Yinghai Lu2944e162008-06-01 13:17:38 -07001099 printk(KERN_INFO "update e820 for early_reserve_e820\n");
1100 update_e820();
Yinghai Lufc9036e2008-07-03 11:39:00 -07001101 update_e820_saved();
Yinghai Lu2944e162008-06-01 13:17:38 -07001102
1103 return addr;
1104}
1105
Yinghai Luee0c80f2008-06-03 19:34:00 -07001106#ifdef CONFIG_X86_32
1107# ifdef CONFIG_X86_PAE
1108# define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
1109# else
1110# define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
1111# endif
1112#else /* CONFIG_X86_32 */
Yinghai Lubd70e522008-06-04 13:21:29 -07001113# define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
Yinghai Luee0c80f2008-06-03 19:34:00 -07001114#endif
1115
1116/*
Yinghai Luee0c80f2008-06-03 19:34:00 -07001117 * Find the highest page frame number we have available
1118 */
Yinghai Luf361a452008-07-10 20:38:26 -07001119static unsigned long __init e820_end_pfn(unsigned long limit_pfn, unsigned type)
Yinghai Luee0c80f2008-06-03 19:34:00 -07001120{
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001121 int i;
1122 unsigned long last_pfn = 0;
Yinghai Luee0c80f2008-06-03 19:34:00 -07001123 unsigned long max_arch_pfn = MAX_ARCH_PFN;
1124
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001125 for (i = 0; i < e820.nr_map; i++) {
1126 struct e820entry *ei = &e820.map[i];
Yinghai Luf361a452008-07-10 20:38:26 -07001127 unsigned long start_pfn;
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001128 unsigned long end_pfn;
1129
Yinghai Luf361a452008-07-10 20:38:26 -07001130 if (ei->type != type)
Yinghai Luc22d4c12008-07-09 03:01:14 -07001131 continue;
Yinghai Luc22d4c12008-07-09 03:01:14 -07001132
Yinghai Luf361a452008-07-10 20:38:26 -07001133 start_pfn = ei->addr >> PAGE_SHIFT;
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001134 end_pfn = (ei->addr + ei->size) >> PAGE_SHIFT;
Yinghai Luf361a452008-07-10 20:38:26 -07001135
1136 if (start_pfn >= limit_pfn)
1137 continue;
1138 if (end_pfn > limit_pfn) {
1139 last_pfn = limit_pfn;
1140 break;
1141 }
Yinghai Lu2dc807b2008-07-08 18:56:38 -07001142 if (end_pfn > last_pfn)
1143 last_pfn = end_pfn;
1144 }
Yinghai Luee0c80f2008-06-03 19:34:00 -07001145
1146 if (last_pfn > max_arch_pfn)
1147 last_pfn = max_arch_pfn;
Yinghai Luee0c80f2008-06-03 19:34:00 -07001148
Paul Jackson5dab8ec2008-06-25 05:44:40 -07001149 printk(KERN_INFO "last_pfn = %#lx max_arch_pfn = %#lx\n",
Yinghai Luee0c80f2008-06-03 19:34:00 -07001150 last_pfn, max_arch_pfn);
1151 return last_pfn;
1152}
Yinghai Luf361a452008-07-10 20:38:26 -07001153unsigned long __init e820_end_of_ram_pfn(void)
1154{
1155 return e820_end_pfn(MAX_ARCH_PFN, E820_RAM);
1156}
Yinghai Luee0c80f2008-06-03 19:34:00 -07001157
Yinghai Luf361a452008-07-10 20:38:26 -07001158unsigned long __init e820_end_of_low_ram_pfn(void)
1159{
1160 return e820_end_pfn(1UL<<(32 - PAGE_SHIFT), E820_RAM);
1161}
Yinghai Luee0c80f2008-06-03 19:34:00 -07001162/*
1163 * Finds an active region in the address range from start_pfn to last_pfn and
1164 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
1165 */
1166int __init e820_find_active_region(const struct e820entry *ei,
1167 unsigned long start_pfn,
1168 unsigned long last_pfn,
1169 unsigned long *ei_startpfn,
1170 unsigned long *ei_endpfn)
1171{
1172 u64 align = PAGE_SIZE;
1173
1174 *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT;
1175 *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT;
1176
1177 /* Skip map entries smaller than a page */
1178 if (*ei_startpfn >= *ei_endpfn)
1179 return 0;
1180
1181 /* Skip if map is outside the node */
1182 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
1183 *ei_startpfn >= last_pfn)
1184 return 0;
1185
1186 /* Check for overlaps */
1187 if (*ei_startpfn < start_pfn)
1188 *ei_startpfn = start_pfn;
1189 if (*ei_endpfn > last_pfn)
1190 *ei_endpfn = last_pfn;
1191
Yinghai Luee0c80f2008-06-03 19:34:00 -07001192 return 1;
1193}
1194
1195/* Walk the e820 map and register active regions within a node */
1196void __init e820_register_active_regions(int nid, unsigned long start_pfn,
1197 unsigned long last_pfn)
1198{
1199 unsigned long ei_startpfn;
1200 unsigned long ei_endpfn;
1201 int i;
1202
1203 for (i = 0; i < e820.nr_map; i++)
1204 if (e820_find_active_region(&e820.map[i],
1205 start_pfn, last_pfn,
1206 &ei_startpfn, &ei_endpfn))
1207 add_active_range(nid, ei_startpfn, ei_endpfn);
1208}
1209
1210/*
1211 * Find the hole size (in bytes) in the memory range.
1212 * @start: starting address of the memory range to scan
1213 * @end: ending address of the memory range to scan
1214 */
1215u64 __init e820_hole_size(u64 start, u64 end)
1216{
1217 unsigned long start_pfn = start >> PAGE_SHIFT;
1218 unsigned long last_pfn = end >> PAGE_SHIFT;
1219 unsigned long ei_startpfn, ei_endpfn, ram = 0;
1220 int i;
1221
1222 for (i = 0; i < e820.nr_map; i++) {
1223 if (e820_find_active_region(&e820.map[i],
1224 start_pfn, last_pfn,
1225 &ei_startpfn, &ei_endpfn))
1226 ram += ei_endpfn - ei_startpfn;
1227 }
1228 return end - start - ((u64)ram << PAGE_SHIFT);
1229}
Yinghai Luab4a4652008-06-10 12:55:54 -07001230
1231static void early_panic(char *msg)
1232{
1233 early_printk(msg);
1234 panic(msg);
1235}
1236
Yinghai Lu69a77042008-07-10 04:17:00 -07001237static int userdef __initdata;
1238
Yinghai Luab4a4652008-06-10 12:55:54 -07001239/* "mem=nopentium" disables the 4MB page tables. */
1240static int __init parse_memopt(char *p)
1241{
1242 u64 mem_size;
1243
1244 if (!p)
1245 return -EINVAL;
1246
1247#ifdef CONFIG_X86_32
1248 if (!strcmp(p, "nopentium")) {
1249 setup_clear_cpu_cap(X86_FEATURE_PSE);
1250 return 0;
1251 }
1252#endif
1253
Yinghai Lu69a77042008-07-10 04:17:00 -07001254 userdef = 1;
Yinghai Luab4a4652008-06-10 12:55:54 -07001255 mem_size = memparse(p, &p);
Yinghai Lu69a77042008-07-10 04:17:00 -07001256 e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
Bernhard Walle611dfd72008-06-25 21:39:16 +02001257
Yinghai Luab4a4652008-06-10 12:55:54 -07001258 return 0;
1259}
1260early_param("mem", parse_memopt);
1261
Yinghai Luab4a4652008-06-10 12:55:54 -07001262static int __init parse_memmap_opt(char *p)
1263{
1264 char *oldp;
1265 u64 start_at, mem_size;
1266
Cyrill Gorcunova737abd2008-07-05 15:53:39 +04001267 if (!p)
1268 return -EINVAL;
1269
Prarit Bhargavad6be118a2008-09-09 09:56:08 -04001270 if (!strncmp(p, "exactmap", 8)) {
Yinghai Luab4a4652008-06-10 12:55:54 -07001271#ifdef CONFIG_CRASH_DUMP
1272 /*
1273 * If we are doing a crash dump, we still need to know
1274 * the real mem size before original memory map is
1275 * reset.
1276 */
Yinghai Luf361a452008-07-10 20:38:26 -07001277 saved_max_pfn = e820_end_of_ram_pfn();
Yinghai Luab4a4652008-06-10 12:55:54 -07001278#endif
1279 e820.nr_map = 0;
1280 userdef = 1;
1281 return 0;
1282 }
1283
1284 oldp = p;
1285 mem_size = memparse(p, &p);
1286 if (p == oldp)
1287 return -EINVAL;
1288
1289 userdef = 1;
1290 if (*p == '@') {
1291 start_at = memparse(p+1, &p);
Yinghai Lud0be6bd2008-06-15 18:58:51 -07001292 e820_add_region(start_at, mem_size, E820_RAM);
Yinghai Luab4a4652008-06-10 12:55:54 -07001293 } else if (*p == '#') {
1294 start_at = memparse(p+1, &p);
Yinghai Lud0be6bd2008-06-15 18:58:51 -07001295 e820_add_region(start_at, mem_size, E820_ACPI);
Yinghai Luab4a4652008-06-10 12:55:54 -07001296 } else if (*p == '$') {
1297 start_at = memparse(p+1, &p);
Yinghai Lud0be6bd2008-06-15 18:58:51 -07001298 e820_add_region(start_at, mem_size, E820_RESERVED);
Yinghai Lu7b479be2008-07-12 22:57:07 -07001299 } else
Yinghai Lu69a77042008-07-10 04:17:00 -07001300 e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
Yinghai Lu7b479be2008-07-12 22:57:07 -07001301
Yinghai Luab4a4652008-06-10 12:55:54 -07001302 return *p == '\0' ? 0 : -EINVAL;
1303}
1304early_param("memmap", parse_memmap_opt);
1305
1306void __init finish_e820_parsing(void)
1307{
1308 if (userdef) {
Jaswinder Singh Rajputba639032009-03-23 02:13:01 +05301309 u32 nr = e820.nr_map;
Yinghai Luab4a4652008-06-10 12:55:54 -07001310
1311 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
1312 early_panic("Invalid user supplied memory map");
1313 e820.nr_map = nr;
1314
1315 printk(KERN_INFO "user-defined physical RAM map:\n");
1316 e820_print_map("user");
1317 }
1318}
Yinghai Lu41c094f2008-06-16 13:03:31 -07001319
Bernhard Walle5dfcf142008-06-27 13:12:55 +02001320static inline const char *e820_type_to_string(int e820_type)
1321{
1322 switch (e820_type) {
1323 case E820_RESERVED_KERN:
1324 case E820_RAM: return "System RAM";
1325 case E820_ACPI: return "ACPI Tables";
1326 case E820_NVS: return "ACPI Non-volatile Storage";
Cihula, Joseph671eef82008-08-20 16:43:07 -07001327 case E820_UNUSABLE: return "Unusable memory";
Bernhard Walle5dfcf142008-06-27 13:12:55 +02001328 default: return "reserved";
1329 }
1330}
1331
Yinghai Lu41c094f2008-06-16 13:03:31 -07001332/*
1333 * Mark e820 reserved areas as busy for the resource manager.
1334 */
Ingo Molnara5444d12008-08-29 08:09:23 +02001335static struct resource __initdata *e820_res;
Yinghai Lu41c094f2008-06-16 13:03:31 -07001336void __init e820_reserve_resources(void)
1337{
1338 int i;
Yinghai Lu58f7c982008-08-28 13:52:25 -07001339 struct resource *res;
Ingo Molnara5444d12008-08-29 08:09:23 +02001340 u64 end;
Yinghai Lu41c094f2008-06-16 13:03:31 -07001341
Jan Beulich3c1596e2009-09-21 17:03:06 -07001342 res = alloc_bootmem(sizeof(struct resource) * e820.nr_map);
Yinghai Lu58f7c982008-08-28 13:52:25 -07001343 e820_res = res;
Yinghai Lu41c094f2008-06-16 13:03:31 -07001344 for (i = 0; i < e820.nr_map; i++) {
Yinghai Lub4df32f2008-06-28 17:49:59 -07001345 end = e820.map[i].addr + e820.map[i].size - 1;
Jeremy Fitzhardinge8308c542008-09-11 01:31:50 -07001346 if (end != (resource_size_t)end) {
Yinghai Lu41c094f2008-06-16 13:03:31 -07001347 res++;
1348 continue;
1349 }
Bernhard Walle5dfcf142008-06-27 13:12:55 +02001350 res->name = e820_type_to_string(e820.map[i].type);
Yinghai Lub4df32f2008-06-28 17:49:59 -07001351 res->start = e820.map[i].addr;
1352 res->end = end;
1353
Linus Torvalds1f987572008-11-01 10:17:22 -07001354 res->flags = IORESOURCE_MEM;
Ingo Molnara5444d12008-08-29 08:09:23 +02001355
1356 /*
1357 * don't register the region that could be conflicted with
1358 * pci device BAR resource and insert them later in
1359 * pcibios_resource_survey()
1360 */
Linus Torvalds1f987572008-11-01 10:17:22 -07001361 if (e820.map[i].type != E820_RESERVED || res->start < (1ULL<<20)) {
1362 res->flags |= IORESOURCE_BUSY;
Yinghai Lu58f7c982008-08-28 13:52:25 -07001363 insert_resource(&iomem_resource, res);
Linus Torvalds1f987572008-11-01 10:17:22 -07001364 }
Yinghai Lu41c094f2008-06-16 13:03:31 -07001365 res++;
1366 }
Bernhard Walle5dfcf142008-06-27 13:12:55 +02001367
1368 for (i = 0; i < e820_saved.nr_map; i++) {
1369 struct e820entry *entry = &e820_saved.map[i];
1370 firmware_map_add_early(entry->addr,
1371 entry->addr + entry->size - 1,
1372 e820_type_to_string(entry->type));
1373 }
Yinghai Lu41c094f2008-06-16 13:03:31 -07001374}
1375
Linus Torvalds45fbe3e2009-05-06 08:06:44 -07001376/* How much should we pad RAM ending depending on where it is? */
1377static unsigned long ram_alignment(resource_size_t pos)
1378{
1379 unsigned long mb = pos >> 20;
1380
1381 /* To 64kB in the first megabyte */
1382 if (!mb)
1383 return 64*1024;
1384
1385 /* To 1MB in the first 16MB */
1386 if (mb < 16)
1387 return 1024*1024;
1388
Yinghai Lu15b812f2009-10-11 14:17:16 -07001389 /* To 64MB for anything above that */
1390 return 64*1024*1024;
Linus Torvalds45fbe3e2009-05-06 08:06:44 -07001391}
1392
Yinghai Lu7c5371c2009-07-01 12:32:18 -07001393#define MAX_RESOURCE_SIZE ((resource_size_t)-1)
1394
Yinghai Lu58f7c982008-08-28 13:52:25 -07001395void __init e820_reserve_resources_late(void)
1396{
1397 int i;
1398 struct resource *res;
1399
1400 res = e820_res;
1401 for (i = 0; i < e820.nr_map; i++) {
Ingo Molnara5444d12008-08-29 08:09:23 +02001402 if (!res->parent && res->end)
Linus Torvalds1f987572008-11-01 10:17:22 -07001403 insert_resource_expand_to_fit(&iomem_resource, res);
Yinghai Lu58f7c982008-08-28 13:52:25 -07001404 res++;
1405 }
Linus Torvalds45fbe3e2009-05-06 08:06:44 -07001406
1407 /*
1408 * Try to bump up RAM regions to reasonable boundaries to
1409 * avoid stolen RAM:
1410 */
1411 for (i = 0; i < e820.nr_map; i++) {
Yinghai Lu7c5371c2009-07-01 12:32:18 -07001412 struct e820entry *entry = &e820.map[i];
1413 u64 start, end;
Linus Torvalds45fbe3e2009-05-06 08:06:44 -07001414
1415 if (entry->type != E820_RAM)
1416 continue;
1417 start = entry->addr + entry->size;
Yinghai Lu7c5371c2009-07-01 12:32:18 -07001418 end = round_up(start, ram_alignment(start)) - 1;
1419 if (end > MAX_RESOURCE_SIZE)
1420 end = MAX_RESOURCE_SIZE;
1421 if (start >= end)
Linus Torvalds45fbe3e2009-05-06 08:06:44 -07001422 continue;
Yinghai Lu7c5371c2009-07-01 12:32:18 -07001423 reserve_region_with_split(&iomem_resource, start, end,
1424 "RAM buffer");
Linus Torvalds45fbe3e2009-05-06 08:06:44 -07001425 }
Yinghai Lu58f7c982008-08-28 13:52:25 -07001426}
1427
Yinghai Lu95a71a42008-06-18 17:27:08 -07001428char *__init default_machine_specific_memory_setup(void)
Yinghai Lu064d25f2008-06-16 19:58:28 -07001429{
1430 char *who = "BIOS-e820";
Jaswinder Singh Rajputba639032009-03-23 02:13:01 +05301431 u32 new_nr;
Yinghai Lu064d25f2008-06-16 19:58:28 -07001432 /*
1433 * Try to copy the BIOS-supplied E820-map.
1434 *
1435 * Otherwise fake a memory map; one section from 0k->640k,
1436 * the next section from 1mb->appropriate_mem_k
1437 */
1438 new_nr = boot_params.e820_entries;
1439 sanitize_e820_map(boot_params.e820_map,
1440 ARRAY_SIZE(boot_params.e820_map),
1441 &new_nr);
1442 boot_params.e820_entries = new_nr;
Yinghai Ludc8e8122008-07-01 20:02:16 -07001443 if (append_e820_map(boot_params.e820_map, boot_params.e820_entries)
1444 < 0) {
Yinghai Lu95a71a42008-06-18 17:27:08 -07001445 u64 mem_size;
Yinghai Lu41c094f2008-06-16 13:03:31 -07001446
Yinghai Lu064d25f2008-06-16 19:58:28 -07001447 /* compare results from other methods and take the greater */
1448 if (boot_params.alt_mem_k
1449 < boot_params.screen_info.ext_mem_k) {
1450 mem_size = boot_params.screen_info.ext_mem_k;
1451 who = "BIOS-88";
1452 } else {
1453 mem_size = boot_params.alt_mem_k;
1454 who = "BIOS-e801";
1455 }
1456
1457 e820.nr_map = 0;
1458 e820_add_region(0, LOWMEMSIZE(), E820_RAM);
1459 e820_add_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
Yinghai Lu064d25f2008-06-16 19:58:28 -07001460 }
1461
1462 /* In case someone cares... */
1463 return who;
1464}
1465
Yinghai Lu064d25f2008-06-16 19:58:28 -07001466void __init setup_memory_map(void)
1467{
Yinghai Lu0be15522008-07-03 11:35:37 -07001468 char *who;
1469
Thomas Gleixner6b18ae32009-08-20 10:19:54 +02001470 who = x86_init.resources.memory_setup();
Yinghai Lu0be15522008-07-03 11:35:37 -07001471 memcpy(&e820_saved, &e820, sizeof(struct e820map));
Yinghai Lu064d25f2008-06-16 19:58:28 -07001472 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
Yinghai Lu0be15522008-07-03 11:35:37 -07001473 e820_print_map(who);
Yinghai Lu064d25f2008-06-16 19:58:28 -07001474}