efi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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/rtc.h>
  52. #include <asm/uv/uv.h>
  53. #define EFI_DEBUG
  54. struct efi_memory_map memmap;
  55. static struct efi efi_phys __initdata;
  56. static efi_system_table_t efi_systab __initdata;
  57. static efi_config_table_type_t arch_tables[] __initdata = {
  58. #ifdef CONFIG_X86_UV
  59. {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
  60. #endif
  61. {NULL_GUID, NULL, NULL},
  62. };
  63. u64 efi_setup; /* efi setup_data physical address */
  64. static int add_efi_memmap __initdata;
  65. static int __init setup_add_efi_memmap(char *arg)
  66. {
  67. add_efi_memmap = 1;
  68. return 0;
  69. }
  70. early_param("add_efi_memmap", setup_add_efi_memmap);
  71. static efi_status_t __init phys_efi_set_virtual_address_map(
  72. unsigned long memory_map_size,
  73. unsigned long descriptor_size,
  74. u32 descriptor_version,
  75. efi_memory_desc_t *virtual_map)
  76. {
  77. efi_status_t status;
  78. efi_call_phys_prolog();
  79. status = efi_call_phys(efi_phys.set_virtual_address_map,
  80. memory_map_size, descriptor_size,
  81. descriptor_version, virtual_map);
  82. efi_call_phys_epilog();
  83. return status;
  84. }
  85. void efi_get_time(struct timespec *now)
  86. {
  87. efi_status_t status;
  88. efi_time_t eft;
  89. efi_time_cap_t cap;
  90. status = efi.get_time(&eft, &cap);
  91. if (status != EFI_SUCCESS)
  92. pr_err("Oops: efitime: can't read time!\n");
  93. now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
  94. eft.minute, eft.second);
  95. now->tv_nsec = 0;
  96. }
  97. /*
  98. * Tell the kernel about the EFI memory map. This might include
  99. * more than the max 128 entries that can fit in the e820 legacy
  100. * (zeropage) memory map.
  101. */
  102. static void __init do_add_efi_memmap(void)
  103. {
  104. void *p;
  105. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  106. efi_memory_desc_t *md = p;
  107. unsigned long long start = md->phys_addr;
  108. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  109. int e820_type;
  110. switch (md->type) {
  111. case EFI_LOADER_CODE:
  112. case EFI_LOADER_DATA:
  113. case EFI_BOOT_SERVICES_CODE:
  114. case EFI_BOOT_SERVICES_DATA:
  115. case EFI_CONVENTIONAL_MEMORY:
  116. if (md->attribute & EFI_MEMORY_WB)
  117. e820_type = E820_RAM;
  118. else
  119. e820_type = E820_RESERVED;
  120. break;
  121. case EFI_ACPI_RECLAIM_MEMORY:
  122. e820_type = E820_ACPI;
  123. break;
  124. case EFI_ACPI_MEMORY_NVS:
  125. e820_type = E820_NVS;
  126. break;
  127. case EFI_UNUSABLE_MEMORY:
  128. e820_type = E820_UNUSABLE;
  129. break;
  130. default:
  131. /*
  132. * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
  133. * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
  134. * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
  135. */
  136. e820_type = E820_RESERVED;
  137. break;
  138. }
  139. e820_add_region(start, size, e820_type);
  140. }
  141. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  142. }
  143. int __init efi_memblock_x86_reserve_range(void)
  144. {
  145. struct efi_info *e = &boot_params.efi_info;
  146. unsigned long pmap;
  147. if (efi_enabled(EFI_PARAVIRT))
  148. return 0;
  149. #ifdef CONFIG_X86_32
  150. /* Can't handle data above 4GB at this time */
  151. if (e->efi_memmap_hi) {
  152. pr_err("Memory map is above 4GB, disabling EFI.\n");
  153. return -EINVAL;
  154. }
  155. pmap = e->efi_memmap;
  156. #else
  157. pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
  158. #endif
  159. memmap.phys_map = (void *)pmap;
  160. memmap.nr_map = e->efi_memmap_size /
  161. e->efi_memdesc_size;
  162. memmap.desc_size = e->efi_memdesc_size;
  163. memmap.desc_version = e->efi_memdesc_version;
  164. memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
  165. efi.memmap = &memmap;
  166. return 0;
  167. }
  168. static void __init print_efi_memmap(void)
  169. {
  170. #ifdef EFI_DEBUG
  171. efi_memory_desc_t *md;
  172. void *p;
  173. int i;
  174. for (p = memmap.map, i = 0;
  175. p < memmap.map_end;
  176. p += memmap.desc_size, i++) {
  177. char buf[64];
  178. md = p;
  179. pr_info("mem%02u: %s range=[0x%016llx-0x%016llx) (%lluMB)\n",
  180. i, efi_md_typeattr_format(buf, sizeof(buf), md),
  181. md->phys_addr,
  182. md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
  183. (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
  184. }
  185. #endif /* EFI_DEBUG */
  186. }
  187. void __init efi_unmap_memmap(void)
  188. {
  189. clear_bit(EFI_MEMMAP, &efi.flags);
  190. if (memmap.map) {
  191. early_memunmap(memmap.map, memmap.nr_map * memmap.desc_size);
  192. memmap.map = NULL;
  193. }
  194. }
  195. static int __init efi_systab_init(void *phys)
  196. {
  197. if (efi_enabled(EFI_64BIT)) {
  198. efi_system_table_64_t *systab64;
  199. struct efi_setup_data *data = NULL;
  200. u64 tmp = 0;
  201. if (efi_setup) {
  202. data = early_memremap(efi_setup, sizeof(*data));
  203. if (!data)
  204. return -ENOMEM;
  205. }
  206. systab64 = early_memremap((unsigned long)phys,
  207. sizeof(*systab64));
  208. if (systab64 == NULL) {
  209. pr_err("Couldn't map the system table!\n");
  210. if (data)
  211. early_memunmap(data, sizeof(*data));
  212. return -ENOMEM;
  213. }
  214. efi_systab.hdr = systab64->hdr;
  215. efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
  216. systab64->fw_vendor;
  217. tmp |= data ? data->fw_vendor : systab64->fw_vendor;
  218. efi_systab.fw_revision = systab64->fw_revision;
  219. efi_systab.con_in_handle = systab64->con_in_handle;
  220. tmp |= systab64->con_in_handle;
  221. efi_systab.con_in = systab64->con_in;
  222. tmp |= systab64->con_in;
  223. efi_systab.con_out_handle = systab64->con_out_handle;
  224. tmp |= systab64->con_out_handle;
  225. efi_systab.con_out = systab64->con_out;
  226. tmp |= systab64->con_out;
  227. efi_systab.stderr_handle = systab64->stderr_handle;
  228. tmp |= systab64->stderr_handle;
  229. efi_systab.stderr = systab64->stderr;
  230. tmp |= systab64->stderr;
  231. efi_systab.runtime = data ?
  232. (void *)(unsigned long)data->runtime :
  233. (void *)(unsigned long)systab64->runtime;
  234. tmp |= data ? data->runtime : systab64->runtime;
  235. efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
  236. tmp |= systab64->boottime;
  237. efi_systab.nr_tables = systab64->nr_tables;
  238. efi_systab.tables = data ? (unsigned long)data->tables :
  239. systab64->tables;
  240. tmp |= data ? data->tables : systab64->tables;
  241. early_memunmap(systab64, sizeof(*systab64));
  242. if (data)
  243. early_memunmap(data, sizeof(*data));
  244. #ifdef CONFIG_X86_32
  245. if (tmp >> 32) {
  246. pr_err("EFI data located above 4GB, disabling EFI.\n");
  247. return -EINVAL;
  248. }
  249. #endif
  250. } else {
  251. efi_system_table_32_t *systab32;
  252. systab32 = early_memremap((unsigned long)phys,
  253. sizeof(*systab32));
  254. if (systab32 == NULL) {
  255. pr_err("Couldn't map the system table!\n");
  256. return -ENOMEM;
  257. }
  258. efi_systab.hdr = systab32->hdr;
  259. efi_systab.fw_vendor = systab32->fw_vendor;
  260. efi_systab.fw_revision = systab32->fw_revision;
  261. efi_systab.con_in_handle = systab32->con_in_handle;
  262. efi_systab.con_in = systab32->con_in;
  263. efi_systab.con_out_handle = systab32->con_out_handle;
  264. efi_systab.con_out = systab32->con_out;
  265. efi_systab.stderr_handle = systab32->stderr_handle;
  266. efi_systab.stderr = systab32->stderr;
  267. efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
  268. efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
  269. efi_systab.nr_tables = systab32->nr_tables;
  270. efi_systab.tables = systab32->tables;
  271. early_memunmap(systab32, sizeof(*systab32));
  272. }
  273. efi.systab = &efi_systab;
  274. /*
  275. * Verify the EFI Table
  276. */
  277. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  278. pr_err("System table signature incorrect!\n");
  279. return -EINVAL;
  280. }
  281. if ((efi.systab->hdr.revision >> 16) == 0)
  282. pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
  283. efi.systab->hdr.revision >> 16,
  284. efi.systab->hdr.revision & 0xffff);
  285. set_bit(EFI_SYSTEM_TABLES, &efi.flags);
  286. return 0;
  287. }
  288. static int __init efi_runtime_init32(void)
  289. {
  290. efi_runtime_services_32_t *runtime;
  291. runtime = early_memremap((unsigned long)efi.systab->runtime,
  292. sizeof(efi_runtime_services_32_t));
  293. if (!runtime) {
  294. pr_err("Could not map the runtime service table!\n");
  295. return -ENOMEM;
  296. }
  297. /*
  298. * We will only need *early* access to the SetVirtualAddressMap
  299. * EFI runtime service. All other runtime services will be called
  300. * via the virtual mapping.
  301. */
  302. efi_phys.set_virtual_address_map =
  303. (efi_set_virtual_address_map_t *)
  304. (unsigned long)runtime->set_virtual_address_map;
  305. early_memunmap(runtime, sizeof(efi_runtime_services_32_t));
  306. return 0;
  307. }
  308. static int __init efi_runtime_init64(void)
  309. {
  310. efi_runtime_services_64_t *runtime;
  311. runtime = early_memremap((unsigned long)efi.systab->runtime,
  312. sizeof(efi_runtime_services_64_t));
  313. if (!runtime) {
  314. pr_err("Could not map the runtime service table!\n");
  315. return -ENOMEM;
  316. }
  317. /*
  318. * We will only need *early* access to the SetVirtualAddressMap
  319. * EFI runtime service. All other runtime services will be called
  320. * via the virtual mapping.
  321. */
  322. efi_phys.set_virtual_address_map =
  323. (efi_set_virtual_address_map_t *)
  324. (unsigned long)runtime->set_virtual_address_map;
  325. early_memunmap(runtime, sizeof(efi_runtime_services_64_t));
  326. return 0;
  327. }
  328. static int __init efi_runtime_init(void)
  329. {
  330. int rv;
  331. /*
  332. * Check out the runtime services table. We need to map
  333. * the runtime services table so that we can grab the physical
  334. * address of several of the EFI runtime functions, needed to
  335. * set the firmware into virtual mode.
  336. *
  337. * When EFI_PARAVIRT is in force then we could not map runtime
  338. * service memory region because we do not have direct access to it.
  339. * However, runtime services are available through proxy functions
  340. * (e.g. in case of Xen dom0 EFI implementation they call special
  341. * hypercall which executes relevant EFI functions) and that is why
  342. * they are always enabled.
  343. */
  344. if (!efi_enabled(EFI_PARAVIRT)) {
  345. if (efi_enabled(EFI_64BIT))
  346. rv = efi_runtime_init64();
  347. else
  348. rv = efi_runtime_init32();
  349. if (rv)
  350. return rv;
  351. }
  352. set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  353. return 0;
  354. }
  355. static int __init efi_memmap_init(void)
  356. {
  357. if (efi_enabled(EFI_PARAVIRT))
  358. return 0;
  359. /* Map the EFI memory map */
  360. memmap.map = early_memremap((unsigned long)memmap.phys_map,
  361. memmap.nr_map * memmap.desc_size);
  362. if (memmap.map == NULL) {
  363. pr_err("Could not map the memory map!\n");
  364. return -ENOMEM;
  365. }
  366. memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
  367. if (add_efi_memmap)
  368. do_add_efi_memmap();
  369. set_bit(EFI_MEMMAP, &efi.flags);
  370. return 0;
  371. }
  372. void __init efi_init(void)
  373. {
  374. efi_char16_t *c16;
  375. char vendor[100] = "unknown";
  376. int i = 0;
  377. void *tmp;
  378. #ifdef CONFIG_X86_32
  379. if (boot_params.efi_info.efi_systab_hi ||
  380. boot_params.efi_info.efi_memmap_hi) {
  381. pr_info("Table located above 4GB, disabling EFI.\n");
  382. return;
  383. }
  384. efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
  385. #else
  386. efi_phys.systab = (efi_system_table_t *)
  387. (boot_params.efi_info.efi_systab |
  388. ((__u64)boot_params.efi_info.efi_systab_hi<<32));
  389. #endif
  390. if (efi_systab_init(efi_phys.systab))
  391. return;
  392. efi.config_table = (unsigned long)efi.systab->tables;
  393. efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
  394. efi.runtime = (unsigned long)efi.systab->runtime;
  395. /*
  396. * Show what we know for posterity
  397. */
  398. c16 = tmp = early_memremap(efi.systab->fw_vendor, 2);
  399. if (c16) {
  400. for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
  401. vendor[i] = *c16++;
  402. vendor[i] = '\0';
  403. } else
  404. pr_err("Could not map the firmware vendor!\n");
  405. early_memunmap(tmp, 2);
  406. pr_info("EFI v%u.%.02u by %s\n",
  407. efi.systab->hdr.revision >> 16,
  408. efi.systab->hdr.revision & 0xffff, vendor);
  409. if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
  410. return;
  411. if (efi_config_init(arch_tables))
  412. return;
  413. /*
  414. * Note: We currently don't support runtime services on an EFI
  415. * that doesn't match the kernel 32/64-bit mode.
  416. */
  417. if (!efi_runtime_supported())
  418. pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
  419. else {
  420. if (efi_runtime_disabled() || efi_runtime_init())
  421. return;
  422. }
  423. if (efi_memmap_init())
  424. return;
  425. print_efi_memmap();
  426. }
  427. void __init efi_late_init(void)
  428. {
  429. efi_bgrt_init();
  430. }
  431. void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
  432. {
  433. u64 addr, npages;
  434. addr = md->virt_addr;
  435. npages = md->num_pages;
  436. memrange_efi_to_native(&addr, &npages);
  437. if (executable)
  438. set_memory_x(addr, npages);
  439. else
  440. set_memory_nx(addr, npages);
  441. }
  442. void __init runtime_code_page_mkexec(void)
  443. {
  444. efi_memory_desc_t *md;
  445. void *p;
  446. /* Make EFI runtime service code area executable */
  447. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  448. md = p;
  449. if (md->type != EFI_RUNTIME_SERVICES_CODE)
  450. continue;
  451. efi_set_executable(md, true);
  452. }
  453. }
  454. void __init efi_memory_uc(u64 addr, unsigned long size)
  455. {
  456. unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
  457. u64 npages;
  458. npages = round_up(size, page_shift) / page_shift;
  459. memrange_efi_to_native(&addr, &npages);
  460. set_memory_uc(addr, npages);
  461. }
  462. void __init old_map_region(efi_memory_desc_t *md)
  463. {
  464. u64 start_pfn, end_pfn, end;
  465. unsigned long size;
  466. void *va;
  467. start_pfn = PFN_DOWN(md->phys_addr);
  468. size = md->num_pages << PAGE_SHIFT;
  469. end = md->phys_addr + size;
  470. end_pfn = PFN_UP(end);
  471. if (pfn_range_is_mapped(start_pfn, end_pfn)) {
  472. va = __va(md->phys_addr);
  473. if (!(md->attribute & EFI_MEMORY_WB))
  474. efi_memory_uc((u64)(unsigned long)va, size);
  475. } else
  476. va = efi_ioremap(md->phys_addr, size,
  477. md->type, md->attribute);
  478. md->virt_addr = (u64) (unsigned long) va;
  479. if (!va)
  480. pr_err("ioremap of 0x%llX failed!\n",
  481. (unsigned long long)md->phys_addr);
  482. }
  483. /* Merge contiguous regions of the same type and attribute */
  484. static void __init efi_merge_regions(void)
  485. {
  486. void *p;
  487. efi_memory_desc_t *md, *prev_md = NULL;
  488. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  489. u64 prev_size;
  490. md = p;
  491. if (!prev_md) {
  492. prev_md = md;
  493. continue;
  494. }
  495. if (prev_md->type != md->type ||
  496. prev_md->attribute != md->attribute) {
  497. prev_md = md;
  498. continue;
  499. }
  500. prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
  501. if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
  502. prev_md->num_pages += md->num_pages;
  503. md->type = EFI_RESERVED_TYPE;
  504. md->attribute = 0;
  505. continue;
  506. }
  507. prev_md = md;
  508. }
  509. }
  510. static void __init get_systab_virt_addr(efi_memory_desc_t *md)
  511. {
  512. unsigned long size;
  513. u64 end, systab;
  514. size = md->num_pages << EFI_PAGE_SHIFT;
  515. end = md->phys_addr + size;
  516. systab = (u64)(unsigned long)efi_phys.systab;
  517. if (md->phys_addr <= systab && systab < end) {
  518. systab += md->virt_addr - md->phys_addr;
  519. efi.systab = (efi_system_table_t *)(unsigned long)systab;
  520. }
  521. }
  522. static void __init save_runtime_map(void)
  523. {
  524. #ifdef CONFIG_KEXEC
  525. efi_memory_desc_t *md;
  526. void *tmp, *p, *q = NULL;
  527. int count = 0;
  528. if (efi_enabled(EFI_OLD_MEMMAP))
  529. return;
  530. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  531. md = p;
  532. if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
  533. (md->type == EFI_BOOT_SERVICES_CODE) ||
  534. (md->type == EFI_BOOT_SERVICES_DATA))
  535. continue;
  536. tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
  537. if (!tmp)
  538. goto out;
  539. q = tmp;
  540. memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
  541. count++;
  542. }
  543. efi_runtime_map_setup(q, count, memmap.desc_size);
  544. return;
  545. out:
  546. kfree(q);
  547. pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
  548. #endif
  549. }
  550. static void *realloc_pages(void *old_memmap, int old_shift)
  551. {
  552. void *ret;
  553. ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
  554. if (!ret)
  555. goto out;
  556. /*
  557. * A first-time allocation doesn't have anything to copy.
  558. */
  559. if (!old_memmap)
  560. return ret;
  561. memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
  562. out:
  563. free_pages((unsigned long)old_memmap, old_shift);
  564. return ret;
  565. }
  566. /*
  567. * Map the efi memory ranges of the runtime services and update new_mmap with
  568. * virtual addresses.
  569. */
  570. static void * __init efi_map_regions(int *count, int *pg_shift)
  571. {
  572. void *p, *new_memmap = NULL;
  573. unsigned long left = 0;
  574. efi_memory_desc_t *md;
  575. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  576. md = p;
  577. if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
  578. #ifdef CONFIG_X86_64
  579. if (md->type != EFI_BOOT_SERVICES_CODE &&
  580. md->type != EFI_BOOT_SERVICES_DATA)
  581. #endif
  582. continue;
  583. }
  584. efi_map_region(md);
  585. get_systab_virt_addr(md);
  586. if (left < memmap.desc_size) {
  587. new_memmap = realloc_pages(new_memmap, *pg_shift);
  588. if (!new_memmap)
  589. return NULL;
  590. left += PAGE_SIZE << *pg_shift;
  591. (*pg_shift)++;
  592. }
  593. memcpy(new_memmap + (*count * memmap.desc_size), md,
  594. memmap.desc_size);
  595. left -= memmap.desc_size;
  596. (*count)++;
  597. }
  598. return new_memmap;
  599. }
  600. static void __init kexec_enter_virtual_mode(void)
  601. {
  602. #ifdef CONFIG_KEXEC
  603. efi_memory_desc_t *md;
  604. void *p;
  605. efi.systab = NULL;
  606. /*
  607. * We don't do virtual mode, since we don't do runtime services, on
  608. * non-native EFI
  609. */
  610. if (!efi_is_native()) {
  611. efi_unmap_memmap();
  612. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  613. return;
  614. }
  615. /*
  616. * Map efi regions which were passed via setup_data. The virt_addr is a
  617. * fixed addr which was used in first kernel of a kexec boot.
  618. */
  619. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  620. md = p;
  621. efi_map_region_fixed(md); /* FIXME: add error handling */
  622. get_systab_virt_addr(md);
  623. }
  624. save_runtime_map();
  625. BUG_ON(!efi.systab);
  626. efi_sync_low_kernel_mappings();
  627. /*
  628. * Now that EFI is in virtual mode, update the function
  629. * pointers in the runtime service table to the new virtual addresses.
  630. *
  631. * Call EFI services through wrapper functions.
  632. */
  633. efi.runtime_version = efi_systab.hdr.revision;
  634. efi_native_runtime_setup();
  635. efi.set_virtual_address_map = NULL;
  636. if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
  637. runtime_code_page_mkexec();
  638. /* clean DUMMY object */
  639. efi_delete_dummy_variable();
  640. #endif
  641. }
  642. /*
  643. * This function will switch the EFI runtime services to virtual mode.
  644. * Essentially, we look through the EFI memmap and map every region that
  645. * has the runtime attribute bit set in its memory descriptor into the
  646. * ->trampoline_pgd page table using a top-down VA allocation scheme.
  647. *
  648. * The old method which used to update that memory descriptor with the
  649. * virtual address obtained from ioremap() is still supported when the
  650. * kernel is booted with efi=old_map on its command line. Same old
  651. * method enabled the runtime services to be called without having to
  652. * thunk back into physical mode for every invocation.
  653. *
  654. * The new method does a pagetable switch in a preemption-safe manner
  655. * so that we're in a different address space when calling a runtime
  656. * function. For function arguments passing we do copy the PGDs of the
  657. * kernel page table into ->trampoline_pgd prior to each call.
  658. *
  659. * Specially for kexec boot, efi runtime maps in previous kernel should
  660. * be passed in via setup_data. In that case runtime ranges will be mapped
  661. * to the same virtual addresses as the first kernel, see
  662. * kexec_enter_virtual_mode().
  663. */
  664. static void __init __efi_enter_virtual_mode(void)
  665. {
  666. int count = 0, pg_shift = 0;
  667. void *new_memmap = NULL;
  668. efi_status_t status;
  669. efi.systab = NULL;
  670. efi_merge_regions();
  671. new_memmap = efi_map_regions(&count, &pg_shift);
  672. if (!new_memmap) {
  673. pr_err("Error reallocating memory, EFI runtime non-functional!\n");
  674. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  675. return;
  676. }
  677. save_runtime_map();
  678. BUG_ON(!efi.systab);
  679. if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift)) {
  680. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  681. return;
  682. }
  683. efi_sync_low_kernel_mappings();
  684. efi_dump_pagetable();
  685. if (efi_is_native()) {
  686. status = phys_efi_set_virtual_address_map(
  687. memmap.desc_size * count,
  688. memmap.desc_size,
  689. memmap.desc_version,
  690. (efi_memory_desc_t *)__pa(new_memmap));
  691. } else {
  692. status = efi_thunk_set_virtual_address_map(
  693. efi_phys.set_virtual_address_map,
  694. memmap.desc_size * count,
  695. memmap.desc_size,
  696. memmap.desc_version,
  697. (efi_memory_desc_t *)__pa(new_memmap));
  698. }
  699. if (status != EFI_SUCCESS) {
  700. pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
  701. status);
  702. panic("EFI call to SetVirtualAddressMap() failed!");
  703. }
  704. /*
  705. * Now that EFI is in virtual mode, update the function
  706. * pointers in the runtime service table to the new virtual addresses.
  707. *
  708. * Call EFI services through wrapper functions.
  709. */
  710. efi.runtime_version = efi_systab.hdr.revision;
  711. if (efi_is_native())
  712. efi_native_runtime_setup();
  713. else
  714. efi_thunk_runtime_setup();
  715. efi.set_virtual_address_map = NULL;
  716. efi_runtime_mkexec();
  717. /*
  718. * We mapped the descriptor array into the EFI pagetable above but we're
  719. * not unmapping it here. Here's why:
  720. *
  721. * We're copying select PGDs from the kernel page table to the EFI page
  722. * table and when we do so and make changes to those PGDs like unmapping
  723. * stuff from them, those changes appear in the kernel page table and we
  724. * go boom.
  725. *
  726. * From setup_real_mode():
  727. *
  728. * ...
  729. * trampoline_pgd[0] = init_level4_pgt[pgd_index(__PAGE_OFFSET)].pgd;
  730. *
  731. * In this particular case, our allocation is in PGD 0 of the EFI page
  732. * table but we've copied that PGD from PGD[272] of the EFI page table:
  733. *
  734. * pgd_index(__PAGE_OFFSET = 0xffff880000000000) = 272
  735. *
  736. * where the direct memory mapping in kernel space is.
  737. *
  738. * new_memmap's VA comes from that direct mapping and thus clearing it,
  739. * it would get cleared in the kernel page table too.
  740. *
  741. * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift);
  742. */
  743. free_pages((unsigned long)new_memmap, pg_shift);
  744. /* clean DUMMY object */
  745. efi_delete_dummy_variable();
  746. }
  747. void __init efi_enter_virtual_mode(void)
  748. {
  749. if (efi_enabled(EFI_PARAVIRT))
  750. return;
  751. if (efi_setup)
  752. kexec_enter_virtual_mode();
  753. else
  754. __efi_enter_virtual_mode();
  755. }
  756. /*
  757. * Convenience functions to obtain memory types and attributes
  758. */
  759. u32 efi_mem_type(unsigned long phys_addr)
  760. {
  761. efi_memory_desc_t *md;
  762. void *p;
  763. if (!efi_enabled(EFI_MEMMAP))
  764. return 0;
  765. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  766. md = p;
  767. if ((md->phys_addr <= phys_addr) &&
  768. (phys_addr < (md->phys_addr +
  769. (md->num_pages << EFI_PAGE_SHIFT))))
  770. return md->type;
  771. }
  772. return 0;
  773. }
  774. u64 efi_mem_attributes(unsigned long phys_addr)
  775. {
  776. efi_memory_desc_t *md;
  777. void *p;
  778. if (!efi_enabled(EFI_MEMMAP))
  779. return 0;
  780. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  781. md = p;
  782. if ((md->phys_addr <= phys_addr) &&
  783. (phys_addr < (md->phys_addr +
  784. (md->num_pages << EFI_PAGE_SHIFT))))
  785. return md->attribute;
  786. }
  787. return 0;
  788. }
  789. static int __init arch_parse_efi_cmdline(char *str)
  790. {
  791. if (parse_option_str(str, "old_map"))
  792. set_bit(EFI_OLD_MEMMAP, &efi.flags);
  793. return 0;
  794. }
  795. early_param("efi", arch_parse_efi_cmdline);