]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
UefiCpuPkg/MpInitLib: Fix ASSERT for success.
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / DxeMpLib.c
1 /** @file
2 MP initialize support functions for DXE phase.
3
4 Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "MpLib.h"
16
17 #include <Library/UefiLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
19 #include <Library/DebugAgentLib.h>
20 #include <Library/DxeServicesTableLib.h>
21
22 #include <Protocol/Timer.h>
23
24 #define AP_CHECK_INTERVAL (EFI_TIMER_PERIOD_MILLISECONDS (100))
25 #define AP_SAFE_STACK_SIZE 128
26
27 CPU_MP_DATA *mCpuMpData = NULL;
28 EFI_EVENT mCheckAllApsEvent = NULL;
29 EFI_EVENT mMpInitExitBootServicesEvent = NULL;
30 EFI_EVENT mLegacyBootEvent = NULL;
31 volatile BOOLEAN mStopCheckAllApsStatus = TRUE;
32 VOID *mReservedApLoopFunc = NULL;
33 UINTN mReservedTopOfApStack;
34 volatile UINT32 mNumberToFinish = 0;
35
36 /**
37 Enable Debug Agent to support source debugging on AP function.
38
39 **/
40 VOID
41 EnableDebugAgent (
42 VOID
43 )
44 {
45 //
46 // Initialize Debug Agent to support source level debug in DXE phase
47 //
48 InitializeDebugAgent (DEBUG_AGENT_INIT_DXE_AP, NULL, NULL);
49 }
50
51 /**
52 Get the pointer to CPU MP Data structure.
53
54 @return The pointer to CPU MP Data structure.
55 **/
56 CPU_MP_DATA *
57 GetCpuMpData (
58 VOID
59 )
60 {
61 ASSERT (mCpuMpData != NULL);
62 return mCpuMpData;
63 }
64
65 /**
66 Save the pointer to CPU MP Data structure.
67
68 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
69 **/
70 VOID
71 SaveCpuMpData (
72 IN CPU_MP_DATA *CpuMpData
73 )
74 {
75 mCpuMpData = CpuMpData;
76 }
77
78 /**
79 Get available system memory below 1MB by specified size.
80
81 @param[in] WakeupBufferSize Wakeup buffer size required
82
83 @retval other Return wakeup buffer address below 1MB.
84 @retval -1 Cannot find free memory below 1MB.
85 **/
86 UINTN
87 GetWakeupBuffer (
88 IN UINTN WakeupBufferSize
89 )
90 {
91 EFI_STATUS Status;
92 EFI_PHYSICAL_ADDRESS StartAddress;
93
94 StartAddress = BASE_1MB;
95 Status = gBS->AllocatePages (
96 AllocateMaxAddress,
97 EfiBootServicesData,
98 EFI_SIZE_TO_PAGES (WakeupBufferSize),
99 &StartAddress
100 );
101 ASSERT_EFI_ERROR (Status);
102 if (!EFI_ERROR (Status)) {
103 Status = gBS->FreePages(
104 StartAddress,
105 EFI_SIZE_TO_PAGES (WakeupBufferSize)
106 );
107 ASSERT_EFI_ERROR (Status);
108 DEBUG ((DEBUG_INFO, "WakeupBufferStart = %x, WakeupBufferSize = %x\n",
109 (UINTN) StartAddress, WakeupBufferSize));
110 } else {
111 StartAddress = (EFI_PHYSICAL_ADDRESS) -1;
112 }
113 return (UINTN) StartAddress;
114 }
115
116 /**
117 Get available EfiBootServicesCode memory below 4GB by specified size.
118
119 This buffer is required to safely transfer AP from real address mode to
120 protected mode or long mode, due to the fact that the buffer returned by
121 GetWakeupBuffer() may be marked as non-executable.
122
123 @param[in] BufferSize Wakeup transition buffer size.
124
125 @retval other Return wakeup transition buffer address below 4GB.
126 @retval 0 Cannot find free memory below 4GB.
127 **/
128 UINTN
129 GetModeTransitionBuffer (
130 IN UINTN BufferSize
131 )
132 {
133 EFI_STATUS Status;
134 EFI_PHYSICAL_ADDRESS StartAddress;
135
136 StartAddress = BASE_4GB - 1;
137 Status = gBS->AllocatePages (
138 AllocateMaxAddress,
139 EfiBootServicesCode,
140 EFI_SIZE_TO_PAGES (BufferSize),
141 &StartAddress
142 );
143 if (EFI_ERROR (Status)) {
144 StartAddress = 0;
145 }
146
147 return (UINTN)StartAddress;
148 }
149
150 /**
151 Checks APs status and updates APs status if needed.
152
153 **/
154 VOID
155 CheckAndUpdateApsStatus (
156 VOID
157 )
158 {
159 UINTN ProcessorNumber;
160 EFI_STATUS Status;
161 CPU_MP_DATA *CpuMpData;
162
163 CpuMpData = GetCpuMpData ();
164
165 //
166 // First, check whether pending StartupAllAPs() exists.
167 //
168 if (CpuMpData->WaitEvent != NULL) {
169
170 Status = CheckAllAPs ();
171 //
172 // If all APs finish for StartupAllAPs(), signal the WaitEvent for it.
173 //
174 if (Status != EFI_NOT_READY) {
175 Status = gBS->SignalEvent (CpuMpData->WaitEvent);
176 CpuMpData->WaitEvent = NULL;
177 }
178 }
179
180 //
181 // Second, check whether pending StartupThisAPs() callings exist.
182 //
183 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
184
185 if (CpuMpData->CpuData[ProcessorNumber].WaitEvent == NULL) {
186 continue;
187 }
188
189 Status = CheckThisAP (ProcessorNumber);
190
191 if (Status != EFI_NOT_READY) {
192 gBS->SignalEvent (CpuMpData->CpuData[ProcessorNumber].WaitEvent);
193 CpuMpData->CpuData[ProcessorNumber].WaitEvent = NULL;
194 }
195 }
196 }
197
198 /**
199 Checks APs' status periodically.
200
201 This function is triggered by timer periodically to check the
202 state of APs for StartupAllAPs() and StartupThisAP() executed
203 in non-blocking mode.
204
205 @param[in] Event Event triggered.
206 @param[in] Context Parameter passed with the event.
207
208 **/
209 VOID
210 EFIAPI
211 CheckApsStatus (
212 IN EFI_EVENT Event,
213 IN VOID *Context
214 )
215 {
216 //
217 // If CheckApsStatus() is not stopped, otherwise return immediately.
218 //
219 if (!mStopCheckAllApsStatus) {
220 CheckAndUpdateApsStatus ();
221 }
222 }
223
224 /**
225 Get Protected mode code segment from current GDT table.
226
227 @return Protected mode code segment value.
228 **/
229 UINT16
230 GetProtectedModeCS (
231 VOID
232 )
233 {
234 IA32_DESCRIPTOR GdtrDesc;
235 IA32_SEGMENT_DESCRIPTOR *GdtEntry;
236 UINTN GdtEntryCount;
237 UINT16 Index;
238
239 AsmReadGdtr (&GdtrDesc);
240 GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);
241 GdtEntry = (IA32_SEGMENT_DESCRIPTOR *) GdtrDesc.Base;
242 for (Index = 0; Index < GdtEntryCount; Index++) {
243 if (GdtEntry->Bits.L == 0) {
244 if (GdtEntry->Bits.Type > 8 && GdtEntry->Bits.L == 0) {
245 break;
246 }
247 }
248 GdtEntry++;
249 }
250 ASSERT (Index != GdtEntryCount);
251 return Index * 8;
252 }
253
254 /**
255 Do sync on APs.
256
257 @param[in, out] Buffer Pointer to private data buffer.
258 **/
259 VOID
260 EFIAPI
261 RelocateApLoop (
262 IN OUT VOID *Buffer
263 )
264 {
265 CPU_MP_DATA *CpuMpData;
266 BOOLEAN MwaitSupport;
267 ASM_RELOCATE_AP_LOOP AsmRelocateApLoopFunc;
268 UINTN ProcessorNumber;
269
270 MpInitLibWhoAmI (&ProcessorNumber);
271 CpuMpData = GetCpuMpData ();
272 MwaitSupport = IsMwaitSupport ();
273 AsmRelocateApLoopFunc = (ASM_RELOCATE_AP_LOOP) (UINTN) mReservedApLoopFunc;
274 AsmRelocateApLoopFunc (
275 MwaitSupport,
276 CpuMpData->ApTargetCState,
277 CpuMpData->PmCodeSegment,
278 mReservedTopOfApStack - ProcessorNumber * AP_SAFE_STACK_SIZE,
279 (UINTN) &mNumberToFinish
280 );
281 //
282 // It should never reach here
283 //
284 ASSERT (FALSE);
285 }
286
287 /**
288 Callback function for ExitBootServices.
289
290 @param[in] Event Event whose notification function is being invoked.
291 @param[in] Context The pointer to the notification function's context,
292 which is implementation-dependent.
293
294 **/
295 VOID
296 EFIAPI
297 MpInitChangeApLoopCallback (
298 IN EFI_EVENT Event,
299 IN VOID *Context
300 )
301 {
302 CPU_MP_DATA *CpuMpData;
303
304 CpuMpData = GetCpuMpData ();
305 CpuMpData->PmCodeSegment = GetProtectedModeCS ();
306 CpuMpData->ApLoopMode = PcdGet8 (PcdCpuApLoopMode);
307 mNumberToFinish = CpuMpData->CpuCount - 1;
308 WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, NULL, TRUE);
309 while (mNumberToFinish > 0) {
310 CpuPause ();
311 }
312 DEBUG ((DEBUG_INFO, "%a() done!\n", __FUNCTION__));
313 }
314
315 /**
316 Initialize global data for MP support.
317
318 @param[in] CpuMpData The pointer to CPU MP Data structure.
319 **/
320 VOID
321 InitMpGlobalData (
322 IN CPU_MP_DATA *CpuMpData
323 )
324 {
325 EFI_STATUS Status;
326 EFI_PHYSICAL_ADDRESS Address;
327 UINTN ApSafeBufferSize;
328 UINTN Index;
329 EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc;
330 UINTN StackBase;
331 CPU_INFO_IN_HOB *CpuInfoInHob;
332
333 SaveCpuMpData (CpuMpData);
334
335 if (CpuMpData->CpuCount == 1) {
336 //
337 // If only BSP exists, return
338 //
339 return;
340 }
341
342 if (PcdGetBool (PcdCpuStackGuard)) {
343 //
344 // One extra page at the bottom of the stack is needed for Guard page.
345 //
346 if (CpuMpData->CpuApStackSize <= EFI_PAGE_SIZE) {
347 DEBUG ((DEBUG_ERROR, "PcdCpuApStackSize is not big enough for Stack Guard!\n"));
348 ASSERT (FALSE);
349 }
350
351 //
352 // DXE will reuse stack allocated for APs at PEI phase if it's available.
353 // Let's check it here.
354 //
355 // Note: BSP's stack guard is set at DxeIpl phase. But for the sake of
356 // BSP/AP exchange, stack guard for ApTopOfStack of cpu 0 will still be
357 // set here.
358 //
359 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;
360 for (Index = 0; Index < CpuMpData->CpuCount; ++Index) {
361 if (CpuInfoInHob != NULL && CpuInfoInHob[Index].ApTopOfStack != 0) {
362 StackBase = (UINTN)CpuInfoInHob[Index].ApTopOfStack - CpuMpData->CpuApStackSize;
363 } else {
364 StackBase = CpuMpData->Buffer + Index * CpuMpData->CpuApStackSize;
365 }
366
367 Status = gDS->GetMemorySpaceDescriptor (StackBase, &MemDesc);
368 ASSERT_EFI_ERROR (Status);
369
370 Status = gDS->SetMemorySpaceAttributes (
371 StackBase,
372 EFI_PAGES_TO_SIZE (1),
373 MemDesc.Attributes | EFI_MEMORY_RP
374 );
375 ASSERT_EFI_ERROR (Status);
376
377 DEBUG ((DEBUG_INFO, "Stack Guard set at %lx [cpu%lu]!\n",
378 (UINT64)StackBase, (UINT64)Index));
379 }
380 }
381
382 //
383 // Avoid APs access invalid buffer data which allocated by BootServices,
384 // so we will allocate reserved data for AP loop code. We also need to
385 // allocate this buffer below 4GB due to APs may be transferred to 32bit
386 // protected mode on long mode DXE.
387 // Allocating it in advance since memory services are not available in
388 // Exit Boot Services callback function.
389 //
390 ApSafeBufferSize = EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (
391 CpuMpData->AddressMap.RelocateApLoopFuncSize
392 ));
393 Address = BASE_4GB - 1;
394 Status = gBS->AllocatePages (
395 AllocateMaxAddress,
396 EfiReservedMemoryType,
397 EFI_SIZE_TO_PAGES (ApSafeBufferSize),
398 &Address
399 );
400 ASSERT_EFI_ERROR (Status);
401
402 mReservedApLoopFunc = (VOID *) (UINTN) Address;
403 ASSERT (mReservedApLoopFunc != NULL);
404
405 //
406 // Make sure that the buffer memory is executable if NX protection is enabled
407 // for EfiReservedMemoryType.
408 //
409 // TODO: Check EFI_MEMORY_XP bit set or not once it's available in DXE GCD
410 // service.
411 //
412 Status = gDS->GetMemorySpaceDescriptor (Address, &MemDesc);
413 if (!EFI_ERROR (Status)) {
414 gDS->SetMemorySpaceAttributes (
415 Address,
416 ApSafeBufferSize,
417 MemDesc.Attributes & (~EFI_MEMORY_XP)
418 );
419 }
420
421 ApSafeBufferSize = EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (
422 CpuMpData->CpuCount * AP_SAFE_STACK_SIZE
423 ));
424 Address = BASE_4GB - 1;
425 Status = gBS->AllocatePages (
426 AllocateMaxAddress,
427 EfiReservedMemoryType,
428 EFI_SIZE_TO_PAGES (ApSafeBufferSize),
429 &Address
430 );
431 ASSERT_EFI_ERROR (Status);
432
433 mReservedTopOfApStack = (UINTN) Address + ApSafeBufferSize;
434 ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) == 0);
435 CopyMem (
436 mReservedApLoopFunc,
437 CpuMpData->AddressMap.RelocateApLoopFuncAddress,
438 CpuMpData->AddressMap.RelocateApLoopFuncSize
439 );
440
441 Status = gBS->CreateEvent (
442 EVT_TIMER | EVT_NOTIFY_SIGNAL,
443 TPL_NOTIFY,
444 CheckApsStatus,
445 NULL,
446 &mCheckAllApsEvent
447 );
448 ASSERT_EFI_ERROR (Status);
449
450 //
451 // Set timer to check all APs status.
452 //
453 Status = gBS->SetTimer (
454 mCheckAllApsEvent,
455 TimerPeriodic,
456 AP_CHECK_INTERVAL
457 );
458 ASSERT_EFI_ERROR (Status);
459
460 Status = gBS->CreateEvent (
461 EVT_SIGNAL_EXIT_BOOT_SERVICES,
462 TPL_CALLBACK,
463 MpInitChangeApLoopCallback,
464 NULL,
465 &mMpInitExitBootServicesEvent
466 );
467 ASSERT_EFI_ERROR (Status);
468
469 Status = gBS->CreateEventEx (
470 EVT_NOTIFY_SIGNAL,
471 TPL_CALLBACK,
472 MpInitChangeApLoopCallback,
473 NULL,
474 &gEfiEventLegacyBootGuid,
475 &mLegacyBootEvent
476 );
477 ASSERT_EFI_ERROR (Status);
478 }
479
480 /**
481 This service executes a caller provided function on all enabled APs.
482
483 @param[in] Procedure A pointer to the function to be run on
484 enabled APs of the system. See type
485 EFI_AP_PROCEDURE.
486 @param[in] SingleThread If TRUE, then all the enabled APs execute
487 the function specified by Procedure one by
488 one, in ascending order of processor handle
489 number. If FALSE, then all the enabled APs
490 execute the function specified by Procedure
491 simultaneously.
492 @param[in] WaitEvent The event created by the caller with CreateEvent()
493 service. If it is NULL, then execute in
494 blocking mode. BSP waits until all APs finish
495 or TimeoutInMicroSeconds expires. If it's
496 not NULL, then execute in non-blocking mode.
497 BSP requests the function specified by
498 Procedure to be started on all the enabled
499 APs, and go on executing immediately. If
500 all return from Procedure, or TimeoutInMicroSeconds
501 expires, this event is signaled. The BSP
502 can use the CheckEvent() or WaitForEvent()
503 services to check the state of event. Type
504 EFI_EVENT is defined in CreateEvent() in
505 the Unified Extensible Firmware Interface
506 Specification.
507 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
508 APs to return from Procedure, either for
509 blocking or non-blocking mode. Zero means
510 infinity. If the timeout expires before
511 all APs return from Procedure, then Procedure
512 on the failed APs is terminated. All enabled
513 APs are available for next function assigned
514 by MpInitLibStartupAllAPs() or
515 MPInitLibStartupThisAP().
516 If the timeout expires in blocking mode,
517 BSP returns EFI_TIMEOUT. If the timeout
518 expires in non-blocking mode, WaitEvent
519 is signaled with SignalEvent().
520 @param[in] ProcedureArgument The parameter passed into Procedure for
521 all APs.
522 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
523 if all APs finish successfully, then its
524 content is set to NULL. If not all APs
525 finish before timeout expires, then its
526 content is set to address of the buffer
527 holding handle numbers of the failed APs.
528 The buffer is allocated by MP Initialization
529 library, and it's the caller's responsibility to
530 free the buffer with FreePool() service.
531 In blocking mode, it is ready for consumption
532 when the call returns. In non-blocking mode,
533 it is ready when WaitEvent is signaled. The
534 list of failed CPU is terminated by
535 END_OF_CPU_LIST.
536
537 @retval EFI_SUCCESS In blocking mode, all APs have finished before
538 the timeout expired.
539 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
540 to all enabled APs.
541 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
542 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
543 signaled.
544 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
545 supported.
546 @retval EFI_DEVICE_ERROR Caller processor is AP.
547 @retval EFI_NOT_STARTED No enabled APs exist in the system.
548 @retval EFI_NOT_READY Any enabled APs are busy.
549 @retval EFI_NOT_READY MP Initialize Library is not initialized.
550 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
551 all enabled APs have finished.
552 @retval EFI_INVALID_PARAMETER Procedure is NULL.
553
554 **/
555 EFI_STATUS
556 EFIAPI
557 MpInitLibStartupAllAPs (
558 IN EFI_AP_PROCEDURE Procedure,
559 IN BOOLEAN SingleThread,
560 IN EFI_EVENT WaitEvent OPTIONAL,
561 IN UINTN TimeoutInMicroseconds,
562 IN VOID *ProcedureArgument OPTIONAL,
563 OUT UINTN **FailedCpuList OPTIONAL
564 )
565 {
566 EFI_STATUS Status;
567
568 //
569 // Temporarily stop checkAllApsStatus for avoid resource dead-lock.
570 //
571 mStopCheckAllApsStatus = TRUE;
572
573 Status = StartupAllAPsWorker (
574 Procedure,
575 SingleThread,
576 WaitEvent,
577 TimeoutInMicroseconds,
578 ProcedureArgument,
579 FailedCpuList
580 );
581
582 //
583 // Start checkAllApsStatus
584 //
585 mStopCheckAllApsStatus = FALSE;
586
587 return Status;
588 }
589
590 /**
591 This service lets the caller get one enabled AP to execute a caller-provided
592 function.
593
594 @param[in] Procedure A pointer to the function to be run on the
595 designated AP of the system. See type
596 EFI_AP_PROCEDURE.
597 @param[in] ProcessorNumber The handle number of the AP. The range is
598 from 0 to the total number of logical
599 processors minus 1. The total number of
600 logical processors can be retrieved by
601 MpInitLibGetNumberOfProcessors().
602 @param[in] WaitEvent The event created by the caller with CreateEvent()
603 service. If it is NULL, then execute in
604 blocking mode. BSP waits until this AP finish
605 or TimeoutInMicroSeconds expires. If it's
606 not NULL, then execute in non-blocking mode.
607 BSP requests the function specified by
608 Procedure to be started on this AP,
609 and go on executing immediately. If this AP
610 return from Procedure or TimeoutInMicroSeconds
611 expires, this event is signaled. The BSP
612 can use the CheckEvent() or WaitForEvent()
613 services to check the state of event. Type
614 EFI_EVENT is defined in CreateEvent() in
615 the Unified Extensible Firmware Interface
616 Specification.
617 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
618 this AP to finish this Procedure, either for
619 blocking or non-blocking mode. Zero means
620 infinity. If the timeout expires before
621 this AP returns from Procedure, then Procedure
622 on the AP is terminated. The
623 AP is available for next function assigned
624 by MpInitLibStartupAllAPs() or
625 MpInitLibStartupThisAP().
626 If the timeout expires in blocking mode,
627 BSP returns EFI_TIMEOUT. If the timeout
628 expires in non-blocking mode, WaitEvent
629 is signaled with SignalEvent().
630 @param[in] ProcedureArgument The parameter passed into Procedure on the
631 specified AP.
632 @param[out] Finished If NULL, this parameter is ignored. In
633 blocking mode, this parameter is ignored.
634 In non-blocking mode, if AP returns from
635 Procedure before the timeout expires, its
636 content is set to TRUE. Otherwise, the
637 value is set to FALSE. The caller can
638 determine if the AP returned from Procedure
639 by evaluating this value.
640
641 @retval EFI_SUCCESS In blocking mode, specified AP finished before
642 the timeout expires.
643 @retval EFI_SUCCESS In non-blocking mode, the function has been
644 dispatched to specified AP.
645 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
646 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
647 signaled.
648 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
649 supported.
650 @retval EFI_DEVICE_ERROR The calling processor is an AP.
651 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
652 the specified AP has finished.
653 @retval EFI_NOT_READY The specified AP is busy.
654 @retval EFI_NOT_READY MP Initialize Library is not initialized.
655 @retval EFI_NOT_FOUND The processor with the handle specified by
656 ProcessorNumber does not exist.
657 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
658 @retval EFI_INVALID_PARAMETER Procedure is NULL.
659
660 **/
661 EFI_STATUS
662 EFIAPI
663 MpInitLibStartupThisAP (
664 IN EFI_AP_PROCEDURE Procedure,
665 IN UINTN ProcessorNumber,
666 IN EFI_EVENT WaitEvent OPTIONAL,
667 IN UINTN TimeoutInMicroseconds,
668 IN VOID *ProcedureArgument OPTIONAL,
669 OUT BOOLEAN *Finished OPTIONAL
670 )
671 {
672 EFI_STATUS Status;
673
674 //
675 // temporarily stop checkAllApsStatus for avoid resource dead-lock.
676 //
677 mStopCheckAllApsStatus = TRUE;
678
679 Status = StartupThisAPWorker (
680 Procedure,
681 ProcessorNumber,
682 WaitEvent,
683 TimeoutInMicroseconds,
684 ProcedureArgument,
685 Finished
686 );
687
688 mStopCheckAllApsStatus = FALSE;
689
690 return Status;
691 }
692
693 /**
694 This service switches the requested AP to be the BSP from that point onward.
695 This service changes the BSP for all purposes. This call can only be performed
696 by the current BSP.
697
698 @param[in] ProcessorNumber The handle number of AP that is to become the new
699 BSP. The range is from 0 to the total number of
700 logical processors minus 1. The total number of
701 logical processors can be retrieved by
702 MpInitLibGetNumberOfProcessors().
703 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
704 enabled AP. Otherwise, it will be disabled.
705
706 @retval EFI_SUCCESS BSP successfully switched.
707 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
708 this service returning.
709 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
710 @retval EFI_DEVICE_ERROR The calling processor is an AP.
711 @retval EFI_NOT_FOUND The processor with the handle specified by
712 ProcessorNumber does not exist.
713 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
714 a disabled AP.
715 @retval EFI_NOT_READY The specified AP is busy.
716 @retval EFI_NOT_READY MP Initialize Library is not initialized.
717
718 **/
719 EFI_STATUS
720 EFIAPI
721 MpInitLibSwitchBSP (
722 IN UINTN ProcessorNumber,
723 IN BOOLEAN EnableOldBSP
724 )
725 {
726 EFI_STATUS Status;
727 EFI_TIMER_ARCH_PROTOCOL *Timer;
728 UINT64 TimerPeriod;
729
730 TimerPeriod = 0;
731 //
732 // Locate Timer Arch Protocol
733 //
734 Status = gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID **) &Timer);
735 if (EFI_ERROR (Status)) {
736 Timer = NULL;
737 }
738
739 if (Timer != NULL) {
740 //
741 // Save current rate of DXE Timer
742 //
743 Timer->GetTimerPeriod (Timer, &TimerPeriod);
744 //
745 // Disable DXE Timer and drain pending interrupts
746 //
747 Timer->SetTimerPeriod (Timer, 0);
748 }
749
750 Status = SwitchBSPWorker (ProcessorNumber, EnableOldBSP);
751
752 if (Timer != NULL) {
753 //
754 // Enable and restore rate of DXE Timer
755 //
756 Timer->SetTimerPeriod (Timer, TimerPeriod);
757 }
758
759 return Status;
760 }
761
762 /**
763 This service lets the caller enable or disable an AP from this point onward.
764 This service may only be called from the BSP.
765
766 @param[in] ProcessorNumber The handle number of AP.
767 The range is from 0 to the total number of
768 logical processors minus 1. The total number of
769 logical processors can be retrieved by
770 MpInitLibGetNumberOfProcessors().
771 @param[in] EnableAP Specifies the new state for the processor for
772 enabled, FALSE for disabled.
773 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
774 the new health status of the AP. This flag
775 corresponds to StatusFlag defined in
776 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
777 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
778 bits are ignored. If it is NULL, this parameter
779 is ignored.
780
781 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
782 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
783 prior to this service returning.
784 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
785 @retval EFI_DEVICE_ERROR The calling processor is an AP.
786 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
787 does not exist.
788 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
789 @retval EFI_NOT_READY MP Initialize Library is not initialized.
790
791 **/
792 EFI_STATUS
793 EFIAPI
794 MpInitLibEnableDisableAP (
795 IN UINTN ProcessorNumber,
796 IN BOOLEAN EnableAP,
797 IN UINT32 *HealthFlag OPTIONAL
798 )
799 {
800 EFI_STATUS Status;
801 BOOLEAN TempStopCheckState;
802
803 TempStopCheckState = FALSE;
804 //
805 // temporarily stop checkAllAPsStatus for initialize parameters.
806 //
807 if (!mStopCheckAllApsStatus) {
808 mStopCheckAllApsStatus = TRUE;
809 TempStopCheckState = TRUE;
810 }
811
812 Status = EnableDisableApWorker (ProcessorNumber, EnableAP, HealthFlag);
813
814 if (TempStopCheckState) {
815 mStopCheckAllApsStatus = FALSE;
816 }
817
818 return Status;
819 }