]>
Commit | Line | Data |
---|---|---|
3e8ad6bd JF |
1 | /** @file\r |
2 | MP initialize support functions for DXE phase.\r | |
3 | \r | |
ee0c39fa | 4 | Copyright (c) 2016 - 2019, 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 | 21 | CPU_MP_DATA *mCpuMpData = NULL;\r |
96378861 | 22 | EFI_EVENT mCheckAllApsEvent = NULL;\r |
4d3314f6 | 23 | EFI_EVENT mMpInitExitBootServicesEvent = NULL;\r |
8677a56a | 24 | EFI_EVENT mLegacyBootEvent = NULL;\r |
96378861 | 25 | volatile BOOLEAN mStopCheckAllApsStatus = TRUE;\r |
5183fb37 | 26 | VOID *mReservedApLoopFunc = NULL;\r |
bf2786dc | 27 | UINTN mReservedTopOfApStack;\r |
9f91cb01 | 28 | volatile 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 | |
34 | VOID\r | |
35 | EnableDebugAgent (\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 | |
50 | CPU_MP_DATA *\r | |
51 | GetCpuMpData (\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 | |
64 | VOID\r | |
65 | SaveCpuMpData (\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 |
80 | UINTN\r |
81 | GetWakeupBuffer (\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 | |
126 | UINTN\r | |
127 | GetModeTransitionBuffer (\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 | |
152 | VOID\r | |
153 | CheckAndUpdateApsStatus (\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 | |
207 | VOID\r | |
208 | EFIAPI\r | |
209 | CheckApsStatus (\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 |
227 | UINT16\r | |
228 | GetProtectedModeCS (\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 | |
257 | VOID\r | |
258 | EFIAPI\r | |
259 | RelocateApLoop (\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 | |
293 | VOID\r | |
294 | EFIAPI\r | |
86af2eb8 | 295 | MpInitChangeApLoopCallback (\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 | |
318 | VOID\r | |
319 | InitMpGlobalData (\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 | |
553 | EFI_STATUS\r | |
554 | EFIAPI\r | |
555 | MpInitLibStartupAllAPs (\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 | |
ee0c39fa | 571 | Status = StartupAllCPUsWorker (\r |
86efe976 JF |
572 | Procedure,\r |
573 | SingleThread,\r | |
ee0c39fa | 574 | TRUE,\r |
86efe976 JF |
575 | WaitEvent,\r |
576 | TimeoutInMicroseconds,\r | |
577 | ProcedureArgument,\r | |
578 | FailedCpuList\r | |
579 | );\r | |
580 | \r | |
581 | //\r | |
582 | // Start checkAllApsStatus\r | |
583 | //\r | |
584 | mStopCheckAllApsStatus = FALSE;\r | |
585 | \r | |
586 | return Status;\r | |
3e8ad6bd JF |
587 | }\r |
588 | \r | |
589 | /**\r | |
590 | This service lets the caller get one enabled AP to execute a caller-provided\r | |
591 | function.\r | |
592 | \r | |
593 | @param[in] Procedure A pointer to the function to be run on the\r | |
594 | designated AP of the system. See type\r | |
595 | EFI_AP_PROCEDURE.\r | |
596 | @param[in] ProcessorNumber The handle number of the AP. The range is\r | |
597 | from 0 to the total number of logical\r | |
598 | processors minus 1. The total number of\r | |
599 | logical processors can be retrieved by\r | |
600 | MpInitLibGetNumberOfProcessors().\r | |
601 | @param[in] WaitEvent The event created by the caller with CreateEvent()\r | |
602 | service. If it is NULL, then execute in\r | |
603 | blocking mode. BSP waits until this AP finish\r | |
604 | or TimeoutInMicroSeconds expires. If it's\r | |
605 | not NULL, then execute in non-blocking mode.\r | |
606 | BSP requests the function specified by\r | |
607 | Procedure to be started on this AP,\r | |
608 | and go on executing immediately. If this AP\r | |
609 | return from Procedure or TimeoutInMicroSeconds\r | |
610 | expires, this event is signaled. The BSP\r | |
611 | can use the CheckEvent() or WaitForEvent()\r | |
612 | services to check the state of event. Type\r | |
613 | EFI_EVENT is defined in CreateEvent() in\r | |
614 | the Unified Extensible Firmware Interface\r | |
615 | Specification.\r | |
367284e7 | 616 | @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r |
3e8ad6bd JF |
617 | this AP to finish this Procedure, either for\r |
618 | blocking or non-blocking mode. Zero means\r | |
619 | infinity. If the timeout expires before\r | |
620 | this AP returns from Procedure, then Procedure\r | |
621 | on the AP is terminated. The\r | |
622 | AP is available for next function assigned\r | |
623 | by MpInitLibStartupAllAPs() or\r | |
624 | MpInitLibStartupThisAP().\r | |
625 | If the timeout expires in blocking mode,\r | |
626 | BSP returns EFI_TIMEOUT. If the timeout\r | |
627 | expires in non-blocking mode, WaitEvent\r | |
628 | is signaled with SignalEvent().\r | |
629 | @param[in] ProcedureArgument The parameter passed into Procedure on the\r | |
630 | specified AP.\r | |
631 | @param[out] Finished If NULL, this parameter is ignored. In\r | |
632 | blocking mode, this parameter is ignored.\r | |
633 | In non-blocking mode, if AP returns from\r | |
634 | Procedure before the timeout expires, its\r | |
635 | content is set to TRUE. Otherwise, the\r | |
636 | value is set to FALSE. The caller can\r | |
637 | determine if the AP returned from Procedure\r | |
638 | by evaluating this value.\r | |
639 | \r | |
640 | @retval EFI_SUCCESS In blocking mode, specified AP finished before\r | |
641 | the timeout expires.\r | |
642 | @retval EFI_SUCCESS In non-blocking mode, the function has been\r | |
643 | dispatched to specified AP.\r | |
644 | @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r | |
645 | UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r | |
646 | signaled.\r | |
647 | @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not\r | |
648 | supported.\r | |
649 | @retval EFI_DEVICE_ERROR The calling processor is an AP.\r | |
650 | @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r | |
651 | the specified AP has finished.\r | |
652 | @retval EFI_NOT_READY The specified AP is busy.\r | |
653 | @retval EFI_NOT_READY MP Initialize Library is not initialized.\r | |
654 | @retval EFI_NOT_FOUND The processor with the handle specified by\r | |
655 | ProcessorNumber does not exist.\r | |
656 | @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r | |
657 | @retval EFI_INVALID_PARAMETER Procedure is NULL.\r | |
658 | \r | |
659 | **/\r | |
660 | EFI_STATUS\r | |
661 | EFIAPI\r | |
662 | MpInitLibStartupThisAP (\r | |
663 | IN EFI_AP_PROCEDURE Procedure,\r | |
664 | IN UINTN ProcessorNumber,\r | |
665 | IN EFI_EVENT WaitEvent OPTIONAL,\r | |
666 | IN UINTN TimeoutInMicroseconds,\r | |
667 | IN VOID *ProcedureArgument OPTIONAL,\r | |
668 | OUT BOOLEAN *Finished OPTIONAL\r | |
669 | )\r | |
670 | {\r | |
20ae5774 JF |
671 | EFI_STATUS Status;\r |
672 | \r | |
673 | //\r | |
674 | // temporarily stop checkAllApsStatus for avoid resource dead-lock.\r | |
675 | //\r | |
676 | mStopCheckAllApsStatus = TRUE;\r | |
677 | \r | |
678 | Status = StartupThisAPWorker (\r | |
679 | Procedure,\r | |
680 | ProcessorNumber,\r | |
681 | WaitEvent,\r | |
682 | TimeoutInMicroseconds,\r | |
683 | ProcedureArgument,\r | |
684 | Finished\r | |
685 | );\r | |
686 | \r | |
687 | mStopCheckAllApsStatus = FALSE;\r | |
688 | \r | |
689 | return Status;\r | |
3e8ad6bd JF |
690 | }\r |
691 | \r | |
692 | /**\r | |
693 | This service switches the requested AP to be the BSP from that point onward.\r | |
694 | This service changes the BSP for all purposes. This call can only be performed\r | |
695 | by the current BSP.\r | |
696 | \r | |
697 | @param[in] ProcessorNumber The handle number of AP that is to become the new\r | |
698 | BSP. The range is from 0 to the total number of\r | |
699 | logical processors minus 1. The total number of\r | |
700 | logical processors can be retrieved by\r | |
701 | MpInitLibGetNumberOfProcessors().\r | |
702 | @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r | |
703 | enabled AP. Otherwise, it will be disabled.\r | |
704 | \r | |
705 | @retval EFI_SUCCESS BSP successfully switched.\r | |
706 | @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to\r | |
707 | this service returning.\r | |
708 | @retval EFI_UNSUPPORTED Switching the BSP is not supported.\r | |
709 | @retval EFI_DEVICE_ERROR The calling processor is an AP.\r | |
710 | @retval EFI_NOT_FOUND The processor with the handle specified by\r | |
711 | ProcessorNumber does not exist.\r | |
712 | @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or\r | |
713 | a disabled AP.\r | |
714 | @retval EFI_NOT_READY The specified AP is busy.\r | |
715 | @retval EFI_NOT_READY MP Initialize Library is not initialized.\r | |
716 | \r | |
717 | **/\r | |
718 | EFI_STATUS\r | |
719 | EFIAPI\r | |
720 | MpInitLibSwitchBSP (\r | |
721 | IN UINTN ProcessorNumber,\r | |
722 | IN BOOLEAN EnableOldBSP\r | |
723 | )\r | |
724 | {\r | |
b6e45716 JF |
725 | EFI_STATUS Status;\r |
726 | EFI_TIMER_ARCH_PROTOCOL *Timer;\r | |
727 | UINT64 TimerPeriod;\r | |
41be0da5 | 728 | \r |
8ad05bd2 | 729 | TimerPeriod = 0;\r |
b6e45716 JF |
730 | //\r |
731 | // Locate Timer Arch Protocol\r | |
732 | //\r | |
733 | Status = gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID **) &Timer);\r | |
734 | if (EFI_ERROR (Status)) {\r | |
735 | Timer = NULL;\r | |
736 | }\r | |
737 | \r | |
738 | if (Timer != NULL) {\r | |
739 | //\r | |
740 | // Save current rate of DXE Timer\r | |
741 | //\r | |
742 | Timer->GetTimerPeriod (Timer, &TimerPeriod);\r | |
743 | //\r | |
744 | // Disable DXE Timer and drain pending interrupts\r | |
745 | //\r | |
746 | Timer->SetTimerPeriod (Timer, 0);\r | |
747 | }\r | |
41be0da5 JF |
748 | \r |
749 | Status = SwitchBSPWorker (ProcessorNumber, EnableOldBSP);\r | |
750 | \r | |
b6e45716 JF |
751 | if (Timer != NULL) {\r |
752 | //\r | |
753 | // Enable and restore rate of DXE Timer\r | |
754 | //\r | |
755 | Timer->SetTimerPeriod (Timer, TimerPeriod);\r | |
756 | }\r | |
757 | \r | |
41be0da5 | 758 | return Status;\r |
3e8ad6bd JF |
759 | }\r |
760 | \r | |
761 | /**\r | |
762 | This service lets the caller enable or disable an AP from this point onward.\r | |
763 | This service may only be called from the BSP.\r | |
764 | \r | |
765 | @param[in] ProcessorNumber The handle number of AP.\r | |
766 | The range is from 0 to the total number of\r | |
767 | logical processors minus 1. The total number of\r | |
768 | logical processors can be retrieved by\r | |
769 | MpInitLibGetNumberOfProcessors().\r | |
770 | @param[in] EnableAP Specifies the new state for the processor for\r | |
771 | enabled, FALSE for disabled.\r | |
772 | @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r | |
773 | the new health status of the AP. This flag\r | |
774 | corresponds to StatusFlag defined in\r | |
775 | EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r | |
776 | the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r | |
777 | bits are ignored. If it is NULL, this parameter\r | |
778 | is ignored.\r | |
779 | \r | |
780 | @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r | |
781 | @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed\r | |
782 | prior to this service returning.\r | |
783 | @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r | |
784 | @retval EFI_DEVICE_ERROR The calling processor is an AP.\r | |
785 | @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r | |
786 | does not exist.\r | |
787 | @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r | |
788 | @retval EFI_NOT_READY MP Initialize Library is not initialized.\r | |
789 | \r | |
790 | **/\r | |
791 | EFI_STATUS\r | |
792 | EFIAPI\r | |
793 | MpInitLibEnableDisableAP (\r | |
794 | IN UINTN ProcessorNumber,\r | |
795 | IN BOOLEAN EnableAP,\r | |
796 | IN UINT32 *HealthFlag OPTIONAL\r | |
797 | )\r | |
798 | {\r | |
e37109bc JF |
799 | EFI_STATUS Status;\r |
800 | BOOLEAN TempStopCheckState;\r | |
801 | \r | |
802 | TempStopCheckState = FALSE;\r | |
803 | //\r | |
804 | // temporarily stop checkAllAPsStatus for initialize parameters.\r | |
805 | //\r | |
806 | if (!mStopCheckAllApsStatus) {\r | |
807 | mStopCheckAllApsStatus = TRUE;\r | |
808 | TempStopCheckState = TRUE;\r | |
809 | }\r | |
810 | \r | |
811 | Status = EnableDisableApWorker (ProcessorNumber, EnableAP, HealthFlag);\r | |
812 | \r | |
813 | if (TempStopCheckState) {\r | |
814 | mStopCheckAllApsStatus = FALSE;\r | |
815 | }\r | |
816 | \r | |
817 | return Status;\r | |
3e8ad6bd | 818 | }\r |