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