efi.c 24 KB

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