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