efi.c 24 KB

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