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