]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/AcpiPlatformDxe/Qemu.c
NetworkPkg: Convert non DOS format files to DOS format
[mirror_edk2.git] / OvmfPkg / AcpiPlatformDxe / Qemu.c
index e483856dca95d4e4f2cda79e5757295fafb7bea4..70f3ff6426697041bb49b58bd6dcea868315af7a 100644 (file)
@@ -2,6 +2,9 @@
   OVMF ACPI QEMU support\r
 \r
   Copyright (c) 2008 - 2012, Intel Corporation. All rights reserved.<BR>\r
+\r
+  Copyright (C) 2012-2014, Red Hat, Inc.\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
   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
-**/ \r
+**/\r
 \r
 #include "AcpiPlatform.h"\r
+#include "QemuLoader.h"\r
 #include <Library/BaseMemoryLib.h>\r
 #include <Library/MemoryAllocationLib.h>\r
 #include <Library/QemuFwCfgLib.h>\r
-\r
+#include <Library/DxeServicesTableLib.h>\r
+#include <Library/PcdLib.h>\r
+#include <IndustryStandard/Acpi.h>\r
 \r
 BOOLEAN\r
 QemuDetected (\r
@@ -31,6 +37,27 @@ QemuDetected (
 }\r
 \r
 \r
+STATIC\r
+UINTN\r
+CountBits16 (\r
+  UINT16 Mask\r
+  )\r
+{\r
+  //\r
+  // For all N >= 1, N bits are enough to represent the number of bits set\r
+  // among N bits. It's true for N == 1. When adding a new bit (N := N+1),\r
+  // the maximum number of possibly set bits increases by one, while the\r
+  // representable maximum doubles.\r
+  //\r
+  Mask = ((Mask & 0xAAAA) >> 1) + (Mask & 0x5555);\r
+  Mask = ((Mask & 0xCCCC) >> 2) + (Mask & 0x3333);\r
+  Mask = ((Mask & 0xF0F0) >> 4) + (Mask & 0x0F0F);\r
+  Mask = ((Mask & 0xFF00) >> 8) + (Mask & 0x00FF);\r
+\r
+  return Mask;\r
+}\r
+\r
+\r
 STATIC\r
 EFI_STATUS\r
 EFIAPI\r
@@ -41,58 +68,417 @@ QemuInstallAcpiMadtTable (
   OUT  UINTN                         *TableKey\r
   )\r
 {\r
-  EFI_STATUS                                   Status;\r
-  UINTN                                        Count;\r
-  UINTN                                        Loop;\r
-  EFI_ACPI_DESCRIPTION_HEADER                  *Hdr;\r
-  UINTN                                        NewBufferSize;\r
-  EFI_ACPI_1_0_PROCESSOR_LOCAL_APIC_STRUCTURE  *LocalApic;\r
+  UINTN                                               CpuCount;\r
+  UINTN                                               PciLinkIsoCount;\r
+  UINTN                                               NewBufferSize;\r
+  EFI_ACPI_1_0_MULTIPLE_APIC_DESCRIPTION_TABLE_HEADER *Madt;\r
+  EFI_ACPI_1_0_PROCESSOR_LOCAL_APIC_STRUCTURE         *LocalApic;\r
+  EFI_ACPI_1_0_IO_APIC_STRUCTURE                      *IoApic;\r
+  EFI_ACPI_1_0_INTERRUPT_SOURCE_OVERRIDE_STRUCTURE    *Iso;\r
+  EFI_ACPI_1_0_LOCAL_APIC_NMI_STRUCTURE               *LocalApicNmi;\r
+  VOID                                                *Ptr;\r
+  UINTN                                               Loop;\r
+  EFI_STATUS                                          Status;\r
+\r
+  ASSERT (AcpiTableBufferSize >= sizeof (EFI_ACPI_DESCRIPTION_HEADER));\r
 \r
   QemuFwCfgSelectItem (QemuFwCfgItemSmpCpuCount);\r
-  Count = (UINTN) QemuFwCfgRead16 ();\r
-  ASSERT (Count >= 1);\r
+  CpuCount = QemuFwCfgRead16 ();\r
+  ASSERT (CpuCount >= 1);\r
 \r
-  if (Count == 1) {\r
-    //\r
-    // The pre-built MADT table covers the single CPU case\r
-    //\r
-    return InstallAcpiTable (\r
-             AcpiProtocol,\r
-             AcpiTableBuffer,\r
-             AcpiTableBufferSize,\r
-             TableKey\r
-             );\r
+  //\r
+  // Set Level-tiggered, Active High for these identity mapped IRQs. The bitset\r
+  // corresponds to the union of all possible interrupt assignments for the LNKA,\r
+  // LNKB, LNKC, LNKD PCI interrupt lines. See the DSDT.\r
+  //\r
+  PciLinkIsoCount = CountBits16 (PcdGet16 (Pcd8259LegacyModeEdgeLevel));\r
+\r
+  NewBufferSize = 1                     * sizeof (*Madt) +\r
+                  CpuCount              * sizeof (*LocalApic) +\r
+                  1                     * sizeof (*IoApic) +\r
+                  (1 + PciLinkIsoCount) * sizeof (*Iso) +\r
+                  1                     * sizeof (*LocalApicNmi);\r
+\r
+  Madt = AllocatePool (NewBufferSize);\r
+  if (Madt == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  CopyMem (&(Madt->Header), AcpiTableBuffer, sizeof (EFI_ACPI_DESCRIPTION_HEADER));\r
+  Madt->Header.Length    = (UINT32) NewBufferSize;\r
+  Madt->LocalApicAddress = PcdGet32 (PcdCpuLocalApicBaseAddress);\r
+  Madt->Flags            = EFI_ACPI_1_0_PCAT_COMPAT;\r
+  Ptr = Madt + 1;\r
+\r
+  LocalApic = Ptr;\r
+  for (Loop = 0; Loop < CpuCount; ++Loop) {\r
+    LocalApic->Type            = EFI_ACPI_1_0_PROCESSOR_LOCAL_APIC;\r
+    LocalApic->Length          = sizeof (*LocalApic);\r
+    LocalApic->AcpiProcessorId = (UINT8) Loop;\r
+    LocalApic->ApicId          = (UINT8) Loop;\r
+    LocalApic->Flags           = 1; // enabled\r
+    ++LocalApic;\r
+  }\r
+  Ptr = LocalApic;\r
+\r
+  IoApic = Ptr;\r
+  IoApic->Type             = EFI_ACPI_1_0_IO_APIC;\r
+  IoApic->Length           = sizeof (*IoApic);\r
+  IoApic->IoApicId         = (UINT8) CpuCount;\r
+  IoApic->Reserved         = EFI_ACPI_RESERVED_BYTE;\r
+  IoApic->IoApicAddress    = 0xFEC00000;\r
+  IoApic->SystemVectorBase = 0x00000000;\r
+  Ptr = IoApic + 1;\r
+\r
+  //\r
+  // IRQ0 (8254 Timer) => IRQ2 (PIC) Interrupt Source Override Structure\r
+  //\r
+  Iso = Ptr;\r
+  Iso->Type                        = EFI_ACPI_1_0_INTERRUPT_SOURCE_OVERRIDE;\r
+  Iso->Length                      = sizeof (*Iso);\r
+  Iso->Bus                         = 0x00; // ISA\r
+  Iso->Source                      = 0x00; // IRQ0\r
+  Iso->GlobalSystemInterruptVector = 0x00000002;\r
+  Iso->Flags                       = 0x0000; // Conforms to specs of the bus\r
+  ++Iso;\r
+\r
+  //\r
+  // Set Level-tiggered, Active High for all possible PCI link targets.\r
+  //\r
+  for (Loop = 0; Loop < 16; ++Loop) {\r
+    if ((PcdGet16 (Pcd8259LegacyModeEdgeLevel) & (1 << Loop)) == 0) {\r
+      continue;\r
+    }\r
+    Iso->Type                        = EFI_ACPI_1_0_INTERRUPT_SOURCE_OVERRIDE;\r
+    Iso->Length                      = sizeof (*Iso);\r
+    Iso->Bus                         = 0x00; // ISA\r
+    Iso->Source                      = (UINT8) Loop;\r
+    Iso->GlobalSystemInterruptVector = (UINT32) Loop;\r
+    Iso->Flags                       = 0x000D; // Level-tiggered, Active High\r
+    ++Iso;\r
+  }\r
+  ASSERT (\r
+    (UINTN) (Iso - (EFI_ACPI_1_0_INTERRUPT_SOURCE_OVERRIDE_STRUCTURE *)Ptr) ==\r
+      1 + PciLinkIsoCount\r
+    );\r
+  Ptr = Iso;\r
+\r
+  LocalApicNmi = Ptr;\r
+  LocalApicNmi->Type            = EFI_ACPI_1_0_LOCAL_APIC_NMI;\r
+  LocalApicNmi->Length          = sizeof (*LocalApicNmi);\r
+  LocalApicNmi->AcpiProcessorId = 0xFF; // applies to all processors\r
+  //\r
+  // polarity and trigger mode of the APIC I/O input signals conform to the\r
+  // specifications of the bus\r
+  //\r
+  LocalApicNmi->Flags           = 0x0000;\r
+  //\r
+  // Local APIC interrupt input LINTn to which NMI is connected.\r
+  //\r
+  LocalApicNmi->LocalApicInti   = 0x01;\r
+  Ptr = LocalApicNmi + 1;\r
+\r
+  ASSERT ((UINTN) ((UINT8 *)Ptr - (UINT8 *)Madt) == NewBufferSize);\r
+  Status = InstallAcpiTable (AcpiProtocol, Madt, NewBufferSize, TableKey);\r
+\r
+  FreePool (Madt);\r
+\r
+  return Status;\r
+}\r
+\r
+\r
+#pragma pack(1)\r
+\r
+typedef struct {\r
+  UINT64 Base;\r
+  UINT64 End;\r
+  UINT64 Length;\r
+} PCI_WINDOW;\r
+\r
+typedef struct {\r
+  PCI_WINDOW PciWindow32;\r
+  PCI_WINDOW PciWindow64;\r
+} FIRMWARE_DATA;\r
+\r
+typedef struct {\r
+  UINT8 BytePrefix;\r
+  UINT8 ByteValue;\r
+} AML_BYTE;\r
+\r
+typedef struct {\r
+  UINT8    NameOp;\r
+  UINT8    RootChar;\r
+  UINT8    NameChar[4];\r
+  UINT8    PackageOp;\r
+  UINT8    PkgLength;\r
+  UINT8    NumElements;\r
+  AML_BYTE Pm1aCntSlpTyp;\r
+  AML_BYTE Pm1bCntSlpTyp;\r
+  AML_BYTE Reserved[2];\r
+} SYSTEM_STATE_PACKAGE;\r
+\r
+#pragma pack()\r
+\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+PopulateFwData(\r
+  OUT  FIRMWARE_DATA *FwData\r
+  )\r
+{\r
+  EFI_STATUS                      Status;\r
+  UINTN                           NumDesc;\r
+  EFI_GCD_MEMORY_SPACE_DESCRIPTOR *AllDesc;\r
+\r
+  Status = gDS->GetMemorySpaceMap (&NumDesc, &AllDesc);\r
+  if (Status == EFI_SUCCESS) {\r
+    UINT64 NonMmio32MaxExclTop;\r
+    UINT64 Mmio32MinBase;\r
+    UINT64 Mmio32MaxExclTop;\r
+    UINTN CurDesc;\r
+\r
+    Status = EFI_UNSUPPORTED;\r
+\r
+    NonMmio32MaxExclTop = 0;\r
+    Mmio32MinBase = BASE_4GB;\r
+    Mmio32MaxExclTop = 0;\r
+\r
+    for (CurDesc = 0; CurDesc < NumDesc; ++CurDesc) {\r
+      CONST EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Desc;\r
+      UINT64 ExclTop;\r
+\r
+      Desc = &AllDesc[CurDesc];\r
+      ExclTop = Desc->BaseAddress + Desc->Length;\r
+\r
+      if (ExclTop <= (UINT64) PcdGet32 (PcdOvmfFdBaseAddress)) {\r
+        switch (Desc->GcdMemoryType) {\r
+          case EfiGcdMemoryTypeNonExistent:\r
+            break;\r
+\r
+          case EfiGcdMemoryTypeReserved:\r
+          case EfiGcdMemoryTypeSystemMemory:\r
+            if (NonMmio32MaxExclTop < ExclTop) {\r
+              NonMmio32MaxExclTop = ExclTop;\r
+            }\r
+            break;\r
+\r
+          case EfiGcdMemoryTypeMemoryMappedIo:\r
+            if (Mmio32MinBase > Desc->BaseAddress) {\r
+              Mmio32MinBase = Desc->BaseAddress;\r
+            }\r
+            if (Mmio32MaxExclTop < ExclTop) {\r
+              Mmio32MaxExclTop = ExclTop;\r
+            }\r
+            break;\r
+\r
+          default:\r
+            ASSERT(0);\r
+        }\r
+      }\r
+    }\r
+\r
+    if (Mmio32MinBase < NonMmio32MaxExclTop) {\r
+      Mmio32MinBase = NonMmio32MaxExclTop;\r
+    }\r
+\r
+    if (Mmio32MinBase < Mmio32MaxExclTop) {\r
+      FwData->PciWindow32.Base   = Mmio32MinBase;\r
+      FwData->PciWindow32.End    = Mmio32MaxExclTop - 1;\r
+      FwData->PciWindow32.Length = Mmio32MaxExclTop - Mmio32MinBase;\r
+\r
+      FwData->PciWindow64.Base   = 0;\r
+      FwData->PciWindow64.End    = 0;\r
+      FwData->PciWindow64.Length = 0;\r
+\r
+      Status = EFI_SUCCESS;\r
+    }\r
+\r
+    FreePool (AllDesc);\r
   }\r
 \r
+  DEBUG ((\r
+    DEBUG_INFO,\r
+    "ACPI PciWindow32: Base=0x%08lx End=0x%08lx Length=0x%08lx\n",\r
+    FwData->PciWindow32.Base,\r
+    FwData->PciWindow32.End,\r
+    FwData->PciWindow32.Length\r
+    ));\r
+  DEBUG ((\r
+    DEBUG_INFO,\r
+    "ACPI PciWindow64: Base=0x%08lx End=0x%08lx Length=0x%08lx\n",\r
+    FwData->PciWindow64.Base,\r
+    FwData->PciWindow64.End,\r
+    FwData->PciWindow64.Length\r
+    ));\r
+\r
+  return Status;\r
+}\r
+\r
+\r
+STATIC\r
+VOID\r
+EFIAPI\r
+GetSuspendStates (\r
+  UINTN                *SuspendToRamSize,\r
+  SYSTEM_STATE_PACKAGE *SuspendToRam,\r
+  UINTN                *SuspendToDiskSize,\r
+  SYSTEM_STATE_PACKAGE *SuspendToDisk\r
+  )\r
+{\r
+  STATIC CONST SYSTEM_STATE_PACKAGE Template = {\r
+    0x08,                   // NameOp\r
+    '\\',                   // RootChar\r
+    { '_', 'S', 'x', '_' }, // NameChar[4]\r
+    0x12,                   // PackageOp\r
+    0x0A,                   // PkgLength\r
+    0x04,                   // NumElements\r
+    { 0x0A, 0x00 },         // Pm1aCntSlpTyp\r
+    { 0x0A, 0x00 },         // Pm1bCntSlpTyp -- we don't support it\r
+    {                       // Reserved[2]\r
+      { 0x0A, 0x00 },\r
+      { 0x0A, 0x00 }\r
+    }\r
+  };\r
+  RETURN_STATUS                     Status;\r
+  FIRMWARE_CONFIG_ITEM              FwCfgItem;\r
+  UINTN                             FwCfgSize;\r
+  UINT8                             SystemStates[6];\r
+\r
   //\r
-  // We need to add additional Local APIC entries to the MADT\r
+  // configure defaults\r
   //\r
-  NewBufferSize = AcpiTableBufferSize + ((Count - 1) * sizeof (*LocalApic));\r
-  Hdr = (EFI_ACPI_DESCRIPTION_HEADER*) AllocatePool (NewBufferSize);\r
-  ASSERT (Hdr != NULL);\r
+  *SuspendToRamSize = sizeof Template;\r
+  CopyMem (SuspendToRam, &Template, sizeof Template);\r
+  SuspendToRam->NameChar[2]             = '3'; // S3\r
+  SuspendToRam->Pm1aCntSlpTyp.ByteValue = 1;   // PIIX4: STR\r
 \r
-  CopyMem (Hdr, AcpiTableBuffer, AcpiTableBufferSize);\r
+  *SuspendToDiskSize = sizeof Template;\r
+  CopyMem (SuspendToDisk, &Template, sizeof Template);\r
+  SuspendToDisk->NameChar[2]             = '4'; // S4\r
+  SuspendToDisk->Pm1aCntSlpTyp.ByteValue = 2;   // PIIX4: POSCL\r
 \r
-  LocalApic = (EFI_ACPI_1_0_PROCESSOR_LOCAL_APIC_STRUCTURE*)\r
-                (((UINT8*) Hdr) + AcpiTableBufferSize);\r
+  //\r
+  // check for overrides\r
+  //\r
+  Status = QemuFwCfgFindFile ("etc/system-states", &FwCfgItem, &FwCfgSize);\r
+  if (Status != RETURN_SUCCESS || FwCfgSize != sizeof SystemStates) {\r
+    DEBUG ((DEBUG_INFO, "ACPI using S3/S4 defaults\n"));\r
+    return;\r
+  }\r
+  QemuFwCfgSelectItem (FwCfgItem);\r
+  QemuFwCfgReadBytes (sizeof SystemStates, SystemStates);\r
 \r
   //\r
-  // Add Local APIC entries for the APs to the MADT\r
+  // Each byte corresponds to a system state. In each byte, the MSB tells us\r
+  // whether the given state is enabled. If so, the three LSBs specify the\r
+  // value to be written to the PM control register's SUS_TYP bits.\r
   //\r
-  for (Loop = 1; Loop < Count; Loop++) {\r
-    LocalApic->Type = EFI_ACPI_1_0_PROCESSOR_LOCAL_APIC;\r
-    LocalApic->Length = sizeof (*LocalApic);\r
-    LocalApic->AcpiProcessorId = Loop;\r
-    LocalApic->ApicId = Loop;\r
-    LocalApic->Flags = 1;\r
-    LocalApic++;\r
+  if (SystemStates[3] & BIT7) {\r
+    SuspendToRam->Pm1aCntSlpTyp.ByteValue =\r
+        SystemStates[3] & (BIT2 | BIT1 | BIT0);\r
+    DEBUG ((DEBUG_INFO, "ACPI S3 value: %d\n",\r
+            SuspendToRam->Pm1aCntSlpTyp.ByteValue));\r
+  } else {\r
+    *SuspendToRamSize = 0;\r
+    DEBUG ((DEBUG_INFO, "ACPI S3 disabled\n"));\r
   }\r
 \r
-  Hdr->Length = NewBufferSize;\r
+  if (SystemStates[4] & BIT7) {\r
+    SuspendToDisk->Pm1aCntSlpTyp.ByteValue =\r
+        SystemStates[4] & (BIT2 | BIT1 | BIT0);\r
+    DEBUG ((DEBUG_INFO, "ACPI S4 value: %d\n",\r
+            SuspendToDisk->Pm1aCntSlpTyp.ByteValue));\r
+  } else {\r
+    *SuspendToDiskSize = 0;\r
+    DEBUG ((DEBUG_INFO, "ACPI S4 disabled\n"));\r
+  }\r
+}\r
 \r
-  Status = InstallAcpiTable (AcpiProtocol, Hdr, NewBufferSize, TableKey);\r
 \r
-  FreePool (Hdr);\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+QemuInstallAcpiSsdtTable (\r
+  IN   EFI_ACPI_TABLE_PROTOCOL       *AcpiProtocol,\r
+  IN   VOID                          *AcpiTableBuffer,\r
+  IN   UINTN                         AcpiTableBufferSize,\r
+  OUT  UINTN                         *TableKey\r
+  )\r
+{\r
+  EFI_STATUS    Status;\r
+  FIRMWARE_DATA *FwData;\r
+\r
+  Status = EFI_OUT_OF_RESOURCES;\r
+\r
+  FwData = AllocateReservedPool (sizeof (*FwData));\r
+  if (FwData != NULL) {\r
+    UINTN                SuspendToRamSize;\r
+    SYSTEM_STATE_PACKAGE SuspendToRam;\r
+    UINTN                SuspendToDiskSize;\r
+    SYSTEM_STATE_PACKAGE SuspendToDisk;\r
+    UINTN                SsdtSize;\r
+    UINT8                *Ssdt;\r
+\r
+    GetSuspendStates (&SuspendToRamSize,  &SuspendToRam,\r
+                      &SuspendToDiskSize, &SuspendToDisk);\r
+    SsdtSize = AcpiTableBufferSize + 17 + SuspendToRamSize + SuspendToDiskSize;\r
+    Ssdt = AllocatePool (SsdtSize);\r
+\r
+    if (Ssdt != NULL) {\r
+      Status = PopulateFwData (FwData);\r
+\r
+      if (Status == EFI_SUCCESS) {\r
+        UINT8 *SsdtPtr;\r
+\r
+        SsdtPtr = Ssdt;\r
+\r
+        CopyMem (SsdtPtr, AcpiTableBuffer, AcpiTableBufferSize);\r
+        SsdtPtr += AcpiTableBufferSize;\r
+\r
+        //\r
+        // build "OperationRegion(FWDT, SystemMemory, 0x12345678, 0x87654321)"\r
+        //\r
+        *(SsdtPtr++) = 0x5B; // ExtOpPrefix\r
+        *(SsdtPtr++) = 0x80; // OpRegionOp\r
+        *(SsdtPtr++) = 'F';\r
+        *(SsdtPtr++) = 'W';\r
+        *(SsdtPtr++) = 'D';\r
+        *(SsdtPtr++) = 'T';\r
+        *(SsdtPtr++) = 0x00; // SystemMemory\r
+        *(SsdtPtr++) = 0x0C; // DWordPrefix\r
+\r
+        //\r
+        // no virtual addressing yet, take the four least significant bytes\r
+        //\r
+        CopyMem(SsdtPtr, &FwData, 4);\r
+        SsdtPtr += 4;\r
+\r
+        *(SsdtPtr++) = 0x0C; // DWordPrefix\r
+\r
+        *(UINT32*) SsdtPtr = sizeof (*FwData);\r
+        SsdtPtr += 4;\r
+\r
+        //\r
+        // add suspend system states\r
+        //\r
+        CopyMem (SsdtPtr, &SuspendToRam, SuspendToRamSize);\r
+        SsdtPtr += SuspendToRamSize;\r
+        CopyMem (SsdtPtr, &SuspendToDisk, SuspendToDiskSize);\r
+        SsdtPtr += SuspendToDiskSize;\r
+\r
+        ASSERT((UINTN) (SsdtPtr - Ssdt) == SsdtSize);\r
+        ((EFI_ACPI_DESCRIPTION_HEADER *) Ssdt)->Length = (UINT32) SsdtSize;\r
+        Status = InstallAcpiTable (AcpiProtocol, Ssdt, SsdtSize, TableKey);\r
+      }\r
+\r
+      FreePool(Ssdt);\r
+    }\r
+\r
+    if (Status != EFI_SUCCESS) {\r
+      FreePool(FwData);\r
+    }\r
+  }\r
 \r
   return Status;\r
 }\r
@@ -115,6 +501,9 @@ QemuInstallAcpiTable (
   case EFI_ACPI_1_0_APIC_SIGNATURE:\r
     TableInstallFunction = QemuInstallAcpiMadtTable;\r
     break;\r
+  case EFI_ACPI_1_0_SECONDARY_SYSTEM_DESCRIPTION_TABLE_SIGNATURE:\r
+    TableInstallFunction = QemuInstallAcpiSsdtTable;\r
+    break;\r
   default:\r
     TableInstallFunction = InstallAcpiTable;\r
   }\r
@@ -127,3 +516,368 @@ QemuInstallAcpiTable (
            );\r
 }\r
 \r
+\r
+/**\r
+  Check if an array of bytes starts with an RSD PTR structure.\r
+\r
+  Checksum is ignored.\r
+\r
+  @param[in] Buffer     The array to check.\r
+\r
+  @param[in] Size       Number of bytes in Buffer.\r
+\r
+  @param[out] RsdpSize  If the function returns EFI_SUCCESS, this parameter\r
+                        contains the size of the detected RSD PTR structure.\r
+\r
+  @retval  EFI_SUCCESS         RSD PTR structure detected at the beginning of\r
+                               Buffer, and its advertised size does not exceed\r
+                               Size.\r
+\r
+  @retval  EFI_PROTOCOL_ERROR  RSD PTR structure detected at the beginning of\r
+                               Buffer, but it has inconsistent size.\r
+\r
+  @retval  EFI_NOT_FOUND       RSD PTR structure not found.\r
+\r
+**/\r
+\r
+STATIC\r
+EFI_STATUS\r
+CheckRsdp (\r
+  IN  CONST VOID *Buffer,\r
+  IN  UINTN      Size,\r
+  OUT UINTN      *RsdpSize\r
+  )\r
+{\r
+  CONST UINT64                                       *Signature;\r
+  CONST EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER *Rsdp1;\r
+  CONST EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER *Rsdp2;\r
+\r
+  if (Size < sizeof *Signature) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+  Signature = Buffer;\r
+\r
+  if (*Signature != EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER_SIGNATURE) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  //\r
+  // Signature found -- from this point on we can only report\r
+  // EFI_PROTOCOL_ERROR or EFI_SUCCESS.\r
+  //\r
+  if (Size < sizeof *Rsdp1) {\r
+    return EFI_PROTOCOL_ERROR;\r
+  }\r
+  Rsdp1 = Buffer;\r
+\r
+  if (Rsdp1->Reserved == 0) {\r
+    //\r
+    // ACPI 1.0 doesn't include the Length field\r
+    //\r
+    *RsdpSize = sizeof *Rsdp1;\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  if (Size < sizeof *Rsdp2) {\r
+    return EFI_PROTOCOL_ERROR;\r
+  }\r
+  Rsdp2 = Buffer;\r
+\r
+  if (Size < Rsdp2->Length || Rsdp2->Length < sizeof *Rsdp2) {\r
+    return EFI_PROTOCOL_ERROR;\r
+  }\r
+\r
+  *RsdpSize = Rsdp2->Length;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+//\r
+// We'll be saving the keys of installed tables so that we can roll them back\r
+// in case of failure. 128 tables should be enough for anyone (TM).\r
+//\r
+#define INSTALLED_TABLES_MAX 128\r
+\r
+/**\r
+  Download one ACPI table data file from QEMU and interpret it.\r
+\r
+  @param[in] FwCfgFile         The NUL-terminated name of the fw_cfg file to\r
+                               download and interpret.\r
+\r
+  @param[in] AcpiProtocol      The ACPI table protocol used to install tables.\r
+\r
+  @param[in,out] InstalledKey  On input, an array of INSTALLED_TABLES_MAX UINTN\r
+                               elements, allocated by the caller. On output,\r
+                               the function will have stored (appended) the\r
+                               AcpiProtocol-internal keys of the ACPI tables\r
+                               that the function has installed from the fw_cfg\r
+                               file. The array reflects installed tables even\r
+                               if the function returns with an error.\r
+\r
+  @param[in,out] NumInstalled  On input, the number of entries already used in\r
+                               InstalledKey; it must be in [0,\r
+                               INSTALLED_TABLES_MAX] inclusive. On output, the\r
+                               parameter is updated to the new cumulative count\r
+                               of the keys stored in InstalledKey; the value\r
+                               reflects installed tables even if the function\r
+                               returns with an error.\r
+\r
+  @retval  EFI_INVALID_PARAMETER  NumInstalled is outside the allowed range on\r
+                                  input.\r
+\r
+  @retval  EFI_UNSUPPORTED        Firmware configuration is unavailable.\r
+\r
+  @retval  EFI_NOT_FOUND          The host doesn't export the requested fw_cfg\r
+                                  file.\r
+\r
+  @retval  EFI_OUT_OF_RESOURCES   Memory allocation failed, or no more room in\r
+                                  InstalledKey.\r
+\r
+  @retval  EFI_PROTOCOL_ERROR     Found truncated or invalid ACPI table header\r
+                                  in the fw_cfg contents.\r
+\r
+  @return                         Status codes returned by\r
+                                  AcpiProtocol->InstallAcpiTable().\r
+\r
+**/\r
+\r
+STATIC\r
+EFI_STATUS\r
+InstallQemuLinkedTables (\r
+  IN     CONST CHAR8             *FwCfgFile,\r
+  IN     EFI_ACPI_TABLE_PROTOCOL *AcpiProtocol,\r
+  IN OUT UINTN                   InstalledKey[INSTALLED_TABLES_MAX],\r
+  IN OUT INT32                   *NumInstalled\r
+  )\r
+{\r
+  EFI_STATUS           Status;\r
+  FIRMWARE_CONFIG_ITEM TablesFile;\r
+  UINTN                TablesFileSize;\r
+  UINT8                *Tables;\r
+  UINTN                Processed;\r
+\r
+  if (*NumInstalled < 0 || *NumInstalled > INSTALLED_TABLES_MAX) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Status = QemuFwCfgFindFile (FwCfgFile, &TablesFile, &TablesFileSize);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "%a: \"%a\" unavailable: %r\n", __FUNCTION__,\r
+      FwCfgFile, Status));\r
+    return Status;\r
+  }\r
+\r
+  Tables = AllocatePool (TablesFileSize);\r
+  if (Tables == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  QemuFwCfgSelectItem (TablesFile);\r
+  QemuFwCfgReadBytes (TablesFileSize, Tables);\r
+\r
+  Processed = 0;\r
+  while (Processed < TablesFileSize) {\r
+    UINTN                       Remaining;\r
+    UINTN                       RsdpSize;\r
+    EFI_ACPI_DESCRIPTION_HEADER *Probe;\r
+\r
+    Remaining = TablesFileSize - Processed;\r
+\r
+    //\r
+    // See if we're looking at an RSD PTR structure.\r
+    //\r
+    RsdpSize = 0;\r
+    Status = CheckRsdp (Tables + Processed, Remaining, &RsdpSize);\r
+    if (Status == EFI_PROTOCOL_ERROR) {\r
+      //\r
+      // RSD PTR found but its size is inconsistent; abort processing. (Note\r
+      // that "RSD PTR found" excludes the NUL-padding case by definition.)\r
+      //\r
+      break;\r
+    }\r
+    if (!EFI_ERROR (Status)) {\r
+      //\r
+      // Consistent RSD PTR found, skip it.\r
+      //\r
+      DEBUG ((EFI_D_VERBOSE, "%a: \"%a\" offset 0x%016Lx: RSD PTR "\r
+        "Length=0x%08x\n", __FUNCTION__, FwCfgFile, (UINT64)Processed,\r
+        (UINT32)RsdpSize));\r
+      Processed += RsdpSize;\r
+      continue;\r
+    }\r
+    ASSERT (Status == EFI_NOT_FOUND);\r
+\r
+    //\r
+    // What we're looking at is not an RSD PTR structure; attempt to parse it\r
+    // as an ACPI table.\r
+    //\r
+    if (Remaining < sizeof *Probe) {\r
+      Status = EFI_PROTOCOL_ERROR;\r
+      break;\r
+    }\r
+\r
+    Probe = (EFI_ACPI_DESCRIPTION_HEADER *) (Tables + Processed);\r
+    if (Remaining < Probe->Length || Probe->Length < sizeof *Probe) {\r
+      Status = EFI_PROTOCOL_ERROR;\r
+      break;\r
+    }\r
+\r
+    DEBUG ((EFI_D_VERBOSE, "%a: \"%a\" offset 0x%016Lx:"\r
+      " Signature=\"%-4.4a\" Length=0x%08x\n",\r
+      __FUNCTION__, FwCfgFile, (UINT64) Processed,\r
+      (CONST CHAR8 *) &Probe->Signature, Probe->Length));\r
+\r
+    //\r
+    // skip automatically handled "root" tables: RSDT, XSDT\r
+    //\r
+    if (Probe->Signature !=\r
+                        EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_TABLE_SIGNATURE &&\r
+        Probe->Signature !=\r
+                    EFI_ACPI_2_0_EXTENDED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE) {\r
+      if (*NumInstalled == INSTALLED_TABLES_MAX) {\r
+        DEBUG ((EFI_D_ERROR, "%a: can't install more than %d tables\n",\r
+          __FUNCTION__, INSTALLED_TABLES_MAX));\r
+        Status = EFI_OUT_OF_RESOURCES;\r
+        break;\r
+      }\r
+\r
+      Status = AcpiProtocol->InstallAcpiTable (AcpiProtocol, Probe,\r
+                 Probe->Length, &InstalledKey[*NumInstalled]);\r
+      if (EFI_ERROR (Status)) {\r
+        DEBUG ((EFI_D_ERROR,\r
+          "%a: failed to install table \"%-4.4a\" at \"%a\" offset 0x%Lx: "\r
+          "%r\n", __FUNCTION__, (CONST CHAR8 *)&Probe->Signature, FwCfgFile,\r
+          (UINT64) Processed, Status));\r
+        break;\r
+      }\r
+\r
+      ++*NumInstalled;\r
+    }\r
+\r
+    Processed += Probe->Length;\r
+  }\r
+\r
+  //\r
+  // NUL-padding at the end is accepted\r
+  //\r
+  if (Status == EFI_PROTOCOL_ERROR) {\r
+    UINTN ErrorLocation;\r
+\r
+    ErrorLocation = Processed;\r
+    while (Processed < TablesFileSize && Tables[Processed] == '\0') {\r
+      ++Processed;\r
+    }\r
+    if (Processed < TablesFileSize) {\r
+      DEBUG ((EFI_D_ERROR, "%a: truncated or invalid ACPI table header at "\r
+        "\"%a\" offset 0x%Lx\n", __FUNCTION__, FwCfgFile,\r
+        (UINT64)ErrorLocation));\r
+    }\r
+  }\r
+\r
+  if (Processed == TablesFileSize) {\r
+    Status = EFI_SUCCESS;\r
+  } else {\r
+    ASSERT (EFI_ERROR (Status));\r
+  }\r
+\r
+  FreePool (Tables);\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Download all ACPI table data files from QEMU and interpret them.\r
+\r
+  @param[in] AcpiProtocol  The ACPI table protocol used to install tables.\r
+\r
+  @retval  EFI_UNSUPPORTED       Firmware configuration is unavailable.\r
+\r
+  @retval  EFI_NOT_FOUND         The host doesn't export the required fw_cfg\r
+                                 files.\r
+\r
+  @retval  EFI_OUT_OF_RESOURCES  Memory allocation failed, or more than\r
+                                 INSTALLED_TABLES_MAX tables found.\r
+\r
+  @retval  EFI_PROTOCOL_ERROR    Found invalid fw_cfg contents.\r
+\r
+  @return                        Status codes returned by\r
+                                 AcpiProtocol->InstallAcpiTable().\r
+\r
+**/\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+InstallAllQemuLinkedTables (\r
+  IN   EFI_ACPI_TABLE_PROTOCOL       *AcpiProtocol\r
+  )\r
+{\r
+  UINTN                *InstalledKey;\r
+  INT32                Installed;\r
+  EFI_STATUS           Status;\r
+  FIRMWARE_CONFIG_ITEM LoaderItem;\r
+  UINTN                LoaderSize;\r
+  UINT8                *Loader;\r
+  QEMU_LOADER_ENTRY    *Entry, *End;\r
+\r
+  InstalledKey = AllocatePool (INSTALLED_TABLES_MAX * sizeof *InstalledKey);\r
+  if (InstalledKey == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  Installed = 0;\r
+\r
+  Status = QemuFwCfgFindFile ("etc/table-loader", &LoaderItem, &LoaderSize);\r
+  if (EFI_ERROR (Status)) {\r
+    goto FreeInstalledKey;\r
+  }\r
+  if (LoaderSize % sizeof *Entry != 0) {\r
+    Status = EFI_PROTOCOL_ERROR;\r
+    goto FreeInstalledKey;\r
+  }\r
+\r
+  Loader = AllocatePool (LoaderSize);\r
+  if (Loader == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto FreeInstalledKey;\r
+  }\r
+\r
+  QemuFwCfgSelectItem (LoaderItem);\r
+  QemuFwCfgReadBytes (LoaderSize, Loader);\r
+\r
+  Entry = (QEMU_LOADER_ENTRY *)Loader;\r
+  End   = (QEMU_LOADER_ENTRY *)(Loader + LoaderSize);\r
+  while (Entry < End) {\r
+    if (Entry->Type == QemuLoaderCmdAllocate) {\r
+      QEMU_LOADER_ALLOCATE *Allocate;\r
+\r
+      Allocate = &Entry->Command.Allocate;\r
+      if (Allocate->File[sizeof Allocate->File - 1] != '\0') {\r
+        Status = EFI_PROTOCOL_ERROR;\r
+        break;\r
+      }\r
+\r
+      Status = InstallQemuLinkedTables ((CHAR8 *)Allocate->File, AcpiProtocol,\r
+                 InstalledKey, &Installed);\r
+      if (EFI_ERROR (Status)) {\r
+        ASSERT (Status != EFI_INVALID_PARAMETER);\r
+        break;\r
+      }\r
+    }\r
+    ++Entry;\r
+  }\r
+\r
+  FreePool (Loader);\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    //\r
+    // Roll back partial installation.\r
+    //\r
+    while (Installed > 0) {\r
+      --Installed;\r
+      AcpiProtocol->UninstallAcpiTable (AcpiProtocol, InstalledKey[Installed]);\r
+    }\r
+  } else {\r
+    DEBUG ((EFI_D_INFO, "%a: installed %d tables\n", __FUNCTION__, Installed));\r
+  }\r
+\r
+FreeInstalledKey:\r
+  FreePool (InstalledKey);\r
+  return Status;\r
+}\r