public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH v3 0/3] OvmfPkg/PlatformPei: prefer etc/e820 for memory detection
@ 2021-09-16 15:03 Gerd Hoffmann
  2021-09-16 15:03 ` [PATCH v3 1/3] OvmfPkg/PlatformPei: ScanOrAdd64BitE820Ram improvements Gerd Hoffmann
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Gerd Hoffmann @ 2021-09-16 15:03 UTC (permalink / raw)
  To: devel
  Cc: Philippe Mathieu-Daude, Jiewen Yao, Jordan Justen, Ard Biesheuvel,
	Gerd Hoffmann

Don't use cmos for memory detection if possible.
qemu provides the etc/e820 firmware config file
as alternative since 2013.

v2:
 - fix lowmem detection.
 - pick up review tags.
 - add rfc patch to completely drop cmos support.

v3:
 - fix CI failure.

Gerd Hoffmann (3):
  OvmfPkg/PlatformPei: ScanOrAdd64BitE820Ram improvements
  OvmfPkg/PlatformPei: prefer etc/e820 for memory detection
  RFC: OvmfPkg/PlatformPei: stop using cmos for memory detection

 OvmfPkg/PlatformPei/MemDetect.c | 84 +++++++++++----------------------
 1 file changed, 28 insertions(+), 56 deletions(-)

-- 
2.31.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v3 1/3] OvmfPkg/PlatformPei: ScanOrAdd64BitE820Ram improvements
  2021-09-16 15:03 [PATCH v3 0/3] OvmfPkg/PlatformPei: prefer etc/e820 for memory detection Gerd Hoffmann
@ 2021-09-16 15:03 ` Gerd Hoffmann
  2021-09-16 15:03 ` [PATCH v3 2/3] OvmfPkg/PlatformPei: prefer etc/e820 for memory detection Gerd Hoffmann
  2021-09-16 15:03 ` [PATCH v3 3/3] RFC: OvmfPkg/PlatformPei: stop using cmos " Gerd Hoffmann
  2 siblings, 0 replies; 4+ messages in thread
From: Gerd Hoffmann @ 2021-09-16 15:03 UTC (permalink / raw)
  To: devel
  Cc: Philippe Mathieu-Daude, Jiewen Yao, Jordan Justen, Ard Biesheuvel,
	Gerd Hoffmann

Add a bool parameter to ScanOrAdd64BitE820Ram to explicitly specify
whenever ScanOrAdd64BitE820Ram should add HOBs for high memory (above
4G) or scan only.

Also add a lowmem parameter so ScanOrAdd64BitE820Ram
can report the memory size below 4G.

This allows a more flexible usage of ScanOrAdd64BitE820Ram,
a followup patch will use it for all memory detection.

No functional change.

Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3593
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Reviewed-by: Philippe Mathieu-Daude <philmd@redhat.com>
---
 OvmfPkg/PlatformPei/MemDetect.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/OvmfPkg/PlatformPei/MemDetect.c b/OvmfPkg/PlatformPei/MemDetect.c
