efi_64.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. #define pr_fmt(fmt) "efi: " fmt
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/mm.h>
  21. #include <linux/types.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/bootmem.h>
  24. #include <linux/ioport.h>
  25. #include <linux/init.h>
  26. #include <linux/mc146818rtc.h>
  27. #include <linux/efi.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/io.h>
  30. #include <linux/reboot.h>
  31. #include <linux/slab.h>
  32. #include <linux/ucs2_string.h>
  33. #include <asm/setup.h>
  34. #include <asm/page.h>
  35. #include <asm/e820.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/tlbflush.h>
  38. #include <asm/proto.h>
  39. #include <asm/efi.h>
  40. #include <asm/cacheflush.h>
  41. #include <asm/fixmap.h>
  42. #include <asm/realmode.h>
  43. #include <asm/time.h>
  44. #include <asm/pgalloc.h>
  45. /*
  46. * We allocate runtime services regions bottom-up, starting from -4G, i.e.
  47. * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
  48. */
  49. static u64 efi_va = EFI_VA_START;
  50. struct efi_scratch efi_scratch;
  51. static void __init early_code_mapping_set_exec(int executable)
  52. {
  53. efi_memory_desc_t *md;
  54. if (!(__supported_pte_mask & _PAGE_NX))
  55. return;
  56. /* Make EFI service code area executable */
  57. for_each_efi_memory_desc(md) {
  58. if (md->type == EFI_RUNTIME_SERVICES_CODE ||
  59. md->type == EFI_BOOT_SERVICES_CODE)
  60. efi_set_executable(md, executable);
  61. }
  62. }
  63. pgd_t * __init efi_call_phys_prolog(void)
  64. {
  65. unsigned long vaddress;
  66. pgd_t *save_pgd;
  67. int pgd;
  68. int n_pgds;
  69. if (!efi_enabled(EFI_OLD_MEMMAP)) {
  70. save_pgd = (pgd_t *)read_cr3();
  71. write_cr3((unsigned long)efi_scratch.efi_pgt);
  72. goto out;
  73. }
  74. early_code_mapping_set_exec(1);
  75. n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
  76. save_pgd = kmalloc_array(n_pgds, sizeof(*save_pgd), GFP_KERNEL);
  77. for (pgd = 0; pgd < n_pgds; pgd++) {
  78. save_pgd[pgd] = *pgd_offset_k(pgd * PGDIR_SIZE);
  79. vaddress = (unsigned long)__va(pgd * PGDIR_SIZE);
  80. set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), *pgd_offset_k(vaddress));
  81. }
  82. out:
  83. __flush_tlb_all();
  84. return save_pgd;
  85. }
  86. void __init efi_call_phys_epilog(pgd_t *save_pgd)
  87. {
  88. /*
  89. * After the lock is released, the original page table is restored.
  90. */
  91. int pgd_idx;
  92. int nr_pgds;
  93. if (!efi_enabled(EFI_OLD_MEMMAP)) {
  94. write_cr3((unsigned long)save_pgd);
  95. __flush_tlb_all();
  96. return;
  97. }
  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. static pgd_t *efi_pgd;
  106. /*
  107. * We need our own copy of the higher levels of the page tables
  108. * because we want to avoid inserting EFI region mappings (EFI_VA_END
  109. * to EFI_VA_START) into the standard kernel page tables. Everything
  110. * else can be shared, see efi_sync_low_kernel_mappings().
  111. */
  112. int __init efi_alloc_page_tables(void)
  113. {
  114. pgd_t *pgd;
  115. pud_t *pud;
  116. gfp_t gfp_mask;
  117. if (efi_enabled(EFI_OLD_MEMMAP))
  118. return 0;
  119. gfp_mask = GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO;
  120. efi_pgd = (pgd_t *)__get_free_page(gfp_mask);
  121. if (!efi_pgd)
  122. return -ENOMEM;
  123. pgd = efi_pgd + pgd_index(EFI_VA_END);
  124. pud = pud_alloc_one(NULL, 0);
  125. if (!pud) {
  126. free_page((unsigned long)efi_pgd);
  127. return -ENOMEM;
  128. }
  129. pgd_populate(NULL, pgd, pud);
  130. return 0;
  131. }
  132. /*
  133. * Add low kernel mappings for passing arguments to EFI functions.
  134. */
  135. void efi_sync_low_kernel_mappings(void)
  136. {
  137. unsigned num_entries;
  138. pgd_t *pgd_k, *pgd_efi;
  139. pud_t *pud_k, *pud_efi;
  140. if (efi_enabled(EFI_OLD_MEMMAP))
  141. return;
  142. /*
  143. * We can share all PGD entries apart from the one entry that
  144. * covers the EFI runtime mapping space.
  145. *
  146. * Make sure the EFI runtime region mappings are guaranteed to
  147. * only span a single PGD entry and that the entry also maps
  148. * other important kernel regions.
  149. */
  150. BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
  151. BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
  152. (EFI_VA_END & PGDIR_MASK));
  153. pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
  154. pgd_k = pgd_offset_k(PAGE_OFFSET);
  155. num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
  156. memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
  157. /*
  158. * We share all the PUD entries apart from those that map the
  159. * EFI regions. Copy around them.
  160. */
  161. BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
  162. BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
  163. pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
  164. pud_efi = pud_offset(pgd_efi, 0);
  165. pgd_k = pgd_offset_k(EFI_VA_END);
  166. pud_k = pud_offset(pgd_k, 0);
  167. num_entries = pud_index(EFI_VA_END);
  168. memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
  169. pud_efi = pud_offset(pgd_efi, EFI_VA_START);
  170. pud_k = pud_offset(pgd_k, EFI_VA_START);
  171. num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
  172. memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
  173. }
  174. /*
  175. * Wrapper for slow_virt_to_phys() that handles NULL addresses.
  176. */
  177. static inline phys_addr_t
  178. virt_to_phys_or_null_size(void *va, unsigned long size)
  179. {
  180. bool bad_size;
  181. if (!va)
  182. return 0;
  183. if (virt_addr_valid(va))
  184. return virt_to_phys(va);
  185. /*
  186. * A fully aligned variable on the stack is guaranteed not to
  187. * cross a page bounary. Try to catch strings on the stack by
  188. * checking that 'size' is a power of two.
  189. */
  190. bad_size = size > PAGE_SIZE || !is_power_of_2(size);
  191. WARN_ON(!IS_ALIGNED((unsigned long)va, size) || bad_size);
  192. return slow_virt_to_phys(va);
  193. }
  194. #define virt_to_phys_or_null(addr) \
  195. virt_to_phys_or_null_size((addr), sizeof(*(addr)))
  196. int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
  197. {
  198. unsigned long pfn, text;
  199. struct page *page;
  200. unsigned npages;
  201. pgd_t *pgd;
  202. if (efi_enabled(EFI_OLD_MEMMAP))
  203. return 0;
  204. efi_scratch.efi_pgt = (pgd_t *)__pa(efi_pgd);
  205. pgd = efi_pgd;
  206. /*
  207. * It can happen that the physical address of new_memmap lands in memory
  208. * which is not mapped in the EFI page table. Therefore we need to go
  209. * and ident-map those pages containing the map before calling
  210. * phys_efi_set_virtual_address_map().
  211. */
  212. pfn = pa_memmap >> PAGE_SHIFT;
  213. if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, _PAGE_NX | _PAGE_RW)) {
  214. pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
  215. return 1;
  216. }
  217. efi_scratch.use_pgd = true;
  218. /*
  219. * When making calls to the firmware everything needs to be 1:1
  220. * mapped and addressable with 32-bit pointers. Map the kernel
  221. * text and allocate a new stack because we can't rely on the
  222. * stack pointer being < 4GB.
  223. */
  224. if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
  225. return 0;
  226. page = alloc_page(GFP_KERNEL|__GFP_DMA32);
  227. if (!page)
  228. panic("Unable to allocate EFI runtime stack < 4GB\n");
  229. efi_scratch.phys_stack = virt_to_phys(page_address(page));
  230. efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
  231. npages = (_etext - _text) >> PAGE_SHIFT;
  232. text = __pa(_text);
  233. pfn = text >> PAGE_SHIFT;
  234. if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, _PAGE_RW)) {
  235. pr_err("Failed to map kernel text 1:1\n");
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. static void __init __map_region(efi_memory_desc_t *md, u64 va)
  241. {
  242. unsigned long flags = _PAGE_RW;
  243. unsigned long pfn;
  244. pgd_t *pgd = efi_pgd;
  245. if (!(md->attribute & EFI_MEMORY_WB))
  246. flags |= _PAGE_PCD;
  247. pfn = md->phys_addr >> PAGE_SHIFT;
  248. if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
  249. pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
  250. md->phys_addr, va);
  251. }
  252. void __init efi_map_region(efi_memory_desc_t *md)
  253. {
  254. unsigned long size = md->num_pages << PAGE_SHIFT;
  255. u64 pa = md->phys_addr;
  256. if (efi_enabled(EFI_OLD_MEMMAP))
  257. return old_map_region(md);
  258. /*
  259. * Make sure the 1:1 mappings are present as a catch-all for b0rked
  260. * firmware which doesn't update all internal pointers after switching
  261. * to virtual mode and would otherwise crap on us.
  262. */
  263. __map_region(md, md->phys_addr);
  264. /*
  265. * Enforce the 1:1 mapping as the default virtual address when
  266. * booting in EFI mixed mode, because even though we may be
  267. * running a 64-bit kernel, the firmware may only be 32-bit.
  268. */
  269. if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
  270. md->virt_addr = md->phys_addr;
  271. return;
  272. }
  273. efi_va -= size;
  274. /* Is PA 2M-aligned? */
  275. if (!(pa & (PMD_SIZE - 1))) {
  276. efi_va &= PMD_MASK;
  277. } else {
  278. u64 pa_offset = pa & (PMD_SIZE - 1);
  279. u64 prev_va = efi_va;
  280. /* get us the same offset within this 2M page */
  281. efi_va = (efi_va & PMD_MASK) + pa_offset;
  282. if (efi_va > prev_va)
  283. efi_va -= PMD_SIZE;
  284. }
  285. if (efi_va < EFI_VA_END) {
  286. pr_warn(FW_WARN "VA address range overflow!\n");
  287. return;
  288. }
  289. /* Do the VA map */
  290. __map_region(md, efi_va);
  291. md->virt_addr = efi_va;
  292. }
  293. /*
  294. * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
  295. * md->virt_addr is the original virtual address which had been mapped in kexec
  296. * 1st kernel.
  297. */
  298. void __init efi_map_region_fixed(efi_memory_desc_t *md)
  299. {
  300. __map_region(md, md->phys_addr);
  301. __map_region(md, md->virt_addr);
  302. }
  303. void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
  304. u32 type, u64 attribute)
  305. {
  306. unsigned long last_map_pfn;
  307. if (type == EFI_MEMORY_MAPPED_IO)
  308. return ioremap(phys_addr, size);
  309. last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
  310. if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
  311. unsigned long top = last_map_pfn << PAGE_SHIFT;
  312. efi_ioremap(top, size - (top - phys_addr), type, attribute);
  313. }
  314. if (!(attribute & EFI_MEMORY_WB))
  315. efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
  316. return (void __iomem *)__va(phys_addr);
  317. }
  318. void __init parse_efi_setup(u64 phys_addr, u32 data_len)
  319. {
  320. efi_setup = phys_addr + sizeof(struct setup_data);
  321. }
  322. void __init efi_runtime_update_mappings(void)
  323. {
  324. unsigned long pfn;
  325. pgd_t *pgd = efi_pgd;
  326. efi_memory_desc_t *md;
  327. if (efi_enabled(EFI_OLD_MEMMAP)) {
  328. if (__supported_pte_mask & _PAGE_NX)
  329. runtime_code_page_mkexec();
  330. return;
  331. }
  332. if (!efi_enabled(EFI_NX_PE_DATA))
  333. return;
  334. for_each_efi_memory_desc(md) {
  335. unsigned long pf = 0;
  336. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  337. continue;
  338. if (!(md->attribute & EFI_MEMORY_WB))
  339. pf |= _PAGE_PCD;
  340. if ((md->attribute & EFI_MEMORY_XP) ||
  341. (md->type == EFI_RUNTIME_SERVICES_DATA))
  342. pf |= _PAGE_NX;
  343. if (!(md->attribute & EFI_MEMORY_RO) &&
  344. (md->type != EFI_RUNTIME_SERVICES_CODE))
  345. pf |= _PAGE_RW;
  346. /* Update the 1:1 mapping */
  347. pfn = md->phys_addr >> PAGE_SHIFT;
  348. if (kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf))
  349. pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
  350. md->phys_addr, md->virt_addr);
  351. if (kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf))
  352. pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
  353. md->phys_addr, md->virt_addr);
  354. }
  355. }
  356. void __init efi_dump_pagetable(void)
  357. {
  358. #ifdef CONFIG_EFI_PGT_DUMP
  359. ptdump_walk_pgd_level(NULL, efi_pgd);
  360. #endif
  361. }
  362. #ifdef CONFIG_EFI_MIXED
  363. extern efi_status_t efi64_thunk(u32, ...);
  364. #define runtime_service32(func) \
  365. ({ \
  366. u32 table = (u32)(unsigned long)efi.systab; \
  367. u32 *rt, *___f; \
  368. \
  369. rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
  370. ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
  371. *___f; \
  372. })
  373. /*
  374. * Switch to the EFI page tables early so that we can access the 1:1
  375. * runtime services mappings which are not mapped in any other page
  376. * tables. This function must be called before runtime_service32().
  377. *
  378. * Also, disable interrupts because the IDT points to 64-bit handlers,
  379. * which aren't going to function correctly when we switch to 32-bit.
  380. */
  381. #define efi_thunk(f, ...) \
  382. ({ \
  383. efi_status_t __s; \
  384. unsigned long __flags; \
  385. u32 __func; \
  386. \
  387. local_irq_save(__flags); \
  388. arch_efi_call_virt_setup(); \
  389. \
  390. __func = runtime_service32(f); \
  391. __s = efi64_thunk(__func, __VA_ARGS__); \
  392. \
  393. arch_efi_call_virt_teardown(); \
  394. local_irq_restore(__flags); \
  395. \
  396. __s; \
  397. })
  398. efi_status_t efi_thunk_set_virtual_address_map(
  399. void *phys_set_virtual_address_map,
  400. unsigned long memory_map_size,
  401. unsigned long descriptor_size,
  402. u32 descriptor_version,
  403. efi_memory_desc_t *virtual_map)
  404. {
  405. efi_status_t status;
  406. unsigned long flags;
  407. u32 func;
  408. efi_sync_low_kernel_mappings();
  409. local_irq_save(flags);
  410. efi_scratch.prev_cr3 = read_cr3();
  411. write_cr3((unsigned long)efi_scratch.efi_pgt);
  412. __flush_tlb_all();
  413. func = (u32)(unsigned long)phys_set_virtual_address_map;
  414. status = efi64_thunk(func, memory_map_size, descriptor_size,
  415. descriptor_version, virtual_map);
  416. write_cr3(efi_scratch.prev_cr3);
  417. __flush_tlb_all();
  418. local_irq_restore(flags);
  419. return status;
  420. }
  421. static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  422. {
  423. efi_status_t status;
  424. u32 phys_tm, phys_tc;
  425. spin_lock(&rtc_lock);
  426. phys_tm = virt_to_phys_or_null(tm);
  427. phys_tc = virt_to_phys_or_null(tc);
  428. status = efi_thunk(get_time, phys_tm, phys_tc);
  429. spin_unlock(&rtc_lock);
  430. return status;
  431. }
  432. static efi_status_t efi_thunk_set_time(efi_time_t *tm)
  433. {
  434. efi_status_t status;
  435. u32 phys_tm;
  436. spin_lock(&rtc_lock);
  437. phys_tm = virt_to_phys_or_null(tm);
  438. status = efi_thunk(set_time, phys_tm);
  439. spin_unlock(&rtc_lock);
  440. return status;
  441. }
  442. static efi_status_t
  443. efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
  444. efi_time_t *tm)
  445. {
  446. efi_status_t status;
  447. u32 phys_enabled, phys_pending, phys_tm;
  448. spin_lock(&rtc_lock);
  449. phys_enabled = virt_to_phys_or_null(enabled);
  450. phys_pending = virt_to_phys_or_null(pending);
  451. phys_tm = virt_to_phys_or_null(tm);
  452. status = efi_thunk(get_wakeup_time, phys_enabled,
  453. phys_pending, phys_tm);
  454. spin_unlock(&rtc_lock);
  455. return status;
  456. }
  457. static efi_status_t
  458. efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  459. {
  460. efi_status_t status;
  461. u32 phys_tm;
  462. spin_lock(&rtc_lock);
  463. phys_tm = virt_to_phys_or_null(tm);
  464. status = efi_thunk(set_wakeup_time, enabled, phys_tm);
  465. spin_unlock(&rtc_lock);
  466. return status;
  467. }
  468. static unsigned long efi_name_size(efi_char16_t *name)
  469. {
  470. return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
  471. }
  472. static efi_status_t
  473. efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
  474. u32 *attr, unsigned long *data_size, void *data)
  475. {
  476. efi_status_t status;
  477. u32 phys_name, phys_vendor, phys_attr;
  478. u32 phys_data_size, phys_data;
  479. phys_data_size = virt_to_phys_or_null(data_size);
  480. phys_vendor = virt_to_phys_or_null(vendor);
  481. phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
  482. phys_attr = virt_to_phys_or_null(attr);
  483. phys_data = virt_to_phys_or_null_size(data, *data_size);
  484. status = efi_thunk(get_variable, phys_name, phys_vendor,
  485. phys_attr, phys_data_size, phys_data);
  486. return status;
  487. }
  488. static efi_status_t
  489. efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
  490. u32 attr, unsigned long data_size, void *data)
  491. {
  492. u32 phys_name, phys_vendor, phys_data;
  493. efi_status_t status;
  494. phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
  495. phys_vendor = virt_to_phys_or_null(vendor);
  496. phys_data = virt_to_phys_or_null_size(data, data_size);
  497. /* If data_size is > sizeof(u32) we've got problems */
  498. status = efi_thunk(set_variable, phys_name, phys_vendor,
  499. attr, data_size, phys_data);
  500. return status;
  501. }
  502. static efi_status_t
  503. efi_thunk_get_next_variable(unsigned long *name_size,
  504. efi_char16_t *name,
  505. efi_guid_t *vendor)
  506. {
  507. efi_status_t status;
  508. u32 phys_name_size, phys_name, phys_vendor;
  509. phys_name_size = virt_to_phys_or_null(name_size);
  510. phys_vendor = virt_to_phys_or_null(vendor);
  511. phys_name = virt_to_phys_or_null_size(name, *name_size);
  512. status = efi_thunk(get_next_variable, phys_name_size,
  513. phys_name, phys_vendor);
  514. return status;
  515. }
  516. static efi_status_t
  517. efi_thunk_get_next_high_mono_count(u32 *count)
  518. {
  519. efi_status_t status;
  520. u32 phys_count;
  521. phys_count = virt_to_phys_or_null(count);
  522. status = efi_thunk(get_next_high_mono_count, phys_count);
  523. return status;
  524. }
  525. static void
  526. efi_thunk_reset_system(int reset_type, efi_status_t status,
  527. unsigned long data_size, efi_char16_t *data)
  528. {
  529. u32 phys_data;
  530. phys_data = virt_to_phys_or_null_size(data, data_size);
  531. efi_thunk(reset_system, reset_type, status, data_size, phys_data);
  532. }
  533. static efi_status_t
  534. efi_thunk_update_capsule(efi_capsule_header_t **capsules,
  535. unsigned long count, unsigned long sg_list)
  536. {
  537. /*
  538. * To properly support this function we would need to repackage
  539. * 'capsules' because the firmware doesn't understand 64-bit
  540. * pointers.
  541. */
  542. return EFI_UNSUPPORTED;
  543. }
  544. static efi_status_t
  545. efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
  546. u64 *remaining_space,
  547. u64 *max_variable_size)
  548. {
  549. efi_status_t status;
  550. u32 phys_storage, phys_remaining, phys_max;
  551. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  552. return EFI_UNSUPPORTED;
  553. phys_storage = virt_to_phys_or_null(storage_space);
  554. phys_remaining = virt_to_phys_or_null(remaining_space);
  555. phys_max = virt_to_phys_or_null(max_variable_size);
  556. status = efi_thunk(query_variable_info, attr, phys_storage,
  557. phys_remaining, phys_max);
  558. return status;
  559. }
  560. static efi_status_t
  561. efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
  562. unsigned long count, u64 *max_size,
  563. int *reset_type)
  564. {
  565. /*
  566. * To properly support this function we would need to repackage
  567. * 'capsules' because the firmware doesn't understand 64-bit
  568. * pointers.
  569. */
  570. return EFI_UNSUPPORTED;
  571. }
  572. void efi_thunk_runtime_setup(void)
  573. {
  574. efi.get_time = efi_thunk_get_time;
  575. efi.set_time = efi_thunk_set_time;
  576. efi.get_wakeup_time = efi_thunk_get_wakeup_time;
  577. efi.set_wakeup_time = efi_thunk_set_wakeup_time;
  578. efi.get_variable = efi_thunk_get_variable;
  579. efi.get_next_variable = efi_thunk_get_next_variable;
  580. efi.set_variable = efi_thunk_set_variable;
  581. efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
  582. efi.reset_system = efi_thunk_reset_system;
  583. efi.query_variable_info = efi_thunk_query_variable_info;
  584. efi.update_capsule = efi_thunk_update_capsule;
  585. efi.query_capsule_caps = efi_thunk_query_capsule_caps;
  586. }
  587. #endif /* CONFIG_EFI_MIXED */