efi.c 15 KB

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