efi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. #define EFI_DEBUG 1
  42. #define PFX "EFI: "
  43. int efi_enabled;
  44. EXPORT_SYMBOL(efi_enabled);
  45. struct efi efi;
  46. EXPORT_SYMBOL(efi);
  47. struct efi_memory_map memmap;
  48. struct efi efi_phys __initdata;
  49. static efi_system_table_t efi_systab __initdata;
  50. static int __init setup_noefi(char *arg)
  51. {
  52. efi_enabled = 0;
  53. return 0;
  54. }
  55. early_param("noefi", setup_noefi);
  56. static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  57. {
  58. return efi_call_virt2(get_time, tm, tc);
  59. }
  60. static efi_status_t virt_efi_set_time(efi_time_t *tm)
  61. {
  62. return efi_call_virt1(set_time, tm);
  63. }
  64. static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
  65. efi_bool_t *pending,
  66. efi_time_t *tm)
  67. {
  68. return efi_call_virt3(get_wakeup_time,
  69. enabled, pending, tm);
  70. }
  71. static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  72. {
  73. return efi_call_virt2(set_wakeup_time,
  74. enabled, tm);
  75. }
  76. static efi_status_t virt_efi_get_variable(efi_char16_t *name,
  77. efi_guid_t *vendor,
  78. u32 *attr,
  79. unsigned long *data_size,
  80. void *data)
  81. {
  82. return efi_call_virt5(get_variable,
  83. name, vendor, attr,
  84. data_size, data);
  85. }
  86. static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
  87. efi_char16_t *name,
  88. efi_guid_t *vendor)
  89. {
  90. return efi_call_virt3(get_next_variable,
  91. name_size, name, vendor);
  92. }
  93. static efi_status_t virt_efi_set_variable(efi_char16_t *name,
  94. efi_guid_t *vendor,
  95. unsigned long attr,
  96. unsigned long data_size,
  97. void *data)
  98. {
  99. return efi_call_virt5(set_variable,
  100. name, vendor, attr,
  101. data_size, data);
  102. }
  103. static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
  104. {
  105. return efi_call_virt1(get_next_high_mono_count, count);
  106. }
  107. static void virt_efi_reset_system(int reset_type,
  108. efi_status_t status,
  109. unsigned long data_size,
  110. efi_char16_t *data)
  111. {
  112. efi_call_virt4(reset_system, reset_type, status,
  113. data_size, data);
  114. }
  115. static efi_status_t virt_efi_set_virtual_address_map(
  116. unsigned long memory_map_size,
  117. unsigned long descriptor_size,
  118. u32 descriptor_version,
  119. efi_memory_desc_t *virtual_map)
  120. {
  121. return efi_call_virt4(set_virtual_address_map,
  122. memory_map_size, descriptor_size,
  123. descriptor_version, virtual_map);
  124. }
  125. static efi_status_t __init phys_efi_set_virtual_address_map(
  126. unsigned long memory_map_size,
  127. unsigned long descriptor_size,
  128. u32 descriptor_version,
  129. efi_memory_desc_t *virtual_map)
  130. {
  131. efi_status_t status;
  132. efi_call_phys_prelog();
  133. status = efi_call_phys4(efi_phys.set_virtual_address_map,
  134. memory_map_size, descriptor_size,
  135. descriptor_version, virtual_map);
  136. efi_call_phys_epilog();
  137. return status;
  138. }
  139. static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
  140. efi_time_cap_t *tc)
  141. {
  142. efi_status_t status;
  143. efi_call_phys_prelog();
  144. status = efi_call_phys2(efi_phys.get_time, tm, tc);
  145. efi_call_phys_epilog();
  146. return status;
  147. }
  148. int efi_set_rtc_mmss(unsigned long nowtime)
  149. {
  150. int real_seconds, real_minutes;
  151. efi_status_t status;
  152. efi_time_t eft;
  153. efi_time_cap_t cap;
  154. status = efi.get_time(&eft, &cap);
  155. if (status != EFI_SUCCESS) {
  156. printk(KERN_ERR "Oops: efitime: can't read time!\n");
  157. return -1;
  158. }
  159. real_seconds = nowtime % 60;
  160. real_minutes = nowtime / 60;
  161. if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
  162. real_minutes += 30;
  163. real_minutes %= 60;
  164. eft.minute = real_minutes;
  165. eft.second = real_seconds;
  166. status = efi.set_time(&eft);
  167. if (status != EFI_SUCCESS) {
  168. printk(KERN_ERR "Oops: efitime: can't write time!\n");
  169. return -1;
  170. }
  171. return 0;
  172. }
  173. unsigned long efi_get_time(void)
  174. {
  175. efi_status_t status;
  176. efi_time_t eft;
  177. efi_time_cap_t cap;
  178. status = efi.get_time(&eft, &cap);
  179. if (status != EFI_SUCCESS)
  180. printk(KERN_ERR "Oops: efitime: can't read time!\n");
  181. return mktime(eft.year, eft.month, eft.day, eft.hour,
  182. eft.minute, eft.second);
  183. }
  184. #if EFI_DEBUG
  185. static void __init print_efi_memmap(void)
  186. {
  187. efi_memory_desc_t *md;
  188. void *p;
  189. int i;
  190. for (p = memmap.map, i = 0;
  191. p < memmap.map_end;
  192. p += memmap.desc_size, i++) {
  193. md = p;
  194. printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, "
  195. "range=[0x%016llx-0x%016llx) (%lluMB)\n",
  196. i, md->type, md->attribute, md->phys_addr,
  197. md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
  198. (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
  199. }
  200. }
  201. #endif /* EFI_DEBUG */
  202. void __init efi_init(void)
  203. {
  204. efi_config_table_t *config_tables;
  205. efi_runtime_services_t *runtime;
  206. efi_char16_t *c16;
  207. char vendor[100] = "unknown";
  208. int i = 0;
  209. void *tmp;
  210. #ifdef CONFIG_X86_32
  211. efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
  212. memmap.phys_map = (void *)boot_params.efi_info.efi_memmap;
  213. #else
  214. efi_phys.systab = (efi_system_table_t *)
  215. (boot_params.efi_info.efi_systab |
  216. ((__u64)boot_params.efi_info.efi_systab_hi<<32));
  217. memmap.phys_map = (void *)
  218. (boot_params.efi_info.efi_memmap |
  219. ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
  220. #endif
  221. memmap.nr_map = boot_params.efi_info.efi_memmap_size /
  222. boot_params.efi_info.efi_memdesc_size;
  223. memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
  224. memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
  225. efi.systab = efi_early_ioremap((unsigned long)efi_phys.systab,
  226. sizeof(efi_system_table_t));
  227. if (efi.systab == NULL)
  228. printk(KERN_ERR "Couldn't map the EFI system table!\n");
  229. memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
  230. efi_early_iounmap(efi.systab, sizeof(efi_system_table_t));
  231. efi.systab = &efi_systab;
  232. /*
  233. * Verify the EFI Table
  234. */
  235. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  236. printk(KERN_ERR "EFI system table signature incorrect!\n");
  237. if ((efi.systab->hdr.revision >> 16) == 0)
  238. printk(KERN_ERR "Warning: EFI system table version "
  239. "%d.%02d, expected 1.00 or greater!\n",
  240. efi.systab->hdr.revision >> 16,
  241. efi.systab->hdr.revision & 0xffff);
  242. /*
  243. * Show what we know for posterity
  244. */
  245. c16 = tmp = efi_early_ioremap(efi.systab->fw_vendor, 2);
  246. if (c16) {
  247. for (i = 0; i < sizeof(vendor) && *c16; ++i)
  248. vendor[i] = *c16++;
  249. vendor[i] = '\0';
  250. } else
  251. printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
  252. efi_early_iounmap(tmp, 2);
  253. printk(KERN_INFO "EFI v%u.%.02u by %s \n",
  254. efi.systab->hdr.revision >> 16,
  255. efi.systab->hdr.revision & 0xffff, vendor);
  256. /*
  257. * Let's see what config tables the firmware passed to us.
  258. */
  259. config_tables = efi_early_ioremap(
  260. efi.systab->tables,
  261. efi.systab->nr_tables * sizeof(efi_config_table_t));
  262. if (config_tables == NULL)
  263. printk(KERN_ERR "Could not map EFI Configuration Table!\n");
  264. printk(KERN_INFO);
  265. for (i = 0; i < efi.systab->nr_tables; i++) {
  266. if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
  267. efi.mps = config_tables[i].table;
  268. printk(" MPS=0x%lx ", config_tables[i].table);
  269. } else if (!efi_guidcmp(config_tables[i].guid,
  270. ACPI_20_TABLE_GUID)) {
  271. efi.acpi20 = config_tables[i].table;
  272. printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
  273. } else if (!efi_guidcmp(config_tables[i].guid,
  274. ACPI_TABLE_GUID)) {
  275. efi.acpi = config_tables[i].table;
  276. printk(" ACPI=0x%lx ", config_tables[i].table);
  277. } else if (!efi_guidcmp(config_tables[i].guid,
  278. SMBIOS_TABLE_GUID)) {
  279. efi.smbios = config_tables[i].table;
  280. printk(" SMBIOS=0x%lx ", config_tables[i].table);
  281. } else if (!efi_guidcmp(config_tables[i].guid,
  282. HCDP_TABLE_GUID)) {
  283. efi.hcdp = config_tables[i].table;
  284. printk(" HCDP=0x%lx ", config_tables[i].table);
  285. } else if (!efi_guidcmp(config_tables[i].guid,
  286. UGA_IO_PROTOCOL_GUID)) {
  287. efi.uga = config_tables[i].table;
  288. printk(" UGA=0x%lx ", config_tables[i].table);
  289. }
  290. }
  291. printk("\n");
  292. efi_early_iounmap(config_tables,
  293. efi.systab->nr_tables * sizeof(efi_config_table_t));
  294. /*
  295. * Check out the runtime services table. We need to map
  296. * the runtime services table so that we can grab the physical
  297. * address of several of the EFI runtime functions, needed to
  298. * set the firmware into virtual mode.
  299. */
  300. runtime = efi_early_ioremap((unsigned long)efi.systab->runtime,
  301. sizeof(efi_runtime_services_t));
  302. if (runtime != NULL) {
  303. /*
  304. * We will only need *early* access to the following
  305. * two EFI runtime services before set_virtual_address_map
  306. * is invoked.
  307. */
  308. efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
  309. efi_phys.set_virtual_address_map =
  310. (efi_set_virtual_address_map_t *)
  311. runtime->set_virtual_address_map;
  312. /*
  313. * Make efi_get_time can be called before entering
  314. * virtual mode.
  315. */
  316. efi.get_time = phys_efi_get_time;
  317. } else
  318. printk(KERN_ERR "Could not map the EFI runtime service "
  319. "table!\n");
  320. efi_early_iounmap(runtime, sizeof(efi_runtime_services_t));
  321. /* Map the EFI memory map */
  322. memmap.map = efi_early_ioremap((unsigned long)memmap.phys_map,
  323. memmap.nr_map * memmap.desc_size);
  324. if (memmap.map == NULL)
  325. printk(KERN_ERR "Could not map the EFI memory map!\n");
  326. memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
  327. if (memmap.desc_size != sizeof(efi_memory_desc_t))
  328. printk(KERN_WARNING "Kernel-defined memdesc"
  329. "doesn't match the one from EFI!\n");
  330. #ifdef CONFIG_X86_64
  331. /* Setup for EFI runtime service */
  332. reboot_type = BOOT_EFI;
  333. #endif
  334. #if EFI_DEBUG
  335. print_efi_memmap();
  336. #endif
  337. }
  338. /*
  339. * This function will switch the EFI runtime services to virtual mode.
  340. * Essentially, look through the EFI memmap and map every region that
  341. * has the runtime attribute bit set in its memory descriptor and update
  342. * that memory descriptor with the virtual address obtained from ioremap().
  343. * This enables the runtime services to be called without having to
  344. * thunk back into physical mode for every invocation.
  345. */
  346. void __init efi_enter_virtual_mode(void)
  347. {
  348. efi_memory_desc_t *md;
  349. efi_status_t status;
  350. unsigned long end;
  351. void *p;
  352. efi.systab = NULL;
  353. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  354. md = p;
  355. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  356. continue;
  357. if ((md->attribute & EFI_MEMORY_WB) &&
  358. (((md->phys_addr + (md->num_pages<<EFI_PAGE_SHIFT)) >>
  359. PAGE_SHIFT) < end_pfn_map))
  360. md->virt_addr = (unsigned long)__va(md->phys_addr);
  361. else
  362. md->virt_addr = (unsigned long)
  363. efi_ioremap(md->phys_addr,
  364. md->num_pages << EFI_PAGE_SHIFT);
  365. if (!md->virt_addr)
  366. printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
  367. (unsigned long long)md->phys_addr);
  368. end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
  369. if ((md->phys_addr <= (unsigned long)efi_phys.systab) &&
  370. ((unsigned long)efi_phys.systab < end))
  371. efi.systab = (efi_system_table_t *)(unsigned long)
  372. (md->virt_addr - md->phys_addr +
  373. (unsigned long)efi_phys.systab);
  374. }
  375. BUG_ON(!efi.systab);
  376. status = phys_efi_set_virtual_address_map(
  377. memmap.desc_size * memmap.nr_map,
  378. memmap.desc_size,
  379. memmap.desc_version,
  380. memmap.phys_map);
  381. if (status != EFI_SUCCESS) {
  382. printk(KERN_ALERT "Unable to switch EFI into virtual mode "
  383. "(status=%lx)!\n", status);
  384. panic("EFI call to SetVirtualAddressMap() failed!");
  385. }
  386. /*
  387. * Now that EFI is in virtual mode, update the function
  388. * pointers in the runtime service table to the new virtual addresses.
  389. *
  390. * Call EFI services through wrapper functions.
  391. */
  392. efi.get_time = virt_efi_get_time;
  393. efi.set_time = virt_efi_set_time;
  394. efi.get_wakeup_time = virt_efi_get_wakeup_time;
  395. efi.set_wakeup_time = virt_efi_set_wakeup_time;
  396. efi.get_variable = virt_efi_get_variable;
  397. efi.get_next_variable = virt_efi_get_next_variable;
  398. efi.set_variable = virt_efi_set_variable;
  399. efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
  400. efi.reset_system = virt_efi_reset_system;
  401. efi.set_virtual_address_map = virt_efi_set_virtual_address_map;
  402. #ifdef CONFIG_X86_64
  403. runtime_code_page_mkexec();
  404. #endif
  405. }
  406. /*
  407. * Convenience functions to obtain memory types and attributes
  408. */
  409. u32 efi_mem_type(unsigned long phys_addr)
  410. {
  411. efi_memory_desc_t *md;
  412. void *p;
  413. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  414. md = p;
  415. if ((md->phys_addr <= phys_addr) &&
  416. (phys_addr < (md->phys_addr +
  417. (md->num_pages << EFI_PAGE_SHIFT))))
  418. return md->type;
  419. }
  420. return 0;
  421. }
  422. u64 efi_mem_attributes(unsigned long phys_addr)
  423. {
  424. efi_memory_desc_t *md;
  425. void *p;
  426. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  427. md = p;
  428. if ((md->phys_addr <= phys_addr) &&
  429. (phys_addr < (md->phys_addr +
  430. (md->num_pages << EFI_PAGE_SHIFT))))
  431. return md->attribute;
  432. }
  433. return 0;
  434. }