]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
UefiCpuPkg/MpInitLib: Fix ASSERT for success.
[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
4d3314f6
JF
239 AsmReadGdtr (&GdtrDesc);\r
240 GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);\r
241 GdtEntry = (IA32_SEGMENT_DESCRIPTOR *) GdtrDesc.Base;\r
242 for (Index = 0; Index < GdtEntryCount; Index++) {\r
243 if (GdtEntry->Bits.L == 0) {\r
244 if (GdtEntry->Bits.Type > 8 && GdtEntry->Bits.L == 0) {\r
245 break;\r
246 }\r
247 }\r
248 GdtEntry++;\r
249 }\r
37fba7c2 250 ASSERT (Index != GdtEntryCount);\r
4d3314f6
JF
251 return Index * 8;\r
252}\r
253\r
254/**\r
255 Do sync on APs.\r
256\r
257 @param[in, out] Buffer Pointer to private data buffer.\r
258**/\r
259VOID\r
260EFIAPI\r
261RelocateApLoop (\r
262 IN OUT VOID *Buffer\r
263 )\r
264{\r
265 CPU_MP_DATA *CpuMpData;\r
266 BOOLEAN MwaitSupport;\r
267 ASM_RELOCATE_AP_LOOP AsmRelocateApLoopFunc;\r
bf2786dc 268 UINTN ProcessorNumber;\r
4d3314f6 269\r
7367cc6c 270 MpInitLibWhoAmI (&ProcessorNumber);\r
4d3314f6
JF
271 CpuMpData = GetCpuMpData ();\r
272 MwaitSupport = IsMwaitSupport ();\r
081f6416 273 AsmRelocateApLoopFunc = (ASM_RELOCATE_AP_LOOP) (UINTN) mReservedApLoopFunc;\r
bf2786dc
JF
274 AsmRelocateApLoopFunc (\r
275 MwaitSupport,\r
276 CpuMpData->ApTargetCState,\r
277 CpuMpData->PmCodeSegment,\r
9f91cb01
JF
278 mReservedTopOfApStack - ProcessorNumber * AP_SAFE_STACK_SIZE,\r
279 (UINTN) &mNumberToFinish\r
bf2786dc 280 );\r
4d3314f6
JF
281 //\r
282 // It should never reach here\r
283 //\r
284 ASSERT (FALSE);\r
285}\r
286\r
287/**\r
288 Callback function for ExitBootServices.\r
289\r
290 @param[in] Event Event whose notification function is being invoked.\r
291 @param[in] Context The pointer to the notification function's context,\r
292 which is implementation-dependent.\r
293\r
294**/\r
295VOID\r
296EFIAPI\r
86af2eb8 297MpInitChangeApLoopCallback (\r
4d3314f6
JF
298 IN EFI_EVENT Event,\r
299 IN VOID *Context\r
300 )\r
301{\r
302 CPU_MP_DATA *CpuMpData;\r
5183fb37 303\r
4d3314f6
JF
304 CpuMpData = GetCpuMpData ();\r
305 CpuMpData->PmCodeSegment = GetProtectedModeCS ();\r
306 CpuMpData->ApLoopMode = PcdGet8 (PcdCpuApLoopMode);\r
9f91cb01 307 mNumberToFinish = CpuMpData->CpuCount - 1;\r
cf4e79e4 308 WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, NULL, TRUE);\r
9f91cb01
JF
309 while (mNumberToFinish > 0) {\r
310 CpuPause ();\r
311 }\r
86af2eb8 312 DEBUG ((DEBUG_INFO, "%a() done!\n", __FUNCTION__));\r
4d3314f6
JF
313}\r
314\r
93ca4c0f
JF
315/**\r
316 Initialize global data for MP support.\r
317\r
318 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
319**/\r
320VOID\r
321InitMpGlobalData (\r
322 IN CPU_MP_DATA *CpuMpData\r
323 )\r
324{\r
15720a6c
JW
325 EFI_STATUS Status;\r
326 EFI_PHYSICAL_ADDRESS Address;\r
327 UINTN ApSafeBufferSize;\r
328 UINTN Index;\r
329 EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc;\r
330 UINTN StackBase;\r
52315261 331 CPU_INFO_IN_HOB *CpuInfoInHob;\r
96378861 332\r
93ca4c0f
JF
333 SaveCpuMpData (CpuMpData);\r
334\r
14e8137c
JF
335 if (CpuMpData->CpuCount == 1) {\r
336 //\r
337 // If only BSP exists, return\r
338 //\r
339 return;\r
340 }\r
341\r
15720a6c
JW
342 if (PcdGetBool (PcdCpuStackGuard)) {\r
343 //\r
344 // One extra page at the bottom of the stack is needed for Guard page.\r
345 //\r
346 if (CpuMpData->CpuApStackSize <= EFI_PAGE_SIZE) {\r
347 DEBUG ((DEBUG_ERROR, "PcdCpuApStackSize is not big enough for Stack Guard!\n"));\r
348 ASSERT (FALSE);\r
349 }\r
350\r
52315261
JW
351 //\r
352 // DXE will reuse stack allocated for APs at PEI phase if it's available.\r
353 // Let's check it here.\r
354 //\r
355 // Note: BSP's stack guard is set at DxeIpl phase. But for the sake of\r
356 // BSP/AP exchange, stack guard for ApTopOfStack of cpu 0 will still be\r
357 // set here.\r
358 //\r
359 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;\r
15720a6c 360 for (Index = 0; Index < CpuMpData->CpuCount; ++Index) {\r
52315261 361 if (CpuInfoInHob != NULL && CpuInfoInHob[Index].ApTopOfStack != 0) {\r
20737c2f 362 StackBase = (UINTN)CpuInfoInHob[Index].ApTopOfStack - CpuMpData->CpuApStackSize;\r
52315261
JW
363 } else {\r
364 StackBase = CpuMpData->Buffer + Index * CpuMpData->CpuApStackSize;\r
365 }\r
15720a6c
JW
366\r
367 Status = gDS->GetMemorySpaceDescriptor (StackBase, &MemDesc);\r
368 ASSERT_EFI_ERROR (Status);\r
369\r
370 Status = gDS->SetMemorySpaceAttributes (\r
371 StackBase,\r
372 EFI_PAGES_TO_SIZE (1),\r
373 MemDesc.Attributes | EFI_MEMORY_RP\r
374 );\r
375 ASSERT_EFI_ERROR (Status);\r
52315261
JW
376\r
377 DEBUG ((DEBUG_INFO, "Stack Guard set at %lx [cpu%lu]!\n",\r
378 (UINT64)StackBase, (UINT64)Index));\r
15720a6c
JW
379 }\r
380 }\r
381\r
5183fb37 382 //\r
ffd6b0b1
JF
383 // Avoid APs access invalid buffer data which allocated by BootServices,\r
384 // so we will allocate reserved data for AP loop code. We also need to\r
385 // allocate this buffer below 4GB due to APs may be transferred to 32bit\r
386 // protected mode on long mode DXE.\r
5183fb37
JF
387 // Allocating it in advance since memory services are not available in\r
388 // Exit Boot Services callback function.\r
389 //\r
bc2288f5
JW
390 ApSafeBufferSize = EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (\r
391 CpuMpData->AddressMap.RelocateApLoopFuncSize\r
392 ));\r
ffd6b0b1
JF
393 Address = BASE_4GB - 1;\r
394 Status = gBS->AllocatePages (\r
395 AllocateMaxAddress,\r
396 EfiReservedMemoryType,\r
bf2786dc 397 EFI_SIZE_TO_PAGES (ApSafeBufferSize),\r
ffd6b0b1
JF
398 &Address\r
399 );\r
400 ASSERT_EFI_ERROR (Status);\r
bc2288f5 401\r
ffd6b0b1 402 mReservedApLoopFunc = (VOID *) (UINTN) Address;\r
5183fb37 403 ASSERT (mReservedApLoopFunc != NULL);\r
bc2288f5
JW
404\r
405 //\r
406 // Make sure that the buffer memory is executable if NX protection is enabled\r
407 // for EfiReservedMemoryType.\r
7367cc6c 408 //\r
bc2288f5
JW
409 // TODO: Check EFI_MEMORY_XP bit set or not once it's available in DXE GCD\r
410 // service.\r
411 //\r
412 Status = gDS->GetMemorySpaceDescriptor (Address, &MemDesc);\r
413 if (!EFI_ERROR (Status)) {\r
414 gDS->SetMemorySpaceAttributes (\r
415 Address,\r
416 ApSafeBufferSize,\r
417 MemDesc.Attributes & (~EFI_MEMORY_XP)\r
418 );\r
419 }\r
420\r
421 ApSafeBufferSize = EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (\r
422 CpuMpData->CpuCount * AP_SAFE_STACK_SIZE\r
423 ));\r
424 Address = BASE_4GB - 1;\r
425 Status = gBS->AllocatePages (\r
426 AllocateMaxAddress,\r
427 EfiReservedMemoryType,\r
428 EFI_SIZE_TO_PAGES (ApSafeBufferSize),\r
429 &Address\r
430 );\r
431 ASSERT_EFI_ERROR (Status);\r
432\r
433 mReservedTopOfApStack = (UINTN) Address + ApSafeBufferSize;\r
bf2786dc 434 ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) == 0);\r
ffd6b0b1
JF
435 CopyMem (\r
436 mReservedApLoopFunc,\r
437 CpuMpData->AddressMap.RelocateApLoopFuncAddress,\r
438 CpuMpData->AddressMap.RelocateApLoopFuncSize\r
439 );\r
5183fb37 440\r
96378861
JF
441 Status = gBS->CreateEvent (\r
442 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
443 TPL_NOTIFY,\r
444 CheckApsStatus,\r
445 NULL,\r
446 &mCheckAllApsEvent\r
447 );\r
448 ASSERT_EFI_ERROR (Status);\r
449\r
450 //\r
451 // Set timer to check all APs status.\r
452 //\r
453 Status = gBS->SetTimer (\r
454 mCheckAllApsEvent,\r
455 TimerPeriodic,\r
456 AP_CHECK_INTERVAL\r
457 );\r
458 ASSERT_EFI_ERROR (Status);\r
8677a56a 459\r
4d3314f6
JF
460 Status = gBS->CreateEvent (\r
461 EVT_SIGNAL_EXIT_BOOT_SERVICES,\r
462 TPL_CALLBACK,\r
86af2eb8 463 MpInitChangeApLoopCallback,\r
4d3314f6
JF
464 NULL,\r
465 &mMpInitExitBootServicesEvent\r
466 );\r
467 ASSERT_EFI_ERROR (Status);\r
8677a56a
JF
468\r
469 Status = gBS->CreateEventEx (\r
470 EVT_NOTIFY_SIGNAL,\r
471 TPL_CALLBACK,\r
472 MpInitChangeApLoopCallback,\r
473 NULL,\r
474 &gEfiEventLegacyBootGuid,\r
475 &mLegacyBootEvent\r
476 );\r
477 ASSERT_EFI_ERROR (Status);\r
93ca4c0f 478}\r
3e8ad6bd
JF
479\r
480/**\r
481 This service executes a caller provided function on all enabled APs.\r
482\r
483 @param[in] Procedure A pointer to the function to be run on\r
484 enabled APs of the system. See type\r
485 EFI_AP_PROCEDURE.\r
486 @param[in] SingleThread If TRUE, then all the enabled APs execute\r
487 the function specified by Procedure one by\r
488 one, in ascending order of processor handle\r
489 number. If FALSE, then all the enabled APs\r
490 execute the function specified by Procedure\r
491 simultaneously.\r
492 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
493 service. If it is NULL, then execute in\r
494 blocking mode. BSP waits until all APs finish\r
495 or TimeoutInMicroSeconds expires. If it's\r
496 not NULL, then execute in non-blocking mode.\r
497 BSP requests the function specified by\r
498 Procedure to be started on all the enabled\r
499 APs, and go on executing immediately. If\r
500 all return from Procedure, or TimeoutInMicroSeconds\r
501 expires, this event is signaled. The BSP\r
502 can use the CheckEvent() or WaitForEvent()\r
503 services to check the state of event. Type\r
504 EFI_EVENT is defined in CreateEvent() in\r
505 the Unified Extensible Firmware Interface\r
506 Specification.\r
367284e7 507 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
3e8ad6bd
JF
508 APs to return from Procedure, either for\r
509 blocking or non-blocking mode. Zero means\r
510 infinity. If the timeout expires before\r
511 all APs return from Procedure, then Procedure\r
512 on the failed APs is terminated. All enabled\r
513 APs are available for next function assigned\r
514 by MpInitLibStartupAllAPs() or\r
515 MPInitLibStartupThisAP().\r
516 If the timeout expires in blocking mode,\r
517 BSP returns EFI_TIMEOUT. If the timeout\r
518 expires in non-blocking mode, WaitEvent\r
519 is signaled with SignalEvent().\r
520 @param[in] ProcedureArgument The parameter passed into Procedure for\r
521 all APs.\r
522 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,\r
523 if all APs finish successfully, then its\r
524 content is set to NULL. If not all APs\r
525 finish before timeout expires, then its\r
526 content is set to address of the buffer\r
527 holding handle numbers of the failed APs.\r
528 The buffer is allocated by MP Initialization\r
529 library, and it's the caller's responsibility to\r
530 free the buffer with FreePool() service.\r
531 In blocking mode, it is ready for consumption\r
532 when the call returns. In non-blocking mode,\r
533 it is ready when WaitEvent is signaled. The\r
534 list of failed CPU is terminated by\r
535 END_OF_CPU_LIST.\r
536\r
537 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
538 the timeout expired.\r
539 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
540 to all enabled APs.\r
541 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
542 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
543 signaled.\r
544 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not\r
545 supported.\r
546 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
547 @retval EFI_NOT_STARTED No enabled APs exist in the system.\r
548 @retval EFI_NOT_READY Any enabled APs are busy.\r
549 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
550 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
551 all enabled APs have finished.\r
552 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
553\r
554**/\r
555EFI_STATUS\r
556EFIAPI\r
557MpInitLibStartupAllAPs (\r
558 IN EFI_AP_PROCEDURE Procedure,\r
559 IN BOOLEAN SingleThread,\r
560 IN EFI_EVENT WaitEvent OPTIONAL,\r
561 IN UINTN TimeoutInMicroseconds,\r
562 IN VOID *ProcedureArgument OPTIONAL,\r
563 OUT UINTN **FailedCpuList OPTIONAL\r
564 )\r
565{\r
86efe976
JF
566 EFI_STATUS Status;\r
567\r
568 //\r
569 // Temporarily stop checkAllApsStatus for avoid resource dead-lock.\r
570 //\r
571 mStopCheckAllApsStatus = TRUE;\r
572\r
573 Status = StartupAllAPsWorker (\r
574 Procedure,\r
575 SingleThread,\r
576 WaitEvent,\r
577 TimeoutInMicroseconds,\r
578 ProcedureArgument,\r
579 FailedCpuList\r
580 );\r
581\r
582 //\r
583 // Start checkAllApsStatus\r
584 //\r
585 mStopCheckAllApsStatus = FALSE;\r
586\r
587 return Status;\r
3e8ad6bd
JF
588}\r
589\r
590/**\r
591 This service lets the caller get one enabled AP to execute a caller-provided\r
592 function.\r
593\r
594 @param[in] Procedure A pointer to the function to be run on the\r
595 designated AP of the system. See type\r
596 EFI_AP_PROCEDURE.\r
597 @param[in] ProcessorNumber The handle number of the AP. The range is\r
598 from 0 to the total number of logical\r
599 processors minus 1. The total number of\r
600 logical processors can be retrieved by\r
601 MpInitLibGetNumberOfProcessors().\r
602 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
603 service. If it is NULL, then execute in\r
604 blocking mode. BSP waits until this AP finish\r
605 or TimeoutInMicroSeconds expires. If it's\r
606 not NULL, then execute in non-blocking mode.\r
607 BSP requests the function specified by\r
608 Procedure to be started on this AP,\r
609 and go on executing immediately. If this AP\r
610 return from Procedure or TimeoutInMicroSeconds\r
611 expires, this event is signaled. The BSP\r
612 can use the CheckEvent() or WaitForEvent()\r
613 services to check the state of event. Type\r
614 EFI_EVENT is defined in CreateEvent() in\r
615 the Unified Extensible Firmware Interface\r
616 Specification.\r
367284e7 617 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
3e8ad6bd
JF
618 this AP to finish this Procedure, either for\r
619 blocking or non-blocking mode. Zero means\r
620 infinity. If the timeout expires before\r
621 this AP returns from Procedure, then Procedure\r
622 on the AP is terminated. The\r
623 AP is available for next function assigned\r
624 by MpInitLibStartupAllAPs() or\r
625 MpInitLibStartupThisAP().\r
626 If the timeout expires in blocking mode,\r
627 BSP returns EFI_TIMEOUT. If the timeout\r
628 expires in non-blocking mode, WaitEvent\r
629 is signaled with SignalEvent().\r
630 @param[in] ProcedureArgument The parameter passed into Procedure on the\r
631 specified AP.\r
632 @param[out] Finished If NULL, this parameter is ignored. In\r
633 blocking mode, this parameter is ignored.\r
634 In non-blocking mode, if AP returns from\r
635 Procedure before the timeout expires, its\r
636 content is set to TRUE. Otherwise, the\r
637 value is set to FALSE. The caller can\r
638 determine if the AP returned from Procedure\r
639 by evaluating this value.\r
640\r
641 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
642 the timeout expires.\r
643 @retval EFI_SUCCESS In non-blocking mode, the function has been\r
644 dispatched to specified AP.\r
645 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
646 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
647 signaled.\r
648 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not\r
649 supported.\r
650 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
651 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
652 the specified AP has finished.\r
653 @retval EFI_NOT_READY The specified AP is busy.\r
654 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
655 @retval EFI_NOT_FOUND The processor with the handle specified by\r
656 ProcessorNumber does not exist.\r
657 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
658 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
659\r
660**/\r
661EFI_STATUS\r
662EFIAPI\r
663MpInitLibStartupThisAP (\r
664 IN EFI_AP_PROCEDURE Procedure,\r
665 IN UINTN ProcessorNumber,\r
666 IN EFI_EVENT WaitEvent OPTIONAL,\r
667 IN UINTN TimeoutInMicroseconds,\r
668 IN VOID *ProcedureArgument OPTIONAL,\r
669 OUT BOOLEAN *Finished OPTIONAL\r
670 )\r
671{\r
20ae5774
JF
672 EFI_STATUS Status;\r
673\r
674 //\r
675 // temporarily stop checkAllApsStatus for avoid resource dead-lock.\r
676 //\r
677 mStopCheckAllApsStatus = TRUE;\r
678\r
679 Status = StartupThisAPWorker (\r
680 Procedure,\r
681 ProcessorNumber,\r
682 WaitEvent,\r
683 TimeoutInMicroseconds,\r
684 ProcedureArgument,\r
685 Finished\r
686 );\r
687\r
688 mStopCheckAllApsStatus = FALSE;\r
689\r
690 return Status;\r
3e8ad6bd
JF
691}\r
692\r
693/**\r
694 This service switches the requested AP to be the BSP from that point onward.\r
695 This service changes the BSP for all purposes. This call can only be performed\r
696 by the current BSP.\r
697\r
698 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
699 BSP. The range is from 0 to the total number of\r
700 logical processors minus 1. The total number of\r
701 logical processors can be retrieved by\r
702 MpInitLibGetNumberOfProcessors().\r
703 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
704 enabled AP. Otherwise, it will be disabled.\r
705\r
706 @retval EFI_SUCCESS BSP successfully switched.\r
707 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to\r
708 this service returning.\r
709 @retval EFI_UNSUPPORTED Switching the BSP is not supported.\r
710 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
711 @retval EFI_NOT_FOUND The processor with the handle specified by\r
712 ProcessorNumber does not exist.\r
713 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or\r
714 a disabled AP.\r
715 @retval EFI_NOT_READY The specified AP is busy.\r
716 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
717\r
718**/\r
719EFI_STATUS\r
720EFIAPI\r
721MpInitLibSwitchBSP (\r
722 IN UINTN ProcessorNumber,\r
723 IN BOOLEAN EnableOldBSP\r
724 )\r
725{\r
b6e45716
JF
726 EFI_STATUS Status;\r
727 EFI_TIMER_ARCH_PROTOCOL *Timer;\r
728 UINT64 TimerPeriod;\r
41be0da5 729\r
8ad05bd2 730 TimerPeriod = 0;\r
b6e45716
JF
731 //\r
732 // Locate Timer Arch Protocol\r
733 //\r
734 Status = gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID **) &Timer);\r
735 if (EFI_ERROR (Status)) {\r
736 Timer = NULL;\r
737 }\r
738\r
739 if (Timer != NULL) {\r
740 //\r
741 // Save current rate of DXE Timer\r
742 //\r
743 Timer->GetTimerPeriod (Timer, &TimerPeriod);\r
744 //\r
745 // Disable DXE Timer and drain pending interrupts\r
746 //\r
747 Timer->SetTimerPeriod (Timer, 0);\r
748 }\r
41be0da5
JF
749\r
750 Status = SwitchBSPWorker (ProcessorNumber, EnableOldBSP);\r
751\r
b6e45716
JF
752 if (Timer != NULL) {\r
753 //\r
754 // Enable and restore rate of DXE Timer\r
755 //\r
756 Timer->SetTimerPeriod (Timer, TimerPeriod);\r
757 }\r
758\r
41be0da5 759 return Status;\r
3e8ad6bd
JF
760}\r
761\r
762/**\r
763 This service lets the caller enable or disable an AP from this point onward.\r
764 This service may only be called from the BSP.\r
765\r
766 @param[in] ProcessorNumber The handle number of AP.\r
767 The range is from 0 to the total number of\r
768 logical processors minus 1. The total number of\r
769 logical processors can be retrieved by\r
770 MpInitLibGetNumberOfProcessors().\r
771 @param[in] EnableAP Specifies the new state for the processor for\r
772 enabled, FALSE for disabled.\r
773 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
774 the new health status of the AP. This flag\r
775 corresponds to StatusFlag defined in\r
776 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r
777 the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
778 bits are ignored. If it is NULL, this parameter\r
779 is ignored.\r
780\r
781 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
782 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed\r
783 prior to this service returning.\r
784 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
785 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
786 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
787 does not exist.\r
788 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
789 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
790\r
791**/\r
792EFI_STATUS\r
793EFIAPI\r
794MpInitLibEnableDisableAP (\r
795 IN UINTN ProcessorNumber,\r
796 IN BOOLEAN EnableAP,\r
797 IN UINT32 *HealthFlag OPTIONAL\r
798 )\r
799{\r
e37109bc
JF
800 EFI_STATUS Status;\r
801 BOOLEAN TempStopCheckState;\r
802\r
803 TempStopCheckState = FALSE;\r
804 //\r
805 // temporarily stop checkAllAPsStatus for initialize parameters.\r
806 //\r
807 if (!mStopCheckAllApsStatus) {\r
808 mStopCheckAllApsStatus = TRUE;\r
809 TempStopCheckState = TRUE;\r
810 }\r
811\r
812 Status = EnableDisableApWorker (ProcessorNumber, EnableAP, HealthFlag);\r
813\r
814 if (TempStopCheckState) {\r
815 mStopCheckAllApsStatus = FALSE;\r
816 }\r
817\r
818 return Status;\r
3e8ad6bd 819}\r