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