]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg: AcpiPlatformDxe: enable PCI IO and MMIO while fetching QEMU tables
authorLaszlo Ersek <lersek@redhat.com>
Sun, 13 Mar 2016 14:34:07 +0000 (15:34 +0100)
committerLaszlo Ersek <lersek@redhat.com>
Wed, 23 Mar 2016 16:39:35 +0000 (17:39 +0100)
Now that the previous patches ensure that we can access all PCI devices in
AcpiPlatformDxe, we can enable IO and MMIO decoding for all of them while
we contact QEMU for the ACPI tables. See more details in the patch titled:

  OvmfPkg: introduce gRootBridgesConnectedEventGroupGuid

In particular, this patch will prevent the bug when the 64-bit MMIO
aperture is completely missing from QEMU's _CRS, and consequently Linux
rejects 64-bit BARs with the error message

  pci 0000:00:03.0: can't claim BAR 4 [mem 0x800000000-0x8007fffff 64bit
                    pref]: no compatible bridge window

Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
OvmfPkg/AcpiPlatformDxe/AcpiPlatform.h
OvmfPkg/AcpiPlatformDxe/AcpiPlatformDxe.inf
OvmfPkg/AcpiPlatformDxe/PciDecoding.c [new file with mode: 0644]
OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpi.c
OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpiPlatformDxe.inf

index 55b380b285057be0b6d7ab683980cf1f3f28eb12..08dd7f8f7dd7a5fe36f39bcb566c8578224aa5f8 100644 (file)
@@ -19,6 +19,7 @@
 \r
 #include <Protocol/AcpiTable.h>\r
 #include <Protocol/FirmwareVolume2.h>\r
+#include <Protocol/PciIo.h>\r
 \r
 #include <Library/BaseLib.h>\r
 #include <Library/UefiBootServicesTableLib.h>\r
 \r
 #include <IndustryStandard/Acpi.h>\r
 \r
