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