efi_64.c 19 KB

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