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