xref: /openbmc/u-boot/arch/x86/lib/fsp/fsp_support.c (revision 456ee909)
1 /*
2  * Copyright (C) 2013, Intel Corporation
3  * Copyright (C) 2014, Bin Meng <bmeng.cn@gmail.com>
4  *
5  * SPDX-License-Identifier:	Intel
6  */
7 
8 #include <common.h>
9 #include <asm/fsp/fsp_support.h>
10 #include <asm/post.h>
11 
12 /**
13  * Compares two GUIDs
14  *
15  * If the GUIDs are identical then true is returned.
16  * If there are any bit differences in the two GUIDs, then false is returned.
17  *
18  * @guid1:        A pointer to a 128 bit GUID.
19  * @guid2:        A pointer to a 128 bit GUID.
20  *
21  * @retval true:  guid1 and guid2 are identical.
22  * @retval false: guid1 and guid2 are not identical.
23  */
24 static bool compare_guid(const struct efi_guid *guid1,
25 			 const struct efi_guid *guid2)
26 {
27 	if (memcmp(guid1, guid2, sizeof(struct efi_guid)) == 0)
28 		return true;
29 	else
30 		return false;
31 }
32 
33 struct fsp_header *__attribute__((optimize("O0"))) find_fsp_header(void)
34 {
35 	/*
36 	 * This function may be called before the a stack is established,
37 	 * so special care must be taken. First, it cannot declare any local
38 	 * variable using stack. Only register variable can be used here.
39 	 * Secondly, some compiler version will add prolog or epilog code
40 	 * for the C function. If so the function call may not work before
41 	 * stack is ready.
42 	 *
43 	 * GCC 4.8.1 has been verified to be working for the following codes.
44 	 */
45 	volatile register u8 *fsp asm("eax");
46 
47 	/* Initalize the FSP base */
48 	fsp = (u8 *)CONFIG_FSP_ADDR;
49 
50 	/* Check the FV signature, _FVH */
51 	if (((struct fv_header *)fsp)->sign == EFI_FVH_SIGNATURE) {
52 		/* Go to the end of the FV header and align the address */
53 		fsp += ((struct fv_header *)fsp)->ext_hdr_off;
54 		fsp += ((struct fv_ext_header *)fsp)->ext_hdr_size;
55 		fsp  = (u8 *)(((u32)fsp + 7) & 0xFFFFFFF8);
56 	} else {
57 		fsp  = 0;
58 	}
59 
60 	/* Check the FFS GUID */
61 	if (fsp &&
62 	    ((struct ffs_file_header *)fsp)->name.data1 == FSP_GUID_DATA1 &&
63 	    ((struct ffs_file_header *)fsp)->name.data2 == FSP_GUID_DATA2 &&
64 	    ((struct ffs_file_header *)fsp)->name.data3 == FSP_GUID_DATA3 &&
65 	    ((struct ffs_file_header *)fsp)->name.data4[0] == FSP_GUID_DATA4_0 &&
66 	    ((struct ffs_file_header *)fsp)->name.data4[1] == FSP_GUID_DATA4_1 &&
67 	    ((struct ffs_file_header *)fsp)->name.data4[2] == FSP_GUID_DATA4_2 &&
68 	    ((struct ffs_file_header *)fsp)->name.data4[3] == FSP_GUID_DATA4_3 &&
69 	    ((struct ffs_file_header *)fsp)->name.data4[4] == FSP_GUID_DATA4_4 &&
70 	    ((struct ffs_file_header *)fsp)->name.data4[5] == FSP_GUID_DATA4_5 &&
71 	    ((struct ffs_file_header *)fsp)->name.data4[6] == FSP_GUID_DATA4_6 &&
72 	    ((struct ffs_file_header *)fsp)->name.data4[7] == FSP_GUID_DATA4_7) {
73 		/* Add the FFS header size to find the raw section header */
74 		fsp += sizeof(struct ffs_file_header);
75 	} else {
76 		fsp = 0;
77 	}
78 
79 	if (fsp &&
80 	    ((struct raw_section *)fsp)->type == EFI_SECTION_RAW) {
81 		/* Add the raw section header size to find the FSP header */
82 		fsp += sizeof(struct raw_section);
83 	} else {
84 		fsp = 0;
85 	}
86 
87 	return (struct fsp_header *)fsp;
88 }
89 
90 void fsp_continue(struct shared_data *shared_data, u32 status, void *hob_list)
91 {
92 	u32 stack_len;
93 	u32 stack_base;
94 	u32 stack_top;
95 
96 	post_code(POST_MRC);
97 
98 	assert(status == 0);
99 
100 	/* Get the migrated stack in normal memory */
101 	stack_base = (u32)fsp_get_bootloader_tmp_mem(hob_list, &stack_len);
102 	assert(stack_base != 0);
103 	stack_top  = stack_base + stack_len - sizeof(u32);
104 
105 	/*
106 	 * Old stack base is stored at the very end of the stack top,
107 	 * use it to calculate the migrated shared data base
108 	 */
109 	shared_data = (struct shared_data *)(stack_base +
110 			((u32)shared_data - *(u32 *)stack_top));
111 
112 	/* The boot loader main function entry */
113 	fsp_init_done(hob_list);
114 }
115 
116 void fsp_init(u32 stack_top, u32 boot_mode, void *nvs_buf)
117 {
118 	struct shared_data shared_data;
119 	fsp_init_f init;
120 	struct fsp_init_params params;
121 	struct fspinit_rtbuf rt_buf;
122 	struct vpd_region *fsp_vpd;
123 	struct fsp_header *fsp_hdr;
124 	struct fsp_init_params *params_ptr;
125 	struct upd_region *fsp_upd;
126 
127 #ifdef CONFIG_DEBUG_UART
128 	setup_early_uart();
129 #endif
130 
131 	fsp_hdr = find_fsp_header();
132 	if (fsp_hdr == NULL) {
133 		/* No valid FSP info header was found */
134 		panic("Invalid FSP header");
135 	}
136 
137 	fsp_upd = &shared_data.fsp_upd;
138 	memset(&rt_buf, 0, sizeof(struct fspinit_rtbuf));
139 
140 	/* Reserve a gap in stack top */
141 	rt_buf.common.stack_top = (u32 *)stack_top - 32;
142 	rt_buf.common.boot_mode = boot_mode;
143 	rt_buf.common.upd_data = fsp_upd;
144 
145 	/* Get VPD region start */
146 	fsp_vpd = (struct vpd_region *)(fsp_hdr->img_base +
147 			fsp_hdr->cfg_region_off);
148 
149 	/* Verify the VPD data region is valid */
150 	assert((fsp_vpd->img_rev == VPD_IMAGE_REV) &&
151 	       (fsp_vpd->sign == VPD_IMAGE_ID));
152 
153 	/* Copy default data from Flash */
154 	memcpy(fsp_upd, (void *)(fsp_hdr->img_base + fsp_vpd->upd_offset),
155 	       sizeof(struct upd_region));
156 
157 	/* Verify the UPD data region is valid */
158 	assert(fsp_upd->terminator == UPD_TERMINATOR);
159 
160 	/* Override any UPD setting if required */
161 	update_fsp_upd(fsp_upd);
162 
163 	memset(&params, 0, sizeof(struct fsp_init_params));
164 	params.nvs_buf = nvs_buf;
165 	params.rt_buf = (struct fspinit_rtbuf *)&rt_buf;
166 	params.continuation = (fsp_continuation_f)asm_continuation;
167 
168 	init = (fsp_init_f)(fsp_hdr->img_base + fsp_hdr->fsp_init);
169 	params_ptr = &params;
170 
171 	shared_data.fsp_hdr = fsp_hdr;
172 	shared_data.stack_top = (u32 *)stack_top;
173 
174 	post_code(POST_PRE_MRC);
175 
176 	/* Load GDT for FSP */
177 	setup_fsp_gdt();
178 
179 	/*
180 	 * Use ASM code to ensure the register value in EAX & ECX
181 	 * will be passed into BlContinuationFunc
182 	 */
183 	asm volatile (
184 		"pushl	%0;"
185 		"call	*%%eax;"
186 		".global asm_continuation;"
187 		"asm_continuation:;"
188 		"movl	%%ebx, %%eax;"		/* shared_data */
189 		"movl	4(%%esp), %%edx;"	/* status */
190 		"movl	8(%%esp), %%ecx;"	/* hob_list */
191 		"jmp	fsp_continue;"
192 		: : "m"(params_ptr), "a"(init), "b"(&shared_data)
193 	);
194 
195 	/*
196 	 * Should never get here.
197 	 * Control will continue from fsp_continue.
198 	 * This line below is to prevent the compiler from optimizing
199 	 * structure intialization.
200 	 *
201 	 * DO NOT REMOVE!
202 	 */
203 	init(&params);
204 }
205 
206 u32 fsp_notify(struct fsp_header *fsp_hdr, u32 phase)
207 {
208 	fsp_notify_f notify;
209 	struct fsp_notify_params params;
210 	struct fsp_notify_params *params_ptr;
211 	u32 status;
212 
213 	if (!fsp_hdr)
214 		fsp_hdr = (struct fsp_header *)find_fsp_header();
215 
216 	if (fsp_hdr == NULL) {
217 		/* No valid FSP info header */
218 		panic("Invalid FSP header");
219 	}
220 
221 	notify = (fsp_notify_f)(fsp_hdr->img_base + fsp_hdr->fsp_notify);
222 	params.phase = phase;
223 	params_ptr = &params;
224 
225 	/*
226 	 * Use ASM code to ensure correct parameter is on the stack for
227 	 * FspNotify as U-Boot is using different ABI from FSP
228 	 */
229 	asm volatile (
230 		"pushl	%1;"		/* push notify phase */
231 		"call	*%%eax;"	/* call FspNotify */
232 		"addl	$4, %%esp;"	/* clean up the stack */
233 		: "=a"(status) : "m"(params_ptr), "a"(notify), "m"(*params_ptr)
234 	);
235 
236 	return status;
237 }
238 
239 u32 fsp_get_usable_lowmem_top(const void *hob_list)
240 {
241 	const struct hob_header *hdr;
242 	struct hob_res_desc *res_desc;
243 	phys_addr_t phys_start;
244 	u32 top;
245 
246 	/* Get the HOB list for processing */
247 	hdr = hob_list;
248 
249 	/* * Collect memory ranges */
250 	top = FSP_LOWMEM_BASE;
251 	while (!end_of_hob(hdr)) {
252 		if (hdr->type == HOB_TYPE_RES_DESC) {
253 			res_desc = (struct hob_res_desc *)hdr;
254 			if (res_desc->type == RES_SYS_MEM) {
255 				phys_start = res_desc->phys_start;
256 				/* Need memory above 1MB to be collected here */
257 				if (phys_start >= FSP_LOWMEM_BASE &&
258 				    phys_start < (phys_addr_t)FSP_HIGHMEM_BASE)
259 					top += (u32)(res_desc->len);
260 			}
261 		}
262 		hdr = get_next_hob(hdr);
263 	}
264 
265 	return top;
266 }
267 
268 u64 fsp_get_usable_highmem_top(const void *hob_list)
269 {
270 	const struct hob_header *hdr;
271 	struct hob_res_desc *res_desc;
272 	phys_addr_t phys_start;
273 	u64 top;
274 
275 	/* Get the HOB list for processing */
276 	hdr = hob_list;
277 
278 	/* Collect memory ranges */
279 	top = FSP_HIGHMEM_BASE;
280 	while (!end_of_hob(hdr)) {
281 		if (hdr->type == HOB_TYPE_RES_DESC) {
282 			res_desc = (struct hob_res_desc *)hdr;
283 			if (res_desc->type == RES_SYS_MEM) {
284 				phys_start = res_desc->phys_start;
285 				/* Need memory above 4GB to be collected here */
286 				if (phys_start >= (phys_addr_t)FSP_HIGHMEM_BASE)
287 					top += (u32)(res_desc->len);
288 			}
289 		}
290 		hdr = get_next_hob(hdr);
291 	}
292 
293 	return top;
294 }
295 
296 u64 fsp_get_reserved_mem_from_guid(const void *hob_list, u64 *len,
297 				   struct efi_guid *guid)
298 {
299 	const struct hob_header *hdr;
300 	struct hob_res_desc *res_desc;
301 
302 	/* Get the HOB list for processing */
303 	hdr = hob_list;
304 
305 	/* Collect memory ranges */
306 	while (!end_of_hob(hdr)) {
307 		if (hdr->type == HOB_TYPE_RES_DESC) {
308 			res_desc = (struct hob_res_desc *)hdr;
309 			if (res_desc->type == RES_MEM_RESERVED) {
310 				if (compare_guid(&res_desc->owner, guid)) {
311 					if (len)
312 						*len = (u32)(res_desc->len);
313 
314 					return (u64)(res_desc->phys_start);
315 				}
316 			}
317 		}
318 		hdr = get_next_hob(hdr);
319 	}
320 
321 	return 0;
322 }
323 
324 u32 fsp_get_fsp_reserved_mem(const void *hob_list, u32 *len)
325 {
326 	const struct efi_guid guid = FSP_HOB_RESOURCE_OWNER_FSP_GUID;
327 	u64 length;
328 	u32 base;
329 
330 	base = (u32)fsp_get_reserved_mem_from_guid(hob_list,
331 			&length, (struct efi_guid *)&guid);
332 	if ((len != 0) && (base != 0))
333 		*len = (u32)length;
334 
335 	return base;
336 }
337 
338 u32 fsp_get_tseg_reserved_mem(const void *hob_list, u32 *len)
339 {
340 	const struct efi_guid guid = FSP_HOB_RESOURCE_OWNER_TSEG_GUID;
341 	u64 length;
342 	u32 base;
343 
344 	base = (u32)fsp_get_reserved_mem_from_guid(hob_list,
345 			&length, (struct efi_guid *)&guid);
346 	if ((len != 0) && (base != 0))
347 		*len = (u32)length;
348 
349 	return base;
350 }
351 
352 const struct hob_header *fsp_get_next_hob(uint type, const void *hob_list)
353 {
354 	const struct hob_header *hdr;
355 
356 	hdr = hob_list;
357 
358 	/* Parse the HOB list until end of list or matching type is found */
359 	while (!end_of_hob(hdr)) {
360 		if (hdr->type == type)
361 			return hdr;
362 
363 		hdr = get_next_hob(hdr);
364 	}
365 
366 	return NULL;
367 }
368 
369 const struct hob_header *fsp_get_next_guid_hob(const struct efi_guid *guid,
370 					       const void *hob_list)
371 {
372 	const struct hob_header *hdr;
373 	struct hob_guid *guid_hob;
374 
375 	hdr = hob_list;
376 	while ((hdr = fsp_get_next_hob(HOB_TYPE_GUID_EXT,
377 			hdr)) != NULL) {
378 		guid_hob = (struct hob_guid *)hdr;
379 		if (compare_guid(guid, &(guid_hob->name)))
380 			break;
381 		hdr = get_next_hob(hdr);
382 	}
383 
384 	return hdr;
385 }
386 
387 void *fsp_get_guid_hob_data(const void *hob_list, u32 *len,
388 			    struct efi_guid *guid)
389 {
390 	const struct hob_header *guid_hob;
391 
392 	guid_hob = fsp_get_next_guid_hob(guid, hob_list);
393 	if (guid_hob == NULL) {
394 		return NULL;
395 	} else {
396 		if (len)
397 			*len = get_guid_hob_data_size(guid_hob);
398 
399 		return get_guid_hob_data(guid_hob);
400 	}
401 }
402 
403 void *fsp_get_nvs_data(const void *hob_list, u32 *len)
404 {
405 	const struct efi_guid guid = FSP_NON_VOLATILE_STORAGE_HOB_GUID;
406 
407 	return fsp_get_guid_hob_data(hob_list, len, (struct efi_guid *)&guid);
408 }
409 
410 void *fsp_get_bootloader_tmp_mem(const void *hob_list, u32 *len)
411 {
412 	const struct efi_guid guid = FSP_BOOTLOADER_TEMP_MEM_HOB_GUID;
413 
414 	return fsp_get_guid_hob_data(hob_list, len, (struct efi_guid *)&guid);
415 }
416