public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Gerd Hoffmann" <kraxel@redhat.com>
To: Laszlo Ersek <lersek@redhat.com>
Cc: devel@edk2.groups.io, Pawel Polawski <ppolawsk@redhat.com>,
	Jiewen Yao <jiewen.yao@intel.com>,
	Oliver Steffen <osteffen@redhat.com>,
	Jordan Justen <jordan.l.justen@intel.com>,
	Ard Biesheuvel <ardb+tianocore@kernel.org>
Subject: Re: [PATCH v2 2/4] OvmfPkg/PlatformInitLib: Add PlatformGetLowMemoryCB
Date: Wed, 11 Jan 2023 08:29:25 +0100	[thread overview]
Message-ID: <20230111072925.l47t4ahgynqjyegq@sirius.home.kraxel.org> (raw)
In-Reply-To: <043b03d6-0a6b-c533-255b-24a7805d5cca@redhat.com>

> > +  Candidate = E820Entry->BaseAddr + E820Entry->Length;
> > +  if (Candidate >= BASE_4GB) {
> > +    return;
> > +  }

> (1) This looks like a faithful conversion / extraction, but I'd vaguely
> noticed something earlier, in the original code. Namely, when the
> exclusive end of the range is exactly 4GB, that should still qualify as
> low memory, shouldn't it?
> 
> Not that it matters in practice, because just below 4GB, we'll never
> ever have RAM on QEMU (pc or q35 -- I think even microvm, too).

Yes.

> But, for clarity, changing the limit condition as a separate patch
> (afterwards) might make sense.

Well, BASE_4GB-1 fits into LowMemory (which is UINT32) whereas BASE_4GB
does not ...

> > -    return (UINT32)GetHighestSystemMemoryAddressFromPvhMemmap (TRUE);
> > +    PlatformInfoHob->LowMemory = GetHighestSystemMemoryAddressFromPvhMemmap (TRUE);

> (2) Here you are converting a UINT64 from
> GetHighestSystemMemoryAddressFromPvhMemmap() to UINT32; I think that
> might trip up some MSVC compilers. I suggest preserving the cast.

OK.

>   PlatformInfoHob->LowMemory = 0;

> (I realize the platform info HOB could be zero-filled upon allocation --
> but then at least for consistency with the 4GB+ search initialization, a
> comment could be justified.)

It's explicitly cleared with ZeroMem (in BuildPlatformInfoHob), so there
is no zero-initialize LowMemory again.

