blob: d84c632f140742f8cc3ab4b6563d5ba3fb4adc46 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: Intel
Bin Meng2922b3e2014-12-12 21:05:28 +08002/*
3 * Copyright (C) 2013, Intel Corporation
4 * Copyright (C) 2014, Bin Meng <bmeng.cn@gmail.com>
Bin Meng2922b3e2014-12-12 21:05:28 +08005 */
6
Bin Meng01223e32014-12-12 21:05:29 +08007#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glass6c34fc12019-09-25 08:00:11 -06009#include <asm/fsp1/fsp_support.h>
Bin Meng01223e32014-12-12 21:05:29 +080010#include <asm/post.h>
Bin Meng2922b3e2014-12-12 21:05:28 +080011
Simon Glass13724142019-09-25 08:11:25 -060012struct fsp_header *__attribute__((optimize("O0"))) fsp_find_header(void)
Bin Meng2922b3e2014-12-12 21:05:28 +080013{
Bin Mengdb60d862014-12-17 15:50:49 +080014 /*
15 * This function may be called before the a stack is established,
16 * so special care must be taken. First, it cannot declare any local
17 * variable using stack. Only register variable can be used here.
18 * Secondly, some compiler version will add prolog or epilog code
19 * for the C function. If so the function call may not work before
20 * stack is ready.
21 *
22 * GCC 4.8.1 has been verified to be working for the following codes.
23 */
Bin Meng2922b3e2014-12-12 21:05:28 +080024 volatile register u8 *fsp asm("eax");
25
26 /* Initalize the FSP base */
Bin Meng293f4972014-12-17 15:50:42 +080027 fsp = (u8 *)CONFIG_FSP_ADDR;
Bin Meng2922b3e2014-12-12 21:05:28 +080028
29 /* Check the FV signature, _FVH */
Bin Mengdb60d862014-12-17 15:50:49 +080030 if (((struct fv_header *)fsp)->sign == EFI_FVH_SIGNATURE) {
Bin Meng2922b3e2014-12-12 21:05:28 +080031 /* Go to the end of the FV header and align the address */
Bin Mengdb60d862014-12-17 15:50:49 +080032 fsp += ((struct fv_header *)fsp)->ext_hdr_off;
33 fsp += ((struct fv_ext_header *)fsp)->ext_hdr_size;
Bin Meng2922b3e2014-12-12 21:05:28 +080034 fsp = (u8 *)(((u32)fsp + 7) & 0xFFFFFFF8);
35 } else {
36 fsp = 0;
37 }
38
39 /* Check the FFS GUID */
40 if (fsp &&
Park, Aiden0961cf72019-08-03 08:30:20 +000041 ((struct ffs_file_header *)fsp)->name.b[0] == FSP_GUID_BYTE0 &&
42 ((struct ffs_file_header *)fsp)->name.b[1] == FSP_GUID_BYTE1 &&
43 ((struct ffs_file_header *)fsp)->name.b[2] == FSP_GUID_BYTE2 &&
44 ((struct ffs_file_header *)fsp)->name.b[3] == FSP_GUID_BYTE3 &&
45 ((struct ffs_file_header *)fsp)->name.b[4] == FSP_GUID_BYTE4 &&
46 ((struct ffs_file_header *)fsp)->name.b[5] == FSP_GUID_BYTE5 &&
47 ((struct ffs_file_header *)fsp)->name.b[6] == FSP_GUID_BYTE6 &&
48 ((struct ffs_file_header *)fsp)->name.b[7] == FSP_GUID_BYTE7 &&
49 ((struct ffs_file_header *)fsp)->name.b[8] == FSP_GUID_BYTE8 &&
50 ((struct ffs_file_header *)fsp)->name.b[9] == FSP_GUID_BYTE9 &&
51 ((struct ffs_file_header *)fsp)->name.b[10] == FSP_GUID_BYTE10 &&
52 ((struct ffs_file_header *)fsp)->name.b[11] == FSP_GUID_BYTE11 &&
53 ((struct ffs_file_header *)fsp)->name.b[12] == FSP_GUID_BYTE12 &&
54 ((struct ffs_file_header *)fsp)->name.b[13] == FSP_GUID_BYTE13 &&
55 ((struct ffs_file_header *)fsp)->name.b[14] == FSP_GUID_BYTE14 &&
56 ((struct ffs_file_header *)fsp)->name.b[15] == FSP_GUID_BYTE15) {
Bin Meng2922b3e2014-12-12 21:05:28 +080057 /* Add the FFS header size to find the raw section header */
Bin Mengdb60d862014-12-17 15:50:49 +080058 fsp += sizeof(struct ffs_file_header);
Bin Meng2922b3e2014-12-12 21:05:28 +080059 } else {
60 fsp = 0;
61 }
62
63 if (fsp &&
Bin Mengdb60d862014-12-17 15:50:49 +080064 ((struct raw_section *)fsp)->type == EFI_SECTION_RAW) {
Bin Meng2922b3e2014-12-12 21:05:28 +080065 /* Add the raw section header size to find the FSP header */
Bin Mengdb60d862014-12-17 15:50:49 +080066 fsp += sizeof(struct raw_section);
Bin Meng2922b3e2014-12-12 21:05:28 +080067 } else {
68 fsp = 0;
69 }
70
Simon Glass457191e2015-01-27 22:13:37 -070071 return (struct fsp_header *)fsp;
Bin Meng2922b3e2014-12-12 21:05:28 +080072}
73
Bin Meng001d5c72015-12-10 22:02:56 -080074void fsp_continue(u32 status, void *hob_list)
Bin Meng2922b3e2014-12-12 21:05:28 +080075{
Bin Meng01223e32014-12-12 21:05:29 +080076 post_code(POST_MRC);
77
Bin Mengdb60d862014-12-17 15:50:49 +080078 assert(status == 0);
Bin Meng2922b3e2014-12-12 21:05:28 +080079
Bin Meng2922b3e2014-12-12 21:05:28 +080080 /* The boot loader main function entry */
Bin Meng01223e32014-12-12 21:05:29 +080081 fsp_init_done(hob_list);
Bin Meng2922b3e2014-12-12 21:05:28 +080082}
83
84void fsp_init(u32 stack_top, u32 boot_mode, void *nvs_buf)
85{
Bin Menga0c94dc2015-12-10 22:02:59 -080086 struct fsp_config_data config_data;
Bin Meng2922b3e2014-12-12 21:05:28 +080087 fsp_init_f init;
Bin Mengdb60d862014-12-17 15:50:49 +080088 struct fsp_init_params params;
89 struct fspinit_rtbuf rt_buf;
Bin Mengdb60d862014-12-17 15:50:49 +080090 struct fsp_header *fsp_hdr;
91 struct fsp_init_params *params_ptr;
Bin Mengf9a61892015-12-10 22:03:01 -080092#ifdef CONFIG_FSP_USE_UPD
93 struct vpd_region *fsp_vpd;
Bin Mengdb60d862014-12-17 15:50:49 +080094 struct upd_region *fsp_upd;
Bin Mengf9a61892015-12-10 22:03:01 -080095#endif
Bin Meng2922b3e2014-12-12 21:05:28 +080096
Simon Glass13724142019-09-25 08:11:25 -060097 fsp_hdr = fsp_find_header();
Bin Meng2922b3e2014-12-12 21:05:28 +080098 if (fsp_hdr == NULL) {
99 /* No valid FSP info header was found */
Bin Mengdb60d862014-12-17 15:50:49 +0800100 panic("Invalid FSP header");
Bin Meng2922b3e2014-12-12 21:05:28 +0800101 }
102
Bin Mengc24aebd2015-12-10 22:03:00 -0800103 config_data.common.fsp_hdr = fsp_hdr;
104 config_data.common.stack_top = stack_top;
105 config_data.common.boot_mode = boot_mode;
106
Bin Mengf9a61892015-12-10 22:03:01 -0800107#ifdef CONFIG_FSP_USE_UPD
Bin Meng2922b3e2014-12-12 21:05:28 +0800108 /* Get VPD region start */
Bin Mengdb60d862014-12-17 15:50:49 +0800109 fsp_vpd = (struct vpd_region *)(fsp_hdr->img_base +
Bin Meng2922b3e2014-12-12 21:05:28 +0800110 fsp_hdr->cfg_region_off);
111
Simon Glass8afe6ff2015-01-27 22:13:40 -0700112 /* Verify the VPD data region is valid */
Bin Mengc44ed642015-08-08 22:01:23 +0800113 assert(fsp_vpd->sign == VPD_IMAGE_ID);
Bin Meng2922b3e2014-12-12 21:05:28 +0800114
Bin Mengf9a61892015-12-10 22:03:01 -0800115 fsp_upd = &config_data.fsp_upd;
116
Bin Meng2922b3e2014-12-12 21:05:28 +0800117 /* Copy default data from Flash */
118 memcpy(fsp_upd, (void *)(fsp_hdr->img_base + fsp_vpd->upd_offset),
Bin Mengdb60d862014-12-17 15:50:49 +0800119 sizeof(struct upd_region));
Bin Meng2922b3e2014-12-12 21:05:28 +0800120
Simon Glass8afe6ff2015-01-27 22:13:40 -0700121 /* Verify the UPD data region is valid */
Bin Mengdb60d862014-12-17 15:50:49 +0800122 assert(fsp_upd->terminator == UPD_TERMINATOR);
Bin Mengf9a61892015-12-10 22:03:01 -0800123#endif
124
125 memset(&rt_buf, 0, sizeof(struct fspinit_rtbuf));
126
Bin Mengc24aebd2015-12-10 22:03:00 -0800127 /* Override any configuration if required */
Simon Glass13724142019-09-25 08:11:25 -0600128 fsp_update_configs(&config_data, &rt_buf);
Bin Meng2922b3e2014-12-12 21:05:28 +0800129
Bin Mengdb60d862014-12-17 15:50:49 +0800130 memset(&params, 0, sizeof(struct fsp_init_params));
Bin Meng2922b3e2014-12-12 21:05:28 +0800131 params.nvs_buf = nvs_buf;
Bin Mengdb60d862014-12-17 15:50:49 +0800132 params.rt_buf = (struct fspinit_rtbuf *)&rt_buf;
Simon Glass13724142019-09-25 08:11:25 -0600133 params.continuation = (fsp_continuation_f)fsp_asm_continuation;
Bin Meng2922b3e2014-12-12 21:05:28 +0800134
135 init = (fsp_init_f)(fsp_hdr->img_base + fsp_hdr->fsp_init);
136 params_ptr = &params;
137
Bin Meng01223e32014-12-12 21:05:29 +0800138 post_code(POST_PRE_MRC);
139
Bin Menga3c9fb02015-06-07 11:33:13 +0800140 /* Load GDT for FSP */
141 setup_fsp_gdt();
142
Bin Meng2922b3e2014-12-12 21:05:28 +0800143 /*
Bin Meng001d5c72015-12-10 22:02:56 -0800144 * Use ASM code to ensure the register value in EAX & EDX
145 * will be passed into fsp_continue
Bin Meng2922b3e2014-12-12 21:05:28 +0800146 */
147 asm volatile (
148 "pushl %0;"
149 "call *%%eax;"
Simon Glass13724142019-09-25 08:11:25 -0600150 ".global fsp_asm_continuation;"
151 "fsp_asm_continuation:;"
Bin Meng001d5c72015-12-10 22:02:56 -0800152 "movl 4(%%esp), %%eax;" /* status */
153 "movl 8(%%esp), %%edx;" /* hob_list */
Bin Meng2922b3e2014-12-12 21:05:28 +0800154 "jmp fsp_continue;"
Bin Meng001d5c72015-12-10 22:02:56 -0800155 : : "m"(params_ptr), "a"(init)
Bin Meng2922b3e2014-12-12 21:05:28 +0800156 );
157
158 /*
159 * Should never get here.
Bin Mengdb60d862014-12-17 15:50:49 +0800160 * Control will continue from fsp_continue.
Bin Meng2922b3e2014-12-12 21:05:28 +0800161 * This line below is to prevent the compiler from optimizing
162 * structure intialization.
Bin Mengdb60d862014-12-17 15:50:49 +0800163 *
164 * DO NOT REMOVE!
Bin Meng2922b3e2014-12-12 21:05:28 +0800165 */
166 init(&params);
Bin Meng2922b3e2014-12-12 21:05:28 +0800167}
168
Bin Mengdb60d862014-12-17 15:50:49 +0800169u32 fsp_notify(struct fsp_header *fsp_hdr, u32 phase)
Bin Meng2922b3e2014-12-12 21:05:28 +0800170{
171 fsp_notify_f notify;
Bin Mengdb60d862014-12-17 15:50:49 +0800172 struct fsp_notify_params params;
173 struct fsp_notify_params *params_ptr;
Bin Meng2922b3e2014-12-12 21:05:28 +0800174 u32 status;
175
176 if (!fsp_hdr)
Simon Glass13724142019-09-25 08:11:25 -0600177 fsp_hdr = (struct fsp_header *)fsp_find_header();
Bin Meng2922b3e2014-12-12 21:05:28 +0800178
179 if (fsp_hdr == NULL) {
180 /* No valid FSP info header */
Bin Mengdb60d862014-12-17 15:50:49 +0800181 panic("Invalid FSP header");
Bin Meng2922b3e2014-12-12 21:05:28 +0800182 }
183
184 notify = (fsp_notify_f)(fsp_hdr->img_base + fsp_hdr->fsp_notify);
185 params.phase = phase;
Bin Meng01223e32014-12-12 21:05:29 +0800186 params_ptr = &params;
187
188 /*
189 * Use ASM code to ensure correct parameter is on the stack for
190 * FspNotify as U-Boot is using different ABI from FSP
191 */
192 asm volatile (
193 "pushl %1;" /* push notify phase */
194 "call *%%eax;" /* call FspNotify */
195 "addl $4, %%esp;" /* clean up the stack */
196 : "=a"(status) : "m"(params_ptr), "a"(notify), "m"(*params_ptr)
197 );
Bin Meng2922b3e2014-12-12 21:05:28 +0800198
199 return status;
200}