efi.c 26 KB

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