efi.c 24 KB

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