efi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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. unsigned long flags;
  79. pgd_t *save_pgd;
  80. save_pgd = efi_call_phys_prolog();
  81. /* Disable interrupts around EFI calls: */
  82. local_irq_save(flags);
  83. status = efi_call_phys(efi_phys.set_virtual_address_map,
  84. memory_map_size, descriptor_size,
  85. descriptor_version, virtual_map);
  86. local_irq_restore(flags);
  87. efi_call_phys_epilog(save_pgd);
  88. return status;
  89. }
  90. void efi_get_time(struct timespec *now)
  91. {
  92. efi_status_t status;
  93. efi_time_t eft;
  94. efi_time_cap_t cap;
  95. status = efi.get_time(&eft, &cap);
  96. if (status != EFI_SUCCESS)
  97. pr_err("Oops: efitime: can't read time!\n");
  98. now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
  99. eft.minute, eft.second);
  100. now->tv_nsec = 0;
  101. }
  102. /*
  103. * Tell the kernel about the EFI memory map. This might include
  104. * more than the max 128 entries that can fit in the e820 legacy
  105. * (zeropage) memory map.
  106. */
  107. static void __init do_add_efi_memmap(void)
  108. {
  109. void *p;
  110. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  111. efi_memory_desc_t *md = p;
  112. unsigned long long start = md->phys_addr;
  113. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  114. int e820_type;
  115. switch (md->type) {
  116. case EFI_LOADER_CODE:
  117. case EFI_LOADER_DATA:
  118. case EFI_BOOT_SERVICES_CODE:
  119. case EFI_BOOT_SERVICES_DATA:
  120. case EFI_CONVENTIONAL_MEMORY:
  121. if (md->attribute & EFI_MEMORY_WB)
  122. e820_type = E820_RAM;
  123. else
  124. e820_type = E820_RESERVED;
  125. break;
  126. case EFI_ACPI_RECLAIM_MEMORY:
  127. e820_type = E820_ACPI;
  128. break;
  129. case EFI_ACPI_MEMORY_NVS:
  130. e820_type = E820_NVS;
  131. break;
  132. case EFI_UNUSABLE_MEMORY:
  133. e820_type = E820_UNUSABLE;
  134. break;
  135. default:
  136. /*
  137. * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
  138. * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
  139. * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
  140. */
  141. e820_type = E820_RESERVED;
  142. break;
  143. }
  144. e820_add_region(start, size, e820_type);
  145. }
  146. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  147. }
  148. int __init efi_memblock_x86_reserve_range(void)
  149. {
  150. struct efi_info *e = &boot_params.efi_info;
  151. unsigned long pmap;
  152. if (efi_enabled(EFI_PARAVIRT))
  153. return 0;
  154. #ifdef CONFIG_X86_32
  155. /* Can't handle data above 4GB at this time */
  156. if (e->efi_memmap_hi) {
  157. pr_err("Memory map is above 4GB, disabling EFI.\n");
  158. return -EINVAL;
  159. }
  160. pmap = e->efi_memmap;
  161. #else
  162. pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
  163. #endif
  164. memmap.phys_map = (void *)pmap;
  165. memmap.nr_map = e->efi_memmap_size /
  166. e->efi_memdesc_size;
  167. memmap.desc_size = e->efi_memdesc_size;
  168. memmap.desc_version = e->efi_memdesc_version;
  169. memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
  170. efi.memmap = &memmap;
  171. return 0;
  172. }
  173. static void __init print_efi_memmap(void)
  174. {
  175. #ifdef EFI_DEBUG
  176. efi_memory_desc_t *md;
  177. void *p;
  178. int i;
  179. for (p = memmap.map, i = 0;
  180. p < memmap.map_end;
  181. p += memmap.desc_size, i++) {
  182. char buf[64];
  183. md = p;
  184. pr_info("mem%02u: %s range=[0x%016llx-0x%016llx) (%lluMB)\n",
  185. i, efi_md_typeattr_format(buf, sizeof(buf), md),
  186. md->phys_addr,
  187. md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
  188. (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
  189. }
  190. #endif /* EFI_DEBUG */
  191. }
  192. void __init efi_unmap_memmap(void)
  193. {
  194. clear_bit(EFI_MEMMAP, &efi.flags);
  195. if (memmap.map) {
  196. early_memunmap(memmap.map, memmap.nr_map * memmap.desc_size);
  197. memmap.map = NULL;
  198. }
  199. }
  200. static int __init efi_systab_init(void *phys)
  201. {
  202. if (efi_enabled(EFI_64BIT)) {
  203. efi_system_table_64_t *systab64;
  204. struct efi_setup_data *data = NULL;
  205. u64 tmp = 0;
  206. if (efi_setup) {
  207. data = early_memremap(efi_setup, sizeof(*data));
  208. if (!data)
  209. return -ENOMEM;
  210. }
  211. systab64 = early_memremap((unsigned long)phys,
  212. sizeof(*systab64));
  213. if (systab64 == NULL) {
  214. pr_err("Couldn't map the system table!\n");
  215. if (data)
  216. early_memunmap(data, sizeof(*data));
  217. return -ENOMEM;
  218. }
  219. efi_systab.hdr = systab64->hdr;
  220. efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
  221. systab64->fw_vendor;
  222. tmp |= data ? data->fw_vendor : systab64->fw_vendor;
  223. efi_systab.fw_revision = systab64->fw_revision;
  224. efi_systab.con_in_handle = systab64->con_in_handle;
  225. tmp |= systab64->con_in_handle;
  226. efi_systab.con_in = systab64->con_in;
  227. tmp |= systab64->con_in;
  228. efi_systab.con_out_handle = systab64->con_out_handle;
  229. tmp |= systab64->con_out_handle;
  230. efi_systab.con_out = systab64->con_out;
  231. tmp |= systab64->con_out;
  232. efi_systab.stderr_handle = systab64->stderr_handle;
  233. tmp |= systab64->stderr_handle;
  234. efi_systab.stderr = systab64->stderr;
  235. tmp |= systab64->stderr;
  236. efi_systab.runtime = data ?
  237. (void *)(unsigned long)data->runtime :
  238. (void *)(unsigned long)systab64->runtime;
  239. tmp |= data ? data->runtime : systab64->runtime;
  240. efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
  241. tmp |= systab64->boottime;
  242. efi_systab.nr_tables = systab64->nr_tables;
  243. efi_systab.tables = data ? (unsigned long)data->tables :
  244. systab64->tables;
  245. tmp |= data ? data->tables : systab64->tables;
  246. early_memunmap(systab64, sizeof(*systab64));
  247. if (data)
  248. early_memunmap(data, sizeof(*data));
  249. #ifdef CONFIG_X86_32
  250. if (tmp >> 32) {
  251. pr_err("EFI data located above 4GB, disabling EFI.\n");
  252. return -EINVAL;
  253. }
  254. #endif
  255. } else {
  256. efi_system_table_32_t *systab32;
  257. systab32 = early_memremap((unsigned long)phys,
  258. sizeof(*systab32));
  259. if (systab32 == NULL) {
  260. pr_err("Couldn't map the system table!\n");
  261. return -ENOMEM;
  262. }
  263. efi_systab.hdr = systab32->hdr;
  264. efi_systab.fw_vendor = systab32->fw_vendor;
  265. efi_systab.fw_revision = systab32->fw_revision;
  266. efi_systab.con_in_handle = systab32->con_in_handle;
  267. efi_systab.con_in = systab32->con_in;
  268. efi_systab.con_out_handle = systab32->con_out_handle;
  269. efi_systab.con_out = systab32->con_out;
  270. efi_systab.stderr_handle = systab32->stderr_handle;
  271. efi_systab.stderr = systab32->stderr;
  272. efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
  273. efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
  274. efi_systab.nr_tables = systab32->nr_tables;
  275. efi_systab.tables = systab32->tables;
  276. early_memunmap(systab32, sizeof(*systab32));
  277. }
  278. efi.systab = &efi_systab;
  279. /*
  280. * Verify the EFI Table
  281. */
  282. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  283. pr_err("System table signature incorrect!\n");
  284. return -EINVAL;
  285. }
  286. if ((efi.systab->hdr.revision >> 16) == 0)
  287. pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
  288. efi.systab->hdr.revision >> 16,
  289. efi.systab->hdr.revision & 0xffff);
  290. set_bit(EFI_SYSTEM_TABLES, &efi.flags);
  291. return 0;
  292. }
  293. static int __init efi_runtime_init32(void)
  294. {
  295. efi_runtime_services_32_t *runtime;
  296. runtime = early_memremap((unsigned long)efi.systab->runtime,
  297. sizeof(efi_runtime_services_32_t));
  298. if (!runtime) {
  299. pr_err("Could not map the runtime service table!\n");
  300. return -ENOMEM;
  301. }
  302. /*
  303. * We will only need *early* access to the SetVirtualAddressMap
  304. * EFI runtime service. All other runtime services will be called
  305. * via the virtual mapping.
  306. */
  307. efi_phys.set_virtual_address_map =
  308. (efi_set_virtual_address_map_t *)
  309. (unsigned long)runtime->set_virtual_address_map;
  310. early_memunmap(runtime, sizeof(efi_runtime_services_32_t));
  311. return 0;
  312. }
  313. static int __init efi_runtime_init64(void)
  314. {
  315. efi_runtime_services_64_t *runtime;
  316. runtime = early_memremap((unsigned long)efi.systab->runtime,
  317. sizeof(efi_runtime_services_64_t));
  318. if (!runtime) {
  319. pr_err("Could not map the runtime service table!\n");
  320. return -ENOMEM;
  321. }
  322. /*
  323. * We will only need *early* access to the SetVirtualAddressMap
  324. * EFI runtime service. All other runtime services will be called
  325. * via the virtual mapping.
  326. */
  327. efi_phys.set_virtual_address_map =
  328. (efi_set_virtual_address_map_t *)
  329. (unsigned long)runtime->set_virtual_address_map;
  330. early_memunmap(runtime, sizeof(efi_runtime_services_64_t));
  331. return 0;
  332. }
  333. static int __init efi_runtime_init(void)
  334. {
  335. int rv;
  336. /*
  337. * Check out the runtime services table. We need to map
  338. * the runtime services table so that we can grab the physical
  339. * address of several of the EFI runtime functions, needed to
  340. * set the firmware into virtual mode.
  341. *
  342. * When EFI_PARAVIRT is in force then we could not map runtime
  343. * service memory region because we do not have direct access to it.
  344. * However, runtime services are available through proxy functions
  345. * (e.g. in case of Xen dom0 EFI implementation they call special
  346. * hypercall which executes relevant EFI functions) and that is why
  347. * they are always enabled.
  348. */
  349. if (!efi_enabled(EFI_PARAVIRT)) {
  350. if (efi_enabled(EFI_64BIT))
  351. rv = efi_runtime_init64();
  352. else
  353. rv = efi_runtime_init32();
  354. if (rv)
  355. return rv;
  356. }
  357. set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  358. return 0;
  359. }
  360. static int __init efi_memmap_init(void)
  361. {
  362. if (efi_enabled(EFI_PARAVIRT))
  363. return 0;
  364. /* Map the EFI memory map */
  365. memmap.map = early_memremap((unsigned long)memmap.phys_map,
  366. memmap.nr_map * memmap.desc_size);
  367. if (memmap.map == NULL) {
  368. pr_err("Could not map the memory map!\n");
  369. return -ENOMEM;
  370. }
  371. memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
  372. if (add_efi_memmap)
  373. do_add_efi_memmap();
  374. set_bit(EFI_MEMMAP, &efi.flags);
  375. return 0;
  376. }
  377. void __init efi_init(void)
  378. {
  379. efi_char16_t *c16;
  380. char vendor[100] = "unknown";
  381. int i = 0;
  382. void *tmp;
  383. #ifdef CONFIG_X86_32
  384. if (boot_params.efi_info.efi_systab_hi ||
  385. boot_params.efi_info.efi_memmap_hi) {
  386. pr_info("Table located above 4GB, disabling EFI.\n");
  387. return;
  388. }
  389. efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
  390. #else
  391. efi_phys.systab = (efi_system_table_t *)
  392. (boot_params.efi_info.efi_systab |
  393. ((__u64)boot_params.efi_info.efi_systab_hi<<32));
  394. #endif
  395. if (efi_systab_init(efi_phys.systab))
  396. return;
  397. efi.config_table = (unsigned long)efi.systab->tables;
  398. efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
  399. efi.runtime = (unsigned long)efi.systab->runtime;
  400. /*
  401. * Show what we know for posterity
  402. */
  403. c16 = tmp = early_memremap(efi.systab->fw_vendor, 2);
  404. if (c16) {
  405. for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
  406. vendor[i] = *c16++;
  407. vendor[i] = '\0';
  408. } else
  409. pr_err("Could not map the firmware vendor!\n");
  410. early_memunmap(tmp, 2);
  411. pr_info("EFI v%u.%.02u by %s\n",
  412. efi.systab->hdr.revision >> 16,
  413. efi.systab->hdr.revision & 0xffff, vendor);
  414. if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
  415. return;
  416. if (efi_config_init(arch_tables))
  417. return;
  418. /*
  419. * Note: We currently don't support runtime services on an EFI
  420. * that doesn't match the kernel 32/64-bit mode.
  421. */
  422. if (!efi_runtime_supported())
  423. pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
  424. else {
  425. if (efi_runtime_disabled() || efi_runtime_init())
  426. return;
  427. }
  428. if (efi_memmap_init())
  429. return;
  430. if (efi_enabled(EFI_DBG))
  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 __init 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. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  619. return;
  620. }
  621. /*
  622. * Map efi regions which were passed via setup_data. The virt_addr is a
  623. * fixed addr which was used in first kernel of a kexec boot.
  624. */
  625. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  626. md = p;
  627. efi_map_region_fixed(md); /* FIXME: add error handling */
  628. get_systab_virt_addr(md);
  629. }
  630. save_runtime_map();
  631. BUG_ON(!efi.systab);
  632. efi_sync_low_kernel_mappings();
  633. /*
  634. * Now that EFI is in virtual mode, update the function
  635. * pointers in the runtime service table to the new virtual addresses.
  636. *
  637. * Call EFI services through wrapper functions.
  638. */
  639. efi.runtime_version = efi_systab.hdr.revision;
  640. efi_native_runtime_setup();
  641. efi.set_virtual_address_map = NULL;
  642. if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
  643. runtime_code_page_mkexec();
  644. /* clean DUMMY object */
  645. efi_delete_dummy_variable();
  646. #endif
  647. }
  648. /*
  649. * This function will switch the EFI runtime services to virtual mode.
  650. * Essentially, we look through the EFI memmap and map every region that
  651. * has the runtime attribute bit set in its memory descriptor into the
  652. * ->trampoline_pgd page table using a top-down VA allocation scheme.
  653. *
  654. * The old method which used to update that memory descriptor with the
  655. * virtual address obtained from ioremap() is still supported when the
  656. * kernel is booted with efi=old_map on its command line. Same old
  657. * method enabled the runtime services to be called without having to
  658. * thunk back into physical mode for every invocation.
  659. *
  660. * The new method does a pagetable switch in a preemption-safe manner
  661. * so that we're in a different address space when calling a runtime
  662. * function. For function arguments passing we do copy the PGDs of the
  663. * kernel page table into ->trampoline_pgd prior to each call.
  664. *
  665. * Specially for kexec boot, efi runtime maps in previous kernel should
  666. * be passed in via setup_data. In that case runtime ranges will be mapped
  667. * to the same virtual addresses as the first kernel, see
  668. * kexec_enter_virtual_mode().
  669. */
  670. static void __init __efi_enter_virtual_mode(void)
  671. {
  672. int count = 0, pg_shift = 0;
  673. void *new_memmap = NULL;
  674. efi_status_t status;
  675. efi.systab = NULL;
  676. efi_merge_regions();
  677. new_memmap = efi_map_regions(&count, &pg_shift);
  678. if (!new_memmap) {
  679. pr_err("Error reallocating memory, EFI runtime non-functional!\n");
  680. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  681. return;
  682. }
  683. save_runtime_map();
  684. BUG_ON(!efi.systab);
  685. if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift)) {
  686. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  687. return;
  688. }
  689. efi_sync_low_kernel_mappings();
  690. efi_dump_pagetable();
  691. if (efi_is_native()) {
  692. status = phys_efi_set_virtual_address_map(
  693. memmap.desc_size * count,
  694. memmap.desc_size,
  695. memmap.desc_version,
  696. (efi_memory_desc_t *)__pa(new_memmap));
  697. } else {
  698. status = efi_thunk_set_virtual_address_map(
  699. efi_phys.set_virtual_address_map,
  700. memmap.desc_size * count,
  701. memmap.desc_size,
  702. memmap.desc_version,
  703. (efi_memory_desc_t *)__pa(new_memmap));
  704. }
  705. if (status != EFI_SUCCESS) {
  706. pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
  707. status);
  708. panic("EFI call to SetVirtualAddressMap() failed!");
  709. }
  710. /*
  711. * Now that EFI is in virtual mode, update the function
  712. * pointers in the runtime service table to the new virtual addresses.
  713. *
  714. * Call EFI services through wrapper functions.
  715. */
  716. efi.runtime_version = efi_systab.hdr.revision;
  717. if (efi_is_native())
  718. efi_native_runtime_setup();
  719. else
  720. efi_thunk_runtime_setup();
  721. efi.set_virtual_address_map = NULL;
  722. efi_runtime_mkexec();
  723. /*
  724. * We mapped the descriptor array into the EFI pagetable above but we're
  725. * not unmapping it here. Here's why:
  726. *
  727. * We're copying select PGDs from the kernel page table to the EFI page
  728. * table and when we do so and make changes to those PGDs like unmapping
  729. * stuff from them, those changes appear in the kernel page table and we
  730. * go boom.
  731. *
  732. * From setup_real_mode():
  733. *
  734. * ...
  735. * trampoline_pgd[0] = init_level4_pgt[pgd_index(__PAGE_OFFSET)].pgd;
  736. *
  737. * In this particular case, our allocation is in PGD 0 of the EFI page
  738. * table but we've copied that PGD from PGD[272] of the EFI page table:
  739. *
  740. * pgd_index(__PAGE_OFFSET = 0xffff880000000000) = 272
  741. *
  742. * where the direct memory mapping in kernel space is.
  743. *
  744. * new_memmap's VA comes from that direct mapping and thus clearing it,
  745. * it would get cleared in the kernel page table too.
  746. *
  747. * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift);
  748. */
  749. free_pages((unsigned long)new_memmap, pg_shift);
  750. /* clean DUMMY object */
  751. efi_delete_dummy_variable();
  752. }
  753. void __init efi_enter_virtual_mode(void)
  754. {
  755. if (efi_enabled(EFI_PARAVIRT))
  756. return;
  757. if (efi_setup)
  758. kexec_enter_virtual_mode();
  759. else
  760. __efi_enter_virtual_mode();
  761. }
  762. /*
  763. * Convenience functions to obtain memory types and attributes
  764. */
  765. u32 efi_mem_type(unsigned long phys_addr)
  766. {
  767. efi_memory_desc_t *md;
  768. void *p;
  769. if (!efi_enabled(EFI_MEMMAP))
  770. return 0;
  771. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  772. md = p;
  773. if ((md->phys_addr <= phys_addr) &&
  774. (phys_addr < (md->phys_addr +
  775. (md->num_pages << EFI_PAGE_SHIFT))))
  776. return md->type;
  777. }
  778. return 0;
  779. }
  780. u64 efi_mem_attributes(unsigned long phys_addr)
  781. {
  782. efi_memory_desc_t *md;
  783. void *p;
  784. if (!efi_enabled(EFI_MEMMAP))
  785. return 0;
  786. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  787. md = p;
  788. if ((md->phys_addr <= phys_addr) &&
  789. (phys_addr < (md->phys_addr +
  790. (md->num_pages << EFI_PAGE_SHIFT))))
  791. return md->attribute;
  792. }
  793. return 0;
  794. }
  795. static int __init arch_parse_efi_cmdline(char *str)
  796. {
  797. if (parse_option_str(str, "old_map"))
  798. set_bit(EFI_OLD_MEMMAP, &efi.flags);
  799. if (parse_option_str(str, "debug"))
  800. set_bit(EFI_DBG, &efi.flags);
  801. return 0;
  802. }
  803. early_param("efi", arch_parse_efi_cmdline);