]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/BaseXApicX2ApicLib/BaseXApicX2ApicLib.c
UefiCpuPkg: Clean up source files
[mirror_edk2.git] / UefiCpuPkg / Library / BaseXApicX2ApicLib / BaseXApicX2ApicLib.c
CommitLineData
bf73cc4b 1/** @file\r
2 Local APIC Library.\r
3\r
4 This local APIC library instance supports x2APIC capable processors\r
5 which have xAPIC and x2APIC modes.\r
6\r
7367cc6c 7 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r
061ead7a
LD
8 Copyright (c) 2017, AMD Inc. All rights reserved.<BR>\r
9\r
bf73cc4b 10 This program and the accompanying materials\r
11 are licensed and made available under the terms and conditions of the BSD License\r
12 which accompanies this distribution. The full text of the license may be found at\r
13 http://opensource.org/licenses/bsd-license.php\r
14\r
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
17\r
18**/\r
19\r
28a7ddf0 20#include <Register/Cpuid.h>\r
061ead7a 21#include <Register/Amd/Cpuid.h>\r
a742e186 22#include <Register/Msr.h>\r
bf73cc4b 23#include <Register/LocalApic.h>\r
24\r
25#include <Library/BaseLib.h>\r
26#include <Library/DebugLib.h>\r
27#include <Library/LocalApicLib.h>\r
28#include <Library/IoLib.h>\r
29#include <Library/TimerLib.h>\r
e9cd66d0 30#include <Library/PcdLib.h>\r
bf73cc4b 31\r
32//\r
33// Library internal functions\r
34//\r
35\r
061ead7a
LD
36/**\r
37 Determine if the standard CPU signature is "AuthenticAMD".\r
38\r
39 @retval TRUE The CPU signature matches.\r
40 @retval FALSE The CPU signature does not match.\r
41\r
42**/\r
43BOOLEAN\r
44StandardSignatureIsAuthenticAMD (\r
45 VOID\r
46 )\r
47{\r
48 UINT32 RegEbx;\r
49 UINT32 RegEcx;\r
50 UINT32 RegEdx;\r
51\r
ae66c6f1 52 AsmCpuid (CPUID_SIGNATURE, NULL, &RegEbx, &RegEcx, &RegEdx);\r
061ead7a
LD
53 return (RegEbx == CPUID_SIGNATURE_AUTHENTIC_AMD_EBX &&\r
54 RegEcx == CPUID_SIGNATURE_AUTHENTIC_AMD_ECX &&\r
55 RegEdx == CPUID_SIGNATURE_AUTHENTIC_AMD_EDX);\r
56}\r
57\r
e9cd66d0
MK
58/**\r
59 Determine if the CPU supports the Local APIC Base Address MSR.\r
60\r
61 @retval TRUE The CPU supports the Local APIC Base Address MSR.\r
62 @retval FALSE The CPU does not support the Local APIC Base Address MSR.\r
63\r
64**/\r
65BOOLEAN\r
66LocalApicBaseAddressMsrSupported (\r
67 VOID\r
68 )\r
69{\r
70 UINT32 RegEax;\r
71 UINTN FamilyId;\r
7367cc6c 72\r
e9cd66d0
MK
73 AsmCpuid (1, &RegEax, NULL, NULL, NULL);\r
74 FamilyId = BitFieldRead32 (RegEax, 8, 11);\r
75 if (FamilyId == 0x04 || FamilyId == 0x05) {\r
76 //\r
7367cc6c 77 // CPUs with a FamilyId of 0x04 or 0x05 do not support the\r
e9cd66d0
MK
78 // Local APIC Base Address MSR\r
79 //\r
80 return FALSE;\r
81 }\r
82 return TRUE;\r
83}\r
84\r
a66e0c7d 85/**\r
86 Retrieve the base address of local APIC.\r
87\r
88 @return The base address of local APIC.\r
89\r
90**/\r
91UINTN\r
92EFIAPI\r
93GetLocalApicBaseAddress (\r
94 VOID\r
95 )\r
96{\r
a742e186 97 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;\r
e9cd66d0
MK
98\r
99 if (!LocalApicBaseAddressMsrSupported ()) {\r
100 //\r
101 // If CPU does not support Local APIC Base Address MSR, then retrieve\r
102 // Local APIC Base Address from PCD\r
103 //\r
104 return PcdGet32 (PcdCpuLocalApicBaseAddress);\r
105 }\r
106\r
a742e186 107 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
7367cc6c 108\r
a742e186
JF
109 return (UINTN)(LShiftU64 ((UINT64) ApicBaseMsr.Bits.ApicBaseHi, 32)) +\r
110 (((UINTN)ApicBaseMsr.Bits.ApicBase) << 12);\r
a66e0c7d 111}\r
112\r
113/**\r
114 Set the base address of local APIC.\r
115\r
116 If BaseAddress is not aligned on a 4KB boundary, then ASSERT().\r
117\r
118 @param[in] BaseAddress Local APIC base address to be set.\r
119\r
120**/\r
121VOID\r
122EFIAPI\r
123SetLocalApicBaseAddress (\r
124 IN UINTN BaseAddress\r
125 )\r
126{\r
a742e186 127 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;\r
a66e0c7d 128\r
129 ASSERT ((BaseAddress & (SIZE_4KB - 1)) == 0);\r
130\r
e9cd66d0
MK
131 if (!LocalApicBaseAddressMsrSupported ()) {\r
132 //\r
133 // Ignore set request of the CPU does not support APIC Base Address MSR\r
134 //\r
135 return;\r
136 }\r
137\r
a742e186 138 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
a66e0c7d 139\r
a742e186
JF
140 ApicBaseMsr.Bits.ApicBase = (UINT32) (BaseAddress >> 12);\r
141 ApicBaseMsr.Bits.ApicBaseHi = (UINT32) (RShiftU64((UINT64) BaseAddress, 32));\r
a66e0c7d 142\r
a742e186 143 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
a66e0c7d 144}\r
145\r
bf73cc4b 146/**\r
147 Read from a local APIC register.\r
148\r
149 This function reads from a local APIC register either in xAPIC or x2APIC mode.\r
150 It is required that in xAPIC mode wider registers (64-bit or 256-bit) must be\r
151 accessed using multiple 32-bit loads or stores, so this function only performs\r
152 32-bit read.\r
153\r
154 @param MmioOffset The MMIO offset of the local APIC register in xAPIC mode.\r
155 It must be 16-byte aligned.\r
156\r
157 @return 32-bit Value read from the register.\r
158**/\r
159UINT32\r
160EFIAPI\r
161ReadLocalApicReg (\r
162 IN UINTN MmioOffset\r
163 )\r
164{\r
165 UINT32 MsrIndex;\r
166\r
167 ASSERT ((MmioOffset & 0xf) == 0);\r
168\r
169 if (GetApicMode () == LOCAL_APIC_MODE_XAPIC) {\r
a66e0c7d 170 return MmioRead32 (GetLocalApicBaseAddress() + MmioOffset);\r
bf73cc4b 171 } else {\r
172 //\r
173 // DFR is not supported in x2APIC mode.\r
174 //\r
175 ASSERT (MmioOffset != XAPIC_ICR_DFR_OFFSET);\r
176 //\r
177 // Note that in x2APIC mode, ICR is a 64-bit MSR that needs special treatment. It\r
178 // is not supported in this function for simplicity.\r
179 //\r
180 ASSERT (MmioOffset != XAPIC_ICR_HIGH_OFFSET);\r
181\r
182 MsrIndex = (UINT32)(MmioOffset >> 4) + X2APIC_MSR_BASE_ADDRESS;\r
183 return AsmReadMsr32 (MsrIndex);\r
184 }\r
185}\r
186\r
187/**\r
188 Write to a local APIC register.\r
189\r
190 This function writes to a local APIC register either in xAPIC or x2APIC mode.\r
191 It is required that in xAPIC mode wider registers (64-bit or 256-bit) must be\r
192 accessed using multiple 32-bit loads or stores, so this function only performs\r
193 32-bit write.\r
194\r
195 if the register index is invalid or unsupported in current APIC mode, then ASSERT.\r
196\r
197 @param MmioOffset The MMIO offset of the local APIC register in xAPIC mode.\r
198 It must be 16-byte aligned.\r
199 @param Value Value to be written to the register.\r
200**/\r
201VOID\r
202EFIAPI\r
203WriteLocalApicReg (\r
204 IN UINTN MmioOffset,\r
205 IN UINT32 Value\r
206 )\r
207{\r
208 UINT32 MsrIndex;\r
209\r
210 ASSERT ((MmioOffset & 0xf) == 0);\r
211\r
212 if (GetApicMode () == LOCAL_APIC_MODE_XAPIC) {\r
a66e0c7d 213 MmioWrite32 (GetLocalApicBaseAddress() + MmioOffset, Value);\r
bf73cc4b 214 } else {\r
215 //\r
216 // DFR is not supported in x2APIC mode.\r
217 //\r
218 ASSERT (MmioOffset != XAPIC_ICR_DFR_OFFSET);\r
219 //\r
220 // Note that in x2APIC mode, ICR is a 64-bit MSR that needs special treatment. It\r
221 // is not supported in this function for simplicity.\r
222 //\r
223 ASSERT (MmioOffset != XAPIC_ICR_HIGH_OFFSET);\r
224 ASSERT (MmioOffset != XAPIC_ICR_LOW_OFFSET);\r
225\r
226 MsrIndex = (UINT32)(MmioOffset >> 4) + X2APIC_MSR_BASE_ADDRESS;\r
227 //\r
228 // The serializing semantics of WRMSR are relaxed when writing to the APIC registers.\r
229 // Use memory fence here to force the serializing semantics to be consisent with xAPIC mode.\r
230 //\r
231 MemoryFence ();\r
232 AsmWriteMsr32 (MsrIndex, Value);\r
233 }\r
234}\r
235\r
236/**\r
237 Send an IPI by writing to ICR.\r
238\r
7367cc6c 239 This function returns after the IPI has been accepted by the target processor.\r
bf73cc4b 240\r
241 @param IcrLow 32-bit value to be written to the low half of ICR.\r
242 @param ApicId APIC ID of the target processor if this IPI is targeted for a specific processor.\r
243**/\r
244VOID\r
245SendIpi (\r
246 IN UINT32 IcrLow,\r
247 IN UINT32 ApicId\r
248 )\r
249{\r
250 UINT64 MsrValue;\r
251 LOCAL_APIC_ICR_LOW IcrLowReg;\r
a66e0c7d 252 UINTN LocalApciBaseAddress;\r
9c71e1e0
JF
253 UINT32 IcrHigh;\r
254 BOOLEAN InterruptState;\r
bf73cc4b 255\r
9c71e1e0
JF
256 //\r
257 // Legacy APIC or X2APIC?\r
258 //\r
bf73cc4b 259 if (GetApicMode () == LOCAL_APIC_MODE_XAPIC) {\r
260 ASSERT (ApicId <= 0xff);\r
261\r
9c71e1e0
JF
262 InterruptState = SaveAndDisableInterrupts ();\r
263\r
bf73cc4b 264 //\r
9c71e1e0 265 // Get base address of this LAPIC\r
bf73cc4b 266 //\r
a66e0c7d 267 LocalApciBaseAddress = GetLocalApicBaseAddress();\r
9c71e1e0
JF
268\r
269 //\r
270 // Save existing contents of ICR high 32 bits\r
271 //\r
272 IcrHigh = MmioRead32 (LocalApciBaseAddress + XAPIC_ICR_HIGH_OFFSET);\r
273\r
274 //\r
275 // Wait for DeliveryStatus clear in case a previous IPI\r
276 // is still being sent\r
277 //\r
278 do {\r
279 IcrLowReg.Uint32 = MmioRead32 (LocalApciBaseAddress + XAPIC_ICR_LOW_OFFSET);\r
280 } while (IcrLowReg.Bits.DeliveryStatus != 0);\r
281\r
282 //\r
283 // For xAPIC, the act of writing to the low doubleword of the ICR causes the IPI to be sent.\r
284 //\r
a66e0c7d 285 MmioWrite32 (LocalApciBaseAddress + XAPIC_ICR_HIGH_OFFSET, ApicId << 24);\r
286 MmioWrite32 (LocalApciBaseAddress + XAPIC_ICR_LOW_OFFSET, IcrLow);\r
9c71e1e0
JF
287\r
288 //\r
289 // Wait for DeliveryStatus clear again\r
290 //\r
bf73cc4b 291 do {\r
a66e0c7d 292 IcrLowReg.Uint32 = MmioRead32 (LocalApciBaseAddress + XAPIC_ICR_LOW_OFFSET);\r
bf73cc4b 293 } while (IcrLowReg.Bits.DeliveryStatus != 0);\r
9c71e1e0
JF
294\r
295 //\r
296 // And restore old contents of ICR high\r
297 //\r
298 MmioWrite32 (LocalApciBaseAddress + XAPIC_ICR_HIGH_OFFSET, IcrHigh);\r
299\r
300 SetInterruptState (InterruptState);\r
301\r
bf73cc4b 302 } else {\r
303 //\r
7367cc6c 304 // For x2APIC, A single MSR write to the Interrupt Command Register is required for dispatching an\r
bf73cc4b 305 // interrupt in x2APIC mode.\r
306 //\r
23394428 307 MsrValue = LShiftU64 ((UINT64) ApicId, 32) | IcrLow;\r
bf73cc4b 308 AsmWriteMsr64 (X2APIC_MSR_ICR_ADDRESS, MsrValue);\r
309 }\r
310}\r
311\r
312//\r
313// Library API implementation functions\r
314//\r
315\r
316/**\r
317 Get the current local APIC mode.\r
318\r
319 If local APIC is disabled, then ASSERT.\r
320\r
321 @retval LOCAL_APIC_MODE_XAPIC current APIC mode is xAPIC.\r
322 @retval LOCAL_APIC_MODE_X2APIC current APIC mode is x2APIC.\r
323**/\r
324UINTN\r
325EFIAPI\r
326GetApicMode (\r
327 VOID\r
328 )\r
329{\r
a742e186 330 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;\r
e9cd66d0
MK
331\r
332 if (!LocalApicBaseAddressMsrSupported ()) {\r
333 //\r
334 // If CPU does not support APIC Base Address MSR, then return XAPIC mode\r
335 //\r
336 return LOCAL_APIC_MODE_XAPIC;\r
337 }\r
bf73cc4b 338\r
a742e186 339 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
bf73cc4b 340 //\r
341 // Local APIC should have been enabled\r
342 //\r
a742e186
JF
343 ASSERT (ApicBaseMsr.Bits.EN != 0);\r
344 if (ApicBaseMsr.Bits.EXTD != 0) {\r
bf73cc4b 345 return LOCAL_APIC_MODE_X2APIC;\r
346 } else {\r
347 return LOCAL_APIC_MODE_XAPIC;\r
348 }\r
349}\r
350\r
351/**\r
352 Set the current local APIC mode.\r
353\r
354 If the specified local APIC mode is not valid, then ASSERT.\r
355 If the specified local APIC mode can't be set as current, then ASSERT.\r
356\r
357 @param ApicMode APIC mode to be set.\r
9c71e1e0
JF
358\r
359 @note This API must not be called from an interrupt handler or SMI handler.\r
360 It may result in unpredictable behavior.\r
bf73cc4b 361**/\r
362VOID\r
363EFIAPI\r
364SetApicMode (\r
365 IN UINTN ApicMode\r
366 )\r
367{\r
a742e186
JF
368 UINTN CurrentMode;\r
369 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;\r
e9cd66d0
MK
370\r
371 if (!LocalApicBaseAddressMsrSupported ()) {\r
372 //\r
373 // Ignore set request if the CPU does not support APIC Base Address MSR\r
374 //\r
375 return;\r
376 }\r
bf73cc4b 377\r
378 CurrentMode = GetApicMode ();\r
379 if (CurrentMode == LOCAL_APIC_MODE_XAPIC) {\r
380 switch (ApicMode) {\r
381 case LOCAL_APIC_MODE_XAPIC:\r
382 break;\r
383 case LOCAL_APIC_MODE_X2APIC:\r
a742e186
JF
384 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
385 ApicBaseMsr.Bits.EXTD = 1;\r
386 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
bf73cc4b 387 break;\r
388 default:\r
389 ASSERT (FALSE);\r
390 }\r
391 } else {\r
392 switch (ApicMode) {\r
393 case LOCAL_APIC_MODE_XAPIC:\r
394 //\r
395 // Transition from x2APIC mode to xAPIC mode is a two-step process:\r
396 // x2APIC -> Local APIC disabled -> xAPIC\r
397 //\r
a742e186
JF
398 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
399 ApicBaseMsr.Bits.EXTD = 0;\r
400 ApicBaseMsr.Bits.EN = 0;\r
401 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
402 ApicBaseMsr.Bits.EN = 1;\r
403 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
bf73cc4b 404 break;\r
405 case LOCAL_APIC_MODE_X2APIC:\r
406 break;\r
407 default:\r
408 ASSERT (FALSE);\r
409 }\r
410 }\r
411}\r
412\r
413/**\r
414 Get the initial local APIC ID of the executing processor assigned by hardware upon power on or reset.\r
415\r
6e3e4d70 416 In xAPIC mode, the initial local APIC ID may be different from current APIC ID.\r
7367cc6c 417 In x2APIC mode, the local APIC ID can't be changed and there is no concept of initial APIC ID. In this case,\r
bf73cc4b 418 the 32-bit local APIC ID is returned as initial APIC ID.\r
419\r
420 @return 32-bit initial local APIC ID of the executing processor.\r
421**/\r
422UINT32\r
423EFIAPI\r
424GetInitialApicId (\r
425 VOID\r
426 )\r
427{\r
6e3e4d70
JF
428 UINT32 ApicId;\r
429 UINT32 MaxCpuIdIndex;\r
bf73cc4b 430 UINT32 RegEbx;\r
431\r
432 if (GetApicMode () == LOCAL_APIC_MODE_XAPIC) {\r
6e3e4d70
JF
433 //\r
434 // Get the max index of basic CPUID\r
435 //\r
436 AsmCpuid (CPUID_SIGNATURE, &MaxCpuIdIndex, NULL, NULL, NULL);\r
437 //\r
7367cc6c 438 // If CPUID Leaf B is supported,\r
4af3ae14 439 // And CPUID.0BH:EBX[15:0] reports a non-zero value,\r
6e3e4d70
JF
440 // Then the initial 32-bit APIC ID = CPUID.0BH:EDX\r
441 // Else the initial 8-bit APIC ID = CPUID.1:EBX[31:24]\r
442 //\r
443 if (MaxCpuIdIndex >= CPUID_EXTENDED_TOPOLOGY) {\r
4af3ae14
LE
444 AsmCpuidEx (CPUID_EXTENDED_TOPOLOGY, 0, NULL, &RegEbx, NULL, &ApicId);\r
445 if ((RegEbx & (BIT16 - 1)) != 0) {\r
446 return ApicId;\r
447 }\r
6e3e4d70 448 }\r
bf73cc4b 449 AsmCpuid (CPUID_VERSION_INFO, NULL, &RegEbx, NULL, NULL);\r
450 return RegEbx >> 24;\r
451 } else {\r
452 return GetApicId ();\r
453 }\r
454}\r
455\r
456/**\r
457 Get the local APIC ID of the executing processor.\r
458\r
459 @return 32-bit local APIC ID of the executing processor.\r
460**/\r
461UINT32\r
462EFIAPI\r
463GetApicId (\r
464 VOID\r
465 )\r
466{\r
467 UINT32 ApicId;\r
6e3e4d70 468 UINT32 InitApicId;\r
bf73cc4b 469\r
470 ApicId = ReadLocalApicReg (XAPIC_ID_OFFSET);\r
471 if (GetApicMode () == LOCAL_APIC_MODE_XAPIC) {\r
6e3e4d70 472 ApicId = ((InitApicId = GetInitialApicId ()) < 0x100) ? (ApicId >> 24) : InitApicId;\r
bf73cc4b 473 }\r
6e3e4d70 474\r
bf73cc4b 475 return ApicId;\r
476}\r
477\r
ae40aef1 478/**\r
479 Get the value of the local APIC version register.\r
480\r
481 @return the value of the local APIC version register.\r
482**/\r
483UINT32\r
484EFIAPI\r
485GetApicVersion (\r
486 VOID\r
487 )\r
488{\r
489 return ReadLocalApicReg (XAPIC_VERSION_OFFSET);\r
490}\r
491\r
492/**\r
493 Send a Fixed IPI to a specified target processor.\r
494\r
7367cc6c 495 This function returns after the IPI has been accepted by the target processor.\r
ae40aef1 496\r
497 @param ApicId The local APIC ID of the target processor.\r
498 @param Vector The vector number of the interrupt being sent.\r
499**/\r
500VOID\r
501EFIAPI\r
502SendFixedIpi (\r
503 IN UINT32 ApicId,\r
504 IN UINT8 Vector\r
505 )\r
506{\r
507 LOCAL_APIC_ICR_LOW IcrLow;\r
508\r
509 IcrLow.Uint32 = 0;\r
510 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_FIXED;\r
511 IcrLow.Bits.Level = 1;\r
512 IcrLow.Bits.Vector = Vector;\r
513 SendIpi (IcrLow.Uint32, ApicId);\r
514}\r
515\r
516/**\r
517 Send a Fixed IPI to all processors excluding self.\r
518\r
7367cc6c 519 This function returns after the IPI has been accepted by the target processors.\r
ae40aef1 520\r
521 @param Vector The vector number of the interrupt being sent.\r
522**/\r
523VOID\r
524EFIAPI\r
525SendFixedIpiAllExcludingSelf (\r
526 IN UINT8 Vector\r
527 )\r
528{\r
529 LOCAL_APIC_ICR_LOW IcrLow;\r
530\r
531 IcrLow.Uint32 = 0;\r
532 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_FIXED;\r
533 IcrLow.Bits.Level = 1;\r
534 IcrLow.Bits.DestinationShorthand = LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF;\r
535 IcrLow.Bits.Vector = Vector;\r
536 SendIpi (IcrLow.Uint32, 0);\r
537}\r
538\r
bf73cc4b 539/**\r
540 Send a SMI IPI to a specified target processor.\r
541\r
7367cc6c 542 This function returns after the IPI has been accepted by the target processor.\r
bf73cc4b 543\r
544 @param ApicId Specify the local APIC ID of the target processor.\r
545**/\r
546VOID\r
547EFIAPI\r
548SendSmiIpi (\r
549 IN UINT32 ApicId\r
550 )\r
551{\r
552 LOCAL_APIC_ICR_LOW IcrLow;\r
553\r
554 IcrLow.Uint32 = 0;\r
555 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_SMI;\r
556 IcrLow.Bits.Level = 1;\r
557 SendIpi (IcrLow.Uint32, ApicId);\r
558}\r
559\r
560/**\r
561 Send a SMI IPI to all processors excluding self.\r
562\r
7367cc6c 563 This function returns after the IPI has been accepted by the target processors.\r
bf73cc4b 564**/\r
565VOID\r
566EFIAPI\r
567SendSmiIpiAllExcludingSelf (\r
568 VOID\r
569 )\r
570{\r
571 LOCAL_APIC_ICR_LOW IcrLow;\r
572\r
573 IcrLow.Uint32 = 0;\r
574 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_SMI;\r
575 IcrLow.Bits.Level = 1;\r
576 IcrLow.Bits.DestinationShorthand = LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF;\r
577 SendIpi (IcrLow.Uint32, 0);\r
578}\r
579\r
580/**\r
581 Send an INIT IPI to a specified target processor.\r
582\r
7367cc6c 583 This function returns after the IPI has been accepted by the target processor.\r
bf73cc4b 584\r
585 @param ApicId Specify the local APIC ID of the target processor.\r
586**/\r
587VOID\r
588EFIAPI\r
589SendInitIpi (\r
590 IN UINT32 ApicId\r
591 )\r
592{\r
593 LOCAL_APIC_ICR_LOW IcrLow;\r
594\r
595 IcrLow.Uint32 = 0;\r
596 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_INIT;\r
597 IcrLow.Bits.Level = 1;\r
598 SendIpi (IcrLow.Uint32, ApicId);\r
599}\r
600\r
601/**\r
602 Send an INIT IPI to all processors excluding self.\r
603\r
7367cc6c 604 This function returns after the IPI has been accepted by the target processors.\r
bf73cc4b 605**/\r
606VOID\r
607EFIAPI\r
608SendInitIpiAllExcludingSelf (\r
609 VOID\r
610 )\r
611{\r
612 LOCAL_APIC_ICR_LOW IcrLow;\r
613\r
614 IcrLow.Uint32 = 0;\r
615 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_INIT;\r
616 IcrLow.Bits.Level = 1;\r
617 IcrLow.Bits.DestinationShorthand = LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF;\r
618 SendIpi (IcrLow.Uint32, 0);\r
619}\r
620\r
621/**\r
622 Send an INIT-Start-up-Start-up IPI sequence to a specified target processor.\r
623\r
7367cc6c 624 This function returns after the IPI has been accepted by the target processor.\r
bf73cc4b 625\r
626 if StartupRoutine >= 1M, then ASSERT.\r
627 if StartupRoutine is not multiple of 4K, then ASSERT.\r
628\r
629 @param ApicId Specify the local APIC ID of the target processor.\r
630 @param StartupRoutine Points to a start-up routine which is below 1M physical\r
631 address and 4K aligned.\r
632**/\r
633VOID\r
634EFIAPI\r
635SendInitSipiSipi (\r
636 IN UINT32 ApicId,\r
637 IN UINT32 StartupRoutine\r
638 )\r
639{\r
640 LOCAL_APIC_ICR_LOW IcrLow;\r
641\r
642 ASSERT (StartupRoutine < 0x100000);\r
643 ASSERT ((StartupRoutine & 0xfff) == 0);\r
644\r
645 SendInitIpi (ApicId);\r
cf1eb6e6 646 MicroSecondDelay (PcdGet32(PcdCpuInitIpiDelayInMicroSeconds));\r
bf73cc4b 647 IcrLow.Uint32 = 0;\r
648 IcrLow.Bits.Vector = (StartupRoutine >> 12);\r
649 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_STARTUP;\r
650 IcrLow.Bits.Level = 1;\r
651 SendIpi (IcrLow.Uint32, ApicId);\r
bf252e29
ED
652 if (!StandardSignatureIsAuthenticAMD ()) {\r
653 MicroSecondDelay (200);\r
654 SendIpi (IcrLow.Uint32, ApicId);\r
655 }\r
bf73cc4b 656}\r
657\r
658/**\r
659 Send an INIT-Start-up-Start-up IPI sequence to all processors excluding self.\r
660\r
7367cc6c 661 This function returns after the IPI has been accepted by the target processors.\r
bf73cc4b 662\r
663 if StartupRoutine >= 1M, then ASSERT.\r
664 if StartupRoutine is not multiple of 4K, then ASSERT.\r
665\r
666 @param StartupRoutine Points to a start-up routine which is below 1M physical\r
667 address and 4K aligned.\r
668**/\r
669VOID\r
670EFIAPI\r
671SendInitSipiSipiAllExcludingSelf (\r
672 IN UINT32 StartupRoutine\r
673 )\r
674{\r
675 LOCAL_APIC_ICR_LOW IcrLow;\r
676\r
677 ASSERT (StartupRoutine < 0x100000);\r
678 ASSERT ((StartupRoutine & 0xfff) == 0);\r
679\r
680 SendInitIpiAllExcludingSelf ();\r
cf1eb6e6 681 MicroSecondDelay (PcdGet32(PcdCpuInitIpiDelayInMicroSeconds));\r
bf73cc4b 682 IcrLow.Uint32 = 0;\r
683 IcrLow.Bits.Vector = (StartupRoutine >> 12);\r
684 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_STARTUP;\r
685 IcrLow.Bits.Level = 1;\r
686 IcrLow.Bits.DestinationShorthand = LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF;\r
687 SendIpi (IcrLow.Uint32, 0);\r
bf252e29
ED
688 if (!StandardSignatureIsAuthenticAMD ()) {\r
689 MicroSecondDelay (200);\r
690 SendIpi (IcrLow.Uint32, 0);\r
691 }\r
bf73cc4b 692}\r
693\r
14e4ca25
MK
694/**\r
695 Initialize the state of the SoftwareEnable bit in the Local APIC\r
696 Spurious Interrupt Vector register.\r
697\r
698 @param Enable If TRUE, then set SoftwareEnable to 1\r
699 If FALSE, then set SoftwareEnable to 0.\r
700\r
701**/\r
702VOID\r
703EFIAPI\r
704InitializeLocalApicSoftwareEnable (\r
705 IN BOOLEAN Enable\r
706 )\r
707{\r
708 LOCAL_APIC_SVR Svr;\r
709\r
710 //\r
711 // Set local APIC software-enabled bit.\r
712 //\r
713 Svr.Uint32 = ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET);\r
714 if (Enable) {\r
715 if (Svr.Bits.SoftwareEnable == 0) {\r
716 Svr.Bits.SoftwareEnable = 1;\r
717 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
718 }\r
719 } else {\r
720 if (Svr.Bits.SoftwareEnable == 1) {\r
721 Svr.Bits.SoftwareEnable = 0;\r
722 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
723 }\r
724 }\r
725}\r
726\r
bf73cc4b 727/**\r
728 Programming Virtual Wire Mode.\r
729\r
730 This function programs the local APIC for virtual wire mode following\r
731 the example described in chapter A.3 of the MP 1.4 spec.\r
732\r
733 IOxAPIC is not involved in this type of virtual wire mode.\r
734**/\r
735VOID\r
736EFIAPI\r
737ProgramVirtualWireMode (\r
738 VOID\r
739 )\r
740{\r
741 LOCAL_APIC_SVR Svr;\r
742 LOCAL_APIC_LVT_LINT Lint;\r
743\r
744 //\r
745 // Enable the APIC via SVR and set the spurious interrupt to use Int 00F.\r
746 //\r
747 Svr.Uint32 = ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET);\r
748 Svr.Bits.SpuriousVector = 0xf;\r
749 Svr.Bits.SoftwareEnable = 1;\r
750 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
751\r
752 //\r
753 // Program the LINT0 vector entry as ExtInt. Not masked, edge, active high.\r
754 //\r
ae40aef1 755 Lint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET);\r
bf73cc4b 756 Lint.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_EXTINT;\r
757 Lint.Bits.InputPinPolarity = 0;\r
758 Lint.Bits.TriggerMode = 0;\r
759 Lint.Bits.Mask = 0;\r
ae40aef1 760 WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET, Lint.Uint32);\r
bf73cc4b 761\r
762 //\r
763 // Program the LINT0 vector entry as NMI. Not masked, edge, active high.\r
764 //\r
ae40aef1 765 Lint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET);\r
bf73cc4b 766 Lint.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_NMI;\r
767 Lint.Bits.InputPinPolarity = 0;\r
768 Lint.Bits.TriggerMode = 0;\r
769 Lint.Bits.Mask = 0;\r
ae40aef1 770 WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET, Lint.Uint32);\r
bf73cc4b 771}\r
772\r
b1b8c631 773/**\r
774 Disable LINT0 & LINT1 interrupts.\r
775\r
776 This function sets the mask flag in the LVT LINT0 & LINT1 registers.\r
777**/\r
778VOID\r
779EFIAPI\r
780DisableLvtInterrupts (\r
781 VOID\r
782 )\r
783{\r
784 LOCAL_APIC_LVT_LINT LvtLint;\r
785\r
786 LvtLint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET);\r
787 LvtLint.Bits.Mask = 1;\r
788 WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET, LvtLint.Uint32);\r
789\r
790 LvtLint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET);\r
791 LvtLint.Bits.Mask = 1;\r
792 WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET, LvtLint.Uint32);\r
793}\r
794\r
bf73cc4b 795/**\r
796 Read the initial count value from the init-count register.\r
797\r
798 @return The initial count value read from the init-count register.\r
799**/\r
800UINT32\r
801EFIAPI\r
802GetApicTimerInitCount (\r
803 VOID\r
804 )\r
805{\r
806 return ReadLocalApicReg (XAPIC_TIMER_INIT_COUNT_OFFSET);\r
807}\r
808\r
809/**\r
810 Read the current count value from the current-count register.\r
811\r
812 @return The current count value read from the current-count register.\r
813**/\r
814UINT32\r
815EFIAPI\r
816GetApicTimerCurrentCount (\r
817 VOID\r
818 )\r
819{\r
820 return ReadLocalApicReg (XAPIC_TIMER_CURRENT_COUNT_OFFSET);\r
821}\r
822\r
823/**\r
824 Initialize the local APIC timer.\r
825\r
826 The local APIC timer is initialized and enabled.\r
827\r
828 @param DivideValue The divide value for the DCR. It is one of 1,2,4,8,16,32,64,128.\r
829 If it is 0, then use the current divide value in the DCR.\r
830 @param InitCount The initial count value.\r
831 @param PeriodicMode If TRUE, timer mode is peridoic. Othewise, timer mode is one-shot.\r
832 @param Vector The timer interrupt vector number.\r
833**/\r
834VOID\r
835EFIAPI\r
836InitializeApicTimer (\r
837 IN UINTN DivideValue,\r
838 IN UINT32 InitCount,\r
839 IN BOOLEAN PeriodicMode,\r
840 IN UINT8 Vector\r
841 )\r
842{\r
bf73cc4b 843 LOCAL_APIC_DCR Dcr;\r
844 LOCAL_APIC_LVT_TIMER LvtTimer;\r
845 UINT32 Divisor;\r
846\r
847 //\r
848 // Ensure local APIC is in software-enabled state.\r
849 //\r
14e4ca25 850 InitializeLocalApicSoftwareEnable (TRUE);\r
bf73cc4b 851\r
852 //\r
853 // Program init-count register.\r
854 //\r
855 WriteLocalApicReg (XAPIC_TIMER_INIT_COUNT_OFFSET, InitCount);\r
856\r
857 if (DivideValue != 0) {\r
858 ASSERT (DivideValue <= 128);\r
859 ASSERT (DivideValue == GetPowerOfTwo32((UINT32)DivideValue));\r
860 Divisor = (UINT32)((HighBitSet32 ((UINT32)DivideValue) - 1) & 0x7);\r
861\r
862 Dcr.Uint32 = ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET);\r
863 Dcr.Bits.DivideValue1 = (Divisor & 0x3);\r
864 Dcr.Bits.DivideValue2 = (Divisor >> 2);\r
7367cc6c 865 WriteLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET, Dcr.Uint32);\r
bf73cc4b 866 }\r
867\r
868 //\r
869 // Enable APIC timer interrupt with specified timer mode.\r
870 //\r
871 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
872 if (PeriodicMode) {\r
873 LvtTimer.Bits.TimerMode = 1;\r
874 } else {\r
875 LvtTimer.Bits.TimerMode = 0;\r
876 }\r
877 LvtTimer.Bits.Mask = 0;\r
878 LvtTimer.Bits.Vector = Vector;\r
879 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET, LvtTimer.Uint32);\r
880}\r
881\r
ae40aef1 882/**\r
883 Get the state of the local APIC timer.\r
884\r
6d72ff7d
HW
885 This function will ASSERT if the local APIC is not software enabled.\r
886\r
ae40aef1 887 @param DivideValue Return the divide value for the DCR. It is one of 1,2,4,8,16,32,64,128.\r
888 @param PeriodicMode Return the timer mode. If TRUE, timer mode is peridoic. Othewise, timer mode is one-shot.\r
889 @param Vector Return the timer interrupt vector number.\r
890**/\r
891VOID\r
892EFIAPI\r
893GetApicTimerState (\r
894 OUT UINTN *DivideValue OPTIONAL,\r
895 OUT BOOLEAN *PeriodicMode OPTIONAL,\r
896 OUT UINT8 *Vector OPTIONAL\r
897 )\r
898{\r
899 UINT32 Divisor;\r
900 LOCAL_APIC_DCR Dcr;\r
901 LOCAL_APIC_LVT_TIMER LvtTimer;\r
902\r
6d72ff7d
HW
903 //\r
904 // Check the APIC Software Enable/Disable bit (bit 8) in Spurious-Interrupt\r
905 // Vector Register.\r
906 // This bit will be 1, if local APIC is software enabled.\r
907 //\r
908 ASSERT ((ReadLocalApicReg(XAPIC_SPURIOUS_VECTOR_OFFSET) & BIT8) != 0);\r
909\r
ae40aef1 910 if (DivideValue != NULL) {\r
911 Dcr.Uint32 = ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET);\r
912 Divisor = Dcr.Bits.DivideValue1 | (Dcr.Bits.DivideValue2 << 2);\r
913 Divisor = (Divisor + 1) & 0x7;\r
914 *DivideValue = ((UINTN)1) << Divisor;\r
915 }\r
916\r
917 if (PeriodicMode != NULL || Vector != NULL) {\r
918 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
919 if (PeriodicMode != NULL) {\r
920 if (LvtTimer.Bits.TimerMode == 1) {\r
921 *PeriodicMode = TRUE;\r
922 } else {\r
923 *PeriodicMode = FALSE;\r
924 }\r
925 }\r
926 if (Vector != NULL) {\r
927 *Vector = (UINT8) LvtTimer.Bits.Vector;\r
928 }\r
929 }\r
930}\r
931\r
bf73cc4b 932/**\r
933 Enable the local APIC timer interrupt.\r
934**/\r
935VOID\r
936EFIAPI\r
937EnableApicTimerInterrupt (\r
938 VOID\r
939 )\r
940{\r
941 LOCAL_APIC_LVT_TIMER LvtTimer;\r
942\r
943 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
944 LvtTimer.Bits.Mask = 0;\r
945 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET, LvtTimer.Uint32);\r
946}\r
947\r
948/**\r
949 Disable the local APIC timer interrupt.\r
950**/\r
951VOID\r
952EFIAPI\r
953DisableApicTimerInterrupt (\r
954 VOID\r
955 )\r
956{\r
957 LOCAL_APIC_LVT_TIMER LvtTimer;\r
958\r
959 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
960 LvtTimer.Bits.Mask = 1;\r
961 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET, LvtTimer.Uint32);\r
962}\r
963\r
964/**\r
965 Get the local APIC timer interrupt state.\r
966\r
967 @retval TRUE The local APIC timer interrupt is enabled.\r
968 @retval FALSE The local APIC timer interrupt is disabled.\r
969**/\r
970BOOLEAN\r
971EFIAPI\r
972GetApicTimerInterruptState (\r
973 VOID\r
974 )\r
975{\r
976 LOCAL_APIC_LVT_TIMER LvtTimer;\r
977\r
978 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
979 return (BOOLEAN)(LvtTimer.Bits.Mask == 0);\r
980}\r
981\r
982/**\r
983 Send EOI to the local APIC.\r
984**/\r
985VOID\r
986EFIAPI\r
987SendApicEoi (\r
988 VOID\r
989 )\r
990{\r
991 WriteLocalApicReg (XAPIC_EOI_OFFSET, 0);\r
992}\r
993\r
5f867ad0 994/**\r
7367cc6c 995 Get the 32-bit address that a device should use to send a Message Signaled\r
5f867ad0 996 Interrupt (MSI) to the Local APIC of the currently executing processor.\r
997\r
998 @return 32-bit address used to send an MSI to the Local APIC.\r
999**/\r
1000UINT32\r
7367cc6c 1001EFIAPI\r
5f867ad0 1002GetApicMsiAddress (\r
1003 VOID\r
1004 )\r
1005{\r
1006 LOCAL_APIC_MSI_ADDRESS MsiAddress;\r
1007\r
1008 //\r
7367cc6c 1009 // Return address for an MSI interrupt to be delivered only to the APIC ID\r
5f867ad0 1010 // of the currently executing processor.\r
1011 //\r
1012 MsiAddress.Uint32 = 0;\r
1013 MsiAddress.Bits.BaseAddress = 0xFEE;\r
1014 MsiAddress.Bits.DestinationId = GetApicId ();\r
1015 return MsiAddress.Uint32;\r
1016}\r
7367cc6c 1017\r
5f867ad0 1018/**\r
7367cc6c 1019 Get the 64-bit data value that a device should use to send a Message Signaled\r
5f867ad0 1020 Interrupt (MSI) to the Local APIC of the currently executing processor.\r
1021\r
1022 If Vector is not in range 0x10..0xFE, then ASSERT().\r
1023 If DeliveryMode is not supported, then ASSERT().\r
7367cc6c
LG
1024\r
1025 @param Vector The 8-bit interrupt vector associated with the MSI.\r
5f867ad0 1026 Must be in the range 0x10..0xFE\r
7367cc6c 1027 @param DeliveryMode A 3-bit value that specifies how the recept of the MSI\r
5f867ad0 1028 is handled. The only supported values are:\r
1029 0: LOCAL_APIC_DELIVERY_MODE_FIXED\r
1030 1: LOCAL_APIC_DELIVERY_MODE_LOWEST_PRIORITY\r
1031 2: LOCAL_APIC_DELIVERY_MODE_SMI\r
1032 4: LOCAL_APIC_DELIVERY_MODE_NMI\r
1033 5: LOCAL_APIC_DELIVERY_MODE_INIT\r
1034 7: LOCAL_APIC_DELIVERY_MODE_EXTINT\r
7367cc6c
LG
1035\r
1036 @param LevelTriggered TRUE specifies a level triggered interrupt.\r
5f867ad0 1037 FALSE specifies an edge triggered interrupt.\r
1038 @param AssertionLevel Ignored if LevelTriggered is FALSE.\r
7367cc6c 1039 TRUE specifies a level triggered interrupt that active\r
5f867ad0 1040 when the interrupt line is asserted.\r
7367cc6c 1041 FALSE specifies a level triggered interrupt that active\r
5f867ad0 1042 when the interrupt line is deasserted.\r
1043\r
1044 @return 64-bit data value used to send an MSI to the Local APIC.\r
1045**/\r
1046UINT64\r
7367cc6c 1047EFIAPI\r
5f867ad0 1048GetApicMsiValue (\r
1049 IN UINT8 Vector,\r
1050 IN UINTN DeliveryMode,\r
1051 IN BOOLEAN LevelTriggered,\r
1052 IN BOOLEAN AssertionLevel\r
1053 )\r
1054{\r
1055 LOCAL_APIC_MSI_DATA MsiData;\r
1056\r
1057 ASSERT (Vector >= 0x10 && Vector <= 0xFE);\r
1058 ASSERT (DeliveryMode < 8 && DeliveryMode != 6 && DeliveryMode != 3);\r
7367cc6c 1059\r
5f867ad0 1060 MsiData.Uint64 = 0;\r
1061 MsiData.Bits.Vector = Vector;\r
1062 MsiData.Bits.DeliveryMode = (UINT32)DeliveryMode;\r
1063 if (LevelTriggered) {\r
1064 MsiData.Bits.TriggerMode = 1;\r
1065 if (AssertionLevel) {\r
1066 MsiData.Bits.Level = 1;\r
1067 }\r
1068 }\r
1069 return MsiData.Uint64;\r
1070}\r
73152f19
LD
1071\r
1072/**\r
1073 Get Package ID/Core ID/Thread ID of a processor.\r
1074\r
1075 The algorithm assumes the target system has symmetry across physical\r
1076 package boundaries with respect to the number of logical processors\r
1077 per package, number of cores per package.\r
1078\r
1079 @param[in] InitialApicId Initial APIC ID of the target logical processor.\r
1080 @param[out] Package Returns the processor package ID.\r
1081 @param[out] Core Returns the processor core ID.\r
1082 @param[out] Thread Returns the processor thread ID.\r
1083**/\r
1084VOID\r
1c8ca9a0 1085EFIAPI\r
262128e5 1086GetProcessorLocationByApicId (\r
73152f19
LD
1087 IN UINT32 InitialApicId,\r
1088 OUT UINT32 *Package OPTIONAL,\r
1089 OUT UINT32 *Core OPTIONAL,\r
1090 OUT UINT32 *Thread OPTIONAL\r
1091 )\r
1092{\r
061ead7a
LD
1093 BOOLEAN TopologyLeafSupported;\r
1094 CPUID_VERSION_INFO_EBX VersionInfoEbx;\r
1095 CPUID_VERSION_INFO_EDX VersionInfoEdx;\r
1096 CPUID_CACHE_PARAMS_EAX CacheParamsEax;\r
1097 CPUID_EXTENDED_TOPOLOGY_EAX ExtendedTopologyEax;\r
1098 CPUID_EXTENDED_TOPOLOGY_EBX ExtendedTopologyEbx;\r
1099 CPUID_EXTENDED_TOPOLOGY_ECX ExtendedTopologyEcx;\r
1100 CPUID_AMD_EXTENDED_CPU_SIG_ECX AmdExtendedCpuSigEcx;\r
1101 CPUID_AMD_PROCESSOR_TOPOLOGY_EBX AmdProcessorTopologyEbx;\r
061ead7a
LD
1102 CPUID_AMD_VIR_PHY_ADDRESS_SIZE_ECX AmdVirPhyAddressSizeEcx;\r
1103 UINT32 MaxStandardCpuIdIndex;\r
1104 UINT32 MaxExtendedCpuIdIndex;\r
1105 UINT32 SubIndex;\r
1106 UINTN LevelType;\r
1107 UINT32 MaxLogicProcessorsPerPackage;\r
1108 UINT32 MaxCoresPerPackage;\r
061ead7a
LD
1109 UINTN ThreadBits;\r
1110 UINTN CoreBits;\r
73152f19
LD
1111\r
1112 //\r
1113 // Check if the processor is capable of supporting more than one logical processor.\r
1114 //\r
ae66c6f1 1115 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);\r
73152f19
LD
1116 if (VersionInfoEdx.Bits.HTT == 0) {\r
1117 if (Thread != NULL) {\r
061ead7a 1118 *Thread = 0;\r
73152f19
LD
1119 }\r
1120 if (Core != NULL) {\r
061ead7a 1121 *Core = 0;\r
73152f19
LD
1122 }\r
1123 if (Package != NULL) {\r
1124 *Package = 0;\r
1125 }\r
1126 return;\r
1127 }\r
1128\r
73152f19 1129 //\r
061ead7a 1130 // Assume three-level mapping of APIC ID: Package|Core|Thread.\r
73152f19 1131 //\r
061ead7a
LD
1132 ThreadBits = 0;\r
1133 CoreBits = 0;\r
73152f19
LD
1134\r
1135 //\r
061ead7a 1136 // Get max index of CPUID\r
73152f19 1137 //\r
ae66c6f1
LD
1138 AsmCpuid (CPUID_SIGNATURE, &MaxStandardCpuIdIndex, NULL, NULL, NULL);\r
1139 AsmCpuid (CPUID_EXTENDED_FUNCTION, &MaxExtendedCpuIdIndex, NULL, NULL, NULL);\r
73152f19
LD
1140\r
1141 //\r
1142 // If the extended topology enumeration leaf is available, it\r
1143 // is the preferred mechanism for enumerating topology.\r
1144 //\r
061ead7a
LD
1145 TopologyLeafSupported = FALSE;\r
1146 if (MaxStandardCpuIdIndex >= CPUID_EXTENDED_TOPOLOGY) {\r
73152f19
LD
1147 AsmCpuidEx(\r
1148 CPUID_EXTENDED_TOPOLOGY,\r
1149 0,\r
1150 &ExtendedTopologyEax.Uint32,\r
1151 &ExtendedTopologyEbx.Uint32,\r
1152 &ExtendedTopologyEcx.Uint32,\r
1153 NULL\r
1154 );\r
1155 //\r
1156 // If CPUID.(EAX=0BH, ECX=0H):EBX returns zero and maximum input value for\r
1157 // basic CPUID information is greater than 0BH, then CPUID.0BH leaf is not\r
1158 // supported on that processor.\r
1159 //\r
1160 if (ExtendedTopologyEbx.Uint32 != 0) {\r
1161 TopologyLeafSupported = TRUE;\r
1162\r
1163 //\r
1164 // Sub-leaf index 0 (ECX= 0 as input) provides enumeration parameters to extract\r
1165 // the SMT sub-field of x2APIC ID.\r
1166 //\r
1167 LevelType = ExtendedTopologyEcx.Bits.LevelType;\r
ae66c6f1 1168 ASSERT (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT);\r
73152f19
LD
1169 ThreadBits = ExtendedTopologyEax.Bits.ApicIdShift;\r
1170\r
1171 //\r
1172 // Software must not assume any "level type" encoding\r
1173 // value to be related to any sub-leaf index, except sub-leaf 0.\r
1174 //\r
1175 SubIndex = 1;\r
1176 do {\r
ae66c6f1 1177 AsmCpuidEx (\r
73152f19
LD
1178 CPUID_EXTENDED_TOPOLOGY,\r
1179 SubIndex,\r
1180 &ExtendedTopologyEax.Uint32,\r
1181 NULL,\r
1182 &ExtendedTopologyEcx.Uint32,\r
1183 NULL\r
1184 );\r
1185 LevelType = ExtendedTopologyEcx.Bits.LevelType;\r
1186 if (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_CORE) {\r
1187 CoreBits = ExtendedTopologyEax.Bits.ApicIdShift - ThreadBits;\r
1188 break;\r
1189 }\r
1190 SubIndex++;\r
1191 } while (LevelType != CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_INVALID);\r
1192 }\r
1193 }\r
1194\r
1195 if (!TopologyLeafSupported) {\r
061ead7a
LD
1196 //\r
1197 // Get logical processor count\r
1198 //\r
ae66c6f1 1199 AsmCpuid (CPUID_VERSION_INFO, NULL, &VersionInfoEbx.Uint32, NULL, NULL);\r
73152f19 1200 MaxLogicProcessorsPerPackage = VersionInfoEbx.Bits.MaximumAddressableIdsForLogicalProcessors;\r
061ead7a
LD
1201\r
1202 //\r
1203 // Assume single-core processor\r
1204 //\r
1205 MaxCoresPerPackage = 1;\r
1206\r
1207 //\r
1208 // Check for topology extensions on AMD processor\r
1209 //\r
1210 if (StandardSignatureIsAuthenticAMD()) {\r
1211 if (MaxExtendedCpuIdIndex >= CPUID_AMD_PROCESSOR_TOPOLOGY) {\r
ae66c6f1 1212 AsmCpuid (CPUID_EXTENDED_CPU_SIG, NULL, NULL, &AmdExtendedCpuSigEcx.Uint32, NULL);\r
061ead7a 1213 if (AmdExtendedCpuSigEcx.Bits.TopologyExtensions != 0) {\r
061ead7a 1214 //\r
ae66c6f1 1215 // Account for max possible thread count to decode ApicId\r
061ead7a 1216 //\r
ae66c6f1
LD
1217 AsmCpuid (CPUID_VIR_PHY_ADDRESS_SIZE, NULL, NULL, &AmdVirPhyAddressSizeEcx.Uint32, NULL);\r
1218 MaxLogicProcessorsPerPackage = 1 << AmdVirPhyAddressSizeEcx.Bits.ApicIdCoreIdSize;\r
061ead7a
LD
1219\r
1220 //\r
ae66c6f1 1221 // Get cores per processor package\r
061ead7a 1222 //\r
ae66c6f1
LD
1223 AsmCpuid (CPUID_AMD_PROCESSOR_TOPOLOGY, NULL, &AmdProcessorTopologyEbx.Uint32, NULL, NULL);\r
1224 MaxCoresPerPackage = MaxLogicProcessorsPerPackage / (AmdProcessorTopologyEbx.Bits.ThreadsPerCore + 1);\r
061ead7a
LD
1225 }\r
1226 }\r
73152f19
LD
1227 }\r
1228 else {\r
1229 //\r
061ead7a 1230 // Extract core count based on CACHE information\r
73152f19 1231 //\r
061ead7a 1232 if (MaxStandardCpuIdIndex >= CPUID_CACHE_PARAMS) {\r
ae66c6f1 1233 AsmCpuidEx (CPUID_CACHE_PARAMS, 0, &CacheParamsEax.Uint32, NULL, NULL, NULL);\r
061ead7a
LD
1234 if (CacheParamsEax.Uint32 != 0) {\r
1235 MaxCoresPerPackage = CacheParamsEax.Bits.MaximumAddressableIdsForLogicalProcessors + 1;\r
1236 }\r
1237 }\r
73152f19
LD
1238 }\r
1239\r
1240 ThreadBits = (UINTN)(HighBitSet32(MaxLogicProcessorsPerPackage / MaxCoresPerPackage - 1) + 1);\r
061ead7a
LD
1241 CoreBits = (UINTN)(HighBitSet32(MaxCoresPerPackage - 1) + 1);\r
1242 }\r
73152f19
LD
1243\r
1244 if (Thread != NULL) {\r
061ead7a 1245 *Thread = InitialApicId & ((1 << ThreadBits) - 1);\r
73152f19
LD
1246 }\r
1247 if (Core != NULL) {\r
061ead7a 1248 *Core = (InitialApicId >> ThreadBits) & ((1 << CoreBits) - 1);\r
73152f19
LD
1249 }\r
1250 if (Package != NULL) {\r
1251 *Package = (InitialApicId >> (ThreadBits + CoreBits));\r
1252 }\r
1253}\r