efi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * Common EFI (Extensible Firmware Interface) support functions
  3. * Based on Extensible Firmware Interface Specification version 1.0
  4. *
  5. * Copyright (C) 1999 VA Linux Systems
  6. * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
  7. * Copyright (C) 1999-2002 Hewlett-Packard Co.
  8. * David Mosberger-Tang <davidm@hpl.hp.com>
  9. * Stephane Eranian <eranian@hpl.hp.com>
  10. * Copyright (C) 2005-2008 Intel Co.
  11. * Fenghua Yu <fenghua.yu@intel.com>
  12. * Bibo Mao <bibo.mao@intel.com>
  13. * Chandramouli Narayanan <mouli@linux.intel.com>
  14. * Huang Ying <ying.huang@intel.com>
  15. *
  16. * Copied from efi_32.c to eliminate the duplicated code between EFI
  17. * 32/64 support code. --ying 2007-10-26
  18. *
  19. * All EFI Runtime Services are not implemented yet as EFI only
  20. * supports physical mode addressing on SoftSDV. This is to be fixed
  21. * in a future version. --drummond 1999-07-20
  22. *
  23. * Implemented EFI runtime services and virtual mode calls. --davidm
  24. *
  25. * Goutham Rao: <goutham.rao@intel.com>
  26. * Skip non-WB memory and ignore empty memory ranges.
  27. */
  28. #include <linux/kernel.h>
  29. #include <linux/init.h>
  30. #include <linux/efi.h>
  31. #include <linux/bootmem.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/time.h>
  35. #include <linux/io.h>
  36. #include <linux/reboot.h>
  37. #include <linux/bcd.h>
  38. #include <asm/setup.h>
  39. #include <asm/efi.h>
  40. #include <asm/time.h>
  41. #include <asm/cacheflush.h>
  42. #include <asm/tlbflush.h>
  43. #include <asm/x86_init.h>
  44. #define EFI_DEBUG 1
  45. #define PFX "EFI: "
  46. int efi_enabled;
  47. EXPORT_SYMBOL(efi_enabled);
  48. struct efi efi;
  49. EXPORT_SYMBOL(efi);
  50. struct efi_memory_map memmap;
  51. static struct efi efi_phys __initdata;
  52. static efi_system_table_t efi_systab __initdata;
  53. static int __init setup_noefi(char *arg)
  54. {
  55. efi_enabled = 0;
  56. return 0;
  57. }
  58. early_param("noefi", setup_noefi);
  59. int add_efi_memmap;
  60. EXPORT_SYMBOL(add_efi_memmap);
  61. static int __init setup_add_efi_memmap(char *arg)
  62. {
  63. add_efi_memmap = 1;
  64. return 0;
  65. }
  66. early_param("add_efi_memmap", setup_add_efi_memmap);
  67. static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  68. {
  69. return efi_call_virt2(get_time, tm, tc);
  70. }
  71. static efi_status_t virt_efi_set_time(efi_time_t *tm)
  72. {
  73. return efi_call_virt1(set_time, tm);
  74. }
  75. static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
  76. efi_bool_t *pending,
  77. efi_time_t *tm)
  78. {
  79. return efi_call_virt3(get_wakeup_time,
  80. enabled, pending, tm);
  81. }
  82. static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  83. {
  84. return efi_call_virt2(set_wakeup_time,
  85. enabled, tm);
  86. }
  87. static efi_status_t virt_efi_get_variable(efi_char16_t *name,
  88. efi_guid_t *vendor,
  89. u32 *attr,
  90. unsigned long *data_size,
  91. void *data)
  92. {
  93. return efi_call_virt5(get_variable,
  94. name, vendor, attr,
  95. data_size, data);
  96. }
  97. static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
  98. efi_char16_t *name,
  99. efi_guid_t *vendor)
  100. {
  101. return efi_call_virt3(get_next_variable,
  102. name_size, name, vendor);
  103. }
  104. static efi_status_t virt_efi_set_variable(efi_char16_t *name,
  105. efi_guid_t *vendor,
  106. unsigned long attr,
  107. unsigned long data_size,
  108. void *data)
  109. {
  110. return efi_call_virt5(set_variable,
  111. name, vendor, attr,
  112. data_size, data);
  113. }
  114. static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
  115. {
  116. return efi_call_virt1(get_next_high_mono_count, count);
  117. }
  118. static void virt_efi_reset_system(int reset_type,
  119. efi_status_t status,
  120. unsigned long data_size,
  121. efi_char16_t *data)
  122. {
  123. efi_call_virt4(reset_system, reset_type, status,
  124. data_size, data);
  125. }
  126. static efi_status_t virt_efi_set_virtual_address_map(
  127. unsigned long memory_map_size,
  128. unsigned long descriptor_size,
  129. u32 descriptor_version,
  130. efi_memory_desc_t *virtual_map)
  131. {
  132. return efi_call_virt4(set_virtual_address_map,
  133. memory_map_size, descriptor_size,
  134. descriptor_version, virtual_map);
  135. }
  136. static efi_status_t __init phys_efi_set_virtual_address_map(
  137. unsigned long memory_map_size,
  138. unsigned long descriptor_size,
  139. u32 descriptor_version,
  140. efi_memory_desc_t *virtual_map)
  141. {
  142. efi_status_t status;
  143. efi_call_phys_prelog();
  144. status = efi_call_phys4(efi_phys.set_virtual_address_map,
  145. memory_map_size, descriptor_size,
  146. descriptor_version, virtual_map);
  147. efi_call_phys_epilog();
  148. return status;
  149. }
  150. static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
  151. efi_time_cap_t *tc)
  152. {
  153. efi_status_t status;
  154. efi_call_phys_prelog();
  155. status = efi_call_phys2(efi_phys.get_time, tm, tc);
  156. efi_call_phys_epilog();
  157. return status;
  158. }
  159. int efi_set_rtc_mmss(unsigned long nowtime)
  160. {
  161. int real_seconds, real_minutes;
  162. efi_status_t status;
  163. efi_time_t eft;
  164. efi_time_cap_t cap;
  165. status = efi.get_time(&eft, &cap);
  166. if (status != EFI_SUCCESS) {
  167. printk(KERN_ERR "Oops: efitime: can't read time!\n");
  168. return -1;
  169. }
  170. real_seconds = nowtime % 60;
  171. real_minutes = nowtime / 60;
  172. if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
  173. real_minutes += 30;
  174. real_minutes %= 60;
  175. eft.minute = real_minutes;
  176. eft.second = real_seconds;
  177. status = efi.set_time(&eft);
  178. if (status != EFI_SUCCESS) {
  179. printk(KERN_ERR "Oops: efitime: can't write time!\n");
  180. return -1;
  181. }
  182. return 0;
  183. }
  184. unsigned long efi_get_time(void)
  185. {
  186. efi_status_t status;
  187. efi_time_t eft;
  188. efi_time_cap_t cap;
  189. status = efi.get_time(&eft, &cap);
  190. if (status != EFI_SUCCESS)
  191. printk(KERN_ERR "Oops: efitime: can't read time!\n");
  192. return mktime(eft.year, eft.month, eft.day, eft.hour,
  193. eft.minute, eft.second);
  194. }
  195. /*
  196. * Tell the kernel about the EFI memory map. This might include
  197. * more than the max 128 entries that can fit in the e820 legacy
  198. * (zeropage) memory map.
  199. */
  200. static void __init do_add_efi_memmap(void)
  201. {
  202. void *p;
  203. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  204. efi_memory_desc_t *md = p;
  205. unsigned long long start = md->phys_addr;
  206. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  207. int e820_type;
  208. switch (md->type) {
  209. case EFI_LOADER_CODE:
  210. case EFI_LOADER_DATA:
  211. case EFI_BOOT_SERVICES_CODE:
  212. case EFI_BOOT_SERVICES_DATA:
  213. case EFI_CONVENTIONAL_MEMORY:
  214. if (md->attribute & EFI_MEMORY_WB)
  215. e820_type = E820_RAM;
  216. else
  217. e820_type = E820_RESERVED;
  218. break;
  219. case EFI_ACPI_RECLAIM_MEMORY:
  220. e820_type = E820_ACPI;
  221. break;
  222. case EFI_ACPI_MEMORY_NVS:
  223. e820_type = E820_NVS;
  224. break;
  225. case EFI_UNUSABLE_MEMORY:
  226. e820_type = E820_UNUSABLE;
  227. break;
  228. default:
  229. /*
  230. * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
  231. * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
  232. * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
  233. */
  234. e820_type = E820_RESERVED;
  235. break;
  236. }
  237. e820_add_region(start, size, e820_type);
  238. }
  239. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  240. }
  241. void __init efi_reserve_early(void)
  242. {
  243. unsigned long pmap;
  244. #ifdef CONFIG_X86_32
  245. pmap = boot_params.efi_info.efi_memmap;
  246. #else
  247. pmap = (boot_params.efi_info.efi_memmap |
  248. ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
  249. #endif
  250. memmap.phys_map = (void *)pmap;
  251. memmap.nr_map = boot_params.efi_info.efi_memmap_size /
  252. boot_params.efi_info.efi_memdesc_size;
  253. memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
  254. memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
  255. reserve_early(pmap, pmap + memmap.nr_map * memmap.desc_size,
  256. "EFI memmap");
  257. }
  258. #if EFI_DEBUG
  259. static void __init print_efi_memmap(void)
  260. {
  261. efi_memory_desc_t *md;
  262. void *p;
  263. int i;
  264. for (p = memmap.map, i = 0;
  265. p < memmap.map_end;
  266. p += memmap.desc_size, i++) {
  267. md = p;
  268. printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, "
  269. "range=[0x%016llx-0x%016llx) (%lluMB)\n",
  270. i, md->type, md->attribute, md->phys_addr,
  271. md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
  272. (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
  273. }
  274. }
  275. #endif /* EFI_DEBUG */
  276. void __init efi_init(void)
  277. {
  278. efi_config_table_t *config_tables;
  279. efi_runtime_services_t *runtime;
  280. efi_char16_t *c16;
  281. char vendor[100] = "unknown";
  282. int i = 0;
  283. void *tmp;
  284. #ifdef CONFIG_X86_32
  285. efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
  286. #else
  287. efi_phys.systab = (efi_system_table_t *)
  288. (boot_params.efi_info.efi_systab |
  289. ((__u64)boot_params.efi_info.efi_systab_hi<<32));
  290. #endif
  291. efi.systab = early_ioremap((unsigned long)efi_phys.systab,
  292. sizeof(efi_system_table_t));
  293. if (efi.systab == NULL)
  294. printk(KERN_ERR "Couldn't map the EFI system table!\n");
  295. memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
  296. early_iounmap(efi.systab, sizeof(efi_system_table_t));
  297. efi.systab = &efi_systab;
  298. /*
  299. * Verify the EFI Table
  300. */
  301. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  302. printk(KERN_ERR "EFI system table signature incorrect!\n");
  303. if ((efi.systab->hdr.revision >> 16) == 0)
  304. printk(KERN_ERR "Warning: EFI system table version "
  305. "%d.%02d, expected 1.00 or greater!\n",
  306. efi.systab->hdr.revision >> 16,
  307. efi.systab->hdr.revision & 0xffff);
  308. /*
  309. * Show what we know for posterity
  310. */
  311. c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
  312. if (c16) {
  313. for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
  314. vendor[i] = *c16++;
  315. vendor[i] = '\0';
  316. } else
  317. printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
  318. early_iounmap(tmp, 2);
  319. printk(KERN_INFO "EFI v%u.%.02u by %s \n",
  320. efi.systab->hdr.revision >> 16,
  321. efi.systab->hdr.revision & 0xffff, vendor);
  322. /*
  323. * Let's see what config tables the firmware passed to us.
  324. */
  325. config_tables = early_ioremap(
  326. efi.systab->tables,
  327. efi.systab->nr_tables * sizeof(efi_config_table_t));
  328. if (config_tables == NULL)
  329. printk(KERN_ERR "Could not map EFI Configuration Table!\n");
  330. printk(KERN_INFO);
  331. for (i = 0; i < efi.systab->nr_tables; i++) {
  332. if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
  333. efi.mps = config_tables[i].table;
  334. printk(" MPS=0x%lx ", config_tables[i].table);
  335. } else if (!efi_guidcmp(config_tables[i].guid,
  336. ACPI_20_TABLE_GUID)) {
  337. efi.acpi20 = config_tables[i].table;
  338. printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
  339. } else if (!efi_guidcmp(config_tables[i].guid,
  340. ACPI_TABLE_GUID)) {
  341. efi.acpi = config_tables[i].table;
  342. printk(" ACPI=0x%lx ", config_tables[i].table);
  343. } else if (!efi_guidcmp(config_tables[i].guid,
  344. SMBIOS_TABLE_GUID)) {
  345. efi.smbios = config_tables[i].table;
  346. printk(" SMBIOS=0x%lx ", config_tables[i].table);
  347. #ifdef CONFIG_X86_UV
  348. } else if (!efi_guidcmp(config_tables[i].guid,
  349. UV_SYSTEM_TABLE_GUID)) {
  350. efi.uv_systab = config_tables[i].table;
  351. printk(" UVsystab=0x%lx ", config_tables[i].table);
  352. #endif
  353. } else if (!efi_guidcmp(config_tables[i].guid,
  354. HCDP_TABLE_GUID)) {
  355. efi.hcdp = config_tables[i].table;
  356. printk(" HCDP=0x%lx ", config_tables[i].table);
  357. } else if (!efi_guidcmp(config_tables[i].guid,
  358. UGA_IO_PROTOCOL_GUID)) {
  359. efi.uga = config_tables[i].table;
  360. printk(" UGA=0x%lx ", config_tables[i].table);
  361. }
  362. }
  363. printk("\n");
  364. early_iounmap(config_tables,
  365. efi.systab->nr_tables * sizeof(efi_config_table_t));
  366. /*
  367. * Check out the runtime services table. We need to map
  368. * the runtime services table so that we can grab the physical
  369. * address of several of the EFI runtime functions, needed to
  370. * set the firmware into virtual mode.
  371. */
  372. runtime = early_ioremap((unsigned long)efi.systab->runtime,
  373. sizeof(efi_runtime_services_t));
  374. if (runtime != NULL) {
  375. /*
  376. * We will only need *early* access to the following
  377. * two EFI runtime services before set_virtual_address_map
  378. * is invoked.
  379. */
  380. efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
  381. efi_phys.set_virtual_address_map =
  382. (efi_set_virtual_address_map_t *)
  383. runtime->set_virtual_address_map;
  384. /*
  385. * Make efi_get_time can be called before entering
  386. * virtual mode.
  387. */
  388. efi.get_time = phys_efi_get_time;
  389. } else
  390. printk(KERN_ERR "Could not map the EFI runtime service "
  391. "table!\n");
  392. early_iounmap(runtime, sizeof(efi_runtime_services_t));
  393. /* Map the EFI memory map */
  394. memmap.map = early_ioremap((unsigned long)memmap.phys_map,
  395. memmap.nr_map * memmap.desc_size);
  396. if (memmap.map == NULL)
  397. printk(KERN_ERR "Could not map the EFI memory map!\n");
  398. memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
  399. if (memmap.desc_size != sizeof(efi_memory_desc_t))
  400. printk(KERN_WARNING
  401. "Kernel-defined memdesc doesn't match the one from EFI!\n");
  402. if (add_efi_memmap)
  403. do_add_efi_memmap();
  404. #ifdef CONFIG_X86_32
  405. x86_platform.get_wallclock = efi_get_time;
  406. x86_platform.set_wallclock = efi_set_rtc_mmss;
  407. #endif
  408. /* Setup for EFI runtime service */
  409. reboot_type = BOOT_EFI;
  410. #if EFI_DEBUG
  411. print_efi_memmap();
  412. #endif
  413. }
  414. static void __init runtime_code_page_mkexec(void)
  415. {
  416. efi_memory_desc_t *md;
  417. void *p;
  418. u64 addr, npages;
  419. /* Make EFI runtime service code area executable */
  420. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  421. md = p;
  422. if (md->type != EFI_RUNTIME_SERVICES_CODE)
  423. continue;
  424. addr = md->virt_addr;
  425. npages = md->num_pages;
  426. memrange_efi_to_native(&addr, &npages);
  427. set_memory_x(addr, npages);
  428. }
  429. }
  430. /*
  431. * This function will switch the EFI runtime services to virtual mode.
  432. * Essentially, look through the EFI memmap and map every region that
  433. * has the runtime attribute bit set in its memory descriptor and update
  434. * that memory descriptor with the virtual address obtained from ioremap().
  435. * This enables the runtime services to be called without having to
  436. * thunk back into physical mode for every invocation.
  437. */
  438. void __init efi_enter_virtual_mode(void)
  439. {
  440. efi_memory_desc_t *md;
  441. efi_status_t status;
  442. unsigned long size;
  443. u64 end, systab, addr, npages, end_pfn;
  444. void *p, *va;
  445. efi.systab = NULL;
  446. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  447. md = p;
  448. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  449. continue;
  450. size = md->num_pages << EFI_PAGE_SHIFT;
  451. end = md->phys_addr + size;
  452. end_pfn = PFN_UP(end);
  453. if (end_pfn <= max_low_pfn_mapped
  454. || (end_pfn > (1UL << (32 - PAGE_SHIFT))
  455. && end_pfn <= max_pfn_mapped))
  456. va = __va(md->phys_addr);
  457. else
  458. va = efi_ioremap(md->phys_addr, size, md->type);
  459. md->virt_addr = (u64) (unsigned long) va;
  460. if (!va) {
  461. printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
  462. (unsigned long long)md->phys_addr);
  463. continue;
  464. }
  465. if (!(md->attribute & EFI_MEMORY_WB)) {
  466. addr = md->virt_addr;
  467. npages = md->num_pages;
  468. memrange_efi_to_native(&addr, &npages);
  469. set_memory_uc(addr, npages);
  470. }
  471. systab = (u64) (unsigned long) efi_phys.systab;
  472. if (md->phys_addr <= systab && systab < end) {
  473. systab += md->virt_addr - md->phys_addr;
  474. efi.systab = (efi_system_table_t *) (unsigned long) systab;
  475. }
  476. }
  477. BUG_ON(!efi.systab);
  478. status = phys_efi_set_virtual_address_map(
  479. memmap.desc_size * memmap.nr_map,
  480. memmap.desc_size,
  481. memmap.desc_version,
  482. memmap.phys_map);
  483. if (status != EFI_SUCCESS) {
  484. printk(KERN_ALERT "Unable to switch EFI into virtual mode "
  485. "(status=%lx)!\n", status);
  486. panic("EFI call to SetVirtualAddressMap() failed!");
  487. }
  488. /*
  489. * Now that EFI is in virtual mode, update the function
  490. * pointers in the runtime service table to the new virtual addresses.
  491. *
  492. * Call EFI services through wrapper functions.
  493. */
  494. efi.get_time = virt_efi_get_time;
  495. efi.set_time = virt_efi_set_time;
  496. efi.get_wakeup_time = virt_efi_get_wakeup_time;
  497. efi.set_wakeup_time = virt_efi_set_wakeup_time;
  498. efi.get_variable = virt_efi_get_variable;
  499. efi.get_next_variable = virt_efi_get_next_variable;
  500. efi.set_variable = virt_efi_set_variable;
  501. efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
  502. efi.reset_system = virt_efi_reset_system;
  503. efi.set_virtual_address_map = virt_efi_set_virtual_address_map;
  504. if (__supported_pte_mask & _PAGE_NX)
  505. runtime_code_page_mkexec();
  506. early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
  507. memmap.map = NULL;
  508. }
  509. /*
  510. * Convenience functions to obtain memory types and attributes
  511. */
  512. u32 efi_mem_type(unsigned long phys_addr)
  513. {
  514. efi_memory_desc_t *md;
  515. void *p;
  516. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  517. md = p;
  518. if ((md->phys_addr <= phys_addr) &&
  519. (phys_addr < (md->phys_addr +
  520. (md->num_pages << EFI_PAGE_SHIFT))))
  521. return md->type;
  522. }
  523. return 0;
  524. }
  525. u64 efi_mem_attributes(unsigned long phys_addr)
  526. {
  527. efi_memory_desc_t *md;
  528. void *p;
  529. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  530. md = p;
  531. if ((md->phys_addr <= phys_addr) &&
  532. (phys_addr < (md->phys_addr +
  533. (md->num_pages << EFI_PAGE_SHIFT))))
  534. return md->attribute;
  535. }
  536. return 0;
  537. }