]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
UefiCpuPkg/MpInitLib: Check APs Status and update APs status
[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 volatile BOOLEAN mStopCheckAllApsStatus = TRUE;
25
26
27 /**
28 Get the pointer to CPU MP Data structure.
29
30 @return The pointer to CPU MP Data structure.
31 **/
32 CPU_MP_DATA *
33 GetCpuMpData (
34 VOID
35 )
36 {
37 ASSERT (mCpuMpData != NULL);
38 return mCpuMpData;
39 }
40
41 /**
42 Save the pointer to CPU MP Data structure.
43
44 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
45 **/
46 VOID
47 SaveCpuMpData (
48 IN CPU_MP_DATA *CpuMpData
49 )
50 {
51 mCpuMpData = CpuMpData;
52 }
53
54 /**
55 Allocate reset vector buffer.
56
57 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
58 **/
59 VOID
60 AllocateResetVector (
61 IN OUT CPU_MP_DATA *CpuMpData
62 )
63 {
64 EFI_STATUS Status;
65 UINTN ApResetVectorSize;
66 EFI_PHYSICAL_ADDRESS StartAddress;
67
68 ApResetVectorSize = CpuMpData->AddressMap.RendezvousFunnelSize +
69 sizeof (MP_CPU_EXCHANGE_INFO);
70
71 StartAddress = BASE_1MB;
72 Status = gBS->AllocatePages (
73 AllocateMaxAddress,
74 EfiACPIMemoryNVS,
75 EFI_SIZE_TO_PAGES (ApResetVectorSize),
76 &StartAddress
77 );
78 ASSERT_EFI_ERROR (Status);
79
80 CpuMpData->WakeupBuffer = (UINTN) StartAddress;
81 CpuMpData->MpCpuExchangeInfo = (MP_CPU_EXCHANGE_INFO *) (UINTN)
82 (CpuMpData->WakeupBuffer + CpuMpData->AddressMap.RendezvousFunnelSize);
83 //
84 // copy AP reset code in it
85 //
86 CopyMem (
87 (VOID *) CpuMpData->WakeupBuffer,
88 (VOID *) CpuMpData->AddressMap.RendezvousFunnelAddress,
89 CpuMpData->AddressMap.RendezvousFunnelSize
90 );
91 }
92
93 /**
94 Free AP reset vector buffer.
95
96 @param[in] CpuMpData The pointer to CPU MP Data structure.
97 **/
98 VOID
99 FreeResetVector (
100 IN CPU_MP_DATA *CpuMpData
101 )
102 {
103 EFI_STATUS Status;
104 UINTN ApResetVectorSize;
105 ApResetVectorSize = CpuMpData->AddressMap.RendezvousFunnelSize +
106 sizeof (MP_CPU_EXCHANGE_INFO);
107 Status = gBS->FreePages(
108 (EFI_PHYSICAL_ADDRESS)CpuMpData->WakeupBuffer,
109 EFI_SIZE_TO_PAGES (ApResetVectorSize)
110 );
111 ASSERT_EFI_ERROR (Status);
112 }
113
114 /**
115 Checks APs status and updates APs status if needed.
116
117 **/
118 VOID
119 CheckAndUpdateApsStatus (
120 VOID
121 )
122 {
123 UINTN ProcessorNumber;
124 EFI_STATUS Status;
125 CPU_MP_DATA *CpuMpData;
126
127 CpuMpData = GetCpuMpData ();
128
129 //
130 // First, check whether pending StartupAllAPs() exists.
131 //
132 if (CpuMpData->WaitEvent != NULL) {
133
134 Status = CheckAllAPs ();
135 //
136 // If all APs finish for StartupAllAPs(), signal the WaitEvent for it.
137 //
138 if (Status != EFI_NOT_READY) {
139 Status = gBS->SignalEvent (CpuMpData->WaitEvent);
140 CpuMpData->WaitEvent = NULL;
141 }
142 }
143
144 //
145 // Second, check whether pending StartupThisAPs() callings exist.
146 //
147 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
148
149 if (CpuMpData->CpuData[ProcessorNumber].WaitEvent == NULL) {
150 continue;
151 }
152
153 Status = CheckThisAP (ProcessorNumber);
154
155 if (Status != EFI_NOT_READY) {
156 gBS->SignalEvent (CpuMpData->CpuData[ProcessorNumber].WaitEvent);
157 CpuMpData->CpuData[ProcessorNumber].WaitEvent = NULL;
158 }
159 }
160 }
161
162 /**
163 Checks APs' status periodically.
164
165 This function is triggerred by timer perodically to check the
166 state of APs for StartupAllAPs() and StartupThisAP() executed
167 in non-blocking mode.
168
169 @param[in] Event Event triggered.
170 @param[in] Context Parameter passed with the event.
171
172 **/
173 VOID
174 EFIAPI
175 CheckApsStatus (
176 IN EFI_EVENT Event,
177 IN VOID *Context
178 )
179 {
180 //
181 // If CheckApsStatus() is not stopped, otherwise return immediately.
182 //
183 if (!mStopCheckAllApsStatus) {
184 CheckAndUpdateApsStatus ();
185 }
186 }
187
188 /**
189 Initialize global data for MP support.
190
191 @param[in] CpuMpData The pointer to CPU MP Data structure.
192 **/
193 VOID
194 InitMpGlobalData (
195 IN CPU_MP_DATA *CpuMpData
196 )
197 {
198 EFI_STATUS Status;
199
200 SaveCpuMpData (CpuMpData);
201
202 Status = gBS->CreateEvent (
203 EVT_TIMER | EVT_NOTIFY_SIGNAL,
204 TPL_NOTIFY,
205 CheckApsStatus,
206 NULL,
207 &mCheckAllApsEvent
208 );
209 ASSERT_EFI_ERROR (Status);
210
211 //
212 // Set timer to check all APs status.
213 //
214 Status = gBS->SetTimer (
215 mCheckAllApsEvent,
216 TimerPeriodic,
217 AP_CHECK_INTERVAL
218 );
219 ASSERT_EFI_ERROR (Status);
220 }
221
222 /**
223 This service executes a caller provided function on all enabled APs.
224
225 @param[in] Procedure A pointer to the function to be run on
226 enabled APs of the system. See type
227 EFI_AP_PROCEDURE.
228 @param[in] SingleThread If TRUE, then all the enabled APs execute
229 the function specified by Procedure one by
230 one, in ascending order of processor handle
231 number. If FALSE, then all the enabled APs
232 execute the function specified by Procedure
233 simultaneously.
234 @param[in] WaitEvent The event created by the caller with CreateEvent()
235 service. If it is NULL, then execute in
236 blocking mode. BSP waits until all APs finish
237 or TimeoutInMicroSeconds expires. If it's
238 not NULL, then execute in non-blocking mode.
239 BSP requests the function specified by
240 Procedure to be started on all the enabled
241 APs, and go on executing immediately. If
242 all return from Procedure, or TimeoutInMicroSeconds
243 expires, this event is signaled. The BSP
244 can use the CheckEvent() or WaitForEvent()
245 services to check the state of event. Type
246 EFI_EVENT is defined in CreateEvent() in
247 the Unified Extensible Firmware Interface
248 Specification.
249 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
250 APs to return from Procedure, either for
251 blocking or non-blocking mode. Zero means
252 infinity. If the timeout expires before
253 all APs return from Procedure, then Procedure
254 on the failed APs is terminated. All enabled
255 APs are available for next function assigned
256 by MpInitLibStartupAllAPs() or
257 MPInitLibStartupThisAP().
258 If the timeout expires in blocking mode,
259 BSP returns EFI_TIMEOUT. If the timeout
260 expires in non-blocking mode, WaitEvent
261 is signaled with SignalEvent().
262 @param[in] ProcedureArgument The parameter passed into Procedure for
263 all APs.
264 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
265 if all APs finish successfully, then its
266 content is set to NULL. If not all APs
267 finish before timeout expires, then its
268 content is set to address of the buffer
269 holding handle numbers of the failed APs.
270 The buffer is allocated by MP Initialization
271 library, and it's the caller's responsibility to
272 free the buffer with FreePool() service.
273 In blocking mode, it is ready for consumption
274 when the call returns. In non-blocking mode,
275 it is ready when WaitEvent is signaled. The
276 list of failed CPU is terminated by
277 END_OF_CPU_LIST.
278
279 @retval EFI_SUCCESS In blocking mode, all APs have finished before
280 the timeout expired.
281 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
282 to all enabled APs.
283 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
284 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
285 signaled.
286 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
287 supported.
288 @retval EFI_DEVICE_ERROR Caller processor is AP.
289 @retval EFI_NOT_STARTED No enabled APs exist in the system.
290 @retval EFI_NOT_READY Any enabled APs are busy.
291 @retval EFI_NOT_READY MP Initialize Library is not initialized.
292 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
293 all enabled APs have finished.
294 @retval EFI_INVALID_PARAMETER Procedure is NULL.
295
296 **/
297 EFI_STATUS
298 EFIAPI
299 MpInitLibStartupAllAPs (
300 IN EFI_AP_PROCEDURE Procedure,
301 IN BOOLEAN SingleThread,
302 IN EFI_EVENT WaitEvent OPTIONAL,
303 IN UINTN TimeoutInMicroseconds,
304 IN VOID *ProcedureArgument OPTIONAL,
305 OUT UINTN **FailedCpuList OPTIONAL
306 )
307 {
308 return EFI_UNSUPPORTED;
309 }
310
311 /**
312 This service lets the caller get one enabled AP to execute a caller-provided
313 function.
314
315 @param[in] Procedure A pointer to the function to be run on the
316 designated AP of the system. See type
317 EFI_AP_PROCEDURE.
318 @param[in] ProcessorNumber The handle number of the AP. The range is
319 from 0 to the total number of logical
320 processors minus 1. The total number of
321 logical processors can be retrieved by
322 MpInitLibGetNumberOfProcessors().
323 @param[in] WaitEvent The event created by the caller with CreateEvent()
324 service. If it is NULL, then execute in
325 blocking mode. BSP waits until this AP finish
326 or TimeoutInMicroSeconds expires. If it's
327 not NULL, then execute in non-blocking mode.
328 BSP requests the function specified by
329 Procedure to be started on this AP,
330 and go on executing immediately. If this AP
331 return from Procedure or TimeoutInMicroSeconds
332 expires, this event is signaled. The BSP
333 can use the CheckEvent() or WaitForEvent()
334 services to check the state of event. Type
335 EFI_EVENT is defined in CreateEvent() in
336 the Unified Extensible Firmware Interface
337 Specification.
338 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
339 this AP to finish this Procedure, either for
340 blocking or non-blocking mode. Zero means
341 infinity. If the timeout expires before
342 this AP returns from Procedure, then Procedure
343 on the AP is terminated. The
344 AP is available for next function assigned
345 by MpInitLibStartupAllAPs() or
346 MpInitLibStartupThisAP().
347 If the timeout expires in blocking mode,
348 BSP returns EFI_TIMEOUT. If the timeout
349 expires in non-blocking mode, WaitEvent
350 is signaled with SignalEvent().
351 @param[in] ProcedureArgument The parameter passed into Procedure on the
352 specified AP.
353 @param[out] Finished If NULL, this parameter is ignored. In
354 blocking mode, this parameter is ignored.
355 In non-blocking mode, if AP returns from
356 Procedure before the timeout expires, its
357 content is set to TRUE. Otherwise, the
358 value is set to FALSE. The caller can
359 determine if the AP returned from Procedure
360 by evaluating this value.
361
362 @retval EFI_SUCCESS In blocking mode, specified AP finished before
363 the timeout expires.
364 @retval EFI_SUCCESS In non-blocking mode, the function has been
365 dispatched to specified AP.
366 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
367 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
368 signaled.
369 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
370 supported.
371 @retval EFI_DEVICE_ERROR The calling processor is an AP.
372 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
373 the specified AP has finished.
374 @retval EFI_NOT_READY The specified AP is busy.
375 @retval EFI_NOT_READY MP Initialize Library is not initialized.
376 @retval EFI_NOT_FOUND The processor with the handle specified by
377 ProcessorNumber does not exist.
378 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
379 @retval EFI_INVALID_PARAMETER Procedure is NULL.
380
381 **/
382 EFI_STATUS
383 EFIAPI
384 MpInitLibStartupThisAP (
385 IN EFI_AP_PROCEDURE Procedure,
386 IN UINTN ProcessorNumber,
387 IN EFI_EVENT WaitEvent OPTIONAL,
388 IN UINTN TimeoutInMicroseconds,
389 IN VOID *ProcedureArgument OPTIONAL,
390 OUT BOOLEAN *Finished OPTIONAL
391 )
392 {
393 return EFI_UNSUPPORTED;
394 }
395
396 /**
397 This service switches the requested AP to be the BSP from that point onward.
398 This service changes the BSP for all purposes. This call can only be performed
399 by the current BSP.
400
401 @param[in] ProcessorNumber The handle number of AP that is to become the new
402 BSP. The range is from 0 to the total number of
403 logical processors minus 1. The total number of
404 logical processors can be retrieved by
405 MpInitLibGetNumberOfProcessors().
406 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
407 enabled AP. Otherwise, it will be disabled.
408
409 @retval EFI_SUCCESS BSP successfully switched.
410 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
411 this service returning.
412 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
413 @retval EFI_DEVICE_ERROR The calling processor is an AP.
414 @retval EFI_NOT_FOUND The processor with the handle specified by
415 ProcessorNumber does not exist.
416 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
417 a disabled AP.
418 @retval EFI_NOT_READY The specified AP is busy.
419 @retval EFI_NOT_READY MP Initialize Library is not initialized.
420
421 **/
422 EFI_STATUS
423 EFIAPI
424 MpInitLibSwitchBSP (
425 IN UINTN ProcessorNumber,
426 IN BOOLEAN EnableOldBSP
427 )
428 {
429 EFI_STATUS Status;
430 BOOLEAN OldInterruptState;
431
432 //
433 // Before send both BSP and AP to a procedure to exchange their roles,
434 // interrupt must be disabled. This is because during the exchange role
435 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
436 // be corrupted, since interrupt return address will be pushed to stack
437 // by hardware.
438 //
439 OldInterruptState = SaveAndDisableInterrupts ();
440
441 //
442 // Mask LINT0 & LINT1 for the old BSP
443 //
444 DisableLvtInterrupts ();
445
446 Status = SwitchBSPWorker (ProcessorNumber, EnableOldBSP);
447
448 //
449 // Restore interrupt state.
450 //
451 SetInterruptState (OldInterruptState);
452
453 return Status;
454 }
455
456 /**
457 This service lets the caller enable or disable an AP from this point onward.
458 This service may only be called from the BSP.
459
460 @param[in] ProcessorNumber The handle number of AP.
461 The range is from 0 to the total number of
462 logical processors minus 1. The total number of
463 logical processors can be retrieved by
464 MpInitLibGetNumberOfProcessors().
465 @param[in] EnableAP Specifies the new state for the processor for
466 enabled, FALSE for disabled.
467 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
468 the new health status of the AP. This flag
469 corresponds to StatusFlag defined in
470 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
471 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
472 bits are ignored. If it is NULL, this parameter
473 is ignored.
474
475 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
476 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
477 prior to this service returning.
478 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
479 @retval EFI_DEVICE_ERROR The calling processor is an AP.
480 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
481 does not exist.
482 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
483 @retval EFI_NOT_READY MP Initialize Library is not initialized.
484
485 **/
486 EFI_STATUS
487 EFIAPI
488 MpInitLibEnableDisableAP (
489 IN UINTN ProcessorNumber,
490 IN BOOLEAN EnableAP,
491 IN UINT32 *HealthFlag OPTIONAL
492 )
493 {
494 EFI_STATUS Status;
495 BOOLEAN TempStopCheckState;
496
497 TempStopCheckState = FALSE;
498 //
499 // temporarily stop checkAllAPsStatus for initialize parameters.
500 //
501 if (!mStopCheckAllApsStatus) {
502 mStopCheckAllApsStatus = TRUE;
503 TempStopCheckState = TRUE;
504 }
505
506 Status = EnableDisableApWorker (ProcessorNumber, EnableAP, HealthFlag);
507
508 if (TempStopCheckState) {
509 mStopCheckAllApsStatus = FALSE;
510 }
511
512 return Status;
513 }