Commit | Line | Data |
---|---|---|
2f36fa13 | 1 | /* |
1da177e4 LT |
2 | * Handle the memory map. |
3 | * The functions here do the job until bootmem takes over. | |
8059b2a2 VP |
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 | * | |
1da177e4 | 10 | */ |
1da177e4 LT |
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> | |
5f5609df | 17 | #include <linux/kexec.h> |
b9491ac8 | 18 | #include <linux/module.h> |
e8eff5ac | 19 | #include <linux/mm.h> |
74dfd666 RW |
20 | #include <linux/suspend.h> |
21 | #include <linux/pfn.h> | |
b9491ac8 | 22 | |
1a91023a | 23 | #include <asm/pgtable.h> |
1da177e4 LT |
24 | #include <asm/page.h> |
25 | #include <asm/e820.h> | |
26 | #include <asm/proto.h> | |
30c82645 | 27 | #include <asm/setup.h> |
2bc0414e | 28 | #include <asm/sections.h> |
718fc13b | 29 | #include <asm/kdebug.h> |
1da177e4 | 30 | |
b92e9fac | 31 | struct e820map e820; |
3bd4d18c | 32 | |
2f36fa13 | 33 | /* |
1da177e4 LT |
34 | * PFN of last memory page. |
35 | */ | |
2f36fa13 | 36 | unsigned long end_pfn; |
1da177e4 | 37 | |
2f36fa13 | 38 | /* |
1da177e4 LT |
39 | * end_pfn only includes RAM, while end_pfn_map includes all e820 entries. |
40 | * The direct mapping extends to end_pfn_map, so that we can directly access | |
41 | * apertures, ACPI and other tables without having to play with fixmaps. | |
2f36fa13 TG |
42 | */ |
43 | unsigned long end_pfn_map; | |
1da177e4 | 44 | |
2f36fa13 | 45 | /* |
1da177e4 LT |
46 | * Last pfn which the user wants to use. |
47 | */ | |
caff0710 | 48 | static unsigned long __initdata end_user_pfn = MAXMEM>>PAGE_SHIFT; |
1da177e4 | 49 | |
75175278 AK |
50 | /* |
51 | * Early reserved memory areas. | |
52 | */ | |
53 | #define MAX_EARLY_RES 20 | |
30c82645 | 54 | |
75175278 AK |
55 | struct early_res { |
56 | unsigned long start, end; | |
57 | }; | |
58 | static struct early_res early_res[MAX_EARLY_RES] __initdata = { | |
59 | { 0, PAGE_SIZE }, /* BIOS data page */ | |
60 | #ifdef CONFIG_SMP | |
61 | { SMP_TRAMPOLINE_BASE, SMP_TRAMPOLINE_BASE + 2*PAGE_SIZE }, | |
1da177e4 | 62 | #endif |
75175278 AK |
63 | {} |
64 | }; | |
65 | ||
66 | void __init reserve_early(unsigned long start, unsigned long end) | |
67 | { | |
68 | int i; | |
69 | struct early_res *r; | |
70 | for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) { | |
71 | r = &early_res[i]; | |
72 | if (end > r->start && start < r->end) | |
73 | panic("Duplicated early reservation %lx-%lx\n", | |
74 | start, end); | |
1da177e4 | 75 | } |
75175278 AK |
76 | if (i >= MAX_EARLY_RES) |
77 | panic("Too many early reservations"); | |
78 | r = &early_res[i]; | |
79 | r->start = start; | |
80 | r->end = end; | |
81 | } | |
ac71d12c | 82 | |
75175278 AK |
83 | void __init early_res_to_bootmem(void) |
84 | { | |
85 | int i; | |
86 | for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) { | |
87 | struct early_res *r = &early_res[i]; | |
88 | reserve_bootmem_generic(r->start, r->end - r->start); | |
ac71d12c | 89 | } |
75175278 | 90 | } |
ac71d12c | 91 | |
75175278 AK |
92 | /* Check for already reserved areas */ |
93 | static inline int bad_addr(unsigned long *addrp, unsigned long size) | |
94 | { | |
95 | int i; | |
96 | unsigned long addr = *addrp, last; | |
97 | int changed = 0; | |
98 | again: | |
99 | last = addr + size; | |
100 | for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) { | |
101 | struct early_res *r = &early_res[i]; | |
102 | if (last >= r->start && addr < r->end) { | |
103 | *addrp = addr = r->end; | |
104 | changed = 1; | |
105 | goto again; | |
106 | } | |
076422d2 | 107 | } |
75175278 | 108 | return changed; |
2f36fa13 | 109 | } |
1da177e4 | 110 | |
95222368 AV |
111 | /* |
112 | * This function checks if any part of the range <start,end> is mapped | |
113 | * with type. | |
114 | */ | |
b92e9fac | 115 | int |
eee5a9fa | 116 | e820_any_mapped(unsigned long start, unsigned long end, unsigned type) |
2f36fa13 | 117 | { |
1da177e4 | 118 | int i; |
2f36fa13 TG |
119 | |
120 | for (i = 0; i < e820.nr_map; i++) { | |
121 | struct e820entry *ei = &e820.map[i]; | |
122 | ||
123 | if (type && ei->type != type) | |
1da177e4 | 124 | continue; |
48c8b113 | 125 | if (ei->addr >= end || ei->addr + ei->size <= start) |
2f36fa13 TG |
126 | continue; |
127 | return 1; | |
128 | } | |
1da177e4 LT |
129 | return 0; |
130 | } | |
b92e9fac | 131 | EXPORT_SYMBOL_GPL(e820_any_mapped); |
1da177e4 | 132 | |
79e453d4 LT |
133 | /* |
134 | * This function checks if the entire range <start,end> is mapped with type. | |
135 | * | |
136 | * Note: this function only works correct if the e820 table is sorted and | |
137 | * not-overlapping, which is the case | |
138 | */ | |
2f36fa13 TG |
139 | int __init e820_all_mapped(unsigned long start, unsigned long end, |
140 | unsigned type) | |
79e453d4 LT |
141 | { |
142 | int i; | |
2f36fa13 | 143 | |
79e453d4 LT |
144 | for (i = 0; i < e820.nr_map; i++) { |
145 | struct e820entry *ei = &e820.map[i]; | |
2f36fa13 | 146 | |
79e453d4 LT |
147 | if (type && ei->type != type) |
148 | continue; | |
149 | /* is the region (part) in overlap with the current region ?*/ | |
150 | if (ei->addr >= end || ei->addr + ei->size <= start) | |
151 | continue; | |
152 | ||
153 | /* if the region is at the beginning of <start,end> we move | |
154 | * start to the end of the region since it's ok until there | |
155 | */ | |
156 | if (ei->addr <= start) | |
157 | start = ei->addr + ei->size; | |
2f36fa13 TG |
158 | /* |
159 | * if start is now at or beyond end, we're done, full | |
160 | * coverage | |
161 | */ | |
79e453d4 | 162 | if (start >= end) |
2f36fa13 | 163 | return 1; |
79e453d4 LT |
164 | } |
165 | return 0; | |
166 | } | |
167 | ||
2f36fa13 TG |
168 | /* |
169 | * Find a free area in a specific range. | |
170 | */ | |
171 | unsigned long __init find_e820_area(unsigned long start, unsigned long end, | |
172 | unsigned size) | |
173 | { | |
174 | int i; | |
175 | ||
176 | for (i = 0; i < e820.nr_map; i++) { | |
177 | struct e820entry *ei = &e820.map[i]; | |
178 | unsigned long addr = ei->addr, last; | |
179 | ||
180 | if (ei->type != E820_RAM) | |
181 | continue; | |
182 | if (addr < start) | |
1da177e4 | 183 | addr = start; |
2f36fa13 TG |
184 | if (addr > ei->addr + ei->size) |
185 | continue; | |
7ca97c61 | 186 | while (bad_addr(&addr, size) && addr+size <= ei->addr+ei->size) |
1da177e4 | 187 | ; |
73bb8919 | 188 | last = PAGE_ALIGN(addr) + size; |
1da177e4 LT |
189 | if (last > ei->addr + ei->size) |
190 | continue; | |
2f36fa13 | 191 | if (last > end) |
1da177e4 | 192 | continue; |
2f36fa13 TG |
193 | return addr; |
194 | } | |
195 | return -1UL; | |
196 | } | |
1da177e4 | 197 | |
1da177e4 LT |
198 | /* |
199 | * Find the highest page frame number we have available | |
200 | */ | |
201 | unsigned long __init e820_end_of_ram(void) | |
202 | { | |
2f36fa13 TG |
203 | unsigned long end_pfn; |
204 | ||
5cb248ab | 205 | end_pfn = find_max_pfn_with_active_regions(); |
2f36fa13 TG |
206 | |
207 | if (end_pfn > end_pfn_map) | |
1da177e4 LT |
208 | end_pfn_map = end_pfn; |
209 | if (end_pfn_map > MAXMEM>>PAGE_SHIFT) | |
210 | end_pfn_map = MAXMEM>>PAGE_SHIFT; | |
211 | if (end_pfn > end_user_pfn) | |
212 | end_pfn = end_user_pfn; | |
2f36fa13 TG |
213 | if (end_pfn > end_pfn_map) |
214 | end_pfn = end_pfn_map; | |
1da177e4 | 215 | |
2f36fa13 TG |
216 | printk(KERN_INFO "end_pfn_map = %lu\n", end_pfn_map); |
217 | return end_pfn; | |
1da177e4 LT |
218 | } |
219 | ||
485761bd | 220 | /* |
1da177e4 LT |
221 | * Mark e820 reserved areas as busy for the resource manager. |
222 | */ | |
c9cce83d BW |
223 | void __init e820_reserve_resources(struct resource *code_resource, |
224 | struct resource *data_resource, struct resource *bss_resource) | |
1da177e4 LT |
225 | { |
226 | int i; | |
227 | for (i = 0; i < e820.nr_map; i++) { | |
228 | struct resource *res; | |
1da177e4 LT |
229 | res = alloc_bootmem_low(sizeof(struct resource)); |
230 | switch (e820.map[i].type) { | |
231 | case E820_RAM: res->name = "System RAM"; break; | |
232 | case E820_ACPI: res->name = "ACPI Tables"; break; | |
233 | case E820_NVS: res->name = "ACPI Non-volatile Storage"; break; | |
234 | default: res->name = "reserved"; | |
235 | } | |
236 | res->start = e820.map[i].addr; | |
237 | res->end = res->start + e820.map[i].size - 1; | |
238 | res->flags = IORESOURCE_MEM | IORESOURCE_BUSY; | |
239 | request_resource(&iomem_resource, res); | |
240 | if (e820.map[i].type == E820_RAM) { | |
241 | /* | |
2f36fa13 TG |
242 | * We don't know which RAM region contains kernel data, |
243 | * so we try it repeatedly and let the resource manager | |
244 | * test it. | |
1da177e4 | 245 | */ |
c9cce83d BW |
246 | request_resource(res, code_resource); |
247 | request_resource(res, data_resource); | |
248 | request_resource(res, bss_resource); | |
5f5609df | 249 | #ifdef CONFIG_KEXEC |
5c3391f9 BW |
250 | if (crashk_res.start != crashk_res.end) |
251 | request_resource(res, &crashk_res); | |
5f5609df | 252 | #endif |
1da177e4 LT |
253 | } |
254 | } | |
255 | } | |
256 | ||
e8eff5ac RW |
257 | /* |
258 | * Find the ranges of physical addresses that do not correspond to | |
259 | * e820 RAM areas and mark the corresponding pages as nosave for software | |
260 | * suspend and suspend to RAM. | |
261 | * | |
262 | * This function requires the e820 map to be sorted and without any | |
263 | * overlapping entries and assumes the first e820 area to be RAM. | |
264 | */ | |
265 | void __init e820_mark_nosave_regions(void) | |
266 | { | |
267 | int i; | |
268 | unsigned long paddr; | |
269 | ||
270 | paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE); | |
271 | for (i = 1; i < e820.nr_map; i++) { | |
272 | struct e820entry *ei = &e820.map[i]; | |
273 | ||
274 | if (paddr < ei->addr) | |
74dfd666 RW |
275 | register_nosave_region(PFN_DOWN(paddr), |
276 | PFN_UP(ei->addr)); | |
e8eff5ac RW |
277 | |
278 | paddr = round_down(ei->addr + ei->size, PAGE_SIZE); | |
279 | if (ei->type != E820_RAM) | |
74dfd666 RW |
280 | register_nosave_region(PFN_UP(ei->addr), |
281 | PFN_DOWN(paddr)); | |
e8eff5ac RW |
282 | |
283 | if (paddr >= (end_pfn << PAGE_SHIFT)) | |
284 | break; | |
285 | } | |
286 | } | |
287 | ||
3af044e0 DR |
288 | /* |
289 | * Finds an active region in the address range from start_pfn to end_pfn and | |
290 | * returns its range in ei_startpfn and ei_endpfn for the e820 entry. | |
291 | */ | |
292 | static int __init e820_find_active_region(const struct e820entry *ei, | |
293 | unsigned long start_pfn, | |
294 | unsigned long end_pfn, | |
295 | unsigned long *ei_startpfn, | |
296 | unsigned long *ei_endpfn) | |
297 | { | |
298 | *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT; | |
299 | *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT; | |
300 | ||
301 | /* Skip map entries smaller than a page */ | |
302 | if (*ei_startpfn >= *ei_endpfn) | |
303 | return 0; | |
304 | ||
305 | /* Check if end_pfn_map should be updated */ | |
306 | if (ei->type != E820_RAM && *ei_endpfn > end_pfn_map) | |
307 | end_pfn_map = *ei_endpfn; | |
308 | ||
309 | /* Skip if map is outside the node */ | |
310 | if (ei->type != E820_RAM || *ei_endpfn <= start_pfn || | |
311 | *ei_startpfn >= end_pfn) | |
312 | return 0; | |
313 | ||
314 | /* Check for overlaps */ | |
315 | if (*ei_startpfn < start_pfn) | |
316 | *ei_startpfn = start_pfn; | |
317 | if (*ei_endpfn > end_pfn) | |
318 | *ei_endpfn = end_pfn; | |
319 | ||
320 | /* Obey end_user_pfn to save on memmap */ | |
321 | if (*ei_startpfn >= end_user_pfn) | |
322 | return 0; | |
323 | if (*ei_endpfn > end_user_pfn) | |
324 | *ei_endpfn = end_user_pfn; | |
325 | ||
326 | return 1; | |
327 | } | |
328 | ||
5cb248ab MG |
329 | /* Walk the e820 map and register active regions within a node */ |
330 | void __init | |
331 | e820_register_active_regions(int nid, unsigned long start_pfn, | |
332 | unsigned long end_pfn) | |
333 | { | |
3af044e0 DR |
334 | unsigned long ei_startpfn; |
335 | unsigned long ei_endpfn; | |
5cb248ab | 336 | int i; |
5cb248ab | 337 | |
3af044e0 DR |
338 | for (i = 0; i < e820.nr_map; i++) |
339 | if (e820_find_active_region(&e820.map[i], | |
340 | start_pfn, end_pfn, | |
341 | &ei_startpfn, &ei_endpfn)) | |
342 | add_active_range(nid, ei_startpfn, ei_endpfn); | |
5cb248ab MG |
343 | } |
344 | ||
2f36fa13 | 345 | /* |
1da177e4 | 346 | * Add a memory region to the kernel e820 map. |
2f36fa13 | 347 | */ |
1da177e4 LT |
348 | void __init add_memory_region(unsigned long start, unsigned long size, int type) |
349 | { | |
350 | int x = e820.nr_map; | |
351 | ||
352 | if (x == E820MAX) { | |
353 | printk(KERN_ERR "Ooops! Too many entries in the memory map!\n"); | |
354 | return; | |
355 | } | |
356 | ||
357 | e820.map[x].addr = start; | |
358 | e820.map[x].size = size; | |
359 | e820.map[x].type = type; | |
360 | e820.nr_map++; | |
361 | } | |
362 | ||
a7e96629 DR |
363 | /* |
364 | * Find the hole size (in bytes) in the memory range. | |
365 | * @start: starting address of the memory range to scan | |
366 | * @end: ending address of the memory range to scan | |
367 | */ | |
368 | unsigned long __init e820_hole_size(unsigned long start, unsigned long end) | |
369 | { | |
370 | unsigned long start_pfn = start >> PAGE_SHIFT; | |
371 | unsigned long end_pfn = end >> PAGE_SHIFT; | |
2f36fa13 | 372 | unsigned long ei_startpfn, ei_endpfn, ram = 0; |
a7e96629 DR |
373 | int i; |
374 | ||
375 | for (i = 0; i < e820.nr_map; i++) { | |
376 | if (e820_find_active_region(&e820.map[i], | |
377 | start_pfn, end_pfn, | |
378 | &ei_startpfn, &ei_endpfn)) | |
379 | ram += ei_endpfn - ei_startpfn; | |
380 | } | |
381 | return end - start - (ram << PAGE_SHIFT); | |
382 | } | |
383 | ||
013d23e1 | 384 | static void __init e820_print_map(char *who) |
1da177e4 LT |
385 | { |
386 | int i; | |
387 | ||
388 | for (i = 0; i < e820.nr_map; i++) { | |
5a3ece79 | 389 | printk(KERN_INFO " %s: %016Lx - %016Lx ", who, |
2f36fa13 TG |
390 | (unsigned long long) e820.map[i].addr, |
391 | (unsigned long long) | |
392 | (e820.map[i].addr + e820.map[i].size)); | |
1da177e4 | 393 | switch (e820.map[i].type) { |
2f36fa13 TG |
394 | case E820_RAM: |
395 | printk(KERN_CONT "(usable)\n"); | |
396 | break; | |
1da177e4 | 397 | case E820_RESERVED: |
2f36fa13 TG |
398 | printk(KERN_CONT "(reserved)\n"); |
399 | break; | |
1da177e4 | 400 | case E820_ACPI: |
2f36fa13 TG |
401 | printk(KERN_CONT "(ACPI data)\n"); |
402 | break; | |
1da177e4 | 403 | case E820_NVS: |
2f36fa13 TG |
404 | printk(KERN_CONT "(ACPI NVS)\n"); |
405 | break; | |
406 | default: | |
407 | printk(KERN_CONT "type %u\n", e820.map[i].type); | |
408 | break; | |
1da177e4 LT |
409 | } |
410 | } | |
411 | } | |
412 | ||
413 | /* | |
414 | * Sanitize the BIOS e820 map. | |
415 | * | |
2f36fa13 | 416 | * Some e820 responses include overlapping entries. The following |
1da177e4 LT |
417 | * replaces the original e820 map with a new one, removing overlaps. |
418 | * | |
419 | */ | |
2f36fa13 | 420 | static int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map) |
1da177e4 LT |
421 | { |
422 | struct change_member { | |
423 | struct e820entry *pbios; /* pointer to original bios entry */ | |
424 | unsigned long long addr; /* address for this change point */ | |
425 | }; | |
426 | static struct change_member change_point_list[2*E820MAX] __initdata; | |
427 | static struct change_member *change_point[2*E820MAX] __initdata; | |
428 | static struct e820entry *overlap_list[E820MAX] __initdata; | |
429 | static struct e820entry new_bios[E820MAX] __initdata; | |
430 | struct change_member *change_tmp; | |
431 | unsigned long current_type, last_type; | |
432 | unsigned long long last_addr; | |
433 | int chgidx, still_changing; | |
434 | int overlap_entries; | |
435 | int new_bios_entry; | |
8059b2a2 | 436 | int old_nr, new_nr, chg_nr; |
1da177e4 LT |
437 | int i; |
438 | ||
439 | /* | |
2f36fa13 TG |
440 | Visually we're performing the following |
441 | (1,2,3,4 = memory types)... | |
1da177e4 LT |
442 | |
443 | Sample memory map (w/overlaps): | |
444 | ____22__________________ | |
445 | ______________________4_ | |
446 | ____1111________________ | |
447 | _44_____________________ | |
448 | 11111111________________ | |
449 | ____________________33__ | |
450 | ___________44___________ | |
451 | __________33333_________ | |
452 | ______________22________ | |
453 | ___________________2222_ | |
454 | _________111111111______ | |
455 | _____________________11_ | |
456 | _________________4______ | |
457 | ||
458 | Sanitized equivalent (no overlap): | |
459 | 1_______________________ | |
460 | _44_____________________ | |
461 | ___1____________________ | |
462 | ____22__________________ | |
463 | ______11________________ | |
464 | _________1______________ | |
465 | __________3_____________ | |
466 | ___________44___________ | |
467 | _____________33_________ | |
468 | _______________2________ | |
469 | ________________1_______ | |
470 | _________________4______ | |
471 | ___________________2____ | |
472 | ____________________33__ | |
473 | ______________________4_ | |
474 | */ | |
475 | ||
476 | /* if there's only one memory region, don't bother */ | |
477 | if (*pnr_map < 2) | |
478 | return -1; | |
479 | ||
480 | old_nr = *pnr_map; | |
481 | ||
482 | /* bail out if we find any unreasonable addresses in bios map */ | |
2f36fa13 | 483 | for (i = 0; i < old_nr; i++) |
1da177e4 LT |
484 | if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) |
485 | return -1; | |
486 | ||
487 | /* create pointers for initial change-point information (for sorting) */ | |
2f36fa13 | 488 | for (i = 0; i < 2 * old_nr; i++) |
1da177e4 LT |
489 | change_point[i] = &change_point_list[i]; |
490 | ||
8059b2a2 VP |
491 | /* record all known change-points (starting and ending addresses), |
492 | omitting those that are for empty memory regions */ | |
1da177e4 | 493 | chgidx = 0; |
2f36fa13 | 494 | for (i = 0; i < old_nr; i++) { |
8059b2a2 VP |
495 | if (biosmap[i].size != 0) { |
496 | change_point[chgidx]->addr = biosmap[i].addr; | |
497 | change_point[chgidx++]->pbios = &biosmap[i]; | |
2f36fa13 TG |
498 | change_point[chgidx]->addr = biosmap[i].addr + |
499 | biosmap[i].size; | |
8059b2a2 VP |
500 | change_point[chgidx++]->pbios = &biosmap[i]; |
501 | } | |
1da177e4 | 502 | } |
8059b2a2 | 503 | chg_nr = chgidx; |
1da177e4 LT |
504 | |
505 | /* sort change-point list by memory addresses (low -> high) */ | |
506 | still_changing = 1; | |
507 | while (still_changing) { | |
508 | still_changing = 0; | |
2f36fa13 TG |
509 | for (i = 1; i < chg_nr; i++) { |
510 | unsigned long long curaddr, lastaddr; | |
511 | unsigned long long curpbaddr, lastpbaddr; | |
512 | ||
513 | curaddr = change_point[i]->addr; | |
514 | lastaddr = change_point[i - 1]->addr; | |
515 | curpbaddr = change_point[i]->pbios->addr; | |
516 | lastpbaddr = change_point[i - 1]->pbios->addr; | |
517 | ||
518 | /* | |
519 | * swap entries, when: | |
520 | * | |
521 | * curaddr > lastaddr or | |
522 | * curaddr == lastaddr and curaddr == curpbaddr and | |
523 | * lastaddr != lastpbaddr | |
524 | */ | |
525 | if (curaddr < lastaddr || | |
526 | (curaddr == lastaddr && curaddr == curpbaddr && | |
527 | lastaddr != lastpbaddr)) { | |
1da177e4 LT |
528 | change_tmp = change_point[i]; |
529 | change_point[i] = change_point[i-1]; | |
530 | change_point[i-1] = change_tmp; | |
2f36fa13 | 531 | still_changing = 1; |
1da177e4 LT |
532 | } |
533 | } | |
534 | } | |
535 | ||
536 | /* create a new bios memory map, removing overlaps */ | |
2f36fa13 TG |
537 | overlap_entries = 0; /* number of entries in the overlap table */ |
538 | new_bios_entry = 0; /* index for creating new bios map entries */ | |
1da177e4 LT |
539 | last_type = 0; /* start with undefined memory type */ |
540 | last_addr = 0; /* start with 0 as last starting address */ | |
2f36fa13 | 541 | |
1da177e4 | 542 | /* loop through change-points, determining affect on the new bios map */ |
2f36fa13 | 543 | for (chgidx = 0; chgidx < chg_nr; chgidx++) { |
1da177e4 | 544 | /* keep track of all overlapping bios entries */ |
2f36fa13 TG |
545 | if (change_point[chgidx]->addr == |
546 | change_point[chgidx]->pbios->addr) { | |
547 | /* | |
548 | * add map entry to overlap list (> 1 entry | |
549 | * implies an overlap) | |
550 | */ | |
551 | overlap_list[overlap_entries++] = | |
552 | change_point[chgidx]->pbios; | |
553 | } else { | |
554 | /* | |
555 | * remove entry from list (order independent, | |
556 | * so swap with last) | |
557 | */ | |
558 | for (i = 0; i < overlap_entries; i++) { | |
559 | if (overlap_list[i] == | |
560 | change_point[chgidx]->pbios) | |
561 | overlap_list[i] = | |
562 | overlap_list[overlap_entries-1]; | |
1da177e4 LT |
563 | } |
564 | overlap_entries--; | |
565 | } | |
2f36fa13 TG |
566 | /* |
567 | * if there are overlapping entries, decide which | |
568 | * "type" to use (larger value takes precedence -- | |
569 | * 1=usable, 2,3,4,4+=unusable) | |
570 | */ | |
1da177e4 | 571 | current_type = 0; |
2f36fa13 | 572 | for (i = 0; i < overlap_entries; i++) |
1da177e4 LT |
573 | if (overlap_list[i]->type > current_type) |
574 | current_type = overlap_list[i]->type; | |
2f36fa13 TG |
575 | /* |
576 | * continue building up new bios map based on this | |
577 | * information | |
578 | */ | |
1da177e4 LT |
579 | if (current_type != last_type) { |
580 | if (last_type != 0) { | |
581 | new_bios[new_bios_entry].size = | |
582 | change_point[chgidx]->addr - last_addr; | |
2f36fa13 TG |
583 | /* |
584 | * move forward only if the new size | |
585 | * was non-zero | |
586 | */ | |
1da177e4 | 587 | if (new_bios[new_bios_entry].size != 0) |
2f36fa13 TG |
588 | /* |
589 | * no more space left for new | |
590 | * bios entries ? | |
591 | */ | |
1da177e4 | 592 | if (++new_bios_entry >= E820MAX) |
2f36fa13 | 593 | break; |
1da177e4 LT |
594 | } |
595 | if (current_type != 0) { | |
2f36fa13 TG |
596 | new_bios[new_bios_entry].addr = |
597 | change_point[chgidx]->addr; | |
1da177e4 | 598 | new_bios[new_bios_entry].type = current_type; |
2f36fa13 | 599 | last_addr = change_point[chgidx]->addr; |
1da177e4 LT |
600 | } |
601 | last_type = current_type; | |
602 | } | |
603 | } | |
2f36fa13 TG |
604 | /* retain count for new bios entries */ |
605 | new_nr = new_bios_entry; | |
1da177e4 LT |
606 | |
607 | /* copy new bios mapping into original location */ | |
2f36fa13 | 608 | memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry)); |
1da177e4 LT |
609 | *pnr_map = new_nr; |
610 | ||
611 | return 0; | |
612 | } | |
613 | ||
614 | /* | |
615 | * Copy the BIOS e820 map into a safe place. | |
616 | * | |
617 | * Sanity-check it while we're at it.. | |
618 | * | |
619 | * If we're lucky and live on a modern system, the setup code | |
620 | * will have given us a memory map that we can use to properly | |
621 | * set up memory. If we aren't, we'll fake a memory map. | |
1da177e4 | 622 | */ |
2f36fa13 | 623 | static int __init copy_e820_map(struct e820entry *biosmap, int nr_map) |
1da177e4 LT |
624 | { |
625 | /* Only one memory region (or negative)? Ignore it */ | |
626 | if (nr_map < 2) | |
627 | return -1; | |
628 | ||
629 | do { | |
630 | unsigned long start = biosmap->addr; | |
631 | unsigned long size = biosmap->size; | |
632 | unsigned long end = start + size; | |
633 | unsigned long type = biosmap->type; | |
634 | ||
635 | /* Overflow in 64 bits? Ignore the memory map. */ | |
636 | if (start > end) | |
637 | return -1; | |
638 | ||
1da177e4 | 639 | add_memory_region(start, size, type); |
2f36fa13 | 640 | } while (biosmap++, --nr_map); |
1da177e4 LT |
641 | return 0; |
642 | } | |
643 | ||
013d23e1 | 644 | static void early_panic(char *msg) |
1da177e4 | 645 | { |
8380aabb AK |
646 | early_printk(msg); |
647 | panic(msg); | |
648 | } | |
1da177e4 | 649 | |
746ef0cd GOC |
650 | /* We're not void only for x86 32-bit compat */ |
651 | char * __init machine_specific_memory_setup(void) | |
8380aabb | 652 | { |
746ef0cd | 653 | char *who = "BIOS-e820"; |
1da177e4 LT |
654 | /* |
655 | * Try to copy the BIOS-supplied E820-map. | |
656 | * | |
657 | * Otherwise fake a memory map; one section from 0k->640k, | |
658 | * the next section from 1mb->appropriate_mem_k | |
659 | */ | |
30c82645 PA |
660 | sanitize_e820_map(boot_params.e820_map, &boot_params.e820_entries); |
661 | if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0) | |
8380aabb | 662 | early_panic("Cannot find a valid memory map"); |
1da177e4 | 663 | printk(KERN_INFO "BIOS-provided physical RAM map:\n"); |
746ef0cd GOC |
664 | e820_print_map(who); |
665 | ||
666 | /* In case someone cares... */ | |
667 | return who; | |
1da177e4 LT |
668 | } |
669 | ||
2c8c0e6b AK |
670 | static int __init parse_memopt(char *p) |
671 | { | |
672 | if (!p) | |
673 | return -EINVAL; | |
674 | end_user_pfn = memparse(p, &p); | |
2f36fa13 | 675 | end_user_pfn >>= PAGE_SHIFT; |
2c8c0e6b | 676 | return 0; |
2f36fa13 | 677 | } |
2c8c0e6b | 678 | early_param("mem", parse_memopt); |
1da177e4 | 679 | |
2c8c0e6b | 680 | static int userdef __initdata; |
1da177e4 | 681 | |
2c8c0e6b | 682 | static int __init parse_memmap_opt(char *p) |
69cda7b1 | 683 | { |
2c8c0e6b | 684 | char *oldp; |
69cda7b1 AM |
685 | unsigned long long start_at, mem_size; |
686 | ||
2c8c0e6b AK |
687 | if (!strcmp(p, "exactmap")) { |
688 | #ifdef CONFIG_CRASH_DUMP | |
2f36fa13 TG |
689 | /* |
690 | * If we are doing a crash dump, we still need to know | |
691 | * the real mem size before original memory map is | |
2c8c0e6b AK |
692 | * reset. |
693 | */ | |
15803a43 | 694 | e820_register_active_regions(0, 0, -1UL); |
2c8c0e6b | 695 | saved_max_pfn = e820_end_of_ram(); |
15803a43 | 696 | remove_all_active_ranges(); |
2c8c0e6b AK |
697 | #endif |
698 | end_pfn_map = 0; | |
699 | e820.nr_map = 0; | |
700 | userdef = 1; | |
701 | return 0; | |
702 | } | |
703 | ||
704 | oldp = p; | |
705 | mem_size = memparse(p, &p); | |
706 | if (p == oldp) | |
707 | return -EINVAL; | |
b3ca74a2 VB |
708 | |
709 | userdef = 1; | |
69cda7b1 | 710 | if (*p == '@') { |
2c8c0e6b | 711 | start_at = memparse(p+1, &p); |
69cda7b1 AM |
712 | add_memory_region(start_at, mem_size, E820_RAM); |
713 | } else if (*p == '#') { | |
2c8c0e6b | 714 | start_at = memparse(p+1, &p); |
69cda7b1 AM |
715 | add_memory_region(start_at, mem_size, E820_ACPI); |
716 | } else if (*p == '$') { | |
2c8c0e6b | 717 | start_at = memparse(p+1, &p); |
69cda7b1 AM |
718 | add_memory_region(start_at, mem_size, E820_RESERVED); |
719 | } else { | |
720 | end_user_pfn = (mem_size >> PAGE_SHIFT); | |
721 | } | |
2c8c0e6b AK |
722 | return *p == '\0' ? 0 : -EINVAL; |
723 | } | |
724 | early_param("memmap", parse_memmap_opt); | |
725 | ||
43999d9e | 726 | void __init finish_e820_parsing(void) |
2c8c0e6b AK |
727 | { |
728 | if (userdef) { | |
b3ca74a2 VB |
729 | char nr = e820.nr_map; |
730 | ||
731 | if (sanitize_e820_map(e820.map, &nr) < 0) | |
732 | early_panic("Invalid user supplied memory map"); | |
733 | e820.nr_map = nr; | |
734 | ||
2c8c0e6b AK |
735 | printk(KERN_INFO "user-defined physical RAM map:\n"); |
736 | e820_print_map("user"); | |
737 | } | |
69cda7b1 AM |
738 | } |
739 | ||
aaf23042 YL |
740 | void __init update_e820(void) |
741 | { | |
742 | u8 nr_map; | |
743 | ||
744 | nr_map = e820.nr_map; | |
745 | if (sanitize_e820_map(e820.map, &nr_map)) | |
746 | return; | |
747 | e820.nr_map = nr_map; | |
748 | printk(KERN_INFO "modified physical RAM map:\n"); | |
749 | e820_print_map("modified"); | |
750 | } | |
751 | ||
a1e97782 | 752 | unsigned long pci_mem_start = 0xaeedbabe; |
2ee60e17 | 753 | EXPORT_SYMBOL(pci_mem_start); |
a1e97782 AK |
754 | |
755 | /* | |
756 | * Search for the biggest gap in the low 32 bits of the e820 | |
757 | * memory space. We pass this space to PCI to assign MMIO resources | |
758 | * for hotplug or unconfigured devices in. | |
759 | * Hopefully the BIOS let enough space left. | |
760 | */ | |
761 | __init void e820_setup_gap(void) | |
762 | { | |
f0eca962 | 763 | unsigned long gapstart, gapsize, round; |
a1e97782 AK |
764 | unsigned long last; |
765 | int i; | |
766 | int found = 0; | |
767 | ||
768 | last = 0x100000000ull; | |
769 | gapstart = 0x10000000; | |
770 | gapsize = 0x400000; | |
771 | i = e820.nr_map; | |
772 | while (--i >= 0) { | |
773 | unsigned long long start = e820.map[i].addr; | |
774 | unsigned long long end = start + e820.map[i].size; | |
775 | ||
776 | /* | |
777 | * Since "last" is at most 4GB, we know we'll | |
778 | * fit in 32 bits if this condition is true | |
779 | */ | |
780 | if (last > end) { | |
781 | unsigned long gap = last - end; | |
782 | ||
783 | if (gap > gapsize) { | |
784 | gapsize = gap; | |
785 | gapstart = end; | |
786 | found = 1; | |
787 | } | |
788 | } | |
789 | if (start < last) | |
790 | last = start; | |
791 | } | |
792 | ||
793 | if (!found) { | |
794 | gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024; | |
2f36fa13 TG |
795 | printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit " |
796 | "address range\n" | |
797 | KERN_ERR "PCI: Unassigned devices with 32bit resource " | |
798 | "registers may break!\n"); | |
a1e97782 AK |
799 | } |
800 | ||
801 | /* | |
f0eca962 DR |
802 | * See how much we want to round up: start off with |
803 | * rounding to the next 1MB area. | |
a1e97782 | 804 | */ |
f0eca962 DR |
805 | round = 0x100000; |
806 | while ((gapsize >> 4) > round) | |
807 | round += round; | |
808 | /* Fun with two's complement */ | |
809 | pci_mem_start = (gapstart + round) & -round; | |
a1e97782 | 810 | |
2f36fa13 TG |
811 | printk(KERN_INFO |
812 | "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n", | |
813 | pci_mem_start, gapstart, gapsize); | |
a1e97782 | 814 | } |
e820482c KA |
815 | |
816 | int __init arch_get_ram_range(int slot, u64 *addr, u64 *size) | |
817 | { | |
818 | int i; | |
819 | ||
820 | if (slot < 0 || slot >= e820.nr_map) | |
821 | return -1; | |
822 | for (i = slot; i < e820.nr_map; i++) { | |
823 | if (e820.map[i].type != E820_RAM) | |
824 | continue; | |
825 | break; | |
826 | } | |
827 | if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT)) | |
828 | return -1; | |
829 | *addr = e820.map[i].addr; | |
830 | *size = min_t(u64, e820.map[i].size + e820.map[i].addr, | |
831 | max_pfn << PAGE_SHIFT) - *addr; | |
832 | return i + 1; | |
833 | } |