]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
UefiCpuPkg/MpInitLib: Implementation of MpInitLibStartupAllAPs()
[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 EFI_STATUS Status;
309
310 //
311 // Temporarily stop checkAllApsStatus for avoid resource dead-lock.
312 //
313 mStopCheckAllApsStatus = TRUE;
314
315 Status = StartupAllAPsWorker (
316 Procedure,
317 SingleThread,
318 WaitEvent,
319 TimeoutInMicroseconds,
320 ProcedureArgument,
321 FailedCpuList
322 );
323
324 //
325 // Start checkAllApsStatus
326 //
327 mStopCheckAllApsStatus = FALSE;
328
329 return Status;
330 }
331
332 /**
333 This service lets the caller get one enabled AP to execute a caller-provided
334 function.
335
336 @param[in] Procedure A pointer to the function to be run on the
337 designated AP of the system. See type
338 EFI_AP_PROCEDURE.
339 @param[in] ProcessorNumber The handle number of the AP. The range is
340 from 0 to the total number of logical
341 processors minus 1. The total number of
342 logical processors can be retrieved by
343 MpInitLibGetNumberOfProcessors().
344 @param[in] WaitEvent The event created by the caller with CreateEvent()
345 service. If it is NULL, then execute in
346 blocking mode. BSP waits until this AP finish
347 or TimeoutInMicroSeconds expires. If it's
348 not NULL, then execute in non-blocking mode.
349 BSP requests the function specified by
350 Procedure to be started on this AP,
351 and go on executing immediately. If this AP
352 return from Procedure or TimeoutInMicroSeconds
353 expires, this event is signaled. The BSP
354 can use the CheckEvent() or WaitForEvent()
355 services to check the state of event. Type
356 EFI_EVENT is defined in CreateEvent() in
357 the Unified Extensible Firmware Interface
358 Specification.
359 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
360 this AP to finish this Procedure, either for
361 blocking or non-blocking mode. Zero means
362 infinity. If the timeout expires before
363 this AP returns from Procedure, then Procedure
364 on the AP is terminated. The
365 AP is available for next function assigned
366 by MpInitLibStartupAllAPs() or
367 MpInitLibStartupThisAP().
368 If the timeout expires in blocking mode,
369 BSP returns EFI_TIMEOUT. If the timeout
370 expires in non-blocking mode, WaitEvent
371 is signaled with SignalEvent().
372 @param[in] ProcedureArgument The parameter passed into Procedure on the
373 specified AP.
374 @param[out] Finished If NULL, this parameter is ignored. In
375 blocking mode, this parameter is ignored.
376 In non-blocking mode, if AP returns from
377 Procedure before the timeout expires, its
378 content is set to TRUE. Otherwise, the
379 value is set to FALSE. The caller can
380 determine if the AP returned from Procedure
381 by evaluating this value.
382
383 @retval EFI_SUCCESS In blocking mode, specified AP finished before
384 the timeout expires.
385 @retval EFI_SUCCESS In non-blocking mode, the function has been
386 dispatched to specified AP.
387 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
388 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
389 signaled.
390 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
391 supported.
392 @retval EFI_DEVICE_ERROR The calling processor is an AP.
393 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
394 the specified AP has finished.
395 @retval EFI_NOT_READY The specified AP is busy.
396 @retval EFI_NOT_READY MP Initialize Library is not initialized.
397 @retval EFI_NOT_FOUND The processor with the handle specified by
398 ProcessorNumber does not exist.
399 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
400 @retval EFI_INVALID_PARAMETER Procedure is NULL.
401
402 **/
403 EFI_STATUS
404 EFIAPI
405 MpInitLibStartupThisAP (
406 IN EFI_AP_PROCEDURE Procedure,
407 IN UINTN ProcessorNumber,
408 IN EFI_EVENT WaitEvent OPTIONAL,
409 IN UINTN TimeoutInMicroseconds,
410 IN VOID *ProcedureArgument OPTIONAL,
411 OUT BOOLEAN *Finished OPTIONAL
412 )
413 {
414 EFI_STATUS Status;
415
416 //
417 // temporarily stop checkAllApsStatus for avoid resource dead-lock.
418 //
419 mStopCheckAllApsStatus = TRUE;
420
421 Status = StartupThisAPWorker (
422 Procedure,
423 ProcessorNumber,
424 WaitEvent,
425 TimeoutInMicroseconds,
426 ProcedureArgument,
427 Finished
428 );
429
430 mStopCheckAllApsStatus = FALSE;
431
432 return Status;
433 }
434
435 /**
436 This service switches the requested AP to be the BSP from that point onward.
437 This service changes the BSP for all purposes. This call can only be performed
438 by the current BSP.
439
440 @param[in] ProcessorNumber The handle number of AP that is to become the new
441 BSP. The range is from 0 to the total number of
442 logical processors minus 1. The total number of
443 logical processors can be retrieved by
444 MpInitLibGetNumberOfProcessors().
445 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
446 enabled AP. Otherwise, it will be disabled.
447
448 @retval EFI_SUCCESS BSP successfully switched.
449 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
450 this service returning.
451 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
452 @retval EFI_DEVICE_ERROR The calling processor is an AP.
453 @retval EFI_NOT_FOUND The processor with the handle specified by
454 ProcessorNumber does not exist.
455 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
456 a disabled AP.
457 @retval EFI_NOT_READY The specified AP is busy.
458 @retval EFI_NOT_READY MP Initialize Library is not initialized.
459
460 **/
461 EFI_STATUS
462 EFIAPI
463 MpInitLibSwitchBSP (
464 IN UINTN ProcessorNumber,
465 IN BOOLEAN EnableOldBSP
466 )
467 {
468 EFI_STATUS Status;
469 BOOLEAN OldInterruptState;
470
471 //
472 // Before send both BSP and AP to a procedure to exchange their roles,
473 // interrupt must be disabled. This is because during the exchange role
474 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
475 // be corrupted, since interrupt return address will be pushed to stack
476 // by hardware.
477 //
478 OldInterruptState = SaveAndDisableInterrupts ();
479
480 //
481 // Mask LINT0 & LINT1 for the old BSP
482 //
483 DisableLvtInterrupts ();
484
485 Status = SwitchBSPWorker (ProcessorNumber, EnableOldBSP);
486
487 //
488 // Restore interrupt state.
489 //
490 SetInterruptState (OldInterruptState);
491
492 return Status;
493 }
494
495 /**
496 This service lets the caller enable or disable an AP from this point onward.
497 This service may only be called from the BSP.
498
499 @param[in] ProcessorNumber The handle number of AP.
500 The range is from 0 to the total number of
501 logical processors minus 1. The total number of
502 logical processors can be retrieved by
503 MpInitLibGetNumberOfProcessors().
504 @param[in] EnableAP Specifies the new state for the processor for
505 enabled, FALSE for disabled.
506 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
507 the new health status of the AP. This flag
508 corresponds to StatusFlag defined in
509 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
510 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
511 bits are ignored. If it is NULL, this parameter
512 is ignored.
513
514 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
515 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
516 prior to this service returning.
517 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
518 @retval EFI_DEVICE_ERROR The calling processor is an AP.
519 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
520 does not exist.
521 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
522 @retval EFI_NOT_READY MP Initialize Library is not initialized.
523
524 **/
525 EFI_STATUS
526 EFIAPI
527 MpInitLibEnableDisableAP (
528 IN UINTN ProcessorNumber,
529 IN BOOLEAN EnableAP,
530 IN UINT32 *HealthFlag OPTIONAL
531 )
532 {
533 EFI_STATUS Status;
534 BOOLEAN TempStopCheckState;
535
536 TempStopCheckState = FALSE;
537 //
538 // temporarily stop checkAllAPsStatus for initialize parameters.
539 //
540 if (!mStopCheckAllApsStatus) {
541 mStopCheckAllApsStatus = TRUE;
542 TempStopCheckState = TRUE;
543 }
544
545 Status = EnableDisableApWorker (ProcessorNumber, EnableAP, HealthFlag);
546
547 if (TempStopCheckState) {
548 mStopCheckAllApsStatus = FALSE;
549 }
550
551 return Status;
552 }