]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFspPkg/Library/BaseFspCommonLib/FspCommonLib.c
0f48ccf01d93991d4343f8bbc516c7c3dc317fc4
[mirror_edk2.git] / IntelFspPkg / Library / BaseFspCommonLib / FspCommonLib.c
1 /** @file
2
3 Copyright (c) 2014 - 2015, 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 <FspApi.h>
14
15 #pragma pack(1)
16
17 //
18 // Cont Func Parameter 2 +0x3C
19 // Cont Func Parameter 1 +0x38
20 //
21 // API Parameter +0x34
22 // API return address +0x30
23 //
24 // push FspInfoHeader +0x2C
25 // pushfd +0x28
26 // cli
27 // pushad +0x24
28 // sub esp, 8 +0x00
29 // sidt fword ptr [esp]
30 //
31 typedef struct {
32 UINT16 IdtrLimit;
33 UINT32 IdtrBase;
34 UINT16 Reserved;
35 UINT32 Edi;
36 UINT32 Esi;
37 UINT32 Ebp;
38 UINT32 Esp;
39 UINT32 Ebx;
40 UINT32 Edx;
41 UINT32 Ecx;
42 UINT32 Eax;
43 UINT16 Flags[2];
44 UINT32 FspInfoHeader;
45 UINT32 ApiRet;
46 UINT32 ApiParam;
47 } CONTEXT_STACK;
48
49 #define CONTEXT_STACK_OFFSET(x) (UINT32)&((CONTEXT_STACK *)(UINTN)0)->x
50
51 #pragma pack()
52
53 /**
54 This function sets the FSP global data pointer.
55
56 @param[in] FspData Fsp global data pointer.
57
58 **/
59 VOID
60 EFIAPI
61 SetFspGlobalDataPointer (
62 IN FSP_GLOBAL_DATA *FspData
63 )
64 {
65 ASSERT (FspData != NULL);
66 *((volatile UINT32 *)(UINTN)PcdGet32(PcdGlobalDataPointerAddress)) = (UINT32)(UINTN)FspData;
67 }
68
69 /**
70 This function gets the FSP global data pointer.
71
72 **/
73 FSP_GLOBAL_DATA *
74 EFIAPI
75 GetFspGlobalDataPointer (
76 VOID
77 )
78 {
79 FSP_GLOBAL_DATA *FspData;
80
81 FspData = *(FSP_GLOBAL_DATA **)(UINTN)PcdGet32(PcdGlobalDataPointerAddress);
82 return FspData;
83 }
84
85 /**
86 This function gets back the FSP API parameter passed by the bootlaoder.
87
88 @retval ApiParameter FSP API parameter passed by the bootlaoder.
89 **/
90 UINT32
91 EFIAPI
92 GetFspApiParameter (
93 VOID
94 )
95 {
96 FSP_GLOBAL_DATA *FspData;
97
98 FspData = GetFspGlobalDataPointer ();
99 return *(UINT32 *)(UINTN)(FspData->CoreStack + CONTEXT_STACK_OFFSET(ApiParam));
100 }
101
102 /**
103 This function sets the FSP API parameter in the stack.
104
105 @param[in] Value New parameter value.
106
107 **/
108 VOID
109 EFIAPI
110 SetFspApiParameter (
111 IN UINT32 Value
112 )
113 {
114 FSP_GLOBAL_DATA *FspData;
115
116 FspData = GetFspGlobalDataPointer ();
117 *(UINT32 *)(UINTN)(FspData->CoreStack + CONTEXT_STACK_OFFSET(ApiParam)) = Value;
118 }
119
120 /**
121 This function sets the FSP continuation function parameters in the stack.
122
123 @param[in] Value New parameter value to set.
124 @param[in] Index Parameter index.
125 **/
126 VOID
127 EFIAPI
128 SetFspContinuationFuncParameter (
129 IN UINT32 Value,
130 IN UINT32 Index
131 )
132 {
133 FSP_GLOBAL_DATA *FspData;
134
135 FspData = GetFspGlobalDataPointer ();
136 *(UINT32 *)(UINTN)(FspData->CoreStack + CONTEXT_STACK_OFFSET(ApiParam) + (Index + 1) * sizeof(UINT32)) = Value;
137 }
138
139
140 /**
141 This function changes the BootLoader return address in stack.
142
143 @param[in] ReturnAddress Address to return.
144
145 **/
146 VOID
147 EFIAPI
148 SetFspApiReturnAddress (
149 IN UINT32 ReturnAddress
150 )
151 {
152 FSP_GLOBAL_DATA *FspData;
153
154 FspData = GetFspGlobalDataPointer ();
155 *(UINT32 *)(UINTN)(FspData->CoreStack + CONTEXT_STACK_OFFSET(ApiRet)) = ReturnAddress;
156 }
157
158 /**
159 This function set the API status code returned to the BootLoader.
160
161 @param[in] ReturnStatus Status code to return.
162
163 **/
164 VOID
165 EFIAPI
166 SetFspApiReturnStatus (
167 IN UINT32 ReturnStatus
168 )
169 {
170 FSP_GLOBAL_DATA *FspData;
171
172 FspData = GetFspGlobalDataPointer ();
173 *(UINT32 *)(UINTN)(FspData->CoreStack + CONTEXT_STACK_OFFSET(Eax)) = ReturnStatus;
174 }
175
176 /**
177 This function sets the context switching stack to a new stack frame.
178
179 @param[in] NewStackTop New core stack to be set.
180
181 **/
182 VOID
183 EFIAPI
184 SetFspCoreStackPointer (
185 IN VOID *NewStackTop
186 )
187 {
188 FSP_GLOBAL_DATA *FspData;
189 UINT32 *OldStack;
190 UINT32 *NewStack;
191 UINT32 StackContextLen;
192
193 FspData = GetFspGlobalDataPointer ();
194 StackContextLen = sizeof(CONTEXT_STACK) / sizeof(UINT32);
195
196 //
197 // Reserve space for the ContinuationFunc two parameters
198 //
199 OldStack = (UINT32 *)FspData->CoreStack;
200 NewStack = (UINT32 *)NewStackTop - StackContextLen - 2;
201 FspData->CoreStack = (UINT32)NewStack;
202 while (StackContextLen-- != 0) {
203 *NewStack++ = *OldStack++;
204 }
205 }
206
207 /**
208 This function sets the platform specific data pointer.
209
210 @param[in] PlatformData Fsp platform specific data pointer.
211
212 **/
213 VOID
214 EFIAPI
215 SetFspPlatformDataPointer (
216 IN VOID *PlatformData
217 )
218 {
219 FSP_GLOBAL_DATA *FspData;
220
221 FspData = GetFspGlobalDataPointer ();
222 FspData->PlatformData.DataPtr = PlatformData;
223 }
224
225
226 /**
227 This function gets the platform specific data pointer.
228
229 @param[in] PlatformData Fsp platform specific data pointer.
230
231 **/
232 VOID *
233 EFIAPI
234 GetFspPlatformDataPointer (
235 VOID
236 )
237 {
238 FSP_GLOBAL_DATA *FspData;
239
240 FspData = GetFspGlobalDataPointer ();
241 return FspData->PlatformData.DataPtr;
242 }
243
244
245 /**
246 This function sets the UPD data pointer.
247
248 @param[in] UpdDataRgnPtr UPD data pointer.
249 **/
250 VOID
251 EFIAPI
252 SetFspUpdDataPointer (
253 IN VOID *UpdDataRgnPtr
254 )
255 {
256 FSP_GLOBAL_DATA *FspData;
257
258 //
259 // Get the Fsp Global Data Pointer
260 //
261 FspData = GetFspGlobalDataPointer ();
262
263 //
264 // Set the UPD pointer.
265 //
266 FspData->UpdDataRgnPtr = UpdDataRgnPtr;
267 }
268
269 /**
270 This function gets the UPD data pointer.
271
272 @return UpdDataRgnPtr UPD data pointer.
273 **/
274 VOID *
275 EFIAPI
276 GetFspUpdDataPointer (
277 VOID
278 )
279 {
280 FSP_GLOBAL_DATA *FspData;
281
282 FspData = GetFspGlobalDataPointer ();
283 return FspData->UpdDataRgnPtr;
284 }
285
286
287 /**
288 This function sets the memory init UPD data pointer.
289
290 @param[in] MemoryInitUpdPtr memory init UPD data pointer.
291 **/
292 VOID
293 EFIAPI
294 SetFspMemoryInitUpdDataPointer (
295 IN VOID *MemoryInitUpdPtr
296 )
297 {
298 FSP_GLOBAL_DATA *FspData;
299
300 //
301 // Get the Fsp Global Data Pointer
302 //
303 FspData = GetFspGlobalDataPointer ();
304
305 //
306 // Set the memory init UPD pointer.
307 //
308 FspData->MemoryInitUpdPtr = MemoryInitUpdPtr;
309 }
310
311 /**
312 This function gets the memory init UPD data pointer.
313
314 @return memory init UPD data pointer.
315 **/
316 VOID *
317 EFIAPI
318 GetFspMemoryInitUpdDataPointer (
319 VOID
320 )
321 {
322 FSP_GLOBAL_DATA *FspData;
323
324 FspData = GetFspGlobalDataPointer ();
325 return FspData->MemoryInitUpdPtr;
326 }
327
328
329 /**
330 This function sets the silicon init UPD data pointer.
331
332 @param[in] SiliconInitUpdPtr silicon init UPD data pointer.
333 **/
334 VOID
335 EFIAPI
336 SetFspSiliconInitUpdDataPointer (
337 IN VOID *SiliconInitUpdPtr
338 )
339 {
340 FSP_GLOBAL_DATA *FspData;
341
342 //
343 // Get the Fsp Global Data Pointer
344 //
345 FspData = GetFspGlobalDataPointer ();
346
347 //
348 // Set the silicon init UPD data pointer.
349 //
350 FspData->SiliconInitUpdPtr = SiliconInitUpdPtr;
351 }
352
353 /**
354 This function gets the silicon init UPD data pointer.
355
356 @return silicon init UPD data pointer.
357 **/
358 VOID *
359 EFIAPI
360 GetFspSiliconInitUpdDataPointer (
361 VOID
362 )
363 {
364 FSP_GLOBAL_DATA *FspData;
365
366 FspData = GetFspGlobalDataPointer ();
367 return FspData->SiliconInitUpdPtr;
368 }
369
370
371 /**
372 Set FSP measurement point timestamp.
373
374 @param[in] Id Measurement point ID.
375
376 @return performance timestamp.
377 **/
378 UINT64
379 EFIAPI
380 SetFspMeasurePoint (
381 IN UINT8 Id
382 )
383 {
384 FSP_GLOBAL_DATA *FspData;
385
386 //
387 // Bit [55: 0] will be the timestamp
388 // Bit [63:56] will be the ID
389 //
390 FspData = GetFspGlobalDataPointer ();
391 if (FspData->PerfIdx < sizeof(FspData->PerfData) / sizeof(FspData->PerfData[0])) {
392 FspData->PerfData[FspData->PerfIdx] = AsmReadTsc ();
393 ((UINT8 *)(&FspData->PerfData[FspData->PerfIdx]))[7] = Id;
394 }
395
396 return FspData->PerfData[(FspData->PerfIdx)++];
397 }
398
399 /**
400 This function gets the FSP info header pointer.
401
402 @retval FspInfoHeader FSP info header pointer
403 **/
404 FSP_INFO_HEADER *
405 EFIAPI
406 GetFspInfoHeader (
407 VOID
408 )
409 {
410 return GetFspGlobalDataPointer()->FspInfoHeader;
411 }
412
413 /**
414 This function gets the FSP info header pointer using the API stack context.
415
416 @retval FspInfoHeader FSP info header pointer using the API stack context
417 **/
418 FSP_INFO_HEADER *
419 EFIAPI
420 GetFspInfoHeaderFromApiContext (
421 VOID
422 )
423 {
424 FSP_GLOBAL_DATA *FspData;
425
426 FspData = GetFspGlobalDataPointer ();
427 return (FSP_INFO_HEADER *)(*(UINT32 *)(UINTN)(FspData->CoreStack + CONTEXT_STACK_OFFSET(FspInfoHeader)));
428 }
429
430 /**
431 This function gets the VPD data pointer.
432
433 @return VpdDataRgnPtr VPD data pointer.
434 **/
435 VOID *
436 EFIAPI
437 GetFspVpdDataPointer (
438 VOID
439 )
440 {
441 FSP_INFO_HEADER *FspInfoHeader;
442
443 FspInfoHeader = GetFspInfoHeader ();
444 return (VOID *)(FspInfoHeader->ImageBase + FspInfoHeader->CfgRegionOffset);
445 }
446
447 /**
448 This function gets FSP API calling mode.
449
450 @retval API calling mode
451 **/
452 UINT8
453 EFIAPI
454 GetFspApiCallingMode (
455 VOID
456 )
457 {
458 return GetFspGlobalDataPointer()->ApiMode;
459 }
460
461 /**
462 This function sets FSP API calling mode.
463
464 @param[in] Mode API calling mode
465 **/
466 VOID
467 EFIAPI
468 SetFspApiCallingMode (
469 UINT8 Mode
470 )
471 {
472 FSP_GLOBAL_DATA *FspData;
473
474 FspData = GetFspGlobalDataPointer ();
475 FspData->ApiMode = Mode;
476 }
477