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