e820.c 32 KB

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