]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/MpInitLib/PeiMpLib.c
UefiCpuPkg MpInitLib: Fix typo "sCPUID" to "CPUID"
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / PeiMpLib.c
CommitLineData
3e8ad6bd
JF
1/** @file\r
2 MP initialize support functions for PEI phase.\r
3\r
5986cf38 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
43c9fdcc
JF
16\r
17/**\r
18 Enable Debug Agent to support source debugging on AP function.\r
19\r
20**/\r
21VOID\r
22EnableDebugAgent (\r
23 VOID\r
24 )\r
25{\r
26}\r
27\r
93ca4c0f
JF
28/**\r
29 Get pointer to CPU MP Data structure.\r
30\r
31 @return The pointer to CPU MP Data structure.\r
32**/\r
33CPU_MP_DATA *\r
34GetCpuMpData (\r
35 VOID\r
36 )\r
37{\r
38 CPU_MP_DATA *CpuMpData;\r
39\r
40 CpuMpData = GetCpuMpDataFromGuidedHob ();\r
41 ASSERT (CpuMpData != NULL);\r
42 return CpuMpData;\r
43}\r
44\r
45/**\r
46 Save the pointer to CPU MP Data structure.\r
47\r
48 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.\r
49**/\r
50VOID\r
51SaveCpuMpData (\r
52 IN CPU_MP_DATA *CpuMpData\r
53 )\r
54{\r
55 UINT64 Data64;\r
56 //\r
57 // Build location of CPU MP DATA buffer in HOB\r
58 //\r
59 Data64 = (UINT64) (UINTN) CpuMpData;\r
60 BuildGuidDataHob (\r
61 &mCpuInitMpLibHobGuid,\r
62 (VOID *) &Data64,\r
63 sizeof (UINT64)\r
64 );\r
65}\r
66\r
ed66e0e3
JF
67/**\r
68 Check if AP wakeup buffer is overlapped with existing allocated buffer.\r
69\r
70 @param[in] WakeupBufferStart AP wakeup buffer start address.\r
71 @param[in] WakeupBufferEnd AP wakeup buffer end address.\r
72\r
73 @retval TRUE There is overlap.\r
74 @retval FALSE There is no overlap.\r
75**/\r
76BOOLEAN\r
77CheckOverlapWithAllocatedBuffer (\r
5986cf38
RN
78 IN UINT64 WakeupBufferStart,\r
79 IN UINT64 WakeupBufferEnd\r
ed66e0e3
JF
80 )\r
81{\r
82 EFI_PEI_HOB_POINTERS Hob;\r
83 EFI_HOB_MEMORY_ALLOCATION *MemoryHob;\r
84 BOOLEAN Overlapped;\r
5986cf38
RN
85 UINT64 MemoryStart;\r
86 UINT64 MemoryEnd;\r
ed66e0e3
JF
87\r
88 Overlapped = FALSE;\r
89 //\r
90 // Get the HOB list for processing\r
91 //\r
92 Hob.Raw = GetHobList ();\r
93 //\r
94 // Collect memory ranges\r
95 //\r
96 while (!END_OF_HOB_LIST (Hob)) {\r
97 if (Hob.Header->HobType == EFI_HOB_TYPE_MEMORY_ALLOCATION) {\r
98 MemoryHob = Hob.MemoryAllocation;\r
5986cf38
RN
99 MemoryStart = MemoryHob->AllocDescriptor.MemoryBaseAddress;\r
100 MemoryEnd = MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength;\r
ed66e0e3
JF
101 if (!((WakeupBufferStart >= MemoryEnd) || (WakeupBufferEnd <= MemoryStart))) {\r
102 Overlapped = TRUE;\r
103 break;\r
104 }\r
105 }\r
106 Hob.Raw = GET_NEXT_HOB (Hob);\r
107 }\r
108 return Overlapped;\r
109}\r
110\r
111/**\r
112 Get available system memory below 1MB by specified size.\r
113\r
114 @param[in] WakeupBufferSize Wakeup buffer size required\r
115\r
116 @retval other Return wakeup buffer address below 1MB.\r
117 @retval -1 Cannot find free memory below 1MB.\r
118**/\r
119UINTN\r
120GetWakeupBuffer (\r
121 IN UINTN WakeupBufferSize\r
122 )\r
123{\r
124 EFI_PEI_HOB_POINTERS Hob;\r
5986cf38
RN
125 UINT64 WakeupBufferStart;\r
126 UINT64 WakeupBufferEnd;\r
ed66e0e3
JF
127\r
128 WakeupBufferSize = (WakeupBufferSize + SIZE_4KB - 1) & ~(SIZE_4KB - 1);\r
129\r
130 //\r
131 // Get the HOB list for processing\r
132 //\r
133 Hob.Raw = GetHobList ();\r
134\r
135 //\r
136 // Collect memory ranges\r
137 //\r
138 while (!END_OF_HOB_LIST (Hob)) {\r
139 if (Hob.Header->HobType == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
140 if ((Hob.ResourceDescriptor->PhysicalStart < BASE_1MB) &&\r
141 (Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) &&\r
142 ((Hob.ResourceDescriptor->ResourceAttribute &\r
143 (EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED |\r
144 EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED |\r
145 EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED\r
146 )) == 0)\r
147 ) {\r
148 //\r
149 // Need memory under 1MB to be collected here\r
150 //\r
5986cf38 151 WakeupBufferEnd = Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength;\r
ed66e0e3
JF
152 if (WakeupBufferEnd > BASE_1MB) {\r
153 //\r
154 // Wakeup buffer should be under 1MB\r
155 //\r
156 WakeupBufferEnd = BASE_1MB;\r
157 }\r
158 while (WakeupBufferEnd > WakeupBufferSize) {\r
159 //\r
160 // Wakeup buffer should be aligned on 4KB\r
161 //\r
162 WakeupBufferStart = (WakeupBufferEnd - WakeupBufferSize) & ~(SIZE_4KB - 1);\r
163 if (WakeupBufferStart < Hob.ResourceDescriptor->PhysicalStart) {\r
164 break;\r
165 }\r
166 if (CheckOverlapWithAllocatedBuffer (WakeupBufferStart, WakeupBufferEnd)) {\r
167 //\r
168 // If this range is overlapped with existing allocated buffer, skip it\r
169 // and find the next range\r
170 //\r
171 WakeupBufferEnd -= WakeupBufferSize;\r
172 continue;\r
173 }\r
174 DEBUG ((DEBUG_INFO, "WakeupBufferStart = %x, WakeupBufferSize = %x\n",\r
175 WakeupBufferStart, WakeupBufferSize));\r
5986cf38 176 return (UINTN)WakeupBufferStart;\r
ed66e0e3
JF
177 }\r
178 }\r
179 }\r
180 //\r
181 // Find the next HOB\r
182 //\r
183 Hob.Raw = GET_NEXT_HOB (Hob);\r
184 }\r
185\r
186 return (UINTN) -1;\r
187}\r
188\r
f32bfe6d
JW
189/**\r
190 Get available EfiBootServicesCode memory below 4GB by specified size.\r
191\r
192 This buffer is required to safely transfer AP from real address mode to\r
193 protected mode or long mode, due to the fact that the buffer returned by\r
194 GetWakeupBuffer() may be marked as non-executable.\r
195\r
196 @param[in] BufferSize Wakeup transition buffer size.\r
197\r
198 @retval other Return wakeup transition buffer address below 4GB.\r
199 @retval 0 Cannot find free memory below 4GB.\r
200**/\r
201UINTN\r
202GetModeTransitionBuffer (\r
203 IN UINTN BufferSize\r
204 )\r
205{\r
206 //\r
207 // PEI phase doesn't need to do such transition. So simply return 0.\r
208 //\r
209 return 0;\r
210}\r
211\r
08085f08
JF
212/**\r
213 Checks APs status and updates APs status if needed.\r
214\r
215**/\r
216VOID\r
217CheckAndUpdateApsStatus (\r
218 VOID\r
219 )\r
220{\r
221}\r
222\r
93ca4c0f
JF
223/**\r
224 Initialize global data for MP support.\r
225\r
226 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
227**/\r
228VOID\r
229InitMpGlobalData (\r
230 IN CPU_MP_DATA *CpuMpData\r
231 )\r
232{\r
93ca4c0f
JF
233 SaveCpuMpData (CpuMpData);\r
234}\r
235\r
3e8ad6bd
JF
236/**\r
237 This service executes a caller provided function on all enabled APs.\r
238\r
239 @param[in] Procedure A pointer to the function to be run on\r
240 enabled APs of the system. See type\r
241 EFI_AP_PROCEDURE.\r
242 @param[in] SingleThread If TRUE, then all the enabled APs execute\r
243 the function specified by Procedure one by\r
244 one, in ascending order of processor handle\r
245 number. If FALSE, then all the enabled APs\r
246 execute the function specified by Procedure\r
247 simultaneously.\r
248 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
249 service. If it is NULL, then execute in\r
250 blocking mode. BSP waits until all APs finish\r
251 or TimeoutInMicroSeconds expires. If it's\r
252 not NULL, then execute in non-blocking mode.\r
253 BSP requests the function specified by\r
254 Procedure to be started on all the enabled\r
255 APs, and go on executing immediately. If\r
256 all return from Procedure, or TimeoutInMicroSeconds\r
257 expires, this event is signaled. The BSP\r
258 can use the CheckEvent() or WaitForEvent()\r
259 services to check the state of event. Type\r
260 EFI_EVENT is defined in CreateEvent() in\r
261 the Unified Extensible Firmware Interface\r
262 Specification.\r
367284e7 263 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
3e8ad6bd
JF
264 APs to return from Procedure, either for\r
265 blocking or non-blocking mode. Zero means\r
266 infinity. If the timeout expires before\r
267 all APs return from Procedure, then Procedure\r
268 on the failed APs is terminated. All enabled\r
269 APs are available for next function assigned\r
270 by MpInitLibStartupAllAPs() or\r
271 MPInitLibStartupThisAP().\r
272 If the timeout expires in blocking mode,\r
273 BSP returns EFI_TIMEOUT. If the timeout\r
274 expires in non-blocking mode, WaitEvent\r
275 is signaled with SignalEvent().\r
276 @param[in] ProcedureArgument The parameter passed into Procedure for\r
277 all APs.\r
278 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,\r
279 if all APs finish successfully, then its\r
280 content is set to NULL. If not all APs\r
281 finish before timeout expires, then its\r
282 content is set to address of the buffer\r
283 holding handle numbers of the failed APs.\r
284 The buffer is allocated by MP Initialization\r
285 library, and it's the caller's responsibility to\r
286 free the buffer with FreePool() service.\r
287 In blocking mode, it is ready for consumption\r
288 when the call returns. In non-blocking mode,\r
289 it is ready when WaitEvent is signaled. The\r
290 list of failed CPU is terminated by\r
291 END_OF_CPU_LIST.\r
292\r
293 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
294 the timeout expired.\r
295 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
296 to all enabled APs.\r
297 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
298 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
299 signaled.\r
300 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not\r
301 supported.\r
302 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
303 @retval EFI_NOT_STARTED No enabled APs exist in the system.\r
304 @retval EFI_NOT_READY Any enabled APs are busy.\r
305 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
306 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
307 all enabled APs have finished.\r
308 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
309\r
310**/\r
311EFI_STATUS\r
312EFIAPI\r
313MpInitLibStartupAllAPs (\r
314 IN EFI_AP_PROCEDURE Procedure,\r
315 IN BOOLEAN SingleThread,\r
316 IN EFI_EVENT WaitEvent OPTIONAL,\r
317 IN UINTN TimeoutInMicroseconds,\r
318 IN VOID *ProcedureArgument OPTIONAL,\r
319 OUT UINTN **FailedCpuList OPTIONAL\r
320 )\r
321{\r
86efe976
JF
322 if (WaitEvent != NULL) {\r
323 return EFI_UNSUPPORTED;\r
324 }\r
325\r
326 return StartupAllAPsWorker (\r
327 Procedure,\r
328 SingleThread,\r
329 NULL,\r
330 TimeoutInMicroseconds,\r
331 ProcedureArgument,\r
332 FailedCpuList\r
333 );\r
3e8ad6bd
JF
334}\r
335\r
336/**\r
337 This service lets the caller get one enabled AP to execute a caller-provided\r
338 function.\r
339\r
340 @param[in] Procedure A pointer to the function to be run on the\r
341 designated AP of the system. See type\r
342 EFI_AP_PROCEDURE.\r
343 @param[in] ProcessorNumber The handle number of the AP. The range is\r
344 from 0 to the total number of logical\r
345 processors minus 1. The total number of\r
346 logical processors can be retrieved by\r
347 MpInitLibGetNumberOfProcessors().\r
348 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
349 service. If it is NULL, then execute in\r
350 blocking mode. BSP waits until this AP finish\r
351 or TimeoutInMicroSeconds expires. If it's\r
352 not NULL, then execute in non-blocking mode.\r
353 BSP requests the function specified by\r
354 Procedure to be started on this AP,\r
355 and go on executing immediately. If this AP\r
356 return from Procedure or TimeoutInMicroSeconds\r
357 expires, this event is signaled. The BSP\r
358 can use the CheckEvent() or WaitForEvent()\r
359 services to check the state of event. Type\r
360 EFI_EVENT is defined in CreateEvent() in\r
361 the Unified Extensible Firmware Interface\r
362 Specification.\r
367284e7 363 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
3e8ad6bd
JF
364 this AP to finish this Procedure, either for\r
365 blocking or non-blocking mode. Zero means\r
366 infinity. If the timeout expires before\r
367 this AP returns from Procedure, then Procedure\r
368 on the AP is terminated. The\r
369 AP is available for next function assigned\r
370 by MpInitLibStartupAllAPs() or\r
371 MpInitLibStartupThisAP().\r
372 If the timeout expires in blocking mode,\r
373 BSP returns EFI_TIMEOUT. If the timeout\r
374 expires in non-blocking mode, WaitEvent\r
375 is signaled with SignalEvent().\r
376 @param[in] ProcedureArgument The parameter passed into Procedure on the\r
377 specified AP.\r
378 @param[out] Finished If NULL, this parameter is ignored. In\r
379 blocking mode, this parameter is ignored.\r
380 In non-blocking mode, if AP returns from\r
381 Procedure before the timeout expires, its\r
382 content is set to TRUE. Otherwise, the\r
383 value is set to FALSE. The caller can\r
384 determine if the AP returned from Procedure\r
385 by evaluating this value.\r
386\r
387 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
388 the timeout expires.\r
389 @retval EFI_SUCCESS In non-blocking mode, the function has been\r
390 dispatched to specified AP.\r
391 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
392 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
393 signaled.\r
394 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not\r
395 supported.\r
396 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
397 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
398 the specified AP has finished.\r
399 @retval EFI_NOT_READY The specified AP is busy.\r
400 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
401 @retval EFI_NOT_FOUND The processor with the handle specified by\r
402 ProcessorNumber does not exist.\r
403 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
404 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
405\r
406**/\r
407EFI_STATUS\r
408EFIAPI\r
409MpInitLibStartupThisAP (\r
410 IN EFI_AP_PROCEDURE Procedure,\r
411 IN UINTN ProcessorNumber,\r
412 IN EFI_EVENT WaitEvent OPTIONAL,\r
413 IN UINTN TimeoutInMicroseconds,\r
414 IN VOID *ProcedureArgument OPTIONAL,\r
415 OUT BOOLEAN *Finished OPTIONAL\r
416 )\r
417{\r
20ae5774
JF
418 if (WaitEvent != NULL) {\r
419 return EFI_UNSUPPORTED;\r
420 }\r
421\r
422 return StartupThisAPWorker (\r
423 Procedure,\r
424 ProcessorNumber,\r
425 NULL,\r
426 TimeoutInMicroseconds,\r
427 ProcedureArgument,\r
428 Finished\r
429 );\r
3e8ad6bd
JF
430}\r
431\r
432/**\r
433 This service switches the requested AP to be the BSP from that point onward.\r
434 This service changes the BSP for all purposes. This call can only be performed\r
435 by the current BSP.\r
436\r
437 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
438 BSP. The range is from 0 to the total number of\r
439 logical processors minus 1. The total number of\r
440 logical processors can be retrieved by\r
441 MpInitLibGetNumberOfProcessors().\r
442 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
443 enabled AP. Otherwise, it will be disabled.\r
444\r
445 @retval EFI_SUCCESS BSP successfully switched.\r
446 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to\r
447 this service returning.\r
448 @retval EFI_UNSUPPORTED Switching the BSP is not supported.\r
449 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
450 @retval EFI_NOT_FOUND The processor with the handle specified by\r
451 ProcessorNumber does not exist.\r
452 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or\r
453 a disabled AP.\r
454 @retval EFI_NOT_READY The specified AP is busy.\r
455 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
456\r
457**/\r
458EFI_STATUS\r
459EFIAPI\r
460MpInitLibSwitchBSP (\r
461 IN UINTN ProcessorNumber,\r
462 IN BOOLEAN EnableOldBSP\r
463 )\r
464{\r
41be0da5 465 return SwitchBSPWorker (ProcessorNumber, EnableOldBSP);\r
3e8ad6bd
JF
466}\r
467\r
468/**\r
469 This service lets the caller enable or disable an AP from this point onward.\r
470 This service may only be called from the BSP.\r
471\r
472 @param[in] ProcessorNumber The handle number of AP.\r
473 The range is from 0 to the total number of\r
474 logical processors minus 1. The total number of\r
475 logical processors can be retrieved by\r
476 MpInitLibGetNumberOfProcessors().\r
477 @param[in] EnableAP Specifies the new state for the processor for\r
478 enabled, FALSE for disabled.\r
479 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
480 the new health status of the AP. This flag\r
481 corresponds to StatusFlag defined in\r
482 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r
483 the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
484 bits are ignored. If it is NULL, this parameter\r
485 is ignored.\r
486\r
487 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
488 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed\r
489 prior to this service returning.\r
490 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
491 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
492 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
493 does not exist.\r
494 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
495 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
496\r
497**/\r
498EFI_STATUS\r
499EFIAPI\r
500MpInitLibEnableDisableAP (\r
501 IN UINTN ProcessorNumber,\r
502 IN BOOLEAN EnableAP,\r
503 IN UINT32 *HealthFlag OPTIONAL\r
504 )\r
505{\r
e37109bc 506 return EnableDisableApWorker (ProcessorNumber, EnableAP, HealthFlag);\r
3e8ad6bd
JF
507}\r
508\r
509\r