xref: /openbmc/u-boot/arch/x86/cpu/efi/payload.c (revision f77d4410)
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 	usb_init();
170 
171 	return 0;
172 }
173 
174 unsigned int install_e820_map(unsigned int max_entries,
175 			      struct e820_entry *entries)
176 {
177 	struct efi_mem_desc *desc, *end;
178 	struct efi_entry_memmap *map;
179 	int size, ret;
180 	efi_physical_addr_t last_end_addr = 0;
181 	struct e820_entry *last_entry = NULL;
182 	__u32 e820_type;
183 	unsigned int num_entries = 0;
184 
185 	ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
186 	if (ret) {
187 		printf("Cannot find EFI memory map tables, ret=%d\n", ret);
188 
189 		return -ENODEV;
190 	}
191 
192 	end = (struct efi_mem_desc *)((ulong)map + size);
193 	for (desc = map->desc; desc < end;
194 	     desc = efi_get_next_mem_desc(map, desc)) {
195 		if (desc->num_pages == 0)
196 			continue;
197 
198 		switch (desc->type) {
199 		case EFI_LOADER_CODE:
200 		case EFI_LOADER_DATA:
201 		case EFI_BOOT_SERVICES_CODE:
202 		case EFI_BOOT_SERVICES_DATA:
203 		case EFI_CONVENTIONAL_MEMORY:
204 			e820_type = E820_RAM;
205 			break;
206 
207 		case EFI_RESERVED_MEMORY_TYPE:
208 		case EFI_RUNTIME_SERVICES_CODE:
209 		case EFI_RUNTIME_SERVICES_DATA:
210 		case EFI_MMAP_IO:
211 		case EFI_MMAP_IO_PORT:
212 		case EFI_PAL_CODE:
213 			e820_type = E820_RESERVED;
214 			break;
215 
216 		case EFI_ACPI_RECLAIM_MEMORY:
217 			e820_type = E820_ACPI;
218 			break;
219 
220 		case EFI_ACPI_MEMORY_NVS:
221 			e820_type = E820_NVS;
222 			break;
223 
224 		case EFI_UNUSABLE_MEMORY:
225 			e820_type = E820_UNUSABLE;
226 			break;
227 
228 		default:
229 			printf("Invalid EFI memory descriptor type (0x%x)!\n",
230 			       desc->type);
231 			continue;
232 		}
233 
234 		if (last_entry != NULL && last_entry->type == e820_type &&
235 		    desc->physical_start == last_end_addr) {
236 			last_entry->size += (desc->num_pages << EFI_PAGE_SHIFT);
237 			last_end_addr += (desc->num_pages << EFI_PAGE_SHIFT);
238 		} else {
239 			if (num_entries >= E820MAX)
240 				break;
241 
242 			entries[num_entries].addr = desc->physical_start;
243 			entries[num_entries].size = desc->num_pages;
244 			entries[num_entries].size <<= EFI_PAGE_SHIFT;
245 			entries[num_entries].type = e820_type;
246 			last_entry = &entries[num_entries];
247 			last_end_addr = last_entry->addr + last_entry->size;
248 			num_entries++;
249 		}
250 	}
251 
252 	return num_entries;
253 }
254 
255 void setup_efi_info(struct efi_info *efi_info)
256 {
257 	struct efi_entry_systable *table;
258 	struct efi_entry_memmap *map;
259 	char *signature;
260 	int size, ret;
261 
262 	memset(efi_info, 0, sizeof(struct efi_info));
263 
264 	ret = efi_info_get(EFIET_SYS_TABLE, (void **)&table, &size);
265 	if (ret) {
266 		printf("Cannot find EFI system table, ret=%d\n", ret);
267 		return;
268 	}
269 	efi_info->efi_systab = (u32)(table->sys_table);
270 
271 	ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
272 	if (ret) {
273 		printf("Cannot find EFI memory map tables, ret=%d\n", ret);
274 		return;
275 	}
276 	efi_info->efi_memdesc_size = map->desc_size;
277 	efi_info->efi_memdesc_version = map->version;
278 	efi_info->efi_memmap = (u32)(map->desc);
279 	efi_info->efi_memmap_size = size - sizeof(struct efi_entry_memmap);
280 
281 #ifdef CONFIG_EFI_STUB_64BIT
282 	efi_info->efi_systab_hi = table->sys_table >> 32;
283 	efi_info->efi_memmap_hi = (u64)(u32)(map->desc) >> 32;
284 	signature = EFI64_LOADER_SIGNATURE;
285 #else
286 	signature = EFI32_LOADER_SIGNATURE;
287 #endif
288 	memcpy(&efi_info->efi_loader_signature, signature, 4);
289 }
290