efi.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2015 Linaro Ltd <[email protected]>
  4. */
  5. #include <linux/efi.h>
  6. #include <linux/memblock.h>
  7. #include <asm/efi.h>
  8. #include <asm/mach/map.h>
  9. #include <asm/mmu_context.h>
  10. static int __init set_permissions(pte_t *ptep, unsigned long addr, void *data)
  11. {
  12. efi_memory_desc_t *md = data;
  13. pte_t pte = *ptep;
  14. if (md->attribute & EFI_MEMORY_RO)
  15. pte = set_pte_bit(pte, __pgprot(L_PTE_RDONLY));
  16. if (md->attribute & EFI_MEMORY_XP)
  17. pte = set_pte_bit(pte, __pgprot(L_PTE_XN));
  18. set_pte_ext(ptep, pte, PTE_EXT_NG);
  19. return 0;
  20. }
  21. int __init efi_set_mapping_permissions(struct mm_struct *mm,
  22. efi_memory_desc_t *md)
  23. {
  24. unsigned long base, size;
  25. base = md->virt_addr;
  26. size = md->num_pages << EFI_PAGE_SHIFT;
  27. /*
  28. * We can only use apply_to_page_range() if we can guarantee that the
  29. * entire region was mapped using pages. This should be the case if the
  30. * region does not cover any naturally aligned SECTION_SIZE sized
  31. * blocks.
  32. */
  33. if (round_down(base + size, SECTION_SIZE) <
  34. round_up(base, SECTION_SIZE) + SECTION_SIZE)
  35. return apply_to_page_range(mm, base, size, set_permissions, md);
  36. return 0;
  37. }
  38. int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)
  39. {
  40. struct map_desc desc = {
  41. .virtual = md->virt_addr,
  42. .pfn = __phys_to_pfn(md->phys_addr),
  43. .length = md->num_pages * EFI_PAGE_SIZE,
  44. };
  45. /*
  46. * Order is important here: memory regions may have all of the
  47. * bits below set (and usually do), so we check them in order of
  48. * preference.
  49. */
  50. if (md->attribute & EFI_MEMORY_WB)
  51. desc.type = MT_MEMORY_RWX;
  52. else if (md->attribute & EFI_MEMORY_WT)
  53. desc.type = MT_MEMORY_RWX_NONCACHED;
  54. else if (md->attribute & EFI_MEMORY_WC)
  55. desc.type = MT_DEVICE_WC;
  56. else
  57. desc.type = MT_DEVICE;
  58. create_mapping_late(mm, &desc, true);
  59. /*
  60. * If stricter permissions were specified, apply them now.
  61. */
  62. if (md->attribute & (EFI_MEMORY_RO | EFI_MEMORY_XP))
  63. return efi_set_mapping_permissions(mm, md);
  64. return 0;
  65. }
  66. static unsigned long __initdata screen_info_table = EFI_INVALID_TABLE_ADDR;
  67. static unsigned long __initdata cpu_state_table = EFI_INVALID_TABLE_ADDR;
  68. const efi_config_table_type_t efi_arch_tables[] __initconst = {
  69. {LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID, &screen_info_table},
  70. {LINUX_EFI_ARM_CPU_STATE_TABLE_GUID, &cpu_state_table},
  71. {}
  72. };
  73. static void __init load_screen_info_table(void)
  74. {
  75. struct screen_info *si;
  76. if (screen_info_table != EFI_INVALID_TABLE_ADDR) {
  77. si = early_memremap_ro(screen_info_table, sizeof(*si));
  78. if (!si) {
  79. pr_err("Could not map screen_info config table\n");
  80. return;
  81. }
  82. screen_info = *si;
  83. early_memunmap(si, sizeof(*si));
  84. /* dummycon on ARM needs non-zero values for columns/lines */
  85. screen_info.orig_video_cols = 80;
  86. screen_info.orig_video_lines = 25;
  87. if (memblock_is_map_memory(screen_info.lfb_base))
  88. memblock_mark_nomap(screen_info.lfb_base,
  89. screen_info.lfb_size);
  90. }
  91. }
  92. static void __init load_cpu_state_table(void)
  93. {
  94. if (cpu_state_table != EFI_INVALID_TABLE_ADDR) {
  95. struct efi_arm_entry_state *state;
  96. bool dump_state = true;
  97. state = early_memremap_ro(cpu_state_table,
  98. sizeof(struct efi_arm_entry_state));
  99. if (state == NULL) {
  100. pr_warn("Unable to map CPU entry state table.\n");
  101. return;
  102. }
  103. if ((state->sctlr_before_ebs & 1) == 0)
  104. pr_warn(FW_BUG "EFI stub was entered with MMU and Dcache disabled, please fix your firmware!\n");
  105. else if ((state->sctlr_after_ebs & 1) == 0)
  106. pr_warn(FW_BUG "ExitBootServices() returned with MMU and Dcache disabled, please fix your firmware!\n");
  107. else
  108. dump_state = false;
  109. if (dump_state || efi_enabled(EFI_DBG)) {
  110. pr_info("CPSR at EFI stub entry : 0x%08x\n",
  111. state->cpsr_before_ebs);
  112. pr_info("SCTLR at EFI stub entry : 0x%08x\n",
  113. state->sctlr_before_ebs);
  114. pr_info("CPSR after ExitBootServices() : 0x%08x\n",
  115. state->cpsr_after_ebs);
  116. pr_info("SCTLR after ExitBootServices(): 0x%08x\n",
  117. state->sctlr_after_ebs);
  118. }
  119. early_memunmap(state, sizeof(struct efi_arm_entry_state));
  120. }
  121. }
  122. void __init arm_efi_init(void)
  123. {
  124. efi_init();
  125. load_screen_info_table();
  126. /* ARM does not permit early mappings to persist across paging_init() */
  127. efi_memmap_unmap();
  128. load_cpu_state_table();
  129. }