e820.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. /*
  2. * Low level x86 E820 memory map handling functions.
  3. *
  4. * The firmware and bootloader passes us the "E820 table", which is the primary
  5. * physical memory layout description available about x86 systems.
  6. *
  7. * The kernel takes the E820 memory layout and optionally modifies it with
  8. * quirks and other tweaks, and feeds that into the generic Linux memory
  9. * allocation code routines via a platform independent interface (memblock, etc.).
  10. */
  11. #include <linux/crash_dump.h>
  12. #include <linux/memblock.h>
  13. #include <linux/suspend.h>
  14. #include <linux/acpi.h>
  15. #include <linux/firmware-map.h>
  16. #include <linux/sort.h>
  17. #include <asm/e820/api.h>
  18. #include <asm/setup.h>
  19. /*
  20. * We organize the E820 table into three main data structures:
  21. *
  22. * - 'e820_table_firmware': the original firmware version passed to us by the
  23. * bootloader - not modified by the kernel. It is composed of two parts:
  24. * the first 128 E820 memory entries in boot_params.e820_table and the remaining
  25. * (if any) entries of the SETUP_E820_EXT nodes. We use this to:
  26. *
  27. * - inform the user about the firmware's notion of memory layout
  28. * via /sys/firmware/memmap
  29. *
  30. * - the hibernation code uses it to generate a kernel-independent MD5
  31. * fingerprint of the physical memory layout of a system.
  32. *
  33. * - 'e820_table_kexec': a slightly modified (by the kernel) firmware version
  34. * passed to us by the bootloader - the major difference between
  35. * e820_table_firmware[] and this one is that, the latter marks the setup_data
  36. * list created by the EFI boot stub as reserved, so that kexec can reuse the
  37. * setup_data information in the second kernel. Besides, e820_table_kexec[]
  38. * might also be modified by the kexec itself to fake a mptable.
  39. * We use this to:
  40. *
  41. * - kexec, which is a bootloader in disguise, uses the original E820
  42. * layout to pass to the kexec-ed kernel. This way the original kernel
  43. * can have a restricted E820 map while the kexec()-ed kexec-kernel
  44. * can have access to full memory - etc.
  45. *
  46. * - 'e820_table': this is the main E820 table that is massaged by the
  47. * low level x86 platform code, or modified by boot parameters, before
  48. * passed on to higher level MM layers.
  49. *
  50. * Once the E820 map has been converted to the standard Linux memory layout
  51. * information its role stops - modifying it has no effect and does not get
  52. * re-propagated. So itsmain role is a temporary bootstrap storage of firmware
  53. * specific memory layout data during early bootup.
  54. */
  55. static struct e820_table e820_table_init __initdata;
  56. static struct e820_table e820_table_kexec_init __initdata;
  57. static struct e820_table e820_table_firmware_init __initdata;
  58. struct e820_table *e820_table __refdata = &e820_table_init;
  59. struct e820_table *e820_table_kexec __refdata = &e820_table_kexec_init;
  60. struct e820_table *e820_table_firmware __refdata = &e820_table_firmware_init;
  61. /* For PCI or other memory-mapped resources */
  62. unsigned long pci_mem_start = 0xaeedbabe;
  63. #ifdef CONFIG_PCI
  64. EXPORT_SYMBOL(pci_mem_start);
  65. #endif
  66. /*
  67. * This function checks if any part of the range <start,end> is mapped
  68. * with type.
  69. */
  70. bool e820__mapped_any(u64 start, u64 end, enum e820_type type)
  71. {
  72. int i;
  73. for (i = 0; i < e820_table->nr_entries; i++) {
  74. struct e820_entry *entry = &e820_table->entries[i];
  75. if (type && entry->type != type)
  76. continue;
  77. if (entry->addr >= end || entry->addr + entry->size <= start)
  78. continue;
  79. return 1;
  80. }
  81. return 0;
  82. }
  83. EXPORT_SYMBOL_GPL(e820__mapped_any);
  84. /*
  85. * This function checks if the entire <start,end> range is mapped with 'type'.
  86. *
  87. * Note: this function only works correctly once the E820 table is sorted and
  88. * not-overlapping (at least for the range specified), which is the case normally.
  89. */
  90. static struct e820_entry *__e820__mapped_all(u64 start, u64 end,
  91. enum e820_type type)
  92. {
  93. int i;
  94. for (i = 0; i < e820_table->nr_entries; i++) {
  95. struct e820_entry *entry = &e820_table->entries[i];
  96. if (type && entry->type != type)
  97. continue;
  98. /* Is the region (part) in overlap with the current region? */
  99. if (entry->addr >= end || entry->addr + entry->size <= start)
  100. continue;
  101. /*
  102. * If the region is at the beginning of <start,end> we move
  103. * 'start' to the end of the region since it's ok until there
  104. */
  105. if (entry->addr <= start)
  106. start = entry->addr + entry->size;
  107. /*
  108. * If 'start' is now at or beyond 'end', we're done, full
  109. * coverage of the desired range exists:
  110. */
  111. if (start >= end)
  112. return entry;
  113. }
  114. return NULL;
  115. }
  116. /*
  117. * This function checks if the entire range <start,end> is mapped with type.
  118. */
  119. bool __init e820__mapped_all(u64 start, u64 end, enum e820_type type)
  120. {
  121. return __e820__mapped_all(start, end, type);
  122. }
  123. /*
  124. * This function returns the type associated with the range <start,end>.
  125. */
  126. int e820__get_entry_type(u64 start, u64 end)
  127. {
  128. struct e820_entry *entry = __e820__mapped_all(start, end, 0);
  129. return entry ? entry->type : -EINVAL;
  130. }
  131. /*
  132. * Add a memory region to the kernel E820 map.
  133. */
  134. static void __init __e820__range_add(struct e820_table *table, u64 start, u64 size, enum e820_type type)
  135. {
  136. int x = table->nr_entries;
  137. if (x >= ARRAY_SIZE(table->entries)) {
  138. pr_err("too many entries; ignoring [mem %#010llx-%#010llx]\n",
  139. start, start + size - 1);
  140. return;
  141. }
  142. table->entries[x].addr = start;
  143. table->entries[x].size = size;
  144. table->entries[x].type = type;
  145. table->nr_entries++;
  146. }
  147. void __init e820__range_add(u64 start, u64 size, enum e820_type type)
  148. {
  149. __e820__range_add(e820_table, start, size, type);
  150. }
  151. static void __init e820_print_type(enum e820_type type)
  152. {
  153. switch (type) {
  154. case E820_TYPE_RAM: /* Fall through: */
  155. case E820_TYPE_RESERVED_KERN: pr_cont("usable"); break;
  156. case E820_TYPE_RESERVED: pr_cont("reserved"); break;
  157. case E820_TYPE_ACPI: pr_cont("ACPI data"); break;
  158. case E820_TYPE_NVS: pr_cont("ACPI NVS"); break;
  159. case E820_TYPE_UNUSABLE: pr_cont("unusable"); break;
  160. case E820_TYPE_PMEM: /* Fall through: */
  161. case E820_TYPE_PRAM: pr_cont("persistent (type %u)", type); break;
  162. default: pr_cont("type %u", type); break;
  163. }
  164. }
  165. void __init e820__print_table(char *who)
  166. {
  167. int i;
  168. for (i = 0; i < e820_table->nr_entries; i++) {
  169. pr_info("%s: [mem %#018Lx-%#018Lx] ",
  170. who,
  171. e820_table->entries[i].addr,
  172. e820_table->entries[i].addr + e820_table->entries[i].size - 1);
  173. e820_print_type(e820_table->entries[i].type);
  174. pr_cont("\n");
  175. }
  176. }
  177. /*
  178. * Sanitize an E820 map.
  179. *
  180. * Some E820 layouts include overlapping entries. The following
  181. * replaces the original E820 map with a new one, removing overlaps,
  182. * and resolving conflicting memory types in favor of highest
  183. * numbered type.
  184. *
  185. * The input parameter 'entries' points to an array of 'struct
  186. * e820_entry' which on entry has elements in the range [0, *nr_entries)
  187. * valid, and which has space for up to max_nr_entries entries.
  188. * On return, the resulting sanitized E820 map entries will be in
  189. * overwritten in the same location, starting at 'entries'.
  190. *
  191. * The integer pointed to by nr_entries must be valid on entry (the
  192. * current number of valid entries located at 'entries'). If the
  193. * sanitizing succeeds the *nr_entries will be updated with the new
  194. * number of valid entries (something no more than max_nr_entries).
  195. *
  196. * The return value from e820__update_table() is zero if it
  197. * successfully 'sanitized' the map entries passed in, and is -1
  198. * if it did nothing, which can happen if either of (1) it was
  199. * only passed one map entry, or (2) any of the input map entries
  200. * were invalid (start + size < start, meaning that the size was
  201. * so big the described memory range wrapped around through zero.)
  202. *
  203. * Visually we're performing the following
  204. * (1,2,3,4 = memory types)...
  205. *
  206. * Sample memory map (w/overlaps):
  207. * ____22__________________
  208. * ______________________4_
  209. * ____1111________________
  210. * _44_____________________
  211. * 11111111________________
  212. * ____________________33__
  213. * ___________44___________
  214. * __________33333_________
  215. * ______________22________
  216. * ___________________2222_
  217. * _________111111111______
  218. * _____________________11_
  219. * _________________4______
  220. *
  221. * Sanitized equivalent (no overlap):
  222. * 1_______________________
  223. * _44_____________________
  224. * ___1____________________
  225. * ____22__________________
  226. * ______11________________
  227. * _________1______________
  228. * __________3_____________
  229. * ___________44___________
  230. * _____________33_________
  231. * _______________2________
  232. * ________________1_______
  233. * _________________4______
  234. * ___________________2____
  235. * ____________________33__
  236. * ______________________4_
  237. */
  238. struct change_member {
  239. /* Pointer to the original entry: */
  240. struct e820_entry *entry;
  241. /* Address for this change point: */
  242. unsigned long long addr;
  243. };
  244. static struct change_member change_point_list[2*E820_MAX_ENTRIES] __initdata;
  245. static struct change_member *change_point[2*E820_MAX_ENTRIES] __initdata;
  246. static struct e820_entry *overlap_list[E820_MAX_ENTRIES] __initdata;
  247. static struct e820_entry new_entries[E820_MAX_ENTRIES] __initdata;
  248. static int __init cpcompare(const void *a, const void *b)
  249. {
  250. struct change_member * const *app = a, * const *bpp = b;
  251. const struct change_member *ap = *app, *bp = *bpp;
  252. /*
  253. * Inputs are pointers to two elements of change_point[]. If their
  254. * addresses are not equal, their difference dominates. If the addresses
  255. * are equal, then consider one that represents the end of its region
  256. * to be greater than one that does not.
  257. */
  258. if (ap->addr != bp->addr)
  259. return ap->addr > bp->addr ? 1 : -1;
  260. return (ap->addr != ap->entry->addr) - (bp->addr != bp->entry->addr);
  261. }
  262. int __init e820__update_table(struct e820_table *table)
  263. {
  264. struct e820_entry *entries = table->entries;
  265. u32 max_nr_entries = ARRAY_SIZE(table->entries);
  266. enum e820_type current_type, last_type;
  267. unsigned long long last_addr;
  268. u32 new_nr_entries, overlap_entries;
  269. u32 i, chg_idx, chg_nr;
  270. /* If there's only one memory region, don't bother: */
  271. if (table->nr_entries < 2)
  272. return -1;
  273. BUG_ON(table->nr_entries > max_nr_entries);
  274. /* Bail out if we find any unreasonable addresses in the map: */
  275. for (i = 0; i < table->nr_entries; i++) {
  276. if (entries[i].addr + entries[i].size < entries[i].addr)
  277. return -1;
  278. }
  279. /* Create pointers for initial change-point information (for sorting): */
  280. for (i = 0; i < 2 * table->nr_entries; i++)
  281. change_point[i] = &change_point_list[i];
  282. /*
  283. * Record all known change-points (starting and ending addresses),
  284. * omitting empty memory regions:
  285. */
  286. chg_idx = 0;
  287. for (i = 0; i < table->nr_entries; i++) {
  288. if (entries[i].size != 0) {
  289. change_point[chg_idx]->addr = entries[i].addr;
  290. change_point[chg_idx++]->entry = &entries[i];
  291. change_point[chg_idx]->addr = entries[i].addr + entries[i].size;
  292. change_point[chg_idx++]->entry = &entries[i];
  293. }
  294. }
  295. chg_nr = chg_idx;
  296. /* Sort change-point list by memory addresses (low -> high): */
  297. sort(change_point, chg_nr, sizeof(*change_point), cpcompare, NULL);
  298. /* Create a new memory map, removing overlaps: */
  299. overlap_entries = 0; /* Number of entries in the overlap table */
  300. new_nr_entries = 0; /* Index for creating new map entries */
  301. last_type = 0; /* Start with undefined memory type */
  302. last_addr = 0; /* Start with 0 as last starting address */
  303. /* Loop through change-points, determining effect on the new map: */
  304. for (chg_idx = 0; chg_idx < chg_nr; chg_idx++) {
  305. /* Keep track of all overlapping entries */
  306. if (change_point[chg_idx]->addr == change_point[chg_idx]->entry->addr) {
  307. /* Add map entry to overlap list (> 1 entry implies an overlap) */
  308. overlap_list[overlap_entries++] = change_point[chg_idx]->entry;
  309. } else {
  310. /* Remove entry from list (order independent, so swap with last): */
  311. for (i = 0; i < overlap_entries; i++) {
  312. if (overlap_list[i] == change_point[chg_idx]->entry)
  313. overlap_list[i] = overlap_list[overlap_entries-1];
  314. }
  315. overlap_entries--;
  316. }
  317. /*
  318. * If there are overlapping entries, decide which
  319. * "type" to use (larger value takes precedence --
  320. * 1=usable, 2,3,4,4+=unusable)
  321. */
  322. current_type = 0;
  323. for (i = 0; i < overlap_entries; i++) {
  324. if (overlap_list[i]->type > current_type)
  325. current_type = overlap_list[i]->type;
  326. }
  327. /* Continue building up new map based on this information: */
  328. if (current_type != last_type || current_type == E820_TYPE_PRAM) {
  329. if (last_type != 0) {
  330. new_entries[new_nr_entries].size = change_point[chg_idx]->addr - last_addr;
  331. /* Move forward only if the new size was non-zero: */
  332. if (new_entries[new_nr_entries].size != 0)
  333. /* No more space left for new entries? */
  334. if (++new_nr_entries >= max_nr_entries)
  335. break;
  336. }
  337. if (current_type != 0) {
  338. new_entries[new_nr_entries].addr = change_point[chg_idx]->addr;
  339. new_entries[new_nr_entries].type = current_type;
  340. last_addr = change_point[chg_idx]->addr;
  341. }
  342. last_type = current_type;
  343. }
  344. }
  345. /* Copy the new entries into the original location: */
  346. memcpy(entries, new_entries, new_nr_entries*sizeof(*entries));
  347. table->nr_entries = new_nr_entries;
  348. return 0;
  349. }
  350. static int __init __append_e820_table(struct boot_e820_entry *entries, u32 nr_entries)
  351. {
  352. struct boot_e820_entry *entry = entries;
  353. while (nr_entries) {
  354. u64 start = entry->addr;
  355. u64 size = entry->size;
  356. u64 end = start + size - 1;
  357. u32 type = entry->type;
  358. /* Ignore the entry on 64-bit overflow: */
  359. if (start > end && likely(size))
  360. return -1;
  361. e820__range_add(start, size, type);
  362. entry++;
  363. nr_entries--;
  364. }
  365. return 0;
  366. }
  367. /*
  368. * Copy the BIOS E820 map into a safe place.
  369. *
  370. * Sanity-check it while we're at it..
  371. *
  372. * If we're lucky and live on a modern system, the setup code
  373. * will have given us a memory map that we can use to properly
  374. * set up memory. If we aren't, we'll fake a memory map.
  375. */
  376. static int __init append_e820_table(struct boot_e820_entry *entries, u32 nr_entries)
  377. {
  378. /* Only one memory region (or negative)? Ignore it */
  379. if (nr_entries < 2)
  380. return -1;
  381. return __append_e820_table(entries, nr_entries);
  382. }
  383. static u64 __init
  384. __e820__range_update(struct e820_table *table, u64 start, u64 size, enum e820_type old_type, enum e820_type new_type)
  385. {
  386. u64 end;
  387. unsigned int i;
  388. u64 real_updated_size = 0;
  389. BUG_ON(old_type == new_type);
  390. if (size > (ULLONG_MAX - start))
  391. size = ULLONG_MAX - start;
  392. end = start + size;
  393. printk(KERN_DEBUG "e820: update [mem %#010Lx-%#010Lx] ", start, end - 1);
  394. e820_print_type(old_type);
  395. pr_cont(" ==> ");
  396. e820_print_type(new_type);
  397. pr_cont("\n");
  398. for (i = 0; i < table->nr_entries; i++) {
  399. struct e820_entry *entry = &table->entries[i];
  400. u64 final_start, final_end;
  401. u64 entry_end;
  402. if (entry->type != old_type)
  403. continue;
  404. entry_end = entry->addr + entry->size;
  405. /* Completely covered by new range? */
  406. if (entry->addr >= start && entry_end <= end) {
  407. entry->type = new_type;
  408. real_updated_size += entry->size;
  409. continue;
  410. }
  411. /* New range is completely covered? */
  412. if (entry->addr < start && entry_end > end) {
  413. __e820__range_add(table, start, size, new_type);
  414. __e820__range_add(table, end, entry_end - end, entry->type);
  415. entry->size = start - entry->addr;
  416. real_updated_size += size;
  417. continue;
  418. }
  419. /* Partially covered: */
  420. final_start = max(start, entry->addr);
  421. final_end = min(end, entry_end);
  422. if (final_start >= final_end)
  423. continue;
  424. __e820__range_add(table, final_start, final_end - final_start, new_type);
  425. real_updated_size += final_end - final_start;
  426. /*
  427. * Left range could be head or tail, so need to update
  428. * its size first:
  429. */
  430. entry->size -= final_end - final_start;
  431. if (entry->addr < final_start)
  432. continue;
  433. entry->addr = final_end;
  434. }
  435. return real_updated_size;
  436. }
  437. u64 __init e820__range_update(u64 start, u64 size, enum e820_type old_type, enum e820_type new_type)
  438. {
  439. return __e820__range_update(e820_table, start, size, old_type, new_type);
  440. }
  441. static u64 __init e820__range_update_kexec(u64 start, u64 size, enum e820_type old_type, enum e820_type new_type)
  442. {
  443. return __e820__range_update(e820_table_kexec, start, size, old_type, new_type);
  444. }
  445. /* Remove a range of memory from the E820 table: */
  446. u64 __init e820__range_remove(u64 start, u64 size, enum e820_type old_type, bool check_type)
  447. {
  448. int i;
  449. u64 end;
  450. u64 real_removed_size = 0;
  451. if (size > (ULLONG_MAX - start))
  452. size = ULLONG_MAX - start;
  453. end = start + size;
  454. printk(KERN_DEBUG "e820: remove [mem %#010Lx-%#010Lx] ", start, end - 1);
  455. if (check_type)
  456. e820_print_type(old_type);
  457. pr_cont("\n");
  458. for (i = 0; i < e820_table->nr_entries; i++) {
  459. struct e820_entry *entry = &e820_table->entries[i];
  460. u64 final_start, final_end;
  461. u64 entry_end;
  462. if (check_type && entry->type != old_type)
  463. continue;
  464. entry_end = entry->addr + entry->size;
  465. /* Completely covered? */
  466. if (entry->addr >= start && entry_end <= end) {
  467. real_removed_size += entry->size;
  468. memset(entry, 0, sizeof(*entry));
  469. continue;
  470. }
  471. /* Is the new range completely covered? */
  472. if (entry->addr < start && entry_end > end) {
  473. e820__range_add(end, entry_end - end, entry->type);
  474. entry->size = start - entry->addr;
  475. real_removed_size += size;
  476. continue;
  477. }
  478. /* Partially covered: */
  479. final_start = max(start, entry->addr);
  480. final_end = min(end, entry_end);
  481. if (final_start >= final_end)
  482. continue;
  483. real_removed_size += final_end - final_start;
  484. /*
  485. * Left range could be head or tail, so need to update
  486. * the size first:
  487. */
  488. entry->size -= final_end - final_start;
  489. if (entry->addr < final_start)
  490. continue;
  491. entry->addr = final_end;
  492. }
  493. return real_removed_size;
  494. }
  495. void __init e820__update_table_print(void)
  496. {
  497. if (e820__update_table(e820_table))
  498. return;
  499. pr_info("modified physical RAM map:\n");
  500. e820__print_table("modified");
  501. }
  502. static void __init e820__update_table_kexec(void)
  503. {
  504. e820__update_table(e820_table_kexec);
  505. }
  506. #define MAX_GAP_END 0x100000000ull
  507. /*
  508. * Search for a gap in the E820 memory space from 0 to MAX_GAP_END (4GB).
  509. */
  510. static int __init e820_search_gap(unsigned long *gapstart, unsigned long *gapsize)
  511. {
  512. unsigned long long last = MAX_GAP_END;
  513. int i = e820_table->nr_entries;
  514. int found = 0;
  515. while (--i >= 0) {
  516. unsigned long long start = e820_table->entries[i].addr;
  517. unsigned long long end = start + e820_table->entries[i].size;
  518. /*
  519. * Since "last" is at most 4GB, we know we'll
  520. * fit in 32 bits if this condition is true:
  521. */
  522. if (last > end) {
  523. unsigned long gap = last - end;
  524. if (gap >= *gapsize) {
  525. *gapsize = gap;
  526. *gapstart = end;
  527. found = 1;
  528. }
  529. }
  530. if (start < last)
  531. last = start;
  532. }
  533. return found;
  534. }
  535. /*
  536. * Search for the biggest gap in the low 32 bits of the E820
  537. * memory space. We pass this space to the PCI subsystem, so
  538. * that it can assign MMIO resources for hotplug or
  539. * unconfigured devices in.
  540. *
  541. * Hopefully the BIOS let enough space left.
  542. */
  543. __init void e820__setup_pci_gap(void)
  544. {
  545. unsigned long gapstart, gapsize;
  546. int found;
  547. gapsize = 0x400000;
  548. found = e820_search_gap(&gapstart, &gapsize);
  549. if (!found) {
  550. #ifdef CONFIG_X86_64
  551. gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024;
  552. pr_err("Cannot find an available gap in the 32-bit address range\n");
  553. pr_err("PCI devices with unassigned 32-bit BARs may not work!\n");
  554. #else
  555. gapstart = 0x10000000;
  556. #endif
  557. }
  558. /*
  559. * e820__reserve_resources_late() protects stolen RAM already:
  560. */
  561. pci_mem_start = gapstart;
  562. pr_info("[mem %#010lx-%#010lx] available for PCI devices\n",
  563. gapstart, gapstart + gapsize - 1);
  564. }
  565. /*
  566. * Called late during init, in free_initmem().
  567. *
  568. * Initial e820_table and e820_table_kexec are largish __initdata arrays.
  569. *
  570. * Copy them to a (usually much smaller) dynamically allocated area that is
  571. * sized precisely after the number of e820 entries.
  572. *
  573. * This is done after we've performed all the fixes and tweaks to the tables.
  574. * All functions which modify them are __init functions, which won't exist
  575. * after free_initmem().
  576. */
  577. __init void e820__reallocate_tables(void)
  578. {
  579. struct e820_table *n;
  580. int size;
  581. size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table->nr_entries;
  582. n = kmalloc(size, GFP_KERNEL);
  583. BUG_ON(!n);
  584. memcpy(n, e820_table, size);
  585. e820_table = n;
  586. size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table_kexec->nr_entries;
  587. n = kmalloc(size, GFP_KERNEL);
  588. BUG_ON(!n);
  589. memcpy(n, e820_table_kexec, size);
  590. e820_table_kexec = n;
  591. size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table_firmware->nr_entries;
  592. n = kmalloc(size, GFP_KERNEL);
  593. BUG_ON(!n);
  594. memcpy(n, e820_table_firmware, size);
  595. e820_table_firmware = n;
  596. }
  597. /*
  598. * Because of the small fixed size of struct boot_params, only the first
  599. * 128 E820 memory entries are passed to the kernel via boot_params.e820_table,
  600. * the remaining (if any) entries are passed via the SETUP_E820_EXT node of
  601. * struct setup_data, which is parsed here.
  602. */
  603. void __init e820__memory_setup_extended(u64 phys_addr, u32 data_len)
  604. {
  605. int entries;
  606. struct boot_e820_entry *extmap;
  607. struct setup_data *sdata;
  608. sdata = early_memremap(phys_addr, data_len);
  609. entries = sdata->len / sizeof(*extmap);
  610. extmap = (struct boot_e820_entry *)(sdata->data);
  611. __append_e820_table(extmap, entries);
  612. e820__update_table(e820_table);
  613. memcpy(e820_table_kexec, e820_table, sizeof(*e820_table_kexec));
  614. memcpy(e820_table_firmware, e820_table, sizeof(*e820_table_firmware));
  615. early_memunmap(sdata, data_len);
  616. pr_info("extended physical RAM map:\n");
  617. e820__print_table("extended");
  618. }
  619. /*
  620. * Find the ranges of physical addresses that do not correspond to
  621. * E820 RAM areas and register the corresponding pages as 'nosave' for
  622. * hibernation (32-bit) or software suspend and suspend to RAM (64-bit).
  623. *
  624. * This function requires the E820 map to be sorted and without any
  625. * overlapping entries.
  626. */
  627. void __init e820__register_nosave_regions(unsigned long limit_pfn)
  628. {
  629. int i;
  630. unsigned long pfn = 0;
  631. for (i = 0; i < e820_table->nr_entries; i++) {
  632. struct e820_entry *entry = &e820_table->entries[i];
  633. if (pfn < PFN_UP(entry->addr))
  634. register_nosave_region(pfn, PFN_UP(entry->addr));
  635. pfn = PFN_DOWN(entry->addr + entry->size);
  636. if (entry->type != E820_TYPE_RAM && entry->type != E820_TYPE_RESERVED_KERN)
  637. register_nosave_region(PFN_UP(entry->addr), pfn);
  638. if (pfn >= limit_pfn)
  639. break;
  640. }
  641. }
  642. #ifdef CONFIG_ACPI
  643. /*
  644. * Register ACPI NVS memory regions, so that we can save/restore them during
  645. * hibernation and the subsequent resume:
  646. */
  647. static int __init e820__register_nvs_regions(void)
  648. {
  649. int i;
  650. for (i = 0; i < e820_table->nr_entries; i++) {
  651. struct e820_entry *entry = &e820_table->entries[i];
  652. if (entry->type == E820_TYPE_NVS)
  653. acpi_nvs_register(entry->addr, entry->size);
  654. }
  655. return 0;
  656. }
  657. core_initcall(e820__register_nvs_regions);
  658. #endif
  659. /*
  660. * Allocate the requested number of bytes with the requsted alignment
  661. * and return (the physical address) to the caller. Also register this
  662. * range in the 'kexec' E820 table as a reserved range.
  663. *
  664. * This allows kexec to fake a new mptable, as if it came from the real
  665. * system.
  666. */
  667. u64 __init e820__memblock_alloc_reserved(u64 size, u64 align)
  668. {
  669. u64 addr;
  670. addr = __memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  671. if (addr) {
  672. e820__range_update_kexec(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
  673. pr_info("update e820_table_kexec for e820__memblock_alloc_reserved()\n");
  674. e820__update_table_kexec();
  675. }
  676. return addr;
  677. }
  678. #ifdef CONFIG_X86_32
  679. # ifdef CONFIG_X86_PAE
  680. # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
  681. # else
  682. # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
  683. # endif
  684. #else /* CONFIG_X86_32 */
  685. # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
  686. #endif
  687. /*
  688. * Find the highest page frame number we have available
  689. */
  690. static unsigned long __init e820_end_pfn(unsigned long limit_pfn, enum e820_type type)
  691. {
  692. int i;
  693. unsigned long last_pfn = 0;
  694. unsigned long max_arch_pfn = MAX_ARCH_PFN;
  695. for (i = 0; i < e820_table->nr_entries; i++) {
  696. struct e820_entry *entry = &e820_table->entries[i];
  697. unsigned long start_pfn;
  698. unsigned long end_pfn;
  699. if (entry->type != type)
  700. continue;
  701. start_pfn = entry->addr >> PAGE_SHIFT;
  702. end_pfn = (entry->addr + entry->size) >> PAGE_SHIFT;
  703. if (start_pfn >= limit_pfn)
  704. continue;
  705. if (end_pfn > limit_pfn) {
  706. last_pfn = limit_pfn;
  707. break;
  708. }
  709. if (end_pfn > last_pfn)
  710. last_pfn = end_pfn;
  711. }
  712. if (last_pfn > max_arch_pfn)
  713. last_pfn = max_arch_pfn;
  714. pr_info("last_pfn = %#lx max_arch_pfn = %#lx\n",
  715. last_pfn, max_arch_pfn);
  716. return last_pfn;
  717. }
  718. unsigned long __init e820__end_of_ram_pfn(void)
  719. {
  720. return e820_end_pfn(MAX_ARCH_PFN, E820_TYPE_RAM);
  721. }
  722. unsigned long __init e820__end_of_low_ram_pfn(void)
  723. {
  724. return e820_end_pfn(1UL << (32 - PAGE_SHIFT), E820_TYPE_RAM);
  725. }
  726. static void __init early_panic(char *msg)
  727. {
  728. early_printk(msg);
  729. panic(msg);
  730. }
  731. static int userdef __initdata;
  732. /* The "mem=nopentium" boot option disables 4MB page tables on 32-bit kernels: */
  733. static int __init parse_memopt(char *p)
  734. {
  735. u64 mem_size;
  736. if (!p)
  737. return -EINVAL;
  738. if (!strcmp(p, "nopentium")) {
  739. #ifdef CONFIG_X86_32
  740. setup_clear_cpu_cap(X86_FEATURE_PSE);
  741. return 0;
  742. #else
  743. pr_warn("mem=nopentium ignored! (only supported on x86_32)\n");
  744. return -EINVAL;
  745. #endif
  746. }
  747. userdef = 1;
  748. mem_size = memparse(p, &p);
  749. /* Don't remove all memory when getting "mem={invalid}" parameter: */
  750. if (mem_size == 0)
  751. return -EINVAL;
  752. e820__range_remove(mem_size, ULLONG_MAX - mem_size, E820_TYPE_RAM, 1);
  753. return 0;
  754. }
  755. early_param("mem", parse_memopt);
  756. static int __init parse_memmap_one(char *p)
  757. {
  758. char *oldp;
  759. u64 start_at, mem_size;
  760. if (!p)
  761. return -EINVAL;
  762. if (!strncmp(p, "exactmap", 8)) {
  763. #ifdef CONFIG_CRASH_DUMP
  764. /*
  765. * If we are doing a crash dump, we still need to know
  766. * the real memory size before the original memory map is
  767. * reset.
  768. */
  769. saved_max_pfn = e820__end_of_ram_pfn();
  770. #endif
  771. e820_table->nr_entries = 0;
  772. userdef = 1;
  773. return 0;
  774. }
  775. oldp = p;
  776. mem_size = memparse(p, &p);
  777. if (p == oldp)
  778. return -EINVAL;
  779. userdef = 1;
  780. if (*p == '@') {
  781. start_at = memparse(p+1, &p);
  782. e820__range_add(start_at, mem_size, E820_TYPE_RAM);
  783. } else if (*p == '#') {
  784. start_at = memparse(p+1, &p);
  785. e820__range_add(start_at, mem_size, E820_TYPE_ACPI);
  786. } else if (*p == '$') {
  787. start_at = memparse(p+1, &p);
  788. e820__range_add(start_at, mem_size, E820_TYPE_RESERVED);
  789. } else if (*p == '!') {
  790. start_at = memparse(p+1, &p);
  791. e820__range_add(start_at, mem_size, E820_TYPE_PRAM);
  792. } else if (*p == '%') {
  793. enum e820_type from = 0, to = 0;
  794. start_at = memparse(p + 1, &p);
  795. if (*p == '-')
  796. from = simple_strtoull(p + 1, &p, 0);
  797. if (*p == '+')
  798. to = simple_strtoull(p + 1, &p, 0);
  799. if (*p != '\0')
  800. return -EINVAL;
  801. if (from && to)
  802. e820__range_update(start_at, mem_size, from, to);
  803. else if (to)
  804. e820__range_add(start_at, mem_size, to);
  805. else if (from)
  806. e820__range_remove(start_at, mem_size, from, 1);
  807. else
  808. e820__range_remove(start_at, mem_size, 0, 0);
  809. } else {
  810. e820__range_remove(mem_size, ULLONG_MAX - mem_size, E820_TYPE_RAM, 1);
  811. }
  812. return *p == '\0' ? 0 : -EINVAL;
  813. }
  814. static int __init parse_memmap_opt(char *str)
  815. {
  816. while (str) {
  817. char *k = strchr(str, ',');
  818. if (k)
  819. *k++ = 0;
  820. parse_memmap_one(str);
  821. str = k;
  822. }
  823. return 0;
  824. }
  825. early_param("memmap", parse_memmap_opt);
  826. /*
  827. * Reserve all entries from the bootloader's extensible data nodes list,
  828. * because if present we are going to use it later on to fetch e820
  829. * entries from it:
  830. */
  831. void __init e820__reserve_setup_data(void)
  832. {
  833. struct setup_data *data;
  834. u64 pa_data;
  835. pa_data = boot_params.hdr.setup_data;
  836. if (!pa_data)
  837. return;
  838. while (pa_data) {
  839. data = early_memremap(pa_data, sizeof(*data));
  840. e820__range_update(pa_data, sizeof(*data)+data->len, E820_TYPE_RAM, E820_TYPE_RESERVED_KERN);
  841. e820__range_update_kexec(pa_data, sizeof(*data)+data->len, E820_TYPE_RAM, E820_TYPE_RESERVED_KERN);
  842. pa_data = data->next;
  843. early_memunmap(data, sizeof(*data));
  844. }
  845. e820__update_table(e820_table);
  846. e820__update_table(e820_table_kexec);
  847. pr_info("extended physical RAM map:\n");
  848. e820__print_table("reserve setup_data");
  849. }
  850. /*
  851. * Called after parse_early_param(), after early parameters (such as mem=)
  852. * have been processed, in which case we already have an E820 table filled in
  853. * via the parameter callback function(s), but it's not sorted and printed yet:
  854. */
  855. void __init e820__finish_early_params(void)
  856. {
  857. if (userdef) {
  858. if (e820__update_table(e820_table) < 0)
  859. early_panic("Invalid user supplied memory map");
  860. pr_info("user-defined physical RAM map:\n");
  861. e820__print_table("user");
  862. }
  863. }
  864. static const char *__init e820_type_to_string(struct e820_entry *entry)
  865. {
  866. switch (entry->type) {
  867. case E820_TYPE_RESERVED_KERN: /* Fall-through: */
  868. case E820_TYPE_RAM: return "System RAM";
  869. case E820_TYPE_ACPI: return "ACPI Tables";
  870. case E820_TYPE_NVS: return "ACPI Non-volatile Storage";
  871. case E820_TYPE_UNUSABLE: return "Unusable memory";
  872. case E820_TYPE_PRAM: return "Persistent Memory (legacy)";
  873. case E820_TYPE_PMEM: return "Persistent Memory";
  874. case E820_TYPE_RESERVED: return "Reserved";
  875. default: return "Unknown E820 type";
  876. }
  877. }
  878. static unsigned long __init e820_type_to_iomem_type(struct e820_entry *entry)
  879. {
  880. switch (entry->type) {
  881. case E820_TYPE_RESERVED_KERN: /* Fall-through: */
  882. case E820_TYPE_RAM: return IORESOURCE_SYSTEM_RAM;
  883. case E820_TYPE_ACPI: /* Fall-through: */
  884. case E820_TYPE_NVS: /* Fall-through: */
  885. case E820_TYPE_UNUSABLE: /* Fall-through: */
  886. case E820_TYPE_PRAM: /* Fall-through: */
  887. case E820_TYPE_PMEM: /* Fall-through: */
  888. case E820_TYPE_RESERVED: /* Fall-through: */
  889. default: return IORESOURCE_MEM;
  890. }
  891. }
  892. static unsigned long __init e820_type_to_iores_desc(struct e820_entry *entry)
  893. {
  894. switch (entry->type) {
  895. case E820_TYPE_ACPI: return IORES_DESC_ACPI_TABLES;
  896. case E820_TYPE_NVS: return IORES_DESC_ACPI_NV_STORAGE;
  897. case E820_TYPE_PMEM: return IORES_DESC_PERSISTENT_MEMORY;
  898. case E820_TYPE_PRAM: return IORES_DESC_PERSISTENT_MEMORY_LEGACY;
  899. case E820_TYPE_RESERVED_KERN: /* Fall-through: */
  900. case E820_TYPE_RAM: /* Fall-through: */
  901. case E820_TYPE_UNUSABLE: /* Fall-through: */
  902. case E820_TYPE_RESERVED: /* Fall-through: */
  903. default: return IORES_DESC_NONE;
  904. }
  905. }
  906. static bool __init do_mark_busy(enum e820_type type, struct resource *res)
  907. {
  908. /* this is the legacy bios/dos rom-shadow + mmio region */
  909. if (res->start < (1ULL<<20))
  910. return true;
  911. /*
  912. * Treat persistent memory like device memory, i.e. reserve it
  913. * for exclusive use of a driver
  914. */
  915. switch (type) {
  916. case E820_TYPE_RESERVED:
  917. case E820_TYPE_PRAM:
  918. case E820_TYPE_PMEM:
  919. return false;
  920. case E820_TYPE_RESERVED_KERN:
  921. case E820_TYPE_RAM:
  922. case E820_TYPE_ACPI:
  923. case E820_TYPE_NVS:
  924. case E820_TYPE_UNUSABLE:
  925. default:
  926. return true;
  927. }
  928. }
  929. /*
  930. * Mark E820 reserved areas as busy for the resource manager:
  931. */
  932. static struct resource __initdata *e820_res;
  933. void __init e820__reserve_resources(void)
  934. {
  935. int i;
  936. struct resource *res;
  937. u64 end;
  938. res = memblock_alloc(sizeof(*res) * e820_table->nr_entries,
  939. SMP_CACHE_BYTES);
  940. e820_res = res;
  941. for (i = 0; i < e820_table->nr_entries; i++) {
  942. struct e820_entry *entry = e820_table->entries + i;
  943. end = entry->addr + entry->size - 1;
  944. if (end != (resource_size_t)end) {
  945. res++;
  946. continue;
  947. }
  948. res->start = entry->addr;
  949. res->end = end;
  950. res->name = e820_type_to_string(entry);
  951. res->flags = e820_type_to_iomem_type(entry);
  952. res->desc = e820_type_to_iores_desc(entry);
  953. /*
  954. * Don't register the region that could be conflicted with
  955. * PCI device BAR resources and insert them later in
  956. * pcibios_resource_survey():
  957. */
  958. if (do_mark_busy(entry->type, res)) {
  959. res->flags |= IORESOURCE_BUSY;
  960. insert_resource(&iomem_resource, res);
  961. }
  962. res++;
  963. }
  964. /* Expose the bootloader-provided memory layout to the sysfs. */
  965. for (i = 0; i < e820_table_firmware->nr_entries; i++) {
  966. struct e820_entry *entry = e820_table_firmware->entries + i;
  967. firmware_map_add_early(entry->addr, entry->addr + entry->size, e820_type_to_string(entry));
  968. }
  969. }
  970. /*
  971. * How much should we pad the end of RAM, depending on where it is?
  972. */
  973. static unsigned long __init ram_alignment(resource_size_t pos)
  974. {
  975. unsigned long mb = pos >> 20;
  976. /* To 64kB in the first megabyte */
  977. if (!mb)
  978. return 64*1024;
  979. /* To 1MB in the first 16MB */
  980. if (mb < 16)
  981. return 1024*1024;
  982. /* To 64MB for anything above that */
  983. return 64*1024*1024;
  984. }
  985. #define MAX_RESOURCE_SIZE ((resource_size_t)-1)
  986. void __init e820__reserve_resources_late(void)
  987. {
  988. int i;
  989. struct resource *res;
  990. res = e820_res;
  991. for (i = 0; i < e820_table->nr_entries; i++) {
  992. if (!res->parent && res->end)
  993. insert_resource_expand_to_fit(&iomem_resource, res);
  994. res++;
  995. }
  996. /*
  997. * Try to bump up RAM regions to reasonable boundaries, to
  998. * avoid stolen RAM:
  999. */
  1000. for (i = 0; i < e820_table->nr_entries; i++) {
  1001. struct e820_entry *entry = &e820_table->entries[i];
  1002. u64 start, end;
  1003. if (entry->type != E820_TYPE_RAM)
  1004. continue;
  1005. start = entry->addr + entry->size;
  1006. end = round_up(start, ram_alignment(start)) - 1;
  1007. if (end > MAX_RESOURCE_SIZE)
  1008. end = MAX_RESOURCE_SIZE;
  1009. if (start >= end)
  1010. continue;
  1011. printk(KERN_DEBUG "e820: reserve RAM buffer [mem %#010llx-%#010llx]\n", start, end);
  1012. reserve_region_with_split(&iomem_resource, start, end, "RAM buffer");
  1013. }
  1014. }
  1015. /*
  1016. * Pass the firmware (bootloader) E820 map to the kernel and process it:
  1017. */
  1018. char *__init e820__memory_setup_default(void)
  1019. {
  1020. char *who = "BIOS-e820";
  1021. /*
  1022. * Try to copy the BIOS-supplied E820-map.
  1023. *
  1024. * Otherwise fake a memory map; one section from 0k->640k,
  1025. * the next section from 1mb->appropriate_mem_k
  1026. */
  1027. if (append_e820_table(boot_params.e820_table, boot_params.e820_entries) < 0) {
  1028. u64 mem_size;
  1029. /* Compare results from other methods and take the one that gives more RAM: */
  1030. if (boot_params.alt_mem_k < boot_params.screen_info.ext_mem_k) {
  1031. mem_size = boot_params.screen_info.ext_mem_k;
  1032. who = "BIOS-88";
  1033. } else {
  1034. mem_size = boot_params.alt_mem_k;
  1035. who = "BIOS-e801";
  1036. }
  1037. e820_table->nr_entries = 0;
  1038. e820__range_add(0, LOWMEMSIZE(), E820_TYPE_RAM);
  1039. e820__range_add(HIGH_MEMORY, mem_size << 10, E820_TYPE_RAM);
  1040. }
  1041. /* We just appended a lot of ranges, sanitize the table: */
  1042. e820__update_table(e820_table);
  1043. return who;
  1044. }
  1045. /*
  1046. * Calls e820__memory_setup_default() in essence to pick up the firmware/bootloader
  1047. * E820 map - with an optional platform quirk available for virtual platforms
  1048. * to override this method of boot environment processing:
  1049. */
  1050. void __init e820__memory_setup(void)
  1051. {
  1052. char *who;
  1053. /* This is a firmware interface ABI - make sure we don't break it: */
  1054. BUILD_BUG_ON(sizeof(struct boot_e820_entry) != 20);
  1055. who = x86_init.resources.memory_setup();
  1056. memcpy(e820_table_kexec, e820_table, sizeof(*e820_table_kexec));
  1057. memcpy(e820_table_firmware, e820_table, sizeof(*e820_table_firmware));
  1058. pr_info("BIOS-provided physical RAM map:\n");
  1059. e820__print_table(who);
  1060. }
  1061. void __init e820__memblock_setup(void)
  1062. {
  1063. int i;
  1064. u64 end;
  1065. /*
  1066. * The bootstrap memblock region count maximum is 128 entries
  1067. * (INIT_MEMBLOCK_REGIONS), but EFI might pass us more E820 entries
  1068. * than that - so allow memblock resizing.
  1069. *
  1070. * This is safe, because this call happens pretty late during x86 setup,
  1071. * so we know about reserved memory regions already. (This is important
  1072. * so that memblock resizing does no stomp over reserved areas.)
  1073. */
  1074. memblock_allow_resize();
  1075. for (i = 0; i < e820_table->nr_entries; i++) {
  1076. struct e820_entry *entry = &e820_table->entries[i];
  1077. end = entry->addr + entry->size;
  1078. if (end != (resource_size_t)end)
  1079. continue;
  1080. if (entry->type != E820_TYPE_RAM && entry->type != E820_TYPE_RESERVED_KERN)
  1081. continue;
  1082. memblock_add(entry->addr, entry->size);
  1083. }
  1084. /* Throw away partial pages: */
  1085. memblock_trim_memory(PAGE_SIZE);
  1086. memblock_dump_all();
  1087. }