> > diff --git a/OvmfPkg/PlatformPei/MemDetect.c b/OvmfPkg/PlatformPei/MemDetect.c
> > index 3d8375320dcb..41d186986ba8 100644
> > --- a/OvmfPkg/PlatformPei/MemDetect.c
> > +++ b/OvmfPkg/PlatformPei/MemDetect.c
> > @@ -271,7 +271,8 @@ PublishPeiMemory (
> >    UINT32                S3AcpiReservedMemoryBase;
> >    UINT32                S3AcpiReservedMemorySize;
> >
> > -  LowerMemorySize = PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);
> > +  PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);
> > +  LowerMemorySize = PlatformInfoHob->LowMemory;
> >    if (PlatformInfoHob->SmmSmramRequire) {
> >      //
> >      // TSEG is chipped from the end of low RAM
> 
> So I really like how small this hunk is, and I wondered why it differed
> so much from the rest, where you removed the local variables.
> 
> I understand now: because PublishPeiMemory() actually modifies
> "LowerMemorySize" in multiple steps. OK then, I see both points; here we
> need to keep "LowerMemorySize", because we can't modify the platform
> info HOB; but in the rest of the hunks, it's better if we just remove
> the useless locals. OK.

Yes, that is the pattern.  LowMemory holds the memory installed.
PublishPeiMemory calculates how edk2 uses that memory, which is
something different.

> code duplication is causing some churn for the present patch. I suggest
> reordering the branches as follows:
> 
> - microvm: do nothing, just return
> - cloudhv: constant assignments, then return
> - grab LowerMemorySize -- commonly needed for the rest!
> - handle q35
> - handle i440fx as default / fallback.

Makes sense indeed.

> (9) BTW, still regarding commit 2a0bd3bffc80 ("OvmfPkg/PlatformInitLib:
> q35 mtrr setup fix", 2022-09-28) -- does the following code comment
> still apply?
> 
>     //
>     // Set the memory range from the start of the 32-bit MMIO area (32-bit PCI
>     // MMIO aperture on i440fx, PCIEXBAR on q35) to 4GB as uncacheable.
>     //
> 
> Because, in case the new branch introduced by commit 2a0bd3bffc80 takes
> effect (namely, "Uc32Base = BASE_2GB"), I'm not sure where the PCIEXBAR
> starts, and then the above comment may no longer hold.

PCIEXBAR location doesn't change, it's still at 0xb0000000.

> >    ZeroMem (&PlatformInfoHob, sizeof (PlatformInfoHob));
> > +  PlatformGetSystemMemorySizeBelow4gb (&PlatformInfoHob);
> >    PlatformInfoHob.HostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);
> > -  LowMemorySize                   = PlatformGetSystemMemorySizeBelow4gb (&PlatformInfoHob);
> > +  LowMemorySize                   = PlatformInfoHob.LowMemory;
> >    ASSERT (LowMemorySize != 0);
> >    LowMemoryStart = FixedPcdGet32 (PcdOvmfDxeMemFvBase) + FixedPcdGet32 (PcdOvmfDxeMemFvSize);
> >    LowMemorySize -= LowMemoryStart;
> 
> (10) I think this PlatformGetSystemMemorySizeBelow4gb() call is not
> placed correctly.
> 
> PlatformGetSystemMemorySizeBelow4gb() depends on "HostBridgeDevId", but
> this hunk reorders the setting of "HostBridgeDevId" against the call to
> PlatformGetSystemMemorySizeBelow4gb().

Correct, nice catch.  Placed it there for optical reasons (keep the nice '='
alignment) but didn't realize that.

> > diff --git a/OvmfPkg/Library/PlatformInitLib/Platform.c b/OvmfPkg/Library/PlatformInitLib/Platform.c
> > index 3e13c5d4b34f..9ab0342fd8c0 100644
> > --- a/OvmfPkg/Library/PlatformInitLib/Platform.c
> > +++ b/OvmfPkg/Library/PlatformInitLib/Platform.c
> > @@ -128,7 +128,6 @@ PlatformMemMapInitialization (
> >  {
> >    UINT64  PciIoBase;
> >    UINT64  PciIoSize;
> > -  UINT32  TopOfLowRam;
> >    UINT64  PciExBarBase;
> >    UINT32  PciBase;
> >    UINT32  PciSize;
> > @@ -150,7 +149,7 @@ PlatformMemMapInitialization (
> >      return;
> >    }
> >
> > -  TopOfLowRam  = PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);
> > +  PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);
> >    PciExBarBase = 0;
> >    if (PlatformInfoHob->HostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {
> >      //
> > @@ -158,11 +157,11 @@ PlatformMemMapInitialization (
> >      // the base of the 32-bit PCI host aperture.
> >      //
> >      PciExBarBase = PcdGet64 (PcdPciExpressBaseAddress);
> > -    ASSERT (TopOfLowRam <= PciExBarBase);
> > +    ASSERT (PlatformInfoHob->LowMemory <= PciExBarBase);
> >      ASSERT (PciExBarBase <= MAX_UINT32 - SIZE_256MB);
> >      PciBase = (UINT32)(PciExBarBase + SIZE_256MB);
> 
> This change looks OK, but, similarly to my question (9):
> 
> (11) Is the following comment still up to date:
> 
>     //
>     // The MMCONFIG area is expected to fall between the top of low RAM and
>     // the base of the 32-bit PCI host aperture.
>     //
> 
> with regard to the new branch introduced by commit 2a0bd3bffc80
> ("OvmfPkg/PlatformInitLib: q35 mtrr setup fix", 2022-09-28)?

root@fedora ~# cat /proc/iomem 
[ ... ]
7ebfe000-7effffff : System RAM
7f000000-7fffffff : Reserved
80000000-afffffff : PCI Bus 0000:00
b0000000-bfffffff : PCI MMCONFIG 0000 [bus 00-ff]
  b0000000-bfffffff : Reserved
    b0000000-bfffffff : pnp 00:04
c0000000-febfffff : PCI Bus 0000:00
[ ... ]
root@fedora ~# cat /proc/mtrr 
reg00: base=0x080000000 ( 2048MB), size= 2048MB, count=1: uncachable
reg01: base=0x800000000 (32768MB), size=32768MB, count=1: uncachable

With gigabyte-alignment being the common case these days it might make
sense to place the MMCONFIG area at 0xe0000000 instead ...

take care,
  Gerd


  reply	other threads:[~2023-01-11  7:29 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-10  8:21 [PATCH v2 0/4] OvmfPkg: check 64bit mmio window for resource conflicts Gerd Hoffmann
2023-01-10  8:21 ` [PATCH v2 1/4] OvmfPkg/PlatformInitLib: Add PlatformScanE820 and GetFirstNonAddressCB Gerd Hoffmann
2023-01-10 15:41   ` Laszlo Ersek
2023-01-10  8:21 ` [PATCH v2 2/4] OvmfPkg/PlatformInitLib: Add PlatformGetLowMemoryCB Gerd Hoffmann
2023-01-10 16:53   ` Laszlo Ersek
2023-01-11  7:29     ` Gerd Hoffmann [this message]
2023-01-11 13:56       ` Laszlo Ersek
2023-01-11 15:23         ` Gerd Hoffmann
2023-01-12 11:09           ` Laszlo Ersek
2023-01-12 14:03             ` Gerd Hoffmann
2023-01-12 15:44               ` Laszlo Ersek
2023-01-10  8:21 ` [PATCH v2 3/4] OvmfPkg/PlatformInitLib: Add PlatformAddHobCB Gerd Hoffmann
2023-01-10 17:42   ` Laszlo Ersek
2023-01-11  8:06     ` Gerd Hoffmann
2023-01-11 14:08       ` Laszlo Ersek
2023-01-10  8:21 ` [PATCH v2 4/4] OvmfPkg/PlatformInitLib: Add PlatformReservationConflictCB Gerd Hoffmann
2023-01-10 17:55   ` Laszlo Ersek
2023-01-11  8:26     ` Gerd Hoffmann
2023-01-12 10:36       ` Laszlo Ersek

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=20230111072925.l47t4ahgynqjyegq@sirius.home.kraxel.org \
    --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