efi.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. *
  16. * Copied from efi_32.c to eliminate the duplicated code between EFI
  17. * 32/64 support code. --ying 2007-10-26
  18. *
  19. * All EFI Runtime Services are not implemented yet as EFI only
  20. * supports physical mode addressing on SoftSDV. This is to be fixed
  21. * in a future version. --drummond 1999-07-20
  22. *
  23. * Implemented EFI runtime services and virtual mode calls. --davidm
  24. *
  25. * Goutham Rao: <goutham.rao@intel.com>
  26. * Skip non-WB memory and ignore empty memory ranges.
  27. */
  28. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/kernel.h>
  30. #include <linux/init.h>
  31. #include <linux/efi.h>
  32. #include <linux/efi-bgrt.h>
  33. #include <linux/export.h>
  34. #include <linux/bootmem.h>
  35. #include <linux/memblock.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/time.h>
  39. #include <linux/io.h>
  40. #include <linux/reboot.h>
  41. #include <linux/bcd.h>
  42. #include <asm/setup.h>
  43. #include <asm/efi.h>
  44. #include <asm/time.h>
  45. #include <asm/cacheflush.h>
  46. #include <asm/tlbflush.h>
  47. #include <asm/x86_init.h>
  48. #define EFI_DEBUG 1
  49. struct efi __read_mostly efi = {
  50. .mps = EFI_INVALID_TABLE_ADDR,
  51. .acpi = EFI_INVALID_TABLE_ADDR,
  52. .acpi20 = EFI_INVALID_TABLE_ADDR,
  53. .smbios = EFI_INVALID_TABLE_ADDR,
  54. .sal_systab = EFI_INVALID_TABLE_ADDR,
  55. .boot_info = EFI_INVALID_TABLE_ADDR,
  56. .hcdp = EFI_INVALID_TABLE_ADDR,
  57. .uga = EFI_INVALID_TABLE_ADDR,
  58. .uv_systab = EFI_INVALID_TABLE_ADDR,
  59. };
  60. EXPORT_SYMBOL(efi);
  61. struct efi_memory_map memmap;
  62. static struct efi efi_phys __initdata;
  63. static efi_system_table_t efi_systab __initdata;
  64. static inline bool efi_is_native(void)
  65. {
  66. return IS_ENABLED(CONFIG_X86_64) == efi_enabled(EFI_64BIT);
  67. }
  68. unsigned long x86_efi_facility;
  69. /*
  70. * Returns 1 if 'facility' is enabled, 0 otherwise.
  71. */
  72. int efi_enabled(int facility)
  73. {
  74. return test_bit(facility, &x86_efi_facility) != 0;
  75. }
  76. EXPORT_SYMBOL(efi_enabled);
  77. static bool __initdata disable_runtime = false;
  78. static int __init setup_noefi(char *arg)
  79. {
  80. disable_runtime = true;
  81. return 0;
  82. }
  83. early_param("noefi", setup_noefi);
  84. int add_efi_memmap;
  85. EXPORT_SYMBOL(add_efi_memmap);
  86. static int __init setup_add_efi_memmap(char *arg)
  87. {
  88. add_efi_memmap = 1;
  89. return 0;
  90. }
  91. early_param("add_efi_memmap", setup_add_efi_memmap);
  92. static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  93. {
  94. unsigned long flags;
  95. efi_status_t status;
  96. spin_lock_irqsave(&rtc_lock, flags);
  97. status = efi_call_virt2(get_time, tm, tc);
  98. spin_unlock_irqrestore(&rtc_lock, flags);
  99. return status;
  100. }
  101. static efi_status_t virt_efi_set_time(efi_time_t *tm)
  102. {
  103. unsigned long flags;
  104. efi_status_t status;
  105. spin_lock_irqsave(&rtc_lock, flags);
  106. status = efi_call_virt1(set_time, tm);
  107. spin_unlock_irqrestore(&rtc_lock, flags);
  108. return status;
  109. }
  110. static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
  111. efi_bool_t *pending,
  112. efi_time_t *tm)
  113. {
  114. unsigned long flags;
  115. efi_status_t status;
  116. spin_lock_irqsave(&rtc_lock, flags);
  117. status = efi_call_virt3(get_wakeup_time,
  118. enabled, pending, tm);
  119. spin_unlock_irqrestore(&rtc_lock, flags);
  120. return status;
  121. }
  122. static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  123. {
  124. unsigned long flags;
  125. efi_status_t status;
  126. spin_lock_irqsave(&rtc_lock, flags);
  127. status = efi_call_virt2(set_wakeup_time,
  128. enabled, tm);
  129. spin_unlock_irqrestore(&rtc_lock, flags);
  130. return status;
  131. }
  132. static efi_status_t virt_efi_get_variable(efi_char16_t *name,
  133. efi_guid_t *vendor,
  134. u32 *attr,
  135. unsigned long *data_size,
  136. void *data)
  137. {
  138. return efi_call_virt5(get_variable,
  139. name, vendor, attr,
  140. data_size, data);
  141. }
  142. static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
  143. efi_char16_t *name,
  144. efi_guid_t *vendor)
  145. {
  146. return efi_call_virt3(get_next_variable,
  147. name_size, name, vendor);
  148. }
  149. static efi_status_t virt_efi_set_variable(efi_char16_t *name,
  150. efi_guid_t *vendor,
  151. u32 attr,
  152. unsigned long data_size,
  153. void *data)
  154. {
  155. return efi_call_virt5(set_variable,
  156. name, vendor, attr,
  157. data_size, data);
  158. }
  159. static efi_status_t virt_efi_query_variable_info(u32 attr,
  160. u64 *storage_space,
  161. u64 *remaining_space,
  162. u64 *max_variable_size)
  163. {
  164. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  165. return EFI_UNSUPPORTED;
  166. return efi_call_virt4(query_variable_info, attr, storage_space,
  167. remaining_space, max_variable_size);
  168. }
  169. static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
  170. {
  171. return efi_call_virt1(get_next_high_mono_count, count);
  172. }
  173. static void virt_efi_reset_system(int reset_type,
  174. efi_status_t status,
  175. unsigned long data_size,
  176. efi_char16_t *data)
  177. {
  178. efi_call_virt4(reset_system, reset_type, status,
  179. data_size, data);
  180. }
  181. static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
  182. unsigned long count,
  183. unsigned long sg_list)
  184. {
  185. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  186. return EFI_UNSUPPORTED;
  187. return efi_call_virt3(update_capsule, capsules, count, sg_list);
  188. }
  189. static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
  190. unsigned long count,
  191. u64 *max_size,
  192. int *reset_type)
  193. {
  194. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  195. return EFI_UNSUPPORTED;
  196. return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
  197. reset_type);
  198. }
  199. static efi_status_t __init phys_efi_set_virtual_address_map(
  200. unsigned long memory_map_size,
  201. unsigned long descriptor_size,
  202. u32 descriptor_version,
  203. efi_memory_desc_t *virtual_map)
  204. {
  205. efi_status_t status;
  206. efi_call_phys_prelog();
  207. status = efi_call_phys4(efi_phys.set_virtual_address_map,
  208. memory_map_size, descriptor_size,
  209. descriptor_version, virtual_map);
  210. efi_call_phys_epilog();
  211. return status;
  212. }
  213. static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
  214. efi_time_cap_t *tc)
  215. {
  216. unsigned long flags;
  217. efi_status_t status;
  218. spin_lock_irqsave(&rtc_lock, flags);
  219. efi_call_phys_prelog();
  220. status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
  221. virt_to_phys(tc));
  222. efi_call_phys_epilog();
  223. spin_unlock_irqrestore(&rtc_lock, flags);
  224. return status;
  225. }
  226. int efi_set_rtc_mmss(unsigned long nowtime)
  227. {
  228. int real_seconds, real_minutes;
  229. efi_status_t status;
  230. efi_time_t eft;
  231. efi_time_cap_t cap;
  232. status = efi.get_time(&eft, &cap);
  233. if (status != EFI_SUCCESS) {
  234. pr_err("Oops: efitime: can't read time!\n");
  235. return -1;
  236. }
  237. real_seconds = nowtime % 60;
  238. real_minutes = nowtime / 60;
  239. if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
  240. real_minutes += 30;
  241. real_minutes %= 60;
  242. eft.minute = real_minutes;
  243. eft.second = real_seconds;
  244. status = efi.set_time(&eft);
  245. if (status != EFI_SUCCESS) {
  246. pr_err("Oops: efitime: can't write time!\n");
  247. return -1;
  248. }
  249. return 0;
  250. }
  251. unsigned long efi_get_time(void)
  252. {
  253. efi_status_t status;
  254. efi_time_t eft;
  255. efi_time_cap_t cap;
  256. status = efi.get_time(&eft, &cap);
  257. if (status != EFI_SUCCESS)
  258. pr_err("Oops: efitime: can't read time!\n");
  259. return mktime(eft.year, eft.month, eft.day, eft.hour,
  260. eft.minute, eft.second);
  261. }
  262. /*
  263. * Tell the kernel about the EFI memory map. This might include
  264. * more than the max 128 entries that can fit in the e820 legacy
  265. * (zeropage) memory map.
  266. */
  267. static void __init do_add_efi_memmap(void)
  268. {
  269. void *p;
  270. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  271. efi_memory_desc_t *md = p;
  272. unsigned long long start = md->phys_addr;
  273. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  274. int e820_type;
  275. switch (md->type) {
  276. case EFI_LOADER_CODE:
  277. case EFI_LOADER_DATA:
  278. case EFI_BOOT_SERVICES_CODE:
  279. case EFI_BOOT_SERVICES_DATA:
  280. case EFI_CONVENTIONAL_MEMORY:
  281. if (md->attribute & EFI_MEMORY_WB)
  282. e820_type = E820_RAM;
  283. else
  284. e820_type = E820_RESERVED;
  285. break;
  286. case EFI_ACPI_RECLAIM_MEMORY:
  287. e820_type = E820_ACPI;
  288. break;
  289. case EFI_ACPI_MEMORY_NVS:
  290. e820_type = E820_NVS;
  291. break;
  292. case EFI_UNUSABLE_MEMORY:
  293. e820_type = E820_UNUSABLE;
  294. break;
  295. default:
  296. /*
  297. * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
  298. * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
  299. * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
  300. */
  301. e820_type = E820_RESERVED;
  302. break;
  303. }
  304. e820_add_region(start, size, e820_type);
  305. }
  306. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  307. }
  308. int __init efi_memblock_x86_reserve_range(void)
  309. {
  310. unsigned long pmap;
  311. #ifdef CONFIG_X86_32
  312. /* Can't handle data above 4GB at this time */
  313. if (boot_params.efi_info.efi_memmap_hi) {
  314. pr_err("Memory map is above 4GB, disabling EFI.\n");
  315. return -EINVAL;
  316. }
  317. pmap = boot_params.efi_info.efi_memmap;
  318. #else
  319. pmap = (boot_params.efi_info.efi_memmap |
  320. ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
  321. #endif
  322. memmap.phys_map = (void *)pmap;
  323. memmap.nr_map = boot_params.efi_info.efi_memmap_size /
  324. boot_params.efi_info.efi_memdesc_size;
  325. memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
  326. memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
  327. memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
  328. return 0;
  329. }
  330. #if EFI_DEBUG
  331. static void __init print_efi_memmap(void)
  332. {
  333. efi_memory_desc_t *md;
  334. void *p;
  335. int i;
  336. for (p = memmap.map, i = 0;
  337. p < memmap.map_end;
  338. p += memmap.desc_size, i++) {
  339. md = p;
  340. pr_info("mem%02u: type=%u, attr=0x%llx, "
  341. "range=[0x%016llx-0x%016llx) (%lluMB)\n",
  342. i, md->type, md->attribute, md->phys_addr,
  343. md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
  344. (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
  345. }
  346. }
  347. #endif /* EFI_DEBUG */
  348. void __init efi_reserve_boot_services(void)
  349. {
  350. void *p;
  351. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  352. efi_memory_desc_t *md = p;
  353. u64 start = md->phys_addr;
  354. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  355. if (md->type != EFI_BOOT_SERVICES_CODE &&
  356. md->type != EFI_BOOT_SERVICES_DATA)
  357. continue;
  358. /* Only reserve where possible:
  359. * - Not within any already allocated areas
  360. * - Not over any memory area (really needed, if above?)
  361. * - Not within any part of the kernel
  362. * - Not the bios reserved area
  363. */
  364. if ((start+size >= virt_to_phys(_text)
  365. && start <= virt_to_phys(_end)) ||
  366. !e820_all_mapped(start, start+size, E820_RAM) ||
  367. memblock_is_region_reserved(start, size)) {
  368. /* Could not reserve, skip it */
  369. md->num_pages = 0;
  370. memblock_dbg("Could not reserve boot range "
  371. "[0x%010llx-0x%010llx]\n",
  372. start, start+size-1);
  373. } else
  374. memblock_reserve(start, size);
  375. }
  376. }
  377. void __init efi_unmap_memmap(void)
  378. {
  379. clear_bit(EFI_MEMMAP, &x86_efi_facility);
  380. if (memmap.map) {
  381. early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
  382. memmap.map = NULL;
  383. }
  384. }
  385. void __init efi_free_boot_services(void)
  386. {
  387. void *p;
  388. if (!efi_is_native())
  389. return;
  390. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  391. efi_memory_desc_t *md = p;
  392. unsigned long long start = md->phys_addr;
  393. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  394. if (md->type != EFI_BOOT_SERVICES_CODE &&
  395. md->type != EFI_BOOT_SERVICES_DATA)
  396. continue;
  397. /* Could not reserve boot area */
  398. if (!size)
  399. continue;
  400. free_bootmem_late(start, size);
  401. }
  402. efi_unmap_memmap();
  403. }
  404. static int __init efi_systab_init(void *phys)
  405. {
  406. if (efi_enabled(EFI_64BIT)) {
  407. efi_system_table_64_t *systab64;
  408. u64 tmp = 0;
  409. systab64 = early_ioremap((unsigned long)phys,
  410. sizeof(*systab64));
  411. if (systab64 == NULL) {
  412. pr_err("Couldn't map the system table!\n");
  413. return -ENOMEM;
  414. }
  415. efi_systab.hdr = systab64->hdr;
  416. efi_systab.fw_vendor = systab64->fw_vendor;
  417. tmp |= systab64->fw_vendor;
  418. efi_systab.fw_revision = systab64->fw_revision;
  419. efi_systab.con_in_handle = systab64->con_in_handle;
  420. tmp |= systab64->con_in_handle;
  421. efi_systab.con_in = systab64->con_in;
  422. tmp |= systab64->con_in;
  423. efi_systab.con_out_handle = systab64->con_out_handle;
  424. tmp |= systab64->con_out_handle;
  425. efi_systab.con_out = systab64->con_out;
  426. tmp |= systab64->con_out;
  427. efi_systab.stderr_handle = systab64->stderr_handle;
  428. tmp |= systab64->stderr_handle;
  429. efi_systab.stderr = systab64->stderr;
  430. tmp |= systab64->stderr;
  431. efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
  432. tmp |= systab64->runtime;
  433. efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
  434. tmp |= systab64->boottime;
  435. efi_systab.nr_tables = systab64->nr_tables;
  436. efi_systab.tables = systab64->tables;
  437. tmp |= systab64->tables;
  438. early_iounmap(systab64, sizeof(*systab64));
  439. #ifdef CONFIG_X86_32
  440. if (tmp >> 32) {
  441. pr_err("EFI data located above 4GB, disabling EFI.\n");
  442. return -EINVAL;
  443. }
  444. #endif
  445. } else {
  446. efi_system_table_32_t *systab32;
  447. systab32 = early_ioremap((unsigned long)phys,
  448. sizeof(*systab32));
  449. if (systab32 == NULL) {
  450. pr_err("Couldn't map the system table!\n");
  451. return -ENOMEM;
  452. }
  453. efi_systab.hdr = systab32->hdr;
  454. efi_systab.fw_vendor = systab32->fw_vendor;
  455. efi_systab.fw_revision = systab32->fw_revision;
  456. efi_systab.con_in_handle = systab32->con_in_handle;
  457. efi_systab.con_in = systab32->con_in;
  458. efi_systab.con_out_handle = systab32->con_out_handle;
  459. efi_systab.con_out = systab32->con_out;
  460. efi_systab.stderr_handle = systab32->stderr_handle;
  461. efi_systab.stderr = systab32->stderr;
  462. efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
  463. efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
  464. efi_systab.nr_tables = systab32->nr_tables;
  465. efi_systab.tables = systab32->tables;
  466. early_iounmap(systab32, sizeof(*systab32));
  467. }
  468. efi.systab = &efi_systab;
  469. /*
  470. * Verify the EFI Table
  471. */
  472. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  473. pr_err("System table signature incorrect!\n");
  474. return -EINVAL;
  475. }
  476. if ((efi.systab->hdr.revision >> 16) == 0)
  477. pr_err("Warning: System table version "
  478. "%d.%02d, expected 1.00 or greater!\n",
  479. efi.systab->hdr.revision >> 16,
  480. efi.systab->hdr.revision & 0xffff);
  481. return 0;
  482. }
  483. static int __init efi_config_init(u64 tables, int nr_tables)
  484. {
  485. void *config_tables, *tablep;
  486. int i, sz;
  487. if (efi_enabled(EFI_64BIT))
  488. sz = sizeof(efi_config_table_64_t);
  489. else
  490. sz = sizeof(efi_config_table_32_t);
  491. /*
  492. * Let's see what config tables the firmware passed to us.
  493. */
  494. config_tables = early_ioremap(tables, nr_tables * sz);
  495. if (config_tables == NULL) {
  496. pr_err("Could not map Configuration table!\n");
  497. return -ENOMEM;
  498. }
  499. tablep = config_tables;
  500. pr_info("");
  501. for (i = 0; i < efi.systab->nr_tables; i++) {
  502. efi_guid_t guid;
  503. unsigned long table;
  504. if (efi_enabled(EFI_64BIT)) {
  505. u64 table64;
  506. guid = ((efi_config_table_64_t *)tablep)->guid;
  507. table64 = ((efi_config_table_64_t *)tablep)->table;
  508. table = table64;
  509. #ifdef CONFIG_X86_32
  510. if (table64 >> 32) {
  511. pr_cont("\n");
  512. pr_err("Table located above 4GB, disabling EFI.\n");
  513. early_iounmap(config_tables,
  514. efi.systab->nr_tables * sz);
  515. return -EINVAL;
  516. }
  517. #endif
  518. } else {
  519. guid = ((efi_config_table_32_t *)tablep)->guid;
  520. table = ((efi_config_table_32_t *)tablep)->table;
  521. }
  522. if (!efi_guidcmp(guid, MPS_TABLE_GUID)) {
  523. efi.mps = table;
  524. pr_cont(" MPS=0x%lx ", table);
  525. } else if (!efi_guidcmp(guid, ACPI_20_TABLE_GUID)) {
  526. efi.acpi20 = table;
  527. pr_cont(" ACPI 2.0=0x%lx ", table);
  528. } else if (!efi_guidcmp(guid, ACPI_TABLE_GUID)) {
  529. efi.acpi = table;
  530. pr_cont(" ACPI=0x%lx ", table);
  531. } else if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) {
  532. efi.smbios = table;
  533. pr_cont(" SMBIOS=0x%lx ", table);
  534. #ifdef CONFIG_X86_UV
  535. } else if (!efi_guidcmp(guid, UV_SYSTEM_TABLE_GUID)) {
  536. efi.uv_systab = table;
  537. pr_cont(" UVsystab=0x%lx ", table);
  538. #endif
  539. } else if (!efi_guidcmp(guid, HCDP_TABLE_GUID)) {
  540. efi.hcdp = table;
  541. pr_cont(" HCDP=0x%lx ", table);
  542. } else if (!efi_guidcmp(guid, UGA_IO_PROTOCOL_GUID)) {
  543. efi.uga = table;
  544. pr_cont(" UGA=0x%lx ", table);
  545. }
  546. tablep += sz;
  547. }
  548. pr_cont("\n");
  549. early_iounmap(config_tables, efi.systab->nr_tables * sz);
  550. return 0;
  551. }
  552. static int __init efi_runtime_init(void)
  553. {
  554. efi_runtime_services_t *runtime;
  555. /*
  556. * Check out the runtime services table. We need to map
  557. * the runtime services table so that we can grab the physical
  558. * address of several of the EFI runtime functions, needed to
  559. * set the firmware into virtual mode.
  560. */
  561. runtime = early_ioremap((unsigned long)efi.systab->runtime,
  562. sizeof(efi_runtime_services_t));
  563. if (!runtime) {
  564. pr_err("Could not map the runtime service table!\n");
  565. return -ENOMEM;
  566. }
  567. /*
  568. * We will only need *early* access to the following
  569. * two EFI runtime services before set_virtual_address_map
  570. * is invoked.
  571. */
  572. efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
  573. efi_phys.set_virtual_address_map =
  574. (efi_set_virtual_address_map_t *)
  575. runtime->set_virtual_address_map;
  576. /*
  577. * Make efi_get_time can be called before entering
  578. * virtual mode.
  579. */
  580. efi.get_time = phys_efi_get_time;
  581. early_iounmap(runtime, sizeof(efi_runtime_services_t));
  582. return 0;
  583. }
  584. static int __init efi_memmap_init(void)
  585. {
  586. /* Map the EFI memory map */
  587. memmap.map = early_ioremap((unsigned long)memmap.phys_map,
  588. memmap.nr_map * memmap.desc_size);
  589. if (memmap.map == NULL) {
  590. pr_err("Could not map the memory map!\n");
  591. return -ENOMEM;
  592. }
  593. memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
  594. if (add_efi_memmap)
  595. do_add_efi_memmap();
  596. return 0;
  597. }
  598. void __init efi_init(void)
  599. {
  600. efi_char16_t *c16;
  601. char vendor[100] = "unknown";
  602. int i = 0;
  603. void *tmp;
  604. #ifdef CONFIG_X86_32
  605. if (boot_params.efi_info.efi_systab_hi ||
  606. boot_params.efi_info.efi_memmap_hi) {
  607. pr_info("Table located above 4GB, disabling EFI.\n");
  608. return;
  609. }
  610. efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
  611. #else
  612. efi_phys.systab = (efi_system_table_t *)
  613. (boot_params.efi_info.efi_systab |
  614. ((__u64)boot_params.efi_info.efi_systab_hi<<32));
  615. #endif
  616. if (efi_systab_init(efi_phys.systab))
  617. return;
  618. set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
  619. /*
  620. * Show what we know for posterity
  621. */
  622. c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
  623. if (c16) {
  624. for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
  625. vendor[i] = *c16++;
  626. vendor[i] = '\0';
  627. } else
  628. pr_err("Could not map the firmware vendor!\n");
  629. early_iounmap(tmp, 2);
  630. pr_info("EFI v%u.%.02u by %s\n",
  631. efi.systab->hdr.revision >> 16,
  632. efi.systab->hdr.revision & 0xffff, vendor);
  633. if (efi_config_init(efi.systab->tables, efi.systab->nr_tables))
  634. return;
  635. set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
  636. /*
  637. * Note: We currently don't support runtime services on an EFI
  638. * that doesn't match the kernel 32/64-bit mode.
  639. */
  640. if (!efi_is_native())
  641. pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
  642. else {
  643. if (disable_runtime || efi_runtime_init())
  644. return;
  645. set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
  646. }
  647. if (efi_memmap_init())
  648. return;
  649. set_bit(EFI_MEMMAP, &x86_efi_facility);
  650. #ifdef CONFIG_X86_32
  651. if (efi_is_native()) {
  652. x86_platform.get_wallclock = efi_get_time;
  653. x86_platform.set_wallclock = efi_set_rtc_mmss;
  654. }
  655. #endif
  656. #if EFI_DEBUG
  657. print_efi_memmap();
  658. #endif
  659. }
  660. void __init efi_late_init(void)
  661. {
  662. efi_bgrt_init();
  663. }
  664. void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
  665. {
  666. u64 addr, npages;
  667. addr = md->virt_addr;
  668. npages = md->num_pages;
  669. memrange_efi_to_native(&addr, &npages);
  670. if (executable)
  671. set_memory_x(addr, npages);
  672. else
  673. set_memory_nx(addr, npages);
  674. }
  675. static void __init runtime_code_page_mkexec(void)
  676. {
  677. efi_memory_desc_t *md;
  678. void *p;
  679. /* Make EFI runtime service code area executable */
  680. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  681. md = p;
  682. if (md->type != EFI_RUNTIME_SERVICES_CODE)
  683. continue;
  684. efi_set_executable(md, true);
  685. }
  686. }
  687. /*
  688. * We can't ioremap data in EFI boot services RAM, because we've already mapped
  689. * it as RAM. So, look it up in the existing EFI memory map instead. Only
  690. * callable after efi_enter_virtual_mode and before efi_free_boot_services.
  691. */
  692. void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
  693. {
  694. void *p;
  695. if (WARN_ON(!memmap.map))
  696. return NULL;
  697. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  698. efi_memory_desc_t *md = p;
  699. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  700. u64 end = md->phys_addr + size;
  701. if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
  702. md->type != EFI_BOOT_SERVICES_CODE &&
  703. md->type != EFI_BOOT_SERVICES_DATA)
  704. continue;
  705. if (!md->virt_addr)
  706. continue;
  707. if (phys_addr >= md->phys_addr && phys_addr < end) {
  708. phys_addr += md->virt_addr - md->phys_addr;
  709. return (__force void __iomem *)(unsigned long)phys_addr;
  710. }
  711. }
  712. return NULL;
  713. }
  714. void efi_memory_uc(u64 addr, unsigned long size)
  715. {
  716. unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
  717. u64 npages;
  718. npages = round_up(size, page_shift) / page_shift;
  719. memrange_efi_to_native(&addr, &npages);
  720. set_memory_uc(addr, npages);
  721. }
  722. /*
  723. * This function will switch the EFI runtime services to virtual mode.
  724. * Essentially, look through the EFI memmap and map every region that
  725. * has the runtime attribute bit set in its memory descriptor and update
  726. * that memory descriptor with the virtual address obtained from ioremap().
  727. * This enables the runtime services to be called without having to
  728. * thunk back into physical mode for every invocation.
  729. */
  730. void __init efi_enter_virtual_mode(void)
  731. {
  732. efi_memory_desc_t *md, *prev_md = NULL;
  733. efi_status_t status;
  734. unsigned long size;
  735. u64 end, systab, end_pfn;
  736. void *p, *va, *new_memmap = NULL;
  737. int count = 0;
  738. efi.systab = NULL;
  739. /*
  740. * We don't do virtual mode, since we don't do runtime services, on
  741. * non-native EFI
  742. */
  743. if (!efi_is_native()) {
  744. efi_unmap_memmap();
  745. return;
  746. }
  747. /* Merge contiguous regions of the same type and attribute */
  748. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  749. u64 prev_size;
  750. md = p;
  751. if (!prev_md) {
  752. prev_md = md;
  753. continue;
  754. }
  755. if (prev_md->type != md->type ||
  756. prev_md->attribute != md->attribute) {
  757. prev_md = md;
  758. continue;
  759. }
  760. prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
  761. if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
  762. prev_md->num_pages += md->num_pages;
  763. md->type = EFI_RESERVED_TYPE;
  764. md->attribute = 0;
  765. continue;
  766. }
  767. prev_md = md;
  768. }
  769. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  770. md = p;
  771. if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
  772. md->type != EFI_BOOT_SERVICES_CODE &&
  773. md->type != EFI_BOOT_SERVICES_DATA)
  774. continue;
  775. size = md->num_pages << EFI_PAGE_SHIFT;
  776. end = md->phys_addr + size;
  777. end_pfn = PFN_UP(end);
  778. if (end_pfn <= max_low_pfn_mapped
  779. || (end_pfn > (1UL << (32 - PAGE_SHIFT))
  780. && end_pfn <= max_pfn_mapped)) {
  781. va = __va(md->phys_addr);
  782. if (!(md->attribute & EFI_MEMORY_WB))
  783. efi_memory_uc((u64)(unsigned long)va, size);
  784. } else
  785. va = efi_ioremap(md->phys_addr, size,
  786. md->type, md->attribute);
  787. md->virt_addr = (u64) (unsigned long) va;
  788. if (!va) {
  789. pr_err("ioremap of 0x%llX failed!\n",
  790. (unsigned long long)md->phys_addr);
  791. continue;
  792. }
  793. systab = (u64) (unsigned long) efi_phys.systab;
  794. if (md->phys_addr <= systab && systab < end) {
  795. systab += md->virt_addr - md->phys_addr;
  796. efi.systab = (efi_system_table_t *) (unsigned long) systab;
  797. }
  798. new_memmap = krealloc(new_memmap,
  799. (count + 1) * memmap.desc_size,
  800. GFP_KERNEL);
  801. memcpy(new_memmap + (count * memmap.desc_size), md,
  802. memmap.desc_size);
  803. count++;
  804. }
  805. BUG_ON(!efi.systab);
  806. status = phys_efi_set_virtual_address_map(
  807. memmap.desc_size * count,
  808. memmap.desc_size,
  809. memmap.desc_version,
  810. (efi_memory_desc_t *)__pa(new_memmap));
  811. if (status != EFI_SUCCESS) {
  812. pr_alert("Unable to switch EFI into virtual mode "
  813. "(status=%lx)!\n", status);
  814. panic("EFI call to SetVirtualAddressMap() failed!");
  815. }
  816. /*
  817. * Now that EFI is in virtual mode, update the function
  818. * pointers in the runtime service table to the new virtual addresses.
  819. *
  820. * Call EFI services through wrapper functions.
  821. */
  822. efi.runtime_version = efi_systab.hdr.revision;
  823. efi.get_time = virt_efi_get_time;
  824. efi.set_time = virt_efi_set_time;
  825. efi.get_wakeup_time = virt_efi_get_wakeup_time;
  826. efi.set_wakeup_time = virt_efi_set_wakeup_time;
  827. efi.get_variable = virt_efi_get_variable;
  828. efi.get_next_variable = virt_efi_get_next_variable;
  829. efi.set_variable = virt_efi_set_variable;
  830. efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
  831. efi.reset_system = virt_efi_reset_system;
  832. efi.set_virtual_address_map = NULL;
  833. efi.query_variable_info = virt_efi_query_variable_info;
  834. efi.update_capsule = virt_efi_update_capsule;
  835. efi.query_capsule_caps = virt_efi_query_capsule_caps;
  836. if (__supported_pte_mask & _PAGE_NX)
  837. runtime_code_page_mkexec();
  838. kfree(new_memmap);
  839. }
  840. /*
  841. * Convenience functions to obtain memory types and attributes
  842. */
  843. u32 efi_mem_type(unsigned long phys_addr)
  844. {
  845. efi_memory_desc_t *md;
  846. void *p;
  847. if (!efi_enabled(EFI_MEMMAP))
  848. return 0;
  849. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  850. md = p;
  851. if ((md->phys_addr <= phys_addr) &&
  852. (phys_addr < (md->phys_addr +
  853. (md->num_pages << EFI_PAGE_SHIFT))))
  854. return md->type;
  855. }
  856. return 0;
  857. }
  858. u64 efi_mem_attributes(unsigned long phys_addr)
  859. {
  860. efi_memory_desc_t *md;
  861. void *p;
  862. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  863. md = p;
  864. if ((md->phys_addr <= phys_addr) &&
  865. (phys_addr < (md->phys_addr +
  866. (md->num_pages << EFI_PAGE_SHIFT))))
  867. return md->attribute;
  868. }
  869. return 0;
  870. }