e820.c 16 KB

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