]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
MdeModulePkg/Core: fix too many available pages between BS_Data
[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
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
bf2786dc 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
081f6416 309 WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, NULL);\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
bf2786dc
JF
391 ApSafeBufferSize = CpuMpData->AddressMap.RelocateApLoopFuncSize;\r
392 ApSafeBufferSize += CpuMpData->CpuCount * AP_SAFE_STACK_SIZE;\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
402 mReservedApLoopFunc = (VOID *) (UINTN) Address;\r
5183fb37 403 ASSERT (mReservedApLoopFunc != NULL);\r
bf2786dc
JF
404 mReservedTopOfApStack = (UINTN) Address + EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (ApSafeBufferSize));\r
405 ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) == 0);\r
ffd6b0b1
JF
406 CopyMem (\r
407 mReservedApLoopFunc,\r
408 CpuMpData->AddressMap.RelocateApLoopFuncAddress,\r
409 CpuMpData->AddressMap.RelocateApLoopFuncSize\r
410 );\r
5183fb37 411\r
96378861
JF
412 Status = gBS->CreateEvent (\r
413 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
414 TPL_NOTIFY,\r
415 CheckApsStatus,\r
416 NULL,\r
417 &mCheckAllApsEvent\r
418 );\r
419 ASSERT_EFI_ERROR (Status);\r
420\r
421 //\r
422 // Set timer to check all APs status.\r
423 //\r
424 Status = gBS->SetTimer (\r
425 mCheckAllApsEvent,\r
426 TimerPeriodic,\r
427 AP_CHECK_INTERVAL\r
428 );\r
429 ASSERT_EFI_ERROR (Status);\r
8677a56a 430\r
4d3314f6
JF
431 Status = gBS->CreateEvent (\r
432 EVT_SIGNAL_EXIT_BOOT_SERVICES,\r
433 TPL_CALLBACK,\r
86af2eb8 434 MpInitChangeApLoopCallback,\r
4d3314f6
JF
435 NULL,\r
436 &mMpInitExitBootServicesEvent\r
437 );\r
438 ASSERT_EFI_ERROR (Status);\r
8677a56a
JF
439\r
440 Status = gBS->CreateEventEx (\r
441 EVT_NOTIFY_SIGNAL,\r
442 TPL_CALLBACK,\r
443 MpInitChangeApLoopCallback,\r
444 NULL,\r
445 &gEfiEventLegacyBootGuid,\r
446 &mLegacyBootEvent\r
447 );\r
448 ASSERT_EFI_ERROR (Status);\r
93ca4c0f 449}\r
3e8ad6bd
JF
450\r
451/**\r
452 This service executes a caller provided function on all enabled APs.\r
453\r
454 @param[in] Procedure A pointer to the function to be run on\r
455 enabled APs of the system. See type\r
456 EFI_AP_PROCEDURE.\r
457 @param[in] SingleThread If TRUE, then all the enabled APs execute\r
458 the function specified by Procedure one by\r
459 one, in ascending order of processor handle\r
460 number. If FALSE, then all the enabled APs\r
461 execute the function specified by Procedure\r
462 simultaneously.\r
463 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
464 service. If it is NULL, then execute in\r
465 blocking mode. BSP waits until all APs finish\r
466 or TimeoutInMicroSeconds expires. If it's\r
467 not NULL, then execute in non-blocking mode.\r
468 BSP requests the function specified by\r
469 Procedure to be started on all the enabled\r
470 APs, and go on executing immediately. If\r
471 all return from Procedure, or TimeoutInMicroSeconds\r
472 expires, this event is signaled. The BSP\r
473 can use the CheckEvent() or WaitForEvent()\r
474 services to check the state of event. Type\r
475 EFI_EVENT is defined in CreateEvent() in\r
476 the Unified Extensible Firmware Interface\r
477 Specification.\r
367284e7 478 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
3e8ad6bd
JF
479 APs to return from Procedure, either for\r
480 blocking or non-blocking mode. Zero means\r
481 infinity. If the timeout expires before\r
482 all APs return from Procedure, then Procedure\r
483 on the failed APs is terminated. All enabled\r
484 APs are available for next function assigned\r
485 by MpInitLibStartupAllAPs() or\r
486 MPInitLibStartupThisAP().\r
487 If the timeout expires in blocking mode,\r
488 BSP returns EFI_TIMEOUT. If the timeout\r
489 expires in non-blocking mode, WaitEvent\r
490 is signaled with SignalEvent().\r
491 @param[in] ProcedureArgument The parameter passed into Procedure for\r
492 all APs.\r
493 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,\r
494 if all APs finish successfully, then its\r
495 content is set to NULL. If not all APs\r
496 finish before timeout expires, then its\r
497 content is set to address of the buffer\r
498 holding handle numbers of the failed APs.\r
499 The buffer is allocated by MP Initialization\r
500 library, and it's the caller's responsibility to\r
501 free the buffer with FreePool() service.\r
502 In blocking mode, it is ready for consumption\r
503 when the call returns. In non-blocking mode,\r
504 it is ready when WaitEvent is signaled. The\r
505 list of failed CPU is terminated by\r
506 END_OF_CPU_LIST.\r
507\r
508 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
509 the timeout expired.\r
510 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
511 to all enabled APs.\r
512 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
513 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
514 signaled.\r
515 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not\r
516 supported.\r
517 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
518 @retval EFI_NOT_STARTED No enabled APs exist in the system.\r
519 @retval EFI_NOT_READY Any enabled APs are busy.\r
520 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
521 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
522 all enabled APs have finished.\r
523 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
524\r
525**/\r
526EFI_STATUS\r
527EFIAPI\r
528MpInitLibStartupAllAPs (\r
529 IN EFI_AP_PROCEDURE Procedure,\r
530 IN BOOLEAN SingleThread,\r
531 IN EFI_EVENT WaitEvent OPTIONAL,\r
532 IN UINTN TimeoutInMicroseconds,\r
533 IN VOID *ProcedureArgument OPTIONAL,\r
534 OUT UINTN **FailedCpuList OPTIONAL\r
535 )\r
536{\r
86efe976
JF
537 EFI_STATUS Status;\r
538\r
539 //\r
540 // Temporarily stop checkAllApsStatus for avoid resource dead-lock.\r
541 //\r
542 mStopCheckAllApsStatus = TRUE;\r
543\r
544 Status = StartupAllAPsWorker (\r
545 Procedure,\r
546 SingleThread,\r
547 WaitEvent,\r
548 TimeoutInMicroseconds,\r
549 ProcedureArgument,\r
550 FailedCpuList\r
551 );\r
552\r
553 //\r
554 // Start checkAllApsStatus\r
555 //\r
556 mStopCheckAllApsStatus = FALSE;\r
557\r
558 return Status;\r
3e8ad6bd
JF
559}\r
560\r
561/**\r
562 This service lets the caller get one enabled AP to execute a caller-provided\r
563 function.\r
564\r
565 @param[in] Procedure A pointer to the function to be run on the\r
566 designated AP of the system. See type\r
567 EFI_AP_PROCEDURE.\r
568 @param[in] ProcessorNumber The handle number of the AP. The range is\r
569 from 0 to the total number of logical\r
570 processors minus 1. The total number of\r
571 logical processors can be retrieved by\r
572 MpInitLibGetNumberOfProcessors().\r
573 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
574 service. If it is NULL, then execute in\r
575 blocking mode. BSP waits until this AP finish\r
576 or TimeoutInMicroSeconds expires. If it's\r
577 not NULL, then execute in non-blocking mode.\r
578 BSP requests the function specified by\r
579 Procedure to be started on this AP,\r
580 and go on executing immediately. If this AP\r
581 return from Procedure or TimeoutInMicroSeconds\r
582 expires, this event is signaled. The BSP\r
583 can use the CheckEvent() or WaitForEvent()\r
584 services to check the state of event. Type\r
585 EFI_EVENT is defined in CreateEvent() in\r
586 the Unified Extensible Firmware Interface\r
587 Specification.\r
367284e7 588 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
3e8ad6bd
JF
589 this AP to finish this Procedure, either for\r
590 blocking or non-blocking mode. Zero means\r
591 infinity. If the timeout expires before\r
592 this AP returns from Procedure, then Procedure\r
593 on the AP is terminated. The\r
594 AP is available for next function assigned\r
595 by MpInitLibStartupAllAPs() or\r
596 MpInitLibStartupThisAP().\r
597 If the timeout expires in blocking mode,\r
598 BSP returns EFI_TIMEOUT. If the timeout\r
599 expires in non-blocking mode, WaitEvent\r
600 is signaled with SignalEvent().\r
601 @param[in] ProcedureArgument The parameter passed into Procedure on the\r
602 specified AP.\r
603 @param[out] Finished If NULL, this parameter is ignored. In\r
604 blocking mode, this parameter is ignored.\r
605 In non-blocking mode, if AP returns from\r
606 Procedure before the timeout expires, its\r
607 content is set to TRUE. Otherwise, the\r
608 value is set to FALSE. The caller can\r
609 determine if the AP returned from Procedure\r
610 by evaluating this value.\r
611\r
612 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
613 the timeout expires.\r
614 @retval EFI_SUCCESS In non-blocking mode, the function has been\r
615 dispatched to specified AP.\r
616 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
617 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
618 signaled.\r
619 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not\r
620 supported.\r
621 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
622 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
623 the specified AP has finished.\r
624 @retval EFI_NOT_READY The specified AP is busy.\r
625 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
626 @retval EFI_NOT_FOUND The processor with the handle specified by\r
627 ProcessorNumber does not exist.\r
628 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
629 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
630\r
631**/\r
632EFI_STATUS\r
633EFIAPI\r
634MpInitLibStartupThisAP (\r
635 IN EFI_AP_PROCEDURE Procedure,\r
636 IN UINTN ProcessorNumber,\r
637 IN EFI_EVENT WaitEvent OPTIONAL,\r
638 IN UINTN TimeoutInMicroseconds,\r
639 IN VOID *ProcedureArgument OPTIONAL,\r
640 OUT BOOLEAN *Finished OPTIONAL\r
641 )\r
642{\r
20ae5774
JF
643 EFI_STATUS Status;\r
644\r
645 //\r
646 // temporarily stop checkAllApsStatus for avoid resource dead-lock.\r
647 //\r
648 mStopCheckAllApsStatus = TRUE;\r
649\r
650 Status = StartupThisAPWorker (\r
651 Procedure,\r
652 ProcessorNumber,\r
653 WaitEvent,\r
654 TimeoutInMicroseconds,\r
655 ProcedureArgument,\r
656 Finished\r
657 );\r
658\r
659 mStopCheckAllApsStatus = FALSE;\r
660\r
661 return Status;\r
3e8ad6bd
JF
662}\r
663\r
664/**\r
665 This service switches the requested AP to be the BSP from that point onward.\r
666 This service changes the BSP for all purposes. This call can only be performed\r
667 by the current BSP.\r
668\r
669 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
670 BSP. The range is from 0 to the total number of\r
671 logical processors minus 1. The total number of\r
672 logical processors can be retrieved by\r
673 MpInitLibGetNumberOfProcessors().\r
674 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
675 enabled AP. Otherwise, it will be disabled.\r
676\r
677 @retval EFI_SUCCESS BSP successfully switched.\r
678 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to\r
679 this service returning.\r
680 @retval EFI_UNSUPPORTED Switching the BSP is not supported.\r
681 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
682 @retval EFI_NOT_FOUND The processor with the handle specified by\r
683 ProcessorNumber does not exist.\r
684 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or\r
685 a disabled AP.\r
686 @retval EFI_NOT_READY The specified AP is busy.\r
687 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
688\r
689**/\r
690EFI_STATUS\r
691EFIAPI\r
692MpInitLibSwitchBSP (\r
693 IN UINTN ProcessorNumber,\r
694 IN BOOLEAN EnableOldBSP\r
695 )\r
696{\r
b6e45716
JF
697 EFI_STATUS Status;\r
698 EFI_TIMER_ARCH_PROTOCOL *Timer;\r
699 UINT64 TimerPeriod;\r
41be0da5 700\r
8ad05bd2 701 TimerPeriod = 0;\r
b6e45716
JF
702 //\r
703 // Locate Timer Arch Protocol\r
704 //\r
705 Status = gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID **) &Timer);\r
706 if (EFI_ERROR (Status)) {\r
707 Timer = NULL;\r
708 }\r
709\r
710 if (Timer != NULL) {\r
711 //\r
712 // Save current rate of DXE Timer\r
713 //\r
714 Timer->GetTimerPeriod (Timer, &TimerPeriod);\r
715 //\r
716 // Disable DXE Timer and drain pending interrupts\r
717 //\r
718 Timer->SetTimerPeriod (Timer, 0);\r
719 }\r
41be0da5
JF
720\r
721 Status = SwitchBSPWorker (ProcessorNumber, EnableOldBSP);\r
722\r
b6e45716
JF
723 if (Timer != NULL) {\r
724 //\r
725 // Enable and restore rate of DXE Timer\r
726 //\r
727 Timer->SetTimerPeriod (Timer, TimerPeriod);\r
728 }\r
729\r
41be0da5 730 return Status;\r
3e8ad6bd
JF
731}\r
732\r
733/**\r
734 This service lets the caller enable or disable an AP from this point onward.\r
735 This service may only be called from the BSP.\r
736\r
737 @param[in] ProcessorNumber The handle number of AP.\r
738 The range is from 0 to the total number of\r
739 logical processors minus 1. The total number of\r
740 logical processors can be retrieved by\r
741 MpInitLibGetNumberOfProcessors().\r
742 @param[in] EnableAP Specifies the new state for the processor for\r
743 enabled, FALSE for disabled.\r
744 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
745 the new health status of the AP. This flag\r
746 corresponds to StatusFlag defined in\r
747 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r
748 the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
749 bits are ignored. If it is NULL, this parameter\r
750 is ignored.\r
751\r
752 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
753 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed\r
754 prior to this service returning.\r
755 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
756 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
757 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
758 does not exist.\r
759 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
760 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
761\r
762**/\r
763EFI_STATUS\r
764EFIAPI\r
765MpInitLibEnableDisableAP (\r
766 IN UINTN ProcessorNumber,\r
767 IN BOOLEAN EnableAP,\r
768 IN UINT32 *HealthFlag OPTIONAL\r
769 )\r
770{\r
e37109bc
JF
771 EFI_STATUS Status;\r
772 BOOLEAN TempStopCheckState;\r
773\r
774 TempStopCheckState = FALSE;\r
775 //\r
776 // temporarily stop checkAllAPsStatus for initialize parameters.\r
777 //\r
778 if (!mStopCheckAllApsStatus) {\r
779 mStopCheckAllApsStatus = TRUE;\r
780 TempStopCheckState = TRUE;\r
781 }\r
782\r
783 Status = EnableDisableApWorker (ProcessorNumber, EnableAP, HealthFlag);\r
784\r
785 if (TempStopCheckState) {\r
786 mStopCheckAllApsStatus = FALSE;\r
787 }\r
788\r
789 return Status;\r
3e8ad6bd 790}\r