blob: 19a25dd640817081c2209110f1189715dd848b2c [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>
Simon Glassc3dcef12021-12-29 11:57:53 -070010#include <efi_api.h>
Simon Glassd2269f22015-08-04 12:34:01 -060011#include <errno.h>
Simon Glass6980b6b2019-11-14 12:57:45 -070012#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Bin Mengdb59dd32018-06-17 05:57:53 -070014#include <usb.h>
Bin Meng106dcfc2018-08-23 08:24:10 -070015#include <asm/bootparam.h>
Bin Meng2f65c072018-08-23 08:24:08 -070016#include <asm/e820.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Bin Meng6bd41092018-06-12 08:36:17 -070018#include <asm/post.h>
Simon Glassd2269f22015-08-04 12:34:01 -060019
20DECLARE_GLOBAL_DATA_PTR;
21
22/*
23 * This function looks for the highest region of memory lower than 4GB which
24 * has enough space for U-Boot where U-Boot is aligned on a page boundary.
25 * It overrides the default implementation found elsewhere which simply
26 * picks the end of ram, wherever that may be. The location of the stack,
27 * the relocation address, and how far U-Boot is moved by relocation are
28 * set in the global data structure.
29 */
Pali Rohár4f4f5832022-09-09 17:32:40 +020030phys_size_t board_get_usable_ram_top(phys_size_t total_size)
Simon Glassd2269f22015-08-04 12:34:01 -060031{
32 struct efi_mem_desc *desc, *end;
33 struct efi_entry_memmap *map;
34 int ret, size;
35 uintptr_t dest_addr = 0;
36 struct efi_mem_desc *largest = NULL;
37
38 /*
39 * Find largest area of memory below 4GB. We could
40 * call efi_build_mem_table() for a more accurate picture since it
41 * merges areas together where possible. But that function uses more
42 * pre-relocation memory, and it's not critical that we find the
43 * absolute largest region.
44 */
45 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
46 if (ret) {
47 /* We should have stopped in dram_init(), something is wrong */
48 debug("%s: Missing memory map\n", __func__);
49 goto err;
50 }
51
52 end = (struct efi_mem_desc *)((ulong)map + size);
53 desc = map->desc;
Simon Glass6f4d7aa2022-01-04 03:51:11 -070054 for (; desc < end; desc = efi_get_next_mem_desc(desc, map->desc_size)) {
Simon Glassd2269f22015-08-04 12:34:01 -060055 if (desc->type != EFI_CONVENTIONAL_MEMORY ||
56 desc->physical_start >= 1ULL << 32)
57 continue;
58 if (!largest || desc->num_pages > largest->num_pages)
59 largest = desc;
60 }
61
62 /* If no suitable area was found, return an error. */
63 assert(largest);
64 if (!largest || (largest->num_pages << EFI_PAGE_SHIFT) < (2 << 20))
65 goto err;
66
67 dest_addr = largest->physical_start + (largest->num_pages <<
68 EFI_PAGE_SHIFT);
69
70 return (ulong)dest_addr;
71err:
72 panic("No available memory found for relocation");
73 return 0;
74}
75
76int dram_init(void)
77{
78 struct efi_mem_desc *desc, *end;
79 struct efi_entry_memmap *map;
80 int size, ret;
81
82 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
83 if (ret) {
84 printf("Cannot find EFI memory map tables, ret=%d\n", ret);
85
86 return -ENODEV;
87 }
88
89 end = (struct efi_mem_desc *)((ulong)map + size);
90 gd->ram_size = 0;
91 desc = map->desc;
Simon Glass6f4d7aa2022-01-04 03:51:11 -070092 for (; desc < end; desc = efi_get_next_mem_desc(desc, map->desc_size)) {
Simon Glassd2269f22015-08-04 12:34:01 -060093 if (desc->type < EFI_MMAP_IO)
94 gd->ram_size += desc->num_pages << EFI_PAGE_SHIFT;
95 }
96
97 return 0;
98}
99
Simon Glass2f949c32017-03-31 08:40:32 -0600100int dram_init_banksize(void)
Simon Glassd2269f22015-08-04 12:34:01 -0600101{
102 struct efi_mem_desc *desc, *end;
103 struct efi_entry_memmap *map;
104 int ret, size;
105 int num_banks;
106
107 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
108 if (ret) {
109 /* We should have stopped in dram_init(), something is wrong */
110 debug("%s: Missing memory map\n", __func__);
Simon Glass2f949c32017-03-31 08:40:32 -0600111 return -ENXIO;
Simon Glassd2269f22015-08-04 12:34:01 -0600112 }
113 end = (struct efi_mem_desc *)((ulong)map + size);
114 desc = map->desc;
115 for (num_banks = 0;
116 desc < end && num_banks < CONFIG_NR_DRAM_BANKS;
Simon Glass6f4d7aa2022-01-04 03:51:11 -0700117 desc = efi_get_next_mem_desc(desc, map->desc_size)) {
Simon Glassd2269f22015-08-04 12:34:01 -0600118 /*
Bin Meng7eb28a52018-06-22 01:38:30 -0700119 * We only use conventional memory and ignore
Simon Glassd2269f22015-08-04 12:34:01 -0600120 * anything less than 1MB.
121 */
122 if (desc->type != EFI_CONVENTIONAL_MEMORY ||
Simon Glassd2269f22015-08-04 12:34:01 -0600123 (desc->num_pages << EFI_PAGE_SHIFT) < 1 << 20)
124 continue;
125 gd->bd->bi_dram[num_banks].start = desc->physical_start;
126 gd->bd->bi_dram[num_banks].size = desc->num_pages <<
127 EFI_PAGE_SHIFT;
128 num_banks++;
129 }
Simon Glass2f949c32017-03-31 08:40:32 -0600130
131 return 0;
Simon Glassd2269f22015-08-04 12:34:01 -0600132}
133
Bin Meng6bd41092018-06-12 08:36:17 -0700134int arch_cpu_init(void)
135{
136 post_code(POST_CPU_INIT);
137
138 return x86_cpu_init_f();
139}
140
Simon Glassee7c36f2017-03-28 10:27:30 -0600141int checkcpu(void)
142{
143 return 0;
144}
145
Simon Glassd2269f22015-08-04 12:34:01 -0600146int print_cpuinfo(void)
147{
148 return default_print_cpuinfo();
149}
150
151/* Find any available tables and copy them to a safe place */
152int reserve_arch(void)
153{
154 struct efi_info_hdr *hdr;
155
156 debug("table=%lx\n", gd->arch.table);
157 if (!gd->arch.table)
158 return 0;
159
160 hdr = (struct efi_info_hdr *)gd->arch.table;
161
162 gd->start_addr_sp -= hdr->total_size;
163 memcpy((void *)gd->start_addr_sp, hdr, hdr->total_size);
164 debug("Stashing EFI table at %lx to %lx, size %x\n",
165 gd->arch.table, gd->start_addr_sp, hdr->total_size);
166 gd->arch.table = gd->start_addr_sp;
167
168 return 0;
169}
Bin Mengdb59dd32018-06-17 05:57:53 -0700170
171int last_stage_init(void)
172{
173 /* start usb so that usb keyboard can be used as input device */
Simon Glass04443802023-02-05 17:55:28 -0700174 if (IS_ENABLED(CONFIG_USB_KEYBOARD))
Bin Menga7bb8ef2018-11-29 20:07:13 -0800175 usb_init();
Bin Mengdb59dd32018-06-17 05:57:53 -0700176
177 return 0;
178}
Bin Meng2f65c072018-08-23 08:24:08 -0700179
180unsigned int install_e820_map(unsigned int max_entries,
181 struct e820_entry *entries)
182{
183 struct efi_mem_desc *desc, *end;
184 struct efi_entry_memmap *map;
185 int size, ret;
186 efi_physical_addr_t last_end_addr = 0;
187 struct e820_entry *last_entry = NULL;
188 __u32 e820_type;
189 unsigned int num_entries = 0;
190
191 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
192 if (ret) {
193 printf("Cannot find EFI memory map tables, ret=%d\n", ret);
194
195 return -ENODEV;
196 }
197
198 end = (struct efi_mem_desc *)((ulong)map + size);
199 for (desc = map->desc; desc < end;
Simon Glass6f4d7aa2022-01-04 03:51:11 -0700200 desc = efi_get_next_mem_desc(desc, map->desc_size)) {
Bin Meng2f65c072018-08-23 08:24:08 -0700201 if (desc->num_pages == 0)
202 continue;
203
204 switch (desc->type) {
205 case EFI_LOADER_CODE:
206 case EFI_LOADER_DATA:
207 case EFI_BOOT_SERVICES_CODE:
208 case EFI_BOOT_SERVICES_DATA:
209 case EFI_CONVENTIONAL_MEMORY:
210 e820_type = E820_RAM;
211 break;
212
213 case EFI_RESERVED_MEMORY_TYPE:
214 case EFI_RUNTIME_SERVICES_CODE:
215 case EFI_RUNTIME_SERVICES_DATA:
216 case EFI_MMAP_IO:
217 case EFI_MMAP_IO_PORT:
218 case EFI_PAL_CODE:
219 e820_type = E820_RESERVED;
220 break;
221
222 case EFI_ACPI_RECLAIM_MEMORY:
223 e820_type = E820_ACPI;
224 break;
225
226 case EFI_ACPI_MEMORY_NVS:
227 e820_type = E820_NVS;
228 break;
229
230 case EFI_UNUSABLE_MEMORY:
231 e820_type = E820_UNUSABLE;
232 break;
233
234 default:
235 printf("Invalid EFI memory descriptor type (0x%x)!\n",
236 desc->type);
237 continue;
238 }
239
240 if (last_entry != NULL && last_entry->type == e820_type &&
241 desc->physical_start == last_end_addr) {
242 last_entry->size += (desc->num_pages << EFI_PAGE_SHIFT);
243 last_end_addr += (desc->num_pages << EFI_PAGE_SHIFT);
244 } else {
245 if (num_entries >= E820MAX)
246 break;
247
248 entries[num_entries].addr = desc->physical_start;
249 entries[num_entries].size = desc->num_pages;
250 entries[num_entries].size <<= EFI_PAGE_SHIFT;
251 entries[num_entries].type = e820_type;
252 last_entry = &entries[num_entries];
253 last_end_addr = last_entry->addr + last_entry->size;
254 num_entries++;
255 }
256 }
257
258 return num_entries;
259}
Bin Meng106dcfc2018-08-23 08:24:10 -0700260
261void setup_efi_info(struct efi_info *efi_info)
262{
263 struct efi_entry_systable *table;
264 struct efi_entry_memmap *map;
265 char *signature;
266 int size, ret;
267
268 memset(efi_info, 0, sizeof(struct efi_info));
269
270 ret = efi_info_get(EFIET_SYS_TABLE, (void **)&table, &size);
271 if (ret) {
272 printf("Cannot find EFI system table, ret=%d\n", ret);
273 return;
274 }
275 efi_info->efi_systab = (u32)(table->sys_table);
276
277 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
278 if (ret) {
279 printf("Cannot find EFI memory map tables, ret=%d\n", ret);
280 return;
281 }
282 efi_info->efi_memdesc_size = map->desc_size;
283 efi_info->efi_memdesc_version = map->version;
Simon Glass912e06f2021-09-24 18:30:21 -0600284 efi_info->efi_memmap = (ulong)(map->desc);
Bin Meng106dcfc2018-08-23 08:24:10 -0700285 efi_info->efi_memmap_size = size - sizeof(struct efi_entry_memmap);
286
287#ifdef CONFIG_EFI_STUB_64BIT
288 efi_info->efi_systab_hi = table->sys_table >> 32;
Simon Glass912e06f2021-09-24 18:30:21 -0600289 efi_info->efi_memmap_hi = (u64)(ulong)map->desc >> 32;
Bin Meng106dcfc2018-08-23 08:24:10 -0700290 signature = EFI64_LOADER_SIGNATURE;
291#else
292 signature = EFI32_LOADER_SIGNATURE;
293#endif
294 memcpy(&efi_info->efi_loader_signature, signature, 4);
295}
Simon Glass912e06f2021-09-24 18:30:21 -0600296
297void efi_show_bdinfo(void)
298{
299 struct efi_entry_systable *table = NULL;
Simon Glassc3dcef12021-12-29 11:57:53 -0700300 struct efi_system_table *sys_table;
Simon Glass912e06f2021-09-24 18:30:21 -0600301 int size, ret;
302
303 ret = efi_info_get(EFIET_SYS_TABLE, (void **)&table, &size);
Simon Glassc3dcef12021-12-29 11:57:53 -0700304 if (!ret) {
305 bdinfo_print_num_l("efi_table", table->sys_table);
306 sys_table = (struct efi_system_table *)(uintptr_t)
307 table->sys_table;
308 bdinfo_print_num_l(" revision", sys_table->fw_revision);
309 }
Simon Glass912e06f2021-09-24 18:30:21 -0600310}