]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/BaseXApicLib/BaseXApicLib.c
MdePkg/BaseXApicLib: Support IA32 processors without MSR_IA32_APIC_BASE_ADDRESS
[mirror_edk2.git] / UefiCpuPkg / Library / BaseXApicLib / BaseXApicLib.c
index 16dbd4be6ed47a4154e635d0734d74ee9e768c0e..f219b07888c0873a5279e8c3c83349de17aecbb8 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
 // 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 +137,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 +164,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 +182,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 +245,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 +270,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 +287,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 +299,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,12 +338,79 @@ 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
+/**\r
+  Get the value of the local APIC version register.\r
+\r
+  @return  the value of the local APIC version register.\r
+**/\r
+UINT32\r
+EFIAPI\r
+GetApicVersion (\r
+  VOID\r
+  )\r
+{\r
+  return ReadLocalApicReg (XAPIC_VERSION_OFFSET);\r
+}\r
+\r
+/**\r
+  Send a Fixed IPI to a specified target processor.\r
+\r
+  This function returns after the IPI has been accepted by the target processor. \r
+\r
+  @param  ApicId   The local APIC ID of the target processor.\r
+  @param  Vector   The vector number of the interrupt being sent.\r
+**/\r
+VOID\r
+EFIAPI\r
+SendFixedIpi (\r
+  IN UINT32          ApicId,\r
+  IN UINT8           Vector\r
+  )\r
+{\r
+  LOCAL_APIC_ICR_LOW IcrLow;\r
+\r
+  IcrLow.Uint32 = 0;\r
+  IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_FIXED;\r
+  IcrLow.Bits.Level = 1;\r
+  IcrLow.Bits.Vector = Vector;\r
+  SendIpi (IcrLow.Uint32, ApicId);\r
+}\r
+\r
+/**\r
+  Send a Fixed IPI to all processors excluding self.\r
+\r
+  This function returns after the IPI has been accepted by the target processors. \r
+\r
+  @param  Vector   The vector number of the interrupt being sent.\r
+**/\r
+VOID\r
+EFIAPI\r
+SendFixedIpiAllExcludingSelf (\r
+  IN UINT8           Vector\r
+  )\r
+{\r
+  LOCAL_APIC_ICR_LOW IcrLow;\r
+\r
+  IcrLow.Uint32 = 0;\r
+  IcrLow.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_FIXED;\r
+  IcrLow.Bits.Level = 1;\r
+  IcrLow.Bits.DestinationShorthand = LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF;\r
+  IcrLow.Bits.Vector = Vector;\r
+  SendIpi (IcrLow.Uint32, 0);\r
+}\r
+\r
 /**\r
   Send a SMI IPI to a specified target processor.\r
 \r
@@ -309,7 +518,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
@@ -342,7 +551,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
@@ -381,43 +590,44 @@ ProgramVirtualWireMode (
   //\r
   // Program the LINT0 vector entry as ExtInt. Not masked, edge, active high.\r
   //\r
-  Lint.Uint32 = ReadLocalApicReg (XAPIC_LINT0_VECTOR_OFFSET);\r
+  Lint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET);\r
   Lint.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_EXTINT;\r
   Lint.Bits.InputPinPolarity = 0;\r
   Lint.Bits.TriggerMode = 0;\r
   Lint.Bits.Mask = 0;\r
-  WriteLocalApicReg (XAPIC_LINT0_VECTOR_OFFSET, Lint.Uint32);\r
+  WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET, Lint.Uint32);\r
 \r
   //\r
   // Program the LINT0 vector entry as NMI. Not masked, edge, active high.\r
   //\r
-  Lint.Uint32 = ReadLocalApicReg (XAPIC_LINT1_VECTOR_OFFSET);\r
+  Lint.Uint32 = ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET);\r
   Lint.Bits.DeliveryMode = LOCAL_APIC_DELIVERY_MODE_NMI;\r
   Lint.Bits.InputPinPolarity = 0;\r
   Lint.Bits.TriggerMode = 0;\r
   Lint.Bits.Mask = 0;\r
-  WriteLocalApicReg (XAPIC_LINT1_VECTOR_OFFSET, Lint.Uint32);\r
+  WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET, Lint.Uint32);\r
 }\r
 \r
 /**\r
-  Get the divide value from the DCR (Divide Configuration Register) by which\r
-  the processor's bus clock is divided to form the time base for the APIC timer.\r
+  Disable LINT0 & LINT1 interrupts.\r
 \r
-  @return The divide value is one of 1,2,4,8,16,32,64,128.\r
+  This function sets the mask flag in the LVT LINT0 & LINT1 registers.\r
 **/\r
-UINTN\r
+VOID\r
 EFIAPI\r
-GetApicTimerDivisor (\r
+DisableLvtInterrupts (\r
   VOID\r
   )\r
 {\r
-  UINT32         DivideValue;\r
-  LOCAL_APIC_DCR Dcr;\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
-  Dcr.Uint32 = ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET);\r
-  DivideValue = Dcr.Bits.DivideValue1 | (Dcr.Bits.DivideValue2 << 2);\r
-  DivideValue = (DivideValue + 1) & 0x7;\r
-  return ((UINTN)1) << DivideValue;\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
@@ -510,6 +720,47 @@ InitializeApicTimer (
   WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET, LvtTimer.Uint32);\r
 }\r
 \r
+/**\r
+  Get the state of the local APIC timer.\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
+**/\r
+VOID\r
+EFIAPI\r
+GetApicTimerState (\r
+  OUT UINTN    *DivideValue  OPTIONAL,\r
+  OUT BOOLEAN  *PeriodicMode  OPTIONAL,\r
+  OUT UINT8    *Vector  OPTIONAL\r
+  )\r
+{\r
+  UINT32 Divisor;\r
+  LOCAL_APIC_DCR Dcr;\r
+  LOCAL_APIC_LVT_TIMER LvtTimer;\r
+\r
+  if (DivideValue != NULL) {\r
+    Dcr.Uint32 = ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET);\r
+    Divisor = Dcr.Bits.DivideValue1 | (Dcr.Bits.DivideValue2 << 2);\r
+    Divisor = (Divisor + 1) & 0x7;\r
+    *DivideValue = ((UINTN)1) << Divisor;\r
+  }\r
+\r
+  if (PeriodicMode != NULL || Vector != NULL) {\r
+    LvtTimer.Uint32 = ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET);\r
+    if (PeriodicMode != NULL) {\r
+      if (LvtTimer.Bits.TimerMode == 1) {\r
+        *PeriodicMode = TRUE;\r
+      } else {\r
+        *PeriodicMode = FALSE;\r
+      }\r
+    }\r
+    if (Vector != NULL) {\r
+      *Vector = (UINT8) LvtTimer.Bits.Vector;\r
+    }\r
+  }\r
+}\r
+\r
 /**\r
   Enable the local APIC timer interrupt.\r
 **/\r
@@ -572,3 +823,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