efi_64.c 15 KB

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