]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/BaseXApicX2ApicLib/BaseXApicX2ApicLib.c
UefiCpuPkg/CpuDxe: Enable protection for newly added page table
[mirror_edk2.git] / UefiCpuPkg / Library / BaseXApicX2ApicLib / BaseXApicX2ApicLib.c
index 1e3c03905c71d29d0b01d3929fb69b7010c90f9e..1f4dcf709f28d8a1c5614faffbe26ef9a61937e1 100644 (file)
@@ -5,6 +5,8 @@
   which have xAPIC and x2APIC modes.\r
 \r
   Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2017, AMD Inc. All rights reserved.<BR>\r
+\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -16,6 +18,7 @@
 **/\r
 \r
 #include <Register/Cpuid.h>\r
+#include <Register/Amd/Cpuid.h>\r
 #include <Register/Msr.h>\r
 #include <Register/LocalApic.h>\r
 \r
 // Library internal functions\r
 //\r
 \r
+/**\r
+  Determine if the standard CPU signature is "AuthenticAMD".\r
+\r
+  @retval TRUE  The CPU signature matches.\r
+  @retval FALSE The CPU signature does not match.\r
+\r
+**/\r
+BOOLEAN\r
+StandardSignatureIsAuthenticAMD (\r
+  VOID\r
+  )\r
+{\r
+  UINT32  RegEbx;\r
+  UINT32  RegEcx;\r
+  UINT32  RegEdx;\r
+\r
+  AsmCpuid (CPUID_SIGNATURE, NULL, &RegEbx, &RegEcx, &RegEdx);\r
+  return (RegEbx == CPUID_SIGNATURE_AUTHENTIC_AMD_EBX &&\r
+          RegEcx == CPUID_SIGNATURE_AUTHENTIC_AMD_ECX &&\r
+          RegEdx == CPUID_SIGNATURE_AUTHENTIC_AMD_EDX);\r
+}\r
+\r
 /**\r
   Determine if the CPU supports the Local APIC Base Address MSR.\r
 \r
@@ -411,12 +436,15 @@ GetInitialApicId (
     AsmCpuid (CPUID_SIGNATURE, &MaxCpuIdIndex, NULL, NULL, NULL);\r
     //\r
     // If CPUID Leaf B is supported, \r
+    // And CPUID.0BH:EBX[15:0] reports a non-zero value,\r
     // Then the initial 32-bit APIC ID = CPUID.0BH:EDX\r
     // Else the initial 8-bit APIC ID = CPUID.1:EBX[31:24]\r
     //\r
     if (MaxCpuIdIndex >= CPUID_EXTENDED_TOPOLOGY) {\r
-      AsmCpuidEx (CPUID_EXTENDED_TOPOLOGY, 0, NULL, NULL, NULL, &ApicId);\r
-      return ApicId;\r
+      AsmCpuidEx (CPUID_EXTENDED_TOPOLOGY, 0, NULL, &RegEbx, NULL, &ApicId);\r
+      if ((RegEbx & (BIT16 - 1)) != 0) {\r
+        return ApicId;\r
+      }\r
     }\r
     AsmCpuid (CPUID_VERSION_INFO, NULL, &RegEbx, NULL, NULL);\r
     return RegEbx >> 24;\r
@@ -1050,6 +1078,7 @@ GetApicMsiValue (
   @param[out]  Thread        Returns the processor thread ID.\r
 **/\r
 VOID\r
