public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Gerd Hoffmann" <kraxel@redhat.com>
To: devel@edk2.groups.io
Cc: Bob Feng <bob.c.feng@intel.com>,
	Yuwei Chen <yuwei.chen@intel.com>,
	Erdem Aktas <erdemaktas@google.com>,
	Jordan Justen <jordan.l.justen@intel.com>,
	Peter Grehan <grehan@freebsd.org>,
	Rebecca Cran <rebecca@bsdio.com>,
	Liming Gao <gaoliming@byosoft.com.cn>,
	Ard Biesheuvel <ardb+tianocore@kernel.org>,
	Gerd Hoffmann <kraxel@redhat.com>,
	Jiewen Yao <jiewen.yao@intel.com>, Min Xu <min.m.xu@intel.com>,
	Brijesh Singh <brijesh.singh@amd.com>,
	Oliver Steffen <osteffen@redhat.com>,
	Pawel Polawski <ppolawsk@redhat.com>,
	Tom Lendacky <thomas.lendacky@amd.com>,
	James Bottomley <jejb@linux.ibm.com>
Subject: [PATCH 13/14] OvmfPkg/QemuFwCfgLib: rewrite fw_cfg probe
Date: Fri,  2 Dec 2022 14:10:08 +0100	[thread overview]
Message-ID: <20221202131009.3190040-14-kraxel@redhat.com> (raw)
In-Reply-To: <20221202131009.3190040-1-kraxel@redhat.com>

Move the code to a new QemuFwCfgProbe() function.  Use direct Io*() calls
instead of indirect QemuFwCfg*() calls to make sure we don't get
recursive calls.  Also simplify CC guest detection.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 .../Library/QemuFwCfgLib/QemuFwCfgPeiLib.inf  |   1 -
 OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgPei.c   | 101 +++++++-----------
 2 files changed, 41 insertions(+), 61 deletions(-)

diff --git a/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgPeiLib.inf b/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgPeiLib.inf
index 3910511880c9..1d7543a7d40f 100644
--- a/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgPeiLib.inf
+++ b/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgPeiLib.inf
@@ -41,7 +41,6 @@ [LibraryClasses]
   DebugLib
   IoLib
   MemoryAllocationLib
-  MemEncryptSevLib
 
 [Pcd]
   gUefiOvmfPkgTokenSpaceGuid.PcdOvmfWorkAreaBase
diff --git a/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgPei.c b/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgPei.c
index 7ab7027af168..a936fd103955 100644
--- a/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgPei.c
+++ b/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgPei.c
@@ -13,7 +13,6 @@
 #include <Library/IoLib.h>
 #include <Library/DebugLib.h>
 #include <Library/QemuFwCfgLib.h>
-#include <Library/MemEncryptSevLib.h>
 #include <WorkArea.h>
 
 #include "QemuFwCfgLibInternal.h"
@@ -27,15 +26,16 @@ STATIC BOOLEAN  mQemuFwCfgDmaSupported;
   @retval    TRUE   It is Tdx guest
   @retval    FALSE  It is not Tdx guest
 **/
+STATIC
 BOOLEAN
