efi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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_unmap_memmap(void)
  367. {
  368. if (memmap.map) {
  369. early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
  370. memmap.map = NULL;
  371. }
  372. }
  373. void __init efi_free_boot_services(void)
  374. {
  375. void *p;
  376. if (!efi_native)
  377. return;
  378. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  379. efi_memory_desc_t *md = p;
  380. unsigned long long start = md->phys_addr;
  381. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  382. if (md->type != EFI_BOOT_SERVICES_CODE &&
  383. md->type != EFI_BOOT_SERVICES_DATA)
  384. continue;
  385. /* Could not reserve boot area */
  386. if (!size)
  387. continue;
  388. free_bootmem_late(start, size);
  389. }
  390. efi_unmap_memmap();
  391. }
  392. static int __init efi_systab_init(void *phys)
  393. {
  394. if (efi_64bit) {
  395. efi_system_table_64_t *systab64;
  396. u64 tmp = 0;
  397. systab64 = early_ioremap((unsigned long)phys,
  398. sizeof(*systab64));
  399. if (systab64 == NULL) {
  400. pr_err("Couldn't map the system table!\n");
  401. return -ENOMEM;
  402. }
  403. efi_systab.hdr = systab64->hdr;
  404. efi_systab.fw_vendor = systab64->fw_vendor;
  405. tmp |= systab64->fw_vendor;
  406. efi_systab.fw_revision = systab64->fw_revision;
  407. efi_systab.con_in_handle = systab64->con_in_handle;
  408. tmp |= systab64->con_in_handle;
  409. efi_systab.con_in = systab64->con_in;
  410. tmp |= systab64->con_in;
  411. efi_systab.con_out_handle = systab64->con_out_handle;
  412. tmp |= systab64->con_out_handle;
  413. efi_systab.con_out = systab64->con_out;
  414. tmp |= systab64->con_out;
  415. efi_systab.stderr_handle = systab64->stderr_handle;
  416. tmp |= systab64->stderr_handle;
  417. efi_systab.stderr = systab64->stderr;
  418. tmp |= systab64->stderr;
  419. efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
  420. tmp |= systab64->runtime;
  421. efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
  422. tmp |= systab64->boottime;
  423. efi_systab.nr_tables = systab64->nr_tables;
  424. efi_systab.tables = systab64->tables;
  425. tmp |= systab64->tables;
  426. early_iounmap(systab64, sizeof(*systab64));
  427. #ifdef CONFIG_X86_32
  428. if (tmp >> 32) {
  429. pr_err("EFI data located above 4GB, disabling EFI.\n");
  430. return -EINVAL;
  431. }
  432. #endif
  433. } else {
  434. efi_system_table_32_t *systab32;
  435. systab32 = early_ioremap((unsigned long)phys,
  436. sizeof(*systab32));
  437. if (systab32 == NULL) {
  438. pr_err("Couldn't map the system table!\n");
  439. return -ENOMEM;
  440. }
  441. efi_systab.hdr = systab32->hdr;
  442. efi_systab.fw_vendor = systab32->fw_vendor;
  443. efi_systab.fw_revision = systab32->fw_revision;
  444. efi_systab.con_in_handle = systab32->con_in_handle;
  445. efi_systab.con_in = systab32->con_in;
  446. efi_systab.con_out_handle = systab32->con_out_handle;
  447. efi_systab.con_out = systab32->con_out;
  448. efi_systab.stderr_handle = systab32->stderr_handle;
  449. efi_systab.stderr = systab32->stderr;
  450. efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
  451. efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
  452. efi_systab.nr_tables = systab32->nr_tables;
  453. efi_systab.tables = systab32->tables;
  454. early_iounmap(systab32, sizeof(*systab32));
  455. }
  456. efi.systab = &efi_systab;
  457. /*
  458. * Verify the EFI Table
  459. */
  460. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  461. pr_err("System table signature incorrect!\n");
  462. return -EINVAL;
  463. }
  464. if ((efi.systab->hdr.revision >> 16) == 0)
  465. pr_err("Warning: System table version "
  466. "%d.%02d, expected 1.00 or greater!\n",
  467. efi.systab->hdr.revision >> 16,
  468. efi.systab->hdr.revision & 0xffff);
  469. return 0;
  470. }
  471. static int __init efi_config_init(u64 tables, int nr_tables)
  472. {
  473. void *config_tables, *tablep;
  474. int i, sz;
  475. if (efi_64bit)
  476. sz = sizeof(efi_config_table_64_t);
  477. else
  478. sz = sizeof(efi_config_table_32_t);
  479. /*
  480. * Let's see what config tables the firmware passed to us.
  481. */
  482. config_tables = early_ioremap(tables, nr_tables * sz);
  483. if (config_tables == NULL) {
  484. pr_err("Could not map Configuration table!\n");
  485. return -ENOMEM;
  486. }
  487. tablep = config_tables;
  488. pr_info("");
  489. for (i = 0; i < efi.systab->nr_tables; i++) {
  490. efi_guid_t guid;
  491. unsigned long table;
  492. if (efi_64bit) {
  493. u64 table64;
  494. guid = ((efi_config_table_64_t *)tablep)->guid;
  495. table64 = ((efi_config_table_64_t *)tablep)->table;
  496. table = table64;
  497. #ifdef CONFIG_X86_32
  498. if (table64 >> 32) {
  499. pr_cont("\n");
  500. pr_err("Table located above 4GB, disabling EFI.\n");
  501. early_iounmap(config_tables,
  502. efi.systab->nr_tables * sz);
  503. return -EINVAL;
  504. }
  505. #endif
  506. } else {
  507. guid = ((efi_config_table_32_t *)tablep)->guid;
  508. table = ((efi_config_table_32_t *)tablep)->table;
  509. }
  510. if (!efi_guidcmp(guid, MPS_TABLE_GUID)) {
  511. efi.mps = table;
  512. pr_cont(" MPS=0x%lx ", table);
  513. } else if (!efi_guidcmp(guid, ACPI_20_TABLE_GUID)) {
  514. efi.acpi20 = table;
  515. pr_cont(" ACPI 2.0=0x%lx ", table);
  516. } else if (!efi_guidcmp(guid, ACPI_TABLE_GUID)) {
  517. efi.acpi = table;
  518. pr_cont(" ACPI=0x%lx ", table);
  519. } else if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) {
  520. efi.smbios = table;
  521. pr_cont(" SMBIOS=0x%lx ", table);
  522. #ifdef CONFIG_X86_UV
  523. } else if (!efi_guidcmp(guid, UV_SYSTEM_TABLE_GUID)) {
  524. efi.uv_systab = table;
  525. pr_cont(" UVsystab=0x%lx ", table);
  526. #endif
  527. } else if (!efi_guidcmp(guid, HCDP_TABLE_GUID)) {
  528. efi.hcdp = table;
  529. pr_cont(" HCDP=0x%lx ", table);
  530. } else if (!efi_guidcmp(guid, UGA_IO_PROTOCOL_GUID)) {
  531. efi.uga = table;
  532. pr_cont(" UGA=0x%lx ", table);
  533. }
  534. tablep += sz;
  535. }
  536. pr_cont("\n");
  537. early_iounmap(config_tables, efi.systab->nr_tables * sz);
  538. return 0;
  539. }
  540. static int __init efi_runtime_init(void)
  541. {
  542. efi_runtime_services_t *runtime;
  543. /*
  544. * Check out the runtime services table. We need to map
  545. * the runtime services table so that we can grab the physical
  546. * address of several of the EFI runtime functions, needed to
  547. * set the firmware into virtual mode.
  548. */
  549. runtime = early_ioremap((unsigned long)efi.systab->runtime,
  550. sizeof(efi_runtime_services_t));
  551. if (!runtime) {
  552. pr_err("Could not map the runtime service table!\n");
  553. return -ENOMEM;
  554. }
  555. /*
  556. * We will only need *early* access to the following
  557. * two EFI runtime services before set_virtual_address_map
  558. * is invoked.
  559. */
  560. efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
  561. efi_phys.set_virtual_address_map =
  562. (efi_set_virtual_address_map_t *)
  563. runtime->set_virtual_address_map;
  564. /*
  565. * Make efi_get_time can be called before entering
  566. * virtual mode.
  567. */
  568. efi.get_time = phys_efi_get_time;
  569. early_iounmap(runtime, sizeof(efi_runtime_services_t));
  570. return 0;
  571. }
  572. static int __init efi_memmap_init(void)
  573. {
  574. /* Map the EFI memory map */
  575. memmap.map = early_ioremap((unsigned long)memmap.phys_map,
  576. memmap.nr_map * memmap.desc_size);
  577. if (memmap.map == NULL) {
  578. pr_err("Could not map the memory map!\n");
  579. return -ENOMEM;
  580. }
  581. memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
  582. if (add_efi_memmap)
  583. do_add_efi_memmap();
  584. return 0;
  585. }
  586. void __init efi_init(void)
  587. {
  588. efi_char16_t *c16;
  589. char vendor[100] = "unknown";
  590. int i = 0;
  591. void *tmp;
  592. #ifdef CONFIG_X86_32
  593. if (boot_params.efi_info.efi_systab_hi ||
  594. boot_params.efi_info.efi_memmap_hi) {
  595. pr_info("Table located above 4GB, disabling EFI.\n");
  596. efi_enabled = 0;
  597. return;
  598. }
  599. efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
  600. efi_native = !efi_64bit;
  601. #else
  602. efi_phys.systab = (efi_system_table_t *)
  603. (boot_params.efi_info.efi_systab |
  604. ((__u64)boot_params.efi_info.efi_systab_hi<<32));
  605. efi_native = efi_64bit;
  606. #endif
  607. if (efi_systab_init(efi_phys.systab)) {
  608. efi_enabled = 0;
  609. return;
  610. }
  611. /*
  612. * Show what we know for posterity
  613. */
  614. c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
  615. if (c16) {
  616. for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
  617. vendor[i] = *c16++;
  618. vendor[i] = '\0';
  619. } else
  620. pr_err("Could not map the firmware vendor!\n");
  621. early_iounmap(tmp, 2);
  622. pr_info("EFI v%u.%.02u by %s\n",
  623. efi.systab->hdr.revision >> 16,
  624. efi.systab->hdr.revision & 0xffff, vendor);
  625. if (efi_config_init(efi.systab->tables, efi.systab->nr_tables)) {
  626. efi_enabled = 0;
  627. return;
  628. }
  629. /*
  630. * Note: We currently don't support runtime services on an EFI
  631. * that doesn't match the kernel 32/64-bit mode.
  632. */
  633. if (!efi_native)
  634. pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
  635. else if (efi_runtime_init()) {
  636. efi_enabled = 0;
  637. return;
  638. }
  639. if (efi_memmap_init()) {
  640. efi_enabled = 0;
  641. return;
  642. }
  643. #ifdef CONFIG_X86_32
  644. if (efi_native) {
  645. x86_platform.get_wallclock = efi_get_time;
  646. x86_platform.set_wallclock = efi_set_rtc_mmss;
  647. }
  648. #endif
  649. #if EFI_DEBUG
  650. print_efi_memmap();
  651. #endif
  652. }
  653. void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
  654. {
  655. u64 addr, npages;
  656. addr = md->virt_addr;
  657. npages = md->num_pages;
  658. memrange_efi_to_native(&addr, &npages);
  659. if (executable)
  660. set_memory_x(addr, npages);
  661. else
  662. set_memory_nx(addr, npages);
  663. }
  664. static void __init runtime_code_page_mkexec(void)
  665. {
  666. efi_memory_desc_t *md;
  667. void *p;
  668. /* Make EFI runtime service code area executable */
  669. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  670. md = p;
  671. if (md->type != EFI_RUNTIME_SERVICES_CODE)
  672. continue;
  673. efi_set_executable(md, true);
  674. }
  675. }
  676. /*
  677. * This function will switch the EFI runtime services to virtual mode.
  678. * Essentially, look through the EFI memmap and map every region that
  679. * has the runtime attribute bit set in its memory descriptor and update
  680. * that memory descriptor with the virtual address obtained from ioremap().
  681. * This enables the runtime services to be called without having to
  682. * thunk back into physical mode for every invocation.
  683. */
  684. void __init efi_enter_virtual_mode(void)
  685. {
  686. efi_memory_desc_t *md, *prev_md = NULL;
  687. efi_status_t status;
  688. unsigned long size;
  689. u64 end, systab, addr, npages, end_pfn;
  690. void *p, *va, *new_memmap = NULL;
  691. int count = 0;
  692. efi.systab = NULL;
  693. /*
  694. * We don't do virtual mode, since we don't do runtime services, on
  695. * non-native EFI
  696. */
  697. if (!efi_native) {
  698. efi_unmap_memmap();
  699. return;
  700. }
  701. /* Merge contiguous regions of the same type and attribute */
  702. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  703. u64 prev_size;
  704. md = p;
  705. if (!prev_md) {
  706. prev_md = md;
  707. continue;
  708. }
  709. if (prev_md->type != md->type ||
  710. prev_md->attribute != md->attribute) {
  711. prev_md = md;
  712. continue;
  713. }
  714. prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
  715. if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
  716. prev_md->num_pages += md->num_pages;
  717. md->type = EFI_RESERVED_TYPE;
  718. md->attribute = 0;
  719. continue;
  720. }
  721. prev_md = md;
  722. }
  723. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  724. md = p;
  725. if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
  726. md->type != EFI_BOOT_SERVICES_CODE &&
  727. md->type != EFI_BOOT_SERVICES_DATA)
  728. continue;
  729. size = md->num_pages << EFI_PAGE_SHIFT;
  730. end = md->phys_addr + size;
  731. end_pfn = PFN_UP(end);
  732. if (end_pfn <= max_low_pfn_mapped
  733. || (end_pfn > (1UL << (32 - PAGE_SHIFT))
  734. && end_pfn <= max_pfn_mapped))
  735. va = __va(md->phys_addr);
  736. else
  737. va = efi_ioremap(md->phys_addr, size, md->type);
  738. md->virt_addr = (u64) (unsigned long) va;
  739. if (!va) {
  740. pr_err("ioremap of 0x%llX failed!\n",
  741. (unsigned long long)md->phys_addr);
  742. continue;
  743. }
  744. if (!(md->attribute & EFI_MEMORY_WB)) {
  745. addr = md->virt_addr;
  746. npages = md->num_pages;
  747. memrange_efi_to_native(&addr, &npages);
  748. set_memory_uc(addr, npages);
  749. }
  750. systab = (u64) (unsigned long) efi_phys.systab;
  751. if (md->phys_addr <= systab && systab < end) {
  752. systab += md->virt_addr - md->phys_addr;
  753. efi.systab = (efi_system_table_t *) (unsigned long) systab;
  754. }
  755. new_memmap = krealloc(new_memmap,
  756. (count + 1) * memmap.desc_size,
  757. GFP_KERNEL);
  758. memcpy(new_memmap + (count * memmap.desc_size), md,
  759. memmap.desc_size);
  760. count++;
  761. }
  762. BUG_ON(!efi.systab);
  763. status = phys_efi_set_virtual_address_map(
  764. memmap.desc_size * count,
  765. memmap.desc_size,
  766. memmap.desc_version,
  767. (efi_memory_desc_t *)__pa(new_memmap));
  768. if (status != EFI_SUCCESS) {
  769. pr_alert("Unable to switch EFI into virtual mode "
  770. "(status=%lx)!\n", status);
  771. panic("EFI call to SetVirtualAddressMap() failed!");
  772. }
  773. /*
  774. * Now that EFI is in virtual mode, update the function
  775. * pointers in the runtime service table to the new virtual addresses.
  776. *
  777. * Call EFI services through wrapper functions.
  778. */
  779. efi.runtime_version = efi_systab.fw_revision;
  780. efi.get_time = virt_efi_get_time;
  781. efi.set_time = virt_efi_set_time;
  782. efi.get_wakeup_time = virt_efi_get_wakeup_time;
  783. efi.set_wakeup_time = virt_efi_set_wakeup_time;
  784. efi.get_variable = virt_efi_get_variable;
  785. efi.get_next_variable = virt_efi_get_next_variable;
  786. efi.set_variable = virt_efi_set_variable;
  787. efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
  788. efi.reset_system = virt_efi_reset_system;
  789. efi.set_virtual_address_map = NULL;
  790. efi.query_variable_info = virt_efi_query_variable_info;
  791. efi.update_capsule = virt_efi_update_capsule;
  792. efi.query_capsule_caps = virt_efi_query_capsule_caps;
  793. if (__supported_pte_mask & _PAGE_NX)
  794. runtime_code_page_mkexec();
  795. kfree(new_memmap);
  796. }
  797. /*
  798. * Convenience functions to obtain memory types and attributes
  799. */
  800. u32 efi_mem_type(unsigned long phys_addr)
  801. {
  802. efi_memory_desc_t *md;
  803. void *p;
  804. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  805. md = p;
  806. if ((md->phys_addr <= phys_addr) &&
  807. (phys_addr < (md->phys_addr +
  808. (md->num_pages << EFI_PAGE_SHIFT))))
  809. return md->type;
  810. }
  811. return 0;
  812. }
  813. u64 efi_mem_attributes(unsigned long phys_addr)
  814. {
  815. efi_memory_desc_t *md;
  816. void *p;
  817. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  818. md = p;
  819. if ((md->phys_addr <= phys_addr) &&
  820. (phys_addr < (md->phys_addr +
  821. (md->num_pages << EFI_PAGE_SHIFT))))
  822. return md->attribute;
  823. }
  824. return 0;
  825. }