VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/IntelFsp2Pkg/Library/BaseFspCommonLib/FspCommonLib.c@ 108794

Last change on this file since 108794 was 108794, checked in by vboxsync, 2 weeks ago

Devices/EFI/FirmwareNew: Merge edk2-stable202502 from the vendor branch and make it build for the important platforms, bugref:4643

  • Property svn:eol-style set to native
File size: 10.6 KB
Line 
1/** @file
2
3 Copyright (c) 2014 - 2022, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
5
6**/
7
8#include <PiPei.h>
9#include <Library/BaseLib.h>
10#include <Library/DebugLib.h>
11#include <Library/PcdLib.h>
12#include <FspGlobalData.h>
13#include <FspEas.h>
14#include <Library/FspSwitchStackLib.h>
15
16#pragma pack(1)
17
18typedef struct {
19 UINT16 IdtrLimit;
20 UINT32 IdtrBase;
21 UINT16 Reserved;
22 UINT32 Cr0;
23 UINT32 Cr3;
24 UINT32 Cr4;
25 UINT32 Efer; // lower 32-bit of EFER since only NXE bit (BIT11) need to be restored.
26 UINT32 Registers[8]; // General Purpose Registers: Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx and Eax
27 UINT16 Flags[2];
28 UINT32 FspInfoHeader;
29 UINT32 ApiRet;
30 UINT32 ApiParam[2];
31} CONTEXT_STACK;
32
33typedef struct {
34 UINT64 Idtr[2]; // IDTR Limit - bit0:bi15, IDTR Base - bit16:bit79
35 UINT64 Cr0;
36 UINT64 Cr3;
37 UINT64 Cr4;
38 UINT64 Efer;
39 UINT64 Registers[16]; // General Purpose Registers: RDI, RSI, RBP, RSP, RBX, RDX, RCX, RAX, and R15 to R8
40 UINT32 Flags[2];
41 UINT64 FspInfoHeader;
42 UINT64 ApiParam[2];
43 UINT64 Reserved; // The reserved QWORD is needed for stack alignment in X64.
44 UINT64 ApiRet; // 64bit stack format is different from the 32bit one due to x64 calling convention
45} CONTEXT_STACK_64;
46
47#define CONTEXT_STACK_OFFSET(x) (sizeof(UINTN) == sizeof (UINT32) ? (UINTN)&((CONTEXT_STACK *)(UINTN)0)->x : (UINTN)&((CONTEXT_STACK_64 *)(UINTN)0)->x)
48
49#pragma pack()
50
51/**
52 This function sets the FSP global data pointer.
53
54 @param[in] FspData FSP global data pointer.
55
56**/
57VOID
58EFIAPI
59SetFspGlobalDataPointer (
60 IN FSP_GLOBAL_DATA *FspData
61 )
62{
63 ASSERT (FspData != NULL);
64 *((volatile UINT32 *)(UINTN)PcdGet32 (PcdGlobalDataPointerAddress)) = (UINT32)(UINTN)FspData;
65}
66
67/**
68 This function gets the FSP global data pointer.
69
70**/
71FSP_GLOBAL_DATA *
72EFIAPI
73GetFspGlobalDataPointer (
74 VOID
75 )
76{
77 UINT32 FspDataAddress;
78
79 FspDataAddress = *(UINT32 *)(UINTN)PcdGet32 (PcdGlobalDataPointerAddress);
80 return (FSP_GLOBAL_DATA *)(UINTN)FspDataAddress;
81}
82
83/**
84 This function gets back the FSP API first parameter passed by the bootloader.
85
86 @retval ApiParameter FSP API first parameter passed by the bootloader.
87**/
88UINTN
89EFIAPI
90GetFspApiParameter (
91 VOID
92 )
93{
94 FSP_GLOBAL_DATA *FspData;
95
96 FspData = GetFspGlobalDataPointer ();
97 return *(UINTN *)(FspData->CoreStack + CONTEXT_STACK_OFFSET (ApiParam[0]));
98}
99
100/**
101 This function returns the FSP entry stack pointer from address of the first API parameter.
102
103 @retval FSP entry stack pointer.
104**/
105VOID *
106EFIAPI
107GetFspEntryStack (
108 VOID
109 )
110{
111 FSP_GLOBAL_DATA *FspData;
112
113 FspData = GetFspGlobalDataPointer ();
114 return (VOID *)(FspData->CoreStack + CONTEXT_STACK_OFFSET (ApiParam[0]));
115}
116
117/**
118 This function gets back the FSP API second parameter passed by the bootloader.
119
120 @retval ApiParameter FSP API second parameter passed by the bootloader.
121**/
122UINTN
123EFIAPI
124GetFspApiParameter2 (
125 VOID
126 )
127{
128 FSP_GLOBAL_DATA *FspData;
129
130 FspData = GetFspGlobalDataPointer ();
131 return *(UINTN *)(FspData->CoreStack + CONTEXT_STACK_OFFSET (ApiParam[1]));
132}
133
134/**
135 This function sets the FSP API parameter in the stack.
136
137 @param[in] Value New parameter value.
138
139**/
140VOID
141EFIAPI
142SetFspApiParameter (
143 IN UINT32 Value
144 )
145{
146 FSP_GLOBAL_DATA *FspData;
147
148 FspData = GetFspGlobalDataPointer ();
149 *(UINTN *)(FspData->CoreStack + CONTEXT_STACK_OFFSET (ApiParam)) = Value;
150}
151
152/**
153 This function set the API status code returned to the BootLoader.
154
155 @param[in] ReturnStatus Status code to return.
156
157**/
158VOID
159EFIAPI
160SetFspApiReturnStatus (
161 IN UINTN ReturnStatus
162 )
163{
164 FSP_GLOBAL_DATA *FspData;
165
166 FspData = GetFspGlobalDataPointer ();
167 *(UINTN *)(FspData->CoreStack + CONTEXT_STACK_OFFSET (Registers[7])) = ReturnStatus;
168}
169
170/**
171 This function sets the context switching stack to a new stack frame.
172
173 @param[in] NewStackTop New core stack to be set.
174
175**/
176VOID
177EFIAPI
178SetFspCoreStackPointer (
179 IN VOID *NewStackTop
180 )
181{
182 FSP_GLOBAL_DATA *FspData;
183 UINTN *OldStack;
184 UINTN *NewStack;
185 UINT32 StackContextLen;
186
187 FspData = GetFspGlobalDataPointer ();
188 StackContextLen = sizeof (CONTEXT_STACK) / sizeof (UINTN);
189
190 //
191 // Reserve space for the ContinuationFunc two parameters
192 //
193 OldStack = (UINTN *)FspData->CoreStack;
194 NewStack = (UINTN *)NewStackTop - StackContextLen - 2;
195 FspData->CoreStack = (UINTN)NewStack;
196 while (StackContextLen-- != 0) {
197 *NewStack++ = *OldStack++;
198 }
199}
200
201/**
202 This function sets the platform specific data pointer.
203
204 @param[in] PlatformData FSP platform specific data pointer.
205
206**/
207VOID
208EFIAPI
209SetFspPlatformDataPointer (
210 IN VOID *PlatformData
211 )
212{
213 FSP_GLOBAL_DATA *FspData;
214
215 FspData = GetFspGlobalDataPointer ();
216 FspData->PlatformData.DataPtr = PlatformData;
217}
218
219/**
220 This function gets the platform specific data pointer.
221
222 @param[in] PlatformData FSP platform specific data pointer.
223
224**/
225VOID *
226EFIAPI
227GetFspPlatformDataPointer (
228 VOID
229 )
230{
231 FSP_GLOBAL_DATA *FspData;
232
233 FspData = GetFspGlobalDataPointer ();
234 return FspData->PlatformData.DataPtr;
235}
236
237/**
238 This function sets the UPD data pointer.
239
240 @param[in] UpdDataPtr UPD data pointer.
241**/
242VOID
243EFIAPI
244SetFspUpdDataPointer (
245 IN VOID *UpdDataPtr
246 )
247{
248 FSP_GLOBAL_DATA *FspData;
249
250 //
251 // Get the FSP Global Data Pointer
252 //
253 FspData = GetFspGlobalDataPointer ();
254
255 //
256 // Set the UPD pointer.
257 //
258 FspData->UpdDataPtr = UpdDataPtr;
259}
260
261/**
262 This function gets the UPD data pointer.
263
264 @return UpdDataPtr UPD data pointer.
265**/
266VOID *
267EFIAPI
268GetFspUpdDataPointer (
269 VOID
270 )
271{
272 FSP_GLOBAL_DATA *FspData;
273
274 FspData = GetFspGlobalDataPointer ();
275 return FspData->UpdDataPtr;
276}
277
278/**
279 This function sets the FspMemoryInit UPD data pointer.
280
281 @param[in] MemoryInitUpdPtr FspMemoryInit UPD data pointer.
282**/
283VOID
284EFIAPI
285SetFspMemoryInitUpdDataPointer (
286 IN VOID *MemoryInitUpdPtr
287 )
288{
289 FSP_GLOBAL_DATA *FspData;
290
291 //
292 // Get the FSP Global Data Pointer
293 //
294 FspData = GetFspGlobalDataPointer ();
295
296 //
297 // Set the FspMemoryInit UPD pointer.
298 //
299 FspData->MemoryInitUpdPtr = MemoryInitUpdPtr;
300}
301
302/**
303 This function gets the FspMemoryInit UPD data pointer.
304
305 @return FspMemoryInit UPD data pointer.
306**/
307VOID *
308EFIAPI
309GetFspMemoryInitUpdDataPointer (
310 VOID
311 )
312{
313 FSP_GLOBAL_DATA *FspData;
314
315 FspData = GetFspGlobalDataPointer ();
316 return FspData->MemoryInitUpdPtr;
317}
318
319/**
320 This function sets the FspSiliconInit UPD data pointer.
321
322 @param[in] SiliconInitUpdPtr FspSiliconInit UPD data pointer.
323**/
324VOID
325EFIAPI
326SetFspSiliconInitUpdDataPointer (
327 IN VOID *SiliconInitUpdPtr
328 )
329{
330 FSP_GLOBAL_DATA *FspData;
331
332 //
333 // Get the FSP Global Data Pointer
334 //
335 FspData = GetFspGlobalDataPointer ();
336
337 //
338 // Set the FspSiliconInit UPD data pointer.
339 //
340 FspData->SiliconInitUpdPtr = SiliconInitUpdPtr;
341}
342
343/**
344 This function gets the FspSiliconInit UPD data pointer.
345
346 @return FspSiliconInit UPD data pointer.
347**/
348VOID *
349EFIAPI
350GetFspSiliconInitUpdDataPointer (
351 VOID
352 )
353{
354 FSP_GLOBAL_DATA *FspData;
355
356 FspData = GetFspGlobalDataPointer ();
357 return FspData->SiliconInitUpdPtr;
358}
359
360/**
361 This function sets the FspSmmInit UPD data pointer.
362
363 @param[in] SmmInitUpdPtr FspSmmInit UPD data pointer.
364**/
365VOID
366EFIAPI
367SetFspSmmInitUpdDataPointer (
368 IN VOID *SmmInitUpdPtr
369 )
370{
371 FSP_GLOBAL_DATA *FspData;
372
373 //
374 // Get the FSP Global Data Pointer
375 //
376 FspData = GetFspGlobalDataPointer ();
377
378 //
379 // Set the FspSmmInit UPD data pointer.
380 //
381 FspData->SmmInitUpdPtr = SmmInitUpdPtr;
382}
383
384/**
385 This function gets the FspSmmInit UPD data pointer.
386
387 @return FspSmmInit UPD data pointer.
388**/
389VOID *
390EFIAPI
391GetFspSmmInitUpdDataPointer (
392 VOID
393 )
394{
395 FSP_GLOBAL_DATA *FspData;
396
397 FspData = GetFspGlobalDataPointer ();
398 return FspData->SmmInitUpdPtr;
399}
400
401/**
402 Set FSP measurement point timestamp.
403
404 @param[in] Id Measurement point ID.
405
406 @return performance timestamp if current PerfIdx is valid,
407 else return 0 as invalid performance timestamp
408**/
409UINT64
410EFIAPI
411SetFspMeasurePoint (
412 IN UINT8 Id
413 )
414{
415 FSP_GLOBAL_DATA *FspData;
416
417 //
418 // Bit [55: 0] will be the timestamp
419 // Bit [63:56] will be the ID
420 //
421 FspData = GetFspGlobalDataPointer ();
422 if (FspData->PerfIdx < sizeof (FspData->PerfData) / sizeof (FspData->PerfData[0])) {
423 FspData->PerfData[FspData->PerfIdx] = AsmReadTsc ();
424 ((UINT8 *)(&FspData->PerfData[FspData->PerfIdx]))[7] = Id;
425 return FspData->PerfData[(FspData->PerfIdx)++];
426 }
427
428 return 0;
429}
430
431/**
432 This function gets the FSP info header pointer.
433
434 @retval FspInfoHeader FSP info header pointer
435**/
436FSP_INFO_HEADER *
437EFIAPI
438GetFspInfoHeader (
439 VOID
440 )
441{
442 return GetFspGlobalDataPointer ()->FspInfoHeader;
443}
444
445/**
446 This function sets the FSP info header pointer.
447
448 @param[in] FspInfoHeader FSP info header pointer
449**/
450VOID
451EFIAPI
452SetFspInfoHeader (
453 FSP_INFO_HEADER *FspInfoHeader
454 )
455{
456 GetFspGlobalDataPointer ()->FspInfoHeader = FspInfoHeader;
457}
458
459/**
460 This function gets the FSP info header pointer using the API stack context.
461
462 @retval FspInfoHeader FSP info header pointer using the API stack context
463**/
464FSP_INFO_HEADER *
465EFIAPI
466GetFspInfoHeaderFromApiContext (
467 VOID
468 )
469{
470 FSP_GLOBAL_DATA *FspData;
471
472 FspData = GetFspGlobalDataPointer ();
473 return (FSP_INFO_HEADER *)(*(UINTN *)(FspData->CoreStack + CONTEXT_STACK_OFFSET (FspInfoHeader)));
474}
475
476/**
477 This function gets the CfgRegion data pointer.
478
479 @return CfgRegion data pointer.
480**/
481VOID *
482EFIAPI
483GetFspCfgRegionDataPointer (
484 VOID
485 )
486{
487 FSP_INFO_HEADER *FspInfoHeader;
488
489 FspInfoHeader = GetFspInfoHeader ();
490 return (VOID *)(UINTN)(FspInfoHeader->ImageBase + FspInfoHeader->CfgRegionOffset);
491}
492
493/**
494 This function gets FSP API calling index.
495
496 @retval API calling index
497**/
498UINT8
499EFIAPI
500GetFspApiCallingIndex (
501 VOID
502 )
503{
504 return GetFspGlobalDataPointer ()->ApiIdx;
505}
506
507/**
508 This function sets FSP API calling mode.
509
510 @param[in] Index API calling index
511**/
512VOID
513EFIAPI
514SetFspApiCallingIndex (
515 UINT8 Index
516 )
517{
518 FSP_GLOBAL_DATA *FspData;
519
520 FspData = GetFspGlobalDataPointer ();
521 FspData->ApiIdx = Index;
522}
523
524/**
525 This function gets FSP Phase StatusCode.
526
527 @retval StatusCode
528**/
529UINT32
530EFIAPI
531GetPhaseStatusCode (
532 VOID
533 )
534{
535 return GetFspGlobalDataPointer ()->StatusCode;
536}
537
538/**
539 This function sets FSP Phase StatusCode.
540
541 @param[in] Mode Phase StatusCode
542**/
543VOID
544EFIAPI
545SetPhaseStatusCode (
546 UINT32 StatusCode
547 )
548{
549 FSP_GLOBAL_DATA *FspData;
550
551 FspData = GetFspGlobalDataPointer ();
552 FspData->StatusCode = StatusCode;
553}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette