efi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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, tm, tc);
  208. efi_call_phys_epilog();
  209. spin_unlock_irqrestore(&rtc_lock, flags);
  210. return status;
  211. }
  212. int efi_set_rtc_mmss(unsigned long nowtime)
  213. {
  214. int real_seconds, real_minutes;
  215. efi_status_t status;
  216. efi_time_t eft;
  217. efi_time_cap_t cap;
  218. status = efi.get_time(&eft, &cap);
  219. if (status != EFI_SUCCESS) {
  220. printk(KERN_ERR "Oops: efitime: can't read time!\n");
  221. return -1;
  222. }
  223. real_seconds = nowtime % 60;
  224. real_minutes = nowtime / 60;
  225. if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
  226. real_minutes += 30;
  227. real_minutes %= 60;
  228. eft.minute = real_minutes;
  229. eft.second = real_seconds;
  230. status = efi.set_time(&eft);
  231. if (status != EFI_SUCCESS) {
  232. printk(KERN_ERR "Oops: efitime: can't write time!\n");
  233. return -1;
  234. }
  235. return 0;
  236. }
  237. unsigned long efi_get_time(void)
  238. {
  239. efi_status_t status;
  240. efi_time_t eft;
  241. efi_time_cap_t cap;
  242. status = efi.get_time(&eft, &cap);
  243. if (status != EFI_SUCCESS)
  244. printk(KERN_ERR "Oops: efitime: can't read time!\n");
  245. return mktime(eft.year, eft.month, eft.day, eft.hour,
  246. eft.minute, eft.second);
  247. }
  248. /*
  249. * Tell the kernel about the EFI memory map. This might include
  250. * more than the max 128 entries that can fit in the e820 legacy
  251. * (zeropage) memory map.
  252. */
  253. static void __init do_add_efi_memmap(void)
  254. {
  255. void *p;
  256. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  257. efi_memory_desc_t *md = p;
  258. unsigned long long start = md->phys_addr;
  259. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  260. int e820_type;
  261. switch (md->type) {
  262. case EFI_LOADER_CODE:
  263. case EFI_LOADER_DATA:
  264. case EFI_BOOT_SERVICES_CODE:
  265. case EFI_BOOT_SERVICES_DATA:
  266. case EFI_CONVENTIONAL_MEMORY:
  267. if (md->attribute & EFI_MEMORY_WB)
  268. e820_type = E820_RAM;
  269. else
  270. e820_type = E820_RESERVED;
  271. break;
  272. case EFI_ACPI_RECLAIM_MEMORY:
  273. e820_type = E820_ACPI;
  274. break;
  275. case EFI_ACPI_MEMORY_NVS:
  276. e820_type = E820_NVS;
  277. break;
  278. case EFI_UNUSABLE_MEMORY:
  279. e820_type = E820_UNUSABLE;
  280. break;
  281. default:
  282. /*
  283. * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
  284. * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
  285. * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
  286. */
  287. e820_type = E820_RESERVED;
  288. break;
  289. }
  290. e820_add_region(start, size, e820_type);
  291. }
  292. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  293. }
  294. void __init efi_memblock_x86_reserve_range(void)
  295. {
  296. unsigned long pmap;
  297. #ifdef CONFIG_X86_32
  298. pmap = boot_params.efi_info.efi_memmap;
  299. #else
  300. pmap = (boot_params.efi_info.efi_memmap |
  301. ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
  302. #endif
  303. memmap.phys_map = (void *)pmap;
  304. memmap.nr_map = boot_params.efi_info.efi_memmap_size /
  305. boot_params.efi_info.efi_memdesc_size;
  306. memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
  307. memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
  308. memblock_x86_reserve_range(pmap, pmap + memmap.nr_map * memmap.desc_size,
  309. "EFI memmap");
  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_x86_check_reserved_size(&start, &size,
  349. 1<<EFI_PAGE_SHIFT)) {
  350. /* Could not reserve, skip it */
  351. md->num_pages = 0;
  352. memblock_dbg(PFX "Could not reserve boot range "
  353. "[0x%010llx-0x%010llx]\n",
  354. start, start+size-1);
  355. } else
  356. memblock_x86_reserve_range(start, start+size,
  357. "EFI Boot");
  358. }
  359. }
  360. static void __init efi_free_boot_services(void)
  361. {
  362. void *p;
  363. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  364. efi_memory_desc_t *md = p;
  365. unsigned long long start = md->phys_addr;
  366. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  367. if (md->type != EFI_BOOT_SERVICES_CODE &&
  368. md->type != EFI_BOOT_SERVICES_DATA)
  369. continue;
  370. /* Could not reserve boot area */
  371. if (!size)
  372. continue;
  373. free_bootmem_late(start, size);
  374. }
  375. }
  376. void __init efi_init(void)
  377. {
  378. efi_config_table_t *config_tables;
  379. efi_runtime_services_t *runtime;
  380. efi_char16_t *c16;
  381. char vendor[100] = "unknown";
  382. int i = 0;
  383. void *tmp;
  384. #ifdef CONFIG_X86_32
  385. efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
  386. #else
  387. efi_phys.systab = (efi_system_table_t *)
  388. (boot_params.efi_info.efi_systab |
  389. ((__u64)boot_params.efi_info.efi_systab_hi<<32));
  390. #endif
  391. efi.systab = early_ioremap((unsigned long)efi_phys.systab,
  392. sizeof(efi_system_table_t));
  393. if (efi.systab == NULL)
  394. printk(KERN_ERR "Couldn't map the EFI system table!\n");
  395. memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
  396. early_iounmap(efi.systab, sizeof(efi_system_table_t));
  397. efi.systab = &efi_systab;
  398. /*
  399. * Verify the EFI Table
  400. */
  401. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  402. printk(KERN_ERR "EFI system table signature incorrect!\n");
  403. if ((efi.systab->hdr.revision >> 16) == 0)
  404. printk(KERN_ERR "Warning: EFI system table version "
  405. "%d.%02d, expected 1.00 or greater!\n",
  406. efi.systab->hdr.revision >> 16,
  407. efi.systab->hdr.revision & 0xffff);
  408. /*
  409. * Show what we know for posterity
  410. */
  411. c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
  412. if (c16) {
  413. for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
  414. vendor[i] = *c16++;
  415. vendor[i] = '\0';
  416. } else
  417. printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
  418. early_iounmap(tmp, 2);
  419. printk(KERN_INFO "EFI v%u.%.02u by %s\n",
  420. efi.systab->hdr.revision >> 16,
  421. efi.systab->hdr.revision & 0xffff, vendor);
  422. /*
  423. * Let's see what config tables the firmware passed to us.
  424. */
  425. config_tables = early_ioremap(
  426. efi.systab->tables,
  427. efi.systab->nr_tables * sizeof(efi_config_table_t));
  428. if (config_tables == NULL)
  429. printk(KERN_ERR "Could not map EFI Configuration Table!\n");
  430. printk(KERN_INFO);
  431. for (i = 0; i < efi.systab->nr_tables; i++) {
  432. if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
  433. efi.mps = config_tables[i].table;
  434. printk(" MPS=0x%lx ", config_tables[i].table);
  435. } else if (!efi_guidcmp(config_tables[i].guid,
  436. ACPI_20_TABLE_GUID)) {
  437. efi.acpi20 = config_tables[i].table;
  438. printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
  439. } else if (!efi_guidcmp(config_tables[i].guid,
  440. ACPI_TABLE_GUID)) {
  441. efi.acpi = config_tables[i].table;
  442. printk(" ACPI=0x%lx ", config_tables[i].table);
  443. } else if (!efi_guidcmp(config_tables[i].guid,
  444. SMBIOS_TABLE_GUID)) {
  445. efi.smbios = config_tables[i].table;
  446. printk(" SMBIOS=0x%lx ", config_tables[i].table);
  447. #ifdef CONFIG_X86_UV
  448. } else if (!efi_guidcmp(config_tables[i].guid,
  449. UV_SYSTEM_TABLE_GUID)) {
  450. efi.uv_systab = config_tables[i].table;
  451. printk(" UVsystab=0x%lx ", config_tables[i].table);
  452. #endif
  453. } else if (!efi_guidcmp(config_tables[i].guid,
  454. HCDP_TABLE_GUID)) {
  455. efi.hcdp = config_tables[i].table;
  456. printk(" HCDP=0x%lx ", config_tables[i].table);
  457. } else if (!efi_guidcmp(config_tables[i].guid,
  458. UGA_IO_PROTOCOL_GUID)) {
  459. efi.uga = config_tables[i].table;
  460. printk(" UGA=0x%lx ", config_tables[i].table);
  461. }
  462. }
  463. printk("\n");
  464. early_iounmap(config_tables,
  465. efi.systab->nr_tables * sizeof(efi_config_table_t));
  466. /*
  467. * Check out the runtime services table. We need to map
  468. * the runtime services table so that we can grab the physical
  469. * address of several of the EFI runtime functions, needed to
  470. * set the firmware into virtual mode.
  471. */
  472. runtime = early_ioremap((unsigned long)efi.systab->runtime,
  473. sizeof(efi_runtime_services_t));
  474. if (runtime != NULL) {
  475. /*
  476. * We will only need *early* access to the following
  477. * two EFI runtime services before set_virtual_address_map
  478. * is invoked.
  479. */
  480. efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
  481. efi_phys.set_virtual_address_map =
  482. (efi_set_virtual_address_map_t *)
  483. runtime->set_virtual_address_map;
  484. /*
  485. * Make efi_get_time can be called before entering
  486. * virtual mode.
  487. */
  488. efi.get_time = phys_efi_get_time;
  489. } else
  490. printk(KERN_ERR "Could not map the EFI runtime service "
  491. "table!\n");
  492. early_iounmap(runtime, sizeof(efi_runtime_services_t));
  493. /* Map the EFI memory map */
  494. memmap.map = early_ioremap((unsigned long)memmap.phys_map,
  495. memmap.nr_map * memmap.desc_size);
  496. if (memmap.map == NULL)
  497. printk(KERN_ERR "Could not map the EFI memory map!\n");
  498. memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
  499. if (memmap.desc_size != sizeof(efi_memory_desc_t))
  500. printk(KERN_WARNING
  501. "Kernel-defined memdesc doesn't match the one from EFI!\n");
  502. if (add_efi_memmap)
  503. do_add_efi_memmap();
  504. #ifdef CONFIG_X86_32
  505. x86_platform.get_wallclock = efi_get_time;
  506. x86_platform.set_wallclock = efi_set_rtc_mmss;
  507. #endif
  508. #if EFI_DEBUG
  509. print_efi_memmap();
  510. #endif
  511. }
  512. void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
  513. {
  514. u64 addr, npages;
  515. addr = md->virt_addr;
  516. npages = md->num_pages;
  517. memrange_efi_to_native(&addr, &npages);
  518. if (executable)
  519. set_memory_x(addr, npages);
  520. else
  521. set_memory_nx(addr, npages);
  522. }
  523. static void __init runtime_code_page_mkexec(void)
  524. {
  525. efi_memory_desc_t *md;
  526. void *p;
  527. /* Make EFI runtime service code area executable */
  528. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  529. md = p;
  530. if (md->type != EFI_RUNTIME_SERVICES_CODE)
  531. continue;
  532. efi_set_executable(md, true);
  533. }
  534. }
  535. /*
  536. * This function will switch the EFI runtime services to virtual mode.
  537. * Essentially, look through the EFI memmap and map every region that
  538. * has the runtime attribute bit set in its memory descriptor and update
  539. * that memory descriptor with the virtual address obtained from ioremap().
  540. * This enables the runtime services to be called without having to
  541. * thunk back into physical mode for every invocation.
  542. */
  543. void __init efi_enter_virtual_mode(void)
  544. {
  545. efi_memory_desc_t *md, *prev_md = NULL;
  546. efi_status_t status;
  547. unsigned long size;
  548. u64 end, systab, addr, npages, end_pfn;
  549. void *p, *va, *new_memmap = NULL;
  550. int count = 0;
  551. efi.systab = NULL;
  552. /* Merge contiguous regions of the same type and attribute */
  553. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  554. u64 prev_size;
  555. md = p;
  556. if (!prev_md) {
  557. prev_md = md;
  558. continue;
  559. }
  560. if (prev_md->type != md->type ||
  561. prev_md->attribute != md->attribute) {
  562. prev_md = md;
  563. continue;
  564. }
  565. prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
  566. if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
  567. prev_md->num_pages += md->num_pages;
  568. md->type = EFI_RESERVED_TYPE;
  569. md->attribute = 0;
  570. continue;
  571. }
  572. prev_md = md;
  573. }
  574. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  575. md = p;
  576. if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
  577. md->type != EFI_BOOT_SERVICES_CODE &&
  578. md->type != EFI_BOOT_SERVICES_DATA)
  579. continue;
  580. size = md->num_pages << EFI_PAGE_SHIFT;
  581. end = md->phys_addr + size;
  582. end_pfn = PFN_UP(end);
  583. if (end_pfn <= max_low_pfn_mapped
  584. || (end_pfn > (1UL << (32 - PAGE_SHIFT))
  585. && end_pfn <= max_pfn_mapped))
  586. va = __va(md->phys_addr);
  587. else
  588. va = efi_ioremap(md->phys_addr, size, md->type);
  589. md->virt_addr = (u64) (unsigned long) va;
  590. if (!va) {
  591. printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
  592. (unsigned long long)md->phys_addr);
  593. continue;
  594. }
  595. if (!(md->attribute & EFI_MEMORY_WB)) {
  596. addr = md->virt_addr;
  597. npages = md->num_pages;
  598. memrange_efi_to_native(&addr, &npages);
  599. set_memory_uc(addr, npages);
  600. }
  601. systab = (u64) (unsigned long) efi_phys.systab;
  602. if (md->phys_addr <= systab && systab < end) {
  603. systab += md->virt_addr - md->phys_addr;
  604. efi.systab = (efi_system_table_t *) (unsigned long) systab;
  605. }
  606. new_memmap = krealloc(new_memmap,
  607. (count + 1) * memmap.desc_size,
  608. GFP_KERNEL);
  609. memcpy(new_memmap + (count * memmap.desc_size), md,
  610. memmap.desc_size);
  611. count++;
  612. }
  613. BUG_ON(!efi.systab);
  614. status = phys_efi_set_virtual_address_map(
  615. memmap.desc_size * count,
  616. memmap.desc_size,
  617. memmap.desc_version,
  618. (efi_memory_desc_t *)__pa(new_memmap));
  619. if (status != EFI_SUCCESS) {
  620. printk(KERN_ALERT "Unable to switch EFI into virtual mode "
  621. "(status=%lx)!\n", status);
  622. panic("EFI call to SetVirtualAddressMap() failed!");
  623. }
  624. /*
  625. * Thankfully, it does seem that no runtime services other than
  626. * SetVirtualAddressMap() will touch boot services code, so we can
  627. * get rid of it all at this point
  628. */
  629. efi_free_boot_services();
  630. /*
  631. * Now that EFI is in virtual mode, update the function
  632. * pointers in the runtime service table to the new virtual addresses.
  633. *
  634. * Call EFI services through wrapper functions.
  635. */
  636. efi.get_time = virt_efi_get_time;
  637. efi.set_time = virt_efi_set_time;
  638. efi.get_wakeup_time = virt_efi_get_wakeup_time;
  639. efi.set_wakeup_time = virt_efi_set_wakeup_time;
  640. efi.get_variable = virt_efi_get_variable;
  641. efi.get_next_variable = virt_efi_get_next_variable;
  642. efi.set_variable = virt_efi_set_variable;
  643. efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
  644. efi.reset_system = virt_efi_reset_system;
  645. efi.set_virtual_address_map = NULL;
  646. efi.query_variable_info = virt_efi_query_variable_info;
  647. efi.update_capsule = virt_efi_update_capsule;
  648. efi.query_capsule_caps = virt_efi_query_capsule_caps;
  649. if (__supported_pte_mask & _PAGE_NX)
  650. runtime_code_page_mkexec();
  651. early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
  652. memmap.map = NULL;
  653. kfree(new_memmap);
  654. }
  655. /*
  656. * Convenience functions to obtain memory types and attributes
  657. */
  658. u32 efi_mem_type(unsigned long phys_addr)
  659. {
  660. efi_memory_desc_t *md;
  661. void *p;
  662. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  663. md = p;
  664. if ((md->phys_addr <= phys_addr) &&
  665. (phys_addr < (md->phys_addr +
  666. (md->num_pages << EFI_PAGE_SHIFT))))
  667. return md->type;
  668. }
  669. return 0;
  670. }
  671. u64 efi_mem_attributes(unsigned long phys_addr)
  672. {
  673. efi_memory_desc_t *md;
  674. void *p;
  675. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  676. md = p;
  677. if ((md->phys_addr <= phys_addr) &&
  678. (phys_addr < (md->phys_addr +
  679. (md->num_pages << EFI_PAGE_SHIFT))))
  680. return md->attribute;
  681. }
  682. return 0;
  683. }