e820.c 17 KB

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