]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
Revert "UefiCpuPkg: Duplicated AsmRelocateApLoop as AsmRelocateApLoopAmd"
[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
33a3408f 4 Copyright (c) 2016 - 2020, 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
33a3408f
YX
381 CPU_MP_DATA *CpuMpData;\r
382 BOOLEAN MwaitSupport;\r
383 ASM_RELOCATE_AP_LOOP AsmRelocateApLoopFunc;\r
384 UINTN ProcessorNumber;\r
385 UINTN StackStart;\r
4d3314f6 386\r
7367cc6c 387 MpInitLibWhoAmI (&ProcessorNumber);\r
4d3314f6
JF
388 CpuMpData = GetCpuMpData ();\r
389 MwaitSupport = IsMwaitSupport ();\r
33a3408f
YX
390 if (CpuMpData->UseSevEsAPMethod) {\r
391 StackStart = CpuMpData->SevEsAPResetStackStart;\r
20da7ca4 392 } else {\r
33a3408f 393 StackStart = mReservedTopOfApStack;\r
20da7ca4 394 }\r
053e878b 395\r
33a3408f
YX
396 AsmRelocateApLoopFunc = (ASM_RELOCATE_AP_LOOP)(UINTN)mReservedApLoopFunc;\r
397 AsmRelocateApLoopFunc (\r
398 MwaitSupport,\r
399 CpuMpData->ApTargetCState,\r
400 CpuMpData->PmCodeSegment,\r
401 StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE,\r
402 (UINTN)&mNumberToFinish,\r
403 CpuMpData->Pm16CodeSegment,\r
404 CpuMpData->SevEsAPBuffer,\r
405 CpuMpData->WakeupBuffer\r
406 );\r
4d3314f6
JF
407 //\r
408 // It should never reach here\r
409 //\r
410 ASSERT (FALSE);\r
411}\r
412\r
413/**\r
414 Callback function for ExitBootServices.\r
415\r
416 @param[in] Event Event whose notification function is being invoked.\r
417 @param[in] Context The pointer to the notification function's context,\r
418 which is implementation-dependent.\r
419\r
420**/\r
421VOID\r
422EFIAPI\r
86af2eb8 423MpInitChangeApLoopCallback (\r
053e878b
MK
424 IN EFI_EVENT Event,\r
425 IN VOID *Context\r
4d3314f6
JF
426 )\r
427{\r
053e878b 428 CPU_MP_DATA *CpuMpData;\r
5183fb37 429\r
053e878b
MK
430 CpuMpData = GetCpuMpData ();\r
431 CpuMpData->PmCodeSegment = GetProtectedModeCS ();\r
7b7508ad 432 CpuMpData->Pm16CodeSegment = GetProtectedMode16CS ();\r
053e878b
MK
433 CpuMpData->ApLoopMode = PcdGet8 (PcdCpuApLoopMode);\r
434 mNumberToFinish = CpuMpData->CpuCount - 1;\r
cf4e79e4 435 WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, NULL, TRUE);\r
9f91cb01
JF
436 while (mNumberToFinish > 0) {\r
437 CpuPause ();\r
438 }\r
20da7ca4 439\r
06544455 440 if (CpuMpData->UseSevEsAPMethod && (CpuMpData->WakeupBuffer != (UINTN)-1)) {\r
20da7ca4
TL
441 //\r
442 // There are APs present. Re-use reserved memory area below 1MB from\r
443 // WakeupBuffer as the area to be used for transitioning to 16-bit mode\r
444 // in support of booting of the AP by an OS.\r
445 //\r
446 CopyMem (\r
053e878b
MK
447 (VOID *)CpuMpData->WakeupBuffer,\r
448 (VOID *)(CpuMpData->AddressMap.RendezvousFunnelAddress +\r
449 CpuMpData->AddressMap.SwitchToRealPM16ModeOffset),\r
20da7ca4
TL
450 CpuMpData->AddressMap.SwitchToRealPM16ModeSize\r
451 );\r
452 }\r
453\r
86af2eb8 454 DEBUG ((DEBUG_INFO, "%a() done!\n", __FUNCTION__));\r
4d3314f6
JF
455}\r
456\r
93ca4c0f
JF
457/**\r
458 Initialize global data for MP support.\r
459\r
460 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
461**/\r
462VOID\r
463InitMpGlobalData (\r
053e878b 464 IN CPU_MP_DATA *CpuMpData\r
93ca4c0f
JF
465 )\r
466{\r
053e878b 467 EFI_STATUS Status;\r
cbcf0cd6 468 EFI_PHYSICAL_ADDRESS Address;\r
053e878b
MK
469 UINTN ApSafeBufferSize;\r
470 UINTN Index;\r
471 EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc;\r
472 UINTN StackBase;\r
473 CPU_INFO_IN_HOB *CpuInfoInHob;\r
96378861 474\r
93ca4c0f
JF
475 SaveCpuMpData (CpuMpData);\r
476\r
14e8137c
JF
477 if (CpuMpData->CpuCount == 1) {\r
478 //\r
479 // If only BSP exists, return\r
480 //\r
481 return;\r
482 }\r
483\r
15720a6c
JW
484 if (PcdGetBool (PcdCpuStackGuard)) {\r
485 //\r
486 // One extra page at the bottom of the stack is needed for Guard page.\r
487 //\r
488 if (CpuMpData->CpuApStackSize <= EFI_PAGE_SIZE) {\r
489 DEBUG ((DEBUG_ERROR, "PcdCpuApStackSize is not big enough for Stack Guard!\n"));\r
490 ASSERT (FALSE);\r
491 }\r
492\r
52315261
JW
493 //\r
494 // DXE will reuse stack allocated for APs at PEI phase if it's available.\r
495 // Let's check it here.\r
496 //\r
497 // Note: BSP's stack guard is set at DxeIpl phase. But for the sake of\r
498 // BSP/AP exchange, stack guard for ApTopOfStack of cpu 0 will still be\r
499 // set here.\r
500 //\r
501 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;\r
15720a6c 502 for (Index = 0; Index < CpuMpData->CpuCount; ++Index) {\r
053e878b 503 if ((CpuInfoInHob != NULL) && (CpuInfoInHob[Index].ApTopOfStack != 0)) {\r
20737c2f 504 StackBase = (UINTN)CpuInfoInHob[Index].ApTopOfStack - CpuMpData->CpuApStackSize;\r
52315261
JW
505 } else {\r
506 StackBase = CpuMpData->Buffer + Index * CpuMpData->CpuApStackSize;\r
507 }\r
15720a6c
JW
508\r
509 Status = gDS->GetMemorySpaceDescriptor (StackBase, &MemDesc);\r
510 ASSERT_EFI_ERROR (Status);\r
511\r
512 Status = gDS->SetMemorySpaceAttributes (\r
513 StackBase,\r
514 EFI_PAGES_TO_SIZE (1),\r
515 MemDesc.Attributes | EFI_MEMORY_RP\r
516 );\r
517 ASSERT_EFI_ERROR (Status);\r
52315261 518\r
053e878b
MK
519 DEBUG ((\r
520 DEBUG_INFO,\r
521 "Stack Guard set at %lx [cpu%lu]!\n",\r
522 (UINT64)StackBase,\r
523 (UINT64)Index\r
524 ));\r
15720a6c
JW
525 }\r
526 }\r
527\r
5183fb37 528 //\r
ffd6b0b1
JF
529 // Avoid APs access invalid buffer data which allocated by BootServices,\r
530 // so we will allocate reserved data for AP loop code. We also need to\r
531 // allocate this buffer below 4GB due to APs may be transferred to 32bit\r
532 // protected mode on long mode DXE.\r
5183fb37
JF
533 // Allocating it in advance since memory services are not available in\r
534 // Exit Boot Services callback function.\r
535 //\r
053e878b
MK
536 ApSafeBufferSize = EFI_PAGES_TO_SIZE (\r
537 EFI_SIZE_TO_PAGES (\r
cbcf0cd6 538 CpuMpData->AddressMap.RelocateApLoopFuncSize\r
053e878b
MK
539 )\r
540 );\r
cbcf0cd6
YX
541 Address = BASE_4GB - 1;\r
542 Status = gBS->AllocatePages (\r
543 AllocateMaxAddress,\r
544 EfiReservedMemoryType,\r
545 EFI_SIZE_TO_PAGES (ApSafeBufferSize),\r
546 &Address\r
547 );\r
548 ASSERT_EFI_ERROR (Status);\r
bc2288f5 549\r
cbcf0cd6
YX
550 mReservedApLoopFunc = (VOID *)(UINTN)Address;\r
551 ASSERT (mReservedApLoopFunc != NULL);\r
73ccde8f 552\r
cbcf0cd6
YX
553 //\r
554 // Make sure that the buffer memory is executable if NX protection is enabled\r
555 // for EfiReservedMemoryType.\r
556 //\r
557 // TODO: Check EFI_MEMORY_XP bit set or not once it's available in DXE GCD\r
558 // service.\r
559 //\r
560 Status = gDS->GetMemorySpaceDescriptor (Address, &MemDesc);\r
561 if (!EFI_ERROR (Status)) {\r
562 gDS->SetMemorySpaceAttributes (\r
563 Address,\r
564 ApSafeBufferSize,\r
565 MemDesc.Attributes & (~EFI_MEMORY_XP)\r
566 );\r
73ccde8f
XY
567 }\r
568\r
cbcf0cd6
YX
569 ApSafeBufferSize = EFI_PAGES_TO_SIZE (\r
570 EFI_SIZE_TO_PAGES (\r
571 CpuMpData->CpuCount * AP_SAFE_STACK_SIZE\r
572 )\r
573 );\r
574 Address = BASE_4GB - 1;\r
575 Status = gBS->AllocatePages (\r
576 AllocateMaxAddress,\r
577 EfiReservedMemoryType,\r
578 EFI_SIZE_TO_PAGES (ApSafeBufferSize),\r
579 &Address\r
580 );\r
581 ASSERT_EFI_ERROR (Status);\r
582\r
583 mReservedTopOfApStack = (UINTN)Address + ApSafeBufferSize;\r
584 ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) == 0);\r
585 CopyMem (\r
586 mReservedApLoopFunc,\r
587 CpuMpData->AddressMap.RelocateApLoopFuncAddress,\r
588 CpuMpData->AddressMap.RelocateApLoopFuncSize\r
589 );\r
5183fb37 590\r
96378861
JF
591 Status = gBS->CreateEvent (\r
592 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
593 TPL_NOTIFY,\r
594 CheckApsStatus,\r
595 NULL,\r
596 &mCheckAllApsEvent\r
597 );\r
598 ASSERT_EFI_ERROR (Status);\r
599\r
600 //\r
601 // Set timer to check all APs status.\r
602 //\r
603 Status = gBS->SetTimer (\r
604 mCheckAllApsEvent,\r
605 TimerPeriodic,\r
a1c35ff3
HW
606 EFI_TIMER_PERIOD_MICROSECONDS (\r
607 PcdGet32 (PcdCpuApStatusCheckIntervalInMicroSeconds)\r
608 )\r
96378861
JF
609 );\r
610 ASSERT_EFI_ERROR (Status);\r
8677a56a 611\r
4d3314f6
JF
612 Status = gBS->CreateEvent (\r
613 EVT_SIGNAL_EXIT_BOOT_SERVICES,\r
614 TPL_CALLBACK,\r
86af2eb8 615 MpInitChangeApLoopCallback,\r
4d3314f6
JF
616 NULL,\r
617 &mMpInitExitBootServicesEvent\r
618 );\r
619 ASSERT_EFI_ERROR (Status);\r
8677a56a
JF
620\r
621 Status = gBS->CreateEventEx (\r
622 EVT_NOTIFY_SIGNAL,\r
623 TPL_CALLBACK,\r
624 MpInitChangeApLoopCallback,\r
625 NULL,\r
626 &gEfiEventLegacyBootGuid,\r
627 &mLegacyBootEvent\r
628 );\r
629 ASSERT_EFI_ERROR (Status);\r
93ca4c0f 630}\r
3e8ad6bd
JF
631\r
632/**\r
633 This service executes a caller provided function on all enabled APs.\r
634\r
635 @param[in] Procedure A pointer to the function to be run on\r
636 enabled APs of the system. See type\r
637 EFI_AP_PROCEDURE.\r
638 @param[in] SingleThread If TRUE, then all the enabled APs execute\r
639 the function specified by Procedure one by\r
640 one, in ascending order of processor handle\r
641 number. If FALSE, then all the enabled APs\r
642 execute the function specified by Procedure\r
643 simultaneously.\r
644 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
645 service. If it is NULL, then execute in\r
646 blocking mode. BSP waits until all APs finish\r
647 or TimeoutInMicroSeconds expires. If it's\r
648 not NULL, then execute in non-blocking mode.\r
649 BSP requests the function specified by\r
650 Procedure to be started on all the enabled\r
651 APs, and go on executing immediately. If\r
652 all return from Procedure, or TimeoutInMicroSeconds\r
653 expires, this event is signaled. The BSP\r
654 can use the CheckEvent() or WaitForEvent()\r
655 services to check the state of event. Type\r
656 EFI_EVENT is defined in CreateEvent() in\r
657 the Unified Extensible Firmware Interface\r
658 Specification.\r
367284e7 659 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
3e8ad6bd
JF
660 APs to return from Procedure, either for\r
661 blocking or non-blocking mode. Zero means\r
662 infinity. If the timeout expires before\r
663 all APs return from Procedure, then Procedure\r
664 on the failed APs is terminated. All enabled\r
665 APs are available for next function assigned\r
666 by MpInitLibStartupAllAPs() or\r
667 MPInitLibStartupThisAP().\r
668 If the timeout expires in blocking mode,\r
669 BSP returns EFI_TIMEOUT. If the timeout\r
670 expires in non-blocking mode, WaitEvent\r
671 is signaled with SignalEvent().\r
672 @param[in] ProcedureArgument The parameter passed into Procedure for\r
673 all APs.\r
674 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,\r
675 if all APs finish successfully, then its\r
676 content is set to NULL. If not all APs\r
677 finish before timeout expires, then its\r
678 content is set to address of the buffer\r
679 holding handle numbers of the failed APs.\r
680 The buffer is allocated by MP Initialization\r
681 library, and it's the caller's responsibility to\r
682 free the buffer with FreePool() service.\r
683 In blocking mode, it is ready for consumption\r
684 when the call returns. In non-blocking mode,\r
685 it is ready when WaitEvent is signaled. The\r
686 list of failed CPU is terminated by\r
687 END_OF_CPU_LIST.\r
688\r
689 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
690 the timeout expired.\r
691 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
692 to all enabled APs.\r
693 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
694 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
695 signaled.\r
696 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not\r
697 supported.\r
698 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
699 @retval EFI_NOT_STARTED No enabled APs exist in the system.\r
700 @retval EFI_NOT_READY Any enabled APs are busy.\r
701 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
702 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
703 all enabled APs have finished.\r
704 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
705\r
706**/\r
707EFI_STATUS\r
708EFIAPI\r
709MpInitLibStartupAllAPs (\r
053e878b
MK
710 IN EFI_AP_PROCEDURE Procedure,\r
711 IN BOOLEAN SingleThread,\r
712 IN EFI_EVENT WaitEvent OPTIONAL,\r
713 IN UINTN TimeoutInMicroseconds,\r
714 IN VOID *ProcedureArgument OPTIONAL,\r
715 OUT UINTN **FailedCpuList OPTIONAL\r
3e8ad6bd
JF
716 )\r
717{\r
053e878b 718 EFI_STATUS Status;\r
86efe976
JF
719\r
720 //\r
721 // Temporarily stop checkAllApsStatus for avoid resource dead-lock.\r
722 //\r
723 mStopCheckAllApsStatus = TRUE;\r
724\r
ee0c39fa 725 Status = StartupAllCPUsWorker (\r
86efe976
JF
726 Procedure,\r
727 SingleThread,\r
ee0c39fa 728 TRUE,\r
86efe976
JF
729 WaitEvent,\r
730 TimeoutInMicroseconds,\r
731 ProcedureArgument,\r
732 FailedCpuList\r
733 );\r
734\r
735 //\r
736 // Start checkAllApsStatus\r
737 //\r
738 mStopCheckAllApsStatus = FALSE;\r
739\r
740 return Status;\r
3e8ad6bd
JF
741}\r
742\r
743/**\r
744 This service lets the caller get one enabled AP to execute a caller-provided\r
745 function.\r
746\r
747 @param[in] Procedure A pointer to the function to be run on the\r
748 designated AP of the system. See type\r
749 EFI_AP_PROCEDURE.\r
750 @param[in] ProcessorNumber The handle number of the AP. The range is\r
751 from 0 to the total number of logical\r
752 processors minus 1. The total number of\r
753 logical processors can be retrieved by\r
754 MpInitLibGetNumberOfProcessors().\r
755 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
756 service. If it is NULL, then execute in\r
757 blocking mode. BSP waits until this AP finish\r
758 or TimeoutInMicroSeconds expires. If it's\r
759 not NULL, then execute in non-blocking mode.\r
760 BSP requests the function specified by\r
761 Procedure to be started on this AP,\r
762 and go on executing immediately. If this AP\r
763 return from Procedure or TimeoutInMicroSeconds\r
764 expires, this event is signaled. The BSP\r
765 can use the CheckEvent() or WaitForEvent()\r
766 services to check the state of event. Type\r
767 EFI_EVENT is defined in CreateEvent() in\r
768 the Unified Extensible Firmware Interface\r
769 Specification.\r
367284e7 770 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
3e8ad6bd
JF
771 this AP to finish this Procedure, either for\r
772 blocking or non-blocking mode. Zero means\r
773 infinity. If the timeout expires before\r
774 this AP returns from Procedure, then Procedure\r
775 on the AP is terminated. The\r
776 AP is available for next function assigned\r
777 by MpInitLibStartupAllAPs() or\r
778 MpInitLibStartupThisAP().\r
779 If the timeout expires in blocking mode,\r
780 BSP returns EFI_TIMEOUT. If the timeout\r
781 expires in non-blocking mode, WaitEvent\r
782 is signaled with SignalEvent().\r
783 @param[in] ProcedureArgument The parameter passed into Procedure on the\r
784 specified AP.\r
785 @param[out] Finished If NULL, this parameter is ignored. In\r
786 blocking mode, this parameter is ignored.\r
787 In non-blocking mode, if AP returns from\r
788 Procedure before the timeout expires, its\r
789 content is set to TRUE. Otherwise, the\r
790 value is set to FALSE. The caller can\r
791 determine if the AP returned from Procedure\r
792 by evaluating this value.\r
793\r
794 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
795 the timeout expires.\r
796 @retval EFI_SUCCESS In non-blocking mode, the function has been\r
797 dispatched to specified AP.\r
798 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
799 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
800 signaled.\r
801 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not\r
802 supported.\r
803 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
804 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
805 the specified AP has finished.\r
806 @retval EFI_NOT_READY The specified AP is busy.\r
807 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
808 @retval EFI_NOT_FOUND The processor with the handle specified by\r
809 ProcessorNumber does not exist.\r
810 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
811 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
812\r
813**/\r
814EFI_STATUS\r
815EFIAPI\r
816MpInitLibStartupThisAP (\r
053e878b
MK
817 IN EFI_AP_PROCEDURE Procedure,\r
818 IN UINTN ProcessorNumber,\r
819 IN EFI_EVENT WaitEvent OPTIONAL,\r
820 IN UINTN TimeoutInMicroseconds,\r
821 IN VOID *ProcedureArgument OPTIONAL,\r
822 OUT BOOLEAN *Finished OPTIONAL\r
3e8ad6bd
JF
823 )\r
824{\r
053e878b 825 EFI_STATUS Status;\r
20ae5774
JF
826\r
827 //\r
828 // temporarily stop checkAllApsStatus for avoid resource dead-lock.\r
829 //\r
830 mStopCheckAllApsStatus = TRUE;\r
831\r
832 Status = StartupThisAPWorker (\r
833 Procedure,\r
834 ProcessorNumber,\r
835 WaitEvent,\r
836 TimeoutInMicroseconds,\r
837 ProcedureArgument,\r
838 Finished\r
839 );\r
840\r
841 mStopCheckAllApsStatus = FALSE;\r
842\r
843 return Status;\r
3e8ad6bd
JF
844}\r
845\r
846/**\r
847 This service switches the requested AP to be the BSP from that point onward.\r
848 This service changes the BSP for all purposes. This call can only be performed\r
849 by the current BSP.\r
850\r
851 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
852 BSP. The range is from 0 to the total number of\r
853 logical processors minus 1. The total number of\r
854 logical processors can be retrieved by\r
855 MpInitLibGetNumberOfProcessors().\r
856 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
857 enabled AP. Otherwise, it will be disabled.\r
858\r
859 @retval EFI_SUCCESS BSP successfully switched.\r
860 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to\r
861 this service returning.\r
862 @retval EFI_UNSUPPORTED Switching the BSP is not supported.\r
863 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
864 @retval EFI_NOT_FOUND The processor with the handle specified by\r
865 ProcessorNumber does not exist.\r
866 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or\r
867 a disabled AP.\r
868 @retval EFI_NOT_READY The specified AP is busy.\r
869 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
870\r
871**/\r
872EFI_STATUS\r
873EFIAPI\r
874MpInitLibSwitchBSP (\r
053e878b
MK
875 IN UINTN ProcessorNumber,\r
876 IN BOOLEAN EnableOldBSP\r
3e8ad6bd
JF
877 )\r
878{\r
053e878b
MK
879 EFI_STATUS Status;\r
880 EFI_TIMER_ARCH_PROTOCOL *Timer;\r
881 UINT64 TimerPeriod;\r
41be0da5 882\r
8ad05bd2 883 TimerPeriod = 0;\r
b6e45716
JF
884 //\r
885 // Locate Timer Arch Protocol\r
886 //\r
053e878b 887 Status = gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID **)&Timer);\r
b6e45716
JF
888 if (EFI_ERROR (Status)) {\r
889 Timer = NULL;\r
890 }\r
891\r
892 if (Timer != NULL) {\r
893 //\r
894 // Save current rate of DXE Timer\r
895 //\r
896 Timer->GetTimerPeriod (Timer, &TimerPeriod);\r
897 //\r
898 // Disable DXE Timer and drain pending interrupts\r
899 //\r
900 Timer->SetTimerPeriod (Timer, 0);\r
901 }\r
41be0da5
JF
902\r
903 Status = SwitchBSPWorker (ProcessorNumber, EnableOldBSP);\r
904\r
b6e45716
JF
905 if (Timer != NULL) {\r
906 //\r
907 // Enable and restore rate of DXE Timer\r
908 //\r
909 Timer->SetTimerPeriod (Timer, TimerPeriod);\r
910 }\r
911\r
41be0da5 912 return Status;\r
3e8ad6bd
JF
913}\r
914\r
915/**\r
916 This service lets the caller enable or disable an AP from this point onward.\r
917 This service may only be called from the BSP.\r
918\r
919 @param[in] ProcessorNumber The handle number of AP.\r
920 The range is from 0 to the total number of\r
921 logical processors minus 1. The total number of\r
922 logical processors can be retrieved by\r
923 MpInitLibGetNumberOfProcessors().\r
924 @param[in] EnableAP Specifies the new state for the processor for\r
925 enabled, FALSE for disabled.\r
926 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
927 the new health status of the AP. This flag\r
928 corresponds to StatusFlag defined in\r
929 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r
930 the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
931 bits are ignored. If it is NULL, this parameter\r
932 is ignored.\r
933\r
934 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
935 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed\r
936 prior to this service returning.\r
937 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
938 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
939 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
940 does not exist.\r
941 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
942 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
943\r
944**/\r
945EFI_STATUS\r
946EFIAPI\r
947MpInitLibEnableDisableAP (\r
053e878b
MK
948 IN UINTN ProcessorNumber,\r
949 IN BOOLEAN EnableAP,\r
950 IN UINT32 *HealthFlag OPTIONAL\r
3e8ad6bd
JF
951 )\r
952{\r
053e878b
MK
953 EFI_STATUS Status;\r
954 BOOLEAN TempStopCheckState;\r
e37109bc
JF
955\r
956 TempStopCheckState = FALSE;\r
957 //\r
958 // temporarily stop checkAllAPsStatus for initialize parameters.\r
959 //\r
960 if (!mStopCheckAllApsStatus) {\r
961 mStopCheckAllApsStatus = TRUE;\r
962 TempStopCheckState = TRUE;\r
963 }\r
964\r
965 Status = EnableDisableApWorker (ProcessorNumber, EnableAP, HealthFlag);\r
966\r
967 if (TempStopCheckState) {\r
968 mStopCheckAllApsStatus = FALSE;\r
969 }\r
970\r
971 return Status;\r
3e8ad6bd 972}\r
c788c2b1
SF
973\r
974/**\r
975 This funtion will try to invoke platform specific microcode shadow logic to\r
976 relocate microcode update patches into memory.\r
977\r
4ac82ea1 978 @param[in, out] CpuMpData The pointer to CPU MP Data structure.\r
c788c2b1
SF
979\r
980 @retval EFI_SUCCESS Shadow microcode success.\r
981 @retval EFI_OUT_OF_RESOURCES No enough resource to complete the operation.\r
982 @retval EFI_UNSUPPORTED Can't find platform specific microcode shadow\r
983 PPI/Protocol.\r
984**/\r
985EFI_STATUS\r
986PlatformShadowMicrocode (\r
053e878b 987 IN OUT CPU_MP_DATA *CpuMpData\r
c788c2b1
SF
988 )\r
989{\r
990 //\r
991 // There is no DXE version of platform shadow microcode protocol so far.\r
992 // A platform which only uses DxeMpInitLib instance could only supports\r
993 // the PCD based microcode shadowing.\r
994 //\r
995 return EFI_UNSUPPORTED;\r
996}\r