efi.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. /*
  2. * Common EFI (Extensible Firmware Interface) support functions
  3. * Based on Extensible Firmware Interface Specification version 1.0
  4. *
  5. * Copyright (C) 1999 VA Linux Systems
  6. * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
  7. * Copyright (C) 1999-2002 Hewlett-Packard Co.
  8. * David Mosberger-Tang <davidm@hpl.hp.com>
  9. * Stephane Eranian <eranian@hpl.hp.com>
  10. * Copyright (C) 2005-2008 Intel Co.
  11. * Fenghua Yu <fenghua.yu@intel.com>
  12. * Bibo Mao <bibo.mao@intel.com>
  13. * Chandramouli Narayanan <mouli@linux.intel.com>
  14. * Huang Ying <ying.huang@intel.com>
  15. * Copyright (C) 2013 SuSE Labs
  16. * Borislav Petkov <bp@suse.de> - runtime services VA mapping
  17. *
  18. * Copied from efi_32.c to eliminate the duplicated code between EFI
  19. * 32/64 support code. --ying 2007-10-26
  20. *
  21. * All EFI Runtime Services are not implemented yet as EFI only
  22. * supports physical mode addressing on SoftSDV. This is to be fixed
  23. * in a future version. --drummond 1999-07-20
  24. *
  25. * Implemented EFI runtime services and virtual mode calls. --davidm
  26. *
  27. * Goutham Rao: <goutham.rao@intel.com>
  28. * Skip non-WB memory and ignore empty memory ranges.
  29. */
  30. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  31. #include <linux/kernel.h>
  32. #include <linux/init.h>
  33. #include <linux/efi.h>
  34. #include <linux/efi-bgrt.h>
  35. #include <linux/export.h>
  36. #include <linux/bootmem.h>
  37. #include <linux/slab.h>
  38. #include <linux/memblock.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/cacheflush.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_RAM;
  125. else
  126. e820_type = E820_RESERVED;
  127. break;
  128. case EFI_ACPI_RECLAIM_MEMORY:
  129. e820_type = E820_ACPI;
  130. break;
  131. case EFI_ACPI_MEMORY_NVS:
  132. e820_type = E820_NVS;
  133. break;
  134. case EFI_UNUSABLE_MEMORY:
  135. e820_type = E820_UNUSABLE;
  136. break;
  137. case EFI_PERSISTENT_MEMORY:
  138. e820_type = E820_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_RESERVED;
  147. break;
  148. }
  149. e820_add_region(start, size, e820_type);
  150. }
  151. sanitize_e820_map(e820->map, ARRAY_SIZE(e820->map), &e820->nr_map);
  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_late_init(void)
  470. {
  471. efi_bgrt_init();
  472. }
  473. void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
  474. {
  475. u64 addr, npages;
  476. addr = md->virt_addr;
  477. npages = md->num_pages;
  478. memrange_efi_to_native(&addr, &npages);
  479. if (executable)
  480. set_memory_x(addr, npages);
  481. else
  482. set_memory_nx(addr, npages);
  483. }
  484. void __init runtime_code_page_mkexec(void)
  485. {
  486. efi_memory_desc_t *md;
  487. /* Make EFI runtime service code area executable */
  488. for_each_efi_memory_desc(md) {
  489. if (md->type != EFI_RUNTIME_SERVICES_CODE)
  490. continue;
  491. efi_set_executable(md, true);
  492. }
  493. }
  494. void __init efi_memory_uc(u64 addr, unsigned long size)
  495. {
  496. unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
  497. u64 npages;
  498. npages = round_up(size, page_shift) / page_shift;
  499. memrange_efi_to_native(&addr, &npages);
  500. set_memory_uc(addr, npages);
  501. }
  502. void __init old_map_region(efi_memory_desc_t *md)
  503. {
  504. u64 start_pfn, end_pfn, end;
  505. unsigned long size;
  506. void *va;
  507. start_pfn = PFN_DOWN(md->phys_addr);
  508. size = md->num_pages << PAGE_SHIFT;
  509. end = md->phys_addr + size;
  510. end_pfn = PFN_UP(end);
  511. if (pfn_range_is_mapped(start_pfn, end_pfn)) {
  512. va = __va(md->phys_addr);
  513. if (!(md->attribute & EFI_MEMORY_WB))
  514. efi_memory_uc((u64)(unsigned long)va, size);
  515. } else
  516. va = efi_ioremap(md->phys_addr, size,
  517. md->type, md->attribute);
  518. md->virt_addr = (u64) (unsigned long) va;
  519. if (!va)
  520. pr_err("ioremap of 0x%llX failed!\n",
  521. (unsigned long long)md->phys_addr);
  522. }
  523. /* Merge contiguous regions of the same type and attribute */
  524. static void __init efi_merge_regions(void)
  525. {
  526. efi_memory_desc_t *md, *prev_md = NULL;
  527. for_each_efi_memory_desc(md) {
  528. u64 prev_size;
  529. if (!prev_md) {
  530. prev_md = md;
  531. continue;
  532. }
  533. if (prev_md->type != md->type ||
  534. prev_md->attribute != md->attribute) {
  535. prev_md = md;
  536. continue;
  537. }
  538. prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
  539. if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
  540. prev_md->num_pages += md->num_pages;
  541. md->type = EFI_RESERVED_TYPE;
  542. md->attribute = 0;
  543. continue;
  544. }
  545. prev_md = md;
  546. }
  547. }
  548. static void __init get_systab_virt_addr(efi_memory_desc_t *md)
  549. {
  550. unsigned long size;
  551. u64 end, systab;
  552. size = md->num_pages << EFI_PAGE_SHIFT;
  553. end = md->phys_addr + size;
  554. systab = (u64)(unsigned long)efi_phys.systab;
  555. if (md->phys_addr <= systab && systab < end) {
  556. systab += md->virt_addr - md->phys_addr;
  557. efi.systab = (efi_system_table_t *)(unsigned long)systab;
  558. }
  559. }
  560. static void *realloc_pages(void *old_memmap, int old_shift)
  561. {
  562. void *ret;
  563. ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
  564. if (!ret)
  565. goto out;
  566. /*
  567. * A first-time allocation doesn't have anything to copy.
  568. */
  569. if (!old_memmap)
  570. return ret;
  571. memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
  572. out:
  573. free_pages((unsigned long)old_memmap, old_shift);
  574. return ret;
  575. }
  576. /*
  577. * Iterate the EFI memory map in reverse order because the regions
  578. * will be mapped top-down. The end result is the same as if we had
  579. * mapped things forward, but doesn't require us to change the
  580. * existing implementation of efi_map_region().
  581. */
  582. static inline void *efi_map_next_entry_reverse(void *entry)
  583. {
  584. /* Initial call */
  585. if (!entry)
  586. return efi.memmap.map_end - efi.memmap.desc_size;
  587. entry -= efi.memmap.desc_size;
  588. if (entry < efi.memmap.map)
  589. return NULL;
  590. return entry;
  591. }
  592. /*
  593. * efi_map_next_entry - Return the next EFI memory map descriptor
  594. * @entry: Previous EFI memory map descriptor
  595. *
  596. * This is a helper function to iterate over the EFI memory map, which
  597. * we do in different orders depending on the current configuration.
  598. *
  599. * To begin traversing the memory map @entry must be %NULL.
  600. *
  601. * Returns %NULL when we reach the end of the memory map.
  602. */
  603. static void *efi_map_next_entry(void *entry)
  604. {
  605. if (!efi_enabled(EFI_OLD_MEMMAP) && efi_enabled(EFI_64BIT)) {
  606. /*
  607. * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE
  608. * config table feature requires us to map all entries
  609. * in the same order as they appear in the EFI memory
  610. * map. That is to say, entry N must have a lower
  611. * virtual address than entry N+1. This is because the
  612. * firmware toolchain leaves relative references in
  613. * the code/data sections, which are split and become
  614. * separate EFI memory regions. Mapping things
  615. * out-of-order leads to the firmware accessing
  616. * unmapped addresses.
  617. *
  618. * Since we need to map things this way whether or not
  619. * the kernel actually makes use of
  620. * EFI_PROPERTIES_TABLE, let's just switch to this
  621. * scheme by default for 64-bit.
  622. */
  623. return efi_map_next_entry_reverse(entry);
  624. }
  625. /* Initial call */
  626. if (!entry)
  627. return efi.memmap.map;
  628. entry += efi.memmap.desc_size;
  629. if (entry >= efi.memmap.map_end)
  630. return NULL;
  631. return entry;
  632. }
  633. static bool should_map_region(efi_memory_desc_t *md)
  634. {
  635. /*
  636. * Runtime regions always require runtime mappings (obviously).
  637. */
  638. if (md->attribute & EFI_MEMORY_RUNTIME)
  639. return true;
  640. /*
  641. * 32-bit EFI doesn't suffer from the bug that requires us to
  642. * reserve boot services regions, and mixed mode support
  643. * doesn't exist for 32-bit kernels.
  644. */
  645. if (IS_ENABLED(CONFIG_X86_32))
  646. return false;
  647. /*
  648. * Map all of RAM so that we can access arguments in the 1:1
  649. * mapping when making EFI runtime calls.
  650. */
  651. if (IS_ENABLED(CONFIG_EFI_MIXED) && !efi_is_native()) {
  652. if (md->type == EFI_CONVENTIONAL_MEMORY ||
  653. md->type == EFI_LOADER_DATA ||
  654. md->type == EFI_LOADER_CODE)
  655. return true;
  656. }
  657. /*
  658. * Map boot services regions as a workaround for buggy
  659. * firmware that accesses them even when they shouldn't.
  660. *
  661. * See efi_{reserve,free}_boot_services().
  662. */
  663. if (md->type == EFI_BOOT_SERVICES_CODE ||
  664. md->type == EFI_BOOT_SERVICES_DATA)
  665. return true;
  666. return false;
  667. }
  668. /*
  669. * Map the efi memory ranges of the runtime services and update new_mmap with
  670. * virtual addresses.
  671. */
  672. static void * __init efi_map_regions(int *count, int *pg_shift)
  673. {
  674. void *p, *new_memmap = NULL;
  675. unsigned long left = 0;
  676. unsigned long desc_size;
  677. efi_memory_desc_t *md;
  678. desc_size = efi.memmap.desc_size;
  679. p = NULL;
  680. while ((p = efi_map_next_entry(p))) {
  681. md = p;
  682. if (!should_map_region(md))
  683. continue;
  684. efi_map_region(md);
  685. get_systab_virt_addr(md);
  686. if (left < desc_size) {
  687. new_memmap = realloc_pages(new_memmap, *pg_shift);
  688. if (!new_memmap)
  689. return NULL;
  690. left += PAGE_SIZE << *pg_shift;
  691. (*pg_shift)++;
  692. }
  693. memcpy(new_memmap + (*count * desc_size), md, desc_size);
  694. left -= desc_size;
  695. (*count)++;
  696. }
  697. return new_memmap;
  698. }
  699. static void __init kexec_enter_virtual_mode(void)
  700. {
  701. #ifdef CONFIG_KEXEC_CORE
  702. efi_memory_desc_t *md;
  703. unsigned int num_pages;
  704. efi.systab = NULL;
  705. /*
  706. * We don't do virtual mode, since we don't do runtime services, on
  707. * non-native EFI
  708. */
  709. if (!efi_is_native()) {
  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. /* clean DUMMY object */
  758. efi_delete_dummy_variable();
  759. #endif
  760. }
  761. /*
  762. * This function will switch the EFI runtime services to virtual mode.
  763. * Essentially, we look through the EFI memmap and map every region that
  764. * has the runtime attribute bit set in its memory descriptor into the
  765. * efi_pgd page table.
  766. *
  767. * The old method which used to update that memory descriptor with the
  768. * virtual address obtained from ioremap() is still supported when the
  769. * kernel is booted with efi=old_map on its command line. Same old
  770. * method enabled the runtime services to be called without having to
  771. * thunk back into physical mode for every invocation.
  772. *
  773. * The new method does a pagetable switch in a preemption-safe manner
  774. * so that we're in a different address space when calling a runtime
  775. * function. For function arguments passing we do copy the PUDs of the
  776. * kernel page table into efi_pgd prior to each call.
  777. *
  778. * Specially for kexec boot, efi runtime maps in previous kernel should
  779. * be passed in via setup_data. In that case runtime ranges will be mapped
  780. * to the same virtual addresses as the first kernel, see
  781. * kexec_enter_virtual_mode().
  782. */
  783. static void __init __efi_enter_virtual_mode(void)
  784. {
  785. int count = 0, pg_shift = 0;
  786. void *new_memmap = NULL;
  787. efi_status_t status;
  788. unsigned long pa;
  789. efi.systab = NULL;
  790. if (efi_alloc_page_tables()) {
  791. pr_err("Failed to allocate EFI page tables\n");
  792. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  793. return;
  794. }
  795. efi_merge_regions();
  796. new_memmap = efi_map_regions(&count, &pg_shift);
  797. if (!new_memmap) {
  798. pr_err("Error reallocating memory, EFI runtime non-functional!\n");
  799. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  800. return;
  801. }
  802. pa = __pa(new_memmap);
  803. /*
  804. * Unregister the early EFI memmap from efi_init() and install
  805. * the new EFI memory map that we are about to pass to the
  806. * firmware via SetVirtualAddressMap().
  807. */
  808. efi_memmap_unmap();
  809. if (efi_memmap_init_late(pa, efi.memmap.desc_size * count)) {
  810. pr_err("Failed to remap late EFI memory map\n");
  811. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  812. return;
  813. }
  814. BUG_ON(!efi.systab);
  815. if (efi_setup_page_tables(pa, 1 << pg_shift)) {
  816. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  817. return;
  818. }
  819. efi_sync_low_kernel_mappings();
  820. if (efi_is_native()) {
  821. status = phys_efi_set_virtual_address_map(
  822. efi.memmap.desc_size * count,
  823. efi.memmap.desc_size,
  824. efi.memmap.desc_version,
  825. (efi_memory_desc_t *)pa);
  826. } else {
  827. status = efi_thunk_set_virtual_address_map(
  828. efi_phys.set_virtual_address_map,
  829. efi.memmap.desc_size * count,
  830. efi.memmap.desc_size,
  831. efi.memmap.desc_version,
  832. (efi_memory_desc_t *)pa);
  833. }
  834. if (status != EFI_SUCCESS) {
  835. pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
  836. status);
  837. panic("EFI call to SetVirtualAddressMap() failed!");
  838. }
  839. /*
  840. * Now that EFI is in virtual mode, update the function
  841. * pointers in the runtime service table to the new virtual addresses.
  842. *
  843. * Call EFI services through wrapper functions.
  844. */
  845. efi.runtime_version = efi_systab.hdr.revision;
  846. if (efi_is_native())
  847. efi_native_runtime_setup();
  848. else
  849. efi_thunk_runtime_setup();
  850. efi.set_virtual_address_map = NULL;
  851. /*
  852. * Apply more restrictive page table mapping attributes now that
  853. * SVAM() has been called and the firmware has performed all
  854. * necessary relocation fixups for the new virtual addresses.
  855. */
  856. efi_runtime_update_mappings();
  857. efi_dump_pagetable();
  858. /* clean DUMMY object */
  859. efi_delete_dummy_variable();
  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. }
  870. /*
  871. * Convenience functions to obtain memory types and attributes
  872. */
  873. u32 efi_mem_type(unsigned long phys_addr)
  874. {
  875. efi_memory_desc_t *md;
  876. if (!efi_enabled(EFI_MEMMAP))
  877. return 0;
  878. for_each_efi_memory_desc(md) {
  879. if ((md->phys_addr <= phys_addr) &&
  880. (phys_addr < (md->phys_addr +
  881. (md->num_pages << EFI_PAGE_SHIFT))))
  882. return md->type;
  883. }
  884. return 0;
  885. }
  886. static int __init arch_parse_efi_cmdline(char *str)
  887. {
  888. if (!str) {
  889. pr_warn("need at least one option\n");
  890. return -EINVAL;
  891. }
  892. if (parse_option_str(str, "old_map"))
  893. set_bit(EFI_OLD_MEMMAP, &efi.flags);
  894. return 0;
  895. }
  896. early_param("efi", arch_parse_efi_cmdline);