efi_64.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <asm/setup.h>
  30. #include <asm/page.h>
  31. #include <asm/e820.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/tlbflush.h>
  34. #include <asm/cacheflush.h>
  35. #include <asm/proto.h>
  36. #include <asm/efi.h>
  37. static pgd_t save_pgd __initdata;
  38. static unsigned long efi_flags __initdata;
  39. static void __init early_mapping_set_exec(unsigned long start,
  40. unsigned long end,
  41. int executable)
  42. {
  43. pte_t *kpte;
  44. int level;
  45. while (start < end) {
  46. kpte = lookup_address((unsigned long)__va(start), &level);
  47. BUG_ON(!kpte);
  48. if (executable)
  49. set_pte(kpte, pte_mkexec(*kpte));
  50. else
  51. set_pte(kpte, __pte((pte_val(*kpte) | _PAGE_NX) & \
  52. __supported_pte_mask));
  53. if (pte_huge(*kpte))
  54. start = (start + PMD_SIZE) & PMD_MASK;
  55. else
  56. start = (start + PAGE_SIZE) & PAGE_MASK;
  57. }
  58. }
  59. static void __init early_runtime_code_mapping_set_exec(int executable)
  60. {
  61. efi_memory_desc_t *md;
  62. void *p;
  63. /* Make EFI runtime 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. unsigned long end;
  68. end = md->phys_addr + (md->num_pages << PAGE_SHIFT);
  69. early_mapping_set_exec(md->phys_addr, end, executable);
  70. }
  71. }
  72. }
  73. void __init efi_call_phys_prelog(void)
  74. {
  75. unsigned long vaddress;
  76. local_irq_save(efi_flags);
  77. early_runtime_code_mapping_set_exec(1);
  78. vaddress = (unsigned long)__va(0x0UL);
  79. save_pgd = *pgd_offset_k(0x0UL);
  80. set_pgd(pgd_offset_k(0x0UL), *pgd_offset_k(vaddress));
  81. __flush_tlb_all();
  82. }
  83. void __init efi_call_phys_epilog(void)
  84. {
  85. /*
  86. * After the lock is released, the original page table is restored.
  87. */
  88. set_pgd(pgd_offset_k(0x0UL), save_pgd);
  89. early_runtime_code_mapping_set_exec(0);
  90. __flush_tlb_all();
  91. local_irq_restore(efi_flags);
  92. }
  93. /*
  94. * We need to map the EFI memory map again after init_memory_mapping().
  95. */
  96. void __init efi_map_memmap(void)
  97. {
  98. memmap.map = __va(memmap.phys_map);
  99. memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
  100. }
  101. void __init efi_reserve_bootmem(void)
  102. {
  103. reserve_bootmem_generic((unsigned long)memmap.phys_map,
  104. memmap.nr_map * memmap.desc_size);
  105. }
  106. void __init runtime_code_page_mkexec(void)
  107. {
  108. efi_memory_desc_t *md;
  109. void *p;
  110. /* Make EFI runtime service code area executable */
  111. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  112. md = p;
  113. if (md->type == EFI_RUNTIME_SERVICES_CODE)
  114. change_page_attr_addr(md->virt_addr,
  115. md->num_pages,
  116. PAGE_KERNEL_EXEC);
  117. }
  118. __flush_tlb_all();
  119. }
  120. void __iomem * __init efi_ioremap(unsigned long offset,
  121. unsigned long size)
  122. {
  123. static unsigned pages_mapped;
  124. unsigned long last_addr;
  125. unsigned i, pages;
  126. last_addr = offset + size - 1;
  127. offset &= PAGE_MASK;
  128. pages = (PAGE_ALIGN(last_addr) - offset) >> PAGE_SHIFT;
  129. if (pages_mapped + pages > MAX_EFI_IO_PAGES)
  130. return NULL;
  131. for (i = 0; i < pages; i++) {
  132. set_fixmap_nocache(FIX_EFI_IO_MAP_FIRST_PAGE - pages_mapped,
  133. offset);
  134. offset += PAGE_SIZE;
  135. pages_mapped++;
  136. }
  137. return (void __iomem *)__fix_to_virt(FIX_EFI_IO_MAP_FIRST_PAGE - \
  138. (pages_mapped - pages));
  139. }