efi.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Common EFI (Extensible Firmware Interface) support functions
  4. * Based on Extensible Firmware Interface Specification version 1.0
  5. *
  6. * Copyright (C) 1999 VA Linux Systems
  7. * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
  8. * Copyright (C) 1999-2002 Hewlett-Packard Co.
  9. * David Mosberger-Tang <davidm@hpl.hp.com>
  10. * Stephane Eranian <eranian@hpl.hp.com>
  11. * Copyright (C) 2005-2008 Intel Co.
  12. * Fenghua Yu <fenghua.yu@intel.com>
  13. * Bibo Mao <bibo.mao@intel.com>
  14. * Chandramouli Narayanan <mouli@linux.intel.com>
  15. * Huang Ying <ying.huang@intel.com>
  16. * Copyright (C) 2013 SuSE Labs
  17. * Borislav Petkov <bp@suse.de> - runtime services VA mapping
  18. *
  19. * Copied from efi_32.c to eliminate the duplicated code between EFI
  20. * 32/64 support code. --ying 2007-10-26
  21. *
  22. * All EFI Runtime Services are not implemented yet as EFI only
  23. * supports physical mode addressing on SoftSDV. This is to be fixed
  24. * in a future version. --drummond 1999-07-20
  25. *
  26. * Implemented EFI runtime services and virtual mode calls. --davidm
  27. *
  28. * Goutham Rao: <goutham.rao@intel.com>
  29. * Skip non-WB memory and ignore empty memory ranges.
  30. */
  31. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  32. #include <linux/kernel.h>
  33. #include <linux/init.h>
  34. #include <linux/efi.h>
  35. #include <linux/efi-bgrt.h>
  36. #include <linux/export.h>
  37. #include <linux/bootmem.h>
  38. #include <linux/slab.h>
  39. #include <linux/memblock.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/uaccess.h>
  42. #include <linux/time.h>
  43. #include <linux/io.h>
  44. #include <linux/reboot.h>
  45. #include <linux/bcd.h>
  46. #include <asm/setup.h>
  47. #include <asm/efi.h>
  48. #include <asm/e820/api.h>
  49. #include <asm/time.h>
  50. #include <asm/set_memory.h>
  51. #include <asm/tlbflush.h>
  52. #include <asm/x86_init.h>
  53. #include <asm/uv/uv.h>
  54. static struct efi efi_phys __initdata;
  55. static efi_system_table_t efi_systab __initdata;
  56. static efi_config_table_type_t arch_tables[] __initdata = {
  57. #ifdef CONFIG_X86_UV
  58. {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
  59. #endif
  60. {NULL_GUID, NULL, NULL},
  61. };
  62. u64 efi_setup; /* efi setup_data physical address */
  63. static int add_efi_memmap __initdata;
  64. static int __init setup_add_efi_memmap(char *arg)
  65. {
  66. add_efi_memmap = 1;
  67. return 0;
  68. }
  69. early_param("add_efi_memmap", setup_add_efi_memmap);
  70. static efi_status_t __init phys_efi_set_virtual_address_map(
  71. unsigned long memory_map_size,
  72. unsigned long descriptor_size,
  73. u32 descriptor_version,
  74. efi_memory_desc_t *virtual_map)
  75. {
  76. efi_status_t status;
  77. unsigned long flags;
  78. pgd_t *save_pgd;
  79. save_pgd = efi_call_phys_prolog();
  80. /* Disable interrupts around EFI calls: */
  81. local_irq_save(flags);
  82. status = efi_call_phys(efi_phys.set_virtual_address_map,
  83. memory_map_size, descriptor_size,
  84. descriptor_version, virtual_map);
  85. local_irq_restore(flags);
  86. efi_call_phys_epilog(save_pgd);
  87. return status;
  88. }
  89. void __init efi_find_mirror(void)
  90. {
  91. efi_memory_desc_t *md;
  92. u64 mirror_size = 0, total_size = 0;
  93. for_each_efi_memory_desc(md) {
  94. unsigned long long start = md->phys_addr;
  95. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  96. total_size += size;
  97. if (md->attribute & EFI_MEMORY_MORE_RELIABLE) {
  98. memblock_mark_mirror(start, size);
  99. mirror_size += size;
  100. }
  101. }
  102. if (mirror_size)
  103. pr_info("Memory: %lldM/%lldM mirrored memory\n",
  104. mirror_size>>20, total_size>>20);
  105. }
  106. /*
  107. * Tell the kernel about the EFI memory map. This might include
  108. * more than the max 128 entries that can fit in the e820 legacy
  109. * (zeropage) memory map.
  110. */
  111. static void __init do_add_efi_memmap(void)
  112. {
  113. efi_memory_desc_t *md;
  114. for_each_efi_memory_desc(md) {
  115. unsigned long long start = md->phys_addr;
  116. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  117. int e820_type;
  118. switch (md->type) {
  119. case EFI_LOADER_CODE:
  120. case EFI_LOADER_DATA:
  121. case EFI_BOOT_SERVICES_CODE:
  122. case EFI_BOOT_SERVICES_DATA:
  123. case EFI_CONVENTIONAL_MEMORY:
  124. if (md->attribute & EFI_MEMORY_WB)
  125. e820_type = E820_TYPE_RAM;
  126. else
  127. e820_type = E820_TYPE_RESERVED;
  128. break;
  129. case EFI_ACPI_RECLAIM_MEMORY:
  130. e820_type = E820_TYPE_ACPI;
  131. break;
  132. case EFI_ACPI_MEMORY_NVS:
  133. e820_type = E820_TYPE_NVS;
  134. break;
  135. case EFI_UNUSABLE_MEMORY:
  136. e820_type = E820_TYPE_UNUSABLE;
  137. break;
  138. case EFI_PERSISTENT_MEMORY:
  139. e820_type = E820_TYPE_PMEM;
  140. break;
  141. default:
  142. /*
  143. * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
  144. * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
  145. * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
  146. */
  147. e820_type = E820_TYPE_RESERVED;
  148. break;
  149. }
  150. e820__range_add(start, size, e820_type);
  151. }
  152. e820__update_table(e820_table);
  153. }
  154. int __init efi_memblock_x86_reserve_range(void)
  155. {
  156. struct efi_info *e = &boot_params.efi_info;
  157. struct efi_memory_map_data data;
  158. phys_addr_t pmap;
  159. int rv;
  160. if (efi_enabled(EFI_PARAVIRT))
  161. return 0;
  162. #ifdef CONFIG_X86_32
  163. /* Can't handle data above 4GB at this time */
  164. if (e->efi_memmap_hi) {
  165. pr_err("Memory map is above 4GB, disabling EFI.\n");
  166. return -EINVAL;
  167. }
  168. pmap = e->efi_memmap;
  169. #else
  170. pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
  171. #endif
  172. data.phys_map = pmap;
  173. data.size = e->efi_memmap_size;
  174. data.desc_size = e->efi_memdesc_size;
  175. data.desc_version = e->efi_memdesc_version;
  176. rv = efi_memmap_init_early(&data);
  177. if (rv)
  178. return rv;
  179. if (add_efi_memmap)
  180. do_add_efi_memmap();
  181. WARN(efi.memmap.desc_version != 1,
  182. "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
  183. efi.memmap.desc_version);
  184. memblock_reserve(pmap, efi.memmap.nr_map * efi.memmap.desc_size);
  185. return 0;
  186. }
  187. #define OVERFLOW_ADDR_SHIFT (64 - EFI_PAGE_SHIFT)
  188. #define OVERFLOW_ADDR_MASK (U64_MAX << OVERFLOW_ADDR_SHIFT)
  189. #define U64_HIGH_BIT (~(U64_MAX >> 1))
  190. static bool __init efi_memmap_entry_valid(const efi_memory_desc_t *md, int i)
  191. {
  192. u64 end = (md->num_pages << EFI_PAGE_SHIFT) + md->phys_addr - 1;
  193. u64 end_hi = 0;
  194. char buf[64];
  195. if (md->num_pages == 0) {
  196. end = 0;
  197. } else if (md->num_pages > EFI_PAGES_MAX ||
  198. EFI_PAGES_MAX - md->num_pages <
  199. (md->phys_addr >> EFI_PAGE_SHIFT)) {
  200. end_hi = (md->num_pages & OVERFLOW_ADDR_MASK)
  201. >> OVERFLOW_ADDR_SHIFT;
  202. if ((md->phys_addr & U64_HIGH_BIT) && !(end & U64_HIGH_BIT))
  203. end_hi += 1;
  204. } else {
  205. return true;
  206. }
  207. pr_warn_once(FW_BUG "Invalid EFI memory map entries:\n");
  208. if (end_hi) {
  209. pr_warn("mem%02u: %s range=[0x%016llx-0x%llx%016llx] (invalid)\n",
  210. i, efi_md_typeattr_format(buf, sizeof(buf), md),
  211. md->phys_addr, end_hi, end);
  212. } else {
  213. pr_warn("mem%02u: %s range=[0x%016llx-0x%016llx] (invalid)\n",
  214. i, efi_md_typeattr_format(buf, sizeof(buf), md),
  215. md->phys_addr, end);
  216. }
  217. return false;
  218. }
  219. static void __init efi_clean_memmap(void)
  220. {
  221. efi_memory_desc_t *out = efi.memmap.map;
  222. const efi_memory_desc_t *in = out;
  223. const efi_memory_desc_t *end = efi.memmap.map_end;
  224. int i, n_removal;
  225. for (i = n_removal = 0; in < end; i++) {
  226. if (efi_memmap_entry_valid(in, i)) {
  227. if (out != in)
  228. memcpy(out, in, efi.memmap.desc_size);
  229. out = (void *)out + efi.memmap.desc_size;
  230. } else {
  231. n_removal++;
  232. }
  233. in = (void *)in + efi.memmap.desc_size;
  234. }
  235. if (n_removal > 0) {
  236. u64 size = efi.memmap.nr_map - n_removal;
  237. pr_warn("Removing %d invalid memory map entries.\n", n_removal);
  238. efi_memmap_install(efi.memmap.phys_map, size);
  239. }
  240. }
  241. void __init efi_print_memmap(void)
  242. {
  243. efi_memory_desc_t *md;
  244. int i = 0;
  245. for_each_efi_memory_desc(md) {
  246. char buf[64];
  247. pr_info("mem%02u: %s range=[0x%016llx-0x%016llx] (%lluMB)\n",
  248. i++, efi_md_typeattr_format(buf, sizeof(buf), md),
  249. md->phys_addr,
  250. md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1,
  251. (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
  252. }
  253. }
  254. static int __init efi_systab_init(void *phys)
  255. {
  256. if (efi_enabled(EFI_64BIT)) {
  257. efi_system_table_64_t *systab64;
  258. struct efi_setup_data *data = NULL;
  259. u64 tmp = 0;
  260. if (efi_setup) {
  261. data = early_memremap(efi_setup, sizeof(*data));
  262. if (!data)
  263. return -ENOMEM;
  264. }
  265. systab64 = early_memremap((unsigned long)phys,
  266. sizeof(*systab64));
  267. if (systab64 == NULL) {
  268. pr_err("Couldn't map the system table!\n");
  269. if (data)
  270. early_memunmap(data, sizeof(*data));
  271. return -ENOMEM;
  272. }
  273. efi_systab.hdr = systab64->hdr;
  274. efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
  275. systab64->fw_vendor;
  276. tmp |= data ? data->fw_vendor : systab64->fw_vendor;
  277. efi_systab.fw_revision = systab64->fw_revision;
  278. efi_systab.con_in_handle = systab64->con_in_handle;
  279. tmp |= systab64->con_in_handle;
  280. efi_systab.con_in = systab64->con_in;
  281. tmp |= systab64->con_in;
  282. efi_systab.con_out_handle = systab64->con_out_handle;
  283. tmp |= systab64->con_out_handle;
  284. efi_systab.con_out = systab64->con_out;
  285. tmp |= systab64->con_out;
  286. efi_systab.stderr_handle = systab64->stderr_handle;
  287. tmp |= systab64->stderr_handle;
  288. efi_systab.stderr = systab64->stderr;
  289. tmp |= systab64->stderr;
  290. efi_systab.runtime = data ?
  291. (void *)(unsigned long)data->runtime :
  292. (void *)(unsigned long)systab64->runtime;
  293. tmp |= data ? data->runtime : systab64->runtime;
  294. efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
  295. tmp |= systab64->boottime;
  296. efi_systab.nr_tables = systab64->nr_tables;
  297. efi_systab.tables = data ? (unsigned long)data->tables :
  298. systab64->tables;
  299. tmp |= data ? data->tables : systab64->tables;
  300. early_memunmap(systab64, sizeof(*systab64));
  301. if (data)
  302. early_memunmap(data, sizeof(*data));
  303. #ifdef CONFIG_X86_32
  304. if (tmp >> 32) {
  305. pr_err("EFI data located above 4GB, disabling EFI.\n");
  306. return -EINVAL;
  307. }
  308. #endif
  309. } else {
  310. efi_system_table_32_t *systab32;
  311. systab32 = early_memremap((unsigned long)phys,
  312. sizeof(*systab32));
  313. if (systab32 == NULL) {
  314. pr_err("Couldn't map the system table!\n");
  315. return -ENOMEM;
  316. }
  317. efi_systab.hdr = systab32->hdr;
  318. efi_systab.fw_vendor = systab32->fw_vendor;
  319. efi_systab.fw_revision = systab32->fw_revision;
  320. efi_systab.con_in_handle = systab32->con_in_handle;
  321. efi_systab.con_in = systab32->con_in;
  322. efi_systab.con_out_handle = systab32->con_out_handle;
  323. efi_systab.con_out = systab32->con_out;
  324. efi_systab.stderr_handle = systab32->stderr_handle;
  325. efi_systab.stderr = systab32->stderr;
  326. efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
  327. efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
  328. efi_systab.nr_tables = systab32->nr_tables;
  329. efi_systab.tables = systab32->tables;
  330. early_memunmap(systab32, sizeof(*systab32));
  331. }
  332. efi.systab = &efi_systab;
  333. /*
  334. * Verify the EFI Table
  335. */
  336. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  337. pr_err("System table signature incorrect!\n");
  338. return -EINVAL;
  339. }
  340. if ((efi.systab->hdr.revision >> 16) == 0)
  341. pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
  342. efi.systab->hdr.revision >> 16,
  343. efi.systab->hdr.revision & 0xffff);
  344. return 0;
  345. }
  346. static int __init efi_runtime_init32(void)
  347. {
  348. efi_runtime_services_32_t *runtime;
  349. runtime = early_memremap((unsigned long)efi.systab->runtime,
  350. sizeof(efi_runtime_services_32_t));
  351. if (!runtime) {
  352. pr_err("Could not map the runtime service table!\n");
  353. return -ENOMEM;
  354. }
  355. /*
  356. * We will only need *early* access to the SetVirtualAddressMap
  357. * EFI runtime service. All other runtime services will be called
  358. * via the virtual mapping.
  359. */
  360. efi_phys.set_virtual_address_map =
  361. (efi_set_virtual_address_map_t *)
  362. (unsigned long)runtime->set_virtual_address_map;
  363. early_memunmap(runtime, sizeof(efi_runtime_services_32_t));
  364. return 0;
  365. }
  366. static int __init efi_runtime_init64(void)
  367. {
  368. efi_runtime_services_64_t *runtime;
  369. runtime = early_memremap((unsigned long)efi.systab->runtime,
  370. sizeof(efi_runtime_services_64_t));
  371. if (!runtime) {
  372. pr_err("Could not map the runtime service table!\n");
  373. return -ENOMEM;
  374. }
  375. /*
  376. * We will only need *early* access to the SetVirtualAddressMap
  377. * EFI runtime service. All other runtime services will be called
  378. * via the virtual mapping.
  379. */
  380. efi_phys.set_virtual_address_map =
  381. (efi_set_virtual_address_map_t *)
  382. (unsigned long)runtime->set_virtual_address_map;
  383. early_memunmap(runtime, sizeof(efi_runtime_services_64_t));
  384. return 0;
  385. }
  386. static int __init efi_runtime_init(void)
  387. {
  388. int rv;
  389. /*
  390. * Check out the runtime services table. We need to map
  391. * the runtime services table so that we can grab the physical
  392. * address of several of the EFI runtime functions, needed to
  393. * set the firmware into virtual mode.
  394. *
  395. * When EFI_PARAVIRT is in force then we could not map runtime
  396. * service memory region because we do not have direct access to it.
  397. * However, runtime services are available through proxy functions
  398. * (e.g. in case of Xen dom0 EFI implementation they call special
  399. * hypercall which executes relevant EFI functions) and that is why
  400. * they are always enabled.
  401. */
  402. if (!efi_enabled(EFI_PARAVIRT)) {
  403. if (efi_enabled(EFI_64BIT))
  404. rv = efi_runtime_init64();
  405. else
  406. rv = efi_runtime_init32();
  407. if (rv)
  408. return rv;
  409. }
  410. set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  411. return 0;
  412. }
  413. void __init efi_init(void)
  414. {
  415. efi_char16_t *c16;
  416. char vendor[100] = "unknown";
  417. int i = 0;
  418. void *tmp;
  419. #ifdef CONFIG_X86_32
  420. if (boot_params.efi_info.efi_systab_hi ||
  421. boot_params.efi_info.efi_memmap_hi) {
  422. pr_info("Table located above 4GB, disabling EFI.\n");
  423. return;
  424. }
  425. efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
  426. #else
  427. efi_phys.systab = (efi_system_table_t *)
  428. (boot_params.efi_info.efi_systab |
  429. ((__u64)boot_params.efi_info.efi_systab_hi<<32));
  430. #endif
  431. if (efi_systab_init(efi_phys.systab))
  432. return;
  433. efi.config_table = (unsigned long)efi.systab->tables;
  434. efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
  435. efi.runtime = (unsigned long)efi.systab->runtime;
  436. /*
  437. * Show what we know for posterity
  438. */
  439. c16 = tmp = early_memremap(efi.systab->fw_vendor, 2);
  440. if (c16) {
  441. for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
  442. vendor[i] = *c16++;
  443. vendor[i] = '\0';
  444. } else
  445. pr_err("Could not map the firmware vendor!\n");
  446. early_memunmap(tmp, 2);
  447. pr_info("EFI v%u.%.02u by %s\n",
  448. efi.systab->hdr.revision >> 16,
  449. efi.systab->hdr.revision & 0xffff, vendor);
  450. if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
  451. return;
  452. if (efi_config_init(arch_tables))
  453. return;
  454. /*
  455. * Note: We currently don't support runtime services on an EFI
  456. * that doesn't match the kernel 32/64-bit mode.
  457. */
  458. if (!efi_runtime_supported())
  459. pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
  460. else {
  461. if (efi_runtime_disabled() || efi_runtime_init()) {
  462. efi_memmap_unmap();
  463. return;
  464. }
  465. }
  466. efi_clean_memmap();
  467. if (efi_enabled(EFI_DBG))
  468. efi_print_memmap();
  469. }
  470. void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
  471. {
  472. u64 addr, npages;
  473. addr = md->virt_addr;
  474. npages = md->num_pages;
  475. memrange_efi_to_native(&addr, &npages);
  476. if (executable)
  477. set_memory_x(addr, npages);
  478. else
  479. set_memory_nx(addr, npages);
  480. }
  481. void __init runtime_code_page_mkexec(void)
  482. {
  483. efi_memory_desc_t *md;
  484. /* Make EFI runtime service code area executable */
  485. for_each_efi_memory_desc(md) {
  486. if (md->type != EFI_RUNTIME_SERVICES_CODE)
  487. continue;
  488. efi_set_executable(md, true);
  489. }
  490. }
  491. void __init efi_memory_uc(u64 addr, unsigned long size)
  492. {
  493. unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
  494. u64 npages;
  495. npages = round_up(size, page_shift) / page_shift;
  496. memrange_efi_to_native(&addr, &npages);
  497. set_memory_uc(addr, npages);
  498. }
  499. void __init old_map_region(efi_memory_desc_t *md)
  500. {
  501. u64 start_pfn, end_pfn, end;
  502. unsigned long size;
  503. void *va;
  504. start_pfn = PFN_DOWN(md->phys_addr);
  505. size = md->num_pages << PAGE_SHIFT;
  506. end = md->phys_addr + size;
  507. end_pfn = PFN_UP(end);
  508. if (pfn_range_is_mapped(start_pfn, end_pfn)) {
  509. va = __va(md->phys_addr);
  510. if (!(md->attribute & EFI_MEMORY_WB))
  511. efi_memory_uc((u64)(unsigned long)va, size);
  512. } else
  513. va = efi_ioremap(md->phys_addr, size,
  514. md->type, md->attribute);
  515. md->virt_addr = (u64) (unsigned long) va;
  516. if (!va)
  517. pr_err("ioremap of 0x%llX failed!\n",
  518. (unsigned long long)md->phys_addr);
  519. }
  520. /* Merge contiguous regions of the same type and attribute */
  521. static void __init efi_merge_regions(void)
  522. {
  523. efi_memory_desc_t *md, *prev_md = NULL;
  524. for_each_efi_memory_desc(md) {
  525. u64 prev_size;
  526. if (!prev_md) {
  527. prev_md = md;
  528. continue;
  529. }
  530. if (prev_md->type != md->type ||
  531. prev_md->attribute != md->attribute) {
  532. prev_md = md;
  533. continue;
  534. }
  535. prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
  536. if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
  537. prev_md->num_pages += md->num_pages;
  538. md->type = EFI_RESERVED_TYPE;
  539. md->attribute = 0;
  540. continue;
  541. }
  542. prev_md = md;
  543. }
  544. }
  545. static void __init get_systab_virt_addr(efi_memory_desc_t *md)
  546. {
  547. unsigned long size;
  548. u64 end, systab;
  549. size = md->num_pages << EFI_PAGE_SHIFT;
  550. end = md->phys_addr + size;
  551. systab = (u64)(unsigned long)efi_phys.systab;
  552. if (md->phys_addr <= systab && systab < end) {
  553. systab += md->virt_addr - md->phys_addr;
  554. efi.systab = (efi_system_table_t *)(unsigned long)systab;
  555. }
  556. }
  557. static void *realloc_pages(void *old_memmap, int old_shift)
  558. {
  559. void *ret;
  560. ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
  561. if (!ret)
  562. goto out;
  563. /*
  564. * A first-time allocation doesn't have anything to copy.
  565. */
  566. if (!old_memmap)
  567. return ret;
  568. memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
  569. out:
  570. free_pages((unsigned long)old_memmap, old_shift);
  571. return ret;
  572. }
  573. /*
  574. * Iterate the EFI memory map in reverse order because the regions
  575. * will be mapped top-down. The end result is the same as if we had
  576. * mapped things forward, but doesn't require us to change the
  577. * existing implementation of efi_map_region().
  578. */
  579. static inline void *efi_map_next_entry_reverse(void *entry)
  580. {
  581. /* Initial call */
  582. if (!entry)
  583. return efi.memmap.map_end - efi.memmap.desc_size;
  584. entry -= efi.memmap.desc_size;
  585. if (entry < efi.memmap.map)
  586. return NULL;
  587. return entry;
  588. }
  589. /*
  590. * efi_map_next_entry - Return the next EFI memory map descriptor
  591. * @entry: Previous EFI memory map descriptor
  592. *
  593. * This is a helper function to iterate over the EFI memory map, which
  594. * we do in different orders depending on the current configuration.
  595. *
  596. * To begin traversing the memory map @entry must be %NULL.
  597. *
  598. * Returns %NULL when we reach the end of the memory map.
  599. */
  600. static void *efi_map_next_entry(void *entry)
  601. {
  602. if (!efi_enabled(EFI_OLD_MEMMAP) && efi_enabled(EFI_64BIT)) {
  603. /*
  604. * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE
  605. * config table feature requires us to map all entries
  606. * in the same order as they appear in the EFI memory
  607. * map. That is to say, entry N must have a lower
  608. * virtual address than entry N+1. This is because the
  609. * firmware toolchain leaves relative references in
  610. * the code/data sections, which are split and become
  611. * separate EFI memory regions. Mapping things
  612. * out-of-order leads to the firmware accessing
  613. * unmapped addresses.
  614. *
  615. * Since we need to map things this way whether or not
  616. * the kernel actually makes use of
  617. * EFI_PROPERTIES_TABLE, let's just switch to this
  618. * scheme by default for 64-bit.
  619. */
  620. return efi_map_next_entry_reverse(entry);
  621. }
  622. /* Initial call */
  623. if (!entry)
  624. return efi.memmap.map;
  625. entry += efi.memmap.desc_size;
  626. if (entry >= efi.memmap.map_end)
  627. return NULL;
  628. return entry;
  629. }
  630. static bool should_map_region(efi_memory_desc_t *md)
  631. {
  632. /*
  633. * Runtime regions always require runtime mappings (obviously).
  634. */
  635. if (md->attribute & EFI_MEMORY_RUNTIME)
  636. return true;
  637. /*
  638. * 32-bit EFI doesn't suffer from the bug that requires us to
  639. * reserve boot services regions, and mixed mode support
  640. * doesn't exist for 32-bit kernels.
  641. */
  642. if (IS_ENABLED(CONFIG_X86_32))
  643. return false;
  644. /*
  645. * Map all of RAM so that we can access arguments in the 1:1
  646. * mapping when making EFI runtime calls.
  647. */
  648. if (IS_ENABLED(CONFIG_EFI_MIXED) && !efi_is_native()) {
  649. if (md->type == EFI_CONVENTIONAL_MEMORY ||
  650. md->type == EFI_LOADER_DATA ||
  651. md->type == EFI_LOADER_CODE)
  652. return true;
  653. }
  654. /*
  655. * Map boot services regions as a workaround for buggy
  656. * firmware that accesses them even when they shouldn't.
  657. *
  658. * See efi_{reserve,free}_boot_services().
  659. */
  660. if (md->type == EFI_BOOT_SERVICES_CODE ||
  661. md->type == EFI_BOOT_SERVICES_DATA)
  662. return true;
  663. return false;
  664. }
  665. /*
  666. * Map the efi memory ranges of the runtime services and update new_mmap with
  667. * virtual addresses.
  668. */
  669. static void * __init efi_map_regions(int *count, int *pg_shift)
  670. {
  671. void *p, *new_memmap = NULL;
  672. unsigned long left = 0;
  673. unsigned long desc_size;
  674. efi_memory_desc_t *md;
  675. desc_size = efi.memmap.desc_size;
  676. p = NULL;
  677. while ((p = efi_map_next_entry(p))) {
  678. md = p;
  679. if (!should_map_region(md))
  680. continue;
  681. efi_map_region(md);
  682. get_systab_virt_addr(md);
  683. if (left < desc_size) {
  684. new_memmap = realloc_pages(new_memmap, *pg_shift);
  685. if (!new_memmap)
  686. return NULL;
  687. left += PAGE_SIZE << *pg_shift;
  688. (*pg_shift)++;
  689. }
  690. memcpy(new_memmap + (*count * desc_size), md, desc_size);
  691. left -= desc_size;
  692. (*count)++;
  693. }
  694. return new_memmap;
  695. }
  696. static void __init kexec_enter_virtual_mode(void)
  697. {
  698. #ifdef CONFIG_KEXEC_CORE
  699. efi_memory_desc_t *md;
  700. unsigned int num_pages;
  701. efi.systab = NULL;
  702. /*
  703. * We don't do virtual mode, since we don't do runtime services, on
  704. * non-native EFI. With efi=old_map, we don't do runtime services in
  705. * kexec kernel because in the initial boot something else might
  706. * have been mapped at these virtual addresses.
  707. */
  708. if (!efi_is_native() || efi_enabled(EFI_OLD_MEMMAP)) {
  709. efi_memmap_unmap();
  710. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  711. return;
  712. }
  713. if (efi_alloc_page_tables()) {
  714. pr_err("Failed to allocate EFI page tables\n");
  715. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  716. return;
  717. }
  718. /*
  719. * Map efi regions which were passed via setup_data. The virt_addr is a
  720. * fixed addr which was used in first kernel of a kexec boot.
  721. */
  722. for_each_efi_memory_desc(md) {
  723. efi_map_region_fixed(md); /* FIXME: add error handling */
  724. get_systab_virt_addr(md);
  725. }
  726. /*
  727. * Unregister the early EFI memmap from efi_init() and install
  728. * the new EFI memory map.
  729. */
  730. efi_memmap_unmap();
  731. if (efi_memmap_init_late(efi.memmap.phys_map,
  732. efi.memmap.desc_size * efi.memmap.nr_map)) {
  733. pr_err("Failed to remap late EFI memory map\n");
  734. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  735. return;
  736. }
  737. BUG_ON(!efi.systab);
  738. num_pages = ALIGN(efi.memmap.nr_map * efi.memmap.desc_size, PAGE_SIZE);
  739. num_pages >>= PAGE_SHIFT;
  740. if (efi_setup_page_tables(efi.memmap.phys_map, num_pages)) {
  741. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  742. return;
  743. }
  744. efi_sync_low_kernel_mappings();
  745. /*
  746. * Now that EFI is in virtual mode, update the function
  747. * pointers in the runtime service table to the new virtual addresses.
  748. *
  749. * Call EFI services through wrapper functions.
  750. */
  751. efi.runtime_version = efi_systab.hdr.revision;
  752. efi_native_runtime_setup();
  753. efi.set_virtual_address_map = NULL;
  754. if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
  755. runtime_code_page_mkexec();
  756. /* clean DUMMY object */
  757. efi_delete_dummy_variable();
  758. #endif
  759. }
  760. /*
  761. * This function will switch the EFI runtime services to virtual mode.
  762. * Essentially, we look through the EFI memmap and map every region that
  763. * has the runtime attribute bit set in its memory descriptor into the
  764. * efi_pgd page table.
  765. *
  766. * The old method which used to update that memory descriptor with the
  767. * virtual address obtained from ioremap() is still supported when the
  768. * kernel is booted with efi=old_map on its command line. Same old
  769. * method enabled the runtime services to be called without having to
  770. * thunk back into physical mode for every invocation.
  771. *
  772. * The new method does a pagetable switch in a preemption-safe manner
  773. * so that we're in a different address space when calling a runtime
  774. * function. For function arguments passing we do copy the PUDs of the
  775. * kernel page table into efi_pgd prior to each call.
  776. *
  777. * Specially for kexec boot, efi runtime maps in previous kernel should
  778. * be passed in via setup_data. In that case runtime ranges will be mapped
  779. * to the same virtual addresses as the first kernel, see
  780. * kexec_enter_virtual_mode().
  781. */
  782. static void __init __efi_enter_virtual_mode(void)
  783. {
  784. int count = 0, pg_shift = 0;
  785. void *new_memmap = NULL;
  786. efi_status_t status;
  787. unsigned long pa;
  788. efi.systab = NULL;
  789. if (efi_alloc_page_tables()) {
  790. pr_err("Failed to allocate EFI page tables\n");
  791. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  792. return;
  793. }
  794. efi_merge_regions();
  795. new_memmap = efi_map_regions(&count, &pg_shift);
  796. if (!new_memmap) {
  797. pr_err("Error reallocating memory, EFI runtime non-functional!\n");
  798. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  799. return;
  800. }
  801. pa = __pa(new_memmap);
  802. /*
  803. * Unregister the early EFI memmap from efi_init() and install
  804. * the new EFI memory map that we are about to pass to the
  805. * firmware via SetVirtualAddressMap().
  806. */
  807. efi_memmap_unmap();
  808. if (efi_memmap_init_late(pa, efi.memmap.desc_size * count)) {
  809. pr_err("Failed to remap late EFI memory map\n");
  810. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  811. return;
  812. }
  813. if (efi_enabled(EFI_DBG)) {
  814. pr_info("EFI runtime memory map:\n");
  815. efi_print_memmap();
  816. }
  817. BUG_ON(!efi.systab);
  818. if (efi_setup_page_tables(pa, 1 << pg_shift)) {
  819. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  820. return;
  821. }
  822. efi_sync_low_kernel_mappings();
  823. if (efi_is_native()) {
  824. status = phys_efi_set_virtual_address_map(
  825. efi.memmap.desc_size * count,
  826. efi.memmap.desc_size,
  827. efi.memmap.desc_version,
  828. (efi_memory_desc_t *)pa);
  829. } else {
  830. status = efi_thunk_set_virtual_address_map(
  831. efi_phys.set_virtual_address_map,
  832. efi.memmap.desc_size * count,
  833. efi.memmap.desc_size,
  834. efi.memmap.desc_version,
  835. (efi_memory_desc_t *)pa);
  836. }
  837. if (status != EFI_SUCCESS) {
  838. pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
  839. status);
  840. panic("EFI call to SetVirtualAddressMap() failed!");
  841. }
  842. /*
  843. * Now that EFI is in virtual mode, update the function
  844. * pointers in the runtime service table to the new virtual addresses.
  845. *
  846. * Call EFI services through wrapper functions.
  847. */
  848. efi.runtime_version = efi_systab.hdr.revision;
  849. if (efi_is_native())
  850. efi_native_runtime_setup();
  851. else
  852. efi_thunk_runtime_setup();
  853. efi.set_virtual_address_map = NULL;
  854. /*
  855. * Apply more restrictive page table mapping attributes now that
  856. * SVAM() has been called and the firmware has performed all
  857. * necessary relocation fixups for the new virtual addresses.
  858. */
  859. efi_runtime_update_mappings();
  860. /* clean DUMMY object */
  861. efi_delete_dummy_variable();
  862. }
  863. void __init efi_enter_virtual_mode(void)
  864. {
  865. if (efi_enabled(EFI_PARAVIRT))
  866. return;
  867. if (efi_setup)
  868. kexec_enter_virtual_mode();
  869. else
  870. __efi_enter_virtual_mode();
  871. efi_dump_pagetable();
  872. }
  873. static int __init arch_parse_efi_cmdline(char *str)
  874. {
  875. if (!str) {
  876. pr_warn("need at least one option\n");
  877. return -EINVAL;
  878. }
  879. if (parse_option_str(str, "old_map"))
  880. set_bit(EFI_OLD_MEMMAP, &efi.flags);
  881. return 0;
  882. }
  883. early_param("efi", arch_parse_efi_cmdline);