e820.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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 <asm/pgtable.h>
  20. #include <asm/page.h>
  21. #include <asm/e820.h>
  22. #include <asm/proto.h>
  23. #include <asm/bootsetup.h>
  24. #include <asm/sections.h>
  25. /*
  26. * PFN of last memory page.
  27. */
  28. unsigned long end_pfn;
  29. EXPORT_SYMBOL(end_pfn);
  30. /*
  31. * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
  32. * The direct mapping extends to end_pfn_map, so that we can directly access
  33. * apertures, ACPI and other tables without having to play with fixmaps.
  34. */
  35. unsigned long end_pfn_map;
  36. /*
  37. * Last pfn which the user wants to use.
  38. */
  39. unsigned long end_user_pfn = MAXMEM>>PAGE_SHIFT;
  40. extern struct resource code_resource, data_resource;
  41. /* Check for some hardcoded bad areas that early boot is not allowed to touch */
  42. static inline int bad_addr(unsigned long *addrp, unsigned long size)
  43. {
  44. unsigned long addr = *addrp, last = addr + size;
  45. /* various gunk below that needed for SMP startup */
  46. if (addr < 0x8000) {
  47. *addrp = 0x8000;
  48. return 1;
  49. }
  50. /* direct mapping tables of the kernel */
  51. if (last >= table_start<<PAGE_SHIFT && addr < table_end<<PAGE_SHIFT) {
  52. *addrp = table_end << PAGE_SHIFT;
  53. return 1;
  54. }
  55. /* initrd */
  56. #ifdef CONFIG_BLK_DEV_INITRD
  57. if (LOADER_TYPE && INITRD_START && last >= INITRD_START &&
  58. addr < INITRD_START+INITRD_SIZE) {
  59. *addrp = INITRD_START + INITRD_SIZE;
  60. return 1;
  61. }
  62. #endif
  63. /* kernel code + 640k memory hole (later should not be needed, but
  64. be paranoid for now) */
  65. if (last >= 640*1024 && addr < 1024*1024) {
  66. *addrp = 1024*1024;
  67. return 1;
  68. }
  69. if (last >= __pa_symbol(&_text) && last < __pa_symbol(&_end)) {
  70. *addrp = __pa_symbol(&_end);
  71. return 1;
  72. }
  73. if (last >= ebda_addr && addr < ebda_addr + ebda_size) {
  74. *addrp = ebda_addr + ebda_size;
  75. return 1;
  76. }
  77. /* XXX ramdisk image here? */
  78. return 0;
  79. }
  80. /*
  81. * This function checks if any part of the range <start,end> is mapped
  82. * with type.
  83. */
  84. int __meminit
  85. e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
  86. {
  87. int i;
  88. for (i = 0; i < e820.nr_map; i++) {
  89. struct e820entry *ei = &e820.map[i];
  90. if (type && ei->type != type)
  91. continue;
  92. if (ei->addr >= end || ei->addr + ei->size <= start)
  93. continue;
  94. return 1;
  95. }
  96. return 0;
  97. }
  98. /*
  99. * Find a free area in a specific range.
  100. */
  101. unsigned long __init find_e820_area(unsigned long start, unsigned long end, unsigned size)
  102. {
  103. int i;
  104. for (i = 0; i < e820.nr_map; i++) {
  105. struct e820entry *ei = &e820.map[i];
  106. unsigned long addr = ei->addr, last;
  107. if (ei->type != E820_RAM)
  108. continue;
  109. if (addr < start)
  110. addr = start;
  111. if (addr > ei->addr + ei->size)
  112. continue;
  113. while (bad_addr(&addr, size) && addr+size <= ei->addr+ei->size)
  114. ;
  115. last = addr + size;
  116. if (last > ei->addr + ei->size)
  117. continue;
  118. if (last > end)
  119. continue;
  120. return addr;
  121. }
  122. return -1UL;
  123. }
  124. /*
  125. * Free bootmem based on the e820 table for a node.
  126. */
  127. void __init e820_bootmem_free(pg_data_t *pgdat, unsigned long start,unsigned long end)
  128. {
  129. int i;
  130. for (i = 0; i < e820.nr_map; i++) {
  131. struct e820entry *ei = &e820.map[i];
  132. unsigned long last, addr;
  133. if (ei->type != E820_RAM ||
  134. ei->addr+ei->size <= start ||
  135. ei->addr >= end)
  136. continue;
  137. addr = round_up(ei->addr, PAGE_SIZE);
  138. if (addr < start)
  139. addr = start;
  140. last = round_down(ei->addr + ei->size, PAGE_SIZE);
  141. if (last >= end)
  142. last = end;
  143. if (last > addr && last-addr >= PAGE_SIZE)
  144. free_bootmem_node(pgdat, addr, last-addr);
  145. }
  146. }
  147. /*
  148. * Find the highest page frame number we have available
  149. */
  150. unsigned long __init e820_end_of_ram(void)
  151. {
  152. int i;
  153. unsigned long end_pfn = 0;
  154. for (i = 0; i < e820.nr_map; i++) {
  155. struct e820entry *ei = &e820.map[i];
  156. unsigned long start, end;
  157. start = round_up(ei->addr, PAGE_SIZE);
  158. end = round_down(ei->addr + ei->size, PAGE_SIZE);
  159. if (start >= end)
  160. continue;
  161. if (ei->type == E820_RAM) {
  162. if (end > end_pfn<<PAGE_SHIFT)
  163. end_pfn = end>>PAGE_SHIFT;
  164. } else {
  165. if (end > end_pfn_map<<PAGE_SHIFT)
  166. end_pfn_map = end>>PAGE_SHIFT;
  167. }
  168. }
  169. if (end_pfn > end_pfn_map)
  170. end_pfn_map = end_pfn;
  171. if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
  172. end_pfn_map = MAXMEM>>PAGE_SHIFT;
  173. if (end_pfn > end_user_pfn)
  174. end_pfn = end_user_pfn;
  175. if (end_pfn > end_pfn_map)
  176. end_pfn = end_pfn_map;
  177. return end_pfn;
  178. }
  179. /*
  180. * Compute how much memory is missing in a range.
  181. * Unlike the other functions in this file the arguments are in page numbers.
  182. */
  183. unsigned long __init
  184. e820_hole_size(unsigned long start_pfn, unsigned long end_pfn)
  185. {
  186. unsigned long ram = 0;
  187. unsigned long start = start_pfn << PAGE_SHIFT;
  188. unsigned long end = end_pfn << PAGE_SHIFT;
  189. int i;
  190. for (i = 0; i < e820.nr_map; i++) {
  191. struct e820entry *ei = &e820.map[i];
  192. unsigned long last, addr;
  193. if (ei->type != E820_RAM ||
  194. ei->addr+ei->size <= start ||
  195. ei->addr >= end)
  196. continue;
  197. addr = round_up(ei->addr, PAGE_SIZE);
  198. if (addr < start)
  199. addr = start;
  200. last = round_down(ei->addr + ei->size, PAGE_SIZE);
  201. if (last >= end)
  202. last = end;
  203. if (last > addr)
  204. ram += last - addr;
  205. }
  206. return ((end - start) - ram) >> PAGE_SHIFT;
  207. }
  208. /*
  209. * Mark e820 reserved areas as busy for the resource manager.
  210. */
  211. void __init e820_reserve_resources(void)
  212. {
  213. int i;
  214. for (i = 0; i < e820.nr_map; i++) {
  215. struct resource *res;
  216. res = alloc_bootmem_low(sizeof(struct resource));
  217. switch (e820.map[i].type) {
  218. case E820_RAM: res->name = "System RAM"; break;
  219. case E820_ACPI: res->name = "ACPI Tables"; break;
  220. case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
  221. default: res->name = "reserved";
  222. }
  223. res->start = e820.map[i].addr;
  224. res->end = res->start + e820.map[i].size - 1;
  225. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  226. request_resource(&iomem_resource, res);
  227. if (e820.map[i].type == E820_RAM) {
  228. /*
  229. * We don't know which RAM region contains kernel data,
  230. * so we try it repeatedly and let the resource manager
  231. * test it.
  232. */
  233. request_resource(res, &code_resource);
  234. request_resource(res, &data_resource);
  235. #ifdef CONFIG_KEXEC
  236. request_resource(res, &crashk_res);
  237. #endif
  238. }
  239. }
  240. }
  241. /*
  242. * Add a memory region to the kernel e820 map.
  243. */
  244. void __init add_memory_region(unsigned long start, unsigned long size, int type)
  245. {
  246. int x = e820.nr_map;
  247. if (x == E820MAX) {
  248. printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
  249. return;
  250. }
  251. e820.map[x].addr = start;
  252. e820.map[x].size = size;
  253. e820.map[x].type = type;
  254. e820.nr_map++;
  255. }
  256. void __init e820_print_map(char *who)
  257. {
  258. int i;
  259. for (i = 0; i < e820.nr_map; i++) {
  260. printk(" %s: %016Lx - %016Lx ", who,
  261. (unsigned long long) e820.map[i].addr,
  262. (unsigned long long) (e820.map[i].addr + e820.map[i].size));
  263. switch (e820.map[i].type) {
  264. case E820_RAM: printk("(usable)\n");
  265. break;
  266. case E820_RESERVED:
  267. printk("(reserved)\n");
  268. break;
  269. case E820_ACPI:
  270. printk("(ACPI data)\n");
  271. break;
  272. case E820_NVS:
  273. printk("(ACPI NVS)\n");
  274. break;
  275. default: printk("type %u\n", e820.map[i].type);
  276. break;
  277. }
  278. }
  279. }
  280. /*
  281. * Sanitize the BIOS e820 map.
  282. *
  283. * Some e820 responses include overlapping entries. The following
  284. * replaces the original e820 map with a new one, removing overlaps.
  285. *
  286. */
  287. static int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
  288. {
  289. struct change_member {
  290. struct e820entry *pbios; /* pointer to original bios entry */
  291. unsigned long long addr; /* address for this change point */
  292. };
  293. static struct change_member change_point_list[2*E820MAX] __initdata;
  294. static struct change_member *change_point[2*E820MAX] __initdata;
  295. static struct e820entry *overlap_list[E820MAX] __initdata;
  296. static struct e820entry new_bios[E820MAX] __initdata;
  297. struct change_member *change_tmp;
  298. unsigned long current_type, last_type;
  299. unsigned long long last_addr;
  300. int chgidx, still_changing;
  301. int overlap_entries;
  302. int new_bios_entry;
  303. int old_nr, new_nr, chg_nr;
  304. int i;
  305. /*
  306. Visually we're performing the following (1,2,3,4 = memory types)...
  307. Sample memory map (w/overlaps):
  308. ____22__________________
  309. ______________________4_
  310. ____1111________________
  311. _44_____________________
  312. 11111111________________
  313. ____________________33__
  314. ___________44___________
  315. __________33333_________
  316. ______________22________
  317. ___________________2222_
  318. _________111111111______
  319. _____________________11_
  320. _________________4______
  321. Sanitized equivalent (no overlap):
  322. 1_______________________
  323. _44_____________________
  324. ___1____________________
  325. ____22__________________
  326. ______11________________
  327. _________1______________
  328. __________3_____________
  329. ___________44___________
  330. _____________33_________
  331. _______________2________
  332. ________________1_______
  333. _________________4______
  334. ___________________2____
  335. ____________________33__
  336. ______________________4_
  337. */
  338. /* if there's only one memory region, don't bother */
  339. if (*pnr_map < 2)
  340. return -1;
  341. old_nr = *pnr_map;
  342. /* bail out if we find any unreasonable addresses in bios map */
  343. for (i=0; i<old_nr; i++)
  344. if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
  345. return -1;
  346. /* create pointers for initial change-point information (for sorting) */
  347. for (i=0; i < 2*old_nr; i++)
  348. change_point[i] = &change_point_list[i];
  349. /* record all known change-points (starting and ending addresses),
  350. omitting those that are for empty memory regions */
  351. chgidx = 0;
  352. for (i=0; i < old_nr; i++) {
  353. if (biosmap[i].size != 0) {
  354. change_point[chgidx]->addr = biosmap[i].addr;
  355. change_point[chgidx++]->pbios = &biosmap[i];
  356. change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
  357. change_point[chgidx++]->pbios = &biosmap[i];
  358. }
  359. }
  360. chg_nr = chgidx;
  361. /* sort change-point list by memory addresses (low -> high) */
  362. still_changing = 1;
  363. while (still_changing) {
  364. still_changing = 0;
  365. for (i=1; i < chg_nr; i++) {
  366. /* if <current_addr> > <last_addr>, swap */
  367. /* or, if current=<start_addr> & last=<end_addr>, swap */
  368. if ((change_point[i]->addr < change_point[i-1]->addr) ||
  369. ((change_point[i]->addr == change_point[i-1]->addr) &&
  370. (change_point[i]->addr == change_point[i]->pbios->addr) &&
  371. (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
  372. )
  373. {
  374. change_tmp = change_point[i];
  375. change_point[i] = change_point[i-1];
  376. change_point[i-1] = change_tmp;
  377. still_changing=1;
  378. }
  379. }
  380. }
  381. /* create a new bios memory map, removing overlaps */
  382. overlap_entries=0; /* number of entries in the overlap table */
  383. new_bios_entry=0; /* index for creating new bios map entries */
  384. last_type = 0; /* start with undefined memory type */
  385. last_addr = 0; /* start with 0 as last starting address */
  386. /* loop through change-points, determining affect on the new bios map */
  387. for (chgidx=0; chgidx < chg_nr; chgidx++)
  388. {
  389. /* keep track of all overlapping bios entries */
  390. if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
  391. {
  392. /* add map entry to overlap list (> 1 entry implies an overlap) */
  393. overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
  394. }
  395. else
  396. {
  397. /* remove entry from list (order independent, so swap with last) */
  398. for (i=0; i<overlap_entries; i++)
  399. {
  400. if (overlap_list[i] == change_point[chgidx]->pbios)
  401. overlap_list[i] = overlap_list[overlap_entries-1];
  402. }
  403. overlap_entries--;
  404. }
  405. /* if there are overlapping entries, decide which "type" to use */
  406. /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
  407. current_type = 0;
  408. for (i=0; i<overlap_entries; i++)
  409. if (overlap_list[i]->type > current_type)
  410. current_type = overlap_list[i]->type;
  411. /* continue building up new bios map based on this information */
  412. if (current_type != last_type) {
  413. if (last_type != 0) {
  414. new_bios[new_bios_entry].size =
  415. change_point[chgidx]->addr - last_addr;
  416. /* move forward only if the new size was non-zero */
  417. if (new_bios[new_bios_entry].size != 0)
  418. if (++new_bios_entry >= E820MAX)
  419. break; /* no more space left for new bios entries */
  420. }
  421. if (current_type != 0) {
  422. new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
  423. new_bios[new_bios_entry].type = current_type;
  424. last_addr=change_point[chgidx]->addr;
  425. }
  426. last_type = current_type;
  427. }
  428. }
  429. new_nr = new_bios_entry; /* retain count for new bios entries */
  430. /* copy new bios mapping into original location */
  431. memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
  432. *pnr_map = new_nr;
  433. return 0;
  434. }
  435. /*
  436. * Copy the BIOS e820 map into a safe place.
  437. *
  438. * Sanity-check it while we're at it..
  439. *
  440. * If we're lucky and live on a modern system, the setup code
  441. * will have given us a memory map that we can use to properly
  442. * set up memory. If we aren't, we'll fake a memory map.
  443. *
  444. * We check to see that the memory map contains at least 2 elements
  445. * before we'll use it, because the detection code in setup.S may
  446. * not be perfect and most every PC known to man has two memory
  447. * regions: one from 0 to 640k, and one from 1mb up. (The IBM
  448. * thinkpad 560x, for example, does not cooperate with the memory
  449. * detection code.)
  450. */
  451. static int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
  452. {
  453. /* Only one memory region (or negative)? Ignore it */
  454. if (nr_map < 2)
  455. return -1;
  456. do {
  457. unsigned long start = biosmap->addr;
  458. unsigned long size = biosmap->size;
  459. unsigned long end = start + size;
  460. unsigned long type = biosmap->type;
  461. /* Overflow in 64 bits? Ignore the memory map. */
  462. if (start > end)
  463. return -1;
  464. /*
  465. * Some BIOSes claim RAM in the 640k - 1M region.
  466. * Not right. Fix it up.
  467. *
  468. * This should be removed on Hammer which is supposed to not
  469. * have non e820 covered ISA mappings there, but I had some strange
  470. * problems so it stays for now. -AK
  471. */
  472. if (type == E820_RAM) {
  473. if (start < 0x100000ULL && end > 0xA0000ULL) {
  474. if (start < 0xA0000ULL)
  475. add_memory_region(start, 0xA0000ULL-start, type);
  476. if (end <= 0x100000ULL)
  477. continue;
  478. start = 0x100000ULL;
  479. size = end - start;
  480. }
  481. }
  482. add_memory_region(start, size, type);
  483. } while (biosmap++,--nr_map);
  484. return 0;
  485. }
  486. void __init setup_memory_region(void)
  487. {
  488. char *who = "BIOS-e820";
  489. /*
  490. * Try to copy the BIOS-supplied E820-map.
  491. *
  492. * Otherwise fake a memory map; one section from 0k->640k,
  493. * the next section from 1mb->appropriate_mem_k
  494. */
  495. sanitize_e820_map(E820_MAP, &E820_MAP_NR);
  496. if (copy_e820_map(E820_MAP, E820_MAP_NR) < 0) {
  497. unsigned long mem_size;
  498. /* compare results from other methods and take the greater */
  499. if (ALT_MEM_K < EXT_MEM_K) {
  500. mem_size = EXT_MEM_K;
  501. who = "BIOS-88";
  502. } else {
  503. mem_size = ALT_MEM_K;
  504. who = "BIOS-e801";
  505. }
  506. e820.nr_map = 0;
  507. add_memory_region(0, LOWMEMSIZE(), E820_RAM);
  508. add_memory_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
  509. }
  510. printk(KERN_INFO "BIOS-provided physical RAM map:\n");
  511. e820_print_map(who);
  512. }
  513. void __init parse_memopt(char *p, char **from)
  514. {
  515. end_user_pfn = memparse(p, from);
  516. end_user_pfn >>= PAGE_SHIFT;
  517. }
  518. void __init parse_memmapopt(char *p, char **from)
  519. {
  520. unsigned long long start_at, mem_size;
  521. mem_size = memparse(p, from);
  522. p = *from;
  523. if (*p == '@') {
  524. start_at = memparse(p+1, from);
  525. add_memory_region(start_at, mem_size, E820_RAM);
  526. } else if (*p == '#') {
  527. start_at = memparse(p+1, from);
  528. add_memory_region(start_at, mem_size, E820_ACPI);
  529. } else if (*p == '$') {
  530. start_at = memparse(p+1, from);
  531. add_memory_region(start_at, mem_size, E820_RESERVED);
  532. } else {
  533. end_user_pfn = (mem_size >> PAGE_SHIFT);
  534. }
  535. p = *from;
  536. }
  537. unsigned long pci_mem_start = 0xaeedbabe;
  538. EXPORT_SYMBOL(pci_mem_start);
  539. /*
  540. * Search for the biggest gap in the low 32 bits of the e820
  541. * memory space. We pass this space to PCI to assign MMIO resources
  542. * for hotplug or unconfigured devices in.
  543. * Hopefully the BIOS let enough space left.
  544. */
  545. __init void e820_setup_gap(void)
  546. {
  547. unsigned long gapstart, gapsize, round;
  548. unsigned long last;
  549. int i;
  550. int found = 0;
  551. last = 0x100000000ull;
  552. gapstart = 0x10000000;
  553. gapsize = 0x400000;
  554. i = e820.nr_map;
  555. while (--i >= 0) {
  556. unsigned long long start = e820.map[i].addr;
  557. unsigned long long end = start + e820.map[i].size;
  558. /*
  559. * Since "last" is at most 4GB, we know we'll
  560. * fit in 32 bits if this condition is true
  561. */
  562. if (last > end) {
  563. unsigned long gap = last - end;
  564. if (gap > gapsize) {
  565. gapsize = gap;
  566. gapstart = end;
  567. found = 1;
  568. }
  569. }
  570. if (start < last)
  571. last = start;
  572. }
  573. if (!found) {
  574. gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
  575. printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit address range\n"
  576. KERN_ERR "PCI: Unassigned devices with 32bit resource registers may break!\n");
  577. }
  578. /*
  579. * See how much we want to round up: start off with
  580. * rounding to the next 1MB area.
  581. */
  582. round = 0x100000;
  583. while ((gapsize >> 4) > round)
  584. round += round;
  585. /* Fun with two's complement */
  586. pci_mem_start = (gapstart + round) & -round;
  587. printk(KERN_INFO "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
  588. pci_mem_start, gapstart, gapsize);
  589. }