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