]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
UefiCpuPkg: Split the path in RelocateApLoop into two.
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / DxeMpLib.c
... / ...
CommitLineData
1/** @file\r
2 MP initialize support functions for DXE phase.\r
3\r
4 Copyright (c) 2016 - 2023, Intel Corporation. All rights reserved.<BR>\r
5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
6\r
7**/\r
8\r
9#include "MpLib.h"\r
10\r
11#include <Library/UefiLib.h>\r
12#include <Library/UefiBootServicesTableLib.h>\r
13#include <Library/DebugAgentLib.h>\r
14#include <Library/DxeServicesTableLib.h>\r
15#include <Library/CcExitLib.h>\r
16#include <Register/Amd/Fam17Msr.h>\r
17#include <Register/Amd/Ghcb.h>\r
18\r
19#include <Protocol/Timer.h>\r
20\r
21#define AP_SAFE_STACK_SIZE 128\r
22\r
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
28RELOCATE_AP_LOOP_ENTRY mReservedApLoop;\r
29UINTN mReservedTopOfApStack;\r
30volatile UINT32 mNumberToFinish = 0;\r
31\r
32//\r
33// Begin wakeup buffer allocation below 0x88000\r
34//\r
35STATIC EFI_PHYSICAL_ADDRESS mSevEsDxeWakeupBuffer = 0x88000;\r
36\r
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
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
73 IN CPU_MP_DATA *CpuMpData\r
74 )\r
75{\r
76 mCpuMpData = CpuMpData;\r
77}\r
78\r
79/**\r
80 Get available system memory below 0x88000 by specified size.\r
81\r
82 @param[in] WakeupBufferSize Wakeup buffer size required\r
83\r
84 @retval other Return wakeup buffer address below 1MB.\r
85 @retval -1 Cannot find free memory below 1MB.\r
86**/\r
87UINTN\r
88GetWakeupBuffer (\r
89 IN UINTN WakeupBufferSize\r
90 )\r
91{\r
92 EFI_STATUS Status;\r
93 EFI_PHYSICAL_ADDRESS StartAddress;\r
94 EFI_MEMORY_TYPE MemoryType;\r
95\r
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
104 MemoryType = EfiReservedMemoryType;\r
105 } else {\r
106 MemoryType = EfiBootServicesData;\r
107 }\r
108\r
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
117 if (ConfidentialComputingGuestHas (CCAttrAmdSevEs)) {\r
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
125\r
126 Status = gBS->AllocatePages (\r
127 AllocateMaxAddress,\r
128 MemoryType,\r
129 EFI_SIZE_TO_PAGES (WakeupBufferSize),\r
130 &StartAddress\r
131 );\r
132 ASSERT_EFI_ERROR (Status);\r
133 if (EFI_ERROR (Status)) {\r
134 StartAddress = (EFI_PHYSICAL_ADDRESS)-1;\r
135 } else if (ConfidentialComputingGuestHas (CCAttrAmdSevEs)) {\r
136 //\r
137 // Next SEV-ES wakeup buffer allocation must be below this allocation\r
138 //\r
139 mSevEsDxeWakeupBuffer = StartAddress;\r
140 }\r
141\r
142 DEBUG ((\r
143 DEBUG_INFO,\r
144 "WakeupBufferStart = %x, WakeupBufferSize = %x\n",\r
145 (UINTN)StartAddress,\r
146 WakeupBufferSize\r
147 ));\r
148\r
149 return (UINTN)StartAddress;\r
150}\r
151\r
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
165AllocateCodeBuffer (\r
166 IN UINTN BufferSize\r
167 )\r
168{\r
169 EFI_STATUS Status;\r
170 EFI_PHYSICAL_ADDRESS StartAddress;\r
171\r
172 StartAddress = BASE_4GB - 1;\r
173 Status = gBS->AllocatePages (\r
174 AllocateMaxAddress,\r
175 EfiBootServicesCode,\r
176 EFI_SIZE_TO_PAGES (BufferSize),\r
177 &StartAddress\r
178 );\r
179 if (EFI_ERROR (Status)) {\r
180 StartAddress = 0;\r
181 }\r
182\r
183 return (UINTN)StartAddress;\r
184}\r
185\r
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
199 EFI_STATUS Status;\r
200 EFI_PHYSICAL_ADDRESS StartAddress;\r
201 MSR_SEV_ES_GHCB_REGISTER Msr;\r
202 GHCB *Ghcb;\r
203 BOOLEAN InterruptState;\r
204\r
205 //\r
206 // Allocate 1 page for AP jump table page\r
207 //\r
208 StartAddress = BASE_4GB - 1;\r
209 Status = gBS->AllocatePages (\r
210 AllocateMaxAddress,\r
211 EfiReservedMemoryType,\r
212 1,\r
213 &StartAddress\r
214 );\r
215 ASSERT_EFI_ERROR (Status);\r
216\r
217 DEBUG ((DEBUG_INFO, "Dxe: SevEsAPMemory = %lx\n", (UINTN)StartAddress));\r
218\r
219 //\r
220 // Save the SevEsAPMemory as the AP jump table.\r
221 //\r
222 Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB);\r
223 Ghcb = Msr.Ghcb;\r
224\r
225 CcExitVmgInit (Ghcb, &InterruptState);\r
226 CcExitVmgExit (Ghcb, SVM_EXIT_AP_JUMP_TABLE, 0, (UINT64)(UINTN)StartAddress);\r
227 CcExitVmgDone (Ghcb, InterruptState);\r
228\r
229 return (UINTN)StartAddress;\r
230}\r
231\r
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
241 UINTN ProcessorNumber;\r
242 EFI_STATUS Status;\r
243 CPU_MP_DATA *CpuMpData;\r
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
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
256 Status = gBS->SignalEvent (CpuMpData->WaitEvent);\r
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
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
273 CpuMpData->CpuData[ProcessorNumber].WaitEvent = NULL;\r
274 }\r
275 }\r
276}\r
277\r
278/**\r
279 Checks APs' status periodically.\r
280\r
281 This function is triggered by timer periodically to check the\r
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
292 IN EFI_EVENT Event,\r
293 IN VOID *Context\r
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
303\r
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
320 Index = (UINT16)-1;\r
321 AsmReadGdtr (&GdtrDesc);\r
322 GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);\r
323 GdtEntry = (IA32_SEGMENT_DESCRIPTOR *)GdtrDesc.Base;\r
324 for (Index = 0; Index < GdtEntryCount; Index++) {\r
325 if (GdtEntry->Bits.L == 0) {\r
326 if ((GdtEntry->Bits.Type > 8) && (GdtEntry->Bits.DB == 0)) {\r
327 break;\r
328 }\r
329 }\r
330\r
331 GdtEntry++;\r
332 }\r
333\r
334 ASSERT (Index != GdtEntryCount);\r
335 return Index * 8;\r
336}\r
337\r
338/**\r
339 Get Protected mode code segment from current GDT table.\r
340\r
341 @return Protected mode code segment value.\r
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
353 AsmReadGdtr (&GdtrDesc);\r
354 GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);\r
355 GdtEntry = (IA32_SEGMENT_DESCRIPTOR *)GdtrDesc.Base;\r
356 for (Index = 0; Index < GdtEntryCount; Index++) {\r
357 if (GdtEntry->Bits.L == 0) {\r
358 if ((GdtEntry->Bits.Type > 8) && (GdtEntry->Bits.DB == 1)) {\r
359 break;\r
360 }\r
361 }\r
362\r
363 GdtEntry++;\r
364 }\r
365\r
366 ASSERT (Index != GdtEntryCount);\r
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
381 CPU_MP_DATA *CpuMpData;\r
382 BOOLEAN MwaitSupport;\r
383 UINTN ProcessorNumber;\r
384 UINTN StackStart;\r
385\r
386 MpInitLibWhoAmI (&ProcessorNumber);\r
387 CpuMpData = GetCpuMpData ();\r
388 MwaitSupport = IsMwaitSupport ();\r
389 if (CpuMpData->UseSevEsAPMethod) {\r
390 //\r
391 // 64-bit AMD processors with SEV-ES\r
392 //\r
393 StackStart = CpuMpData->SevEsAPResetStackStart;\r
394 mReservedApLoop.AmdSevEntry (\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
404 } else {\r
405 //\r
406 // Intel processors (32-bit or 64-bit), 32-bit AMD processors, or 64-bit AMD processors without SEV-ES\r
407 //\r
408 StackStart = mReservedTopOfApStack;\r
409 mReservedApLoop.GenericEntry (\r
410 MwaitSupport,\r
411 CpuMpData->ApTargetCState,\r
412 CpuMpData->PmCodeSegment,\r
413 StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE,\r
414 (UINTN)&mNumberToFinish,\r
415 CpuMpData->Pm16CodeSegment,\r
416 CpuMpData->SevEsAPBuffer,\r
417 CpuMpData->WakeupBuffer\r
418 );\r
419 }\r
420\r
421 //\r
422 // It should never reach here\r
423 //\r
424 ASSERT (FALSE);\r
425}\r
426\r
427/**\r
428 Callback function for ExitBootServices.\r
429\r
430 @param[in] Event Event whose notification function is being invoked.\r
431 @param[in] Context The pointer to the notification function's context,\r
432 which is implementation-dependent.\r
433\r
434**/\r
435VOID\r
436EFIAPI\r
437MpInitChangeApLoopCallback (\r
438 IN EFI_EVENT Event,\r
439 IN VOID *Context\r
440 )\r
441{\r
442 CPU_MP_DATA *CpuMpData;\r
443\r
444 CpuMpData = GetCpuMpData ();\r
445 CpuMpData->PmCodeSegment = GetProtectedModeCS ();\r
446 CpuMpData->Pm16CodeSegment = GetProtectedMode16CS ();\r
447 CpuMpData->ApLoopMode = PcdGet8 (PcdCpuApLoopMode);\r
448 mNumberToFinish = CpuMpData->CpuCount - 1;\r
449 WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, NULL, TRUE);\r
450 while (mNumberToFinish > 0) {\r
451 CpuPause ();\r
452 }\r
453\r
454 if (CpuMpData->UseSevEsAPMethod && (CpuMpData->WakeupBuffer != (UINTN)-1)) {\r
455 //\r
456 // There are APs present. Re-use reserved memory area below 1MB from\r
457 // WakeupBuffer as the area to be used for transitioning to 16-bit mode\r
458 // in support of booting of the AP by an OS.\r
459 //\r
460 CopyMem (\r
461 (VOID *)CpuMpData->WakeupBuffer,\r
462 (VOID *)(CpuMpData->AddressMap.RendezvousFunnelAddress +\r
463 CpuMpData->AddressMap.SwitchToRealPM16ModeOffset),\r
464 CpuMpData->AddressMap.SwitchToRealPM16ModeSize\r
465 );\r
466 }\r
467\r
468 DEBUG ((DEBUG_INFO, "%a() done!\n", __FUNCTION__));\r
469}\r
470\r
471/**\r
472 Initialize global data for MP support.\r
473\r
474 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
475**/\r
476VOID\r
477InitMpGlobalData (\r
478 IN CPU_MP_DATA *CpuMpData\r
479 )\r
480{\r
481 EFI_STATUS Status;\r
482 EFI_PHYSICAL_ADDRESS Address;\r
483 UINTN ApSafeBufferSize;\r
484 UINTN Index;\r
485 EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc;\r
486 UINTN StackBase;\r
487 CPU_INFO_IN_HOB *CpuInfoInHob;\r
488\r
489 SaveCpuMpData (CpuMpData);\r
490\r
491 if (CpuMpData->CpuCount == 1) {\r
492 //\r
493 // If only BSP exists, return\r
494 //\r
495 return;\r
496 }\r
497\r
498 if (PcdGetBool (PcdCpuStackGuard)) {\r
499 //\r
500 // One extra page at the bottom of the stack is needed for Guard page.\r
501 //\r
502 if (CpuMpData->CpuApStackSize <= EFI_PAGE_SIZE) {\r
503 DEBUG ((DEBUG_ERROR, "PcdCpuApStackSize is not big enough for Stack Guard!\n"));\r
504 ASSERT (FALSE);\r
505 }\r
506\r
507 //\r
508 // DXE will reuse stack allocated for APs at PEI phase if it's available.\r
509 // Let's check it here.\r
510 //\r
511 // Note: BSP's stack guard is set at DxeIpl phase. But for the sake of\r
512 // BSP/AP exchange, stack guard for ApTopOfStack of cpu 0 will still be\r
513 // set here.\r
514 //\r
515 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;\r
516 for (Index = 0; Index < CpuMpData->CpuCount; ++Index) {\r
517 if ((CpuInfoInHob != NULL) && (CpuInfoInHob[Index].ApTopOfStack != 0)) {\r
518 StackBase = (UINTN)CpuInfoInHob[Index].ApTopOfStack - CpuMpData->CpuApStackSize;\r
519 } else {\r
520 StackBase = CpuMpData->Buffer + Index * CpuMpData->CpuApStackSize;\r
521 }\r
522\r
523 Status = gDS->GetMemorySpaceDescriptor (StackBase, &MemDesc);\r
524 ASSERT_EFI_ERROR (Status);\r
525\r
526 Status = gDS->SetMemorySpaceAttributes (\r
527 StackBase,\r
528 EFI_PAGES_TO_SIZE (1),\r
529 MemDesc.Attributes | EFI_MEMORY_RP\r
530 );\r
531 ASSERT_EFI_ERROR (Status);\r
532\r
533 DEBUG ((\r
534 DEBUG_INFO,\r
535 "Stack Guard set at %lx [cpu%lu]!\n",\r
536 (UINT64)StackBase,\r
537 (UINT64)Index\r
538 ));\r
539 }\r
540 }\r
541\r
542 //\r
543 // Avoid APs access invalid buffer data which allocated by BootServices,\r
544 // so we will allocate reserved data for AP loop code. We also need to\r
545 // allocate this buffer below 4GB due to APs may be transferred to 32bit\r
546 // protected mode on long mode DXE.\r
547 // Allocating it in advance since memory services are not available in\r
548 // Exit Boot Services callback function.\r
549 //\r
550 ApSafeBufferSize = EFI_PAGES_TO_SIZE (\r
551 EFI_SIZE_TO_PAGES (\r
552 CpuMpData->AddressMap.RelocateApLoopFuncSize\r
553 )\r
554 );\r
555 Address = BASE_4GB - 1;\r
556 Status = gBS->AllocatePages (\r
557 AllocateMaxAddress,\r
558 EfiReservedMemoryType,\r
559 EFI_SIZE_TO_PAGES (ApSafeBufferSize),\r
560 &Address\r
561 );\r
562 ASSERT_EFI_ERROR (Status);\r
563\r
564 mReservedApLoop.Data = (VOID *)(UINTN)Address;\r
565 ASSERT (mReservedApLoop.Data != NULL);\r
566\r
567 //\r
568 // Make sure that the buffer memory is executable if NX protection is enabled\r
569 // for EfiReservedMemoryType.\r
570 //\r
571 // TODO: Check EFI_MEMORY_XP bit set or not once it's available in DXE GCD\r
572 // service.\r
573 //\r
574 Status = gDS->GetMemorySpaceDescriptor (Address, &MemDesc);\r
575 if (!EFI_ERROR (Status)) {\r
576 gDS->SetMemorySpaceAttributes (\r
577 Address,\r
578 ApSafeBufferSize,\r
579 MemDesc.Attributes & (~EFI_MEMORY_XP)\r
580 );\r
581 }\r
582\r
583 ApSafeBufferSize = EFI_PAGES_TO_SIZE (\r
584 EFI_SIZE_TO_PAGES (\r
585 CpuMpData->CpuCount * AP_SAFE_STACK_SIZE\r
586 )\r
587 );\r
588 Address = BASE_4GB - 1;\r
589 Status = gBS->AllocatePages (\r
590 AllocateMaxAddress,\r
591 EfiReservedMemoryType,\r
592 EFI_SIZE_TO_PAGES (ApSafeBufferSize),\r
593 &Address\r
594 );\r
595 ASSERT_EFI_ERROR (Status);\r
596\r
597 mReservedTopOfApStack = (UINTN)Address + ApSafeBufferSize;\r
598 ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) == 0);\r
599 CopyMem (\r
600 mReservedApLoop.Data,\r
601 CpuMpData->AddressMap.RelocateApLoopFuncAddress,\r
602 CpuMpData->AddressMap.RelocateApLoopFuncSize\r
603 );\r
604\r
605 Status = gBS->CreateEvent (\r
606 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
607 TPL_NOTIFY,\r
608 CheckApsStatus,\r
609 NULL,\r
610 &mCheckAllApsEvent\r
611 );\r
612 ASSERT_EFI_ERROR (Status);\r
613\r
614 //\r
615 // Set timer to check all APs status.\r
616 //\r
617 Status = gBS->SetTimer (\r
618 mCheckAllApsEvent,\r
619 TimerPeriodic,\r
620 EFI_TIMER_PERIOD_MICROSECONDS (\r
621 PcdGet32 (PcdCpuApStatusCheckIntervalInMicroSeconds)\r
622 )\r
623 );\r
624 ASSERT_EFI_ERROR (Status);\r
625\r
626 Status = gBS->CreateEvent (\r
627 EVT_SIGNAL_EXIT_BOOT_SERVICES,\r
628 TPL_CALLBACK,\r
629 MpInitChangeApLoopCallback,\r
630 NULL,\r
631 &mMpInitExitBootServicesEvent\r
632 );\r
633 ASSERT_EFI_ERROR (Status);\r
634\r
635 Status = gBS->CreateEventEx (\r
636 EVT_NOTIFY_SIGNAL,\r
637 TPL_CALLBACK,\r
638 MpInitChangeApLoopCallback,\r
639 NULL,\r
640 &gEfiEventLegacyBootGuid,\r
641 &mLegacyBootEvent\r
642 );\r
643 ASSERT_EFI_ERROR (Status);\r
644}\r
645\r
646/**\r
647 This service executes a caller provided function on all enabled APs.\r
648\r
649 @param[in] Procedure A pointer to the function to be run on\r
650 enabled APs of the system. See type\r
651 EFI_AP_PROCEDURE.\r
652 @param[in] SingleThread If TRUE, then all the enabled APs execute\r
653 the function specified by Procedure one by\r
654 one, in ascending order of processor handle\r
655 number. If FALSE, then all the enabled APs\r
656 execute the function specified by Procedure\r
657 simultaneously.\r
658 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
659 service. If it is NULL, then execute in\r
660 blocking mode. BSP waits until all APs finish\r
661 or TimeoutInMicroSeconds expires. If it's\r
662 not NULL, then execute in non-blocking mode.\r
663 BSP requests the function specified by\r
664 Procedure to be started on all the enabled\r
665 APs, and go on executing immediately. If\r
666 all return from Procedure, or TimeoutInMicroSeconds\r
667 expires, this event is signaled. The BSP\r
668 can use the CheckEvent() or WaitForEvent()\r
669 services to check the state of event. Type\r
670 EFI_EVENT is defined in CreateEvent() in\r
671 the Unified Extensible Firmware Interface\r
672 Specification.\r
673 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
674 APs to return from Procedure, either for\r
675 blocking or non-blocking mode. Zero means\r
676 infinity. If the timeout expires before\r
677 all APs return from Procedure, then Procedure\r
678 on the failed APs is terminated. All enabled\r
679 APs are available for next function assigned\r
680 by MpInitLibStartupAllAPs() or\r
681 MPInitLibStartupThisAP().\r
682 If the timeout expires in blocking mode,\r
683 BSP returns EFI_TIMEOUT. If the timeout\r
684 expires in non-blocking mode, WaitEvent\r
685 is signaled with SignalEvent().\r
686 @param[in] ProcedureArgument The parameter passed into Procedure for\r
687 all APs.\r
688 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,\r
689 if all APs finish successfully, then its\r
690 content is set to NULL. If not all APs\r
691 finish before timeout expires, then its\r
692 content is set to address of the buffer\r
693 holding handle numbers of the failed APs.\r
694 The buffer is allocated by MP Initialization\r
695 library, and it's the caller's responsibility to\r
696 free the buffer with FreePool() service.\r
697 In blocking mode, it is ready for consumption\r
698 when the call returns. In non-blocking mode,\r
699 it is ready when WaitEvent is signaled. The\r
700 list of failed CPU is terminated by\r
701 END_OF_CPU_LIST.\r
702\r
703 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
704 the timeout expired.\r
705 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
706 to all enabled APs.\r
707 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
708 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
709 signaled.\r
710 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not\r
711 supported.\r
712 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
713 @retval EFI_NOT_STARTED No enabled APs exist in the system.\r
714 @retval EFI_NOT_READY Any enabled APs are busy.\r
715 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
716 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
717 all enabled APs have finished.\r
718 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
719\r
720**/\r
721EFI_STATUS\r
722EFIAPI\r
723MpInitLibStartupAllAPs (\r
724 IN EFI_AP_PROCEDURE Procedure,\r
725 IN BOOLEAN SingleThread,\r
726 IN EFI_EVENT WaitEvent OPTIONAL,\r
727 IN UINTN TimeoutInMicroseconds,\r
728 IN VOID *ProcedureArgument OPTIONAL,\r
729 OUT UINTN **FailedCpuList OPTIONAL\r
730 )\r
731{\r
732 EFI_STATUS Status;\r
733\r
734 //\r
735 // Temporarily stop checkAllApsStatus for avoid resource dead-lock.\r
736 //\r
737 mStopCheckAllApsStatus = TRUE;\r
738\r
739 Status = StartupAllCPUsWorker (\r
740 Procedure,\r
741 SingleThread,\r
742 TRUE,\r
743 WaitEvent,\r
744 TimeoutInMicroseconds,\r
745 ProcedureArgument,\r
746 FailedCpuList\r
747 );\r
748\r
749 //\r
750 // Start checkAllApsStatus\r
751 //\r
752 mStopCheckAllApsStatus = FALSE;\r
753\r
754 return Status;\r
755}\r
756\r
757/**\r
758 This service lets the caller get one enabled AP to execute a caller-provided\r
759 function.\r
760\r
761 @param[in] Procedure A pointer to the function to be run on the\r
762 designated AP of the system. See type\r
763 EFI_AP_PROCEDURE.\r
764 @param[in] ProcessorNumber The handle number of the AP. The range is\r
765 from 0 to the total number of logical\r
766 processors minus 1. The total number of\r
767 logical processors can be retrieved by\r
768 MpInitLibGetNumberOfProcessors().\r
769 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
770 service. If it is NULL, then execute in\r
771 blocking mode. BSP waits until this AP finish\r
772 or TimeoutInMicroSeconds expires. If it's\r
773 not NULL, then execute in non-blocking mode.\r
774 BSP requests the function specified by\r
775 Procedure to be started on this AP,\r
776 and go on executing immediately. If this AP\r
777 return from Procedure or TimeoutInMicroSeconds\r
778 expires, this event is signaled. The BSP\r
779 can use the CheckEvent() or WaitForEvent()\r
780 services to check the state of event. Type\r
781 EFI_EVENT is defined in CreateEvent() in\r
782 the Unified Extensible Firmware Interface\r
783 Specification.\r
784 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
785 this AP to finish this Procedure, either for\r
786 blocking or non-blocking mode. Zero means\r
787 infinity. If the timeout expires before\r
788 this AP returns from Procedure, then Procedure\r
789 on the AP is terminated. The\r
790 AP is available for next function assigned\r
791 by MpInitLibStartupAllAPs() or\r
792 MpInitLibStartupThisAP().\r
793 If the timeout expires in blocking mode,\r
794 BSP returns EFI_TIMEOUT. If the timeout\r
795 expires in non-blocking mode, WaitEvent\r
796 is signaled with SignalEvent().\r
797 @param[in] ProcedureArgument The parameter passed into Procedure on the\r
798 specified AP.\r
799 @param[out] Finished If NULL, this parameter is ignored. In\r
800 blocking mode, this parameter is ignored.\r
801 In non-blocking mode, if AP returns from\r
802 Procedure before the timeout expires, its\r
803 content is set to TRUE. Otherwise, the\r
804 value is set to FALSE. The caller can\r
805 determine if the AP returned from Procedure\r
806 by evaluating this value.\r
807\r
808 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
809 the timeout expires.\r
810 @retval EFI_SUCCESS In non-blocking mode, the function has been\r
811 dispatched to specified AP.\r
812 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
813 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
814 signaled.\r
815 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not\r
816 supported.\r
817 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
818 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
819 the specified AP has finished.\r
820 @retval EFI_NOT_READY The specified AP is busy.\r
821 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
822 @retval EFI_NOT_FOUND The processor with the handle specified by\r
823 ProcessorNumber does not exist.\r
824 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
825 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
826\r
827**/\r
828EFI_STATUS\r
829EFIAPI\r
830MpInitLibStartupThisAP (\r
831 IN EFI_AP_PROCEDURE Procedure,\r
832 IN UINTN ProcessorNumber,\r
833 IN EFI_EVENT WaitEvent OPTIONAL,\r
834 IN UINTN TimeoutInMicroseconds,\r
835 IN VOID *ProcedureArgument OPTIONAL,\r
836 OUT BOOLEAN *Finished OPTIONAL\r
837 )\r
838{\r
839 EFI_STATUS Status;\r
840\r
841 //\r
842 // temporarily stop checkAllApsStatus for avoid resource dead-lock.\r
843 //\r
844 mStopCheckAllApsStatus = TRUE;\r
845\r
846 Status = StartupThisAPWorker (\r
847 Procedure,\r
848 ProcessorNumber,\r
849 WaitEvent,\r
850 TimeoutInMicroseconds,\r
851 ProcedureArgument,\r
852 Finished\r
853 );\r
854\r
855 mStopCheckAllApsStatus = FALSE;\r
856\r
857 return Status;\r
858}\r
859\r
860/**\r
861 This service switches the requested AP to be the BSP from that point onward.\r
862 This service changes the BSP for all purposes. This call can only be performed\r
863 by the current BSP.\r
864\r
865 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
866 BSP. The range is from 0 to the total number of\r
867 logical processors minus 1. The total number of\r
868 logical processors can be retrieved by\r
869 MpInitLibGetNumberOfProcessors().\r
870 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
871 enabled AP. Otherwise, it will be disabled.\r
872\r
873 @retval EFI_SUCCESS BSP successfully switched.\r
874 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to\r
875 this service returning.\r
876 @retval EFI_UNSUPPORTED Switching the BSP is not supported.\r
877 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
878 @retval EFI_NOT_FOUND The processor with the handle specified by\r
879 ProcessorNumber does not exist.\r
880 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or\r
881 a disabled AP.\r
882 @retval EFI_NOT_READY The specified AP is busy.\r
883 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
884\r
885**/\r
886EFI_STATUS\r
887EFIAPI\r
888MpInitLibSwitchBSP (\r
889 IN UINTN ProcessorNumber,\r
890 IN BOOLEAN EnableOldBSP\r
891 )\r
892{\r
893 EFI_STATUS Status;\r
894 EFI_TIMER_ARCH_PROTOCOL *Timer;\r
895 UINT64 TimerPeriod;\r
896\r
897 TimerPeriod = 0;\r
898 //\r
899 // Locate Timer Arch Protocol\r
900 //\r
901 Status = gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID **)&Timer);\r
902 if (EFI_ERROR (Status)) {\r
903 Timer = NULL;\r
904 }\r
905\r
906 if (Timer != NULL) {\r
907 //\r
908 // Save current rate of DXE Timer\r
909 //\r
910 Timer->GetTimerPeriod (Timer, &TimerPeriod);\r
911 //\r
912 // Disable DXE Timer and drain pending interrupts\r
913 //\r
914 Timer->SetTimerPeriod (Timer, 0);\r
915 }\r
916\r
917 Status = SwitchBSPWorker (ProcessorNumber, EnableOldBSP);\r
918\r
919 if (Timer != NULL) {\r
920 //\r
921 // Enable and restore rate of DXE Timer\r
922 //\r
923 Timer->SetTimerPeriod (Timer, TimerPeriod);\r
924 }\r
925\r
926 return Status;\r
927}\r
928\r
929/**\r
930 This service lets the caller enable or disable an AP from this point onward.\r
931 This service may only be called from the BSP.\r
932\r
933 @param[in] ProcessorNumber The handle number of AP.\r
934 The range is from 0 to the total number of\r
935 logical processors minus 1. The total number of\r
936 logical processors can be retrieved by\r
937 MpInitLibGetNumberOfProcessors().\r
938 @param[in] EnableAP Specifies the new state for the processor for\r
939 enabled, FALSE for disabled.\r
940 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
941 the new health status of the AP. This flag\r
942 corresponds to StatusFlag defined in\r
943 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r
944 the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
945 bits are ignored. If it is NULL, this parameter\r
946 is ignored.\r
947\r
948 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
949 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed\r
950 prior to this service returning.\r
951 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
952 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
953 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
954 does not exist.\r
955 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
956 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
957\r
958**/\r
959EFI_STATUS\r
960EFIAPI\r
961MpInitLibEnableDisableAP (\r
962 IN UINTN ProcessorNumber,\r
963 IN BOOLEAN EnableAP,\r
964 IN UINT32 *HealthFlag OPTIONAL\r
965 )\r
966{\r
967 EFI_STATUS Status;\r
968 BOOLEAN TempStopCheckState;\r
969\r
970 TempStopCheckState = FALSE;\r
971 //\r
972 // temporarily stop checkAllAPsStatus for initialize parameters.\r
973 //\r
974 if (!mStopCheckAllApsStatus) {\r
975 mStopCheckAllApsStatus = TRUE;\r
976 TempStopCheckState = TRUE;\r
977 }\r
978\r
979 Status = EnableDisableApWorker (ProcessorNumber, EnableAP, HealthFlag);\r
980\r
981 if (TempStopCheckState) {\r
982 mStopCheckAllApsStatus = FALSE;\r
983 }\r
984\r
985 return Status;\r
986}\r
987\r
988/**\r
989 This funtion will try to invoke platform specific microcode shadow logic to\r
990 relocate microcode update patches into memory.\r
991\r
992 @param[in, out] CpuMpData The pointer to CPU MP Data structure.\r
993\r
994 @retval EFI_SUCCESS Shadow microcode success.\r
995 @retval EFI_OUT_OF_RESOURCES No enough resource to complete the operation.\r
996 @retval EFI_UNSUPPORTED Can't find platform specific microcode shadow\r
997 PPI/Protocol.\r
998**/\r
999EFI_STATUS\r
1000PlatformShadowMicrocode (\r
1001 IN OUT CPU_MP_DATA *CpuMpData\r
1002 )\r
1003{\r
1004 //\r
1005 // There is no DXE version of platform shadow microcode protocol so far.\r
1006 // A platform which only uses DxeMpInitLib instance could only supports\r
1007 // the PCD based microcode shadowing.\r
1008 //\r
1009 return EFI_UNSUPPORTED;\r
1010}\r