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