efi.c 25 KB

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