]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/BaseXApicX2ApicLib/BaseXApicX2ApicLib.c
UefiCpuPkg/LocalApicLib: fix feature test for Extended Topology CPUID leaf
[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
4af3ae14 414 // And CPUID.0BH:EBX[15:0] reports a non-zero value,\r
6e3e4d70
JF
415 // Then the initial 32-bit APIC ID = CPUID.0BH:EDX\r
416 // Else the initial 8-bit APIC ID = CPUID.1:EBX[31:24]\r
417 //\r
418 if (MaxCpuIdIndex >= CPUID_EXTENDED_TOPOLOGY) {\r
4af3ae14
LE
419 AsmCpuidEx (CPUID_EXTENDED_TOPOLOGY, 0, NULL, &RegEbx, NULL, &ApicId);\r
420 if ((RegEbx & (BIT16 - 1)) != 0) {\r
421 return ApicId;\r
422 }\r
6e3e4d70 423 }\r
bf73cc4b 424 AsmCpuid (CPUID_VERSION_INFO, NULL, &RegEbx, NULL, NULL);\r
425 return RegEbx >> 24;\r
426 } else {\r
427 return GetApicId ();\r
428 }\r
429}\r
430\r
431/**\r
432 Get the local APIC ID of the executing processor.\r
433\r
434 @return 32-bit local APIC ID of the executing processor.\r
435**/\r
436UINT32\r
437EFIAPI\r
438GetApicId (\r
439 VOID\r
440 )\r
441{\r
442 UINT32 ApicId;\r
6e3e4d70 443 UINT32 InitApicId;\r
bf73cc4b 444\r
445 ApicId = ReadLocalApicReg (XAPIC_ID_OFFSET);\r
446 if (GetApicMode () == LOCAL_APIC_MODE_XAPIC) {\r
6e3e4d70 447 ApicId = ((InitApicId = GetInitialApicId ()) < 0x100) ? (ApicId >> 24) : InitApicId;\r
bf73cc4b 448 }\r
6e3e4d70 449\r
bf73cc4b 450 return ApicId;\r
451}\r
452\r
ae40aef1 453/**\r
454 Get the value of the local APIC version register.\r
455\r
456 @return the value of the local APIC version register.\r
457**/\r
458UINT32\r
459EFIAPI\r
460GetApicVersion (\r
461 VOID\r
462 )\r
463{\r
464 return ReadLocalApicReg (XAPIC_VERSION_OFFSET);\r
465}\r
466\r
467/**\r
468 Send a Fixed IPI to a specified target processor.\r
469\r
470 This function returns after the IPI has been accepted by the target processor. \r
471\r
472 @param ApicId The local APIC ID of the target processor.\r
473 @param Vector The vector number of the interrupt being sent.\r
474**/\r
475VOID\r
476EFIAPI\r
477SendFixedIpi (\r
478 IN UINT32 ApicId,\r
479 IN UINT8 Vector\r
480 )\r
481{\r
482 LOCAL_APIC_ICR_LOW IcrLow;\r
483\r
484 IcrLow.Uint32 = 0;\r
485 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_FIXED;\r
486 IcrLow.Bits.Level = 1;\r
487 IcrLow.Bits.Vector = Vector;\r
488 SendIpi (IcrLow.Uint32, ApicId);\r
489}\r
490\r
491/**\r
492 Send a Fixed IPI to all processors excluding self.\r
493\r
494 This function returns after the IPI has been accepted by the target processors. \r
495\r
496 @param Vector The vector number of the interrupt being sent.\r
497**/\r
498VOID\r
499EFIAPI\r
500SendFixedIpiAllExcludingSelf (\r
501 IN UINT8 Vector\r
502 )\r
503{\r
504 LOCAL_APIC_ICR_LOW IcrLow;\r
505\r
506 IcrLow.Uint32 = 0;\r
507 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_FIXED;\r
508 IcrLow.Bits.Level = 1;\r
509 IcrLow.Bits.DestinationShorthand = LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF;\r
510 IcrLow.Bits.Vector = Vector;\r
511 SendIpi (IcrLow.Uint32, 0);\r
512}\r
513\r
bf73cc4b 514/**\r
515 Send a SMI IPI to a specified target processor.\r
516\r
517 This function returns after the IPI has been accepted by the target processor. \r
518\r
519 @param ApicId Specify the local APIC ID of the target processor.\r
520**/\r
521VOID\r
522EFIAPI\r
523SendSmiIpi (\r
524 IN UINT32 ApicId\r
525 )\r
526{\r
527 LOCAL_APIC_ICR_LOW IcrLow;\r
528\r
529 IcrLow.Uint32 = 0;\r
530 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_SMI;\r
531 IcrLow.Bits.Level = 1;\r
532 SendIpi (IcrLow.Uint32, ApicId);\r
533}\r
534\r
535/**\r
536 Send a SMI IPI to all processors excluding self.\r
537\r
538 This function returns after the IPI has been accepted by the target processors. \r
539**/\r
540VOID\r
541EFIAPI\r
542SendSmiIpiAllExcludingSelf (\r
543 VOID\r
544 )\r
545{\r
546 LOCAL_APIC_ICR_LOW IcrLow;\r
547\r
548 IcrLow.Uint32 = 0;\r
549 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_SMI;\r
550 IcrLow.Bits.Level = 1;\r
551 IcrLow.Bits.DestinationShorthand = LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF;\r
552 SendIpi (IcrLow.Uint32, 0);\r
553}\r
554\r
555/**\r
556 Send an INIT IPI to a specified target processor.\r
557\r
558 This function returns after the IPI has been accepted by the target processor. \r
559\r
560 @param ApicId Specify the local APIC ID of the target processor.\r
561**/\r
562VOID\r
563EFIAPI\r
564SendInitIpi (\r
565 IN UINT32 ApicId\r
566 )\r
567{\r
568 LOCAL_APIC_ICR_LOW IcrLow;\r
569\r
570 IcrLow.Uint32 = 0;\r
571 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_INIT;\r
572 IcrLow.Bits.Level = 1;\r
573 SendIpi (IcrLow.Uint32, ApicId);\r
574}\r
575\r
576/**\r
577 Send an INIT IPI to all processors excluding self.\r
578\r
579 This function returns after the IPI has been accepted by the target processors. \r
580**/\r
581VOID\r
582EFIAPI\r
583SendInitIpiAllExcludingSelf (\r
584 VOID\r
585 )\r
586{\r
587 LOCAL_APIC_ICR_LOW IcrLow;\r
588\r
589 IcrLow.Uint32 = 0;\r
590 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_INIT;\r
591 IcrLow.Bits.Level = 1;\r
592 IcrLow.Bits.DestinationShorthand = LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF;\r
593 SendIpi (IcrLow.Uint32, 0);\r
594}\r
595\r
596/**\r
597 Send an INIT-Start-up-Start-up IPI sequence to a specified target processor.\r
598\r
599 This function returns after the IPI has been accepted by the target processor. \r
600\r
601 if StartupRoutine >= 1M, then ASSERT.\r
602 if StartupRoutine is not multiple of 4K, then ASSERT.\r
603\r
604 @param ApicId Specify the local APIC ID of the target processor.\r
605 @param StartupRoutine Points to a start-up routine which is below 1M physical\r
606 address and 4K aligned.\r
607**/\r
608VOID\r
609EFIAPI\r
610SendInitSipiSipi (\r
611 IN UINT32 ApicId,\r
612 IN UINT32 StartupRoutine\r
613 )\r
614{\r
615 LOCAL_APIC_ICR_LOW IcrLow;\r
616\r
617 ASSERT (StartupRoutine < 0x100000);\r
618 ASSERT ((StartupRoutine & 0xfff) == 0);\r
619\r
620 SendInitIpi (ApicId);\r
cf1eb6e6 621 MicroSecondDelay (PcdGet32(PcdCpuInitIpiDelayInMicroSeconds));\r
bf73cc4b 622 IcrLow.Uint32 = 0;\r
623 IcrLow.Bits.Vector = (StartupRoutine >> 12);\r
624 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_STARTUP;\r
625 IcrLow.Bits.Level = 1;\r
626 SendIpi (IcrLow.Uint32, ApicId);\r
627 MicroSecondDelay (200);\r
628 SendIpi (IcrLow.Uint32, ApicId);\r
629}\r
630\r
631/**\r
632 Send an INIT-Start-up-Start-up IPI sequence to all processors excluding self.\r
633\r
634 This function returns after the IPI has been accepted by the target processors. \r
635\r
636 if StartupRoutine >= 1M, then ASSERT.\r
637 if StartupRoutine is not multiple of 4K, then ASSERT.\r
638\r
639 @param StartupRoutine Points to a start-up routine which is below 1M physical\r
640 address and 4K aligned.\r
641**/\r
642VOID\r
643EFIAPI\r
644SendInitSipiSipiAllExcludingSelf (\r
645 IN UINT32 StartupRoutine\r
646 )\r
647{\r
648 LOCAL_APIC_ICR_LOW IcrLow;\r
649\r
650 ASSERT (StartupRoutine < 0x100000);\r
651 ASSERT ((StartupRoutine & 0xfff) == 0);\r
652\r
653 SendInitIpiAllExcludingSelf ();\r
cf1eb6e6 654 MicroSecondDelay (PcdGet32(PcdCpuInitIpiDelayInMicroSeconds));\r
bf73cc4b 655 IcrLow.Uint32 = 0;\r
656 IcrLow.Bits.Vector = (StartupRoutine >> 12);\r
657 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_STARTUP;\r
658 IcrLow.Bits.Level = 1;\r
659 IcrLow.Bits.DestinationShorthand = LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF;\r
660 SendIpi (IcrLow.Uint32, 0);\r
661 MicroSecondDelay (200);\r
662 SendIpi (IcrLow.Uint32, 0);\r
663}\r
664\r
14e4ca25
MK
665/**\r
666 Initialize the state of the SoftwareEnable bit in the Local APIC\r
667 Spurious Interrupt Vector register.\r
668\r
669 @param Enable If TRUE, then set SoftwareEnable to 1\r
670 If FALSE, then set SoftwareEnable to 0.\r
671\r
672**/\r
673VOID\r
674EFIAPI\r
675InitializeLocalApicSoftwareEnable (\r
676 IN BOOLEAN Enable\r
677 )\r
678{\r
679 LOCAL_APIC_SVR Svr;\r
680\r
681 //\r
682 // Set local APIC software-enabled bit.\r
683 //\r
684 Svr.Uint32 = ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET);\r
685 if (Enable) {\r
686 if (Svr.Bits.SoftwareEnable == 0) {\r
687 Svr.Bits.SoftwareEnable = 1;\r
688 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
689 }\r
690 } else {\r
691 if (Svr.Bits.SoftwareEnable == 1) {\r
692 Svr.Bits.SoftwareEnable = 0;\r
693 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
694 }\r
695 }\r
696}\r
697\r
bf73cc4b 698/**\r
699 Programming Virtual Wire Mode.\r
700\r
701 This function programs the local APIC for virtual wire mode following\r
702 the example described in chapter A.3 of the MP 1.4 spec.\r
703\r
704 IOxAPIC is not involved in this type of virtual wire mode.\r
705**/\r
706VOID\r
707EFIAPI\r
708ProgramVirtualWireMode (\r
709 VOID\r
710 )\r
711{\r
712 LOCAL_APIC_SVR Svr;\r
713 LOCAL_APIC_LVT_LINT Lint;\r
714\r
715 //\r
716 // Enable the APIC via SVR and set the spurious interrupt to use Int 00F.\r
717 //\r
718 Svr.Uint32 = ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET);\r
719 Svr.Bits.SpuriousVector = 0xf;\r
720 Svr.Bits.SoftwareEnable = 1;\r
721 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
722\r
723 //\r
724 // Program the LINT0 vector entry as ExtInt. Not masked, edge, active high.\r
725 //\r
ae40aef1 726 Lint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET);\r
bf73cc4b 727 Lint.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_EXTINT;\r
728 Lint.Bits.InputPinPolarity = 0;\r
729 Lint.Bits.TriggerMode = 0;\r
730 Lint.Bits.Mask = 0;\r
ae40aef1 731 WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET, Lint.Uint32);\r
bf73cc4b 732\r
733 //\r
734 // Program the LINT0 vector entry as NMI. Not masked, edge, active high.\r
735 //\r
ae40aef1 736 Lint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET);\r
bf73cc4b 737 Lint.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_NMI;\r
738 Lint.Bits.InputPinPolarity = 0;\r
739 Lint.Bits.TriggerMode = 0;\r
740 Lint.Bits.Mask = 0;\r
ae40aef1 741 WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET, Lint.Uint32);\r
bf73cc4b 742}\r
743\r
b1b8c631 744/**\r
745 Disable LINT0 & LINT1 interrupts.\r
746\r
747 This function sets the mask flag in the LVT LINT0 & LINT1 registers.\r
748**/\r
749VOID\r
750EFIAPI\r
751DisableLvtInterrupts (\r
752 VOID\r
753 )\r
754{\r
755 LOCAL_APIC_LVT_LINT LvtLint;\r
756\r
757 LvtLint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET);\r
758 LvtLint.Bits.Mask = 1;\r
759 WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET, LvtLint.Uint32);\r
760\r
761 LvtLint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET);\r
762 LvtLint.Bits.Mask = 1;\r
763 WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET, LvtLint.Uint32);\r
764}\r
765\r
bf73cc4b 766/**\r
767 Read the initial count value from the init-count register.\r
768\r
769 @return The initial count value read from the init-count register.\r
770**/\r
771UINT32\r
772EFIAPI\r
773GetApicTimerInitCount (\r
774 VOID\r
775 )\r
776{\r
777 return ReadLocalApicReg (XAPIC_TIMER_INIT_COUNT_OFFSET);\r
778}\r
779\r
780/**\r
781 Read the current count value from the current-count register.\r
782\r
783 @return The current count value read from the current-count register.\r
784**/\r
785UINT32\r
786EFIAPI\r
787GetApicTimerCurrentCount (\r
788 VOID\r
789 )\r
790{\r
791 return ReadLocalApicReg (XAPIC_TIMER_CURRENT_COUNT_OFFSET);\r
792}\r
793\r
794/**\r
795 Initialize the local APIC timer.\r
796\r
797 The local APIC timer is initialized and enabled.\r
798\r
799 @param DivideValue The divide value for the DCR. It is one of 1,2,4,8,16,32,64,128.\r
800 If it is 0, then use the current divide value in the DCR.\r
801 @param InitCount The initial count value.\r
802 @param PeriodicMode If TRUE, timer mode is peridoic. Othewise, timer mode is one-shot.\r
803 @param Vector The timer interrupt vector number.\r
804**/\r
805VOID\r
806EFIAPI\r
807InitializeApicTimer (\r
808 IN UINTN DivideValue,\r
809 IN UINT32 InitCount,\r
810 IN BOOLEAN PeriodicMode,\r
811 IN UINT8 Vector\r
812 )\r
813{\r
bf73cc4b 814 LOCAL_APIC_DCR Dcr;\r
815 LOCAL_APIC_LVT_TIMER LvtTimer;\r
816 UINT32 Divisor;\r
817\r
818 //\r
819 // Ensure local APIC is in software-enabled state.\r
820 //\r
14e4ca25 821 InitializeLocalApicSoftwareEnable (TRUE);\r
bf73cc4b 822\r
823 //\r
824 // Program init-count register.\r
825 //\r
826 WriteLocalApicReg (XAPIC_TIMER_INIT_COUNT_OFFSET, InitCount);\r
827\r
828 if (DivideValue != 0) {\r
829 ASSERT (DivideValue <= 128);\r
830 ASSERT (DivideValue == GetPowerOfTwo32((UINT32)DivideValue));\r
831 Divisor = (UINT32)((HighBitSet32 ((UINT32)DivideValue) - 1) & 0x7);\r
832\r
833 Dcr.Uint32 = ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET);\r
834 Dcr.Bits.DivideValue1 = (Divisor & 0x3);\r
835 Dcr.Bits.DivideValue2 = (Divisor >> 2);\r
836 WriteLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET, Dcr.Uint32); \r
837 }\r
838\r
839 //\r
840 // Enable APIC timer interrupt with specified timer mode.\r
841 //\r
842 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
843 if (PeriodicMode) {\r
844 LvtTimer.Bits.TimerMode = 1;\r
845 } else {\r
846 LvtTimer.Bits.TimerMode = 0;\r
847 }\r
848 LvtTimer.Bits.Mask = 0;\r
849 LvtTimer.Bits.Vector = Vector;\r
850 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET, LvtTimer.Uint32);\r
851}\r
852\r
ae40aef1 853/**\r
854 Get the state of the local APIC timer.\r
855\r
6d72ff7d
HW
856 This function will ASSERT if the local APIC is not software enabled.\r
857\r
ae40aef1 858 @param DivideValue Return the divide value for the DCR. It is one of 1,2,4,8,16,32,64,128.\r
859 @param PeriodicMode Return the timer mode. If TRUE, timer mode is peridoic. Othewise, timer mode is one-shot.\r
860 @param Vector Return the timer interrupt vector number.\r
861**/\r
862VOID\r
863EFIAPI\r
864GetApicTimerState (\r
865 OUT UINTN *DivideValue OPTIONAL,\r
866 OUT BOOLEAN *PeriodicMode OPTIONAL,\r
867 OUT UINT8 *Vector OPTIONAL\r
868 )\r
869{\r
870 UINT32 Divisor;\r
871 LOCAL_APIC_DCR Dcr;\r
872 LOCAL_APIC_LVT_TIMER LvtTimer;\r
873\r
6d72ff7d
HW
874 //\r
875 // Check the APIC Software Enable/Disable bit (bit 8) in Spurious-Interrupt\r
876 // Vector Register.\r
877 // This bit will be 1, if local APIC is software enabled.\r
878 //\r
879 ASSERT ((ReadLocalApicReg(XAPIC_SPURIOUS_VECTOR_OFFSET) & BIT8) != 0);\r
880\r
ae40aef1 881 if (DivideValue != NULL) {\r
882 Dcr.Uint32 = ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET);\r
883 Divisor = Dcr.Bits.DivideValue1 | (Dcr.Bits.DivideValue2 << 2);\r
884 Divisor = (Divisor + 1) & 0x7;\r
885 *DivideValue = ((UINTN)1) << Divisor;\r
886 }\r
887\r
888 if (PeriodicMode != NULL || Vector != NULL) {\r
889 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
890 if (PeriodicMode != NULL) {\r
891 if (LvtTimer.Bits.TimerMode == 1) {\r
892 *PeriodicMode = TRUE;\r
893 } else {\r
894 *PeriodicMode = FALSE;\r
895 }\r
896 }\r
897 if (Vector != NULL) {\r
898 *Vector = (UINT8) LvtTimer.Bits.Vector;\r
899 }\r
900 }\r
901}\r
902\r
bf73cc4b 903/**\r
904 Enable the local APIC timer interrupt.\r
905**/\r
906VOID\r
907EFIAPI\r
908EnableApicTimerInterrupt (\r
909 VOID\r
910 )\r
911{\r
912 LOCAL_APIC_LVT_TIMER LvtTimer;\r
913\r
914 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
915 LvtTimer.Bits.Mask = 0;\r
916 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET, LvtTimer.Uint32);\r
917}\r
918\r
919/**\r
920 Disable the local APIC timer interrupt.\r
921**/\r
922VOID\r
923EFIAPI\r
924DisableApicTimerInterrupt (\r
925 VOID\r
926 )\r
927{\r
928 LOCAL_APIC_LVT_TIMER LvtTimer;\r
929\r
930 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
931 LvtTimer.Bits.Mask = 1;\r
932 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET, LvtTimer.Uint32);\r
933}\r
934\r
935/**\r
936 Get the local APIC timer interrupt state.\r
937\r
938 @retval TRUE The local APIC timer interrupt is enabled.\r
939 @retval FALSE The local APIC timer interrupt is disabled.\r
940**/\r
941BOOLEAN\r
942EFIAPI\r
943GetApicTimerInterruptState (\r
944 VOID\r
945 )\r
946{\r
947 LOCAL_APIC_LVT_TIMER LvtTimer;\r
948\r
949 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
950 return (BOOLEAN)(LvtTimer.Bits.Mask == 0);\r
951}\r
952\r
953/**\r
954 Send EOI to the local APIC.\r
955**/\r
956VOID\r
957EFIAPI\r
958SendApicEoi (\r
959 VOID\r
960 )\r
961{\r
962 WriteLocalApicReg (XAPIC_EOI_OFFSET, 0);\r
963}\r
964\r
5f867ad0 965/**\r
966 Get the 32-bit address that a device should use to send a Message Signaled \r
967 Interrupt (MSI) to the Local APIC of the currently executing processor.\r
968\r
969 @return 32-bit address used to send an MSI to the Local APIC.\r
970**/\r
971UINT32\r
972EFIAPI \r
973GetApicMsiAddress (\r
974 VOID\r
975 )\r
976{\r
977 LOCAL_APIC_MSI_ADDRESS MsiAddress;\r
978\r
979 //\r
980 // Return address for an MSI interrupt to be delivered only to the APIC ID \r
981 // of the currently executing processor.\r
982 //\r
983 MsiAddress.Uint32 = 0;\r
984 MsiAddress.Bits.BaseAddress = 0xFEE;\r
985 MsiAddress.Bits.DestinationId = GetApicId ();\r
986 return MsiAddress.Uint32;\r
987}\r
988 \r
989/**\r
990 Get the 64-bit data value that a device should use to send a Message Signaled \r
991 Interrupt (MSI) to the Local APIC of the currently executing processor.\r
992\r
993 If Vector is not in range 0x10..0xFE, then ASSERT().\r
994 If DeliveryMode is not supported, then ASSERT().\r
995 \r
996 @param Vector The 8-bit interrupt vector associated with the MSI. \r
997 Must be in the range 0x10..0xFE\r
998 @param DeliveryMode A 3-bit value that specifies how the recept of the MSI \r
999 is handled. The only supported values are:\r
1000 0: LOCAL_APIC_DELIVERY_MODE_FIXED\r
1001 1: LOCAL_APIC_DELIVERY_MODE_LOWEST_PRIORITY\r
1002 2: LOCAL_APIC_DELIVERY_MODE_SMI\r
1003 4: LOCAL_APIC_DELIVERY_MODE_NMI\r
1004 5: LOCAL_APIC_DELIVERY_MODE_INIT\r
1005 7: LOCAL_APIC_DELIVERY_MODE_EXTINT\r
1006 \r
1007 @param LevelTriggered TRUE specifies a level triggered interrupt. \r
1008 FALSE specifies an edge triggered interrupt.\r
1009 @param AssertionLevel Ignored if LevelTriggered is FALSE.\r
1010 TRUE specifies a level triggered interrupt that active \r
1011 when the interrupt line is asserted.\r
1012 FALSE specifies a level triggered interrupt that active \r
1013 when the interrupt line is deasserted.\r
1014\r
1015 @return 64-bit data value used to send an MSI to the Local APIC.\r
1016**/\r
1017UINT64\r
1018EFIAPI \r
1019GetApicMsiValue (\r
1020 IN UINT8 Vector,\r
1021 IN UINTN DeliveryMode,\r
1022 IN BOOLEAN LevelTriggered,\r
1023 IN BOOLEAN AssertionLevel\r
1024 )\r
1025{\r
1026 LOCAL_APIC_MSI_DATA MsiData;\r
1027\r
1028 ASSERT (Vector >= 0x10 && Vector <= 0xFE);\r
1029 ASSERT (DeliveryMode < 8 && DeliveryMode != 6 && DeliveryMode != 3);\r
1030 \r
1031 MsiData.Uint64 = 0;\r
1032 MsiData.Bits.Vector = Vector;\r
1033 MsiData.Bits.DeliveryMode = (UINT32)DeliveryMode;\r
1034 if (LevelTriggered) {\r
1035 MsiData.Bits.TriggerMode = 1;\r
1036 if (AssertionLevel) {\r
1037 MsiData.Bits.Level = 1;\r
1038 }\r
1039 }\r
1040 return MsiData.Uint64;\r
1041}\r
73152f19
LD
1042\r
1043/**\r
1044 Get Package ID/Core ID/Thread ID of a processor.\r
1045\r
1046 The algorithm assumes the target system has symmetry across physical\r
1047 package boundaries with respect to the number of logical processors\r
1048 per package, number of cores per package.\r
1049\r
1050 @param[in] InitialApicId Initial APIC ID of the target logical processor.\r
1051 @param[out] Package Returns the processor package ID.\r
1052 @param[out] Core Returns the processor core ID.\r
1053 @param[out] Thread Returns the processor thread ID.\r
1054**/\r
1055VOID\r
1c8ca9a0 1056EFIAPI\r
262128e5 1057GetProcessorLocationByApicId (\r
73152f19
LD
1058 IN UINT32 InitialApicId,\r
1059 OUT UINT32 *Package OPTIONAL,\r
1060 OUT UINT32 *Core OPTIONAL,\r
1061 OUT UINT32 *Thread OPTIONAL\r
1062 )\r
1063{\r
1064 BOOLEAN TopologyLeafSupported;\r
1065 UINTN ThreadBits;\r
1066 UINTN CoreBits;\r
1067 CPUID_VERSION_INFO_EBX VersionInfoEbx;\r
1068 CPUID_VERSION_INFO_EDX VersionInfoEdx;\r
1069 CPUID_CACHE_PARAMS_EAX CacheParamsEax;\r
1070 CPUID_EXTENDED_TOPOLOGY_EAX ExtendedTopologyEax;\r
1071 CPUID_EXTENDED_TOPOLOGY_EBX ExtendedTopologyEbx;\r
1072 CPUID_EXTENDED_TOPOLOGY_ECX ExtendedTopologyEcx;\r
1073 UINT32 MaxCpuIdIndex;\r
1074 UINT32 SubIndex;\r
1075 UINTN LevelType;\r
1076 UINT32 MaxLogicProcessorsPerPackage;\r
1077 UINT32 MaxCoresPerPackage;\r
1078\r
1079 //\r
1080 // Check if the processor is capable of supporting more than one logical processor.\r
1081 //\r
1082 AsmCpuid(CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);\r
1083 if (VersionInfoEdx.Bits.HTT == 0) {\r
1084 if (Thread != NULL) {\r
1085 *Thread = 0;\r
1086 }\r
1087 if (Core != NULL) {\r
1088 *Core = 0;\r
1089 }\r
1090 if (Package != NULL) {\r
1091 *Package = 0;\r
1092 }\r
1093 return;\r
1094 }\r
1095\r
1096 ThreadBits = 0;\r
1097 CoreBits = 0;\r
1098\r
1099 //\r
1100 // Assume three-level mapping of APIC ID: Package:Core:SMT.\r
1101 //\r
1102 TopologyLeafSupported = FALSE;\r
1103\r
1104 //\r
1105 // Get the max index of basic CPUID\r
1106 //\r
1107 AsmCpuid(CPUID_SIGNATURE, &MaxCpuIdIndex, NULL, NULL, NULL);\r
1108\r
1109 //\r
1110 // If the extended topology enumeration leaf is available, it\r
1111 // is the preferred mechanism for enumerating topology.\r
1112 //\r
1113 if (MaxCpuIdIndex >= CPUID_EXTENDED_TOPOLOGY) {\r
1114 AsmCpuidEx(\r
1115 CPUID_EXTENDED_TOPOLOGY,\r
1116 0,\r
1117 &ExtendedTopologyEax.Uint32,\r
1118 &ExtendedTopologyEbx.Uint32,\r
1119 &ExtendedTopologyEcx.Uint32,\r
1120 NULL\r
1121 );\r
1122 //\r
1123 // If CPUID.(EAX=0BH, ECX=0H):EBX returns zero and maximum input value for\r
1124 // basic CPUID information is greater than 0BH, then CPUID.0BH leaf is not\r
1125 // supported on that processor.\r
1126 //\r
1127 if (ExtendedTopologyEbx.Uint32 != 0) {\r
1128 TopologyLeafSupported = TRUE;\r
1129\r
1130 //\r
1131 // Sub-leaf index 0 (ECX= 0 as input) provides enumeration parameters to extract\r
1132 // the SMT sub-field of x2APIC ID.\r
1133 //\r
1134 LevelType = ExtendedTopologyEcx.Bits.LevelType;\r
1135 ASSERT(LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT);\r
1136 ThreadBits = ExtendedTopologyEax.Bits.ApicIdShift;\r
1137\r
1138 //\r
1139 // Software must not assume any "level type" encoding\r
1140 // value to be related to any sub-leaf index, except sub-leaf 0.\r
1141 //\r
1142 SubIndex = 1;\r
1143 do {\r
1144 AsmCpuidEx(\r
1145 CPUID_EXTENDED_TOPOLOGY,\r
1146 SubIndex,\r
1147 &ExtendedTopologyEax.Uint32,\r
1148 NULL,\r
1149 &ExtendedTopologyEcx.Uint32,\r
1150 NULL\r
1151 );\r
1152 LevelType = ExtendedTopologyEcx.Bits.LevelType;\r
1153 if (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_CORE) {\r
1154 CoreBits = ExtendedTopologyEax.Bits.ApicIdShift - ThreadBits;\r
1155 break;\r
1156 }\r
1157 SubIndex++;\r
1158 } while (LevelType != CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_INVALID);\r
1159 }\r
1160 }\r
1161\r
1162 if (!TopologyLeafSupported) {\r
1163 AsmCpuid(CPUID_VERSION_INFO, NULL, &VersionInfoEbx.Uint32, NULL, NULL);\r
1164 MaxLogicProcessorsPerPackage = VersionInfoEbx.Bits.MaximumAddressableIdsForLogicalProcessors;\r
1165 if (MaxCpuIdIndex >= CPUID_CACHE_PARAMS) {\r
1166 AsmCpuidEx(CPUID_CACHE_PARAMS, 0, &CacheParamsEax.Uint32, NULL, NULL, NULL);\r
1167 MaxCoresPerPackage = CacheParamsEax.Bits.MaximumAddressableIdsForLogicalProcessors + 1;\r
1168 }\r
1169 else {\r
1170 //\r
1171 // Must be a single-core processor.\r
1172 //\r
1173 MaxCoresPerPackage = 1;\r
1174 }\r
1175\r
1176 ThreadBits = (UINTN)(HighBitSet32(MaxLogicProcessorsPerPackage / MaxCoresPerPackage - 1) + 1);\r
1177 CoreBits = (UINTN)(HighBitSet32(MaxCoresPerPackage - 1) + 1); }\r
1178\r
1179 if (Thread != NULL) {\r
1180 *Thread = InitialApicId & ((1 << ThreadBits) - 1);\r
1181 }\r
1182 if (Core != NULL) {\r
1183 *Core = (InitialApicId >> ThreadBits) & ((1 << CoreBits) - 1);\r
1184 }\r
1185 if (Package != NULL) {\r
1186 *Package = (InitialApicId >> (ThreadBits + CoreBits));\r
1187 }\r
1188}\r