]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - UefiCpuPkg/Library/BaseXApicX2ApicLib/BaseXApicX2ApicLib.c
StandaloneMmPkg: Replace BSD License with BSD+Patent License
[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 - 2019, 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 if (!StandardSignatureIsAuthenticAMD ()) {\r
653 MicroSecondDelay (200);\r
654 SendIpi (IcrLow.Uint32, ApicId);\r
655 }\r
656}\r
657\r
658/**\r
659 Send an INIT-Start-up-Start-up IPI sequence to all processors excluding self.\r
660\r
661 This function returns after the IPI has been accepted by the target processors.\r
662\r
663 if StartupRoutine >= 1M, then ASSERT.\r
664 if StartupRoutine is not multiple of 4K, then ASSERT.\r
665\r
666 @param StartupRoutine Points to a start-up routine which is below 1M physical\r
667 address and 4K aligned.\r
668**/\r
669VOID\r
670EFIAPI\r
671SendInitSipiSipiAllExcludingSelf (\r
672 IN UINT32 StartupRoutine\r
673 )\r
674{\r
675 LOCAL_APIC_ICR_LOW IcrLow;\r
676\r
677 ASSERT (StartupRoutine < 0x100000);\r
678 ASSERT ((StartupRoutine & 0xfff) == 0);\r
679\r
680 SendInitIpiAllExcludingSelf ();\r
681 MicroSecondDelay (PcdGet32(PcdCpuInitIpiDelayInMicroSeconds));\r
682 IcrLow.Uint32 = 0;\r
683 IcrLow.Bits.Vector = (StartupRoutine >> 12);\r
684 IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_STARTUP;\r
685 IcrLow.Bits.Level = 1;\r
686 IcrLow.Bits.DestinationShorthand = LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF;\r
687 SendIpi (IcrLow.Uint32, 0);\r
688 if (!StandardSignatureIsAuthenticAMD ()) {\r
689 MicroSecondDelay (200);\r
690 SendIpi (IcrLow.Uint32, 0);\r
691 }\r
692}\r
693\r
694/**\r
695 Initialize the state of the SoftwareEnable bit in the Local APIC\r
696 Spurious Interrupt Vector register.\r
697\r
698 @param Enable If TRUE, then set SoftwareEnable to 1\r
699 If FALSE, then set SoftwareEnable to 0.\r
700\r
701**/\r
702VOID\r
703EFIAPI\r
704InitializeLocalApicSoftwareEnable (\r
705 IN BOOLEAN Enable\r
706 )\r
707{\r
708 LOCAL_APIC_SVR Svr;\r
709\r
710 //\r
711 // Set local APIC software-enabled bit.\r
712 //\r
713 Svr.Uint32 = ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET);\r
714 if (Enable) {\r
715 if (Svr.Bits.SoftwareEnable == 0) {\r
716 Svr.Bits.SoftwareEnable = 1;\r
717 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
718 }\r
719 } else {\r
720 if (Svr.Bits.SoftwareEnable == 1) {\r
721 Svr.Bits.SoftwareEnable = 0;\r
722 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
723 }\r
724 }\r
725}\r
726\r
727/**\r
728 Programming Virtual Wire Mode.\r
729\r
730 This function programs the local APIC for virtual wire mode following\r
731 the example described in chapter A.3 of the MP 1.4 spec.\r
732\r
733 IOxAPIC is not involved in this type of virtual wire mode.\r
734**/\r
735VOID\r
736EFIAPI\r
737ProgramVirtualWireMode (\r
738 VOID\r
739 )\r
740{\r
741 LOCAL_APIC_SVR Svr;\r
742 LOCAL_APIC_LVT_LINT Lint;\r
743\r
744 //\r
745 // Enable the APIC via SVR and set the spurious interrupt to use Int 00F.\r
746 //\r
747 Svr.Uint32 = ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET);\r
748 Svr.Bits.SpuriousVector = 0xf;\r
749 Svr.Bits.SoftwareEnable = 1;\r
750 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
751\r
752 //\r
753 // Program the LINT0 vector entry as ExtInt. Not masked, edge, active high.\r
754 //\r
755 Lint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET);\r
756 Lint.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_EXTINT;\r
757 Lint.Bits.InputPinPolarity = 0;\r
758 Lint.Bits.TriggerMode = 0;\r
759 Lint.Bits.Mask = 0;\r
760 WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET, Lint.Uint32);\r
761\r
762 //\r
763 // Program the LINT0 vector entry as NMI. Not masked, edge, active high.\r
764 //\r
765 Lint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET);\r
766 Lint.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_NMI;\r
767 Lint.Bits.InputPinPolarity = 0;\r
768 Lint.Bits.TriggerMode = 0;\r
769 Lint.Bits.Mask = 0;\r
770 WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET, Lint.Uint32);\r
771}\r
772\r
773/**\r
774 Disable LINT0 & LINT1 interrupts.\r
775\r
776 This function sets the mask flag in the LVT LINT0 & LINT1 registers.\r
777**/\r
778VOID\r
779EFIAPI\r
780DisableLvtInterrupts (\r
781 VOID\r
782 )\r
783{\r
784 LOCAL_APIC_LVT_LINT LvtLint;\r
785\r
786 LvtLint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET);\r
787 LvtLint.Bits.Mask = 1;\r
788 WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET, LvtLint.Uint32);\r
789\r
790 LvtLint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET);\r
791 LvtLint.Bits.Mask = 1;\r
792 WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET, LvtLint.Uint32);\r
793}\r
794\r
795/**\r
796 Read the initial count value from the init-count register.\r
797\r
798 @return The initial count value read from the init-count register.\r
799**/\r
800UINT32\r
801EFIAPI\r
802GetApicTimerInitCount (\r
803 VOID\r
804 )\r
805{\r
806 return ReadLocalApicReg (XAPIC_TIMER_INIT_COUNT_OFFSET);\r
807}\r
808\r
809/**\r
810 Read the current count value from the current-count register.\r
811\r
812 @return The current count value read from the current-count register.\r
813**/\r
814UINT32\r
815EFIAPI\r
816GetApicTimerCurrentCount (\r
817 VOID\r
818 )\r
819{\r
820 return ReadLocalApicReg (XAPIC_TIMER_CURRENT_COUNT_OFFSET);\r
821}\r
822\r
823/**\r
824 Initialize the local APIC timer.\r
825\r
826 The local APIC timer is initialized and enabled.\r
827\r
828 @param DivideValue The divide value for the DCR. It is one of 1,2,4,8,16,32,64,128.\r
829 If it is 0, then use the current divide value in the DCR.\r
830 @param InitCount The initial count value.\r
831 @param PeriodicMode If TRUE, timer mode is peridoic. Othewise, timer mode is one-shot.\r
832 @param Vector The timer interrupt vector number.\r
833**/\r
834VOID\r
835EFIAPI\r
836InitializeApicTimer (\r
837 IN UINTN DivideValue,\r
838 IN UINT32 InitCount,\r
839 IN BOOLEAN PeriodicMode,\r
840 IN UINT8 Vector\r
841 )\r
842{\r
843 LOCAL_APIC_DCR Dcr;\r
844 LOCAL_APIC_LVT_TIMER LvtTimer;\r
845 UINT32 Divisor;\r
846\r
847 //\r
848 // Ensure local APIC is in software-enabled state.\r
849 //\r
850 InitializeLocalApicSoftwareEnable (TRUE);\r
851\r
852 //\r
853 // Program init-count register.\r
854 //\r
855 WriteLocalApicReg (XAPIC_TIMER_INIT_COUNT_OFFSET, InitCount);\r
856\r
857 if (DivideValue != 0) {\r
858 ASSERT (DivideValue <= 128);\r
859 ASSERT (DivideValue == GetPowerOfTwo32((UINT32)DivideValue));\r
860 Divisor = (UINT32)((HighBitSet32 ((UINT32)DivideValue) - 1) & 0x7);\r
861\r
862 Dcr.Uint32 = ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET);\r
863 Dcr.Bits.DivideValue1 = (Divisor & 0x3);\r
864 Dcr.Bits.DivideValue2 = (Divisor >> 2);\r
865 WriteLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET, Dcr.Uint32);\r
866 }\r
867\r
868 //\r
869 // Enable APIC timer interrupt with specified timer mode.\r
870 //\r
871 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
872 if (PeriodicMode) {\r
873 LvtTimer.Bits.TimerMode = 1;\r
874 } else {\r
875 LvtTimer.Bits.TimerMode = 0;\r
876 }\r
877 LvtTimer.Bits.Mask = 0;\r
878 LvtTimer.Bits.Vector = Vector;\r
879 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET, LvtTimer.Uint32);\r
880}\r
881\r
882/**\r
883 Get the state of the local APIC timer.\r
884\r
885 This function will ASSERT if the local APIC is not software enabled.\r
886\r
887 @param DivideValue Return the divide value for the DCR. It is one of 1,2,4,8,16,32,64,128.\r
888 @param PeriodicMode Return the timer mode. If TRUE, timer mode is peridoic. Othewise, timer mode is one-shot.\r
889 @param Vector Return the timer interrupt vector number.\r
890**/\r
891VOID\r
892EFIAPI\r
893GetApicTimerState (\r
894 OUT UINTN *DivideValue OPTIONAL,\r
895 OUT BOOLEAN *PeriodicMode OPTIONAL,\r
896 OUT UINT8 *Vector OPTIONAL\r
897 )\r
898{\r
899 UINT32 Divisor;\r
900 LOCAL_APIC_DCR Dcr;\r
901 LOCAL_APIC_LVT_TIMER LvtTimer;\r
902\r
903 //\r
904 // Check the APIC Software Enable/Disable bit (bit 8) in Spurious-Interrupt\r
905 // Vector Register.\r
906 // This bit will be 1, if local APIC is software enabled.\r
907 //\r
908 ASSERT ((ReadLocalApicReg(XAPIC_SPURIOUS_VECTOR_OFFSET) & BIT8) != 0);\r
909\r
910 if (DivideValue != NULL) {\r
911 Dcr.Uint32 = ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET);\r
912 Divisor = Dcr.Bits.DivideValue1 | (Dcr.Bits.DivideValue2 << 2);\r
913 Divisor = (Divisor + 1) & 0x7;\r
914 *DivideValue = ((UINTN)1) << Divisor;\r
915 }\r
916\r
917 if (PeriodicMode != NULL || Vector != NULL) {\r
918 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
919 if (PeriodicMode != NULL) {\r
920 if (LvtTimer.Bits.TimerMode == 1) {\r
921 *PeriodicMode = TRUE;\r
922 } else {\r
923 *PeriodicMode = FALSE;\r
924 }\r
925 }\r
926 if (Vector != NULL) {\r
927 *Vector = (UINT8) LvtTimer.Bits.Vector;\r
928 }\r
929 }\r
930}\r
931\r
932/**\r
933 Enable the local APIC timer interrupt.\r
934**/\r
935VOID\r
936EFIAPI\r
937EnableApicTimerInterrupt (\r
938 VOID\r
939 )\r
940{\r
941 LOCAL_APIC_LVT_TIMER LvtTimer;\r
942\r
943 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
944 LvtTimer.Bits.Mask = 0;\r
945 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET, LvtTimer.Uint32);\r
946}\r
947\r
948/**\r
949 Disable the local APIC timer interrupt.\r
950**/\r
951VOID\r
952EFIAPI\r
953DisableApicTimerInterrupt (\r
954 VOID\r
955 )\r
956{\r
957 LOCAL_APIC_LVT_TIMER LvtTimer;\r
958\r
959 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
960 LvtTimer.Bits.Mask = 1;\r
961 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET, LvtTimer.Uint32);\r
962}\r
963\r
964/**\r
965 Get the local APIC timer interrupt state.\r
966\r
967 @retval TRUE The local APIC timer interrupt is enabled.\r
968 @retval FALSE The local APIC timer interrupt is disabled.\r
969**/\r
970BOOLEAN\r
971EFIAPI\r
972GetApicTimerInterruptState (\r
973 VOID\r
974 )\r
975{\r
976 LOCAL_APIC_LVT_TIMER LvtTimer;\r
977\r
978 LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
979 return (BOOLEAN)(LvtTimer.Bits.Mask == 0);\r
980}\r
981\r
982/**\r
983 Send EOI to the local APIC.\r
984**/\r
985VOID\r
986EFIAPI\r
987SendApicEoi (\r
988 VOID\r
989 )\r
990{\r
991 WriteLocalApicReg (XAPIC_EOI_OFFSET, 0);\r
992}\r
993\r
994/**\r
995 Get the 32-bit address that a device should use to send a Message Signaled\r
996 Interrupt (MSI) to the Local APIC of the currently executing processor.\r
997\r
998 @return 32-bit address used to send an MSI to the Local APIC.\r
999**/\r
1000UINT32\r
1001EFIAPI\r
1002GetApicMsiAddress (\r
1003 VOID\r
1004 )\r
1005{\r
1006 LOCAL_APIC_MSI_ADDRESS MsiAddress;\r
1007\r
1008 //\r
1009 // Return address for an MSI interrupt to be delivered only to the APIC ID\r
1010 // of the currently executing processor.\r
1011 //\r
1012 MsiAddress.Uint32 = 0;\r
1013 MsiAddress.Bits.BaseAddress = 0xFEE;\r
1014 MsiAddress.Bits.DestinationId = GetApicId ();\r
1015 return MsiAddress.Uint32;\r
1016}\r
1017\r
1018/**\r
1019 Get the 64-bit data value that a device should use to send a Message Signaled\r
1020 Interrupt (MSI) to the Local APIC of the currently executing processor.\r
1021\r
1022 If Vector is not in range 0x10..0xFE, then ASSERT().\r
1023 If DeliveryMode is not supported, then ASSERT().\r
1024\r
1025 @param Vector The 8-bit interrupt vector associated with the MSI.\r
1026 Must be in the range 0x10..0xFE\r
1027 @param DeliveryMode A 3-bit value that specifies how the recept of the MSI\r
1028 is handled. The only supported values are:\r
1029 0: LOCAL_APIC_DELIVERY_MODE_FIXED\r
1030 1: LOCAL_APIC_DELIVERY_MODE_LOWEST_PRIORITY\r
1031 2: LOCAL_APIC_DELIVERY_MODE_SMI\r
1032 4: LOCAL_APIC_DELIVERY_MODE_NMI\r
1033 5: LOCAL_APIC_DELIVERY_MODE_INIT\r
1034 7: LOCAL_APIC_DELIVERY_MODE_EXTINT\r
1035\r
1036 @param LevelTriggered TRUE specifies a level triggered interrupt.\r
1037 FALSE specifies an edge triggered interrupt.\r
1038 @param AssertionLevel Ignored if LevelTriggered is FALSE.\r
1039 TRUE specifies a level triggered interrupt that active\r
1040 when the interrupt line is asserted.\r
1041 FALSE specifies a level triggered interrupt that active\r
1042 when the interrupt line is deasserted.\r
1043\r
1044 @return 64-bit data value used to send an MSI to the Local APIC.\r
1045**/\r
1046UINT64\r
1047EFIAPI\r
1048GetApicMsiValue (\r
1049 IN UINT8 Vector,\r
1050 IN UINTN DeliveryMode,\r
1051 IN BOOLEAN LevelTriggered,\r
1052 IN BOOLEAN AssertionLevel\r
1053 )\r
1054{\r
1055 LOCAL_APIC_MSI_DATA MsiData;\r
1056\r
1057 ASSERT (Vector >= 0x10 && Vector <= 0xFE);\r
1058 ASSERT (DeliveryMode < 8 && DeliveryMode != 6 && DeliveryMode != 3);\r
1059\r
1060 MsiData.Uint64 = 0;\r
1061 MsiData.Bits.Vector = Vector;\r
1062 MsiData.Bits.DeliveryMode = (UINT32)DeliveryMode;\r
1063 if (LevelTriggered) {\r
1064 MsiData.Bits.TriggerMode = 1;\r
1065 if (AssertionLevel) {\r
1066 MsiData.Bits.Level = 1;\r
1067 }\r
1068 }\r
1069 return MsiData.Uint64;\r
1070}\r
1071\r
1072/**\r
1073 Get Package ID/Core ID/Thread ID of a processor.\r
1074\r
1075 The algorithm assumes the target system has symmetry across physical\r
1076 package boundaries with respect to the number of logical processors\r
1077 per package, number of cores per package.\r
1078\r
1079 @param[in] InitialApicId Initial APIC ID of the target logical processor.\r
1080 @param[out] Package Returns the processor package ID.\r
1081 @param[out] Core Returns the processor core ID.\r
1082 @param[out] Thread Returns the processor thread ID.\r
1083**/\r
1084VOID\r
1085EFIAPI\r
1086GetProcessorLocationByApicId (\r
1087 IN UINT32 InitialApicId,\r
1088 OUT UINT32 *Package OPTIONAL,\r
1089 OUT UINT32 *Core OPTIONAL,\r
1090 OUT UINT32 *Thread OPTIONAL\r
1091 )\r
1092{\r
1093 BOOLEAN TopologyLeafSupported;\r
1094 CPUID_VERSION_INFO_EBX VersionInfoEbx;\r
1095 CPUID_VERSION_INFO_EDX VersionInfoEdx;\r
1096 CPUID_CACHE_PARAMS_EAX CacheParamsEax;\r
1097 CPUID_EXTENDED_TOPOLOGY_EAX ExtendedTopologyEax;\r
1098 CPUID_EXTENDED_TOPOLOGY_EBX ExtendedTopologyEbx;\r
1099 CPUID_EXTENDED_TOPOLOGY_ECX ExtendedTopologyEcx;\r
1100 CPUID_AMD_EXTENDED_CPU_SIG_ECX AmdExtendedCpuSigEcx;\r
1101 CPUID_AMD_PROCESSOR_TOPOLOGY_EBX AmdProcessorTopologyEbx;\r
1102 CPUID_AMD_VIR_PHY_ADDRESS_SIZE_ECX AmdVirPhyAddressSizeEcx;\r
1103 UINT32 MaxStandardCpuIdIndex;\r
1104 UINT32 MaxExtendedCpuIdIndex;\r
1105 UINT32 SubIndex;\r
1106 UINTN LevelType;\r
1107 UINT32 MaxLogicProcessorsPerPackage;\r
1108 UINT32 MaxCoresPerPackage;\r
1109 UINTN ThreadBits;\r
1110 UINTN CoreBits;\r
1111\r
1112 //\r
1113 // Check if the processor is capable of supporting more than one logical processor.\r
1114 //\r
1115 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);\r
1116 if (VersionInfoEdx.Bits.HTT == 0) {\r
1117 if (Thread != NULL) {\r
1118 *Thread = 0;\r
1119 }\r
1120 if (Core != NULL) {\r
1121 *Core = 0;\r
1122 }\r
1123 if (Package != NULL) {\r
1124 *Package = 0;\r
1125 }\r
1126 return;\r
1127 }\r
1128\r
1129 //\r
1130 // Assume three-level mapping of APIC ID: Package|Core|Thread.\r
1131 //\r
1132 ThreadBits = 0;\r
1133 CoreBits = 0;\r
1134\r
1135 //\r
1136 // Get max index of CPUID\r
1137 //\r
1138 AsmCpuid (CPUID_SIGNATURE, &MaxStandardCpuIdIndex, NULL, NULL, NULL);\r
1139 AsmCpuid (CPUID_EXTENDED_FUNCTION, &MaxExtendedCpuIdIndex, NULL, NULL, NULL);\r
1140\r
1141 //\r
1142 // If the extended topology enumeration leaf is available, it\r
1143 // is the preferred mechanism for enumerating topology.\r
1144 //\r
1145 TopologyLeafSupported = FALSE;\r
1146 if (MaxStandardCpuIdIndex >= CPUID_EXTENDED_TOPOLOGY) {\r
1147 AsmCpuidEx(\r
1148 CPUID_EXTENDED_TOPOLOGY,\r
1149 0,\r
1150 &ExtendedTopologyEax.Uint32,\r
1151 &ExtendedTopologyEbx.Uint32,\r
1152 &ExtendedTopologyEcx.Uint32,\r
1153 NULL\r
1154 );\r
1155 //\r
1156 // If CPUID.(EAX=0BH, ECX=0H):EBX returns zero and maximum input value for\r
1157 // basic CPUID information is greater than 0BH, then CPUID.0BH leaf is not\r
1158 // supported on that processor.\r
1159 //\r
1160 if (ExtendedTopologyEbx.Uint32 != 0) {\r
1161 TopologyLeafSupported = TRUE;\r
1162\r
1163 //\r
1164 // Sub-leaf index 0 (ECX= 0 as input) provides enumeration parameters to extract\r
1165 // the SMT sub-field of x2APIC ID.\r
1166 //\r
1167 LevelType = ExtendedTopologyEcx.Bits.LevelType;\r
1168 ASSERT (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT);\r
1169 ThreadBits = ExtendedTopologyEax.Bits.ApicIdShift;\r
1170\r
1171 //\r
1172 // Software must not assume any "level type" encoding\r
1173 // value to be related to any sub-leaf index, except sub-leaf 0.\r
1174 //\r
1175 SubIndex = 1;\r
1176 do {\r
1177 AsmCpuidEx (\r
1178 CPUID_EXTENDED_TOPOLOGY,\r
1179 SubIndex,\r
1180 &ExtendedTopologyEax.Uint32,\r
1181 NULL,\r
1182 &ExtendedTopologyEcx.Uint32,\r
1183 NULL\r
1184 );\r
1185 LevelType = ExtendedTopologyEcx.Bits.LevelType;\r
1186 if (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_CORE) {\r
1187 CoreBits = ExtendedTopologyEax.Bits.ApicIdShift - ThreadBits;\r
1188 break;\r
1189 }\r
1190 SubIndex++;\r
1191 } while (LevelType != CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_INVALID);\r
1192 }\r
1193 }\r
1194\r
1195 if (!TopologyLeafSupported) {\r
1196 //\r
1197 // Get logical processor count\r
1198 //\r
1199 AsmCpuid (CPUID_VERSION_INFO, NULL, &VersionInfoEbx.Uint32, NULL, NULL);\r
1200 MaxLogicProcessorsPerPackage = VersionInfoEbx.Bits.MaximumAddressableIdsForLogicalProcessors;\r
1201\r
1202 //\r
1203 // Assume single-core processor\r
1204 //\r
1205 MaxCoresPerPackage = 1;\r
1206\r
1207 //\r
1208 // Check for topology extensions on AMD processor\r
1209 //\r
1210 if (StandardSignatureIsAuthenticAMD()) {\r
1211 if (MaxExtendedCpuIdIndex >= CPUID_AMD_PROCESSOR_TOPOLOGY) {\r
1212 AsmCpuid (CPUID_EXTENDED_CPU_SIG, NULL, NULL, &AmdExtendedCpuSigEcx.Uint32, NULL);\r
1213 if (AmdExtendedCpuSigEcx.Bits.TopologyExtensions != 0) {\r
1214 //\r
1215 // Account for max possible thread count to decode ApicId\r
1216 //\r
1217 AsmCpuid (CPUID_VIR_PHY_ADDRESS_SIZE, NULL, NULL, &AmdVirPhyAddressSizeEcx.Uint32, NULL);\r
1218 MaxLogicProcessorsPerPackage = 1 << AmdVirPhyAddressSizeEcx.Bits.ApicIdCoreIdSize;\r
1219\r
1220 //\r
1221 // Get cores per processor package\r
1222 //\r
1223 AsmCpuid (CPUID_AMD_PROCESSOR_TOPOLOGY, NULL, &AmdProcessorTopologyEbx.Uint32, NULL, NULL);\r
1224 MaxCoresPerPackage = MaxLogicProcessorsPerPackage / (AmdProcessorTopologyEbx.Bits.ThreadsPerCore + 1);\r
1225 }\r
1226 }\r
1227 }\r
1228 else {\r
1229 //\r
1230 // Extract core count based on CACHE information\r
1231 //\r
1232 if (MaxStandardCpuIdIndex >= CPUID_CACHE_PARAMS) {\r
1233 AsmCpuidEx (CPUID_CACHE_PARAMS, 0, &CacheParamsEax.Uint32, NULL, NULL, NULL);\r
1234 if (CacheParamsEax.Uint32 != 0) {\r
1235 MaxCoresPerPackage = CacheParamsEax.Bits.MaximumAddressableIdsForLogicalProcessors + 1;\r
1236 }\r
1237 }\r
1238 }\r
1239\r
1240 ThreadBits = (UINTN)(HighBitSet32(MaxLogicProcessorsPerPackage / MaxCoresPerPackage - 1) + 1);\r
1241 CoreBits = (UINTN)(HighBitSet32(MaxCoresPerPackage - 1) + 1);\r
1242 }\r
1243\r
1244 if (Thread != NULL) {\r
1245 *Thread = InitialApicId & ((1 << ThreadBits) - 1);\r
1246 }\r
1247 if (Core != NULL) {\r
1248 *Core = (InitialApicId >> ThreadBits) & ((1 << CoreBits) - 1);\r
1249 }\r
1250 if (Package != NULL) {\r
1251 *Package = (InitialApicId >> (ThreadBits + CoreBits));\r
1252 }\r
1253}\r
1254\r
1255/**\r
1256 Get Package ID/Die ID/Tile ID/Module ID/Core ID/Thread ID of a processor.\r
1257\r
1258 The algorithm assumes the target system has symmetry across physical\r
1259 package boundaries with respect to the number of threads per core, number of\r
1260 cores per module, number of modules per tile, number of tiles per die, number\r
1261 of dies per package.\r
1262\r
1263 @param[in] InitialApicId Initial APIC ID of the target logical processor.\r
1264 @param[out] Package Returns the processor package ID.\r
1265 @param[out] Die Returns the processor die ID.\r
1266 @param[out] Tile Returns the processor tile ID.\r
1267 @param[out] Module Returns the processor module ID.\r
1268 @param[out] Core Returns the processor core ID.\r
1269 @param[out] Thread Returns the processor thread ID.\r
1270**/\r
1271VOID\r
1272EFIAPI\r
1273GetProcessorLocation2ByApicId (\r
1274 IN UINT32 InitialApicId,\r
1275 OUT UINT32 *Package OPTIONAL,\r
1276 OUT UINT32 *Die OPTIONAL,\r
1277 OUT UINT32 *Tile OPTIONAL,\r
1278 OUT UINT32 *Module OPTIONAL,\r
1279 OUT UINT32 *Core OPTIONAL,\r
1280 OUT UINT32 *Thread OPTIONAL\r
1281 )\r
1282{\r
1283 CPUID_EXTENDED_TOPOLOGY_EAX ExtendedTopologyEax;\r
1284 CPUID_EXTENDED_TOPOLOGY_ECX ExtendedTopologyEcx;\r
1285 UINT32 MaxStandardCpuIdIndex;\r
1286 UINT32 Index;\r
1287 UINTN LevelType;\r
1288 UINT32 Bits[CPUID_V2_EXTENDED_TOPOLOGY_LEVEL_TYPE_DIE + 2];\r
1289 UINT32 *Location[CPUID_V2_EXTENDED_TOPOLOGY_LEVEL_TYPE_DIE + 2];\r
1290\r
1291 for (LevelType = 0; LevelType < ARRAY_SIZE (Bits); LevelType++) {\r
1292 Bits[LevelType] = 0;\r
1293 }\r
1294\r
1295 //\r
1296 // Get max index of CPUID\r
1297 //\r
1298 AsmCpuid (CPUID_SIGNATURE, &MaxStandardCpuIdIndex, NULL, NULL, NULL);\r
1299 if (MaxStandardCpuIdIndex < CPUID_V2_EXTENDED_TOPOLOGY) {\r
1300 if (Die != NULL) {\r
1301 *Die = 0;\r
1302 }\r
1303 if (Tile != NULL) {\r
1304 *Tile = 0;\r
1305 }\r
1306 if (Module != NULL) {\r
1307 *Module = 0;\r
1308 }\r
1309 GetProcessorLocationByApicId (InitialApicId, Package, Core, Thread);\r
1310 return;\r
1311 }\r
1312\r
1313 //\r
1314 // If the V2 extended topology enumeration leaf is available, it\r
1315 // is the preferred mechanism for enumerating topology.\r
1316 //\r
1317 for (Index = 0; ; Index++) {\r
1318 AsmCpuidEx(\r
1319 CPUID_V2_EXTENDED_TOPOLOGY,\r
1320 Index,\r
1321 &ExtendedTopologyEax.Uint32,\r
1322 NULL,\r
1323 &ExtendedTopologyEcx.Uint32,\r
1324 NULL\r
1325 );\r
1326\r
1327 LevelType = ExtendedTopologyEcx.Bits.LevelType;\r
1328\r
1329 //\r
1330 // first level reported should be SMT.\r
1331 //\r
1332 ASSERT ((Index != 0) || (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT));\r
1333 if (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_INVALID) {\r
1334 break;\r
1335 }\r
1336 ASSERT (LevelType < ARRAY_SIZE (Bits));\r
1337 Bits[LevelType] = ExtendedTopologyEax.Bits.ApicIdShift;\r
1338 }\r
1339\r
1340 for (LevelType = CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_CORE; LevelType < ARRAY_SIZE (Bits); LevelType++) {\r
1341 //\r
1342 // If there are more levels between level-1 (low-level) and level-2 (high-level), the unknown levels will be ignored\r
1343 // and treated as an extension of the last known level (i.e., level-1 in this case).\r
1344 //\r
1345 if (Bits[LevelType] == 0) {\r
1346 Bits[LevelType] = Bits[LevelType - 1];\r
1347 }\r
1348 }\r
1349\r
1350 Location[CPUID_V2_EXTENDED_TOPOLOGY_LEVEL_TYPE_DIE + 1] = Package;\r
1351 Location[CPUID_V2_EXTENDED_TOPOLOGY_LEVEL_TYPE_DIE ] = Die;\r
1352 Location[CPUID_V2_EXTENDED_TOPOLOGY_LEVEL_TYPE_TILE ] = Tile;\r
1353 Location[CPUID_V2_EXTENDED_TOPOLOGY_LEVEL_TYPE_MODULE ] = Module;\r
1354 Location[CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_CORE ] = Core;\r
1355 Location[CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT ] = Thread;\r
1356\r
1357 Bits[CPUID_V2_EXTENDED_TOPOLOGY_LEVEL_TYPE_DIE + 1] = 32;\r
1358\r
1359 for ( LevelType = CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT\r
1360 ; LevelType <= CPUID_V2_EXTENDED_TOPOLOGY_LEVEL_TYPE_DIE + 1\r
1361 ; LevelType ++\r
1362 ) {\r
1363 if (Location[LevelType] != NULL) {\r
1364 //\r
1365 // Bits[i] holds the number of bits to shift right on x2APIC ID to get a unique\r
1366 // topology ID of the next level type.\r
1367 //\r
1368 *Location[LevelType] = InitialApicId >> Bits[LevelType - 1];\r
1369\r
1370 //\r
1371 // Bits[i] - Bits[i-1] holds the number of bits for the next ONE level type.\r
1372 //\r
1373 *Location[LevelType] &= (1 << (Bits[LevelType] - Bits[LevelType - 1])) - 1;\r
1374 }\r
1375 }\r
1376}\r