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