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