efi.c 25 KB

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