efi.c 28 KB

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