index 2c2c4641ec8a..500c1d4d5231 100644
--- a/OvmfPkg/PlatformPei/MemDetect.c
+++ b/OvmfPkg/PlatformPei/MemDetect.c
@@ -200,6 +200,8 @@ QemuUc32BaseInitialization (
 STATIC
 EFI_STATUS
 ScanOrAdd64BitE820Ram (
+  IN BOOLEAN AddHighHob,
+  OUT UINT64 *LowMemory OPTIONAL,
   OUT UINT64 *MaxAddress OPTIONAL
   )
 {
@@ -217,6 +219,9 @@ ScanOrAdd64BitE820Ram (
     return EFI_PROTOCOL_ERROR;
   }
 
+  if (LowMemory != NULL) {
+    *LowMemory = 0;
+  }
   if (MaxAddress != NULL) {
     *MaxAddress = BASE_4GB;
   }
@@ -232,9 +237,8 @@ ScanOrAdd64BitE820Ram (
       E820Entry.Length,
       E820Entry.Type
       ));
-    if (E820Entry.Type == EfiAcpiAddressRangeMemory &&
-        E820Entry.BaseAddr >= BASE_4GB) {
-      if (MaxAddress == NULL) {
+    if (E820Entry.Type == EfiAcpiAddressRangeMemory) {
+      if (AddHighHob && E820Entry.BaseAddr >= BASE_4GB) {
         UINT64 Base;
         UINT64 End;
 
@@ -254,11 +258,12 @@ ScanOrAdd64BitE820Ram (
             End
             ));
         }
-      } else {
+      }
+      if (MaxAddress || LowMemory) {
         UINT64 Candidate;
 
         Candidate = E820Entry.BaseAddr + E820Entry.Length;
-        if (Candidate > *MaxAddress) {
+        if (MaxAddress && Candidate > *MaxAddress) {
           *MaxAddress = Candidate;
           DEBUG ((
             DEBUG_VERBOSE,
@@ -267,6 +272,15 @@ ScanOrAdd64BitE820Ram (
             *MaxAddress
             ));
         }
+        if (LowMemory && Candidate > *LowMemory && Candidate < BASE_4GB) {
+          *LowMemory = Candidate;
+          DEBUG ((
+            DEBUG_VERBOSE,
+            "%a: LowMemory=0x%Lx\n",
+            __FUNCTION__,
+            *LowMemory
+            ));
+        }
       }
     }
   }
@@ -353,7 +367,7 @@ GetFirstNonAddress (
   // 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 = ScanOrAdd64BitE820Ram (&FirstNonAddress);
+  Status = ScanOrAdd64BitE820Ram (FALSE, NULL, &FirstNonAddress);
   if (EFI_ERROR (Status)) {
     FirstNonAddress = BASE_4GB + GetSystemMemorySizeAbove4gb ();
   }
@@ -754,7 +768,7 @@ QemuInitializeRam (
     // entries. Otherwise, create a single memory HOB with the flat >=4GB
     // memory size read from the CMOS.
     //
-    Status = ScanOrAdd64BitE820Ram (NULL);
+    Status = ScanOrAdd64BitE820Ram (TRUE, NULL, NULL);
     if (EFI_ERROR (Status) && UpperMemorySize != 0) {
       AddMemoryBaseSizeHob (BASE_4GB, UpperMemorySize);
     }
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v3 2/3] OvmfPkg/PlatformPei: prefer etc/e820 for memory detection
  2021-09-16 15:03 [PATCH v3 0/3] OvmfPkg/PlatformPei: prefer etc/e820 for memory detection Gerd Hoffmann
  2021-09-16 15:03 ` [PATCH v3 1/3] OvmfPkg/PlatformPei: ScanOrAdd64BitE820Ram improvements Gerd Hoffmann
@ 2021-09-16 15:03 ` Gerd Hoffmann
  2021-09-16 15:03 ` [PATCH v3 3/3] RFC: OvmfPkg/PlatformPei: stop using cmos " Gerd Hoffmann
  2 siblings, 0 replies; 4+ messages in thread
From: Gerd Hoffmann @ 2021-09-16 15:03 UTC (permalink / raw)
  To: devel
  Cc: Philippe Mathieu-Daude, Jiewen Yao, Jordan Justen, Ard Biesheuvel,
	Gerd Hoffmann

Prefer the e820 map provided via qemu firmware config interface
for memory detection.  Use rtc cmos only as fallback, which should
be rarely needed these days as qemu supports etc/e820 since 2013.

Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3593
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Reviewed-by: Philippe Mathieu-Daude <philmd@redhat.com>
---
 OvmfPkg/PlatformPei/MemDetect.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/OvmfPkg/PlatformPei/MemDetect.c b/OvmfPkg/PlatformPei/MemDetect.c
index 500c1d4d5231..1d942b12d519 100644
--- a/OvmfPkg/PlatformPei/MemDetect.c
+++ b/OvmfPkg/PlatformPei/MemDetect.c
@@ -293,9 +293,16 @@ GetSystemMemorySizeBelow4gb (
   VOID
   )
 {
+  EFI_STATUS Status;
+  UINT64 LowerMemorySize = 0;
   UINT8 Cmos0x34;
   UINT8 Cmos0x35;
 
+  Status = ScanOrAdd64BitE820Ram (FALSE, &LowerMemorySize, NULL);
+  if (Status == EFI_SUCCESS && LowerMemorySize > 0) {
+    return (UINT32)LowerMemorySize;
+  }
+
   //
   // CMOS 0x34/0x35 specifies the system memory above 16 MB.
   // * CMOS(0x35) is the high byte
@@ -722,7 +729,6 @@ QemuInitializeRam (
   // Determine total memory size available
   //
   LowerMemorySize = GetSystemMemorySizeBelow4gb ();
-  UpperMemorySize = GetSystemMemorySizeAbove4gb ();
 
   if (mBootMode == BOOT_ON_S3_RESUME) {
     //
@@ -769,8 +775,11 @@ QemuInitializeRam (
     // memory size read from the CMOS.
     //
     Status = ScanOrAdd64BitE820Ram (TRUE, NULL, NULL);
-    if (EFI_ERROR (Status) && UpperMemorySize != 0) {
-      AddMemoryBaseSizeHob (BASE_4GB, UpperMemorySize);
+    if (EFI_ERROR (Status)) {
+      UpperMemorySize = GetSystemMemorySizeAbove4gb ();
+      if (UpperMemorySize != 0) {
+        AddMemoryBaseSizeHob (BASE_4GB, UpperMemorySize);
+      }
     }
   }
 
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v3 3/3] RFC: OvmfPkg/PlatformPei: stop using cmos for memory detection
  2021-09-16 15:03 [PATCH v3 0/3] OvmfPkg/PlatformPei: prefer etc/e820 for memory detection Gerd Hoffmann
  2021-09-16 15:03 ` [PATCH v3 1/3] OvmfPkg/PlatformPei: ScanOrAdd64BitE820Ram improvements Gerd Hoffmann
  2021-09-16 15:03 ` [PATCH v3 2/3] OvmfPkg/PlatformPei: prefer etc/e820 for memory detection Gerd Hoffmann
@ 2021-09-16 15:03 ` Gerd Hoffmann
  2 siblings, 0 replies; 4+ messages in thread
From: Gerd Hoffmann @ 2021-09-16 15:03 UTC (permalink / raw)
  To: devel
  Cc: Philippe Mathieu-Daude, Jiewen Yao, Jordan Justen, Ard Biesheuvel,
	Gerd Hoffmann

Not needed for qemu 1.7 (released in 2013) and newer.

Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3593
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 OvmfPkg/PlatformPei/MemDetect.c | 59 +++------------------------------
 1 file changed, 4 insertions(+), 55 deletions(-)

diff --git a/OvmfPkg/PlatformPei/MemDetect.c b/OvmfPkg/PlatformPei/MemDetect.c
index 1d942b12d519..778d150e956b 100644
--- a/OvmfPkg/PlatformPei/MemDetect.c
+++ b/OvmfPkg/PlatformPei/MemDetect.c
@@ -37,7 +37,6 @@ Module Name:
 #include <Library/QemuFwCfgSimpleParserLib.h>
 
 #include "Platform.h"
-#include "Cmos.h"
 
 UINT8 mPhysMemAddressWidth;
 
@@ -295,52 +294,12 @@ GetSystemMemorySizeBelow4gb (
 {
   EFI_STATUS Status;
   UINT64 LowerMemorySize = 0;
-  UINT8 Cmos0x34;
-  UINT8 Cmos0x35;
 
   Status = ScanOrAdd64BitE820Ram (FALSE, &LowerMemorySize, NULL);
-  if (Status == EFI_SUCCESS && LowerMemorySize > 0) {
-    return (UINT32)LowerMemorySize;
-  }
+  ASSERT_EFI_ERROR (Status);
+  ASSERT (LowerMemorySize > 0);
+  return (UINT32)LowerMemorySize;
 
-  //
-  // CMOS 0x34/0x35 specifies the system memory above 16 MB.
-  // * CMOS(0x35) is the high byte
-  // * CMOS(0x34) is the low byte
-  // * The size is specified in 64kb chunks
-  // * Since this is memory above 16MB, the 16MB must be added
-  //   into the calculation to get the total memory size.
-  //
-
-  Cmos0x34 = (UINT8) CmosRead8 (0x34);
-  Cmos0x35 = (UINT8) CmosRead8 (0x35);
-
-  return (UINT32) (((UINTN)((Cmos0x35 << 8) + Cmos0x34) << 16) + SIZE_16MB);
-}
-
-
-STATIC
-UINT64
-GetSystemMemorySizeAbove4gb (
-  )
-{
-  UINT32 Size;
-  UINTN  CmosIndex;
-
-  //
-  // CMOS 0x5b-0x5d specifies the system memory above 4GB MB.
-  // * CMOS(0x5d) is the most significant size byte
-  // * CMOS(0x5c) is the middle size byte
-  // * CMOS(0x5b) is the least significant size byte
-  // * The size is specified in 64kb chunks
-  //
-
-  Size = 0;
-  for (CmosIndex = 0x5d; CmosIndex >= 0x5b; CmosIndex--) {
-    Size = (UINT32) (Size << 8) + (UINT32) CmosRead8 (CmosIndex);
-  }
-
-  return LShiftU64 (Size, 16);
 }
 
 
@@ -371,12 +330,9 @@ GetFirstNonAddress (
   // If QEMU presents an E820 map, then get the highest exclusive >=4GB RAM
   // address from it. This can express an address >= 4GB+1TB.
   //
-  // 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 = ScanOrAdd64BitE820Ram (FALSE, NULL, &FirstNonAddress);
   if (EFI_ERROR (Status)) {
-    FirstNonAddress = BASE_4GB + GetSystemMemorySizeAbove4gb ();
+    FirstNonAddress = BASE_4GB;
   }
 
   //
@@ -719,7 +675,6 @@ QemuInitializeRam (
   )
 {
   UINT64                      LowerMemorySize;
-  UINT64                      UpperMemorySize;
   MTRR_SETTINGS               MtrrSettings;
   EFI_STATUS                  Status;
 
@@ -775,12 +730,6 @@ QemuInitializeRam (
     // memory size read from the CMOS.
     //
     Status = ScanOrAdd64BitE820Ram (TRUE, NULL, NULL);
-    if (EFI_ERROR (Status)) {
-      UpperMemorySize = GetSystemMemorySizeAbove4gb ();
-      if (UpperMemorySize != 0) {
-        AddMemoryBaseSizeHob (BASE_4GB, UpperMemorySize);
-      }
-    }
   }
 
   //
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-09-16 15:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-09-16 15:03 [PATCH v3 0/3] OvmfPkg/PlatformPei: prefer etc/e820 for memory detection Gerd Hoffmann
2021-09-16 15:03 ` [PATCH v3 1/3] OvmfPkg/PlatformPei: ScanOrAdd64BitE820Ram improvements Gerd Hoffmann
2021-09-16 15:03 ` [PATCH v3 2/3] OvmfPkg/PlatformPei: prefer etc/e820 for memory detection Gerd Hoffmann
2021-09-16 15:03 ` [PATCH v3 3/3] RFC: OvmfPkg/PlatformPei: stop using cmos " Gerd Hoffmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox