]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - UefiCpuPkg/Library/BaseXApicX2ApicLib/BaseXApicX2ApicLib.c
UefiCpuPkg/CpuDxe: Enable protection for newly added page table
[mirror_edk2.git] / UefiCpuPkg / Library / BaseXApicX2ApicLib / BaseXApicX2ApicLib.c
... / ...
CommitLineData
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
7 Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>\r
8 Copyright (c) 2017, AMD Inc. All rights reserved.<BR>\r
9\r
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
20#include <Register/Cpuid.h>\r
21#include <Register/Amd/Cpuid.h>\r
22#include <Register/Msr.h>\r
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
30#include <Library/PcdLib.h>\r
31\r
32//\r
33// Library internal functions\r
34//\r
35\r
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
52 AsmCpuid (CPUID_SIGNATURE, NULL, &RegEbx, &RegEcx, &RegEdx);\r
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
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
72 \r
73 AsmCpuid (1, &RegEax, NULL, NULL, NULL);\r
74 FamilyId = BitFieldRead32 (RegEax, 8, 11);\r
75 if (FamilyId == 0x04 || FamilyId == 0x05) {\r
76 //\r
77 // CPUs with a FamilyId of 0x04 or 0x05 do not support the \r
78 // Local APIC Base Address MSR\r
79 //\r
80 return FALSE;\r
81 }\r
82 return TRUE;\r
83}\r
84\r
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
97 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;\r
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
107 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
108 \r
109 return (UINTN)(LShiftU64 ((UINT64) ApicBaseMsr.Bits.ApicBaseHi, 32)) +\r
110 (((UINTN)ApicBaseMsr.Bits.ApicBase) << 12);\r
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
127 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;\r
128\r
129 ASSERT ((BaseAddress & (SIZE_4KB - 1)) == 0);\r
130\r
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
138 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
139\r
140 ApicBaseMsr.Bits.ApicBase = (UINT32) (BaseAddress >> 12);\r
141 ApicBaseMsr.Bits.ApicBaseHi = (UINT32) (RShiftU64((UINT64) BaseAddress, 32));\r
142\r
143 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
144}\r
145\r
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
170 return MmioRead32 (GetLocalApicBaseAddress() + MmioOffset);\r
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
213 MmioWrite32 (GetLocalApicBaseAddress() + MmioOffset, Value);\r
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
239 This function returns after the IPI has been accepted by the target processor. \r
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
252 UINTN LocalApciBaseAddress;\r
253 UINT32 IcrHigh;\r
254 BOOLEAN InterruptState;\r
255\r
256 //\r
257 // Legacy APIC or X2APIC?\r
258 //\r
259 if (GetApicMode () == LOCAL_APIC_MODE_XAPIC) {\r
260 ASSERT (ApicId <= 0xff);\r
261\r
262 InterruptState = SaveAndDisableInterrupts ();\r
263\r
264 //\r
265 // Get base address of this LAPIC\r
266 //\r
267 LocalApciBaseAddress = GetLocalApicBaseAddress();\r
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
285 MmioWrite32 (LocalApciBaseAddress + XAPIC_ICR_HIGH_OFFSET, ApicId << 24);\r
286 MmioWrite32 (LocalApciBaseAddress + XAPIC_ICR_LOW_OFFSET, IcrLow);\r
287\r
288 //\r
289 // Wait for DeliveryStatus clear again\r
290 //\r
291 do {\r
292 IcrLowReg.Uint32 = MmioRead32 (LocalApciBaseAddress + XAPIC_ICR_LOW_OFFSET);\r
293 } while (IcrLowReg.Bits.DeliveryStatus != 0);\r
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
302 } else {\r
303 //\r
304 // For x2APIC, A single MSR write to the Interrupt Command Register is required for dispatching an \r
305 // interrupt in x2APIC mode.\r
306 //\r
307 MsrValue = LShiftU64 ((UINT64) ApicId, 32) | IcrLow;\r
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
330 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;\r
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
338\r
339 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
340 //\r
341 // Local APIC should have been enabled\r
342 //\r
343 ASSERT (ApicBaseMsr.Bits.EN != 0);\r
344 if (ApicBaseMsr.Bits.EXTD != 0) {\r
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
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
361**/\r
362VOID\r
363EFIAPI\r
364SetApicMode (\r
365 IN UINTN ApicMode\r
366 )\r
367{\r
368 UINTN CurrentMode;\r
369 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;\r
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
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
384 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
385 ApicBaseMsr.Bits.EXTD = 1;\r
386 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
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
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
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
416 In xAPIC mode, the initial local APIC ID may be different from current APIC ID.\r
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
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
428 UINT32 ApicId;\r
429 UINT32 MaxCpuIdIndex;\r
430 UINT32 RegEbx;\r
431\r
432 if (GetApicMode () == LOCAL_APIC_MODE_XAPIC) {\r
433 //\r
434 // Get the max index of basic CPUID\r
435 //\r
436 AsmCpuid (CPUID_SIGNATURE, &MaxCpuIdIndex, NULL, NULL, NULL);\r
437 //\r
438 // If CPUID Leaf B is supported, \r
439 // And CPUID.0BH:EBX[15:0] reports a non-zero value,\r
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
444 AsmCpuidEx (CPUID_EXTENDED_TOPOLOGY, 0, NULL, &RegEbx, NULL, &ApicId);\r
445 if ((RegEbx & (BIT16 - 1)) != 0) {\r
446 return ApicId;\r
447 }\r
448 }\r
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
468 UINT32 InitApicId;\r
469\r
470 ApicId = ReadLocalApicReg (XAPIC_ID_OFFSET);\r
471 if (GetApicMode () == LOCAL_APIC_MODE_XAPIC) {\r
472 ApicId = ((InitApicId = GetInitialApicId ()) < 0x100) ? (ApicId >> 24) : InitApicId;\r
473 }\r
474\r
475 return ApicId;\r
476}\r
477\r
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
495 This function returns after the IPI has been accepted by the target processor. \r
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
519 This function returns after the IPI has been accepted by the target processors. \r
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
539/**\r
540 Send a SMI IPI to a specified target processor.\r
541\r
542 This function returns after the IPI has been accepted by the target processor. \r
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
563 This function returns after the IPI has been accepted by the target processors. \r
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
583 This function returns after the IPI has been accepted by the target processor. \r
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
604 This function returns after the IPI has been accepted by the target processors. \r
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
624 This function returns after the IPI has been accepted by the target processor. \r
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
646 MicroSecondDelay (PcdGet32(PcdCpuInitIpiDelayInMicroSeconds));\r
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
652 MicroSecondDelay (200);\r
653 SendIpi (IcrLow.Uint32, ApicId);\r
654}\r
655\r
656/**\r
657 Send an INIT-Start-up-Start-up IPI sequence to all processors excluding self.\r
658\r
659 This function returns after the IPI has been accepted by the target processors. \r
660\r
661 if StartupRoutine >= 1M, then ASSERT.\r
662 if StartupRoutine is not multiple of 4K, then ASSERT.\r
663\r
664 @param StartupRoutine Points to a start-up routine which is below 1M physical\r
665 address and 4K aligned.\r
666**/\r
667VOID\r
668EFIAPI\r
669SendInitSipiSipiAllExcludingSelf (\r
670 IN UINT32 StartupRoutine\r
671 )\r
672{\r
673 LOCAL_APIC_ICR_LOW IcrLow;\r
674\r
675 ASSERT (StartupRoutine < 0x100000);\r
676 ASSERT ((StartupRoutine & 0xfff) == 0);\r
677\r
678 SendInitIpiAllExcludingSelf ();\r
679 MicroSecondDelay (PcdGet32(PcdCpuInitIpiDelayInMicroSeconds));\r
680 IcrLow.Uint32 = 0;\r
681 IcrLow.Bits.Vector = (StartupRoutine >> 12);\r
682 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_STARTUP;\r
683 IcrLow.Bits.Level = 1;\r
684 IcrLow.Bits.DestinationShorthand = LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF;\r
685 SendIpi (IcrLow.Uint32, 0);\r
686 MicroSecondDelay (200);\r
687 SendIpi (IcrLow.Uint32, 0);\r
688}\r
689\r
690/**\r
691 Initialize the state of the SoftwareEnable bit in the Local APIC\r
692 Spurious Interrupt Vector register.\r
693\r
694 @param Enable If TRUE, then set SoftwareEnable to 1\r
695 If FALSE, then set SoftwareEnable to 0.\r
696\r
697**/\r
698VOID\r
699EFIAPI\r
700InitializeLocalApicSoftwareEnable (\r
701 IN BOOLEAN Enable\r
702 )\r
703{\r
704 LOCAL_APIC_SVR Svr;\r
705\r
706 //\r
707 // Set local APIC software-enabled bit.\r
708 //\r
709 Svr.Uint32 = ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET);\r
710 if (Enable) {\r
711 if (Svr.Bits.SoftwareEnable == 0) {\r
712 Svr.Bits.SoftwareEnable = 1;\r
713 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
714 }\r
715 } else {\r
716 if (Svr.Bits.SoftwareEnable == 1) {\r
717 Svr.Bits.SoftwareEnable = 0;\r
718 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
719 }\r
720 }\r
721}\r
722\r
723/**\r
724 Programming Virtual Wire Mode.\r
725\r
726 This function programs the local APIC for virtual wire mode following\r
727 the example described in chapter A.3 of the MP 1.4 spec.\r
728\r
729 IOxAPIC is not involved in this type of virtual wire mode.\r
730**/\r
731VOID\r
732EFIAPI\r
733ProgramVirtualWireMode (\r
734 VOID\r
735 )\r
736{\r
737 LOCAL_APIC_SVR Svr;\r
738 LOCAL_APIC_LVT_LINT Lint;\r
739\r
740 //\r
741 // Enable the APIC via SVR and set the spurious interrupt to use Int 00F.\r
742 //\r
743 Svr.Uint32 = ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET);\r
744 Svr.Bits.SpuriousVector = 0xf;\r
745 Svr.Bits.SoftwareEnable = 1;\r
746 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
747\r
748 //\r
749 // Program the LINT0 vector entry as ExtInt. Not masked, edge, active high.\r
750 //\r
751 Lint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET);\r
752 Lint.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_EXTINT;\r
753 Lint.Bits.InputPinPolarity = 0;\r
754 Lint.Bits.TriggerMode = 0;\r
755 Lint.Bits.Mask = 0;\r
756 WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET, Lint.Uint32);\r
757\r
758 //\r
759 // Program the LINT0 vector entry as NMI. Not masked, edge, active high.\r
760 //\r
761 Lint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET);\r
762 Lint.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_NMI;\r
763 Lint.Bits.InputPinPolarity = 0;\r
764 Lint.Bits.TriggerMode = 0;\r
765 Lint.Bits.Mask = 0;\r
766 WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET, Lint.Uint32);\r
767}\r
768\r
769/**\r
770 Disable LINT0 & LINT1 interrupts.\r
771\r
772 This function sets the mask flag in the LVT LINT0 & LINT1 registers.\r
773**/\r
774VOID\r
775EFIAPI\r
776DisableLvtInterrupts (\r
777 VOID\r
778 )\r
779{\r
780 LOCAL_APIC_LVT_LINT LvtLint;\r
781\r
782 LvtLint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET);\r
783 LvtLint.Bits.Mask = 1;\r
784 WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET, LvtLint.Uint32);\r
785\r
786 LvtLint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET);\r
787 LvtLint.Bits.Mask = 1;\r
788 WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET, LvtLint.Uint32);\r
789}\r
790\r
791/**\r
792 Read the initial count value from the init-count register.\r
793\r
794 @return The initial count value read from the init-count register.\r
795**/\r
796UINT32\r
797EFIAPI\r
798GetApicTimerInitCount (\r
799 VOID\r
800 )\r
801{\r
802 return ReadLocalApicReg (XAPIC_TIMER_INIT_COUNT_OFFSET);\r
803}\r
804\r
805/**\r
806 Read the current count value from the current-count register.\r
807\r
808 @return The current count value read from the current-count register.\r
809**/\r
810UINT32\r
811EFIAPI\r
812GetApicTimerCurrentCount (\r
813 VOID\r
814 )\r
815{\r
816 return ReadLocalApicReg (XAPIC_TIMER_CURRENT_COUNT_OFFSET);\r
817}\r
818\r
819/**\r
820 Initialize the local APIC timer.\r
821\r
822 The local APIC timer is initialized and enabled.\r
823\r
824 @param DivideValue The divide value for the DCR. It is one of 1,2,4,8,16,32,64,128.\r
825 If it is 0, then use the current divide value in the DCR.\r
826 @param InitCount The initial count value.\r
827 @param PeriodicMode If TRUE, timer mode is peridoic. Othewise, timer mode is one-shot.\r
828 @param Vector The timer interrupt vector number.\r
829**/\r
830VOID\r
831EFIAPI\r
832InitializeApicTimer (\r
833 IN UINTN DivideValue,\r
834 IN UINT32 InitCount,\r
835 IN BOOLEAN PeriodicMode,\r
836 IN UINT8 Vector\r
837 )\r
838{\r
839 LOCAL_APIC_DCR Dcr;\r
840 LOCAL_APIC_LVT_TIMER LvtTimer;\r
841 UINT32 Divisor;\r
842\r
843 //\r
844 // Ensure local APIC is in software-enabled state.\r
845 //\r
846 InitializeLocalApicSoftwareEnable (TRUE);\r
847\r
848 //\r
849 // Program init-count register.\r
850 //\r
851 WriteLocalApicReg (XAPIC_TIMER_INIT_COUNT_OFFSET, InitCount);\r
852\r
853 if (DivideValue != 0) {\r
854 ASSERT (DivideValue <= 128);\r
855 ASSERT (DivideValue == GetPowerOfTwo32((UINT32)DivideValue));\r
856 Divisor = (UINT32)((HighBitSet32 ((UINT32)DivideValue) - 1) & 0x7);\r
857\r
858 Dcr.Uint32 = ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET);\r
859 Dcr.Bits.DivideValue1 = (Divisor & 0x3);\r
860 Dcr.Bits.DivideValue2 = (Divisor >> 2);\r
861 WriteLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET, Dcr.Uint32); \r
862 }\r
863\r
864 //\r
865 // Enable APIC timer interrupt with specified timer mode.\r
866 //\r
867 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
868 if (PeriodicMode) {\r
869 LvtTimer.Bits.TimerMode = 1;\r
870 } else {\r
871 LvtTimer.Bits.TimerMode = 0;\r
872 }\r
873 LvtTimer.Bits.Mask = 0;\r
874 LvtTimer.Bits.Vector = Vector;\r
875 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET, LvtTimer.Uint32);\r
876}\r
877\r
878/**\r
879 Get the state of the local APIC timer.\r
880\r
881 This function will ASSERT if the local APIC is not software enabled.\r
882\r
883 @param DivideValue Return the divide value for the DCR. It is one of 1,2,4,8,16,32,64,128.\r
884 @param PeriodicMode Return the timer mode. If TRUE, timer mode is peridoic. Othewise, timer mode is one-shot.\r
885 @param Vector Return the timer interrupt vector number.\r
886**/\r
887VOID\r
888EFIAPI\r
889GetApicTimerState (\r
890 OUT UINTN *DivideValue OPTIONAL,\r
891 OUT BOOLEAN *PeriodicMode OPTIONAL,\r
892 OUT UINT8 *Vector OPTIONAL\r
893 )\r
894{\r
895 UINT32 Divisor;\r
896 LOCAL_APIC_DCR Dcr;\r
897 LOCAL_APIC_LVT_TIMER LvtTimer;\r
898\r
899 //\r
900 // Check the APIC Software Enable/Disable bit (bit 8) in Spurious-Interrupt\r
901 // Vector Register.\r
902 // This bit will be 1, if local APIC is software enabled.\r
903 //\r
904 ASSERT ((ReadLocalApicReg(XAPIC_SPURIOUS_VECTOR_OFFSET) & BIT8) != 0);\r
905\r
906 if (DivideValue != NULL) {\r
907 Dcr.Uint32 = ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET);\r
908 Divisor = Dcr.Bits.DivideValue1 | (Dcr.Bits.DivideValue2 << 2);\r
909 Divisor = (Divisor + 1) & 0x7;\r
910 *DivideValue = ((UINTN)1) << Divisor;\r
911 }\r
912\r
913 if (PeriodicMode != NULL || Vector != NULL) {\r
914 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
915 if (PeriodicMode != NULL) {\r
916 if (LvtTimer.Bits.TimerMode == 1) {\r
917 *PeriodicMode = TRUE;\r
918 } else {\r
919 *PeriodicMode = FALSE;\r
920 }\r
921 }\r
922 if (Vector != NULL) {\r
923 *Vector = (UINT8) LvtTimer.Bits.Vector;\r
924 }\r
925 }\r
926}\r
927\r
928/**\r
929 Enable the local APIC timer interrupt.\r
930**/\r
931VOID\r
932EFIAPI\r
933EnableApicTimerInterrupt (\r
934 VOID\r
935 )\r
936{\r
937 LOCAL_APIC_LVT_TIMER LvtTimer;\r
938\r
939 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
940 LvtTimer.Bits.Mask = 0;\r
941 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET, LvtTimer.Uint32);\r
942}\r
943\r
944/**\r
945 Disable the local APIC timer interrupt.\r
946**/\r
947VOID\r
948EFIAPI\r
949DisableApicTimerInterrupt (\r
950 VOID\r
951 )\r
952{\r
953 LOCAL_APIC_LVT_TIMER LvtTimer;\r
954\r
955 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
956 LvtTimer.Bits.Mask = 1;\r
957 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET, LvtTimer.Uint32);\r
958}\r
959\r
960/**\r
961 Get the local APIC timer interrupt state.\r
962\r
963 @retval TRUE The local APIC timer interrupt is enabled.\r
964 @retval FALSE The local APIC timer interrupt is disabled.\r
965**/\r
966BOOLEAN\r
967EFIAPI\r
968GetApicTimerInterruptState (\r
969 VOID\r
970 )\r
971{\r
972 LOCAL_APIC_LVT_TIMER LvtTimer;\r
973\r
974 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
975 return (BOOLEAN)(LvtTimer.Bits.Mask == 0);\r
976}\r
977\r
978/**\r
979 Send EOI to the local APIC.\r
980**/\r
981VOID\r
982EFIAPI\r
983SendApicEoi (\r
984 VOID\r
985 )\r
986{\r
987 WriteLocalApicReg (XAPIC_EOI_OFFSET, 0);\r
988}\r
989\r
990/**\r
991 Get the 32-bit address that a device should use to send a Message Signaled \r
992 Interrupt (MSI) to the Local APIC of the currently executing processor.\r
993\r
994 @return 32-bit address used to send an MSI to the Local APIC.\r
995**/\r
996UINT32\r
997EFIAPI \r
998GetApicMsiAddress (\r
999 VOID\r
1000 )\r
1001{\r
1002 LOCAL_APIC_MSI_ADDRESS MsiAddress;\r
1003\r
1004 //\r
1005 // Return address for an MSI interrupt to be delivered only to the APIC ID \r
1006 // of the currently executing processor.\r
1007 //\r
1008 MsiAddress.Uint32 = 0;\r
1009 MsiAddress.Bits.BaseAddress = 0xFEE;\r
1010 MsiAddress.Bits.DestinationId = GetApicId ();\r
1011 return MsiAddress.Uint32;\r
1012}\r
1013 \r
1014/**\r
1015 Get the 64-bit data value that a device should use to send a Message Signaled \r
1016 Interrupt (MSI) to the Local APIC of the currently executing processor.\r
1017\r
1018 If Vector is not in range 0x10..0xFE, then ASSERT().\r
1019 If DeliveryMode is not supported, then ASSERT().\r
1020 \r
1021 @param Vector The 8-bit interrupt vector associated with the MSI. \r
1022 Must be in the range 0x10..0xFE\r
1023 @param DeliveryMode A 3-bit value that specifies how the recept of the MSI \r
1024 is handled. The only supported values are:\r
1025 0: LOCAL_APIC_DELIVERY_MODE_FIXED\r
1026 1: LOCAL_APIC_DELIVERY_MODE_LOWEST_PRIORITY\r
1027 2: LOCAL_APIC_DELIVERY_MODE_SMI\r
1028 4: LOCAL_APIC_DELIVERY_MODE_NMI\r
1029 5: LOCAL_APIC_DELIVERY_MODE_INIT\r
1030 7: LOCAL_APIC_DELIVERY_MODE_EXTINT\r
1031 \r
1032 @param LevelTriggered TRUE specifies a level triggered interrupt. \r
1033 FALSE specifies an edge triggered interrupt.\r
1034 @param AssertionLevel Ignored if LevelTriggered is FALSE.\r
1035 TRUE specifies a level triggered interrupt that active \r
1036 when the interrupt line is asserted.\r
1037 FALSE specifies a level triggered interrupt that active \r
1038 when the interrupt line is deasserted.\r
1039\r
1040 @return 64-bit data value used to send an MSI to the Local APIC.\r
1041**/\r
1042UINT64\r
1043EFIAPI \r
1044GetApicMsiValue (\r
1045 IN UINT8 Vector,\r
1046 IN UINTN DeliveryMode,\r
1047 IN BOOLEAN LevelTriggered,\r
1048 IN BOOLEAN AssertionLevel\r
1049 )\r
1050{\r
1051 LOCAL_APIC_MSI_DATA MsiData;\r
1052\r
1053 ASSERT (Vector >= 0x10 && Vector <= 0xFE);\r
1054 ASSERT (DeliveryMode < 8 && DeliveryMode != 6 && DeliveryMode != 3);\r
1055 \r
1056 MsiData.Uint64 = 0;\r
1057 MsiData.Bits.Vector = Vector;\r
1058 MsiData.Bits.DeliveryMode = (UINT32)DeliveryMode;\r
1059 if (LevelTriggered) {\r
1060 MsiData.Bits.TriggerMode = 1;\r
1061 if (AssertionLevel) {\r
1062 MsiData.Bits.Level = 1;\r
1063 }\r
1064 }\r
1065 return MsiData.Uint64;\r
1066}\r
1067\r
1068/**\r
1069 Get Package ID/Core ID/Thread ID of a processor.\r
1070\r
1071 The algorithm assumes the target system has symmetry across physical\r
1072 package boundaries with respect to the number of logical processors\r
1073 per package, number of cores per package.\r
1074\r
1075 @param[in] InitialApicId Initial APIC ID of the target logical processor.\r
1076 @param[out] Package Returns the processor package ID.\r
1077 @param[out] Core Returns the processor core ID.\r
1078 @param[out] Thread Returns the processor thread ID.\r
1079**/\r
1080VOID\r
1081EFIAPI\r
1082GetProcessorLocationByApicId (\r
1083 IN UINT32 InitialApicId,\r
1084 OUT UINT32 *Package OPTIONAL,\r
1085 OUT UINT32 *Core OPTIONAL,\r
1086 OUT UINT32 *Thread OPTIONAL\r
1087 )\r
1088{\r
1089 BOOLEAN TopologyLeafSupported;\r
1090 CPUID_VERSION_INFO_EBX VersionInfoEbx;\r
1091 CPUID_VERSION_INFO_EDX VersionInfoEdx;\r
1092 CPUID_CACHE_PARAMS_EAX CacheParamsEax;\r
1093 CPUID_EXTENDED_TOPOLOGY_EAX ExtendedTopologyEax;\r
1094 CPUID_EXTENDED_TOPOLOGY_EBX ExtendedTopologyEbx;\r
1095 CPUID_EXTENDED_TOPOLOGY_ECX ExtendedTopologyEcx;\r
1096 CPUID_AMD_EXTENDED_CPU_SIG_ECX AmdExtendedCpuSigEcx;\r
1097 CPUID_AMD_PROCESSOR_TOPOLOGY_EBX AmdProcessorTopologyEbx;\r
1098 CPUID_AMD_VIR_PHY_ADDRESS_SIZE_ECX AmdVirPhyAddressSizeEcx;\r
1099 UINT32 MaxStandardCpuIdIndex;\r
1100 UINT32 MaxExtendedCpuIdIndex;\r
1101 UINT32 SubIndex;\r
1102 UINTN LevelType;\r
1103 UINT32 MaxLogicProcessorsPerPackage;\r
1104 UINT32 MaxCoresPerPackage;\r
1105 UINTN ThreadBits;\r
1106 UINTN CoreBits;\r
1107\r
1108 //\r
1109 // Check if the processor is capable of supporting more than one logical processor.\r
1110 //\r
1111 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);\r
1112 if (VersionInfoEdx.Bits.HTT == 0) {\r
1113 if (Thread != NULL) {\r
1114 *Thread = 0;\r
1115 }\r
1116 if (Core != NULL) {\r
1117 *Core = 0;\r
1118 }\r
1119 if (Package != NULL) {\r
1120 *Package = 0;\r
1121 }\r
1122 return;\r
1123 }\r
1124\r
1125 //\r
1126 // Assume three-level mapping of APIC ID: Package|Core|Thread.\r
1127 //\r
1128 ThreadBits = 0;\r
1129 CoreBits = 0;\r
1130\r
1131 //\r
1132 // Get max index of CPUID\r
1133 //\r
1134 AsmCpuid (CPUID_SIGNATURE, &MaxStandardCpuIdIndex, NULL, NULL, NULL);\r
1135 AsmCpuid (CPUID_EXTENDED_FUNCTION, &MaxExtendedCpuIdIndex, NULL, NULL, NULL);\r
1136\r
1137 //\r
1138 // If the extended topology enumeration leaf is available, it\r
1139 // is the preferred mechanism for enumerating topology.\r
1140 //\r
1141 TopologyLeafSupported = FALSE;\r
1142 if (MaxStandardCpuIdIndex >= CPUID_EXTENDED_TOPOLOGY) {\r
1143 AsmCpuidEx(\r
1144 CPUID_EXTENDED_TOPOLOGY,\r
1145 0,\r
1146 &ExtendedTopologyEax.Uint32,\r
1147 &ExtendedTopologyEbx.Uint32,\r
1148 &ExtendedTopologyEcx.Uint32,\r
1149 NULL\r
1150 );\r
1151 //\r
1152 // If CPUID.(EAX=0BH, ECX=0H):EBX returns zero and maximum input value for\r
1153 // basic CPUID information is greater than 0BH, then CPUID.0BH leaf is not\r
1154 // supported on that processor.\r
1155 //\r
1156 if (ExtendedTopologyEbx.Uint32 != 0) {\r
1157 TopologyLeafSupported = TRUE;\r
1158\r
1159 //\r
1160 // Sub-leaf index 0 (ECX= 0 as input) provides enumeration parameters to extract\r
1161 // the SMT sub-field of x2APIC ID.\r
1162 //\r
1163 LevelType = ExtendedTopologyEcx.Bits.LevelType;\r
1164 ASSERT (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT);\r
1165 ThreadBits = ExtendedTopologyEax.Bits.ApicIdShift;\r
1166\r
1167 //\r
1168 // Software must not assume any "level type" encoding\r
1169 // value to be related to any sub-leaf index, except sub-leaf 0.\r
1170 //\r
1171 SubIndex = 1;\r
1172 do {\r
1173 AsmCpuidEx (\r
1174 CPUID_EXTENDED_TOPOLOGY,\r
1175 SubIndex,\r
1176 &ExtendedTopologyEax.Uint32,\r
1177 NULL,\r
1178 &ExtendedTopologyEcx.Uint32,\r
1179 NULL\r
1180 );\r
1181 LevelType = ExtendedTopologyEcx.Bits.LevelType;\r
1182 if (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_CORE) {\r
1183 CoreBits = ExtendedTopologyEax.Bits.ApicIdShift - ThreadBits;\r
1184 break;\r
1185 }\r
1186 SubIndex++;\r
1187 } while (LevelType != CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_INVALID);\r
1188 }\r
1189 }\r
1190\r
1191 if (!TopologyLeafSupported) {\r
1192 //\r
1193 // Get logical processor count\r
1194 //\r
1195 AsmCpuid (CPUID_VERSION_INFO, NULL, &VersionInfoEbx.Uint32, NULL, NULL);\r
1196 MaxLogicProcessorsPerPackage = VersionInfoEbx.Bits.MaximumAddressableIdsForLogicalProcessors;\r
1197\r
1198 //\r
1199 // Assume single-core processor\r
1200 //\r
1201 MaxCoresPerPackage = 1;\r
1202\r
1203 //\r
1204 // Check for topology extensions on AMD processor\r
1205 //\r
1206 if (StandardSignatureIsAuthenticAMD()) {\r
1207 if (MaxExtendedCpuIdIndex >= CPUID_AMD_PROCESSOR_TOPOLOGY) {\r
1208 AsmCpuid (CPUID_EXTENDED_CPU_SIG, NULL, NULL, &AmdExtendedCpuSigEcx.Uint32, NULL);\r
1209 if (AmdExtendedCpuSigEcx.Bits.TopologyExtensions != 0) {\r
1210 //\r
1211 // Account for max possible thread count to decode ApicId\r
1212 //\r
1213 AsmCpuid (CPUID_VIR_PHY_ADDRESS_SIZE, NULL, NULL, &AmdVirPhyAddressSizeEcx.Uint32, NULL);\r
1214 MaxLogicProcessorsPerPackage = 1 << AmdVirPhyAddressSizeEcx.Bits.ApicIdCoreIdSize;\r
1215\r
1216 //\r
1217 // Get cores per processor package\r
1218 //\r
1219 AsmCpuid (CPUID_AMD_PROCESSOR_TOPOLOGY, NULL, &AmdProcessorTopologyEbx.Uint32, NULL, NULL);\r
1220 MaxCoresPerPackage = MaxLogicProcessorsPerPackage / (AmdProcessorTopologyEbx.Bits.ThreadsPerCore + 1);\r
1221 }\r
1222 }\r
1223 }\r
1224 else {\r
1225 //\r
1226 // Extract core count based on CACHE information\r
1227 //\r
1228 if (MaxStandardCpuIdIndex >= CPUID_CACHE_PARAMS) {\r
1229 AsmCpuidEx (CPUID_CACHE_PARAMS, 0, &CacheParamsEax.Uint32, NULL, NULL, NULL);\r
1230 if (CacheParamsEax.Uint32 != 0) {\r
1231 MaxCoresPerPackage = CacheParamsEax.Bits.MaximumAddressableIdsForLogicalProcessors + 1;\r
1232 }\r
1233 }\r
1234 }\r
1235\r
1236 ThreadBits = (UINTN)(HighBitSet32(MaxLogicProcessorsPerPackage / MaxCoresPerPackage - 1) + 1);\r
1237 CoreBits = (UINTN)(HighBitSet32(MaxCoresPerPackage - 1) + 1);\r
1238 }\r
1239\r
1240 if (Thread != NULL) {\r
1241 *Thread = InitialApicId & ((1 << ThreadBits) - 1);\r
1242 }\r
1243 if (Core != NULL) {\r
1244 *Core = (InitialApicId >> ThreadBits) & ((1 << CoreBits) - 1);\r
1245 }\r
1246 if (Package != NULL) {\r
1247 *Package = (InitialApicId >> (ThreadBits + CoreBits));\r
1248 }\r
1249}\r