2 * Handle the memory map.
3 * The functions here do the job until bootmem takes over.
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>
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>
20 #include <linux/pfn.h>
22 #include <asm/pgtable.h>
25 #include <asm/setup.h>
29 /* For PCI or other memory-mapped resources */
30 unsigned long pci_mem_start
= 0xaeedbabe;
32 EXPORT_SYMBOL(pci_mem_start
);
36 * This function checks if any part of the range <start,end> is mapped
40 e820_any_mapped(u64 start
, u64 end
, unsigned type
)
44 for (i
= 0; i
< e820
.nr_map
; i
++) {
45 struct e820entry
*ei
= &e820
.map
[i
];
47 if (type
&& ei
->type
!= type
)
49 if (ei
->addr
>= end
|| ei
->addr
+ ei
->size
<= start
)
55 EXPORT_SYMBOL_GPL(e820_any_mapped
);
58 * This function checks if the entire range <start,end> is mapped with type.
60 * Note: this function only works correct if the e820 table is sorted and
61 * not-overlapping, which is the case
63 int __init
e820_all_mapped(u64 start
, u64 end
, unsigned type
)
67 for (i
= 0; i
< e820
.nr_map
; i
++) {
68 struct e820entry
*ei
= &e820
.map
[i
];
70 if (type
&& ei
->type
!= type
)
72 /* is the region (part) in overlap with the current region ?*/
73 if (ei
->addr
>= end
|| ei
->addr
+ ei
->size
<= start
)
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
79 if (ei
->addr
<= start
)
80 start
= ei
->addr
+ ei
->size
;
82 * if start is now at or beyond end, we're done, full
92 * Add a memory region to the kernel e820 map.
94 void __init
add_memory_region(u64 start
, u64 size
, int type
)
98 if (x
== ARRAY_SIZE(e820
.map
)) {
99 printk(KERN_ERR
"Ooops! Too many entries in the memory map!\n");
103 e820
.map
[x
].addr
= start
;
104 e820
.map
[x
].size
= size
;
105 e820
.map
[x
].type
= type
;
109 void __init
e820_print_map(char *who
)
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
,
117 (e820
.map
[i
].addr
+ e820
.map
[i
].size
));
118 switch (e820
.map
[i
].type
) {
120 printk(KERN_CONT
"(usable)\n");
123 printk(KERN_CONT
"(reserved)\n");
126 printk(KERN_CONT
"(ACPI data)\n");
129 printk(KERN_CONT
"(ACPI NVS)\n");
132 printk(KERN_CONT
"type %u\n", e820
.map
[i
].type
);
139 * Sanitize the BIOS e820 map.
141 * Some e820 responses include overlapping entries. The following
142 * replaces the original e820 map with a new one, removing overlaps,
143 * and resolving conflicting memory types in favor of highest
146 * The input parameter biosmap points to an array of 'struct
147 * e820entry' which on entry has elements in the range [0, *pnr_map)
148 * valid, and which has space for up to max_nr_map entries.
149 * On return, the resulting sanitized e820 map entries will be in
150 * overwritten in the same location, starting at biosmap.
152 * The integer pointed to by pnr_map must be valid on entry (the
153 * current number of valid entries located at biosmap) and will
154 * be updated on return, with the new number of valid entries
155 * (something no more than max_nr_map.)
157 * The return value from sanitize_e820_map() is zero if it
158 * successfully 'sanitized' the map entries passed in, and is -1
159 * if it did nothing, which can happen if either of (1) it was
160 * only passed one map entry, or (2) any of the input map entries
161 * were invalid (start + size < start, meaning that the size was
162 * so big the described memory range wrapped around through zero.)
164 * Visually we're performing the following
165 * (1,2,3,4 = memory types)...
167 * Sample memory map (w/overlaps):
168 * ____22__________________
169 * ______________________4_
170 * ____1111________________
171 * _44_____________________
172 * 11111111________________
173 * ____________________33__
174 * ___________44___________
175 * __________33333_________
176 * ______________22________
177 * ___________________2222_
178 * _________111111111______
179 * _____________________11_
180 * _________________4______
182 * Sanitized equivalent (no overlap):
183 * 1_______________________
184 * _44_____________________
185 * ___1____________________
186 * ____22__________________
187 * ______11________________
188 * _________1______________
189 * __________3_____________
190 * ___________44___________
191 * _____________33_________
192 * _______________2________
193 * ________________1_______
194 * _________________4______
195 * ___________________2____
196 * ____________________33__
197 * ______________________4_
200 int __init
sanitize_e820_map(struct e820entry
*biosmap
, int max_nr_map
,
203 struct change_member
{
204 struct e820entry
*pbios
; /* pointer to original bios entry */
205 unsigned long long addr
; /* address for this change point */
207 static struct change_member change_point_list
[2*E820_X_MAX
] __initdata
;
208 static struct change_member
*change_point
[2*E820_X_MAX
] __initdata
;
209 static struct e820entry
*overlap_list
[E820_X_MAX
] __initdata
;
210 static struct e820entry new_bios
[E820_X_MAX
] __initdata
;
211 struct change_member
*change_tmp
;
212 unsigned long current_type
, last_type
;
213 unsigned long long last_addr
;
214 int chgidx
, still_changing
;
217 int old_nr
, new_nr
, chg_nr
;
220 /* if there's only one memory region, don't bother */
225 BUG_ON(old_nr
> max_nr_map
);
227 /* bail out if we find any unreasonable addresses in bios map */
228 for (i
= 0; i
< old_nr
; i
++)
229 if (biosmap
[i
].addr
+ biosmap
[i
].size
< biosmap
[i
].addr
)
232 /* create pointers for initial change-point information (for sorting) */
233 for (i
= 0; i
< 2 * old_nr
; i
++)
234 change_point
[i
] = &change_point_list
[i
];
236 /* record all known change-points (starting and ending addresses),
237 omitting those that are for empty memory regions */
239 for (i
= 0; i
< old_nr
; i
++) {
240 if (biosmap
[i
].size
!= 0) {
241 change_point
[chgidx
]->addr
= biosmap
[i
].addr
;
242 change_point
[chgidx
++]->pbios
= &biosmap
[i
];
243 change_point
[chgidx
]->addr
= biosmap
[i
].addr
+
245 change_point
[chgidx
++]->pbios
= &biosmap
[i
];
250 /* sort change-point list by memory addresses (low -> high) */
252 while (still_changing
) {
254 for (i
= 1; i
< chg_nr
; i
++) {
255 unsigned long long curaddr
, lastaddr
;
256 unsigned long long curpbaddr
, lastpbaddr
;
258 curaddr
= change_point
[i
]->addr
;
259 lastaddr
= change_point
[i
- 1]->addr
;
260 curpbaddr
= change_point
[i
]->pbios
->addr
;
261 lastpbaddr
= change_point
[i
- 1]->pbios
->addr
;
264 * swap entries, when:
266 * curaddr > lastaddr or
267 * curaddr == lastaddr and curaddr == curpbaddr and
268 * lastaddr != lastpbaddr
270 if (curaddr
< lastaddr
||
271 (curaddr
== lastaddr
&& curaddr
== curpbaddr
&&
272 lastaddr
!= lastpbaddr
)) {
273 change_tmp
= change_point
[i
];
274 change_point
[i
] = change_point
[i
-1];
275 change_point
[i
-1] = change_tmp
;
281 /* create a new bios memory map, removing overlaps */
282 overlap_entries
= 0; /* number of entries in the overlap table */
283 new_bios_entry
= 0; /* index for creating new bios map entries */
284 last_type
= 0; /* start with undefined memory type */
285 last_addr
= 0; /* start with 0 as last starting address */
287 /* loop through change-points, determining affect on the new bios map */
288 for (chgidx
= 0; chgidx
< chg_nr
; chgidx
++) {
289 /* keep track of all overlapping bios entries */
290 if (change_point
[chgidx
]->addr
==
291 change_point
[chgidx
]->pbios
->addr
) {
293 * add map entry to overlap list (> 1 entry
294 * implies an overlap)
296 overlap_list
[overlap_entries
++] =
297 change_point
[chgidx
]->pbios
;
300 * remove entry from list (order independent,
303 for (i
= 0; i
< overlap_entries
; i
++) {
304 if (overlap_list
[i
] ==
305 change_point
[chgidx
]->pbios
)
307 overlap_list
[overlap_entries
-1];
312 * if there are overlapping entries, decide which
313 * "type" to use (larger value takes precedence --
314 * 1=usable, 2,3,4,4+=unusable)
317 for (i
= 0; i
< overlap_entries
; i
++)
318 if (overlap_list
[i
]->type
> current_type
)
319 current_type
= overlap_list
[i
]->type
;
321 * continue building up new bios map based on this
324 if (current_type
!= last_type
) {
325 if (last_type
!= 0) {
326 new_bios
[new_bios_entry
].size
=
327 change_point
[chgidx
]->addr
- last_addr
;
329 * move forward only if the new size
332 if (new_bios
[new_bios_entry
].size
!= 0)
334 * no more space left for new
337 if (++new_bios_entry
>= max_nr_map
)
340 if (current_type
!= 0) {
341 new_bios
[new_bios_entry
].addr
=
342 change_point
[chgidx
]->addr
;
343 new_bios
[new_bios_entry
].type
= current_type
;
344 last_addr
= change_point
[chgidx
]->addr
;
346 last_type
= current_type
;
349 /* retain count for new bios entries */
350 new_nr
= new_bios_entry
;
352 /* copy new bios mapping into original location */
353 memcpy(biosmap
, new_bios
, new_nr
* sizeof(struct e820entry
));
360 * Copy the BIOS e820 map into a safe place.
362 * Sanity-check it while we're at it..
364 * If we're lucky and live on a modern system, the setup code
365 * will have given us a memory map that we can use to properly
366 * set up memory. If we aren't, we'll fake a memory map.
368 int __init
copy_e820_map(struct e820entry
*biosmap
, int nr_map
)
370 /* Only one memory region (or negative)? Ignore it */
375 u64 start
= biosmap
->addr
;
376 u64 size
= biosmap
->size
;
377 u64 end
= start
+ size
;
378 u32 type
= biosmap
->type
;
380 /* Overflow in 64 bits? Ignore the memory map. */
384 add_memory_region(start
, size
, type
);
385 } while (biosmap
++, --nr_map
);
389 u64 __init
update_memory_range(u64 start
, u64 size
, unsigned old_type
,
393 u64 real_updated_size
= 0;
395 BUG_ON(old_type
== new_type
);
397 for (i
= 0; i
< e820
.nr_map
; i
++) {
398 struct e820entry
*ei
= &e820
.map
[i
];
399 u64 final_start
, final_end
;
400 if (ei
->type
!= old_type
)
402 /* totally covered? */
403 if (ei
->addr
>= start
&&
404 (ei
->addr
+ ei
->size
) <= (start
+ size
)) {
406 real_updated_size
+= ei
->size
;
409 /* partially covered */
410 final_start
= max(start
, ei
->addr
);
411 final_end
= min(start
+ size
, ei
->addr
+ ei
->size
);
412 if (final_start
>= final_end
)
414 add_memory_region(final_start
, final_end
- final_start
,
416 real_updated_size
+= final_end
- final_start
;
418 return real_updated_size
;
421 void __init
update_e820(void)
425 nr_map
= e820
.nr_map
;
426 if (sanitize_e820_map(e820
.map
, ARRAY_SIZE(e820
.map
), &nr_map
))
428 e820
.nr_map
= nr_map
;
429 printk(KERN_INFO
"modified physical RAM map:\n");
430 e820_print_map("modified");
434 * Search for the biggest gap in the low 32 bits of the e820
435 * memory space. We pass this space to PCI to assign MMIO resources
436 * for hotplug or unconfigured devices in.
437 * Hopefully the BIOS let enough space left.
439 __init
void e820_setup_gap(void)
441 unsigned long gapstart
, gapsize
, round
;
442 unsigned long long last
;
446 last
= 0x100000000ull
;
447 gapstart
= 0x10000000;
451 unsigned long long start
= e820
.map
[i
].addr
;
452 unsigned long long end
= start
+ e820
.map
[i
].size
;
455 * Since "last" is at most 4GB, we know we'll
456 * fit in 32 bits if this condition is true
459 unsigned long gap
= last
- end
;
473 gapstart
= (end_pfn
<< PAGE_SHIFT
) + 1024*1024;
474 printk(KERN_ERR
"PCI: Warning: Cannot find a gap in the 32bit "
476 KERN_ERR
"PCI: Unassigned devices with 32bit resource "
477 "registers may break!\n");
482 * See how much we want to round up: start off with
483 * rounding to the next 1MB area.
486 while ((gapsize
>> 4) > round
)
488 /* Fun with two's complement */
489 pci_mem_start
= (gapstart
+ round
) & -round
;
492 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
493 pci_mem_start
, gapstart
, gapsize
);