efi_64.c 15 KB

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