efi.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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. #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 = early_memremap_ro(efi.systab->fw_vendor,
  439. sizeof(vendor) * sizeof(efi_char16_t));
  440. if (c16) {
  441. for (i = 0; i < sizeof(vendor) - 1 && c16[i]; ++i)
  442. vendor[i] = c16[i];
  443. vendor[i] = '\0';
  444. early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t));
  445. } else {
  446. pr_err("Could not map the firmware vendor!\n");
  447. }
  448. pr_info("EFI v%u.%.02u by %s\n",
  449. efi.systab->hdr.revision >> 16,
  450. efi.systab->hdr.revision & 0xffff, vendor);
  451. if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
  452. return;
  453. if (efi_config_init(arch_tables))
  454. return;
  455. /*
  456. * Note: We currently don't support runtime services on an EFI
  457. * that doesn't match the kernel 32/64-bit mode.
  458. */
  459. if (!efi_runtime_supported())
  460. pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
  461. else {
  462. if (efi_runtime_disabled() || efi_runtime_init()) {
  463. efi_memmap_unmap();
  464. return;
  465. }
  466. }
  467. efi_clean_memmap();
  468. if (efi_enabled(EFI_DBG))
  469. efi_print_memmap();
  470. }
  471. void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
  472. {
  473. u64 addr, npages;
  474. addr = md->virt_addr;
  475. npages = md->num_pages;
  476. memrange_efi_to_native(&addr, &npages);
  477. if (executable)
  478. set_memory_x(addr, npages);
  479. else
  480. set_memory_nx(addr, npages);
  481. }
  482. void __init runtime_code_page_mkexec(void)
  483. {
  484. efi_memory_desc_t *md;
  485. /* Make EFI runtime service code area executable */
  486. for_each_efi_memory_desc(md) {
  487. if (md->type != EFI_RUNTIME_SERVICES_CODE)
  488. continue;
  489. efi_set_executable(md, true);
  490. }
  491. }
  492. void __init efi_memory_uc(u64 addr, unsigned long size)
  493. {
  494. unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
  495. u64 npages;
  496. npages = round_up(size, page_shift) / page_shift;
  497. memrange_efi_to_native(&addr, &npages);
  498. set_memory_uc(addr, npages);
  499. }
  500. void __init old_map_region(efi_memory_desc_t *md)
  501. {
  502. u64 start_pfn, end_pfn, end;
  503. unsigned long size;
  504. void *va;
  505. start_pfn = PFN_DOWN(md->phys_addr);
  506. size = md->num_pages << PAGE_SHIFT;
  507. end = md->phys_addr + size;
  508. end_pfn = PFN_UP(end);
  509. if (pfn_range_is_mapped(start_pfn, end_pfn)) {
  510. va = __va(md->phys_addr);
  511. if (!(md->attribute & EFI_MEMORY_WB))
  512. efi_memory_uc((u64)(unsigned long)va, size);
  513. } else
  514. va = efi_ioremap(md->phys_addr, size,
  515. md->type, md->attribute);
  516. md->virt_addr = (u64) (unsigned long) va;
  517. if (!va)
  518. pr_err("ioremap of 0x%llX failed!\n",
  519. (unsigned long long)md->phys_addr);
  520. }
  521. /* Merge contiguous regions of the same type and attribute */
  522. static void __init efi_merge_regions(void)
  523. {
  524. efi_memory_desc_t *md, *prev_md = NULL;
  525. for_each_efi_memory_desc(md) {
  526. u64 prev_size;
  527. if (!prev_md) {
  528. prev_md = md;
  529. continue;
  530. }
  531. if (prev_md->type != md->type ||
  532. prev_md->attribute != md->attribute) {
  533. prev_md = md;
  534. continue;
  535. }
  536. prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
  537. if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
  538. prev_md->num_pages += md->num_pages;
  539. md->type = EFI_RESERVED_TYPE;
  540. md->attribute = 0;
  541. continue;
  542. }
  543. prev_md = md;
  544. }
  545. }
  546. static void __init get_systab_virt_addr(efi_memory_desc_t *md)
  547. {
  548. unsigned long size;
  549. u64 end, systab;
  550. size = md->num_pages << EFI_PAGE_SHIFT;
  551. end = md->phys_addr + size;
  552. systab = (u64)(unsigned long)efi_phys.systab;
  553. if (md->phys_addr <= systab && systab < end) {
  554. systab += md->virt_addr - md->phys_addr;
  555. efi.systab = (efi_system_table_t *)(unsigned long)systab;
  556. }
  557. }
  558. static void *realloc_pages(void *old_memmap, int old_shift)
  559. {
  560. void *ret;
  561. ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
  562. if (!ret)
  563. goto out;
  564. /*
  565. * A first-time allocation doesn't have anything to copy.
  566. */
  567. if (!old_memmap)
  568. return ret;
  569. memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
  570. out:
  571. free_pages((unsigned long)old_memmap, old_shift);
  572. return ret;
  573. }
  574. /*
  575. * Iterate the EFI memory map in reverse order because the regions
  576. * will be mapped top-down. The end result is the same as if we had
  577. * mapped things forward, but doesn't require us to change the
  578. * existing implementation of efi_map_region().
  579. */
  580. static inline void *efi_map_next_entry_reverse(void *entry)
  581. {
  582. /* Initial call */
  583. if (!entry)
  584. return efi.memmap.map_end - efi.memmap.desc_size;
  585. entry -= efi.memmap.desc_size;
  586. if (entry < efi.memmap.map)
  587. return NULL;
  588. return entry;
  589. }
  590. /*
  591. * efi_map_next_entry - Return the next EFI memory map descriptor
  592. * @entry: Previous EFI memory map descriptor
  593. *
  594. * This is a helper function to iterate over the EFI memory map, which
  595. * we do in different orders depending on the current configuration.
  596. *
  597. * To begin traversing the memory map @entry must be %NULL.
  598. *
  599. * Returns %NULL when we reach the end of the memory map.
  600. */
  601. static void *efi_map_next_entry(void *entry)
  602. {
  603. if (!efi_enabled(EFI_OLD_MEMMAP) && efi_enabled(EFI_64BIT)) {
  604. /*
  605. * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE
  606. * config table feature requires us to map all entries
  607. * in the same order as they appear in the EFI memory
  608. * map. That is to say, entry N must have a lower
  609. * virtual address than entry N+1. This is because the
  610. * firmware toolchain leaves relative references in
  611. * the code/data sections, which are split and become
  612. * separate EFI memory regions. Mapping things
  613. * out-of-order leads to the firmware accessing
  614. * unmapped addresses.
  615. *
  616. * Since we need to map things this way whether or not
  617. * the kernel actually makes use of
  618. * EFI_PROPERTIES_TABLE, let's just switch to this
  619. * scheme by default for 64-bit.
  620. */
  621. return efi_map_next_entry_reverse(entry);
  622. }
  623. /* Initial call */
  624. if (!entry)
  625. return efi.memmap.map;
  626. entry += efi.memmap.desc_size;
  627. if (entry >= efi.memmap.map_end)
  628. return NULL;
  629. return entry;
  630. }
  631. static bool should_map_region(efi_memory_desc_t *md)
  632. {
  633. /*
  634. * Runtime regions always require runtime mappings (obviously).
  635. */
  636. if (md->attribute & EFI_MEMORY_RUNTIME)
  637. return true;
  638. /*
  639. * 32-bit EFI doesn't suffer from the bug that requires us to
  640. * reserve boot services regions, and mixed mode support
  641. * doesn't exist for 32-bit kernels.
  642. */
  643. if (IS_ENABLED(CONFIG_X86_32))
  644. return false;
  645. /*
  646. * Map all of RAM so that we can access arguments in the 1:1
  647. * mapping when making EFI runtime calls.
  648. */
  649. if (IS_ENABLED(CONFIG_EFI_MIXED) && !efi_is_native()) {
  650. if (md->type == EFI_CONVENTIONAL_MEMORY ||
  651. md->type == EFI_LOADER_DATA ||
  652. md->type == EFI_LOADER_CODE)
  653. return true;
  654. }
  655. /*
  656. * Map boot services regions as a workaround for buggy
  657. * firmware that accesses them even when they shouldn't.
  658. *
  659. * See efi_{reserve,free}_boot_services().
  660. */
  661. if (md->type == EFI_BOOT_SERVICES_CODE ||
  662. md->type == EFI_BOOT_SERVICES_DATA)
  663. return true;
  664. return false;
  665. }
  666. /*
  667. * Map the efi memory ranges of the runtime services and update new_mmap with
  668. * virtual addresses.
  669. */
  670. static void * __init efi_map_regions(int *count, int *pg_shift)
  671. {
  672. void *p, *new_memmap = NULL;
  673. unsigned long left = 0;
  674. unsigned long desc_size;
  675. efi_memory_desc_t *md;
  676. desc_size = efi.memmap.desc_size;
  677. p = NULL;
  678. while ((p = efi_map_next_entry(p))) {
  679. md = p;
  680. if (!should_map_region(md))
  681. continue;
  682. efi_map_region(md);
  683. get_systab_virt_addr(md);
  684. if (left < desc_size) {
  685. new_memmap = realloc_pages(new_memmap, *pg_shift);
  686. if (!new_memmap)
  687. return NULL;
  688. left += PAGE_SIZE << *pg_shift;
  689. (*pg_shift)++;
  690. }
  691. memcpy(new_memmap + (*count * desc_size), md, desc_size);
  692. left -= desc_size;
  693. (*count)++;
  694. }
  695. return new_memmap;
  696. }
  697. static void __init kexec_enter_virtual_mode(void)
  698. {
  699. #ifdef CONFIG_KEXEC_CORE
  700. efi_memory_desc_t *md;
  701. unsigned int num_pages;
  702. efi.systab = NULL;
  703. /*
  704. * We don't do virtual mode, since we don't do runtime services, on
  705. * non-native EFI. With efi=old_map, we don't do runtime services in
  706. * kexec kernel because in the initial boot something else might
  707. * have been mapped at these virtual addresses.
  708. */
  709. if (!efi_is_native() || efi_enabled(EFI_OLD_MEMMAP)) {
  710. efi_memmap_unmap();
  711. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  712. return;
  713. }
  714. if (efi_alloc_page_tables()) {
  715. pr_err("Failed to allocate EFI page tables\n");
  716. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  717. return;
  718. }
  719. /*
  720. * Map efi regions which were passed via setup_data. The virt_addr is a
  721. * fixed addr which was used in first kernel of a kexec boot.
  722. */
  723. for_each_efi_memory_desc(md) {
  724. efi_map_region_fixed(md); /* FIXME: add error handling */
  725. get_systab_virt_addr(md);
  726. }
  727. /*
  728. * Unregister the early EFI memmap from efi_init() and install
  729. * the new EFI memory map.
  730. */
  731. efi_memmap_unmap();
  732. if (efi_memmap_init_late(efi.memmap.phys_map,
  733. efi.memmap.desc_size * efi.memmap.nr_map)) {
  734. pr_err("Failed to remap late EFI memory map\n");
  735. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  736. return;
  737. }
  738. BUG_ON(!efi.systab);
  739. num_pages = ALIGN(efi.memmap.nr_map * efi.memmap.desc_size, PAGE_SIZE);
  740. num_pages >>= PAGE_SHIFT;
  741. if (efi_setup_page_tables(efi.memmap.phys_map, num_pages)) {
  742. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  743. return;
  744. }
  745. efi_sync_low_kernel_mappings();
  746. /*
  747. * Now that EFI is in virtual mode, update the function
  748. * pointers in the runtime service table to the new virtual addresses.
  749. *
  750. * Call EFI services through wrapper functions.
  751. */
  752. efi.runtime_version = efi_systab.hdr.revision;
  753. efi_native_runtime_setup();
  754. efi.set_virtual_address_map = NULL;
  755. if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
  756. runtime_code_page_mkexec();
  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. goto err;
  791. }
  792. efi_merge_regions();
  793. new_memmap = efi_map_regions(&count, &pg_shift);
  794. if (!new_memmap) {
  795. pr_err("Error reallocating memory, EFI runtime non-functional!\n");
  796. goto err;
  797. }
  798. pa = __pa(new_memmap);
  799. /*
  800. * Unregister the early EFI memmap from efi_init() and install
  801. * the new EFI memory map that we are about to pass to the
  802. * firmware via SetVirtualAddressMap().
  803. */
  804. efi_memmap_unmap();
  805. if (efi_memmap_init_late(pa, efi.memmap.desc_size * count)) {
  806. pr_err("Failed to remap late EFI memory map\n");
  807. goto err;
  808. }
  809. if (efi_enabled(EFI_DBG)) {
  810. pr_info("EFI runtime memory map:\n");
  811. efi_print_memmap();
  812. }
  813. if (WARN_ON(!efi.systab))
  814. goto err;
  815. if (efi_setup_page_tables(pa, 1 << pg_shift))
  816. goto err;
  817. efi_sync_low_kernel_mappings();
  818. if (efi_is_native()) {
  819. status = phys_efi_set_virtual_address_map(
  820. efi.memmap.desc_size * count,
  821. efi.memmap.desc_size,
  822. efi.memmap.desc_version,
  823. (efi_memory_desc_t *)pa);
  824. } else {
  825. status = efi_thunk_set_virtual_address_map(
  826. efi_phys.set_virtual_address_map,
  827. efi.memmap.desc_size * count,
  828. efi.memmap.desc_size,
  829. efi.memmap.desc_version,
  830. (efi_memory_desc_t *)pa);
  831. }
  832. if (status != EFI_SUCCESS) {
  833. pr_err("Unable to switch EFI into virtual mode (status=%lx)!\n",
  834. status);
  835. goto err;
  836. }
  837. /*
  838. * Now that EFI is in virtual mode, update the function
  839. * pointers in the runtime service table to the new virtual addresses.
  840. *
  841. * Call EFI services through wrapper functions.
  842. */
  843. efi.runtime_version = efi_systab.hdr.revision;
  844. if (efi_is_native())
  845. efi_native_runtime_setup();
  846. else
  847. efi_thunk_runtime_setup();
  848. efi.set_virtual_address_map = NULL;
  849. /*
  850. * Apply more restrictive page table mapping attributes now that
  851. * SVAM() has been called and the firmware has performed all
  852. * necessary relocation fixups for the new virtual addresses.
  853. */
  854. efi_runtime_update_mappings();
  855. /* clean DUMMY object */
  856. efi_delete_dummy_variable();
  857. return;
  858. err:
  859. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  860. }
  861. void __init efi_enter_virtual_mode(void)
  862. {
  863. if (efi_enabled(EFI_PARAVIRT))
  864. return;
  865. if (efi_setup)
  866. kexec_enter_virtual_mode();
  867. else
  868. __efi_enter_virtual_mode();
  869. efi_dump_pagetable();
  870. }
  871. static int __init arch_parse_efi_cmdline(char *str)
  872. {
  873. if (!str) {
  874. pr_warn("need at least one option\n");
  875. return -EINVAL;
  876. }
  877. if (parse_option_str(str, "old_map"))
  878. set_bit(EFI_OLD_MEMMAP, &efi.flags);
  879. return 0;
  880. }
  881. early_param("efi", arch_parse_efi_cmdline);