]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
Revert "UefiCpuPkg: Has APs in 64 bit long-mode before booting to OS."
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / DxeMpLib.c
CommitLineData
3e8ad6bd
JF
1/** @file\r
2 MP initialize support functions for DXE phase.\r
3\r
7bda8c64 4 Copyright (c) 2016 - 2022, Intel Corporation. All rights reserved.<BR>\r
0acd8697 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
3e8ad6bd
JF
6\r
7**/\r
8\r
9#include "MpLib.h"\r
96378861
JF
10\r
11#include <Library/UefiLib.h>\r
12#include <Library/UefiBootServicesTableLib.h>\r
43c9fdcc 13#include <Library/DebugAgentLib.h>\r
15720a6c 14#include <Library/DxeServicesTableLib.h>\r
a89f558d 15#include <Library/CcExitLib.h>\r
7b7508ad
TL
16#include <Register/Amd/Fam17Msr.h>\r
17#include <Register/Amd/Ghcb.h>\r
96378861 18\r
b6e45716
JF
19#include <Protocol/Timer.h>\r
20\r
053e878b 21#define AP_SAFE_STACK_SIZE 128\r
96378861 22\r
053e878b
MK
23CPU_MP_DATA *mCpuMpData = NULL;\r
24EFI_EVENT mCheckAllApsEvent = NULL;\r
25EFI_EVENT mMpInitExitBootServicesEvent = NULL;\r
26EFI_EVENT mLegacyBootEvent = NULL;\r
27volatile BOOLEAN mStopCheckAllApsStatus = TRUE;\r
28VOID *mReservedApLoopFunc = NULL;\r
29UINTN mReservedTopOfApStack;\r
30volatile UINT32 mNumberToFinish = 0;\r
93ca4c0f 31\r
dbc22a17
TL
32//\r
33// Begin wakeup buffer allocation below 0x88000\r
34//\r
053e878b 35STATIC EFI_PHYSICAL_ADDRESS mSevEsDxeWakeupBuffer = 0x88000;\r
dbc22a17 36\r
43c9fdcc
JF
37/**\r
38 Enable Debug Agent to support source debugging on AP function.\r
39\r
40**/\r
41VOID\r
42EnableDebugAgent (\r
43 VOID\r
44 )\r
45{\r
46 //\r
47 // Initialize Debug Agent to support source level debug in DXE phase\r
48 //\r
49 InitializeDebugAgent (DEBUG_AGENT_INIT_DXE_AP, NULL, NULL);\r
50}\r
51\r
93ca4c0f
JF
52/**\r
53 Get the pointer to CPU MP Data structure.\r
54\r
55 @return The pointer to CPU MP Data structure.\r
56**/\r
57CPU_MP_DATA *\r
58GetCpuMpData (\r
59 VOID\r
60 )\r
61{\r
62 ASSERT (mCpuMpData != NULL);\r
63 return mCpuMpData;\r
64}\r
65\r
66/**\r
67 Save the pointer to CPU MP Data structure.\r
68\r
69 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.\r
70**/\r
71VOID\r
72SaveCpuMpData (\r
053e878b 73 IN CPU_MP_DATA *CpuMpData\r
93ca4c0f
JF
74 )\r
75{\r
76 mCpuMpData = CpuMpData;\r
77}\r
78\r
96378861 79/**\r
e4ff6349 80 Get available system memory below 0x88000 by specified size.\r
ed66e0e3 81\r
a6b3d753 82 @param[in] WakeupBufferSize Wakeup buffer size required\r
3ed4e502 83\r
a6b3d753
SZ
84 @retval other Return wakeup buffer address below 1MB.\r
85 @retval -1 Cannot find free memory below 1MB.\r
ed66e0e3 86**/\r
a6b3d753
SZ
87UINTN\r
88GetWakeupBuffer (\r
053e878b 89 IN UINTN WakeupBufferSize\r
ed66e0e3
JF
90 )\r
91{\r
053e878b
MK
92 EFI_STATUS Status;\r
93 EFI_PHYSICAL_ADDRESS StartAddress;\r
94 EFI_MEMORY_TYPE MemoryType;\r
20da7ca4 95\r
06544455
TL
96 if (ConfidentialComputingGuestHas (CCAttrAmdSevEs) &&\r
97 !ConfidentialComputingGuestHas (CCAttrAmdSevSnp))\r
98 {\r
99 //\r
100 // An SEV-ES-only guest requires the memory to be reserved. SEV-SNP, which\r
101 // is also considered SEV-ES, uses a different AP startup method, though,\r
102 // which does not have the same requirement.\r
103 //\r
20da7ca4
TL
104 MemoryType = EfiReservedMemoryType;\r
105 } else {\r
106 MemoryType = EfiBootServicesData;\r
107 }\r
a6b3d753 108\r
e4ff6349
ED
109 //\r
110 // Try to allocate buffer below 1M for waking vector.\r
111 // LegacyBios driver only reports warning when page allocation in range\r
112 // [0x60000, 0x88000) fails.\r
113 // This library is consumed by CpuDxe driver to produce CPU Arch protocol.\r
114 // LagacyBios driver depends on CPU Arch protocol which guarantees below\r
115 // allocation runs earlier than LegacyBios driver.\r
116 //\r
b95908e0 117 if (ConfidentialComputingGuestHas (CCAttrAmdSevEs)) {\r
dbc22a17
TL
118 //\r
119 // SEV-ES Wakeup buffer should be under 0x88000 and under any previous one\r
120 //\r
121 StartAddress = mSevEsDxeWakeupBuffer;\r
122 } else {\r
123 StartAddress = 0x88000;\r
124 }\r
053e878b 125\r
a6b3d753
SZ
126 Status = gBS->AllocatePages (\r
127 AllocateMaxAddress,\r
20da7ca4 128 MemoryType,\r
a6b3d753
SZ
129 EFI_SIZE_TO_PAGES (WakeupBufferSize),\r
130 &StartAddress\r
131 );\r
132 ASSERT_EFI_ERROR (Status);\r
e4ff6349 133 if (EFI_ERROR (Status)) {\r
053e878b 134 StartAddress = (EFI_PHYSICAL_ADDRESS)-1;\r
b95908e0 135 } else if (ConfidentialComputingGuestHas (CCAttrAmdSevEs)) {\r
dbc22a17
TL
136 //\r
137 // Next SEV-ES wakeup buffer allocation must be below this allocation\r
138 //\r
139 mSevEsDxeWakeupBuffer = StartAddress;\r
3ed4e502 140 }\r
e4ff6349 141\r
053e878b
MK
142 DEBUG ((\r
143 DEBUG_INFO,\r
144 "WakeupBufferStart = %x, WakeupBufferSize = %x\n",\r
145 (UINTN)StartAddress,\r
146 WakeupBufferSize\r
147 ));\r
e4ff6349 148\r
053e878b 149 return (UINTN)StartAddress;\r
ed66e0e3
JF
150}\r
151\r
f32bfe6d
JW
152/**\r
153 Get available EfiBootServicesCode memory below 4GB by specified size.\r
154\r
155 This buffer is required to safely transfer AP from real address mode to\r
156 protected mode or long mode, due to the fact that the buffer returned by\r
157 GetWakeupBuffer() may be marked as non-executable.\r
158\r
159 @param[in] BufferSize Wakeup transition buffer size.\r
160\r
161 @retval other Return wakeup transition buffer address below 4GB.\r
162 @retval 0 Cannot find free memory below 4GB.\r
163**/\r
164UINTN\r
54aeed7e 165AllocateCodeBuffer (\r
053e878b 166 IN UINTN BufferSize\r
f32bfe6d
JW
167 )\r
168{\r
053e878b
MK
169 EFI_STATUS Status;\r
170 EFI_PHYSICAL_ADDRESS StartAddress;\r
f32bfe6d
JW
171\r
172 StartAddress = BASE_4GB - 1;\r
053e878b
MK
173 Status = gBS->AllocatePages (\r
174 AllocateMaxAddress,\r
175 EfiBootServicesCode,\r
176 EFI_SIZE_TO_PAGES (BufferSize),\r
177 &StartAddress\r
178 );\r
f32bfe6d
JW
179 if (EFI_ERROR (Status)) {\r
180 StartAddress = 0;\r
181 }\r
182\r
183 return (UINTN)StartAddress;\r
184}\r
185\r
7b7508ad
TL
186/**\r
187 Return the address of the SEV-ES AP jump table.\r
188\r
189 This buffer is required in order for an SEV-ES guest to transition from\r
190 UEFI into an OS.\r
191\r
192 @return Return SEV-ES AP jump table buffer\r
193**/\r
194UINTN\r
195GetSevEsAPMemory (\r
196 VOID\r
197 )\r
198{\r
20da7ca4
TL
199 EFI_STATUS Status;\r
200 EFI_PHYSICAL_ADDRESS StartAddress;\r
201 MSR_SEV_ES_GHCB_REGISTER Msr;\r
202 GHCB *Ghcb;\r
1b0db1ec 203 BOOLEAN InterruptState;\r
7b7508ad
TL
204\r
205 //\r
206 // Allocate 1 page for AP jump table page\r
207 //\r
208 StartAddress = BASE_4GB - 1;\r
053e878b
MK
209 Status = gBS->AllocatePages (\r
210 AllocateMaxAddress,\r
211 EfiReservedMemoryType,\r
212 1,\r
213 &StartAddress\r
214 );\r
7b7508ad
TL
215 ASSERT_EFI_ERROR (Status);\r
216\r
053e878b 217 DEBUG ((DEBUG_INFO, "Dxe: SevEsAPMemory = %lx\n", (UINTN)StartAddress));\r
7b7508ad 218\r
20da7ca4
TL
219 //\r
220 // Save the SevEsAPMemory as the AP jump table.\r
221 //\r
222 Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB);\r
053e878b 223 Ghcb = Msr.Ghcb;\r
20da7ca4 224\r
765ba5bf
MX
225 CcExitVmgInit (Ghcb, &InterruptState);\r
226 CcExitVmgExit (Ghcb, SVM_EXIT_AP_JUMP_TABLE, 0, (UINT64)(UINTN)StartAddress);\r
227 CcExitVmgDone (Ghcb, InterruptState);\r
20da7ca4 228\r
053e878b 229 return (UINTN)StartAddress;\r
7b7508ad
TL
230}\r
231\r
96378861
JF
232/**\r
233 Checks APs status and updates APs status if needed.\r
234\r
235**/\r
236VOID\r
237CheckAndUpdateApsStatus (\r
238 VOID\r
239 )\r
240{\r
053e878b
MK
241 UINTN ProcessorNumber;\r
242 EFI_STATUS Status;\r
243 CPU_MP_DATA *CpuMpData;\r
08085f08
JF
244\r
245 CpuMpData = GetCpuMpData ();\r
246\r
247 //\r
248 // First, check whether pending StartupAllAPs() exists.\r
249 //\r
250 if (CpuMpData->WaitEvent != NULL) {\r
08085f08
JF
251 Status = CheckAllAPs ();\r
252 //\r
253 // If all APs finish for StartupAllAPs(), signal the WaitEvent for it.\r
254 //\r
255 if (Status != EFI_NOT_READY) {\r
053e878b 256 Status = gBS->SignalEvent (CpuMpData->WaitEvent);\r
08085f08
JF
257 CpuMpData->WaitEvent = NULL;\r
258 }\r
259 }\r
260\r
261 //\r
262 // Second, check whether pending StartupThisAPs() callings exist.\r
263 //\r
264 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {\r
08085f08
JF
265 if (CpuMpData->CpuData[ProcessorNumber].WaitEvent == NULL) {\r
266 continue;\r
267 }\r
268\r
269 Status = CheckThisAP (ProcessorNumber);\r
270\r
271 if (Status != EFI_NOT_READY) {\r
272 gBS->SignalEvent (CpuMpData->CpuData[ProcessorNumber].WaitEvent);\r
053e878b 273 CpuMpData->CpuData[ProcessorNumber].WaitEvent = NULL;\r
08085f08
JF
274 }\r
275 }\r
96378861
JF
276}\r
277\r
278/**\r
279 Checks APs' status periodically.\r
280\r
438f1766 281 This function is triggered by timer periodically to check the\r
96378861
JF
282 state of APs for StartupAllAPs() and StartupThisAP() executed\r
283 in non-blocking mode.\r
284\r
285 @param[in] Event Event triggered.\r
286 @param[in] Context Parameter passed with the event.\r
287\r
288**/\r
289VOID\r
290EFIAPI\r
291CheckApsStatus (\r
053e878b
MK
292 IN EFI_EVENT Event,\r
293 IN VOID *Context\r
96378861
JF
294 )\r
295{\r
296 //\r
297 // If CheckApsStatus() is not stopped, otherwise return immediately.\r
298 //\r
299 if (!mStopCheckAllApsStatus) {\r
300 CheckAndUpdateApsStatus ();\r
301 }\r
302}\r
ed66e0e3 303\r
7b7508ad
TL
304/**\r
305 Get Protected mode code segment with 16-bit default addressing\r
306 from current GDT table.\r
307\r
308 @return Protected mode 16-bit code segment value.\r
309**/\r
310UINT16\r
311GetProtectedMode16CS (\r
312 VOID\r
313 )\r
314{\r
315 IA32_DESCRIPTOR GdtrDesc;\r
316 IA32_SEGMENT_DESCRIPTOR *GdtEntry;\r
317 UINTN GdtEntryCount;\r
318 UINT16 Index;\r
319\r
053e878b 320 Index = (UINT16)-1;\r
7b7508ad
TL
321 AsmReadGdtr (&GdtrDesc);\r
322 GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);\r
053e878b 323 GdtEntry = (IA32_SEGMENT_DESCRIPTOR *)GdtrDesc.Base;\r
7b7508ad
TL
324 for (Index = 0; Index < GdtEntryCount; Index++) {\r
325 if (GdtEntry->Bits.L == 0) {\r
053e878b 326 if ((GdtEntry->Bits.Type > 8) && (GdtEntry->Bits.DB == 0)) {\r
7b7508ad
TL
327 break;\r
328 }\r
329 }\r
053e878b 330\r
7b7508ad
TL
331 GdtEntry++;\r
332 }\r
053e878b 333\r
7b7508ad
TL
334 ASSERT (Index != GdtEntryCount);\r
335 return Index * 8;\r
336}\r
337\r
4d3314f6
JF
338/**\r
339 Get Protected mode code segment from current GDT table.\r
340\r
b31c1ad1 341 @return Protected mode code segment value.\r
4d3314f6
JF
342**/\r
343UINT16\r
344GetProtectedModeCS (\r
345 VOID\r
346 )\r
347{\r
348 IA32_DESCRIPTOR GdtrDesc;\r
349 IA32_SEGMENT_DESCRIPTOR *GdtEntry;\r
350 UINTN GdtEntryCount;\r
351 UINT16 Index;\r
352\r
4d3314f6
JF
353 AsmReadGdtr (&GdtrDesc);\r
354 GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);\r
053e878b 355 GdtEntry = (IA32_SEGMENT_DESCRIPTOR *)GdtrDesc.Base;\r
4d3314f6
JF
356 for (Index = 0; Index < GdtEntryCount; Index++) {\r
357 if (GdtEntry->Bits.L == 0) {\r
053e878b 358 if ((GdtEntry->Bits.Type > 8) && (GdtEntry->Bits.DB == 1)) {\r
4d3314f6
JF
359 break;\r
360 }\r
361 }\r
053e878b 362\r
4d3314f6
JF
363 GdtEntry++;\r
364 }\r
053e878b 365\r
37fba7c2 366 ASSERT (Index != GdtEntryCount);\r
4d3314f6
JF
367 return Index * 8;\r
368}\r
369\r
370/**\r
371 Do sync on APs.\r
372\r
373 @param[in, out] Buffer Pointer to private data buffer.\r
374**/\r
375VOID\r
376EFIAPI\r
377RelocateApLoop (\r
378 IN OUT VOID *Buffer\r
379 )\r
380{\r
7bda8c64
YX
381 CPU_MP_DATA *CpuMpData;\r
382 BOOLEAN MwaitSupport;\r
383 ASM_RELOCATE_AP_LOOP AsmRelocateApLoopFunc;\r
384 ASM_RELOCATE_AP_LOOP_AMD AsmRelocateApLoopFuncAmd;\r
385 UINTN ProcessorNumber;\r
386 UINTN StackStart;\r
4d3314f6 387\r
7367cc6c 388 MpInitLibWhoAmI (&ProcessorNumber);\r
4d3314f6
JF
389 CpuMpData = GetCpuMpData ();\r
390 MwaitSupport = IsMwaitSupport ();\r
7bda8c64
YX
391 if (StandardSignatureIsAuthenticAMD ()) {\r
392 StackStart = CpuMpData->UseSevEsAPMethod ? CpuMpData->SevEsAPResetStackStart : mReservedTopOfApStack;\r
393 AsmRelocateApLoopFuncAmd = (ASM_RELOCATE_AP_LOOP_AMD)(UINTN)mReservedApLoopFunc;\r
394 AsmRelocateApLoopFuncAmd (\r
395 MwaitSupport,\r
396 CpuMpData->ApTargetCState,\r
397 CpuMpData->PmCodeSegment,\r
398 StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE,\r
399 (UINTN)&mNumberToFinish,\r
400 CpuMpData->Pm16CodeSegment,\r
401 CpuMpData->SevEsAPBuffer,\r
402 CpuMpData->WakeupBuffer\r
403 );\r
20da7ca4 404 } else {\r
7bda8c64
YX
405 StackStart = mReservedTopOfApStack;\r
406 AsmRelocateApLoopFunc = (ASM_RELOCATE_AP_LOOP)(UINTN)mReservedApLoopFunc;\r
407 AsmRelocateApLoopFunc (\r
408 MwaitSupport,\r
409 CpuMpData->ApTargetCState,\r
cbcf0cd6 410 CpuMpData->PmCodeSegment,\r
7bda8c64
YX
411 StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE,\r
412 (UINTN)&mNumberToFinish,\r
cbcf0cd6
YX
413 CpuMpData->Pm16CodeSegment,\r
414 CpuMpData->SevEsAPBuffer,\r
415 CpuMpData->WakeupBuffer\r
7bda8c64 416 );\r
20da7ca4 417 }\r
053e878b 418\r
4d3314f6
JF
419 //\r
420 // It should never reach here\r
421 //\r
422 ASSERT (FALSE);\r
423}\r
424\r
425/**\r
426 Callback function for ExitBootServices.\r
427\r
428 @param[in] Event Event whose notification function is being invoked.\r
429 @param[in] Context The pointer to the notification function's context,\r
430 which is implementation-dependent.\r
431\r
432**/\r
433VOID\r
434EFIAPI\r
86af2eb8 435MpInitChangeApLoopCallback (\r
053e878b
MK
436 IN EFI_EVENT Event,\r
437 IN VOID *Context\r
4d3314f6
JF
438 )\r
439{\r
053e878b 440 CPU_MP_DATA *CpuMpData;\r
5183fb37 441\r
053e878b
MK
442 CpuMpData = GetCpuMpData ();\r
443 CpuMpData->PmCodeSegment = GetProtectedModeCS ();\r
7b7508ad 444 CpuMpData->Pm16CodeSegment = GetProtectedMode16CS ();\r
053e878b
MK
445 CpuMpData->ApLoopMode = PcdGet8 (PcdCpuApLoopMode);\r
446 mNumberToFinish = CpuMpData->CpuCount - 1;\r
cf4e79e4 447 WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, NULL, TRUE);\r
9f91cb01
JF
448 while (mNumberToFinish > 0) {\r
449 CpuPause ();\r
450 }\r
20da7ca4 451\r
06544455 452 if (CpuMpData->UseSevEsAPMethod && (CpuMpData->WakeupBuffer != (UINTN)-1)) {\r
20da7ca4
TL
453 //\r
454 // There are APs present. Re-use reserved memory area below 1MB from\r
455 // WakeupBuffer as the area to be used for transitioning to 16-bit mode\r
456 // in support of booting of the AP by an OS.\r
457 //\r
458 CopyMem (\r
053e878b
MK
459 (VOID *)CpuMpData->WakeupBuffer,\r
460 (VOID *)(CpuMpData->AddressMap.RendezvousFunnelAddress +\r
461 CpuMpData->AddressMap.SwitchToRealPM16ModeOffset),\r
20da7ca4
TL
462 CpuMpData->AddressMap.SwitchToRealPM16ModeSize\r
463 );\r
464 }\r
465\r
86af2eb8 466 DEBUG ((DEBUG_INFO, "%a() done!\n", __FUNCTION__));\r
4d3314f6
JF
467}\r
468\r
93ca4c0f
JF
469/**\r
470 Initialize global data for MP support.\r
471\r
472 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
473**/\r
474VOID\r
475InitMpGlobalData (\r
053e878b 476 IN CPU_MP_DATA *CpuMpData\r
93ca4c0f
JF
477 )\r
478{\r
053e878b 479 EFI_STATUS Status;\r
cbcf0cd6 480 EFI_PHYSICAL_ADDRESS Address;\r
053e878b
MK
481 UINTN ApSafeBufferSize;\r
482 UINTN Index;\r
483 EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc;\r
484 UINTN StackBase;\r
485 CPU_INFO_IN_HOB *CpuInfoInHob;\r
96378861 486\r
93ca4c0f
JF
487 SaveCpuMpData (CpuMpData);\r
488\r
14e8137c
JF
489 if (CpuMpData->CpuCount == 1) {\r
490 //\r
491 // If only BSP exists, return\r
492 //\r
493 return;\r
494 }\r
495\r
15720a6c
JW
496 if (PcdGetBool (PcdCpuStackGuard)) {\r
497 //\r
498 // One extra page at the bottom of the stack is needed for Guard page.\r
499 //\r
500 if (CpuMpData->CpuApStackSize <= EFI_PAGE_SIZE) {\r
501 DEBUG ((DEBUG_ERROR, "PcdCpuApStackSize is not big enough for Stack Guard!\n"));\r
502 ASSERT (FALSE);\r
503 }\r
504\r
52315261
JW
505 //\r
506 // DXE will reuse stack allocated for APs at PEI phase if it's available.\r
507 // Let's check it here.\r
508 //\r
509 // Note: BSP's stack guard is set at DxeIpl phase. But for the sake of\r
510 // BSP/AP exchange, stack guard for ApTopOfStack of cpu 0 will still be\r
511 // set here.\r
512 //\r
513 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;\r
15720a6c 514 for (Index = 0; Index < CpuMpData->CpuCount; ++Index) {\r
053e878b 515 if ((CpuInfoInHob != NULL) && (CpuInfoInHob[Index].ApTopOfStack != 0)) {\r
20737c2f 516 StackBase = (UINTN)CpuInfoInHob[Index].ApTopOfStack - CpuMpData->CpuApStackSize;\r
52315261
JW
517 } else {\r
518 StackBase = CpuMpData->Buffer + Index * CpuMpData->CpuApStackSize;\r
519 }\r
15720a6c
JW
520\r
521 Status = gDS->GetMemorySpaceDescriptor (StackBase, &MemDesc);\r
522 ASSERT_EFI_ERROR (Status);\r
523\r
524 Status = gDS->SetMemorySpaceAttributes (\r
525 StackBase,\r
526 EFI_PAGES_TO_SIZE (1),\r
527 MemDesc.Attributes | EFI_MEMORY_RP\r
528 );\r
529 ASSERT_EFI_ERROR (Status);\r
52315261 530\r
053e878b
MK
531 DEBUG ((\r
532 DEBUG_INFO,\r
533 "Stack Guard set at %lx [cpu%lu]!\n",\r
534 (UINT64)StackBase,\r
535 (UINT64)Index\r
536 ));\r
15720a6c
JW
537 }\r
538 }\r
539\r
5183fb37 540 //\r
ffd6b0b1
JF
541 // Avoid APs access invalid buffer data which allocated by BootServices,\r
542 // so we will allocate reserved data for AP loop code. We also need to\r
543 // allocate this buffer below 4GB due to APs may be transferred to 32bit\r
544 // protected mode on long mode DXE.\r
5183fb37
JF
545 // Allocating it in advance since memory services are not available in\r
546 // Exit Boot Services callback function.\r
547 //\r
053e878b
MK
548 ApSafeBufferSize = EFI_PAGES_TO_SIZE (\r
549 EFI_SIZE_TO_PAGES (\r
cbcf0cd6 550 CpuMpData->AddressMap.RelocateApLoopFuncSize\r
053e878b
MK
551 )\r
552 );\r
cbcf0cd6
YX
553 Address = BASE_4GB - 1;\r
554 Status = gBS->AllocatePages (\r
555 AllocateMaxAddress,\r
556 EfiReservedMemoryType,\r
557 EFI_SIZE_TO_PAGES (ApSafeBufferSize),\r
558 &Address\r
559 );\r
560 ASSERT_EFI_ERROR (Status);\r
bc2288f5 561\r
cbcf0cd6
YX
562 mReservedApLoopFunc = (VOID *)(UINTN)Address;\r
563 ASSERT (mReservedApLoopFunc != NULL);\r
73ccde8f 564\r
cbcf0cd6
YX
565 //\r
566 // Make sure that the buffer memory is executable if NX protection is enabled\r
567 // for EfiReservedMemoryType.\r
568 //\r
569 // TODO: Check EFI_MEMORY_XP bit set or not once it's available in DXE GCD\r
570 // service.\r
571 //\r
572 Status = gDS->GetMemorySpaceDescriptor (Address, &MemDesc);\r
573 if (!EFI_ERROR (Status)) {\r
574 gDS->SetMemorySpaceAttributes (\r
575 Address,\r
576 ApSafeBufferSize,\r
577 MemDesc.Attributes & (~EFI_MEMORY_XP)\r
578 );\r
73ccde8f
XY
579 }\r
580\r
cbcf0cd6
YX
581 ApSafeBufferSize = EFI_PAGES_TO_SIZE (\r
582 EFI_SIZE_TO_PAGES (\r
583 CpuMpData->CpuCount * AP_SAFE_STACK_SIZE\r
584 )\r
585 );\r
586 Address = BASE_4GB - 1;\r
587 Status = gBS->AllocatePages (\r
588 AllocateMaxAddress,\r
589 EfiReservedMemoryType,\r
590 EFI_SIZE_TO_PAGES (ApSafeBufferSize),\r
591 &Address\r
592 );\r
593 ASSERT_EFI_ERROR (Status);\r
594\r
595 mReservedTopOfApStack = (UINTN)Address + ApSafeBufferSize;\r
596 ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) == 0);\r
597 CopyMem (\r
598 mReservedApLoopFunc,\r
599 CpuMpData->AddressMap.RelocateApLoopFuncAddress,\r
600 CpuMpData->AddressMap.RelocateApLoopFuncSize\r
601 );\r
5183fb37 602\r
96378861
JF
603 Status = gBS->CreateEvent (\r
604 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
605 TPL_NOTIFY,\r
606 CheckApsStatus,\r
607 NULL,\r
608 &mCheckAllApsEvent\r
609 );\r
610 ASSERT_EFI_ERROR (Status);\r
611\r
612 //\r
613 // Set timer to check all APs status.\r
614 //\r
615 Status = gBS->SetTimer (\r
616 mCheckAllApsEvent,\r
617 TimerPeriodic,\r
a1c35ff3
HW
618 EFI_TIMER_PERIOD_MICROSECONDS (\r
619 PcdGet32 (PcdCpuApStatusCheckIntervalInMicroSeconds)\r
620 )\r
96378861
JF
621 );\r
622 ASSERT_EFI_ERROR (Status);\r
8677a56a 623\r
4d3314f6
JF
624 Status = gBS->CreateEvent (\r
625 EVT_SIGNAL_EXIT_BOOT_SERVICES,\r
626 TPL_CALLBACK,\r
86af2eb8 627 MpInitChangeApLoopCallback,\r
4d3314f6
JF
628 NULL,\r
629 &mMpInitExitBootServicesEvent\r
630 );\r
631 ASSERT_EFI_ERROR (Status);\r
8677a56a
JF
632\r
633 Status = gBS->CreateEventEx (\r
634 EVT_NOTIFY_SIGNAL,\r
635 TPL_CALLBACK,\r
636 MpInitChangeApLoopCallback,\r
637 NULL,\r
638 &gEfiEventLegacyBootGuid,\r
639 &mLegacyBootEvent\r
640 );\r
641 ASSERT_EFI_ERROR (Status);\r
93ca4c0f 642}\r
3e8ad6bd
JF
643\r
644/**\r
645 This service executes a caller provided function on all enabled APs.\r
646\r
647 @param[in] Procedure A pointer to the function to be run on\r
648 enabled APs of the system. See type\r
649 EFI_AP_PROCEDURE.\r
650 @param[in] SingleThread If TRUE, then all the enabled APs execute\r
651 the function specified by Procedure one by\r
652 one, in ascending order of processor handle\r
653 number. If FALSE, then all the enabled APs\r
654 execute the function specified by Procedure\r
655 simultaneously.\r
656 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
657 service. If it is NULL, then execute in\r
658 blocking mode. BSP waits until all APs finish\r
659 or TimeoutInMicroSeconds expires. If it's\r
660 not NULL, then execute in non-blocking mode.\r
661 BSP requests the function specified by\r
662 Procedure to be started on all the enabled\r
663 APs, and go on executing immediately. If\r
664 all return from Procedure, or TimeoutInMicroSeconds\r
665 expires, this event is signaled. The BSP\r
666 can use the CheckEvent() or WaitForEvent()\r
667 services to check the state of event. Type\r
668 EFI_EVENT is defined in CreateEvent() in\r
669 the Unified Extensible Firmware Interface\r
670 Specification.\r
367284e7 671 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
3e8ad6bd
JF
672 APs to return from Procedure, either for\r
673 blocking or non-blocking mode. Zero means\r
674 infinity. If the timeout expires before\r
675 all APs return from Procedure, then Procedure\r
676 on the failed APs is terminated. All enabled\r
677 APs are available for next function assigned\r
678 by MpInitLibStartupAllAPs() or\r
679 MPInitLibStartupThisAP().\r
680 If the timeout expires in blocking mode,\r
681 BSP returns EFI_TIMEOUT. If the timeout\r
682 expires in non-blocking mode, WaitEvent\r
683 is signaled with SignalEvent().\r
684 @param[in] ProcedureArgument The parameter passed into Procedure for\r
685 all APs.\r
686 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,\r
687 if all APs finish successfully, then its\r
688 content is set to NULL. If not all APs\r
689 finish before timeout expires, then its\r
690 content is set to address of the buffer\r
691 holding handle numbers of the failed APs.\r
692 The buffer is allocated by MP Initialization\r
693 library, and it's the caller's responsibility to\r
694 free the buffer with FreePool() service.\r
695 In blocking mode, it is ready for consumption\r
696 when the call returns. In non-blocking mode,\r
697 it is ready when WaitEvent is signaled. The\r
698 list of failed CPU is terminated by\r
699 END_OF_CPU_LIST.\r
700\r
701 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
702 the timeout expired.\r
703 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
704 to all enabled APs.\r
705 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
706 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
707 signaled.\r
708 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not\r
709 supported.\r
710 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
711 @retval EFI_NOT_STARTED No enabled APs exist in the system.\r
712 @retval EFI_NOT_READY Any enabled APs are busy.\r
713 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
714 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
715 all enabled APs have finished.\r
716 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
717\r
718**/\r
719EFI_STATUS\r
720EFIAPI\r
721MpInitLibStartupAllAPs (\r
053e878b
MK
722 IN EFI_AP_PROCEDURE Procedure,\r
723 IN BOOLEAN SingleThread,\r
724 IN EFI_EVENT WaitEvent OPTIONAL,\r
725 IN UINTN TimeoutInMicroseconds,\r
726 IN VOID *ProcedureArgument OPTIONAL,\r
727 OUT UINTN **FailedCpuList OPTIONAL\r
3e8ad6bd
JF
728 )\r
729{\r
053e878b 730 EFI_STATUS Status;\r
86efe976
JF
731\r
732 //\r
733 // Temporarily stop checkAllApsStatus for avoid resource dead-lock.\r
734 //\r
735 mStopCheckAllApsStatus = TRUE;\r
736\r
ee0c39fa 737 Status = StartupAllCPUsWorker (\r
86efe976
JF
738 Procedure,\r
739 SingleThread,\r
ee0c39fa 740 TRUE,\r
86efe976
JF
741 WaitEvent,\r
742 TimeoutInMicroseconds,\r
743 ProcedureArgument,\r
744 FailedCpuList\r
745 );\r
746\r
747 //\r
748 // Start checkAllApsStatus\r
749 //\r
750 mStopCheckAllApsStatus = FALSE;\r
751\r
752 return Status;\r
3e8ad6bd
JF
753}\r
754\r
755/**\r
756 This service lets the caller get one enabled AP to execute a caller-provided\r
757 function.\r
758\r
759 @param[in] Procedure A pointer to the function to be run on the\r
760 designated AP of the system. See type\r
761 EFI_AP_PROCEDURE.\r
762 @param[in] ProcessorNumber The handle number of the AP. The range is\r
763 from 0 to the total number of logical\r
764 processors minus 1. The total number of\r
765 logical processors can be retrieved by\r
766 MpInitLibGetNumberOfProcessors().\r
767 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
768 service. If it is NULL, then execute in\r
769 blocking mode. BSP waits until this AP finish\r
770 or TimeoutInMicroSeconds expires. If it's\r
771 not NULL, then execute in non-blocking mode.\r
772 BSP requests the function specified by\r
773 Procedure to be started on this AP,\r
774 and go on executing immediately. If this AP\r
775 return from Procedure or TimeoutInMicroSeconds\r
776 expires, this event is signaled. The BSP\r
777 can use the CheckEvent() or WaitForEvent()\r
778 services to check the state of event. Type\r
779 EFI_EVENT is defined in CreateEvent() in\r
780 the Unified Extensible Firmware Interface\r
781 Specification.\r
367284e7 782 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
3e8ad6bd
JF
783 this AP to finish this Procedure, either for\r
784 blocking or non-blocking mode. Zero means\r
785 infinity. If the timeout expires before\r
786 this AP returns from Procedure, then Procedure\r
787 on the AP is terminated. The\r
788 AP is available for next function assigned\r
789 by MpInitLibStartupAllAPs() or\r
790 MpInitLibStartupThisAP().\r
791 If the timeout expires in blocking mode,\r
792 BSP returns EFI_TIMEOUT. If the timeout\r
793 expires in non-blocking mode, WaitEvent\r
794 is signaled with SignalEvent().\r
795 @param[in] ProcedureArgument The parameter passed into Procedure on the\r
796 specified AP.\r
797 @param[out] Finished If NULL, this parameter is ignored. In\r
798 blocking mode, this parameter is ignored.\r
799 In non-blocking mode, if AP returns from\r
800 Procedure before the timeout expires, its\r
801 content is set to TRUE. Otherwise, the\r
802 value is set to FALSE. The caller can\r
803 determine if the AP returned from Procedure\r
804 by evaluating this value.\r
805\r
806 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
807 the timeout expires.\r
808 @retval EFI_SUCCESS In non-blocking mode, the function has been\r
809 dispatched to specified AP.\r
810 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
811 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
812 signaled.\r
813 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not\r
814 supported.\r
815 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
816 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
817 the specified AP has finished.\r
818 @retval EFI_NOT_READY The specified AP is busy.\r
819 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
820 @retval EFI_NOT_FOUND The processor with the handle specified by\r
821 ProcessorNumber does not exist.\r
822 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
823 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
824\r
825**/\r
826EFI_STATUS\r
827EFIAPI\r
828MpInitLibStartupThisAP (\r
053e878b
MK
829 IN EFI_AP_PROCEDURE Procedure,\r
830 IN UINTN ProcessorNumber,\r
831 IN EFI_EVENT WaitEvent OPTIONAL,\r
832 IN UINTN TimeoutInMicroseconds,\r
833 IN VOID *ProcedureArgument OPTIONAL,\r
834 OUT BOOLEAN *Finished OPTIONAL\r
3e8ad6bd
JF
835 )\r
836{\r
053e878b 837 EFI_STATUS Status;\r
20ae5774
JF
838\r
839 //\r
840 // temporarily stop checkAllApsStatus for avoid resource dead-lock.\r
841 //\r
842 mStopCheckAllApsStatus = TRUE;\r
843\r
844 Status = StartupThisAPWorker (\r
845 Procedure,\r
846 ProcessorNumber,\r
847 WaitEvent,\r
848 TimeoutInMicroseconds,\r
849 ProcedureArgument,\r
850 Finished\r
851 );\r
852\r
853 mStopCheckAllApsStatus = FALSE;\r
854\r
855 return Status;\r
3e8ad6bd
JF
856}\r
857\r
858/**\r
859 This service switches the requested AP to be the BSP from that point onward.\r
860 This service changes the BSP for all purposes. This call can only be performed\r
861 by the current BSP.\r
862\r
863 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
864 BSP. The range is from 0 to the total number of\r
865 logical processors minus 1. The total number of\r
866 logical processors can be retrieved by\r
867 MpInitLibGetNumberOfProcessors().\r
868 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
869 enabled AP. Otherwise, it will be disabled.\r
870\r
871 @retval EFI_SUCCESS BSP successfully switched.\r
872 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to\r
873 this service returning.\r
874 @retval EFI_UNSUPPORTED Switching the BSP is not supported.\r
875 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
876 @retval EFI_NOT_FOUND The processor with the handle specified by\r
877 ProcessorNumber does not exist.\r
878 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or\r
879 a disabled AP.\r
880 @retval EFI_NOT_READY The specified AP is busy.\r
881 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
882\r
883**/\r
884EFI_STATUS\r
885EFIAPI\r
886MpInitLibSwitchBSP (\r
053e878b
MK
887 IN UINTN ProcessorNumber,\r
888 IN BOOLEAN EnableOldBSP\r
3e8ad6bd
JF
889 )\r
890{\r
053e878b
MK
891 EFI_STATUS Status;\r
892 EFI_TIMER_ARCH_PROTOCOL *Timer;\r
893 UINT64 TimerPeriod;\r
41be0da5 894\r
8ad05bd2 895 TimerPeriod = 0;\r
b6e45716
JF
896 //\r
897 // Locate Timer Arch Protocol\r
898 //\r
053e878b 899 Status = gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID **)&Timer);\r
b6e45716
JF
900 if (EFI_ERROR (Status)) {\r
901 Timer = NULL;\r
902 }\r
903\r
904 if (Timer != NULL) {\r
905 //\r
906 // Save current rate of DXE Timer\r
907 //\r
908 Timer->GetTimerPeriod (Timer, &TimerPeriod);\r
909 //\r
910 // Disable DXE Timer and drain pending interrupts\r
911 //\r
912 Timer->SetTimerPeriod (Timer, 0);\r
913 }\r
41be0da5
JF
914\r
915 Status = SwitchBSPWorker (ProcessorNumber, EnableOldBSP);\r
916\r
b6e45716
JF
917 if (Timer != NULL) {\r
918 //\r
919 // Enable and restore rate of DXE Timer\r
920 //\r
921 Timer->SetTimerPeriod (Timer, TimerPeriod);\r
922 }\r
923\r
41be0da5 924 return Status;\r
3e8ad6bd
JF
925}\r
926\r
927/**\r
928 This service lets the caller enable or disable an AP from this point onward.\r
929 This service may only be called from the BSP.\r
930\r
931 @param[in] ProcessorNumber The handle number of AP.\r
932 The range is from 0 to the total number of\r
933 logical processors minus 1. The total number of\r
934 logical processors can be retrieved by\r
935 MpInitLibGetNumberOfProcessors().\r
936 @param[in] EnableAP Specifies the new state for the processor for\r
937 enabled, FALSE for disabled.\r
938 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
939 the new health status of the AP. This flag\r
940 corresponds to StatusFlag defined in\r
941 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r
942 the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
943 bits are ignored. If it is NULL, this parameter\r
944 is ignored.\r
945\r
946 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
947 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed\r
948 prior to this service returning.\r
949 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
950 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
951 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
952 does not exist.\r
953 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
954 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
955\r
956**/\r
957EFI_STATUS\r
958EFIAPI\r
959MpInitLibEnableDisableAP (\r
053e878b
MK
960 IN UINTN ProcessorNumber,\r
961 IN BOOLEAN EnableAP,\r
962 IN UINT32 *HealthFlag OPTIONAL\r
3e8ad6bd
JF
963 )\r
964{\r
053e878b
MK
965 EFI_STATUS Status;\r
966 BOOLEAN TempStopCheckState;\r
e37109bc
JF
967\r
968 TempStopCheckState = FALSE;\r
969 //\r
970 // temporarily stop checkAllAPsStatus for initialize parameters.\r
971 //\r
972 if (!mStopCheckAllApsStatus) {\r
973 mStopCheckAllApsStatus = TRUE;\r
974 TempStopCheckState = TRUE;\r
975 }\r
976\r
977 Status = EnableDisableApWorker (ProcessorNumber, EnableAP, HealthFlag);\r
978\r
979 if (TempStopCheckState) {\r
980 mStopCheckAllApsStatus = FALSE;\r
981 }\r
982\r
983 return Status;\r
3e8ad6bd 984}\r
c788c2b1
SF
985\r
986/**\r
987 This funtion will try to invoke platform specific microcode shadow logic to\r
988 relocate microcode update patches into memory.\r
989\r
4ac82ea1 990 @param[in, out] CpuMpData The pointer to CPU MP Data structure.\r
c788c2b1
SF
991\r
992 @retval EFI_SUCCESS Shadow microcode success.\r
993 @retval EFI_OUT_OF_RESOURCES No enough resource to complete the operation.\r
994 @retval EFI_UNSUPPORTED Can't find platform specific microcode shadow\r
995 PPI/Protocol.\r
996**/\r
997EFI_STATUS\r
998PlatformShadowMicrocode (\r
053e878b 999 IN OUT CPU_MP_DATA *CpuMpData\r
c788c2b1
SF
1000 )\r
1001{\r
1002 //\r
1003 // There is no DXE version of platform shadow microcode protocol so far.\r
1004 // A platform which only uses DxeMpInitLib instance could only supports\r
1005 // the PCD based microcode shadowing.\r
1006 //\r
1007 return EFI_UNSUPPORTED;\r
1008}\r