Commit | Line | Data |
---|---|---|
b79cd8f1 YL |
1 | /* |
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> | |
21 | ||
22 | #include <asm/pgtable.h> | |
23 | #include <asm/page.h> | |
24 | #include <asm/e820.h> | |
25 | #include <asm/setup.h> | |
26 | ||
27 | struct e820map e820; | |
28 | ||
29 | /* For PCI or other memory-mapped resources */ | |
30 | unsigned long pci_mem_start = 0xaeedbabe; | |
31 | #ifdef CONFIG_PCI | |
32 | EXPORT_SYMBOL(pci_mem_start); | |
33 | #endif | |
34 | ||
35 | /* | |
36 | * This function checks if any part of the range <start,end> is mapped | |
37 | * with type. | |
38 | */ | |
39 | int | |
40 | e820_any_mapped(u64 start, u64 end, unsigned type) | |
41 | { | |
42 | int i; | |
43 | ||
44 | for (i = 0; i < e820.nr_map; i++) { | |
45 | struct e820entry *ei = &e820.map[i]; | |
46 | ||
47 | if (type && ei->type != type) | |
48 | continue; | |
49 | if (ei->addr >= end || ei->addr + ei->size <= start) | |
50 | continue; | |
51 | return 1; | |
52 | } | |
53 | return 0; | |
54 | } | |
55 | EXPORT_SYMBOL_GPL(e820_any_mapped); | |
56 | ||
57 | /* | |
58 | * This function checks if the entire range <start,end> is mapped with type. | |
59 | * | |
60 | * Note: this function only works correct if the e820 table is sorted and | |
61 | * not-overlapping, which is the case | |
62 | */ | |
63 | int __init e820_all_mapped(u64 start, u64 end, unsigned type) | |
64 | { | |
65 | int i; | |
66 | ||
67 | for (i = 0; i < e820.nr_map; i++) { | |
68 | struct e820entry *ei = &e820.map[i]; | |
69 | ||
70 | if (type && ei->type != type) | |
71 | continue; | |
72 | /* is the region (part) in overlap with the current region ?*/ | |
73 | if (ei->addr >= end || ei->addr + ei->size <= start) | |
74 | continue; | |
75 | ||
76 | /* if the region is at the beginning of <start,end> we move | |
77 | * start to the end of the region since it's ok until there | |
78 | */ | |
79 | if (ei->addr <= start) | |
80 | start = ei->addr + ei->size; | |
81 | /* | |
82 | * if start is now at or beyond end, we're done, full | |
83 | * coverage | |
84 | */ | |
85 | if (start >= end) | |
86 | return 1; | |
87 | } | |
88 | return 0; | |
89 | } | |
90 | ||
91 | /* | |
92 | * Add a memory region to the kernel e820 map. | |
93 | */ | |
94 | void __init add_memory_region(u64 start, u64 size, int type) | |
95 | { | |
96 | int x = e820.nr_map; | |
97 | ||
c3965bd1 | 98 | if (x == ARRAY_SIZE(e820.map)) { |
b79cd8f1 YL |
99 | printk(KERN_ERR "Ooops! Too many entries in the memory map!\n"); |
100 | return; | |
101 | } | |
102 | ||
103 | e820.map[x].addr = start; | |
104 | e820.map[x].size = size; | |
105 | e820.map[x].type = type; | |
106 | e820.nr_map++; | |
107 | } | |
108 | ||
109 | void __init e820_print_map(char *who) | |
110 | { | |
111 | int i; | |
112 | ||
113 | for (i = 0; i < e820.nr_map; i++) { | |
114 | printk(KERN_INFO " %s: %016Lx - %016Lx ", who, | |
115 | (unsigned long long) e820.map[i].addr, | |
116 | (unsigned long long) | |
117 | (e820.map[i].addr + e820.map[i].size)); | |
118 | switch (e820.map[i].type) { | |
119 | case E820_RAM: | |
120 | printk(KERN_CONT "(usable)\n"); | |
121 | break; | |
122 | case E820_RESERVED: | |
123 | printk(KERN_CONT "(reserved)\n"); | |
124 | break; | |
125 | case E820_ACPI: | |
126 | printk(KERN_CONT "(ACPI data)\n"); | |
127 | break; | |
128 | case E820_NVS: | |
129 | printk(KERN_CONT "(ACPI NVS)\n"); | |
130 | break; | |
131 | default: | |
132 | printk(KERN_CONT "type %u\n", e820.map[i].type); | |
133 | break; | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
138 | /* | |
139 | * Sanitize the BIOS e820 map. | |
140 | * | |
141 | * Some e820 responses include overlapping entries. The following | |
142 | * replaces the original e820 map with a new one, removing overlaps. | |
143 | * | |
144 | */ | |
c3965bd1 | 145 | int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map, |
6e9bcc79 | 146 | int *pnr_map) |
b79cd8f1 YL |
147 | { |
148 | struct change_member { | |
149 | struct e820entry *pbios; /* pointer to original bios entry */ | |
150 | unsigned long long addr; /* address for this change point */ | |
151 | }; | |
028b7858 PJ |
152 | static struct change_member change_point_list[2*E820_X_MAX] __initdata; |
153 | static struct change_member *change_point[2*E820_X_MAX] __initdata; | |
154 | static struct e820entry *overlap_list[E820_X_MAX] __initdata; | |
155 | static struct e820entry new_bios[E820_X_MAX] __initdata; | |
b79cd8f1 YL |
156 | struct change_member *change_tmp; |
157 | unsigned long current_type, last_type; | |
158 | unsigned long long last_addr; | |
159 | int chgidx, still_changing; | |
160 | int overlap_entries; | |
161 | int new_bios_entry; | |
162 | int old_nr, new_nr, chg_nr; | |
163 | int i; | |
164 | ||
165 | /* | |
166 | Visually we're performing the following | |
167 | (1,2,3,4 = memory types)... | |
168 | ||
169 | Sample memory map (w/overlaps): | |
170 | ____22__________________ | |
171 | ______________________4_ | |
172 | ____1111________________ | |
173 | _44_____________________ | |
174 | 11111111________________ | |
175 | ____________________33__ | |
176 | ___________44___________ | |
177 | __________33333_________ | |
178 | ______________22________ | |
179 | ___________________2222_ | |
180 | _________111111111______ | |
181 | _____________________11_ | |
182 | _________________4______ | |
183 | ||
184 | Sanitized equivalent (no overlap): | |
185 | 1_______________________ | |
186 | _44_____________________ | |
187 | ___1____________________ | |
188 | ____22__________________ | |
189 | ______11________________ | |
190 | _________1______________ | |
191 | __________3_____________ | |
192 | ___________44___________ | |
193 | _____________33_________ | |
194 | _______________2________ | |
195 | ________________1_______ | |
196 | _________________4______ | |
197 | ___________________2____ | |
198 | ____________________33__ | |
199 | ______________________4_ | |
200 | */ | |
201 | ||
202 | /* if there's only one memory region, don't bother */ | |
203 | if (*pnr_map < 2) | |
204 | return -1; | |
205 | ||
206 | old_nr = *pnr_map; | |
6e9bcc79 | 207 | BUG_ON(old_nr > max_nr_map); |
b79cd8f1 YL |
208 | |
209 | /* bail out if we find any unreasonable addresses in bios map */ | |
210 | for (i = 0; i < old_nr; i++) | |
211 | if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) | |
212 | return -1; | |
213 | ||
214 | /* create pointers for initial change-point information (for sorting) */ | |
215 | for (i = 0; i < 2 * old_nr; i++) | |
216 | change_point[i] = &change_point_list[i]; | |
217 | ||
218 | /* record all known change-points (starting and ending addresses), | |
219 | omitting those that are for empty memory regions */ | |
220 | chgidx = 0; | |
221 | for (i = 0; i < old_nr; i++) { | |
222 | if (biosmap[i].size != 0) { | |
223 | change_point[chgidx]->addr = biosmap[i].addr; | |
224 | change_point[chgidx++]->pbios = &biosmap[i]; | |
225 | change_point[chgidx]->addr = biosmap[i].addr + | |
226 | biosmap[i].size; | |
227 | change_point[chgidx++]->pbios = &biosmap[i]; | |
228 | } | |
229 | } | |
230 | chg_nr = chgidx; | |
231 | ||
232 | /* sort change-point list by memory addresses (low -> high) */ | |
233 | still_changing = 1; | |
234 | while (still_changing) { | |
235 | still_changing = 0; | |
236 | for (i = 1; i < chg_nr; i++) { | |
237 | unsigned long long curaddr, lastaddr; | |
238 | unsigned long long curpbaddr, lastpbaddr; | |
239 | ||
240 | curaddr = change_point[i]->addr; | |
241 | lastaddr = change_point[i - 1]->addr; | |
242 | curpbaddr = change_point[i]->pbios->addr; | |
243 | lastpbaddr = change_point[i - 1]->pbios->addr; | |
244 | ||
245 | /* | |
246 | * swap entries, when: | |
247 | * | |
248 | * curaddr > lastaddr or | |
249 | * curaddr == lastaddr and curaddr == curpbaddr and | |
250 | * lastaddr != lastpbaddr | |
251 | */ | |
252 | if (curaddr < lastaddr || | |
253 | (curaddr == lastaddr && curaddr == curpbaddr && | |
254 | lastaddr != lastpbaddr)) { | |
255 | change_tmp = change_point[i]; | |
256 | change_point[i] = change_point[i-1]; | |
257 | change_point[i-1] = change_tmp; | |
258 | still_changing = 1; | |
259 | } | |
260 | } | |
261 | } | |
262 | ||
263 | /* create a new bios memory map, removing overlaps */ | |
264 | overlap_entries = 0; /* number of entries in the overlap table */ | |
265 | new_bios_entry = 0; /* index for creating new bios map entries */ | |
266 | last_type = 0; /* start with undefined memory type */ | |
267 | last_addr = 0; /* start with 0 as last starting address */ | |
268 | ||
269 | /* loop through change-points, determining affect on the new bios map */ | |
270 | for (chgidx = 0; chgidx < chg_nr; chgidx++) { | |
271 | /* keep track of all overlapping bios entries */ | |
272 | if (change_point[chgidx]->addr == | |
273 | change_point[chgidx]->pbios->addr) { | |
274 | /* | |
275 | * add map entry to overlap list (> 1 entry | |
276 | * implies an overlap) | |
277 | */ | |
278 | overlap_list[overlap_entries++] = | |
279 | change_point[chgidx]->pbios; | |
280 | } else { | |
281 | /* | |
282 | * remove entry from list (order independent, | |
283 | * so swap with last) | |
284 | */ | |
285 | for (i = 0; i < overlap_entries; i++) { | |
286 | if (overlap_list[i] == | |
287 | change_point[chgidx]->pbios) | |
288 | overlap_list[i] = | |
289 | overlap_list[overlap_entries-1]; | |
290 | } | |
291 | overlap_entries--; | |
292 | } | |
293 | /* | |
294 | * if there are overlapping entries, decide which | |
295 | * "type" to use (larger value takes precedence -- | |
296 | * 1=usable, 2,3,4,4+=unusable) | |
297 | */ | |
298 | current_type = 0; | |
299 | for (i = 0; i < overlap_entries; i++) | |
300 | if (overlap_list[i]->type > current_type) | |
301 | current_type = overlap_list[i]->type; | |
302 | /* | |
303 | * continue building up new bios map based on this | |
304 | * information | |
305 | */ | |
306 | if (current_type != last_type) { | |
307 | if (last_type != 0) { | |
308 | new_bios[new_bios_entry].size = | |
309 | change_point[chgidx]->addr - last_addr; | |
310 | /* | |
311 | * move forward only if the new size | |
312 | * was non-zero | |
313 | */ | |
314 | if (new_bios[new_bios_entry].size != 0) | |
315 | /* | |
316 | * no more space left for new | |
317 | * bios entries ? | |
318 | */ | |
c3965bd1 | 319 | if (++new_bios_entry >= max_nr_map) |
b79cd8f1 YL |
320 | break; |
321 | } | |
322 | if (current_type != 0) { | |
323 | new_bios[new_bios_entry].addr = | |
324 | change_point[chgidx]->addr; | |
325 | new_bios[new_bios_entry].type = current_type; | |
326 | last_addr = change_point[chgidx]->addr; | |
327 | } | |
328 | last_type = current_type; | |
329 | } | |
330 | } | |
331 | /* retain count for new bios entries */ | |
332 | new_nr = new_bios_entry; | |
333 | ||
334 | /* copy new bios mapping into original location */ | |
335 | memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry)); | |
336 | *pnr_map = new_nr; | |
337 | ||
338 | return 0; | |
339 | } | |
340 | ||
341 | /* | |
342 | * Copy the BIOS e820 map into a safe place. | |
343 | * | |
344 | * Sanity-check it while we're at it.. | |
345 | * | |
346 | * If we're lucky and live on a modern system, the setup code | |
347 | * will have given us a memory map that we can use to properly | |
348 | * set up memory. If we aren't, we'll fake a memory map. | |
349 | */ | |
350 | int __init copy_e820_map(struct e820entry *biosmap, int nr_map) | |
351 | { | |
352 | /* Only one memory region (or negative)? Ignore it */ | |
353 | if (nr_map < 2) | |
354 | return -1; | |
355 | ||
356 | do { | |
357 | u64 start = biosmap->addr; | |
358 | u64 size = biosmap->size; | |
359 | u64 end = start + size; | |
360 | u32 type = biosmap->type; | |
361 | ||
362 | /* Overflow in 64 bits? Ignore the memory map. */ | |
363 | if (start > end) | |
364 | return -1; | |
365 | ||
366 | add_memory_region(start, size, type); | |
367 | } while (biosmap++, --nr_map); | |
368 | return 0; | |
369 | } | |
370 | ||
371 | u64 __init update_memory_range(u64 start, u64 size, unsigned old_type, | |
372 | unsigned new_type) | |
373 | { | |
374 | int i; | |
375 | u64 real_updated_size = 0; | |
376 | ||
377 | BUG_ON(old_type == new_type); | |
378 | ||
379 | for (i = 0; i < e820.nr_map; i++) { | |
380 | struct e820entry *ei = &e820.map[i]; | |
381 | u64 final_start, final_end; | |
382 | if (ei->type != old_type) | |
383 | continue; | |
384 | /* totally covered? */ | |
385 | if (ei->addr >= start && | |
386 | (ei->addr + ei->size) <= (start + size)) { | |
387 | ei->type = new_type; | |
388 | real_updated_size += ei->size; | |
389 | continue; | |
390 | } | |
391 | /* partially covered */ | |
392 | final_start = max(start, ei->addr); | |
393 | final_end = min(start + size, ei->addr + ei->size); | |
394 | if (final_start >= final_end) | |
395 | continue; | |
396 | add_memory_region(final_start, final_end - final_start, | |
397 | new_type); | |
398 | real_updated_size += final_end - final_start; | |
399 | } | |
400 | return real_updated_size; | |
401 | } | |
402 | ||
403 | void __init update_e820(void) | |
404 | { | |
6e9bcc79 | 405 | int nr_map; |
b79cd8f1 YL |
406 | |
407 | nr_map = e820.nr_map; | |
c3965bd1 | 408 | if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map)) |
b79cd8f1 YL |
409 | return; |
410 | e820.nr_map = nr_map; | |
411 | printk(KERN_INFO "modified physical RAM map:\n"); | |
412 | e820_print_map("modified"); | |
413 | } | |
414 | ||
415 | /* | |
416 | * Search for the biggest gap in the low 32 bits of the e820 | |
417 | * memory space. We pass this space to PCI to assign MMIO resources | |
418 | * for hotplug or unconfigured devices in. | |
419 | * Hopefully the BIOS let enough space left. | |
420 | */ | |
421 | __init void e820_setup_gap(void) | |
422 | { | |
423 | unsigned long gapstart, gapsize, round; | |
424 | unsigned long long last; | |
425 | int i; | |
426 | int found = 0; | |
427 | ||
428 | last = 0x100000000ull; | |
429 | gapstart = 0x10000000; | |
430 | gapsize = 0x400000; | |
431 | i = e820.nr_map; | |
432 | while (--i >= 0) { | |
433 | unsigned long long start = e820.map[i].addr; | |
434 | unsigned long long end = start + e820.map[i].size; | |
435 | ||
436 | /* | |
437 | * Since "last" is at most 4GB, we know we'll | |
438 | * fit in 32 bits if this condition is true | |
439 | */ | |
440 | if (last > end) { | |
441 | unsigned long gap = last - end; | |
442 | ||
443 | if (gap > gapsize) { | |
444 | gapsize = gap; | |
445 | gapstart = end; | |
446 | found = 1; | |
447 | } | |
448 | } | |
449 | if (start < last) | |
450 | last = start; | |
451 | } | |
452 | ||
453 | #ifdef CONFIG_X86_64 | |
454 | if (!found) { | |
455 | gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024; | |
456 | printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit " | |
457 | "address range\n" | |
458 | KERN_ERR "PCI: Unassigned devices with 32bit resource " | |
459 | "registers may break!\n"); | |
460 | } | |
461 | #endif | |
462 | ||
463 | /* | |
464 | * See how much we want to round up: start off with | |
465 | * rounding to the next 1MB area. | |
466 | */ | |
467 | round = 0x100000; | |
468 | while ((gapsize >> 4) > round) | |
469 | round += round; | |
470 | /* Fun with two's complement */ | |
471 | pci_mem_start = (gapstart + round) & -round; | |
472 | ||
473 | printk(KERN_INFO | |
474 | "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n", | |
475 | pci_mem_start, gapstart, gapsize); | |
476 | } | |
477 |