]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/PiSmmCpuDxeSmm/CpuService.c
UefiCpuPkg: Replace BSD License with BSD+Patent License
[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
41 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,\r
42 IN UINTN ProcessorNumber,\r
43 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer\r
44 )\r
45{\r
46 //\r
47 // Check parameter\r
48 //\r
49 if (ProcessorNumber >= mMaxNumberOfCpus || ProcessorInfoBuffer == NULL) {\r
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
78 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,\r
79 IN UINTN ProcessorNumber\r
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
93 if (gSmmCpuPrivate->Operation[ProcessorNumber] != SmmCpuNone ||\r
94 gSmst->CurrentlyExecutingCpu == ProcessorNumber) {\r
95 return EFI_UNSUPPORTED;\r
96 }\r
97\r
98 //\r
99 // Setting of the BSP for next SMI is pending until all SMI handlers are finished\r
100 //\r
101 gSmmCpuPrivate->Operation[ProcessorNumber] = SmmCpuSwitchBsp;\r
102 return EFI_SUCCESS;\r
103}\r
104\r
105/**\r
106 Notify that a processor was hot-added.\r
107\r
108 @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.\r
109 @param[in] ProcessorId Local APIC ID of the hot-added processor.\r
110 @param[out] ProcessorNumber The handle number of the hot-added processor.\r
111\r
112 @retval EFI_SUCCESS The hot-addition of the specified processors was successfully notified.\r
113 @retval EFI_UNSUPPORTED Hot addition of processor is not supported.\r
114 @retval EFI_NOT_FOUND The processor with the handle specified by ProcessorNumber does not exist.\r
115 @retval EFI_INVALID_PARAMETER ProcessorNumber is invalid.\r
116 @retval EFI_ALREADY_STARTED The processor is already online in the system.\r
117**/\r
118EFI_STATUS\r
119EFIAPI\r
120SmmAddProcessor (\r
121 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,\r
122 IN UINT64 ProcessorId,\r
123 OUT UINTN *ProcessorNumber\r
124 )\r
125{\r
126 UINTN Index;\r
127\r
128 if (!FeaturePcdGet (PcdCpuHotPlugSupport)) {\r
129 return EFI_UNSUPPORTED;\r
130 }\r
131\r
132 //\r
133 // Check parameter\r
134 //\r
135 if (ProcessorNumber == NULL || ProcessorId == INVALID_APIC_ID) {\r
136 return EFI_INVALID_PARAMETER;\r
137 }\r
138\r
139 //\r
140 // Check if the processor already exists\r
141 //\r
142\r
143 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
144 if (gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId == ProcessorId) {\r
145 return EFI_ALREADY_STARTED;\r
146 }\r
147 }\r
148\r
149 //\r
150 // Check CPU hot plug data. The CPU RAS handler should have created the mapping\r
151 // of the APIC ID to SMBASE.\r
152 //\r
153 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
154 if (mCpuHotPlugData.ApicId[Index] == ProcessorId &&\r
155 gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId == INVALID_APIC_ID) {\r
156 gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId = ProcessorId;\r
157 gSmmCpuPrivate->ProcessorInfo[Index].StatusFlag = 0;\r
262128e5 158 GetProcessorLocationByApicId (\r
73152f19
LD
159 (UINT32)ProcessorId,\r
160 &gSmmCpuPrivate->ProcessorInfo[Index].Location.Package,\r
161 &gSmmCpuPrivate->ProcessorInfo[Index].Location.Core,\r
162 &gSmmCpuPrivate->ProcessorInfo[Index].Location.Thread\r
163 );\r
529a5a86
MK
164\r
165 *ProcessorNumber = Index;\r
166 gSmmCpuPrivate->Operation[Index] = SmmCpuAdd;\r
167 return EFI_SUCCESS;\r
168 }\r
169 }\r
170\r
171 return EFI_INVALID_PARAMETER;\r
172}\r
173\r
174/**\r
175 Notify that a processor was hot-removed.\r
176\r
177 @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.\r
178 @param[in] ProcessorNumber The handle number of the hot-added processor.\r
179\r
180 @retval EFI_SUCCESS The hot-removal of the specified processors was successfully notified.\r
181 @retval EFI_UNSUPPORTED Hot removal of processor is not supported.\r
182 @retval EFI_UNSUPPORTED Hot removal of BSP is not supported.\r
183 @retval EFI_UNSUPPORTED Hot removal of a processor with pending hot-plug operation is not supported.\r
184 @retval EFI_INVALID_PARAMETER ProcessorNumber is invalid.\r
185**/\r
186EFI_STATUS\r
187EFIAPI\r
188SmmRemoveProcessor (\r
189 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,\r
190 IN UINTN ProcessorNumber\r
191 )\r
192{\r
193 if (!FeaturePcdGet (PcdCpuHotPlugSupport)) {\r
194 return EFI_UNSUPPORTED;\r
195 }\r
196\r
197 //\r
198 // Check parameter\r
199 //\r
200 if (ProcessorNumber >= mMaxNumberOfCpus ||\r
201 gSmmCpuPrivate->ProcessorInfo[ProcessorNumber].ProcessorId == INVALID_APIC_ID) {\r
202 return EFI_INVALID_PARAMETER;\r
203 }\r
204\r
205 //\r
206 // Can't remove BSP\r
207 //\r
208 if (ProcessorNumber == gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu) {\r
209 return EFI_UNSUPPORTED;\r
210 }\r
211\r
212 if (gSmmCpuPrivate->Operation[ProcessorNumber] != SmmCpuNone) {\r
213 return EFI_UNSUPPORTED;\r
214 }\r
215\r
216 gSmmCpuPrivate->ProcessorInfo[ProcessorNumber].ProcessorId = INVALID_APIC_ID;\r
217 mCpuHotPlugData.ApicId[ProcessorNumber] = INVALID_APIC_ID;\r
218\r
219 //\r
220 // Removal of the processor from the CPU list is pending until all SMI handlers are finished\r
221 //\r
222 gSmmCpuPrivate->Operation[ProcessorNumber] = SmmCpuRemove;\r
223 return EFI_SUCCESS;\r
224}\r
225\r
226/**\r
227 This return the handle number for the calling processor.\r
228\r
229 @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.\r
230 @param[out] ProcessorNumber The handle number of currently executing processor.\r
231\r
232 @retval EFI_SUCCESS The current processor handle number was returned\r
233 in ProcessorNumber.\r
234 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
235\r
236**/\r
237EFI_STATUS\r
238EFIAPI\r
239SmmWhoAmI (\r
240 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,\r
241 OUT UINTN *ProcessorNumber\r
242 )\r
243{\r
244 UINTN Index;\r
245 UINT64 ApicId;\r
246\r
247 //\r
248 // Check parameter\r
249 //\r
250 if (ProcessorNumber == NULL) {\r
251 return EFI_INVALID_PARAMETER;\r
252 }\r
253\r
254 ApicId = GetApicId ();\r
255\r
256 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
257 if (gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId == ApicId) {\r
258 *ProcessorNumber = Index;\r
259 return EFI_SUCCESS;\r
260 }\r
261 }\r
262 //\r
263 // This should not happen\r
264 //\r
265 ASSERT (FALSE);\r
266 return EFI_NOT_FOUND;\r
267}\r
268\r
269/**\r
270 Update the SMM CPU list per the pending operation.\r
271\r
272 This function is called after return from SMI handlers.\r
273**/\r
274VOID\r
275SmmCpuUpdate (\r
276 VOID\r
277 )\r
278{\r
279 UINTN Index;\r
280\r
281 //\r
282 // Handle pending BSP switch operations\r
283 //\r
284 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
285 if (gSmmCpuPrivate->Operation[Index] == SmmCpuSwitchBsp) {\r
286 gSmmCpuPrivate->Operation[Index] = SmmCpuNone;\r
287 mSmmMpSyncData->SwitchBsp = TRUE;\r
288 mSmmMpSyncData->CandidateBsp[Index] = TRUE;\r
289 }\r
290 }\r
291\r
292 //\r
293 // Handle pending hot-add operations\r
294 //\r
295 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
296 if (gSmmCpuPrivate->Operation[Index] == SmmCpuAdd) {\r
297 gSmmCpuPrivate->Operation[Index] = SmmCpuNone;\r
298 mNumberOfCpus++;\r
299 }\r
300 }\r
301\r
302 //\r
303 // Handle pending hot-remove operations\r
304 //\r
305 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
306 if (gSmmCpuPrivate->Operation[Index] == SmmCpuRemove) {\r
307 gSmmCpuPrivate->Operation[Index] = SmmCpuNone;\r
308 mNumberOfCpus--;\r
309 }\r
310 }\r
311}\r
312\r
313/**\r
314 Register exception handler.\r
315\r
316 @param This A pointer to the SMM_CPU_SERVICE_PROTOCOL instance.\r
317 @param ExceptionType Defines which interrupt or exception to hook. Type EFI_EXCEPTION_TYPE and\r
318 the valid values for this parameter are defined in EFI_DEBUG_SUPPORT_PROTOCOL\r
319 of the UEFI 2.0 specification.\r
320 @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER\r
321 that is called when a processor interrupt occurs.\r
322 If this parameter is NULL, then the handler will be uninstalled.\r
323\r
324 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r
325 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was previously installed.\r
326 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not previously installed.\r
327 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.\r
328\r
329**/\r
330EFI_STATUS\r
331EFIAPI\r
332SmmRegisterExceptionHandler (\r
333 IN EFI_SMM_CPU_SERVICE_PROTOCOL *This,\r
334 IN EFI_EXCEPTION_TYPE ExceptionType,\r
335 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r
336 )\r
337{\r
338 return RegisterCpuInterruptHandler (ExceptionType, InterruptHandler);\r
339}\r
340\r
341/**\r
342 Initialize SMM CPU Services.\r
343\r
344 It installs EFI SMM CPU Services Protocol.\r
345\r
346 @param ImageHandle The firmware allocated handle for the EFI image.\r
347\r
348 @retval EFI_SUCCESS EFI SMM CPU Services Protocol was installed successfully.\r
349**/\r
350EFI_STATUS\r
351InitializeSmmCpuServices (\r
352 IN EFI_HANDLE Handle\r
353 )\r
354{\r
355 EFI_STATUS Status;\r
356\r
357 Status = gSmst->SmmInstallProtocolInterface (\r
358 &Handle,\r
359 &gEfiSmmCpuServiceProtocolGuid,\r
360 EFI_NATIVE_INTERFACE,\r
361 &mSmmCpuService\r
362 );\r
363 ASSERT_EFI_ERROR (Status);\r
364 return Status;\r
365}\r
366\r