1 // SPDX-License-Identifier: GPL-2.0
10 static efi_guid_t cpu_state_guid = LINUX_EFI_ARM_CPU_STATE_TABLE_GUID;
12 struct efi_arm_entry_state *efi_entry_state;
14 static void get_cpu_state(u32 *cpsr, u32 *sctlr)
16 asm("mrs %0, cpsr" : "=r"(*cpsr));
17 if ((*cpsr & MODE_MASK) == HYP_MODE)
18 asm("mrc p15, 4, %0, c1, c0, 0" : "=r"(*sctlr));
20 asm("mrc p15, 0, %0, c1, c0, 0" : "=r"(*sctlr));
23 efi_status_t check_platform_features(void)
29 get_cpu_state(&cpsr, &sctlr);
31 efi_info("Entering in %s mode with MMU %sabled\n",
32 ((cpsr & MODE_MASK) == HYP_MODE) ? "HYP" : "SVC",
33 (sctlr & 1) ? "en" : "dis");
35 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
36 sizeof(*efi_entry_state),
37 (void **)&efi_entry_state);
38 if (status != EFI_SUCCESS) {
39 efi_err("allocate_pool() failed\n");
43 efi_entry_state->cpsr_before_ebs = cpsr;
44 efi_entry_state->sctlr_before_ebs = sctlr;
46 status = efi_bs_call(install_configuration_table, &cpu_state_guid,
48 if (status != EFI_SUCCESS) {
49 efi_err("install_configuration_table() failed\n");
53 /* non-LPAE kernels can run anywhere */
54 if (!IS_ENABLED(CONFIG_ARM_LPAE))
57 /* LPAE kernels need compatible hardware */
58 block = cpuid_feature_extract(CPUID_EXT_MMFR0, 0);
60 efi_err("This LPAE kernel is not supported by your CPU\n");
61 status = EFI_UNSUPPORTED;
67 efi_bs_call(install_configuration_table, &cpu_state_guid, NULL);
69 efi_bs_call(free_pool, efi_entry_state);
73 void efi_handle_post_ebs_state(void)
75 get_cpu_state(&efi_entry_state->cpsr_after_ebs,
76 &efi_entry_state->sctlr_after_ebs);
79 static efi_guid_t screen_info_guid = LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID;
81 struct screen_info *alloc_screen_info(void)
83 struct screen_info *si;
87 * Unlike on arm64, where we can directly fill out the screen_info
88 * structure from the stub, we need to allocate a buffer to hold
89 * its contents while we hand over to the kernel proper from the
92 status = efi_bs_call(allocate_pool, EFI_RUNTIME_SERVICES_DATA,
93 sizeof(*si), (void **)&si);
95 if (status != EFI_SUCCESS)
98 status = efi_bs_call(install_configuration_table,
99 &screen_info_guid, si);
100 if (status == EFI_SUCCESS)
103 efi_bs_call(free_pool, si);
107 void free_screen_info(struct screen_info *si)
112 efi_bs_call(install_configuration_table, &screen_info_guid, NULL);
113 efi_bs_call(free_pool, si);
116 static efi_status_t reserve_kernel_base(unsigned long dram_base,
117 unsigned long *reserve_addr,
118 unsigned long *reserve_size)
120 efi_physical_addr_t alloc_addr;
121 efi_memory_desc_t *memory_map;
122 unsigned long nr_pages, map_size, desc_size, buff_size;
126 struct efi_boot_memmap map = {
128 .map_size = &map_size,
129 .desc_size = &desc_size,
132 .buff_size = &buff_size,
136 * Reserve memory for the uncompressed kernel image. This is
137 * all that prevents any future allocations from conflicting
138 * with the kernel. Since we can't tell from the compressed
139 * image how much DRAM the kernel actually uses (due to BSS
140 * size uncertainty) we allocate the maximum possible size.
141 * Do this very early, as prints can cause memory allocations
142 * that may conflict with this.
144 alloc_addr = dram_base + MAX_UNCOMP_KERNEL_SIZE;
145 nr_pages = MAX_UNCOMP_KERNEL_SIZE / EFI_PAGE_SIZE;
146 status = efi_bs_call(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS,
147 EFI_BOOT_SERVICES_DATA, nr_pages, &alloc_addr);
148 if (status == EFI_SUCCESS) {
149 if (alloc_addr == dram_base) {
150 *reserve_addr = alloc_addr;
151 *reserve_size = MAX_UNCOMP_KERNEL_SIZE;
155 * If we end up here, the allocation succeeded but starts below
156 * dram_base. This can only occur if the real base of DRAM is
157 * not a multiple of 128 MB, in which case dram_base will have
158 * been rounded up. Since this implies that a part of the region
159 * was already occupied, we need to fall through to the code
160 * below to ensure that the existing allocations don't conflict.
161 * For this reason, we use EFI_BOOT_SERVICES_DATA above and not
162 * EFI_LOADER_DATA, which we wouldn't able to distinguish from
163 * allocations that we want to disallow.
168 * If the allocation above failed, we may still be able to proceed:
169 * if the only allocations in the region are of types that will be
170 * released to the OS after ExitBootServices(), the decompressor can
171 * safely overwrite them.
173 status = efi_get_memory_map(&map);
174 if (status != EFI_SUCCESS) {
175 efi_err("reserve_kernel_base(): Unable to retrieve memory map.\n");
179 for (l = 0; l < map_size; l += desc_size) {
180 efi_memory_desc_t *desc;
183 desc = (void *)memory_map + l;
184 start = desc->phys_addr;
185 end = start + desc->num_pages * EFI_PAGE_SIZE;
187 /* Skip if entry does not intersect with region */
188 if (start >= dram_base + MAX_UNCOMP_KERNEL_SIZE ||
192 switch (desc->type) {
193 case EFI_BOOT_SERVICES_CODE:
194 case EFI_BOOT_SERVICES_DATA:
195 /* Ignore types that are released to the OS anyway */
198 case EFI_CONVENTIONAL_MEMORY:
199 /* Skip soft reserved conventional memory */
200 if (efi_soft_reserve_enabled() &&
201 (desc->attribute & EFI_MEMORY_SP))
205 * Reserve the intersection between this entry and the
208 start = max(start, (u64)dram_base);
209 end = min(end, (u64)dram_base + MAX_UNCOMP_KERNEL_SIZE);
211 status = efi_bs_call(allocate_pages,
212 EFI_ALLOCATE_ADDRESS,
214 (end - start) / EFI_PAGE_SIZE,
216 if (status != EFI_SUCCESS) {
217 efi_err("reserve_kernel_base(): alloc failed.\n");
222 case EFI_LOADER_CODE:
223 case EFI_LOADER_DATA:
225 * These regions may be released and reallocated for
226 * another purpose (including EFI_RUNTIME_SERVICE_DATA)
227 * at any time during the execution of the OS loader,
228 * so we cannot consider them as safe.
232 * Treat any other allocation in the region as unsafe */
233 status = EFI_OUT_OF_RESOURCES;
238 status = EFI_SUCCESS;
240 efi_bs_call(free_pool, memory_map);
244 efi_status_t handle_kernel_image(unsigned long *image_addr,
245 unsigned long *image_size,
246 unsigned long *reserve_addr,
247 unsigned long *reserve_size,
248 unsigned long dram_base,
249 efi_loaded_image_t *image)
251 unsigned long kernel_base;
254 /* use a 16 MiB aligned base for the decompressed kernel */
255 kernel_base = round_up(dram_base, SZ_16M) + TEXT_OFFSET;
258 * Note that some platforms (notably, the Raspberry Pi 2) put
259 * spin-tables and other pieces of firmware at the base of RAM,
260 * abusing the fact that the window of TEXT_OFFSET bytes at the
261 * base of the kernel image is only partially used at the moment.
262 * (Up to 5 pages are used for the swapper page tables)
264 status = reserve_kernel_base(kernel_base - 5 * PAGE_SIZE, reserve_addr,
266 if (status != EFI_SUCCESS) {
267 efi_err("Unable to allocate memory for uncompressed kernel.\n");
271 *image_addr = kernel_base;