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