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