e820.c 16 KB

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