]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MpInitLib/PeiMpLib.c
UefiCpuPkg/MpInitLib: Do not wakeup AP if only one processor supported
[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 if (CpuMpData->CpuCount == 1) {
326 //
327 // If only BSP exists, return
328 //
329 return;
330 }
331
332 //
333 // Register an event for EndOfPei
334 //
335 Status = PeiServicesNotifyPpi (&mMpInitLibNotifyList);
336 ASSERT_EFI_ERROR (Status);
337 }
338
339 /**
340 This service executes a caller provided function on all enabled APs.
341
342 @param[in] Procedure A pointer to the function to be run on
343 enabled APs of the system. See type
344 EFI_AP_PROCEDURE.
345 @param[in] SingleThread If TRUE, then all the enabled APs execute
346 the function specified by Procedure one by
347 one, in ascending order of processor handle
348 number. If FALSE, then all the enabled APs
349 execute the function specified by Procedure
350 simultaneously.
351 @param[in] WaitEvent The event created by the caller with CreateEvent()
352 service. If it is NULL, then execute in
353 blocking mode. BSP waits until all APs finish
354 or TimeoutInMicroSeconds expires. If it's
355 not NULL, then execute in non-blocking mode.
356 BSP requests the function specified by
357 Procedure to be started on all the enabled
358 APs, and go on executing immediately. If
359 all return from Procedure, or TimeoutInMicroSeconds
360 expires, this event is signaled. The BSP
361 can use the CheckEvent() or WaitForEvent()
362 services to check the state of event. Type
363 EFI_EVENT is defined in CreateEvent() in
364 the Unified Extensible Firmware Interface
365 Specification.
366 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
367 APs to return from Procedure, either for
368 blocking or non-blocking mode. Zero means
369 infinity. If the timeout expires before
370 all APs return from Procedure, then Procedure
371 on the failed APs is terminated. All enabled
372 APs are available for next function assigned
373 by MpInitLibStartupAllAPs() or
374 MPInitLibStartupThisAP().
375 If the timeout expires in blocking mode,
376 BSP returns EFI_TIMEOUT. If the timeout
377 expires in non-blocking mode, WaitEvent
378 is signaled with SignalEvent().
379 @param[in] ProcedureArgument The parameter passed into Procedure for
380 all APs.
381 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
382 if all APs finish successfully, then its
383 content is set to NULL. If not all APs
384 finish before timeout expires, then its
385 content is set to address of the buffer
386 holding handle numbers of the failed APs.
387 The buffer is allocated by MP Initialization
388 library, and it's the caller's responsibility to
389 free the buffer with FreePool() service.
390 In blocking mode, it is ready for consumption
391 when the call returns. In non-blocking mode,
392 it is ready when WaitEvent is signaled. The
393 list of failed CPU is terminated by
394 END_OF_CPU_LIST.
395
396 @retval EFI_SUCCESS In blocking mode, all APs have finished before
397 the timeout expired.
398 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
399 to all enabled APs.
400 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
401 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
402 signaled.
403 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
404 supported.
405 @retval EFI_DEVICE_ERROR Caller processor is AP.
406 @retval EFI_NOT_STARTED No enabled APs exist in the system.
407 @retval EFI_NOT_READY Any enabled APs are busy.
408 @retval EFI_NOT_READY MP Initialize Library is not initialized.
409 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
410 all enabled APs have finished.
411 @retval EFI_INVALID_PARAMETER Procedure is NULL.
412
413 **/
414 EFI_STATUS
415 EFIAPI
416 MpInitLibStartupAllAPs (
417 IN EFI_AP_PROCEDURE Procedure,
418 IN BOOLEAN SingleThread,
419 IN EFI_EVENT WaitEvent OPTIONAL,
420 IN UINTN TimeoutInMicroseconds,
421 IN VOID *ProcedureArgument OPTIONAL,
422 OUT UINTN **FailedCpuList OPTIONAL
423 )
424 {
425 if (WaitEvent != NULL) {
426 return EFI_UNSUPPORTED;
427 }
428
429 return StartupAllAPsWorker (
430 Procedure,
431 SingleThread,
432 NULL,
433 TimeoutInMicroseconds,
434 ProcedureArgument,
435 FailedCpuList
436 );
437 }
438
439 /**
440 This service lets the caller get one enabled AP to execute a caller-provided
441 function.
442
443 @param[in] Procedure A pointer to the function to be run on the
444 designated AP of the system. See type
445 EFI_AP_PROCEDURE.
446 @param[in] ProcessorNumber The handle number of the AP. The range is
447 from 0 to the total number of logical
448 processors minus 1. The total number of
449 logical processors can be retrieved by
450 MpInitLibGetNumberOfProcessors().
451 @param[in] WaitEvent The event created by the caller with CreateEvent()
452 service. If it is NULL, then execute in
453 blocking mode. BSP waits until this AP finish
454 or TimeoutInMicroSeconds expires. If it's
455 not NULL, then execute in non-blocking mode.
456 BSP requests the function specified by
457 Procedure to be started on this AP,
458 and go on executing immediately. If this AP
459 return from Procedure or TimeoutInMicroSeconds
460 expires, this event is signaled. The BSP
461 can use the CheckEvent() or WaitForEvent()
462 services to check the state of event. Type
463 EFI_EVENT is defined in CreateEvent() in
464 the Unified Extensible Firmware Interface
465 Specification.
466 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
467 this AP to finish this Procedure, either for
468 blocking or non-blocking mode. Zero means
469 infinity. If the timeout expires before
470 this AP returns from Procedure, then Procedure
471 on the AP is terminated. The
472 AP is available for next function assigned
473 by MpInitLibStartupAllAPs() or
474 MpInitLibStartupThisAP().
475 If the timeout expires in blocking mode,
476 BSP returns EFI_TIMEOUT. If the timeout
477 expires in non-blocking mode, WaitEvent
478 is signaled with SignalEvent().
479 @param[in] ProcedureArgument The parameter passed into Procedure on the
480 specified AP.
481 @param[out] Finished If NULL, this parameter is ignored. In
482 blocking mode, this parameter is ignored.
483 In non-blocking mode, if AP returns from
484 Procedure before the timeout expires, its
485 content is set to TRUE. Otherwise, the
486 value is set to FALSE. The caller can
487 determine if the AP returned from Procedure
488 by evaluating this value.
489
490 @retval EFI_SUCCESS In blocking mode, specified AP finished before
491 the timeout expires.
492 @retval EFI_SUCCESS In non-blocking mode, the function has been
493 dispatched to specified AP.
494 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
495 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
496 signaled.
497 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
498 supported.
499 @retval EFI_DEVICE_ERROR The calling processor is an AP.
500 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
501 the specified AP has finished.
502 @retval EFI_NOT_READY The specified AP is busy.
503 @retval EFI_NOT_READY MP Initialize Library is not initialized.
504 @retval EFI_NOT_FOUND The processor with the handle specified by
505 ProcessorNumber does not exist.
506 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
507 @retval EFI_INVALID_PARAMETER Procedure is NULL.
508
509 **/
510 EFI_STATUS
511 EFIAPI
512 MpInitLibStartupThisAP (
513 IN EFI_AP_PROCEDURE Procedure,
514 IN UINTN ProcessorNumber,
515 IN EFI_EVENT WaitEvent OPTIONAL,
516 IN UINTN TimeoutInMicroseconds,
517 IN VOID *ProcedureArgument OPTIONAL,
518 OUT BOOLEAN *Finished OPTIONAL
519 )
520 {
521 if (WaitEvent != NULL) {
522 return EFI_UNSUPPORTED;
523 }
524
525 return StartupThisAPWorker (
526 Procedure,
527 ProcessorNumber,
528 NULL,
529 TimeoutInMicroseconds,
530 ProcedureArgument,
531 Finished
532 );
533 }
534
535 /**
536 This service switches the requested AP to be the BSP from that point onward.
537 This service changes the BSP for all purposes. This call can only be performed
538 by the current BSP.
539
540 @param[in] ProcessorNumber The handle number of AP that is to become the new
541 BSP. The range is from 0 to the total number of
542 logical processors minus 1. The total number of
543 logical processors can be retrieved by
544 MpInitLibGetNumberOfProcessors().
545 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
546 enabled AP. Otherwise, it will be disabled.
547
548 @retval EFI_SUCCESS BSP successfully switched.
549 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
550 this service returning.
551 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
552 @retval EFI_DEVICE_ERROR The calling processor is an AP.
553 @retval EFI_NOT_FOUND The processor with the handle specified by
554 ProcessorNumber does not exist.
555 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
556 a disabled AP.
557 @retval EFI_NOT_READY The specified AP is busy.
558 @retval EFI_NOT_READY MP Initialize Library is not initialized.
559
560 **/
561 EFI_STATUS
562 EFIAPI
563 MpInitLibSwitchBSP (
564 IN UINTN ProcessorNumber,
565 IN BOOLEAN EnableOldBSP
566 )
567 {
568 return SwitchBSPWorker (ProcessorNumber, EnableOldBSP);
569 }
570
571 /**
572 This service lets the caller enable or disable an AP from this point onward.
573 This service may only be called from the BSP.
574
575 @param[in] ProcessorNumber The handle number of AP.
576 The range is from 0 to the total number of
577 logical processors minus 1. The total number of
578 logical processors can be retrieved by
579 MpInitLibGetNumberOfProcessors().
580 @param[in] EnableAP Specifies the new state for the processor for
581 enabled, FALSE for disabled.
582 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
583 the new health status of the AP. This flag
584 corresponds to StatusFlag defined in
585 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
586 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
587 bits are ignored. If it is NULL, this parameter
588 is ignored.
589
590 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
591 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
592 prior to this service returning.
593 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
594 @retval EFI_DEVICE_ERROR The calling processor is an AP.
595 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
596 does not exist.
597 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
598 @retval EFI_NOT_READY MP Initialize Library is not initialized.
599
600 **/
601 EFI_STATUS
602 EFIAPI
603 MpInitLibEnableDisableAP (
604 IN UINTN ProcessorNumber,
605 IN BOOLEAN EnableAP,
606 IN UINT32 *HealthFlag OPTIONAL
607 )
608 {
609 return EnableDisableApWorker (ProcessorNumber, EnableAP, HealthFlag);
610 }
611
612