e820.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  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. static void __init __e820_add_region(struct e820map *e820x, u64 start, u64 size,
  103. int type)
  104. {
  105. int x = e820x->nr_map;
  106. if (x >= ARRAY_SIZE(e820x->map)) {
  107. printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
  108. return;
  109. }
  110. e820x->map[x].addr = start;
  111. e820x->map[x].size = size;
  112. e820x->map[x].type = type;
  113. e820x->nr_map++;
  114. }
  115. void __init e820_add_region(u64 start, u64 size, int type)
  116. {
  117. __e820_add_region(&e820, start, size, type);
  118. }
  119. static void __init e820_print_type(u32 type)
  120. {
  121. switch (type) {
  122. case E820_RAM:
  123. case E820_RESERVED_KERN:
  124. printk(KERN_CONT "(usable)");
  125. break;
  126. case E820_RESERVED:
  127. printk(KERN_CONT "(reserved)");
  128. break;
  129. case E820_ACPI:
  130. printk(KERN_CONT "(ACPI data)");
  131. break;
  132. case E820_NVS:
  133. printk(KERN_CONT "(ACPI NVS)");
  134. break;
  135. case E820_UNUSABLE:
  136. printk(KERN_CONT "(unusable)");
  137. break;
  138. default:
  139. printk(KERN_CONT "type %u", type);
  140. break;
  141. }
  142. }
  143. void __init e820_print_map(char *who)
  144. {
  145. int i;
  146. for (i = 0; i < e820.nr_map; i++) {
  147. printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
  148. (unsigned long long) e820.map[i].addr,
  149. (unsigned long long)
  150. (e820.map[i].addr + e820.map[i].size));
  151. e820_print_type(e820.map[i].type);
  152. printk(KERN_CONT "\n");
  153. }
  154. }
  155. /*
  156. * Sanitize the BIOS e820 map.
  157. *
  158. * Some e820 responses include overlapping entries. The following
  159. * replaces the original e820 map with a new one, removing overlaps,
  160. * and resolving conflicting memory types in favor of highest
  161. * numbered type.
  162. *
  163. * The input parameter biosmap points to an array of 'struct
  164. * e820entry' which on entry has elements in the range [0, *pnr_map)
  165. * valid, and which has space for up to max_nr_map entries.
  166. * On return, the resulting sanitized e820 map entries will be in
  167. * overwritten in the same location, starting at biosmap.
  168. *
  169. * The integer pointed to by pnr_map must be valid on entry (the
  170. * current number of valid entries located at biosmap) and will
  171. * be updated on return, with the new number of valid entries
  172. * (something no more than max_nr_map.)
  173. *
  174. * The return value from sanitize_e820_map() is zero if it
  175. * successfully 'sanitized' the map entries passed in, and is -1
  176. * if it did nothing, which can happen if either of (1) it was
  177. * only passed one map entry, or (2) any of the input map entries
  178. * were invalid (start + size < start, meaning that the size was
  179. * so big the described memory range wrapped around through zero.)
  180. *
  181. * Visually we're performing the following
  182. * (1,2,3,4 = memory types)...
  183. *
  184. * Sample memory map (w/overlaps):
  185. * ____22__________________
  186. * ______________________4_
  187. * ____1111________________
  188. * _44_____________________
  189. * 11111111________________
  190. * ____________________33__
  191. * ___________44___________
  192. * __________33333_________
  193. * ______________22________
  194. * ___________________2222_
  195. * _________111111111______
  196. * _____________________11_
  197. * _________________4______
  198. *
  199. * Sanitized equivalent (no overlap):
  200. * 1_______________________
  201. * _44_____________________
  202. * ___1____________________
  203. * ____22__________________
  204. * ______11________________
  205. * _________1______________
  206. * __________3_____________
  207. * ___________44___________
  208. * _____________33_________
  209. * _______________2________
  210. * ________________1_______
  211. * _________________4______
  212. * ___________________2____
  213. * ____________________33__
  214. * ______________________4_
  215. */
  216. int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map,
  217. u32 *pnr_map)
  218. {
  219. struct change_member {
  220. struct e820entry *pbios; /* pointer to original bios entry */
  221. unsigned long long addr; /* address for this change point */
  222. };
  223. static struct change_member change_point_list[2*E820_X_MAX] __initdata;
  224. static struct change_member *change_point[2*E820_X_MAX] __initdata;
  225. static struct e820entry *overlap_list[E820_X_MAX] __initdata;
  226. static struct e820entry new_bios[E820_X_MAX] __initdata;
  227. struct change_member *change_tmp;
  228. unsigned long current_type, last_type;
  229. unsigned long long last_addr;
  230. int chgidx, still_changing;
  231. int overlap_entries;
  232. int new_bios_entry;
  233. int old_nr, new_nr, chg_nr;
  234. int i;
  235. /* if there's only one memory region, don't bother */
  236. if (*pnr_map < 2)
  237. return -1;
  238. old_nr = *pnr_map;
  239. BUG_ON(old_nr > max_nr_map);
  240. /* bail out if we find any unreasonable addresses in bios map */
  241. for (i = 0; i < old_nr; i++)
  242. if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
  243. return -1;
  244. /* create pointers for initial change-point information (for sorting) */
  245. for (i = 0; i < 2 * old_nr; i++)
  246. change_point[i] = &change_point_list[i];
  247. /* record all known change-points (starting and ending addresses),
  248. omitting those that are for empty memory regions */
  249. chgidx = 0;
  250. for (i = 0; i < old_nr; i++) {
  251. if (biosmap[i].size != 0) {
  252. change_point[chgidx]->addr = biosmap[i].addr;
  253. change_point[chgidx++]->pbios = &biosmap[i];
  254. change_point[chgidx]->addr = biosmap[i].addr +
  255. biosmap[i].size;
  256. change_point[chgidx++]->pbios = &biosmap[i];
  257. }
  258. }
  259. chg_nr = chgidx;
  260. /* sort change-point list by memory addresses (low -> high) */
  261. still_changing = 1;
  262. while (still_changing) {
  263. still_changing = 0;
  264. for (i = 1; i < chg_nr; i++) {
  265. unsigned long long curaddr, lastaddr;
  266. unsigned long long curpbaddr, lastpbaddr;
  267. curaddr = change_point[i]->addr;
  268. lastaddr = change_point[i - 1]->addr;
  269. curpbaddr = change_point[i]->pbios->addr;
  270. lastpbaddr = change_point[i - 1]->pbios->addr;
  271. /*
  272. * swap entries, when:
  273. *
  274. * curaddr > lastaddr or
  275. * curaddr == lastaddr and curaddr == curpbaddr and
  276. * lastaddr != lastpbaddr
  277. */
  278. if (curaddr < lastaddr ||
  279. (curaddr == lastaddr && curaddr == curpbaddr &&
  280. lastaddr != lastpbaddr)) {
  281. change_tmp = change_point[i];
  282. change_point[i] = change_point[i-1];
  283. change_point[i-1] = change_tmp;
  284. still_changing = 1;
  285. }
  286. }
  287. }
  288. /* create a new bios memory map, removing overlaps */
  289. overlap_entries = 0; /* number of entries in the overlap table */
  290. new_bios_entry = 0; /* index for creating new bios map entries */
  291. last_type = 0; /* start with undefined memory type */
  292. last_addr = 0; /* start with 0 as last starting address */
  293. /* loop through change-points, determining affect on the new bios map */
  294. for (chgidx = 0; chgidx < chg_nr; chgidx++) {
  295. /* keep track of all overlapping bios entries */
  296. if (change_point[chgidx]->addr ==
  297. change_point[chgidx]->pbios->addr) {
  298. /*
  299. * add map entry to overlap list (> 1 entry
  300. * implies an overlap)
  301. */
  302. overlap_list[overlap_entries++] =
  303. change_point[chgidx]->pbios;
  304. } else {
  305. /*
  306. * remove entry from list (order independent,
  307. * so swap with last)
  308. */
  309. for (i = 0; i < overlap_entries; i++) {
  310. if (overlap_list[i] ==
  311. change_point[chgidx]->pbios)
  312. overlap_list[i] =
  313. overlap_list[overlap_entries-1];
  314. }
  315. overlap_entries--;
  316. }
  317. /*
  318. * if there are overlapping entries, decide which
  319. * "type" to use (larger value takes precedence --
  320. * 1=usable, 2,3,4,4+=unusable)
  321. */
  322. current_type = 0;
  323. for (i = 0; i < overlap_entries; i++)
  324. if (overlap_list[i]->type > current_type)
  325. current_type = overlap_list[i]->type;
  326. /*
  327. * continue building up new bios map based on this
  328. * information
  329. */
  330. if (current_type != last_type) {
  331. if (last_type != 0) {
  332. new_bios[new_bios_entry].size =
  333. change_point[chgidx]->addr - last_addr;
  334. /*
  335. * move forward only if the new size
  336. * was non-zero
  337. */
  338. if (new_bios[new_bios_entry].size != 0)
  339. /*
  340. * no more space left for new
  341. * bios entries ?
  342. */
  343. if (++new_bios_entry >= max_nr_map)
  344. break;
  345. }
  346. if (current_type != 0) {
  347. new_bios[new_bios_entry].addr =
  348. change_point[chgidx]->addr;
  349. new_bios[new_bios_entry].type = current_type;
  350. last_addr = change_point[chgidx]->addr;
  351. }
  352. last_type = current_type;
  353. }
  354. }
  355. /* retain count for new bios entries */
  356. new_nr = new_bios_entry;
  357. /* copy new bios mapping into original location */
  358. memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
  359. *pnr_map = new_nr;
  360. return 0;
  361. }
  362. static int __init __append_e820_map(struct e820entry *biosmap, int nr_map)
  363. {
  364. while (nr_map) {
  365. u64 start = biosmap->addr;
  366. u64 size = biosmap->size;
  367. u64 end = start + size;
  368. u32 type = biosmap->type;
  369. /* Overflow in 64 bits? Ignore the memory map. */
  370. if (start > end)
  371. return -1;
  372. e820_add_region(start, size, type);
  373. biosmap++;
  374. nr_map--;
  375. }
  376. return 0;
  377. }
  378. /*
  379. * Copy the BIOS e820 map into a safe place.
  380. *
  381. * Sanity-check it while we're at it..
  382. *
  383. * If we're lucky and live on a modern system, the setup code
  384. * will have given us a memory map that we can use to properly
  385. * set up memory. If we aren't, we'll fake a memory map.
  386. */
  387. static int __init append_e820_map(struct e820entry *biosmap, int nr_map)
  388. {
  389. /* Only one memory region (or negative)? Ignore it */
  390. if (nr_map < 2)
  391. return -1;
  392. return __append_e820_map(biosmap, nr_map);
  393. }
  394. static u64 __init __e820_update_range(struct e820map *e820x, u64 start,
  395. u64 size, unsigned old_type,
  396. unsigned new_type)
  397. {
  398. u64 end;
  399. unsigned int i;
  400. u64 real_updated_size = 0;
  401. BUG_ON(old_type == new_type);
  402. if (size > (ULLONG_MAX - start))
  403. size = ULLONG_MAX - start;
  404. end = start + size;
  405. printk(KERN_DEBUG "e820 update range: %016Lx - %016Lx ",
  406. (unsigned long long) start,
  407. (unsigned long long) end);
  408. e820_print_type(old_type);
  409. printk(KERN_CONT " ==> ");
  410. e820_print_type(new_type);
  411. printk(KERN_CONT "\n");
  412. for (i = 0; i < e820x->nr_map; i++) {
  413. struct e820entry *ei = &e820x->map[i];
  414. u64 final_start, final_end;
  415. u64 ei_end;
  416. if (ei->type != old_type)
  417. continue;
  418. ei_end = ei->addr + ei->size;
  419. /* totally covered by new range? */
  420. if (ei->addr >= start && ei_end <= end) {
  421. ei->type = new_type;
  422. real_updated_size += ei->size;
  423. continue;
  424. }
  425. /* new range is totally covered? */
  426. if (ei->addr < start && ei_end > end) {
  427. __e820_add_region(e820x, start, size, new_type);
  428. __e820_add_region(e820x, end, ei_end - end, ei->type);
  429. ei->size = start - ei->addr;
  430. real_updated_size += size;
  431. continue;
  432. }
  433. /* partially covered */
  434. final_start = max(start, ei->addr);
  435. final_end = min(end, ei_end);
  436. if (final_start >= final_end)
  437. continue;
  438. __e820_add_region(e820x, final_start, final_end - final_start,
  439. new_type);
  440. real_updated_size += final_end - final_start;
  441. /*
  442. * left range could be head or tail, so need to update
  443. * size at first.
  444. */
  445. ei->size -= final_end - final_start;
  446. if (ei->addr < final_start)
  447. continue;
  448. ei->addr = final_end;
  449. }
  450. return real_updated_size;
  451. }
  452. u64 __init e820_update_range(u64 start, u64 size, unsigned old_type,
  453. unsigned new_type)
  454. {
  455. return __e820_update_range(&e820, start, size, old_type, new_type);
  456. }
  457. static u64 __init e820_update_range_saved(u64 start, u64 size,
  458. unsigned old_type, unsigned new_type)
  459. {
  460. return __e820_update_range(&e820_saved, start, size, old_type,
  461. new_type);
  462. }
  463. /* make e820 not cover the range */
  464. u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type,
  465. int checktype)
  466. {
  467. int i;
  468. u64 end;
  469. u64 real_removed_size = 0;
  470. if (size > (ULLONG_MAX - start))
  471. size = ULLONG_MAX - start;
  472. end = start + size;
  473. printk(KERN_DEBUG "e820 remove range: %016Lx - %016Lx ",
  474. (unsigned long long) start,
  475. (unsigned long long) end);
  476. e820_print_type(old_type);
  477. printk(KERN_CONT "\n");
  478. for (i = 0; i < e820.nr_map; i++) {
  479. struct e820entry *ei = &e820.map[i];
  480. u64 final_start, final_end;
  481. if (checktype && ei->type != old_type)
  482. continue;
  483. /* totally covered? */
  484. if (ei->addr >= start &&
  485. (ei->addr + ei->size) <= (start + size)) {
  486. real_removed_size += ei->size;
  487. memset(ei, 0, sizeof(struct e820entry));
  488. continue;
  489. }
  490. /* partially covered */
  491. final_start = max(start, ei->addr);
  492. final_end = min(start + size, ei->addr + ei->size);
  493. if (final_start >= final_end)
  494. continue;
  495. real_removed_size += final_end - final_start;
  496. ei->size -= final_end - final_start;
  497. if (ei->addr < final_start)
  498. continue;
  499. ei->addr = final_end;
  500. }
  501. return real_removed_size;
  502. }
  503. void __init update_e820(void)
  504. {
  505. u32 nr_map;
  506. nr_map = e820.nr_map;
  507. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map))
  508. return;
  509. e820.nr_map = nr_map;
  510. printk(KERN_INFO "modified physical RAM map:\n");
  511. e820_print_map("modified");
  512. }
  513. static void __init update_e820_saved(void)
  514. {
  515. u32 nr_map;
  516. nr_map = e820_saved.nr_map;
  517. if (sanitize_e820_map(e820_saved.map, ARRAY_SIZE(e820_saved.map), &nr_map))
  518. return;
  519. e820_saved.nr_map = nr_map;
  520. }
  521. #define MAX_GAP_END 0x100000000ull
  522. /*
  523. * Search for a gap in the e820 memory space from start_addr to end_addr.
  524. */
  525. __init int e820_search_gap(unsigned long *gapstart, unsigned long *gapsize,
  526. unsigned long start_addr, unsigned long long end_addr)
  527. {
  528. unsigned long long last;
  529. int i = e820.nr_map;
  530. int found = 0;
  531. last = (end_addr && end_addr < MAX_GAP_END) ? end_addr : MAX_GAP_END;
  532. while (--i >= 0) {
  533. unsigned long long start = e820.map[i].addr;
  534. unsigned long long end = start + e820.map[i].size;
  535. if (end < start_addr)
  536. continue;
  537. /*
  538. * Since "last" is at most 4GB, we know we'll
  539. * fit in 32 bits if this condition is true
  540. */
  541. if (last > end) {
  542. unsigned long gap = last - end;
  543. if (gap >= *gapsize) {
  544. *gapsize = gap;
  545. *gapstart = end;
  546. found = 1;
  547. }
  548. }
  549. if (start < last)
  550. last = start;
  551. }
  552. return found;
  553. }
  554. /*
  555. * Search for the biggest gap in the low 32 bits of the e820
  556. * memory space. We pass this space to PCI to assign MMIO resources
  557. * for hotplug or unconfigured devices in.
  558. * Hopefully the BIOS let enough space left.
  559. */
  560. __init void e820_setup_gap(void)
  561. {
  562. unsigned long gapstart, gapsize;
  563. int found;
  564. gapstart = 0x10000000;
  565. gapsize = 0x400000;
  566. found = e820_search_gap(&gapstart, &gapsize, 0, MAX_GAP_END);
  567. #ifdef CONFIG_X86_64
  568. if (!found) {
  569. gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024;
  570. printk(KERN_ERR
  571. "PCI: Warning: Cannot find a gap in the 32bit address range\n"
  572. "PCI: Unassigned devices with 32bit resource registers may break!\n");
  573. }
  574. #endif
  575. /*
  576. * e820_reserve_resources_late protect stolen RAM already
  577. */
  578. pci_mem_start = gapstart;
  579. printk(KERN_INFO
  580. "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
  581. pci_mem_start, gapstart, gapsize);
  582. }
  583. /**
  584. * Because of the size limitation of struct boot_params, only first
  585. * 128 E820 memory entries are passed to kernel via
  586. * boot_params.e820_map, others are passed via SETUP_E820_EXT node of
  587. * linked list of struct setup_data, which is parsed here.
  588. */
  589. void __init parse_e820_ext(struct setup_data *sdata, unsigned long pa_data)
  590. {
  591. u32 map_len;
  592. int entries;
  593. struct e820entry *extmap;
  594. entries = sdata->len / sizeof(struct e820entry);
  595. map_len = sdata->len + sizeof(struct setup_data);
  596. if (map_len > PAGE_SIZE)
  597. sdata = early_ioremap(pa_data, map_len);
  598. extmap = (struct e820entry *)(sdata->data);
  599. __append_e820_map(extmap, entries);
  600. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  601. if (map_len > PAGE_SIZE)
  602. early_iounmap(sdata, map_len);
  603. printk(KERN_INFO "extended physical RAM map:\n");
  604. e820_print_map("extended");
  605. }
  606. #if defined(CONFIG_X86_64) || \
  607. (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION))
  608. /**
  609. * Find the ranges of physical addresses that do not correspond to
  610. * e820 RAM areas and mark the corresponding pages as nosave for
  611. * hibernation (32 bit) or software suspend and suspend to RAM (64 bit).
  612. *
  613. * This function requires the e820 map to be sorted and without any
  614. * overlapping entries and assumes the first e820 area to be RAM.
  615. */
  616. void __init e820_mark_nosave_regions(unsigned long limit_pfn)
  617. {
  618. int i;
  619. unsigned long pfn;
  620. pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
  621. for (i = 1; i < e820.nr_map; i++) {
  622. struct e820entry *ei = &e820.map[i];
  623. if (pfn < PFN_UP(ei->addr))
  624. register_nosave_region(pfn, PFN_UP(ei->addr));
  625. pfn = PFN_DOWN(ei->addr + ei->size);
  626. if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
  627. register_nosave_region(PFN_UP(ei->addr), pfn);
  628. if (pfn >= limit_pfn)
  629. break;
  630. }
  631. }
  632. #endif
  633. #ifdef CONFIG_HIBERNATION
  634. /**
  635. * Mark ACPI NVS memory region, so that we can save/restore it during
  636. * hibernation and the subsequent resume.
  637. */
  638. static int __init e820_mark_nvs_memory(void)
  639. {
  640. int i;
  641. for (i = 0; i < e820.nr_map; i++) {
  642. struct e820entry *ei = &e820.map[i];
  643. if (ei->type == E820_NVS)
  644. hibernate_nvs_register(ei->addr, ei->size);
  645. }
  646. return 0;
  647. }
  648. core_initcall(e820_mark_nvs_memory);
  649. #endif
  650. /*
  651. * Early reserved memory areas.
  652. */
  653. /*
  654. * need to make sure this one is bigger enough before
  655. * find_e820_area could be used
  656. */
  657. #define MAX_EARLY_RES_X 32
  658. struct early_res {
  659. u64 start, end;
  660. char name[15];
  661. char overlap_ok;
  662. };
  663. static struct early_res early_res_x[MAX_EARLY_RES_X] __initdata;
  664. static int max_early_res __initdata = MAX_EARLY_RES_X;
  665. static struct early_res *early_res __initdata = &early_res_x[0];
  666. static int early_res_count __initdata;
  667. static int __init find_overlapped_early(u64 start, u64 end)
  668. {
  669. int i;
  670. struct early_res *r;
  671. for (i = 0; i < max_early_res && early_res[i].end; i++) {
  672. r = &early_res[i];
  673. if (end > r->start && start < r->end)
  674. break;
  675. }
  676. return i;
  677. }
  678. /*
  679. * Drop the i-th range from the early reservation map,
  680. * by copying any higher ranges down one over it, and
  681. * clearing what had been the last slot.
  682. */
  683. static void __init drop_range(int i)
  684. {
  685. int j;
  686. for (j = i + 1; j < max_early_res && early_res[j].end; j++)
  687. ;
  688. memmove(&early_res[i], &early_res[i + 1],
  689. (j - 1 - i) * sizeof(struct early_res));
  690. early_res[j - 1].end = 0;
  691. early_res_count--;
  692. }
  693. /*
  694. * Split any existing ranges that:
  695. * 1) are marked 'overlap_ok', and
  696. * 2) overlap with the stated range [start, end)
  697. * into whatever portion (if any) of the existing range is entirely
  698. * below or entirely above the stated range. Drop the portion
  699. * of the existing range that overlaps with the stated range,
  700. * which will allow the caller of this routine to then add that
  701. * stated range without conflicting with any existing range.
  702. */
  703. static void __init drop_overlaps_that_are_ok(u64 start, u64 end)
  704. {
  705. int i;
  706. struct early_res *r;
  707. u64 lower_start, lower_end;
  708. u64 upper_start, upper_end;
  709. char name[15];
  710. for (i = 0; i < max_early_res && early_res[i].end; i++) {
  711. r = &early_res[i];
  712. /* Continue past non-overlapping ranges */
  713. if (end <= r->start || start >= r->end)
  714. continue;
  715. /*
  716. * Leave non-ok overlaps as is; let caller
  717. * panic "Overlapping early reservations"
  718. * when it hits this overlap.
  719. */
  720. if (!r->overlap_ok)
  721. return;
  722. /*
  723. * We have an ok overlap. We will drop it from the early
  724. * reservation map, and add back in any non-overlapping
  725. * portions (lower or upper) as separate, overlap_ok,
  726. * non-overlapping ranges.
  727. */
  728. /* 1. Note any non-overlapping (lower or upper) ranges. */
  729. strncpy(name, r->name, sizeof(name) - 1);
  730. lower_start = lower_end = 0;
  731. upper_start = upper_end = 0;
  732. if (r->start < start) {
  733. lower_start = r->start;
  734. lower_end = start;
  735. }
  736. if (r->end > end) {
  737. upper_start = end;
  738. upper_end = r->end;
  739. }
  740. /* 2. Drop the original ok overlapping range */
  741. drop_range(i);
  742. i--; /* resume for-loop on copied down entry */
  743. /* 3. Add back in any non-overlapping ranges. */
  744. if (lower_end)
  745. reserve_early_overlap_ok(lower_start, lower_end, name);
  746. if (upper_end)
  747. reserve_early_overlap_ok(upper_start, upper_end, name);
  748. }
  749. }
  750. static void __init __reserve_early(u64 start, u64 end, char *name,
  751. int overlap_ok)
  752. {
  753. int i;
  754. struct early_res *r;
  755. i = find_overlapped_early(start, end);
  756. if (i >= max_early_res)
  757. panic("Too many early reservations");
  758. r = &early_res[i];
  759. if (r->end)
  760. panic("Overlapping early reservations "
  761. "%llx-%llx %s to %llx-%llx %s\n",
  762. start, end - 1, name?name:"", r->start,
  763. r->end - 1, r->name);
  764. r->start = start;
  765. r->end = end;
  766. r->overlap_ok = overlap_ok;
  767. if (name)
  768. strncpy(r->name, name, sizeof(r->name) - 1);
  769. early_res_count++;
  770. }
  771. /*
  772. * A few early reservtations come here.
  773. *
  774. * The 'overlap_ok' in the name of this routine does -not- mean it
  775. * is ok for these reservations to overlap an earlier reservation.
  776. * Rather it means that it is ok for subsequent reservations to
  777. * overlap this one.
  778. *
  779. * Use this entry point to reserve early ranges when you are doing
  780. * so out of "Paranoia", reserving perhaps more memory than you need,
  781. * just in case, and don't mind a subsequent overlapping reservation
  782. * that is known to be needed.
  783. *
  784. * The drop_overlaps_that_are_ok() call here isn't really needed.
  785. * It would be needed if we had two colliding 'overlap_ok'
  786. * reservations, so that the second such would not panic on the
  787. * overlap with the first. We don't have any such as of this
  788. * writing, but might as well tolerate such if it happens in
  789. * the future.
  790. */
  791. void __init reserve_early_overlap_ok(u64 start, u64 end, char *name)
  792. {
  793. drop_overlaps_that_are_ok(start, end);
  794. __reserve_early(start, end, name, 1);
  795. }
  796. static void __init __check_and_double_early_res(u64 start)
  797. {
  798. u64 end, size, mem;
  799. struct early_res *new;
  800. /* do we have enough slots left ? */
  801. if ((max_early_res - early_res_count) > max(max_early_res/8, 2))
  802. return;
  803. /* double it */
  804. end = max_pfn_mapped << PAGE_SHIFT;
  805. size = sizeof(struct early_res) * max_early_res * 2;
  806. mem = find_e820_area(start, end, size, sizeof(struct early_res));
  807. if (mem == -1ULL)
  808. panic("can not find more space for early_res array");
  809. new = __va(mem);
  810. /* save the first one for own */
  811. new[0].start = mem;
  812. new[0].end = mem + size;
  813. new[0].overlap_ok = 0;
  814. /* copy old to new */
  815. if (early_res == early_res_x) {
  816. memcpy(&new[1], &early_res[0],
  817. sizeof(struct early_res) * max_early_res);
  818. memset(&new[max_early_res+1], 0,
  819. sizeof(struct early_res) * (max_early_res - 1));
  820. early_res_count++;
  821. } else {
  822. memcpy(&new[1], &early_res[1],
  823. sizeof(struct early_res) * (max_early_res - 1));
  824. memset(&new[max_early_res], 0,
  825. sizeof(struct early_res) * max_early_res);
  826. }
  827. memset(&early_res[0], 0, sizeof(struct early_res) * max_early_res);
  828. early_res = new;
  829. max_early_res *= 2;
  830. printk(KERN_DEBUG "early_res array is doubled to %d at [%llx - %llx]\n",
  831. max_early_res, mem, mem + size - 1);
  832. }
  833. /*
  834. * Most early reservations come here.
  835. *
  836. * We first have drop_overlaps_that_are_ok() drop any pre-existing
  837. * 'overlap_ok' ranges, so that we can then reserve this memory
  838. * range without risk of panic'ing on an overlapping overlap_ok
  839. * early reservation.
  840. */
  841. void __init reserve_early(u64 start, u64 end, char *name)
  842. {
  843. if (start >= end)
  844. return;
  845. __check_and_double_early_res(end);
  846. drop_overlaps_that_are_ok(start, end);
  847. __reserve_early(start, end, name, 0);
  848. }
  849. void __init reserve_early_without_check(u64 start, u64 end, char *name)
  850. {
  851. struct early_res *r;
  852. if (start >= end)
  853. return;
  854. __check_and_double_early_res(end);
  855. r = &early_res[early_res_count];
  856. r->start = start;
  857. r->end = end;
  858. r->overlap_ok = 0;
  859. if (name)
  860. strncpy(r->name, name, sizeof(r->name) - 1);
  861. early_res_count++;
  862. }
  863. void __init free_early(u64 start, u64 end)
  864. {
  865. struct early_res *r;
  866. int i;
  867. i = find_overlapped_early(start, end);
  868. r = &early_res[i];
  869. if (i >= max_early_res || r->end != end || r->start != start)
  870. panic("free_early on not reserved area: %llx-%llx!",
  871. start, end - 1);
  872. drop_range(i);
  873. }
  874. #ifdef CONFIG_NO_BOOTMEM
  875. static void __init subtract_early_res(struct range *range, int az)
  876. {
  877. int i, count;
  878. u64 final_start, final_end;
  879. int idx = 0;
  880. count = 0;
  881. for (i = 0; i < max_early_res && early_res[i].end; i++)
  882. count++;
  883. /* need to skip first one ?*/
  884. if (early_res != early_res_x)
  885. idx = 1;
  886. #if 1
  887. printk(KERN_INFO "Subtract (%d early reservations)\n", count);
  888. #endif
  889. for (i = idx; i < count; i++) {
  890. struct early_res *r = &early_res[i];
  891. #if 0
  892. printk(KERN_INFO " #%d [%010llx - %010llx] %15s", i,
  893. r->start, r->end, r->name);
  894. #endif
  895. final_start = PFN_DOWN(r->start);
  896. final_end = PFN_UP(r->end);
  897. if (final_start >= final_end) {
  898. #if 0
  899. printk(KERN_CONT "\n");
  900. #endif
  901. continue;
  902. }
  903. #if 0
  904. printk(KERN_CONT " subtract pfn [%010llx - %010llx]\n",
  905. final_start, final_end);
  906. #endif
  907. subtract_range(range, az, final_start, final_end);
  908. }
  909. }
  910. int __init get_free_all_memory_range(struct range **rangep, int nodeid)
  911. {
  912. int i, count;
  913. u64 start = 0, end;
  914. u64 size;
  915. u64 mem;
  916. struct range *range;
  917. int nr_range;
  918. count = 0;
  919. for (i = 0; i < max_early_res && early_res[i].end; i++)
  920. count++;
  921. count *= 2;
  922. size = sizeof(struct range) * count;
  923. #ifdef MAX_DMA32_PFN
  924. if (max_pfn_mapped > MAX_DMA32_PFN)
  925. start = MAX_DMA32_PFN << PAGE_SHIFT;
  926. #endif
  927. end = max_pfn_mapped << PAGE_SHIFT;
  928. mem = find_e820_area(start, end, size, sizeof(struct range));
  929. if (mem == -1ULL)
  930. panic("can not find more space for range free");
  931. range = __va(mem);
  932. /* use early_node_map[] and early_res to get range array at first */
  933. memset(range, 0, size);
  934. nr_range = 0;
  935. /* need to go over early_node_map to find out good range for node */
  936. nr_range = add_from_early_node_map(range, count, nr_range, nodeid);
  937. subtract_early_res(range, count);
  938. nr_range = clean_sort_range(range, count);
  939. /* need to clear it ? */
  940. if (nodeid == MAX_NUMNODES) {
  941. memset(&early_res[0], 0,
  942. sizeof(struct early_res) * max_early_res);
  943. early_res = NULL;
  944. max_early_res = 0;
  945. }
  946. *rangep = range;
  947. return nr_range;
  948. }
  949. #else
  950. void __init early_res_to_bootmem(u64 start, u64 end)
  951. {
  952. int i, count;
  953. u64 final_start, final_end;
  954. int idx = 0;
  955. count = 0;
  956. for (i = 0; i < max_early_res && early_res[i].end; i++)
  957. count++;
  958. /* need to skip first one ?*/
  959. if (early_res != early_res_x)
  960. idx = 1;
  961. printk(KERN_INFO "(%d/%d early reservations) ==> bootmem [%010llx - %010llx]\n",
  962. count - idx, max_early_res, start, end);
  963. for (i = idx; i < count; i++) {
  964. struct early_res *r = &early_res[i];
  965. printk(KERN_INFO " #%d [%010llx - %010llx] %16s", i,
  966. r->start, r->end, r->name);
  967. final_start = max(start, r->start);
  968. final_end = min(end, r->end);
  969. if (final_start >= final_end) {
  970. printk(KERN_CONT "\n");
  971. continue;
  972. }
  973. printk(KERN_CONT " ==> [%010llx - %010llx]\n",
  974. final_start, final_end);
  975. reserve_bootmem_generic(final_start, final_end - final_start,
  976. BOOTMEM_DEFAULT);
  977. }
  978. /* clear them */
  979. memset(&early_res[0], 0, sizeof(struct early_res) * max_early_res);
  980. early_res = NULL;
  981. max_early_res = 0;
  982. early_res_count = 0;
  983. }
  984. #endif
  985. /* Check for already reserved areas */
  986. static inline int __init bad_addr(u64 *addrp, u64 size, u64 align)
  987. {
  988. int i;
  989. u64 addr = *addrp;
  990. int changed = 0;
  991. struct early_res *r;
  992. again:
  993. i = find_overlapped_early(addr, addr + size);
  994. r = &early_res[i];
  995. if (i < max_early_res && r->end) {
  996. *addrp = addr = round_up(r->end, align);
  997. changed = 1;
  998. goto again;
  999. }
  1000. return changed;
  1001. }
  1002. /* Check for already reserved areas */
  1003. static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align)
  1004. {
  1005. int i;
  1006. u64 addr = *addrp, last;
  1007. u64 size = *sizep;
  1008. int changed = 0;
  1009. again:
  1010. last = addr + size;
  1011. for (i = 0; i < max_early_res && early_res[i].end; i++) {
  1012. struct early_res *r = &early_res[i];
  1013. if (last > r->start && addr < r->start) {
  1014. size = r->start - addr;
  1015. changed = 1;
  1016. goto again;
  1017. }
  1018. if (last > r->end && addr < r->end) {
  1019. addr = round_up(r->end, align);
  1020. size = last - addr;
  1021. changed = 1;
  1022. goto again;
  1023. }
  1024. if (last <= r->end && addr >= r->start) {
  1025. (*sizep)++;
  1026. return 0;
  1027. }
  1028. }
  1029. if (changed) {
  1030. *addrp = addr;
  1031. *sizep = size;
  1032. }
  1033. return changed;
  1034. }
  1035. /*
  1036. * Find a free area with specified alignment in a specific range.
  1037. * only with the area.between start to end is active range from early_node_map
  1038. * so they are good as RAM
  1039. */
  1040. u64 __init find_early_area(u64 ei_start, u64 ei_last, u64 start, u64 end,
  1041. u64 size, u64 align)
  1042. {
  1043. u64 addr, last;
  1044. addr = round_up(ei_start, align);
  1045. if (addr < start)
  1046. addr = round_up(start, align);
  1047. if (addr >= ei_last)
  1048. goto out;
  1049. while (bad_addr(&addr, size, align) && addr+size <= ei_last)
  1050. ;
  1051. last = addr + size;
  1052. if (last > ei_last)
  1053. goto out;
  1054. if (last > end)
  1055. goto out;
  1056. return addr;
  1057. out:
  1058. return -1ULL;
  1059. }
  1060. /*
  1061. * Find a free area with specified alignment in a specific range.
  1062. */
  1063. u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align)
  1064. {
  1065. int i;
  1066. for (i = 0; i < e820.nr_map; i++) {
  1067. struct e820entry *ei = &e820.map[i];
  1068. u64 addr;
  1069. u64 ei_start, ei_last;
  1070. if (ei->type != E820_RAM)
  1071. continue;
  1072. ei_last = ei->addr + ei->size;
  1073. ei_start = ei->addr;
  1074. addr = find_early_area(ei_start, ei_last, start, end,
  1075. size, align);
  1076. if (addr == -1ULL)
  1077. continue;
  1078. return addr;
  1079. }
  1080. return -1ULL;
  1081. }
  1082. /*
  1083. * Find next free range after *start
  1084. */
  1085. u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align)
  1086. {
  1087. int i;
  1088. for (i = 0; i < e820.nr_map; i++) {
  1089. struct e820entry *ei = &e820.map[i];
  1090. u64 addr, last;
  1091. u64 ei_last;
  1092. if (ei->type != E820_RAM)
  1093. continue;
  1094. addr = round_up(ei->addr, align);
  1095. ei_last = ei->addr + ei->size;
  1096. if (addr < start)
  1097. addr = round_up(start, align);
  1098. if (addr >= ei_last)
  1099. continue;
  1100. *sizep = ei_last - addr;
  1101. while (bad_addr_size(&addr, sizep, align) &&
  1102. addr + *sizep <= ei_last)
  1103. ;
  1104. last = addr + *sizep;
  1105. if (last > ei_last)
  1106. continue;
  1107. return addr;
  1108. }
  1109. return -1ULL;
  1110. }
  1111. /*
  1112. * pre allocated 4k and reserved it in e820
  1113. */
  1114. u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align)
  1115. {
  1116. u64 size = 0;
  1117. u64 addr;
  1118. u64 start;
  1119. for (start = startt; ; start += size) {
  1120. start = find_e820_area_size(start, &size, align);
  1121. if (!(start + 1))
  1122. return 0;
  1123. if (size >= sizet)
  1124. break;
  1125. }
  1126. #ifdef CONFIG_X86_32
  1127. if (start >= MAXMEM)
  1128. return 0;
  1129. if (start + size > MAXMEM)
  1130. size = MAXMEM - start;
  1131. #endif
  1132. addr = round_down(start + size - sizet, align);
  1133. if (addr < start)
  1134. return 0;
  1135. e820_update_range(addr, sizet, E820_RAM, E820_RESERVED);
  1136. e820_update_range_saved(addr, sizet, E820_RAM, E820_RESERVED);
  1137. printk(KERN_INFO "update e820 for early_reserve_e820\n");
  1138. update_e820();
  1139. update_e820_saved();
  1140. return addr;
  1141. }
  1142. #ifdef CONFIG_X86_32
  1143. # ifdef CONFIG_X86_PAE
  1144. # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
  1145. # else
  1146. # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
  1147. # endif
  1148. #else /* CONFIG_X86_32 */
  1149. # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
  1150. #endif
  1151. /*
  1152. * Find the highest page frame number we have available
  1153. */
  1154. static unsigned long __init e820_end_pfn(unsigned long limit_pfn, unsigned type)
  1155. {
  1156. int i;
  1157. unsigned long last_pfn = 0;
  1158. unsigned long max_arch_pfn = MAX_ARCH_PFN;
  1159. for (i = 0; i < e820.nr_map; i++) {
  1160. struct e820entry *ei = &e820.map[i];
  1161. unsigned long start_pfn;
  1162. unsigned long end_pfn;
  1163. if (ei->type != type)
  1164. continue;
  1165. start_pfn = ei->addr >> PAGE_SHIFT;
  1166. end_pfn = (ei->addr + ei->size) >> PAGE_SHIFT;
  1167. if (start_pfn >= limit_pfn)
  1168. continue;
  1169. if (end_pfn > limit_pfn) {
  1170. last_pfn = limit_pfn;
  1171. break;
  1172. }
  1173. if (end_pfn > last_pfn)
  1174. last_pfn = end_pfn;
  1175. }
  1176. if (last_pfn > max_arch_pfn)
  1177. last_pfn = max_arch_pfn;
  1178. printk(KERN_INFO "last_pfn = %#lx max_arch_pfn = %#lx\n",
  1179. last_pfn, max_arch_pfn);
  1180. return last_pfn;
  1181. }
  1182. unsigned long __init e820_end_of_ram_pfn(void)
  1183. {
  1184. return e820_end_pfn(MAX_ARCH_PFN, E820_RAM);
  1185. }
  1186. unsigned long __init e820_end_of_low_ram_pfn(void)
  1187. {
  1188. return e820_end_pfn(1UL<<(32 - PAGE_SHIFT), E820_RAM);
  1189. }
  1190. /*
  1191. * Finds an active region in the address range from start_pfn to last_pfn and
  1192. * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
  1193. */
  1194. int __init e820_find_active_region(const struct e820entry *ei,
  1195. unsigned long start_pfn,
  1196. unsigned long last_pfn,
  1197. unsigned long *ei_startpfn,
  1198. unsigned long *ei_endpfn)
  1199. {
  1200. u64 align = PAGE_SIZE;
  1201. *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT;
  1202. *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT;
  1203. /* Skip map entries smaller than a page */
  1204. if (*ei_startpfn >= *ei_endpfn)
  1205. return 0;
  1206. /* Skip if map is outside the node */
  1207. if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
  1208. *ei_startpfn >= last_pfn)
  1209. return 0;
  1210. /* Check for overlaps */
  1211. if (*ei_startpfn < start_pfn)
  1212. *ei_startpfn = start_pfn;
  1213. if (*ei_endpfn > last_pfn)
  1214. *ei_endpfn = last_pfn;
  1215. return 1;
  1216. }
  1217. /* Walk the e820 map and register active regions within a node */
  1218. void __init e820_register_active_regions(int nid, unsigned long start_pfn,
  1219. unsigned long last_pfn)
  1220. {
  1221. unsigned long ei_startpfn;
  1222. unsigned long ei_endpfn;
  1223. int i;
  1224. for (i = 0; i < e820.nr_map; i++)
  1225. if (e820_find_active_region(&e820.map[i],
  1226. start_pfn, last_pfn,
  1227. &ei_startpfn, &ei_endpfn))
  1228. add_active_range(nid, ei_startpfn, ei_endpfn);
  1229. }
  1230. /*
  1231. * Find the hole size (in bytes) in the memory range.
  1232. * @start: starting address of the memory range to scan
  1233. * @end: ending address of the memory range to scan
  1234. */
  1235. u64 __init e820_hole_size(u64 start, u64 end)
  1236. {
  1237. unsigned long start_pfn = start >> PAGE_SHIFT;
  1238. unsigned long last_pfn = end >> PAGE_SHIFT;
  1239. unsigned long ei_startpfn, ei_endpfn, ram = 0;
  1240. int i;
  1241. for (i = 0; i < e820.nr_map; i++) {
  1242. if (e820_find_active_region(&e820.map[i],
  1243. start_pfn, last_pfn,
  1244. &ei_startpfn, &ei_endpfn))
  1245. ram += ei_endpfn - ei_startpfn;
  1246. }
  1247. return end - start - ((u64)ram << PAGE_SHIFT);
  1248. }
  1249. static void early_panic(char *msg)
  1250. {
  1251. early_printk(msg);
  1252. panic(msg);
  1253. }
  1254. static int userdef __initdata;
  1255. /* "mem=nopentium" disables the 4MB page tables. */
  1256. static int __init parse_memopt(char *p)
  1257. {
  1258. u64 mem_size;
  1259. if (!p)
  1260. return -EINVAL;
  1261. #ifdef CONFIG_X86_32
  1262. if (!strcmp(p, "nopentium")) {
  1263. setup_clear_cpu_cap(X86_FEATURE_PSE);
  1264. return 0;
  1265. }
  1266. #endif
  1267. userdef = 1;
  1268. mem_size = memparse(p, &p);
  1269. e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
  1270. return 0;
  1271. }
  1272. early_param("mem", parse_memopt);
  1273. static int __init parse_memmap_opt(char *p)
  1274. {
  1275. char *oldp;
  1276. u64 start_at, mem_size;
  1277. if (!p)
  1278. return -EINVAL;
  1279. if (!strncmp(p, "exactmap", 8)) {
  1280. #ifdef CONFIG_CRASH_DUMP
  1281. /*
  1282. * If we are doing a crash dump, we still need to know
  1283. * the real mem size before original memory map is
  1284. * reset.
  1285. */
  1286. saved_max_pfn = e820_end_of_ram_pfn();
  1287. #endif
  1288. e820.nr_map = 0;
  1289. userdef = 1;
  1290. return 0;
  1291. }
  1292. oldp = p;
  1293. mem_size = memparse(p, &p);
  1294. if (p == oldp)
  1295. return -EINVAL;
  1296. userdef = 1;
  1297. if (*p == '@') {
  1298. start_at = memparse(p+1, &p);
  1299. e820_add_region(start_at, mem_size, E820_RAM);
  1300. } else if (*p == '#') {
  1301. start_at = memparse(p+1, &p);
  1302. e820_add_region(start_at, mem_size, E820_ACPI);
  1303. } else if (*p == '$') {
  1304. start_at = memparse(p+1, &p);
  1305. e820_add_region(start_at, mem_size, E820_RESERVED);
  1306. } else
  1307. e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
  1308. return *p == '\0' ? 0 : -EINVAL;
  1309. }
  1310. early_param("memmap", parse_memmap_opt);
  1311. void __init finish_e820_parsing(void)
  1312. {
  1313. if (userdef) {
  1314. u32 nr = e820.nr_map;
  1315. if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
  1316. early_panic("Invalid user supplied memory map");
  1317. e820.nr_map = nr;
  1318. printk(KERN_INFO "user-defined physical RAM map:\n");
  1319. e820_print_map("user");
  1320. }
  1321. }
  1322. static inline const char *e820_type_to_string(int e820_type)
  1323. {
  1324. switch (e820_type) {
  1325. case E820_RESERVED_KERN:
  1326. case E820_RAM: return "System RAM";
  1327. case E820_ACPI: return "ACPI Tables";
  1328. case E820_NVS: return "ACPI Non-volatile Storage";
  1329. case E820_UNUSABLE: return "Unusable memory";
  1330. default: return "reserved";
  1331. }
  1332. }
  1333. /*
  1334. * Mark e820 reserved areas as busy for the resource manager.
  1335. */
  1336. static struct resource __initdata *e820_res;
  1337. void __init e820_reserve_resources(void)
  1338. {
  1339. int i;
  1340. struct resource *res;
  1341. u64 end;
  1342. res = alloc_bootmem(sizeof(struct resource) * e820.nr_map);
  1343. e820_res = res;
  1344. for (i = 0; i < e820.nr_map; i++) {
  1345. end = e820.map[i].addr + e820.map[i].size - 1;
  1346. if (end != (resource_size_t)end) {
  1347. res++;
  1348. continue;
  1349. }
  1350. res->name = e820_type_to_string(e820.map[i].type);
  1351. res->start = e820.map[i].addr;
  1352. res->end = end;
  1353. res->flags = IORESOURCE_MEM;
  1354. /*
  1355. * don't register the region that could be conflicted with
  1356. * pci device BAR resource and insert them later in
  1357. * pcibios_resource_survey()
  1358. */
  1359. if (e820.map[i].type != E820_RESERVED || res->start < (1ULL<<20)) {
  1360. res->flags |= IORESOURCE_BUSY;
  1361. insert_resource(&iomem_resource, res);
  1362. }
  1363. res++;
  1364. }
  1365. for (i = 0; i < e820_saved.nr_map; i++) {
  1366. struct e820entry *entry = &e820_saved.map[i];
  1367. firmware_map_add_early(entry->addr,
  1368. entry->addr + entry->size - 1,
  1369. e820_type_to_string(entry->type));
  1370. }
  1371. }
  1372. /* How much should we pad RAM ending depending on where it is? */
  1373. static unsigned long ram_alignment(resource_size_t pos)
  1374. {
  1375. unsigned long mb = pos >> 20;
  1376. /* To 64kB in the first megabyte */
  1377. if (!mb)
  1378. return 64*1024;
  1379. /* To 1MB in the first 16MB */
  1380. if (mb < 16)
  1381. return 1024*1024;
  1382. /* To 64MB for anything above that */
  1383. return 64*1024*1024;
  1384. }
  1385. #define MAX_RESOURCE_SIZE ((resource_size_t)-1)
  1386. void __init e820_reserve_resources_late(void)
  1387. {
  1388. int i;
  1389. struct resource *res;
  1390. res = e820_res;
  1391. for (i = 0; i < e820.nr_map; i++) {
  1392. if (!res->parent && res->end)
  1393. insert_resource_expand_to_fit(&iomem_resource, res);
  1394. res++;
  1395. }
  1396. /*
  1397. * Try to bump up RAM regions to reasonable boundaries to
  1398. * avoid stolen RAM:
  1399. */
  1400. for (i = 0; i < e820.nr_map; i++) {
  1401. struct e820entry *entry = &e820.map[i];
  1402. u64 start, end;
  1403. if (entry->type != E820_RAM)
  1404. continue;
  1405. start = entry->addr + entry->size;
  1406. end = round_up(start, ram_alignment(start)) - 1;
  1407. if (end > MAX_RESOURCE_SIZE)
  1408. end = MAX_RESOURCE_SIZE;
  1409. if (start >= end)
  1410. continue;
  1411. printk(KERN_DEBUG "reserve RAM buffer: %016llx - %016llx ",
  1412. start, end);
  1413. reserve_region_with_split(&iomem_resource, start, end,
  1414. "RAM buffer");
  1415. }
  1416. }
  1417. char *__init default_machine_specific_memory_setup(void)
  1418. {
  1419. char *who = "BIOS-e820";
  1420. u32 new_nr;
  1421. /*
  1422. * Try to copy the BIOS-supplied E820-map.
  1423. *
  1424. * Otherwise fake a memory map; one section from 0k->640k,
  1425. * the next section from 1mb->appropriate_mem_k
  1426. */
  1427. new_nr = boot_params.e820_entries;
  1428. sanitize_e820_map(boot_params.e820_map,
  1429. ARRAY_SIZE(boot_params.e820_map),
  1430. &new_nr);
  1431. boot_params.e820_entries = new_nr;
  1432. if (append_e820_map(boot_params.e820_map, boot_params.e820_entries)
  1433. < 0) {
  1434. u64 mem_size;
  1435. /* compare results from other methods and take the greater */
  1436. if (boot_params.alt_mem_k
  1437. < boot_params.screen_info.ext_mem_k) {
  1438. mem_size = boot_params.screen_info.ext_mem_k;
  1439. who = "BIOS-88";
  1440. } else {
  1441. mem_size = boot_params.alt_mem_k;
  1442. who = "BIOS-e801";
  1443. }
  1444. e820.nr_map = 0;
  1445. e820_add_region(0, LOWMEMSIZE(), E820_RAM);
  1446. e820_add_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
  1447. }
  1448. /* In case someone cares... */
  1449. return who;
  1450. }
  1451. void __init setup_memory_map(void)
  1452. {
  1453. char *who;
  1454. who = x86_init.resources.memory_setup();
  1455. memcpy(&e820_saved, &e820, sizeof(struct e820map));
  1456. printk(KERN_INFO "BIOS-provided physical RAM map:\n");
  1457. e820_print_map(who);
  1458. }