efi.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/kernel.h>
  30. #include <linux/init.h>
  31. #include <linux/efi.h>
  32. #include <linux/efi-bgrt.h>
  33. #include <linux/export.h>
  34. #include <linux/bootmem.h>
  35. #include <linux/memblock.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/time.h>
  39. #include <linux/io.h>
  40. #include <linux/reboot.h>
  41. #include <linux/bcd.h>
  42. #include <asm/setup.h>
  43. #include <asm/efi.h>
  44. #include <asm/time.h>
  45. #include <asm/cacheflush.h>
  46. #include <asm/tlbflush.h>
  47. #include <asm/x86_init.h>
  48. #define EFI_DEBUG 1
  49. struct efi __read_mostly efi = {
  50. .mps = EFI_INVALID_TABLE_ADDR,
  51. .acpi = EFI_INVALID_TABLE_ADDR,
  52. .acpi20 = EFI_INVALID_TABLE_ADDR,
  53. .smbios = EFI_INVALID_TABLE_ADDR,
  54. .sal_systab = EFI_INVALID_TABLE_ADDR,
  55. .boot_info = EFI_INVALID_TABLE_ADDR,
  56. .hcdp = EFI_INVALID_TABLE_ADDR,
  57. .uga = EFI_INVALID_TABLE_ADDR,
  58. .uv_systab = EFI_INVALID_TABLE_ADDR,
  59. };
  60. EXPORT_SYMBOL(efi);
  61. struct efi_memory_map memmap;
  62. static struct efi efi_phys __initdata;
  63. static efi_system_table_t efi_systab __initdata;
  64. static inline bool efi_is_native(void)
  65. {
  66. return IS_ENABLED(CONFIG_X86_64) == efi_enabled(EFI_64BIT);
  67. }
  68. unsigned long x86_efi_facility;
  69. /*
  70. * Returns 1 if 'facility' is enabled, 0 otherwise.
  71. */
  72. int efi_enabled(int facility)
  73. {
  74. return test_bit(facility, &x86_efi_facility) != 0;
  75. }
  76. EXPORT_SYMBOL(efi_enabled);
  77. static bool disable_runtime = false;
  78. static int __init setup_noefi(char *arg)
  79. {
  80. disable_runtime = true;
  81. return 0;
  82. }
  83. early_param("noefi", setup_noefi);
  84. int add_efi_memmap;
  85. EXPORT_SYMBOL(add_efi_memmap);
  86. static int __init setup_add_efi_memmap(char *arg)
  87. {
  88. add_efi_memmap = 1;
  89. return 0;
  90. }
  91. early_param("add_efi_memmap", setup_add_efi_memmap);
  92. static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  93. {
  94. unsigned long flags;
  95. efi_status_t status;
  96. spin_lock_irqsave(&rtc_lock, flags);
  97. status = efi_call_virt2(get_time, tm, tc);
  98. spin_unlock_irqrestore(&rtc_lock, flags);
  99. return status;
  100. }
  101. static efi_status_t virt_efi_set_time(efi_time_t *tm)
  102. {
  103. unsigned long flags;
  104. efi_status_t status;
  105. spin_lock_irqsave(&rtc_lock, flags);
  106. status = efi_call_virt1(set_time, tm);
  107. spin_unlock_irqrestore(&rtc_lock, flags);
  108. return status;
  109. }
  110. static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
  111. efi_bool_t *pending,
  112. efi_time_t *tm)
  113. {
  114. unsigned long flags;
  115. efi_status_t status;
  116. spin_lock_irqsave(&rtc_lock, flags);
  117. status = efi_call_virt3(get_wakeup_time,
  118. enabled, pending, tm);
  119. spin_unlock_irqrestore(&rtc_lock, flags);
  120. return status;
  121. }
  122. static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  123. {
  124. unsigned long flags;
  125. efi_status_t status;
  126. spin_lock_irqsave(&rtc_lock, flags);
  127. status = efi_call_virt2(set_wakeup_time,
  128. enabled, tm);
  129. spin_unlock_irqrestore(&rtc_lock, flags);
  130. return status;
  131. }
  132. static efi_status_t virt_efi_get_variable(efi_char16_t *name,
  133. efi_guid_t *vendor,
  134. u32 *attr,
  135. unsigned long *data_size,
  136. void *data)
  137. {
  138. return efi_call_virt5(get_variable,
  139. name, vendor, attr,
  140. data_size, data);
  141. }
  142. static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
  143. efi_char16_t *name,
  144. efi_guid_t *vendor)
  145. {
  146. return efi_call_virt3(get_next_variable,
  147. name_size, name, vendor);
  148. }
  149. static efi_status_t virt_efi_set_variable(efi_char16_t *name,
  150. efi_guid_t *vendor,
  151. u32 attr,
  152. unsigned long data_size,
  153. void *data)
  154. {
  155. return efi_call_virt5(set_variable,
  156. name, vendor, attr,
  157. data_size, data);
  158. }
  159. static efi_status_t virt_efi_query_variable_info(u32 attr,
  160. u64 *storage_space,
  161. u64 *remaining_space,
  162. u64 *max_variable_size)
  163. {
  164. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  165. return EFI_UNSUPPORTED;
  166. return efi_call_virt4(query_variable_info, attr, storage_space,
  167. remaining_space, max_variable_size);
  168. }
  169. static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
  170. {
  171. return efi_call_virt1(get_next_high_mono_count, count);
  172. }
  173. static void virt_efi_reset_system(int reset_type,
  174. efi_status_t status,
  175. unsigned long data_size,
  176. efi_char16_t *data)
  177. {
  178. efi_call_virt4(reset_system, reset_type, status,
  179. data_size, data);
  180. }
  181. static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
  182. unsigned long count,
  183. unsigned long sg_list)
  184. {
  185. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  186. return EFI_UNSUPPORTED;
  187. return efi_call_virt3(update_capsule, capsules, count, sg_list);
  188. }
  189. static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
  190. unsigned long count,
  191. u64 *max_size,
  192. int *reset_type)
  193. {
  194. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  195. return EFI_UNSUPPORTED;
  196. return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
  197. reset_type);
  198. }
  199. static efi_status_t __init phys_efi_set_virtual_address_map(
  200. unsigned long memory_map_size,
  201. unsigned long descriptor_size,
  202. u32 descriptor_version,
  203. efi_memory_desc_t *virtual_map)
  204. {
  205. efi_status_t status;
  206. efi_call_phys_prelog();
  207. status = efi_call_phys4(efi_phys.set_virtual_address_map,
  208. memory_map_size, descriptor_size,
  209. descriptor_version, virtual_map);
  210. efi_call_phys_epilog();
  211. return status;
  212. }
  213. static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
  214. efi_time_cap_t *tc)
  215. {
  216. unsigned long flags;
  217. efi_status_t status;
  218. spin_lock_irqsave(&rtc_lock, flags);
  219. efi_call_phys_prelog();
  220. status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
  221. virt_to_phys(tc));
  222. efi_call_phys_epilog();
  223. spin_unlock_irqrestore(&rtc_lock, flags);
  224. return status;
  225. }
  226. int efi_set_rtc_mmss(unsigned long nowtime)
  227. {
  228. int real_seconds, real_minutes;
  229. efi_status_t status;
  230. efi_time_t eft;
  231. efi_time_cap_t cap;
  232. status = efi.get_time(&eft, &cap);
  233. if (status != EFI_SUCCESS) {
  234. pr_err("Oops: efitime: can't read time!\n");
  235. return -1;
  236. }
  237. real_seconds = nowtime % 60;
  238. real_minutes = nowtime / 60;
  239. if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
  240. real_minutes += 30;
  241. real_minutes %= 60;
  242. eft.minute = real_minutes;
  243. eft.second = real_seconds;
  244. status = efi.set_time(&eft);
  245. if (status != EFI_SUCCESS) {
  246. pr_err("Oops: efitime: can't write time!\n");
  247. return -1;
  248. }
  249. return 0;
  250. }
  251. unsigned long efi_get_time(void)
  252. {
  253. efi_status_t status;
  254. efi_time_t eft;
  255. efi_time_cap_t cap;
  256. status = efi.get_time(&eft, &cap);
  257. if (status != EFI_SUCCESS)
  258. pr_err("Oops: efitime: can't read time!\n");
  259. return mktime(eft.year, eft.month, eft.day, eft.hour,
  260. eft.minute, eft.second);
  261. }
  262. /*
  263. * Tell the kernel about the EFI memory map. This might include
  264. * more than the max 128 entries that can fit in the e820 legacy
  265. * (zeropage) memory map.
  266. */
  267. static void __init do_add_efi_memmap(void)
  268. {
  269. void *p;
  270. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  271. efi_memory_desc_t *md = p;
  272. unsigned long long start = md->phys_addr;
  273. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  274. int e820_type;
  275. switch (md->type) {
  276. case EFI_LOADER_CODE:
  277. case EFI_LOADER_DATA:
  278. case EFI_BOOT_SERVICES_CODE:
  279. case EFI_BOOT_SERVICES_DATA:
  280. case EFI_CONVENTIONAL_MEMORY:
  281. if (md->attribute & EFI_MEMORY_WB)
  282. e820_type = E820_RAM;
  283. else
  284. e820_type = E820_RESERVED;
  285. break;
  286. case EFI_ACPI_RECLAIM_MEMORY:
  287. e820_type = E820_ACPI;
  288. break;
  289. case EFI_ACPI_MEMORY_NVS:
  290. e820_type = E820_NVS;
  291. break;
  292. case EFI_UNUSABLE_MEMORY:
  293. e820_type = E820_UNUSABLE;
  294. break;
  295. default:
  296. /*
  297. * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
  298. * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
  299. * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
  300. */
  301. e820_type = E820_RESERVED;
  302. break;
  303. }
  304. e820_add_region(start, size, e820_type);
  305. }
  306. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  307. }
  308. int __init efi_memblock_x86_reserve_range(void)
  309. {
  310. unsigned long pmap;
  311. #ifdef CONFIG_X86_32
  312. /* Can't handle data above 4GB at this time */
  313. if (boot_params.efi_info.efi_memmap_hi) {
  314. pr_err("Memory map is above 4GB, disabling EFI.\n");
  315. return -EINVAL;
  316. }
  317. pmap = boot_params.efi_info.efi_memmap;
  318. #else
  319. pmap = (boot_params.efi_info.efi_memmap |
  320. ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
  321. #endif
  322. memmap.phys_map = (void *)pmap;
  323. memmap.nr_map = boot_params.efi_info.efi_memmap_size /
  324. boot_params.efi_info.efi_memdesc_size;
  325. memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
  326. memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
  327. memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
  328. return 0;
  329. }
  330. #if EFI_DEBUG
  331. static void __init print_efi_memmap(void)
  332. {
  333. efi_memory_desc_t *md;
  334. void *p;
  335. int i;
  336. for (p = memmap.map, i = 0;
  337. p < memmap.map_end;
  338. p += memmap.desc_size, i++) {
  339. md = p;
  340. pr_info("mem%02u: type=%u, attr=0x%llx, "
  341. "range=[0x%016llx-0x%016llx) (%lluMB)\n",
  342. i, md->type, md->attribute, md->phys_addr,
  343. md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
  344. (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
  345. }
  346. }
  347. #endif /* EFI_DEBUG */
  348. void __init efi_reserve_boot_services(void)
  349. {
  350. void *p;
  351. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  352. efi_memory_desc_t *md = p;
  353. u64 start = md->phys_addr;
  354. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  355. if (md->type != EFI_BOOT_SERVICES_CODE &&
  356. md->type != EFI_BOOT_SERVICES_DATA)
  357. continue;
  358. /* Only reserve where possible:
  359. * - Not within any already allocated areas
  360. * - Not over any memory area (really needed, if above?)
  361. * - Not within any part of the kernel
  362. * - Not the bios reserved area
  363. */
  364. if ((start+size >= __pa_symbol(_text)
  365. && start <= __pa_symbol(_end)) ||
  366. !e820_all_mapped(start, start+size, E820_RAM) ||
  367. memblock_is_region_reserved(start, size)) {
  368. /* Could not reserve, skip it */
  369. md->num_pages = 0;
  370. memblock_dbg("Could not reserve boot range "
  371. "[0x%010llx-0x%010llx]\n",
  372. start, start+size-1);
  373. } else
  374. memblock_reserve(start, size);
  375. }
  376. }
  377. void __init efi_unmap_memmap(void)
  378. {
  379. clear_bit(EFI_MEMMAP, &x86_efi_facility);
  380. if (memmap.map) {
  381. early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
  382. memmap.map = NULL;
  383. }
  384. }
  385. void __init efi_free_boot_services(void)
  386. {
  387. void *p;
  388. if (!efi_is_native())
  389. return;
  390. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  391. efi_memory_desc_t *md = p;
  392. unsigned long long start = md->phys_addr;
  393. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  394. if (md->type != EFI_BOOT_SERVICES_CODE &&
  395. md->type != EFI_BOOT_SERVICES_DATA)
  396. continue;
  397. /* Could not reserve boot area */
  398. if (!size)
  399. continue;
  400. free_bootmem_late(start, size);
  401. }
  402. efi_unmap_memmap();
  403. }
  404. static int __init efi_systab_init(void *phys)
  405. {
  406. if (efi_enabled(EFI_64BIT)) {
  407. efi_system_table_64_t *systab64;
  408. u64 tmp = 0;
  409. systab64 = early_ioremap((unsigned long)phys,
  410. sizeof(*systab64));
  411. if (systab64 == NULL) {
  412. pr_err("Couldn't map the system table!\n");
  413. return -ENOMEM;
  414. }
  415. efi_systab.hdr = systab64->hdr;
  416. efi_systab.fw_vendor = systab64->fw_vendor;
  417. tmp |= systab64->fw_vendor;
  418. efi_systab.fw_revision = systab64->fw_revision;
  419. efi_systab.con_in_handle = systab64->con_in_handle;
  420. tmp |= systab64->con_in_handle;
  421. efi_systab.con_in = systab64->con_in;
  422. tmp |= systab64->con_in;
  423. efi_systab.con_out_handle = systab64->con_out_handle;
  424. tmp |= systab64->con_out_handle;
  425. efi_systab.con_out = systab64->con_out;
  426. tmp |= systab64->con_out;
  427. efi_systab.stderr_handle = systab64->stderr_handle;
  428. tmp |= systab64->stderr_handle;
  429. efi_systab.stderr = systab64->stderr;
  430. tmp |= systab64->stderr;
  431. efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
  432. tmp |= systab64->runtime;
  433. efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
  434. tmp |= systab64->boottime;
  435. efi_systab.nr_tables = systab64->nr_tables;
  436. efi_systab.tables = systab64->tables;
  437. tmp |= systab64->tables;
  438. early_iounmap(systab64, sizeof(*systab64));
  439. #ifdef CONFIG_X86_32
  440. if (tmp >> 32) {
  441. pr_err("EFI data located above 4GB, disabling EFI.\n");
  442. return -EINVAL;
  443. }
  444. #endif
  445. } else {
  446. efi_system_table_32_t *systab32;
  447. systab32 = early_ioremap((unsigned long)phys,
  448. sizeof(*systab32));
  449. if (systab32 == NULL) {
  450. pr_err("Couldn't map the system table!\n");
  451. return -ENOMEM;
  452. }
  453. efi_systab.hdr = systab32->hdr;
  454. efi_systab.fw_vendor = systab32->fw_vendor;
  455. efi_systab.fw_revision = systab32->fw_revision;
  456. efi_systab.con_in_handle = systab32->con_in_handle;
  457. efi_systab.con_in = systab32->con_in;
  458. efi_systab.con_out_handle = systab32->con_out_handle;
  459. efi_systab.con_out = systab32->con_out;
  460. efi_systab.stderr_handle = systab32->stderr_handle;
  461. efi_systab.stderr = systab32->stderr;
  462. efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
  463. efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
  464. efi_systab.nr_tables = systab32->nr_tables;
  465. efi_systab.tables = systab32->tables;
  466. early_iounmap(systab32, sizeof(*systab32));
  467. }
  468. efi.systab = &efi_systab;
  469. /*
  470. * Verify the EFI Table
  471. */
  472. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  473. pr_err("System table signature incorrect!\n");
  474. return -EINVAL;
  475. }
  476. if ((efi.systab->hdr.revision >> 16) == 0)
  477. pr_err("Warning: System table version "
  478. "%d.%02d, expected 1.00 or greater!\n",
  479. efi.systab->hdr.revision >> 16,
  480. efi.systab->hdr.revision & 0xffff);
  481. return 0;
  482. }
  483. static int __init efi_config_init(u64 tables, int nr_tables)
  484. {
  485. void *config_tables, *tablep;
  486. int i, sz;
  487. if (efi_enabled(EFI_64BIT))
  488. sz = sizeof(efi_config_table_64_t);
  489. else
  490. sz = sizeof(efi_config_table_32_t);
  491. /*
  492. * Let's see what config tables the firmware passed to us.
  493. */
  494. config_tables = early_ioremap(tables, nr_tables * sz);
  495. if (config_tables == NULL) {
  496. pr_err("Could not map Configuration table!\n");
  497. return -ENOMEM;
  498. }
  499. tablep = config_tables;
  500. pr_info("");
  501. for (i = 0; i < efi.systab->nr_tables; i++) {
  502. efi_guid_t guid;
  503. unsigned long table;
  504. if (efi_enabled(EFI_64BIT)) {
  505. u64 table64;
  506. guid = ((efi_config_table_64_t *)tablep)->guid;
  507. table64 = ((efi_config_table_64_t *)tablep)->table;
  508. table = table64;
  509. #ifdef CONFIG_X86_32
  510. if (table64 >> 32) {
  511. pr_cont("\n");
  512. pr_err("Table located above 4GB, disabling EFI.\n");
  513. early_iounmap(config_tables,
  514. efi.systab->nr_tables * sz);
  515. return -EINVAL;
  516. }
  517. #endif
  518. } else {
  519. guid = ((efi_config_table_32_t *)tablep)->guid;
  520. table = ((efi_config_table_32_t *)tablep)->table;
  521. }
  522. if (!efi_guidcmp(guid, MPS_TABLE_GUID)) {
  523. efi.mps = table;
  524. pr_cont(" MPS=0x%lx ", table);
  525. } else if (!efi_guidcmp(guid, ACPI_20_TABLE_GUID)) {
  526. efi.acpi20 = table;
  527. pr_cont(" ACPI 2.0=0x%lx ", table);
  528. } else if (!efi_guidcmp(guid, ACPI_TABLE_GUID)) {
  529. efi.acpi = table;
  530. pr_cont(" ACPI=0x%lx ", table);
  531. } else if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) {
  532. efi.smbios = table;
  533. pr_cont(" SMBIOS=0x%lx ", table);
  534. #ifdef CONFIG_X86_UV
  535. } else if (!efi_guidcmp(guid, UV_SYSTEM_TABLE_GUID)) {
  536. efi.uv_systab = table;
  537. pr_cont(" UVsystab=0x%lx ", table);
  538. #endif
  539. } else if (!efi_guidcmp(guid, HCDP_TABLE_GUID)) {
  540. efi.hcdp = table;
  541. pr_cont(" HCDP=0x%lx ", table);
  542. } else if (!efi_guidcmp(guid, UGA_IO_PROTOCOL_GUID)) {
  543. efi.uga = table;
  544. pr_cont(" UGA=0x%lx ", table);
  545. }
  546. tablep += sz;
  547. }
  548. pr_cont("\n");
  549. early_iounmap(config_tables, efi.systab->nr_tables * sz);
  550. return 0;
  551. }
  552. static int __init efi_runtime_init(void)
  553. {
  554. efi_runtime_services_t *runtime;
  555. /*
  556. * Check out the runtime services table. We need to map
  557. * the runtime services table so that we can grab the physical
  558. * address of several of the EFI runtime functions, needed to
  559. * set the firmware into virtual mode.
  560. */
  561. runtime = early_ioremap((unsigned long)efi.systab->runtime,
  562. sizeof(efi_runtime_services_t));
  563. if (!runtime) {
  564. pr_err("Could not map the runtime service table!\n");
  565. return -ENOMEM;
  566. }
  567. /*
  568. * We will only need *early* access to the following
  569. * two EFI runtime services before set_virtual_address_map
  570. * is invoked.
  571. */
  572. efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
  573. efi_phys.set_virtual_address_map =
  574. (efi_set_virtual_address_map_t *)
  575. runtime->set_virtual_address_map;
  576. /*
  577. * Make efi_get_time can be called before entering
  578. * virtual mode.
  579. */
  580. efi.get_time = phys_efi_get_time;
  581. early_iounmap(runtime, sizeof(efi_runtime_services_t));
  582. return 0;
  583. }
  584. static int __init efi_memmap_init(void)
  585. {
  586. /* Map the EFI memory map */
  587. memmap.map = early_ioremap((unsigned long)memmap.phys_map,
  588. memmap.nr_map * memmap.desc_size);
  589. if (memmap.map == NULL) {
  590. pr_err("Could not map the memory map!\n");
  591. return -ENOMEM;
  592. }
  593. memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
  594. if (add_efi_memmap)
  595. do_add_efi_memmap();
  596. return 0;
  597. }
  598. void __init efi_init(void)
  599. {
  600. efi_char16_t *c16;
  601. char vendor[100] = "unknown";
  602. int i = 0;
  603. void *tmp;
  604. #ifdef CONFIG_X86_32
  605. if (boot_params.efi_info.efi_systab_hi ||
  606. boot_params.efi_info.efi_memmap_hi) {
  607. pr_info("Table located above 4GB, disabling EFI.\n");
  608. return;
  609. }
  610. efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
  611. #else
  612. efi_phys.systab = (efi_system_table_t *)
  613. (boot_params.efi_info.efi_systab |
  614. ((__u64)boot_params.efi_info.efi_systab_hi<<32));
  615. #endif
  616. if (efi_systab_init(efi_phys.systab))
  617. return;
  618. set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
  619. /*
  620. * Show what we know for posterity
  621. */
  622. c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
  623. if (c16) {
  624. for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
  625. vendor[i] = *c16++;
  626. vendor[i] = '\0';
  627. } else
  628. pr_err("Could not map the firmware vendor!\n");
  629. early_iounmap(tmp, 2);
  630. pr_info("EFI v%u.%.02u by %s\n",
  631. efi.systab->hdr.revision >> 16,
  632. efi.systab->hdr.revision & 0xffff, vendor);
  633. if (efi_config_init(efi.systab->tables, efi.systab->nr_tables))
  634. return;
  635. set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
  636. /*
  637. * Note: We currently don't support runtime services on an EFI
  638. * that doesn't match the kernel 32/64-bit mode.
  639. */
  640. if (!efi_is_native())
  641. pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
  642. else {
  643. if (disable_runtime || efi_runtime_init())
  644. return;
  645. set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
  646. }
  647. if (efi_memmap_init())
  648. return;
  649. set_bit(EFI_MEMMAP, &x86_efi_facility);
  650. #ifdef CONFIG_X86_32
  651. if (efi_is_native()) {
  652. x86_platform.get_wallclock = efi_get_time;
  653. x86_platform.set_wallclock = efi_set_rtc_mmss;
  654. }
  655. #endif
  656. #if EFI_DEBUG
  657. print_efi_memmap();
  658. #endif
  659. }
  660. void __init efi_late_init(void)
  661. {
  662. efi_bgrt_init();
  663. }
  664. void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
  665. {
  666. u64 addr, npages;
  667. addr = md->virt_addr;
  668. npages = md->num_pages;
  669. memrange_efi_to_native(&addr, &npages);
  670. if (executable)
  671. set_memory_x(addr, npages);
  672. else
  673. set_memory_nx(addr, npages);
  674. }
  675. static void __init runtime_code_page_mkexec(void)
  676. {
  677. efi_memory_desc_t *md;
  678. void *p;
  679. /* Make EFI runtime service code area executable */
  680. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  681. md = p;
  682. if (md->type != EFI_RUNTIME_SERVICES_CODE)
  683. continue;
  684. efi_set_executable(md, true);
  685. }
  686. }
  687. /*
  688. * We can't ioremap data in EFI boot services RAM, because we've already mapped
  689. * it as RAM. So, look it up in the existing EFI memory map instead. Only
  690. * callable after efi_enter_virtual_mode and before efi_free_boot_services.
  691. */
  692. void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
  693. {
  694. void *p;
  695. if (WARN_ON(!memmap.map))
  696. return NULL;
  697. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  698. efi_memory_desc_t *md = p;
  699. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  700. u64 end = md->phys_addr + size;
  701. if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
  702. md->type != EFI_BOOT_SERVICES_CODE &&
  703. md->type != EFI_BOOT_SERVICES_DATA)
  704. continue;
  705. if (!md->virt_addr)
  706. continue;
  707. if (phys_addr >= md->phys_addr && phys_addr < end) {
  708. phys_addr += md->virt_addr - md->phys_addr;
  709. return (__force void __iomem *)(unsigned long)phys_addr;
  710. }
  711. }
  712. return NULL;
  713. }
  714. void efi_memory_uc(u64 addr, unsigned long size)
  715. {
  716. unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
  717. u64 npages;
  718. npages = round_up(size, page_shift) / page_shift;
  719. memrange_efi_to_native(&addr, &npages);
  720. set_memory_uc(addr, npages);
  721. }
  722. /*
  723. * This function will switch the EFI runtime services to virtual mode.
  724. * Essentially, look through the EFI memmap and map every region that
  725. * has the runtime attribute bit set in its memory descriptor and update
  726. * that memory descriptor with the virtual address obtained from ioremap().
  727. * This enables the runtime services to be called without having to
  728. * thunk back into physical mode for every invocation.
  729. */
  730. void __init efi_enter_virtual_mode(void)
  731. {
  732. efi_memory_desc_t *md, *prev_md = NULL;
  733. efi_status_t status;
  734. unsigned long size;
  735. u64 end, systab, start_pfn, end_pfn;
  736. void *p, *va, *new_memmap = NULL;
  737. int count = 0;
  738. efi.systab = NULL;
  739. /*
  740. * We don't do virtual mode, since we don't do runtime services, on
  741. * non-native EFI
  742. */
  743. if (!efi_is_native()) {
  744. efi_unmap_memmap();
  745. return;
  746. }
  747. /* Merge contiguous regions of the same type and attribute */
  748. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  749. u64 prev_size;
  750. md = p;
  751. if (!prev_md) {
  752. prev_md = md;
  753. continue;
  754. }
  755. if (prev_md->type != md->type ||
  756. prev_md->attribute != md->attribute) {
  757. prev_md = md;
  758. continue;
  759. }
  760. prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
  761. if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
  762. prev_md->num_pages += md->num_pages;
  763. md->type = EFI_RESERVED_TYPE;
  764. md->attribute = 0;
  765. continue;
  766. }
  767. prev_md = md;
  768. }
  769. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  770. md = p;
  771. if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
  772. md->type != EFI_BOOT_SERVICES_CODE &&
  773. md->type != EFI_BOOT_SERVICES_DATA)
  774. continue;
  775. size = md->num_pages << EFI_PAGE_SHIFT;
  776. end = md->phys_addr + size;
  777. start_pfn = PFN_DOWN(md->phys_addr);
  778. end_pfn = PFN_UP(end);
  779. if (pfn_range_is_mapped(start_pfn, end_pfn)) {
  780. va = __va(md->phys_addr);
  781. if (!(md->attribute & EFI_MEMORY_WB))
  782. efi_memory_uc((u64)(unsigned long)va, size);
  783. } else
  784. va = efi_ioremap(md->phys_addr, size,
  785. md->type, md->attribute);
  786. md->virt_addr = (u64) (unsigned long) va;
  787. if (!va) {
  788. pr_err("ioremap of 0x%llX failed!\n",
  789. (unsigned long long)md->phys_addr);
  790. continue;
  791. }
  792. systab = (u64) (unsigned long) efi_phys.systab;
  793. if (md->phys_addr <= systab && systab < end) {
  794. systab += md->virt_addr - md->phys_addr;
  795. efi.systab = (efi_system_table_t *) (unsigned long) systab;
  796. }
  797. new_memmap = krealloc(new_memmap,
  798. (count + 1) * memmap.desc_size,
  799. GFP_KERNEL);
  800. memcpy(new_memmap + (count * memmap.desc_size), md,
  801. memmap.desc_size);
  802. count++;
  803. }
  804. BUG_ON(!efi.systab);
  805. status = phys_efi_set_virtual_address_map(
  806. memmap.desc_size * count,
  807. memmap.desc_size,
  808. memmap.desc_version,
  809. (efi_memory_desc_t *)__pa(new_memmap));
  810. if (status != EFI_SUCCESS) {
  811. pr_alert("Unable to switch EFI into virtual mode "
  812. "(status=%lx)!\n", status);
  813. panic("EFI call to SetVirtualAddressMap() failed!");
  814. }
  815. /*
  816. * Now that EFI is in virtual mode, update the function
  817. * pointers in the runtime service table to the new virtual addresses.
  818. *
  819. * Call EFI services through wrapper functions.
  820. */
  821. efi.runtime_version = efi_systab.hdr.revision;
  822. efi.get_time = virt_efi_get_time;
  823. efi.set_time = virt_efi_set_time;
  824. efi.get_wakeup_time = virt_efi_get_wakeup_time;
  825. efi.set_wakeup_time = virt_efi_set_wakeup_time;
  826. efi.get_variable = virt_efi_get_variable;
  827. efi.get_next_variable = virt_efi_get_next_variable;
  828. efi.set_variable = virt_efi_set_variable;
  829. efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
  830. efi.reset_system = virt_efi_reset_system;
  831. efi.set_virtual_address_map = NULL;
  832. efi.query_variable_info = virt_efi_query_variable_info;
  833. efi.update_capsule = virt_efi_update_capsule;
  834. efi.query_capsule_caps = virt_efi_query_capsule_caps;
  835. if (__supported_pte_mask & _PAGE_NX)
  836. runtime_code_page_mkexec();
  837. kfree(new_memmap);
  838. }
  839. /*
  840. * Convenience functions to obtain memory types and attributes
  841. */
  842. u32 efi_mem_type(unsigned long phys_addr)
  843. {
  844. efi_memory_desc_t *md;
  845. void *p;
  846. if (!efi_enabled(EFI_MEMMAP))
  847. return 0;
  848. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  849. md = p;
  850. if ((md->phys_addr <= phys_addr) &&
  851. (phys_addr < (md->phys_addr +
  852. (md->num_pages << EFI_PAGE_SHIFT))))
  853. return md->type;
  854. }
  855. return 0;
  856. }
  857. u64 efi_mem_attributes(unsigned long phys_addr)
  858. {
  859. efi_memory_desc_t *md;
  860. void *p;
  861. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  862. md = p;
  863. if ((md->phys_addr <= phys_addr) &&
  864. (phys_addr < (md->phys_addr +
  865. (md->num_pages << EFI_PAGE_SHIFT))))
  866. return md->attribute;
  867. }
  868. return 0;
  869. }