]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/CpuMpPei/PeiMpServices.c
UefiCpuPkg/CpuMpPei: Register callback on End Of Pei PPI
[mirror_edk2.git] / UefiCpuPkg / CpuMpPei / PeiMpServices.c
CommitLineData
887810c8
JF
1/** @file
2 Implementation of Multiple Processor PPI services.
3
4 Copyright (c) 2015, 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 "PeiMpServices.h"
16
e35d0347
JF
17//
18// CPU MP PPI to be installed
19//
20EFI_PEI_MP_SERVICES_PPI mMpServicesPpi = {
21 PeiGetNumberOfProcessors,
22 PeiGetProcessorInfo,
23 PeiStartupAllAPs,
24 PeiStartupThisAP,
25 PeiSwitchBSP,
26 PeiEnableDisableAP,
27 PeiWhoAmI,
28};
29
30EFI_PEI_PPI_DESCRIPTOR mPeiCpuMpPpiDesc = {
31 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
32 &gEfiPeiMpServicesPpiGuid,
33 &mMpServicesPpi
34};
887810c8 35
bf55f5b2
JF
36/**
37 Get CPU Package/Core/Thread location information.
38
39 @param InitialApicId CPU APIC ID
40 @param Location Pointer to CPU location information
41**/
42VOID
43ExtractProcessorLocation (
44 IN UINT32 InitialApicId,
45 OUT EFI_CPU_PHYSICAL_LOCATION *Location
46 )
47{
48 BOOLEAN TopologyLeafSupported;
49 UINTN ThreadBits;
50 UINTN CoreBits;
51 UINT32 RegEax;
52 UINT32 RegEbx;
53 UINT32 RegEcx;
54 UINT32 RegEdx;
55 UINT32 MaxCpuIdIndex;
56 UINT32 SubIndex;
57 UINTN LevelType;
58 UINT32 MaxLogicProcessorsPerPackage;
59 UINT32 MaxCoresPerPackage;
60
61 //
62 // Check if the processor is capable of supporting more than one logical processor.
63 //
64 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &RegEdx);
65 if ((RegEdx & BIT28) == 0) {
66 Location->Thread = 0;
67 Location->Core = 0;
68 Location->Package = 0;
69 return;
70 }
71
72 ThreadBits = 0;
73 CoreBits = 0;
74
75 //
76 // Assume three-level mapping of APIC ID: Package:Core:SMT.
77 //
78
79 TopologyLeafSupported = FALSE;
80 //
81 // Get the max index of basic CPUID
82 //
83 AsmCpuid (CPUID_SIGNATURE, &MaxCpuIdIndex, NULL, NULL, NULL);
84
85 //
86 // If the extended topology enumeration leaf is available, it
87 // is the preferred mechanism for enumerating topology.
88 //
89 if (MaxCpuIdIndex >= CPUID_EXTENDED_TOPOLOGY) {
90 AsmCpuidEx (CPUID_EXTENDED_TOPOLOGY, 0, &RegEax, &RegEbx, &RegEcx, NULL);
91 //
92 // If CPUID.(EAX=0BH, ECX=0H):EBX returns zero and maximum input value for
93 // basic CPUID information is greater than 0BH, then CPUID.0BH leaf is not
94 // supported on that processor.
95 //
96 if (RegEbx != 0) {
97 TopologyLeafSupported = TRUE;
98
99 //
100 // Sub-leaf index 0 (ECX= 0 as input) provides enumeration parameters to extract
101 // the SMT sub-field of x2APIC ID.
102 //
103 LevelType = (RegEcx >> 8) & 0xff;
104 ASSERT (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT);
105 ThreadBits = RegEax & 0x1f;
106
107 //
108 // Software must not assume any "level type" encoding
109 // value to be related to any sub-leaf index, except sub-leaf 0.
110 //
111 SubIndex = 1;
112 do {
113 AsmCpuidEx (CPUID_EXTENDED_TOPOLOGY, SubIndex, &RegEax, NULL, &RegEcx, NULL);
114 LevelType = (RegEcx >> 8) & 0xff;
115 if (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_CORE) {
116 CoreBits = (RegEax & 0x1f) - ThreadBits;
117 break;
118 }
119 SubIndex++;
120 } while (LevelType != CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_INVALID);
121 }
122 }
123
124 if (!TopologyLeafSupported) {
125 AsmCpuid (CPUID_VERSION_INFO, NULL, &RegEbx, NULL, NULL);
126 MaxLogicProcessorsPerPackage = (RegEbx >> 16) & 0xff;
127 if (MaxCpuIdIndex >= CPUID_CACHE_PARAMS) {
128 AsmCpuidEx (CPUID_CACHE_PARAMS, 0, &RegEax, NULL, NULL, NULL);
129 MaxCoresPerPackage = (RegEax >> 26) + 1;
130 } else {
131 //
132 // Must be a single-core processor.
133 //
134 MaxCoresPerPackage = 1;
135 }
136
137 ThreadBits = (UINTN) (HighBitSet32 (MaxLogicProcessorsPerPackage / MaxCoresPerPackage - 1) + 1);
138 CoreBits = (UINTN) (HighBitSet32 (MaxCoresPerPackage - 1) + 1);
139 }
140
141 Location->Thread = InitialApicId & ~((-1) << ThreadBits);
142 Location->Core = (InitialApicId >> ThreadBits) & ~((-1) << CoreBits);
143 Location->Package = (InitialApicId >> (ThreadBits + CoreBits));
144}
887810c8
JF
145
146/**
147 Find the current Processor number by APIC ID.
148
149 @param PeiCpuMpData Pointer to PEI CPU MP Data
150 @param ProcessorNumber Return the pocessor number found
151
152 @retval EFI_SUCCESS ProcessorNumber is found and returned.
153 @retval EFI_NOT_FOUND ProcessorNumber is not found.
154**/
155EFI_STATUS
156GetProcessorNumber (
157 IN PEI_CPU_MP_DATA *PeiCpuMpData,
158 OUT UINTN *ProcessorNumber
159 )
160{
161 UINTN TotalProcessorNumber;
162 UINTN Index;
163
164 TotalProcessorNumber = PeiCpuMpData->CpuCount;
165 for (Index = 0; Index < TotalProcessorNumber; Index ++) {
166 if (PeiCpuMpData->CpuData[Index].ApicId == GetInitialApicId ()) {
167 *ProcessorNumber = Index;
168 return EFI_SUCCESS;
169 }
170 }
171 return EFI_NOT_FOUND;
172}
173
3798f351
JF
174/**
175 Worker function for SwitchBSP().
176
177 Worker function for SwitchBSP(), assigned to the AP which is intended to become BSP.
178
179 @param Buffer Pointer to CPU MP Data
180**/
181VOID
182EFIAPI
183FutureBSPProc (
184 IN VOID *Buffer
185 )
186{
187 PEI_CPU_MP_DATA *DataInHob;
188
189 DataInHob = (PEI_CPU_MP_DATA *) Buffer;
190 AsmExchangeRole (&DataInHob->APInfo, &DataInHob->BSPInfo);
191}
192
a2cc8cae
JF
193/**
194 This service retrieves the number of logical processor in the platform
195 and the number of those logical processors that are enabled on this boot.
196 This service may only be called from the BSP.
197
198 This function is used to retrieve the following information:
199 - The number of logical processors that are present in the system.
200 - The number of enabled logical processors in the system at the instant
201 this call is made.
202
203 Because MP Service Ppi provides services to enable and disable processors
204 dynamically, the number of enabled logical processors may vary during the
205 course of a boot session.
206
207 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.
208 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then
209 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors
210 is returned in NumberOfProcessors, the number of currently enabled processor
211 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.
212
213 @param[in] PeiServices An indirect pointer to the PEI Services Table
214 published by the PEI Foundation.
215 @param[in] This Pointer to this instance of the PPI.
216 @param[out] NumberOfProcessors Pointer to the total number of logical processors in
217 the system, including the BSP and disabled APs.
218 @param[out] NumberOfEnabledProcessors
219 Number of processors in the system that are enabled.
220
221 @retval EFI_SUCCESS The number of logical processors and enabled
222 logical processors was retrieved.
223 @retval EFI_DEVICE_ERROR The calling processor is an AP.
224 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.
225 NumberOfEnabledProcessors is NULL.
226**/
227EFI_STATUS
228EFIAPI
229PeiGetNumberOfProcessors (
230 IN CONST EFI_PEI_SERVICES **PeiServices,
231 IN EFI_PEI_MP_SERVICES_PPI *This,
232 OUT UINTN *NumberOfProcessors,
233 OUT UINTN *NumberOfEnabledProcessors
234 )
235{
236 PEI_CPU_MP_DATA *PeiCpuMpData;
237 UINTN CallerNumber;
238 UINTN ProcessorNumber;
239 UINTN EnabledProcessorNumber;
240 UINTN Index;
241
242 PeiCpuMpData = GetMpHobData ();
243 if (PeiCpuMpData == NULL) {
244 return EFI_NOT_FOUND;
245 }
246
247 if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {
248 return EFI_INVALID_PARAMETER;
249 }
250
251 //
252 // Check whether caller processor is BSP
253 //
254 PeiWhoAmI (PeiServices, This, &CallerNumber);
255 if (CallerNumber != PeiCpuMpData->BspNumber) {
256 return EFI_DEVICE_ERROR;
257 }
258
259 ProcessorNumber = PeiCpuMpData->CpuCount;
260 EnabledProcessorNumber = 0;
261 for (Index = 0; Index < ProcessorNumber; Index++) {
262 if (PeiCpuMpData->CpuData[Index].State != CpuStateDisabled) {
263 EnabledProcessorNumber ++;
264 }
265 }
266
267 *NumberOfProcessors = ProcessorNumber;
268 *NumberOfEnabledProcessors = EnabledProcessorNumber;
269
270 return EFI_SUCCESS;
271}
887810c8 272
bf55f5b2
JF
273/**
274 Gets detailed MP-related information on the requested processor at the
275 instant this call is made. This service may only be called from the BSP.
276
277 This service retrieves detailed MP-related information about any processor
278 on the platform. Note the following:
279 - The processor information may change during the course of a boot session.
280 - The information presented here is entirely MP related.
281
282 Information regarding the number of caches and their sizes, frequency of operation,
283 slot numbers is all considered platform-related information and is not provided
284 by this service.
285
286 @param[in] PeiServices An indirect pointer to the PEI Services Table
287 published by the PEI Foundation.
288 @param[in] This Pointer to this instance of the PPI.
289 @param[in] ProcessorNumber Pointer to the total number of logical processors in
290 the system, including the BSP and disabled APs.
291 @param[out] ProcessorInfoBuffer Number of processors in the system that are enabled.
292
293 @retval EFI_SUCCESS Processor information was returned.
294 @retval EFI_DEVICE_ERROR The calling processor is an AP.
295 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
296 @retval EFI_NOT_FOUND The processor with the handle specified by
297 ProcessorNumber does not exist in the platform.
298**/
299EFI_STATUS
300EFIAPI
301PeiGetProcessorInfo (
302 IN CONST EFI_PEI_SERVICES **PeiServices,
303 IN EFI_PEI_MP_SERVICES_PPI *This,
304 IN UINTN ProcessorNumber,
305 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer
306 )
307{
308 PEI_CPU_MP_DATA *PeiCpuMpData;
309 UINTN CallerNumber;
310
311 PeiCpuMpData = GetMpHobData ();
312 if (PeiCpuMpData == NULL) {
313 return EFI_NOT_FOUND;
314 }
315
316 //
317 // Check whether caller processor is BSP
318 //
319 PeiWhoAmI (PeiServices, This, &CallerNumber);
320 if (CallerNumber != PeiCpuMpData->BspNumber) {
321 return EFI_DEVICE_ERROR;
322 }
323
324 if (ProcessorInfoBuffer == NULL) {
325 return EFI_INVALID_PARAMETER;
326 }
327
328 if (ProcessorNumber >= PeiCpuMpData->CpuCount) {
329 return EFI_NOT_FOUND;
330 }
331
332 ProcessorInfoBuffer->ProcessorId = (UINT64) PeiCpuMpData->CpuData[ProcessorNumber].ApicId;
333 ProcessorInfoBuffer->StatusFlag = 0;
334 if (PeiCpuMpData->CpuData[ProcessorNumber].ApicId == GetInitialApicId()) {
335 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT;
336 }
337 if (PeiCpuMpData->CpuData[ProcessorNumber].Health.Uint32 == 0) {
338 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_HEALTH_STATUS_BIT;
339 }
340 if (PeiCpuMpData->CpuData[ProcessorNumber].State == CpuStateDisabled) {
341 ProcessorInfoBuffer->StatusFlag &= ~PROCESSOR_ENABLED_BIT;
342 } else {
343 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_ENABLED_BIT;
344 }
345
346 //
347 // Get processor location information
348 //
349 ExtractProcessorLocation (PeiCpuMpData->CpuData[ProcessorNumber].ApicId, &ProcessorInfoBuffer->Location);
350
351 return EFI_SUCCESS;
352}
353
60ca9e8c
JF
354/**
355 This service executes a caller provided function on all enabled APs. APs can
356 run either simultaneously or one at a time in sequence. This service supports
357 both blocking requests only. This service may only
358 be called from the BSP.
359
360 This function is used to dispatch all the enabled APs to the function specified
361 by Procedure. If any enabled AP is busy, then EFI_NOT_READY is returned
362 immediately and Procedure is not started on any AP.
363
364 If SingleThread is TRUE, all the enabled APs execute the function specified by
365 Procedure one by one, in ascending order of processor handle number. Otherwise,
366 all the enabled APs execute the function specified by Procedure simultaneously.
367
368 If the timeout specified by TimeoutInMicroSeconds expires before all APs return
369 from Procedure, then Procedure on the failed APs is terminated. All enabled APs
370 are always available for further calls to EFI_PEI_MP_SERVICES_PPI.StartupAllAPs()
371 and EFI_PEI_MP_SERVICES_PPI.StartupThisAP(). If FailedCpuList is not NULL, its
372 content points to the list of processor handle numbers in which Procedure was
373 terminated.
374
375 Note: It is the responsibility of the consumer of the EFI_PEI_MP_SERVICES_PPI.StartupAllAPs()
376 to make sure that the nature of the code that is executed on the BSP and the
377 dispatched APs is well controlled. The MP Services Ppi does not guarantee
378 that the Procedure function is MP-safe. Hence, the tasks that can be run in
379 parallel are limited to certain independent tasks and well-controlled exclusive
380 code. PEI services and Ppis may not be called by APs unless otherwise
381 specified.
382
383 In blocking execution mode, BSP waits until all APs finish or
384 TimeoutInMicroSeconds expires.
385
386 @param[in] PeiServices An indirect pointer to the PEI Services Table
387 published by the PEI Foundation.
388 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.
389 @param[in] Procedure A pointer to the function to be run on enabled APs of
390 the system.
391 @param[in] SingleThread If TRUE, then all the enabled APs execute the function
392 specified by Procedure one by one, in ascending order
393 of processor handle number. If FALSE, then all the
394 enabled APs execute the function specified by Procedure
395 simultaneously.
396 @param[in] TimeoutInMicroSeconds
397 Indicates the time limit in microseconds for APs to
398 return from Procedure, for blocking mode only. Zero
399 means infinity. If the timeout expires before all APs
400 return from Procedure, then Procedure on the failed APs
401 is terminated. All enabled APs are available for next
402 function assigned by EFI_PEI_MP_SERVICES_PPI.StartupAllAPs()
403 or EFI_PEI_MP_SERVICES_PPI.StartupThisAP(). If the
404 timeout expires in blocking mode, BSP returns
405 EFI_TIMEOUT.
406 @param[in] ProcedureArgument The parameter passed into Procedure for all APs.
407
408 @retval EFI_SUCCESS In blocking mode, all APs have finished before the
409 timeout expired.
410 @retval EFI_DEVICE_ERROR Caller processor is AP.
411 @retval EFI_NOT_STARTED No enabled APs exist in the system.
412 @retval EFI_NOT_READY Any enabled APs are busy.
413 @retval EFI_TIMEOUT In blocking mode, the timeout expired before all
414 enabled APs have finished.
415 @retval EFI_INVALID_PARAMETER Procedure is NULL.
416**/
417EFI_STATUS
418EFIAPI
419PeiStartupAllAPs (
420 IN CONST EFI_PEI_SERVICES **PeiServices,
421 IN EFI_PEI_MP_SERVICES_PPI *This,
422 IN EFI_AP_PROCEDURE Procedure,
423 IN BOOLEAN SingleThread,
424 IN UINTN TimeoutInMicroSeconds,
425 IN VOID *ProcedureArgument OPTIONAL
426 )
427{
428 PEI_CPU_MP_DATA *PeiCpuMpData;
429 UINTN ProcessorNumber;
430 UINTN Index;
431 UINTN CallerNumber;
432 BOOLEAN HasEnabledAp;
433 BOOLEAN HasEnabledIdleAp;
434 volatile UINT32 *FinishedCount;
435 EFI_STATUS Status;
436 UINTN WaitCountIndex;
437 UINTN WaitCountNumber;
438
439 PeiCpuMpData = GetMpHobData ();
440 if (PeiCpuMpData == NULL) {
441 return EFI_NOT_FOUND;
442 }
443
444 //
445 // Check whether caller processor is BSP
446 //
447 PeiWhoAmI (PeiServices, This, &CallerNumber);
448 if (CallerNumber != PeiCpuMpData->BspNumber) {
449 return EFI_DEVICE_ERROR;
450 }
451
452 ProcessorNumber = PeiCpuMpData->CpuCount;
453
454 HasEnabledAp = FALSE;
455 HasEnabledIdleAp = FALSE;
456 for (Index = 0; Index < ProcessorNumber; Index ++) {
457 if (Index == CallerNumber) {
458 //
459 // Skip BSP
460 //
461 continue;
462 }
463 if (PeiCpuMpData->CpuData[Index].State != CpuStateDisabled) {
464 HasEnabledAp = TRUE;
465 if (PeiCpuMpData->CpuData[Index].State != CpuStateBusy) {
466 HasEnabledIdleAp = TRUE;
467 }
468 }
469 }
470 if (!HasEnabledAp) {
471 //
472 // If no enabled AP exists, return EFI_NOT_STARTED.
473 //
474 return EFI_NOT_STARTED;
475 }
476 if (!HasEnabledIdleAp) {
477 //
478 // If any enabled APs are busy, return EFI_NOT_READY.
479 //
480 return EFI_NOT_READY;
481 }
482
8f7b315b
JF
483 if (PeiCpuMpData->EndOfPeiFlag) {
484 //
485 // Backup original data and copy AP reset vector in it
486 //
487 BackupAndPrepareWakeupBuffer(PeiCpuMpData);
488 }
489
60ca9e8c
JF
490 WaitCountNumber = TimeoutInMicroSeconds / CPU_CHECK_AP_INTERVAL + 1;
491 WaitCountIndex = 0;
492 FinishedCount = &PeiCpuMpData->FinishedCount;
493 if (!SingleThread) {
494 WakeUpAP (PeiCpuMpData, TRUE, 0, Procedure, ProcedureArgument);
495 //
496 // Wait to finish
497 //
498 if (TimeoutInMicroSeconds == 0) {
499 while (*FinishedCount < ProcessorNumber - 1) {
500 CpuPause ();
501 }
502 Status = EFI_SUCCESS;
503 } else {
504 Status = EFI_TIMEOUT;
505 for (WaitCountIndex = 0; WaitCountIndex < WaitCountNumber; WaitCountIndex++) {
506 MicroSecondDelay (CPU_CHECK_AP_INTERVAL);
507 if (*FinishedCount >= ProcessorNumber - 1) {
508 Status = EFI_SUCCESS;
509 break;
510 }
511 }
512 }
513 } else {
514 Status = EFI_SUCCESS;
515 for (Index = 0; Index < ProcessorNumber; Index++) {
516 if (Index == CallerNumber) {
517 continue;
518 }
519 WakeUpAP (PeiCpuMpData, FALSE, PeiCpuMpData->CpuData[Index].ApicId, Procedure, ProcedureArgument);
520 //
521 // Wait to finish
522 //
523 if (TimeoutInMicroSeconds == 0) {
524 while (*FinishedCount < 1) {
525 CpuPause ();
526 }
527 } else {
528 for (WaitCountIndex = 0; WaitCountIndex < WaitCountNumber; WaitCountIndex++) {
529 MicroSecondDelay (CPU_CHECK_AP_INTERVAL);
530 if (*FinishedCount >= 1) {
531 break;
532 }
533 }
534 if (WaitCountIndex == WaitCountNumber) {
535 Status = EFI_TIMEOUT;
536 }
537 }
538 }
539 }
540
8f7b315b
JF
541 if (PeiCpuMpData->EndOfPeiFlag) {
542 //
543 // Restore original data
544 //
545 RestoreWakeupBuffer(PeiCpuMpData);
546 }
547
60ca9e8c
JF
548 return Status;
549}
550
d11bbfff
JF
551/**
552 This service lets the caller get one enabled AP to execute a caller-provided
553 function. The caller can request the BSP to wait for the completion
554 of the AP. This service may only be called from the BSP.
555
556 This function is used to dispatch one enabled AP to the function specified by
557 Procedure passing in the argument specified by ProcedureArgument.
558 The execution is in blocking mode. The BSP waits until the AP finishes or
559 TimeoutInMicroSecondss expires.
560
561 If the timeout specified by TimeoutInMicroseconds expires before the AP returns
562 from Procedure, then execution of Procedure by the AP is terminated. The AP is
563 available for subsequent calls to EFI_PEI_MP_SERVICES_PPI.StartupAllAPs() and
564 EFI_PEI_MP_SERVICES_PPI.StartupThisAP().
565
566 @param[in] PeiServices An indirect pointer to the PEI Services Table
567 published by the PEI Foundation.
568 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.
569 @param[in] Procedure A pointer to the function to be run on enabled APs of
570 the system.
571 @param[in] ProcessorNumber The handle number of the AP. The range is from 0 to the
572 total number of logical processors minus 1. The total
573 number of logical processors can be retrieved by
574 EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors().
575 @param[in] TimeoutInMicroseconds
576 Indicates the time limit in microseconds for APs to
577 return from Procedure, for blocking mode only. Zero
578 means infinity. If the timeout expires before all APs
579 return from Procedure, then Procedure on the failed APs
580 is terminated. All enabled APs are available for next
581 function assigned by EFI_PEI_MP_SERVICES_PPI.StartupAllAPs()
582 or EFI_PEI_MP_SERVICES_PPI.StartupThisAP(). If the
583 timeout expires in blocking mode, BSP returns
584 EFI_TIMEOUT.
585 @param[in] ProcedureArgument The parameter passed into Procedure for all APs.
586
587 @retval EFI_SUCCESS In blocking mode, specified AP finished before the
588 timeout expires.
589 @retval EFI_DEVICE_ERROR The calling processor is an AP.
590 @retval EFI_TIMEOUT In blocking mode, the timeout expired before the
591 specified AP has finished.
592 @retval EFI_NOT_FOUND The processor with the handle specified by
593 ProcessorNumber does not exist.
594 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
595 @retval EFI_INVALID_PARAMETER Procedure is NULL.
596**/
597EFI_STATUS
598EFIAPI
599PeiStartupThisAP (
600 IN CONST EFI_PEI_SERVICES **PeiServices,
601 IN EFI_PEI_MP_SERVICES_PPI *This,
602 IN EFI_AP_PROCEDURE Procedure,
603 IN UINTN ProcessorNumber,
604 IN UINTN TimeoutInMicroseconds,
605 IN VOID *ProcedureArgument OPTIONAL
606 )
607{
608 PEI_CPU_MP_DATA *PeiCpuMpData;
609 UINTN CallerNumber;
610 volatile UINT32 *FinishedCount;
611 EFI_STATUS Status;
612 UINTN WaitCountIndex;
613 UINTN WaitCountNumber;
614
615 PeiCpuMpData = GetMpHobData ();
616 if (PeiCpuMpData == NULL) {
617 return EFI_NOT_FOUND;
618 }
619
620 //
621 // Check whether caller processor is BSP
622 //
623 PeiWhoAmI (PeiServices, This, &CallerNumber);
624 if (CallerNumber != PeiCpuMpData->BspNumber) {
625 return EFI_DEVICE_ERROR;
626 }
627
628 if (ProcessorNumber >= PeiCpuMpData->CpuCount) {
629 return EFI_NOT_FOUND;
630 }
631
632 if (ProcessorNumber == PeiCpuMpData->BspNumber || Procedure == NULL) {
633 return EFI_INVALID_PARAMETER;
634 }
635
636 //
637 // Check whether specified AP is disabled
638 //
639 if (PeiCpuMpData->CpuData[ProcessorNumber].State == CpuStateDisabled) {
640 return EFI_INVALID_PARAMETER;
641 }
642
8f7b315b
JF
643 if (PeiCpuMpData->EndOfPeiFlag) {
644 //
645 // Backup original data and copy AP reset vector in it
646 //
647 BackupAndPrepareWakeupBuffer(PeiCpuMpData);
648 }
649
d11bbfff
JF
650 WaitCountNumber = TimeoutInMicroseconds / CPU_CHECK_AP_INTERVAL + 1;
651 WaitCountIndex = 0;
652 FinishedCount = &PeiCpuMpData->FinishedCount;
653
654 WakeUpAP (PeiCpuMpData, FALSE, PeiCpuMpData->CpuData[ProcessorNumber].ApicId, Procedure, ProcedureArgument);
655
656 //
657 // Wait to finish
658 //
659 if (TimeoutInMicroseconds == 0) {
660 while (*FinishedCount < 1) {
661 CpuPause() ;
662 }
663 Status = EFI_SUCCESS;
664 } else {
665 Status = EFI_TIMEOUT;
666 for (WaitCountIndex = 0; WaitCountIndex < WaitCountNumber; WaitCountIndex++) {
667 MicroSecondDelay (CPU_CHECK_AP_INTERVAL);
668 if (*FinishedCount >= 1) {
669 Status = EFI_SUCCESS;
670 break;
671 }
672 }
673 }
674
8f7b315b
JF
675 if (PeiCpuMpData->EndOfPeiFlag) {
676 //
677 // Backup original data and copy AP reset vector in it
678 //
679 RestoreWakeupBuffer(PeiCpuMpData);
680 }
681
d11bbfff
JF
682 return Status;
683}
684
3798f351
JF
685/**
686 This service switches the requested AP to be the BSP from that point onward.
687 This service changes the BSP for all purposes. This call can only be performed
688 by the current BSP.
689
690 This service switches the requested AP to be the BSP from that point onward.
691 This service changes the BSP for all purposes. The new BSP can take over the
692 execution of the old BSP and continue seamlessly from where the old one left
693 off.
694
695 If the BSP cannot be switched prior to the return from this service, then
696 EFI_UNSUPPORTED must be returned.
697
698 @param[in] PeiServices An indirect pointer to the PEI Services Table
699 published by the PEI Foundation.
700 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.
701 @param[in] ProcessorNumber The handle number of the AP. The range is from 0 to the
702 total number of logical processors minus 1. The total
703 number of logical processors can be retrieved by
704 EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors().
705 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an enabled
706 AP. Otherwise, it will be disabled.
707
708 @retval EFI_SUCCESS BSP successfully switched.
709 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to this
710 service returning.
711 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
712 @retval EFI_SUCCESS The calling processor is an AP.
713 @retval EFI_NOT_FOUND The processor with the handle specified by
714 ProcessorNumber does not exist.
715 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or a disabled
716 AP.
717 @retval EFI_NOT_READY The specified AP is busy.
718**/
719EFI_STATUS
720EFIAPI
721PeiSwitchBSP (
722 IN CONST EFI_PEI_SERVICES **PeiServices,
723 IN EFI_PEI_MP_SERVICES_PPI *This,
724 IN UINTN ProcessorNumber,
725 IN BOOLEAN EnableOldBSP
726 )
727{
728 PEI_CPU_MP_DATA *PeiCpuMpData;
729 UINTN CallerNumber;
730 MSR_IA32_APIC_BASE ApicBaseMsr;
731
732 PeiCpuMpData = GetMpHobData ();
733 if (PeiCpuMpData == NULL) {
734 return EFI_NOT_FOUND;
735 }
736
737 //
738 // Check whether caller processor is BSP
739 //
740 PeiWhoAmI (PeiServices, This, &CallerNumber);
741 if (CallerNumber != PeiCpuMpData->BspNumber) {
742 return EFI_SUCCESS;
743 }
744
745 if (ProcessorNumber >= PeiCpuMpData->CpuCount) {
746 return EFI_NOT_FOUND;
747 }
748
749 //
750 // Check whether specified AP is disabled
751 //
752 if (PeiCpuMpData->CpuData[ProcessorNumber].State == CpuStateDisabled) {
753 return EFI_INVALID_PARAMETER;
754 }
755
756 //
757 // Check whether ProcessorNumber specifies the current BSP
758 //
759 if (ProcessorNumber == PeiCpuMpData->BspNumber) {
760 return EFI_INVALID_PARAMETER;
761 }
762
763 //
764 // Check whether specified AP is busy
765 //
766 if (PeiCpuMpData->CpuData[ProcessorNumber].State == CpuStateBusy) {
767 return EFI_NOT_READY;
768 }
769
770 //
771 // Clear the BSP bit of MSR_IA32_APIC_BASE
772 //
773 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE_ADDRESS);
774 ApicBaseMsr.Bits.Bsp = 0;
775 AsmWriteMsr64 (MSR_IA32_APIC_BASE_ADDRESS, ApicBaseMsr.Uint64);
776
777 PeiCpuMpData->BSPInfo.State = CPU_SWITCH_STATE_IDLE;
778 PeiCpuMpData->APInfo.State = CPU_SWITCH_STATE_IDLE;
779
8f7b315b
JF
780 if (PeiCpuMpData->EndOfPeiFlag) {
781 //
782 // Backup original data and copy AP reset vector in it
783 //
784 BackupAndPrepareWakeupBuffer(PeiCpuMpData);
785 }
786
3798f351
JF
787 //
788 // Need to wakeUp AP (future BSP).
789 //
790 WakeUpAP (PeiCpuMpData, FALSE, PeiCpuMpData->CpuData[ProcessorNumber].ApicId, FutureBSPProc, PeiCpuMpData);
791
792 AsmExchangeRole (&PeiCpuMpData->BSPInfo, &PeiCpuMpData->APInfo);
793
8f7b315b
JF
794 if (PeiCpuMpData->EndOfPeiFlag) {
795 //
796 // Backup original data and copy AP reset vector in it
797 //
798 RestoreWakeupBuffer(PeiCpuMpData);
799 }
800
3798f351
JF
801 //
802 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
803 //
804 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE_ADDRESS);
805 ApicBaseMsr.Bits.Bsp = 1;
806 AsmWriteMsr64 (MSR_IA32_APIC_BASE_ADDRESS, ApicBaseMsr.Uint64);
807
808 return EFI_SUCCESS;
809}
810
53d3f06f
JF
811/**
812 This service lets the caller enable or disable an AP from this point onward.
813 This service may only be called from the BSP.
814
815 This service allows the caller enable or disable an AP from this point onward.
816 The caller can optionally specify the health status of the AP by Health. If
817 an AP is being disabled, then the state of the disabled AP is implementation
818 dependent. If an AP is enabled, then the implementation must guarantee that a
819 complete initialization sequence is performed on the AP, so the AP is in a state
820 that is compatible with an MP operating system.
821
822 If the enable or disable AP operation cannot be completed prior to the return
823 from this service, then EFI_UNSUPPORTED must be returned.
824
825 @param[in] PeiServices An indirect pointer to the PEI Services Table
826 published by the PEI Foundation.
827 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.
828 @param[in] ProcessorNumber The handle number of the AP. The range is from 0 to the
829 total number of logical processors minus 1. The total
830 number of logical processors can be retrieved by
831 EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors().
832 @param[in] EnableAP Specifies the new state for the processor for enabled,
833 FALSE for disabled.
834 @param[in] HealthFlag If not NULL, a pointer to a value that specifies the
835 new health status of the AP. This flag corresponds to
836 StatusFlag defined in EFI_PEI_MP_SERVICES_PPI.GetProcessorInfo().
837 Only the PROCESSOR_HEALTH_STATUS_BIT is used. All other
838 bits are ignored. If it is NULL, this parameter is
839 ignored.
840
841 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
842 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed prior
843 to this service returning.
844 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
845 @retval EFI_DEVICE_ERROR The calling processor is an AP.
846 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
847 does not exist.
848 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
849**/
850EFI_STATUS
851EFIAPI
852PeiEnableDisableAP (
853 IN CONST EFI_PEI_SERVICES **PeiServices,
854 IN EFI_PEI_MP_SERVICES_PPI *This,
855 IN UINTN ProcessorNumber,
856 IN BOOLEAN EnableAP,
857 IN UINT32 *HealthFlag OPTIONAL
858 )
859{
860 PEI_CPU_MP_DATA *PeiCpuMpData;
861 UINTN CallerNumber;
862
863 PeiCpuMpData = GetMpHobData ();
864 if (PeiCpuMpData == NULL) {
865 return EFI_NOT_FOUND;
866 }
867
868 //
869 // Check whether caller processor is BSP
870 //
871 PeiWhoAmI (PeiServices, This, &CallerNumber);
872 if (CallerNumber != PeiCpuMpData->BspNumber) {
873 return EFI_DEVICE_ERROR;
874 }
875
876 if (ProcessorNumber == PeiCpuMpData->BspNumber) {
877 return EFI_INVALID_PARAMETER;
878 }
879
880 if (ProcessorNumber >= PeiCpuMpData->CpuCount) {
881 return EFI_NOT_FOUND;
882 }
883
884 if (!EnableAP) {
885 PeiCpuMpData->CpuData[ProcessorNumber].State = CpuStateDisabled;
886 } else {
887 PeiCpuMpData->CpuData[ProcessorNumber].State = CpuStateIdle;
888 }
889
890 if (HealthFlag != NULL) {
891 PeiCpuMpData->CpuData[ProcessorNumber].CpuHealthy =
892 (BOOLEAN) ((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) != 0);
893 }
894 return EFI_SUCCESS;
895}
d11bbfff 896
887810c8
JF
897/**
898 This return the handle number for the calling processor. This service may be
899 called from the BSP and APs.
900
901 This service returns the processor handle number for the calling processor.
902 The returned value is in the range from 0 to the total number of logical
903 processors minus 1. The total number of logical processors can be retrieved
904 with EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors(). This service may be
905 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
906 is returned. Otherwise, the current processors handle number is returned in
907 ProcessorNumber, and EFI_SUCCESS is returned.
908
909 @param[in] PeiServices An indirect pointer to the PEI Services Table
910 published by the PEI Foundation.
911 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.
912 @param[out] ProcessorNumber The handle number of the AP. The range is from 0 to the
913 total number of logical processors minus 1. The total
914 number of logical processors can be retrieved by
915 EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors().
916
917 @retval EFI_SUCCESS The current processor handle number was returned in
918 ProcessorNumber.
919 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
920**/
921EFI_STATUS
922EFIAPI
923PeiWhoAmI (
924 IN CONST EFI_PEI_SERVICES **PeiServices,
925 IN EFI_PEI_MP_SERVICES_PPI *This,
926 OUT UINTN *ProcessorNumber
927 )
928{
929 PEI_CPU_MP_DATA *PeiCpuMpData;
930
931 PeiCpuMpData = GetMpHobData ();
932 if (PeiCpuMpData == NULL) {
933 return EFI_NOT_FOUND;
934 }
935
936 if (ProcessorNumber == NULL) {
937 return EFI_INVALID_PARAMETER;
938 }
939
940 return GetProcessorNumber (PeiCpuMpData, ProcessorNumber);
941}
942