efi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. switch (md->type) {
  208. case EFI_LOADER_CODE:
  209. case EFI_LOADER_DATA:
  210. case EFI_BOOT_SERVICES_CODE:
  211. case EFI_BOOT_SERVICES_DATA:
  212. case EFI_CONVENTIONAL_MEMORY:
  213. if (md->attribute & EFI_MEMORY_WB)
  214. e820_type = E820_RAM;
  215. else
  216. e820_type = E820_RESERVED;
  217. break;
  218. case EFI_ACPI_RECLAIM_MEMORY:
  219. e820_type = E820_ACPI;
  220. break;
  221. case EFI_ACPI_MEMORY_NVS:
  222. e820_type = E820_NVS;
  223. break;
  224. case EFI_UNUSABLE_MEMORY:
  225. e820_type = E820_UNUSABLE;
  226. break;
  227. default:
  228. /*
  229. * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
  230. * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
  231. * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
  232. */
  233. e820_type = E820_RESERVED;
  234. break;
  235. }
  236. e820_add_region(start, size, e820_type);
  237. }
  238. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  239. }
  240. void __init efi_reserve_early(void)
  241. {
  242. unsigned long pmap;
  243. #ifdef CONFIG_X86_32
  244. pmap = boot_params.efi_info.efi_memmap;
  245. #else
  246. pmap = (boot_params.efi_info.efi_memmap |
  247. ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
  248. #endif
  249. memmap.phys_map = (void *)pmap;
  250. memmap.nr_map = boot_params.efi_info.efi_memmap_size /
  251. boot_params.efi_info.efi_memdesc_size;
  252. memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
  253. memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
  254. reserve_early(pmap, pmap + memmap.nr_map * memmap.desc_size,
  255. "EFI memmap");
  256. }
  257. #if EFI_DEBUG
  258. static void __init print_efi_memmap(void)
  259. {
  260. efi_memory_desc_t *md;
  261. void *p;
  262. int i;
  263. for (p = memmap.map, i = 0;
  264. p < memmap.map_end;
  265. p += memmap.desc_size, i++) {
  266. md = p;
  267. printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, "
  268. "range=[0x%016llx-0x%016llx) (%lluMB)\n",
  269. i, md->type, md->attribute, md->phys_addr,
  270. md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
  271. (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
  272. }
  273. }
  274. #endif /* EFI_DEBUG */
  275. void __init efi_init(void)
  276. {
  277. efi_config_table_t *config_tables;
  278. efi_runtime_services_t *runtime;
  279. efi_char16_t *c16;
  280. char vendor[100] = "unknown";
  281. int i = 0;
  282. void *tmp;
  283. #ifdef CONFIG_X86_32
  284. efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
  285. #else
  286. efi_phys.systab = (efi_system_table_t *)
  287. (boot_params.efi_info.efi_systab |
  288. ((__u64)boot_params.efi_info.efi_systab_hi<<32));
  289. #endif
  290. efi.systab = early_ioremap((unsigned long)efi_phys.systab,
  291. sizeof(efi_system_table_t));
  292. if (efi.systab == NULL)
  293. printk(KERN_ERR "Couldn't map the EFI system table!\n");
  294. memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
  295. early_iounmap(efi.systab, sizeof(efi_system_table_t));
  296. efi.systab = &efi_systab;
  297. /*
  298. * Verify the EFI Table
  299. */
  300. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  301. printk(KERN_ERR "EFI system table signature incorrect!\n");
  302. if ((efi.systab->hdr.revision >> 16) == 0)
  303. printk(KERN_ERR "Warning: EFI system table version "
  304. "%d.%02d, expected 1.00 or greater!\n",
  305. efi.systab->hdr.revision >> 16,
  306. efi.systab->hdr.revision & 0xffff);
  307. /*
  308. * Show what we know for posterity
  309. */
  310. c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
  311. if (c16) {
  312. for (i = 0; i < sizeof(vendor) && *c16; ++i)
  313. vendor[i] = *c16++;
  314. vendor[i] = '\0';
  315. } else
  316. printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
  317. early_iounmap(tmp, 2);
  318. printk(KERN_INFO "EFI v%u.%.02u by %s \n",
  319. efi.systab->hdr.revision >> 16,
  320. efi.systab->hdr.revision & 0xffff, vendor);
  321. /*
  322. * Let's see what config tables the firmware passed to us.
  323. */
  324. config_tables = early_ioremap(
  325. efi.systab->tables,
  326. efi.systab->nr_tables * sizeof(efi_config_table_t));
  327. if (config_tables == NULL)
  328. printk(KERN_ERR "Could not map EFI Configuration Table!\n");
  329. printk(KERN_INFO);
  330. for (i = 0; i < efi.systab->nr_tables; i++) {
  331. if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
  332. efi.mps = config_tables[i].table;
  333. printk(" MPS=0x%lx ", config_tables[i].table);
  334. } else if (!efi_guidcmp(config_tables[i].guid,
  335. ACPI_20_TABLE_GUID)) {
  336. efi.acpi20 = config_tables[i].table;
  337. printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
  338. } else if (!efi_guidcmp(config_tables[i].guid,
  339. ACPI_TABLE_GUID)) {
  340. efi.acpi = config_tables[i].table;
  341. printk(" ACPI=0x%lx ", config_tables[i].table);
  342. } else if (!efi_guidcmp(config_tables[i].guid,
  343. SMBIOS_TABLE_GUID)) {
  344. efi.smbios = config_tables[i].table;
  345. printk(" SMBIOS=0x%lx ", config_tables[i].table);
  346. #ifdef CONFIG_X86_UV
  347. } else if (!efi_guidcmp(config_tables[i].guid,
  348. UV_SYSTEM_TABLE_GUID)) {
  349. efi.uv_systab = config_tables[i].table;
  350. printk(" UVsystab=0x%lx ", config_tables[i].table);
  351. #endif
  352. } else if (!efi_guidcmp(config_tables[i].guid,
  353. HCDP_TABLE_GUID)) {
  354. efi.hcdp = config_tables[i].table;
  355. printk(" HCDP=0x%lx ", config_tables[i].table);
  356. } else if (!efi_guidcmp(config_tables[i].guid,
  357. UGA_IO_PROTOCOL_GUID)) {
  358. efi.uga = config_tables[i].table;
  359. printk(" UGA=0x%lx ", config_tables[i].table);
  360. }
  361. }
  362. printk("\n");
  363. early_iounmap(config_tables,
  364. efi.systab->nr_tables * sizeof(efi_config_table_t));
  365. /*
  366. * Check out the runtime services table. We need to map
  367. * the runtime services table so that we can grab the physical
  368. * address of several of the EFI runtime functions, needed to
  369. * set the firmware into virtual mode.
  370. */
  371. runtime = early_ioremap((unsigned long)efi.systab->runtime,
  372. sizeof(efi_runtime_services_t));
  373. if (runtime != NULL) {
  374. /*
  375. * We will only need *early* access to the following
  376. * two EFI runtime services before set_virtual_address_map
  377. * is invoked.
  378. */
  379. efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
  380. efi_phys.set_virtual_address_map =
  381. (efi_set_virtual_address_map_t *)
  382. runtime->set_virtual_address_map;
  383. /*
  384. * Make efi_get_time can be called before entering
  385. * virtual mode.
  386. */
  387. efi.get_time = phys_efi_get_time;
  388. } else
  389. printk(KERN_ERR "Could not map the EFI runtime service "
  390. "table!\n");
  391. early_iounmap(runtime, sizeof(efi_runtime_services_t));
  392. /* Map the EFI memory map */
  393. memmap.map = early_ioremap((unsigned long)memmap.phys_map,
  394. memmap.nr_map * memmap.desc_size);
  395. if (memmap.map == NULL)
  396. printk(KERN_ERR "Could not map the EFI memory map!\n");
  397. memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
  398. if (memmap.desc_size != sizeof(efi_memory_desc_t))
  399. printk(KERN_WARNING
  400. "Kernel-defined memdesc doesn't match the one from EFI!\n");
  401. if (add_efi_memmap)
  402. do_add_efi_memmap();
  403. /* Setup for EFI runtime service */
  404. reboot_type = BOOT_EFI;
  405. #if EFI_DEBUG
  406. print_efi_memmap();
  407. #endif
  408. }
  409. static void __init runtime_code_page_mkexec(void)
  410. {
  411. efi_memory_desc_t *md;
  412. void *p;
  413. u64 addr, npages;
  414. /* Make EFI runtime service code area executable */
  415. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  416. md = p;
  417. if (md->type != EFI_RUNTIME_SERVICES_CODE)
  418. continue;
  419. addr = md->virt_addr;
  420. npages = md->num_pages;
  421. memrange_efi_to_native(&addr, &npages);
  422. set_memory_x(addr, npages);
  423. }
  424. }
  425. /*
  426. * This function will switch the EFI runtime services to virtual mode.
  427. * Essentially, look through the EFI memmap and map every region that
  428. * has the runtime attribute bit set in its memory descriptor and update
  429. * that memory descriptor with the virtual address obtained from ioremap().
  430. * This enables the runtime services to be called without having to
  431. * thunk back into physical mode for every invocation.
  432. */
  433. void __init efi_enter_virtual_mode(void)
  434. {
  435. efi_memory_desc_t *md;
  436. efi_status_t status;
  437. unsigned long size;
  438. u64 end, systab, addr, npages, end_pfn;
  439. void *p, *va;
  440. efi.systab = NULL;
  441. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  442. md = p;
  443. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  444. continue;
  445. size = md->num_pages << EFI_PAGE_SHIFT;
  446. end = md->phys_addr + size;
  447. end_pfn = PFN_UP(end);
  448. if (end_pfn <= max_low_pfn_mapped
  449. || (end_pfn > (1UL << (32 - PAGE_SHIFT))
  450. && end_pfn <= max_pfn_mapped))
  451. va = __va(md->phys_addr);
  452. else
  453. va = efi_ioremap(md->phys_addr, size);
  454. md->virt_addr = (u64) (unsigned long) va;
  455. if (!va) {
  456. printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
  457. (unsigned long long)md->phys_addr);
  458. continue;
  459. }
  460. if (!(md->attribute & EFI_MEMORY_WB)) {
  461. addr = md->virt_addr;
  462. npages = md->num_pages;
  463. memrange_efi_to_native(&addr, &npages);
  464. set_memory_uc(addr, npages);
  465. }
  466. systab = (u64) (unsigned long) efi_phys.systab;
  467. if (md->phys_addr <= systab && systab < end) {
  468. systab += md->virt_addr - md->phys_addr;
  469. efi.systab = (efi_system_table_t *) (unsigned long) systab;
  470. }
  471. }
  472. BUG_ON(!efi.systab);
  473. status = phys_efi_set_virtual_address_map(
  474. memmap.desc_size * memmap.nr_map,
  475. memmap.desc_size,
  476. memmap.desc_version,
  477. memmap.phys_map);
  478. if (status != EFI_SUCCESS) {
  479. printk(KERN_ALERT "Unable to switch EFI into virtual mode "
  480. "(status=%lx)!\n", status);
  481. panic("EFI call to SetVirtualAddressMap() failed!");
  482. }
  483. /*
  484. * Now that EFI is in virtual mode, update the function
  485. * pointers in the runtime service table to the new virtual addresses.
  486. *
  487. * Call EFI services through wrapper functions.
  488. */
  489. efi.get_time = virt_efi_get_time;
  490. efi.set_time = virt_efi_set_time;
  491. efi.get_wakeup_time = virt_efi_get_wakeup_time;
  492. efi.set_wakeup_time = virt_efi_set_wakeup_time;
  493. efi.get_variable = virt_efi_get_variable;
  494. efi.get_next_variable = virt_efi_get_next_variable;
  495. efi.set_variable = virt_efi_set_variable;
  496. efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
  497. efi.reset_system = virt_efi_reset_system;
  498. efi.set_virtual_address_map = virt_efi_set_virtual_address_map;
  499. if (__supported_pte_mask & _PAGE_NX)
  500. runtime_code_page_mkexec();
  501. early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
  502. memmap.map = NULL;
  503. }
  504. /*
  505. * Convenience functions to obtain memory types and attributes
  506. */
  507. u32 efi_mem_type(unsigned long phys_addr)
  508. {
  509. efi_memory_desc_t *md;
  510. void *p;
  511. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  512. md = p;
  513. if ((md->phys_addr <= phys_addr) &&
  514. (phys_addr < (md->phys_addr +
  515. (md->num_pages << EFI_PAGE_SHIFT))))
  516. return md->type;
  517. }
  518. return 0;
  519. }
  520. u64 efi_mem_attributes(unsigned long phys_addr)
  521. {
  522. efi_memory_desc_t *md;
  523. void *p;
  524. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  525. md = p;
  526. if ((md->phys_addr <= phys_addr) &&
  527. (phys_addr < (md->phys_addr +
  528. (md->num_pages << EFI_PAGE_SHIFT))))
  529. return md->attribute;
  530. }
  531. return 0;
  532. }