e820.c 16 KB

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