-QemuFwCfgIsTdxGuest (
+QemuFwCfgIsCcGuest (
   VOID
   )
 {
   CONFIDENTIAL_COMPUTING_WORK_AREA_HEADER  *CcWorkAreaHeader;
 
   CcWorkAreaHeader = (CONFIDENTIAL_COMPUTING_WORK_AREA_HEADER *)FixedPcdGet32 (PcdOvmfWorkAreaBase);
-  return (CcWorkAreaHeader != NULL && CcWorkAreaHeader->GuestType == CcGuestTypeIntelTdx);
+  return (CcWorkAreaHeader != NULL && CcWorkAreaHeader->GuestType != CcGuestTypeNonEncrypted);
 }
 
 /**
@@ -57,62 +57,49 @@ QemuFwCfgIsAvailable (
   return InternalQemuFwCfgIsAvailable ();
 }
 
+STATIC
+VOID
+QemuFwCfgProbe (
+  BOOLEAN  *Supported,
+  BOOLEAN  *DmaSupported
+  )
+{
+  UINT32   Signature;
+  UINT32   Revision;
+  BOOLEAN  CcGuest;
+
+  // Use direct Io* calls for probing to avoid recursion.
+  IoWrite16 (FW_CFG_IO_SELECTOR, (UINT16)QemuFwCfgItemSignature);
+  IoReadFifo8 (FW_CFG_IO_DATA, sizeof Signature, &Signature);
+  IoWrite16 (FW_CFG_IO_SELECTOR, (UINT16)QemuFwCfgItemInterfaceVersion);
+  IoReadFifo8 (FW_CFG_IO_DATA, sizeof Revision, &Revision);
+  CcGuest = QemuFwCfgIsCcGuest ();
+
+  *Supported    = FALSE;
+  *DmaSupported = FALSE;
+  if ((Signature == SIGNATURE_32 ('Q', 'E', 'M', 'U')) && (Revision >= 1)) {
+    *Supported = TRUE;
+    if ((Revision & FW_CFG_F_DMA) && !CcGuest) {
+      *DmaSupported = TRUE;
+    }
+  }
+
+  DEBUG ((
+    DEBUG_INFO,
+    "%a: Supported %d, DMA %d\n",
+    __func__,
+    *Supported,
+    *DmaSupported
+    ));
+}
+
 RETURN_STATUS
 EFIAPI
 QemuFwCfgInitialize (
   VOID
   )
 {
-  UINT32  Signature;
-  UINT32  Revision;
-
-  //
-  // Enable the access routines while probing to see if it is supported.
-  // For probing we always use the IO Port (IoReadFifo8()) access method.
-  //
-  mQemuFwCfgSupported    = TRUE;
-  mQemuFwCfgDmaSupported = FALSE;
-
-  QemuFwCfgSelectItem (QemuFwCfgItemSignature);
-  Signature = QemuFwCfgRead32 ();
-  DEBUG ((DEBUG_INFO, "FW CFG Signature: 0x%x\n", Signature));
-  QemuFwCfgSelectItem (QemuFwCfgItemInterfaceVersion);
-  Revision = QemuFwCfgRead32 ();
-  DEBUG ((DEBUG_INFO, "FW CFG Revision: 0x%x\n", Revision));
-  if ((Signature != SIGNATURE_32 ('Q', 'E', 'M', 'U')) ||
-      (Revision < 1)
-      )
-  {
-    DEBUG ((DEBUG_INFO, "QemuFwCfg interface not supported.\n"));
-    mQemuFwCfgSupported = FALSE;
-    return RETURN_SUCCESS;
-  }
-
-  if ((Revision & FW_CFG_F_DMA) == 0) {
-    DEBUG ((DEBUG_INFO, "QemuFwCfg interface (IO Port) is supported.\n"));
-  } else {
-    //
-    // If SEV is enabled then we do not support DMA operations in PEI phase.
-    // This is mainly because DMA in SEV guest requires using bounce buffer
-    // (which need to allocate dynamic memory and allocating a PAGE size'd
-    // buffer can be challenge in PEI phase)
-    //
-    if (MemEncryptSevIsEnabled ()) {
-      DEBUG ((DEBUG_INFO, "SEV: QemuFwCfg fallback to IO Port interface.\n"));
-    } else if (QemuFwCfgIsTdxGuest ()) {
-      //
-      // If TDX is enabled then we do not support DMA operations in PEI phase.
-      // This is mainly because DMA in TDX guest requires using bounce buffer
-      // (which need to allocate dynamic memory and allocating a PAGE size'd
-      // buffer can be challenge in PEI phase)
-      //
-      DEBUG ((DEBUG_INFO, "TDX: QemuFwCfg fallback to IO Port interface.\n"));
-    } else {
-      mQemuFwCfgDmaSupported = TRUE;
-      DEBUG ((DEBUG_INFO, "QemuFwCfg interface (DMA) is supported.\n"));
-    }
-  }
-
+  QemuFwCfgProbe (&mQemuFwCfgSupported, &mQemuFwCfgDmaSupported);
   return RETURN_SUCCESS;
 }
 
@@ -183,17 +170,11 @@ InternalQemuFwCfgDmaBytes (
     return;
   }
 
-  //
-  // SEV does not support DMA operations in PEI stage, we should
-  // not have reached here.
-  //
-  ASSERT (!MemEncryptSevIsEnabled ());
-
   //
   // TDX does not support DMA operations in PEI stage, we should
   // not have reached here.
   //
-  ASSERT (!QemuFwCfgIsTdxGuest ());
+  ASSERT (!QemuFwCfgIsCcGuest ());
 
   Access.Control = SwapBytes32 (Control);
   Access.Length  = SwapBytes32 (Size);
-- 
2.38.1


  parent reply	other threads:[~2022-12-02 13:10 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-02 13:09 [PATCH 00/14] OvmfPkg: avoid global variables in PEI Gerd Hoffmann
2022-12-02 13:09 ` [PATCH 01/14] tools_def: remove GCC_IA32_CC_FLAGS/GCC_X64_CC_FLAGS Gerd Hoffmann
2022-12-02 13:09 ` [PATCH 02/14] tools_def: add -fno-omit-frame-pointer to GCC48_{IA32,X64}_CC_FLAGS Gerd Hoffmann
2022-12-02 13:09 ` [PATCH 03/14] OvmfPkg/PlatformPei: AmdSev: stop using mPlatformInfoHob Gerd Hoffmann
2022-12-02 13:09 ` [PATCH 04/14] OvmfPkg/PlatformPei: PeiFv: " Gerd Hoffmann
2022-12-02 13:10 ` [PATCH 05/14] OvmfPkg/PlatformPei Q35 SMM helpers: " Gerd Hoffmann
2022-12-02 13:10 ` [PATCH 06/14] OvmfPkg/PlatformPei: PeiMemory: " Gerd Hoffmann
2022-12-02 13:10 ` [PATCH 07/14] OvmfPkg/PlatformPei: MemTypeInfo: " Gerd Hoffmann
2022-12-02 13:10 ` [PATCH 08/14] OvmfPkg/PlatformPei: NoExec: " Gerd Hoffmann
2022-12-02 13:10 ` [PATCH 09/14] OvmfPkg/PlatformPei: Verification: " Gerd Hoffmann
2022-12-02 13:10 ` [PATCH 10/14] OvmfPkg/PlatformPei: remove mPlatformInfoHob Gerd Hoffmann
2022-12-02 13:10 ` [PATCH 11/14] OvmfPkg/PlatformPei: remove mFeatureControlValue Gerd Hoffmann
2022-12-02 13:10 ` [PATCH 12/14] OvmfPkg/DebugLibIoPort: use Rom version for PEI Gerd Hoffmann
2022-12-02 13:10 ` Gerd Hoffmann [this message]
2022-12-02 13:10 ` [PATCH 14/14] OvmfPkg/QemuFwCfgLib: remove mQemuFwCfgSupported + mQemuFwCfgDmaSupported Gerd Hoffmann
2022-12-12 16:58 ` [PATCH 00/14] OvmfPkg: avoid global variables in PEI Lendacky, Thomas
2022-12-14 17:26   ` Ard Biesheuvel

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=20221202131009.3190040-14-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