From nobody Sat May 4 15:11:46 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=arm.com Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 163422303361885.20792568474997; Thu, 14 Oct 2021 07:50:33 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.209434.365902 (Exim 4.92) (envelope-from ) id 1mb23l-0005pV-EY; Thu, 14 Oct 2021 14:50:17 +0000 Received: by outflank-mailman (output) from mailman id 209434.365902; Thu, 14 Oct 2021 14:50:17 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mb23l-0005pM-BM; Thu, 14 Oct 2021 14:50:17 +0000 Received: by outflank-mailman (input) for mailman id 209434; Thu, 14 Oct 2021 14:50:16 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mb23k-0005ov-Ar for xen-devel@lists.xenproject.org; Thu, 14 Oct 2021 14:50:16 +0000 Received: from foss.arm.com (unknown [217.140.110.172]) by us1-rack-iad1.inumbo.com (Halon) with ESMTP id 8da1b29e-5231-433d-9283-254277e46922; Thu, 14 Oct 2021 14:50:15 +0000 (UTC) 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 34787D6E; Thu, 14 Oct 2021 07:50:15 -0700 (PDT) Received: from e109506.cambridge.arm.com (e109506.cambridge.arm.com [10.1.199.62]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 326763F694; Thu, 14 Oct 2021 07:50:14 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 8da1b29e-5231-433d-9283-254277e46922 From: Bertrand Marquis To: xen-devel@lists.xenproject.org Cc: iwj@xenproject.org, Paul Durrant , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Wei Liu Subject: [PATCH v6 1/3] xen/vpci: Move ecam access functions to common code Date: Thu, 14 Oct 2021 15:49:49 +0100 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1634223035414100003 PCI standard is using ECAM and not MCFG which is coming from ACPI[1]. Use ECAM/ecam instead of MCFG in common code and in new functions added in common vpci code by this patch. Rename vpci_access_allowed to vpci_ecam_access_allowed and move it from arch/x86/hvm/io.c to drivers/vpci/vpci.c. Create vpci_ecam_mmio_{read,write} in drivers/vpci/vpci.c that contains the common code to perform these operations, changed vpci_mmcfg_{read,write} accordingly to make use of these functions. The vpci_ecam_mmio_{read,write} functions are returning 0 on error and 1 on success. As the x86 code was previously always returning X86EMUL_OKAY the return code is ignored. A comment has been added in the code to show that this is intentional. Those functions will be used in a following patch inside by arm vpci implementation. Rename MMCFG_SBDF to ECAM_SBDF. Not functional change intended with this patch. [1] https://wiki.osdev.org/PCI_Express Suggested-by: Roger Pau Monn=C3=A9 Signed-off-by: Bertrand Marquis Reviewed-by: Stefano Stabellini --- Changes in v6: Patch added --- xen/arch/x86/hvm/io.c | 50 +++++--------------------------- xen/drivers/vpci/vpci.c | 60 +++++++++++++++++++++++++++++++++++++++ xen/include/asm-x86/pci.h | 2 +- xen/include/xen/vpci.h | 10 +++++++ 4 files changed, 78 insertions(+), 44 deletions(-) diff --git a/xen/arch/x86/hvm/io.c b/xen/arch/x86/hvm/io.c index 046a8eb4ed..340b8c8b0e 100644 --- a/xen/arch/x86/hvm/io.c +++ b/xen/arch/x86/hvm/io.c @@ -260,20 +260,6 @@ unsigned int hvm_pci_decode_addr(unsigned int cf8, uns= igned int addr, return CF8_ADDR_LO(cf8) | (addr & 3); } =20 -/* Do some sanity checks. */ -static bool vpci_access_allowed(unsigned int reg, unsigned int len) -{ - /* Check access size. */ - if ( len !=3D 1 && len !=3D 2 && len !=3D 4 && len !=3D 8 ) - return false; - - /* Check that access is size aligned. */ - if ( (reg & (len - 1)) ) - return false; - - return true; -} - /* vPCI config space IO ports handlers (0xcf8/0xcfc). */ static bool vpci_portio_accept(const struct hvm_io_handler *handler, const ioreq_t *p) @@ -305,7 +291,7 @@ static int vpci_portio_read(const struct hvm_io_handler= *handler, =20 reg =3D hvm_pci_decode_addr(cf8, addr, &sbdf); =20 - if ( !vpci_access_allowed(reg, size) ) + if ( !vpci_ecam_access_allowed(reg, size) ) return X86EMUL_OKAY; =20 *data =3D vpci_read(sbdf, reg, size); @@ -335,7 +321,7 @@ static int vpci_portio_write(const struct hvm_io_handle= r *handler, =20 reg =3D hvm_pci_decode_addr(cf8, addr, &sbdf); =20 - if ( !vpci_access_allowed(reg, size) ) + if ( !vpci_ecam_access_allowed(reg, size) ) return X86EMUL_OKAY; =20 vpci_write(sbdf, reg, size, data); @@ -394,7 +380,7 @@ static unsigned int vpci_mmcfg_decode_addr(const struct= hvm_mmcfg *mmcfg, paddr_t addr, pci_sbdf_t *sbdf) { addr -=3D mmcfg->addr; - sbdf->bdf =3D MMCFG_BDF(addr); + sbdf->bdf =3D ECAM_BDF(addr); sbdf->bus +=3D mmcfg->start_bus; sbdf->seg =3D mmcfg->segment; =20 @@ -434,25 +420,8 @@ static int vpci_mmcfg_read(struct vcpu *v, unsigned lo= ng addr, reg =3D vpci_mmcfg_decode_addr(mmcfg, addr, &sbdf); read_unlock(&d->arch.hvm.mmcfg_lock); =20 - if ( !vpci_access_allowed(reg, len) || - (reg + len) > PCI_CFG_SPACE_EXP_SIZE ) - return X86EMUL_OKAY; - - /* - * According to the PCIe 3.1A specification: - * - Configuration Reads and Writes must usually be DWORD or smaller - * in size. - * - Because Root Complex implementations are not required to support - * accesses to a RCRB that cross DW boundaries [...] software - * should take care not to cause the generation of such accesses - * when accessing a RCRB unless the Root Complex will support the - * access. - * Xen however supports 8byte accesses by splitting them into two - * 4byte accesses. - */ - *data =3D vpci_read(sbdf, reg, min(4u, len)); - if ( len =3D=3D 8 ) - *data |=3D (uint64_t)vpci_read(sbdf, reg + 4, 4) << 32; + /* Ignore return code */ + vpci_ecam_mmio_read(sbdf, reg, len, data); =20 return X86EMUL_OKAY; } @@ -476,13 +445,8 @@ static int vpci_mmcfg_write(struct vcpu *v, unsigned l= ong addr, reg =3D vpci_mmcfg_decode_addr(mmcfg, addr, &sbdf); read_unlock(&d->arch.hvm.mmcfg_lock); =20 - if ( !vpci_access_allowed(reg, len) || - (reg + len) > PCI_CFG_SPACE_EXP_SIZE ) - return X86EMUL_OKAY; - - vpci_write(sbdf, reg, min(4u, len), data); - if ( len =3D=3D 8 ) - vpci_write(sbdf, reg + 4, 4, data >> 32); + /* Ignore return code */ + vpci_ecam_mmio_write(sbdf, reg, len, data); =20 return X86EMUL_OKAY; } diff --git a/xen/drivers/vpci/vpci.c b/xen/drivers/vpci/vpci.c index cbd1bac7fc..c0853176d7 100644 --- a/xen/drivers/vpci/vpci.c +++ b/xen/drivers/vpci/vpci.c @@ -478,6 +478,66 @@ void vpci_write(pci_sbdf_t sbdf, unsigned int reg, uns= igned int size, spin_unlock(&pdev->vpci->lock); } =20 +/* Helper function to check an access size and alignment on vpci space. */ +bool vpci_ecam_access_allowed(unsigned int reg, unsigned int len) +{ + /* + * Check access size. + * + * On arm32 or for 32bit guests on arm, 64bit accesses should be forbi= dden + * but as for those platform ISV register, which gives the access size, + * cannot have a value 3, checking this would just harden the code. + */ + if ( len !=3D 1 && len !=3D 2 && len !=3D 4 && len !=3D 8 ) + return false; + + /* Check that access is size aligned. */ + if ( (reg & (len - 1)) ) + return false; + + return true; +} + +int vpci_ecam_mmio_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int l= en, + unsigned long data) +{ + if ( !vpci_ecam_access_allowed(reg, len) || + (reg + len) > PCI_CFG_SPACE_EXP_SIZE ) + return 0; + + vpci_write(sbdf, reg, min(4u, len), data); + if ( len =3D=3D 8 ) + vpci_write(sbdf, reg + 4, 4, data >> 32); + + return 1; +} + +int vpci_ecam_mmio_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int le= n, + unsigned long *data) +{ + if ( !vpci_ecam_access_allowed(reg, len) || + (reg + len) > PCI_CFG_SPACE_EXP_SIZE ) + return 0; + + /* + * According to the PCIe 3.1A specification: + * - Configuration Reads and Writes must usually be DWORD or smaller + * in size. + * - Because Root Complex implementations are not required to support + * accesses to a RCRB that cross DW boundaries [...] software + * should take care not to cause the generation of such accesses + * when accessing a RCRB unless the Root Complex will support the + * access. + * Xen however supports 8byte accesses by splitting them into two + * 4byte accesses. + */ + *data =3D vpci_read(sbdf, reg, min(4u, len)); + if ( len =3D=3D 8 ) + *data |=3D (uint64_t)vpci_read(sbdf, reg + 4, 4) << 32; + + return 1; +} + /* * Local variables: * mode: C diff --git a/xen/include/asm-x86/pci.h b/xen/include/asm-x86/pci.h index edd7c3e71a..a0df5c1279 100644 --- a/xen/include/asm-x86/pci.h +++ b/xen/include/asm-x86/pci.h @@ -6,7 +6,7 @@ #define CF8_ADDR_HI(cf8) ( ((cf8) & 0x0f000000) >> 16) #define CF8_ENABLED(cf8) (!!((cf8) & 0x80000000)) =20 -#define MMCFG_BDF(addr) ( ((addr) & 0x0ffff000) >> 12) +#define ECAM_BDF(addr) ( ((addr) & 0x0ffff000) >> 12) =20 #define IS_SNB_GFX(id) (id =3D=3D 0x01068086 || id =3D=3D 0x01168086 \ || id =3D=3D 0x01268086 || id =3D=3D 0x01028086 \ diff --git a/xen/include/xen/vpci.h b/xen/include/xen/vpci.h index 9f5b5d52e1..4a0c3d77c9 100644 --- a/xen/include/xen/vpci.h +++ b/xen/include/xen/vpci.h @@ -208,6 +208,16 @@ static inline unsigned int vmsix_entry_nr(const struct= vpci_msix *msix, { return entry - msix->entries; } + +/* ECAM mmio read/write helpers */ +bool vpci_ecam_access_allowed(unsigned int reg, unsigned int len); + +int vpci_ecam_mmio_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int l= en, + unsigned long data); + +int vpci_ecam_mmio_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int le= n, + unsigned long *data); + #endif /* __XEN__ */ =20 #else /* !CONFIG_HAS_VPCI */ --=20 2.25.1 From nobody Sat May 4 15:11:46 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=arm.com Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1634223034638992.6424925970296; Thu, 14 Oct 2021 07:50:34 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.209435.365913 (Exim 4.92) (envelope-from ) id 1mb23n-00067V-NK; Thu, 14 Oct 2021 14:50:19 +0000 Received: by outflank-mailman (output) from mailman id 209435.365913; Thu, 14 Oct 2021 14:50:19 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mb23n-00067M-Jl; Thu, 14 Oct 2021 14:50:19 +0000 Received: by outflank-mailman (input) for mailman id 209435; Thu, 14 Oct 2021 14:50:18 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mb23m-000615-3q for xen-devel@lists.xenproject.org; Thu, 14 Oct 2021 14:50:18 +0000 Received: from foss.arm.com (unknown [217.140.110.172]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTP id 0bd4d608-2cfe-11ec-81c4-12813bfff9fa; Thu, 14 Oct 2021 14:50:17 +0000 (UTC) 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 1472C1480; Thu, 14 Oct 2021 07:50:17 -0700 (PDT) Received: from e109506.cambridge.arm.com (e109506.cambridge.arm.com [10.1.199.62]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 67C553F694; Thu, 14 Oct 2021 07:50:15 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 0bd4d608-2cfe-11ec-81c4-12813bfff9fa From: Bertrand Marquis To: xen-devel@lists.xenproject.org Cc: iwj@xenproject.org, Rahul Singh , Stefano Stabellini , Julien Grall , Volodymyr Babchuk , Andrew Cooper , George Dunlap , Jan Beulich , Wei Liu , Paul Durrant , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Subject: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM. Date: Thu, 14 Oct 2021 15:49:50 +0100 Message-Id: <396b08e2598cf0338e0c7f4ad3cd5cb66db89224.1634221830.git.bertrand.marquis@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1634223035412100002 From: Rahul Singh The existing VPCI support available for X86 is adapted for Arm. When the device is added to XEN via the hyper call =E2=80=9CPHYSDEVOP_pci_device_add=E2=80=9D, VPCI handler for the config spa= ce access is added to the Xen to emulate the PCI devices config space. A MMIO trap handler for the PCI ECAM space is registered in XEN so that when guest is trying to access the PCI config space,XEN will trap the access and emulate read/write using the VPCI and not the real PCI hardware. For Dom0less systems scan_pci_devices() would be used to discover the PCI device in XEN and VPCI handler will be added during XEN boots. This patch is also doing some small fixes to fix compilation errors on arm32 of vpci: - add a cast to unsigned long in print in header.c - add a cast to uint64_t in vpci_ecam_mmio_write Signed-off-by: Rahul Singh Signed-off-by: Bertrand Marquis Reviewed-by: Stefano Stabellini --- Changes in v6: - Use new vpci_ecam_ helpers for PCI access - Do not set XEN_DOMCTL_CDF_vpci for dom0 for now (will be done in a future patch once everything is ready) - rename REGISTER_OFFSET to ECAM_REG_OFFSET and move it to pci.h - remove not needed local variables in vpci_mmio_write, the one in read has been kept to ensure proper compilation on arm32 - move call to vpci_add_handlers before iommu init to simplify exit path - move call to pci_cleanup_msi in the out section of pci_add_device if pdev is not NULL and on error - initialize pdev to NULL to handle properly exit path call of pci_cleanup_msi - keep has_vpci to return false for now as CFG_vpci has been removed. Added a comment on top of the definition. - fix compilation errors on arm32 (print in header.c, cast missing in mmio_write. - local variable was kept in vpci_mmio_read on arm to prevent a cast error in arm32. Change in v5: - Add pci_cleanup_msi(pdev) incleanup part. - Added Reviewed-by: Stefano Stabellini Change in v4: - Move addition of XEN_DOMCTL_CDF_vpci flag to separate patch Change in v3: - Use is_pci_passthrough_enabled() in place of pci_passthrough_enabled vari= able - Reject XEN_DOMCTL_CDF_vpci for x86 in arch_sanitise_domain_config() - Remove IS_ENABLED(CONFIG_HAS_VPCI) from has_vpci() Change in v2: - Add new XEN_DOMCTL_CDF_vpci flag - modify has_vpci() to include XEN_DOMCTL_CDF_vpci - enable vpci support when pci-passthough option is enabled. --- --- xen/arch/arm/Makefile | 1 + xen/arch/arm/domain.c | 4 ++ xen/arch/arm/vpci.c | 74 +++++++++++++++++++++++++++++++++++ xen/arch/arm/vpci.h | 36 +++++++++++++++++ xen/drivers/passthrough/pci.c | 18 ++++++++- xen/drivers/vpci/header.c | 3 +- xen/drivers/vpci/vpci.c | 2 +- xen/include/asm-arm/domain.h | 1 + xen/include/asm-x86/pci.h | 2 - xen/include/public/arch-arm.h | 7 ++++ xen/include/xen/pci.h | 3 ++ 11 files changed, 146 insertions(+), 5 deletions(-) create mode 100644 xen/arch/arm/vpci.c create mode 100644 xen/arch/arm/vpci.h diff --git a/xen/arch/arm/Makefile b/xen/arch/arm/Makefile index 64518848b2..07f634508e 100644 --- a/xen/arch/arm/Makefile +++ b/xen/arch/arm/Makefile @@ -7,6 +7,7 @@ ifneq ($(CONFIG_NO_PLAT),y) obj-y +=3D platforms/ endif obj-$(CONFIG_TEE) +=3D tee/ +obj-$(CONFIG_HAS_VPCI) +=3D vpci.o =20 obj-$(CONFIG_HAS_ALTERNATIVE) +=3D alternative.o obj-y +=3D bootfdt.init.o diff --git a/xen/arch/arm/domain.c b/xen/arch/arm/domain.c index eef0661beb..96e1b23550 100644 --- a/xen/arch/arm/domain.c +++ b/xen/arch/arm/domain.c @@ -39,6 +39,7 @@ #include #include =20 +#include "vpci.h" #include "vuart.h" =20 DEFINE_PER_CPU(struct vcpu *, curr_vcpu); @@ -773,6 +774,9 @@ int arch_domain_create(struct domain *d, if ( is_hardware_domain(d) && (rc =3D domain_vuart_init(d)) ) goto fail; =20 + if ( (rc =3D domain_vpci_init(d)) !=3D 0 ) + goto fail; + return 0; =20 fail: diff --git a/xen/arch/arm/vpci.c b/xen/arch/arm/vpci.c new file mode 100644 index 0000000000..7c3552b65d --- /dev/null +++ b/xen/arch/arm/vpci.c @@ -0,0 +1,74 @@ +/* + * xen/arch/arm/vpci.c + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ +#include +#include + +#include + +static int vpci_mmio_read(struct vcpu *v, mmio_info_t *info, + register_t *r, void *p) +{ + pci_sbdf_t sbdf; + /* data is needed to prevent a pointer cast on 32bit */ + unsigned long data =3D ~0ul; + int ret; + + /* We ignore segment part and always handle segment 0 */ + sbdf.sbdf =3D ECAM_BDF(info->gpa); + + ret =3D vpci_ecam_mmio_read(sbdf, ECAM_REG_OFFSET(info->gpa), + 1U << info->dabt.size, &data); + + *r =3D data; + + return ret; +} + +static int vpci_mmio_write(struct vcpu *v, mmio_info_t *info, + register_t r, void *p) +{ + pci_sbdf_t sbdf; + + /* We ignore segment part and always handle segment 0 */ + sbdf.sbdf =3D ECAM_BDF(info->gpa); + + return vpci_ecam_mmio_write(sbdf, ECAM_REG_OFFSET(info->gpa), + 1U << info->dabt.size, r); +} + +static const struct mmio_handler_ops vpci_mmio_handler =3D { + .read =3D vpci_mmio_read, + .write =3D vpci_mmio_write, +}; + +int domain_vpci_init(struct domain *d) +{ + if ( !has_vpci(d) ) + return 0; + + register_mmio_handler(d, &vpci_mmio_handler, + GUEST_VPCI_ECAM_BASE, GUEST_VPCI_ECAM_SIZE, NULL= ); + + return 0; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ + diff --git a/xen/arch/arm/vpci.h b/xen/arch/arm/vpci.h new file mode 100644 index 0000000000..d8a7b0e3e8 --- /dev/null +++ b/xen/arch/arm/vpci.h @@ -0,0 +1,36 @@ +/* + * xen/arch/arm/vpci.h + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef __ARCH_ARM_VPCI_H__ +#define __ARCH_ARM_VPCI_H__ + +#ifdef CONFIG_HAS_VPCI +int domain_vpci_init(struct domain *d); +#else +static inline int domain_vpci_init(struct domain *d) +{ + return 0; +} +#endif + +#endif /* __ARCH_ARM_VPCI_H__ */ + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/drivers/passthrough/pci.c b/xen/drivers/passthrough/pci.c index 3aa8c3175f..8cc529ecec 100644 --- a/xen/drivers/passthrough/pci.c +++ b/xen/drivers/passthrough/pci.c @@ -658,7 +658,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn, const struct pci_dev_info *info, nodeid_t node) { struct pci_seg *pseg; - struct pci_dev *pdev; + struct pci_dev *pdev =3D NULL; unsigned int slot =3D PCI_SLOT(devfn), func =3D PCI_FUNC(devfn); const char *pdev_type; int ret; @@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn, =20 check_pdev(pdev); =20 +#ifdef CONFIG_ARM + /* + * On ARM PCI devices discovery will be done by Dom0. Add vpci handler= when + * Dom0 inform XEN to add the PCI devices in XEN. + */ + ret =3D vpci_add_handlers(pdev); + if ( ret ) + { + printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret); + goto out; + } +#endif + ret =3D 0; if ( !pdev->domain ) { @@ -784,6 +797,9 @@ out: &PCI_SBDF(seg, bus, slot, func)); } } + else if ( pdev ) + pci_cleanup_msi(pdev); + return ret; } =20 diff --git a/xen/drivers/vpci/header.c b/xen/drivers/vpci/header.c index f8cd55e7c0..c5b025b88b 100644 --- a/xen/drivers/vpci/header.c +++ b/xen/drivers/vpci/header.c @@ -374,7 +374,8 @@ static void bar_write(const struct pci_dev *pdev, unsig= ned int reg, if ( val !=3D (uint32_t)(bar->addr >> (hi ? 32 : 0)) ) gprintk(XENLOG_WARNING, "%pp: ignored BAR %lu write with memory decoding enabl= ed\n", - &pdev->sbdf, bar - pdev->vpci->header.bars + hi); + &pdev->sbdf, + (unsigned long)(bar - pdev->vpci->header.bars + hi)); return; } =20 diff --git a/xen/drivers/vpci/vpci.c b/xen/drivers/vpci/vpci.c index c0853176d7..2bd67fc27a 100644 --- a/xen/drivers/vpci/vpci.c +++ b/xen/drivers/vpci/vpci.c @@ -507,7 +507,7 @@ int vpci_ecam_mmio_write(pci_sbdf_t sbdf, unsigned int = reg, unsigned int len, =20 vpci_write(sbdf, reg, min(4u, len), data); if ( len =3D=3D 8 ) - vpci_write(sbdf, reg + 4, 4, data >> 32); + vpci_write(sbdf, reg + 4, 4, (uint64_t)data >> 32); =20 return 1; } diff --git a/xen/include/asm-arm/domain.h b/xen/include/asm-arm/domain.h index 14e575288f..9b3647587a 100644 --- a/xen/include/asm-arm/domain.h +++ b/xen/include/asm-arm/domain.h @@ -263,6 +263,7 @@ static inline void arch_vcpu_block(struct vcpu *v) {} =20 #define arch_vm_assist_valid_mask(d) (1UL << VMASST_TYPE_runstate_update_f= lag) =20 +/* vPCI is not available on Arm */ #define has_vpci(d) ({ (void)(d); false; }) =20 #endif /* __ASM_DOMAIN_H__ */ diff --git a/xen/include/asm-x86/pci.h b/xen/include/asm-x86/pci.h index a0df5c1279..443f25347d 100644 --- a/xen/include/asm-x86/pci.h +++ b/xen/include/asm-x86/pci.h @@ -6,8 +6,6 @@ #define CF8_ADDR_HI(cf8) ( ((cf8) & 0x0f000000) >> 16) #define CF8_ENABLED(cf8) (!!((cf8) & 0x80000000)) =20 -#define ECAM_BDF(addr) ( ((addr) & 0x0ffff000) >> 12) - #define IS_SNB_GFX(id) (id =3D=3D 0x01068086 || id =3D=3D 0x01168086 \ || id =3D=3D 0x01268086 || id =3D=3D 0x01028086 \ || id =3D=3D 0x01128086 || id =3D=3D 0x01228086 \ diff --git a/xen/include/public/arch-arm.h b/xen/include/public/arch-arm.h index d46c61fca9..44be337dec 100644 --- a/xen/include/public/arch-arm.h +++ b/xen/include/public/arch-arm.h @@ -418,6 +418,13 @@ typedef uint64_t xen_callback_t; #define GUEST_GICV3_GICR0_BASE xen_mk_ullong(0x03020000) /* vCPU0..127= */ #define GUEST_GICV3_GICR0_SIZE xen_mk_ullong(0x01000000) =20 +/* + * 256 MB is reserved for VPCI configuration space based on calculation + * 256 buses =C3=97 32 devices =C3=97 8 functions =C3=97 4 KB =3D 256 MB + */ +#define GUEST_VPCI_ECAM_BASE xen_mk_ullong(0x10000000) +#define GUEST_VPCI_ECAM_SIZE xen_mk_ullong(0x10000000) + /* ACPI tables physical address */ #define GUEST_ACPI_BASE xen_mk_ullong(0x20000000) #define GUEST_ACPI_SIZE xen_mk_ullong(0x02000000) diff --git a/xen/include/xen/pci.h b/xen/include/xen/pci.h index 70ac25345c..db18cb7639 100644 --- a/xen/include/xen/pci.h +++ b/xen/include/xen/pci.h @@ -40,6 +40,9 @@ #define PCI_SBDF3(s,b,df) \ ((pci_sbdf_t){ .sbdf =3D (((s) & 0xffff) << 16) | PCI_BDF2(b, df) }) =20 +#define ECAM_BDF(addr) ( ((addr) & 0x0ffff000) >> 12) +#define ECAM_REG_OFFSET(addr) ((addr) & 0x00000fff) + typedef union { uint32_t sbdf; struct { --=20 2.25.1 From nobody Sat May 4 15:11:46 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=arm.com Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 163422303543626.413695247389683; Thu, 14 Oct 2021 07:50:35 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.209436.365924 (Exim 4.92) (envelope-from ) id 1mb23p-0006Px-5x; Thu, 14 Oct 2021 14:50:21 +0000 Received: by outflank-mailman (output) from mailman id 209436.365924; Thu, 14 Oct 2021 14:50:21 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mb23p-0006Po-2G; Thu, 14 Oct 2021 14:50:21 +0000 Received: by outflank-mailman (input) for mailman id 209436; Thu, 14 Oct 2021 14:50:20 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mb23o-0005ov-Jd for xen-devel@lists.xenproject.org; Thu, 14 Oct 2021 14:50:20 +0000 Received: from foss.arm.com (unknown [217.140.110.172]) by us1-rack-iad1.inumbo.com (Halon) with ESMTP id f5207465-5608-40ae-a030-19d7c318afbc; Thu, 14 Oct 2021 14:50:18 +0000 (UTC) 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 99A6AD6E; Thu, 14 Oct 2021 07:50:18 -0700 (PDT) Received: from e109506.cambridge.arm.com (e109506.cambridge.arm.com [10.1.199.62]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 493633F694; Thu, 14 Oct 2021 07:50:17 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: f5207465-5608-40ae-a030-19d7c318afbc From: Bertrand Marquis To: xen-devel@lists.xenproject.org Cc: iwj@xenproject.org, Rahul Singh , Wei Liu , Anthony PERARD , Juergen Gross , Stefano Stabellini , Julien Grall , Volodymyr Babchuk , Michal Orzel Subject: [PATCH v6 3/3] arm/libxl: Emulated PCI device tree node in libxl Date: Thu, 14 Oct 2021 15:49:51 +0100 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1634223037440100007 Content-Type: text/plain; charset="utf-8" From: Rahul Singh libxl will create an emulated PCI device tree node in the device tree to enable the guest OS to discover the virtual PCI during guest boot. Emulated PCI device tree node will only be created when there is any device assigned to guest. A new area has been reserved in the arm guest physical map at which the VPCI bus is declared in the device tree (reg and ranges parameters of the node). b_info.arch_arm.vpci is set by default to false and it is not set to true anywhere. This way the vpci node is not going to be created and we are only introducing functions to create vpci DT node to be used in the future. Signed-off-by: Rahul Singh Signed-off-by: Michal Orzel --- Changes in v6: According to https://marc.info/?l=3Dxen-devel&m=3D163415838129479&w=3D2: -do not set XEN_DOMCTL_CDF_vpci -do not enable VPCI support (by setting b_info.arch_arm.vpci) -do not define LIBXL_HAVE_BUILDINFO_ARM_VPCI -keep b_info.arch_arm.vpci, make_vpci_node and its helpers Change in v5: - Move setting the arch_arm.vpci and XEN_DOMCTL_CDF_vpci to libxl_arm.c Change in v4: - Gate code for x86 for setting the XEN_DOMCTL_CDF_vpci for x86. Change in v3: - Make GUEST_VPCI_MEM_ADDR address 2MB aligned Change in v2: - enable doamin_vpci_init() when XEN_DOMCTL_CDF_vpci is set for domain. --- tools/libs/light/libxl_arm.c | 105 +++++++++++++++++++++++++++++++ tools/libs/light/libxl_types.idl | 1 + xen/include/public/arch-arm.h | 10 +++ 3 files changed, 116 insertions(+) diff --git a/tools/libs/light/libxl_arm.c b/tools/libs/light/libxl_arm.c index e3140a6e00..52f1ddce48 100644 --- a/tools/libs/light/libxl_arm.c +++ b/tools/libs/light/libxl_arm.c @@ -269,6 +269,58 @@ static int fdt_property_regs(libxl__gc *gc, void *fdt, return fdt_property(fdt, "reg", regs, sizeof(regs)); } =20 +static int fdt_property_values(libxl__gc *gc, void *fdt, + const char *name, unsigned num_cells, ...) +{ + uint32_t prop[num_cells]; + be32 *cells =3D &prop[0]; + int i; + va_list ap; + uint32_t arg; + + va_start(ap, num_cells); + for (i =3D 0 ; i < num_cells; i++) { + arg =3D va_arg(ap, uint32_t); + set_cell(&cells, 1, arg); + } + va_end(ap); + + return fdt_property(fdt, name, prop, sizeof(prop)); +} + +static int fdt_property_vpci_ranges(libxl__gc *gc, void *fdt, + unsigned addr_cells, + unsigned size_cells, + unsigned num_regs, ...) +{ + uint32_t regs[num_regs*((addr_cells*2)+size_cells+1)]; + be32 *cells =3D ®s[0]; + int i; + va_list ap; + uint64_t arg; + + va_start(ap, num_regs); + for (i =3D 0 ; i < num_regs; i++) { + /* Set the memory bit field */ + arg =3D va_arg(ap, uint32_t); + set_cell(&cells, 1, arg); + + /* Set the vpci bus address */ + arg =3D addr_cells ? va_arg(ap, uint64_t) : 0; + set_cell(&cells, addr_cells , arg); + + /* Set the cpu bus address where vpci address is mapped */ + set_cell(&cells, addr_cells, arg); + + /* Set the vpci size requested */ + arg =3D size_cells ? va_arg(ap, uint64_t) : 0; + set_cell(&cells, size_cells, arg); + } + va_end(ap); + + return fdt_property(fdt, "ranges", regs, sizeof(regs)); +} + static int make_root_properties(libxl__gc *gc, const libxl_version_info *vers, void *fdt) @@ -668,6 +720,53 @@ static int make_vpl011_uart_node(libxl__gc *gc, void *= fdt, return 0; } =20 +static int make_vpci_node(libxl__gc *gc, void *fdt, + const struct arch_info *ainfo, + struct xc_dom_image *dom) +{ + int res; + const uint64_t vpci_ecam_base =3D GUEST_VPCI_ECAM_BASE; + const uint64_t vpci_ecam_size =3D GUEST_VPCI_ECAM_SIZE; + const char *name =3D GCSPRINTF("pcie@%"PRIx64, vpci_ecam_base); + + res =3D fdt_begin_node(fdt, name); + if (res) return res; + + res =3D fdt_property_compat(gc, fdt, 1, "pci-host-ecam-generic"); + if (res) return res; + + res =3D fdt_property_string(fdt, "device_type", "pci"); + if (res) return res; + + res =3D fdt_property_regs(gc, fdt, GUEST_ROOT_ADDRESS_CELLS, + GUEST_ROOT_SIZE_CELLS, 1, vpci_ecam_base, vpci_ecam_size); + if (res) return res; + + res =3D fdt_property_values(gc, fdt, "bus-range", 2, 0, 255); + if (res) return res; + + res =3D fdt_property_cell(fdt, "#address-cells", 3); + if (res) return res; + + res =3D fdt_property_cell(fdt, "#size-cells", 2); + if (res) return res; + + res =3D fdt_property_string(fdt, "status", "okay"); + if (res) return res; + + res =3D fdt_property_vpci_ranges(gc, fdt, GUEST_ROOT_ADDRESS_CELLS, + GUEST_ROOT_SIZE_CELLS, 2, + GUEST_VPCI_ADDR_TYPE_MEM, GUEST_VPCI_MEM_ADDR, GUEST_VPCI_MEM_SIZE, + GUEST_VPCI_ADDR_TYPE_PREFETCH_MEM, GUEST_VPCI_PREFETCH_MEM_ADDR, + GUEST_VPCI_PREFETCH_MEM_SIZE); + if (res) return res; + + res =3D fdt_end_node(fdt); + if (res) return res; + + return 0; +} + static const struct arch_info *get_arch_info(libxl__gc *gc, const struct xc_dom_image *do= m) { @@ -971,6 +1070,9 @@ next_resize: if (info->tee =3D=3D LIBXL_TEE_TYPE_OPTEE) FDT( make_optee_node(gc, fdt) ); =20 + if (libxl_defbool_val(info->arch_arm.vpci)) + FDT( make_vpci_node(gc, fdt, ainfo, dom) ); + if (pfdt) FDT( copy_partial_fdt(gc, fdt, pfdt) ); =20 @@ -1189,6 +1291,9 @@ void libxl__arch_domain_build_info_setdefault(libxl__= gc *gc, /* ACPI is disabled by default */ libxl_defbool_setdefault(&b_info->acpi, false); =20 + /* VPCI is disabled by default */ + libxl_defbool_setdefault(&b_info->arch_arm.vpci, false); + if (b_info->type !=3D LIBXL_DOMAIN_TYPE_PV) return; =20 diff --git a/tools/libs/light/libxl_types.idl b/tools/libs/light/libxl_type= s.idl index b96fb5c47e..61418ef6eb 100644 --- a/tools/libs/light/libxl_types.idl +++ b/tools/libs/light/libxl_types.idl @@ -644,6 +644,7 @@ libxl_domain_build_info =3D Struct("domain_build_info",[ =20 ("arch_arm", Struct(None, [("gic_version", libxl_gic_version), ("vuart", libxl_vuart_type), + ("vpci", libxl_defbool), ])), ("arch_x86", Struct(None, [("msr_relaxed", libxl_defbool), ])), diff --git a/xen/include/public/arch-arm.h b/xen/include/public/arch-arm.h index 44be337dec..45aac5d18f 100644 --- a/xen/include/public/arch-arm.h +++ b/xen/include/public/arch-arm.h @@ -433,6 +433,11 @@ typedef uint64_t xen_callback_t; #define GUEST_PL011_BASE xen_mk_ullong(0x22000000) #define GUEST_PL011_SIZE xen_mk_ullong(0x00001000) =20 +/* Guest PCI-PCIe memory space where config space and BAR will be availabl= e.*/ +#define GUEST_VPCI_ADDR_TYPE_MEM xen_mk_ullong(0x02000000) +#define GUEST_VPCI_MEM_ADDR xen_mk_ullong(0x23000000) +#define GUEST_VPCI_MEM_SIZE xen_mk_ullong(0x10000000) + /* * 16MB =3D=3D 4096 pages reserved for guest to use as a region to map its * grant table in. @@ -448,6 +453,11 @@ typedef uint64_t xen_callback_t; #define GUEST_RAM0_BASE xen_mk_ullong(0x40000000) /* 3GB of low RAM @ 1G= B */ #define GUEST_RAM0_SIZE xen_mk_ullong(0xc0000000) =20 +/* 4GB @ 4GB Prefetch Memory for VPCI */ +#define GUEST_VPCI_ADDR_TYPE_PREFETCH_MEM xen_mk_ullong(0x42000000) +#define GUEST_VPCI_PREFETCH_MEM_ADDR xen_mk_ullong(0x100000000) +#define GUEST_VPCI_PREFETCH_MEM_SIZE xen_mk_ullong(0x100000000) + #define GUEST_RAM1_BASE xen_mk_ullong(0x0200000000) /* 1016GB of RAM @ 8= GB */ #define GUEST_RAM1_SIZE xen_mk_ullong(0xfe00000000) =20 --=20 2.25.1