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