efi.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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. #define EFI_MIN_RESERVE 5120
  55. #define EFI_DUMMY_GUID \
  56. EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
  57. static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
  58. struct efi_memory_map memmap;
  59. static struct efi efi_phys __initdata;
  60. static efi_system_table_t efi_systab __initdata;
  61. unsigned long x86_efi_facility;
  62. static __initdata efi_config_table_type_t arch_tables[] = {
  63. #ifdef CONFIG_X86_UV
  64. {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
  65. #endif
  66. {NULL_GUID, NULL, NULL},
  67. };
  68. u64 efi_setup; /* efi setup_data physical address */
  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 bool efi_no_storage_paranoia;
  93. static int __init setup_storage_paranoia(char *arg)
  94. {
  95. efi_no_storage_paranoia = true;
  96. return 0;
  97. }
  98. early_param("efi_no_storage_paranoia", setup_storage_paranoia);
  99. static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  100. {
  101. unsigned long flags;
  102. efi_status_t status;
  103. spin_lock_irqsave(&rtc_lock, flags);
  104. status = efi_call_virt2(get_time, tm, tc);
  105. spin_unlock_irqrestore(&rtc_lock, flags);
  106. return status;
  107. }
  108. static efi_status_t virt_efi_set_time(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_virt1(set_time, tm);
  114. spin_unlock_irqrestore(&rtc_lock, flags);
  115. return status;
  116. }
  117. static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
  118. efi_bool_t *pending,
  119. efi_time_t *tm)
  120. {
  121. unsigned long flags;
  122. efi_status_t status;
  123. spin_lock_irqsave(&rtc_lock, flags);
  124. status = efi_call_virt3(get_wakeup_time,
  125. enabled, pending, tm);
  126. spin_unlock_irqrestore(&rtc_lock, flags);
  127. return status;
  128. }
  129. static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  130. {
  131. unsigned long flags;
  132. efi_status_t status;
  133. spin_lock_irqsave(&rtc_lock, flags);
  134. status = efi_call_virt2(set_wakeup_time,
  135. enabled, tm);
  136. spin_unlock_irqrestore(&rtc_lock, flags);
  137. return status;
  138. }
  139. static efi_status_t virt_efi_get_variable(efi_char16_t *name,
  140. efi_guid_t *vendor,
  141. u32 *attr,
  142. unsigned long *data_size,
  143. void *data)
  144. {
  145. return efi_call_virt5(get_variable,
  146. name, vendor, attr,
  147. data_size, data);
  148. }
  149. static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
  150. efi_char16_t *name,
  151. efi_guid_t *vendor)
  152. {
  153. return efi_call_virt3(get_next_variable,
  154. name_size, name, vendor);
  155. }
  156. static efi_status_t virt_efi_set_variable(efi_char16_t *name,
  157. efi_guid_t *vendor,
  158. u32 attr,
  159. unsigned long data_size,
  160. void *data)
  161. {
  162. return efi_call_virt5(set_variable,
  163. name, vendor, attr,
  164. data_size, data);
  165. }
  166. static efi_status_t virt_efi_query_variable_info(u32 attr,
  167. u64 *storage_space,
  168. u64 *remaining_space,
  169. u64 *max_variable_size)
  170. {
  171. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  172. return EFI_UNSUPPORTED;
  173. return efi_call_virt4(query_variable_info, attr, storage_space,
  174. remaining_space, max_variable_size);
  175. }
  176. static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
  177. {
  178. return efi_call_virt1(get_next_high_mono_count, count);
  179. }
  180. static void virt_efi_reset_system(int reset_type,
  181. efi_status_t status,
  182. unsigned long data_size,
  183. efi_char16_t *data)
  184. {
  185. efi_call_virt4(reset_system, reset_type, status,
  186. data_size, data);
  187. }
  188. static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
  189. unsigned long count,
  190. unsigned long sg_list)
  191. {
  192. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  193. return EFI_UNSUPPORTED;
  194. return efi_call_virt3(update_capsule, capsules, count, sg_list);
  195. }
  196. static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
  197. unsigned long count,
  198. u64 *max_size,
  199. int *reset_type)
  200. {
  201. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  202. return EFI_UNSUPPORTED;
  203. return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
  204. reset_type);
  205. }
  206. static efi_status_t __init phys_efi_set_virtual_address_map(
  207. unsigned long memory_map_size,
  208. unsigned long descriptor_size,
  209. u32 descriptor_version,
  210. efi_memory_desc_t *virtual_map)
  211. {
  212. efi_status_t status;
  213. efi_call_phys_prelog();
  214. status = efi_call_phys4(efi_phys.set_virtual_address_map,
  215. memory_map_size, descriptor_size,
  216. descriptor_version, virtual_map);
  217. efi_call_phys_epilog();
  218. return status;
  219. }
  220. static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
  221. efi_time_cap_t *tc)
  222. {
  223. unsigned long flags;
  224. efi_status_t status;
  225. spin_lock_irqsave(&rtc_lock, flags);
  226. efi_call_phys_prelog();
  227. status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
  228. virt_to_phys(tc));
  229. efi_call_phys_epilog();
  230. spin_unlock_irqrestore(&rtc_lock, flags);
  231. return status;
  232. }
  233. int efi_set_rtc_mmss(const struct timespec *now)
  234. {
  235. unsigned long nowtime = now->tv_sec;
  236. efi_status_t status;
  237. efi_time_t eft;
  238. efi_time_cap_t cap;
  239. struct rtc_time tm;
  240. status = efi.get_time(&eft, &cap);
  241. if (status != EFI_SUCCESS) {
  242. pr_err("Oops: efitime: can't read time!\n");
  243. return -1;
  244. }
  245. rtc_time_to_tm(nowtime, &tm);
  246. if (!rtc_valid_tm(&tm)) {
  247. eft.year = tm.tm_year + 1900;
  248. eft.month = tm.tm_mon + 1;
  249. eft.day = tm.tm_mday;
  250. eft.minute = tm.tm_min;
  251. eft.second = tm.tm_sec;
  252. eft.nanosecond = 0;
  253. } else {
  254. printk(KERN_ERR
  255. "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
  256. __FUNCTION__, nowtime);
  257. return -1;
  258. }
  259. status = efi.set_time(&eft);
  260. if (status != EFI_SUCCESS) {
  261. pr_err("Oops: efitime: can't write time!\n");
  262. return -1;
  263. }
  264. return 0;
  265. }
  266. void efi_get_time(struct timespec *now)
  267. {
  268. efi_status_t status;
  269. efi_time_t eft;
  270. efi_time_cap_t cap;
  271. status = efi.get_time(&eft, &cap);
  272. if (status != EFI_SUCCESS)
  273. pr_err("Oops: efitime: can't read time!\n");
  274. now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
  275. eft.minute, eft.second);
  276. now->tv_nsec = 0;
  277. }
  278. /*
  279. * Tell the kernel about the EFI memory map. This might include
  280. * more than the max 128 entries that can fit in the e820 legacy
  281. * (zeropage) memory map.
  282. */
  283. static void __init do_add_efi_memmap(void)
  284. {
  285. void *p;
  286. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  287. efi_memory_desc_t *md = p;
  288. unsigned long long start = md->phys_addr;
  289. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  290. int e820_type;
  291. switch (md->type) {
  292. case EFI_LOADER_CODE:
  293. case EFI_LOADER_DATA:
  294. case EFI_BOOT_SERVICES_CODE:
  295. case EFI_BOOT_SERVICES_DATA:
  296. case EFI_CONVENTIONAL_MEMORY:
  297. if (md->attribute & EFI_MEMORY_WB)
  298. e820_type = E820_RAM;
  299. else
  300. e820_type = E820_RESERVED;
  301. break;
  302. case EFI_ACPI_RECLAIM_MEMORY:
  303. e820_type = E820_ACPI;
  304. break;
  305. case EFI_ACPI_MEMORY_NVS:
  306. e820_type = E820_NVS;
  307. break;
  308. case EFI_UNUSABLE_MEMORY:
  309. e820_type = E820_UNUSABLE;
  310. break;
  311. default:
  312. /*
  313. * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
  314. * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
  315. * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
  316. */
  317. e820_type = E820_RESERVED;
  318. break;
  319. }
  320. e820_add_region(start, size, e820_type);
  321. }
  322. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  323. }
  324. int __init efi_memblock_x86_reserve_range(void)
  325. {
  326. struct efi_info *e = &boot_params.efi_info;
  327. unsigned long pmap;
  328. #ifdef CONFIG_X86_32
  329. /* Can't handle data above 4GB at this time */
  330. if (e->efi_memmap_hi) {
  331. pr_err("Memory map is above 4GB, disabling EFI.\n");
  332. return -EINVAL;
  333. }
  334. pmap = e->efi_memmap;
  335. #else
  336. pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
  337. #endif
  338. memmap.phys_map = (void *)pmap;
  339. memmap.nr_map = e->efi_memmap_size /
  340. e->efi_memdesc_size;
  341. memmap.desc_size = e->efi_memdesc_size;
  342. memmap.desc_version = e->efi_memdesc_version;
  343. memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
  344. efi.memmap = &memmap;
  345. return 0;
  346. }
  347. static void __init print_efi_memmap(void)
  348. {
  349. #ifdef EFI_DEBUG
  350. efi_memory_desc_t *md;
  351. void *p;
  352. int i;
  353. for (p = memmap.map, i = 0;
  354. p < memmap.map_end;
  355. p += memmap.desc_size, i++) {
  356. md = p;
  357. pr_info("mem%02u: type=%u, attr=0x%llx, "
  358. "range=[0x%016llx-0x%016llx) (%lluMB)\n",
  359. i, md->type, md->attribute, md->phys_addr,
  360. md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
  361. (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
  362. }
  363. #endif /* EFI_DEBUG */
  364. }
  365. void __init efi_reserve_boot_services(void)
  366. {
  367. void *p;
  368. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  369. efi_memory_desc_t *md = p;
  370. u64 start = md->phys_addr;
  371. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  372. if (md->type != EFI_BOOT_SERVICES_CODE &&
  373. md->type != EFI_BOOT_SERVICES_DATA)
  374. continue;
  375. /* Only reserve where possible:
  376. * - Not within any already allocated areas
  377. * - Not over any memory area (really needed, if above?)
  378. * - Not within any part of the kernel
  379. * - Not the bios reserved area
  380. */
  381. if ((start + size > __pa_symbol(_text)
  382. && start <= __pa_symbol(_end)) ||
  383. !e820_all_mapped(start, start+size, E820_RAM) ||
  384. memblock_is_region_reserved(start, size)) {
  385. /* Could not reserve, skip it */
  386. md->num_pages = 0;
  387. memblock_dbg("Could not reserve boot range "
  388. "[0x%010llx-0x%010llx]\n",
  389. start, start+size-1);
  390. } else
  391. memblock_reserve(start, size);
  392. }
  393. }
  394. void __init efi_unmap_memmap(void)
  395. {
  396. clear_bit(EFI_MEMMAP, &x86_efi_facility);
  397. if (memmap.map) {
  398. early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
  399. memmap.map = NULL;
  400. }
  401. }
  402. void __init efi_free_boot_services(void)
  403. {
  404. void *p;
  405. if (!efi_is_native())
  406. return;
  407. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  408. efi_memory_desc_t *md = p;
  409. unsigned long long start = md->phys_addr;
  410. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  411. if (md->type != EFI_BOOT_SERVICES_CODE &&
  412. md->type != EFI_BOOT_SERVICES_DATA)
  413. continue;
  414. /* Could not reserve boot area */
  415. if (!size)
  416. continue;
  417. free_bootmem_late(start, size);
  418. }
  419. efi_unmap_memmap();
  420. }
  421. static int __init efi_systab_init(void *phys)
  422. {
  423. if (efi_enabled(EFI_64BIT)) {
  424. efi_system_table_64_t *systab64;
  425. struct efi_setup_data *data = NULL;
  426. u64 tmp = 0;
  427. if (efi_setup) {
  428. data = early_memremap(efi_setup, sizeof(*data));
  429. if (!data)
  430. return -ENOMEM;
  431. }
  432. systab64 = early_ioremap((unsigned long)phys,
  433. sizeof(*systab64));
  434. if (systab64 == NULL) {
  435. pr_err("Couldn't map the system table!\n");
  436. if (data)
  437. early_iounmap(data, sizeof(*data));
  438. return -ENOMEM;
  439. }
  440. efi_systab.hdr = systab64->hdr;
  441. efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
  442. systab64->fw_vendor;
  443. tmp |= data ? data->fw_vendor : systab64->fw_vendor;
  444. efi_systab.fw_revision = systab64->fw_revision;
  445. efi_systab.con_in_handle = systab64->con_in_handle;
  446. tmp |= systab64->con_in_handle;
  447. efi_systab.con_in = systab64->con_in;
  448. tmp |= systab64->con_in;
  449. efi_systab.con_out_handle = systab64->con_out_handle;
  450. tmp |= systab64->con_out_handle;
  451. efi_systab.con_out = systab64->con_out;
  452. tmp |= systab64->con_out;
  453. efi_systab.stderr_handle = systab64->stderr_handle;
  454. tmp |= systab64->stderr_handle;
  455. efi_systab.stderr = systab64->stderr;
  456. tmp |= systab64->stderr;
  457. efi_systab.runtime = data ?
  458. (void *)(unsigned long)data->runtime :
  459. (void *)(unsigned long)systab64->runtime;
  460. tmp |= data ? data->runtime : systab64->runtime;
  461. efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
  462. tmp |= systab64->boottime;
  463. efi_systab.nr_tables = systab64->nr_tables;
  464. efi_systab.tables = data ? (unsigned long)data->tables :
  465. systab64->tables;
  466. tmp |= data ? data->tables : systab64->tables;
  467. early_iounmap(systab64, sizeof(*systab64));
  468. if (data)
  469. early_iounmap(data, sizeof(*data));
  470. #ifdef CONFIG_X86_32
  471. if (tmp >> 32) {
  472. pr_err("EFI data located above 4GB, disabling EFI.\n");
  473. return -EINVAL;
  474. }
  475. #endif
  476. } else {
  477. efi_system_table_32_t *systab32;
  478. systab32 = early_ioremap((unsigned long)phys,
  479. sizeof(*systab32));
  480. if (systab32 == NULL) {
  481. pr_err("Couldn't map the system table!\n");
  482. return -ENOMEM;
  483. }
  484. efi_systab.hdr = systab32->hdr;
  485. efi_systab.fw_vendor = systab32->fw_vendor;
  486. efi_systab.fw_revision = systab32->fw_revision;
  487. efi_systab.con_in_handle = systab32->con_in_handle;
  488. efi_systab.con_in = systab32->con_in;
  489. efi_systab.con_out_handle = systab32->con_out_handle;
  490. efi_systab.con_out = systab32->con_out;
  491. efi_systab.stderr_handle = systab32->stderr_handle;
  492. efi_systab.stderr = systab32->stderr;
  493. efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
  494. efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
  495. efi_systab.nr_tables = systab32->nr_tables;
  496. efi_systab.tables = systab32->tables;
  497. early_iounmap(systab32, sizeof(*systab32));
  498. }
  499. efi.systab = &efi_systab;
  500. /*
  501. * Verify the EFI Table
  502. */
  503. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  504. pr_err("System table signature incorrect!\n");
  505. return -EINVAL;
  506. }
  507. if ((efi.systab->hdr.revision >> 16) == 0)
  508. pr_err("Warning: System table version "
  509. "%d.%02d, expected 1.00 or greater!\n",
  510. efi.systab->hdr.revision >> 16,
  511. efi.systab->hdr.revision & 0xffff);
  512. return 0;
  513. }
  514. static int __init efi_runtime_init(void)
  515. {
  516. efi_runtime_services_t *runtime;
  517. /*
  518. * Check out the runtime services table. We need to map
  519. * the runtime services table so that we can grab the physical
  520. * address of several of the EFI runtime functions, needed to
  521. * set the firmware into virtual mode.
  522. */
  523. runtime = early_ioremap((unsigned long)efi.systab->runtime,
  524. sizeof(efi_runtime_services_t));
  525. if (!runtime) {
  526. pr_err("Could not map the runtime service table!\n");
  527. return -ENOMEM;
  528. }
  529. /*
  530. * We will only need *early* access to the following
  531. * two EFI runtime services before set_virtual_address_map
  532. * is invoked.
  533. */
  534. efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
  535. efi_phys.set_virtual_address_map =
  536. (efi_set_virtual_address_map_t *)
  537. runtime->set_virtual_address_map;
  538. /*
  539. * Make efi_get_time can be called before entering
  540. * virtual mode.
  541. */
  542. efi.get_time = phys_efi_get_time;
  543. early_iounmap(runtime, sizeof(efi_runtime_services_t));
  544. return 0;
  545. }
  546. static int __init efi_memmap_init(void)
  547. {
  548. /* Map the EFI memory map */
  549. memmap.map = early_ioremap((unsigned long)memmap.phys_map,
  550. memmap.nr_map * memmap.desc_size);
  551. if (memmap.map == NULL) {
  552. pr_err("Could not map the memory map!\n");
  553. return -ENOMEM;
  554. }
  555. memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
  556. if (add_efi_memmap)
  557. do_add_efi_memmap();
  558. return 0;
  559. }
  560. /*
  561. * A number of config table entries get remapped to virtual addresses
  562. * after entering EFI virtual mode. However, the kexec kernel requires
  563. * their physical addresses therefore we pass them via setup_data and
  564. * correct those entries to their respective physical addresses here.
  565. *
  566. * Currently only handles smbios which is necessary for some firmware
  567. * implementation.
  568. */
  569. static int __init efi_reuse_config(u64 tables, int nr_tables)
  570. {
  571. int i, sz, ret = 0;
  572. void *p, *tablep;
  573. struct efi_setup_data *data;
  574. if (!efi_setup)
  575. return 0;
  576. if (!efi_enabled(EFI_64BIT))
  577. return 0;
  578. data = early_memremap(efi_setup, sizeof(*data));
  579. if (!data) {
  580. ret = -ENOMEM;
  581. goto out;
  582. }
  583. if (!data->smbios)
  584. goto out_memremap;
  585. sz = sizeof(efi_config_table_64_t);
  586. p = tablep = early_memremap(tables, nr_tables * sz);
  587. if (!p) {
  588. pr_err("Could not map Configuration table!\n");
  589. ret = -ENOMEM;
  590. goto out_memremap;
  591. }
  592. for (i = 0; i < efi.systab->nr_tables; i++) {
  593. efi_guid_t guid;
  594. guid = ((efi_config_table_64_t *)p)->guid;
  595. if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
  596. ((efi_config_table_64_t *)p)->table = data->smbios;
  597. p += sz;
  598. }
  599. early_iounmap(tablep, nr_tables * sz);
  600. out_memremap:
  601. early_iounmap(data, sizeof(*data));
  602. out:
  603. return ret;
  604. }
  605. void __init efi_init(void)
  606. {
  607. efi_char16_t *c16;
  608. char vendor[100] = "unknown";
  609. int i = 0;
  610. void *tmp;
  611. #ifdef CONFIG_X86_32
  612. if (boot_params.efi_info.efi_systab_hi ||
  613. boot_params.efi_info.efi_memmap_hi) {
  614. pr_info("Table located above 4GB, disabling EFI.\n");
  615. return;
  616. }
  617. efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
  618. #else
  619. efi_phys.systab = (efi_system_table_t *)
  620. (boot_params.efi_info.efi_systab |
  621. ((__u64)boot_params.efi_info.efi_systab_hi<<32));
  622. #endif
  623. if (efi_systab_init(efi_phys.systab))
  624. return;
  625. set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
  626. efi.config_table = (unsigned long)efi.systab->tables;
  627. efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
  628. efi.runtime = (unsigned long)efi.systab->runtime;
  629. /*
  630. * Show what we know for posterity
  631. */
  632. c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
  633. if (c16) {
  634. for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
  635. vendor[i] = *c16++;
  636. vendor[i] = '\0';
  637. } else
  638. pr_err("Could not map the firmware vendor!\n");
  639. early_iounmap(tmp, 2);
  640. pr_info("EFI v%u.%.02u by %s\n",
  641. efi.systab->hdr.revision >> 16,
  642. efi.systab->hdr.revision & 0xffff, vendor);
  643. if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
  644. return;
  645. if (efi_config_init(arch_tables))
  646. return;
  647. set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
  648. /*
  649. * Note: We currently don't support runtime services on an EFI
  650. * that doesn't match the kernel 32/64-bit mode.
  651. */
  652. if (!efi_is_native())
  653. pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
  654. else {
  655. if (disable_runtime || efi_runtime_init())
  656. return;
  657. set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
  658. }
  659. if (efi_memmap_init())
  660. return;
  661. set_bit(EFI_MEMMAP, &x86_efi_facility);
  662. print_efi_memmap();
  663. }
  664. void __init efi_late_init(void)
  665. {
  666. efi_bgrt_init();
  667. }
  668. void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
  669. {
  670. u64 addr, npages;
  671. addr = md->virt_addr;
  672. npages = md->num_pages;
  673. memrange_efi_to_native(&addr, &npages);
  674. if (executable)
  675. set_memory_x(addr, npages);
  676. else
  677. set_memory_nx(addr, npages);
  678. }
  679. void __init runtime_code_page_mkexec(void)
  680. {
  681. efi_memory_desc_t *md;
  682. void *p;
  683. /* Make EFI runtime service code area executable */
  684. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  685. md = p;
  686. if (md->type != EFI_RUNTIME_SERVICES_CODE)
  687. continue;
  688. efi_set_executable(md, true);
  689. }
  690. }
  691. void efi_memory_uc(u64 addr, unsigned long size)
  692. {
  693. unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
  694. u64 npages;
  695. npages = round_up(size, page_shift) / page_shift;
  696. memrange_efi_to_native(&addr, &npages);
  697. set_memory_uc(addr, npages);
  698. }
  699. void __init old_map_region(efi_memory_desc_t *md)
  700. {
  701. u64 start_pfn, end_pfn, end;
  702. unsigned long size;
  703. void *va;
  704. start_pfn = PFN_DOWN(md->phys_addr);
  705. size = md->num_pages << PAGE_SHIFT;
  706. end = md->phys_addr + size;
  707. end_pfn = PFN_UP(end);
  708. if (pfn_range_is_mapped(start_pfn, end_pfn)) {
  709. va = __va(md->phys_addr);
  710. if (!(md->attribute & EFI_MEMORY_WB))
  711. efi_memory_uc((u64)(unsigned long)va, size);
  712. } else
  713. va = efi_ioremap(md->phys_addr, size,
  714. md->type, md->attribute);
  715. md->virt_addr = (u64) (unsigned long) va;
  716. if (!va)
  717. pr_err("ioremap of 0x%llX failed!\n",
  718. (unsigned long long)md->phys_addr);
  719. }
  720. /* Merge contiguous regions of the same type and attribute */
  721. static void __init efi_merge_regions(void)
  722. {
  723. void *p;
  724. efi_memory_desc_t *md, *prev_md = NULL;
  725. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  726. u64 prev_size;
  727. md = p;
  728. if (!prev_md) {
  729. prev_md = md;
  730. continue;
  731. }
  732. if (prev_md->type != md->type ||
  733. prev_md->attribute != md->attribute) {
  734. prev_md = md;
  735. continue;
  736. }
  737. prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
  738. if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
  739. prev_md->num_pages += md->num_pages;
  740. md->type = EFI_RESERVED_TYPE;
  741. md->attribute = 0;
  742. continue;
  743. }
  744. prev_md = md;
  745. }
  746. }
  747. static void __init get_systab_virt_addr(efi_memory_desc_t *md)
  748. {
  749. unsigned long size;
  750. u64 end, systab;
  751. size = md->num_pages << EFI_PAGE_SHIFT;
  752. end = md->phys_addr + size;
  753. systab = (u64)(unsigned long)efi_phys.systab;
  754. if (md->phys_addr <= systab && systab < end) {
  755. systab += md->virt_addr - md->phys_addr;
  756. efi.systab = (efi_system_table_t *)(unsigned long)systab;
  757. }
  758. }
  759. static int __init save_runtime_map(void)
  760. {
  761. efi_memory_desc_t *md;
  762. void *tmp, *p, *q = NULL;
  763. int count = 0;
  764. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  765. md = p;
  766. if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
  767. (md->type == EFI_BOOT_SERVICES_CODE) ||
  768. (md->type == EFI_BOOT_SERVICES_DATA))
  769. continue;
  770. tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
  771. if (!tmp)
  772. goto out;
  773. q = tmp;
  774. memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
  775. count++;
  776. }
  777. efi_runtime_map_setup(q, count, memmap.desc_size);
  778. return 0;
  779. out:
  780. kfree(q);
  781. return -ENOMEM;
  782. }
  783. /*
  784. * Map efi regions which were passed via setup_data. The virt_addr is a fixed
  785. * addr which was used in first kernel of a kexec boot.
  786. */
  787. static void __init efi_map_regions_fixed(void)
  788. {
  789. void *p;
  790. efi_memory_desc_t *md;
  791. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  792. md = p;
  793. efi_map_region_fixed(md); /* FIXME: add error handling */
  794. get_systab_virt_addr(md);
  795. }
  796. }
  797. /*
  798. * Map efi memory ranges for runtime serivce and update new_memmap with virtual
  799. * addresses.
  800. */
  801. static void * __init efi_map_regions(int *count)
  802. {
  803. efi_memory_desc_t *md;
  804. void *p, *tmp, *new_memmap = NULL;
  805. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  806. md = p;
  807. if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
  808. #ifdef CONFIG_X86_64
  809. if (md->type != EFI_BOOT_SERVICES_CODE &&
  810. md->type != EFI_BOOT_SERVICES_DATA)
  811. #endif
  812. continue;
  813. }
  814. efi_map_region(md);
  815. get_systab_virt_addr(md);
  816. tmp = krealloc(new_memmap, (*count + 1) * memmap.desc_size,
  817. GFP_KERNEL);
  818. if (!tmp)
  819. goto out;
  820. new_memmap = tmp;
  821. memcpy(new_memmap + (*count * memmap.desc_size), md,
  822. memmap.desc_size);
  823. (*count)++;
  824. }
  825. return new_memmap;
  826. out:
  827. kfree(new_memmap);
  828. return NULL;
  829. }
  830. /*
  831. * This function will switch the EFI runtime services to virtual mode.
  832. * Essentially, we look through the EFI memmap and map every region that
  833. * has the runtime attribute bit set in its memory descriptor into the
  834. * ->trampoline_pgd page table using a top-down VA allocation scheme.
  835. *
  836. * The old method which used to update that memory descriptor with the
  837. * virtual address obtained from ioremap() is still supported when the
  838. * kernel is booted with efi=old_map on its command line. Same old
  839. * method enabled the runtime services to be called without having to
  840. * thunk back into physical mode for every invocation.
  841. *
  842. * The new method does a pagetable switch in a preemption-safe manner
  843. * so that we're in a different address space when calling a runtime
  844. * function. For function arguments passing we do copy the PGDs of the
  845. * kernel page table into ->trampoline_pgd prior to each call.
  846. *
  847. * Specially for kexec boot, efi runtime maps in previous kernel should
  848. * be passed in via setup_data. In that case runtime ranges will be mapped
  849. * to the same virtual addresses as the first kernel.
  850. */
  851. void __init efi_enter_virtual_mode(void)
  852. {
  853. efi_status_t status;
  854. void *new_memmap = NULL;
  855. int err, count = 0;
  856. efi.systab = NULL;
  857. /*
  858. * We don't do virtual mode, since we don't do runtime services, on
  859. * non-native EFI
  860. */
  861. if (!efi_is_native()) {
  862. efi_unmap_memmap();
  863. return;
  864. }
  865. if (efi_setup) {
  866. efi_map_regions_fixed();
  867. } else {
  868. efi_merge_regions();
  869. new_memmap = efi_map_regions(&count);
  870. if (!new_memmap) {
  871. pr_err("Error reallocating memory, EFI runtime non-functional!\n");
  872. return;
  873. }
  874. }
  875. err = save_runtime_map();
  876. if (err)
  877. pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
  878. BUG_ON(!efi.systab);
  879. efi_setup_page_tables();
  880. efi_sync_low_kernel_mappings();
  881. if (!efi_setup) {
  882. status = phys_efi_set_virtual_address_map(
  883. memmap.desc_size * count,
  884. memmap.desc_size,
  885. memmap.desc_version,
  886. (efi_memory_desc_t *)__pa(new_memmap));
  887. if (status != EFI_SUCCESS) {
  888. pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
  889. status);
  890. panic("EFI call to SetVirtualAddressMap() failed!");
  891. }
  892. }
  893. /*
  894. * Now that EFI is in virtual mode, update the function
  895. * pointers in the runtime service table to the new virtual addresses.
  896. *
  897. * Call EFI services through wrapper functions.
  898. */
  899. efi.runtime_version = efi_systab.hdr.revision;
  900. efi.get_time = virt_efi_get_time;
  901. efi.set_time = virt_efi_set_time;
  902. efi.get_wakeup_time = virt_efi_get_wakeup_time;
  903. efi.set_wakeup_time = virt_efi_set_wakeup_time;
  904. efi.get_variable = virt_efi_get_variable;
  905. efi.get_next_variable = virt_efi_get_next_variable;
  906. efi.set_variable = virt_efi_set_variable;
  907. efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
  908. efi.reset_system = virt_efi_reset_system;
  909. efi.set_virtual_address_map = NULL;
  910. efi.query_variable_info = virt_efi_query_variable_info;
  911. efi.update_capsule = virt_efi_update_capsule;
  912. efi.query_capsule_caps = virt_efi_query_capsule_caps;
  913. efi_runtime_mkexec();
  914. kfree(new_memmap);
  915. /* clean DUMMY object */
  916. efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
  917. EFI_VARIABLE_NON_VOLATILE |
  918. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  919. EFI_VARIABLE_RUNTIME_ACCESS,
  920. 0, NULL);
  921. }
  922. /*
  923. * Convenience functions to obtain memory types and attributes
  924. */
  925. u32 efi_mem_type(unsigned long phys_addr)
  926. {
  927. efi_memory_desc_t *md;
  928. void *p;
  929. if (!efi_enabled(EFI_MEMMAP))
  930. return 0;
  931. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  932. md = p;
  933. if ((md->phys_addr <= phys_addr) &&
  934. (phys_addr < (md->phys_addr +
  935. (md->num_pages << EFI_PAGE_SHIFT))))
  936. return md->type;
  937. }
  938. return 0;
  939. }
  940. u64 efi_mem_attributes(unsigned long phys_addr)
  941. {
  942. efi_memory_desc_t *md;
  943. void *p;
  944. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  945. md = p;
  946. if ((md->phys_addr <= phys_addr) &&
  947. (phys_addr < (md->phys_addr +
  948. (md->num_pages << EFI_PAGE_SHIFT))))
  949. return md->attribute;
  950. }
  951. return 0;
  952. }
  953. /*
  954. * Some firmware has serious problems when using more than 50% of the EFI
  955. * variable store, i.e. it triggers bugs that can brick machines. Ensure that
  956. * we never use more than this safe limit.
  957. *
  958. * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
  959. * store.
  960. */
  961. efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
  962. {
  963. efi_status_t status;
  964. u64 storage_size, remaining_size, max_size;
  965. if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
  966. return 0;
  967. status = efi.query_variable_info(attributes, &storage_size,
  968. &remaining_size, &max_size);
  969. if (status != EFI_SUCCESS)
  970. return status;
  971. /*
  972. * Some firmware implementations refuse to boot if there's insufficient
  973. * space in the variable store. We account for that by refusing the
  974. * write if permitting it would reduce the available space to under
  975. * 5KB. This figure was provided by Samsung, so should be safe.
  976. */
  977. if ((remaining_size - size < EFI_MIN_RESERVE) &&
  978. !efi_no_storage_paranoia) {
  979. /*
  980. * Triggering garbage collection may require that the firmware
  981. * generate a real EFI_OUT_OF_RESOURCES error. We can force
  982. * that by attempting to use more space than is available.
  983. */
  984. unsigned long dummy_size = remaining_size + 1024;
  985. void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
  986. if (!dummy)
  987. return EFI_OUT_OF_RESOURCES;
  988. status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
  989. EFI_VARIABLE_NON_VOLATILE |
  990. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  991. EFI_VARIABLE_RUNTIME_ACCESS,
  992. dummy_size, dummy);
  993. if (status == EFI_SUCCESS) {
  994. /*
  995. * This should have failed, so if it didn't make sure
  996. * that we delete it...
  997. */
  998. efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
  999. EFI_VARIABLE_NON_VOLATILE |
  1000. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  1001. EFI_VARIABLE_RUNTIME_ACCESS,
  1002. 0, dummy);
  1003. }
  1004. kfree(dummy);
  1005. /*
  1006. * The runtime code may now have triggered a garbage collection
  1007. * run, so check the variable info again
  1008. */
  1009. status = efi.query_variable_info(attributes, &storage_size,
  1010. &remaining_size, &max_size);
  1011. if (status != EFI_SUCCESS)
  1012. return status;
  1013. /*
  1014. * There still isn't enough room, so return an error
  1015. */
  1016. if (remaining_size - size < EFI_MIN_RESERVE)
  1017. return EFI_OUT_OF_RESOURCES;
  1018. }
  1019. return EFI_SUCCESS;
  1020. }
  1021. EXPORT_SYMBOL_GPL(efi_query_variable_store);
  1022. static int __init parse_efi_cmdline(char *str)
  1023. {
  1024. if (*str == '=')
  1025. str++;
  1026. if (!strncmp(str, "old_map", 7))
  1027. set_bit(EFI_OLD_MEMMAP, &x86_efi_facility);
  1028. return 0;
  1029. }
  1030. early_param("efi", parse_efi_cmdline);
  1031. void __init efi_apply_memmap_quirks(void)
  1032. {
  1033. /*
  1034. * Once setup is done earlier, unmap the EFI memory map on mismatched
  1035. * firmware/kernel architectures since there is no support for runtime
  1036. * services.
  1037. */
  1038. if (!efi_is_native()) {
  1039. pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n");
  1040. efi_unmap_memmap();
  1041. }
  1042. /*
  1043. * UV doesn't support the new EFI pagetable mapping yet.
  1044. */
  1045. if (is_uv_system())
  1046. set_bit(EFI_OLD_MEMMAP, &x86_efi_facility);
  1047. }