efi.c 21 KB

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