efi.c 21 KB

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