efi_64.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. static pgd_t *save_pgd __initdata;
  41. static unsigned long efi_flags __initdata;
  42. /*
  43. * We allocate runtime services regions bottom-up, starting from -4G, i.e.
  44. * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
  45. */
  46. static u64 efi_va = -4 * (1UL << 30);
  47. #define EFI_VA_END (-68 * (1UL << 30))
  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. };
  57. static void __init early_code_mapping_set_exec(int executable)
  58. {
  59. efi_memory_desc_t *md;
  60. void *p;
  61. if (!(__supported_pte_mask & _PAGE_NX))
  62. return;
  63. /* Make EFI service code area executable */
  64. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  65. md = p;
  66. if (md->type == EFI_RUNTIME_SERVICES_CODE ||
  67. md->type == EFI_BOOT_SERVICES_CODE)
  68. efi_set_executable(md, executable);
  69. }
  70. }
  71. void __init efi_call_phys_prelog(void)
  72. {
  73. unsigned long vaddress;
  74. int pgd;
  75. int n_pgds;
  76. if (!efi_enabled(EFI_OLD_MEMMAP))
  77. return;
  78. early_code_mapping_set_exec(1);
  79. local_irq_save(efi_flags);
  80. n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
  81. save_pgd = kmalloc(n_pgds * sizeof(pgd_t), GFP_KERNEL);
  82. for (pgd = 0; pgd < n_pgds; pgd++) {
  83. save_pgd[pgd] = *pgd_offset_k(pgd * PGDIR_SIZE);
  84. vaddress = (unsigned long)__va(pgd * PGDIR_SIZE);
  85. set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), *pgd_offset_k(vaddress));
  86. }
  87. __flush_tlb_all();
  88. }
  89. void __init efi_call_phys_epilog(void)
  90. {
  91. /*
  92. * After the lock is released, the original page table is restored.
  93. */
  94. int pgd;
  95. int n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
  96. if (!efi_enabled(EFI_OLD_MEMMAP))
  97. return;
  98. for (pgd = 0; pgd < n_pgds; pgd++)
  99. set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), save_pgd[pgd]);
  100. kfree(save_pgd);
  101. __flush_tlb_all();
  102. local_irq_restore(efi_flags);
  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. void efi_setup_page_tables(void)
  120. {
  121. efi_scratch.efi_pgt = (pgd_t *)(unsigned long)real_mode_header->trampoline_pgd;
  122. if (!efi_enabled(EFI_OLD_MEMMAP))
  123. efi_scratch.use_pgd = true;
  124. }
  125. static void __init __map_region(efi_memory_desc_t *md, u64 va)
  126. {
  127. pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
  128. unsigned long pf = 0;
  129. if (!(md->attribute & EFI_MEMORY_WB))
  130. pf |= _PAGE_PCD;
  131. if (kernel_map_pages_in_pgd(pgd, md->phys_addr, va, md->num_pages, pf))
  132. pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
  133. md->phys_addr, va);
  134. }
  135. void __init efi_map_region(efi_memory_desc_t *md)
  136. {
  137. unsigned long size = md->num_pages << PAGE_SHIFT;
  138. u64 pa = md->phys_addr;
  139. if (efi_enabled(EFI_OLD_MEMMAP))
  140. return old_map_region(md);
  141. /*
  142. * Make sure the 1:1 mappings are present as a catch-all for b0rked
  143. * firmware which doesn't update all internal pointers after switching
  144. * to virtual mode and would otherwise crap on us.
  145. */
  146. __map_region(md, md->phys_addr);
  147. efi_va -= size;
  148. /* Is PA 2M-aligned? */
  149. if (!(pa & (PMD_SIZE - 1))) {
  150. efi_va &= PMD_MASK;
  151. } else {
  152. u64 pa_offset = pa & (PMD_SIZE - 1);
  153. u64 prev_va = efi_va;
  154. /* get us the same offset within this 2M page */
  155. efi_va = (efi_va & PMD_MASK) + pa_offset;
  156. if (efi_va > prev_va)
  157. efi_va -= PMD_SIZE;
  158. }
  159. if (efi_va < EFI_VA_END) {
  160. pr_warn(FW_WARN "VA address range overflow!\n");
  161. return;
  162. }
  163. /* Do the VA map */
  164. __map_region(md, efi_va);
  165. md->virt_addr = efi_va;
  166. }
  167. /*
  168. * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
  169. * md->virt_addr is the original virtual address which had been mapped in kexec
  170. * 1st kernel.
  171. */
  172. void __init efi_map_region_fixed(efi_memory_desc_t *md)
  173. {
  174. __map_region(md, md->virt_addr);
  175. }
  176. void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
  177. u32 type, u64 attribute)
  178. {
  179. unsigned long last_map_pfn;
  180. if (type == EFI_MEMORY_MAPPED_IO)
  181. return ioremap(phys_addr, size);
  182. last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
  183. if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
  184. unsigned long top = last_map_pfn << PAGE_SHIFT;
  185. efi_ioremap(top, size - (top - phys_addr), type, attribute);
  186. }
  187. if (!(attribute & EFI_MEMORY_WB))
  188. efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
  189. return (void __iomem *)__va(phys_addr);
  190. }
  191. void __init parse_efi_setup(u64 phys_addr, u32 data_len)
  192. {
  193. efi_setup = phys_addr + sizeof(struct setup_data);
  194. }
  195. void __init efi_runtime_mkexec(void)
  196. {
  197. if (!efi_enabled(EFI_OLD_MEMMAP))
  198. return;
  199. if (__supported_pte_mask & _PAGE_NX)
  200. runtime_code_page_mkexec();
  201. }