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