efi.c 25 KB

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