]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFsp2Pkg/FspSecCore/SecFsp.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / IntelFsp2Pkg / FspSecCore / SecFsp.c
CommitLineData
cf1d4549
JY
1/** @file\r
2\r
630df8c8 3 Copyright (c) 2014 - 2022, Intel Corporation. All rights reserved.<BR>\r
9672cd30 4 SPDX-License-Identifier: BSD-2-Clause-Patent\r
cf1d4549
JY
5\r
6**/\r
7\r
8#include "SecFsp.h"\r
9\r
10/**\r
11\r
12 Calculate the FSP IDT gate descriptor.\r
13\r
14 @param[in] IdtEntryTemplate IDT gate descriptor template.\r
15\r
16 @return FSP specific IDT gate descriptor.\r
17\r
18**/\r
470206ba 19IA32_IDT_GATE_DESCRIPTOR\r
111f2228 20FspGetExceptionHandler (\r
cf1d4549
JY
21 IN UINT64 IdtEntryTemplate\r
22 )\r
23{\r
24 UINT32 Entry;\r
470206ba 25 IA32_IDT_GATE_DESCRIPTOR ExceptionHandler;\r
111f2228
MK
26 IA32_IDT_GATE_DESCRIPTOR *IdtGateDescriptor;\r
27 FSP_INFO_HEADER *FspInfoHeader;\r
28\r
470206ba 29 ZeroMem ((VOID *)&ExceptionHandler, sizeof (IA32_IDT_GATE_DESCRIPTOR));\r
630df8c8 30 FspInfoHeader = (FSP_INFO_HEADER *)(UINTN)AsmGetFspInfoHeader ();\r
470206ba
KT
31 *(UINT64 *) &ExceptionHandler = IdtEntryTemplate;\r
32 IdtGateDescriptor = &ExceptionHandler;\r
111f2228
MK
33 Entry = (IdtGateDescriptor->Bits.OffsetHigh << 16) | IdtGateDescriptor->Bits.OffsetLow;\r
34 Entry = FspInfoHeader->ImageBase + FspInfoHeader->ImageSize - (~Entry + 1);\r
cf1d4549
JY
35 IdtGateDescriptor->Bits.OffsetHigh = (UINT16)(Entry >> 16);\r
36 IdtGateDescriptor->Bits.OffsetLow = (UINT16)Entry;\r
37\r
38 return ExceptionHandler;\r
39}\r
40\r
41/**\r
42 This interface fills platform specific data.\r
43\r
44 @param[in,out] FspData Pointer to the FSP global data.\r
45\r
46**/\r
47VOID\r
48EFIAPI\r
49SecGetPlatformData (\r
111f2228 50 IN OUT FSP_GLOBAL_DATA *FspData\r
cf1d4549
JY
51 )\r
52{\r
111f2228
MK
53 FSP_PLAT_DATA *FspPlatformData;\r
54 UINT32 TopOfCar;\r
55 UINT32 *StackPtr;\r
56 UINT32 DwordSize;\r
cf1d4549
JY
57\r
58 FspPlatformData = &FspData->PlatformData;\r
59\r
60 //\r
61 // The entries of platform information, together with the number of them,\r
62 // reside in the bottom of stack, left untouched by normal stack operation.\r
63 //\r
64\r
111f2228 65 FspPlatformData->DataPtr = NULL;\r
cf1d4549
JY
66 FspPlatformData->MicrocodeRegionBase = 0;\r
67 FspPlatformData->MicrocodeRegionSize = 0;\r
68 FspPlatformData->CodeRegionBase = 0;\r
69 FspPlatformData->CodeRegionSize = 0;\r
70\r
71 //\r
72 // Pointer to the size field\r
73 //\r
111f2228 74 TopOfCar = PcdGet32 (PcdTemporaryRamBase) + PcdGet32 (PcdTemporaryRamSize);\r
cf1d4549
JY
75 StackPtr = (UINT32 *)(TopOfCar - sizeof (UINT32));\r
76\r
77 if (*(StackPtr - 1) == FSP_MCUD_SIGNATURE) {\r
78 while (*StackPtr != 0) {\r
79 if (*(StackPtr - 1) == FSP_MCUD_SIGNATURE) {\r
80 //\r
81 // This following data was pushed onto stack after TempRamInit API\r
82 //\r
83 DwordSize = 4;\r
84 StackPtr = StackPtr - 1 - DwordSize;\r
85 CopyMem (&(FspPlatformData->MicrocodeRegionBase), StackPtr, (DwordSize << 2));\r
86 StackPtr--;\r
87 } else if (*(StackPtr - 1) == FSP_PER0_SIGNATURE) {\r
88 //\r
89 // This is the performance data for InitTempMemory API entry/exit\r
90 //\r
91 DwordSize = 4;\r
92 StackPtr = StackPtr - 1 - DwordSize;\r
93 CopyMem (FspData->PerfData, StackPtr, (DwordSize << 2));\r
94\r
95 ((UINT8 *)(&FspData->PerfData[0]))[7] = FSP_PERF_ID_API_TEMP_RAM_INIT_ENTRY;\r
96 ((UINT8 *)(&FspData->PerfData[1]))[7] = FSP_PERF_ID_API_TEMP_RAM_INIT_EXIT;\r
97\r
98 StackPtr--;\r
99 } else {\r
100 StackPtr -= (*StackPtr);\r
101 }\r
102 }\r
103 }\r
104}\r
105\r
106/**\r
107\r
108 Initialize the FSP global data region.\r
109 It needs to be done as soon as possible after the stack is setup.\r
110\r
111 @param[in,out] PeiFspData Pointer of the FSP global data.\r
112 @param[in] BootLoaderStack BootLoader stack.\r
113 @param[in] ApiIdx The index of the FSP API.\r
114\r
115**/\r
116VOID\r
117FspGlobalDataInit (\r
111f2228 118 IN OUT FSP_GLOBAL_DATA *PeiFspData,\r
630df8c8 119 IN UINTN BootLoaderStack,\r
111f2228 120 IN UINT8 ApiIdx\r
cf1d4549
JY
121 )\r
122{\r
111f2228
MK
123 VOID *FspmUpdDataPtr;\r
124 CHAR8 ImageId[9];\r
125 UINTN Idx;\r
cf1d4549
JY
126\r
127 //\r
128 // Set FSP Global Data pointer\r
129 //\r
111f2228
MK
130 SetFspGlobalDataPointer (PeiFspData);\r
131 ZeroMem ((VOID *)PeiFspData, sizeof (FSP_GLOBAL_DATA));\r
cf1d4549 132\r
111f2228 133 PeiFspData->Signature = FSP_GLOBAL_DATA_SIGNATURE;\r
d40965b9 134 PeiFspData->Version = FSP_GLOBAL_DATA_VERSION;\r
111f2228
MK
135 PeiFspData->CoreStack = BootLoaderStack;\r
136 PeiFspData->PerfIdx = 2;\r
137 PeiFspData->PerfSig = FSP_PERFORMANCE_DATA_SIGNATURE;\r
3d35a6c2
CC
138 //\r
139 // Cache FspHobList pointer passed by bootloader via ApiParameter2\r
140 //\r
141 PeiFspData->FspHobListPtr = (VOID **)GetFspApiParameter2 ();\r
cf1d4549
JY
142\r
143 SetFspMeasurePoint (FSP_PERF_ID_API_FSP_MEMORY_INIT_ENTRY);\r
144\r
145 //\r
146 // Get FSP Header offset\r
147 // It may have multiple FVs, so look into the last one for FSP header\r
148 //\r
630df8c8 149 PeiFspData->FspInfoHeader = (FSP_INFO_HEADER *)(UINTN)AsmGetFspInfoHeader ();\r
cf1d4549
JY
150 SecGetPlatformData (PeiFspData);\r
151\r
152 //\r
153 // Set API calling mode\r
154 //\r
155 SetFspApiCallingIndex (ApiIdx);\r
e37bb20c 156\r
cf1d4549
JY
157 //\r
158 // Set UPD pointer\r
159 //\r
111f2228 160 FspmUpdDataPtr = (VOID *)GetFspApiParameter ();\r
cf1d4549 161 if (FspmUpdDataPtr == NULL) {\r
630df8c8 162 FspmUpdDataPtr = (VOID *)(UINTN)(PeiFspData->FspInfoHeader->ImageBase + PeiFspData->FspInfoHeader->CfgRegionOffset);\r
cf1d4549 163 }\r
111f2228 164\r
cf1d4549
JY
165 SetFspUpdDataPointer (FspmUpdDataPtr);\r
166 SetFspMemoryInitUpdDataPointer (FspmUpdDataPtr);\r
167 SetFspSiliconInitUpdDataPointer (NULL);\r
168\r
f2cdb268
CC
169 //\r
170 // Initialize OnSeparateStack value.\r
171 //\r
172 if (PcdGet8 (PcdFspHeapSizePercentage) != 0) {\r
173 //\r
174 // FSP is running on its own stack and may need switching stack when calling bootloader functions.\r
175 //\r
176 GetFspGlobalDataPointer ()->OnSeparateStack = 1;\r
177 }\r
178\r
cf1d4549
JY
179 //\r
180 // Initialize serial port\r
181 // It might have been done in ProcessLibraryConstructorList(), however,\r
182 // the FSP global data is not initialized at that time. So do it again\r
183 // for safe.\r
184 //\r
185 SerialPortInitialize ();\r
186\r
187 //\r
91cc60ba 188 // Ensure the global data pointer is valid\r
cf1d4549
JY
189 //\r
190 ASSERT (GetFspGlobalDataPointer () == PeiFspData);\r
191\r
192 for (Idx = 0; Idx < 8; Idx++) {\r
193 ImageId[Idx] = PeiFspData->FspInfoHeader->ImageId[Idx];\r
194 }\r
111f2228 195\r
cf1d4549
JY
196 ImageId[Idx] = 0;\r
197\r
111f2228
MK
198 DEBUG ((\r
199 DEBUG_INFO | DEBUG_INIT,\r
200 "\n============= FSP Spec v%d.%d Header Revision v%x (%a v%x.%x.%x.%x) =============\n", \\r
201 (PeiFspData->FspInfoHeader->SpecVersion >> 4) & 0xF, \\r
202 PeiFspData->FspInfoHeader->SpecVersion & 0xF, \\r
203 PeiFspData->FspInfoHeader->HeaderRevision, \\r
204 ImageId, \\r
205 (PeiFspData->FspInfoHeader->ImageRevision >> 24) & 0xFF, \\r
206 (PeiFspData->FspInfoHeader->ImageRevision >> 16) & 0xFF, \\r
7935be0f 207 (PeiFspData->FspInfoHeader->HeaderRevision >= 6) ? \\r
470206ba
KT
208 (((PeiFspData->FspInfoHeader->ImageRevision >> 8) & 0xFF) | (PeiFspData->FspInfoHeader->ExtendedImageRevision & 0xFF00)) : \\r
209 ((PeiFspData->FspInfoHeader->ImageRevision >> 8) & 0xFF), \\r
7935be0f 210 (PeiFspData->FspInfoHeader->HeaderRevision >= 6) ? \\r
470206ba
KT
211 ((PeiFspData->FspInfoHeader->ImageRevision & 0xFF) | ((PeiFspData->FspInfoHeader->ExtendedImageRevision & 0xFF) << 8)) : \\r
212 (PeiFspData->FspInfoHeader->ImageRevision & 0xFF)\r
111f2228 213 ));\r
cf1d4549
JY
214}\r
215\r
216/**\r
217\r
218 Adjust the FSP data pointers after the stack is migrated to memory.\r
219\r
220 @param[in] OffsetGap The offset gap between the old stack and the new stack.\r
221\r
222**/\r
223VOID\r
224FspDataPointerFixUp (\r
ec0b5484 225 IN UINTN OffsetGap\r
cf1d4549
JY
226 )\r
227{\r
228 FSP_GLOBAL_DATA *NewFspData;\r
229\r
111f2228 230 NewFspData = (FSP_GLOBAL_DATA *)((UINTN)GetFspGlobalDataPointer () + (UINTN)OffsetGap);\r
cf1d4549
JY
231 SetFspGlobalDataPointer (NewFspData);\r
232}\r