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 <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_pages(gfp_mask, PGD_ALLOCATION_ORDER);
  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. * Certain firmware versions are way too sentimential and still believe
  220. * they are exclusive and unquestionable owners of the first physical page,
  221. * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY
  222. * (but then write-access it later during SetVirtualAddressMap()).
  223. *
  224. * Create a 1:1 mapping for this page, to avoid triple faults during early
  225. * boot with such firmware. We are free to hand this page to the BIOS,
  226. * as trim_bios_range() will reserve the first page and isolate it away
  227. * from memory allocators anyway.
  228. */
  229. if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, _PAGE_RW)) {
  230. pr_err("Failed to create 1:1 mapping for the first page!\n");
  231. return 1;
  232. }
  233. /*
  234. * When making calls to the firmware everything needs to be 1:1
  235. * mapped and addressable with 32-bit pointers. Map the kernel
  236. * text and allocate a new stack because we can't rely on the
  237. * stack pointer being < 4GB.
  238. */
  239. if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
  240. return 0;
  241. page = alloc_page(GFP_KERNEL|__GFP_DMA32);
  242. if (!page)
  243. panic("Unable to allocate EFI runtime stack < 4GB\n");
  244. efi_scratch.phys_stack = virt_to_phys(page_address(page));
  245. efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
  246. npages = (_etext - _text) >> PAGE_SHIFT;
  247. text = __pa(_text);
  248. pfn = text >> PAGE_SHIFT;
  249. if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, _PAGE_RW)) {
  250. pr_err("Failed to map kernel text 1:1\n");
  251. return 1;
  252. }
  253. return 0;
  254. }
  255. static void __init __map_region(efi_memory_desc_t *md, u64 va)
  256. {
  257. unsigned long flags = _PAGE_RW;
  258. unsigned long pfn;
  259. pgd_t *pgd = efi_pgd;
  260. if (!(md->attribute & EFI_MEMORY_WB))
  261. flags |= _PAGE_PCD;
  262. pfn = md->phys_addr >> PAGE_SHIFT;
  263. if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
  264. pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
  265. md->phys_addr, va);
  266. }
  267. void __init efi_map_region(efi_memory_desc_t *md)
  268. {
  269. unsigned long size = md->num_pages << PAGE_SHIFT;
  270. u64 pa = md->phys_addr;
  271. if (efi_enabled(EFI_OLD_MEMMAP))
  272. return old_map_region(md);
  273. /*
  274. * Make sure the 1:1 mappings are present as a catch-all for b0rked
  275. * firmware which doesn't update all internal pointers after switching
  276. * to virtual mode and would otherwise crap on us.
  277. */
  278. __map_region(md, md->phys_addr);
  279. /*
  280. * Enforce the 1:1 mapping as the default virtual address when
  281. * booting in EFI mixed mode, because even though we may be
  282. * running a 64-bit kernel, the firmware may only be 32-bit.
  283. */
  284. if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
  285. md->virt_addr = md->phys_addr;
  286. return;
  287. }
  288. efi_va -= size;
  289. /* Is PA 2M-aligned? */
  290. if (!(pa & (PMD_SIZE - 1))) {
  291. efi_va &= PMD_MASK;
  292. } else {
  293. u64 pa_offset = pa & (PMD_SIZE - 1);
  294. u64 prev_va = efi_va;
  295. /* get us the same offset within this 2M page */
  296. efi_va = (efi_va & PMD_MASK) + pa_offset;
  297. if (efi_va > prev_va)
  298. efi_va -= PMD_SIZE;
  299. }
  300. if (efi_va < EFI_VA_END) {
  301. pr_warn(FW_WARN "VA address range overflow!\n");
  302. return;
  303. }
  304. /* Do the VA map */
  305. __map_region(md, efi_va);
  306. md->virt_addr = efi_va;
  307. }
  308. /*
  309. * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
  310. * md->virt_addr is the original virtual address which had been mapped in kexec
  311. * 1st kernel.
  312. */
  313. void __init efi_map_region_fixed(efi_memory_desc_t *md)
  314. {
  315. __map_region(md, md->phys_addr);
  316. __map_region(md, md->virt_addr);
  317. }
  318. void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
  319. u32 type, u64 attribute)
  320. {
  321. unsigned long last_map_pfn;
  322. if (type == EFI_MEMORY_MAPPED_IO)
  323. return ioremap(phys_addr, size);
  324. last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
  325. if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
  326. unsigned long top = last_map_pfn << PAGE_SHIFT;
  327. efi_ioremap(top, size - (top - phys_addr), type, attribute);
  328. }
  329. if (!(attribute & EFI_MEMORY_WB))
  330. efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
  331. return (void __iomem *)__va(phys_addr);
  332. }
  333. void __init parse_efi_setup(u64 phys_addr, u32 data_len)
  334. {
  335. efi_setup = phys_addr + sizeof(struct setup_data);
  336. }
  337. void __init efi_runtime_update_mappings(void)
  338. {
  339. unsigned long pfn;
  340. pgd_t *pgd = efi_pgd;
  341. efi_memory_desc_t *md;
  342. if (efi_enabled(EFI_OLD_MEMMAP)) {
  343. if (__supported_pte_mask & _PAGE_NX)
  344. runtime_code_page_mkexec();
  345. return;
  346. }
  347. if (!efi_enabled(EFI_NX_PE_DATA))
  348. return;
  349. for_each_efi_memory_desc(md) {
  350. unsigned long pf = 0;
  351. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  352. continue;
  353. if (!(md->attribute & EFI_MEMORY_WB))
  354. pf |= _PAGE_PCD;
  355. if ((md->attribute & EFI_MEMORY_XP) ||
  356. (md->type == EFI_RUNTIME_SERVICES_DATA))
  357. pf |= _PAGE_NX;
  358. if (!(md->attribute & EFI_MEMORY_RO) &&
  359. (md->type != EFI_RUNTIME_SERVICES_CODE))
  360. pf |= _PAGE_RW;
  361. /* Update the 1:1 mapping */
  362. pfn = md->phys_addr >> PAGE_SHIFT;
  363. if (kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf))
  364. pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
  365. md->phys_addr, md->virt_addr);
  366. if (kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf))
  367. pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
  368. md->phys_addr, md->virt_addr);
  369. }
  370. }
  371. void __init efi_dump_pagetable(void)
  372. {
  373. #ifdef CONFIG_EFI_PGT_DUMP
  374. ptdump_walk_pgd_level(NULL, efi_pgd);
  375. #endif
  376. }
  377. #ifdef CONFIG_EFI_MIXED
  378. extern efi_status_t efi64_thunk(u32, ...);
  379. #define runtime_service32(func) \
  380. ({ \
  381. u32 table = (u32)(unsigned long)efi.systab; \
  382. u32 *rt, *___f; \
  383. \
  384. rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
  385. ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
  386. *___f; \
  387. })
  388. /*
  389. * Switch to the EFI page tables early so that we can access the 1:1
  390. * runtime services mappings which are not mapped in any other page
  391. * tables. This function must be called before runtime_service32().
  392. *
  393. * Also, disable interrupts because the IDT points to 64-bit handlers,
  394. * which aren't going to function correctly when we switch to 32-bit.
  395. */
  396. #define efi_thunk(f, ...) \
  397. ({ \
  398. efi_status_t __s; \
  399. unsigned long __flags; \
  400. u32 __func; \
  401. \
  402. local_irq_save(__flags); \
  403. arch_efi_call_virt_setup(); \
  404. \
  405. __func = runtime_service32(f); \
  406. __s = efi64_thunk(__func, __VA_ARGS__); \
  407. \
  408. arch_efi_call_virt_teardown(); \
  409. local_irq_restore(__flags); \
  410. \
  411. __s; \
  412. })
  413. efi_status_t efi_thunk_set_virtual_address_map(
  414. void *phys_set_virtual_address_map,
  415. unsigned long memory_map_size,
  416. unsigned long descriptor_size,
  417. u32 descriptor_version,
  418. efi_memory_desc_t *virtual_map)
  419. {
  420. efi_status_t status;
  421. unsigned long flags;
  422. u32 func;
  423. efi_sync_low_kernel_mappings();
  424. local_irq_save(flags);
  425. efi_scratch.prev_cr3 = read_cr3();
  426. write_cr3((unsigned long)efi_scratch.efi_pgt);
  427. __flush_tlb_all();
  428. func = (u32)(unsigned long)phys_set_virtual_address_map;
  429. status = efi64_thunk(func, memory_map_size, descriptor_size,
  430. descriptor_version, virtual_map);
  431. write_cr3(efi_scratch.prev_cr3);
  432. __flush_tlb_all();
  433. local_irq_restore(flags);
  434. return status;
  435. }
  436. static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  437. {
  438. efi_status_t status;
  439. u32 phys_tm, phys_tc;
  440. spin_lock(&rtc_lock);
  441. phys_tm = virt_to_phys_or_null(tm);
  442. phys_tc = virt_to_phys_or_null(tc);
  443. status = efi_thunk(get_time, phys_tm, phys_tc);
  444. spin_unlock(&rtc_lock);
  445. return status;
  446. }
  447. static efi_status_t efi_thunk_set_time(efi_time_t *tm)
  448. {
  449. efi_status_t status;
  450. u32 phys_tm;
  451. spin_lock(&rtc_lock);
  452. phys_tm = virt_to_phys_or_null(tm);
  453. status = efi_thunk(set_time, phys_tm);
  454. spin_unlock(&rtc_lock);
  455. return status;
  456. }
  457. static efi_status_t
  458. efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
  459. efi_time_t *tm)
  460. {
  461. efi_status_t status;
  462. u32 phys_enabled, phys_pending, phys_tm;
  463. spin_lock(&rtc_lock);
  464. phys_enabled = virt_to_phys_or_null(enabled);
  465. phys_pending = virt_to_phys_or_null(pending);
  466. phys_tm = virt_to_phys_or_null(tm);
  467. status = efi_thunk(get_wakeup_time, phys_enabled,
  468. phys_pending, phys_tm);
  469. spin_unlock(&rtc_lock);
  470. return status;
  471. }
  472. static efi_status_t
  473. efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  474. {
  475. efi_status_t status;
  476. u32 phys_tm;
  477. spin_lock(&rtc_lock);
  478. phys_tm = virt_to_phys_or_null(tm);
  479. status = efi_thunk(set_wakeup_time, enabled, phys_tm);
  480. spin_unlock(&rtc_lock);
  481. return status;
  482. }
  483. static unsigned long efi_name_size(efi_char16_t *name)
  484. {
  485. return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
  486. }
  487. static efi_status_t
  488. efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
  489. u32 *attr, unsigned long *data_size, void *data)
  490. {
  491. efi_status_t status;
  492. u32 phys_name, phys_vendor, phys_attr;
  493. u32 phys_data_size, phys_data;
  494. phys_data_size = virt_to_phys_or_null(data_size);
  495. phys_vendor = virt_to_phys_or_null(vendor);
  496. phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
  497. phys_attr = virt_to_phys_or_null(attr);
  498. phys_data = virt_to_phys_or_null_size(data, *data_size);
  499. status = efi_thunk(get_variable, phys_name, phys_vendor,
  500. phys_attr, phys_data_size, phys_data);
  501. return status;
  502. }
  503. static efi_status_t
  504. efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
  505. u32 attr, unsigned long data_size, void *data)
  506. {
  507. u32 phys_name, phys_vendor, phys_data;
  508. efi_status_t status;
  509. phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
  510. phys_vendor = virt_to_phys_or_null(vendor);
  511. phys_data = virt_to_phys_or_null_size(data, data_size);
  512. /* If data_size is > sizeof(u32) we've got problems */
  513. status = efi_thunk(set_variable, phys_name, phys_vendor,
  514. attr, data_size, phys_data);
  515. return status;
  516. }
  517. static efi_status_t
  518. efi_thunk_get_next_variable(unsigned long *name_size,
  519. efi_char16_t *name,
  520. efi_guid_t *vendor)
  521. {
  522. efi_status_t status;
  523. u32 phys_name_size, phys_name, phys_vendor;
  524. phys_name_size = virt_to_phys_or_null(name_size);
  525. phys_vendor = virt_to_phys_or_null(vendor);
  526. phys_name = virt_to_phys_or_null_size(name, *name_size);
  527. status = efi_thunk(get_next_variable, phys_name_size,
  528. phys_name, phys_vendor);
  529. return status;
  530. }
  531. static efi_status_t
  532. efi_thunk_get_next_high_mono_count(u32 *count)
  533. {
  534. efi_status_t status;
  535. u32 phys_count;
  536. phys_count = virt_to_phys_or_null(count);
  537. status = efi_thunk(get_next_high_mono_count, phys_count);
  538. return status;
  539. }
  540. static void
  541. efi_thunk_reset_system(int reset_type, efi_status_t status,
  542. unsigned long data_size, efi_char16_t *data)
  543. {
  544. u32 phys_data;
  545. phys_data = virt_to_phys_or_null_size(data, data_size);
  546. efi_thunk(reset_system, reset_type, status, data_size, phys_data);
  547. }
  548. static efi_status_t
  549. efi_thunk_update_capsule(efi_capsule_header_t **capsules,
  550. unsigned long count, unsigned long sg_list)
  551. {
  552. /*
  553. * To properly support this function we would need to repackage
  554. * 'capsules' because the firmware doesn't understand 64-bit
  555. * pointers.
  556. */
  557. return EFI_UNSUPPORTED;
  558. }
  559. static efi_status_t
  560. efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
  561. u64 *remaining_space,
  562. u64 *max_variable_size)
  563. {
  564. efi_status_t status;
  565. u32 phys_storage, phys_remaining, phys_max;
  566. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  567. return EFI_UNSUPPORTED;
  568. phys_storage = virt_to_phys_or_null(storage_space);
  569. phys_remaining = virt_to_phys_or_null(remaining_space);
  570. phys_max = virt_to_phys_or_null(max_variable_size);
  571. status = efi_thunk(query_variable_info, attr, phys_storage,
  572. phys_remaining, phys_max);
  573. return status;
  574. }
  575. static efi_status_t
  576. efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
  577. unsigned long count, u64 *max_size,
  578. int *reset_type)
  579. {
  580. /*
  581. * To properly support this function we would need to repackage
  582. * 'capsules' because the firmware doesn't understand 64-bit
  583. * pointers.
  584. */
  585. return EFI_UNSUPPORTED;
  586. }
  587. void efi_thunk_runtime_setup(void)
  588. {
  589. efi.get_time = efi_thunk_get_time;
  590. efi.set_time = efi_thunk_set_time;
  591. efi.get_wakeup_time = efi_thunk_get_wakeup_time;
  592. efi.set_wakeup_time = efi_thunk_set_wakeup_time;
  593. efi.get_variable = efi_thunk_get_variable;
  594. efi.get_next_variable = efi_thunk_get_next_variable;
  595. efi.set_variable = efi_thunk_set_variable;
  596. efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
  597. efi.reset_system = efi_thunk_reset_system;
  598. efi.query_variable_info = efi_thunk_query_variable_info;
  599. efi.update_capsule = efi_thunk_update_capsule;
  600. efi.query_capsule_caps = efi_thunk_query_capsule_caps;
  601. }
  602. #endif /* CONFIG_EFI_MIXED */