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