e820.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  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. for (i = 0; i < e820.nr_map; i++) {
  371. struct e820entry *ei = &e820.map[i];
  372. u64 final_start, final_end;
  373. if (ei->type != old_type)
  374. continue;
  375. /* totally covered? */
  376. if (ei->addr >= start &&
  377. (ei->addr + ei->size) <= (start + size)) {
  378. ei->type = new_type;
  379. real_updated_size += ei->size;
  380. continue;
  381. }
  382. /* partially covered */
  383. final_start = max(start, ei->addr);
  384. final_end = min(start + size, ei->addr + ei->size);
  385. if (final_start >= final_end)
  386. continue;
  387. e820_add_region(final_start, final_end - final_start,
  388. new_type);
  389. real_updated_size += final_end - final_start;
  390. }
  391. return real_updated_size;
  392. }
  393. /* make e820 not cover the range */
  394. u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type,
  395. int checktype)
  396. {
  397. int i;
  398. u64 real_removed_size = 0;
  399. for (i = 0; i < e820.nr_map; i++) {
  400. struct e820entry *ei = &e820.map[i];
  401. u64 final_start, final_end;
  402. if (checktype && ei->type != old_type)
  403. continue;
  404. /* totally covered? */
  405. if (ei->addr >= start &&
  406. (ei->addr + ei->size) <= (start + size)) {
  407. real_removed_size += ei->size;
  408. memset(ei, 0, sizeof(struct e820entry));
  409. continue;
  410. }
  411. /* partially covered */
  412. final_start = max(start, ei->addr);
  413. final_end = min(start + size, ei->addr + ei->size);
  414. if (final_start >= final_end)
  415. continue;
  416. real_removed_size += final_end - final_start;
  417. ei->size -= final_end - final_start;
  418. if (ei->addr < final_start)
  419. continue;
  420. ei->addr = final_end;
  421. }
  422. return real_removed_size;
  423. }
  424. void __init update_e820(void)
  425. {
  426. int nr_map;
  427. nr_map = e820.nr_map;
  428. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map))
  429. return;
  430. e820.nr_map = nr_map;
  431. printk(KERN_INFO "modified physical RAM map:\n");
  432. e820_print_map("modified");
  433. }
  434. /*
  435. * Search for the biggest gap in the low 32 bits of the e820
  436. * memory space. We pass this space to PCI to assign MMIO resources
  437. * for hotplug or unconfigured devices in.
  438. * Hopefully the BIOS let enough space left.
  439. */
  440. __init void e820_setup_gap(void)
  441. {
  442. unsigned long gapstart, gapsize, round;
  443. unsigned long long last;
  444. int i;
  445. int found = 0;
  446. last = 0x100000000ull;
  447. gapstart = 0x10000000;
  448. gapsize = 0x400000;
  449. i = e820.nr_map;
  450. while (--i >= 0) {
  451. unsigned long long start = e820.map[i].addr;
  452. unsigned long long end = start + e820.map[i].size;
  453. /*
  454. * Since "last" is at most 4GB, we know we'll
  455. * fit in 32 bits if this condition is true
  456. */
  457. if (last > end) {
  458. unsigned long gap = last - end;
  459. if (gap > gapsize) {
  460. gapsize = gap;
  461. gapstart = end;
  462. found = 1;
  463. }
  464. }
  465. if (start < last)
  466. last = start;
  467. }
  468. #ifdef CONFIG_X86_64
  469. if (!found) {
  470. gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
  471. printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
  472. "address range\n"
  473. KERN_ERR "PCI: Unassigned devices with 32bit resource "
  474. "registers may break!\n");
  475. }
  476. #endif
  477. /*
  478. * See how much we want to round up: start off with
  479. * rounding to the next 1MB area.
  480. */
  481. round = 0x100000;
  482. while ((gapsize >> 4) > round)
  483. round += round;
  484. /* Fun with two's complement */
  485. pci_mem_start = (gapstart + round) & -round;
  486. printk(KERN_INFO
  487. "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
  488. pci_mem_start, gapstart, gapsize);
  489. }
  490. /**
  491. * Because of the size limitation of struct boot_params, only first
  492. * 128 E820 memory entries are passed to kernel via
  493. * boot_params.e820_map, others are passed via SETUP_E820_EXT node of
  494. * linked list of struct setup_data, which is parsed here.
  495. */
  496. void __init parse_e820_ext(struct setup_data *sdata, unsigned long pa_data)
  497. {
  498. u32 map_len;
  499. int entries;
  500. struct e820entry *extmap;
  501. entries = sdata->len / sizeof(struct e820entry);
  502. map_len = sdata->len + sizeof(struct setup_data);
  503. if (map_len > PAGE_SIZE)
  504. sdata = early_ioremap(pa_data, map_len);
  505. extmap = (struct e820entry *)(sdata->data);
  506. __copy_e820_map(extmap, entries);
  507. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  508. if (map_len > PAGE_SIZE)
  509. early_iounmap(sdata, map_len);
  510. printk(KERN_INFO "extended physical RAM map:\n");
  511. e820_print_map("extended");
  512. }
  513. #if defined(CONFIG_X86_64) || \
  514. (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION))
  515. /**
  516. * Find the ranges of physical addresses that do not correspond to
  517. * e820 RAM areas and mark the corresponding pages as nosave for
  518. * hibernation (32 bit) or software suspend and suspend to RAM (64 bit).
  519. *
  520. * This function requires the e820 map to be sorted and without any
  521. * overlapping entries and assumes the first e820 area to be RAM.
  522. */
  523. void __init e820_mark_nosave_regions(unsigned long limit_pfn)
  524. {
  525. int i;
  526. unsigned long pfn;
  527. pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
  528. for (i = 1; i < e820.nr_map; i++) {
  529. struct e820entry *ei = &e820.map[i];
  530. if (pfn < PFN_UP(ei->addr))
  531. register_nosave_region(pfn, PFN_UP(ei->addr));
  532. pfn = PFN_DOWN(ei->addr + ei->size);
  533. if (ei->type != E820_RAM)
  534. register_nosave_region(PFN_UP(ei->addr), pfn);
  535. if (pfn >= limit_pfn)
  536. break;
  537. }
  538. }
  539. #endif
  540. /*
  541. * Early reserved memory areas.
  542. */
  543. #define MAX_EARLY_RES 20
  544. struct early_res {
  545. u64 start, end;
  546. char name[16];
  547. char overlap_ok;
  548. };
  549. static struct early_res early_res[MAX_EARLY_RES] __initdata = {
  550. { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
  551. #if defined(CONFIG_X86_64) && defined(CONFIG_X86_TRAMPOLINE)
  552. { TRAMPOLINE_BASE, TRAMPOLINE_BASE + 2 * PAGE_SIZE, "TRAMPOLINE" },
  553. #endif
  554. #if defined(CONFIG_X86_32) && defined(CONFIG_SMP)
  555. /*
  556. * But first pinch a few for the stack/trampoline stuff
  557. * FIXME: Don't need the extra page at 4K, but need to fix
  558. * trampoline before removing it. (see the GDT stuff)
  559. */
  560. { PAGE_SIZE, PAGE_SIZE + PAGE_SIZE, "EX TRAMPOLINE" },
  561. /*
  562. * Has to be in very low memory so we can execute
  563. * real-mode AP code.
  564. */
  565. { TRAMPOLINE_BASE, TRAMPOLINE_BASE + PAGE_SIZE, "TRAMPOLINE" },
  566. #endif
  567. {}
  568. };
  569. static int __init find_overlapped_early(u64 start, u64 end)
  570. {
  571. int i;
  572. struct early_res *r;
  573. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  574. r = &early_res[i];
  575. if (end > r->start && start < r->end)
  576. break;
  577. }
  578. return i;
  579. }
  580. /*
  581. * Drop the i-th range from the early reservation map,
  582. * by copying any higher ranges down one over it, and
  583. * clearing what had been the last slot.
  584. */
  585. static void __init drop_range(int i)
  586. {
  587. int j;
  588. for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++)
  589. ;
  590. memmove(&early_res[i], &early_res[i + 1],
  591. (j - 1 - i) * sizeof(struct early_res));
  592. early_res[j - 1].end = 0;
  593. }
  594. /*
  595. * Split any existing ranges that:
  596. * 1) are marked 'overlap_ok', and
  597. * 2) overlap with the stated range [start, end)
  598. * into whatever portion (if any) of the existing range is entirely
  599. * below or entirely above the stated range. Drop the portion
  600. * of the existing range that overlaps with the stated range,
  601. * which will allow the caller of this routine to then add that
  602. * stated range without conflicting with any existing range.
  603. */
  604. static void __init drop_overlaps_that_are_ok(u64 start, u64 end)
  605. {
  606. int i;
  607. struct early_res *r;
  608. u64 lower_start, lower_end;
  609. u64 upper_start, upper_end;
  610. char name[16];
  611. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  612. r = &early_res[i];
  613. /* Continue past non-overlapping ranges */
  614. if (end <= r->start || start >= r->end)
  615. continue;
  616. /*
  617. * Leave non-ok overlaps as is; let caller
  618. * panic "Overlapping early reservations"
  619. * when it hits this overlap.
  620. */
  621. if (!r->overlap_ok)
  622. return;
  623. /*
  624. * We have an ok overlap. We will drop it from the early
  625. * reservation map, and add back in any non-overlapping
  626. * portions (lower or upper) as separate, overlap_ok,
  627. * non-overlapping ranges.
  628. */
  629. /* 1. Note any non-overlapping (lower or upper) ranges. */
  630. strncpy(name, r->name, sizeof(name) - 1);
  631. lower_start = lower_end = 0;
  632. upper_start = upper_end = 0;
  633. if (r->start < start) {
  634. lower_start = r->start;
  635. lower_end = start;
  636. }
  637. if (r->end > end) {
  638. upper_start = end;
  639. upper_end = r->end;
  640. }
  641. /* 2. Drop the original ok overlapping range */
  642. drop_range(i);
  643. i--; /* resume for-loop on copied down entry */
  644. /* 3. Add back in any non-overlapping ranges. */
  645. if (lower_end)
  646. reserve_early_overlap_ok(lower_start, lower_end, name);
  647. if (upper_end)
  648. reserve_early_overlap_ok(upper_start, upper_end, name);
  649. }
  650. }
  651. static void __init __reserve_early(u64 start, u64 end, char *name,
  652. int overlap_ok)
  653. {
  654. int i;
  655. struct early_res *r;
  656. i = find_overlapped_early(start, end);
  657. if (i >= MAX_EARLY_RES)
  658. panic("Too many early reservations");
  659. r = &early_res[i];
  660. if (r->end)
  661. panic("Overlapping early reservations "
  662. "%llx-%llx %s to %llx-%llx %s\n",
  663. start, end - 1, name?name:"", r->start,
  664. r->end - 1, r->name);
  665. r->start = start;
  666. r->end = end;
  667. r->overlap_ok = overlap_ok;
  668. if (name)
  669. strncpy(r->name, name, sizeof(r->name) - 1);
  670. }
  671. /*
  672. * A few early reservtations come here.
  673. *
  674. * The 'overlap_ok' in the name of this routine does -not- mean it
  675. * is ok for these reservations to overlap an earlier reservation.
  676. * Rather it means that it is ok for subsequent reservations to
  677. * overlap this one.
  678. *
  679. * Use this entry point to reserve early ranges when you are doing
  680. * so out of "Paranoia", reserving perhaps more memory than you need,
  681. * just in case, and don't mind a subsequent overlapping reservation
  682. * that is known to be needed.
  683. *
  684. * The drop_overlaps_that_are_ok() call here isn't really needed.
  685. * It would be needed if we had two colliding 'overlap_ok'
  686. * reservations, so that the second such would not panic on the
  687. * overlap with the first. We don't have any such as of this
  688. * writing, but might as well tolerate such if it happens in
  689. * the future.
  690. */
  691. void __init reserve_early_overlap_ok(u64 start, u64 end, char *name)
  692. {
  693. drop_overlaps_that_are_ok(start, end);
  694. __reserve_early(start, end, name, 1);
  695. }
  696. /*
  697. * Most early reservations come here.
  698. *
  699. * We first have drop_overlaps_that_are_ok() drop any pre-existing
  700. * 'overlap_ok' ranges, so that we can then reserve this memory
  701. * range without risk of panic'ing on an overlapping overlap_ok
  702. * early reservation.
  703. */
  704. void __init reserve_early(u64 start, u64 end, char *name)
  705. {
  706. drop_overlaps_that_are_ok(start, end);
  707. __reserve_early(start, end, name, 0);
  708. }
  709. void __init free_early(u64 start, u64 end)
  710. {
  711. struct early_res *r;
  712. int i;
  713. i = find_overlapped_early(start, end);
  714. r = &early_res[i];
  715. if (i >= MAX_EARLY_RES || r->end != end || r->start != start)
  716. panic("free_early on not reserved area: %llx-%llx!",
  717. start, end - 1);
  718. drop_range(i);
  719. }
  720. void __init early_res_to_bootmem(u64 start, u64 end)
  721. {
  722. int i;
  723. u64 final_start, final_end;
  724. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  725. struct early_res *r = &early_res[i];
  726. final_start = max(start, r->start);
  727. final_end = min(end, r->end);
  728. if (final_start >= final_end)
  729. continue;
  730. printk(KERN_INFO " early res: %d [%llx-%llx] %s\n", i,
  731. final_start, final_end - 1, r->name);
  732. reserve_bootmem_generic(final_start, final_end - final_start,
  733. BOOTMEM_DEFAULT);
  734. }
  735. }
  736. /* Check for already reserved areas */
  737. static inline int __init bad_addr(u64 *addrp, u64 size, u64 align)
  738. {
  739. int i;
  740. u64 addr = *addrp;
  741. int changed = 0;
  742. struct early_res *r;
  743. again:
  744. i = find_overlapped_early(addr, addr + size);
  745. r = &early_res[i];
  746. if (i < MAX_EARLY_RES && r->end) {
  747. *addrp = addr = round_up(r->end, align);
  748. changed = 1;
  749. goto again;
  750. }
  751. return changed;
  752. }
  753. /* Check for already reserved areas */
  754. static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align)
  755. {
  756. int i;
  757. u64 addr = *addrp, last;
  758. u64 size = *sizep;
  759. int changed = 0;
  760. again:
  761. last = addr + size;
  762. for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
  763. struct early_res *r = &early_res[i];
  764. if (last > r->start && addr < r->start) {
  765. size = r->start - addr;
  766. changed = 1;
  767. goto again;
  768. }
  769. if (last > r->end && addr < r->end) {
  770. addr = round_up(r->end, align);
  771. size = last - addr;
  772. changed = 1;
  773. goto again;
  774. }
  775. if (last <= r->end && addr >= r->start) {
  776. (*sizep)++;
  777. return 0;
  778. }
  779. }
  780. if (changed) {
  781. *addrp = addr;
  782. *sizep = size;
  783. }
  784. return changed;
  785. }
  786. /*
  787. * Find a free area with specified alignment in a specific range.
  788. */
  789. u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align)
  790. {
  791. int i;
  792. for (i = 0; i < e820.nr_map; i++) {
  793. struct e820entry *ei = &e820.map[i];
  794. u64 addr, last;
  795. u64 ei_last;
  796. if (ei->type != E820_RAM)
  797. continue;
  798. addr = round_up(ei->addr, align);
  799. ei_last = ei->addr + ei->size;
  800. if (addr < start)
  801. addr = round_up(start, align);
  802. if (addr >= ei_last)
  803. continue;
  804. while (bad_addr(&addr, size, align) && addr+size <= ei_last)
  805. ;
  806. last = addr + size;
  807. if (last > ei_last)
  808. continue;
  809. if (last > end)
  810. continue;
  811. return addr;
  812. }
  813. return -1ULL;
  814. }
  815. /*
  816. * Find next free range after *start
  817. */
  818. u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align)
  819. {
  820. int i;
  821. for (i = 0; i < e820.nr_map; i++) {
  822. struct e820entry *ei = &e820.map[i];
  823. u64 addr, last;
  824. u64 ei_last;
  825. if (ei->type != E820_RAM)
  826. continue;
  827. addr = round_up(ei->addr, align);
  828. ei_last = ei->addr + ei->size;
  829. if (addr < start)
  830. addr = round_up(start, align);
  831. if (addr >= ei_last)
  832. continue;
  833. *sizep = ei_last - addr;
  834. while (bad_addr_size(&addr, sizep, align) &&
  835. addr + *sizep <= ei_last)
  836. ;
  837. last = addr + *sizep;
  838. if (last > ei_last)
  839. continue;
  840. return addr;
  841. }
  842. return -1UL;
  843. }
  844. /*
  845. * pre allocated 4k and reserved it in e820
  846. */
  847. u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align)
  848. {
  849. u64 size = 0;
  850. u64 addr;
  851. u64 start;
  852. start = startt;
  853. while (size < sizet)
  854. start = find_e820_area_size(start, &size, align);
  855. if (size < sizet)
  856. return 0;
  857. addr = round_down(start + size - sizet, align);
  858. e820_update_range(addr, sizet, E820_RAM, E820_RESERVED);
  859. printk(KERN_INFO "update e820 for early_reserve_e820\n");
  860. update_e820();
  861. return addr;
  862. }
  863. #ifdef CONFIG_X86_32
  864. # ifdef CONFIG_X86_PAE
  865. # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
  866. # else
  867. # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
  868. # endif
  869. #else /* CONFIG_X86_32 */
  870. # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
  871. #endif
  872. /*
  873. * Last pfn which the user wants to use.
  874. */
  875. unsigned long __initdata end_user_pfn = MAX_ARCH_PFN;
  876. /*
  877. * Find the highest page frame number we have available
  878. */
  879. unsigned long __init e820_end_of_ram(void)
  880. {
  881. unsigned long last_pfn;
  882. unsigned long max_arch_pfn = MAX_ARCH_PFN;
  883. last_pfn = find_max_pfn_with_active_regions();
  884. if (last_pfn > max_arch_pfn)
  885. last_pfn = max_arch_pfn;
  886. if (last_pfn > end_user_pfn)
  887. last_pfn = end_user_pfn;
  888. printk(KERN_INFO "last_pfn = 0x%lx max_arch_pfn = 0x%lx\n",
  889. last_pfn, max_arch_pfn);
  890. return last_pfn;
  891. }
  892. /*
  893. * Finds an active region in the address range from start_pfn to last_pfn and
  894. * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
  895. */
  896. int __init e820_find_active_region(const struct e820entry *ei,
  897. unsigned long start_pfn,
  898. unsigned long last_pfn,
  899. unsigned long *ei_startpfn,
  900. unsigned long *ei_endpfn)
  901. {
  902. u64 align = PAGE_SIZE;
  903. *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT;
  904. *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT;
  905. /* Skip map entries smaller than a page */
  906. if (*ei_startpfn >= *ei_endpfn)
  907. return 0;
  908. /* Skip if map is outside the node */
  909. if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
  910. *ei_startpfn >= last_pfn)
  911. return 0;
  912. /* Check for overlaps */
  913. if (*ei_startpfn < start_pfn)
  914. *ei_startpfn = start_pfn;
  915. if (*ei_endpfn > last_pfn)
  916. *ei_endpfn = last_pfn;
  917. /* Obey end_user_pfn to save on memmap */
  918. if (*ei_startpfn >= end_user_pfn)
  919. return 0;
  920. if (*ei_endpfn > end_user_pfn)
  921. *ei_endpfn = end_user_pfn;
  922. return 1;
  923. }
  924. /* Walk the e820 map and register active regions within a node */
  925. void __init e820_register_active_regions(int nid, unsigned long start_pfn,
  926. unsigned long last_pfn)
  927. {
  928. unsigned long ei_startpfn;
  929. unsigned long ei_endpfn;
  930. int i;
  931. for (i = 0; i < e820.nr_map; i++)
  932. if (e820_find_active_region(&e820.map[i],
  933. start_pfn, last_pfn,
  934. &ei_startpfn, &ei_endpfn))
  935. add_active_range(nid, ei_startpfn, ei_endpfn);
  936. }
  937. /*
  938. * Find the hole size (in bytes) in the memory range.
  939. * @start: starting address of the memory range to scan
  940. * @end: ending address of the memory range to scan
  941. */
  942. u64 __init e820_hole_size(u64 start, u64 end)
  943. {
  944. unsigned long start_pfn = start >> PAGE_SHIFT;
  945. unsigned long last_pfn = end >> PAGE_SHIFT;
  946. unsigned long ei_startpfn, ei_endpfn, ram = 0;
  947. int i;
  948. for (i = 0; i < e820.nr_map; i++) {
  949. if (e820_find_active_region(&e820.map[i],
  950. start_pfn, last_pfn,
  951. &ei_startpfn, &ei_endpfn))
  952. ram += ei_endpfn - ei_startpfn;
  953. }
  954. return end - start - ((u64)ram << PAGE_SHIFT);
  955. }
  956. static void early_panic(char *msg)
  957. {
  958. early_printk(msg);
  959. panic(msg);
  960. }
  961. /* "mem=nopentium" disables the 4MB page tables. */
  962. static int __init parse_memopt(char *p)
  963. {
  964. u64 mem_size;
  965. if (!p)
  966. return -EINVAL;
  967. #ifdef CONFIG_X86_32
  968. if (!strcmp(p, "nopentium")) {
  969. setup_clear_cpu_cap(X86_FEATURE_PSE);
  970. return 0;
  971. }
  972. #endif
  973. mem_size = memparse(p, &p);
  974. end_user_pfn = mem_size>>PAGE_SHIFT;
  975. return 0;
  976. }
  977. early_param("mem", parse_memopt);
  978. static int userdef __initdata;
  979. static int __init parse_memmap_opt(char *p)
  980. {
  981. char *oldp;
  982. u64 start_at, mem_size;
  983. if (!strcmp(p, "exactmap")) {
  984. #ifdef CONFIG_CRASH_DUMP
  985. /*
  986. * If we are doing a crash dump, we still need to know
  987. * the real mem size before original memory map is
  988. * reset.
  989. */
  990. e820_register_active_regions(0, 0, -1UL);
  991. saved_max_pfn = e820_end_of_ram();
  992. remove_all_active_ranges();
  993. #endif
  994. e820.nr_map = 0;
  995. userdef = 1;
  996. return 0;
  997. }
  998. oldp = p;
  999. mem_size = memparse(p, &p);
  1000. if (p == oldp)
  1001. return -EINVAL;
  1002. userdef = 1;
  1003. if (*p == '@') {
  1004. start_at = memparse(p+1, &p);
  1005. e820_add_region(start_at, mem_size, E820_RAM);
  1006. } else if (*p == '#') {
  1007. start_at = memparse(p+1, &p);
  1008. e820_add_region(start_at, mem_size, E820_ACPI);
  1009. } else if (*p == '$') {
  1010. start_at = memparse(p+1, &p);
  1011. e820_add_region(start_at, mem_size, E820_RESERVED);
  1012. } else {
  1013. end_user_pfn = (mem_size >> PAGE_SHIFT);
  1014. }
  1015. return *p == '\0' ? 0 : -EINVAL;
  1016. }
  1017. early_param("memmap", parse_memmap_opt);
  1018. void __init finish_e820_parsing(void)
  1019. {
  1020. if (userdef) {
  1021. int nr = e820.nr_map;
  1022. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
  1023. early_panic("Invalid user supplied memory map");
  1024. e820.nr_map = nr;
  1025. printk(KERN_INFO "user-defined physical RAM map:\n");
  1026. e820_print_map("user");
  1027. }
  1028. }
  1029. /*
  1030. * Mark e820 reserved areas as busy for the resource manager.
  1031. */
  1032. void __init e820_reserve_resources(void)
  1033. {
  1034. int i;
  1035. struct resource *res;
  1036. res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
  1037. for (i = 0; i < e820.nr_map; i++) {
  1038. switch (e820.map[i].type) {
  1039. case E820_RAM: res->name = "System RAM"; break;
  1040. case E820_ACPI: res->name = "ACPI Tables"; break;
  1041. case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
  1042. default: res->name = "reserved";
  1043. }
  1044. res->start = e820.map[i].addr;
  1045. res->end = res->start + e820.map[i].size - 1;
  1046. #ifndef CONFIG_RESOURCES_64BIT
  1047. if (res->end > 0x100000000ULL) {
  1048. res++;
  1049. continue;
  1050. }
  1051. #endif
  1052. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  1053. insert_resource(&iomem_resource, res);
  1054. res++;
  1055. }
  1056. }
  1057. char *__init default_machine_specific_memory_setup(void)
  1058. {
  1059. char *who = "BIOS-e820";
  1060. int new_nr;
  1061. /*
  1062. * Try to copy the BIOS-supplied E820-map.
  1063. *
  1064. * Otherwise fake a memory map; one section from 0k->640k,
  1065. * the next section from 1mb->appropriate_mem_k
  1066. */
  1067. new_nr = boot_params.e820_entries;
  1068. sanitize_e820_map(boot_params.e820_map,
  1069. ARRAY_SIZE(boot_params.e820_map),
  1070. &new_nr);
  1071. boot_params.e820_entries = new_nr;
  1072. if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0) {
  1073. u64 mem_size;
  1074. /* compare results from other methods and take the greater */
  1075. if (boot_params.alt_mem_k
  1076. < boot_params.screen_info.ext_mem_k) {
  1077. mem_size = boot_params.screen_info.ext_mem_k;
  1078. who = "BIOS-88";
  1079. } else {
  1080. mem_size = boot_params.alt_mem_k;
  1081. who = "BIOS-e801";
  1082. }
  1083. e820.nr_map = 0;
  1084. e820_add_region(0, LOWMEMSIZE(), E820_RAM);
  1085. e820_add_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
  1086. }
  1087. /* In case someone cares... */
  1088. return who;
  1089. }
  1090. char *__init __attribute__((weak)) machine_specific_memory_setup(void)
  1091. {
  1092. return default_machine_specific_memory_setup();
  1093. }
  1094. /* Overridden in paravirt.c if CONFIG_PARAVIRT */
  1095. char * __init __attribute__((weak)) memory_setup(void)
  1096. {
  1097. return machine_specific_memory_setup();
  1098. }
  1099. void __init setup_memory_map(void)
  1100. {
  1101. printk(KERN_INFO "BIOS-provided physical RAM map:\n");
  1102. e820_print_map(memory_setup());
  1103. }
  1104. #ifdef CONFIG_X86_64
  1105. int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
  1106. {
  1107. int i;
  1108. if (slot < 0 || slot >= e820.nr_map)
  1109. return -1;
  1110. for (i = slot; i < e820.nr_map; i++) {
  1111. if (e820.map[i].type != E820_RAM)
  1112. continue;
  1113. break;
  1114. }
  1115. if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
  1116. return -1;
  1117. *addr = e820.map[i].addr;
  1118. *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
  1119. max_pfn << PAGE_SHIFT) - *addr;
  1120. return i + 1;
  1121. }
  1122. #endif