efi.c 19 KB

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