UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / UefiCpuPkg / CpuDxe / CpuMp.c
CommitLineData
6022e28c 1/** @file\r
7fadaacd 2 CPU DXE Module to produce CPU MP Protocol.\r
6022e28c 3\r
e7abb94d 4 Copyright (c) 2008 - 2022, Intel Corporation. All rights reserved.<BR>\r
0acd8697 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
6022e28c
JJ
6\r
7**/\r
8\r
9#include "CpuDxe.h"\r
10#include "CpuMp.h"\r
11\r
053e878b
MK
12EFI_HANDLE mMpServiceHandle = NULL;\r
13UINTN mNumberOfProcessors = 1;\r
acb2172d 14\r
003973d9 15EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate = {\r
d894d8b7 16 GetNumberOfProcessors,\r
e7938b5a 17 GetProcessorInfo,\r
5fee172f 18 StartupAllAPs,\r
3f4f0af8 19 StartupThisAP,\r
b7c05ba5 20 SwitchBSP,\r
fa7ce675 21 EnableDisableAP,\r
cfa2fac1 22 WhoAmI\r
003973d9
CF
23};\r
24\r
d894d8b7
CF
25/**\r
26 This service retrieves the number of logical processor in the platform\r
27 and the number of those logical processors that are enabled on this boot.\r
28 This service may only be called from the BSP.\r
29\r
30 This function is used to retrieve the following information:\r
31 - The number of logical processors that are present in the system.\r
32 - The number of enabled logical processors in the system at the instant\r
33 this call is made.\r
34\r
35 Because MP Service Protocol provides services to enable and disable processors\r
36 dynamically, the number of enabled logical processors may vary during the\r
37 course of a boot session.\r
38\r
39 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.\r
40 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then\r
41 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors\r
42 is returned in NumberOfProcessors, the number of currently enabled processor\r
43 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.\r
44\r
45 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
46 instance.\r
47 @param[out] NumberOfProcessors Pointer to the total number of logical\r
48 processors in the system, including the BSP\r
49 and disabled APs.\r
50 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical\r
51 processors that exist in system, including\r
52 the BSP.\r
53\r
54 @retval EFI_SUCCESS The number of logical processors and enabled\r
55 logical processors was retrieved.\r
56 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
57 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.\r
58 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.\r
59\r
60**/\r
61EFI_STATUS\r
62EFIAPI\r
63GetNumberOfProcessors (\r
64 IN EFI_MP_SERVICES_PROTOCOL *This,\r
65 OUT UINTN *NumberOfProcessors,\r
66 OUT UINTN *NumberOfEnabledProcessors\r
67 )\r
68{\r
69 if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {\r
70 return EFI_INVALID_PARAMETER;\r
71 }\r
72\r
7fadaacd
JF
73 return MpInitLibGetNumberOfProcessors (\r
74 NumberOfProcessors,\r
75 NumberOfEnabledProcessors\r
76 );\r
d894d8b7
CF
77}\r
78\r
e7938b5a
CF
79/**\r
80 Gets detailed MP-related information on the requested processor at the\r
81 instant this call is made. This service may only be called from the BSP.\r
82\r
83 This service retrieves detailed MP-related information about any processor\r
84 on the platform. Note the following:\r
85 - The processor information may change during the course of a boot session.\r
86 - The information presented here is entirely MP related.\r
87\r
88 Information regarding the number of caches and their sizes, frequency of operation,\r
89 slot numbers is all considered platform-related information and is not provided\r
90 by this service.\r
91\r
92 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
93 instance.\r
94 @param[in] ProcessorNumber The handle number of processor.\r
95 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for\r
96 the requested processor is deposited.\r
97\r
98 @retval EFI_SUCCESS Processor information was returned.\r
99 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
100 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.\r
101 @retval EFI_NOT_FOUND The processor with the handle specified by\r
102 ProcessorNumber does not exist in the platform.\r
103\r
104**/\r
105EFI_STATUS\r
106EFIAPI\r
107GetProcessorInfo (\r
108 IN EFI_MP_SERVICES_PROTOCOL *This,\r
109 IN UINTN ProcessorNumber,\r
110 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer\r
111 )\r
112{\r
7fadaacd 113 return MpInitLibGetProcessorInfo (ProcessorNumber, ProcessorInfoBuffer, NULL);\r
e7938b5a
CF
114}\r
115\r
5fee172f
CF
116/**\r
117 This service executes a caller provided function on all enabled APs. APs can\r
118 run either simultaneously or one at a time in sequence. This service supports\r
119 both blocking and non-blocking requests. The non-blocking requests use EFI\r
120 events so the BSP can detect when the APs have finished. This service may only\r
121 be called from the BSP.\r
122\r
123 This function is used to dispatch all the enabled APs to the function specified\r
124 by Procedure. If any enabled AP is busy, then EFI_NOT_READY is returned\r
125 immediately and Procedure is not started on any AP.\r
126\r
127 If SingleThread is TRUE, all the enabled APs execute the function specified by\r
128 Procedure one by one, in ascending order of processor handle number. Otherwise,\r
129 all the enabled APs execute the function specified by Procedure simultaneously.\r
130\r
131 If WaitEvent is NULL, execution is in blocking mode. The BSP waits until all\r
132 APs finish or TimeoutInMicroseconds expires. Otherwise, execution is in non-blocking\r
133 mode, and the BSP returns from this service without waiting for APs. If a\r
134 non-blocking mode is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT\r
135 is signaled, then EFI_UNSUPPORTED must be returned.\r
136\r
137 If the timeout specified by TimeoutInMicroseconds expires before all APs return\r
138 from Procedure, then Procedure on the failed APs is terminated. All enabled APs\r
139 are always available for further calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
140 and EFI_MP_SERVICES_PROTOCOL.StartupThisAP(). If FailedCpuList is not NULL, its\r
141 content points to the list of processor handle numbers in which Procedure was\r
142 terminated.\r
143\r
144 Note: It is the responsibility of the consumer of the EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
145 to make sure that the nature of the code that is executed on the BSP and the\r
146 dispatched APs is well controlled. The MP Services Protocol does not guarantee\r
147 that the Procedure function is MP-safe. Hence, the tasks that can be run in\r
148 parallel are limited to certain independent tasks and well-controlled exclusive\r
149 code. EFI services and protocols may not be called by APs unless otherwise\r
150 specified.\r
151\r
152 In blocking execution mode, BSP waits until all APs finish or\r
153 TimeoutInMicroseconds expires.\r
154\r
155 In non-blocking execution mode, BSP is freed to return to the caller and then\r
156 proceed to the next task without having to wait for APs. The following\r
157 sequence needs to occur in a non-blocking execution mode:\r
158\r
159 -# The caller that intends to use this MP Services Protocol in non-blocking\r
160 mode creates WaitEvent by calling the EFI CreateEvent() service. The caller\r
161 invokes EFI_MP_SERVICES_PROTOCOL.StartupAllAPs(). If the parameter WaitEvent\r
162 is not NULL, then StartupAllAPs() executes in non-blocking mode. It requests\r
163 the function specified by Procedure to be started on all the enabled APs,\r
164 and releases the BSP to continue with other tasks.\r
165 -# The caller can use the CheckEvent() and WaitForEvent() services to check\r
166 the state of the WaitEvent created in step 1.\r
f60f4cfe 167 -# When the APs complete their task or TimeoutInMicroSeconds expires, the MP\r
5fee172f
CF
168 Service signals WaitEvent by calling the EFI SignalEvent() function. If\r
169 FailedCpuList is not NULL, its content is available when WaitEvent is\r
170 signaled. If all APs returned from Procedure prior to the timeout, then\r
171 FailedCpuList is set to NULL. If not all APs return from Procedure before\r
172 the timeout, then FailedCpuList is filled in with the list of the failed\r
173 APs. The buffer is allocated by MP Service Protocol using AllocatePool().\r
174 It is the caller's responsibility to free the buffer with FreePool() service.\r
175 -# This invocation of SignalEvent() function informs the caller that invoked\r
176 EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() that either all the APs completed\r
177 the specified task or a timeout occurred. The contents of FailedCpuList\r
178 can be examined to determine which APs did not complete the specified task\r
179 prior to the timeout.\r
180\r
181 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
182 instance.\r
183 @param[in] Procedure A pointer to the function to be run on\r
184 enabled APs of the system. See type\r
185 EFI_AP_PROCEDURE.\r
186 @param[in] SingleThread If TRUE, then all the enabled APs execute\r
187 the function specified by Procedure one by\r
188 one, in ascending order of processor handle\r
189 number. If FALSE, then all the enabled APs\r
190 execute the function specified by Procedure\r
191 simultaneously.\r
192 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
193 service. If it is NULL, then execute in\r
194 blocking mode. BSP waits until all APs finish\r
195 or TimeoutInMicroseconds expires. If it's\r
196 not NULL, then execute in non-blocking mode.\r
197 BSP requests the function specified by\r
198 Procedure to be started on all the enabled\r
199 APs, and go on executing immediately. If\r
200 all return from Procedure, or TimeoutInMicroseconds\r
201 expires, this event is signaled. The BSP\r
202 can use the CheckEvent() or WaitForEvent()\r
203 services to check the state of event. Type\r
204 EFI_EVENT is defined in CreateEvent() in\r
205 the Unified Extensible Firmware Interface\r
206 Specification.\r
207 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
208 APs to return from Procedure, either for\r
209 blocking or non-blocking mode. Zero means\r
210 infinity. If the timeout expires before\r
211 all APs return from Procedure, then Procedure\r
212 on the failed APs is terminated. All enabled\r
213 APs are available for next function assigned\r
214 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
215 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
216 If the timeout expires in blocking mode,\r
217 BSP returns EFI_TIMEOUT. If the timeout\r
218 expires in non-blocking mode, WaitEvent\r
219 is signaled with SignalEvent().\r
220 @param[in] ProcedureArgument The parameter passed into Procedure for\r
221 all APs.\r
222 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,\r
223 if all APs finish successfully, then its\r
224 content is set to NULL. If not all APs\r
225 finish before timeout expires, then its\r
226 content is set to address of the buffer\r
227 holding handle numbers of the failed APs.\r
228 The buffer is allocated by MP Service Protocol,\r
229 and it's the caller's responsibility to\r
230 free the buffer with FreePool() service.\r
231 In blocking mode, it is ready for consumption\r
232 when the call returns. In non-blocking mode,\r
233 it is ready when WaitEvent is signaled. The\r
234 list of failed CPU is terminated by\r
235 END_OF_CPU_LIST.\r
236\r
237 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
238 the timeout expired.\r
239 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
240 to all enabled APs.\r
241 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
242 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
243 signaled.\r
244 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
245 @retval EFI_NOT_STARTED No enabled APs exist in the system.\r
246 @retval EFI_NOT_READY Any enabled APs are busy.\r
247 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
248 all enabled APs have finished.\r
249 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
250\r
251**/\r
252EFI_STATUS\r
253EFIAPI\r
254StartupAllAPs (\r
255 IN EFI_MP_SERVICES_PROTOCOL *This,\r
256 IN EFI_AP_PROCEDURE Procedure,\r
257 IN BOOLEAN SingleThread,\r
258 IN EFI_EVENT WaitEvent OPTIONAL,\r
259 IN UINTN TimeoutInMicroseconds,\r
260 IN VOID *ProcedureArgument OPTIONAL,\r
261 OUT UINTN **FailedCpuList OPTIONAL\r
262 )\r
263{\r
7fadaacd
JF
264 return MpInitLibStartupAllAPs (\r
265 Procedure,\r
266 SingleThread,\r
267 WaitEvent,\r
268 TimeoutInMicroseconds,\r
269 ProcedureArgument,\r
270 FailedCpuList\r
271 );\r
5fee172f
CF
272}\r
273\r
3f4f0af8
CF
274/**\r
275 This service lets the caller get one enabled AP to execute a caller-provided\r
276 function. The caller can request the BSP to either wait for the completion\r
277 of the AP or just proceed with the next task by using the EFI event mechanism.\r
278 See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking\r
279 execution support. This service may only be called from the BSP.\r
280\r
281 This function is used to dispatch one enabled AP to the function specified by\r
282 Procedure passing in the argument specified by ProcedureArgument. If WaitEvent\r
283 is NULL, execution is in blocking mode. The BSP waits until the AP finishes or\r
f60f4cfe 284 TimeoutInMicroSeconds expires. Otherwise, execution is in non-blocking mode.\r
3f4f0af8
CF
285 BSP proceeds to the next task without waiting for the AP. If a non-blocking mode\r
286 is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,\r
287 then EFI_UNSUPPORTED must be returned.\r
288\r
289 If the timeout specified by TimeoutInMicroseconds expires before the AP returns\r
290 from Procedure, then execution of Procedure by the AP is terminated. The AP is\r
291 available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and\r
292 EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
293\r
294 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
295 instance.\r
f3b91fa0
JF
296 @param[in] Procedure A pointer to the function to be run on the\r
297 designated AP of the system. See type\r
3f4f0af8
CF
298 EFI_AP_PROCEDURE.\r
299 @param[in] ProcessorNumber The handle number of the AP. The range is\r
300 from 0 to the total number of logical\r
301 processors minus 1. The total number of\r
302 logical processors can be retrieved by\r
303 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
304 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
305 service. If it is NULL, then execute in\r
f3b91fa0
JF
306 blocking mode. BSP waits until this AP finish\r
307 or TimeoutInMicroSeconds expires. If it's\r
3f4f0af8
CF
308 not NULL, then execute in non-blocking mode.\r
309 BSP requests the function specified by\r
f3b91fa0
JF
310 Procedure to be started on this AP,\r
311 and go on executing immediately. If this AP\r
312 return from Procedure or TimeoutInMicroSeconds\r
3f4f0af8
CF
313 expires, this event is signaled. The BSP\r
314 can use the CheckEvent() or WaitForEvent()\r
315 services to check the state of event. Type\r
316 EFI_EVENT is defined in CreateEvent() in\r
317 the Unified Extensible Firmware Interface\r
318 Specification.\r
367284e7 319 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
f3b91fa0 320 this AP to finish this Procedure, either for\r
3f4f0af8
CF
321 blocking or non-blocking mode. Zero means\r
322 infinity. If the timeout expires before\r
f3b91fa0
JF
323 this AP returns from Procedure, then Procedure\r
324 on the AP is terminated. The\r
325 AP is available for next function assigned\r
3f4f0af8
CF
326 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
327 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
328 If the timeout expires in blocking mode,\r
329 BSP returns EFI_TIMEOUT. If the timeout\r
330 expires in non-blocking mode, WaitEvent\r
331 is signaled with SignalEvent().\r
f3b91fa0
JF
332 @param[in] ProcedureArgument The parameter passed into Procedure on the\r
333 specified AP.\r
3f4f0af8
CF
334 @param[out] Finished If NULL, this parameter is ignored. In\r
335 blocking mode, this parameter is ignored.\r
336 In non-blocking mode, if AP returns from\r
337 Procedure before the timeout expires, its\r
338 content is set to TRUE. Otherwise, the\r
339 value is set to FALSE. The caller can\r
340 determine if the AP returned from Procedure\r
341 by evaluating this value.\r
342\r
343 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
344 the timeout expires.\r
345 @retval EFI_SUCCESS In non-blocking mode, the function has been\r
346 dispatched to specified AP.\r
347 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
348 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
349 signaled.\r
350 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
351 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
352 the specified AP has finished.\r
353 @retval EFI_NOT_READY The specified AP is busy.\r
354 @retval EFI_NOT_FOUND The processor with the handle specified by\r
355 ProcessorNumber does not exist.\r
356 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
357 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
358\r
359**/\r
360EFI_STATUS\r
361EFIAPI\r
362StartupThisAP (\r
363 IN EFI_MP_SERVICES_PROTOCOL *This,\r
364 IN EFI_AP_PROCEDURE Procedure,\r
365 IN UINTN ProcessorNumber,\r
366 IN EFI_EVENT WaitEvent OPTIONAL,\r
367 IN UINTN TimeoutInMicroseconds,\r
368 IN VOID *ProcedureArgument OPTIONAL,\r
369 OUT BOOLEAN *Finished OPTIONAL\r
370 )\r
371{\r
7fadaacd
JF
372 return MpInitLibStartupThisAP (\r
373 Procedure,\r
374 ProcessorNumber,\r
375 WaitEvent,\r
376 TimeoutInMicroseconds,\r
377 ProcedureArgument,\r
378 Finished\r
379 );\r
3f4f0af8
CF
380}\r
381\r
b7c05ba5
CF
382/**\r
383 This service switches the requested AP to be the BSP from that point onward.\r
384 This service changes the BSP for all purposes. This call can only be performed\r
385 by the current BSP.\r
386\r
387 This service switches the requested AP to be the BSP from that point onward.\r
388 This service changes the BSP for all purposes. The new BSP can take over the\r
389 execution of the old BSP and continue seamlessly from where the old one left\r
390 off. This service may not be supported after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT\r
391 is signaled.\r
392\r
393 If the BSP cannot be switched prior to the return from this service, then\r
394 EFI_UNSUPPORTED must be returned.\r
395\r
396 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
397 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
398 BSP. The range is from 0 to the total number of\r
399 logical processors minus 1. The total number of\r
400 logical processors can be retrieved by\r
401 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
402 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
403 enabled AP. Otherwise, it will be disabled.\r
404\r
405 @retval EFI_SUCCESS BSP successfully switched.\r
406 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to\r
407 this service returning.\r
408 @retval EFI_UNSUPPORTED Switching the BSP is not supported.\r
29b237f8 409 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
b7c05ba5
CF
410 @retval EFI_NOT_FOUND The processor with the handle specified by\r
411 ProcessorNumber does not exist.\r
412 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or\r
413 a disabled AP.\r
414 @retval EFI_NOT_READY The specified AP is busy.\r
415\r
416**/\r
417EFI_STATUS\r
418EFIAPI\r
419SwitchBSP (\r
420 IN EFI_MP_SERVICES_PROTOCOL *This,\r
421 IN UINTN ProcessorNumber,\r
422 IN BOOLEAN EnableOldBSP\r
423 )\r
424{\r
7fadaacd 425 return MpInitLibSwitchBSP (ProcessorNumber, EnableOldBSP);\r
b7c05ba5
CF
426}\r
427\r
fa7ce675
CF
428/**\r
429 This service lets the caller enable or disable an AP from this point onward.\r
430 This service may only be called from the BSP.\r
431\r
432 This service allows the caller enable or disable an AP from this point onward.\r
433 The caller can optionally specify the health status of the AP by Health. If\r
434 an AP is being disabled, then the state of the disabled AP is implementation\r
435 dependent. If an AP is enabled, then the implementation must guarantee that a\r
436 complete initialization sequence is performed on the AP, so the AP is in a state\r
437 that is compatible with an MP operating system. This service may not be supported\r
438 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.\r
439\r
440 If the enable or disable AP operation cannot be completed prior to the return\r
441 from this service, then EFI_UNSUPPORTED must be returned.\r
442\r
443 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
f3b91fa0
JF
444 @param[in] ProcessorNumber The handle number of AP.\r
445 The range is from 0 to the total number of\r
fa7ce675
CF
446 logical processors minus 1. The total number of\r
447 logical processors can be retrieved by\r
448 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
449 @param[in] EnableAP Specifies the new state for the processor for\r
450 enabled, FALSE for disabled.\r
451 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
452 the new health status of the AP. This flag\r
453 corresponds to StatusFlag defined in\r
454 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r
455 the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
456 bits are ignored. If it is NULL, this parameter\r
457 is ignored.\r
458\r
459 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
460 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed\r
461 prior to this service returning.\r
462 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
463 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
464 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
465 does not exist.\r
466 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
467\r
468**/\r
469EFI_STATUS\r
470EFIAPI\r
471EnableDisableAP (\r
472 IN EFI_MP_SERVICES_PROTOCOL *This,\r
473 IN UINTN ProcessorNumber,\r
474 IN BOOLEAN EnableAP,\r
475 IN UINT32 *HealthFlag OPTIONAL\r
476 )\r
477{\r
7fadaacd 478 return MpInitLibEnableDisableAP (ProcessorNumber, EnableAP, HealthFlag);\r
fa7ce675
CF
479}\r
480\r
cfa2fac1
CF
481/**\r
482 This return the handle number for the calling processor. This service may be\r
483 called from the BSP and APs.\r
484\r
485 This service returns the processor handle number for the calling processor.\r
486 The returned value is in the range from 0 to the total number of logical\r
487 processors minus 1. The total number of logical processors can be retrieved\r
488 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be\r
489 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER\r
490 is returned. Otherwise, the current processors handle number is returned in\r
491 ProcessorNumber, and EFI_SUCCESS is returned.\r
492\r
493 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
f3b91fa0
JF
494 @param[out] ProcessorNumber Pointer to the handle number of AP.\r
495 The range is from 0 to the total number of\r
cfa2fac1
CF
496 logical processors minus 1. The total number of\r
497 logical processors can be retrieved by\r
498 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
499\r
500 @retval EFI_SUCCESS The current processor handle number was returned\r
501 in ProcessorNumber.\r
502 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
503\r
504**/\r
505EFI_STATUS\r
506EFIAPI\r
507WhoAmI (\r
508 IN EFI_MP_SERVICES_PROTOCOL *This,\r
509 OUT UINTN *ProcessorNumber\r
510 )\r
511{\r
053e878b 512 return MpInitLibWhoAmI (ProcessorNumber);\r
03673ae1 513}\r
1535c888 514\r
db61e163
JF
515/**\r
516 Collects BIST data from HOB.\r
517\r
518 This function collects BIST data from HOB built from Sec Platform Information\r
519 PPI or SEC Platform Information2 PPI.\r
520\r
521**/\r
522VOID\r
523CollectBistDataFromHob (\r
524 VOID\r
525 )\r
526{\r
527 EFI_HOB_GUID_TYPE *GuidHob;\r
528 EFI_SEC_PLATFORM_INFORMATION_RECORD2 *SecPlatformInformation2;\r
529 EFI_SEC_PLATFORM_INFORMATION_RECORD *SecPlatformInformation;\r
530 UINTN NumberOfData;\r
531 EFI_SEC_PLATFORM_INFORMATION_CPU *CpuInstance;\r
532 EFI_SEC_PLATFORM_INFORMATION_CPU BspCpuInstance;\r
533 UINTN ProcessorNumber;\r
7fadaacd
JF
534 EFI_PROCESSOR_INFORMATION ProcessorInfo;\r
535 EFI_HEALTH_FLAGS BistData;\r
7d17ab47 536 UINTN CpuInstanceNumber;\r
db61e163
JF
537\r
538 SecPlatformInformation2 = NULL;\r
539 SecPlatformInformation = NULL;\r
540\r
541 //\r
542 // Get gEfiSecPlatformInformation2PpiGuid Guided HOB firstly\r
543 //\r
544 GuidHob = GetFirstGuidHob (&gEfiSecPlatformInformation2PpiGuid);\r
545 if (GuidHob != NULL) {\r
546 //\r
547 // Sec Platform Information2 PPI includes BSP/APs' BIST information\r
548 //\r
549 SecPlatformInformation2 = GET_GUID_HOB_DATA (GuidHob);\r
053e878b
MK
550 NumberOfData = SecPlatformInformation2->NumberOfCpus;\r
551 CpuInstance = SecPlatformInformation2->CpuInstance;\r
db61e163
JF
552 } else {\r
553 //\r
554 // Otherwise, get gEfiSecPlatformInformationPpiGuid Guided HOB\r
555 //\r
556 GuidHob = GetFirstGuidHob (&gEfiSecPlatformInformationPpiGuid);\r
557 if (GuidHob != NULL) {\r
558 SecPlatformInformation = GET_GUID_HOB_DATA (GuidHob);\r
053e878b 559 NumberOfData = 1;\r
db61e163
JF
560 //\r
561 // SEC Platform Information only includes BSP's BIST information\r
562 // does not have BSP's APIC ID\r
563 //\r
053e878b
MK
564 BspCpuInstance.CpuLocation = GetApicId ();\r
565 BspCpuInstance.InfoRecord.IA32HealthFlags.Uint32 = SecPlatformInformation->IA32HealthFlags.Uint32;\r
566 CpuInstance = &BspCpuInstance;\r
db61e163 567 } else {\r
af9bce40 568 DEBUG ((DEBUG_INFO, "Does not find any HOB stored CPU BIST information!\n"));\r
db61e163
JF
569 //\r
570 // Does not find any HOB stored BIST information\r
571 //\r
572 return;\r
573 }\r
574 }\r
575\r
7d17ab47
JF
576 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {\r
577 MpInitLibGetProcessorInfo (ProcessorNumber, &ProcessorInfo, &BistData);\r
578 for (CpuInstanceNumber = 0; CpuInstanceNumber < NumberOfData; CpuInstanceNumber++) {\r
579 if (ProcessorInfo.ProcessorId == CpuInstance[CpuInstanceNumber].CpuLocation) {\r
db61e163
JF
580 //\r
581 // Update CPU health status for MP Services Protocol according to BIST data.\r
582 //\r
7d17ab47 583 BistData = CpuInstance[CpuInstanceNumber].InfoRecord.IA32HealthFlags;\r
db61e163
JF
584 }\r
585 }\r
053e878b 586\r
7d17ab47
JF
587 if (BistData.Uint32 != 0) {\r
588 //\r
589 // Report Status Code that self test is failed\r
590 //\r
591 REPORT_STATUS_CODE (\r
592 EFI_ERROR_CODE | EFI_ERROR_MAJOR,\r
593 (EFI_COMPUTING_UNIT_HOST_PROCESSOR | EFI_CU_HP_EC_SELF_TEST)\r
594 );\r
595 }\r
db61e163
JF
596 }\r
597}\r
598\r
0f7bccf5
ZL
599//\r
600// Structure for InitializeSeparateExceptionStacks\r
601//\r
602typedef struct {\r
4b7bd4c5
ZL
603 VOID *Buffer;\r
604 UINTN BufferSize;\r
605 EFI_STATUS Status;\r
0f7bccf5 606} EXCEPTION_STACK_SWITCH_CONTEXT;\r
01953ce6
JW
607\r
608/**\r
609 Initializes CPU exceptions handlers for the sake of stack switch requirement.\r
610\r
e7abb94d 611 This function is a wrapper of InitializeSeparateExceptionStacks. It's mainly\r
01953ce6
JW
612 for the sake of AP's init because of EFI_AP_PROCEDURE API requirement.\r
613\r
614 @param[in,out] Buffer The pointer to private data buffer.\r
615\r
616**/\r
617VOID\r
618EFIAPI\r
619InitializeExceptionStackSwitchHandlers (\r
053e878b 620 IN OUT VOID *Buffer\r
01953ce6
JW
621 )\r
622{\r
0f7bccf5 623 EXCEPTION_STACK_SWITCH_CONTEXT *SwitchStackData;\r
4b7bd4c5 624 UINTN Index;\r
01953ce6 625\r
4b7bd4c5 626 MpInitLibWhoAmI (&Index);\r
0f7bccf5 627 SwitchStackData = (EXCEPTION_STACK_SWITCH_CONTEXT *)Buffer;\r
4b7bd4c5
ZL
628\r
629 //\r
630 // This may be called twice for each Cpu. Only run InitializeSeparateExceptionStacks\r
631 // if this is the first call or the first call failed because of size too small.\r
632 //\r
633 if ((SwitchStackData[Index].Status == EFI_NOT_STARTED) || (SwitchStackData[Index].Status == EFI_BUFFER_TOO_SMALL)) {\r
634 SwitchStackData[Index].Status = InitializeSeparateExceptionStacks (SwitchStackData[Index].Buffer, &SwitchStackData[Index].BufferSize);\r
635 }\r
01953ce6
JW
636}\r
637\r
638/**\r
639 Initializes MP exceptions handlers for the sake of stack switch requirement.\r
640\r
641 This function will allocate required resources required to setup stack switch\r
0f7bccf5 642 and pass them through SwitchStackData to each logic processor.\r
01953ce6
JW
643\r
644**/\r
645VOID\r
646InitializeMpExceptionStackSwitchHandlers (\r
647 VOID\r
648 )\r
649{\r
0f7bccf5 650 UINTN Index;\r
4b7bd4c5 651 EXCEPTION_STACK_SWITCH_CONTEXT *SwitchStackData;\r
0f7bccf5 652 UINTN BufferSize;\r
4b7bd4c5
ZL
653 EFI_STATUS Status;\r
654 UINT8 *Buffer;\r
01953ce6 655\r
4b7bd4c5
ZL
656 SwitchStackData = AllocateZeroPool (mNumberOfProcessors * sizeof (EXCEPTION_STACK_SWITCH_CONTEXT));\r
657 ASSERT (SwitchStackData != NULL);\r
01953ce6 658 for (Index = 0; Index < mNumberOfProcessors; ++Index) {\r
4b7bd4c5
ZL
659 //\r
660 // Because the procedure may runs multiple times, use the status EFI_NOT_STARTED\r
661 // to indicate the procedure haven't been run yet.\r
662 //\r
663 SwitchStackData[Index].Status = EFI_NOT_STARTED;\r
664 }\r
0f7bccf5 665\r
4b7bd4c5
ZL
666 Status = MpInitLibStartupAllCPUs (\r
667 InitializeExceptionStackSwitchHandlers,\r
668 0,\r
669 SwitchStackData\r
670 );\r
671 ASSERT_EFI_ERROR (Status);\r
672\r
673 BufferSize = 0;\r
674 for (Index = 0; Index < mNumberOfProcessors; ++Index) {\r
675 if (SwitchStackData[Index].Status == EFI_BUFFER_TOO_SMALL) {\r
676 ASSERT (SwitchStackData[Index].BufferSize != 0);\r
677 BufferSize += SwitchStackData[Index].BufferSize;\r
01953ce6 678 } else {\r
4b7bd4c5
ZL
679 ASSERT (SwitchStackData[Index].Status == EFI_SUCCESS);\r
680 ASSERT (SwitchStackData[Index].BufferSize == 0);\r
01953ce6 681 }\r
4b7bd4c5 682 }\r
01953ce6 683\r
4b7bd4c5
ZL
684 if (BufferSize != 0) {\r
685 Buffer = AllocateRuntimeZeroPool (BufferSize);\r
686 ASSERT (Buffer != NULL);\r
687 BufferSize = 0;\r
688 for (Index = 0; Index < mNumberOfProcessors; ++Index) {\r
689 if (SwitchStackData[Index].Status == EFI_BUFFER_TOO_SMALL) {\r
690 SwitchStackData[Index].Buffer = (VOID *)(&Buffer[BufferSize]);\r
691 BufferSize += SwitchStackData[Index].BufferSize;\r
692 DEBUG ((\r
693 DEBUG_INFO,\r
694 "Buffer[cpu%lu] for InitializeExceptionStackSwitchHandlers: 0x%lX with size 0x%lX\n",\r
695 (UINT64)(UINTN)Index,\r
696 (UINT64)(UINTN)SwitchStackData[Index].Buffer,\r
697 (UINT64)(UINTN)SwitchStackData[Index].BufferSize\r
698 ));\r
699 }\r
0f7bccf5 700 }\r
01953ce6 701\r
4b7bd4c5
ZL
702 Status = MpInitLibStartupAllCPUs (\r
703 InitializeExceptionStackSwitchHandlers,\r
704 0,\r
705 SwitchStackData\r
706 );\r
707 ASSERT_EFI_ERROR (Status);\r
708 for (Index = 0; Index < mNumberOfProcessors; ++Index) {\r
709 ASSERT (SwitchStackData[Index].Status == EFI_SUCCESS);\r
01953ce6 710 }\r
01953ce6 711 }\r
4b7bd4c5
ZL
712\r
713 FreePool (SwitchStackData);\r
01953ce6
JW
714}\r
715\r
dcc02621
JW
716/**\r
717 Initializes MP exceptions handlers for special features, such as Heap Guard\r
718 and Stack Guard.\r
719**/\r
720VOID\r
721InitializeMpExceptionHandlers (\r
722 VOID\r
723 )\r
724{\r
725 //\r
726 // Enable non-stop mode for #PF triggered by Heap Guard or NULL Pointer\r
727 // Detection.\r
728 //\r
729 if (HEAP_GUARD_NONSTOP_MODE || NULL_DETECTION_NONSTOP_MODE) {\r
730 RegisterCpuInterruptHandler (EXCEPT_IA32_DEBUG, DebugExceptionHandler);\r
731 RegisterCpuInterruptHandler (EXCEPT_IA32_PAGE_FAULT, PageFaultExceptionHandler);\r
732 }\r
733\r
734 //\r
735 // Setup stack switch for Stack Guard feature.\r
736 //\r
737 if (PcdGetBool (PcdCpuStackGuard)) {\r
738 InitializeMpExceptionStackSwitchHandlers ();\r
739 }\r
740}\r
741\r
6022e28c
JJ
742/**\r
743 Initialize Multi-processor support.\r
744\r
745**/\r
746VOID\r
747InitializeMpSupport (\r
748 VOID\r
749 )\r
750{\r
053e878b
MK
751 EFI_STATUS Status;\r
752 UINTN NumberOfProcessors;\r
753 UINTN NumberOfEnabledProcessors;\r
4a50c272 754\r
6a26a597 755 //\r
0a55f3bd 756 // Wakeup APs to do initialization\r
6a26a597 757 //\r
0a55f3bd
JF
758 Status = MpInitLibInitialize ();\r
759 ASSERT_EFI_ERROR (Status);\r
fe078dd5 760\r
0a55f3bd
JF
761 MpInitLibGetNumberOfProcessors (&NumberOfProcessors, &NumberOfEnabledProcessors);\r
762 mNumberOfProcessors = NumberOfProcessors;\r
d2f0ff1e 763 DEBUG ((DEBUG_INFO, "Detect CPU count: %d\n", mNumberOfProcessors));\r
1aa6bf52 764\r
01953ce6 765 //\r
dcc02621 766 // Initialize special exception handlers for each logic processor.\r
01953ce6 767 //\r
dcc02621 768 InitializeMpExceptionHandlers ();\r
01953ce6 769\r
db61e163
JF
770 //\r
771 // Update CPU healthy information from Guided HOB\r
772 //\r
773 CollectBistDataFromHob ();\r
774\r
4a50c272
CF
775 Status = gBS->InstallMultipleProtocolInterfaces (\r
776 &mMpServiceHandle,\r
053e878b
MK
777 &gEfiMpServiceProtocolGuid,\r
778 &mMpServicesTemplate,\r
4a50c272
CF
779 NULL\r
780 );\r
781 ASSERT_EFI_ERROR (Status);\r
6a26a597 782}\r