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