efi.c 26 KB

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