efi.c 25 KB

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