efi.c 26 KB

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