]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
UefiCpuPkg/MpInitLib: Not use disabled AP when call StartAllAPs.
[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
7367cc6c 4 Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>\r
3e8ad6bd
JF
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
43c9fdcc 19#include <Library/DebugAgentLib.h>\r
15720a6c 20#include <Library/DxeServicesTableLib.h>\r
96378861 21\r
b6e45716
JF
22#include <Protocol/Timer.h>\r
23\r
96378861 24#define AP_CHECK_INTERVAL (EFI_TIMER_PERIOD_MILLISECONDS (100))\r
bf2786dc 25#define AP_SAFE_STACK_SIZE 128\r
96378861 26\r
93ca4c0f 27CPU_MP_DATA *mCpuMpData = NULL;\r
96378861 28EFI_EVENT mCheckAllApsEvent = NULL;\r
4d3314f6 29EFI_EVENT mMpInitExitBootServicesEvent = NULL;\r
8677a56a 30EFI_EVENT mLegacyBootEvent = NULL;\r
96378861 31volatile BOOLEAN mStopCheckAllApsStatus = TRUE;\r
5183fb37 32VOID *mReservedApLoopFunc = NULL;\r
bf2786dc 33UINTN mReservedTopOfApStack;\r
9f91cb01 34volatile UINT32 mNumberToFinish = 0;\r
93ca4c0f 35\r
43c9fdcc
JF
36/**\r
37 Enable Debug Agent to support source debugging on AP function.\r
38\r
39**/\r
40VOID\r
41EnableDebugAgent (\r
42 VOID\r
43 )\r
44{\r
45 //\r
46 // Initialize Debug Agent to support source level debug in DXE phase\r
47 //\r
48 InitializeDebugAgent (DEBUG_AGENT_INIT_DXE_AP, NULL, NULL);\r
49}\r
50\r
93ca4c0f
JF
51/**\r
52 Get the pointer to CPU MP Data structure.\r
53\r
54 @return The pointer to CPU MP Data structure.\r
55**/\r
56CPU_MP_DATA *\r
57GetCpuMpData (\r
58 VOID\r
59 )\r
60{\r
61 ASSERT (mCpuMpData != NULL);\r
62 return mCpuMpData;\r
63}\r
64\r
65/**\r
66 Save the pointer to CPU MP Data structure.\r
67\r
68 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.\r
69**/\r
70VOID\r
71SaveCpuMpData (\r
72 IN CPU_MP_DATA *CpuMpData\r
73 )\r
74{\r
75 mCpuMpData = CpuMpData;\r
76}\r
77\r
96378861 78/**\r
a6b3d753 79 Get available system memory below 1MB by specified size.\r
ed66e0e3 80\r
a6b3d753 81 @param[in] WakeupBufferSize Wakeup buffer size required\r
3ed4e502 82\r
a6b3d753
SZ
83 @retval other Return wakeup buffer address below 1MB.\r
84 @retval -1 Cannot find free memory below 1MB.\r
ed66e0e3 85**/\r
a6b3d753
SZ
86UINTN\r
87GetWakeupBuffer (\r
88 IN UINTN WakeupBufferSize\r
ed66e0e3
JF
89 )\r
90{\r
a6b3d753
SZ
91 EFI_STATUS Status;\r
92 EFI_PHYSICAL_ADDRESS StartAddress;\r
93\r
94 StartAddress = BASE_1MB;\r
95 Status = gBS->AllocatePages (\r
96 AllocateMaxAddress,\r
97 EfiBootServicesData,\r
98 EFI_SIZE_TO_PAGES (WakeupBufferSize),\r
99 &StartAddress\r
100 );\r
101 ASSERT_EFI_ERROR (Status);\r
102 if (!EFI_ERROR (Status)) {\r
3ed4e502 103 Status = gBS->FreePages(\r
a6b3d753
SZ
104 StartAddress,\r
105 EFI_SIZE_TO_PAGES (WakeupBufferSize)\r
3ed4e502
JF
106 );\r
107 ASSERT_EFI_ERROR (Status);\r
a6b3d753
SZ
108 DEBUG ((DEBUG_INFO, "WakeupBufferStart = %x, WakeupBufferSize = %x\n",\r
109 (UINTN) StartAddress, WakeupBufferSize));\r
110 } else {\r
111 StartAddress = (EFI_PHYSICAL_ADDRESS) -1;\r
3ed4e502 112 }\r
a6b3d753 113 return (UINTN) StartAddress;\r
ed66e0e3
JF
114}\r
115\r
f32bfe6d
JW
116/**\r
117 Get available EfiBootServicesCode memory below 4GB by specified size.\r
118\r
119 This buffer is required to safely transfer AP from real address mode to\r
120 protected mode or long mode, due to the fact that the buffer returned by\r
121 GetWakeupBuffer() may be marked as non-executable.\r
122\r
123 @param[in] BufferSize Wakeup transition buffer size.\r
124\r
125 @retval other Return wakeup transition buffer address below 4GB.\r
126 @retval 0 Cannot find free memory below 4GB.\r
127**/\r
128UINTN\r
129GetModeTransitionBuffer (\r
130 IN UINTN BufferSize\r
131 )\r
132{\r
133 EFI_STATUS Status;\r
134 EFI_PHYSICAL_ADDRESS StartAddress;\r
135\r
136 StartAddress = BASE_4GB - 1;\r
137 Status = gBS->AllocatePages (\r
138 AllocateMaxAddress,\r
139 EfiBootServicesCode,\r
140 EFI_SIZE_TO_PAGES (BufferSize),\r
141 &StartAddress\r
142 );\r
143 if (EFI_ERROR (Status)) {\r
144 StartAddress = 0;\r
145 }\r
146\r
147 return (UINTN)StartAddress;\r
148}\r
149\r
96378861
JF
150/**\r
151 Checks APs status and updates APs status if needed.\r
152\r
153**/\r
154VOID\r
155CheckAndUpdateApsStatus (\r
156 VOID\r
157 )\r
158{\r
08085f08
JF
159 UINTN ProcessorNumber;\r
160 EFI_STATUS Status;\r
161 CPU_MP_DATA *CpuMpData;\r
162\r
163 CpuMpData = GetCpuMpData ();\r
164\r
165 //\r
166 // First, check whether pending StartupAllAPs() exists.\r
167 //\r
168 if (CpuMpData->WaitEvent != NULL) {\r
169\r
170 Status = CheckAllAPs ();\r
171 //\r
172 // If all APs finish for StartupAllAPs(), signal the WaitEvent for it.\r
173 //\r
174 if (Status != EFI_NOT_READY) {\r
175 Status = gBS->SignalEvent (CpuMpData->WaitEvent);\r
176 CpuMpData->WaitEvent = NULL;\r
177 }\r
178 }\r
179\r
180 //\r
181 // Second, check whether pending StartupThisAPs() callings exist.\r
182 //\r
183 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {\r
184\r
185 if (CpuMpData->CpuData[ProcessorNumber].WaitEvent == NULL) {\r
186 continue;\r
187 }\r
188\r
189 Status = CheckThisAP (ProcessorNumber);\r
190\r
191 if (Status != EFI_NOT_READY) {\r
192 gBS->SignalEvent (CpuMpData->CpuData[ProcessorNumber].WaitEvent);\r
193 CpuMpData->CpuData[ProcessorNumber].WaitEvent = NULL;\r
194 }\r
195 }\r
96378861
JF
196}\r
197\r
198/**\r
199 Checks APs' status periodically.\r
200\r
438f1766 201 This function is triggered by timer periodically to check the\r
96378861
JF
202 state of APs for StartupAllAPs() and StartupThisAP() executed\r
203 in non-blocking mode.\r
204\r
205 @param[in] Event Event triggered.\r
206 @param[in] Context Parameter passed with the event.\r
207\r
208**/\r
209VOID\r
210EFIAPI\r
211CheckApsStatus (\r
212 IN EFI_EVENT Event,\r
213 IN VOID *Context\r
214 )\r
215{\r
216 //\r
217 // If CheckApsStatus() is not stopped, otherwise return immediately.\r
218 //\r
219 if (!mStopCheckAllApsStatus) {\r
220 CheckAndUpdateApsStatus ();\r
221 }\r
222}\r
ed66e0e3 223\r
4d3314f6
JF
224/**\r
225 Get Protected mode code segment from current GDT table.\r
226\r
b31c1ad1 227 @return Protected mode code segment value.\r
4d3314f6
JF
228**/\r
229UINT16\r
230GetProtectedModeCS (\r
231 VOID\r
232 )\r
233{\r
234 IA32_DESCRIPTOR GdtrDesc;\r
235 IA32_SEGMENT_DESCRIPTOR *GdtEntry;\r
236 UINTN GdtEntryCount;\r
237 UINT16 Index;\r
238\r
239 Index = (UINT16) -1;\r
240 AsmReadGdtr (&GdtrDesc);\r
241 GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);\r
242 GdtEntry = (IA32_SEGMENT_DESCRIPTOR *) GdtrDesc.Base;\r
243 for (Index = 0; Index < GdtEntryCount; Index++) {\r
244 if (GdtEntry->Bits.L == 0) {\r
245 if (GdtEntry->Bits.Type > 8 && GdtEntry->Bits.L == 0) {\r
246 break;\r
247 }\r
248 }\r
249 GdtEntry++;\r
250 }\r
251 ASSERT (Index != -1);\r
252 return Index * 8;\r
253}\r
254\r
255/**\r
256 Do sync on APs.\r
257\r
258 @param[in, out] Buffer Pointer to private data buffer.\r
259**/\r
260VOID\r
261EFIAPI\r
262RelocateApLoop (\r
263 IN OUT VOID *Buffer\r
264 )\r
265{\r
266 CPU_MP_DATA *CpuMpData;\r
267 BOOLEAN MwaitSupport;\r
268 ASM_RELOCATE_AP_LOOP AsmRelocateApLoopFunc;\r
bf2786dc 269 UINTN ProcessorNumber;\r
4d3314f6 270\r
7367cc6c 271 MpInitLibWhoAmI (&ProcessorNumber);\r
4d3314f6
JF
272 CpuMpData = GetCpuMpData ();\r
273 MwaitSupport = IsMwaitSupport ();\r
081f6416 274 AsmRelocateApLoopFunc = (ASM_RELOCATE_AP_LOOP) (UINTN) mReservedApLoopFunc;\r
bf2786dc
JF
275 AsmRelocateApLoopFunc (\r
276 MwaitSupport,\r
277 CpuMpData->ApTargetCState,\r
278 CpuMpData->PmCodeSegment,\r
9f91cb01
JF
279 mReservedTopOfApStack - ProcessorNumber * AP_SAFE_STACK_SIZE,\r
280 (UINTN) &mNumberToFinish\r
bf2786dc 281 );\r
4d3314f6
JF
282 //\r
283 // It should never reach here\r
284 //\r
285 ASSERT (FALSE);\r
286}\r
287\r
288/**\r
289 Callback function for ExitBootServices.\r
290\r
291 @param[in] Event Event whose notification function is being invoked.\r
292 @param[in] Context The pointer to the notification function's context,\r
293 which is implementation-dependent.\r
294\r
295**/\r
296VOID\r
297EFIAPI\r
86af2eb8 298MpInitChangeApLoopCallback (\r
4d3314f6
JF
299 IN EFI_EVENT Event,\r
300 IN VOID *Context\r
301 )\r
302{\r
303 CPU_MP_DATA *CpuMpData;\r
5183fb37 304\r
4d3314f6
JF
305 CpuMpData = GetCpuMpData ();\r
306 CpuMpData->PmCodeSegment = GetProtectedModeCS ();\r
307 CpuMpData->ApLoopMode = PcdGet8 (PcdCpuApLoopMode);\r
9f91cb01 308 mNumberToFinish = CpuMpData->CpuCount - 1;\r
cf4e79e4 309 WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, NULL, TRUE);\r
9f91cb01
JF
310 while (mNumberToFinish > 0) {\r
311 CpuPause ();\r
312 }\r
86af2eb8 313 DEBUG ((DEBUG_INFO, "%a() done!\n", __FUNCTION__));\r
4d3314f6
JF
314}\r
315\r
93ca4c0f
JF
316/**\r
317 Initialize global data for MP support.\r
318\r
319 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
320**/\r
321VOID\r
322InitMpGlobalData (\r
323 IN CPU_MP_DATA *CpuMpData\r
324 )\r
325{\r
15720a6c
JW
326 EFI_STATUS Status;\r
327 EFI_PHYSICAL_ADDRESS Address;\r
328 UINTN ApSafeBufferSize;\r
329 UINTN Index;\r
330 EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc;\r
331 UINTN StackBase;\r
52315261 332 CPU_INFO_IN_HOB *CpuInfoInHob;\r
96378861 333\r
93ca4c0f
JF
334 SaveCpuMpData (CpuMpData);\r
335\r
14e8137c
JF
336 if (CpuMpData->CpuCount == 1) {\r
337 //\r
338 // If only BSP exists, return\r
339 //\r
340 return;\r
341 }\r
342\r
15720a6c
JW
343 if (PcdGetBool (PcdCpuStackGuard)) {\r
344 //\r
345 // One extra page at the bottom of the stack is needed for Guard page.\r
346 //\r
347 if (CpuMpData->CpuApStackSize <= EFI_PAGE_SIZE) {\r
348 DEBUG ((DEBUG_ERROR, "PcdCpuApStackSize is not big enough for Stack Guard!\n"));\r
349 ASSERT (FALSE);\r
350 }\r
351\r
52315261
JW
352 //\r
353 // DXE will reuse stack allocated for APs at PEI phase if it's available.\r
354 // Let's check it here.\r
355 //\r
356 // Note: BSP's stack guard is set at DxeIpl phase. But for the sake of\r
357 // BSP/AP exchange, stack guard for ApTopOfStack of cpu 0 will still be\r
358 // set here.\r
359 //\r
360 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;\r
15720a6c 361 for (Index = 0; Index < CpuMpData->CpuCount; ++Index) {\r
52315261 362 if (CpuInfoInHob != NULL && CpuInfoInHob[Index].ApTopOfStack != 0) {\r
20737c2f 363 StackBase = (UINTN)CpuInfoInHob[Index].ApTopOfStack - CpuMpData->CpuApStackSize;\r
52315261
JW
364 } else {\r
365 StackBase = CpuMpData->Buffer + Index * CpuMpData->CpuApStackSize;\r
366 }\r
15720a6c
JW
367\r
368 Status = gDS->GetMemorySpaceDescriptor (StackBase, &MemDesc);\r
369 ASSERT_EFI_ERROR (Status);\r
370\r
371 Status = gDS->SetMemorySpaceAttributes (\r
372 StackBase,\r
373 EFI_PAGES_TO_SIZE (1),\r
374 MemDesc.Attributes | EFI_MEMORY_RP\r
375 );\r
376 ASSERT_EFI_ERROR (Status);\r
52315261
JW
377\r
378 DEBUG ((DEBUG_INFO, "Stack Guard set at %lx [cpu%lu]!\n",\r
379 (UINT64)StackBase, (UINT64)Index));\r
15720a6c
JW
380 }\r
381 }\r
382\r
5183fb37 383 //\r
ffd6b0b1
JF
384 // Avoid APs access invalid buffer data which allocated by BootServices,\r
385 // so we will allocate reserved data for AP loop code. We also need to\r
386 // allocate this buffer below 4GB due to APs may be transferred to 32bit\r
387 // protected mode on long mode DXE.\r
5183fb37
JF
388 // Allocating it in advance since memory services are not available in\r
389 // Exit Boot Services callback function.\r
390 //\r
bc2288f5
JW
391 ApSafeBufferSize = EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (\r
392 CpuMpData->AddressMap.RelocateApLoopFuncSize\r
393 ));\r
ffd6b0b1
JF
394 Address = BASE_4GB - 1;\r
395 Status = gBS->AllocatePages (\r
396 AllocateMaxAddress,\r
397 EfiReservedMemoryType,\r
bf2786dc 398 EFI_SIZE_TO_PAGES (ApSafeBufferSize),\r
ffd6b0b1
JF
399 &Address\r
400 );\r
401 ASSERT_EFI_ERROR (Status);\r
bc2288f5 402\r
ffd6b0b1 403 mReservedApLoopFunc = (VOID *) (UINTN) Address;\r
5183fb37 404 ASSERT (mReservedApLoopFunc != NULL);\r
bc2288f5
JW
405\r
406 //\r
407 // Make sure that the buffer memory is executable if NX protection is enabled\r
408 // for EfiReservedMemoryType.\r
7367cc6c 409 //\r
bc2288f5
JW
410 // TODO: Check EFI_MEMORY_XP bit set or not once it's available in DXE GCD\r
411 // service.\r
412 //\r
413 Status = gDS->GetMemorySpaceDescriptor (Address, &MemDesc);\r
414 if (!EFI_ERROR (Status)) {\r
415 gDS->SetMemorySpaceAttributes (\r
416 Address,\r
417 ApSafeBufferSize,\r
418 MemDesc.Attributes & (~EFI_MEMORY_XP)\r
419 );\r
420 }\r
421\r
422 ApSafeBufferSize = EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (\r
423 CpuMpData->CpuCount * AP_SAFE_STACK_SIZE\r
424 ));\r
425 Address = BASE_4GB - 1;\r
426 Status = gBS->AllocatePages (\r
427 AllocateMaxAddress,\r
428 EfiReservedMemoryType,\r
429 EFI_SIZE_TO_PAGES (ApSafeBufferSize),\r
430 &Address\r
431 );\r
432 ASSERT_EFI_ERROR (Status);\r
433\r
434 mReservedTopOfApStack = (UINTN) Address + ApSafeBufferSize;\r
bf2786dc 435 ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) == 0);\r
ffd6b0b1
JF
436 CopyMem (\r
437 mReservedApLoopFunc,\r
438 CpuMpData->AddressMap.RelocateApLoopFuncAddress,\r
439 CpuMpData->AddressMap.RelocateApLoopFuncSize\r
440 );\r
5183fb37 441\r
96378861
JF
442 Status = gBS->CreateEvent (\r
443 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
444 TPL_NOTIFY,\r
445 CheckApsStatus,\r
446 NULL,\r
447 &mCheckAllApsEvent\r
448 );\r
449 ASSERT_EFI_ERROR (Status);\r
450\r
451 //\r
452 // Set timer to check all APs status.\r
453 //\r
454 Status = gBS->SetTimer (\r
455 mCheckAllApsEvent,\r
456 TimerPeriodic,\r
457 AP_CHECK_INTERVAL\r
458 );\r
459 ASSERT_EFI_ERROR (Status);\r
8677a56a 460\r
4d3314f6
JF
461 Status = gBS->CreateEvent (\r
462 EVT_SIGNAL_EXIT_BOOT_SERVICES,\r
463 TPL_CALLBACK,\r
86af2eb8 464 MpInitChangeApLoopCallback,\r
4d3314f6
JF
465 NULL,\r
466 &mMpInitExitBootServicesEvent\r
467 );\r
468 ASSERT_EFI_ERROR (Status);\r
8677a56a
JF
469\r
470 Status = gBS->CreateEventEx (\r
471 EVT_NOTIFY_SIGNAL,\r
472 TPL_CALLBACK,\r
473 MpInitChangeApLoopCallback,\r
474 NULL,\r
475 &gEfiEventLegacyBootGuid,\r
476 &mLegacyBootEvent\r
477 );\r
478 ASSERT_EFI_ERROR (Status);\r
93ca4c0f 479}\r
3e8ad6bd
JF
480\r
481/**\r
482 This service executes a caller provided function on all enabled APs.\r
483\r
484 @param[in] Procedure A pointer to the function to be run on\r
485 enabled APs of the system. See type\r
486 EFI_AP_PROCEDURE.\r
487 @param[in] SingleThread If TRUE, then all the enabled APs execute\r
488 the function specified by Procedure one by\r
489 one, in ascending order of processor handle\r
490 number. If FALSE, then all the enabled APs\r
491 execute the function specified by Procedure\r
492 simultaneously.\r
493 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
494 service. If it is NULL, then execute in\r
495 blocking mode. BSP waits until all APs finish\r
496 or TimeoutInMicroSeconds expires. If it's\r
497 not NULL, then execute in non-blocking mode.\r
498 BSP requests the function specified by\r
499 Procedure to be started on all the enabled\r
500 APs, and go on executing immediately. If\r
501 all return from Procedure, or TimeoutInMicroSeconds\r
502 expires, this event is signaled. The BSP\r
503 can use the CheckEvent() or WaitForEvent()\r
504 services to check the state of event. Type\r
505 EFI_EVENT is defined in CreateEvent() in\r
506 the Unified Extensible Firmware Interface\r
507 Specification.\r
367284e7 508 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
3e8ad6bd
JF
509 APs to return from Procedure, either for\r
510 blocking or non-blocking mode. Zero means\r
511 infinity. If the timeout expires before\r
512 all APs return from Procedure, then Procedure\r
513 on the failed APs is terminated. All enabled\r
514 APs are available for next function assigned\r
515 by MpInitLibStartupAllAPs() or\r
516 MPInitLibStartupThisAP().\r
517 If the timeout expires in blocking mode,\r
518 BSP returns EFI_TIMEOUT. If the timeout\r
519 expires in non-blocking mode, WaitEvent\r
520 is signaled with SignalEvent().\r
521 @param[in] ProcedureArgument The parameter passed into Procedure for\r
522 all APs.\r
523 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,\r
524 if all APs finish successfully, then its\r
525 content is set to NULL. If not all APs\r
526 finish before timeout expires, then its\r
527 content is set to address of the buffer\r
528 holding handle numbers of the failed APs.\r
529 The buffer is allocated by MP Initialization\r
530 library, and it's the caller's responsibility to\r
531 free the buffer with FreePool() service.\r
532 In blocking mode, it is ready for consumption\r
533 when the call returns. In non-blocking mode,\r
534 it is ready when WaitEvent is signaled. The\r
535 list of failed CPU is terminated by\r
536 END_OF_CPU_LIST.\r
537\r
538 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
539 the timeout expired.\r
540 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
541 to all enabled APs.\r
542 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
543 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
544 signaled.\r
545 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not\r
546 supported.\r
547 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
548 @retval EFI_NOT_STARTED No enabled APs exist in the system.\r
549 @retval EFI_NOT_READY Any enabled APs are busy.\r
550 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
551 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
552 all enabled APs have finished.\r
553 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
554\r
555**/\r
556EFI_STATUS\r
557EFIAPI\r
558MpInitLibStartupAllAPs (\r
559 IN EFI_AP_PROCEDURE Procedure,\r
560 IN BOOLEAN SingleThread,\r
561 IN EFI_EVENT WaitEvent OPTIONAL,\r
562 IN UINTN TimeoutInMicroseconds,\r
563 IN VOID *ProcedureArgument OPTIONAL,\r
564 OUT UINTN **FailedCpuList OPTIONAL\r
565 )\r
566{\r
86efe976
JF
567 EFI_STATUS Status;\r
568\r
569 //\r
570 // Temporarily stop checkAllApsStatus for avoid resource dead-lock.\r
571 //\r
572 mStopCheckAllApsStatus = TRUE;\r
573\r
574 Status = StartupAllAPsWorker (\r
575 Procedure,\r
576 SingleThread,\r
577 WaitEvent,\r
578 TimeoutInMicroseconds,\r
579 ProcedureArgument,\r
580 FailedCpuList\r
581 );\r
582\r
583 //\r
584 // Start checkAllApsStatus\r
585 //\r
586 mStopCheckAllApsStatus = FALSE;\r
587\r
588 return Status;\r
3e8ad6bd
JF
589}\r
590\r
591/**\r
592 This service lets the caller get one enabled AP to execute a caller-provided\r
593 function.\r
594\r
595 @param[in] Procedure A pointer to the function to be run on the\r
596 designated AP of the system. See type\r
597 EFI_AP_PROCEDURE.\r
598 @param[in] ProcessorNumber The handle number of the AP. The range is\r
599 from 0 to the total number of logical\r
600 processors minus 1. The total number of\r
601 logical processors can be retrieved by\r
602 MpInitLibGetNumberOfProcessors().\r
603 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
604 service. If it is NULL, then execute in\r
605 blocking mode. BSP waits until this AP finish\r
606 or TimeoutInMicroSeconds expires. If it's\r
607 not NULL, then execute in non-blocking mode.\r
608 BSP requests the function specified by\r
609 Procedure to be started on this AP,\r
610 and go on executing immediately. If this AP\r
611 return from Procedure or TimeoutInMicroSeconds\r
612 expires, this event is signaled. The BSP\r
613 can use the CheckEvent() or WaitForEvent()\r
614 services to check the state of event. Type\r
615 EFI_EVENT is defined in CreateEvent() in\r
616 the Unified Extensible Firmware Interface\r
617 Specification.\r
367284e7 618 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
3e8ad6bd
JF
619 this AP to finish this Procedure, either for\r
620 blocking or non-blocking mode. Zero means\r
621 infinity. If the timeout expires before\r
622 this AP returns from Procedure, then Procedure\r
623 on the AP is terminated. The\r
624 AP is available for next function assigned\r
625 by MpInitLibStartupAllAPs() or\r
626 MpInitLibStartupThisAP().\r
627 If the timeout expires in blocking mode,\r
628 BSP returns EFI_TIMEOUT. If the timeout\r
629 expires in non-blocking mode, WaitEvent\r
630 is signaled with SignalEvent().\r
631 @param[in] ProcedureArgument The parameter passed into Procedure on the\r
632 specified AP.\r
633 @param[out] Finished If NULL, this parameter is ignored. In\r
634 blocking mode, this parameter is ignored.\r
635 In non-blocking mode, if AP returns from\r
636 Procedure before the timeout expires, its\r
637 content is set to TRUE. Otherwise, the\r
638 value is set to FALSE. The caller can\r
639 determine if the AP returned from Procedure\r
640 by evaluating this value.\r
641\r
642 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
643 the timeout expires.\r
644 @retval EFI_SUCCESS In non-blocking mode, the function has been\r
645 dispatched to specified AP.\r
646 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
647 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
648 signaled.\r
649 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not\r
650 supported.\r
651 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
652 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
653 the specified AP has finished.\r
654 @retval EFI_NOT_READY The specified AP is busy.\r
655 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
656 @retval EFI_NOT_FOUND The processor with the handle specified by\r
657 ProcessorNumber does not exist.\r
658 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
659 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
660\r
661**/\r
662EFI_STATUS\r
663EFIAPI\r
664MpInitLibStartupThisAP (\r
665 IN EFI_AP_PROCEDURE Procedure,\r
666 IN UINTN ProcessorNumber,\r
667 IN EFI_EVENT WaitEvent OPTIONAL,\r
668 IN UINTN TimeoutInMicroseconds,\r
669 IN VOID *ProcedureArgument OPTIONAL,\r
670 OUT BOOLEAN *Finished OPTIONAL\r
671 )\r
672{\r
20ae5774
JF
673 EFI_STATUS Status;\r
674\r
675 //\r
676 // temporarily stop checkAllApsStatus for avoid resource dead-lock.\r
677 //\r
678 mStopCheckAllApsStatus = TRUE;\r
679\r
680 Status = StartupThisAPWorker (\r
681 Procedure,\r
682 ProcessorNumber,\r
683 WaitEvent,\r
684 TimeoutInMicroseconds,\r
685 ProcedureArgument,\r
686 Finished\r
687 );\r
688\r
689 mStopCheckAllApsStatus = FALSE;\r
690\r
691 return Status;\r
3e8ad6bd
JF
692}\r
693\r
694/**\r
695 This service switches the requested AP to be the BSP from that point onward.\r
696 This service changes the BSP for all purposes. This call can only be performed\r
697 by the current BSP.\r
698\r
699 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
700 BSP. The range is from 0 to the total number of\r
701 logical processors minus 1. The total number of\r
702 logical processors can be retrieved by\r
703 MpInitLibGetNumberOfProcessors().\r
704 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
705 enabled AP. Otherwise, it will be disabled.\r
706\r
707 @retval EFI_SUCCESS BSP successfully switched.\r
708 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to\r
709 this service returning.\r
710 @retval EFI_UNSUPPORTED Switching the BSP is not supported.\r
711 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
712 @retval EFI_NOT_FOUND The processor with the handle specified by\r
713 ProcessorNumber does not exist.\r
714 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or\r
715 a disabled AP.\r
716 @retval EFI_NOT_READY The specified AP is busy.\r
717 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
718\r
719**/\r
720EFI_STATUS\r
721EFIAPI\r
722MpInitLibSwitchBSP (\r
723 IN UINTN ProcessorNumber,\r
724 IN BOOLEAN EnableOldBSP\r
725 )\r
726{\r
b6e45716
JF
727 EFI_STATUS Status;\r
728 EFI_TIMER_ARCH_PROTOCOL *Timer;\r
729 UINT64 TimerPeriod;\r
41be0da5 730\r
8ad05bd2 731 TimerPeriod = 0;\r
b6e45716
JF
732 //\r
733 // Locate Timer Arch Protocol\r
734 //\r
735 Status = gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID **) &Timer);\r
736 if (EFI_ERROR (Status)) {\r
737 Timer = NULL;\r
738 }\r
739\r
740 if (Timer != NULL) {\r
741 //\r
742 // Save current rate of DXE Timer\r
743 //\r
744 Timer->GetTimerPeriod (Timer, &TimerPeriod);\r
745 //\r
746 // Disable DXE Timer and drain pending interrupts\r
747 //\r
748 Timer->SetTimerPeriod (Timer, 0);\r
749 }\r
41be0da5
JF
750\r
751 Status = SwitchBSPWorker (ProcessorNumber, EnableOldBSP);\r
752\r
b6e45716
JF
753 if (Timer != NULL) {\r
754 //\r
755 // Enable and restore rate of DXE Timer\r
756 //\r
757 Timer->SetTimerPeriod (Timer, TimerPeriod);\r
758 }\r
759\r
41be0da5 760 return Status;\r
3e8ad6bd
JF
761}\r
762\r
763/**\r
764 This service lets the caller enable or disable an AP from this point onward.\r
765 This service may only be called from the BSP.\r
766\r
767 @param[in] ProcessorNumber The handle number of AP.\r
768 The range is from 0 to the total number of\r
769 logical processors minus 1. The total number of\r
770 logical processors can be retrieved by\r
771 MpInitLibGetNumberOfProcessors().\r
772 @param[in] EnableAP Specifies the new state for the processor for\r
773 enabled, FALSE for disabled.\r
774 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
775 the new health status of the AP. This flag\r
776 corresponds to StatusFlag defined in\r
777 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r
778 the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
779 bits are ignored. If it is NULL, this parameter\r
780 is ignored.\r
781\r
782 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
783 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed\r
784 prior to this service returning.\r
785 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
786 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
787 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
788 does not exist.\r
789 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
790 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
791\r
792**/\r
793EFI_STATUS\r
794EFIAPI\r
795MpInitLibEnableDisableAP (\r
796 IN UINTN ProcessorNumber,\r
797 IN BOOLEAN EnableAP,\r
798 IN UINT32 *HealthFlag OPTIONAL\r
799 )\r
800{\r
e37109bc
JF
801 EFI_STATUS Status;\r
802 BOOLEAN TempStopCheckState;\r
803\r
804 TempStopCheckState = FALSE;\r
805 //\r
806 // temporarily stop checkAllAPsStatus for initialize parameters.\r
807 //\r
808 if (!mStopCheckAllApsStatus) {\r
809 mStopCheckAllApsStatus = TRUE;\r
810 TempStopCheckState = TRUE;\r
811 }\r
812\r
813 Status = EnableDisableApWorker (ProcessorNumber, EnableAP, HealthFlag);\r
814\r
815 if (TempStopCheckState) {\r
816 mStopCheckAllApsStatus = FALSE;\r
817 }\r
818\r
819 return Status;\r
3e8ad6bd 820}\r