blob: 6e311a12d20bced28d2a6bb947ec41f5ffe57fc1 [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
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Simon Glass6c34fc12019-09-25 08:00:11 -06008#include <asm/fsp1/fsp_support.h>
Bin Meng01223e32014-12-12 21:05:29 +08009#include <asm/post.h>
Bin Meng2922b3e2014-12-12 21:05:28 +080010
Simon Glass13724142019-09-25 08:11:25 -060011struct fsp_header *__attribute__((optimize("O0"))) fsp_find_header(void)
Bin Meng2922b3e2014-12-12 21:05:28 +080012{
Bin Mengdb60d862014-12-17 15:50:49 +080013 /*
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
19 * stack is ready.
20 *
21 * GCC 4.8.1 has been verified to be working for the following codes.
22 */
Bin Meng2922b3e2014-12-12 21:05:28 +080023 volatile register u8 *fsp asm("eax");
24
25 /* Initalize the FSP base */
Bin Meng293f4972014-12-17 15:50:42 +080026 fsp = (u8 *)CONFIG_FSP_ADDR;
Bin Meng2922b3e2014-12-12 21:05:28 +080027
28 /* Check the FV signature, _FVH */
Bin Mengdb60d862014-12-17 15:50:49 +080029 if (((struct fv_header *)fsp)->sign == EFI_FVH_SIGNATURE) {
Bin Meng2922b3e2014-12-12 21:05:28 +080030 /* Go to the end of the FV header and align the address */
Bin Mengdb60d862014-12-17 15:50:49 +080031 fsp += ((struct fv_header *)fsp)->ext_hdr_off;
32 fsp += ((struct fv_ext_header *)fsp)->ext_hdr_size;
Bin Meng2922b3e2014-12-12 21:05:28 +080033 fsp = (u8 *)(((u32)fsp + 7) & 0xFFFFFFF8);
34 } else {
35 fsp = 0;
36 }
37
38 /* Check the FFS GUID */
39 if (fsp &&
Park, Aiden0961cf72019-08-03 08:30:20 +000040 ((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) {
Bin Meng2922b3e2014-12-12 21:05:28 +080056 /* Add the FFS header size to find the raw section header */
Bin Mengdb60d862014-12-17 15:50:49 +080057 fsp += sizeof(struct ffs_file_header);
Bin Meng2922b3e2014-12-12 21:05:28 +080058 } else {
59 fsp = 0;
60 }
61
62 if (fsp &&
Bin Mengdb60d862014-12-17 15:50:49 +080063 ((struct raw_section *)fsp)->type == EFI_SECTION_RAW) {
Bin Meng2922b3e2014-12-12 21:05:28 +080064 /* Add the raw section header size to find the FSP header */
Bin Mengdb60d862014-12-17 15:50:49 +080065 fsp += sizeof(struct raw_section);
Bin Meng2922b3e2014-12-12 21:05:28 +080066 } else {
67 fsp = 0;
68 }
69
Simon Glass457191e2015-01-27 22:13:37 -070070 return (struct fsp_header *)fsp;
Bin Meng2922b3e2014-12-12 21:05:28 +080071}
72
Bin Meng001d5c72015-12-10 22:02:56 -080073void fsp_continue(u32 status, void *hob_list)
Bin Meng2922b3e2014-12-12 21:05:28 +080074{
Bin Meng01223e32014-12-12 21:05:29 +080075 post_code(POST_MRC);
76
Bin Mengdb60d862014-12-17 15:50:49 +080077 assert(status == 0);
Bin Meng2922b3e2014-12-12 21:05:28 +080078
Bin Meng2922b3e2014-12-12 21:05:28 +080079 /* The boot loader main function entry */
Bin Meng01223e32014-12-12 21:05:29 +080080 fsp_init_done(hob_list);
Bin Meng2922b3e2014-12-12 21:05:28 +080081}
82
83void fsp_init(u32 stack_top, u32 boot_mode, void *nvs_buf)
84{
Bin Menga0c94dc2015-12-10 22:02:59 -080085 struct fsp_config_data config_data;
Bin Meng2922b3e2014-12-12 21:05:28 +080086 fsp_init_f init;
Bin Mengdb60d862014-12-17 15:50:49 +080087 struct fsp_init_params params;
88 struct fspinit_rtbuf rt_buf;
Bin Mengdb60d862014-12-17 15:50:49 +080089 struct fsp_header *fsp_hdr;
90 struct fsp_init_params *params_ptr;
Bin Mengf9a61892015-12-10 22:03:01 -080091#ifdef CONFIG_FSP_USE_UPD
92 struct vpd_region *fsp_vpd;
Bin Mengdb60d862014-12-17 15:50:49 +080093 struct upd_region *fsp_upd;
Bin Mengf9a61892015-12-10 22:03:01 -080094#endif
Bin Meng2922b3e2014-12-12 21:05:28 +080095
Simon Glass13724142019-09-25 08:11:25 -060096 fsp_hdr = fsp_find_header();
Bin Meng2922b3e2014-12-12 21:05:28 +080097 if (fsp_hdr == NULL) {
98 /* No valid FSP info header was found */
Bin Mengdb60d862014-12-17 15:50:49 +080099 panic("Invalid FSP header");
Bin Meng2922b3e2014-12-12 21:05:28 +0800100 }
101
Bin Mengc24aebd2015-12-10 22:03:00 -0800102 config_data.common.fsp_hdr = fsp_hdr;
103 config_data.common.stack_top = stack_top;
104 config_data.common.boot_mode = boot_mode;
105
Bin Mengf9a61892015-12-10 22:03:01 -0800106#ifdef CONFIG_FSP_USE_UPD
Bin Meng2922b3e2014-12-12 21:05:28 +0800107 /* Get VPD region start */
Bin Mengdb60d862014-12-17 15:50:49 +0800108 fsp_vpd = (struct vpd_region *)(fsp_hdr->img_base +
Bin Meng2922b3e2014-12-12 21:05:28 +0800109 fsp_hdr->cfg_region_off);
110
Simon Glass8afe6ff2015-01-27 22:13:40 -0700111 /* Verify the VPD data region is valid */
Bin Mengc44ed642015-08-08 22:01:23 +0800112 assert(fsp_vpd->sign == VPD_IMAGE_ID);
Bin Meng2922b3e2014-12-12 21:05:28 +0800113
Bin Mengf9a61892015-12-10 22:03:01 -0800114 fsp_upd = &config_data.fsp_upd;
115
Bin Meng2922b3e2014-12-12 21:05:28 +0800116 /* Copy default data from Flash */
117 memcpy(fsp_upd, (void *)(fsp_hdr->img_base + fsp_vpd->upd_offset),
Bin Mengdb60d862014-12-17 15:50:49 +0800118 sizeof(struct upd_region));
Bin Meng2922b3e2014-12-12 21:05:28 +0800119
Simon Glass8afe6ff2015-01-27 22:13:40 -0700120 /* Verify the UPD data region is valid */
Bin Mengdb60d862014-12-17 15:50:49 +0800121 assert(fsp_upd->terminator == UPD_TERMINATOR);
Bin Mengf9a61892015-12-10 22:03:01 -0800122#endif
123
124 memset(&rt_buf, 0, sizeof(struct fspinit_rtbuf));
125
Bin Mengc24aebd2015-12-10 22:03:00 -0800126 /* Override any configuration if required */
Simon Glass13724142019-09-25 08:11:25 -0600127 fsp_update_configs(&config_data, &rt_buf);
Bin Meng2922b3e2014-12-12 21:05:28 +0800128
Bin Mengdb60d862014-12-17 15:50:49 +0800129 memset(&params, 0, sizeof(struct fsp_init_params));
Bin Meng2922b3e2014-12-12 21:05:28 +0800130 params.nvs_buf = nvs_buf;
Bin Mengdb60d862014-12-17 15:50:49 +0800131 params.rt_buf = (struct fspinit_rtbuf *)&rt_buf;
Simon Glass13724142019-09-25 08:11:25 -0600132 params.continuation = (fsp_continuation_f)fsp_asm_continuation;
Bin Meng2922b3e2014-12-12 21:05:28 +0800133
134 init = (fsp_init_f)(fsp_hdr->img_base + fsp_hdr->fsp_init);
135 params_ptr = &params;
136
Bin Meng01223e32014-12-12 21:05:29 +0800137 post_code(POST_PRE_MRC);
138
Bin Menga3c9fb02015-06-07 11:33:13 +0800139 /* Load GDT for FSP */
140 setup_fsp_gdt();
141
Bin Meng2922b3e2014-12-12 21:05:28 +0800142 /*
Bin Meng001d5c72015-12-10 22:02:56 -0800143 * Use ASM code to ensure the register value in EAX & EDX
144 * will be passed into fsp_continue
Bin Meng2922b3e2014-12-12 21:05:28 +0800145 */
146 asm volatile (
147 "pushl %0;"
148 "call *%%eax;"
Simon Glass13724142019-09-25 08:11:25 -0600149 ".global fsp_asm_continuation;"
150 "fsp_asm_continuation:;"
Bin Meng001d5c72015-12-10 22:02:56 -0800151 "movl 4(%%esp), %%eax;" /* status */
152 "movl 8(%%esp), %%edx;" /* hob_list */
Bin Meng2922b3e2014-12-12 21:05:28 +0800153 "jmp fsp_continue;"
Bin Meng001d5c72015-12-10 22:02:56 -0800154 : : "m"(params_ptr), "a"(init)
Bin Meng2922b3e2014-12-12 21:05:28 +0800155 );
156
157 /*
158 * Should never get here.
Bin Mengdb60d862014-12-17 15:50:49 +0800159 * Control will continue from fsp_continue.
Bin Meng2922b3e2014-12-12 21:05:28 +0800160 * This line below is to prevent the compiler from optimizing
161 * structure intialization.
Bin Mengdb60d862014-12-17 15:50:49 +0800162 *
163 * DO NOT REMOVE!
Bin Meng2922b3e2014-12-12 21:05:28 +0800164 */
165 init(&params);
Bin Meng2922b3e2014-12-12 21:05:28 +0800166}
167
Bin Mengdb60d862014-12-17 15:50:49 +0800168u32 fsp_notify(struct fsp_header *fsp_hdr, u32 phase)
Bin Meng2922b3e2014-12-12 21:05:28 +0800169{
170 fsp_notify_f notify;
Bin Mengdb60d862014-12-17 15:50:49 +0800171 struct fsp_notify_params params;
172 struct fsp_notify_params *params_ptr;
Bin Meng2922b3e2014-12-12 21:05:28 +0800173 u32 status;
174
175 if (!fsp_hdr)
Simon Glass13724142019-09-25 08:11:25 -0600176 fsp_hdr = (struct fsp_header *)fsp_find_header();
Bin Meng2922b3e2014-12-12 21:05:28 +0800177
178 if (fsp_hdr == NULL) {
179 /* No valid FSP info header */
Bin Mengdb60d862014-12-17 15:50:49 +0800180 panic("Invalid FSP header");
Bin Meng2922b3e2014-12-12 21:05:28 +0800181 }
182
183 notify = (fsp_notify_f)(fsp_hdr->img_base + fsp_hdr->fsp_notify);
184 params.phase = phase;
Bin Meng01223e32014-12-12 21:05:29 +0800185 params_ptr = &params;
186
187 /*
188 * Use ASM code to ensure correct parameter is on the stack for
189 * FspNotify as U-Boot is using different ABI from FSP
190 */
191 asm volatile (
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)
196 );
Bin Meng2922b3e2014-12-12 21:05:28 +0800197
198 return status;
199}