efi_64.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * x86_64 specific EFI support functions
  3. * Based on Extensible Firmware Interface Specification version 1.0
  4. *
  5. * Copyright (C) 2005-2008 Intel Co.
  6. * Fenghua Yu <fenghua.yu@intel.com>
  7. * Bibo Mao <bibo.mao@intel.com>
  8. * Chandramouli Narayanan <mouli@linux.intel.com>
  9. * Huang Ying <ying.huang@intel.com>
  10. *
  11. * Code to convert EFI to E820 map has been implemented in elilo bootloader
  12. * based on a EFI patch by Edgar Hucek. Based on the E820 map, the page table
  13. * is setup appropriately for EFI runtime code.
  14. * - mouli 06/14/2007.
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/mm.h>
  20. #include <linux/types.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/ioport.h>
  24. #include <linux/module.h>
  25. #include <linux/efi.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/io.h>
  28. #include <linux/reboot.h>
  29. #include <linux/slab.h>
  30. #include <asm/setup.h>
  31. #include <asm/page.h>
  32. #include <asm/e820.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/tlbflush.h>
  35. #include <asm/proto.h>
  36. #include <asm/efi.h>
  37. #include <asm/cacheflush.h>
  38. #include <asm/fixmap.h>
  39. #include <asm/realmode.h>
  40. #include <asm/time.h>
  41. static pgd_t *save_pgd __initdata;
  42. static unsigned long efi_flags __initdata;
  43. /*
  44. * We allocate runtime services regions bottom-up, starting from -4G, i.e.
  45. * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
  46. */
  47. static u64 efi_va = -4 * (1UL << 30);
  48. #define EFI_VA_END (-68 * (1UL << 30))
  49. /*
  50. * Scratch space used for switching the pagetable in the EFI stub
  51. */
  52. struct efi_scratch {
  53. u64 r15;
  54. u64 prev_cr3;
  55. pgd_t *efi_pgt;
  56. bool use_pgd;
  57. u64 phys_stack;
  58. } __packed;
  59. static void __init early_code_mapping_set_exec(int executable)
  60. {
  61. efi_memory_desc_t *md;
  62. void *p;
  63. if (!(__supported_pte_mask & _PAGE_NX))
  64. return;
  65. /* Make EFI service code area executable */
  66. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  67. md = p;
  68. if (md->type == EFI_RUNTIME_SERVICES_CODE ||
  69. md->type == EFI_BOOT_SERVICES_CODE)
  70. efi_set_executable(md, executable);
  71. }
  72. }
  73. void __init efi_call_phys_prelog(void)
  74. {
  75. unsigned long vaddress;
  76. int pgd;
  77. int n_pgds;
  78. if (!efi_enabled(EFI_OLD_MEMMAP))
  79. return;
  80. early_code_mapping_set_exec(1);
  81. local_irq_save(efi_flags);
  82. n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
  83. save_pgd = kmalloc(n_pgds * sizeof(pgd_t), GFP_KERNEL);
  84. for (pgd = 0; pgd < n_pgds; pgd++) {
  85. save_pgd[pgd] = *pgd_offset_k(pgd * PGDIR_SIZE);
  86. vaddress = (unsigned long)__va(pgd * PGDIR_SIZE);
  87. set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), *pgd_offset_k(vaddress));
  88. }
  89. __flush_tlb_all();
  90. }
  91. void __init efi_call_phys_epilog(void)
  92. {
  93. /*
  94. * After the lock is released, the original page table is restored.
  95. */
  96. int pgd;
  97. int n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
  98. if (!efi_enabled(EFI_OLD_MEMMAP))
  99. return;
  100. for (pgd = 0; pgd < n_pgds; pgd++)
  101. set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), save_pgd[pgd]);
  102. kfree(save_pgd);
  103. __flush_tlb_all();
  104. local_irq_restore(efi_flags);
  105. early_code_mapping_set_exec(0);
  106. }
  107. /*
  108. * Add low kernel mappings for passing arguments to EFI functions.
  109. */
  110. void efi_sync_low_kernel_mappings(void)
  111. {
  112. unsigned num_pgds;
  113. pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
  114. if (efi_enabled(EFI_OLD_MEMMAP))
  115. return;
  116. num_pgds = pgd_index(MODULES_END - 1) - pgd_index(PAGE_OFFSET);
  117. memcpy(pgd + pgd_index(PAGE_OFFSET),
  118. init_mm.pgd + pgd_index(PAGE_OFFSET),
  119. sizeof(pgd_t) * num_pgds);
  120. }
  121. int efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
  122. {
  123. unsigned long text;
  124. struct page *page;
  125. unsigned npages;
  126. pgd_t *pgd;
  127. if (efi_enabled(EFI_OLD_MEMMAP))
  128. return 0;
  129. efi_scratch.efi_pgt = (pgd_t *)(unsigned long)real_mode_header->trampoline_pgd;
  130. pgd = __va(efi_scratch.efi_pgt);
  131. /*
  132. * It can happen that the physical address of new_memmap lands in memory
  133. * which is not mapped in the EFI page table. Therefore we need to go
  134. * and ident-map those pages containing the map before calling
  135. * phys_efi_set_virtual_address_map().
  136. */
  137. if (kernel_map_pages_in_pgd(pgd, pa_memmap, pa_memmap, num_pages, _PAGE_NX)) {
  138. pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
  139. return 1;
  140. }
  141. efi_scratch.use_pgd = true;
  142. /*
  143. * When making calls to the firmware everything needs to be 1:1
  144. * mapped and addressable with 32-bit pointers. Map the kernel
  145. * text and allocate a new stack because we can't rely on the
  146. * stack pointer being < 4GB.
  147. */
  148. if (!IS_ENABLED(CONFIG_EFI_MIXED))
  149. return 0;
  150. page = alloc_page(GFP_KERNEL|__GFP_DMA32);
  151. if (!page)
  152. panic("Unable to allocate EFI runtime stack < 4GB\n");
  153. efi_scratch.phys_stack = virt_to_phys(page_address(page));
  154. efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
  155. npages = (_end - _text) >> PAGE_SHIFT;
  156. text = __pa(_text);
  157. if (kernel_map_pages_in_pgd(pgd, text >> PAGE_SHIFT, text, npages, 0)) {
  158. pr_err("Failed to map kernel text 1:1\n");
  159. return 1;
  160. }
  161. return 0;
  162. }
  163. void efi_cleanup_page_tables(unsigned long pa_memmap, unsigned num_pages)
  164. {
  165. pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
  166. kernel_unmap_pages_in_pgd(pgd, pa_memmap, num_pages);
  167. }
  168. static void __init __map_region(efi_memory_desc_t *md, u64 va)
  169. {
  170. pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
  171. unsigned long pf = 0;
  172. if (!(md->attribute & EFI_MEMORY_WB))
  173. pf |= _PAGE_PCD;
  174. if (kernel_map_pages_in_pgd(pgd, md->phys_addr, va, md->num_pages, pf))
  175. pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
  176. md->phys_addr, va);
  177. }
  178. void __init efi_map_region(efi_memory_desc_t *md)
  179. {
  180. unsigned long size = md->num_pages << PAGE_SHIFT;
  181. u64 pa = md->phys_addr;
  182. if (efi_enabled(EFI_OLD_MEMMAP))
  183. return old_map_region(md);
  184. /*
  185. * Make sure the 1:1 mappings are present as a catch-all for b0rked
  186. * firmware which doesn't update all internal pointers after switching
  187. * to virtual mode and would otherwise crap on us.
  188. */
  189. __map_region(md, md->phys_addr);
  190. /*
  191. * Enforce the 1:1 mapping as the default virtual address when
  192. * booting in EFI mixed mode, because even though we may be
  193. * running a 64-bit kernel, the firmware may only be 32-bit.
  194. */
  195. if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
  196. md->virt_addr = md->phys_addr;
  197. return;
  198. }
  199. efi_va -= size;
  200. /* Is PA 2M-aligned? */
  201. if (!(pa & (PMD_SIZE - 1))) {
  202. efi_va &= PMD_MASK;
  203. } else {
  204. u64 pa_offset = pa & (PMD_SIZE - 1);
  205. u64 prev_va = efi_va;
  206. /* get us the same offset within this 2M page */
  207. efi_va = (efi_va & PMD_MASK) + pa_offset;
  208. if (efi_va > prev_va)
  209. efi_va -= PMD_SIZE;
  210. }
  211. if (efi_va < EFI_VA_END) {
  212. pr_warn(FW_WARN "VA address range overflow!\n");
  213. return;
  214. }
  215. /* Do the VA map */
  216. __map_region(md, efi_va);
  217. md->virt_addr = efi_va;
  218. }
  219. /*
  220. * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
  221. * md->virt_addr is the original virtual address which had been mapped in kexec
  222. * 1st kernel.
  223. */
  224. void __init efi_map_region_fixed(efi_memory_desc_t *md)
  225. {
  226. __map_region(md, md->virt_addr);
  227. }
  228. void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
  229. u32 type, u64 attribute)
  230. {
  231. unsigned long last_map_pfn;
  232. if (type == EFI_MEMORY_MAPPED_IO)
  233. return ioremap(phys_addr, size);
  234. last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
  235. if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
  236. unsigned long top = last_map_pfn << PAGE_SHIFT;
  237. efi_ioremap(top, size - (top - phys_addr), type, attribute);
  238. }
  239. if (!(attribute & EFI_MEMORY_WB))
  240. efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
  241. return (void __iomem *)__va(phys_addr);
  242. }
  243. void __init parse_efi_setup(u64 phys_addr, u32 data_len)
  244. {
  245. efi_setup = phys_addr + sizeof(struct setup_data);
  246. }
  247. void __init efi_runtime_mkexec(void)
  248. {
  249. if (!efi_enabled(EFI_OLD_MEMMAP))
  250. return;
  251. if (__supported_pte_mask & _PAGE_NX)
  252. runtime_code_page_mkexec();
  253. }
  254. void __init efi_dump_pagetable(void)
  255. {
  256. #ifdef CONFIG_EFI_PGT_DUMP
  257. pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
  258. ptdump_walk_pgd_level(NULL, pgd);
  259. #endif
  260. }
  261. #ifdef CONFIG_EFI_MIXED
  262. extern efi_status_t efi64_thunk(u32, ...);
  263. #define runtime_service32(func) \
  264. ({ \
  265. u32 table = (u32)(unsigned long)efi.systab; \
  266. u32 *rt, *___f; \
  267. \
  268. rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
  269. ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
  270. *___f; \
  271. })
  272. /*
  273. * Switch to the EFI page tables early so that we can access the 1:1
  274. * runtime services mappings which are not mapped in any other page
  275. * tables. This function must be called before runtime_service32().
  276. *
  277. * Also, disable interrupts because the IDT points to 64-bit handlers,
  278. * which aren't going to function correctly when we switch to 32-bit.
  279. */
  280. #define efi_thunk(f, ...) \
  281. ({ \
  282. efi_status_t __s; \
  283. unsigned long flags; \
  284. u32 func; \
  285. \
  286. efi_sync_low_kernel_mappings(); \
  287. local_irq_save(flags); \
  288. \
  289. efi_scratch.prev_cr3 = read_cr3(); \
  290. write_cr3((unsigned long)efi_scratch.efi_pgt); \
  291. __flush_tlb_all(); \
  292. \
  293. func = runtime_service32(f); \
  294. __s = efi64_thunk(func, __VA_ARGS__); \
  295. \
  296. write_cr3(efi_scratch.prev_cr3); \
  297. __flush_tlb_all(); \
  298. local_irq_restore(flags); \
  299. \
  300. __s; \
  301. })
  302. efi_status_t efi_thunk_set_virtual_address_map(
  303. void *phys_set_virtual_address_map,
  304. unsigned long memory_map_size,
  305. unsigned long descriptor_size,
  306. u32 descriptor_version,
  307. efi_memory_desc_t *virtual_map)
  308. {
  309. efi_status_t status;
  310. unsigned long flags;
  311. u32 func;
  312. efi_sync_low_kernel_mappings();
  313. local_irq_save(flags);
  314. efi_scratch.prev_cr3 = read_cr3();
  315. write_cr3((unsigned long)efi_scratch.efi_pgt);
  316. __flush_tlb_all();
  317. func = (u32)(unsigned long)phys_set_virtual_address_map;
  318. status = efi64_thunk(func, memory_map_size, descriptor_size,
  319. descriptor_version, virtual_map);
  320. write_cr3(efi_scratch.prev_cr3);
  321. __flush_tlb_all();
  322. local_irq_restore(flags);
  323. return status;
  324. }
  325. static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  326. {
  327. efi_status_t status;
  328. u32 phys_tm, phys_tc;
  329. spin_lock(&rtc_lock);
  330. phys_tm = virt_to_phys(tm);
  331. phys_tc = virt_to_phys(tc);
  332. status = efi_thunk(get_time, phys_tm, phys_tc);
  333. spin_unlock(&rtc_lock);
  334. return status;
  335. }
  336. static efi_status_t efi_thunk_set_time(efi_time_t *tm)
  337. {
  338. efi_status_t status;
  339. u32 phys_tm;
  340. spin_lock(&rtc_lock);
  341. phys_tm = virt_to_phys(tm);
  342. status = efi_thunk(set_time, phys_tm);
  343. spin_unlock(&rtc_lock);
  344. return status;
  345. }
  346. static efi_status_t
  347. efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
  348. efi_time_t *tm)
  349. {
  350. efi_status_t status;
  351. u32 phys_enabled, phys_pending, phys_tm;
  352. spin_lock(&rtc_lock);
  353. phys_enabled = virt_to_phys(enabled);
  354. phys_pending = virt_to_phys(pending);
  355. phys_tm = virt_to_phys(tm);
  356. status = efi_thunk(get_wakeup_time, phys_enabled,
  357. phys_pending, phys_tm);
  358. spin_unlock(&rtc_lock);
  359. return status;
  360. }
  361. static efi_status_t
  362. efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  363. {
  364. efi_status_t status;
  365. u32 phys_tm;
  366. spin_lock(&rtc_lock);
  367. phys_tm = virt_to_phys(tm);
  368. status = efi_thunk(set_wakeup_time, enabled, phys_tm);
  369. spin_unlock(&rtc_lock);
  370. return status;
  371. }
  372. static efi_status_t
  373. efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
  374. u32 *attr, unsigned long *data_size, void *data)
  375. {
  376. efi_status_t status;
  377. u32 phys_name, phys_vendor, phys_attr;
  378. u32 phys_data_size, phys_data;
  379. phys_data_size = virt_to_phys(data_size);
  380. phys_vendor = virt_to_phys(vendor);
  381. phys_name = virt_to_phys(name);
  382. phys_attr = virt_to_phys(attr);
  383. phys_data = virt_to_phys(data);
  384. status = efi_thunk(get_variable, phys_name, phys_vendor,
  385. phys_attr, phys_data_size, phys_data);
  386. return status;
  387. }
  388. static efi_status_t
  389. efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
  390. u32 attr, unsigned long data_size, void *data)
  391. {
  392. u32 phys_name, phys_vendor, phys_data;
  393. efi_status_t status;
  394. phys_name = virt_to_phys(name);
  395. phys_vendor = virt_to_phys(vendor);
  396. phys_data = virt_to_phys(data);
  397. /* If data_size is > sizeof(u32) we've got problems */
  398. status = efi_thunk(set_variable, phys_name, phys_vendor,
  399. attr, data_size, phys_data);
  400. return status;
  401. }
  402. static efi_status_t
  403. efi_thunk_get_next_variable(unsigned long *name_size,
  404. efi_char16_t *name,
  405. efi_guid_t *vendor)
  406. {
  407. efi_status_t status;
  408. u32 phys_name_size, phys_name, phys_vendor;
  409. phys_name_size = virt_to_phys(name_size);
  410. phys_vendor = virt_to_phys(vendor);
  411. phys_name = virt_to_phys(name);
  412. status = efi_thunk(get_next_variable, phys_name_size,
  413. phys_name, phys_vendor);
  414. return status;
  415. }
  416. static efi_status_t
  417. efi_thunk_get_next_high_mono_count(u32 *count)
  418. {
  419. efi_status_t status;
  420. u32 phys_count;
  421. phys_count = virt_to_phys(count);
  422. status = efi_thunk(get_next_high_mono_count, phys_count);
  423. return status;
  424. }
  425. static void
  426. efi_thunk_reset_system(int reset_type, efi_status_t status,
  427. unsigned long data_size, efi_char16_t *data)
  428. {
  429. u32 phys_data;
  430. phys_data = virt_to_phys(data);
  431. efi_thunk(reset_system, reset_type, status, data_size, phys_data);
  432. }
  433. static efi_status_t
  434. efi_thunk_update_capsule(efi_capsule_header_t **capsules,
  435. unsigned long count, unsigned long sg_list)
  436. {
  437. /*
  438. * To properly support this function we would need to repackage
  439. * 'capsules' because the firmware doesn't understand 64-bit
  440. * pointers.
  441. */
  442. return EFI_UNSUPPORTED;
  443. }
  444. static efi_status_t
  445. efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
  446. u64 *remaining_space,
  447. u64 *max_variable_size)
  448. {
  449. efi_status_t status;
  450. u32 phys_storage, phys_remaining, phys_max;
  451. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  452. return EFI_UNSUPPORTED;
  453. phys_storage = virt_to_phys(storage_space);
  454. phys_remaining = virt_to_phys(remaining_space);
  455. phys_max = virt_to_phys(max_variable_size);
  456. status = efi_thunk(query_variable_info, attr, phys_storage,
  457. phys_remaining, phys_max);
  458. return status;
  459. }
  460. static efi_status_t
  461. efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
  462. unsigned long count, u64 *max_size,
  463. int *reset_type)
  464. {
  465. /*
  466. * To properly support this function we would need to repackage
  467. * 'capsules' because the firmware doesn't understand 64-bit
  468. * pointers.
  469. */
  470. return EFI_UNSUPPORTED;
  471. }
  472. void efi_thunk_runtime_setup(void)
  473. {
  474. efi.get_time = efi_thunk_get_time;
  475. efi.set_time = efi_thunk_set_time;
  476. efi.get_wakeup_time = efi_thunk_get_wakeup_time;
  477. efi.set_wakeup_time = efi_thunk_set_wakeup_time;
  478. efi.get_variable = efi_thunk_get_variable;
  479. efi.get_next_variable = efi_thunk_get_next_variable;
  480. efi.set_variable = efi_thunk_set_variable;
  481. efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
  482. efi.reset_system = efi_thunk_reset_system;
  483. efi.query_variable_info = efi_thunk_query_variable_info;
  484. efi.update_capsule = efi_thunk_update_capsule;
  485. efi.query_capsule_caps = efi_thunk_query_capsule_caps;
  486. }
  487. #endif /* CONFIG_EFI_MIXED */