blob: 708bfbe7ee488d6f6e8a9915a9eb89bcdd8238e8 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass8e366502015-08-04 12:34:01 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass8e366502015-08-04 12:34:01 -06005 */
6
7#include <common.h>
Simon Glass30c7c432019-11-14 12:57:34 -07008#include <cpu_func.h>
Simon Glass8e366502015-08-04 12:34:01 -06009#include <efi.h>
Simon Glass40b17232021-12-29 11:57:53 -070010#include <efi_api.h>
Simon Glass8e366502015-08-04 12:34:01 -060011#include <errno.h>
Simon Glass91caa3b2023-08-21 21:17:01 -060012#include <event.h>
Simon Glass67c4e9f2019-11-14 12:57:45 -070013#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Bin Meng1ab2c012018-06-17 05:57:53 -070015#include <usb.h>
Bin Meng1fdeacd2018-08-23 08:24:10 -070016#include <asm/bootparam.h>
Bin Mengaac79252018-08-23 08:24:08 -070017#include <asm/e820.h>
Simon Glass401d1c42020-10-30 21:38:53 -060018#include <asm/global_data.h>
Bin Meng3773c6a2018-06-12 08:36:17 -070019#include <asm/post.h>
Simon Glass8e366502015-08-04 12:34:01 -060020
21DECLARE_GLOBAL_DATA_PTR;
22
23/*
24 * This function looks for the highest region of memory lower than 4GB which
25 * has enough space for U-Boot where U-Boot is aligned on a page boundary.
26 * It overrides the default implementation found elsewhere which simply
27 * picks the end of ram, wherever that may be. The location of the stack,
28 * the relocation address, and how far U-Boot is moved by relocation are
29 * set in the global data structure.
30 */
Heinrich Schuchardtd768dd82023-08-12 20:16:58 +020031phys_addr_t board_get_usable_ram_top(phys_size_t total_size)
Simon Glass8e366502015-08-04 12:34:01 -060032{
33 struct efi_mem_desc *desc, *end;
34 struct efi_entry_memmap *map;
35 int ret, size;
36 uintptr_t dest_addr = 0;
37 struct efi_mem_desc *largest = NULL;
38
39 /*
40 * Find largest area of memory below 4GB. We could
41 * call efi_build_mem_table() for a more accurate picture since it
42 * merges areas together where possible. But that function uses more
43 * pre-relocation memory, and it's not critical that we find the
44 * absolute largest region.
45 */
46 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
47 if (ret) {
48 /* We should have stopped in dram_init(), something is wrong */
49 debug("%s: Missing memory map\n", __func__);
50 goto err;
51 }
52
53 end = (struct efi_mem_desc *)((ulong)map + size);
54 desc = map->desc;
Simon Glassce1dc0c2022-01-04 03:51:11 -070055 for (; desc < end; desc = efi_get_next_mem_desc(desc, map->desc_size)) {
Simon Glass8e366502015-08-04 12:34:01 -060056 if (desc->type != EFI_CONVENTIONAL_MEMORY ||
57 desc->physical_start >= 1ULL << 32)
58 continue;
59 if (!largest || desc->num_pages > largest->num_pages)
60 largest = desc;
61 }
62
63 /* If no suitable area was found, return an error. */
64 assert(largest);
65 if (!largest || (largest->num_pages << EFI_PAGE_SHIFT) < (2 << 20))
66 goto err;
67
68 dest_addr = largest->physical_start + (largest->num_pages <<
69 EFI_PAGE_SHIFT);
70
71 return (ulong)dest_addr;
72err:
73 panic("No available memory found for relocation");
74 return 0;
75}
76
77int dram_init(void)
78{
79 struct efi_mem_desc *desc, *end;
80 struct efi_entry_memmap *map;
81 int size, ret;
82
83 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
84 if (ret) {
85 printf("Cannot find EFI memory map tables, ret=%d\n", ret);
86
87 return -ENODEV;
88 }
89
90 end = (struct efi_mem_desc *)((ulong)map + size);
91 gd->ram_size = 0;
92 desc = map->desc;
Simon Glassce1dc0c2022-01-04 03:51:11 -070093 for (; desc < end; desc = efi_get_next_mem_desc(desc, map->desc_size)) {
Simon Glass8e366502015-08-04 12:34:01 -060094 if (desc->type < EFI_MMAP_IO)
95 gd->ram_size += desc->num_pages << EFI_PAGE_SHIFT;
96 }
97
98 return 0;
99}
100
Simon Glass76b00ac2017-03-31 08:40:32 -0600101int dram_init_banksize(void)
Simon Glass8e366502015-08-04 12:34:01 -0600102{
103 struct efi_mem_desc *desc, *end;
104 struct efi_entry_memmap *map;
105 int ret, size;
106 int num_banks;
107
108 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
109 if (ret) {
110 /* We should have stopped in dram_init(), something is wrong */
111 debug("%s: Missing memory map\n", __func__);
Simon Glass76b00ac2017-03-31 08:40:32 -0600112 return -ENXIO;
Simon Glass8e366502015-08-04 12:34:01 -0600113 }
114 end = (struct efi_mem_desc *)((ulong)map + size);
115 desc = map->desc;
116 for (num_banks = 0;
117 desc < end && num_banks < CONFIG_NR_DRAM_BANKS;
Simon Glassce1dc0c2022-01-04 03:51:11 -0700118 desc = efi_get_next_mem_desc(desc, map->desc_size)) {
Simon Glass8e366502015-08-04 12:34:01 -0600119 /*
Bin Meng59837472018-06-22 01:38:30 -0700120 * We only use conventional memory and ignore
Simon Glass8e366502015-08-04 12:34:01 -0600121 * anything less than 1MB.
122 */
123 if (desc->type != EFI_CONVENTIONAL_MEMORY ||
Simon Glass8e366502015-08-04 12:34:01 -0600124 (desc->num_pages << EFI_PAGE_SHIFT) < 1 << 20)
125 continue;
126 gd->bd->bi_dram[num_banks].start = desc->physical_start;
127 gd->bd->bi_dram[num_banks].size = desc->num_pages <<
128 EFI_PAGE_SHIFT;
129 num_banks++;
130 }
Simon Glass76b00ac2017-03-31 08:40:32 -0600131
132 return 0;
Simon Glass8e366502015-08-04 12:34:01 -0600133}
134
Bin Meng3773c6a2018-06-12 08:36:17 -0700135int arch_cpu_init(void)
136{
137 post_code(POST_CPU_INIT);
138
139 return x86_cpu_init_f();
140}
141
Simon Glass76d1d022017-03-28 10:27:30 -0600142int checkcpu(void)
143{
144 return 0;
145}
146
Simon Glass8e366502015-08-04 12:34:01 -0600147int print_cpuinfo(void)
148{
149 return default_print_cpuinfo();
150}
151
152/* Find any available tables and copy them to a safe place */
153int reserve_arch(void)
154{
155 struct efi_info_hdr *hdr;
156
157 debug("table=%lx\n", gd->arch.table);
158 if (!gd->arch.table)
159 return 0;
160
161 hdr = (struct efi_info_hdr *)gd->arch.table;
162
163 gd->start_addr_sp -= hdr->total_size;
164 memcpy((void *)gd->start_addr_sp, hdr, hdr->total_size);
165 debug("Stashing EFI table at %lx to %lx, size %x\n",
166 gd->arch.table, gd->start_addr_sp, hdr->total_size);
167 gd->arch.table = gd->start_addr_sp;
168
169 return 0;
170}
Bin Meng1ab2c012018-06-17 05:57:53 -0700171
Simon Glass91caa3b2023-08-21 21:17:01 -0600172static int last_stage_init(void)
Bin Meng1ab2c012018-06-17 05:57:53 -0700173{
174 /* start usb so that usb keyboard can be used as input device */
Simon Glass91b614e2023-02-05 17:55:28 -0700175 if (IS_ENABLED(CONFIG_USB_KEYBOARD))
Bin Mengd3d65312018-11-29 20:07:13 -0800176 usb_init();
Bin Meng1ab2c012018-06-17 05:57:53 -0700177
178 return 0;
179}
Simon Glass91caa3b2023-08-21 21:17:01 -0600180EVENT_SPY_SIMPLE(EVT_LAST_STAGE_INIT, last_stage_init);
Bin Mengaac79252018-08-23 08:24:08 -0700181
182unsigned int install_e820_map(unsigned int max_entries,
183 struct e820_entry *entries)
184{
185 struct efi_mem_desc *desc, *end;
186 struct efi_entry_memmap *map;
187 int size, ret;
188 efi_physical_addr_t last_end_addr = 0;
189 struct e820_entry *last_entry = NULL;
190 __u32 e820_type;
191 unsigned int num_entries = 0;
192
193 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
194 if (ret) {
195 printf("Cannot find EFI memory map tables, ret=%d\n", ret);
196
197 return -ENODEV;
198 }
199
200 end = (struct efi_mem_desc *)((ulong)map + size);
201 for (desc = map->desc; desc < end;
Simon Glassce1dc0c2022-01-04 03:51:11 -0700202 desc = efi_get_next_mem_desc(desc, map->desc_size)) {
Bin Mengaac79252018-08-23 08:24:08 -0700203 if (desc->num_pages == 0)
204 continue;
205
206 switch (desc->type) {
207 case EFI_LOADER_CODE:
208 case EFI_LOADER_DATA:
209 case EFI_BOOT_SERVICES_CODE:
210 case EFI_BOOT_SERVICES_DATA:
211 case EFI_CONVENTIONAL_MEMORY:
212 e820_type = E820_RAM;
213 break;
214
215 case EFI_RESERVED_MEMORY_TYPE:
216 case EFI_RUNTIME_SERVICES_CODE:
217 case EFI_RUNTIME_SERVICES_DATA:
218 case EFI_MMAP_IO:
219 case EFI_MMAP_IO_PORT:
220 case EFI_PAL_CODE:
221 e820_type = E820_RESERVED;
222 break;
223
224 case EFI_ACPI_RECLAIM_MEMORY:
225 e820_type = E820_ACPI;
226 break;
227
228 case EFI_ACPI_MEMORY_NVS:
229 e820_type = E820_NVS;
230 break;
231
232 case EFI_UNUSABLE_MEMORY:
233 e820_type = E820_UNUSABLE;
234 break;
235
236 default:
237 printf("Invalid EFI memory descriptor type (0x%x)!\n",
238 desc->type);
239 continue;
240 }
241
242 if (last_entry != NULL && last_entry->type == e820_type &&
243 desc->physical_start == last_end_addr) {
244 last_entry->size += (desc->num_pages << EFI_PAGE_SHIFT);
245 last_end_addr += (desc->num_pages << EFI_PAGE_SHIFT);
246 } else {
247 if (num_entries >= E820MAX)
248 break;
249
250 entries[num_entries].addr = desc->physical_start;
251 entries[num_entries].size = desc->num_pages;
252 entries[num_entries].size <<= EFI_PAGE_SHIFT;
253 entries[num_entries].type = e820_type;
254 last_entry = &entries[num_entries];
255 last_end_addr = last_entry->addr + last_entry->size;
256 num_entries++;
257 }
258 }
259
260 return num_entries;
261}
Bin Meng1fdeacd2018-08-23 08:24:10 -0700262
263void setup_efi_info(struct efi_info *efi_info)
264{
265 struct efi_entry_systable *table;
266 struct efi_entry_memmap *map;
267 char *signature;
268 int size, ret;
269
270 memset(efi_info, 0, sizeof(struct efi_info));
271
272 ret = efi_info_get(EFIET_SYS_TABLE, (void **)&table, &size);
273 if (ret) {
274 printf("Cannot find EFI system table, ret=%d\n", ret);
275 return;
276 }
277 efi_info->efi_systab = (u32)(table->sys_table);
278
279 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
280 if (ret) {
281 printf("Cannot find EFI memory map tables, ret=%d\n", ret);
282 return;
283 }
284 efi_info->efi_memdesc_size = map->desc_size;
285 efi_info->efi_memdesc_version = map->version;
Simon Glass567dfef2021-09-24 18:30:21 -0600286 efi_info->efi_memmap = (ulong)(map->desc);
Bin Meng1fdeacd2018-08-23 08:24:10 -0700287 efi_info->efi_memmap_size = size - sizeof(struct efi_entry_memmap);
288
289#ifdef CONFIG_EFI_STUB_64BIT
290 efi_info->efi_systab_hi = table->sys_table >> 32;
Simon Glass567dfef2021-09-24 18:30:21 -0600291 efi_info->efi_memmap_hi = (u64)(ulong)map->desc >> 32;
Bin Meng1fdeacd2018-08-23 08:24:10 -0700292 signature = EFI64_LOADER_SIGNATURE;
293#else
294 signature = EFI32_LOADER_SIGNATURE;
295#endif
296 memcpy(&efi_info->efi_loader_signature, signature, 4);
297}
Simon Glass567dfef2021-09-24 18:30:21 -0600298
299void efi_show_bdinfo(void)
300{
301 struct efi_entry_systable *table = NULL;
Simon Glass40b17232021-12-29 11:57:53 -0700302 struct efi_system_table *sys_table;
Simon Glass567dfef2021-09-24 18:30:21 -0600303 int size, ret;
304
305 ret = efi_info_get(EFIET_SYS_TABLE, (void **)&table, &size);
Simon Glass40b17232021-12-29 11:57:53 -0700306 if (!ret) {
307 bdinfo_print_num_l("efi_table", table->sys_table);
308 sys_table = (struct efi_system_table *)(uintptr_t)
309 table->sys_table;
310 bdinfo_print_num_l(" revision", sys_table->fw_revision);
311 }
Simon Glass567dfef2021-09-24 18:30:21 -0600312}