]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/BaseXApicLib/BaseXApicLib.c
UefiCpuPkg/PiSmmCpuDxeSmm: Remove duplicate aligned buffer on S3 path
[mirror_edk2.git] / UefiCpuPkg / Library / BaseXApicLib / BaseXApicLib.c
index 1ac3853071e3dd25db362a06c10bf98db7812f71..1fca66ef1b293b76483aa818d5e757c094e5d01c 100644 (file)
@@ -3,7 +3,7 @@
 \r
   This local APIC library instance supports xAPIC mode only.\r
 \r
-  Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>\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
@@ -14,6 +14,7 @@
 \r
 **/\r
 \r
+#include <Register/Cpuid.h>\r
 #include <Register/LocalApic.h>\r
 \r
 #include <Library/BaseLib.h>\r
 // Library internal functions\r
 //\r
 \r
+/**\r
+  Determine if the CPU supports the Local APIC Base Address MSR.\r
+\r
+  @retval TRUE  The CPU supports the Local APIC Base Address MSR.\r
+  @retval FALSE The CPU does not support the Local APIC Base Address MSR.\r
+\r
+**/\r
+BOOLEAN\r
+LocalApicBaseAddressMsrSupported (\r
+  VOID\r
+  )\r
+{\r
+  UINT32  RegEax;\r
+  UINTN   FamilyId;\r
+  \r
+  AsmCpuid (1, &RegEax, NULL, NULL, NULL);\r
+  FamilyId = BitFieldRead32 (RegEax, 8, 11);\r
+  if (FamilyId == 0x04 || FamilyId == 0x05) {\r
+    //\r
+    // CPUs with a FamilyId of 0x04 or 0x05 do not support the \r
+    // Local APIC Base Address MSR\r
+    //\r
+    return FALSE;\r
+  }\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Retrieve the base address of local APIC.\r
+\r
+  @return The base address of local APIC.\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+GetLocalApicBaseAddress (\r
+  VOID\r
+  )\r
+{\r
+  MSR_IA32_APIC_BASE  ApicBaseMsr;\r
+\r
+  if (!LocalApicBaseAddressMsrSupported ()) {\r
+    //\r
+    // If CPU does not support Local APIC Base Address MSR, then retrieve\r
+    // Local APIC Base Address from PCD\r
+    //\r
+    return PcdGet32 (PcdCpuLocalApicBaseAddress);\r
+  }\r
+\r
+  ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE_ADDRESS);\r
+  \r
+  return (UINTN)(LShiftU64 ((UINT64) ApicBaseMsr.Bits.ApicBaseHigh, 32)) +\r
+           (((UINTN)ApicBaseMsr.Bits.ApicBaseLow) << 12);\r
+}\r
+\r
+/**\r
+  Set the base address of local APIC.\r
+\r
+  If BaseAddress is not aligned on a 4KB boundary, then ASSERT().\r
+\r
+  @param[in] BaseAddress   Local APIC base address to be set.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+SetLocalApicBaseAddress (\r
+  IN UINTN                BaseAddress\r
+  )\r
+{\r
+  MSR_IA32_APIC_BASE  ApicBaseMsr;\r
+\r
+  ASSERT ((BaseAddress & (SIZE_4KB - 1)) == 0);\r
+\r
+  if (!LocalApicBaseAddressMsrSupported ()) {\r
+    //\r
+    // Ignore set request if the CPU does not support APIC Base Address MSR\r
+    //\r
+    return;\r
+  }\r
+\r
+  ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE_ADDRESS);\r
+\r
+  ApicBaseMsr.Bits.ApicBaseLow  = (UINT32) (BaseAddress >> 12);\r
+  ApicBaseMsr.Bits.ApicBaseHigh = (UINT32) (RShiftU64((UINT64) BaseAddress, 32));\r
+\r
+  AsmWriteMsr64 (MSR_IA32_APIC_BASE_ADDRESS, ApicBaseMsr.Uint64);\r
+}\r
+\r
 /**\r
   Read from a local APIC register.\r
 \r
@@ -49,7 +138,7 @@ ReadLocalApicReg (
   ASSERT ((MmioOffset & 0xf) == 0);\r
   ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC);\r
 \r
-  return MmioRead32 (PcdGet32 (PcdCpuLocalApicBaseAddress) + MmioOffset);\r
+  return MmioRead32 (GetLocalApicBaseAddress() + MmioOffset);\r
 }\r
 \r
 /**\r
@@ -76,7 +165,7 @@ WriteLocalApicReg (
   ASSERT ((MmioOffset & 0xf) == 0);\r
   ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC);\r
 \r
-  MmioWrite32 (PcdGet32 (PcdCpuLocalApicBaseAddress) + MmioOffset, Value);\r
+  MmioWrite32 (GetLocalApicBaseAddress() + MmioOffset, Value);\r
 }\r
 \r
 /**\r
@@ -94,18 +183,47 @@ SendIpi (
   )\r
 {\r
   LOCAL_APIC_ICR_LOW IcrLowReg;\r
+  UINT32             IcrHigh;\r
+  BOOLEAN            InterruptState;\r
 \r
   ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC);\r
   ASSERT (ApicId <= 0xff);\r
 \r
+  InterruptState = SaveAndDisableInterrupts ();\r
+\r
+  //\r
+  // Save existing contents of ICR high 32 bits\r
+  //\r
+  IcrHigh = ReadLocalApicReg (XAPIC_ICR_HIGH_OFFSET);\r
+\r
+  //\r
+  // Wait for DeliveryStatus clear in case a previous IPI\r
+  //  is still being sent\r
+  //\r
+  do {\r
+    IcrLowReg.Uint32 = ReadLocalApicReg (XAPIC_ICR_LOW_OFFSET);\r
+  } while (IcrLowReg.Bits.DeliveryStatus != 0);\r
+\r
   //\r
   // For xAPIC, the act of writing to the low doubleword of the ICR causes the IPI to be sent.\r
   //\r
   WriteLocalApicReg (XAPIC_ICR_HIGH_OFFSET, ApicId << 24);\r
   WriteLocalApicReg (XAPIC_ICR_LOW_OFFSET, IcrLow);\r
+\r
+  //\r
+  // Wait for DeliveryStatus clear again\r
+  //\r
   do {\r
     IcrLowReg.Uint32 = ReadLocalApicReg (XAPIC_ICR_LOW_OFFSET);\r
   } while (IcrLowReg.Bits.DeliveryStatus != 0);\r
+\r
+  //\r
+  // And restore old contents of ICR high\r
+  //\r
+  WriteLocalApicReg (XAPIC_ICR_HIGH_OFFSET, IcrHigh);\r
+\r
+  SetInterruptState (InterruptState);\r
+\r
 }\r
 \r
 //\r
@@ -128,14 +246,19 @@ GetApicMode (
 {\r
   DEBUG_CODE (\r
     {\r
-      MSR_IA32_APIC_BASE ApicBaseMsr;\r
+      MSR_IA32_APIC_BASE  ApicBaseMsr;\r
 \r
-      ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE_ADDRESS);\r
       //\r
-      // Local APIC should have been enabled\r
+      // Check to see if the CPU supports the APIC Base Address MSR \r
       //\r
-      ASSERT (ApicBaseMsr.Bits.En != 0);\r
-      ASSERT (ApicBaseMsr.Bits.Extd == 0);\r
+      if (LocalApicBaseAddressMsrSupported ()) {\r
+        ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE_ADDRESS);\r
+        //\r
+        // Local APIC should have been enabled\r
+        //\r
+        ASSERT (ApicBaseMsr.Bits.En != 0);\r
+        ASSERT (ApicBaseMsr.Bits.Extd == 0);\r
+      }\r
     }\r
   );\r
   return LOCAL_APIC_MODE_XAPIC;\r
@@ -148,6 +271,9 @@ GetApicMode (
   If the specified local APIC mode can't be set as current, then ASSERT.\r
 \r
   @param ApicMode APIC mode to be set.\r
+\r
+  @note  This API must not be called from an interrupt handler or SMI handler.\r
+         It may result in unpredictable behavior.\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -162,7 +288,7 @@ SetApicMode (
 /**\r
   Get the initial local APIC ID of the executing processor assigned by hardware upon power on or reset.\r
 \r
-  In xAPIC mode, the initial local APIC ID is 8-bit, and may be different from current APIC ID.\r
+  In xAPIC mode, the initial local APIC ID may be different from current APIC ID.\r
   In x2APIC mode, the local APIC ID can't be changed and there is no concept of initial APIC ID. In this case, \r
   the 32-bit local APIC ID is returned as initial APIC ID.\r
 \r
@@ -174,10 +300,27 @@ GetInitialApicId (
   VOID\r
   )\r
 {\r
+  UINT32 ApicId;\r
+  UINT32 MaxCpuIdIndex;\r
   UINT32 RegEbx;\r
 \r
   ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC);\r
 \r
+  //\r
+  // Get the max index of basic CPUID\r
+  //\r
+  AsmCpuid (CPUID_SIGNATURE, &MaxCpuIdIndex, NULL, NULL, NULL);\r
+\r
+  //\r
+  // If CPUID Leaf B is supported, \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
+  }\r
+\r
   AsmCpuid (CPUID_VERSION_INFO, NULL, &RegEbx, NULL, NULL);\r
   return RegEbx >> 24;\r
 }\r
@@ -196,9 +339,15 @@ GetApicId (
   UINT32 ApicId;\r
 \r
   ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC);\r
-\r
-  ApicId = ReadLocalApicReg (XAPIC_ID_OFFSET);\r
-  ApicId >>= 24;\r
+  \r
+  if ((ApicId = GetInitialApicId ()) < 0x100) {\r
+    //\r
+    // If the initial local APIC ID is less 0x100, read APIC ID from\r
+    // XAPIC_ID_OFFSET, otherwise return the initial local APIC ID.\r
+    //\r
+    ApicId = ReadLocalApicReg (XAPIC_ID_OFFSET);\r
+    ApicId >>= 24;\r
+  }\r
   return ApicId;\r
 }\r
 \r
@@ -370,7 +519,7 @@ SendInitSipiSipi (
   ASSERT ((StartupRoutine & 0xfff) == 0);\r
 \r
   SendInitIpi (ApicId);\r
-  MicroSecondDelay (10);\r
+  MicroSecondDelay (PcdGet32(PcdCpuInitIpiDelayInMicroSeconds));\r
   IcrLow.Uint32 = 0;\r
   IcrLow.Bits.Vector = (StartupRoutine >> 12);\r
   IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_STARTUP;\r
@@ -403,7 +552,7 @@ SendInitSipiSipiAllExcludingSelf (
   ASSERT ((StartupRoutine & 0xfff) == 0);\r
 \r
   SendInitIpiAllExcludingSelf ();\r
-  MicroSecondDelay (10);\r
+  MicroSecondDelay (PcdGet32(PcdCpuInitIpiDelayInMicroSeconds));\r
   IcrLow.Uint32 = 0;\r
   IcrLow.Bits.Vector = (StartupRoutine >> 12);\r
   IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_STARTUP;\r
@@ -414,6 +563,39 @@ SendInitSipiSipiAllExcludingSelf (
   SendIpi (IcrLow.Uint32, 0);\r
 }\r
 \r
+/**\r
+  Initialize the state of the SoftwareEnable bit in the Local APIC\r
+  Spurious Interrupt Vector register.\r
+\r
+  @param  Enable  If TRUE, then set SoftwareEnable to 1\r
+                  If FALSE, then set SoftwareEnable to 0.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+InitializeLocalApicSoftwareEnable (\r
+  IN BOOLEAN  Enable\r
+  )\r
+{\r
+  LOCAL_APIC_SVR  Svr;\r
+\r
+  //\r
+  // Set local APIC software-enabled bit.\r
+  //\r
+  Svr.Uint32 = ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET);\r
+  if (Enable) {\r
+    if (Svr.Bits.SoftwareEnable == 0) {\r
+      Svr.Bits.SoftwareEnable = 1;\r
+      WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
+    }\r
+  } else {\r
+    if (Svr.Bits.SoftwareEnable == 1) {\r
+      Svr.Bits.SoftwareEnable = 0;\r
+      WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
+    }\r
+  }\r
+}\r
+\r
 /**\r
   Programming Virtual Wire Mode.\r
 \r
@@ -460,6 +642,28 @@ ProgramVirtualWireMode (
   WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET, Lint.Uint32);\r
 }\r
 \r
+/**\r
+  Disable LINT0 & LINT1 interrupts.\r
+\r
+  This function sets the mask flag in the LVT LINT0 & LINT1 registers.\r
+**/\r
+VOID\r
+EFIAPI\r
+DisableLvtInterrupts (\r
+  VOID\r
+  )\r
+{\r
+  LOCAL_APIC_LVT_LINT LvtLint;\r
+\r
+  LvtLint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET);\r
+  LvtLint.Bits.Mask = 1;\r
+  WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET, LvtLint.Uint32);\r
+\r
+  LvtLint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET);\r
+  LvtLint.Bits.Mask = 1;\r
+  WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET, LvtLint.Uint32);\r
+}\r
+\r
 /**\r
   Read the initial count value from the init-count register.\r
 \r
@@ -508,7 +712,6 @@ InitializeApicTimer (
   IN UINT8   Vector\r
   )\r
 {\r
-  LOCAL_APIC_SVR       Svr;\r
   LOCAL_APIC_DCR       Dcr;\r
   LOCAL_APIC_LVT_TIMER LvtTimer;\r
   UINT32               Divisor;\r
@@ -516,9 +719,7 @@ InitializeApicTimer (
   //\r
   // Ensure local APIC is in software-enabled state.\r
   //\r
-  Svr.Uint32 = ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET);\r
-  Svr.Bits.SoftwareEnable = 1;\r
-  WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET, Svr.Uint32);\r
+  InitializeLocalApicSoftwareEnable (TRUE);\r
 \r
   //\r
   // Program init-count register.\r
@@ -553,6 +754,8 @@ InitializeApicTimer (
 /**\r
   Get the state of the local APIC timer.\r
 \r
+  This function will ASSERT if the local APIC is not software enabled.\r
+\r
   @param DivideValue   Return the divide value for the DCR. It is one of 1,2,4,8,16,32,64,128.\r
   @param PeriodicMode  Return the timer mode. If TRUE, timer mode is peridoic. Othewise, timer mode is one-shot.\r
   @param Vector        Return the timer interrupt vector number.\r
@@ -569,6 +772,13 @@ GetApicTimerState (
   LOCAL_APIC_DCR Dcr;\r
   LOCAL_APIC_LVT_TIMER LvtTimer;\r
 \r
+  //\r
+  // Check the APIC Software Enable/Disable bit (bit 8) in Spurious-Interrupt\r
+  // Vector Register.\r
+  // This bit will be 1, if local APIC is software enabled.\r
+  //\r
+  ASSERT ((ReadLocalApicReg(XAPIC_SPURIOUS_VECTOR_OFFSET) & BIT8) != 0);\r
+\r
   if (DivideValue != NULL) {\r
     Dcr.Uint32 = ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET);\r
     Divisor = Dcr.Bits.DivideValue1 | (Dcr.Bits.DivideValue2 << 2);\r
@@ -653,3 +863,80 @@ SendApicEoi (
   WriteLocalApicReg (XAPIC_EOI_OFFSET, 0);\r
 }\r
 \r
+/**\r
+  Get the 32-bit address that a device should use to send a Message Signaled \r
+  Interrupt (MSI) to the Local APIC of the currently executing processor.\r
+\r
+  @return 32-bit address used to send an MSI to the Local APIC.\r
+**/\r
+UINT32\r
+EFIAPI    \r
+GetApicMsiAddress (\r
+  VOID\r
+  )\r
+{\r
+  LOCAL_APIC_MSI_ADDRESS  MsiAddress;\r
+\r
+  //\r
+  // Return address for an MSI interrupt to be delivered only to the APIC ID \r
+  // of the currently executing processor.\r
+  //\r
+  MsiAddress.Uint32             = 0;\r
+  MsiAddress.Bits.BaseAddress   = 0xFEE;\r
+  MsiAddress.Bits.DestinationId = GetApicId ();\r
+  return MsiAddress.Uint32;\r
+}\r
+    \r
+/**\r
+  Get the 64-bit data value that a device should use to send a Message Signaled \r
+  Interrupt (MSI) to the Local APIC of the currently executing processor.\r
+\r
+  If Vector is not in range 0x10..0xFE, then ASSERT().\r
+  If DeliveryMode is not supported, then ASSERT().\r
+  \r
+  @param  Vector          The 8-bit interrupt vector associated with the MSI.  \r
+                          Must be in the range 0x10..0xFE\r
+  @param  DeliveryMode    A 3-bit value that specifies how the recept of the MSI \r
+                          is handled.  The only supported values are:\r
+                            0: LOCAL_APIC_DELIVERY_MODE_FIXED\r
+                            1: LOCAL_APIC_DELIVERY_MODE_LOWEST_PRIORITY\r
+                            2: LOCAL_APIC_DELIVERY_MODE_SMI\r
+                            4: LOCAL_APIC_DELIVERY_MODE_NMI\r
+                            5: LOCAL_APIC_DELIVERY_MODE_INIT\r
+                            7: LOCAL_APIC_DELIVERY_MODE_EXTINT\r
+                          \r
+  @param  LevelTriggered  TRUE specifies a level triggered interrupt.  \r
+                          FALSE specifies an edge triggered interrupt.\r
+  @param  AssertionLevel  Ignored if LevelTriggered is FALSE.\r
+                          TRUE specifies a level triggered interrupt that active \r
+                          when the interrupt line is asserted.\r
+                          FALSE specifies a level triggered interrupt that active \r
+                          when the interrupt line is deasserted.\r
+\r
+  @return 64-bit data value used to send an MSI to the Local APIC.\r
+**/\r
+UINT64\r
+EFIAPI    \r
+GetApicMsiValue (\r
+  IN UINT8    Vector,\r
+  IN UINTN    DeliveryMode,\r
+  IN BOOLEAN  LevelTriggered,\r
+  IN BOOLEAN  AssertionLevel\r
+  )\r
+{\r
+  LOCAL_APIC_MSI_DATA  MsiData;\r
+\r
+  ASSERT (Vector >= 0x10 && Vector <= 0xFE);\r
+  ASSERT (DeliveryMode < 8 && DeliveryMode != 6 && DeliveryMode != 3);\r
+  \r
+  MsiData.Uint64            = 0;\r
+  MsiData.Bits.Vector       = Vector;\r
+  MsiData.Bits.DeliveryMode = (UINT32)DeliveryMode;\r
+  if (LevelTriggered) {\r
+    MsiData.Bits.TriggerMode = 1;\r
+    if (AssertionLevel) {\r
+      MsiData.Bits.Level = 1;\r
+    }\r
+  }\r
+  return MsiData.Uint64;\r
+}\r