blob: 0e7c7c1ba49dc4389ac0e75f867130c25bfdbff5 [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 Glassd2269f22015-08-04 12:34:01 -06008#include <efi.h>
9#include <errno.h>
Bin Mengdb59dd32018-06-17 05:57:53 -070010#include <usb.h>
Bin Meng2f65c072018-08-23 08:24:08 -070011#include <asm/e820.h>
Bin Meng6bd41092018-06-12 08:36:17 -070012#include <asm/post.h>
Simon Glassd2269f22015-08-04 12:34:01 -060013
14DECLARE_GLOBAL_DATA_PTR;
15
16/*
17 * This function looks for the highest region of memory lower than 4GB which
18 * has enough space for U-Boot where U-Boot is aligned on a page boundary.
19 * It overrides the default implementation found elsewhere which simply
20 * picks the end of ram, wherever that may be. The location of the stack,
21 * the relocation address, and how far U-Boot is moved by relocation are
22 * set in the global data structure.
23 */
24ulong board_get_usable_ram_top(ulong total_size)
25{
26 struct efi_mem_desc *desc, *end;
27 struct efi_entry_memmap *map;
28 int ret, size;
29 uintptr_t dest_addr = 0;
30 struct efi_mem_desc *largest = NULL;
31
32 /*
33 * Find largest area of memory below 4GB. We could
34 * call efi_build_mem_table() for a more accurate picture since it
35 * merges areas together where possible. But that function uses more
36 * pre-relocation memory, and it's not critical that we find the
37 * absolute largest region.
38 */
39 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
40 if (ret) {
41 /* We should have stopped in dram_init(), something is wrong */
42 debug("%s: Missing memory map\n", __func__);
43 goto err;
44 }
45
46 end = (struct efi_mem_desc *)((ulong)map + size);
47 desc = map->desc;
48 for (; desc < end; desc = efi_get_next_mem_desc(map, desc)) {
49 if (desc->type != EFI_CONVENTIONAL_MEMORY ||
50 desc->physical_start >= 1ULL << 32)
51 continue;
52 if (!largest || desc->num_pages > largest->num_pages)
53 largest = desc;
54 }
55
56 /* If no suitable area was found, return an error. */
57 assert(largest);
58 if (!largest || (largest->num_pages << EFI_PAGE_SHIFT) < (2 << 20))
59 goto err;
60
61 dest_addr = largest->physical_start + (largest->num_pages <<
62 EFI_PAGE_SHIFT);
63
64 return (ulong)dest_addr;
65err:
66 panic("No available memory found for relocation");
67 return 0;
68}
69
70int dram_init(void)
71{
72 struct efi_mem_desc *desc, *end;
73 struct efi_entry_memmap *map;
74 int size, ret;
75
76 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
77 if (ret) {
78 printf("Cannot find EFI memory map tables, ret=%d\n", ret);
79
80 return -ENODEV;
81 }
82
83 end = (struct efi_mem_desc *)((ulong)map + size);
84 gd->ram_size = 0;
85 desc = map->desc;
86 for (; desc < end; desc = efi_get_next_mem_desc(map, desc)) {
87 if (desc->type < EFI_MMAP_IO)
88 gd->ram_size += desc->num_pages << EFI_PAGE_SHIFT;
89 }
90
91 return 0;
92}
93
Simon Glass2f949c32017-03-31 08:40:32 -060094int dram_init_banksize(void)
Simon Glassd2269f22015-08-04 12:34:01 -060095{
96 struct efi_mem_desc *desc, *end;
97 struct efi_entry_memmap *map;
98 int ret, size;
99 int num_banks;
100
101 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
102 if (ret) {
103 /* We should have stopped in dram_init(), something is wrong */
104 debug("%s: Missing memory map\n", __func__);
Simon Glass2f949c32017-03-31 08:40:32 -0600105 return -ENXIO;
Simon Glassd2269f22015-08-04 12:34:01 -0600106 }
107 end = (struct efi_mem_desc *)((ulong)map + size);
108 desc = map->desc;
109 for (num_banks = 0;
110 desc < end && num_banks < CONFIG_NR_DRAM_BANKS;
111 desc = efi_get_next_mem_desc(map, desc)) {
112 /*
Bin Meng7eb28a52018-06-22 01:38:30 -0700113 * We only use conventional memory and ignore
Simon Glassd2269f22015-08-04 12:34:01 -0600114 * anything less than 1MB.
115 */
116 if (desc->type != EFI_CONVENTIONAL_MEMORY ||
Simon Glassd2269f22015-08-04 12:34:01 -0600117 (desc->num_pages << EFI_PAGE_SHIFT) < 1 << 20)
118 continue;
119 gd->bd->bi_dram[num_banks].start = desc->physical_start;
120 gd->bd->bi_dram[num_banks].size = desc->num_pages <<
121 EFI_PAGE_SHIFT;
122 num_banks++;
123 }
Simon Glass2f949c32017-03-31 08:40:32 -0600124
125 return 0;
Simon Glassd2269f22015-08-04 12:34:01 -0600126}
127
Bin Meng6bd41092018-06-12 08:36:17 -0700128int arch_cpu_init(void)
129{
130 post_code(POST_CPU_INIT);
131
132 return x86_cpu_init_f();
133}
134
Simon Glassee7c36f2017-03-28 10:27:30 -0600135int checkcpu(void)
136{
137 return 0;
138}
139
Simon Glassd2269f22015-08-04 12:34:01 -0600140int print_cpuinfo(void)
141{
142 return default_print_cpuinfo();
143}
144
145/* Find any available tables and copy them to a safe place */
146int reserve_arch(void)
147{
148 struct efi_info_hdr *hdr;
149
150 debug("table=%lx\n", gd->arch.table);
151 if (!gd->arch.table)
152 return 0;
153
154 hdr = (struct efi_info_hdr *)gd->arch.table;
155
156 gd->start_addr_sp -= hdr->total_size;
157 memcpy((void *)gd->start_addr_sp, hdr, hdr->total_size);
158 debug("Stashing EFI table at %lx to %lx, size %x\n",
159 gd->arch.table, gd->start_addr_sp, hdr->total_size);
160 gd->arch.table = gd->start_addr_sp;
161
162 return 0;
163}
Bin Mengdb59dd32018-06-17 05:57:53 -0700164
165int last_stage_init(void)
166{
167 /* start usb so that usb keyboard can be used as input device */
168 usb_init();
169
170 return 0;
171}
Bin Meng2f65c072018-08-23 08:24:08 -0700172
173unsigned int install_e820_map(unsigned int max_entries,
174 struct e820_entry *entries)
175{
176 struct efi_mem_desc *desc, *end;
177 struct efi_entry_memmap *map;
178 int size, ret;
179 efi_physical_addr_t last_end_addr = 0;
180 struct e820_entry *last_entry = NULL;
181 __u32 e820_type;
182 unsigned int num_entries = 0;
183
184 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
185 if (ret) {
186 printf("Cannot find EFI memory map tables, ret=%d\n", ret);
187
188 return -ENODEV;
189 }
190
191 end = (struct efi_mem_desc *)((ulong)map + size);
192 for (desc = map->desc; desc < end;
193 desc = efi_get_next_mem_desc(map, desc)) {
194 if (desc->num_pages == 0)
195 continue;
196
197 switch (desc->type) {
198 case EFI_LOADER_CODE:
199 case EFI_LOADER_DATA:
200 case EFI_BOOT_SERVICES_CODE:
201 case EFI_BOOT_SERVICES_DATA:
202 case EFI_CONVENTIONAL_MEMORY:
203 e820_type = E820_RAM;
204 break;
205
206 case EFI_RESERVED_MEMORY_TYPE:
207 case EFI_RUNTIME_SERVICES_CODE:
208 case EFI_RUNTIME_SERVICES_DATA:
209 case EFI_MMAP_IO:
210 case EFI_MMAP_IO_PORT:
211 case EFI_PAL_CODE:
212 e820_type = E820_RESERVED;
213 break;
214
215 case EFI_ACPI_RECLAIM_MEMORY:
216 e820_type = E820_ACPI;
217 break;
218
219 case EFI_ACPI_MEMORY_NVS:
220 e820_type = E820_NVS;
221 break;
222
223 case EFI_UNUSABLE_MEMORY:
224 e820_type = E820_UNUSABLE;
225 break;
226
227 default:
228 printf("Invalid EFI memory descriptor type (0x%x)!\n",
229 desc->type);
230 continue;
231 }
232
233 if (last_entry != NULL && last_entry->type == e820_type &&
234 desc->physical_start == last_end_addr) {
235 last_entry->size += (desc->num_pages << EFI_PAGE_SHIFT);
236 last_end_addr += (desc->num_pages << EFI_PAGE_SHIFT);
237 } else {
238 if (num_entries >= E820MAX)
239 break;
240
241 entries[num_entries].addr = desc->physical_start;
242 entries[num_entries].size = desc->num_pages;
243 entries[num_entries].size <<= EFI_PAGE_SHIFT;
244 entries[num_entries].type = e820_type;
245 last_entry = &entries[num_entries];
246 last_end_addr = last_entry->addr + last_entry->size;
247 num_entries++;
248 }
249 }
250
251 return num_entries;
252}