efi.c 23 KB

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