e820.c 17 KB

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