public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Gerd Hoffmann" <kraxel@redhat.com>
To: devel@edk2.groups.io
Cc: "Ard Biesheuvel" <ardb+tianocore@kernel.org>,
	"László Érsek" <lersek@redhat.com>,
	"Jordan Justen" <jordan.l.justen@intel.com>,
	"Pawel Polawski" <ppolawsk@redhat.com>,
	"Oliver Steffen" <osteffen@redhat.com>,
	"Jiewen Yao" <jiewen.yao@intel.com>,
	"Gerd Hoffmann" <kraxel@redhat.com>
Subject: [PATCH 2/2] OvmfPkg/PlatformInitLib: check 64bit mmio window for resource conflicts
Date: Fri,  6 Jan 2023 15:04:03 +0100	[thread overview]
Message-ID: <20230106140403.2889131-3-kraxel@redhat.com> (raw)
In-Reply-To: <20230106140403.2889131-1-kraxel@redhat.com>

Add new operation mode to PlatformScanOrAdd64BitE820Ram() which checks
whenever any reservations from the qemu e820 table overlap with the mmio
window.  Should that be the case move it to avoid the conflict.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 OvmfPkg/Library/PlatformInitLib/MemDetect.c | 47 ++++++++++++++++++---
 1 file changed, 41 insertions(+), 6 deletions(-)

diff --git a/OvmfPkg/Library/PlatformInitLib/MemDetect.c b/OvmfPkg/Library/PlatformInitLib/MemDetect.c
index 1255d6300fdd..4f5a04832a4e 100644
--- a/OvmfPkg/Library/PlatformInitLib/MemDetect.c
+++ b/OvmfPkg/Library/PlatformInitLib/MemDetect.c
@@ -126,6 +126,12 @@ PlatformQemuUc32BaseInitialization (
                           entries that start at or above 4GB.
                           It also produces HOBs for reserved entries.
 
+  @param[in out] PlatformInfoHob  If Lowmemory PlatformInfoHob is not NULL,
+                                  then PlatformScanOrAdd64BitE820Ram() checks
+                                  the 64bit PCI MMIO window against conflicts
+                                  with e820 reservations from qemu.  If needed
+                                  the MMIO window will be moved down.
+
   @param[out] LowMemory  If Lowmemory is not NULL, then Lowmemory MaxAddress
                          holds the amout of emory below 4G on output.
 
@@ -147,9 +153,10 @@ PlatformQemuUc32BaseInitialization (
 STATIC
 EFI_STATUS
 PlatformScanOrAdd64BitE820Ram (
-  IN BOOLEAN  AddHighHob,
-  OUT UINT64  *LowMemory OPTIONAL,
-  OUT UINT64  *MaxAddress OPTIONAL
+  IN BOOLEAN                    AddHighHob,
+  IN OUT EFI_HOB_PLATFORM_INFO  *PlatformInfoHob OPTIONAL,
+  OUT UINT64                    *LowMemory OPTIONAL,
+  OUT UINT64                    *MaxAddress OPTIONAL
   )
 {
   EFI_STATUS            Status;
@@ -249,6 +256,33 @@ PlatformScanOrAdd64BitE820Ram (
           E820Entry.Length
           );
       }
+
+      if (PlatformInfoHob) {
+        UINT64  IntersectionBase = MAX (
+                                     E820Entry.BaseAddr,
+                                     PlatformInfoHob->PcdPciMmio64Base
+                                     );
+        UINT64  IntersectionEnd = MIN (
+                                    E820Entry.BaseAddr + E820Entry.Length,
+                                    PlatformInfoHob->PcdPciMmio64Base +
+                                    PlatformInfoHob->PcdPciMmio64Size
+                                    );
+
+        if (IntersectionBase < IntersectionEnd) {
+          // have overlap, so move mmio window down from end of address space
+          UINT64  NewBase = (PlatformInfoHob->PcdPciMmio64Base -
+                             PlatformInfoHob->PcdPciMmio64Size);
+
+          DEBUG ((
+            DEBUG_INFO,
+            "%a: move mmio: 0x%Lx => %Lx\n",
+            __FUNCTION__,
+            PlatformInfoHob->PcdPciMmio64Base,
+            NewBase
+            ));
+          PlatformInfoHob->PcdPciMmio64Base = NewBase;
+        }
+      }
     }
   }
 
