blob: 3a9f7d72868863c73c967f7107742f0972cc5b46 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassd2269f22015-08-04 12:34:01 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassd2269f22015-08-04 12:34:01 -06005 */
6
7#include <common.h>
Simon Glass1fa70f82019-11-14 12:57:34 -07008#include <cpu_func.h>
Simon Glassd2269f22015-08-04 12:34:01 -06009#include <efi.h>
10#include <errno.h>
Simon Glass6980b6b2019-11-14 12:57:45 -070011#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Bin Mengdb59dd32018-06-17 05:57:53 -070013#include <usb.h>
Bin Meng106dcfc2018-08-23 08:24:10 -070014#include <asm/bootparam.h>
Bin Meng2f65c072018-08-23 08:24:08 -070015#include <asm/e820.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Bin Meng6bd41092018-06-12 08:36:17 -070017#include <asm/post.h>
Simon Glassd2269f22015-08-04 12:34:01 -060018
19DECLARE_GLOBAL_DATA_PTR;
20
21/*
22 * This function looks for the highest region of memory lower than 4GB which
23 * has enough space for U-Boot where U-Boot is aligned on a page boundary.
24 * It overrides the default implementation found elsewhere which simply
25 * picks the end of ram, wherever that may be. The location of the stack,
26 * the relocation address, and how far U-Boot is moved by relocation are
27 * set in the global data structure.
28 */
29ulong board_get_usable_ram_top(ulong total_size)
30{
31 struct efi_mem_desc *desc, *end;
32 struct efi_entry_memmap *map;
33 int ret, size;
34 uintptr_t dest_addr = 0;
35 struct efi_mem_desc *largest = NULL;
36
37 /*
38 * Find largest area of memory below 4GB. We could
39 * call efi_build_mem_table() for a more accurate picture since it
40 * merges areas together where possible. But that function uses more
41 * pre-relocation memory, and it's not critical that we find the
42 * absolute largest region.
43 */
44 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
45 if (ret) {
46 /* We should have stopped in dram_init(), something is wrong */
47 debug("%s: Missing memory map\n", __func__);
48 goto err;
49 }
50
51 end = (struct efi_mem_desc *)((ulong)map + size);
52 desc = map->desc;
53 for (; desc < end; desc = efi_get_next_mem_desc(map, desc)) {
54 if (desc->type != EFI_CONVENTIONAL_MEMORY ||
55 desc->physical_start >= 1ULL << 32)
56 continue;
57 if (!largest || desc->num_pages > largest->num_pages)
58 largest = desc;
59 }
60
61 /* If no suitable area was found, return an error. */
62 assert(largest);
63 if (!largest || (largest->num_pages << EFI_PAGE_SHIFT) < (2 << 20))
64 goto err;
65
66 dest_addr = largest->physical_start + (largest->num_pages <<
67 EFI_PAGE_SHIFT);
68
69 return (ulong)dest_addr;
70err:
71 panic("No available memory found for relocation");
72 return 0;
73}
74
75int dram_init(void)
76{
77 struct efi_mem_desc *desc, *end;
78 struct efi_entry_memmap *map;
79 int size, ret;
80
81 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
82 if (ret) {
83 printf("Cannot find EFI memory map tables, ret=%d\n", ret);
84
85 return -ENODEV;
86 }
87
88 end = (struct efi_mem_desc *)((ulong)map + size);
89 gd->ram_size = 0;
90 desc = map->desc;
91 for (; desc < end; desc = efi_get_next_mem_desc(map, desc)) {
92 if (desc->type < EFI_MMAP_IO)
93 gd->ram_size += desc->num_pages << EFI_PAGE_SHIFT;
94 }
95
96 return 0;
97}
98
Simon Glass2f949c32017-03-31 08:40:32 -060099int dram_init_banksize(void)
Simon Glassd2269f22015-08-04 12:34:01 -0600100{
101 struct efi_mem_desc *desc, *end;
102 struct efi_entry_memmap *map;
103 int ret, size;
104 int num_banks;
105
106 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
107 if (ret) {
108 /* We should have stopped in dram_init(), something is wrong */
109 debug("%s: Missing memory map\n", __func__);
Simon Glass2f949c32017-03-31 08:40:32 -0600110 return -ENXIO;
Simon Glassd2269f22015-08-04 12:34:01 -0600111 }
112 end = (struct efi_mem_desc *)((ulong)map + size);
113 desc = map->desc;
114 for (num_banks = 0;
115 desc < end && num_banks < CONFIG_NR_DRAM_BANKS;
116 desc = efi_get_next_mem_desc(map, desc)) {
117 /*
Bin Meng7eb28a52018-06-22 01:38:30 -0700118 * We only use conventional memory and ignore
Simon Glassd2269f22015-08-04 12:34:01 -0600119 * anything less than 1MB.
120 */
121 if (desc->type != EFI_CONVENTIONAL_MEMORY ||
Simon Glassd2269f22015-08-04 12:34:01 -0600122 (desc->num_pages << EFI_PAGE_SHIFT) < 1 << 20)
123 continue;
124 gd->bd->bi_dram[num_banks].start = desc->physical_start;
125 gd->bd->bi_dram[num_banks].size = desc->num_pages <<
126 EFI_PAGE_SHIFT;
127 num_banks++;
128 }
Simon Glass2f949c32017-03-31 08:40:32 -0600129
130 return 0;
Simon Glassd2269f22015-08-04 12:34:01 -0600131}
132
Bin Meng6bd41092018-06-12 08:36:17 -0700133int arch_cpu_init(void)
134{
135 post_code(POST_CPU_INIT);
136
137 return x86_cpu_init_f();
138}
139
Simon Glassee7c36f2017-03-28 10:27:30 -0600140int checkcpu(void)
141{
142 return 0;
143}
144
Simon Glassd2269f22015-08-04 12:34:01 -0600145int print_cpuinfo(void)
146{
147 return default_print_cpuinfo();
148}
149
150/* Find any available tables and copy them to a safe place */
151int reserve_arch(void)
152{
153 struct efi_info_hdr *hdr;
154
155 debug("table=%lx\n", gd->arch.table);
156 if (!gd->arch.table)
157 return 0;
158
159 hdr = (struct efi_info_hdr *)gd->arch.table;
160
161 gd->start_addr_sp -= hdr->total_size;
162 memcpy((void *)gd->start_addr_sp, hdr, hdr->total_size);
163 debug("Stashing EFI table at %lx to %lx, size %x\n",
164 gd->arch.table, gd->start_addr_sp, hdr->total_size);
165 gd->arch.table = gd->start_addr_sp;
166
167 return 0;
168}
Bin Mengdb59dd32018-06-17 05:57:53 -0700169
170int last_stage_init(void)
171{
172 /* start usb so that usb keyboard can be used as input device */
Bin Menga7bb8ef2018-11-29 20:07:13 -0800173 if (CONFIG_IS_ENABLED(USB_KEYBOARD))
174 usb_init();
Bin Mengdb59dd32018-06-17 05:57:53 -0700175
176 return 0;
177}
Bin Meng2f65c072018-08-23 08:24:08 -0700178
179unsigned int install_e820_map(unsigned int max_entries,
180 struct e820_entry *entries)
181{
182 struct efi_mem_desc *desc, *end;
183 struct efi_entry_memmap *map;
184 int size, ret;
185 efi_physical_addr_t last_end_addr = 0;
186 struct e820_entry *last_entry = NULL;
187 __u32 e820_type;
188 unsigned int num_entries = 0;
189
190 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
191 if (ret) {
192 printf("Cannot find EFI memory map tables, ret=%d\n", ret);
193
194 return -ENODEV;
195 }
196
197 end = (struct efi_mem_desc *)((ulong)map + size);
198 for (desc = map->desc; desc < end;
199 desc = efi_get_next_mem_desc(map, desc)) {
200 if (desc->num_pages == 0)
201 continue;
202
203 switch (desc->type) {
204 case EFI_LOADER_CODE:
205 case EFI_LOADER_DATA:
206 case EFI_BOOT_SERVICES_CODE:
207 case EFI_BOOT_SERVICES_DATA:
208 case EFI_CONVENTIONAL_MEMORY:
209 e820_type = E820_RAM;
210 break;
211
212 case EFI_RESERVED_MEMORY_TYPE:
213 case EFI_RUNTIME_SERVICES_CODE:
214 case EFI_RUNTIME_SERVICES_DATA:
215 case EFI_MMAP_IO:
216 case EFI_MMAP_IO_PORT:
217 case EFI_PAL_CODE:
218 e820_type = E820_RESERVED;
219 break;
220
221 case EFI_ACPI_RECLAIM_MEMORY:
222 e820_type = E820_ACPI;
223 break;
224
225 case EFI_ACPI_MEMORY_NVS:
226 e820_type = E820_NVS;
227 break;
228
229 case EFI_UNUSABLE_MEMORY:
230 e820_type = E820_UNUSABLE;
231 break;
232
233 default:
234 printf("Invalid EFI memory descriptor type (0x%x)!\n",
235 desc->type);
236 continue;
237 }
238
239 if (last_entry != NULL && last_entry->type == e820_type &&
240 desc->physical_start == last_end_addr) {
241 last_entry->size += (desc->num_pages << EFI_PAGE_SHIFT);
242 last_end_addr += (desc->num_pages << EFI_PAGE_SHIFT);
243 } else {
244 if (num_entries >= E820MAX)
245 break;
246
247 entries[num_entries].addr = desc->physical_start;
248 entries[num_entries].size = desc->num_pages;
249 entries[num_entries].size <<= EFI_PAGE_SHIFT;
250 entries[num_entries].type = e820_type;
251 last_entry = &entries[num_entries];
252 last_end_addr = last_entry->addr + last_entry->size;
253 num_entries++;
254 }
255 }
256
257 return num_entries;
258}
Bin Meng106dcfc2018-08-23 08:24:10 -0700259
260void setup_efi_info(struct efi_info *efi_info)
261{
262 struct efi_entry_systable *table;
263 struct efi_entry_memmap *map;
264 char *signature;
265 int size, ret;
266
267 memset(efi_info, 0, sizeof(struct efi_info));
268
269 ret = efi_info_get(EFIET_SYS_TABLE, (void **)&table, &size);
270 if (ret) {
271 printf("Cannot find EFI system table, ret=%d\n", ret);
272 return;
273 }
274 efi_info->efi_systab = (u32)(table->sys_table);
275
276 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
277 if (ret) {
278 printf("Cannot find EFI memory map tables, ret=%d\n", ret);
279 return;
280 }
281 efi_info->efi_memdesc_size = map->desc_size;
282 efi_info->efi_memdesc_version = map->version;
Simon Glass912e06f2021-09-24 18:30:21 -0600283 efi_info->efi_memmap = (ulong)(map->desc);
Bin Meng106dcfc2018-08-23 08:24:10 -0700284 efi_info->efi_memmap_size = size - sizeof(struct efi_entry_memmap);
285
286#ifdef CONFIG_EFI_STUB_64BIT
287 efi_info->efi_systab_hi = table->sys_table >> 32;
Simon Glass912e06f2021-09-24 18:30:21 -0600288 efi_info->efi_memmap_hi = (u64)(ulong)map->desc >> 32;
Bin Meng106dcfc2018-08-23 08:24:10 -0700289 signature = EFI64_LOADER_SIGNATURE;
290#else
291 signature = EFI32_LOADER_SIGNATURE;
292#endif
293 memcpy(&efi_info->efi_loader_signature, signature, 4);
294}
Simon Glass912e06f2021-09-24 18:30:21 -0600295
296void efi_show_bdinfo(void)
297{
298 struct efi_entry_systable *table = NULL;
299 int size, ret;
300
301 ret = efi_info_get(EFIET_SYS_TABLE, (void **)&table, &size);
302 bdinfo_print_num_l("efi_table", (ulong)table);
303}