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