+EFIAPI\r
 GetProcessorLocationByApicId (\r
   IN  UINT32  InitialApicId,\r
   OUT UINT32  *Package  OPTIONAL,\r
@@ -1057,31 +1086,35 @@ GetProcessorLocationByApicId (
   OUT UINT32  *Thread  OPTIONAL\r
   )\r
 {\r
-  BOOLEAN                       TopologyLeafSupported;\r
-  UINTN                         ThreadBits;\r
-  UINTN                         CoreBits;\r
-  CPUID_VERSION_INFO_EBX        VersionInfoEbx;\r
-  CPUID_VERSION_INFO_EDX        VersionInfoEdx;\r
-  CPUID_CACHE_PARAMS_EAX        CacheParamsEax;\r
-  CPUID_EXTENDED_TOPOLOGY_EAX   ExtendedTopologyEax;\r
-  CPUID_EXTENDED_TOPOLOGY_EBX   ExtendedTopologyEbx;\r
-  CPUID_EXTENDED_TOPOLOGY_ECX   ExtendedTopologyEcx;\r
-  UINT32                        MaxCpuIdIndex;\r
-  UINT32                        SubIndex;\r
-  UINTN                         LevelType;\r
-  UINT32                        MaxLogicProcessorsPerPackage;\r
-  UINT32                        MaxCoresPerPackage;\r
+  BOOLEAN                             TopologyLeafSupported;\r
+  CPUID_VERSION_INFO_EBX              VersionInfoEbx;\r
+  CPUID_VERSION_INFO_EDX              VersionInfoEdx;\r
+  CPUID_CACHE_PARAMS_EAX              CacheParamsEax;\r
+  CPUID_EXTENDED_TOPOLOGY_EAX         ExtendedTopologyEax;\r
+  CPUID_EXTENDED_TOPOLOGY_EBX         ExtendedTopologyEbx;\r
+  CPUID_EXTENDED_TOPOLOGY_ECX         ExtendedTopologyEcx;\r
+  CPUID_AMD_EXTENDED_CPU_SIG_ECX      AmdExtendedCpuSigEcx;\r
+  CPUID_AMD_PROCESSOR_TOPOLOGY_EBX    AmdProcessorTopologyEbx;\r
+  CPUID_AMD_VIR_PHY_ADDRESS_SIZE_ECX  AmdVirPhyAddressSizeEcx;\r
+  UINT32                              MaxStandardCpuIdIndex;\r
+  UINT32                              MaxExtendedCpuIdIndex;\r
+  UINT32                              SubIndex;\r
+  UINTN                               LevelType;\r
+  UINT32                              MaxLogicProcessorsPerPackage;\r
+  UINT32                              MaxCoresPerPackage;\r
+  UINTN                               ThreadBits;\r
+  UINTN                               CoreBits;\r
 \r
   //\r
   // Check if the processor is capable of supporting more than one logical processor.\r
   //\r
-  AsmCpuid(CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);\r
+  AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);\r
   if (VersionInfoEdx.Bits.HTT == 0) {\r
     if (Thread != NULL) {\r
-      *Thread  = 0;\r
+      *Thread = 0;\r
     }\r
     if (Core != NULL) {\r
-      *Core    = 0;\r
+      *Core = 0;\r
     }\r
     if (Package != NULL) {\r
       *Package = 0;\r
@@ -1089,24 +1122,24 @@ GetProcessorLocationByApicId (
     return;\r
   }\r
 \r
-  ThreadBits = 0;\r
-  CoreBits = 0;\r
-\r
   //\r
-  // Assume three-level mapping of APIC ID: Package:Core:SMT.\r
+  // Assume three-level mapping of APIC ID: Package|Core|Thread.\r
   //\r
-  TopologyLeafSupported = FALSE;\r
+  ThreadBits = 0;\r
+  CoreBits = 0;\r
 \r
   //\r
-  // Get the max index of basic CPUID\r
+  // Get max index of CPUID\r
   //\r
-  AsmCpuid(CPUID_SIGNATURE, &MaxCpuIdIndex, NULL, NULL, NULL);\r
+  AsmCpuid (CPUID_SIGNATURE, &MaxStandardCpuIdIndex, NULL, NULL, NULL);\r
+  AsmCpuid (CPUID_EXTENDED_FUNCTION, &MaxExtendedCpuIdIndex, NULL, NULL, NULL);\r
 \r
   //\r
   // If the extended topology enumeration leaf is available, it\r
   // is the preferred mechanism for enumerating topology.\r
   //\r
-  if (MaxCpuIdIndex >= CPUID_EXTENDED_TOPOLOGY) {\r
+  TopologyLeafSupported = FALSE;\r
+  if (MaxStandardCpuIdIndex >= CPUID_EXTENDED_TOPOLOGY) {\r
     AsmCpuidEx(\r
       CPUID_EXTENDED_TOPOLOGY,\r
       0,\r
@@ -1128,7 +1161,7 @@ GetProcessorLocationByApicId (
       // the SMT sub-field of x2APIC ID.\r
       //\r
       LevelType = ExtendedTopologyEcx.Bits.LevelType;\r
-      ASSERT(LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT);\r
+      ASSERT (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT);\r
       ThreadBits = ExtendedTopologyEax.Bits.ApicIdShift;\r
 \r
       //\r
@@ -1137,7 +1170,7 @@ GetProcessorLocationByApicId (
       //\r
       SubIndex = 1;\r
       do {\r
-        AsmCpuidEx(\r
+        AsmCpuidEx (\r
           CPUID_EXTENDED_TOPOLOGY,\r
           SubIndex,\r
           &ExtendedTopologyEax.Uint32,\r
@@ -1156,27 +1189,59 @@ GetProcessorLocationByApicId (
   }\r
 \r
   if (!TopologyLeafSupported) {\r
-    AsmCpuid(CPUID_VERSION_INFO, NULL, &VersionInfoEbx.Uint32, NULL, NULL);\r
+    //\r
+    // Get logical processor count\r
+    //\r
+    AsmCpuid (CPUID_VERSION_INFO, NULL, &VersionInfoEbx.Uint32, NULL, NULL);\r
     MaxLogicProcessorsPerPackage = VersionInfoEbx.Bits.MaximumAddressableIdsForLogicalProcessors;\r
-    if (MaxCpuIdIndex >= CPUID_CACHE_PARAMS) {\r
-      AsmCpuidEx(CPUID_CACHE_PARAMS, 0, &CacheParamsEax.Uint32, NULL, NULL, NULL);\r
-      MaxCoresPerPackage = CacheParamsEax.Bits.MaximumAddressableIdsForLogicalProcessors + 1;\r
+\r
+    //\r
+    // Assume single-core processor\r
+    //\r
+    MaxCoresPerPackage = 1;\r
+\r
+    //\r
+    // Check for topology extensions on AMD processor\r
+    //\r
+    if (StandardSignatureIsAuthenticAMD()) {\r
+      if (MaxExtendedCpuIdIndex >= CPUID_AMD_PROCESSOR_TOPOLOGY) {\r
+        AsmCpuid (CPUID_EXTENDED_CPU_SIG, NULL, NULL, &AmdExtendedCpuSigEcx.Uint32, NULL);\r
+        if (AmdExtendedCpuSigEcx.Bits.TopologyExtensions != 0) {\r
+          //\r
+          // Account for max possible thread count to decode ApicId\r
+          //\r
+          AsmCpuid (CPUID_VIR_PHY_ADDRESS_SIZE, NULL, NULL, &AmdVirPhyAddressSizeEcx.Uint32, NULL);\r
+          MaxLogicProcessorsPerPackage = 1 << AmdVirPhyAddressSizeEcx.Bits.ApicIdCoreIdSize;\r
+\r
+          //\r
+          // Get cores per processor package\r
+          //\r
+          AsmCpuid (CPUID_AMD_PROCESSOR_TOPOLOGY, NULL, &AmdProcessorTopologyEbx.Uint32, NULL, NULL);\r
+          MaxCoresPerPackage = MaxLogicProcessorsPerPackage / (AmdProcessorTopologyEbx.Bits.ThreadsPerCore + 1);\r
+        }\r
+      }\r
     }\r
     else {\r
       //\r
-      // Must be a single-core processor.\r
+      // Extract core count based on CACHE information\r
       //\r
-      MaxCoresPerPackage = 1;\r
+      if (MaxStandardCpuIdIndex >= CPUID_CACHE_PARAMS) {\r
+        AsmCpuidEx (CPUID_CACHE_PARAMS, 0, &CacheParamsEax.Uint32, NULL, NULL, NULL);\r
+        if (CacheParamsEax.Uint32 != 0) {\r
+          MaxCoresPerPackage = CacheParamsEax.Bits.MaximumAddressableIdsForLogicalProcessors + 1;\r
+        }\r
+      }\r
     }\r
 \r
     ThreadBits = (UINTN)(HighBitSet32(MaxLogicProcessorsPerPackage / MaxCoresPerPackage - 1) + 1);\r
-    CoreBits = (UINTN)(HighBitSet32(MaxCoresPerPackage - 1) + 1);  }\r
+    CoreBits = (UINTN)(HighBitSet32(MaxCoresPerPackage - 1) + 1);\r
+  }\r
 \r
   if (Thread != NULL) {\r
-    *Thread  = InitialApicId & ((1 << ThreadBits) - 1);\r
+    *Thread = InitialApicId & ((1 << ThreadBits) - 1);\r
   }\r
   if (Core != NULL) {\r
-    *Core    = (InitialApicId >> ThreadBits) & ((1 << CoreBits) - 1);\r
+    *Core = (InitialApicId >> ThreadBits) & ((1 << CoreBits) - 1);\r
   }\r
   if (Package != NULL) {\r
     *Package = (InitialApicId >> (ThreadBits + CoreBits));\r