1 // SPDX-License-Identifier: Intel
3 * Copyright (C) 2013, Intel Corporation
4 * Copyright (C) 2014, Bin Meng <bmeng.cn@gmail.com>
8 #include <asm/fsp/fsp_support.h>
11 struct fsp_header *__attribute__((optimize("O0"))) find_fsp_header(void)
14 * This function may be called before the a stack is established,
15 * so special care must be taken. First, it cannot declare any local
16 * variable using stack. Only register variable can be used here.
17 * Secondly, some compiler version will add prolog or epilog code
18 * for the C function. If so the function call may not work before
21 * GCC 4.8.1 has been verified to be working for the following codes.
23 volatile register u8 *fsp asm("eax");
25 /* Initalize the FSP base */
26 fsp = (u8 *)CONFIG_FSP_ADDR;
28 /* Check the FV signature, _FVH */
29 if (((struct fv_header *)fsp)->sign == EFI_FVH_SIGNATURE) {
30 /* Go to the end of the FV header and align the address */
31 fsp += ((struct fv_header *)fsp)->ext_hdr_off;
32 fsp += ((struct fv_ext_header *)fsp)->ext_hdr_size;
33 fsp = (u8 *)(((u32)fsp + 7) & 0xFFFFFFF8);
38 /* Check the FFS GUID */
40 ((struct ffs_file_header *)fsp)->name.b[0] == FSP_GUID_BYTE0 &&
41 ((struct ffs_file_header *)fsp)->name.b[1] == FSP_GUID_BYTE1 &&
42 ((struct ffs_file_header *)fsp)->name.b[2] == FSP_GUID_BYTE2 &&
43 ((struct ffs_file_header *)fsp)->name.b[3] == FSP_GUID_BYTE3 &&
44 ((struct ffs_file_header *)fsp)->name.b[4] == FSP_GUID_BYTE4 &&
45 ((struct ffs_file_header *)fsp)->name.b[5] == FSP_GUID_BYTE5 &&
46 ((struct ffs_file_header *)fsp)->name.b[6] == FSP_GUID_BYTE6 &&
47 ((struct ffs_file_header *)fsp)->name.b[7] == FSP_GUID_BYTE7 &&
48 ((struct ffs_file_header *)fsp)->name.b[8] == FSP_GUID_BYTE8 &&
49 ((struct ffs_file_header *)fsp)->name.b[9] == FSP_GUID_BYTE9 &&
50 ((struct ffs_file_header *)fsp)->name.b[10] == FSP_GUID_BYTE10 &&
51 ((struct ffs_file_header *)fsp)->name.b[11] == FSP_GUID_BYTE11 &&
52 ((struct ffs_file_header *)fsp)->name.b[12] == FSP_GUID_BYTE12 &&
53 ((struct ffs_file_header *)fsp)->name.b[13] == FSP_GUID_BYTE13 &&
54 ((struct ffs_file_header *)fsp)->name.b[14] == FSP_GUID_BYTE14 &&
55 ((struct ffs_file_header *)fsp)->name.b[15] == FSP_GUID_BYTE15) {
56 /* Add the FFS header size to find the raw section header */
57 fsp += sizeof(struct ffs_file_header);
63 ((struct raw_section *)fsp)->type == EFI_SECTION_RAW) {
64 /* Add the raw section header size to find the FSP header */
65 fsp += sizeof(struct raw_section);
70 return (struct fsp_header *)fsp;
73 void fsp_continue(u32 status, void *hob_list)
79 /* The boot loader main function entry */
80 fsp_init_done(hob_list);
83 void fsp_init(u32 stack_top, u32 boot_mode, void *nvs_buf)
85 struct fsp_config_data config_data;
87 struct fsp_init_params params;
88 struct fspinit_rtbuf rt_buf;
89 struct fsp_header *fsp_hdr;
90 struct fsp_init_params *params_ptr;
91 #ifdef CONFIG_FSP_USE_UPD
92 struct vpd_region *fsp_vpd;
93 struct upd_region *fsp_upd;
96 fsp_hdr = find_fsp_header();
97 if (fsp_hdr == NULL) {
98 /* No valid FSP info header was found */
99 panic("Invalid FSP header");
102 config_data.common.fsp_hdr = fsp_hdr;
103 config_data.common.stack_top = stack_top;
104 config_data.common.boot_mode = boot_mode;
106 #ifdef CONFIG_FSP_USE_UPD
107 /* Get VPD region start */
108 fsp_vpd = (struct vpd_region *)(fsp_hdr->img_base +
109 fsp_hdr->cfg_region_off);
111 /* Verify the VPD data region is valid */
112 assert(fsp_vpd->sign == VPD_IMAGE_ID);
114 fsp_upd = &config_data.fsp_upd;
116 /* Copy default data from Flash */
117 memcpy(fsp_upd, (void *)(fsp_hdr->img_base + fsp_vpd->upd_offset),
118 sizeof(struct upd_region));
120 /* Verify the UPD data region is valid */
121 assert(fsp_upd->terminator == UPD_TERMINATOR);
124 memset(&rt_buf, 0, sizeof(struct fspinit_rtbuf));
126 /* Override any configuration if required */
127 update_fsp_configs(&config_data, &rt_buf);
129 memset(¶ms, 0, sizeof(struct fsp_init_params));
130 params.nvs_buf = nvs_buf;
131 params.rt_buf = (struct fspinit_rtbuf *)&rt_buf;
132 params.continuation = (fsp_continuation_f)asm_continuation;
134 init = (fsp_init_f)(fsp_hdr->img_base + fsp_hdr->fsp_init);
135 params_ptr = ¶ms;
137 post_code(POST_PRE_MRC);
139 /* Load GDT for FSP */
143 * Use ASM code to ensure the register value in EAX & EDX
144 * will be passed into fsp_continue
149 ".global asm_continuation;"
151 "movl 4(%%esp), %%eax;" /* status */
152 "movl 8(%%esp), %%edx;" /* hob_list */
154 : : "m"(params_ptr), "a"(init)
158 * Should never get here.
159 * Control will continue from fsp_continue.
160 * This line below is to prevent the compiler from optimizing
161 * structure intialization.
168 u32 fsp_notify(struct fsp_header *fsp_hdr, u32 phase)
171 struct fsp_notify_params params;
172 struct fsp_notify_params *params_ptr;
176 fsp_hdr = (struct fsp_header *)find_fsp_header();
178 if (fsp_hdr == NULL) {
179 /* No valid FSP info header */
180 panic("Invalid FSP header");
183 notify = (fsp_notify_f)(fsp_hdr->img_base + fsp_hdr->fsp_notify);
184 params.phase = phase;
185 params_ptr = ¶ms;
188 * Use ASM code to ensure correct parameter is on the stack for
189 * FspNotify as U-Boot is using different ABI from FSP
192 "pushl %1;" /* push notify phase */
193 "call *%%eax;" /* call FspNotify */
194 "addl $4, %%esp;" /* clean up the stack */
195 : "=a"(status) : "m"(params_ptr), "a"(notify), "m"(*params_ptr)
201 u32 fsp_get_usable_lowmem_top(const void *hob_list)
203 const struct hob_header *hdr;
204 struct hob_res_desc *res_desc;
205 phys_addr_t phys_start;
207 #ifdef CONFIG_FSP_BROKEN_HOB
208 struct hob_mem_alloc *res_mem;
209 phys_addr_t mem_base = 0;
212 /* Get the HOB list for processing */
215 /* * Collect memory ranges */
216 top = FSP_LOWMEM_BASE;
217 while (!end_of_hob(hdr)) {
218 if (hdr->type == HOB_TYPE_RES_DESC) {
219 res_desc = (struct hob_res_desc *)hdr;
220 if (res_desc->type == RES_SYS_MEM) {
221 phys_start = res_desc->phys_start;
222 /* Need memory above 1MB to be collected here */
223 if (phys_start >= FSP_LOWMEM_BASE &&
224 phys_start < (phys_addr_t)FSP_HIGHMEM_BASE)
225 top += (u32)(res_desc->len);
229 #ifdef CONFIG_FSP_BROKEN_HOB
231 * Find out the lowest memory base address allocated by FSP
232 * for the boot service data
234 if (hdr->type == HOB_TYPE_MEM_ALLOC) {
235 res_mem = (struct hob_mem_alloc *)hdr;
237 mem_base = res_mem->mem_base;
238 if (res_mem->mem_base < mem_base)
239 mem_base = res_mem->mem_base;
243 hdr = get_next_hob(hdr);
246 #ifdef CONFIG_FSP_BROKEN_HOB
248 * Check whether the memory top address is below the FSP HOB list.
249 * If not, use the lowest memory base address allocated by FSP as
250 * the memory top address. This is to prevent U-Boot relocation
251 * overwrites the important boot service data which is used by FSP,
252 * otherwise the subsequent call to fsp_notify() will fail.
254 if (top > (u32)hob_list) {
255 debug("Adjust memory top address due to a buggy FSP\n");
263 u64 fsp_get_usable_highmem_top(const void *hob_list)
265 const struct hob_header *hdr;
266 struct hob_res_desc *res_desc;
267 phys_addr_t phys_start;
270 /* Get the HOB list for processing */
273 /* Collect memory ranges */
274 top = FSP_HIGHMEM_BASE;
275 while (!end_of_hob(hdr)) {
276 if (hdr->type == HOB_TYPE_RES_DESC) {
277 res_desc = (struct hob_res_desc *)hdr;
278 if (res_desc->type == RES_SYS_MEM) {
279 phys_start = res_desc->phys_start;
280 /* Need memory above 4GB to be collected here */
281 if (phys_start >= (phys_addr_t)FSP_HIGHMEM_BASE)
282 top += (u32)(res_desc->len);
285 hdr = get_next_hob(hdr);
291 u64 fsp_get_reserved_mem_from_guid(const void *hob_list, u64 *len,
292 const efi_guid_t *guid)
294 const struct hob_header *hdr;
295 struct hob_res_desc *res_desc;
297 /* Get the HOB list for processing */
300 /* Collect memory ranges */
301 while (!end_of_hob(hdr)) {
302 if (hdr->type == HOB_TYPE_RES_DESC) {
303 res_desc = (struct hob_res_desc *)hdr;
304 if (res_desc->type == RES_MEM_RESERVED) {
305 if (!guidcmp(&res_desc->owner, guid)) {
307 *len = (u32)(res_desc->len);
309 return (u64)(res_desc->phys_start);
313 hdr = get_next_hob(hdr);
319 u32 fsp_get_fsp_reserved_mem(const void *hob_list, u32 *len)
321 const efi_guid_t guid = FSP_HOB_RESOURCE_OWNER_FSP_GUID;
325 base = (u32)fsp_get_reserved_mem_from_guid(hob_list,
327 if ((len != 0) && (base != 0))
333 u32 fsp_get_tseg_reserved_mem(const void *hob_list, u32 *len)
335 const efi_guid_t guid = FSP_HOB_RESOURCE_OWNER_TSEG_GUID;
339 base = (u32)fsp_get_reserved_mem_from_guid(hob_list,
341 if ((len != 0) && (base != 0))
347 void *fsp_get_nvs_data(const void *hob_list, u32 *len)
349 const efi_guid_t guid = FSP_NON_VOLATILE_STORAGE_HOB_GUID;
351 return hob_get_guid_hob_data(hob_list, len, &guid);
354 void *fsp_get_bootloader_tmp_mem(const void *hob_list, u32 *len)
356 const efi_guid_t guid = FSP_BOOTLOADER_TEMP_MEM_HOB_GUID;
358 return hob_get_guid_hob_data(hob_list, len, &guid);
361 void *fsp_get_graphics_info(const void *hob_list, u32 *len)
363 const efi_guid_t guid = FSP_GRAPHICS_INFO_HOB_GUID;
365 return hob_get_guid_hob_data(hob_list, len, &guid);