e820.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. #include <linux/kernel.h>
  2. #include <linux/types.h>
  3. #include <linux/init.h>
  4. #include <linux/bootmem.h>
  5. #include <linux/ioport.h>
  6. #include <linux/string.h>
  7. #include <linux/kexec.h>
  8. #include <linux/module.h>
  9. #include <linux/mm.h>
  10. #include <linux/efi.h>
  11. #include <asm/pgtable.h>
  12. #include <asm/page.h>
  13. #include <asm/e820.h>
  14. #ifdef CONFIG_EFI
  15. int efi_enabled = 0;
  16. EXPORT_SYMBOL(efi_enabled);
  17. #endif
  18. struct e820map e820;
  19. struct change_member {
  20. struct e820entry *pbios; /* pointer to original bios entry */
  21. unsigned long long addr; /* address for this change point */
  22. };
  23. static struct change_member change_point_list[2*E820MAX] __initdata;
  24. static struct change_member *change_point[2*E820MAX] __initdata;
  25. static struct e820entry *overlap_list[E820MAX] __initdata;
  26. static struct e820entry new_bios[E820MAX] __initdata;
  27. struct resource data_resource = {
  28. .name = "Kernel data",
  29. .start = 0,
  30. .end = 0,
  31. .flags = IORESOURCE_BUSY | IORESOURCE_MEM
  32. };
  33. struct resource code_resource = {
  34. .name = "Kernel code",
  35. .start = 0,
  36. .end = 0,
  37. .flags = IORESOURCE_BUSY | IORESOURCE_MEM
  38. };
  39. static struct resource system_rom_resource = {
  40. .name = "System ROM",
  41. .start = 0xf0000,
  42. .end = 0xfffff,
  43. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  44. };
  45. static struct resource extension_rom_resource = {
  46. .name = "Extension ROM",
  47. .start = 0xe0000,
  48. .end = 0xeffff,
  49. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  50. };
  51. static struct resource adapter_rom_resources[] = { {
  52. .name = "Adapter ROM",
  53. .start = 0xc8000,
  54. .end = 0,
  55. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  56. }, {
  57. .name = "Adapter ROM",
  58. .start = 0,
  59. .end = 0,
  60. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  61. }, {
  62. .name = "Adapter ROM",
  63. .start = 0,
  64. .end = 0,
  65. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  66. }, {
  67. .name = "Adapter ROM",
  68. .start = 0,
  69. .end = 0,
  70. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  71. }, {
  72. .name = "Adapter ROM",
  73. .start = 0,
  74. .end = 0,
  75. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  76. }, {
  77. .name = "Adapter ROM",
  78. .start = 0,
  79. .end = 0,
  80. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  81. } };
  82. static struct resource video_rom_resource = {
  83. .name = "Video ROM",
  84. .start = 0xc0000,
  85. .end = 0xc7fff,
  86. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  87. };
  88. static struct resource video_ram_resource = {
  89. .name = "Video RAM area",
  90. .start = 0xa0000,
  91. .end = 0xbffff,
  92. .flags = IORESOURCE_BUSY | IORESOURCE_MEM
  93. };
  94. static struct resource standard_io_resources[] = { {
  95. .name = "dma1",
  96. .start = 0x0000,
  97. .end = 0x001f,
  98. .flags = IORESOURCE_BUSY | IORESOURCE_IO
  99. }, {
  100. .name = "pic1",
  101. .start = 0x0020,
  102. .end = 0x0021,
  103. .flags = IORESOURCE_BUSY | IORESOURCE_IO
  104. }, {
  105. .name = "timer0",
  106. .start = 0x0040,
  107. .end = 0x0043,
  108. .flags = IORESOURCE_BUSY | IORESOURCE_IO
  109. }, {
  110. .name = "timer1",
  111. .start = 0x0050,
  112. .end = 0x0053,
  113. .flags = IORESOURCE_BUSY | IORESOURCE_IO
  114. }, {
  115. .name = "keyboard",
  116. .start = 0x0060,
  117. .end = 0x006f,
  118. .flags = IORESOURCE_BUSY | IORESOURCE_IO
  119. }, {
  120. .name = "dma page reg",
  121. .start = 0x0080,
  122. .end = 0x008f,
  123. .flags = IORESOURCE_BUSY | IORESOURCE_IO
  124. }, {
  125. .name = "pic2",
  126. .start = 0x00a0,
  127. .end = 0x00a1,
  128. .flags = IORESOURCE_BUSY | IORESOURCE_IO
  129. }, {
  130. .name = "dma2",
  131. .start = 0x00c0,
  132. .end = 0x00df,
  133. .flags = IORESOURCE_BUSY | IORESOURCE_IO
  134. }, {
  135. .name = "fpu",
  136. .start = 0x00f0,
  137. .end = 0x00ff,
  138. .flags = IORESOURCE_BUSY | IORESOURCE_IO
  139. } };
  140. #define romsignature(x) (*(unsigned short *)(x) == 0xaa55)
  141. static int __init romchecksum(unsigned char *rom, unsigned long length)
  142. {
  143. unsigned char *p, sum = 0;
  144. for (p = rom; p < rom + length; p++)
  145. sum += *p;
  146. return sum == 0;
  147. }
  148. static void __init probe_roms(void)
  149. {
  150. unsigned long start, length, upper;
  151. unsigned char *rom;
  152. int i;
  153. /* video rom */
  154. upper = adapter_rom_resources[0].start;
  155. for (start = video_rom_resource.start; start < upper; start += 2048) {
  156. rom = isa_bus_to_virt(start);
  157. if (!romsignature(rom))
  158. continue;
  159. video_rom_resource.start = start;
  160. /* 0 < length <= 0x7f * 512, historically */
  161. length = rom[2] * 512;
  162. /* if checksum okay, trust length byte */
  163. if (length && romchecksum(rom, length))
  164. video_rom_resource.end = start + length - 1;
  165. request_resource(&iomem_resource, &video_rom_resource);
  166. break;
  167. }
  168. start = (video_rom_resource.end + 1 + 2047) & ~2047UL;
  169. if (start < upper)
  170. start = upper;
  171. /* system rom */
  172. request_resource(&iomem_resource, &system_rom_resource);
  173. upper = system_rom_resource.start;
  174. /* check for extension rom (ignore length byte!) */
  175. rom = isa_bus_to_virt(extension_rom_resource.start);
  176. if (romsignature(rom)) {
  177. length = extension_rom_resource.end - extension_rom_resource.start + 1;
  178. if (romchecksum(rom, length)) {
  179. request_resource(&iomem_resource, &extension_rom_resource);
  180. upper = extension_rom_resource.start;
  181. }
  182. }
  183. /* check for adapter roms on 2k boundaries */
  184. for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += 2048) {
  185. rom = isa_bus_to_virt(start);
  186. if (!romsignature(rom))
  187. continue;
  188. /* 0 < length <= 0x7f * 512, historically */
  189. length = rom[2] * 512;
  190. /* but accept any length that fits if checksum okay */
  191. if (!length || start + length > upper || !romchecksum(rom, length))
  192. continue;
  193. adapter_rom_resources[i].start = start;
  194. adapter_rom_resources[i].end = start + length - 1;
  195. request_resource(&iomem_resource, &adapter_rom_resources[i]);
  196. start = adapter_rom_resources[i++].end & ~2047UL;
  197. }
  198. }
  199. /*
  200. * Request address space for all standard RAM and ROM resources
  201. * and also for regions reported as reserved by the e820.
  202. */
  203. static void __init
  204. legacy_init_iomem_resources(struct resource *code_resource, struct resource *data_resource)
  205. {
  206. int i;
  207. probe_roms();
  208. for (i = 0; i < e820.nr_map; i++) {
  209. struct resource *res;
  210. #ifndef CONFIG_RESOURCES_64BIT
  211. if (e820.map[i].addr + e820.map[i].size > 0x100000000ULL)
  212. continue;
  213. #endif
  214. res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
  215. switch (e820.map[i].type) {
  216. case E820_RAM: res->name = "System RAM"; break;
  217. case E820_ACPI: res->name = "ACPI Tables"; break;
  218. case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
  219. default: res->name = "reserved";
  220. }
  221. res->start = e820.map[i].addr;
  222. res->end = res->start + e820.map[i].size - 1;
  223. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  224. if (request_resource(&iomem_resource, res)) {
  225. kfree(res);
  226. continue;
  227. }
  228. if (e820.map[i].type == E820_RAM) {
  229. /*
  230. * We don't know which RAM region contains kernel data,
  231. * so we try it repeatedly and let the resource manager
  232. * test it.
  233. */
  234. request_resource(res, code_resource);
  235. request_resource(res, data_resource);
  236. #ifdef CONFIG_KEXEC
  237. request_resource(res, &crashk_res);
  238. #endif
  239. }
  240. }
  241. }
  242. /*
  243. * Request address space for all standard resources
  244. *
  245. * This is called just before pcibios_init(), which is also a
  246. * subsys_initcall, but is linked in later (in arch/i386/pci/common.c).
  247. */
  248. static int __init request_standard_resources(void)
  249. {
  250. int i;
  251. printk("Setting up standard PCI resources\n");
  252. if (efi_enabled)
  253. efi_initialize_iomem_resources(&code_resource, &data_resource);
  254. else
  255. legacy_init_iomem_resources(&code_resource, &data_resource);
  256. /* EFI systems may still have VGA */
  257. request_resource(&iomem_resource, &video_ram_resource);
  258. /* request I/O space for devices used on all i[345]86 PCs */
  259. for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
  260. request_resource(&ioport_resource, &standard_io_resources[i]);
  261. return 0;
  262. }
  263. subsys_initcall(request_standard_resources);
  264. void __init add_memory_region(unsigned long long start,
  265. unsigned long long size, int type)
  266. {
  267. int x;
  268. if (!efi_enabled) {
  269. x = e820.nr_map;
  270. if (x == E820MAX) {
  271. printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
  272. return;
  273. }
  274. e820.map[x].addr = start;
  275. e820.map[x].size = size;
  276. e820.map[x].type = type;
  277. e820.nr_map++;
  278. }
  279. } /* add_memory_region */
  280. /*
  281. * Sanitize the BIOS e820 map.
  282. *
  283. * Some e820 responses include overlapping entries. The following
  284. * replaces the original e820 map with a new one, removing overlaps.
  285. *
  286. */
  287. int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
  288. {
  289. struct change_member *change_tmp;
  290. unsigned long current_type, last_type;
  291. unsigned long long last_addr;
  292. int chgidx, still_changing;
  293. int overlap_entries;
  294. int new_bios_entry;
  295. int old_nr, new_nr, chg_nr;
  296. int i;
  297. /*
  298. Visually we're performing the following (1,2,3,4 = memory types)...
  299. Sample memory map (w/overlaps):
  300. ____22__________________
  301. ______________________4_
  302. ____1111________________
  303. _44_____________________
  304. 11111111________________
  305. ____________________33__
  306. ___________44___________
  307. __________33333_________
  308. ______________22________
  309. ___________________2222_
  310. _________111111111______
  311. _____________________11_
  312. _________________4______
  313. Sanitized equivalent (no overlap):
  314. 1_______________________
  315. _44_____________________
  316. ___1____________________
  317. ____22__________________
  318. ______11________________
  319. _________1______________
  320. __________3_____________
  321. ___________44___________
  322. _____________33_________
  323. _______________2________
  324. ________________1_______
  325. _________________4______
  326. ___________________2____
  327. ____________________33__
  328. ______________________4_
  329. */
  330. printk("sanitize start\n");
  331. /* if there's only one memory region, don't bother */
  332. if (*pnr_map < 2) {
  333. printk("sanitize bail 0\n");
  334. return -1;
  335. }
  336. old_nr = *pnr_map;
  337. /* bail out if we find any unreasonable addresses in bios map */
  338. for (i=0; i<old_nr; i++)
  339. if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) {
  340. printk("sanitize bail 1\n");
  341. return -1;
  342. }
  343. /* create pointers for initial change-point information (for sorting) */
  344. for (i=0; i < 2*old_nr; i++)
  345. change_point[i] = &change_point_list[i];
  346. /* record all known change-points (starting and ending addresses),
  347. omitting those that are for empty memory regions */
  348. chgidx = 0;
  349. for (i=0; i < old_nr; i++) {
  350. if (biosmap[i].size != 0) {
  351. change_point[chgidx]->addr = biosmap[i].addr;
  352. change_point[chgidx++]->pbios = &biosmap[i];
  353. change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
  354. change_point[chgidx++]->pbios = &biosmap[i];
  355. }
  356. }
  357. chg_nr = chgidx; /* true number of change-points */
  358. /* sort change-point list by memory addresses (low -> high) */
  359. still_changing = 1;
  360. while (still_changing) {
  361. still_changing = 0;
  362. for (i=1; i < chg_nr; i++) {
  363. /* if <current_addr> > <last_addr>, swap */
  364. /* or, if current=<start_addr> & last=<end_addr>, swap */
  365. if ((change_point[i]->addr < change_point[i-1]->addr) ||
  366. ((change_point[i]->addr == change_point[i-1]->addr) &&
  367. (change_point[i]->addr == change_point[i]->pbios->addr) &&
  368. (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
  369. )
  370. {
  371. change_tmp = change_point[i];
  372. change_point[i] = change_point[i-1];
  373. change_point[i-1] = change_tmp;
  374. still_changing=1;
  375. }
  376. }
  377. }
  378. /* create a new bios memory map, removing overlaps */
  379. overlap_entries=0; /* number of entries in the overlap table */
  380. new_bios_entry=0; /* index for creating new bios map entries */
  381. last_type = 0; /* start with undefined memory type */
  382. last_addr = 0; /* start with 0 as last starting address */
  383. /* loop through change-points, determining affect on the new bios map */
  384. for (chgidx=0; chgidx < chg_nr; chgidx++)
  385. {
  386. /* keep track of all overlapping bios entries */
  387. if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
  388. {
  389. /* add map entry to overlap list (> 1 entry implies an overlap) */
  390. overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
  391. }
  392. else
  393. {
  394. /* remove entry from list (order independent, so swap with last) */
  395. for (i=0; i<overlap_entries; i++)
  396. {
  397. if (overlap_list[i] == change_point[chgidx]->pbios)
  398. overlap_list[i] = overlap_list[overlap_entries-1];
  399. }
  400. overlap_entries--;
  401. }
  402. /* if there are overlapping entries, decide which "type" to use */
  403. /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
  404. current_type = 0;
  405. for (i=0; i<overlap_entries; i++)
  406. if (overlap_list[i]->type > current_type)
  407. current_type = overlap_list[i]->type;
  408. /* continue building up new bios map based on this information */
  409. if (current_type != last_type) {
  410. if (last_type != 0) {
  411. new_bios[new_bios_entry].size =
  412. change_point[chgidx]->addr - last_addr;
  413. /* move forward only if the new size was non-zero */
  414. if (new_bios[new_bios_entry].size != 0)
  415. if (++new_bios_entry >= E820MAX)
  416. break; /* no more space left for new bios entries */
  417. }
  418. if (current_type != 0) {
  419. new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
  420. new_bios[new_bios_entry].type = current_type;
  421. last_addr=change_point[chgidx]->addr;
  422. }
  423. last_type = current_type;
  424. }
  425. }
  426. new_nr = new_bios_entry; /* retain count for new bios entries */
  427. /* copy new bios mapping into original location */
  428. memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
  429. *pnr_map = new_nr;
  430. printk("sanitize end\n");
  431. return 0;
  432. }
  433. /*
  434. * Copy the BIOS e820 map into a safe place.
  435. *
  436. * Sanity-check it while we're at it..
  437. *
  438. * If we're lucky and live on a modern system, the setup code
  439. * will have given us a memory map that we can use to properly
  440. * set up memory. If we aren't, we'll fake a memory map.
  441. *
  442. * We check to see that the memory map contains at least 2 elements
  443. * before we'll use it, because the detection code in setup.S may
  444. * not be perfect and most every PC known to man has two memory
  445. * regions: one from 0 to 640k, and one from 1mb up. (The IBM
  446. * thinkpad 560x, for example, does not cooperate with the memory
  447. * detection code.)
  448. */
  449. int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
  450. {
  451. /* Only one memory region (or negative)? Ignore it */
  452. if (nr_map < 2)
  453. return -1;
  454. do {
  455. unsigned long long start = biosmap->addr;
  456. unsigned long long size = biosmap->size;
  457. unsigned long long end = start + size;
  458. unsigned long type = biosmap->type;
  459. printk("copy_e820_map() start: %016Lx size: %016Lx end: %016Lx type: %ld\n", start, size, end, type);
  460. /* Overflow in 64 bits? Ignore the memory map. */
  461. if (start > end)
  462. return -1;
  463. /*
  464. * Some BIOSes claim RAM in the 640k - 1M region.
  465. * Not right. Fix it up.
  466. */
  467. if (type == E820_RAM) {
  468. printk("copy_e820_map() type is E820_RAM\n");
  469. if (start < 0x100000ULL && end > 0xA0000ULL) {
  470. printk("copy_e820_map() lies in range...\n");
  471. if (start < 0xA0000ULL) {
  472. printk("copy_e820_map() start < 0xA0000ULL\n");
  473. add_memory_region(start, 0xA0000ULL-start, type);
  474. }
  475. if (end <= 0x100000ULL) {
  476. printk("copy_e820_map() end <= 0x100000ULL\n");
  477. continue;
  478. }
  479. start = 0x100000ULL;
  480. size = end - start;
  481. }
  482. }
  483. add_memory_region(start, size, type);
  484. } while (biosmap++,--nr_map);
  485. return 0;
  486. }