]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/PiSmmCpuDxeSmm/CpuService.c
ShellPkg: print only valid characters for file overwrite prompt
[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
29/**\r
30 Get Package ID/Core ID/Thread ID of a processor.\r
31\r
32 APIC ID must be an initial APIC ID.\r
33\r
34 The algorithm below assumes the target system has symmetry across physical package boundaries\r
35 with respect to the number of logical processors per package, number of cores per package.\r
36\r
37 @param ApicId APIC ID of the target logical processor.\r
38 @param Location Returns the processor location information.\r
39**/\r
40VOID\r
41SmmGetProcessorLocation (\r
42 IN UINT32 ApicId,\r
43 OUT EFI_CPU_PHYSICAL_LOCATION *Location\r
44 )\r
45{\r
46 UINTN ThreadBits;\r
47 UINTN CoreBits;\r
48 UINT32 RegEax;\r
49 UINT32 RegEbx;\r
50 UINT32 RegEcx;\r
51 UINT32 RegEdx;\r
52 UINT32 MaxCpuIdIndex;\r
53 UINT32 SubIndex;\r
54 UINTN LevelType;\r
55 UINT32 MaxLogicProcessorsPerPackage;\r
56 UINT32 MaxCoresPerPackage;\r
57 BOOLEAN TopologyLeafSupported;\r
58\r
59 ASSERT (Location != NULL);\r
60\r
61 ThreadBits = 0;\r
62 CoreBits = 0;\r
63 TopologyLeafSupported = FALSE;\r
64\r
65 //\r
66 // Check if the processor is capable of supporting more than one logical processor.\r
67 //\r
68 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &RegEdx);\r
69 ASSERT ((RegEdx & BIT28) != 0);\r
70\r
71 //\r
72 // Assume three-level mapping of APIC ID: Package:Core:SMT.\r
73 //\r
74\r
75 //\r
76 // Get the max index of basic CPUID\r
77 //\r
78 AsmCpuid (CPUID_SIGNATURE, &MaxCpuIdIndex, NULL, NULL, NULL);\r
79\r
80 //\r
81 // If the extended topology enumeration leaf is available, it\r
82 // is the preferred mechanism for enumerating topology.\r
83 //\r
84 if (MaxCpuIdIndex >= CPUID_EXTENDED_TOPOLOGY) {\r
85 AsmCpuidEx (CPUID_EXTENDED_TOPOLOGY, 0, &RegEax, &RegEbx, &RegEcx, NULL);\r
86 //\r
87 // If CPUID.(EAX=0BH, ECX=0H):EBX returns zero and maximum input value for\r
88 // basic CPUID information is greater than 0BH, then CPUID.0BH leaf is not\r
89 // supported on that processor.\r
90 //\r
91 if ((RegEbx & 0xffff) != 0) {\r
92 TopologyLeafSupported = TRUE;\r
93\r
94 //\r
95 // Sub-leaf index 0 (ECX= 0 as input) provides enumeration parameters to extract\r
96 // the SMT sub-field of x2APIC ID.\r
97 //\r
98 LevelType = (RegEcx >> 8) & 0xff;\r
99 ASSERT (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT);\r
100 if ((RegEbx & 0xffff) > 1 ) {\r
101 ThreadBits = RegEax & 0x1f;\r
102 } else {\r
103 //\r
104 // HT is not supported\r
105 //\r
106 ThreadBits = 0;\r
107 }\r
108\r
109 //\r
110 // Software must not assume any "level type" encoding\r
111 // value to be related to any sub-leaf index, except sub-leaf 0.\r
112 //\r
113 SubIndex = 1;\r
114 do {\r
115 AsmCpuidEx (CPUID_EXTENDED_TOPOLOGY, SubIndex, &RegEax, NULL, &RegEcx, NULL);\r
116 LevelType = (RegEcx >> 8) & 0xff;\r
117 if (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_CORE) {\r
118 CoreBits = (RegEax & 0x1f) - ThreadBits;\r
119 break;\r
120 }\r
121 SubIndex++;\r
122 } while (LevelType != CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_INVALID);\r
123 }\r
124 }\r
125\r
126 if (!TopologyLeafSupported) {\r
127 AsmCpuid (CPUID_VERSION_INFO, NULL, &RegEbx, NULL, NULL);\r
128 MaxLogicProcessorsPerPackage = (RegEbx >> 16) & 0xff;\r
129 if (MaxCpuIdIndex >= CPUID_CACHE_PARAMS) {\r
130 AsmCpuidEx (CPUID_CACHE_PARAMS, 0, &RegEax, NULL, NULL, NULL);\r
131 MaxCoresPerPackage = (RegEax >> 26) + 1;\r
132 } else {\r
133 //\r
134 // Must be a single-core processor.\r
135 //\r
136 MaxCoresPerPackage = 1;\r
137 }\r
138\r
139 ThreadBits = (UINTN) (HighBitSet32 (MaxLogicProcessorsPerPackage / MaxCoresPerPackage - 1) + 1);\r
140 CoreBits = (UINTN) (HighBitSet32 (MaxCoresPerPackage - 1) + 1);\r
141 }\r
142\r
143 Location->Thread = ApicId & ~((-1) << ThreadBits);\r
144 Location->Core = (ApicId >> ThreadBits) & ~((-1) << CoreBits);\r
145 Location->Package = (ApicId >> (ThreadBits+ CoreBits));\r
146}\r
147\r
148/**\r
149 Gets processor information on the requested processor at the instant this call is made.\r
150\r
151 @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.\r
152 @param[in] ProcessorNumber The handle number of processor.\r
153 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for\r
154 the requested processor is deposited.\r
155\r
156 @retval EFI_SUCCESS Processor information was returned.\r
157 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.\r
158 @retval EFI_INVALID_PARAMETER ProcessorNumber is invalid.\r
159 @retval EFI_NOT_FOUND The processor with the handle specified by\r
160 ProcessorNumber does not exist in the platform.\r
161\r
162**/\r
163EFI_STATUS\r
164EFIAPI\r
165SmmGetProcessorInfo (\r
166 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,\r
167 IN UINTN ProcessorNumber,\r
168 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer\r
169 )\r
170{\r
171 //\r
172 // Check parameter\r
173 //\r
174 if (ProcessorNumber >= mMaxNumberOfCpus || ProcessorInfoBuffer == NULL) {\r
175 return EFI_INVALID_PARAMETER;\r
176 }\r
177\r
178 if (gSmmCpuPrivate->ProcessorInfo[ProcessorNumber].ProcessorId == INVALID_APIC_ID) {\r
179 return EFI_NOT_FOUND;\r
180 }\r
181\r
182 //\r
183 // Fill in processor information\r
184 //\r
185 CopyMem (ProcessorInfoBuffer, &gSmmCpuPrivate->ProcessorInfo[ProcessorNumber], sizeof (EFI_PROCESSOR_INFORMATION));\r
186 return EFI_SUCCESS;\r
187}\r
188\r
189/**\r
190 This service switches the requested AP to be the BSP since the next SMI.\r
191\r
192 @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.\r
193 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.\r
194\r
195 @retval EFI_SUCCESS BSP will be switched in next SMI.\r
196 @retval EFI_UNSUPPORTED Switching the BSP or a processor to be hot-removed is not supported.\r
197 @retval EFI_NOT_FOUND The processor with the handle specified by ProcessorNumber does not exist.\r
198 @retval EFI_INVALID_PARAMETER ProcessorNumber is invalid.\r
199**/\r
200EFI_STATUS\r
201EFIAPI\r
202SmmSwitchBsp (\r
203 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,\r
204 IN UINTN ProcessorNumber\r
205 )\r
206{\r
207 //\r
208 // Check parameter\r
209 //\r
210 if (ProcessorNumber >= mMaxNumberOfCpus) {\r
211 return EFI_INVALID_PARAMETER;\r
212 }\r
213\r
214 if (gSmmCpuPrivate->ProcessorInfo[ProcessorNumber].ProcessorId == INVALID_APIC_ID) {\r
215 return EFI_NOT_FOUND;\r
216 }\r
217\r
218 if (gSmmCpuPrivate->Operation[ProcessorNumber] != SmmCpuNone ||\r
219 gSmst->CurrentlyExecutingCpu == ProcessorNumber) {\r
220 return EFI_UNSUPPORTED;\r
221 }\r
222\r
223 //\r
224 // Setting of the BSP for next SMI is pending until all SMI handlers are finished\r
225 //\r
226 gSmmCpuPrivate->Operation[ProcessorNumber] = SmmCpuSwitchBsp;\r
227 return EFI_SUCCESS;\r
228}\r
229\r
230/**\r
231 Notify that a processor was hot-added.\r
232\r
233 @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.\r
234 @param[in] ProcessorId Local APIC ID of the hot-added processor.\r
235 @param[out] ProcessorNumber The handle number of the hot-added processor.\r
236\r
237 @retval EFI_SUCCESS The hot-addition of the specified processors was successfully notified.\r
238 @retval EFI_UNSUPPORTED Hot addition of processor is not supported.\r
239 @retval EFI_NOT_FOUND The processor with the handle specified by ProcessorNumber does not exist.\r
240 @retval EFI_INVALID_PARAMETER ProcessorNumber is invalid.\r
241 @retval EFI_ALREADY_STARTED The processor is already online in the system.\r
242**/\r
243EFI_STATUS\r
244EFIAPI\r
245SmmAddProcessor (\r
246 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,\r
247 IN UINT64 ProcessorId,\r
248 OUT UINTN *ProcessorNumber\r
249 )\r
250{\r
251 UINTN Index;\r
252\r
253 if (!FeaturePcdGet (PcdCpuHotPlugSupport)) {\r
254 return EFI_UNSUPPORTED;\r
255 }\r
256\r
257 //\r
258 // Check parameter\r
259 //\r
260 if (ProcessorNumber == NULL || ProcessorId == INVALID_APIC_ID) {\r
261 return EFI_INVALID_PARAMETER;\r
262 }\r
263\r
264 //\r
265 // Check if the processor already exists\r
266 //\r
267\r
268 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
269 if (gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId == ProcessorId) {\r
270 return EFI_ALREADY_STARTED;\r
271 }\r
272 }\r
273\r
274 //\r
275 // Check CPU hot plug data. The CPU RAS handler should have created the mapping\r
276 // of the APIC ID to SMBASE.\r
277 //\r
278 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
279 if (mCpuHotPlugData.ApicId[Index] == ProcessorId &&\r
280 gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId == INVALID_APIC_ID) {\r
281 gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId = ProcessorId;\r
282 gSmmCpuPrivate->ProcessorInfo[Index].StatusFlag = 0;\r
283 SmmGetProcessorLocation ((UINT32)ProcessorId, &gSmmCpuPrivate->ProcessorInfo[Index].Location);\r
284\r
285 *ProcessorNumber = Index;\r
286 gSmmCpuPrivate->Operation[Index] = SmmCpuAdd;\r
287 return EFI_SUCCESS;\r
288 }\r
289 }\r
290\r
291 return EFI_INVALID_PARAMETER;\r
292}\r
293\r
294/**\r
295 Notify that a processor was hot-removed.\r
296\r
297 @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.\r
298 @param[in] ProcessorNumber The handle number of the hot-added processor.\r
299\r
300 @retval EFI_SUCCESS The hot-removal of the specified processors was successfully notified.\r
301 @retval EFI_UNSUPPORTED Hot removal of processor is not supported.\r
302 @retval EFI_UNSUPPORTED Hot removal of BSP is not supported.\r
303 @retval EFI_UNSUPPORTED Hot removal of a processor with pending hot-plug operation is not supported.\r
304 @retval EFI_INVALID_PARAMETER ProcessorNumber is invalid.\r
305**/\r
306EFI_STATUS\r
307EFIAPI\r
308SmmRemoveProcessor (\r
309 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,\r
310 IN UINTN ProcessorNumber\r
311 )\r
312{\r
313 if (!FeaturePcdGet (PcdCpuHotPlugSupport)) {\r
314 return EFI_UNSUPPORTED;\r
315 }\r
316\r
317 //\r
318 // Check parameter\r
319 //\r
320 if (ProcessorNumber >= mMaxNumberOfCpus ||\r
321 gSmmCpuPrivate->ProcessorInfo[ProcessorNumber].ProcessorId == INVALID_APIC_ID) {\r
322 return EFI_INVALID_PARAMETER;\r
323 }\r
324\r
325 //\r
326 // Can't remove BSP\r
327 //\r
328 if (ProcessorNumber == gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu) {\r
329 return EFI_UNSUPPORTED;\r
330 }\r
331\r
332 if (gSmmCpuPrivate->Operation[ProcessorNumber] != SmmCpuNone) {\r
333 return EFI_UNSUPPORTED;\r
334 }\r
335\r
336 gSmmCpuPrivate->ProcessorInfo[ProcessorNumber].ProcessorId = INVALID_APIC_ID;\r
337 mCpuHotPlugData.ApicId[ProcessorNumber] = INVALID_APIC_ID;\r
338\r
339 //\r
340 // Removal of the processor from the CPU list is pending until all SMI handlers are finished\r
341 //\r
342 gSmmCpuPrivate->Operation[ProcessorNumber] = SmmCpuRemove;\r
343 return EFI_SUCCESS;\r
344}\r
345\r
346/**\r
347 This return the handle number for the calling processor.\r
348\r
349 @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.\r
350 @param[out] ProcessorNumber The handle number of currently executing processor.\r
351\r
352 @retval EFI_SUCCESS The current processor handle number was returned\r
353 in ProcessorNumber.\r
354 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
355\r
356**/\r
357EFI_STATUS\r
358EFIAPI\r
359SmmWhoAmI (\r
360 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,\r
361 OUT UINTN *ProcessorNumber\r
362 )\r
363{\r
364 UINTN Index;\r
365 UINT64 ApicId;\r
366\r
367 //\r
368 // Check parameter\r
369 //\r
370 if (ProcessorNumber == NULL) {\r
371 return EFI_INVALID_PARAMETER;\r
372 }\r
373\r
374 ApicId = GetApicId ();\r
375\r
376 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
377 if (gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId == ApicId) {\r
378 *ProcessorNumber = Index;\r
379 return EFI_SUCCESS;\r
380 }\r
381 }\r
382 //\r
383 // This should not happen\r
384 //\r
385 ASSERT (FALSE);\r
386 return EFI_NOT_FOUND;\r
387}\r
388\r
389/**\r
390 Update the SMM CPU list per the pending operation.\r
391\r
392 This function is called after return from SMI handlers.\r
393**/\r
394VOID\r
395SmmCpuUpdate (\r
396 VOID\r
397 )\r
398{\r
399 UINTN Index;\r
400\r
401 //\r
402 // Handle pending BSP switch operations\r
403 //\r
404 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
405 if (gSmmCpuPrivate->Operation[Index] == SmmCpuSwitchBsp) {\r
406 gSmmCpuPrivate->Operation[Index] = SmmCpuNone;\r
407 mSmmMpSyncData->SwitchBsp = TRUE;\r
408 mSmmMpSyncData->CandidateBsp[Index] = TRUE;\r
409 }\r
410 }\r
411\r
412 //\r
413 // Handle pending hot-add operations\r
414 //\r
415 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
416 if (gSmmCpuPrivate->Operation[Index] == SmmCpuAdd) {\r
417 gSmmCpuPrivate->Operation[Index] = SmmCpuNone;\r
418 mNumberOfCpus++;\r
419 }\r
420 }\r
421\r
422 //\r
423 // Handle pending hot-remove operations\r
424 //\r
425 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
426 if (gSmmCpuPrivate->Operation[Index] == SmmCpuRemove) {\r
427 gSmmCpuPrivate->Operation[Index] = SmmCpuNone;\r
428 mNumberOfCpus--;\r
429 }\r
430 }\r
431}\r
432\r
433/**\r
434 Register exception handler.\r
435\r
436 @param This A pointer to the SMM_CPU_SERVICE_PROTOCOL instance.\r
437 @param ExceptionType Defines which interrupt or exception to hook. Type EFI_EXCEPTION_TYPE and\r
438 the valid values for this parameter are defined in EFI_DEBUG_SUPPORT_PROTOCOL\r
439 of the UEFI 2.0 specification.\r
440 @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER\r
441 that is called when a processor interrupt occurs.\r
442 If this parameter is NULL, then the handler will be uninstalled.\r
443\r
444 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r
445 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was previously installed.\r
446 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not previously installed.\r
447 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.\r
448\r
449**/\r
450EFI_STATUS\r
451EFIAPI\r
452SmmRegisterExceptionHandler (\r
453 IN EFI_SMM_CPU_SERVICE_PROTOCOL *This,\r
454 IN EFI_EXCEPTION_TYPE ExceptionType,\r
455 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r
456 )\r
457{\r
458 return RegisterCpuInterruptHandler (ExceptionType, InterruptHandler);\r
459}\r
460\r
461/**\r
462 Initialize SMM CPU Services.\r
463\r
464 It installs EFI SMM CPU Services Protocol.\r
465\r
466 @param ImageHandle The firmware allocated handle for the EFI image.\r
467\r
468 @retval EFI_SUCCESS EFI SMM CPU Services Protocol was installed successfully.\r
469**/\r
470EFI_STATUS\r
471InitializeSmmCpuServices (\r
472 IN EFI_HANDLE Handle\r
473 )\r
474{\r
475 EFI_STATUS Status;\r
476\r
477 Status = gSmst->SmmInstallProtocolInterface (\r
478 &Handle,\r
479 &gEfiSmmCpuServiceProtocolGuid,\r
480 EFI_NATIVE_INTERFACE,\r
481 &mSmmCpuService\r
482 );\r
483 ASSERT_EFI_ERROR (Status);\r
484 return Status;\r
485}\r
486\r