]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/PiSmmCpuDxeSmm/CpuService.c
UefiCpuPkg: Replace Opcode with the corresponding instructions.
[mirror_edk2.git] / UefiCpuPkg / PiSmmCpuDxeSmm / CpuService.c
1 /** @file
2 Implementation of SMM CPU Services Protocol.
3
4 Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "PiSmmCpuDxeSmm.h"
10
11 //
12 // SMM CPU Service Protocol instance
13 //
14 EFI_SMM_CPU_SERVICE_PROTOCOL mSmmCpuService = {
15 SmmGetProcessorInfo,
16 SmmSwitchBsp,
17 SmmAddProcessor,
18 SmmRemoveProcessor,
19 SmmWhoAmI,
20 SmmRegisterExceptionHandler
21 };
22
23 /**
24 Gets processor information on the requested processor at the instant this call is made.
25
26 @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.
27 @param[in] ProcessorNumber The handle number of processor.
28 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
29 the requested processor is deposited.
30
31 @retval EFI_SUCCESS Processor information was returned.
32 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
33 @retval EFI_INVALID_PARAMETER ProcessorNumber is invalid.
34 @retval EFI_NOT_FOUND The processor with the handle specified by
35 ProcessorNumber does not exist in the platform.
36
37 **/
38 EFI_STATUS
39 EFIAPI
40 SmmGetProcessorInfo (
41 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,
42 IN UINTN ProcessorNumber,
43 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer
44 )
45 {
46 //
47 // Check parameter
48 //
49 if ((ProcessorNumber >= mMaxNumberOfCpus) || (ProcessorInfoBuffer == NULL)) {
50 return EFI_INVALID_PARAMETER;
51 }
52
53 if (gSmmCpuPrivate->ProcessorInfo[ProcessorNumber].ProcessorId == INVALID_APIC_ID) {
54 return EFI_NOT_FOUND;
55 }
56
57 //
58 // Fill in processor information
59 //
60 CopyMem (ProcessorInfoBuffer, &gSmmCpuPrivate->ProcessorInfo[ProcessorNumber], sizeof (EFI_PROCESSOR_INFORMATION));
61 return EFI_SUCCESS;
62 }
63
64 /**
65 This service switches the requested AP to be the BSP since the next SMI.
66
67 @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.
68 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
69
70 @retval EFI_SUCCESS BSP will be switched in next SMI.
71 @retval EFI_UNSUPPORTED Switching the BSP or a processor to be hot-removed is not supported.
72 @retval EFI_NOT_FOUND The processor with the handle specified by ProcessorNumber does not exist.
73 @retval EFI_INVALID_PARAMETER ProcessorNumber is invalid.
74 **/
75 EFI_STATUS
76 EFIAPI
77 SmmSwitchBsp (
78 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,
79 IN UINTN ProcessorNumber
80 )
81 {
82 //
83 // Check parameter
84 //
85 if (ProcessorNumber >= mMaxNumberOfCpus) {
86 return EFI_INVALID_PARAMETER;
87 }
88
89 if (gSmmCpuPrivate->ProcessorInfo[ProcessorNumber].ProcessorId == INVALID_APIC_ID) {
90 return EFI_NOT_FOUND;
91 }
92
93 if ((gSmmCpuPrivate->Operation[ProcessorNumber] != SmmCpuNone) ||
94 (gSmst->CurrentlyExecutingCpu == ProcessorNumber))
95 {
96 return EFI_UNSUPPORTED;
97 }
98
99 //
100 // Setting of the BSP for next SMI is pending until all SMI handlers are finished
101 //
102 gSmmCpuPrivate->Operation[ProcessorNumber] = SmmCpuSwitchBsp;
103 return EFI_SUCCESS;
104 }
105
106 /**
107 Notify that a processor was hot-added.
108
109 @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.
110 @param[in] ProcessorId Local APIC ID of the hot-added processor.
111 @param[out] ProcessorNumber The handle number of the hot-added processor.
112
113 @retval EFI_SUCCESS The hot-addition of the specified processors was successfully notified.
114 @retval EFI_UNSUPPORTED Hot addition of processor is not supported.
115 @retval EFI_NOT_FOUND The processor with the handle specified by ProcessorNumber does not exist.
116 @retval EFI_INVALID_PARAMETER ProcessorNumber is invalid.
117 @retval EFI_ALREADY_STARTED The processor is already online in the system.
118 **/
119 EFI_STATUS
120 EFIAPI
121 SmmAddProcessor (
122 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,
123 IN UINT64 ProcessorId,
124 OUT UINTN *ProcessorNumber
125 )
126 {
127 UINTN Index;
128
129 if (!FeaturePcdGet (PcdCpuHotPlugSupport)) {
130 return EFI_UNSUPPORTED;
131 }
132
133 //
134 // Check parameter
135 //
136 if ((ProcessorNumber == NULL) || (ProcessorId == INVALID_APIC_ID)) {
137 return EFI_INVALID_PARAMETER;
138 }
139
140 //
141 // Check if the processor already exists
142 //
143
144 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {
145 if (gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId == ProcessorId) {
146 return EFI_ALREADY_STARTED;
147 }
148 }
149
150 //
151 // Check CPU hot plug data. The CPU RAS handler should have created the mapping
152 // of the APIC ID to SMBASE.
153 //
154 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {
155 if ((mCpuHotPlugData.ApicId[Index] == ProcessorId) &&
156 (gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId == INVALID_APIC_ID))
157 {
158 gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId = ProcessorId;
159 gSmmCpuPrivate->ProcessorInfo[Index].StatusFlag = 0;
160 GetProcessorLocationByApicId (
161 (UINT32)ProcessorId,
162 &gSmmCpuPrivate->ProcessorInfo[Index].Location.Package,
163 &gSmmCpuPrivate->ProcessorInfo[Index].Location.Core,
164 &gSmmCpuPrivate->ProcessorInfo[Index].Location.Thread
165 );
166
167 *ProcessorNumber = Index;
168 gSmmCpuPrivate->Operation[Index] = SmmCpuAdd;
169 return EFI_SUCCESS;
170 }
171 }
172
173 return EFI_INVALID_PARAMETER;
174 }
175
176 /**
177 Notify that a processor was hot-removed.
178
179 @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.
180 @param[in] ProcessorNumber The handle number of the hot-added processor.
181
182 @retval EFI_SUCCESS The hot-removal of the specified processors was successfully notified.
183 @retval EFI_UNSUPPORTED Hot removal of processor is not supported.
184 @retval EFI_UNSUPPORTED Hot removal of BSP is not supported.
185 @retval EFI_UNSUPPORTED Hot removal of a processor with pending hot-plug operation is not supported.
186 @retval EFI_INVALID_PARAMETER ProcessorNumber is invalid.
187 **/
188 EFI_STATUS
189 EFIAPI
190 SmmRemoveProcessor (
191 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,
192 IN UINTN ProcessorNumber
193 )
194 {
195 if (!FeaturePcdGet (PcdCpuHotPlugSupport)) {
196 return EFI_UNSUPPORTED;
197 }
198
199 //
200 // Check parameter
201 //
202 if ((ProcessorNumber >= mMaxNumberOfCpus) ||
203 (gSmmCpuPrivate->ProcessorInfo[ProcessorNumber].ProcessorId == INVALID_APIC_ID))
204 {
205 return EFI_INVALID_PARAMETER;
206 }
207
208 //
209 // Can't remove BSP
210 //
211 if (ProcessorNumber == gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu) {
212 return EFI_UNSUPPORTED;
213 }
214
215 if (gSmmCpuPrivate->Operation[ProcessorNumber] != SmmCpuNone) {
216 return EFI_UNSUPPORTED;
217 }
218
219 gSmmCpuPrivate->ProcessorInfo[ProcessorNumber].ProcessorId = INVALID_APIC_ID;
220 mCpuHotPlugData.ApicId[ProcessorNumber] = INVALID_APIC_ID;
221
222 //
223 // Removal of the processor from the CPU list is pending until all SMI handlers are finished
224 //
225 gSmmCpuPrivate->Operation[ProcessorNumber] = SmmCpuRemove;
226 return EFI_SUCCESS;
227 }
228
229 /**
230 This return the handle number for the calling processor.
231
232 @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.
233 @param[out] ProcessorNumber The handle number of currently executing processor.
234
235 @retval EFI_SUCCESS The current processor handle number was returned
236 in ProcessorNumber.
237 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
238
239 **/
240 EFI_STATUS
241 EFIAPI
242 SmmWhoAmI (
243 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,
244 OUT UINTN *ProcessorNumber
245 )
246 {
247 UINTN Index;
248 UINT64 ApicId;
249
250 //
251 // Check parameter
252 //
253 if (ProcessorNumber == NULL) {
254 return EFI_INVALID_PARAMETER;
255 }
256
257 ApicId = GetApicId ();
258
259 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {
260 if (gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId == ApicId) {
261 *ProcessorNumber = Index;
262 return EFI_SUCCESS;
263 }
264 }
265
266 //
267 // This should not happen
268 //
269 ASSERT (FALSE);
270 return EFI_NOT_FOUND;
271 }
272
273 /**
274 Update the SMM CPU list per the pending operation.
275
276 This function is called after return from SMI handlers.
277 **/
278 VOID
279 SmmCpuUpdate (
280 VOID
281 )
282 {
283 UINTN Index;
284
285 //
286 // Handle pending BSP switch operations
287 //
288 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {
289 if (gSmmCpuPrivate->Operation[Index] == SmmCpuSwitchBsp) {
290 gSmmCpuPrivate->Operation[Index] = SmmCpuNone;
291 mSmmMpSyncData->SwitchBsp = TRUE;
292 mSmmMpSyncData->CandidateBsp[Index] = TRUE;
293 }
294 }
295
296 //
297 // Handle pending hot-add operations
298 //
299 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {
300 if (gSmmCpuPrivate->Operation[Index] == SmmCpuAdd) {
301 gSmmCpuPrivate->Operation[Index] = SmmCpuNone;
302 mNumberOfCpus++;
303 }
304 }
305
306 //
307 // Handle pending hot-remove operations
308 //
309 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {
310 if (gSmmCpuPrivate->Operation[Index] == SmmCpuRemove) {
311 gSmmCpuPrivate->Operation[Index] = SmmCpuNone;
312 mNumberOfCpus--;
313 }
314 }
315 }
316
317 /**
318 Register exception handler.
319
320 @param This A pointer to the SMM_CPU_SERVICE_PROTOCOL instance.
321 @param ExceptionType Defines which interrupt or exception to hook. Type EFI_EXCEPTION_TYPE and
322 the valid values for this parameter are defined in EFI_DEBUG_SUPPORT_PROTOCOL
323 of the UEFI 2.0 specification.
324 @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER
325 that is called when a processor interrupt occurs.
326 If this parameter is NULL, then the handler will be uninstalled.
327
328 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
329 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was previously installed.
330 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not previously installed.
331 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.
332
333 **/
334 EFI_STATUS
335 EFIAPI
336 SmmRegisterExceptionHandler (
337 IN EFI_SMM_CPU_SERVICE_PROTOCOL *This,
338 IN EFI_EXCEPTION_TYPE ExceptionType,
339 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
340 )
341 {
342 return RegisterCpuInterruptHandler (ExceptionType, InterruptHandler);
343 }
344
345 /**
346 Initialize SMM CPU Services.
347
348 It installs EFI SMM CPU Services Protocol.
349
350 @param ImageHandle The firmware allocated handle for the EFI image.
351
352 @retval EFI_SUCCESS EFI SMM CPU Services Protocol was installed successfully.
353 **/
354 EFI_STATUS
355 InitializeSmmCpuServices (
356 IN EFI_HANDLE Handle
357 )
358 {
359 EFI_STATUS Status;
360
361 Status = gSmst->SmmInstallProtocolInterface (
362 &Handle,
363 &gEfiSmmCpuServiceProtocolGuid,
364 EFI_NATIVE_INTERFACE,
365 &mSmmCpuService
366 );
367 ASSERT_EFI_ERROR (Status);
368 return Status;
369 }