756e2b41206f8b3caa1dc524f6d888d814b27882
[mirror_edk2.git] / UefiCpuPkg / CpuMpPei / PeiMpServices.c
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
17
18
19 /**
20 Find the current Processor number by APIC ID.
21
22 @param PeiCpuMpData Pointer to PEI CPU MP Data
23 @param ProcessorNumber Return the pocessor number found
24
25 @retval EFI_SUCCESS ProcessorNumber is found and returned.
26 @retval EFI_NOT_FOUND ProcessorNumber is not found.
27 **/
28 EFI_STATUS
29 GetProcessorNumber (
30 IN PEI_CPU_MP_DATA *PeiCpuMpData,
31 OUT UINTN *ProcessorNumber
32 )
33 {
34 UINTN TotalProcessorNumber;
35 UINTN Index;
36
37 TotalProcessorNumber = PeiCpuMpData->CpuCount;
38 for (Index = 0; Index < TotalProcessorNumber; Index ++) {
39 if (PeiCpuMpData->CpuData[Index].ApicId == GetInitialApicId ()) {
40 *ProcessorNumber = Index;
41 return EFI_SUCCESS;
42 }
43 }
44 return EFI_NOT_FOUND;
45 }
46
47 /**
48 This service retrieves the number of logical processor in the platform
49 and the number of those logical processors that are enabled on this boot.
50 This service may only be called from the BSP.
51
52 This function is used to retrieve the following information:
53 - The number of logical processors that are present in the system.
54 - The number of enabled logical processors in the system at the instant
55 this call is made.
56
57 Because MP Service Ppi provides services to enable and disable processors
58 dynamically, the number of enabled logical processors may vary during the
59 course of a boot session.
60
61 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.
62 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then
63 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors
64 is returned in NumberOfProcessors, the number of currently enabled processor
65 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.
66
67 @param[in] PeiServices An indirect pointer to the PEI Services Table
68 published by the PEI Foundation.
69 @param[in] This Pointer to this instance of the PPI.
70 @param[out] NumberOfProcessors Pointer to the total number of logical processors in
71 the system, including the BSP and disabled APs.
72 @param[out] NumberOfEnabledProcessors
73 Number of processors in the system that are enabled.
74
75 @retval EFI_SUCCESS The number of logical processors and enabled
76 logical processors was retrieved.
77 @retval EFI_DEVICE_ERROR The calling processor is an AP.
78 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.
79 NumberOfEnabledProcessors is NULL.
80 **/
81 EFI_STATUS
82 EFIAPI
83 PeiGetNumberOfProcessors (
84 IN CONST EFI_PEI_SERVICES **PeiServices,
85 IN EFI_PEI_MP_SERVICES_PPI *This,
86 OUT UINTN *NumberOfProcessors,
87 OUT UINTN *NumberOfEnabledProcessors
88 )
89 {
90 PEI_CPU_MP_DATA *PeiCpuMpData;
91 UINTN CallerNumber;
92 UINTN ProcessorNumber;
93 UINTN EnabledProcessorNumber;
94 UINTN Index;
95
96 PeiCpuMpData = GetMpHobData ();
97 if (PeiCpuMpData == NULL) {
98 return EFI_NOT_FOUND;
99 }
100
101 if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {
102 return EFI_INVALID_PARAMETER;
103 }
104
105 //
106 // Check whether caller processor is BSP
107 //
108 PeiWhoAmI (PeiServices, This, &CallerNumber);
109 if (CallerNumber != PeiCpuMpData->BspNumber) {
110 return EFI_DEVICE_ERROR;
111 }
112
113 ProcessorNumber = PeiCpuMpData->CpuCount;
114 EnabledProcessorNumber = 0;
115 for (Index = 0; Index < ProcessorNumber; Index++) {
116 if (PeiCpuMpData->CpuData[Index].State != CpuStateDisabled) {
117 EnabledProcessorNumber ++;
118 }
119 }
120
121 *NumberOfProcessors = ProcessorNumber;
122 *NumberOfEnabledProcessors = EnabledProcessorNumber;
123
124 return EFI_SUCCESS;
125 }
126
127 /**
128 This return the handle number for the calling processor. This service may be
129 called from the BSP and APs.
130
131 This service returns the processor handle number for the calling processor.
132 The returned value is in the range from 0 to the total number of logical
133 processors minus 1. The total number of logical processors can be retrieved
134 with EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors(). This service may be
135 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
136 is returned. Otherwise, the current processors handle number is returned in
137 ProcessorNumber, and EFI_SUCCESS is returned.
138
139 @param[in] PeiServices An indirect pointer to the PEI Services Table
140 published by the PEI Foundation.
141 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.
142 @param[out] ProcessorNumber The handle number of the AP. The range is from 0 to the
143 total number of logical processors minus 1. The total
144 number of logical processors can be retrieved by
145 EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors().
146
147 @retval EFI_SUCCESS The current processor handle number was returned in
148 ProcessorNumber.
149 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
150 **/
151 EFI_STATUS
152 EFIAPI
153 PeiWhoAmI (
154 IN CONST EFI_PEI_SERVICES **PeiServices,
155 IN EFI_PEI_MP_SERVICES_PPI *This,
156 OUT UINTN *ProcessorNumber
157 )
158 {
159 PEI_CPU_MP_DATA *PeiCpuMpData;
160
161 PeiCpuMpData = GetMpHobData ();
162 if (PeiCpuMpData == NULL) {
163 return EFI_NOT_FOUND;
164 }
165
166 if (ProcessorNumber == NULL) {
167 return EFI_INVALID_PARAMETER;
168 }
169
170 return GetProcessorNumber (PeiCpuMpData, ProcessorNumber);
171 }
172