blob: 71f64161cc1662a528cc16d93e716fb2796a8876 [file] [log] [blame]
Martin Rotha6427162014-04-25 14:12:13 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013-2014 Sage Electronic Engineering, LLC.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Martin Rotha6427162014-04-25 14:12:13 -060014 */
15
16#include <types.h>
17#include <string.h>
Martin Rotha6427162014-04-25 14:12:13 -060018#include <console/console.h>
19#include <bootstate.h>
20#include <cbmem.h>
21#include "fsp_util.h"
22#include <lib.h> // hexdump
23#include <ip_checksum.h>
24#include <timestamp.h>
Philipp Deppenwiese13e2a322018-11-23 19:01:12 +010025#include <cpu/intel/microcode.h>
Martin Rotha6427162014-04-25 14:12:13 -060026
27#ifndef __PRE_RAM__
28/* Globals pointers for FSP structures */
29void *FspHobListPtr = NULL;
30FSP_INFO_HEADER *fsp_header_ptr = NULL;
31
32void FspNotify (u32 Phase)
33{
34 FSP_NOTFY_PHASE NotifyPhaseProc;
35 NOTIFY_PHASE_PARAMS NotifyPhaseParams;
36 EFI_STATUS Status;
37
38 if (fsp_header_ptr == NULL) {
39 fsp_header_ptr = (void *)find_fsp();
40 if ((u32)fsp_header_ptr < 0xff) {
41 post_code(0x4F); /* output something in case there is no serial */
42 die("Can't find the FSP!\n");
43 }
44 }
45
46 /* call FSP PEI to Notify PostPciEnumeration */
47 NotifyPhaseProc = (FSP_NOTFY_PHASE)(fsp_header_ptr->ImageBase +
48 fsp_header_ptr->NotifyPhaseEntry);
49 NotifyPhaseParams.Phase = Phase;
50
51 timestamp_add_now(Phase == EnumInitPhaseReadyToBoot ?
52 TS_FSP_BEFORE_FINALIZE : TS_FSP_BEFORE_ENUMERATE);
53
54 Status = NotifyPhaseProc (&NotifyPhaseParams);
55
56 timestamp_add_now(Phase == EnumInitPhaseReadyToBoot ?
57 TS_FSP_AFTER_FINALIZE : TS_FSP_AFTER_ENUMERATE);
58
59 if (Status != 0)
60 printk(BIOS_ERR,"FSP API NotifyPhase failed for phase 0x%x with status: 0x%x\n", Phase, Status);
61}
62#endif /* #ifndef __PRE_RAM__ */
63
64#ifdef __PRE_RAM__
65
66/*
67 * Call the FSP to do memory init. The FSP doesn't return to this function.
68 * The FSP returns to the romstage_main_continue().
69 */
Aaron Durbin0370bcf2018-09-05 09:37:11 -060070void __noreturn fsp_early_init (FSP_INFO_HEADER *fsp_ptr)
Martin Rotha6427162014-04-25 14:12:13 -060071{
72 FSP_FSP_INIT FspInitApi;
73 FSP_INIT_PARAMS FspInitParams;
74 FSP_INIT_RT_BUFFER FspRtBuffer;
75#if IS_ENABLED(CONFIG_FSP_USES_UPD)
76 UPD_DATA_REGION fsp_upd_data;
77#endif
78
Philipp Deppenwiese13e2a322018-11-23 19:01:12 +010079 /* Load microcode before RAM init */
80 if (IS_ENABLED(CONFIG_SUPPORT_CPU_UCODE_IN_CBFS))
81 intel_update_microcode_from_cbfs();
82
Elyes HAOUASb0b0c8c2018-07-08 12:33:47 +020083 memset((void *)&FspRtBuffer, 0, sizeof(FSP_INIT_RT_BUFFER));
Kyösti Mälkki1729cd82014-10-16 12:47:25 +030084 FspRtBuffer.Common.StackTop = (u32 *)CONFIG_RAMTOP;
Martin Rotha6427162014-04-25 14:12:13 -060085 FspInitParams.NvsBufferPtr = NULL;
86
87#if IS_ENABLED(CONFIG_FSP_USES_UPD)
88 FspRtBuffer.Common.UpdDataRgnPtr = &fsp_upd_data;
89#endif
90 FspInitParams.RtBufferPtr = (FSP_INIT_RT_BUFFER *)&FspRtBuffer;
91 FspInitParams.ContinuationFunc = (CONTINUATION_PROC)ChipsetFspReturnPoint;
92 FspInitApi = (FSP_FSP_INIT)(fsp_ptr->ImageBase + fsp_ptr->FspInitEntry);
93
94 /* Call the chipset code to fill in the chipset specific structures */
95 chipset_fsp_early_init(&FspInitParams, fsp_ptr);
96
97 /* Call back to romstage for board specific changes */
98 romstage_fsp_rt_buffer_callback(&FspRtBuffer);
99
Duncan Lauriefb509832015-11-22 14:53:57 -0800100 post_code(POST_FSP_MEMORY_INIT);
Martin Rotha6427162014-04-25 14:12:13 -0600101 FspInitApi(&FspInitParams);
102
103 /* Should never return. Control will continue from ContinuationFunc */
104 die("Uh Oh! FspInitApi returned");
105}
106#endif /* __PRE_RAM__ */
107
Elyes HAOUASb0b0c8c2018-07-08 12:33:47 +0200108volatile u8 *find_fsp()
Martin Rotha6427162014-04-25 14:12:13 -0600109{
110
111#ifdef __PRE_RAM__
112 volatile register u8 *fsp_ptr asm ("eax");
113
114 /* Entry point for CAR assembly routine */
115 __asm__ __volatile__ (
Damien Zammitf796dd82017-09-02 17:48:33 +1000116 ".global find_fsp_bypass_prologue\n\t"
117 "find_fsp_bypass_prologue:\n\t"
Martin Rotha6427162014-04-25 14:12:13 -0600118 );
119#else
120 volatile u8 *fsp_ptr;
Elyes HAOUAS0d8f1da2018-05-28 15:48:04 +0200121#endif /* __PRE_RAM__ */
Martin Rotha6427162014-04-25 14:12:13 -0600122
Martin Rotha6427162014-04-25 14:12:13 -0600123 /* The FSP is stored in CBFS */
124 fsp_ptr = (u8 *) CONFIG_FSP_LOC;
125
126 /* Check the FV signature, _FVH */
127 if (((EFI_FIRMWARE_VOLUME_HEADER *)fsp_ptr)->Signature == 0x4856465F) {
128 /* Go to the end of the FV header and align the address. */
129 fsp_ptr += ((EFI_FIRMWARE_VOLUME_HEADER *)fsp_ptr)->ExtHeaderOffset;
130 fsp_ptr += ((EFI_FIRMWARE_VOLUME_EXT_HEADER *)fsp_ptr)->ExtHeaderSize;
131 fsp_ptr = (u8 *)(((u32)fsp_ptr + 7) & 0xFFFFFFF8);
132 } else {
133 fsp_ptr = (u8*)ERROR_NO_FV_SIG;
134 }
135
136 /* Check the FFS GUID */
137 if (((u32)fsp_ptr > 0xff) &&
138 (((u32 *)&(((EFI_FFS_FILE_HEADER *)fsp_ptr)->Name))[0] == 0x912740BE) &&
139 (((u32 *)&(((EFI_FFS_FILE_HEADER *)fsp_ptr)->Name))[1] == 0x47342284) &&
140 (((u32 *)&(((EFI_FFS_FILE_HEADER *)fsp_ptr)->Name))[2] == 0xB08471B9) &&
141 (((u32 *)&(((EFI_FFS_FILE_HEADER *)fsp_ptr)->Name))[3] == 0x0C3F3527)) {
142 /* Add the FFS Header size to the base to find the Raw section Header */
143 fsp_ptr += sizeof(EFI_FFS_FILE_HEADER);
144 } else {
145 fsp_ptr = (u8 *)ERROR_NO_FFS_GUID;
146 }
147
148 if (((u32)fsp_ptr > 0xff) &&
149 ((EFI_RAW_SECTION *)fsp_ptr)->Type == EFI_SECTION_RAW) {
150 /* Add the Raw Header size to the base to find the FSP INFO Header */
151 fsp_ptr += sizeof(EFI_RAW_SECTION);
152 } else {
153 fsp_ptr = (u8 *)ERROR_NO_INFO_HEADER;
154 }
155
156 /* Verify that the FSP is set to the base address we're expecting.*/
157 if (((u32)fsp_ptr > 0xff) &&
158 (*(u32*)(fsp_ptr + FSP_IMAGE_BASE_LOC) != CONFIG_FSP_LOC)) {
159 fsp_ptr = (u8 *)ERROR_IMAGEBASE_MISMATCH;
160 }
161
162 /* Verify the FSP Signature */
163 if (((u32)fsp_ptr > 0xff) &&
164 (*(u32*)(fsp_ptr + FSP_IMAGE_SIG_LOC) != FSP_SIG)){
165 fsp_ptr = (u8 *)ERROR_INFO_HEAD_SIG_MISMATCH;
166 }
167
168 /* Verify the FSP ID */
169 if (((u32)fsp_ptr > 0xff) &&
170 ((*(u32 *)(fsp_ptr + FSP_IMAGE_ID_LOC) != FSP_IMAGE_ID_DWORD0) ||
171 (*(u32 *)(fsp_ptr + (FSP_IMAGE_ID_LOC + 4)) != FSP_IMAGE_ID_DWORD1))) {
172 fsp_ptr = (u8 *)ERROR_FSP_SIG_MISMATCH;
173 }
174
175 return (fsp_ptr);
176}
177
Martin Roth5fc32bf2015-01-11 14:11:55 -0700178/** finds the saved temporary memory information in the FSP HOB list
179 *
180 * @param hob_list_ptr pointer to the start of the hob list
181 * @return pointer to saved CAR MEM or NULL if not found.
182 */
Elyes HAOUASb0b0c8c2018-07-08 12:33:47 +0200183void *find_saved_temp_mem(void *hob_list_ptr)
Martin Roth5fc32bf2015-01-11 14:11:55 -0700184{
185 EFI_GUID temp_hob_guid = FSP_BOOTLOADER_TEMPORARY_MEMORY_HOB_GUID;
186 EFI_HOB_GUID_TYPE *saved_mem_hob =
187 (EFI_HOB_GUID_TYPE *) find_hob_by_guid(
188 hob_list_ptr, &temp_hob_guid);
189
190 if (saved_mem_hob == NULL)
191 return NULL;
192
193 return (void *) ((char *) saved_mem_hob + sizeof(EFI_HOB_GUID_TYPE));
194}
195
Martin Roth22138432015-01-30 20:56:05 -0700196#ifndef FSP_RESERVE_MEMORY_SIZE
197/** @brief locates the HOB containing the location of the fsp reserved mem area
198 *
199 * @param hob_list_ptr pointer to the start of the hob list
200 * @return pointer to the start of the FSP reserved memory or NULL if not found.
201 */
Elyes HAOUASb0b0c8c2018-07-08 12:33:47 +0200202void *find_fsp_reserved_mem(void *hob_list_ptr)
Martin Roth22138432015-01-30 20:56:05 -0700203{
204 EFI_GUID fsp_reserved_guid = FSP_HOB_RESOURCE_OWNER_FSP_GUID;
205 EFI_HOB_RESOURCE_DESCRIPTOR *fsp_reserved_mem =
206 (EFI_HOB_RESOURCE_DESCRIPTOR *) find_hob_by_guid(
207 hob_list_ptr, &fsp_reserved_guid);
208
209 if (fsp_reserved_mem == NULL)
210 return NULL;
211
212 return (void *)((uintptr_t)fsp_reserved_mem->PhysicalStart);
213}
214#endif /* FSP_RESERVE_MEMORY_SIZE */
215
Martin Rotha6427162014-04-25 14:12:13 -0600216#ifndef __PRE_RAM__ /* Only parse HOB data in ramstage */
217
218void print_fsp_info(void) {
219
220 if (fsp_header_ptr == NULL)
221 fsp_header_ptr = (void *)find_fsp();
Martin Rotha4c57402015-12-21 13:01:45 -0700222
223 if ((u32)fsp_header_ptr < 0xff) {
224 post_code(0x4F); /* post code in case there is no serial */
225 die("Can't find the FSP!\n");
226 }
Martin Rotha6427162014-04-25 14:12:13 -0600227
228 if (FspHobListPtr == NULL) {
Elyes HAOUASb0b0c8c2018-07-08 12:33:47 +0200229 FspHobListPtr = (void *)*((u32 *)
230 cbmem_find(CBMEM_ID_HOB_POINTER));
Martin Rotha6427162014-04-25 14:12:13 -0600231 }
232
233 printk(BIOS_SPEW,"fsp_header_ptr: %p\n", fsp_header_ptr);
234 printk(BIOS_INFO,"FSP Header Version: %d\n", fsp_header_ptr->HeaderRevision);
235 printk(BIOS_INFO,"FSP Revision: %d.%d\n",
236 (u8)((fsp_header_ptr->ImageRevision >> 8) & 0xff),
237 (u8)(fsp_header_ptr->ImageRevision & 0xff));
238}
239
Martin Rotha6427162014-04-25 14:12:13 -0600240
241#if IS_ENABLED(CONFIG_ENABLE_MRC_CACHE)
242/**
243 * Save the FSP memory HOB (mrc data) to the MRC area in CBMEM
244 */
245int save_mrc_data(void *hob_start)
246{
247 u32 *mrc_hob;
248 u32 *mrc_hob_data;
249 u32 mrc_hob_size;
250 struct mrc_data_container *mrc_data;
251 int output_len;
252 const EFI_GUID mrc_guid = FSP_NON_VOLATILE_STORAGE_HOB_GUID;
253
254 mrc_hob = GetNextGuidHob(&mrc_guid, hob_start);
255 if (mrc_hob == NULL){
256 printk(BIOS_DEBUG, "Memory Configure Data Hob is not present\n");
257 return(0);
258 }
259
260 mrc_hob_data = GET_GUID_HOB_DATA (mrc_hob);
261 mrc_hob_size = (u32) GET_HOB_LENGTH(mrc_hob);
262
263 printk(BIOS_DEBUG, "Memory Configure Data Hob at %p (size = 0x%x).\n",
264 (void *)mrc_hob_data, mrc_hob_size);
265
266 output_len = ALIGN(mrc_hob_size, 16);
267
268 /* Save the MRC S3/fast boot/ADR restore data to cbmem */
269 mrc_data = cbmem_add (CBMEM_ID_MRCDATA,
270 output_len + sizeof(struct mrc_data_container));
271
272 /* Just return if there was a problem with getting CBMEM */
273 if (mrc_data == NULL) {
274 printk(BIOS_WARNING, "CBMEM was not available to save the fast boot cache data.\n");
275 return 0;
276 }
277
278 printk(BIOS_DEBUG, "Copy FSP MRC DATA to HOB (source addr %p, dest addr %p, %u bytes)\n",
279 (void *)mrc_hob_data, mrc_data, output_len);
280
281 mrc_data->mrc_signature = MRC_DATA_SIGNATURE;
282 mrc_data->mrc_data_size = output_len;
283 mrc_data->reserved = 0;
284 memcpy(mrc_data->mrc_data, (const void *)mrc_hob_data, mrc_hob_size);
285
286 /* Zero the unused space in aligned buffer. */
287 if (output_len > mrc_hob_size)
288 memset((mrc_data->mrc_data + mrc_hob_size), 0,
289 output_len - mrc_hob_size);
290
291 mrc_data->mrc_checksum = compute_ip_checksum(mrc_data->mrc_data,
292 mrc_data->mrc_data_size);
293
294 printk(BIOS_SPEW, "Fast boot data (includes align and checksum):\n");
York Yangc13ad6c2015-04-23 13:00:20 -0700295 hexdump32(BIOS_SPEW, (void *)mrc_data->mrc_data, output_len / 4);
Martin Rotha6427162014-04-25 14:12:13 -0600296 return (1);
297}
298#endif /* CONFIG_ENABLE_MRC_CACHE */
299
300static void find_fsp_hob_update_mrc(void *unused)
301{
302 /* Set the global HOB list pointer */
Elyes HAOUASb0b0c8c2018-07-08 12:33:47 +0200303 FspHobListPtr = (void *)*((u32 *) cbmem_find(CBMEM_ID_HOB_POINTER));
Martin Rotha6427162014-04-25 14:12:13 -0600304
305 if (!FspHobListPtr){
306 printk(BIOS_ERR, "ERROR: Could not find FSP HOB pointer in CBFS!\n");
307 } else {
308 /* 0x0000: Print all types */
309 print_hob_type_structure(0x000, FspHobListPtr);
310
311 #if IS_ENABLED(CONFIG_ENABLE_MRC_CACHE)
Elyes HAOUAS2e4d8062016-08-25 20:50:50 +0200312 if (save_mrc_data(FspHobListPtr))
Martin Rotha6427162014-04-25 14:12:13 -0600313 update_mrc_cache(NULL);
314 else
315 printk(BIOS_DEBUG,"Not updating MRC data in flash.\n");
316 #endif
317 }
318}
319
Martin Roth562d6f32015-04-16 21:20:34 -0600320/** @brief Notify FSP for PostPciEnumeration
321 *
322 * @param unused
323 */
324static void fsp_after_pci_enum(void *unused)
325{
326 /* This call needs to be done before resource allocation. */
327 printk(BIOS_DEBUG, "FspNotify(EnumInitPhaseAfterPciEnumeration)\n");
Duncan Lauriefb509832015-11-22 14:53:57 -0800328 post_code(POST_FSP_NOTIFY_BEFORE_ENUMERATE);
Martin Roth562d6f32015-04-16 21:20:34 -0600329 FspNotify(EnumInitPhaseAfterPciEnumeration);
330 printk(BIOS_DEBUG,
331 "Returned from FspNotify(EnumInitPhaseAfterPciEnumeration)\n");
332}
333
334/** @brief Notify FSP for ReadyToBoot
335 *
336 * @param unused
337 */
338static void fsp_finalize(void *unused)
339{
340 printk(BIOS_DEBUG, "FspNotify(EnumInitPhaseReadyToBoot)\n");
341 print_fsp_info();
Duncan Lauriefb509832015-11-22 14:53:57 -0800342 post_code(POST_FSP_NOTIFY_BEFORE_FINALIZE);
Martin Roth562d6f32015-04-16 21:20:34 -0600343 FspNotify(EnumInitPhaseReadyToBoot);
344 printk(BIOS_DEBUG, "Returned from FspNotify(EnumInitPhaseReadyToBoot)\n");
345}
346
347/* Set up for the ramstage FSP calls */
348BOOT_STATE_INIT_ENTRY(BS_DEV_ENUMERATE, BS_ON_EXIT, fsp_after_pci_enum, NULL);
349BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_BOOT, BS_ON_ENTRY, fsp_finalize, NULL);
350
Martin Rotha6427162014-04-25 14:12:13 -0600351/* Update the MRC/fast boot cache as part of the late table writing stage */
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500352BOOT_STATE_INIT_ENTRY(BS_WRITE_TABLES, BS_ON_ENTRY,
353 find_fsp_hob_update_mrc, NULL);
Martin Rotha6427162014-04-25 14:12:13 -0600354#endif /* #ifndef __PRE_RAM__ */