+typedef struct {\r
+  EFI_PCI_IO_PROTOCOL *PciIo;\r
+  UINT64              PciAttributes;\r
+} ORIGINAL_ATTRIBUTES;\r
+\r
 EFI_STATUS\r
 EFIAPI\r
 InstallAcpiTable (\r
@@ -73,5 +79,17 @@ InstallAcpiTables (
   IN   EFI_ACPI_TABLE_PROTOCOL       *AcpiTable\r
   );\r
 \r
+VOID\r
+EnablePciDecoding (\r
+  OUT ORIGINAL_ATTRIBUTES **OriginalAttributes,\r
+  OUT UINTN               *Count\r
+  );\r
+\r
+VOID\r
+RestorePciDecoding (\r
+  IN ORIGINAL_ATTRIBUTES *OriginalAttributes,\r
+  IN UINTN               Count\r
+  );\r
+\r
 #endif\r
 \r
index 8e98053994ebec6893c0e6ea13207c5b4c6f78c5..654d3a03905d5fcc36eb3228f343d1464bc5d853 100644 (file)
@@ -32,6 +32,7 @@
   QemuFwCfgAcpi.c\r
   Xen.c\r
   EntryPoint.c\r
+  PciDecoding.c\r
 \r
 [Packages]\r
   MdePkg/MdePkg.dec\r
@@ -57,6 +58,7 @@
 \r
 [Protocols]\r
   gEfiAcpiTableProtocolGuid                     # PROTOCOL ALWAYS_CONSUMED\r
+  gEfiPciIoProtocolGuid                         # PROTOCOL SOMETIMES_CONSUMED\r
 \r
 [Guids]\r
   gEfiXenInfoGuid\r
diff --git a/OvmfPkg/AcpiPlatformDxe/PciDecoding.c b/OvmfPkg/AcpiPlatformDxe/PciDecoding.c
new file mode 100644 (file)
index 0000000..3b9b12c
--- /dev/null
@@ -0,0 +1,186 @@
+/** @file\r
+  Temporarily enable IO and MMIO decoding for all PCI devices while QEMU\r
+  regenerates the ACPI tables.\r
+\r
+  Copyright (C) 2016, Red Hat, Inc.\r
+\r
+  This program and the accompanying materials are licensed and made available\r
+  under the terms and conditions of the BSD License which accompanies this\r
+  distribution.  The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.php\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
+  WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+**/\r
+\r
+#include <Library/MemoryAllocationLib.h>\r
+\r
+#include "AcpiPlatform.h"\r
+\r
+\r
+/**\r
+  Collect all PciIo protocol instances in the system. Save their original\r
+  attributes, and enable IO and MMIO decoding for each.\r
+\r
+  This is a best effort function; it doesn't return status codes. Its\r
+  caller is supposed to proceed even if this function fails.\r
+\r
+  @param[out] OriginalAttributes  On output, a dynamically allocated array of\r
+                                  ORIGINAL_ATTRIBUTES elements. The array lists\r
+                                  the PciIo protocol instances found in the\r
+                                  system at the time of the call, plus the\r
+                                  original PCI attributes for each.\r
+\r
+                                  Before returning, the function enables IO and\r
+                                  MMIO decoding for each PciIo instance it\r
+                                  finds.\r
+\r
+                                  On error, or when no such instances are\r
+                                  found, OriginalAttributes is set to NULL.\r
+\r
+  @param[out] Count               On output, the number of elements in\r
+                                  OriginalAttributes. On error it is set to\r
+                                  zero.\r
+**/\r
+VOID\r
+EnablePciDecoding (\r
+  OUT ORIGINAL_ATTRIBUTES **OriginalAttributes,\r
+  OUT UINTN               *Count\r
+  )\r
+{\r
+  EFI_STATUS          Status;\r
+  UINTN               NoHandles;\r
+  EFI_HANDLE          *Handles;\r
+  ORIGINAL_ATTRIBUTES *OrigAttrs;\r
+  UINTN               Idx;\r
+\r
+  *OriginalAttributes = NULL;\r
+  *Count              = 0;\r
+\r
+  if (PcdGetBool (PcdPciDisableBusEnumeration)) {\r
+    //\r
+    // The platform downloads ACPI tables from QEMU in general, but there are\r
+    // no root bridges in this execution. We're done.\r
+    //\r
+    return;\r
+  }\r
+\r
+  Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiPciIoProtocolGuid,\r
+                  NULL /* SearchKey */, &NoHandles, &Handles);\r
+  if (Status == EFI_NOT_FOUND) {\r
+    //\r
+    // No PCI devices were found on either of the root bridges. We're done.\r
+    //\r
+    return;\r
+  }\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_WARN, "%a: LocateHandleBuffer(): %r\n", __FUNCTION__,\r
+      Status));\r
+    return;\r
+  }\r
+\r
+  OrigAttrs = AllocatePool (NoHandles * sizeof *OrigAttrs);\r
+  if (OrigAttrs == NULL) {\r
+    DEBUG ((EFI_D_WARN, "%a: AllocatePool(): out of resources\n",\r
+      __FUNCTION__));\r
+    goto FreeHandles;\r
+  }\r
+\r
+  for (Idx = 0; Idx < NoHandles; ++Idx) {\r
+    EFI_PCI_IO_PROTOCOL *PciIo;\r
+\r
+    //\r
+    // Look up PciIo on the handle and stash it\r
+    //\r
+    Status = gBS->HandleProtocol (Handles[Idx], &gEfiPciIoProtocolGuid,\r
+                    (VOID**)&PciIo);\r
+    ASSERT_EFI_ERROR (Status);\r
+    OrigAttrs[Idx].PciIo = PciIo;\r
+\r
+    //\r
+    // Stash the current attributes\r
+    //\r
+    Status = PciIo->Attributes (PciIo, EfiPciIoAttributeOperationGet, 0,\r
+                      &OrigAttrs[Idx].PciAttributes);\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_WARN, "%a: EfiPciIoAttributeOperationGet: %r\n",\r
+        __FUNCTION__, Status));\r
+      goto RestoreAttributes;\r
+    }\r
+\r
+    //\r
+    // Enable IO and MMIO decoding\r
+    //\r
+    Status = PciIo->Attributes (PciIo, EfiPciIoAttributeOperationEnable,\r
+                      EFI_PCI_IO_ATTRIBUTE_IO | EFI_PCI_IO_ATTRIBUTE_MEMORY,\r
+                      NULL);\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_WARN, "%a: EfiPciIoAttributeOperationEnable: %r\n",\r
+        __FUNCTION__, Status));\r
+      goto RestoreAttributes;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Success\r
+  //\r
+  FreePool (Handles);\r
+  *OriginalAttributes = OrigAttrs;\r
+  *Count              = NoHandles;\r
+  return;\r
+\r
+RestoreAttributes:\r
+  while (Idx > 0) {\r
+    --Idx;\r
+    OrigAttrs[Idx].PciIo->Attributes (OrigAttrs[Idx].PciIo,\r
+                            EfiPciIoAttributeOperationSet,\r
+                            OrigAttrs[Idx].PciAttributes,\r
+                            NULL\r
+                            );\r
+  }\r
+  FreePool (OrigAttrs);\r
+\r
+FreeHandles:\r
+  FreePool (Handles);\r
+}\r
+\r
+\r
+/**\r
+  Restore the original PCI attributes saved with EnablePciDecoding().\r
+\r
+  @param[in] OriginalAttributes  The array allocated and populated by\r
+                                 EnablePciDecoding(). This parameter may be\r
+                                 NULL. If OriginalAttributes is NULL, then the\r
+                                 function is a no-op; otherwise the PciIo\r
+                                 attributes will be restored, and the\r
+                                 OriginalAttributes array will be freed.\r
+\r
+  @param[in] Count               The Count value stored by EnablePciDecoding(),\r
+                                 the number of elements in OriginalAttributes.\r
+                                 Count may be zero if and only if\r
+                                 OriginalAttributes is NULL.\r
+**/\r
+VOID\r
+RestorePciDecoding (\r
+  IN ORIGINAL_ATTRIBUTES *OriginalAttributes,\r
+  IN UINTN               Count\r
+  )\r
+{\r
+  UINTN Idx;\r
+\r
+  ASSERT ((OriginalAttributes == NULL) == (Count == 0));\r
+  if (OriginalAttributes == NULL) {\r
+    return;\r
+  }\r
+\r
+  for (Idx = 0; Idx < Count; ++Idx) {\r
+    OriginalAttributes[Idx].PciIo->Attributes (\r
+                                     OriginalAttributes[Idx].PciIo,\r
+                                     EfiPciIoAttributeOperationSet,\r
+                                     OriginalAttributes[Idx].PciAttributes,\r
+                                     NULL\r
+                                     );\r
+  }\r
+  FreePool (OriginalAttributes);\r
+}\r
index 81620448a046364149e9dbaf5018c2327438cfa3..faaff3757cdb69bf76fb35b7c301acf4f5a8acd2 100644 (file)
@@ -561,6 +561,8 @@ InstallQemuFwCfgTables (
   UINTN                    FwCfgSize;\r
   QEMU_LOADER_ENTRY        *LoaderStart;\r
   CONST QEMU_LOADER_ENTRY  *LoaderEntry, *LoaderEnd;\r
+  ORIGINAL_ATTRIBUTES      *OriginalPciAttributes;\r
+  UINTN                    OriginalPciAttributesCount;\r
   ORDERED_COLLECTION       *Tracker;\r
   UINTN                    *InstalledKey;\r
   INT32                    Installed;\r
@@ -580,8 +582,10 @@ InstallQemuFwCfgTables (
   if (LoaderStart == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
+  EnablePciDecoding (&OriginalPciAttributes, &OriginalPciAttributesCount);\r
   QemuFwCfgSelectItem (FwCfgItem);\r
   QemuFwCfgReadBytes (FwCfgSize, LoaderStart);\r
+  RestorePciDecoding (OriginalPciAttributes, OriginalPciAttributesCount);\r
   LoaderEnd = LoaderStart + FwCfgSize / sizeof *LoaderEntry;\r
 \r
   Tracker = OrderedCollectionInit (BlobCompare, BlobKeyCompare);\r
index c073b2a47ec6e4c94f2fb3ed4cec49441a8f83bd..d99f2d5a95c7d3b9382c9ee247743550a490fc65 100644 (file)
@@ -30,6 +30,7 @@
   QemuFwCfgAcpiPlatform.c\r
   QemuFwCfgAcpi.c\r
   EntryPoint.c\r
+  PciDecoding.c\r
 \r
 [Packages]\r
   MdePkg/MdePkg.dec\r
@@ -47,6 +48,7 @@
 \r
 [Protocols]\r
   gEfiAcpiTableProtocolGuid                     # PROTOCOL ALWAYS_CONSUMED\r
+  gEfiPciIoProtocolGuid                         # PROTOCOL SOMETIMES_CONSUMED\r
 \r
 [Guids]\r
   gRootBridgesConnectedEventGroupGuid\r