efi.c 27 KB

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