]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
UefiCpuPkg/MpInitLib: Fix function header comments typo
[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 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 @return 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
262 CpuMpData = GetCpuMpData ();
263 CpuMpData->PmCodeSegment = GetProtectedModeCS ();
264 CpuMpData->ApLoopMode = PcdGet8 (PcdCpuApLoopMode);
265 WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, mReservedApLoopFunc);
266 DEBUG ((DEBUG_INFO, "MpInitExitBootServicesCallback() done!\n"));
267 }
268
269 /**
270 Initialize global data for MP support.
271
272 @param[in] CpuMpData The pointer to CPU MP Data structure.
273 **/
274 VOID
275 InitMpGlobalData (
276 IN CPU_MP_DATA *CpuMpData
277 )
278 {
279 EFI_STATUS Status;
280
281 SaveCpuMpData (CpuMpData);
282
283 //
284 // Avoid APs access invalid buff data which allocated by BootServices,
285 // so we will allocate reserved data for AP loop code.
286 // Allocating it in advance since memory services are not available in
287 // Exit Boot Services callback function.
288 //
289 mReservedApLoopFunc = AllocateReservedCopyPool (
290 CpuMpData->AddressMap.RelocateApLoopFuncSize,
291 CpuMpData->AddressMap.RelocateApLoopFuncAddress
292 );
293 ASSERT (mReservedApLoopFunc != NULL);
294
295 Status = gBS->CreateEvent (
296 EVT_TIMER | EVT_NOTIFY_SIGNAL,
297 TPL_NOTIFY,
298 CheckApsStatus,
299 NULL,
300 &mCheckAllApsEvent
301 );
302 ASSERT_EFI_ERROR (Status);
303
304 //
305 // Set timer to check all APs status.
306 //
307 Status = gBS->SetTimer (
308 mCheckAllApsEvent,
309 TimerPeriodic,
310 AP_CHECK_INTERVAL
311 );
312 ASSERT_EFI_ERROR (Status);
313 Status = gBS->CreateEvent (
314 EVT_SIGNAL_EXIT_BOOT_SERVICES,
315 TPL_CALLBACK,
316 MpInitExitBootServicesCallback,
317 NULL,
318 &mMpInitExitBootServicesEvent
319 );
320 ASSERT_EFI_ERROR (Status);
321 }
322
323 /**
324 This service executes a caller provided function on all enabled APs.
325
326 @param[in] Procedure A pointer to the function to be run on
327 enabled APs of the system. See type
328 EFI_AP_PROCEDURE.
329 @param[in] SingleThread If TRUE, then all the enabled APs execute
330 the function specified by Procedure one by
331 one, in ascending order of processor handle
332 number. If FALSE, then all the enabled APs
333 execute the function specified by Procedure
334 simultaneously.
335 @param[in] WaitEvent The event created by the caller with CreateEvent()
336 service. If it is NULL, then execute in
337 blocking mode. BSP waits until all APs finish
338 or TimeoutInMicroSeconds expires. If it's
339 not NULL, then execute in non-blocking mode.
340 BSP requests the function specified by
341 Procedure to be started on all the enabled
342 APs, and go on executing immediately. If
343 all return from Procedure, or TimeoutInMicroSeconds
344 expires, this event is signaled. The BSP
345 can use the CheckEvent() or WaitForEvent()
346 services to check the state of event. Type
347 EFI_EVENT is defined in CreateEvent() in
348 the Unified Extensible Firmware Interface
349 Specification.
350 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
351 APs to return from Procedure, either for
352 blocking or non-blocking mode. Zero means
353 infinity. If the timeout expires before
354 all APs return from Procedure, then Procedure
355 on the failed APs is terminated. All enabled
356 APs are available for next function assigned
357 by MpInitLibStartupAllAPs() or
358 MPInitLibStartupThisAP().
359 If the timeout expires in blocking mode,
360 BSP returns EFI_TIMEOUT. If the timeout
361 expires in non-blocking mode, WaitEvent
362 is signaled with SignalEvent().
363 @param[in] ProcedureArgument The parameter passed into Procedure for
364 all APs.
365 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
366 if all APs finish successfully, then its
367 content is set to NULL. If not all APs
368 finish before timeout expires, then its
369 content is set to address of the buffer
370 holding handle numbers of the failed APs.
371 The buffer is allocated by MP Initialization
372 library, and it's the caller's responsibility to
373 free the buffer with FreePool() service.
374 In blocking mode, it is ready for consumption
375 when the call returns. In non-blocking mode,
376 it is ready when WaitEvent is signaled. The
377 list of failed CPU is terminated by
378 END_OF_CPU_LIST.
379
380 @retval EFI_SUCCESS In blocking mode, all APs have finished before
381 the timeout expired.
382 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
383 to all enabled APs.
384 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
385 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
386 signaled.
387 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
388 supported.
389 @retval EFI_DEVICE_ERROR Caller processor is AP.
390 @retval EFI_NOT_STARTED No enabled APs exist in the system.
391 @retval EFI_NOT_READY Any enabled APs are busy.
392 @retval EFI_NOT_READY MP Initialize Library is not initialized.
393 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
394 all enabled APs have finished.
395 @retval EFI_INVALID_PARAMETER Procedure is NULL.
396
397 **/
398 EFI_STATUS
399 EFIAPI
400 MpInitLibStartupAllAPs (
401 IN EFI_AP_PROCEDURE Procedure,
402 IN BOOLEAN SingleThread,
403 IN EFI_EVENT WaitEvent OPTIONAL,
404 IN UINTN TimeoutInMicroseconds,
405 IN VOID *ProcedureArgument OPTIONAL,
406 OUT UINTN **FailedCpuList OPTIONAL
407 )
408 {
409 EFI_STATUS Status;
410
411 //
412 // Temporarily stop checkAllApsStatus for avoid resource dead-lock.
413 //
414 mStopCheckAllApsStatus = TRUE;
415
416 Status = StartupAllAPsWorker (
417 Procedure,
418 SingleThread,
419 WaitEvent,
420 TimeoutInMicroseconds,
421 ProcedureArgument,
422 FailedCpuList
423 );
424
425 //
426 // Start checkAllApsStatus
427 //
428 mStopCheckAllApsStatus = FALSE;
429
430 return Status;
431 }
432
433 /**
434 This service lets the caller get one enabled AP to execute a caller-provided
435 function.
436
437 @param[in] Procedure A pointer to the function to be run on the
438 designated AP of the system. See type
439 EFI_AP_PROCEDURE.
440 @param[in] ProcessorNumber The handle number of the AP. The range is
441 from 0 to the total number of logical
442 processors minus 1. The total number of
443 logical processors can be retrieved by
444 MpInitLibGetNumberOfProcessors().
445 @param[in] WaitEvent The event created by the caller with CreateEvent()
446 service. If it is NULL, then execute in
447 blocking mode. BSP waits until this AP finish
448 or TimeoutInMicroSeconds expires. If it's
449 not NULL, then execute in non-blocking mode.
450 BSP requests the function specified by
451 Procedure to be started on this AP,
452 and go on executing immediately. If this AP
453 return from Procedure or TimeoutInMicroSeconds
454 expires, this event is signaled. The BSP
455 can use the CheckEvent() or WaitForEvent()
456 services to check the state of event. Type
457 EFI_EVENT is defined in CreateEvent() in
458 the Unified Extensible Firmware Interface
459 Specification.
460 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
461 this AP to finish this Procedure, either for
462 blocking or non-blocking mode. Zero means
463 infinity. If the timeout expires before
464 this AP returns from Procedure, then Procedure
465 on the AP is terminated. The
466 AP is available for next function assigned
467 by MpInitLibStartupAllAPs() or
468 MpInitLibStartupThisAP().
469 If the timeout expires in blocking mode,
470 BSP returns EFI_TIMEOUT. If the timeout
471 expires in non-blocking mode, WaitEvent
472 is signaled with SignalEvent().
473 @param[in] ProcedureArgument The parameter passed into Procedure on the
474 specified AP.
475 @param[out] Finished If NULL, this parameter is ignored. In
476 blocking mode, this parameter is ignored.
477 In non-blocking mode, if AP returns from
478 Procedure before the timeout expires, its
479 content is set to TRUE. Otherwise, the
480 value is set to FALSE. The caller can
481 determine if the AP returned from Procedure
482 by evaluating this value.
483
484 @retval EFI_SUCCESS In blocking mode, specified AP finished before
485 the timeout expires.
486 @retval EFI_SUCCESS In non-blocking mode, the function has been
487 dispatched to specified AP.
488 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
489 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
490 signaled.
491 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
492 supported.
493 @retval EFI_DEVICE_ERROR The calling processor is an AP.
494 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
495 the specified AP has finished.
496 @retval EFI_NOT_READY The specified AP is busy.
497 @retval EFI_NOT_READY MP Initialize Library is not initialized.
498 @retval EFI_NOT_FOUND The processor with the handle specified by
499 ProcessorNumber does not exist.
500 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
501 @retval EFI_INVALID_PARAMETER Procedure is NULL.
502
503 **/
504 EFI_STATUS
505 EFIAPI
506 MpInitLibStartupThisAP (
507 IN EFI_AP_PROCEDURE Procedure,
508 IN UINTN ProcessorNumber,
509 IN EFI_EVENT WaitEvent OPTIONAL,
510 IN UINTN TimeoutInMicroseconds,
511 IN VOID *ProcedureArgument OPTIONAL,
512 OUT BOOLEAN *Finished OPTIONAL
513 )
514 {
515 EFI_STATUS Status;
516
517 //
518 // temporarily stop checkAllApsStatus for avoid resource dead-lock.
519 //
520 mStopCheckAllApsStatus = TRUE;
521
522 Status = StartupThisAPWorker (
523 Procedure,
524 ProcessorNumber,
525 WaitEvent,
526 TimeoutInMicroseconds,
527 ProcedureArgument,
528 Finished
529 );
530
531 mStopCheckAllApsStatus = FALSE;
532
533 return Status;
534 }
535
536 /**
537 This service switches the requested AP to be the BSP from that point onward.
538 This service changes the BSP for all purposes. This call can only be performed
539 by the current BSP.
540
541 @param[in] ProcessorNumber The handle number of AP that is to become the new
542 BSP. The range is from 0 to the total number of
543 logical processors minus 1. The total number of
544 logical processors can be retrieved by
545 MpInitLibGetNumberOfProcessors().
546 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
547 enabled AP. Otherwise, it will be disabled.
548
549 @retval EFI_SUCCESS BSP successfully switched.
550 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
551 this service returning.
552 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
553 @retval EFI_DEVICE_ERROR The calling processor is an AP.
554 @retval EFI_NOT_FOUND The processor with the handle specified by
555 ProcessorNumber does not exist.
556 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
557 a disabled AP.
558 @retval EFI_NOT_READY The specified AP is busy.
559 @retval EFI_NOT_READY MP Initialize Library is not initialized.
560
561 **/
562 EFI_STATUS
563 EFIAPI
564 MpInitLibSwitchBSP (
565 IN UINTN ProcessorNumber,
566 IN BOOLEAN EnableOldBSP
567 )
568 {
569 EFI_STATUS Status;
570 BOOLEAN OldInterruptState;
571
572 //
573 // Before send both BSP and AP to a procedure to exchange their roles,
574 // interrupt must be disabled. This is because during the exchange role
575 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
576 // be corrupted, since interrupt return address will be pushed to stack
577 // by hardware.
578 //
579 OldInterruptState = SaveAndDisableInterrupts ();
580
581 //
582 // Mask LINT0 & LINT1 for the old BSP
583 //
584 DisableLvtInterrupts ();
585
586 Status = SwitchBSPWorker (ProcessorNumber, EnableOldBSP);
587
588 //
589 // Restore interrupt state.
590 //
591 SetInterruptState (OldInterruptState);
592
593 return Status;
594 }
595
596 /**
597 This service lets the caller enable or disable an AP from this point onward.
598 This service may only be called from the BSP.
599
600 @param[in] ProcessorNumber The handle number of AP.
601 The range is from 0 to the total number of
602 logical processors minus 1. The total number of
603 logical processors can be retrieved by
604 MpInitLibGetNumberOfProcessors().
605 @param[in] EnableAP Specifies the new state for the processor for
606 enabled, FALSE for disabled.
607 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
608 the new health status of the AP. This flag
609 corresponds to StatusFlag defined in
610 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
611 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
612 bits are ignored. If it is NULL, this parameter
613 is ignored.
614
615 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
616 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
617 prior to this service returning.
618 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
619 @retval EFI_DEVICE_ERROR The calling processor is an AP.
620 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
621 does not exist.
622 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
623 @retval EFI_NOT_READY MP Initialize Library is not initialized.
624
625 **/
626 EFI_STATUS
627 EFIAPI
628 MpInitLibEnableDisableAP (
629 IN UINTN ProcessorNumber,
630 IN BOOLEAN EnableAP,
631 IN UINT32 *HealthFlag OPTIONAL
632 )
633 {
634 EFI_STATUS Status;
635 BOOLEAN TempStopCheckState;
636
637 TempStopCheckState = FALSE;
638 //
639 // temporarily stop checkAllAPsStatus for initialize parameters.
640 //
641 if (!mStopCheckAllApsStatus) {
642 mStopCheckAllApsStatus = TRUE;
643 TempStopCheckState = TRUE;
644 }
645
646 Status = EnableDisableApWorker (ProcessorNumber, EnableAP, HealthFlag);
647
648 if (TempStopCheckState) {
649 mStopCheckAllApsStatus = FALSE;
650 }
651
652 return Status;
653 }