.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
---|
1 | 2 | /* |
---|
2 | 3 | * Copyright (C) 2013 Linaro Ltd; <roy.franz@linaro.org> |
---|
3 | | - * |
---|
4 | | - * This program is free software; you can redistribute it and/or modify |
---|
5 | | - * it under the terms of the GNU General Public License version 2 as |
---|
6 | | - * published by the Free Software Foundation. |
---|
7 | | - * |
---|
8 | 4 | */ |
---|
9 | 5 | #include <linux/efi.h> |
---|
10 | 6 | #include <asm/efi.h> |
---|
11 | 7 | |
---|
12 | 8 | #include "efistub.h" |
---|
13 | 9 | |
---|
14 | | -efi_status_t check_platform_features(efi_system_table_t *sys_table_arg) |
---|
| 10 | +static efi_guid_t cpu_state_guid = LINUX_EFI_ARM_CPU_STATE_TABLE_GUID; |
---|
| 11 | + |
---|
| 12 | +struct efi_arm_entry_state *efi_entry_state; |
---|
| 13 | + |
---|
| 14 | +static void get_cpu_state(u32 *cpsr, u32 *sctlr) |
---|
15 | 15 | { |
---|
| 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)); |
---|
| 19 | + else |
---|
| 20 | + asm("mrc p15, 0, %0, c1, c0, 0" : "=r"(*sctlr)); |
---|
| 21 | +} |
---|
| 22 | + |
---|
| 23 | +efi_status_t check_platform_features(void) |
---|
| 24 | +{ |
---|
| 25 | + efi_status_t status; |
---|
| 26 | + u32 cpsr, sctlr; |
---|
16 | 27 | int block; |
---|
| 28 | + |
---|
| 29 | + get_cpu_state(&cpsr, &sctlr); |
---|
| 30 | + |
---|
| 31 | + efi_info("Entering in %s mode with MMU %sabled\n", |
---|
| 32 | + ((cpsr & MODE_MASK) == HYP_MODE) ? "HYP" : "SVC", |
---|
| 33 | + (sctlr & 1) ? "en" : "dis"); |
---|
| 34 | + |
---|
| 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"); |
---|
| 40 | + return status; |
---|
| 41 | + } |
---|
| 42 | + |
---|
| 43 | + efi_entry_state->cpsr_before_ebs = cpsr; |
---|
| 44 | + efi_entry_state->sctlr_before_ebs = sctlr; |
---|
| 45 | + |
---|
| 46 | + status = efi_bs_call(install_configuration_table, &cpu_state_guid, |
---|
| 47 | + efi_entry_state); |
---|
| 48 | + if (status != EFI_SUCCESS) { |
---|
| 49 | + efi_err("install_configuration_table() failed\n"); |
---|
| 50 | + goto free_state; |
---|
| 51 | + } |
---|
17 | 52 | |
---|
18 | 53 | /* non-LPAE kernels can run anywhere */ |
---|
19 | 54 | if (!IS_ENABLED(CONFIG_ARM_LPAE)) |
---|
.. | .. |
---|
22 | 57 | /* LPAE kernels need compatible hardware */ |
---|
23 | 58 | block = cpuid_feature_extract(CPUID_EXT_MMFR0, 0); |
---|
24 | 59 | if (block < 5) { |
---|
25 | | - pr_efi_err(sys_table_arg, "This LPAE kernel is not supported by your CPU\n"); |
---|
26 | | - return EFI_UNSUPPORTED; |
---|
| 60 | + efi_err("This LPAE kernel is not supported by your CPU\n"); |
---|
| 61 | + status = EFI_UNSUPPORTED; |
---|
| 62 | + goto drop_table; |
---|
27 | 63 | } |
---|
28 | 64 | return EFI_SUCCESS; |
---|
| 65 | + |
---|
| 66 | +drop_table: |
---|
| 67 | + efi_bs_call(install_configuration_table, &cpu_state_guid, NULL); |
---|
| 68 | +free_state: |
---|
| 69 | + efi_bs_call(free_pool, efi_entry_state); |
---|
| 70 | + return status; |
---|
| 71 | +} |
---|
| 72 | + |
---|
| 73 | +void efi_handle_post_ebs_state(void) |
---|
| 74 | +{ |
---|
| 75 | + get_cpu_state(&efi_entry_state->cpsr_after_ebs, |
---|
| 76 | + &efi_entry_state->sctlr_after_ebs); |
---|
29 | 77 | } |
---|
30 | 78 | |
---|
31 | 79 | static efi_guid_t screen_info_guid = LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID; |
---|
32 | 80 | |
---|
33 | | -struct screen_info *alloc_screen_info(efi_system_table_t *sys_table_arg) |
---|
| 81 | +struct screen_info *alloc_screen_info(void) |
---|
34 | 82 | { |
---|
35 | 83 | struct screen_info *si; |
---|
36 | 84 | efi_status_t status; |
---|
.. | .. |
---|
41 | 89 | * its contents while we hand over to the kernel proper from the |
---|
42 | 90 | * decompressor. |
---|
43 | 91 | */ |
---|
44 | | - status = efi_call_early(allocate_pool, EFI_RUNTIME_SERVICES_DATA, |
---|
45 | | - sizeof(*si), (void **)&si); |
---|
| 92 | + status = efi_bs_call(allocate_pool, EFI_RUNTIME_SERVICES_DATA, |
---|
| 93 | + sizeof(*si), (void **)&si); |
---|
46 | 94 | |
---|
47 | 95 | if (status != EFI_SUCCESS) |
---|
48 | 96 | return NULL; |
---|
49 | 97 | |
---|
50 | | - status = efi_call_early(install_configuration_table, |
---|
51 | | - &screen_info_guid, si); |
---|
| 98 | + status = efi_bs_call(install_configuration_table, |
---|
| 99 | + &screen_info_guid, si); |
---|
52 | 100 | if (status == EFI_SUCCESS) |
---|
53 | 101 | return si; |
---|
54 | 102 | |
---|
55 | | - efi_call_early(free_pool, si); |
---|
| 103 | + efi_bs_call(free_pool, si); |
---|
56 | 104 | return NULL; |
---|
57 | 105 | } |
---|
58 | 106 | |
---|
59 | | -void free_screen_info(efi_system_table_t *sys_table_arg, struct screen_info *si) |
---|
| 107 | +void free_screen_info(struct screen_info *si) |
---|
60 | 108 | { |
---|
61 | 109 | if (!si) |
---|
62 | 110 | return; |
---|
63 | 111 | |
---|
64 | | - efi_call_early(install_configuration_table, &screen_info_guid, NULL); |
---|
65 | | - efi_call_early(free_pool, si); |
---|
| 112 | + efi_bs_call(install_configuration_table, &screen_info_guid, NULL); |
---|
| 113 | + efi_bs_call(free_pool, si); |
---|
66 | 114 | } |
---|
67 | 115 | |
---|
68 | | -static efi_status_t reserve_kernel_base(efi_system_table_t *sys_table_arg, |
---|
69 | | - unsigned long dram_base, |
---|
70 | | - unsigned long *reserve_addr, |
---|
71 | | - unsigned long *reserve_size) |
---|
72 | | -{ |
---|
73 | | - efi_physical_addr_t alloc_addr; |
---|
74 | | - efi_memory_desc_t *memory_map; |
---|
75 | | - unsigned long nr_pages, map_size, desc_size, buff_size; |
---|
76 | | - efi_status_t status; |
---|
77 | | - unsigned long l; |
---|
78 | | - |
---|
79 | | - struct efi_boot_memmap map = { |
---|
80 | | - .map = &memory_map, |
---|
81 | | - .map_size = &map_size, |
---|
82 | | - .desc_size = &desc_size, |
---|
83 | | - .desc_ver = NULL, |
---|
84 | | - .key_ptr = NULL, |
---|
85 | | - .buff_size = &buff_size, |
---|
86 | | - }; |
---|
87 | | - |
---|
88 | | - /* |
---|
89 | | - * Reserve memory for the uncompressed kernel image. This is |
---|
90 | | - * all that prevents any future allocations from conflicting |
---|
91 | | - * with the kernel. Since we can't tell from the compressed |
---|
92 | | - * image how much DRAM the kernel actually uses (due to BSS |
---|
93 | | - * size uncertainty) we allocate the maximum possible size. |
---|
94 | | - * Do this very early, as prints can cause memory allocations |
---|
95 | | - * that may conflict with this. |
---|
96 | | - */ |
---|
97 | | - alloc_addr = dram_base + MAX_UNCOMP_KERNEL_SIZE; |
---|
98 | | - nr_pages = MAX_UNCOMP_KERNEL_SIZE / EFI_PAGE_SIZE; |
---|
99 | | - status = efi_call_early(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS, |
---|
100 | | - EFI_BOOT_SERVICES_DATA, nr_pages, &alloc_addr); |
---|
101 | | - if (status == EFI_SUCCESS) { |
---|
102 | | - if (alloc_addr == dram_base) { |
---|
103 | | - *reserve_addr = alloc_addr; |
---|
104 | | - *reserve_size = MAX_UNCOMP_KERNEL_SIZE; |
---|
105 | | - return EFI_SUCCESS; |
---|
106 | | - } |
---|
107 | | - /* |
---|
108 | | - * If we end up here, the allocation succeeded but starts below |
---|
109 | | - * dram_base. This can only occur if the real base of DRAM is |
---|
110 | | - * not a multiple of 128 MB, in which case dram_base will have |
---|
111 | | - * been rounded up. Since this implies that a part of the region |
---|
112 | | - * was already occupied, we need to fall through to the code |
---|
113 | | - * below to ensure that the existing allocations don't conflict. |
---|
114 | | - * For this reason, we use EFI_BOOT_SERVICES_DATA above and not |
---|
115 | | - * EFI_LOADER_DATA, which we wouldn't able to distinguish from |
---|
116 | | - * allocations that we want to disallow. |
---|
117 | | - */ |
---|
118 | | - } |
---|
119 | | - |
---|
120 | | - /* |
---|
121 | | - * If the allocation above failed, we may still be able to proceed: |
---|
122 | | - * if the only allocations in the region are of types that will be |
---|
123 | | - * released to the OS after ExitBootServices(), the decompressor can |
---|
124 | | - * safely overwrite them. |
---|
125 | | - */ |
---|
126 | | - status = efi_get_memory_map(sys_table_arg, &map); |
---|
127 | | - if (status != EFI_SUCCESS) { |
---|
128 | | - pr_efi_err(sys_table_arg, |
---|
129 | | - "reserve_kernel_base(): Unable to retrieve memory map.\n"); |
---|
130 | | - return status; |
---|
131 | | - } |
---|
132 | | - |
---|
133 | | - for (l = 0; l < map_size; l += desc_size) { |
---|
134 | | - efi_memory_desc_t *desc; |
---|
135 | | - u64 start, end; |
---|
136 | | - |
---|
137 | | - desc = (void *)memory_map + l; |
---|
138 | | - start = desc->phys_addr; |
---|
139 | | - end = start + desc->num_pages * EFI_PAGE_SIZE; |
---|
140 | | - |
---|
141 | | - /* Skip if entry does not intersect with region */ |
---|
142 | | - if (start >= dram_base + MAX_UNCOMP_KERNEL_SIZE || |
---|
143 | | - end <= dram_base) |
---|
144 | | - continue; |
---|
145 | | - |
---|
146 | | - switch (desc->type) { |
---|
147 | | - case EFI_BOOT_SERVICES_CODE: |
---|
148 | | - case EFI_BOOT_SERVICES_DATA: |
---|
149 | | - /* Ignore types that are released to the OS anyway */ |
---|
150 | | - continue; |
---|
151 | | - |
---|
152 | | - case EFI_CONVENTIONAL_MEMORY: |
---|
153 | | - /* |
---|
154 | | - * Reserve the intersection between this entry and the |
---|
155 | | - * region. |
---|
156 | | - */ |
---|
157 | | - start = max(start, (u64)dram_base); |
---|
158 | | - end = min(end, (u64)dram_base + MAX_UNCOMP_KERNEL_SIZE); |
---|
159 | | - |
---|
160 | | - status = efi_call_early(allocate_pages, |
---|
161 | | - EFI_ALLOCATE_ADDRESS, |
---|
162 | | - EFI_LOADER_DATA, |
---|
163 | | - (end - start) / EFI_PAGE_SIZE, |
---|
164 | | - &start); |
---|
165 | | - if (status != EFI_SUCCESS) { |
---|
166 | | - pr_efi_err(sys_table_arg, |
---|
167 | | - "reserve_kernel_base(): alloc failed.\n"); |
---|
168 | | - goto out; |
---|
169 | | - } |
---|
170 | | - break; |
---|
171 | | - |
---|
172 | | - case EFI_LOADER_CODE: |
---|
173 | | - case EFI_LOADER_DATA: |
---|
174 | | - /* |
---|
175 | | - * These regions may be released and reallocated for |
---|
176 | | - * another purpose (including EFI_RUNTIME_SERVICE_DATA) |
---|
177 | | - * at any time during the execution of the OS loader, |
---|
178 | | - * so we cannot consider them as safe. |
---|
179 | | - */ |
---|
180 | | - default: |
---|
181 | | - /* |
---|
182 | | - * Treat any other allocation in the region as unsafe */ |
---|
183 | | - status = EFI_OUT_OF_RESOURCES; |
---|
184 | | - goto out; |
---|
185 | | - } |
---|
186 | | - } |
---|
187 | | - |
---|
188 | | - status = EFI_SUCCESS; |
---|
189 | | -out: |
---|
190 | | - efi_call_early(free_pool, memory_map); |
---|
191 | | - return status; |
---|
192 | | -} |
---|
193 | | - |
---|
194 | | -efi_status_t handle_kernel_image(efi_system_table_t *sys_table, |
---|
195 | | - unsigned long *image_addr, |
---|
| 116 | +efi_status_t handle_kernel_image(unsigned long *image_addr, |
---|
196 | 117 | unsigned long *image_size, |
---|
197 | 118 | unsigned long *reserve_addr, |
---|
198 | 119 | unsigned long *reserve_size, |
---|
199 | | - unsigned long dram_base, |
---|
200 | 120 | efi_loaded_image_t *image) |
---|
201 | 121 | { |
---|
| 122 | + const int slack = TEXT_OFFSET - 5 * PAGE_SIZE; |
---|
| 123 | + int alloc_size = MAX_UNCOMP_KERNEL_SIZE + EFI_PHYS_ALIGN; |
---|
| 124 | + unsigned long alloc_base, kernel_base; |
---|
202 | 125 | efi_status_t status; |
---|
203 | 126 | |
---|
204 | 127 | /* |
---|
205 | | - * Verify that the DRAM base address is compatible with the ARM |
---|
206 | | - * boot protocol, which determines the base of DRAM by masking |
---|
207 | | - * off the low 27 bits of the address at which the zImage is |
---|
208 | | - * loaded. These assumptions are made by the decompressor, |
---|
209 | | - * before any memory map is available. |
---|
| 128 | + * Allocate space for the decompressed kernel as low as possible. |
---|
| 129 | + * The region should be 16 MiB aligned, but the first 'slack' bytes |
---|
| 130 | + * are not used by Linux, so we allow those to be occupied by the |
---|
| 131 | + * firmware. |
---|
210 | 132 | */ |
---|
211 | | - dram_base = round_up(dram_base, SZ_128M); |
---|
212 | | - |
---|
213 | | - status = reserve_kernel_base(sys_table, dram_base, reserve_addr, |
---|
214 | | - reserve_size); |
---|
| 133 | + status = efi_low_alloc_above(alloc_size, EFI_PAGE_SIZE, &alloc_base, 0x0); |
---|
215 | 134 | if (status != EFI_SUCCESS) { |
---|
216 | | - pr_efi_err(sys_table, "Unable to allocate memory for uncompressed kernel.\n"); |
---|
| 135 | + efi_err("Unable to allocate memory for uncompressed kernel.\n"); |
---|
217 | 136 | return status; |
---|
218 | 137 | } |
---|
219 | 138 | |
---|
220 | | - /* |
---|
221 | | - * Relocate the zImage, so that it appears in the lowest 128 MB |
---|
222 | | - * memory window. |
---|
223 | | - */ |
---|
224 | | - *image_size = image->image_size; |
---|
225 | | - status = efi_relocate_kernel(sys_table, image_addr, *image_size, |
---|
226 | | - *image_size, |
---|
227 | | - dram_base + MAX_UNCOMP_KERNEL_SIZE, 0); |
---|
228 | | - if (status != EFI_SUCCESS) { |
---|
229 | | - pr_efi_err(sys_table, "Failed to relocate kernel.\n"); |
---|
230 | | - efi_free(sys_table, *reserve_size, *reserve_addr); |
---|
231 | | - *reserve_size = 0; |
---|
232 | | - return status; |
---|
| 139 | + if ((alloc_base % EFI_PHYS_ALIGN) > slack) { |
---|
| 140 | + /* |
---|
| 141 | + * More than 'slack' bytes are already occupied at the base of |
---|
| 142 | + * the allocation, so we need to advance to the next 16 MiB block. |
---|
| 143 | + */ |
---|
| 144 | + kernel_base = round_up(alloc_base, EFI_PHYS_ALIGN); |
---|
| 145 | + efi_info("Free memory starts at 0x%lx, setting kernel_base to 0x%lx\n", |
---|
| 146 | + alloc_base, kernel_base); |
---|
| 147 | + } else { |
---|
| 148 | + kernel_base = round_down(alloc_base, EFI_PHYS_ALIGN); |
---|
233 | 149 | } |
---|
234 | 150 | |
---|
235 | | - /* |
---|
236 | | - * Check to see if we were able to allocate memory low enough |
---|
237 | | - * in memory. The kernel determines the base of DRAM from the |
---|
238 | | - * address at which the zImage is loaded. |
---|
239 | | - */ |
---|
240 | | - if (*image_addr + *image_size > dram_base + ZIMAGE_OFFSET_LIMIT) { |
---|
241 | | - pr_efi_err(sys_table, "Failed to relocate kernel, no low memory available.\n"); |
---|
242 | | - efi_free(sys_table, *reserve_size, *reserve_addr); |
---|
243 | | - *reserve_size = 0; |
---|
244 | | - efi_free(sys_table, *image_size, *image_addr); |
---|
245 | | - *image_size = 0; |
---|
246 | | - return EFI_LOAD_ERROR; |
---|
| 151 | + *reserve_addr = kernel_base + slack; |
---|
| 152 | + *reserve_size = MAX_UNCOMP_KERNEL_SIZE; |
---|
| 153 | + |
---|
| 154 | + /* now free the parts that we will not use */ |
---|
| 155 | + if (*reserve_addr > alloc_base) { |
---|
| 156 | + efi_bs_call(free_pages, alloc_base, |
---|
| 157 | + (*reserve_addr - alloc_base) / EFI_PAGE_SIZE); |
---|
| 158 | + alloc_size -= *reserve_addr - alloc_base; |
---|
247 | 159 | } |
---|
| 160 | + efi_bs_call(free_pages, *reserve_addr + MAX_UNCOMP_KERNEL_SIZE, |
---|
| 161 | + (alloc_size - MAX_UNCOMP_KERNEL_SIZE) / EFI_PAGE_SIZE); |
---|
| 162 | + |
---|
| 163 | + *image_addr = kernel_base + TEXT_OFFSET; |
---|
| 164 | + *image_size = 0; |
---|
| 165 | + |
---|
| 166 | + efi_debug("image addr == 0x%lx, reserve_addr == 0x%lx\n", |
---|
| 167 | + *image_addr, *reserve_addr); |
---|
| 168 | + |
---|
248 | 169 | return EFI_SUCCESS; |
---|
249 | 170 | } |
---|