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