From nobody Fri May 3 16:11:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+90657+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+90657+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1655847126; cv=none; d=zohomail.com; s=zohoarc; b=DW+6oLggtUpVJN+hsIRlLb80yf8YTcXMLvyFaLp9Ds6L5uXfvN97VgNNLor6K9cmurTjw1di6p3S1fCosi9q8jAxxe1sy/ceWNeHaGwPCZxCSggtH+5rektaXzeX2KID1di7pzxwC5Upttrzb7aprKEtC0HsF3zAYKapowVYDro= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1655847126; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=1zt+98YzTw8fHJnzdGH6FNqEnStObwyatyBCcoVEteA=; b=Uk6fXSNiJ6GALI+zt/8jR0b59/Qrbl6RbfhYw+PLL41jBfDQVBWazlQXf2XXH52K2y78j+ql4NBzhyLBFF853bZ0784KarLV6CAeNXevBcQO/xSd1t4qTcJ7dBJXBbOCCB6eNKv05afwGI/um9ERHQxJwWSaT8kfaG8DsGQzLOg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+90657+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1655847126195696.4766100354603; Tue, 21 Jun 2022 14:32:06 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id gSO4YY1788612xf6F55IQVa8; Tue, 21 Jun 2022 14:32:05 -0700 X-Received: from smtp-fw-33001.amazon.com (smtp-fw-33001.amazon.com [207.171.190.10]) by mx.groups.io with SMTP id smtpd.web10.40.1655847125081867180 for ; Tue, 21 Jun 2022 14:32:05 -0700 X-IronPort-AV: E=Sophos;i="5.92,210,1650931200"; d="scan'208";a="204106123" X-Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-pdx-2a-6a4112b2.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-33001.sea14.amazon.com with ESMTP; 21 Jun 2022 21:31:49 +0000 X-Received: from EX13D49EUC003.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan2.pdx.amazon.com [10.236.137.194]) by email-inbound-relay-pdx-2a-6a4112b2.us-west-2.amazon.com (Postfix) with ESMTPS id 063654C8D37; Tue, 21 Jun 2022 21:31:47 +0000 (UTC) X-Received: from ub4014a598e6c52.ant.amazon.com (10.43.161.183) by EX13D49EUC003.ant.amazon.com (10.43.164.91) with Microsoft SMTP Server (TLS) id 15.0.1497.36; Tue, 21 Jun 2022 21:31:42 +0000 From: "Ojeda Leon, Nicolas via groups.io" To: CC: , , , Nicolas Ojeda Leon , Alexander Graf , Gerd Hoffmann Subject: [edk2-devel] [PATCH v6 1/5] OvmfPkg/Library: Create base HardwareInfoLib for PCI Host Bridges Date: Tue, 21 Jun 2022 23:31:24 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.43.161.183] X-ClientProxiedBy: EX13D39UWA004.ant.amazon.com (10.43.160.73) To EX13D49EUC003.ant.amazon.com (10.43.164.91) Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ncoleon@amazon.com X-Gm-Message-State: FyV0eoCYGYRVSdWOmu0H3SX9x1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1655847125; bh=iX+9AaH77J4etWvD10dXnwIusSJcyZi3Gj4nNckLoxo=; h=CC:Content-Type:Date:From:Reply-To:Subject:To; b=qeQA2dK8Ow36PcbGTuR59KNRIwQyRyq99i5VR66GgBO/F5Y5tRTJjH20AujLoh1j/bO mZHnEEpaJz+PE2HVyGdShGYNEHZLc1wYpcwc5i8KAYLardp2KfmhI/blmTZWV461/nhRA 1S1zG5Vsaw/fu8gdJsWhUiHE30YLH1apYGo= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1655847128122100001 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Create the Hardware Info library base together with the specifics to describe PCI Host Bridges. The Hardware Info library is intended to be used for disclosing non-discoverable hardware information from the host to the guest in Ovmf platforms. Core functionality will provide the possibility to parse information from a generic BLOB into runtime structures. The library is conceived in a generic way so that further hardware elements can also be described using it. For such purpose the length of the BLOB is not restricted but instead regarded as a sequence of header-info elements that allow the parsing during runtime. The first type of hardware defined will be PCI host bridges, providing the possibility to define multiple and specify the resources each of them can use. This enables the guest firmware to configure PCI resources properly. Having the size of each individual element favors the reuse of a single interface to convey descriptions of an arbitrary number of heterogenous hardware elements. Furthermore, flexible access mechanisms coupled with the size will grant the possibility of interpreting them in a single run. Define the base types of the generic Hardware Info library to parse heterogeneous data. Also provide the specific changes to support PCI host bridges as the first hardware type supported by the library. Additionally, define the HOST_BRIDGE_INFO structure to describe PCI host bridges along with the functionality to parse such information into proper structures used by the PCI driver in a centralized manner and taking care of versioning. As an example and motivation, the library will be used to define multiple PCI host bridges for complex platforms that require it. The first means of transportation that will be used is going to be fw-cfg, over which a stream of bytes will be transferred and later parsed by the hardware info library. Accordingly, the PCI driver will make use of these host bridges definitions to populate the list of Root Bridges and proceed with the configuration and discovery of underlying hardware components. As mentioned before, the binary data to be parsed by the Hardware Info library should be organized as a sequence of Header-element pairs in which the header describes the type and size of the associated element that comes right after it. As an illustration, to provide inforation of 3 host bridges the data, conceptually, would look like this: Header PCI Host Bridge (type and size) # 1 PCI Host Bridge info # 1 Header PCI Host Bridge (type and size) # 2 PCI Host Bridge info # 2 Header PCI Host Bridge (type and size) # 3 PCI Host Bridge info # 3 Cc: Alexander Graf Cc: Gerd Hoffmann Acked-by: Gerd Hoffmann Signed-off-by: Nicolas Ojeda Leon --- Notes: v6: Preparation for merge: - Updated files' copyright to follow EDK2 format - Modified HOST_BRIDGE_INFO bitfield member to use UINT32 instead of UINT64 to comply with coding guidelines motivated by building on 32-bit windows system, adjusted padding as well to preserve 8-byte alignment while reducing structure size. - Included HardwareInfoTypeMax to the enum following coding guidelines. v4: - Minor change in InvalidateRootBridgeAperture to always set the invalid base address to MAX_UINT64, thus having a universal invalid value. --- .../HardwareInfoPciHostBridgeLib.c | 508 ++++++++++++++++++ .../HardwareInfoPciHostBridgeLib.h | 256 +++++++++ .../HardwareInfoLib/HardwareInfoTypesLib.h | 59 ++ 3 files changed, 823 insertions(+) create mode 100644 OvmfPkg/Library/HardwareInfoLib/HardwareInfoPciHostBrid= geLib.c create mode 100644 OvmfPkg/Library/HardwareInfoLib/HardwareInfoPciHostBrid= geLib.h create mode 100644 OvmfPkg/Library/HardwareInfoLib/HardwareInfoTypesLib.h diff --git a/OvmfPkg/Library/HardwareInfoLib/HardwareInfoPciHostBridgeLib.c= b/OvmfPkg/Library/HardwareInfoLib/HardwareInfoPciHostBridgeLib.c new file mode 100644 index 0000000000..26cd435adf --- /dev/null +++ b/OvmfPkg/Library/HardwareInfoLib/HardwareInfoPciHostBridgeLib.c @@ -0,0 +1,508 @@ +/**@file + Hardware info library with types and accessors to parse information about + PCI host bridges. + + Copyright 2021 - 2022 Amazon.com, Inc. or its affiliates. All Rights Res= erved. + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +#include "HardwareInfoPciHostBridgeLib.h" + +#define IS_RANGE_INVALID(Start, Size, MaxValue) (Start >=3D MaxValue || S= ize =3D=3D 0) + +/** + Calculate the last (inclusive) address of a range. + + @param[in] Start First address of the range + @param[in] Size Size of the range + @return Last address of the range +**/ +STATIC +UINT64 +GetRangeEnd ( + IN UINT64 Start, + IN UINT64 Size, + IN UINT64 MaxValue + ) +{ + if (IS_RANGE_INVALID (Start, Size, MaxValue)) { + return 0; + } + + return Start + Size - 1; +} + +/** + Internal helper to update LastAddress if the Limit address + of the Mem aperture is higher than the provided value. + + @param[in] Mem Pointer to aperture whose limit is + to be compared against accumulative + last address. + @param[out] LastAddress Pointer to accumulative last address + to be updated if Limit is higher +**/ +STATIC +VOID +UpdateLastAddressIfHigher ( + IN PCI_ROOT_BRIDGE_APERTURE *Mem, + OUT UINT64 *LastAddress + ) +{ + if (Mem->Limit > *LastAddress) { + *LastAddress =3D Mem->Limit; + } +} + +EFI_STATUS +HardwareInfoPciHostBridgeLastMmioAddress ( + IN CONST HOST_BRIDGE_INFO *HostBridge, + IN UINTN DataSize, + IN BOOLEAN HighMem, + OUT UINT64 *LastMmioAddress + ) +{ + EFI_STATUS Status; + PCI_ROOT_BRIDGE_APERTURE Mem; + PCI_ROOT_BRIDGE_APERTURE MemAbove4G; + PCI_ROOT_BRIDGE_APERTURE PMem; + PCI_ROOT_BRIDGE_APERTURE PMemAbove4G; + + if (LastMmioAddress =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Set the output to the lowest possible value so that if some step fails + // the overall outcome reflects no information found + // + *LastMmioAddress =3D 0; + + Status =3D HardwareInfoPciHostBridgeGetApertures ( + HostBridge, + DataSize, + NULL, + &Mem, + &MemAbove4G, + &PMem, + &PMemAbove4G, + NULL + ); + + // + // Forward error to caller but ignore warnings given that, very likely, + // the host bridge will have a PIO aperture we are explicitly + // ignoring here since we care only about MMIO resources. + // + if (EFI_ERROR (Status)) { + return Status; + } + + if (HighMem) { + UpdateLastAddressIfHigher (&MemAbove4G, LastMmioAddress); + UpdateLastAddressIfHigher (&PMemAbove4G, LastMmioAddress); + } else { + UpdateLastAddressIfHigher (&Mem, LastMmioAddress); + UpdateLastAddressIfHigher (&PMem, LastMmioAddress); + } + + return EFI_SUCCESS; +} + +/** + Set the boundaries of an aperture to invalid values having + size zero and start MaxValue (yields Start > Limit which + depicts an invalid range) + + @param[in] MaxValue Max value of the aperture's range (depends + on the data type) + @param[out] Aperture Aperture object to invalidate +**/ +STATIC +VOID +InvalidateRootBridgeAperture ( + OUT PCI_ROOT_BRIDGE_APERTURE *Aperture + ) +{ + if (Aperture =3D=3D NULL) { + return; + } + + Aperture->Base =3D MAX_UINT64; + Aperture->Limit =3D 0; +} + +/** + Fill a PCI ROOT BRIDGE APERTURE with the proper values calculated + from the provided start and size. + + @param[in] Start Start address of the aperture + @param[in] Size Size, in bytes, of the aperture + @param[in] MaxValue Max value a valid address could take and which + represents an invalid start address. + @param[out] Aperture Pointer to the aperture to be filled + + @retval EFI_SUCCESS Aperture was filled successfully + @retval EFI_INVALID_PARAMETER Range depicted by Start and Size is + valid but ignored because aperture + pointer is NULL + @retval EFI_WARN_BUFFER_TOO_SMALL Aperture pointer is invalid but the + range also is so no harm. +**/ +STATIC +EFI_STATUS +FillHostBridgeAperture ( + IN UINT64 Start, + IN UINT64 Size, + IN UINT64 MaxValue, + OUT PCI_ROOT_BRIDGE_APERTURE *Aperture + ) +{ + UINT64 End; + + End =3D GetRangeEnd (Start, Size, MaxValue); + + if (Aperture =3D=3D NULL) { + if (!IS_RANGE_INVALID (Start, Size, MaxValue)) { + // + // Report an error to the caller since the range specified in + // the host bridge's resources is non-empty but the provided + // aperture pointer is null, thus the valid range is ignored. + // + return EFI_INVALID_PARAMETER; + } + + return EFI_WARN_BUFFER_TOO_SMALL; + } + + if (IS_RANGE_INVALID (Start, Size, MaxValue)) { + // + // Fill Aperture with invalid range values to signal the + // absence of an address space (empty range) + // + InvalidateRootBridgeAperture (Aperture); + } else { + Aperture->Base =3D Start; + Aperture->Limit =3D End; + } + + return EFI_SUCCESS; +} + +/** + Merge 2 ranges (normal and prefetchable) into a single aperture + comprehending the addresses encompassed by both of them. If both + ranges are not empty they must be contiguous for correctness. + + @param[in] Start Range start address + @param[in] Size Range size in bytes + @param[in] PStart Prefetchable range start address + @param[in] PSize Prefetchable range size in bytes + @param[in] MaxValue Max value a valid address could take and which + represents an invalid start address. + @param[out] Aperture Pointer to the aperture to be filled + + @retval EFI_SUCCESS Aperture was filled successfully + @retval EFI_INVALID_PARAMETER Either range depicted by Start, Size + or PStart, PSize or both are valid + but ignored because aperture pointer + is NULL + @retval EFI_WARN_BUFFER_TOO_SMALL Aperture pointer is invalid but both + ranges are too so no harm. +**/ +STATIC +EFI_STATUS +MergeHostBridgeApertures ( + IN UINT64 Start, + IN UINT64 Size, + IN UINT64 PStart, + IN UINT64 PSize, + IN UINT64 MaxValue, + OUT PCI_ROOT_BRIDGE_APERTURE *Aperture + ) +{ + UINT64 PEnd; + + if (Aperture =3D=3D NULL) { + if (!IS_RANGE_INVALID (Start, Size, MaxValue) || + !IS_RANGE_INVALID (PStart, PSize, MaxValue)) + { + // + // Report an error to the caller since the range specified in + // the host bridge's resources is non-empty but the provided + // aperture pointer is null, thus the valid range is ignored. + // + return EFI_INVALID_PARAMETER; + } + + return EFI_WARN_BUFFER_TOO_SMALL; + } + + // + // Start from an empty range (Limit < Base) + // + InvalidateRootBridgeAperture (Aperture); + + if (!IS_RANGE_INVALID (Start, Size, MaxValue)) { + Aperture->Base =3D Start; + Aperture->Limit =3D Start + Size - 1; + } + + if (!IS_RANGE_INVALID (PStart, PSize, MaxValue)) { + PEnd =3D PStart + PSize - 1; + + if (PStart < Aperture->Base) { + Aperture->Base =3D PStart; + } + + if (PEnd > Aperture->Limit) { + Aperture->Limit =3D PEnd; + } + } + + return EFI_SUCCESS; +} + +EFI_STATUS +HardwareInfoPciHostBridgeGetBusNrRange ( + IN CONST HOST_BRIDGE_INFO *HostBridge, + IN UINTN DataSize, + OUT UINTN *BusNrStart, + OUT UINTN *BusNrLast + ) +{ + if ((HostBridge =3D=3D NULL) || (DataSize =3D=3D 0) || + (BusNrStart =3D=3D NULL) || (BusNrLast =3D=3D NULL)) + { + return EFI_INVALID_PARAMETER; + } + + // + // For now only version 0 is supported + // + if (HostBridge->Version !=3D 0) { + return EFI_INCOMPATIBLE_VERSION; + } + + *BusNrStart =3D HostBridge->BusNrStart; + *BusNrLast =3D HostBridge->BusNrLast; + + return EFI_SUCCESS; +} + +EFI_STATUS +HardwareInfoPciHostBridgeGetApertures ( + IN CONST HOST_BRIDGE_INFO *HostBridge, + IN UINTN DataSize, + OUT PCI_ROOT_BRIDGE_APERTURE *Io, + OUT PCI_ROOT_BRIDGE_APERTURE *Mem, + OUT PCI_ROOT_BRIDGE_APERTURE *MemAbove4G, + OUT PCI_ROOT_BRIDGE_APERTURE *PMem, + OUT PCI_ROOT_BRIDGE_APERTURE *PMemAbove4G, + OUT PCI_ROOT_BRIDGE_APERTURE *PcieConfig + ) +{ + EFI_STATUS Status; + BOOLEAN StickyError; + + StickyError =3D FALSE; + if ((HostBridge =3D=3D NULL) || (DataSize =3D=3D 0)) { + return EFI_INVALID_PARAMETER; + } + + // + // For now only version 0 is supported + // + if (HostBridge->Version !=3D 0) { + return EFI_INCOMPATIBLE_VERSION; + } + + Status =3D FillHostBridgeAperture ( + HostBridge->IoStart, + HostBridge->IoSize, + MAX_UINT32, + Io + ); + StickyError |=3D EFI_ERROR (Status); + + Status =3D FillHostBridgeAperture ( + HostBridge->PcieConfigStart, + HostBridge->PcieConfigSize, + MAX_UINT64, + PcieConfig + ); + StickyError |=3D EFI_ERROR (Status); + + if (HostBridge->Flags.Bits.CombineMemPMem) { + Status =3D MergeHostBridgeApertures ( + HostBridge->MemStart, + HostBridge->MemSize, + HostBridge->PMemStart, + HostBridge->PMemSize, + MAX_UINT32, + Mem + ); + StickyError |=3D EFI_ERROR (Status); + + Status =3D MergeHostBridgeApertures ( + HostBridge->MemAbove4GStart, + HostBridge->MemAbove4GSize, + HostBridge->PMemAbove4GStart, + HostBridge->PMemAbove4GSize, + MAX_UINT64, + MemAbove4G + ); + StickyError |=3D EFI_ERROR (Status); + + // + // Invalidate unused apertures + // + InvalidateRootBridgeAperture (PMem); + InvalidateRootBridgeAperture (PMemAbove4G); + } else { + Status =3D FillHostBridgeAperture ( + HostBridge->MemStart, + HostBridge->MemSize, + MAX_UINT32, + Mem + ); + StickyError |=3D EFI_ERROR (Status); + + Status =3D FillHostBridgeAperture ( + HostBridge->PMemStart, + HostBridge->PMemSize, + MAX_UINT32, + PMem + ); + StickyError |=3D EFI_ERROR (Status); + + Status =3D FillHostBridgeAperture ( + HostBridge->MemAbove4GStart, + HostBridge->MemAbove4GSize, + MAX_UINT64, + MemAbove4G + ); + StickyError |=3D EFI_ERROR (Status); + + Status =3D FillHostBridgeAperture ( + HostBridge->PMemAbove4GStart, + HostBridge->PMemAbove4GSize, + MAX_UINT64, + PMem + ); + StickyError |=3D EFI_ERROR (Status); + } + + if (StickyError) { + // + // If any function returned an error it is due to a valid range + // specified in the host bridge that was ignored due to a NULL + // pointer. Translate it to a warning to allow for calling with + // only a subset of the apertures. + // + return EFI_WARN_STALE_DATA; + } + + return EFI_SUCCESS; +} + +EFI_STATUS +HardwareInfoPciHostBridgeGetFlags ( + IN CONST HOST_BRIDGE_INFO *HostBridge, + IN UINTN DataSize, + OUT UINT64 *Attributes OPTIONAL, + OUT BOOLEAN *DmaAbove4G OPTIONAL, + OUT BOOLEAN *NoExtendedConfigSpace OPTIONAL, + OUT BOOLEAN *CombineMemPMem OPTIONAL + ) +{ + if ((HostBridge =3D=3D NULL) || (DataSize =3D=3D 0)) { + return EFI_INVALID_PARAMETER; + } + + // + // For now only version 0 is supported + // + if (HostBridge->Version !=3D 0) { + return EFI_INCOMPATIBLE_VERSION; + } + + if (Attributes) { + *Attributes =3D HostBridge->Attributes; + } + + if (DmaAbove4G) { + *DmaAbove4G =3D !!HostBridge->Flags.Bits.DmaAbove4G; + } + + if (NoExtendedConfigSpace) { + *NoExtendedConfigSpace =3D !!HostBridge->Flags.Bits.NoExtendedConfigSp= ace; + } + + if (CombineMemPMem) { + *CombineMemPMem =3D !!HostBridge->Flags.Bits.CombineMemPMem; + } + + return EFI_SUCCESS; +} + +EFI_STATUS +HardwareInfoPciHostBridgeGet ( + IN CONST HOST_BRIDGE_INFO *HostBridge, + IN UINTN DataSize, + OUT UINTN *BusNrStart, + OUT UINTN *BusNrLast, + OUT UINT64 *Attributes OPTIONAL, + OUT BOOLEAN *DmaAbove4G OPTIONAL, + OUT BOOLEAN *NoExtendedConfigSpace OPTIONAL, + OUT BOOLEAN *CombineMemPMem OPTIONAL, + OUT PCI_ROOT_BRIDGE_APERTURE *Io OPTIONAL, + OUT PCI_ROOT_BRIDGE_APERTURE *Mem OPTIONAL, + OUT PCI_ROOT_BRIDGE_APERTURE *MemAbove4G OPTIONAL, + OUT PCI_ROOT_BRIDGE_APERTURE *PMem OPTIONAL, + OUT PCI_ROOT_BRIDGE_APERTURE *PMemAbove4G OPTIONAL, + OUT PCI_ROOT_BRIDGE_APERTURE *PcieConfig OPTIONAL + ) +{ + EFI_STATUS Status; + + Status =3D HardwareInfoPciHostBridgeGetBusNrRange ( + HostBridge, + DataSize, + BusNrStart, + BusNrLast + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + Status =3D HardwareInfoPciHostBridgeGetFlags ( + HostBridge, + DataSize, + Attributes, + DmaAbove4G, + NoExtendedConfigSpace, + CombineMemPMem + ); + + if (EFI_ERROR (Status)) { + return Status; + } + + Status =3D HardwareInfoPciHostBridgeGetApertures ( + HostBridge, + DataSize, + Io, + Mem, + MemAbove4G, + PMem, + PMemAbove4G, + PcieConfig + ); + + return Status; +} diff --git a/OvmfPkg/Library/HardwareInfoLib/HardwareInfoPciHostBridgeLib.h= b/OvmfPkg/Library/HardwareInfoLib/HardwareInfoPciHostBridgeLib.h new file mode 100644 index 0000000000..627de118d3 --- /dev/null +++ b/OvmfPkg/Library/HardwareInfoLib/HardwareInfoPciHostBridgeLib.h @@ -0,0 +1,256 @@ +/**@file + Hardware info library with types and accessors to parse information about + PCI host bridges. + + Copyright 2021 - 2022 Amazon.com, Inc. or its affiliates. All Rights Res= erved. + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __HARDWARE_INFO_PCI_HOST_BRIDGE_LIB_H__ +#define __HARDWARE_INFO_PCI_HOST_BRIDGE_LIB_H__ + +#include +#include +#include + +// +// Host Bridge resources information +// +#pragma pack(1) +typedef struct { + // + // Feature tracking, initially 0 + // + UINT64 Version; + + // + // Host bridge enabled attributes (EFI_PCI_ATTRIBUTE_*) + // + UINT64 Attributes; + + union { + UINT32 Uint32; + struct { + UINT32 DmaAbove4G : 1; + UINT32 NoExtendedConfigSpace : 1; + UINT32 CombineMemPMem : 1; + UINT32 Reserved : 29; + } Bits; + } Flags; + + // + // Bus number range + // + UINT8 BusNrStart; + UINT8 BusNrLast; + + UINT8 Padding[2]; + + // + // IO aperture + // + UINT64 IoStart; + UINT64 IoSize; + + // + // 32-bit MMIO aperture + // + UINT64 MemStart; + UINT64 MemSize; + + // + // 32-bit prefetchable MMIO aperture + // + UINT64 PMemStart; + UINT64 PMemSize; + + // + // 64-bit MMIO aperture + // + UINT64 MemAbove4GStart; + UINT64 MemAbove4GSize; + + // + // 64-bit prefetchable MMIO aperture + // + UINT64 PMemAbove4GStart; + UINT64 PMemAbove4GSize; + + // + // MMIO accessible PCIe config space (ECAM) + // + UINT64 PcieConfigStart; + UINT64 PcieConfigSize; +} HOST_BRIDGE_INFO; +#pragma pack() + +/** + Extract the last MMIO address, either from high (64-bit) or low (32-bit) + memory used by the HostBridge's apertures. + + @param[in] HostBridge Root bridge's resources specification + @param[in] DataSize Size in bytes of the actually filled + data available in the HostBridge object + @param[in] HighMem 64-bit (true) or 32-bit (false) MMIO + address + @param[out] LastMmioAddress Pointer to last MMIO address + + @retval EFI_SUCCESS Operation succeeded + @retval EFI_INVALID_PARAMETER One or more pointer parameters are + invalid + @retval EFI_INCOMPATIBLE_VERSION HostBridge information belongs to + an unsupported version +**/ +EFI_STATUS +HardwareInfoPciHostBridgeLastMmioAddress ( + IN CONST HOST_BRIDGE_INFO *HostBridge, + IN UINTN DataSize, + IN BOOLEAN HighMem, + OUT UINT64 *LastMmioAddress + ); + +/** + Interpret the HostBridge resources and extact the bus number + range. + + @param[in] HostBridge Root bridge's resources specification + @param[in] DataSize Size in bytes of the actually filled + data available in the HostBridge object + @param[out] BusNrStart Pointer to the Bus Number range start + @param[out] BusNrLast Pointer to the Bus Number range end + + @retval EFI_SUCCESS Retrieved the bus number range + without any issues. + @retval EFI_INVALID_PARAMETER One of the parameters is invalid, + either NULL pointer or size 0 + @retval EFI_INCOMPATIBLE_VERSION HostBridge data of unsupported + version +**/ +EFI_STATUS +HardwareInfoPciHostBridgeGetBusNrRange ( + IN CONST HOST_BRIDGE_INFO *HostBridge, + IN UINTN DataSize, + OUT UINTN *BusNrStart, + OUT UINTN *BusNrLast + ); + +/** + Interpret the MMIO resources in HostBridge and set the apertures + in 32-bit space (Mem), 64-bit space (MemAbove4G), PIO (IO) and + ECAM (PcieConfig) accordingly. + + The 2 types of apertures in each MMIO space (prefetchable and + non-prefetchable) may be merged into a single window, hence if both + types of apertures are defined while the CombineMemPMem flag is set, + the ranges must be contiguous. + + @param[in] HostBridge Root bridge's resources specification + @param[in] DataSize Size in bytes of the actually filled + data available in the HostBridge object + @param[out] Mem Pointer to 32-bit MMIO aperture + @param[out] MemAbove4G Pointer to 64-bit MMIO aperture + @param[out] PMem Pointer to the 32-bit prefetchable MMIO aperture + @param[out] PMemAbove4G Pointer to the 64-bit prefetchable MMIO aperture + @param[out] PcieConfig Pointer to MMIO mapped PCIe config aperture (EC= AM) + + @retval EFI_INVALID_PARAMETER HostBridge object is invalid + @retval EFI_INCOMPATIBLE_VERSION HostBridge information belongs to + an unsupported version + @retval EFI_WARN_STALE_DATA One or more valid aperture in the + HostBridge's resources were ignored + because corresponding aperture pointer + is NULL. + @retval EFI_SUCCESS Operation executed cleanly, all valid + ranges were parsed into the correspond= ing + aperture object. +**/ +EFI_STATUS +HardwareInfoPciHostBridgeGetApertures ( + IN CONST HOST_BRIDGE_INFO *HostBridge, + IN UINTN DataSize, + OUT PCI_ROOT_BRIDGE_APERTURE *Io, + OUT PCI_ROOT_BRIDGE_APERTURE *Mem, + OUT PCI_ROOT_BRIDGE_APERTURE *MemAbove4G, + OUT PCI_ROOT_BRIDGE_APERTURE *PMem, + OUT PCI_ROOT_BRIDGE_APERTURE *PMemAbove4G, + OUT PCI_ROOT_BRIDGE_APERTURE *PcieConfig + ); + +/** + Retrieve all flags and attributes of a host bridge describing the + resources and capabilities. + + @param[in] HostBridge Host bridge information object + @param[in] DataSize Size in bytes of the actually filled + data available in the HostBridge object + @param[out] Attributes Pointer to the host bridge's attributes + @param[out] DmaAbove4G Pointer to the DMA Above 4G flag + @param[out] NoExtendedConfigSpace Pointer to the Extended Config Space f= lag + @param[out] CombineMemPMem Pointer to the Combine Mem and PMem fl= ag + + @retval EFI_INVALID_PARAMETER HostBridge object is invalid + @retval EFI_INCOMPATIBLE_VERSION HostBridge information belongs to + an unsupported version + @retval EFI_SUCCESS Operation executed cleanly +**/ +EFI_STATUS +HardwareInfoPciHostBridgeGetFlags ( + IN CONST HOST_BRIDGE_INFO *HostBridge, + IN UINTN DataSize, + OUT UINT64 *Attributes OPTIONAL, + OUT BOOLEAN *DmaAbove4G OPTIONAL, + OUT BOOLEAN *NoExtendedConfigSpace OPTIONAL, + OUT BOOLEAN *CombineMemPMem OPTIONAL + ); + +/** + Getter that parses information from a HOST_BRIDGE_INFO object + into smaller chunks of types handled by the PciHostBridgeLib. + + @param[in] HostBridge Host bridge information object + @param[in] DataSize Size in bytes of the actually filled + data available in the HostBridge object + @param[out] BusNrStart Pointer to the Bus Number range start + @param[out] BusNrLast Pointer to the Bus Number range end + @param[out] Attributes Pointer to the host bridge's attributes + @param[out] DmaAbove4G Pointer to the DMA Above 4G flag + @param[out] NoExtendedConfigSpace Pointer to the Extended Config Space f= lag + @param[out] CombineMemPMem Pointer to the Combine Mem and PMem fl= ag + @param[out] Io Pointer to the PIO aperture object + @param[out] Mem Pointer to the 32-bit MMIO aperture ob= ject + @param[out] MemAbove4G Pointer to the 64-bit MMIO aperture ob= ject + @param[out] PMem Pointer to the 32-bit prefetchable MMIO + aperture object + @param[out] PMemAbove4G Pointer to the 64-bit prefetchable MMIO + aperture object + @param[out] PcieConfig MMIO mapped PCIe config aperture (ECAM) + + @retval EFI_SUCCESS Whole operation succeeded + @retval EFI_INVALID_PARAMETER HostBridge object and/or non-optional + output parameters are invalid + @retval EFI_INCOMPATIBLE_VERSION HostBridge information provided belong= s to + and unsupported version + @retval EFI_WARN_STALE_DATA One or more apertures having valid ran= ges + in the HostBridge info were ignored be= cause + the correspnding aperture pointer is N= ULL +**/ +EFI_STATUS +HardwareInfoPciHostBridgeGet ( + IN CONST HOST_BRIDGE_INFO *HostBridge, + IN UINTN DataSize, + OUT UINTN *BusNrStart, + OUT UINTN *BusNrLast, + OUT UINT64 *Attributes OPTIONAL, + OUT BOOLEAN *DmaAbove4G OPTIONAL, + OUT BOOLEAN *NoExtendedConfigSpace OPTIONAL, + OUT BOOLEAN *CombineMemPMem OPTIONAL, + OUT PCI_ROOT_BRIDGE_APERTURE *Io OPTIONAL, + OUT PCI_ROOT_BRIDGE_APERTURE *Mem OPTIONAL, + OUT PCI_ROOT_BRIDGE_APERTURE *MemAbove4G OPTIONAL, + OUT PCI_ROOT_BRIDGE_APERTURE *PMem OPTIONAL, + OUT PCI_ROOT_BRIDGE_APERTURE *PMemAbove4G OPTIONAL, + OUT PCI_ROOT_BRIDGE_APERTURE *PcieConfig OPTIONAL + ); + +#endif // __HARDWARE_INFO_PCI_HOST_BRIDGE_LIB_H__ diff --git a/OvmfPkg/Library/HardwareInfoLib/HardwareInfoTypesLib.h b/OvmfP= kg/Library/HardwareInfoLib/HardwareInfoTypesLib.h new file mode 100644 index 0000000000..8508e42509 --- /dev/null +++ b/OvmfPkg/Library/HardwareInfoLib/HardwareInfoTypesLib.h @@ -0,0 +1,59 @@ +/**@file + Hardware info types' definitions. + General hardware info types to parse the binary data + + Copyright 2021 - 2022 Amazon.com, Inc. or its affiliates. All Rights Res= erved. + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __HARDWARE_INFO_TYPES_LIB_H__ +#define __HARDWARE_INFO_TYPES_LIB_H__ + +// Specific hardware types: +#include "HardwareInfoPciHostBridgeLib.h" + +// +// Hardware info types enumeration listing the supported +// types which have an associated type definition +// +typedef enum { + HardwareInfoTypeUndefined =3D 0, + HardwareInfoTypeHostBridge =3D 1, + + HardwareInfoTypeMax +} HARDWARE_INFO_TYPE; + +// +// Header format preceding and describing an associated hardware +// info element +// +#pragma pack(1) +typedef struct { + union { + UINT64 Uint64; + HARDWARE_INFO_TYPE Value; + } Type; + UINT64 Size; +} HARDWARE_INFO_HEADER; +#pragma pack() + +// +// Generic data structure to access any supported hardware type +// resource definition +// +#pragma pack(1) +typedef struct { + LIST_ENTRY Link; + HARDWARE_INFO_HEADER Header; + union { + UINT8 *Raw; + HOST_BRIDGE_INFO *PciHostBridge; + } Data; +} HARDWARE_INFO; +#pragma pack() + +#define HARDWARE_INFO_FROM_LINK(a) \ + BASE_CR (a, HARDWARE_INFO, Link) + +#endif // __HARDWARE_INFO_TYPES_LIB_H__ --=20 2.17.1 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#90657): https://edk2.groups.io/g/devel/message/90657 Mute This Topic: https://groups.io/mt/91909794/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Fri May 3 16:11:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+90658+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+90658+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1655847339; cv=none; d=zohomail.com; s=zohoarc; b=FnjgfTEVYePqB/XfvHjZUfGfCmxCKxl9t3kNMydgbhOq3N8zB3ve5NPJpf3QII+V+ElgtabpnSMweYvhoC9T4DlCQKodvw+TPW7Av4mHD7qWi7AQoO08umScxan7uAgNOJIDXGQPFMwau40SaCV24gwOQjZM1eGsuccgGkGBERI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1655847339; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=Cw3u0EPzjaqwVhhXGyUTAidvr9DSEc2FGGssn0RZd58=; b=W466vl3ZHIkoR8tISsvEfqQTB6T/wx1GqOtBmwq4tDO89O4G2tTfn0p97OihtM1hhyFpC5ryf+3+yJYlGtqnVi4JKDg/Y5XiVBlhx0P8w6N53GFHe6lUtJEQ0ecc4SHIdBJCKDroRUgt1fmMw2neEbnVTxsyU7RgrXTP6ZGqQPw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+90658+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 16558473397421004.8735924043917; Tue, 21 Jun 2022 14:35:39 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id sswcYY1788612xcRSy53HtKi; Tue, 21 Jun 2022 14:35:39 -0700 X-Received: from smtp-fw-2101.amazon.com (smtp-fw-2101.amazon.com [72.21.196.25]) by mx.groups.io with SMTP id smtpd.web12.79.1655847338550154052 for ; Tue, 21 Jun 2022 14:35:38 -0700 X-IronPort-AV: E=Sophos;i="5.92,210,1650931200"; d="scan'208";a="210412850" X-Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-pdx-2b-31df91b1.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-2101.iad2.amazon.com with ESMTP; 21 Jun 2022 21:35:24 +0000 X-Received: from EX13D49EUC003.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan2.pdx.amazon.com [10.236.137.194]) by email-inbound-relay-pdx-2b-31df91b1.us-west-2.amazon.com (Postfix) with ESMTPS id E28DE43624; Tue, 21 Jun 2022 21:35:22 +0000 (UTC) X-Received: from ub4014a598e6c52.ant.amazon.com (10.43.162.140) by EX13D49EUC003.ant.amazon.com (10.43.164.91) with Microsoft SMTP Server (TLS) id 15.0.1497.36; Tue, 21 Jun 2022 21:35:15 +0000 From: "Ojeda Leon, Nicolas via groups.io" To: CC: , , , , , , , , , , , , , , , Nicolas Ojeda Leon , Alexander Graf , Gerd Hoffmann Subject: [edk2-devel] [PATCH v6 2/5] Ovmf/HardwareInfoLib: Create Pei lib to parse directly from fw-cfg Date: Tue, 21 Jun 2022 23:34:32 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.43.162.140] X-ClientProxiedBy: EX13D44UWC002.ant.amazon.com (10.43.162.169) To EX13D49EUC003.ant.amazon.com (10.43.164.91) Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ncoleon@amazon.com X-Gm-Message-State: q2PTV5fPKTpntJP4W8vVqSdpx1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1655847339; bh=J9U0q9f5gPCa4+HTarhRNGXjPTsQPYoZKXTyN5fUjaI=; h=CC:Content-Type:Date:From:Reply-To:Subject:To; b=lhLv2d80OhHqCobVfdS0bj1vzAqLsMhs9YlpF/FWtY618jS/akJ8bKT90z6kotCe676 prCoxtjEoVF3xj2Rad3NbOjQux05hRKa+x2P7dqohVku5NQIem0aN/QOiQLny6IBps1Gw NuOgm1FyJj58QOWIbsUb663JYVDZ4TRWHSo= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1655847341304100001 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Define the HardwareInfoLib API and create the PeiHardwareInfoLib which implements it, specifically for Pei usage, supporting only static accesses to parse data directly from a fw-cfg file. All list-like APIs are implemented as unsupported and only a fw-cfg wrapper to read hardware info elements is provided. The Hardware Info library is intended to describe non-discoverable hardware information and share that from the host to the guest in Ovmf platforms. The QEMU fw-cfg extension for this library provides a first variation to parse hardware info by reading it directly from a fw-cfg file. This library offers a wrapper function to the plain QmeuFwCfgReadBytes which, specifically, parses header-data pairs out of the binary values in the file. For this purpose, the approach is incremental, reading the file block by block and outputting the values only for a specific known hardware type (e.g. PCI host bridges). One element is returned in each call until the end of the file is reached. Considering fw-cfg as the first means to transport hardware info from the host to the guest, this wrapping library offers the possibility to statically, and in steps, read a specific type of hardware info elements out of the file. This method reads one hardware element of a specific type at a time, without the need to pre-allocate memory and read the whole file or dynamically allocate memory for each new element found. As a usage example, the static approach followed by this library enables early UEFI stages to use and read hardware information supplied by the host. For instance, in early times of the PEI stage, hardware information can be parsed out from a fw-cfg file prescinding from memory services, that may not yet be available, and avoiding dynamic memory allocations. Cc: Alexander Graf Cc: Gerd Hoffmann Acked-by: Gerd Hoffmann Signed-off-by: Nicolas Ojeda Leon --- Notes: v6: Preparation for merge: - Updated files' copyright to follow EDK2 format - Explicitly cast header size to UINTN when reading hardware information from a fw-cfg file. - Before casting verify validity of values to avoid overflows as well as check ReadIndex is inside bounds. - Add HardwareInfoLib library class definition of to OvmfPkg.dec - Include PeiHardwareInfoLib as part of library classes needed by several platforms which indirectly depend on it (fix CI build for all platforms= ). --- ArmVirtPkg/ArmVirtQemu.dsc | 1 + OvmfPkg/AmdSev/AmdSevX64.dsc | 1 + OvmfPkg/Bhyve/BhyveX64.dsc | 1 + OvmfPkg/CloudHv/CloudHvX64.dsc | 1 + OvmfPkg/Include/Library/HardwareInfoLib.h | 159 ++++++++++++++++++ .../Library/HardwareInfoLib/HardwareInfoPei.c | 79 +++++++++ .../HardwareInfoLib/PeiHardwareInfoLib.inf | 39 +++++ .../QemuFwCfgHardwareInfoLib.c | 88 ++++++++++ OvmfPkg/Microvm/MicrovmX64.dsc | 1 + OvmfPkg/OvmfPkg.dec | 4 + OvmfPkg/OvmfPkgIa32.dsc | 1 + OvmfPkg/OvmfPkgIa32X64.dsc | 1 + OvmfPkg/OvmfPkgX64.dsc | 1 + OvmfPkg/OvmfXen.dsc | 1 + 14 files changed, 378 insertions(+) create mode 100644 OvmfPkg/Include/Library/HardwareInfoLib.h create mode 100644 OvmfPkg/Library/HardwareInfoLib/HardwareInfoPei.c create mode 100644 OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf create mode 100644 OvmfPkg/Library/HardwareInfoLib/QemuFwCfgHardwareInfoLi= b.c diff --git a/ArmVirtPkg/ArmVirtQemu.dsc b/ArmVirtPkg/ArmVirtQemu.dsc index aa0ce61630..9369a88858 100644 --- a/ArmVirtPkg/ArmVirtQemu.dsc +++ b/ArmVirtPkg/ArmVirtQemu.dsc @@ -81,6 +81,7 @@ PciSegmentLib|MdePkg/Library/BasePciSegmentLibPci/BasePciSegmentLibPci.i= nf PciHostBridgeLib|OvmfPkg/Fdt/FdtPciHostBridgeLib/FdtPciHostBridgeLib.inf PciHostBridgeUtilityLib|OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostB= ridgeUtilityLib.inf + PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf =20 !if $(TPM2_ENABLE) =3D=3D TRUE Tpm2CommandLib|SecurityPkg/Library/Tpm2CommandLib/Tpm2CommandLib.inf diff --git a/OvmfPkg/AmdSev/AmdSevX64.dsc b/OvmfPkg/AmdSev/AmdSevX64.dsc index 6b3827f7f6..72cbf710df 100644 --- a/OvmfPkg/AmdSev/AmdSevX64.dsc +++ b/OvmfPkg/AmdSev/AmdSevX64.dsc @@ -168,6 +168,7 @@ FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltL= ib.inf BlobVerifierLib|OvmfPkg/AmdSev/BlobVerifierLibSevHashes/BlobVerifierLibS= evHashes.inf MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ib.inf + PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf =20 !if $(SOURCE_DEBUG_ENABLE) =3D=3D TRUE PeCoffExtraActionLib|SourceLevelDebugPkg/Library/PeCoffExtraActionLibDeb= ug/PeCoffExtraActionLibDebug.inf diff --git a/OvmfPkg/Bhyve/BhyveX64.dsc b/OvmfPkg/Bhyve/BhyveX64.dsc index f0166e136c..ada904464e 100644 --- a/OvmfPkg/Bhyve/BhyveX64.dsc +++ b/OvmfPkg/Bhyve/BhyveX64.dsc @@ -170,6 +170,7 @@ MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLi= b.inf LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ib.inf + PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf =20 CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/Customize= dDisplayLib.inf FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltL= ib.inf diff --git a/OvmfPkg/CloudHv/CloudHvX64.dsc b/OvmfPkg/CloudHv/CloudHvX64.dsc index 8a111444f8..89d856e488 100644 --- a/OvmfPkg/CloudHv/CloudHvX64.dsc +++ b/OvmfPkg/CloudHv/CloudHvX64.dsc @@ -180,6 +180,7 @@ VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLi= b.inf + PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif diff --git a/OvmfPkg/Include/Library/HardwareInfoLib.h b/OvmfPkg/Include/Li= brary/HardwareInfoLib.h new file mode 100644 index 0000000000..66d964dab5 --- /dev/null +++ b/OvmfPkg/Include/Library/HardwareInfoLib.h @@ -0,0 +1,159 @@ +/*/@file + Hardware info parsing functions. + Binary data is expected as a consecutive series of header - object pairs. + Complete library providing static Qemu fw-cfg wrappers as well as list-l= ike + interface to dynamically manipulate hardware info objects and parsing fr= om + a generic blob. + + Copyright 2021 - 2022 Amazon.com, Inc. or its affiliates. All Rights Res= erved. + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __HARDWARE_INFO_LIB_H__ +#define __HARDWARE_INFO_LIB_H__ + +#include "../Library/HardwareInfoLib/HardwareInfoTypesLib.h" + +/** + Read, if available, the next Type element in the FwCfg file. + The FwCfg item must already be selected, this is a wrapper around + QemuFwCfgReadBytes and the Data pointer should be set to an existent + memory location with TypeSize bytes allocated for the date to be + properly written. If a Type element is found in the file which has a + size (in the header) greater than TypeSize, it is skipped. + + @param[in] Type Hardware Info Type to search for + @param[in] TypeSize Size (in bytes) of the structure intended= to + be used to dereference the data + @param[in] TotalFileSize Total size (in bytes) of the FwCfg file f= rom + which the data is read. + @param[out] Data Pointer to a memory allocated instance in= to + which the data is written to. + @param[out] DataSize Size in bytes of the actually filled + data available in the Data object after a + successful operation + @param[inout] ReadIndex Index of the next byte to be read. Increm= ented + accordingly after a read operation to ref= lect + up to date status + + @retval EFI_SUCCESS Next element found and read into Data + @retval EFI_INVALID_PARAMETER Operation failed + @retval EFI_END_OF_FILE End of the file reached, no more elements + to read. +**/ +EFI_STATUS +QemuFwCfgReadNextHardwareInfoByType ( + IN HARDWARE_INFO_TYPE Type, + IN UINTN TypeSize, + IN UINTN TotalFileSize, + OUT VOID *Data, + OUT UINTN *DataSize OPTIONAL, + IN OUT UINTN *ReadIndex + ); + +/** + Parse binary data containing resource information of multiple hardware + elements into a list of interpreted resources. + The translation is done on a copy-parse base so the blob can be freed + afterwards. + + @param[in] Blob Binary data to be parsed + @param[in] BlobSize Size (in bytes) of the binary data + @param[in] TypeFilter Optional type to filter entries. Set to + undefined to disable filtering and retrieve a= ll + @param[out] ListHead Head of the list to populate hardware informa= tion + + @retval EFI_SUCCESS Succeed. + @retval EFI_INVALID_PARAMETER Provided Blob inforation is invalid + @retval EFI_OUT_OF_RESOURCES Out of memory, list populated as far as + possible +**/ +EFI_STATUS +CreateHardwareInfoList ( + IN UINT8 *Blob, + IN UINTN BlobSize, + IN HARDWARE_INFO_TYPE TypeFilter, + OUT LIST_ENTRY *ListHead + ); + +/** + Free the dynamically allocated list of HADWARE_INFO items populated + during parsing of Blob + + @param ListHead Head of the list to be destroyed +**/ +VOID +FreeHardwareInfoList ( + IN OUT LIST_ENTRY *ListHead + ); + +/** + Retrieve the number of hardware components of a specific type + in the list. + + @param[in] ListHead Head of the hardware info list + @param[in] Type Type of hardware elements to count + @param[in] TypeSize Size (in bytes) of the structure intended to + be used to dereference the data + @return Count of elements of Type found +**/ +UINTN +GetHardwareInfoCountByType ( + IN LIST_ENTRY *ListHead, + IN HARDWARE_INFO_TYPE Type, + IN UINTN TypeSize + ); + +/** + Get the First Hardware Info entry in the list of the specified type + + @param[in] ListHead Head of the hardware info list + @param[in] Type Hardware Info Type to search for + @param[in] TypeSize Size (in bytes) of the structure intended to + be used to dereference the data + @return Link of first entry of specified type or list head if not found +**/ +LIST_ENTRY * +GetFirstHardwareInfoByType ( + IN LIST_ENTRY *ListHead, + IN HARDWARE_INFO_TYPE Type, + IN UINTN TypeSize + ); + +/** + Get the Next Hardware Info entry in the list with the specified + type, which follows the provided Node. + + @param[in] ListHead Head of the hardware info list + @param[in] Node Current, already processed, node's link + @param[in] Type Hardware Info Type to search for + @param[in] TypeSize Size (in bytes) of the structure intended to + be used to dereference the data + @return Link of next entry, after Node, of the specified type. + List head otherwise +**/ +LIST_ENTRY * +GetNextHardwareInfoByType ( + IN LIST_ENTRY *ListHead, + IN LIST_ENTRY *Node, + IN HARDWARE_INFO_TYPE Type, + IN UINTN TypeSize + ); + +/** + Assess if Node stands at the end of the doubly linked list + + @param[in] ListHead Head of the hardware info list + @param[in] Node Current Node link + + @retval TRUE Node is at the end of the list + @retval FALSE Node is not at the end of the list +**/ +BOOLEAN +EndOfHardwareInfoList ( + IN LIST_ENTRY *ListHead, + IN LIST_ENTRY *Node + ); + +#endif // __HARDWARE_INFO_LIB_H__ diff --git a/OvmfPkg/Library/HardwareInfoLib/HardwareInfoPei.c b/OvmfPkg/Li= brary/HardwareInfoLib/HardwareInfoPei.c new file mode 100644 index 0000000000..beb10ba896 --- /dev/null +++ b/OvmfPkg/Library/HardwareInfoLib/HardwareInfoPei.c @@ -0,0 +1,79 @@ +/*/@file + Hardware info parsing functions. + Binary data is expected as a consecutive series of header - object pairs. + Provides static Qemu fw-cfg wrappers as well as list-like interface to + dynamically manipulate hardware info objects and parsing from a generic + blob. + + Copyright 2021 - 2022 Amazon.com, Inc. or its affiliates. All Rights Res= erved. + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +#include + +EFI_STATUS +CreateHardwareInfoList ( + IN UINT8 *Blob, + IN UINTN BlobSize, + IN HARDWARE_INFO_TYPE TypeFilter, + OUT LIST_ENTRY *ListHead + ) +{ + ASSERT (FALSE); + return EFI_UNSUPPORTED; +} + +VOID +FreeHardwareInfoList ( + IN OUT LIST_ENTRY *ListHead + ) +{ + ASSERT (FALSE); +} + +UINTN +GetHardwareInfoCountByType ( + IN LIST_ENTRY *ListHead, + IN HARDWARE_INFO_TYPE Type, + IN UINTN TypeSize + ) +{ + ASSERT (FALSE); + return 0; +} + +LIST_ENTRY * +GetFirstHardwareInfoByType ( + IN LIST_ENTRY *ListHead, + IN HARDWARE_INFO_TYPE Type, + IN UINTN TypeSize + ) +{ + ASSERT (FALSE); + return ListHead; +} + +LIST_ENTRY * +GetNextHardwareInfoByType ( + IN LIST_ENTRY *ListHead, + IN LIST_ENTRY *Node, + IN HARDWARE_INFO_TYPE Type, + IN UINTN TypeSize + ) +{ + ASSERT (FALSE); + return ListHead; +} + +BOOLEAN +EndOfHardwareInfoList ( + IN LIST_ENTRY *ListHead, + IN LIST_ENTRY *Node + ) +{ + ASSERT (FALSE); + return TRUE; +} diff --git a/OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf b/OvmfP= kg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf new file mode 100644 index 0000000000..00b79bc8d6 --- /dev/null +++ b/OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf @@ -0,0 +1,39 @@ +## @file +# Hardware information library wrappers over Qemu fw-cfg functionality to= parse, +# in a static manner, non-discoverable hardware information. +# +# Copyright 2021 - 2022 Amazon.com, Inc. or its affiliates. All Rights Re= served. +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D PeiHardwareInfoLib + FILE_GUID =3D 3D5011B3-9CBB-4C0B-88E8-1D758283C659 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D PeiHardwareInfoLib + +# +# The following information is for reference only and not required by the = build +# tools. +# +# VALID_ARCHITECTURES =3D X64 +# + +[Sources] + HardwareInfoPei.c + QemuFwCfgHardwareInfoLib.c + HardwareInfoPciHostBridgeLib.c + +[Packages] + MdeModulePkg/MdeModulePkg.dec + MdePkg/MdePkg.dec + OvmfPkg/OvmfPkg.dec + +[LibraryClasses] + DebugLib + QemuFwCfgLib diff --git a/OvmfPkg/Library/HardwareInfoLib/QemuFwCfgHardwareInfoLib.c b/O= vmfPkg/Library/HardwareInfoLib/QemuFwCfgHardwareInfoLib.c new file mode 100644 index 0000000000..48d9dcd8ad --- /dev/null +++ b/OvmfPkg/Library/HardwareInfoLib/QemuFwCfgHardwareInfoLib.c @@ -0,0 +1,88 @@ +/*/@file + Qemu fw-cfg wrappers for hardware info parsing. + Provides an alternative to parse hardware information from a fw-cfg + file without relying on dynamic memory allocations. + + Copyright 2021 - 2022 Amazon.com, Inc. or its affiliates. All Rights Res= erved. + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include + +#include + +/** + Update an optional pointer value if possible + + @param[out] DataSize Pointer to variable to be updated + @param[in] Value Value to set the pointed variable to. +**/ +STATIC +VOID +UpdateDataSize ( + OUT UINTN *DataSize, + IN UINTN Value + ) +{ + if (DataSize =3D=3D NULL) { + return; + } + + *DataSize =3D Value; +} + +EFI_STATUS +QemuFwCfgReadNextHardwareInfoByType ( + IN HARDWARE_INFO_TYPE Type, + IN UINTN TypeSize, + IN UINTN TotalFileSize, + OUT VOID *Data, + OUT UINTN *DataSize OPTIONAL, + IN OUT UINTN *ReadIndex + ) +{ + HARDWARE_INFO_HEADER Header; + + if ((Data =3D=3D NULL) || + (ReadIndex =3D=3D NULL) || + (TypeSize =3D=3D 0) || + (Type =3D=3D HardwareInfoTypeUndefined) || + (TotalFileSize =3D=3D 0)) + { + return EFI_INVALID_PARAMETER; + } + + UpdateDataSize (DataSize, 0); + + while (*ReadIndex < TotalFileSize) { + QemuFwCfgReadBytes (sizeof (Header), &Header); + *ReadIndex +=3D sizeof (Header); + + if ((Header.Size > MAX_UINTN) || (((UINT64)*ReadIndex + Header.Size) >= TotalFileSize)) { + *ReadIndex =3D TotalFileSize; + return EFI_ABORTED; + } + + if ((Header.Type.Value =3D=3D Type) && (Header.Size <=3D TypeSize)) { + QemuFwCfgReadBytes ((UINTN)Header.Size, Data); + + *ReadIndex +=3D (UINTN)Header.Size; + UpdateDataSize (DataSize, (UINTN)Header.Size); + + return EFI_SUCCESS; + } + + // + // Skip the bytes corresponding to the next element as it is + // not of the expected type and/or size. The TotalFileSize + // and individual elements sizes should match so the size + // check is skipped. + // + QemuFwCfgSkipBytes ((UINTN)Header.Size); + *ReadIndex +=3D (UINTN)Header.Size; + } + + return EFI_END_OF_FILE; +} diff --git a/OvmfPkg/Microvm/MicrovmX64.dsc b/OvmfPkg/Microvm/MicrovmX64.dsc index 5b150a959c..38dd708477 100644 --- a/OvmfPkg/Microvm/MicrovmX64.dsc +++ b/OvmfPkg/Microvm/MicrovmX64.dsc @@ -183,6 +183,7 @@ CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/Customize= dDisplayLib.inf FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltL= ib.inf MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ib.inf + PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf =20 !if $(SOURCE_DEBUG_ENABLE) =3D=3D TRUE PeCoffExtraActionLib|SourceLevelDebugPkg/Library/PeCoffExtraActionLibDeb= ug/PeCoffExtraActionLibDebug.inf diff --git a/OvmfPkg/OvmfPkg.dec b/OvmfPkg/OvmfPkg.dec index 7b114a5e63..5af76a5405 100644 --- a/OvmfPkg/OvmfPkg.dec +++ b/OvmfPkg/OvmfPkg.dec @@ -125,6 +125,10 @@ # PeilessStartupLib|Include/Library/PeilessStartupLib.h =20 + ## @libraryclass HardwareInfoLib + # + HardwareInfoLib|Include/Library/HardwareInfoLib.h + [Guids] gUefiOvmfPkgTokenSpaceGuid =3D {0x93bb96af, 0xb9f2, 0x4eb8, {= 0x94, 0x62, 0xe0, 0xba, 0x74, 0x56, 0x42, 0x36}} gEfiXenInfoGuid =3D {0xd3b46f3b, 0xd441, 0x1244, {= 0x9a, 0x12, 0x0, 0x12, 0x27, 0x3f, 0xc1, 0x4d}} diff --git a/OvmfPkg/OvmfPkgIa32.dsc b/OvmfPkg/OvmfPkgIa32.dsc index a9841cbfc3..8bffcf0bca 100644 --- a/OvmfPkg/OvmfPkgIa32.dsc +++ b/OvmfPkg/OvmfPkgIa32.dsc @@ -178,6 +178,7 @@ LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLi= b.inf MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ibNull.inf + PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc index f7949780fa..7732b95bfa 100644 --- a/OvmfPkg/OvmfPkgIa32X64.dsc +++ b/OvmfPkg/OvmfPkgIa32X64.dsc @@ -182,6 +182,7 @@ LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLi= b.inf MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ibNull.inf + PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc index 1448f925b7..459c6fbde1 100644 --- a/OvmfPkg/OvmfPkgX64.dsc +++ b/OvmfPkg/OvmfPkgX64.dsc @@ -194,6 +194,7 @@ LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLi= b.inf MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ib.inf + PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf =20 !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf diff --git a/OvmfPkg/OvmfXen.dsc b/OvmfPkg/OvmfXen.dsc index 6ba4bd729a..03922b5c16 100644 --- a/OvmfPkg/OvmfXen.dsc +++ b/OvmfPkg/OvmfXen.dsc @@ -172,6 +172,7 @@ CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/Customize= dDisplayLib.inf FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltL= ib.inf MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ib.inf + PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf =20 !if $(SOURCE_DEBUG_ENABLE) =3D=3D TRUE PeCoffExtraActionLib|SourceLevelDebugPkg/Library/PeCoffExtraActionLibDeb= ug/PeCoffExtraActionLibDebug.inf --=20 2.17.1 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#90658): https://edk2.groups.io/g/devel/message/90658 Mute This Topic: https://groups.io/mt/91909863/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Fri May 3 16:11:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+90659+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+90659+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1655847510; cv=none; d=zohomail.com; s=zohoarc; b=HcwKyDhEdnQ9/2zaVqdwZm52Y4Ev/RM/CTpHs9L6THJ7WSBBlAqzux6rIlEJ1uqPT4C9yXKigRg0Zw/437Ssn215mbag03wG7gIxNZ6peLAviglIXsJFUfb1WhAq1V1bP9hjcC9j3K4N0v6dtNfu0fJa7tZhVmFW/4jXG1RXI5k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1655847510; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=Hl9H8+NKMziczhPFgR/SFf9ADtBDzNMg6iTjcvdP5hI=; b=Ky59KcITibLd4TH4V/Helw5Tbb8I8edSDuBoENiGOoWzhb7HoRtWM6qiVezCrf8Ih+n0hZnts+ZU5ILN2BoAA9QjEU4w+sTISxqmYz1a2aJMKKagSO1cJ/i+yKoZbZQriMXqvE32WvTWR4AqUoPRb4YWQtNtABE13RI4hUr8Glk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+90659+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1655847510416995.5534655472728; Tue, 21 Jun 2022 14:38:30 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id XDaZYY1788612xs5T6lburVY; Tue, 21 Jun 2022 14:38:30 -0700 X-Received: from smtp-fw-6001.amazon.com (smtp-fw-6001.amazon.com [52.95.48.154]) by mx.groups.io with SMTP id smtpd.web08.112.1655847509069782797 for ; Tue, 21 Jun 2022 14:38:29 -0700 X-Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-pdx-2a-1801e169.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-6001.iad6.amazon.com with ESMTP; 21 Jun 2022 21:38:15 +0000 X-Received: from EX13D49EUC003.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan3.pdx.amazon.com [10.236.137.198]) by email-inbound-relay-pdx-2a-1801e169.us-west-2.amazon.com (Postfix) with ESMTPS id 12DAAC09A5; Tue, 21 Jun 2022 21:38:14 +0000 (UTC) X-Received: from ub4014a598e6c52.ant.amazon.com (10.43.160.124) by EX13D49EUC003.ant.amazon.com (10.43.164.91) with Microsoft SMTP Server (TLS) id 15.0.1497.36; Tue, 21 Jun 2022 21:38:06 +0000 From: "Ojeda Leon, Nicolas via groups.io" To: CC: , , , , , , , , , , , , , , , Nicolas Ojeda Leon , Alexander Graf , Gerd Hoffmann Subject: [edk2-devel] [PATCH v6 3/5] Ovmf/HardwareInfoLib: Add Dxe lib to dynamically parse heterogenous data Date: Tue, 21 Jun 2022 23:37:27 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.43.160.124] X-ClientProxiedBy: EX13D47UWA003.ant.amazon.com (10.43.163.9) To EX13D49EUC003.ant.amazon.com (10.43.164.91) Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ncoleon@amazon.com X-Gm-Message-State: XRRRPIdc0i1a9XkWzTlQS8SZx1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1655847510; bh=t2FnwfEu88/2NY9CMT0Z8dNxpqPA9gDU544ou08gUb8=; h=CC:Content-Type:Date:From:Reply-To:Subject:To; b=bP6zKrRFI1MbSPbaCffWG8TlCO4iGhyJ5095JchkfQab80h+lQIzjixYMlNTc7/Saea 1kJ9bBjJRKh7bLcTAG8YS/wV7ROyWJcFVyxRPn5h6oShOagXnbDLDlpKGBCm0lwDlsY9z y1XPY99ocIiIiBMgMIzStuxch+XJBonPtjA= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1655847512442100001 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Following the Hardware Info library, create the DxeHardwareInfoLib which implements the whole API capable of parsing heterogeneous hardware information. The list-like API grants callers a flexible and common pattern to retrieve the data. Moreover, the initial source is a BLOB which generalizes the host-to-guest transmission mechanism. The Hardware Info library main objective is to provide a way to describe non-discoverable hardware so that the host can share the available resources with the guest in Ovmf platforms. This change features and embraces the main idea behind the library by providing an API that parses a BLOB into a linked list to retrieve hardware data from any source. Additionally, list-like APIs are provided so that the hardware info list can be traversed conveniently. Similarly, the capability is provided to filter results by specific hardware types. However, heterogeneous elements can be added to the list, increasing the flexibility. This way, a single source, for example a fw-cfg file, can be used to describe several instances of multiple types of hardware. This part of the Hardware Info library makes use of dynamic memory and is intended for stages in which memory services are available. A motivation example is the PciHostBridgeLib. This library, part of the PCI driver populates the list of PCI root bridges during DXE stage for future steps to discover the resources under them. The hardware info library can be used to obtain the detailed description of available host bridges, for instance in the form of a fw-cfg file, and parse that information into a dynmaic list that allows, first to verify consistency of the data, and second discover the resources availabe for each root bridge. Cc: Alexander Graf Cc: Gerd Hoffmann Acked-by: Gerd Hoffmann Signed-off-by: Nicolas Ojeda Leon --- Notes: v6: Preparation for merge: - Updated files' copyright to follow EDK2 format - Explicitly cast header size to UINTN when creating hardware info list from a blob acquired received from host. - Before casting verify validity of values to avoid overflows as well as check ReadIndex is inside blob boundaries. - Include DxeHardwareInfoLib as part of library classes needed by several platforms which indirectly depend on it (fix CI build for all platforms= ). --- ArmVirtPkg/ArmVirtQemu.dsc | 1 + OvmfPkg/AmdSev/AmdSevX64.dsc | 1 + OvmfPkg/Bhyve/BhyveX64.dsc | 1 + OvmfPkg/CloudHv/CloudHvX64.dsc | 1 + .../HardwareInfoLib/DxeHardwareInfoLib.inf | 39 +++ .../Library/HardwareInfoLib/HardwareInfoDxe.c | 254 ++++++++++++++++++ OvmfPkg/Microvm/MicrovmX64.dsc | 1 + OvmfPkg/OvmfPkgIa32.dsc | 1 + OvmfPkg/OvmfPkgIa32X64.dsc | 1 + OvmfPkg/OvmfPkgX64.dsc | 1 + OvmfPkg/OvmfXen.dsc | 1 + 11 files changed, 302 insertions(+) create mode 100644 OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf create mode 100644 OvmfPkg/Library/HardwareInfoLib/HardwareInfoDxe.c diff --git a/ArmVirtPkg/ArmVirtQemu.dsc b/ArmVirtPkg/ArmVirtQemu.dsc index 9369a88858..56eb53911d 100644 --- a/ArmVirtPkg/ArmVirtQemu.dsc +++ b/ArmVirtPkg/ArmVirtQemu.dsc @@ -82,6 +82,7 @@ PciHostBridgeLib|OvmfPkg/Fdt/FdtPciHostBridgeLib/FdtPciHostBridgeLib.inf PciHostBridgeUtilityLib|OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostB= ridgeUtilityLib.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf + DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf =20 !if $(TPM2_ENABLE) =3D=3D TRUE Tpm2CommandLib|SecurityPkg/Library/Tpm2CommandLib/Tpm2CommandLib.inf diff --git a/OvmfPkg/AmdSev/AmdSevX64.dsc b/OvmfPkg/AmdSev/AmdSevX64.dsc index 72cbf710df..b265697704 100644 --- a/OvmfPkg/AmdSev/AmdSevX64.dsc +++ b/OvmfPkg/AmdSev/AmdSevX64.dsc @@ -169,6 +169,7 @@ BlobVerifierLib|OvmfPkg/AmdSev/BlobVerifierLibSevHashes/BlobVerifierLibS= evHashes.inf MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ib.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf + DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf =20 !if $(SOURCE_DEBUG_ENABLE) =3D=3D TRUE PeCoffExtraActionLib|SourceLevelDebugPkg/Library/PeCoffExtraActionLibDeb= ug/PeCoffExtraActionLibDebug.inf diff --git a/OvmfPkg/Bhyve/BhyveX64.dsc b/OvmfPkg/Bhyve/BhyveX64.dsc index ada904464e..ec8ad98db7 100644 --- a/OvmfPkg/Bhyve/BhyveX64.dsc +++ b/OvmfPkg/Bhyve/BhyveX64.dsc @@ -171,6 +171,7 @@ LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ib.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf + DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf =20 CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/Customize= dDisplayLib.inf FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltL= ib.inf diff --git a/OvmfPkg/CloudHv/CloudHvX64.dsc b/OvmfPkg/CloudHv/CloudHvX64.dsc index 89d856e488..294a625ea8 100644 --- a/OvmfPkg/CloudHv/CloudHvX64.dsc +++ b/OvmfPkg/CloudHv/CloudHvX64.dsc @@ -181,6 +181,7 @@ LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLi= b.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf + DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif diff --git a/OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf b/OvmfP= kg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf new file mode 100644 index 0000000000..a2f056e117 --- /dev/null +++ b/OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf @@ -0,0 +1,39 @@ +## @file +# Hardware information library to describe non-discoverable hardware reso= urces +# +# Copyright 2021 - 2022 Amazon.com, Inc. or its affiliates. All Rights Re= served. +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D DxeHardwareInfoLib + FILE_GUID =3D F60B206A-5C56-11EC-AEAC-67CB080BCFF2 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D DxeHardwareInfoLib + +# +# The following information is for reference only and not required by the = build +# tools. +# +# VALID_ARCHITECTURES =3D X64 +# + +[Sources] + HardwareInfoDxe.c + HardwareInfoPciHostBridgeLib.c + QemuFwCfgHardwareInfoLib.c + +[Packages] + MdeModulePkg/MdeModulePkg.dec + MdePkg/MdePkg.dec + OvmfPkg/OvmfPkg.dec + +[LibraryClasses] + BaseMemoryLib + DebugLib + MemoryAllocationLib diff --git a/OvmfPkg/Library/HardwareInfoLib/HardwareInfoDxe.c b/OvmfPkg/Li= brary/HardwareInfoLib/HardwareInfoDxe.c new file mode 100644 index 0000000000..a74de52871 --- /dev/null +++ b/OvmfPkg/Library/HardwareInfoLib/HardwareInfoDxe.c @@ -0,0 +1,254 @@ +/*/@file + Hardware info parsing functions. + Binary data is expected as a consecutive series of header - object pairs. + Complete library providing list-like interface to dynamically manipulate + hardware info objects and parsing from a generic blob. + + Copyright 2021 - 2022 Amazon.com, Inc. or its affiliates. All Rights Res= erved. + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include +#include + +#include + +EFI_STATUS +CreateHardwareInfoList ( + IN UINT8 *Blob, + IN UINTN BlobSize, + IN HARDWARE_INFO_TYPE TypeFilter, + OUT LIST_ENTRY *ListHead + ) +{ + UINT8 *Index; + UINT8 *BlobEnd; + HARDWARE_INFO *HwComponent; + + if ((Blob =3D=3D NULL) || (BlobSize <=3D 0) || + (ListHead =3D=3D NULL)) + { + return EFI_INVALID_PARAMETER; + } + + Index =3D Blob; + BlobEnd =3D Blob + BlobSize; + while (Index < BlobEnd) { + HwComponent =3D AllocateZeroPool (sizeof (HARDWARE_INFO)); + + if (HwComponent =3D=3D NULL) { + goto FailedAllocate; + } + + HwComponent->Header.Type.Uint64 =3D *((UINT64 *)Index); + Index +=3D sizeof (HwComponent->Header.Type); + HwComponent->Header.Size =3D *((UINT64 *)(Index)); + Index +=3D sizeof (HwComponent->Header.Size); + + if ((HwComponent->Header.Size > MAX_UINTN) || (Index < Blob) || ((Inde= x + HwComponent->Header.Size) > BlobEnd)) { + goto FreeResources; + } + + // + // Check if optional TypeFilter is set, skip if the current + // object is of a different type and release the partially + // allocated object + // + if ((TypeFilter !=3D HardwareInfoTypeUndefined) && + (HwComponent->Header.Type.Value !=3D TypeFilter)) + { + FreePool (HwComponent); + Index +=3D HwComponent->Header.Size; + continue; + } + + HwComponent->Data.Raw =3D AllocateZeroPool ((UINTN)HwComponent->Header= .Size); + if (HwComponent->Data.Raw =3D=3D NULL) { + goto FreeResources; + } + + CopyMem (HwComponent->Data.Raw, Index, (UINTN)HwComponent->Header.Size= ); + Index +=3D HwComponent->Header.Size; + + InsertTailList (ListHead, &HwComponent->Link); + } + + return EFI_SUCCESS; + +FreeResources: + // + // Clean the resources allocated in the incomplete cycle + // + FreePool (HwComponent); + +FailedAllocate: + DEBUG (( + EFI_D_ERROR, + "%a: Failed to allocate memory for hardware info\n", + __FUNCTION__ + )); + + return EFI_OUT_OF_RESOURCES; +} + +VOID +FreeHardwareInfoList ( + IN OUT LIST_ENTRY *ListHead + ) +{ + LIST_ENTRY *CurrentLink; + HARDWARE_INFO *HwComponent; + + if (IsListEmpty (ListHead)) { + return; + } + + CurrentLink =3D ListHead->ForwardLink; + while (CurrentLink !=3D NULL && CurrentLink !=3D ListHead) { + HwComponent =3D HARDWARE_INFO_FROM_LINK (CurrentLink); + + // + // Remove item from list before invalidating the pointers + // + CurrentLink =3D RemoveEntryList (CurrentLink); + + FreePool (HwComponent->Data.Raw); + FreePool (HwComponent); + } +} + +/** + Validates if the specified Node has a valid data size and is of + specified type. + The data size can be less or equal to the provided type size to be + regarded as valid and thus accessible with the typed pointer. + + For future compatibility the size is allowed to be smaller so that + different versions interpret fields differently and, particularly, + have smaller data structures. However, it cannot be larger than the + type size to avoid accessing memory out of bounds. + + @param[in] Node Hardware Info node to be validated + @param[in] TypeSize Size (in bytes) of the data type intended to be + used to dereference the data. + @retval TRUE Node is valid and can be accessed + @retval FALSE Node is not valid +/*/ +STATIC +BOOLEAN +IsHardwareInfoNodeValidByType ( + IN LIST_ENTRY *ListHead, + IN LIST_ENTRY *Link, + IN HARDWARE_INFO_TYPE Type, + IN UINTN TypeSize + ) +{ + HARDWARE_INFO *HwComponent; + + if (IsNull (ListHead, Link)) { + return FALSE; + } + + HwComponent =3D HARDWARE_INFO_FROM_LINK (Link); + + // + // Verify if the node type is the specified one and the size of + // the data allocated to the node is greater than the size of + // the type intended to dereference it in order to avoid access + // to memory out of bondaries. + // + if ((HwComponent->Header.Type.Value =3D=3D Type) && + (HwComponent->Header.Size >=3D TypeSize)) + { + return TRUE; + } + + return FALSE; +} + +UINTN +GetHardwareInfoCountByType ( + IN LIST_ENTRY *ListHead, + IN HARDWARE_INFO_TYPE Type, + IN UINTN TypeSize + ) +{ + UINTN Count; + LIST_ENTRY *Link; + + Count =3D 0; + for (Link =3D GetFirstHardwareInfoByType (ListHead, Type, TypeSize); + !IsNull (ListHead, Link); + Link =3D GetNextHardwareInfoByType (ListHead, Link, Type, TypeSize)) + { + if (IsHardwareInfoNodeValidByType (ListHead, Link, Type, TypeSize)) { + Count++; + } + } + + return Count; +} + +LIST_ENTRY * +GetFirstHardwareInfoByType ( + IN LIST_ENTRY *ListHead, + IN HARDWARE_INFO_TYPE Type, + IN UINTN TypeSize + ) +{ + LIST_ENTRY *Link; + + if (IsListEmpty (ListHead)) { + return ListHead; + } + + Link =3D GetFirstNode (ListHead); + + if (IsHardwareInfoNodeValidByType (ListHead, Link, Type, TypeSize)) { + return Link; + } + + return GetNextHardwareInfoByType (ListHead, Link, Type, TypeSize); +} + +LIST_ENTRY * +GetNextHardwareInfoByType ( + IN LIST_ENTRY *ListHead, + IN LIST_ENTRY *Node, + IN HARDWARE_INFO_TYPE Type, + IN UINTN TypeSize + ) +{ + LIST_ENTRY *Link; + + Link =3D GetNextNode (ListHead, Node); + + while (!IsNull (ListHead, Link)) { + if (IsHardwareInfoNodeValidByType (ListHead, Link, Type, TypeSize)) { + // + // Found a node of specified type and with valid size. Break and + // return the found node. + // + break; + } + + Link =3D GetNextNode (ListHead, Link); + } + + return Link; +} + +BOOLEAN +EndOfHardwareInfoList ( + IN LIST_ENTRY *ListHead, + IN LIST_ENTRY *Node + ) +{ + return IsNull (ListHead, Node); +} diff --git a/OvmfPkg/Microvm/MicrovmX64.dsc b/OvmfPkg/Microvm/MicrovmX64.dsc index 38dd708477..61db9b6e4c 100644 --- a/OvmfPkg/Microvm/MicrovmX64.dsc +++ b/OvmfPkg/Microvm/MicrovmX64.dsc @@ -184,6 +184,7 @@ FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltL= ib.inf MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ib.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf + DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf =20 !if $(SOURCE_DEBUG_ENABLE) =3D=3D TRUE PeCoffExtraActionLib|SourceLevelDebugPkg/Library/PeCoffExtraActionLibDeb= ug/PeCoffExtraActionLibDebug.inf diff --git a/OvmfPkg/OvmfPkgIa32.dsc b/OvmfPkg/OvmfPkgIa32.dsc index 8bffcf0bca..b611bb6de1 100644 --- a/OvmfPkg/OvmfPkgIa32.dsc +++ b/OvmfPkg/OvmfPkgIa32.dsc @@ -179,6 +179,7 @@ MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLi= b.inf MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ibNull.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf + DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc index 7732b95bfa..e3bfe4324d 100644 --- a/OvmfPkg/OvmfPkgIa32X64.dsc +++ b/OvmfPkg/OvmfPkgIa32X64.dsc @@ -183,6 +183,7 @@ MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLi= b.inf MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ibNull.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf + DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc index 459c6fbde1..ba09b6c72f 100644 --- a/OvmfPkg/OvmfPkgX64.dsc +++ b/OvmfPkg/OvmfPkgX64.dsc @@ -195,6 +195,7 @@ MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLi= b.inf MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ib.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf + DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf =20 !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf diff --git a/OvmfPkg/OvmfXen.dsc b/OvmfPkg/OvmfXen.dsc index 03922b5c16..58a7c97cdd 100644 --- a/OvmfPkg/OvmfXen.dsc +++ b/OvmfPkg/OvmfXen.dsc @@ -173,6 +173,7 @@ FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltL= ib.inf MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ib.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf + DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf =20 !if $(SOURCE_DEBUG_ENABLE) =3D=3D TRUE PeCoffExtraActionLib|SourceLevelDebugPkg/Library/PeCoffExtraActionLibDeb= ug/PeCoffExtraActionLibDebug.inf --=20 2.17.1 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#90659): https://edk2.groups.io/g/devel/message/90659 Mute This Topic: https://groups.io/mt/91909924/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Fri May 3 16:11:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+90660+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+90660+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1655847574; cv=none; d=zohomail.com; s=zohoarc; b=Teb6eDXIH+CPNh2BCOF/uHo3+3DjBJyOu9tjU0pib73js8mwEDGmQL2KDIwTtuzw/w8jRz1ZoMGL5HawNTS1cXPx9Y36XTQO+EInZhxDvF4Spdrucyk3YzDIBYKSstwAoLpbplpY3ib9kyy+jG11fYwHrP09J7fpHkzIUO/C8M8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1655847574; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=lUpm7JpyPmAJBDCj/ekA0MDlsgiE93RKhm2xk/f6yoo=; b=JuNLfLnRNunOdj32M3jwcU7lUQ05sB0EKgHJEFcm8XJEtJITSDUnO+VVrmuQF5oEvrugeJ1jxOmCH3uUo8oxpXEVf1VN6KK2sJu6cYXoI/JIQocLczdjOAbV3ceAr09jfaLU7hm2UKBBjGVQDQNAf5LCpgKrw6qST3apKoxAfY4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+90660+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1655847574318116.04106088275398; Tue, 21 Jun 2022 14:39:34 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id MpzqYY1788612xJtRtL7le5B; Tue, 21 Jun 2022 14:39:34 -0700 X-Received: from smtp-fw-2101.amazon.com (smtp-fw-2101.amazon.com [72.21.196.25]) by mx.groups.io with SMTP id smtpd.web12.157.1655847573237046174 for ; Tue, 21 Jun 2022 14:39:33 -0700 X-IronPort-AV: E=Sophos;i="5.92,210,1650931200"; d="scan'208";a="210414027" X-Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-pdx-2c-4213ea4c.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-2101.iad2.amazon.com with ESMTP; 21 Jun 2022 21:39:32 +0000 X-Received: from EX13D49EUC003.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan3.pdx.amazon.com [10.236.137.198]) by email-inbound-relay-pdx-2c-4213ea4c.us-west-2.amazon.com (Postfix) with ESMTPS id 0431E80F9C; Tue, 21 Jun 2022 21:39:30 +0000 (UTC) X-Received: from ub4014a598e6c52.ant.amazon.com (10.43.160.26) by EX13D49EUC003.ant.amazon.com (10.43.164.91) with Microsoft SMTP Server (TLS) id 15.0.1497.36; Tue, 21 Jun 2022 21:39:26 +0000 From: "Ojeda Leon, Nicolas via groups.io" To: CC: , , , Nicolas Ojeda Leon , Alexander Graf , Gerd Hoffmann Subject: [edk2-devel] [PATCH v6 4/5] Ovmf/PlatformPei: Use host-provided GPA end if available Date: Tue, 21 Jun 2022 23:38:58 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.43.160.26] X-ClientProxiedBy: EX13D29UWC002.ant.amazon.com (10.43.162.254) To EX13D49EUC003.ant.amazon.com (10.43.164.91) Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ncoleon@amazon.com X-Gm-Message-State: 1oE1xu8VDO8IwCG0kzSj7XAix1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1655847574; bh=EoEZeFLpTLiqVWmqtEY8MMQ7yvKU0iSOwaRyJplav5k=; h=CC:Content-Type:Date:From:Reply-To:Subject:To; b=kTi7ApMRd4nF/hLqnWySQf/2rSINYShVmFF9h2ExK3QSKEVyT3NQNRU/128FpP2STpX SbKG6pqe2/s1MjLRit1YP3yIeMsMXXPDOtT0VjcsXkgHQTEqd8ZiabCb7XTC+RyVRqJdq f/cRBpZIN+WlMIu29hTdyoGcvNx4Pq7W6gc= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1655847574617100001 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Read the "hardware-info" item from fw-cfg to extract specifications of PCI host bridges and analyze the 64-bit apertures of them to find out the highest 64-bit MMIO address required which determines the address space required by the guest, and, consequently, the FirstNonAddress used to calculate size of physical addresses. Using the static PeiHardwareInfoLib, read the fw-cfg file of hardware information to extract, one by one, all the host bridges. Find the last 64-bit MMIO address of each host bridge, using the HardwareInfoPciHostBridgeLib API, and compare it to an accumulate value to discover the highest address used, which corresponds to the highest value that must be included in the guest's physical address space. Given that platforms with multiple host bridges may provide the PCI apertures' addresses, the memory detection logic must take into account that, if the host provided the MMIO windows that can and must be used, the guest needs to take those values. Therefore, if the MMIO windows are found in the host-provided fw-cfg file, skip all the logic calculating the physical address size and just use the value provided. Since each PCI host bridge corresponds to an element in the information provided by the host, each of these must be analyzed looking for the highest address used. Cc: Alexander Graf Cc: Gerd Hoffmann Acked-by: Gerd Hoffmann Signed-off-by: Nicolas Ojeda Leon --- Notes: v6: Preparation for merge: v5: - Moved all changes from MemDetect.c into recently created PlatformInitLib/MemDetect.c effectively re-basing this patch onto latest master - Adapted function name to use Platform____ convention without any changes in the logic --- OvmfPkg/Library/PlatformInitLib/MemDetect.c | 148 +++++++++++++++++- .../PlatformInitLib/PlatformInitLib.inf | 1 + 2 files changed, 142 insertions(+), 7 deletions(-) diff --git a/OvmfPkg/Library/PlatformInitLib/MemDetect.c b/OvmfPkg/Library/= PlatformInitLib/MemDetect.c index c28d7601f8..942eaf89cf 100644 --- a/OvmfPkg/Library/PlatformInitLib/MemDetect.c +++ b/OvmfPkg/Library/PlatformInitLib/MemDetect.c @@ -27,6 +27,7 @@ Module Name: #include #include #include +#include #include #include #include @@ -527,6 +528,126 @@ PlatformAddressWidthFromCpuid ( )); } =20 +/** + Iterate over the PCI host bridges resources information optionally provi= ded + in fw-cfg and find the highest address contained in the PCI MMIO windows= . If + the information is found, return the exclusive end; one past the last us= able + address. + + @param[out] PciMmioAddressEnd Pointer to one-after End Address updated w= ith + information extracted from host-provided d= ata + or zero if no information available or an + error happened + + @retval EFI_SUCCESS PCI information was read and the output + parameter updated with the last valid + address in the 64-bit MMIO range. + @retval EFI_INVALID_PARAMETER Pointer parameter is invalid + @retval EFI_INCOMPATIBLE_VERSION Hardware information found in fw-cfg + has an incompatible format + @retval EFI_UNSUPPORTED Fw-cfg is not supported, thus host + provided information, if any, cannot be + read + @retval EFI_NOT_FOUND No PCI host bridge information provided + by the host. +**/ +STATIC +EFI_STATUS +PlatformScanHostProvided64BitPciMmioEnd ( + OUT UINT64 *PciMmioAddressEnd + ) +{ + EFI_STATUS Status; + HOST_BRIDGE_INFO HostBridge; + FIRMWARE_CONFIG_ITEM FwCfgItem; + UINTN FwCfgSize; + UINTN FwCfgReadIndex; + UINTN ReadDataSize; + UINT64 Above4GMmioEnd; + + if (PciMmioAddressEnd =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + *PciMmioAddressEnd =3D 0; + Above4GMmioEnd =3D 0; + + Status =3D QemuFwCfgFindFile ("etc/hardware-info", &FwCfgItem, &FwCfgSiz= e); + if (EFI_ERROR (Status)) { + return Status; + } + + QemuFwCfgSelectItem (FwCfgItem); + + FwCfgReadIndex =3D 0; + while (FwCfgReadIndex < FwCfgSize) { + Status =3D QemuFwCfgReadNextHardwareInfoByType ( + HardwareInfoTypeHostBridge, + sizeof (HostBridge), + FwCfgSize, + &HostBridge, + &ReadDataSize, + &FwCfgReadIndex + ); + + if (Status !=3D EFI_SUCCESS) { + // + // No more data available to read in the file, break + // loop and finish process + // + break; + } + + Status =3D HardwareInfoPciHostBridgeLastMmioAddress ( + &HostBridge, + ReadDataSize, + TRUE, + &Above4GMmioEnd + ); + + if (Status !=3D EFI_SUCCESS) { + // + // Error parsing MMIO apertures and extracting last MMIO + // address, reset PciMmioAddressEnd as if no information was + // found, to avoid moving forward with incomplete data, and + // bail out + // + DEBUG (( + DEBUG_ERROR, + "%a: ignoring malformed hardware information from fw_cfg\n", + __FUNCTION__ + )); + *PciMmioAddressEnd =3D 0; + return Status; + } + + if (Above4GMmioEnd > *PciMmioAddressEnd) { + *PciMmioAddressEnd =3D Above4GMmioEnd; + } + } + + if (*PciMmioAddressEnd > 0) { + // + // Host-provided PCI information was found and a MMIO window end + // derived from it. + // Increase the End address by one to have the output pointing to + // one after the address in use (exclusive end). + // + *PciMmioAddressEnd +=3D 1; + + DEBUG (( + DEBUG_INFO, + "%a: Pci64End=3D0x%Lx\n", + __FUNCTION__, + *PciMmioAddressEnd + )); + + return EFI_SUCCESS; + } + + return EFI_NOT_FOUND; +} + /** Initialize the PhysMemAddressWidth field in PlatformInfoHob based on gue= st RAM size. **/ @@ -536,8 +657,9 @@ PlatformAddressWidthInitialization ( IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob ) { - UINT64 FirstNonAddress; - UINT8 PhysMemAddressWidth; + UINT64 FirstNonAddress; + UINT8 PhysMemAddressWidth; + EFI_STATUS Status; =20 if (PlatformInfoHob->HostBridgeDevId =3D=3D 0xffff /* microvm */) { PlatformAddressWidthFromCpuid (PlatformInfoHob); @@ -545,12 +667,24 @@ PlatformAddressWidthInitialization ( } =20 // - // As guest-physical memory size grows, the permanent PEI RAM requiremen= ts - // are dominated by the identity-mapping page tables built by the DXE IP= L. - // The DXL IPL keys off of the physical address bits advertized in the C= PU - // HOB. To conserve memory, we calculate the minimum address width here. + // First scan host-provided hardware information to assess if the address + // space is already known. If so, guest must use those values. // - FirstNonAddress =3D PlatformGetFirstNonAddress (PlatformInfoHob); + Status =3D PlatformScanHostProvided64BitPciMmioEnd (&FirstNonAddress); + + if (EFI_ERROR (Status)) { + // + // If the host did not provide valid hardware information leading to a + // hard-defined 64-bit MMIO end, fold back to calculating the minimum = range + // needed. + // As guest-physical memory size grows, the permanent PEI RAM requirem= ents + // are dominated by the identity-mapping page tables built by the DXE = IPL. + // The DXL IPL keys off of the physical address bits advertized in the= CPU + // HOB. To conserve memory, we calculate the minimum address width her= e. + // + FirstNonAddress =3D PlatformGetFirstNonAddress (PlatformInfoHob); + } + PhysMemAddressWidth =3D (UINT8)HighBitSet64 (FirstNonAddress); =20 // diff --git a/OvmfPkg/Library/PlatformInitLib/PlatformInitLib.inf b/OvmfPkg/= Library/PlatformInitLib/PlatformInitLib.inf index d2a0bec434..d2fa2d998d 100644 --- a/OvmfPkg/Library/PlatformInitLib/PlatformInitLib.inf +++ b/OvmfPkg/Library/PlatformInitLib/PlatformInitLib.inf @@ -50,6 +50,7 @@ MtrrLib PcdLib PciLib + PeiHardwareInfoLib =20 [LibraryClasses.X64] TdxLib --=20 2.17.1 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#90660): https://edk2.groups.io/g/devel/message/90660 Mute This Topic: https://groups.io/mt/91909947/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Fri May 3 16:11:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+90661+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+90661+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1655847695; cv=none; d=zohomail.com; s=zohoarc; b=MMqTDKyt7vkmMbrEbWT+vtWd/OOI4alk0TUYhhzTMExuOqT1IfPsYiIzbankPA1K109bi5TYPMoc8YDQMGQNzUosRbhpbx8tRp0E6naX2iIEYkarHCKq1vcuB6AZ2DuDjKtZXNs76JrMv6UakqZGQqQtzWRm07lXtL0J5ZD9JN0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1655847695; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=7Rk8ootaCWKkC0RkhjdgfZhmspx4JEzvRXyes2vJ6fo=; b=VRORf9kKDeZ0nDiWcjNakzzJIh5+ufLQWFyEEGDtKJKPFZqhlcgKRywrIjpuRHx+cqcC5zMFYOj4OGYG5Gjz5F/Pp2BJIlq8TSTOqyJ9/470A3JX/k1FB5wH6e/BWXu3OVVee4xStjEpIidB5zwFS87Qob21cDpPL5w6GllRyBk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+90661+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1655847695841818.4640418080833; Tue, 21 Jun 2022 14:41:35 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id j8zCYY1788612x60z9ttxnjB; Tue, 21 Jun 2022 14:41:35 -0700 X-Received: from smtp-fw-80006.amazon.com (smtp-fw-80006.amazon.com [99.78.197.217]) by mx.groups.io with SMTP id smtpd.web09.191.1655847693835176719 for ; Tue, 21 Jun 2022 14:41:34 -0700 X-IronPort-AV: E=Sophos;i="5.92,210,1650931200"; d="scan'208";a="100442823" X-Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO email-inbound-relay-pdx-2c-b09ea7fa.us-west-2.amazon.com) ([10.25.36.214]) by smtp-border-fw-80006.pdx80.corp.amazon.com with ESMTP; 21 Jun 2022 21:41:19 +0000 X-Received: from EX13D49EUC003.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan3.pdx.amazon.com [10.236.137.198]) by email-inbound-relay-pdx-2c-b09ea7fa.us-west-2.amazon.com (Postfix) with ESMTPS id 5ACDA432F2; Tue, 21 Jun 2022 21:41:18 +0000 (UTC) X-Received: from ub4014a598e6c52.ant.amazon.com (10.43.161.183) by EX13D49EUC003.ant.amazon.com (10.43.164.91) with Microsoft SMTP Server (TLS) id 15.0.1497.36; Tue, 21 Jun 2022 21:41:14 +0000 From: "Ojeda Leon, Nicolas via groups.io" To: CC: , , , Nicolas Ojeda Leon , Alexander Graf , Gerd Hoffmann Subject: [edk2-devel] [PATCH v6 5/5] OvmfPkg/PciHostBridgeUtilityLib: Initialize RootBridges apertures with spec Date: Tue, 21 Jun 2022 23:40:48 +0200 Message-ID: <32aacc5e6c8804b6d18da904b43dce27da484a38.1655841172.git.ncoleon@amazon.com> In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.43.161.183] X-ClientProxiedBy: EX13D22UWC001.ant.amazon.com (10.43.162.192) To EX13D49EUC003.ant.amazon.com (10.43.164.91) Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ncoleon@amazon.com X-Gm-Message-State: wVBYDvlaOKxUytrnQKteLjqjx1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1655847695; bh=7hkXa3P1N61V/vtE05vOdcZ0lylN33N0R+PE8S8Uels=; h=CC:Content-Type:Date:From:Reply-To:Subject:To; b=WDG6+lBTyzcTvlvhKlJPToVaHP+uBlNpkTMVWFE5nleewWmqM2d/OHYc4vu+y6lj3cG DyFHHRJgfMRn5XQfIi4ibZiDeXwkrt6VgsodRZ8euRcfFsVx+/lAaBWdja/Ta3SMANG9I 5XiLZSUJA9dJ2NOvipsSvzdySuxsIO+pznY= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1655847697510100002 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Consume the host-provided specification of PCI host bridges if available. Using the DxeHardwareInfoLib, populate a list of hardware descriptors based on the content of the "hardware-info" fw-cfg file, if provided. In the affirmative case, use the resources and attributes specified by the hypervisor for each Host Bridge to create the RootBridge elements. In Ovmf platforms, the host can provide the specification of non-discoverable hardware resources like PCI host bridges. If the proper fw-cfg file is found, parse the contents provided by the host into a linked list by using the Hardware Info library. Then, using the list of PCI host bridges' descriptions, populate the PCI_ROOT_BRIDGES array with the resources and attributes specified by the host. If the file is not provided or no Host Bridge is found in it, fold back to the legacy method based on pre-defined apertures and rules. In some use cases, the host requires additional control over the hardware resources' configurations in the guest for performance and discoverability reasons. For instance, to disclose information about the PCI hierarchy to the guest so that this can profit from optimized accesses. In this case, the host can decide to describe multiple PCI Host Bridges and provide a specific set of resources (e.g. MMIO apertures) so that the guest uses the values provided. Using the provided values may entitle the guest to added performance, for example by using specific MMIO mappings that can enable peer-to-peer communication across the PCI hierarchy or by allocating memory closer to a device for faster DMA transactions. Cc: Alexander Graf Cc: Gerd Hoffmann Acked-by: Gerd Hoffmann Signed-off-by: Nicolas Ojeda Leon --- Notes: v6: Preparation for merge: - Verify the status output of HardwareInfoPciHostBridgeGet and abort the host-provided PCI root bridges parsing if an error happened. - Verify the validity of bus number range (start - end) extracted from host-provided info to avoid creating root bridges that have an invalid range. Abort parsing if error detected. - Cast bus number values down to UINT8 after having verified values are in range. v5: - Added "Acked-by" tag --- .../PciHostBridgeUtilityLib.c | 332 +++++++++++++++++- .../PciHostBridgeUtilityLib.inf | 1 + 2 files changed, 328 insertions(+), 5 deletions(-) diff --git a/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLi= b.c b/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.c index 92e1ea812f..cad2bd6c96 100644 --- a/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.c +++ b/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.c @@ -12,13 +12,16 @@ =20 #include #include +#include #include #include #include +#include #include #include #include #include +#include =20 #pragma pack(1) typedef struct { @@ -189,7 +192,9 @@ PciHostBridgeUtilityUninitRootBridge ( } =20 /** - Utility function to return all the root bridge instances in an array. + Utility function to scan PCI root bridges and create instances for those + that are found not empty. Populate their resources from the default + provided parameters and return all the root bridge instances in an array. =20 @param[out] Count The number of root bridge instances. =20 @@ -217,9 +222,9 @@ PciHostBridgeUtilityUninitRootBridge ( =20 @return All the root bridge instances in an a= rray. **/ +STATIC PCI_ROOT_BRIDGE * -EFIAPI -PciHostBridgeUtilityGetRootBridges ( +PciHostBridgeUtilityGetRootBridgesBusScan ( OUT UINTN *Count, IN UINT64 Attributes, IN UINT64 AllocationAttributes, @@ -243,8 +248,6 @@ PciHostBridgeUtilityGetRootBridges ( UINTN LastRootBridgeNumber; UINTN RootBridgeNumber; =20 - *Count =3D 0; - if ((BusMin > BusMax) || (BusMax > PCI_MAX_BUS)) { DEBUG (( DEBUG_ERROR, @@ -403,6 +406,325 @@ FreeBridges: return NULL; } =20 +/** + Utility function to read root bridges information from host-provided fw-= cfg + file and return them in an array. + + @param[out] Count The number of root bridge instances. + + @return All the root bridge instances in an array parsed from + host-provided fw-cfg file (hardware-info). +**/ +STATIC +PCI_ROOT_BRIDGE * +PciHostBridgeUtilityGetRootBridgesHostProvided ( + OUT UINTN *Count + ) +{ + EFI_STATUS Status; + FIRMWARE_CONFIG_ITEM FwCfgItem; + UINTN FwCfgSize; + PCI_ROOT_BRIDGE *Bridges; + UINTN Initialized; + UINTN LastRootBridgeNumber; + UINTN RootBridgeNumber; + UINTN PciHostBridgeCount; + UINT8 *HardwareInfoBlob; + LIST_ENTRY HwInfoList; + LIST_ENTRY *HwLink; + HARDWARE_INFO *HwInfo; + UINT64 Attributes; + UINT64 AllocationAttributes; + BOOLEAN DmaAbove4G; + BOOLEAN NoExtendedConfigSpace; + BOOLEAN CombineMemPMem; + PCI_ROOT_BRIDGE_APERTURE Io; + PCI_ROOT_BRIDGE_APERTURE Mem; + PCI_ROOT_BRIDGE_APERTURE MemAbove4G; + PCI_ROOT_BRIDGE_APERTURE PMem; + PCI_ROOT_BRIDGE_APERTURE PMemAbove4G; + + // + // Initialize the Hardware Info list head to start with an empty but val= id + // list head. + // + InitializeListHead (&HwInfoList); + HardwareInfoBlob =3D NULL; + Initialized =3D 0; + Bridges =3D NULL; + PciHostBridgeCount =3D 0; + + // + // Hypervisor can provide the specifications (resources) for one or more + // PCI host bridges. Such information comes through fw-cfg as part of + // the hardware-info file. + // + Status =3D QemuFwCfgFindFile ("etc/hardware-info", &FwCfgItem, &FwCfgSiz= e); + + if (EFI_ERROR (Status)) { + return NULL; + } + + HardwareInfoBlob =3D AllocatePool (FwCfgSize); + + if (HardwareInfoBlob =3D=3D NULL) { + DEBUG (( + DEBUG_ERROR, + "%a: Failed to allocate memory for hardware resources info\n", + __FUNCTION__ + )); + return NULL; + } + + QemuFwCfgSelectItem (FwCfgItem); + QemuFwCfgReadBytes (FwCfgSize, HardwareInfoBlob); + + // + // Create the list of hardware info devices filtering for PCI host + // bridges + // + Status =3D CreateHardwareInfoList ( + HardwareInfoBlob, + FwCfgSize, + HardwareInfoTypeHostBridge, + &HwInfoList + ); + + if (EFI_ERROR (Status)) { + DEBUG (( + DEBUG_ERROR, + "%a: Failed to create hardware info list to retrieve host " + "bridges information from fw-cfg\n", + __FUNCTION__ + )); + + goto FreeBridges; + } + + PciHostBridgeCount =3D GetHardwareInfoCountByType ( + &HwInfoList, + HardwareInfoTypeHostBridge, + sizeof (HOST_BRIDGE_INFO) + ); + + if (PciHostBridgeCount =3D=3D 0) { + goto FreeBridges; + } + + DEBUG (( + DEBUG_INFO, + "%a: Host provided description for %Lu root bridges\n", + __FUNCTION__, + PciHostBridgeCount + )); + + // + // Allocate the root bridges + // + Bridges =3D AllocatePool (((UINTN)PciHostBridgeCount) * sizeof *Bridges); + if (Bridges =3D=3D NULL) { + DEBUG ((DEBUG_ERROR, "%a: %r\n", __FUNCTION__, EFI_OUT_OF_RESOURCES)); + goto FreeBridges; + } + + // + // If Host Bridges' specification was obtained from fw-cfg, the list + // contains information to populate all root bridges in the system + // including resources and attributes. + // + HwLink =3D GetFirstHardwareInfoByType ( + &HwInfoList, + HardwareInfoTypeHostBridge, + sizeof (HOST_BRIDGE_INFO) + ); + + while (!EndOfHardwareInfoList (&HwInfoList, HwLink)) { + HwInfo =3D HARDWARE_INFO_FROM_LINK (HwLink); + + Status =3D HardwareInfoPciHostBridgeGet ( + HwInfo->Data.PciHostBridge, + (UINTN)HwInfo->Header.Size, + &RootBridgeNumber, + &LastRootBridgeNumber, + &Attributes, + &DmaAbove4G, + &NoExtendedConfigSpace, + &CombineMemPMem, + &Io, + &Mem, + &MemAbove4G, + &PMem, + &PMemAbove4G, + NULL + ); + + if (EFI_ERROR (Status)) { + goto FreeBridges; + } + + if ((RootBridgeNumber > LastRootBridgeNumber) || (LastRootBridgeNumber= > PCI_MAX_BUS)) { + DEBUG (( + DEBUG_ERROR, + "%a: invalid bus range with BusMin %Lu and BusMax " + "%Lu\n", + __FUNCTION__, + (UINT64)RootBridgeNumber, + (UINT64)LastRootBridgeNumber + )); + goto FreeBridges; + } + + AllocationAttributes =3D 0; + if (CombineMemPMem) { + AllocationAttributes |=3D EFI_PCI_HOST_BRIDGE_COMBINE_MEM_PMEM; + } + + if ((MemAbove4G.Limit > MemAbove4G.Base) || + (PMemAbove4G.Limit > PMemAbove4G.Base)) + { + AllocationAttributes |=3D EFI_PCI_HOST_BRIDGE_MEM64_DECODE; + } + + Status =3D PciHostBridgeUtilityInitRootBridge ( + Attributes, + Attributes, + AllocationAttributes, + DmaAbove4G, + NoExtendedConfigSpace, + (UINT8)RootBridgeNumber, + (UINT8)LastRootBridgeNumber, + &Io, + &Mem, + &MemAbove4G, + &PMem, + &PMemAbove4G, + &Bridges[Initialized] + ); + + if (EFI_ERROR (Status)) { + goto FreeBridges; + } + + ++Initialized; + + HwLink =3D GetNextHardwareInfoByType ( + &HwInfoList, + HwLink, + HardwareInfoTypeHostBridge, + sizeof (HOST_BRIDGE_INFO) + ); + } + + *Count =3D Initialized; + + // + // If resources were allocated for host bridges info, release them + // + if (HardwareInfoBlob) { + FreePool (HardwareInfoBlob); + } + + FreeHardwareInfoList (&HwInfoList); + return Bridges; + +FreeBridges: + while (Initialized > 0) { + --Initialized; + PciHostBridgeUtilityUninitRootBridge (&Bridges[Initialized]); + } + + if (Bridges) { + FreePool (Bridges); + } + + if (HardwareInfoBlob) { + FreePool (HardwareInfoBlob); + } + + FreeHardwareInfoList (&HwInfoList); + return NULL; +} + +/** + Utility function to return all the root bridge instances in an array. + + @param[out] Count The number of root bridge instances. + + @param[in] Attributes Initial attributes. + + @param[in] AllocAttributes Allocation attributes. + + @param[in] DmaAbove4G DMA above 4GB memory. + + @param[in] NoExtendedConfigSpace No Extended Config Space. + + @param[in] BusMin Minimum Bus number, inclusive. + + @param[in] BusMax Maximum Bus number, inclusive. + + @param[in] Io IO aperture. + + @param[in] Mem MMIO aperture. + + @param[in] MemAbove4G MMIO aperture above 4G. + + @param[in] PMem Prefetchable MMIO aperture. + + @param[in] PMemAbove4G Prefetchable MMIO aperture above 4G. + + @return All the root bridge instances in an a= rray. +**/ +PCI_ROOT_BRIDGE * +EFIAPI +PciHostBridgeUtilityGetRootBridges ( + OUT UINTN *Count, + IN UINT64 Attributes, + IN UINT64 AllocationAttributes, + IN BOOLEAN DmaAbove4G, + IN BOOLEAN NoExtendedConfigSpace, + IN UINTN BusMin, + IN UINTN BusMax, + IN PCI_ROOT_BRIDGE_APERTURE *Io, + IN PCI_ROOT_BRIDGE_APERTURE *Mem, + IN PCI_ROOT_BRIDGE_APERTURE *MemAbove4G, + IN PCI_ROOT_BRIDGE_APERTURE *PMem, + IN PCI_ROOT_BRIDGE_APERTURE *PMemAbove4G + ) +{ + PCI_ROOT_BRIDGE *Bridges; + + *Count =3D 0; + + // + // First attempt to get the host provided descriptions of the Root Bridg= es + // if available. + // + Bridges =3D PciHostBridgeUtilityGetRootBridgesHostProvided (Count); + + // + // If host did not provide Root Bridge information, scan the buses and + // auto populate them with default resources. + // + if (Bridges =3D=3D NULL) { + Bridges =3D PciHostBridgeUtilityGetRootBridgesBusScan ( + Count, + Attributes, + AllocationAttributes, + DmaAbove4G, + NoExtendedConfigSpace, + BusMin, + BusMax, + Io, + Mem, + MemAbove4G, + PMem, + PMemAbove4G + ); + } + + return Bridges; +} + /** Utility function to free root bridge instances array from PciHostBridgeUtilityGetRootBridges(). diff --git a/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLi= b.inf b/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.inf index 83a734c172..e4fc903121 100644 --- a/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.inf +++ b/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.inf @@ -38,6 +38,7 @@ BaseMemoryLib DebugLib DevicePathLib + DxeHardwareInfoLib MemoryAllocationLib PciLib QemuFwCfgLib --=20 2.17.1 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#90661): https://edk2.groups.io/g/devel/message/90661 Mute This Topic: https://groups.io/mt/91909982/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-