efi.c 25 KB

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