efi.c 15 KB

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