efi.c 24 KB

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