e820.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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. #include <linux/suspend.h>
  22. #include <linux/firmware-map.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/page.h>
  25. #include <asm/e820.h>
  26. #include <asm/proto.h>
  27. #include <asm/setup.h>
  28. #include <asm/trampoline.h>
  29. /*
  30. * The e820 map is the map that gets modified e.g. with command line parameters
  31. * and that is also registered with modifications in the kernel resource tree
  32. * with the iomem_resource as parent.
  33. *
  34. * The e820_saved is directly saved after the BIOS-provided memory map is
  35. * copied. It doesn't get modified afterwards. It's registered for the
  36. * /sys/firmware/memmap interface.
  37. *
  38. * That memory map is not modified and is used as base for kexec. The kexec'd
  39. * kernel should get the same memory map as the firmware provides. Then the
  40. * user can e.g. boot the original kernel with mem=1G while still booting the
  41. * next kernel with full memory.
  42. */
  43. struct e820map e820;
  44. struct e820map e820_saved;
  45. /* For PCI or other memory-mapped resources */
  46. unsigned long pci_mem_start = 0xaeedbabe;
  47. #ifdef CONFIG_PCI
  48. EXPORT_SYMBOL(pci_mem_start);
  49. #endif
  50. /*
  51. * This function checks if any part of the range <start,end> is mapped
  52. * with type.
  53. */
  54. int
  55. e820_any_mapped(u64 start, u64 end, unsigned type)
  56. {
  57. int i;
  58. for (i = 0; i < e820.nr_map; i++) {
  59. struct e820entry *ei = &e820.map[i];
  60. if (type && ei->type != type)
  61. continue;
  62. if (ei->addr >= end || ei->addr + ei->size <= start)
  63. continue;
  64. return 1;
  65. }
  66. return 0;
  67. }
  68. EXPORT_SYMBOL_GPL(e820_any_mapped);
  69. /*
  70. * This function checks if the entire range <start,end> is mapped with type.
  71. *
  72. * Note: this function only works correct if the e820 table is sorted and
  73. * not-overlapping, which is the case
  74. */
  75. int __init e820_all_mapped(u64 start, u64 end, unsigned type)
  76. {
  77. int i;
  78. for (i = 0; i < e820.nr_map; i++) {
  79. struct e820entry *ei = &e820.map[i];
  80. if (type && ei->type != type)
  81. continue;
  82. /* is the region (part) in overlap with the current region ?*/
  83. if (ei->addr >= end || ei->addr + ei->size <= start)
  84. continue;
  85. /* if the region is at the beginning of <start,end> we move
  86. * start to the end of the region since it's ok until there
  87. */
  88. if (ei->addr <= start)
  89. start = ei->addr + ei->size;
  90. /*
  91. * if start is now at or beyond end, we're done, full
  92. * coverage
  93. */
  94. if (start >= end)
  95. return 1;
  96. }
  97. return 0;
  98. }
  99. /*
  100. * Add a memory region to the kernel e820 map.
  101. */
  102. void __init e820_add_region(u64 start, u64 size, int type)
  103. {
  104. int x = e820.nr_map;
  105. if (x == ARRAY_SIZE(e820.map)) {
  106. printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
  107. return;
  108. }
  109. e820.map[x].addr = start;
  110. e820.map[x].size = size;
  111. e820.map[x].type = type;
  112. e820.nr_map++;
  113. }
  114. void __init e820_print_map(char *who)
  115. {
  116. int i;
  117. for (i = 0; i < e820.nr_map; i++) {
  118. printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
  119. (unsigned long long) e820.map[i].addr,
  120. (unsigned long long)
  121. (e820.map[i].addr + e820.map[i].size));
  122. switch (e820.map[i].type) {
  123. case E820_RAM:
  124. case E820_RESERVED_KERN:
  125. printk(KERN_CONT "(usable)\n");
  126. break;
  127. case E820_RESERVED:
  128. printk(KERN_CONT "(reserved)\n");
  129. break;
  130. case E820_ACPI:
  131. printk(KERN_CONT "(ACPI data)\n");
  132. break;
  133. case E820_NVS:
  134. printk(KERN_CONT "(ACPI NVS)\n");
  135. break;
  136. default:
  137. printk(KERN_CONT "type %u\n", e820.map[i].type);
  138. break;
  139. }
  140. }
  141. }
  142. /*
  143. * Sanitize the BIOS e820 map.
  144. *
  145. * Some e820 responses include overlapping entries. The following
  146. * replaces the original e820 map with a new one, removing overlaps,
  147. * and resolving conflicting memory types in favor of highest
  148. * numbered type.
  149. *
  150. * The input parameter biosmap points to an array of 'struct
  151. * e820entry' which on entry has elements in the range [0, *pnr_map)
  152. * valid, and which has space for up to max_nr_map entries.
  153. * On return, the resulting sanitized e820 map entries will be in
  154. * overwritten in the same location, starting at biosmap.
  155. *
  156. * The integer pointed to by pnr_map must be valid on entry (the
  157. * current number of valid entries located at biosmap) and will
  158. * be updated on return, with the new number of valid entries
  159. * (something no more than max_nr_map.)
  160. *
  161. * The return value from sanitize_e820_map() is zero if it
  162. * successfully 'sanitized' the map entries passed in, and is -1
  163. * if it did nothing, which can happen if either of (1) it was
  164. * only passed one map entry, or (2) any of the input map entries
  165. * were invalid (start + size < start, meaning that the size was
  166. * so big the described memory range wrapped around through zero.)
  167. *
  168. * Visually we're performing the following
  169. * (1,2,3,4 = memory types)...
  170. *
  171. * Sample memory map (w/overlaps):
  172. * ____22__________________
  173. * ______________________4_
  174. * ____1111________________
  175. * _44_____________________
  176. * 11111111________________
  177. * ____________________33__
  178. * ___________44___________
  179. * __________33333_________
  180. * ______________22________
  181. * ___________________2222_
  182. * _________111111111______
  183. * _____________________11_
  184. * _________________4______
  185. *
  186. * Sanitized equivalent (no overlap):
  187. * 1_______________________
  188. * _44_____________________
  189. * ___1____________________
  190. * ____22__________________
  191. * ______11________________
  192. * _________1______________
  193. * __________3_____________
  194. * ___________44___________
  195. * _____________33_________
  196. * _______________2________
  197. * ________________1_______
  198. * _________________4______
  199. * ___________________2____
  200. * ____________________33__
  201. * ______________________4_
  202. */
  203. int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map,
  204. int *pnr_map)
  205. {
  206. struct change_member {
  207. struct e820entry *pbios; /* pointer to original bios entry */
  208. unsigned long long addr; /* address for this change point */
  209. };
  210. static struct change_member change_point_list[2*E820_X_MAX] __initdata;
  211. static struct change_member *change_point[2*E820_X_MAX] __initdata;
  212. static struct e820entry *overlap_list[E820_X_MAX] __initdata;
  213. static struct e820entry new_bios[E820_X_MAX] __initdata;
  214. struct change_member *change_tmp;
  215. unsigned long current_type, last_type;
  216. unsigned long long last_addr;
  217. int chgidx, still_changing;
  218. int overlap_entries;
  219. int new_bios_entry;
  220. int old_nr, new_nr, chg_nr;
  221. int i;
  222. /* if there's only one memory region, don't bother */
  223. if (*pnr_map < 2)
  224. return -1;
  225. old_nr = *pnr_map;
  226. 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)
  230. return -1;
  231. /* create pointers for initial change-point information (for sorting) */
  232. for (i = 0; i < 2 * old_nr; i++)
  233. change_point[i] = &change_point_list[i];
  234. /* record all known change-points (starting and ending addresses),
  235. omitting those that are for empty memory regions */
  236. chgidx = 0;
  237. for (i = 0; i < old_nr; i++) {
  238. if (biosmap[i].size != 0) {
  239. change_point[chgidx]->addr = biosmap[i].addr;
  240. change_point[chgidx++]->pbios = &biosmap[i];
  241. change_point[chgidx]->addr = biosmap[i].addr +
  242. biosmap[i].size;
  243. change_point[chgidx++]->pbios = &biosmap[i];
  244. }
  245. }
  246. chg_nr = chgidx;
  247. /* sort change-point list by memory addresses (low -> high) */
  248. still_changing = 1;
  249. while (still_changing) {
  250. still_changing = 0;
  251. for (i = 1; i < chg_nr; i++) {
  252. unsigned long long curaddr, lastaddr;
  253. unsigned long long curpbaddr, lastpbaddr;
  254. curaddr = change_point[i]->addr;
  255. lastaddr = change_point[i - 1]->addr;
  256. curpbaddr = change_point[i]->pbios->addr;
  257. lastpbaddr = change_point[i - 1]->pbios->addr;
  258. /*
  259. * swap entries, when:
  260. *
  261. * curaddr > lastaddr or
  262. * curaddr == lastaddr and curaddr == curpbaddr and
  263. * lastaddr != lastpbaddr
  264. */
  265. if (curaddr < lastaddr ||
  266. (curaddr == lastaddr && curaddr == curpbaddr &&
  267. lastaddr != lastpbaddr)) {
  268. change_tmp = change_point[i];
  269. change_point[i] = change_point[i-1];
  270. change_point[i-1] = change_tmp;
  271. still_changing = 1;
  272. }
  273. }
  274. }
  275. /* create a new bios memory map, removing overlaps */
  276. overlap_entries = 0; /* number of entries in the overlap table */
  277. new_bios_entry = 0; /* index for creating new bios map entries */
  278. last_type = 0; /* start with undefined memory type */
  279. last_addr = 0; /* start with 0 as last starting address */
  280. /* loop through change-points, determining affect on the new bios map */
  281. for (chgidx = 0; chgidx < chg_nr; chgidx++) {
  282. /* keep track of all overlapping bios entries */
  283. if (change_point[chgidx]->addr ==
  284. change_point[chgidx]->pbios->addr) {
  285. /*
  286. * add map entry to overlap list (> 1 entry
  287. * implies an overlap)
  288. */
  289. overlap_list[overlap_entries++] =
  290. change_point[chgidx]->pbios;
  291. } else {
  292. /*
  293. * remove entry from list (order independent,
  294. * so swap with last)
  295. */
  296. for (i = 0; i < overlap_entries; i++) {
  297. if (overlap_list[i] ==
  298. change_point[chgidx]->pbios)
  299. overlap_list[i] =
  300. overlap_list[overlap_entries-1];
  301. }
  302. overlap_entries--;
  303. }
  304. /*
  305. * if there are overlapping entries, decide which
  306. * "type" to use (larger value takes precedence --
  307. * 1=usable, 2,3,4,4+=unusable)
  308. */
  309. current_type = 0;
  310. for (i = 0; i < overlap_entries; i++)
  311. if (overlap_list[i]->type > current_type)
  312. current_type = overlap_list[i]->type;
  313. /*
  314. * continue building up new bios map based on this
  315. * information
  316. */
  317. if (current_type != last_type) {
  318. if (last_type != 0) {
  319. new_bios[new_bios_entry].size =
  320. change_point[chgidx]->addr - last_addr;
  321. /*
  322. * move forward only if the new size
  323. * was non-zero
  324. */
  325. if (new_bios[new_bios_entry].size != 0)
  326. /*
  327. * no more space left for new
  328. * bios entries ?
  329. */
  330. if (++new_bios_entry >= max_nr_map)
  331. break;
  332. }
  333. if (current_type != 0) {
  334. new_bios[new_bios_entry].addr =
  335. change_point[chgidx]->addr;
  336. new_bios[new_bios_entry].type = current_type;
  337. last_addr = change_point[chgidx]->addr;
  338. }
  339. last_type = current_type;
  340. }
  341. }
  342. /* retain count for new bios entries */
  343. new_nr = new_bios_entry;
  344. /* copy new bios mapping into original location */
  345. memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
  346. *pnr_map = new_nr;
  347. return 0;
  348. }
  349. static int __init __append_e820_map(struct e820entry *biosmap, int nr_map)
  350. {
  351. while (nr_map) {
  352. u64 start = biosmap->addr;
  353. u64 size = biosmap->size;
  354. u64 end = start + size;
  355. u32 type = biosmap->type;
  356. /* Overflow in 64 bits? Ignore the memory map. */
  357. if (start > end)
  358. return -1;
  359. e820_add_region(start, size, type);
  360. biosmap++;
  361. nr_map--;
  362. }
  363. return 0;
  364. }
  365. /*
  366. * Copy the BIOS e820 map into a safe place.
  367. *
  368. * Sanity-check it while we're at it..
  369. *
  370. * If we're lucky and live on a modern system, the setup code
  371. * will have given us a memory map that we can use to properly
  372. * set up memory. If we aren't, we'll fake a memory map.
  373. */
  374. static int __init append_e820_map(struct e820entry *biosmap, int nr_map)
  375. {
  376. /* Only one memory region (or negative)? Ignore it */
  377. if (nr_map < 2)
  378. return -1;
  379. return __append_e820_map(biosmap, nr_map);
  380. }
  381. u64 __init e820_update_range(u64 start, u64 size, unsigned old_type,
  382. unsigned new_type)
  383. {
  384. int i;
  385. u64 real_updated_size = 0;
  386. BUG_ON(old_type == new_type);
  387. if (size > (ULLONG_MAX - start))
  388. size = ULLONG_MAX - start;
  389. for (i = 0; i < e820.nr_map; i++) {
  390. struct e820entry *ei = &e820.map[i];
  391. u64 final_start, final_end;
  392. if (ei->type != old_type)
  393. continue;
  394. /* totally covered? */
  395. if (ei->addr >= start &&
  396. (ei->addr + ei->size) <= (start + size)) {
  397. ei->type = new_type;
  398. real_updated_size += ei->size;
  399. continue;
  400. }
  401. /* partially covered */
  402. final_start = max(start, ei->addr);
  403. final_end = min(start + size, ei->addr + ei->size);
  404. if (final_start >= final_end)
  405. continue;
  406. e820_add_region(final_start, final_end - final_start,
  407. new_type);
  408. real_updated_size += final_end - final_start;
  409. ei->size -= final_end - final_start;
  410. if (ei->addr < final_start)
  411. continue;
  412. ei->addr = final_end;
  413. }
  414. return real_updated_size;
  415. }
  416. /* make e820 not cover the range */
  417. u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type,
  418. int checktype)
  419. {
  420. int i;
  421. u64 real_removed_size = 0;
  422. if (size > (ULLONG_MAX - start))
  423. size = ULLONG_MAX - start;
  424. for (i = 0; i < e820.nr_map; i++) {
  425. struct e820entry *ei = &e820.map[i];
  426. u64 final_start, final_end;
  427. if (checktype && ei->type != old_type)
  428. continue;
  429. /* totally covered? */
  430. if (ei->addr >= start &&
  431. (ei->addr + ei->size) <= (start + size)) {
  432. real_removed_size += ei->size;
  433. memset(ei, 0, sizeof(struct e820entry));
  434. continue;
  435. }
  436. /* partially covered */
  437. final_start = max(start, ei->addr);
  438. final_end = min(start + size, ei->addr + ei->size);
  439. if (final_start >= final_end)
  440. continue;
  441. real_removed_size += final_end - final_start;
  442. ei->size -= final_end - final_start;
  443. if (ei->addr < final_start)
  444. continue;
  445. ei->addr = final_end;
  446. }
  447. return real_removed_size;
  448. }
  449. void __init update_e820(void)
  450. {
  451. int nr_map;
  452. nr_map = e820.nr_map;
  453. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map))
  454. return;
  455. e820.nr_map = nr_map;
  456. printk(KERN_INFO "modified physical RAM map:\n");
  457. e820_print_map("modified");
  458. }
  459. #define MAX_GAP_END 0x100000000ull
  460. /*
  461. * Search for a gap in the e820 memory space from start_addr to end_addr.
  462. */
  463. __init int e820_search_gap(unsigned long *gapstart, unsigned long *gapsize,
  464. unsigned long start_addr, unsigned long long end_addr)
  465. {
  466. unsigned long long last;
  467. int i = e820.nr_map;
  468. int found = 0;
  469. last = (end_addr && end_addr < MAX_GAP_END) ? end_addr : MAX_GAP_END;
  470. while (--i >= 0) {
  471. unsigned long long start = e820.map[i].addr;
  472. unsigned long long end = start + e820.map[i].size;
  473. if (end < start_addr)
  474. continue;
  475. /*
  476. * Since "last" is at most 4GB, we know we'll
  477. * fit in 32 bits if this condition is true
  478. */
  479. if (last > end) {
  480. unsigned long gap = last - end;
  481. if (gap >= *gapsize) {
  482. *gapsize = gap;
  483. *gapstart = end;
  484. found = 1;
  485. }
  486. }
  487. if (start < last)
  488. last = start;
  489. }
  490. return found;
  491. }
  492. /*
  493. * Search for the biggest gap in the low 32 bits of the e820
  494. * memory space. We pass this space to PCI to assign MMIO resources
  495. * for hotplug or unconfigured devices in.
  496. * Hopefully the BIOS let enough space left.
  497. */
  498. __init void e820_setup_gap(void)
  499. {
  500. unsigned long gapstart, gapsize, round;
  501. int found;
  502. gapstart = 0x10000000;
  503. gapsize = 0x400000;
  504. found = e820_search_gap(&gapstart, &gapsize, 0, MAX_GAP_END);
  505. #ifdef CONFIG_X86_64
  506. if (!found) {
  507. gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024;
  508. printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
  509. "address range\n"
  510. KERN_ERR "PCI: Unassigned devices with 32bit resource "
  511. "registers may break!\n");
  512. }
  513. #endif
  514. /*
  515. * See how much we want to round up: start off with
  516. * rounding to the next 1MB area.
  517. */
  518. round = 0x100000;
  519. while ((gapsize >> 4) > round)
  520. round += round;
  521. /* Fun with two's complement */
  522. pci_mem_start = (gapstart + round) & -round;
  523. printk(KERN_INFO
  524. "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
  525. pci_mem_start, gapstart, gapsize);
  526. }
  527. /**
  528. * Because of the size limitation of struct boot_params, only first
  529. * 128 E820 memory entries are passed to kernel via
  530. * boot_params.e820_map, others are passed via SETUP_E820_EXT node of
  531. * linked list of struct setup_data, which is parsed here.
  532. */
  533. void __init parse_e820_ext(struct setup_data *sdata, unsigned long pa_data)
  534. {
  535. u32 map_len;
  536. int entries;
  537. struct e820entry *extmap;
  538. entries = sdata->len / sizeof(struct e820entry);
  539. map_len = sdata->len + sizeof(struct setup_data);
  540. if (map_len > PAGE_SIZE)
  541. sdata = early_ioremap(pa_data, map_len);
  542. extmap = (struct e820entry *)(sdata->data);
  543. __append_e820_map(extmap, entries);
  544. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  545. if (map_len > PAGE_SIZE)
  546. early_iounmap(sdata, map_len);
  547. printk(KERN_INFO "extended physical RAM map:\n");
  548. e820_print_map("extended");
  549. }
  550. #if defined(CONFIG_X86_64) || \
  551. (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION))
  552. /**
  553. * Find the ranges of physical addresses that do not correspond to
  554. * e820 RAM areas and mark the corresponding pages as nosave for
  555. * hibernation (32 bit) or software suspend and suspend to RAM (64 bit).
  556. *
  557. * This function requires the e820 map to be sorted and without any
  558. * overlapping entries and assumes the first e820 area to be RAM.
  559. */
  560. void __init e820_mark_nosave_regions(unsigned long limit_pfn)
  561. {
  562. int i;
  563. unsigned long pfn;
  564. pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
  565. for (i = 1; i < e820.nr_map; i++) {
  566. struct e820entry *ei = &e820.map[i];
  567. if (pfn < PFN_UP(ei->addr))
  568. register_nosave_region(pfn, PFN_UP(ei->addr));
  569. pfn = PFN_DOWN(ei->addr + ei->size);
  570. if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
  571. register_nosave_region(PFN_UP(ei->addr), pfn);
  572. if (pfn >= limit_pfn)
  573. break;
  574. }
  575. }
  576. #endif
  577. /*
  578. * Early reserved memory areas.
  579. */
  580. #define MAX_EARLY_RES 20
  581. struct early_res {
  582. u64 start, end;
  583. char name[16];
  584. char overlap_ok;
  585. };
  586. static struct early_res early_res[MAX_EARLY_RES] __initdata = {
  587. { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
  588. #if defined(CONFIG_X86_64) && defined(CONFIG_X86_TRAMPOLINE)
  589. { TRAMPOLINE_BASE, TRAMPOLINE_BASE + 2 * PAGE_SIZE, "TRAMPOLINE" },
  590. #endif
  591. #if defined(CONFIG_X86_32) && defined(CONFIG_SMP)
  592. /*
  593. * But first pinch a few for the stack/trampoline stuff
  594. * FIXME: Don't need the extra page at 4K, but need to fix
  595. * trampoline before removing it. (see the GDT stuff)
  596. */
  597. { PAGE_SIZE, PAGE_SIZE + PAGE_SIZE, "EX TRAMPOLINE" },
  598. /*
  599. * Has to be in very low memory so we can execute
  600. * real-mode AP code.
  601. */
  602. { TRAMPOLINE_BASE, TRAMPOLINE_BASE + PAGE_SIZE, "TRAMPOLINE" },
  603. #endif
  604. {}
  605. };
  606. static int __init find_overlapped_early(u64 start, u64 end)
  607. {
  608. int i;
  609. struct early_res *r;
  610. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  611. r = &early_res[i];
  612. if (end > r->start && start < r->end)
  613. break;
  614. }
  615. return i;
  616. }
  617. /*
  618. * Drop the i-th range from the early reservation map,
  619. * by copying any higher ranges down one over it, and
  620. * clearing what had been the last slot.
  621. */
  622. static void __init drop_range(int i)
  623. {
  624. int j;
  625. for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++)
  626. ;
  627. memmove(&early_res[i], &early_res[i + 1],
  628. (j - 1 - i) * sizeof(struct early_res));
  629. early_res[j - 1].end = 0;
  630. }
  631. /*
  632. * Split any existing ranges that:
  633. * 1) are marked 'overlap_ok', and
  634. * 2) overlap with the stated range [start, end)
  635. * into whatever portion (if any) of the existing range is entirely
  636. * below or entirely above the stated range. Drop the portion
  637. * of the existing range that overlaps with the stated range,
  638. * which will allow the caller of this routine to then add that
  639. * stated range without conflicting with any existing range.
  640. */
  641. static void __init drop_overlaps_that_are_ok(u64 start, u64 end)
  642. {
  643. int i;
  644. struct early_res *r;
  645. u64 lower_start, lower_end;
  646. u64 upper_start, upper_end;
  647. char name[16];
  648. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  649. r = &early_res[i];
  650. /* Continue past non-overlapping ranges */
  651. if (end <= r->start || start >= r->end)
  652. continue;
  653. /*
  654. * Leave non-ok overlaps as is; let caller
  655. * panic "Overlapping early reservations"
  656. * when it hits this overlap.
  657. */
  658. if (!r->overlap_ok)
  659. return;
  660. /*
  661. * We have an ok overlap. We will drop it from the early
  662. * reservation map, and add back in any non-overlapping
  663. * portions (lower or upper) as separate, overlap_ok,
  664. * non-overlapping ranges.
  665. */
  666. /* 1. Note any non-overlapping (lower or upper) ranges. */
  667. strncpy(name, r->name, sizeof(name) - 1);
  668. lower_start = lower_end = 0;
  669. upper_start = upper_end = 0;
  670. if (r->start < start) {
  671. lower_start = r->start;
  672. lower_end = start;
  673. }
  674. if (r->end > end) {
  675. upper_start = end;
  676. upper_end = r->end;
  677. }
  678. /* 2. Drop the original ok overlapping range */
  679. drop_range(i);
  680. i--; /* resume for-loop on copied down entry */
  681. /* 3. Add back in any non-overlapping ranges. */
  682. if (lower_end)
  683. reserve_early_overlap_ok(lower_start, lower_end, name);
  684. if (upper_end)
  685. reserve_early_overlap_ok(upper_start, upper_end, name);
  686. }
  687. }
  688. static void __init __reserve_early(u64 start, u64 end, char *name,
  689. int overlap_ok)
  690. {
  691. int i;
  692. struct early_res *r;
  693. i = find_overlapped_early(start, end);
  694. if (i >= MAX_EARLY_RES)
  695. panic("Too many early reservations");
  696. r = &early_res[i];
  697. if (r->end)
  698. panic("Overlapping early reservations "
  699. "%llx-%llx %s to %llx-%llx %s\n",
  700. start, end - 1, name?name:"", r->start,
  701. r->end - 1, r->name);
  702. r->start = start;
  703. r->end = end;
  704. r->overlap_ok = overlap_ok;
  705. if (name)
  706. strncpy(r->name, name, sizeof(r->name) - 1);
  707. }
  708. /*
  709. * A few early reservtations come here.
  710. *
  711. * The 'overlap_ok' in the name of this routine does -not- mean it
  712. * is ok for these reservations to overlap an earlier reservation.
  713. * Rather it means that it is ok for subsequent reservations to
  714. * overlap this one.
  715. *
  716. * Use this entry point to reserve early ranges when you are doing
  717. * so out of "Paranoia", reserving perhaps more memory than you need,
  718. * just in case, and don't mind a subsequent overlapping reservation
  719. * that is known to be needed.
  720. *
  721. * The drop_overlaps_that_are_ok() call here isn't really needed.
  722. * It would be needed if we had two colliding 'overlap_ok'
  723. * reservations, so that the second such would not panic on the
  724. * overlap with the first. We don't have any such as of this
  725. * writing, but might as well tolerate such if it happens in
  726. * the future.
  727. */
  728. void __init reserve_early_overlap_ok(u64 start, u64 end, char *name)
  729. {
  730. drop_overlaps_that_are_ok(start, end);
  731. __reserve_early(start, end, name, 1);
  732. }
  733. /*
  734. * Most early reservations come here.
  735. *
  736. * We first have drop_overlaps_that_are_ok() drop any pre-existing
  737. * 'overlap_ok' ranges, so that we can then reserve this memory
  738. * range without risk of panic'ing on an overlapping overlap_ok
  739. * early reservation.
  740. */
  741. void __init reserve_early(u64 start, u64 end, char *name)
  742. {
  743. drop_overlaps_that_are_ok(start, end);
  744. __reserve_early(start, end, name, 0);
  745. }
  746. void __init free_early(u64 start, u64 end)
  747. {
  748. struct early_res *r;
  749. int i;
  750. i = find_overlapped_early(start, end);
  751. r = &early_res[i];
  752. if (i >= MAX_EARLY_RES || r->end != end || r->start != start)
  753. panic("free_early on not reserved area: %llx-%llx!",
  754. start, end - 1);
  755. drop_range(i);
  756. }
  757. void __init early_res_to_bootmem(u64 start, u64 end)
  758. {
  759. int i, count;
  760. u64 final_start, final_end;
  761. count = 0;
  762. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++)
  763. count++;
  764. printk(KERN_INFO "(%d early reservations) ==> bootmem\n", count);
  765. for (i = 0; i < count; i++) {
  766. struct early_res *r = &early_res[i];
  767. printk(KERN_INFO " #%d [%010llx - %010llx] %16s", i,
  768. r->start, r->end, r->name);
  769. final_start = max(start, r->start);
  770. final_end = min(end, r->end);
  771. if (final_start >= final_end) {
  772. printk(KERN_CONT "\n");
  773. continue;
  774. }
  775. printk(KERN_CONT " ==> [%010llx - %010llx]\n",
  776. final_start, final_end);
  777. reserve_bootmem_generic(final_start, final_end - final_start,
  778. BOOTMEM_DEFAULT);
  779. }
  780. }
  781. /* Check for already reserved areas */
  782. static inline int __init bad_addr(u64 *addrp, u64 size, u64 align)
  783. {
  784. int i;
  785. u64 addr = *addrp;
  786. int changed = 0;
  787. struct early_res *r;
  788. again:
  789. i = find_overlapped_early(addr, addr + size);
  790. r = &early_res[i];
  791. if (i < MAX_EARLY_RES && r->end) {
  792. *addrp = addr = round_up(r->end, align);
  793. changed = 1;
  794. goto again;
  795. }
  796. return changed;
  797. }
  798. /* Check for already reserved areas */
  799. static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align)
  800. {
  801. int i;
  802. u64 addr = *addrp, last;
  803. u64 size = *sizep;
  804. int changed = 0;
  805. again:
  806. last = addr + size;
  807. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  808. struct early_res *r = &early_res[i];
  809. if (last > r->start && addr < r->start) {
  810. size = r->start - addr;
  811. changed = 1;
  812. goto again;
  813. }
  814. if (last > r->end && addr < r->end) {
  815. addr = round_up(r->end, align);
  816. size = last - addr;
  817. changed = 1;
  818. goto again;
  819. }
  820. if (last <= r->end && addr >= r->start) {
  821. (*sizep)++;
  822. return 0;
  823. }
  824. }
  825. if (changed) {
  826. *addrp = addr;
  827. *sizep = size;
  828. }
  829. return changed;
  830. }
  831. /*
  832. * Find a free area with specified alignment in a specific range.
  833. */
  834. u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align)
  835. {
  836. int i;
  837. for (i = 0; i < e820.nr_map; i++) {
  838. struct e820entry *ei = &e820.map[i];
  839. u64 addr, last;
  840. u64 ei_last;
  841. if (ei->type != E820_RAM)
  842. continue;
  843. addr = round_up(ei->addr, align);
  844. ei_last = ei->addr + ei->size;
  845. if (addr < start)
  846. addr = round_up(start, align);
  847. if (addr >= ei_last)
  848. continue;
  849. while (bad_addr(&addr, size, align) && addr+size <= ei_last)
  850. ;
  851. last = addr + size;
  852. if (last > ei_last)
  853. continue;
  854. if (last > end)
  855. continue;
  856. return addr;
  857. }
  858. return -1ULL;
  859. }
  860. /*
  861. * Find next free range after *start
  862. */
  863. u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align)
  864. {
  865. int i;
  866. for (i = 0; i < e820.nr_map; i++) {
  867. struct e820entry *ei = &e820.map[i];
  868. u64 addr, last;
  869. u64 ei_last;
  870. if (ei->type != E820_RAM)
  871. continue;
  872. addr = round_up(ei->addr, align);
  873. ei_last = ei->addr + ei->size;
  874. if (addr < start)
  875. addr = round_up(start, align);
  876. if (addr >= ei_last)
  877. continue;
  878. *sizep = ei_last - addr;
  879. while (bad_addr_size(&addr, sizep, align) &&
  880. addr + *sizep <= ei_last)
  881. ;
  882. last = addr + *sizep;
  883. if (last > ei_last)
  884. continue;
  885. return addr;
  886. }
  887. return -1UL;
  888. }
  889. /*
  890. * pre allocated 4k and reserved it in e820
  891. */
  892. u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align)
  893. {
  894. u64 size = 0;
  895. u64 addr;
  896. u64 start;
  897. start = startt;
  898. while (size < sizet)
  899. start = find_e820_area_size(start, &size, align);
  900. if (size < sizet)
  901. return 0;
  902. addr = round_down(start + size - sizet, align);
  903. e820_update_range(addr, sizet, E820_RAM, E820_RESERVED);
  904. printk(KERN_INFO "update e820 for early_reserve_e820\n");
  905. update_e820();
  906. return addr;
  907. }
  908. #ifdef CONFIG_X86_32
  909. # ifdef CONFIG_X86_PAE
  910. # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
  911. # else
  912. # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
  913. # endif
  914. #else /* CONFIG_X86_32 */
  915. # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
  916. #endif
  917. /*
  918. * Last pfn which the user wants to use.
  919. */
  920. unsigned long __initdata end_user_pfn = MAX_ARCH_PFN;
  921. /*
  922. * Find the highest page frame number we have available
  923. */
  924. unsigned long __init e820_end_of_ram(void)
  925. {
  926. unsigned long last_pfn;
  927. unsigned long max_arch_pfn = MAX_ARCH_PFN;
  928. last_pfn = find_max_pfn_with_active_regions();
  929. if (last_pfn > max_arch_pfn)
  930. last_pfn = max_arch_pfn;
  931. if (last_pfn > end_user_pfn)
  932. last_pfn = end_user_pfn;
  933. printk(KERN_INFO "last_pfn = %#lx max_arch_pfn = %#lx\n",
  934. last_pfn, max_arch_pfn);
  935. return last_pfn;
  936. }
  937. /*
  938. * Finds an active region in the address range from start_pfn to last_pfn and
  939. * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
  940. */
  941. int __init e820_find_active_region(const struct e820entry *ei,
  942. unsigned long start_pfn,
  943. unsigned long last_pfn,
  944. unsigned long *ei_startpfn,
  945. unsigned long *ei_endpfn)
  946. {
  947. u64 align = PAGE_SIZE;
  948. *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT;
  949. *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT;
  950. /* Skip map entries smaller than a page */
  951. if (*ei_startpfn >= *ei_endpfn)
  952. return 0;
  953. /* Skip if map is outside the node */
  954. if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
  955. *ei_startpfn >= last_pfn)
  956. return 0;
  957. /* Check for overlaps */
  958. if (*ei_startpfn < start_pfn)
  959. *ei_startpfn = start_pfn;
  960. if (*ei_endpfn > last_pfn)
  961. *ei_endpfn = last_pfn;
  962. /* Obey end_user_pfn to save on memmap */
  963. if (*ei_startpfn >= end_user_pfn)
  964. return 0;
  965. if (*ei_endpfn > end_user_pfn)
  966. *ei_endpfn = end_user_pfn;
  967. return 1;
  968. }
  969. /* Walk the e820 map and register active regions within a node */
  970. void __init e820_register_active_regions(int nid, unsigned long start_pfn,
  971. unsigned long last_pfn)
  972. {
  973. unsigned long ei_startpfn;
  974. unsigned long ei_endpfn;
  975. int i;
  976. for (i = 0; i < e820.nr_map; i++)
  977. if (e820_find_active_region(&e820.map[i],
  978. start_pfn, last_pfn,
  979. &ei_startpfn, &ei_endpfn))
  980. add_active_range(nid, ei_startpfn, ei_endpfn);
  981. }
  982. /*
  983. * Find the hole size (in bytes) in the memory range.
  984. * @start: starting address of the memory range to scan
  985. * @end: ending address of the memory range to scan
  986. */
  987. u64 __init e820_hole_size(u64 start, u64 end)
  988. {
  989. unsigned long start_pfn = start >> PAGE_SHIFT;
  990. unsigned long last_pfn = end >> PAGE_SHIFT;
  991. unsigned long ei_startpfn, ei_endpfn, ram = 0;
  992. int i;
  993. for (i = 0; i < e820.nr_map; i++) {
  994. if (e820_find_active_region(&e820.map[i],
  995. start_pfn, last_pfn,
  996. &ei_startpfn, &ei_endpfn))
  997. ram += ei_endpfn - ei_startpfn;
  998. }
  999. return end - start - ((u64)ram << PAGE_SHIFT);
  1000. }
  1001. static void early_panic(char *msg)
  1002. {
  1003. early_printk(msg);
  1004. panic(msg);
  1005. }
  1006. /* "mem=nopentium" disables the 4MB page tables. */
  1007. static int __init parse_memopt(char *p)
  1008. {
  1009. u64 mem_size;
  1010. if (!p)
  1011. return -EINVAL;
  1012. #ifdef CONFIG_X86_32
  1013. if (!strcmp(p, "nopentium")) {
  1014. setup_clear_cpu_cap(X86_FEATURE_PSE);
  1015. return 0;
  1016. }
  1017. #endif
  1018. mem_size = memparse(p, &p);
  1019. end_user_pfn = mem_size>>PAGE_SHIFT;
  1020. e820_update_range(mem_size, ULLONG_MAX - mem_size,
  1021. E820_RAM, E820_RESERVED);
  1022. return 0;
  1023. }
  1024. early_param("mem", parse_memopt);
  1025. static int userdef __initdata;
  1026. static int __init parse_memmap_opt(char *p)
  1027. {
  1028. char *oldp;
  1029. u64 start_at, mem_size;
  1030. if (!strcmp(p, "exactmap")) {
  1031. #ifdef CONFIG_CRASH_DUMP
  1032. /*
  1033. * If we are doing a crash dump, we still need to know
  1034. * the real mem size before original memory map is
  1035. * reset.
  1036. */
  1037. e820_register_active_regions(0, 0, -1UL);
  1038. saved_max_pfn = e820_end_of_ram();
  1039. remove_all_active_ranges();
  1040. #endif
  1041. e820.nr_map = 0;
  1042. userdef = 1;
  1043. return 0;
  1044. }
  1045. oldp = p;
  1046. mem_size = memparse(p, &p);
  1047. if (p == oldp)
  1048. return -EINVAL;
  1049. userdef = 1;
  1050. if (*p == '@') {
  1051. start_at = memparse(p+1, &p);
  1052. e820_add_region(start_at, mem_size, E820_RAM);
  1053. } else if (*p == '#') {
  1054. start_at = memparse(p+1, &p);
  1055. e820_add_region(start_at, mem_size, E820_ACPI);
  1056. } else if (*p == '$') {
  1057. start_at = memparse(p+1, &p);
  1058. e820_add_region(start_at, mem_size, E820_RESERVED);
  1059. } else {
  1060. end_user_pfn = (mem_size >> PAGE_SHIFT);
  1061. e820_update_range(mem_size, ULLONG_MAX - mem_size,
  1062. E820_RAM, E820_RESERVED);
  1063. }
  1064. return *p == '\0' ? 0 : -EINVAL;
  1065. }
  1066. early_param("memmap", parse_memmap_opt);
  1067. void __init finish_e820_parsing(void)
  1068. {
  1069. if (userdef) {
  1070. int nr = e820.nr_map;
  1071. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
  1072. early_panic("Invalid user supplied memory map");
  1073. e820.nr_map = nr;
  1074. printk(KERN_INFO "user-defined physical RAM map:\n");
  1075. e820_print_map("user");
  1076. }
  1077. }
  1078. static inline const char *e820_type_to_string(int e820_type)
  1079. {
  1080. switch (e820_type) {
  1081. case E820_RESERVED_KERN:
  1082. case E820_RAM: return "System RAM";
  1083. case E820_ACPI: return "ACPI Tables";
  1084. case E820_NVS: return "ACPI Non-volatile Storage";
  1085. default: return "reserved";
  1086. }
  1087. }
  1088. /*
  1089. * Mark e820 reserved areas as busy for the resource manager.
  1090. */
  1091. void __init e820_reserve_resources(void)
  1092. {
  1093. int i;
  1094. struct resource *res;
  1095. u64 end;
  1096. res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
  1097. for (i = 0; i < e820.nr_map; i++) {
  1098. end = e820.map[i].addr + e820.map[i].size - 1;
  1099. #ifndef CONFIG_RESOURCES_64BIT
  1100. if (end > 0x100000000ULL) {
  1101. res++;
  1102. continue;
  1103. }
  1104. #endif
  1105. res->name = e820_type_to_string(e820.map[i].type);
  1106. res->start = e820.map[i].addr;
  1107. res->end = end;
  1108. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  1109. insert_resource(&iomem_resource, res);
  1110. res++;
  1111. }
  1112. for (i = 0; i < e820_saved.nr_map; i++) {
  1113. struct e820entry *entry = &e820_saved.map[i];
  1114. firmware_map_add_early(entry->addr,
  1115. entry->addr + entry->size - 1,
  1116. e820_type_to_string(entry->type));
  1117. }
  1118. }
  1119. char *__init default_machine_specific_memory_setup(void)
  1120. {
  1121. char *who = "BIOS-e820";
  1122. int new_nr;
  1123. /*
  1124. * Try to copy the BIOS-supplied E820-map.
  1125. *
  1126. * Otherwise fake a memory map; one section from 0k->640k,
  1127. * the next section from 1mb->appropriate_mem_k
  1128. */
  1129. new_nr = boot_params.e820_entries;
  1130. sanitize_e820_map(boot_params.e820_map,
  1131. ARRAY_SIZE(boot_params.e820_map),
  1132. &new_nr);
  1133. boot_params.e820_entries = new_nr;
  1134. if (append_e820_map(boot_params.e820_map, boot_params.e820_entries)
  1135. < 0) {
  1136. u64 mem_size;
  1137. /* compare results from other methods and take the greater */
  1138. if (boot_params.alt_mem_k
  1139. < boot_params.screen_info.ext_mem_k) {
  1140. mem_size = boot_params.screen_info.ext_mem_k;
  1141. who = "BIOS-88";
  1142. } else {
  1143. mem_size = boot_params.alt_mem_k;
  1144. who = "BIOS-e801";
  1145. }
  1146. e820.nr_map = 0;
  1147. e820_add_region(0, LOWMEMSIZE(), E820_RAM);
  1148. e820_add_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
  1149. }
  1150. /* In case someone cares... */
  1151. return who;
  1152. }
  1153. char *__init __attribute__((weak)) machine_specific_memory_setup(void)
  1154. {
  1155. return default_machine_specific_memory_setup();
  1156. }
  1157. /* Overridden in paravirt.c if CONFIG_PARAVIRT */
  1158. char * __init __attribute__((weak)) memory_setup(void)
  1159. {
  1160. return machine_specific_memory_setup();
  1161. }
  1162. void __init setup_memory_map(void)
  1163. {
  1164. char *who;
  1165. who = memory_setup();
  1166. memcpy(&e820_saved, &e820, sizeof(struct e820map));
  1167. printk(KERN_INFO "BIOS-provided physical RAM map:\n");
  1168. e820_print_map(who);
  1169. }
  1170. #ifdef CONFIG_X86_64
  1171. int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
  1172. {
  1173. int i;
  1174. if (slot < 0 || slot >= e820.nr_map)
  1175. return -1;
  1176. for (i = slot; i < e820.nr_map; i++) {
  1177. if (e820.map[i].type != E820_RAM)
  1178. continue;
  1179. break;
  1180. }
  1181. if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
  1182. return -1;
  1183. *addr = e820.map[i].addr;
  1184. *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
  1185. max_pfn << PAGE_SHIFT) - *addr;
  1186. return i + 1;
  1187. }
  1188. #endif