@@ -340,7 +374,7 @@ PlatformGetSystemMemorySizeBelow4gb (
     return (UINT32)GetHighestSystemMemoryAddressFromPvhMemmap (TRUE);
   }
 
-  Status = PlatformScanOrAdd64BitE820Ram (FALSE, &LowerMemorySize, NULL);
+  Status = PlatformScanOrAdd64BitE820Ram (FALSE, NULL, &LowerMemorySize, NULL);
   if ((Status == EFI_SUCCESS) && (LowerMemorySize > 0)) {
     return (UINT32)LowerMemorySize;
   }
@@ -412,7 +446,7 @@ PlatformGetFirstNonAddress (
   // Otherwise, get the flat size of the memory above 4GB from the CMOS (which
   // can only express a size smaller than 1TB), and add it to 4GB.
   //
-  Status = PlatformScanOrAdd64BitE820Ram (FALSE, NULL, &FirstNonAddress);
+  Status = PlatformScanOrAdd64BitE820Ram (FALSE, NULL, NULL, &FirstNonAddress);
   if (EFI_ERROR (Status)) {
     FirstNonAddress = BASE_4GB + PlatformGetSystemMemorySizeAbove4gb ();
   }
@@ -648,6 +682,7 @@ PlatformDynamicMmioWindow (
     DEBUG ((DEBUG_INFO, "%a:   MMIO Space 0x%Lx (%Ld GB)\n", __func__, MmioSpace, RShiftU64 (MmioSpace, 30)));
     PlatformInfoHob->PcdPciMmio64Size = MmioSpace;
     PlatformInfoHob->PcdPciMmio64Base = AddrSpace - MmioSpace;
+    PlatformScanOrAdd64BitE820Ram (FALSE, PlatformInfoHob, NULL, NULL);
   } else {
     DEBUG ((DEBUG_INFO, "%a: using classic mmio window\n", __func__));
   }
@@ -960,7 +995,7 @@ PlatformQemuInitializeRam (
     // entries. Otherwise, create a single memory HOB with the flat >=4GB
     // memory size read from the CMOS.
     //
-    Status = PlatformScanOrAdd64BitE820Ram (TRUE, NULL, NULL);
+    Status = PlatformScanOrAdd64BitE820Ram (TRUE, NULL, NULL, NULL);
     if (EFI_ERROR (Status)) {
       UpperMemorySize = PlatformGetSystemMemorySizeAbove4gb ();
       if (UpperMemorySize != 0) {
-- 
2.39.0


  parent reply	other threads:[~2023-01-06 14:04 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-06 14:04 [PATCH 0/2] OvmfPkg: check 64bit mmio window for resource conflicts Gerd Hoffmann
2023-01-06 14:04 ` [PATCH 1/2] OvmfPkg/PlatformInitLib: update PlatformScanOrAdd64BitE820Ram documentation Gerd Hoffmann
2023-01-09  8:07   ` [edk2-devel] " Laszlo Ersek
2023-01-09 10:15     ` Gerd Hoffmann
2023-01-09 11:30       ` Laszlo Ersek
2023-01-06 14:04 ` Gerd Hoffmann [this message]
2023-01-06 20:39   ` [edk2-devel] [PATCH 2/2] OvmfPkg/PlatformInitLib: check 64bit mmio window for resource conflicts Pedro Falcato
2023-01-09  7:02     ` Gerd Hoffmann
2023-01-06 14:14 ` [PATCH 0/2] OvmfPkg: " Ard Biesheuvel
2023-01-06 15:32   ` Gerd Hoffmann
2023-01-06 15:39     ` Yao, Jiewen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230106140403.2889131-3-kraxel@redhat.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox