efi.c 23 KB

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