From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by mx.groups.io with SMTP id smtpd.web09.14321.1637687371341038804 for ; Tue, 23 Nov 2021 09:09:31 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: arm.com, ip: 217.140.110.172, mailfrom: pierre.gondois@arm.com) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 042071063; Tue, 23 Nov 2021 09:09:31 -0800 (PST) Received: from e126645.nice.arm.com (unknown [10.34.129.32]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 3EC7E3F5A1; Tue, 23 Nov 2021 09:09:30 -0800 (PST) From: "PierreGondois" To: devel@edk2.groups.io Cc: Sami Mujawar , Alexei Fedorov Subject: [PATCH v3 13/15] DynamicTablesPkg: FdtHwInfoParser: Add PCI config parser Date: Tue, 23 Nov 2021 18:08:36 +0100 Message-Id: <20211123170838.143805-14-Pierre.Gondois@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211123170838.143805-1-Pierre.Gondois@arm.com> References: <20211123170838.143805-1-Pierre.Gondois@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Pierre Gondois On platforms that implement PCIe, the PCIe configuration space information must be described to a standards-based operating system in the Memory mapped configuration space base address Description (MCFG) table. The PCIe information is described in the platform Device Tree, the bindings for which can be found at: - linux/Documentation/devicetree/bindings/pci/ host-generic-pci.yaml The FdtHwInfoParser implements a PCI configuration space Parser that parses the platform Device Tree to create CM_ARM_PCI_CONFIG_SPACE_INFO objects which are encapsulated in a Configuration Manager descriptor object and added to the platform information repository. The platform Configuration Manager can then utilise this information when generating the MCFG table. Signed-off-by: Pierre Gondois --- Notes: v2: - Handle absence of Pci legacy interrupts in Pci parser. [Pierre] .../Pci/ArmPciConfigSpaceParser.c | 797 ++++++++++++++++++ .../Pci/ArmPciConfigSpaceParser.h | 142 ++++ 2 files changed, 939 insertions(+) create mode 100644 DynamicTablesPkg/Library/FdtHwInfoParserLib/Pci/ArmPc= iConfigSpaceParser.c create mode 100644 DynamicTablesPkg/Library/FdtHwInfoParserLib/Pci/ArmPc= iConfigSpaceParser.h diff --git a/DynamicTablesPkg/Library/FdtHwInfoParserLib/Pci/ArmPciConfig= SpaceParser.c b/DynamicTablesPkg/Library/FdtHwInfoParserLib/Pci/ArmPciCon= figSpaceParser.c new file mode 100644 index 000000000000..94b2721caaa9 --- /dev/null +++ b/DynamicTablesPkg/Library/FdtHwInfoParserLib/Pci/ArmPciConfigSpacePa= rser.c @@ -0,0 +1,797 @@ +/** @file + Arm PCI Configuration Space Parser. + + Copyright (c) 2021, ARM Limited. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + + @par Reference(s): + - linux/Documentation/devicetree/bindings/pci/host-generic-pci.yaml + - PCI Firmware Specification - Revision 3.0 + - Open Firmware Recommended Practice: Interrupt Mapping, Version 0.9 + - Devicetree Specification Release v0.3 + - linux kernel code +**/ + +#include "CmObjectDescUtility.h" +#include + +#include "FdtHwInfoParser.h" +#include "Pci/ArmPciConfigSpaceParser.h" +#include "Gic/ArmGicDispatcher.h" + +/** List of "compatible" property values for host PCIe bridges nodes. + + Any other "compatible" value is not supported by this module. +*/ +STATIC CONST COMPATIBILITY_STR PciCompatibleStr[] =3D { + {"pci-host-ecam-generic"} +}; + +/** COMPATIBILITY_INFO structure for the PCIe. +*/ +STATIC CONST COMPATIBILITY_INFO PciCompatibleInfo =3D { + ARRAY_SIZE (PciCompatibleStr), + PciCompatibleStr +}; + +/** Get the Segment group (also called: Domain Id) of a host-pci node. + + kernel/Documentation/devicetree/bindings/pci/pci.txt: + "It is required to either not set this property at all or set it for a= ll + host bridges in the system" + + The function checks the "linux,pci-domain" property of the host-pci no= de. + Either all host-pci nodes must have this property, or none of them. If= the + property is available, read it. Otherwise dynamically assign the Ids. + + @param [in] Fdt Pointer to a Flattened Device Tree (Fdt). + @param [in] HostPciNode Offset of a host-pci node. + @param [out] SegGroup Segment group assigned to the host-pci contr= oller. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_ABORTED An error occurred. + @retval EFI_INVALID_PARAMETER Invalid parameter. +**/ +STATIC +EFI_STATUS +EFIAPI +GetPciSegGroup ( + IN CONST VOID * Fdt, + IN INT32 HostPciNode, + OUT INT32 * SegGroup + ) +{ + CONST UINT8 * Data; + INT32 DataSize; + STATIC INT32 LocalSegGroup =3D 0; + + if ((Fdt =3D=3D NULL) || + (SegGroup =3D=3D NULL)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + Data =3D fdt_getprop (Fdt, HostPciNode, "linux,pci-domain", &DataSize)= ; + if ((Data =3D=3D NULL) || (DataSize < 0)) { + // Did not find property, assign the DomainIds ourselves. + if (LocalSegGroup < 0) { + // "linux,pci-domain" property was defined for another node. + ASSERT (0); + return EFI_ABORTED; + } + + *SegGroup =3D LocalSegGroup++; + return EFI_SUCCESS; + } + + if ((DataSize > sizeof (UINT32)) || + (LocalSegGroup > 0)) { + // Property on more than 1 cell or + // "linux,pci-domain" property was not defined for a node. + ASSERT (0); + return EFI_ABORTED; + } + + // If one node has the "linux,pci-domain" property, then all the host-= pci + // nodes must have it. + LocalSegGroup =3D -1; + + *SegGroup =3D fdt32_to_cpu (*(UINT32*)Data); + return EFI_SUCCESS; +} + +/** Parse the bus-range controlled by this host-pci node. + + @param [in] Fdt Pointer to a Flattened Device Tree (Fd= t). + @param [in] HostPciNode Offset of a host-pci node. + @param [in, out] PciInfo PCI_PARSER_TABLE structure storing + information about the current host-pci= . + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_ABORTED An error occurred. + @retval EFI_INVALID_PARAMETER Invalid parameter. +**/ +STATIC +EFI_STATUS +EFIAPI +PopulateBusRange ( + IN CONST VOID * Fdt, + IN INT32 HostPciNode, + IN OUT PCI_PARSER_TABLE * PciInfo + ) +{ + CONST UINT8 * Data; + INT32 DataSize; + UINT32 StartBus; + UINT32 EndBus; + + if ((Fdt =3D=3D NULL) || + (PciInfo =3D=3D NULL)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + Data =3D fdt_getprop (Fdt, HostPciNode, "bus-range", &DataSize); + if ((Data =3D=3D NULL) || (DataSize < 0)) { + // No evidence this property is mandatory. Use default values. + StartBus =3D 0; + EndBus =3D 255; + } else if (DataSize =3D=3D (2 * sizeof (UINT32))) { + // If available, the property is on two integers. + StartBus =3D fdt32_to_cpu (((UINT32*)Data)[0]); + EndBus =3D fdt32_to_cpu (((UINT32*)Data)[1]); + } else { + ASSERT (0); + return EFI_ABORTED; + } + + PciInfo->PciConfigSpaceInfo.StartBusNumber =3D StartBus; + PciInfo->PciConfigSpaceInfo.EndBusNumber =3D EndBus; + + return EFI_SUCCESS; +} + +/** Parse the PCI address map. + + The PCI address map is available in the "ranges" device-tree property. + + @param [in] Fdt Pointer to a Flattened Device Tree (Fd= t). + @param [in] HostPciNode Offset of a host-pci node. + @param [in] AddressCells # of cells used to encode an address o= n + the parent bus. + @param [in, out] PciInfo PCI_PARSER_TABLE structure storing + information about the current host-pci= . + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_ABORTED An error occurred. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval EFI_OUT_OF_RESOURCES An allocation has failed. +**/ +STATIC +EFI_STATUS +EFIAPI +ParseAddressMap ( + IN CONST VOID * Fdt, + IN INT32 HostPciNode, + IN INT32 AddressCells, + IN OUT PCI_PARSER_TABLE * PciInfo + ) +{ + CONST UINT8 * Data; + INT32 DataSize; + UINT32 Index; + UINT32 Offset; + UINT32 AddressMapSize; + UINT32 Count; + UINT32 PciAddressAttr; + + CM_ARM_PCI_ADDRESS_MAP_INFO * PciAddressMapInfo; + UINT32 BufferSize; + + // The mapping is done on AddressMapSize bytes. + AddressMapSize =3D (PCI_ADDRESS_CELLS + AddressCells + PCI_SIZE_CELLS)= * + sizeof (UINT32); + + Data =3D fdt_getprop (Fdt, HostPciNode, "ranges", &DataSize); + if ((Data =3D=3D NULL) || + (DataSize < 0) || + ((DataSize % AddressMapSize) !=3D 0)) { + // If error or not on AddressMapSize bytes. + ASSERT (0); + return EFI_ABORTED; + } + + Count =3D DataSize / AddressMapSize; + + // Allocate a buffer to store each address mapping. + BufferSize =3D Count * sizeof (CM_ARM_PCI_ADDRESS_MAP_INFO); + PciAddressMapInfo =3D AllocateZeroPool (BufferSize); + if (PciAddressMapInfo =3D=3D NULL) { + ASSERT (0); + return EFI_OUT_OF_RESOURCES; + } + + for (Index =3D 0; Index < Count; Index++) { + Offset =3D Index * AddressMapSize; + + // Pci address attributes + PciAddressAttr =3D fdt32_to_cpu (*(UINT32*)&Data[Offset]); + PciAddressMapInfo[Index].SpaceCode =3D READ_PCI_SS (PciAddressAttr); + Offset +=3D sizeof (UINT32); + + // Pci address + PciAddressMapInfo[Index].PciAddress =3D + fdt64_to_cpu (*(UINT64*)&Data[Offset]); + Offset +=3D (PCI_ADDRESS_CELLS - 1) * sizeof (UINT32); + + // Cpu address + if (AddressCells =3D=3D 2) { + PciAddressMapInfo[Index].CpuAddress =3D + fdt64_to_cpu (*(UINT64*)&Data[Offset]); + } else { + PciAddressMapInfo[Index].CpuAddress =3D + fdt32_to_cpu (*(UINT32*)&Data[Offset]); + } + Offset +=3D AddressCells * sizeof (UINT32); + + // Address size + PciAddressMapInfo[Index].AddressSize =3D + fdt64_to_cpu (*(UINT64*)&Data[Offset]); + Offset +=3D PCI_SIZE_CELLS * sizeof (UINT32); + } // for + + PciInfo->Mapping[PciMappingTableAddress].ObjectId =3D + CREATE_CM_ARM_OBJECT_ID (EArmObjPciAddressMapInfo); + PciInfo->Mapping[PciMappingTableAddress].Size =3D + sizeof (CM_ARM_PCI_ADDRESS_MAP_INFO) * Count; + PciInfo->Mapping[PciMappingTableAddress].Data =3D PciAddressMapInfo; + PciInfo->Mapping[PciMappingTableAddress].Count =3D Count; + + return EFI_SUCCESS; +} + +/** Parse the PCI interrupt map. + + The PCI interrupt map is available in the "interrupt-map" + and "interrupt-map-mask" device-tree properties. + + Cf Devicetree Specification Release v0.3, + s2.4.3 Interrupt Nexus Properties + + An interrupt-map must be as: + interrupt-map =3D < [child unit address] [child interrupt specifier] + [interrupt-parent] + [parent unit address] [parent interrupt specifier] > + + @param [in] Fdt Pointer to a Flattened Device Tree (Fd= t). + @param [in] HostPciNode Offset of a host-pci node. + @param [in, out] PciInfo PCI_PARSER_TABLE structure storing + information about the current host-pci= . + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_ABORTED An error occurred. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval EFI_NOT_FOUND Not found. + @retval EFI_OUT_OF_RESOURCES An allocation has failed. +**/ +STATIC +EFI_STATUS +EFIAPI +ParseIrqMap ( + IN CONST VOID * Fdt, + IN INT32 HostPciNode, + IN OUT PCI_PARSER_TABLE * PciInfo + ) +{ + EFI_STATUS Status; + CONST UINT8 * Data; + INT32 DataSize; + UINT32 Index; + UINT32 Offset; + + INT32 IntcNode; + INT32 IntcAddressCells; + INT32 IntcCells; + + INT32 PciIntCells; + INT32 IntcPhandle; + + INT32 IrqMapSize; + UINT32 IrqMapCount; + CONST UINT8 * IrqMapMask; + INT32 IrqMapMaskSize; + + INT32 PHandleOffset; + UINT32 GicVersion; + + UINT32 PciAddressAttr; + + + CM_ARM_PCI_INTERRUPT_MAP_INFO * PciInterruptMapInfo; + UINT32 BufferSize; + + Data =3D fdt_getprop (Fdt, HostPciNode, "interrupt-map", &DataSize); + if ((Data =3D=3D NULL) || (DataSize <=3D 0)) { + DEBUG (( + DEBUG_WARN, + "Fdt parser: No Legacy interrupts found for PCI configuration spac= e at " + "address: 0x%lx, group segment: %d\n", + PciInfo->PciConfigSpaceInfo.BaseAddress, + PciInfo->PciConfigSpaceInfo.PciSegmentGroupNumber + )); + return EFI_NOT_FOUND; + } + + // PCI interrupts are expected to be on 1 cell. Check it. + Status =3D FdtGetInterruptCellsInfo (Fdt, HostPciNode, &PciIntCells); + if (EFI_ERROR (Status)) { + ASSERT (0); + return Status; + } + if (PciIntCells !=3D PCI_INTERRUPTS_CELLS) { + ASSERT (0); + return EFI_ABORTED; + } + + IrqMapMask =3D fdt_getprop ( + Fdt, + HostPciNode, + "interrupt-map-mask", + &IrqMapMaskSize + ); + if ((IrqMapMask =3D=3D NULL) || + (IrqMapMaskSize !=3D + (PCI_ADDRESS_CELLS + PCI_INTERRUPTS_CELLS) * sizeof (UINT32))) { + ASSERT (0); + return EFI_ABORTED; + } + + // Get the interrupt-controller of the first irq mapping. + PHandleOffset =3D (PCI_ADDRESS_CELLS + PciIntCells) * sizeof (UINT32); + if (PHandleOffset > DataSize) { + ASSERT (0); + return EFI_ABORTED; + } + IntcPhandle =3D fdt32_to_cpu (*(UINT32*)&Data[PHandleOffset]); + IntcNode =3D fdt_node_offset_by_phandle (Fdt, IntcPhandle); + if (IntcNode < 0) { + ASSERT (0); + return EFI_ABORTED; + } + + // Only support Gic(s) for now. + Status =3D GetGicVersion (Fdt, IntcNode, &GicVersion); + if (EFI_ERROR (Status)) { + ASSERT (0); + return Status; + } + + // Get the "address-cells" property of the IntcNode. + Status =3D FdtGetAddressInfo (Fdt, IntcNode, &IntcAddressCells, NULL); + if (EFI_ERROR (Status)) { + ASSERT (0); + return Status; + } + + // Get the "interrupt-cells" property of the IntcNode. + Status =3D FdtGetInterruptCellsInfo (Fdt, IntcNode, &IntcCells); + if (EFI_ERROR (Status)) { + ASSERT (0); + return Status; + } + + // An irq mapping is done on IrqMapSize bytes + // (which includes 1 cell for the PHandle). + IrqMapSize =3D (PCI_ADDRESS_CELLS + PciIntCells + 1 + + IntcAddressCells + IntcCells) * sizeof (UINT32); + if ((DataSize % IrqMapSize) !=3D 0) { + // The mapping is not done on IrqMapSize bytes. + ASSERT (0); + return EFI_ABORTED; + } + IrqMapCount =3D DataSize / IrqMapSize; + + // We assume the same interrupt-controller is used for all the mapping= s. + // Check this is correct. + for (Index =3D 0; Index < IrqMapCount; Index++) { + if (IntcPhandle !=3D fdt32_to_cpu ( + *(UINT32*)&Data[(Index * IrqMapSize) + PHandleOffset])) { + ASSERT (0); + return EFI_ABORTED; + } + } + + // Allocate a buffer to store each interrupt mapping. + IrqMapCount =3D DataSize / IrqMapSize; + BufferSize =3D IrqMapCount * sizeof (CM_ARM_PCI_ADDRESS_MAP_INFO); + PciInterruptMapInfo =3D AllocateZeroPool (BufferSize); + if (PciInterruptMapInfo =3D=3D NULL) { + ASSERT (0); + return EFI_OUT_OF_RESOURCES; + } + + for (Index =3D 0; Index < IrqMapCount; Index++) { + Offset =3D Index * IrqMapSize; + + // Pci address attributes + PciAddressAttr =3D fdt32_to_cpu ( + (*(UINT32*)&Data[Offset]) & + (*(UINT32*)&IrqMapMask[0]) + ); + PciInterruptMapInfo[Index].PciBus =3D READ_PCI_BBBBBBBB (PciAddressA= ttr); + PciInterruptMapInfo[Index].PciDevice =3D READ_PCI_DDDDD (PciAddressA= ttr); + Offset +=3D PCI_ADDRESS_CELLS * sizeof (UINT32); + + // Pci irq + PciInterruptMapInfo[Index].PciInterrupt =3D fdt32_to_cpu ( + (*(UINT32*)&Data[Offset]) & + (*(UINT32*)&IrqMapMask[3 * sizeof (UI= NT32)]) + ); + // -1 to translate from device-tree (INTA=3D1) to ACPI (INTA=3D0) ir= q IDs. + PciInterruptMapInfo[Index].PciInterrupt -=3D 1; + Offset +=3D PCI_INTERRUPTS_CELLS * sizeof (UINT32); + + // PHandle (skip it) + Offset +=3D sizeof (UINT32); + + // "Parent unit address" (skip it) + Offset +=3D IntcAddressCells * sizeof (UINT32); + + // Interrupt controller interrupt and flags + PciInterruptMapInfo[Index].IntcInterrupt.Interrupt =3D + FdtGetInterruptId ((UINT32*)&Data[Offset]); + PciInterruptMapInfo[Index].IntcInterrupt.Flags =3D + FdtGetInterruptFlags ((UINT32*)&Data[Offset]); + } // for + + PciInfo->Mapping[PciMappingTableInterrupt].ObjectId =3D + CREATE_CM_ARM_OBJECT_ID (EArmObjPciInterruptMapInfo); + PciInfo->Mapping[PciMappingTableInterrupt].Size =3D + sizeof (CM_ARM_PCI_INTERRUPT_MAP_INFO) * IrqMapCount; + PciInfo->Mapping[PciMappingTableInterrupt].Data =3D PciInterruptMapInf= o; + PciInfo->Mapping[PciMappingTableInterrupt].Count =3D IrqMapCount; + + return Status; +} + +/** Parse a Host-pci node. + + @param [in] Fdt Pointer to a Flattened Device Tree (Fdt= ). + @param [in] HostPciNode Offset of a host-pci node. + @param [in, out] PciInfo The CM_ARM_PCI_CONFIG_SPACE_INFO to pop= ulate. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_ABORTED An error occurred. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval EFI_OUT_OF_RESOURCES An allocation has failed. +**/ +STATIC +EFI_STATUS +EFIAPI +PciNodeParser ( + IN CONST VOID * Fdt, + IN INT32 HostPciNode, + IN OUT PCI_PARSER_TABLE * PciInfo + ) +{ + EFI_STATUS Status; + INT32 AddressCells; + INT32 SizeCells; + CONST UINT8 * Data; + INT32 DataSize; + INT32 SegGroup; + + if ((Fdt =3D=3D NULL) || + (PciInfo =3D=3D NULL)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + // Segment Group / DomainId + Status =3D GetPciSegGroup (Fdt, HostPciNode, &SegGroup); + if (EFI_ERROR (Status)) { + ASSERT (0); + return Status; + } + PciInfo->PciConfigSpaceInfo.PciSegmentGroupNumber =3D SegGroup; + + // Bus range + Status =3D PopulateBusRange (Fdt, HostPciNode, PciInfo); + if (EFI_ERROR (Status)) { + ASSERT (0); + return Status; + } + + Status =3D FdtGetParentAddressInfo ( + Fdt, + HostPciNode, + &AddressCells, + &SizeCells + ); + if (EFI_ERROR (Status)) { + ASSERT (0); + return Status; + } + + // Only support 32/64 bits addresses. + if ((AddressCells < 1) || + (AddressCells > 2) || + (SizeCells < 1) || + (SizeCells > 2)) { + ASSERT (0); + return EFI_ABORTED; + } + + Data =3D fdt_getprop (Fdt, HostPciNode, "reg", &DataSize); + if ((Data =3D=3D NULL) || + (DataSize !=3D ((AddressCells + SizeCells) * sizeof (UINT32)))) { + // If error or wrong size. + ASSERT (0); + return EFI_ABORTED; + } + + // Base address + if (AddressCells =3D=3D 2) { + PciInfo->PciConfigSpaceInfo.BaseAddress =3D fdt64_to_cpu (*(UINT64*)= Data); + } else { + PciInfo->PciConfigSpaceInfo.BaseAddress =3D fdt32_to_cpu (*(UINT32*)= Data); + } + + // Address map + Status =3D ParseAddressMap ( + Fdt, + HostPciNode, + AddressCells, + PciInfo + ); + if (EFI_ERROR (Status)) { + ASSERT (0); + return Status; + } + + // Irq map + Status =3D ParseIrqMap ( + Fdt, + HostPciNode, + PciInfo + ); + if (EFI_ERROR (Status) && (Status !=3D EFI_NOT_FOUND)) { + ASSERT (0); + } + + return EFI_SUCCESS; +} + +/** Add the parsed Pci information to the Configuration Manager. + + CmObj of the following types are concerned: + - EArmObjPciConfigSpaceInfo + - EArmObjPciAddressMapInfo + - EArmObjPciInterruptMapInfo + + @param [in] FdtParserHandle A handle to the parser instance. + @param [in] PciTableInfo PCI_PARSER_TABLE structure containing th= e + CmObjs to add. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval EFI_OUT_OF_RESOURCES An allocation has failed. +**/ +STATIC +EFI_STATUS +EFIAPI +PciInfoAdd ( + IN CONST FDT_HW_INFO_PARSER_HANDLE FdtParserHandle, + IN PCI_PARSER_TABLE *PciTableInfo + ) +{ + EFI_STATUS Status; + CM_ARM_PCI_CONFIG_SPACE_INFO *PciConfigSpaceInfo; + + if ((FdtParserHandle =3D=3D NULL) || + (PciTableInfo =3D=3D NULL)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + PciConfigSpaceInfo =3D &PciTableInfo->PciConfigSpaceInfo; + + // Add the address map space CmObj to the Configuration Manager. + Status =3D AddMultipleCmObjWithCmObjRef ( + FdtParserHandle, + &PciTableInfo->Mapping[PciMappingTableAddress], + &PciConfigSpaceInfo->AddressMapToken + ); + if (EFI_ERROR (Status)) { + ASSERT (0); + return Status; + } + + // Add the interrupt map space CmObj to the Configuration Manager. + // Possible to have no legacy interrupts, or no device described and + // thus no interrupt-mapping. + if (PciTableInfo->Mapping[PciMappingTableInterrupt].Count !=3D 0) { + Status =3D AddMultipleCmObjWithCmObjRef ( + FdtParserHandle, + &PciTableInfo->Mapping[PciMappingTableInterrupt], + &PciConfigSpaceInfo->InterruptMapToken + ); + if (EFI_ERROR (Status)) { + ASSERT (0); + return Status; + } + } + + // Add the configuration space CmObj to the Configuration Manager. + Status =3D AddSingleCmObj ( + FdtParserHandle, + CREATE_CM_ARM_OBJECT_ID (EArmObjPciConfigSpaceInfo), + &PciTableInfo->PciConfigSpaceInfo, + sizeof (CM_ARM_PCI_CONFIG_SPACE_INFO), + NULL + ); + ASSERT_EFI_ERROR (Status); + return Status; +} + +/** Free the CmObjDesc of the ParserTable. + + @param [in] PciTableInfo PCI_PARSER_TABLE structure containing th= e + CmObjs to free. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. +**/ +STATIC +EFI_STATUS +EFIAPI +FreeParserTable ( + IN PCI_PARSER_TABLE *PciTableInfo + ) +{ + UINT32 Index; + VOID *Data; + + if (PciTableInfo =3D=3D NULL) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + for (Index =3D 0; Index < PciMappingTableMax; Index++) { + Data =3D PciTableInfo->Mapping[Index].Data; + if (Data !=3D NULL) { + FreePool (Data); + } + } + + return EFI_SUCCESS; +} + +/** CM_ARM_PCI_CONFIG_SPACE_INFO parser function. + + The following structure is populated: + typedef struct CmArmPciConfigSpaceInfo { + UINT64 BaseAddress; // {Populated} + UINT16 PciSegmentGroupNumber; // {Populated} + UINT8 StartBusNumber; // {Populated} + UINT8 EndBusNumber; // {Populated} + } CM_ARM_PCI_CONFIG_SPACE_INFO; + + typedef struct CmArmPciAddressMapInfo { + UINT8 SpaceCode; // {Populated} + UINT64 PciAddress; // {Populated} + UINT64 CpuAddress; // {Populated} + UINT64 AddressSize; // {Populated} + } CM_ARM_PCI_ADDRESS_MAP_INFO; + + typedef struct CmArmPciInterruptMapInfo { + UINT8 PciBus; // {Populated} + UINT8 PciDevice; // {Populated} + UINT8 PciInterrupt; // {Populated} + CM_ARM_GENERIC_INTERRUPT IntcInterrupt; // {Populated} + } CM_ARM_PCI_INTERRUPT_MAP_INFO; + + A parser parses a Device Tree to populate a specific CmObj type. None, + one or many CmObj can be created by the parser. + The created CmObj are then handed to the parser's caller through the + HW_INFO_ADD_OBJECT interface. + This can also be a dispatcher. I.e. a function that not parsing a + Device Tree but calling other parsers. + + @param [in] FdtParserHandle A handle to the parser instance. + @param [in] FdtBranch When searching for DT node name, restrict + the search to this Device Tree branch. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_ABORTED An error occurred. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval EFI_NOT_FOUND Not found. + @retval EFI_UNSUPPORTED Unsupported. +**/ +EFI_STATUS +EFIAPI +ArmPciConfigInfoParser ( + IN CONST FDT_HW_INFO_PARSER_HANDLE FdtParserHandle, + IN INT32 FdtBranch + ) +{ + EFI_STATUS Status; + UINT32 Index; + INT32 PciNode; + UINT32 PciNodeCount; + PCI_PARSER_TABLE PciTableInfo; + VOID *Fdt; + + if (FdtParserHandle =3D=3D NULL) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + Fdt =3D FdtParserHandle->Fdt; + + // Only search host-pci devices. + // PCI Firmware Specification Revision 3.0, s4.1.2. "MCFG Table Descri= ption": + // "This table directly refers to PCI Segment Groups defined in the sy= stem + // via the _SEG object in the ACPI name space for the applicable host = bridge + // device." + Status =3D FdtCountCompatNodeInBranch ( + Fdt, + FdtBranch, + &PciCompatibleInfo, + &PciNodeCount + ); + if (EFI_ERROR (Status)) { + ASSERT (0); + return Status; + } + + if (PciNodeCount =3D=3D 0) { + return EFI_NOT_FOUND; + } + + // Parse each host-pci node in the branch. + PciNode =3D FdtBranch; + for (Index =3D 0; Index < PciNodeCount; Index++) { + ZeroMem (&PciTableInfo, sizeof (PCI_PARSER_TABLE)); + + Status =3D FdtGetNextCompatNodeInBranch ( + Fdt, + FdtBranch, + &PciCompatibleInfo, + &PciNode + ); + if (EFI_ERROR (Status)) { + ASSERT (0); + if (Status =3D=3D EFI_NOT_FOUND) { + // Should have found the node. + Status =3D EFI_ABORTED; + } + return Status; + } + + Status =3D PciNodeParser (Fdt, PciNode, &PciTableInfo); + if (EFI_ERROR (Status)) { + ASSERT (0); + goto error_handler; + } + + // Add Pci information to the Configuration Manager. + Status =3D PciInfoAdd (FdtParserHandle, &PciTableInfo); + if (EFI_ERROR (Status)) { + ASSERT (0); + goto error_handler; + } + + Status =3D FreeParserTable (&PciTableInfo); + if (EFI_ERROR (Status)) { + ASSERT (0); + return Status; + } + } // for + + return Status; + +error_handler: + FreeParserTable (&PciTableInfo); + return Status; +} diff --git a/DynamicTablesPkg/Library/FdtHwInfoParserLib/Pci/ArmPciConfig= SpaceParser.h b/DynamicTablesPkg/Library/FdtHwInfoParserLib/Pci/ArmPciCon= figSpaceParser.h new file mode 100644 index 000000000000..b260c53a82ab --- /dev/null +++ b/DynamicTablesPkg/Library/FdtHwInfoParserLib/Pci/ArmPciConfigSpacePa= rser.h @@ -0,0 +1,142 @@ +/** @file + Arm PCI Configuration Space Parser. + + Copyright (c) 2021, ARM Limited. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + + @par Reference(s): + - linux/Documentation/devicetree/bindings/pci/host-generic-pci.yaml + - PCI Firmware Specification - Revision 3.0 + - Open Firmware Recommended Practice: Interrupt Mapping, Version 0.9 + - Devicetree Specification Release v0.3 + - linux kernel code +**/ + +#ifndef ARM_PCI_CONFIG_SPACE_PARSER_H_ +#define ARM_PCI_CONFIG_SPACE_PARSER_H_ + +/** Read LEN bits at OFF offsets bits of the ADDR. + + @param [in] ADDR Address to read the bits from. + @param [in] OFF Offset of the bits to read. + @param [in] LEN Number of bits to read. + + @return The bits read. +**/ +#define READ_BITS(ADDR, OFF, LEN) (((ADDR) >> (OFF)) & ((1<<(LEN))-1)= ) + +/* Pci address attributes. +*/ +/// 0 if relocatable. +#define READ_PCI_N(ADDR) READ_BITS((ADDR), 31, 1) +/// 1 if prefetchable. +#define READ_PCI_P(ADDR) READ_BITS((ADDR), 30, 1) +/// 1 if aliased. +#define READ_PCI_T(ADDR) READ_BITS((ADDR), 29, 1) +/** Space code. + + 00: Configuration Space + 01: I/O Space + 10: 32-bit-address Memory Space + 11: 64-bit-address Memory Space +*/ +#define READ_PCI_SS(ADDR) READ_BITS((ADDR), 24, 2) +/// Bus number. +#define READ_PCI_BBBBBBBB(ADDR) READ_BITS((ADDR), 16, 8) +/// Device number. +#define READ_PCI_DDDDD(ADDR) READ_BITS((ADDR), 11, 5) + +/** Number of device-tree cells used for PCI nodes properties. + + Values are well defined, except the "#interrupt-cells" which + is assumed to be 1. +*/ +#define PCI_ADDRESS_CELLS 3U +#define PCI_SIZE_CELLS 2U +#define PCI_INTERRUPTS_CELLS 1U + +/** PCI interrupt flags for device-tree. + + Local Bus Specification Revision 3.0, s2.2.6., Interrupt Pins: + - 'Interrupts on PCI are optional and defined as "level sensitive," + asserted low (negative true)' +*/ +#define DT_PCI_IRQ_FLAGS(x) (((x) & 0xF) =3D=3D BIT0) + +/** Indexes in the mapping table. +*/ +typedef enum PciMappingTable { + PciMappingTableAddress, ///< 0 - Address mapping + PciMappingTableInterrupt, ///< 1 - Interrupt mapping + PciMappingTableMax, ///< 2 - Max +} PCI_MAPPING_TABLE; + +#pragma pack(1) + +/** PCI parser table + + Multiple address-map and interrupt map can correspond to + one host-pci device. This structure allows to temporarily + store the CmObjects created and generate tokens once + the whole device tree is parsed. +*/ +typedef struct PciParserTable { + /// PCI Configuration Space Info + CM_ARM_PCI_CONFIG_SPACE_INFO PciConfigSpaceInfo; + + /// Store the address mapping and interrupt mapping as CmObjDesc + /// before adding them to the Configuration Manager. + CM_OBJ_DESCRIPTOR Mapping[PciMappingTableMax]; +} PCI_PARSER_TABLE; + +#pragma pack() + +/** CM_ARM_PCI_CONFIG_SPACE_INFO parser function. + + The following structure is populated: + typedef struct CmArmPciConfigSpaceInfo { + UINT64 BaseAddress; // {Populated} + UINT16 PciSegmentGroupNumber; // {Populated} + UINT8 StartBusNumber; // {Populated} + UINT8 EndBusNumber; // {Populated} + } CM_ARM_PCI_CONFIG_SPACE_INFO; + + typedef struct CmArmPciAddressMapInfo { + UINT8 SpaceCode; // {Populated} + UINT64 PciAddress; // {Populated} + UINT64 CpuAddress; // {Populated} + UINT64 AddressSize; // {Populated} + } CM_ARM_PCI_ADDRESS_MAP_INFO; + + typedef struct CmArmPciInterruptMapInfo { + UINT8 PciBus; // {Populated} + UINT8 PciDevice; // {Populated} + UINT8 PciInterrupt; // {Populated} + CM_ARM_GENERIC_INTERRUPT IntcInterrupt; // {Populated} + } CM_ARM_PCI_INTERRUPT_MAP_INFO; + + A parser parses a Device Tree to populate a specific CmObj type. None, + one or many CmObj can be created by the parser. + The created CmObj are then handed to the parser's caller through the + HW_INFO_ADD_OBJECT interface. + This can also be a dispatcher. I.e. a function that not parsing a + Device Tree but calling other parsers. + + @param [in] FdtParserHandle A handle to the parser instance. + @param [in] FdtBranch When searching for DT node name, restrict + the search to this Device Tree branch. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_ABORTED An error occurred. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval EFI_NOT_FOUND Not found. + @retval EFI_UNSUPPORTED Unsupported. +**/ +EFI_STATUS +EFIAPI +ArmPciConfigInfoParser ( + IN CONST FDT_HW_INFO_PARSER_HANDLE FdtParserHandle, + IN INT32 FdtBranch + ); + +#endif // ARM_PCI_CONFIG_SPACE_PARSER_H_ --=20 2.25.1