]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
UefiCpuPkg/MpInitLib: Place APs in safe loop before hand-off to OS
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / DxeMpLib.c
1 /** @file
2 MP initialize support functions for DXE phase.
3
4 Copyright (c) 2016, 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
20 #define AP_CHECK_INTERVAL (EFI_TIMER_PERIOD_MILLISECONDS (100))
21
22 CPU_MP_DATA *mCpuMpData = NULL;
23 EFI_EVENT mCheckAllApsEvent = NULL;
24 EFI_EVENT mMpInitExitBootServicesEvent = NULL;
25 volatile BOOLEAN mStopCheckAllApsStatus = TRUE;
26
27
28 /**
29 Get the pointer to CPU MP Data structure.
30
31 @return The pointer to CPU MP Data structure.
32 **/
33 CPU_MP_DATA *
34 GetCpuMpData (
35 VOID
36 )
37 {
38 ASSERT (mCpuMpData != NULL);
39 return mCpuMpData;
40 }
41
42 /**
43 Save the pointer to CPU MP Data structure.
44
45 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
46 **/
47 VOID
48 SaveCpuMpData (
49 IN CPU_MP_DATA *CpuMpData
50 )
51 {
52 mCpuMpData = CpuMpData;
53 }
54
55 /**
56 Allocate reset vector buffer.
57
58 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
59 **/
60 VOID
61 AllocateResetVector (
62 IN OUT CPU_MP_DATA *CpuMpData
63 )
64 {
65 EFI_STATUS Status;
66 UINTN ApResetVectorSize;
67 EFI_PHYSICAL_ADDRESS StartAddress;
68
69 ApResetVectorSize = CpuMpData->AddressMap.RendezvousFunnelSize +
70 sizeof (MP_CPU_EXCHANGE_INFO);
71
72 StartAddress = BASE_1MB;
73 Status = gBS->AllocatePages (
74 AllocateMaxAddress,
75 EfiACPIMemoryNVS,
76 EFI_SIZE_TO_PAGES (ApResetVectorSize),
77 &StartAddress
78 );
79 ASSERT_EFI_ERROR (Status);
80
81 CpuMpData->WakeupBuffer = (UINTN) StartAddress;
82 CpuMpData->MpCpuExchangeInfo = (MP_CPU_EXCHANGE_INFO *) (UINTN)
83 (CpuMpData->WakeupBuffer + CpuMpData->AddressMap.RendezvousFunnelSize);
84 //
85 // copy AP reset code in it
86 //
87 CopyMem (
88 (VOID *) CpuMpData->WakeupBuffer,
89 (VOID *) CpuMpData->AddressMap.RendezvousFunnelAddress,
90 CpuMpData->AddressMap.RendezvousFunnelSize
91 );
92 }
93
94 /**
95 Free AP reset vector buffer.
96
97 @param[in] CpuMpData The pointer to CPU MP Data structure.
98 **/
99 VOID
100 FreeResetVector (
101 IN CPU_MP_DATA *CpuMpData
102 )
103 {
104 EFI_STATUS Status;
105 UINTN ApResetVectorSize;
106 ApResetVectorSize = CpuMpData->AddressMap.RendezvousFunnelSize +
107 sizeof (MP_CPU_EXCHANGE_INFO);
108 Status = gBS->FreePages(
109 (EFI_PHYSICAL_ADDRESS)CpuMpData->WakeupBuffer,
110 EFI_SIZE_TO_PAGES (ApResetVectorSize)
111 );
112 ASSERT_EFI_ERROR (Status);
113 }
114
115 /**
116 Checks APs status and updates APs status if needed.
117
118 **/
119 VOID
120 CheckAndUpdateApsStatus (
121 VOID
122 )
123 {
124 UINTN ProcessorNumber;
125 EFI_STATUS Status;
126 CPU_MP_DATA *CpuMpData;
127
128 CpuMpData = GetCpuMpData ();
129
130 //
131 // First, check whether pending StartupAllAPs() exists.
132 //
133 if (CpuMpData->WaitEvent != NULL) {
134
135 Status = CheckAllAPs ();
136 //
137 // If all APs finish for StartupAllAPs(), signal the WaitEvent for it.
138 //
139 if (Status != EFI_NOT_READY) {
140 Status = gBS->SignalEvent (CpuMpData->WaitEvent);
141 CpuMpData->WaitEvent = NULL;
142 }
143 }
144
145 //
146 // Second, check whether pending StartupThisAPs() callings exist.
147 //
148 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
149
150 if (CpuMpData->CpuData[ProcessorNumber].WaitEvent == NULL) {
151 continue;
152 }
153
154 Status = CheckThisAP (ProcessorNumber);
155
156 if (Status != EFI_NOT_READY) {
157 gBS->SignalEvent (CpuMpData->CpuData[ProcessorNumber].WaitEvent);
158 CpuMpData->CpuData[ProcessorNumber].WaitEvent = NULL;
159 }
160 }
161 }
162
163 /**
164 Checks APs' status periodically.
165
166 This function is triggerred by timer perodically to check the
167 state of APs for StartupAllAPs() and StartupThisAP() executed
168 in non-blocking mode.
169
170 @param[in] Event Event triggered.
171 @param[in] Context Parameter passed with the event.
172
173 **/
174 VOID
175 EFIAPI
176 CheckApsStatus (
177 IN EFI_EVENT Event,
178 IN VOID *Context
179 )
180 {
181 //
182 // If CheckApsStatus() is not stopped, otherwise return immediately.
183 //
184 if (!mStopCheckAllApsStatus) {
185 CheckAndUpdateApsStatus ();
186 }
187 }
188
189 /**
190 Get Protected mode code segment from current GDT table.
191
192 @returen Protected mode code segment value.
193 **/
194 UINT16
195 GetProtectedModeCS (
196 VOID
197 )
198 {
199 IA32_DESCRIPTOR GdtrDesc;
200 IA32_SEGMENT_DESCRIPTOR *GdtEntry;
201 UINTN GdtEntryCount;
202 UINT16 Index;
203
204 Index = (UINT16) -1;
205 AsmReadGdtr (&GdtrDesc);
206 GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);
207 GdtEntry = (IA32_SEGMENT_DESCRIPTOR *) GdtrDesc.Base;
208 for (Index = 0; Index < GdtEntryCount; Index++) {
209 if (GdtEntry->Bits.L == 0) {
210 if (GdtEntry->Bits.Type > 8 && GdtEntry->Bits.L == 0) {
211 break;
212 }
213 }
214 GdtEntry++;
215 }
216 ASSERT (Index != -1);
217 return Index * 8;
218 }
219
220 /**
221 Do sync on APs.
222
223 @param[in, out] Buffer Pointer to private data buffer.
224 **/
225 VOID
226 EFIAPI
227 RelocateApLoop (
228 IN OUT VOID *Buffer
229 )
230 {
231 CPU_MP_DATA *CpuMpData;
232 BOOLEAN MwaitSupport;
233 ASM_RELOCATE_AP_LOOP AsmRelocateApLoopFunc;
234
235 CpuMpData = GetCpuMpData ();
236 MwaitSupport = IsMwaitSupport ();
237 AsmRelocateApLoopFunc = (ASM_RELOCATE_AP_LOOP) (UINTN) Buffer;
238 AsmRelocateApLoopFunc (MwaitSupport, CpuMpData->ApTargetCState, CpuMpData->PmCodeSegment);
239 //
240 // It should never reach here
241 //
242 ASSERT (FALSE);
243 }
244
245 /**
246 Callback function for ExitBootServices.
247
248 @param[in] Event Event whose notification function is being invoked.
249 @param[in] Context The pointer to the notification function's context,
250 which is implementation-dependent.
251
252 **/
253 VOID
254 EFIAPI
255 MpInitExitBootServicesCallback (
256 IN EFI_EVENT Event,
257 IN VOID *Context
258 )
259 {
260 CPU_MP_DATA *CpuMpData;
261 VOID *ReservedApLoopFunc;
262 //
263 // Avoid APs access invalid buff data which allocated by BootServices,
264 // so we will allocate reserved data for AP loop code.
265 //
266 CpuMpData = GetCpuMpData ();
267 CpuMpData->PmCodeSegment = GetProtectedModeCS ();
268 CpuMpData->ApLoopMode = PcdGet8 (PcdCpuApLoopMode);
269 ReservedApLoopFunc = AllocateReservedCopyPool (
270 CpuMpData->AddressMap.RelocateApLoopFuncSize,
271 CpuMpData->AddressMap.RelocateApLoopFuncAddress
272 );
273 WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, ReservedApLoopFunc);
274 DEBUG ((DEBUG_INFO, "MpInitExitBootServicesCallback() done!\n"));
275 }
276
277 /**
278 Initialize global data for MP support.
279
280 @param[in] CpuMpData The pointer to CPU MP Data structure.
281 **/
282 VOID
283 InitMpGlobalData (
284 IN CPU_MP_DATA *CpuMpData
285 )
286 {
287 EFI_STATUS Status;
288
289 SaveCpuMpData (CpuMpData);
290
291 Status = gBS->CreateEvent (
292 EVT_TIMER | EVT_NOTIFY_SIGNAL,
293 TPL_NOTIFY,
294 CheckApsStatus,
295 NULL,
296 &mCheckAllApsEvent
297 );
298 ASSERT_EFI_ERROR (Status);
299
300 //
301 // Set timer to check all APs status.
302 //
303 Status = gBS->SetTimer (
304 mCheckAllApsEvent,
305 TimerPeriodic,
306 AP_CHECK_INTERVAL
307 );
308 ASSERT_EFI_ERROR (Status);
309 Status = gBS->CreateEvent (
310 EVT_SIGNAL_EXIT_BOOT_SERVICES,
311 TPL_CALLBACK,
312 MpInitExitBootServicesCallback,
313 NULL,
314 &mMpInitExitBootServicesEvent
315 );
316 ASSERT_EFI_ERROR (Status);
317 }
318
319 /**
320 This service executes a caller provided function on all enabled APs.
321
322 @param[in] Procedure A pointer to the function to be run on
323 enabled APs of the system. See type
324 EFI_AP_PROCEDURE.
325 @param[in] SingleThread If TRUE, then all the enabled APs execute
326 the function specified by Procedure one by
327 one, in ascending order of processor handle
328 number. If FALSE, then all the enabled APs
329 execute the function specified by Procedure
330 simultaneously.
331 @param[in] WaitEvent The event created by the caller with CreateEvent()
332 service. If it is NULL, then execute in
333 blocking mode. BSP waits until all APs finish
334 or TimeoutInMicroSeconds expires. If it's
335 not NULL, then execute in non-blocking mode.
336 BSP requests the function specified by
337 Procedure to be started on all the enabled
338 APs, and go on executing immediately. If
339 all return from Procedure, or TimeoutInMicroSeconds
340 expires, this event is signaled. The BSP
341 can use the CheckEvent() or WaitForEvent()
342 services to check the state of event. Type
343 EFI_EVENT is defined in CreateEvent() in
344 the Unified Extensible Firmware Interface
345 Specification.
346 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
347 APs to return from Procedure, either for
348 blocking or non-blocking mode. Zero means
349 infinity. If the timeout expires before
350 all APs return from Procedure, then Procedure
351 on the failed APs is terminated. All enabled
352 APs are available for next function assigned
353 by MpInitLibStartupAllAPs() or
354 MPInitLibStartupThisAP().
355 If the timeout expires in blocking mode,
356 BSP returns EFI_TIMEOUT. If the timeout
357 expires in non-blocking mode, WaitEvent
358 is signaled with SignalEvent().
359 @param[in] ProcedureArgument The parameter passed into Procedure for
360 all APs.
361 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
362 if all APs finish successfully, then its
363 content is set to NULL. If not all APs
364 finish before timeout expires, then its
365 content is set to address of the buffer
366 holding handle numbers of the failed APs.
367 The buffer is allocated by MP Initialization
368 library, and it's the caller's responsibility to
369 free the buffer with FreePool() service.
370 In blocking mode, it is ready for consumption
371 when the call returns. In non-blocking mode,
372 it is ready when WaitEvent is signaled. The
373 list of failed CPU is terminated by
374 END_OF_CPU_LIST.
375
376 @retval EFI_SUCCESS In blocking mode, all APs have finished before
377 the timeout expired.
378 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
379 to all enabled APs.
380 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
381 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
382 signaled.
383 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
384 supported.
385 @retval EFI_DEVICE_ERROR Caller processor is AP.
386 @retval EFI_NOT_STARTED No enabled APs exist in the system.
387 @retval EFI_NOT_READY Any enabled APs are busy.
388 @retval EFI_NOT_READY MP Initialize Library is not initialized.
389 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
390 all enabled APs have finished.
391 @retval EFI_INVALID_PARAMETER Procedure is NULL.
392
393 **/
394 EFI_STATUS
395 EFIAPI
396 MpInitLibStartupAllAPs (
397 IN EFI_AP_PROCEDURE Procedure,
398 IN BOOLEAN SingleThread,
399 IN EFI_EVENT WaitEvent OPTIONAL,
400 IN UINTN TimeoutInMicroseconds,
401 IN VOID *ProcedureArgument OPTIONAL,
402 OUT UINTN **FailedCpuList OPTIONAL
403 )
404 {
405 EFI_STATUS Status;
406
407 //
408 // Temporarily stop checkAllApsStatus for avoid resource dead-lock.
409 //
410 mStopCheckAllApsStatus = TRUE;
411
412 Status = StartupAllAPsWorker (
413 Procedure,
414 SingleThread,
415 WaitEvent,
416 TimeoutInMicroseconds,
417 ProcedureArgument,
418 FailedCpuList
419 );
420
421 //
422 // Start checkAllApsStatus
423 //
424 mStopCheckAllApsStatus = FALSE;
425
426 return Status;
427 }
428
429 /**
430 This service lets the caller get one enabled AP to execute a caller-provided
431 function.
432
433 @param[in] Procedure A pointer to the function to be run on the
434 designated AP of the system. See type
435 EFI_AP_PROCEDURE.
436 @param[in] ProcessorNumber The handle number of the AP. The range is
437 from 0 to the total number of logical
438 processors minus 1. The total number of
439 logical processors can be retrieved by
440 MpInitLibGetNumberOfProcessors().
441 @param[in] WaitEvent The event created by the caller with CreateEvent()
442 service. If it is NULL, then execute in
443 blocking mode. BSP waits until this AP finish
444 or TimeoutInMicroSeconds expires. If it's
445 not NULL, then execute in non-blocking mode.
446 BSP requests the function specified by
447 Procedure to be started on this AP,
448 and go on executing immediately. If this AP
449 return from Procedure or TimeoutInMicroSeconds
450 expires, this event is signaled. The BSP
451 can use the CheckEvent() or WaitForEvent()
452 services to check the state of event. Type
453 EFI_EVENT is defined in CreateEvent() in
454 the Unified Extensible Firmware Interface
455 Specification.
456 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
457 this AP to finish this Procedure, either for
458 blocking or non-blocking mode. Zero means
459 infinity. If the timeout expires before
460 this AP returns from Procedure, then Procedure
461 on the AP is terminated. The
462 AP is available for next function assigned
463 by MpInitLibStartupAllAPs() or
464 MpInitLibStartupThisAP().
465 If the timeout expires in blocking mode,
466 BSP returns EFI_TIMEOUT. If the timeout
467 expires in non-blocking mode, WaitEvent
468 is signaled with SignalEvent().
469 @param[in] ProcedureArgument The parameter passed into Procedure on the
470 specified AP.
471 @param[out] Finished If NULL, this parameter is ignored. In
472 blocking mode, this parameter is ignored.
473 In non-blocking mode, if AP returns from
474 Procedure before the timeout expires, its
475 content is set to TRUE. Otherwise, the
476 value is set to FALSE. The caller can
477 determine if the AP returned from Procedure
478 by evaluating this value.
479
480 @retval EFI_SUCCESS In blocking mode, specified AP finished before
481 the timeout expires.
482 @retval EFI_SUCCESS In non-blocking mode, the function has been
483 dispatched to specified AP.
484 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
485 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
486 signaled.
487 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
488 supported.
489 @retval EFI_DEVICE_ERROR The calling processor is an AP.
490 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
491 the specified AP has finished.
492 @retval EFI_NOT_READY The specified AP is busy.
493 @retval EFI_NOT_READY MP Initialize Library is not initialized.
494 @retval EFI_NOT_FOUND The processor with the handle specified by
495 ProcessorNumber does not exist.
496 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
497 @retval EFI_INVALID_PARAMETER Procedure is NULL.
498
499 **/
500 EFI_STATUS
501 EFIAPI
502 MpInitLibStartupThisAP (
503 IN EFI_AP_PROCEDURE Procedure,
504 IN UINTN ProcessorNumber,
505 IN EFI_EVENT WaitEvent OPTIONAL,
506 IN UINTN TimeoutInMicroseconds,
507 IN VOID *ProcedureArgument OPTIONAL,
508 OUT BOOLEAN *Finished OPTIONAL
509 )
510 {
511 EFI_STATUS Status;
512
513 //
514 // temporarily stop checkAllApsStatus for avoid resource dead-lock.
515 //
516 mStopCheckAllApsStatus = TRUE;
517
518 Status = StartupThisAPWorker (
519 Procedure,
520 ProcessorNumber,
521 WaitEvent,
522 TimeoutInMicroseconds,
523 ProcedureArgument,
524 Finished
525 );
526
527 mStopCheckAllApsStatus = FALSE;
528
529 return Status;
530 }
531
532 /**
533 This service switches the requested AP to be the BSP from that point onward.
534 This service changes the BSP for all purposes. This call can only be performed
535 by the current BSP.
536
537 @param[in] ProcessorNumber The handle number of AP that is to become the new
538 BSP. The range is from 0 to the total number of
539 logical processors minus 1. The total number of
540 logical processors can be retrieved by
541 MpInitLibGetNumberOfProcessors().
542 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
543 enabled AP. Otherwise, it will be disabled.
544
545 @retval EFI_SUCCESS BSP successfully switched.
546 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
547 this service returning.
548 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
549 @retval EFI_DEVICE_ERROR The calling processor is an AP.
550 @retval EFI_NOT_FOUND The processor with the handle specified by
551 ProcessorNumber does not exist.
552 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
553 a disabled AP.
554 @retval EFI_NOT_READY The specified AP is busy.
555 @retval EFI_NOT_READY MP Initialize Library is not initialized.
556
557 **/
558 EFI_STATUS
559 EFIAPI
560 MpInitLibSwitchBSP (
561 IN UINTN ProcessorNumber,
562 IN BOOLEAN EnableOldBSP
563 )
564 {
565 EFI_STATUS Status;
566 BOOLEAN OldInterruptState;
567
568 //
569 // Before send both BSP and AP to a procedure to exchange their roles,
570 // interrupt must be disabled. This is because during the exchange role
571 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
572 // be corrupted, since interrupt return address will be pushed to stack
573 // by hardware.
574 //
575 OldInterruptState = SaveAndDisableInterrupts ();
576
577 //
578 // Mask LINT0 & LINT1 for the old BSP
579 //
580 DisableLvtInterrupts ();
581
582 Status = SwitchBSPWorker (ProcessorNumber, EnableOldBSP);
583
584 //
585 // Restore interrupt state.
586 //
587 SetInterruptState (OldInterruptState);
588
589 return Status;
590 }
591
592 /**
593 This service lets the caller enable or disable an AP from this point onward.
594 This service may only be called from the BSP.
595
596 @param[in] ProcessorNumber The handle number of AP.
597 The range is from 0 to the total number of
598 logical processors minus 1. The total number of
599 logical processors can be retrieved by
600 MpInitLibGetNumberOfProcessors().
601 @param[in] EnableAP Specifies the new state for the processor for
602 enabled, FALSE for disabled.
603 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
604 the new health status of the AP. This flag
605 corresponds to StatusFlag defined in
606 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
607 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
608 bits are ignored. If it is NULL, this parameter
609 is ignored.
610
611 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
612 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
613 prior to this service returning.
614 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
615 @retval EFI_DEVICE_ERROR The calling processor is an AP.
616 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
617 does not exist.
618 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
619 @retval EFI_NOT_READY MP Initialize Library is not initialized.
620
621 **/
622 EFI_STATUS
623 EFIAPI
624 MpInitLibEnableDisableAP (
625 IN UINTN ProcessorNumber,
626 IN BOOLEAN EnableAP,
627 IN UINT32 *HealthFlag OPTIONAL
628 )
629 {
630 EFI_STATUS Status;
631 BOOLEAN TempStopCheckState;
632
633 TempStopCheckState = FALSE;
634 //
635 // temporarily stop checkAllAPsStatus for initialize parameters.
636 //
637 if (!mStopCheckAllApsStatus) {
638 mStopCheckAllApsStatus = TRUE;
639 TempStopCheckState = TRUE;
640 }
641
642 Status = EnableDisableApWorker (ProcessorNumber, EnableAP, HealthFlag);
643
644 if (TempStopCheckState) {
645 mStopCheckAllApsStatus = FALSE;
646 }
647
648 return Status;
649 }