From nobody Tue Feb 10 02:00:22 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; arc=pass (i=1 dmarc=pass fromdomain=amd.com); dmarc=pass(p=quarantine dis=none) header.from=amd.com ARC-Seal: i=2; a=rsa-sha256; t=1701200721; cv=pass; d=zohomail.com; s=zohoarc; b=OwHNiSEs3ZMXmrNCrlga2R1jYt+vAyzFOsQoHoVxdHbA4O3e7bG8ljCyGXOWS9MMWA+Kj3/45gGoD3YoJ0dsu9MfLkTa8uorOwjCFZPWiTNCDbfPqAFWgbu9iaIJRg2Gp6mRf7XOrEqZSlQnZuPsRtcjj4UbEazNPtp6pEGWhNA= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1701200721; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=1BVDdM0ntVTlJW1zzT07NjWnqB72PNmuaW7nBCP92pk=; b=TxbiNWLsX+Ry891wouymS6ySQEXqFsUnROYa7QVfqn+J5vX8BJzOjGyQ9FeVm3PLebImXObsKD62Od0+Jrx1UDcEKMCM26xFGZUPaDN1Ys9G22z3GBsqZPqRBst5IagB5lrD+BwZ0w+077wSenKQAU2FIpyhJJ0PDNW4BpYqsSo= ARC-Authentication-Results: i=2; mx.zohomail.com; dkim=pass; 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; arc=pass (i=1 dmarc=pass fromdomain=amd.com); dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1701200721796253.63510890205396; Tue, 28 Nov 2023 11:45:21 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.643448.1003681 (Exim 4.92) (envelope-from ) id 1r840w-0005Tj-Iw; Tue, 28 Nov 2023 19:44:58 +0000 Received: by outflank-mailman (output) from mailman id 643448.1003681; Tue, 28 Nov 2023 19:44:58 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r840w-0005Tc-G8; Tue, 28 Nov 2023 19:44:58 +0000 Received: by outflank-mailman (input) for mailman id 643448; Tue, 28 Nov 2023 19:44:56 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r840u-0005Sf-L2 for xen-devel@lists.xenproject.org; Tue, 28 Nov 2023 19:44:56 +0000 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2060f.outbound.protection.outlook.com [2a01:111:f400:7e8a::60f]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 99d3d679-8e26-11ee-98e3-6d05b1d4d9a1; Tue, 28 Nov 2023 20:44:54 +0100 (CET) Received: from DS7PR03CA0317.namprd03.prod.outlook.com (2603:10b6:8:2b::20) by LV3PR12MB9142.namprd12.prod.outlook.com (2603:10b6:408:198::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7025.29; Tue, 28 Nov 2023 19:44:50 +0000 Received: from DS3PEPF000099DD.namprd04.prod.outlook.com (2603:10b6:8:2b:cafe::20) by DS7PR03CA0317.outlook.office365.com (2603:10b6:8:2b::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7046.22 via Frontend Transport; Tue, 28 Nov 2023 19:44:49 +0000 Received: from SATLEXMB03.amd.com (165.204.84.17) by DS3PEPF000099DD.mail.protection.outlook.com (10.167.17.199) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7046.17 via Frontend Transport; Tue, 28 Nov 2023 19:44:49 +0000 Received: from SATLEXMB06.amd.com (10.181.40.147) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Tue, 28 Nov 2023 13:44:49 -0600 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB06.amd.com (10.181.40.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Tue, 28 Nov 2023 13:44:48 -0600 Received: from ubuntu.mshome.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.34 via Frontend Transport; Tue, 28 Nov 2023 13:44:47 -0600 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: 99d3d679-8e26-11ee-98e3-6d05b1d4d9a1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=P5mSnHVqhSMNlhsYtI8Y2wkyHrJd/v9eiRAWALvn0d//T+NV28kXBBmqsezi6XeWllliVZQrsTY/skNrxFMatX+enGTnX/gqq4Vr9Te7IBjYwM0OyK/iETW7kueBiLrpz4Eg+Lan+KQ8Y5CFkrUU1Dky715b3B7TnNPK5Da6Q5VSZlAV/rrTcFO11xwj3NNY/9nrZOy8cNA8VdtoHZgWOF2W/SNhQjvAMpiv0fvkDz1jHfLqQ6OlvGSmdKiPlgnvuGkrd8YGbP5Ebx4yVfHYbtppIqP89efWtXNJGmPSddh1sTOJcgqT4zdSluLDlA443EPEVTvaDOHd0MwDV+7C2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=1BVDdM0ntVTlJW1zzT07NjWnqB72PNmuaW7nBCP92pk=; b=ZeDj3ZU3UQLJO/SLoTTFb7XGsdGI1HH76sagrDuSWe6mrJOsM39lyPZ8OIWEJfTp4f+TdRZSWRj4Gr6GqWAqOk17/nn88C1ESDyyTl6IF4JlGHaUI8cFH/7LPaHS5jkMbEKN2medGCbUSg7l1+ZwNbtaBgjHZmJsOd33L61EjtPa9OJ5oc1BS/nbuEa8LBkMdKc289EG8n8yRojb8v/xDeU8emC0LWn48+XVWAJM9tyR+6vTpwXBKmCLv7QxRHD/+6c0LKkmElWsfAo0tIB3PcPHEKYaz6ytBFUr0wXrUzHiFCwWI+w0nP01+ra+5QNjOSsD2J8/tAoW0roPLGBZEA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1BVDdM0ntVTlJW1zzT07NjWnqB72PNmuaW7nBCP92pk=; b=2SaqMq9kETh1J0/mV3siMxquYy3MYuqaXV5MjBWdSn8l6Lx06byClJIoWKhf/UCQAw7d5lin9nAqE+L4HnVAZgxP/I7PYUEFla3P3kDBcWlL9LS4yqxgTjCvp4mHALi7AuR5r4pMbRDAGb6YL14VsQyVTH5bq6qQnfTOoij3PXA= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; 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; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C From: Stewart Hildebrand To: CC: Stewart Hildebrand , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Wei Liu , Anthony PERARD , Andrew Cooper , George Dunlap , "Jan Beulich" , Julien Grall , "Stefano Stabellini" Subject: [PATCH v8 1/2] xen/vpci: header: status register handler Date: Tue, 28 Nov 2023 14:44:24 -0500 Message-ID: <20231128194427.2513249-2-stewart.hildebrand@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231128194427.2513249-1-stewart.hildebrand@amd.com> References: <20231128194427.2513249-1-stewart.hildebrand@amd.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS3PEPF000099DD:EE_|LV3PR12MB9142:EE_ X-MS-Office365-Filtering-Correlation-Id: 4eb33b39-cb3a-4a1a-b9ee-08dbf04a7ba0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: eWqtwrN4mMvr/C7ApElTVvErFQF2hWIcxtw7mZKk7XMVpRVGemjLVMyYFtd0ZAx2o4gbEWIE6c9PfOuDbF6gxBXYucDzGlX2NHHAychccNqvPqpXhTwxDpqqVefojzfWo8HY8ZVXF+iqMG7htCIWxo+lQzB2g70Pm+FYDdjSnP57Gq0rjHXoNHMCUyDgDKSL06ZNlKQ1iNaG8jxYbVb/rirNFVF+BsqHHZDL8RlEKyrDCD17Uw6nP0dIJQxxaxdTMyccXvFJvfueUNEpdpL6IQuaX2a42rMaq8ZtTE0yCgr5KJmI+/EfwtpF5qavr+b36f3gWPWvPCDwpT99Ht4XSNZdw9UvegIpR7NukvOEw8gk4xaneIBXNHF77wpiSiirznHm0goVihUBrlULLQmmGo3EXu8B2s7gJKMXNLpKfGqIf/jcNHSWs4zP4QUn+wK3skvwpZuBCuHiV3u9tKVtdGS3pJIbdRwcLgupwn0nfUcbdITHZ0R+6CcXHd0YaZTNHbCyzHCt2S4z08s/dAw904xLQ5bx0wsl2rvUfcosix0K97UVRjua2HyQMfALiLwUBjV4TcIt70K3MgZEiZ7caRRCF6+xXFAkBEow/mry5QMBs5S/5LvCFdFP+R9t3IxAIEaElnuUyo51zvQdqUk8Y3LahuIjyXN3oiFr/6MxE5ZaP7ZS9iNnZzjv6SYgLOfUVDN9hcXFfOmjSHM4j5WrW9C5FPVghvbKx/SYP9jdAzBA0oVCI2JyAuNgJQ0TpCz9OUb8rRD+tKEQOHAJ1im4ZA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(396003)(39860400002)(376002)(136003)(346002)(230922051799003)(82310400011)(186009)(1800799012)(451199024)(64100799003)(36840700001)(46966006)(40470700004)(5660300002)(8676002)(8936002)(44832011)(4326008)(316002)(6916009)(70586007)(54906003)(70206006)(41300700001)(30864003)(6666004)(478600001)(19627235002)(966005)(2616005)(1076003)(26005)(336012)(426003)(83380400001)(47076005)(2906002)(40480700001)(36860700001)(86362001)(66899024)(40460700003)(82740400003)(356005)(81166007)(36756003)(36900700001)(357404004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Nov 2023 19:44:49.4653 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4eb33b39-cb3a-4a1a-b9ee-08dbf04a7ba0 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DS3PEPF000099DD.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV3PR12MB9142 X-ZohoMail-DKIM: pass (identity @amd.com) X-ZM-MESSAGEID: 1701200723952000003 Content-Type: text/plain; charset="utf-8" Introduce a handler for the PCI status register, with ability to mask the capabilities bit. The status register contains RsvdZ bits, read-only bits, and write-1-to-clear bits. Additionally, we use RsvdP to mask the capabilities bit. Introduce bitmasks to handle these in vPCI. If a bit in the bitmask is set, then the special meaning applies: ro_mask: read normal, guest write ignore (preserve on write to hardware) rw1c_mask: read normal, write 1 to clear rsvdp_mask: read as zero, guest write ignore (preserve on write to hardwa= re) rsvdz_mask: read as zero, guest write ignore (write zero to hardware) The RO/RW1C/RsvdP/RsvdZ naming and definitions were borrowed from the PCI Express Base 6.1 specification. RsvdP/RsvdZ bits help Xen enforce our view of the world. Xen preserves the value of read-only bits on write to hardware, discarding the guests write value. This is done in case hardware wrongly implements R/O bits as R/W. The mask_cap_list flag will be set in a follow-on patch. Signed-off-by: Stewart Hildebrand --- v7->v8: * move PCI_STATUS_UDF to rsvdz_mask (per PCI Express Base 6 spec) * add support for rsvdp bits * add tests for ro/rw1c/rsvdp/rsvdz bits in tools/tests/vpci/main.c * dropped R-b tag [1] since the patch has changed moderately since the last= rev [1] https://lists.xenproject.org/archives/html/xen-devel/2023-09/msg00909.h= tml v6->v7: * re-work args passed to vpci_add_register_mask() (called in init_bars()) * also check for overlap of (rsvdz_mask & ro_mask) in add_register() * slightly adjust masking operation in vpci_write_helper() v5->v6: * remove duplicate PCI_STATUS_CAP_LIST in constant definition * style fixup in constant definitions * s/res_mask/rsvdz_mask/ * combine a new masking operation into single line * preserve r/o bits on write * get rid of status_read. Instead, use rsvdz_mask for conditionally masking PCI_STATUS_CAP_LIST bit * add comment about PCI_STATUS_CAP_LIST and rsvdp behavior * add sanity checks in add_register * move mask_cap_list from struct vpci_header to local variable v4->v5: * add support for res_mask * add support for ro_mask (squash ro_mask patch) * add constants for reserved, read-only, and rw1c masks v3->v4: * move mask_cap_list setting to the capabilities patch * single pci_conf_read16 in status_read * align mask_cap_list bitfield in struct vpci_header * change to rw1c bit mask instead of treating whole register as rw1c * drop subsystem prefix on renamed add_register function v2->v3: * new patch --- tools/tests/vpci/main.c | 98 ++++++++++++++++++++++++++++++++++++++ xen/drivers/vpci/header.c | 12 +++++ xen/drivers/vpci/vpci.c | 62 +++++++++++++++++++----- xen/include/xen/pci_regs.h | 9 ++++ xen/include/xen/vpci.h | 9 ++++ 5 files changed, 178 insertions(+), 12 deletions(-) diff --git a/tools/tests/vpci/main.c b/tools/tests/vpci/main.c index b9a0a6006bb9..b0bb993be297 100644 --- a/tools/tests/vpci/main.c +++ b/tools/tests/vpci/main.c @@ -70,6 +70,26 @@ static void vpci_write32(const struct pci_dev *pdev, uns= igned int reg, *(uint32_t *)data =3D val; } =20 +struct mask_data { + uint32_t val; + uint32_t rw1c_mask; +}; + +static uint32_t vpci_read32_mask(const struct pci_dev *pdev, unsigned int = reg, + void *data) +{ + struct mask_data *md =3D data; + return md->val; +} + +static void vpci_write32_mask(const struct pci_dev *pdev, unsigned int reg, + uint32_t val, void *data) +{ + struct mask_data *md =3D data; + md->val =3D val | (md->val & md->rw1c_mask); + md->val &=3D ~(val & md->rw1c_mask); +} + #define VPCI_READ(reg, size, data) ({ \ data =3D vpci_read((pci_sbdf_t){ .sbdf =3D 0 }, reg, size); \ }) @@ -94,9 +114,20 @@ static void vpci_write32(const struct pci_dev *pdev, un= signed int reg, assert(!vpci_add_register(test_pdev.vpci, fread, fwrite, off, size, = \ &store)) =20 +#define VPCI_ADD_REG_MASK(fread, fwrite, off, size, store, = \ + ro_mask, rw1c_mask, rsvdp_mask, rsvdz_mask) = \ + assert(!vpci_add_register_mask(test_pdev.vpci, fread, fwrite, off, siz= e, \ + &store, = \ + ro_mask, rw1c_mask, rsvdp_mask, rsvdz_m= ask)) + #define VPCI_ADD_INVALID_REG(fread, fwrite, off, size) = \ assert(vpci_add_register(test_pdev.vpci, fread, fwrite, off, size, NUL= L)) =20 +#define VPCI_ADD_INVALID_REG_MASK(fread, fwrite, off, size, = \ + ro_mask, rw1c_mask, rsvdp_mask, rsvdz_ma= sk) \ + assert(vpci_add_register_mask(test_pdev.vpci, fread, fwrite, off, size= , \ + NULL, ro_mask, rw1c_mask, rsvdp_mask, rs= vdz_mask)) + #define VPCI_REMOVE_REG(off, size) = \ assert(!vpci_remove_register(test_pdev.vpci, off, size)) =20 @@ -154,6 +185,7 @@ main(int argc, char **argv) uint16_t r20[2] =3D { }; uint32_t r24 =3D 0; uint8_t r28, r30; + struct mask_data r32; unsigned int i; int rc; =20 @@ -213,6 +245,14 @@ main(int argc, char **argv) /* Try to add a register with missing handlers. */ VPCI_ADD_INVALID_REG(NULL, NULL, 8, 2); =20 + /* Try to add registers with the same bits set in multiple masks. */ + VPCI_ADD_INVALID_REG_MASK(vpci_read32, vpci_write32, 8, 4, 1, 1, 0, 0); + VPCI_ADD_INVALID_REG_MASK(vpci_read32, vpci_write32, 8, 4, 1, 0, 1, 0); + VPCI_ADD_INVALID_REG_MASK(vpci_read32, vpci_write32, 8, 4, 1, 0, 0, 1); + VPCI_ADD_INVALID_REG_MASK(vpci_read32, vpci_write32, 8, 4, 0, 1, 1, 0); + VPCI_ADD_INVALID_REG_MASK(vpci_read32, vpci_write32, 8, 4, 0, 1, 0, 1); + VPCI_ADD_INVALID_REG_MASK(vpci_read32, vpci_write32, 8, 4, 0, 0, 1, 1); + /* Read/write of unset register. */ VPCI_READ_CHECK(8, 4, 0xffffffff); VPCI_READ_CHECK(8, 2, 0xffff); @@ -287,6 +327,64 @@ main(int argc, char **argv) VPCI_ADD_REG(vpci_read8, vpci_write8, 30, 1, r30); VPCI_WRITE_CHECK(28, 4, 0xffacffdc); =20 + /* + * Test ro/rw1c/rsvdp/rsvdz masks. + * + * 32 24 16 8 0 + * +---------------------------+ + * | r32 | 32 + * +---------------------------+ + * + */ + r32.rw1c_mask =3D 0x0000ff00U; + VPCI_ADD_REG_MASK(vpci_read32_mask, vpci_write32_mask, 32, 4, r32, + 0x000000ffU /* RO */, + r32.rw1c_mask /* RW1C */, + 0x00ff0000U /* RsvdP */, + 0xff000000U /* RsvdZ */); + + /* ro */ + r32.val =3D 0x0f0f0f0fU; + VPCI_READ_CHECK(32, 1, 0x0f); + VPCI_WRITE(32, 1, 0x5a); + VPCI_READ_CHECK(32, 1, 0x0f); + assert(r32.val =3D=3D 0x000f0f0fU); + + /* rw1c */ + r32.val =3D 0x0f0f0f0fU; + VPCI_READ_CHECK(33, 1, 0x0f); + VPCI_WRITE(33, 1, 0x5a); + VPCI_READ_CHECK(33, 1, 0x05); + assert(r32.val =3D=3D 0x000f050fU); + + /* rsvdp */ + r32.val =3D 0x0f0f0f0fU; + VPCI_READ_CHECK(34, 1, 0); + VPCI_WRITE(34, 1, 0x5a); + VPCI_READ_CHECK(34, 1, 0); + assert(r32.val =3D=3D 0x000f0f0fU); + + /* rsvdz */ + r32.val =3D 0x0f0f0f0fU; + VPCI_READ_CHECK(35, 1, 0); + VPCI_WRITE(35, 1, 0x5a); + VPCI_READ_CHECK(35, 1, 0); + assert(r32.val =3D=3D 0x000f0f0fU); + + /* write all 0's */ + r32.val =3D 0x0f0f0f0fU; + VPCI_READ_CHECK(32, 4, 0x00000f0fU); + VPCI_WRITE(32, 4, 0); + VPCI_READ_CHECK(32, 4, 0x00000f0fU); + assert(r32.val =3D=3D 0x000f0f0fU); + + /* write all 1's */ + r32.val =3D 0x0f0f0f0fU; + VPCI_READ_CHECK(32, 4, 0x00000f0fU); + VPCI_WRITE(32, 4, 0xffffffffU); + VPCI_READ_CHECK(32, 4, 0x0000000fU); + assert(r32.val =3D=3D 0x000f000fU); + /* Finally try to remove a couple of registers. */ VPCI_REMOVE_REG(28, 1); VPCI_REMOVE_REG(24, 4); diff --git a/xen/drivers/vpci/header.c b/xen/drivers/vpci/header.c index 767c1ba718d7..351318121e48 100644 --- a/xen/drivers/vpci/header.c +++ b/xen/drivers/vpci/header.c @@ -521,6 +521,7 @@ static int cf_check init_bars(struct pci_dev *pdev) struct vpci_header *header =3D &pdev->vpci->header; struct vpci_bar *bars =3D header->bars; int rc; + bool mask_cap_list =3D false; =20 switch ( pci_conf_read8(pdev->sbdf, PCI_HEADER_TYPE) & 0x7f ) { @@ -544,6 +545,17 @@ static int cf_check init_bars(struct pci_dev *pdev) if ( rc ) return rc; =20 + /* Utilize rsvdp_mask to hide PCI_STATUS_CAP_LIST from the guest. */ + rc =3D vpci_add_register_mask(pdev->vpci, vpci_hw_read16, vpci_hw_writ= e16, + PCI_STATUS, 2, NULL, + PCI_STATUS_RO_MASK & + ~(mask_cap_list ? PCI_STATUS_CAP_LIST = : 0), + PCI_STATUS_RW1C_MASK, + mask_cap_list ? PCI_STATUS_CAP_LIST : 0, + PCI_STATUS_RSVDZ_MASK); + if ( rc ) + return rc; + if ( pdev->ignore_bars ) return 0; =20 diff --git a/xen/drivers/vpci/vpci.c b/xen/drivers/vpci/vpci.c index 3bec9a4153da..96187b70141b 100644 --- a/xen/drivers/vpci/vpci.c +++ b/xen/drivers/vpci/vpci.c @@ -29,6 +29,10 @@ struct vpci_register { unsigned int offset; void *private; struct list_head node; + uint32_t ro_mask; + uint32_t rw1c_mask; + uint32_t rsvdp_mask; + uint32_t rsvdz_mask; }; =20 #ifdef __XEN__ @@ -145,9 +149,17 @@ uint32_t cf_check vpci_hw_read32( return pci_conf_read32(pdev->sbdf, reg); } =20 -int vpci_add_register(struct vpci *vpci, vpci_read_t *read_handler, - vpci_write_t *write_handler, unsigned int offset, - unsigned int size, void *data) +void cf_check vpci_hw_write16( + const struct pci_dev *pdev, unsigned int reg, uint32_t val, void *data) +{ + pci_conf_write16(pdev->sbdf, reg, val); +} + +static int add_register(struct vpci *vpci, vpci_read_t *read_handler, + vpci_write_t *write_handler, unsigned int offset, + unsigned int size, void *data, uint32_t ro_mask, + uint32_t rw1c_mask, uint32_t rsvdp_mask, + uint32_t rsvdz_mask) { struct list_head *prev; struct vpci_register *r; @@ -155,7 +167,10 @@ int vpci_add_register(struct vpci *vpci, vpci_read_t *= read_handler, /* Some sanity checks. */ if ( (size !=3D 1 && size !=3D 2 && size !=3D 4) || offset >=3D PCI_CFG_SPACE_EXP_SIZE || (offset & (size - 1)) || - (!read_handler && !write_handler) ) + (!read_handler && !write_handler) || (ro_mask & rw1c_mask) || + (ro_mask & rsvdp_mask) || (ro_mask & rsvdz_mask) || + (rw1c_mask & rsvdp_mask) || (rw1c_mask & rsvdz_mask) || + (rsvdp_mask & rsvdz_mask) ) return -EINVAL; =20 r =3D xmalloc(struct vpci_register); @@ -167,6 +182,10 @@ int vpci_add_register(struct vpci *vpci, vpci_read_t *= read_handler, r->size =3D size; r->offset =3D offset; r->private =3D data; + r->ro_mask =3D ro_mask & (0xffffffffU >> (32 - 8 * size)); + r->rw1c_mask =3D rw1c_mask & (0xffffffffU >> (32 - 8 * size)); + r->rsvdp_mask =3D rsvdp_mask & (0xffffffffU >> (32 - 8 * size)); + r->rsvdz_mask =3D rsvdz_mask & (0xffffffffU >> (32 - 8 * size)); =20 spin_lock(&vpci->lock); =20 @@ -193,6 +212,24 @@ int vpci_add_register(struct vpci *vpci, vpci_read_t *= read_handler, return 0; } =20 +int vpci_add_register(struct vpci *vpci, vpci_read_t *read_handler, + vpci_write_t *write_handler, unsigned int offset, + unsigned int size, void *data) +{ + return add_register(vpci, read_handler, write_handler, offset, size, d= ata, + 0, 0, 0, 0); +} + +int vpci_add_register_mask(struct vpci *vpci, vpci_read_t *read_handler, + vpci_write_t *write_handler, unsigned int offse= t, + unsigned int size, void *data, uint32_t ro_mask, + uint32_t rw1c_mask, uint32_t rsvdp_mask, + uint32_t rsvdz_mask) +{ + return add_register(vpci, read_handler, write_handler, offset, size, d= ata, + ro_mask, rw1c_mask, rsvdp_mask, rsvdz_mask); +} + int vpci_remove_register(struct vpci *vpci, unsigned int offset, unsigned int size) { @@ -376,6 +413,7 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, u= nsigned int size) } =20 val =3D r->read(pdev, r->offset, r->private); + val &=3D ~(r->rsvdp_mask | r->rsvdz_mask); =20 /* Check if the read is in the middle of a register. */ if ( r->offset < emu.offset ) @@ -407,26 +445,26 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg,= unsigned int size) =20 /* * Perform a maybe partial write to a register. - * - * Note that this will only work for simple registers, if Xen needs to - * trap accesses to rw1c registers (like the status PCI header register) - * the logic in vpci_write will have to be expanded in order to correctly - * deal with them. */ static void vpci_write_helper(const struct pci_dev *pdev, const struct vpci_register *r, unsigned int = size, unsigned int offset, uint32_t data) { + uint32_t val =3D 0; + uint32_t preserved_mask =3D r->ro_mask | r->rsvdp_mask; + ASSERT(size <=3D r->size); =20 - if ( size !=3D r->size ) + if ( (size !=3D r->size) || preserved_mask ) { - uint32_t val; - val =3D r->read(pdev, r->offset, r->private); + val &=3D ~r->rw1c_mask; data =3D merge_result(val, data, size, offset); } =20 + data &=3D ~(preserved_mask | r->rsvdz_mask); + data |=3D val & preserved_mask; + r->write(pdev, r->offset, data & (0xffffffffU >> (32 - 8 * r->size)), r->private); } diff --git a/xen/include/xen/pci_regs.h b/xen/include/xen/pci_regs.h index 84b18736a85d..9909b27425a5 100644 --- a/xen/include/xen/pci_regs.h +++ b/xen/include/xen/pci_regs.h @@ -66,6 +66,15 @@ #define PCI_STATUS_REC_MASTER_ABORT 0x2000 /* Set on master abort */ #define PCI_STATUS_SIG_SYSTEM_ERROR 0x4000 /* Set when we drive SERR */ #define PCI_STATUS_DETECTED_PARITY 0x8000 /* Set on parity error */ +#define PCI_STATUS_RSVDZ_MASK 0x0046 /* Includes PCI_STATUS_UDF */ + +#define PCI_STATUS_RO_MASK (PCI_STATUS_IMM_READY | PCI_STATUS_INTERRUPT |= \ + PCI_STATUS_CAP_LIST | PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK | \ + PCI_STATUS_DEVSEL_MASK) +#define PCI_STATUS_RW1C_MASK (PCI_STATUS_PARITY | \ + PCI_STATUS_SIG_TARGET_ABORT | PCI_STATUS_REC_TARGET_ABORT | \ + PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_SIG_SYSTEM_ERROR | \ + PCI_STATUS_DETECTED_PARITY) =20 #define PCI_CLASS_REVISION 0x08 /* High 24 bits are class, low 8 revision = */ #define PCI_REVISION_ID 0x08 /* Revision ID */ diff --git a/xen/include/xen/vpci.h b/xen/include/xen/vpci.h index d743d96a10b8..8e8e42372ec1 100644 --- a/xen/include/xen/vpci.h +++ b/xen/include/xen/vpci.h @@ -37,6 +37,13 @@ int __must_check vpci_add_register(struct vpci *vpci, vpci_write_t *write_handler, unsigned int offset, unsigned int size, void *data); +int __must_check vpci_add_register_mask(struct vpci *vpci, + vpci_read_t *read_handler, + vpci_write_t *write_handler, + unsigned int offset, unsigned int = size, + void *data, uint32_t ro_mask, + uint32_t rw1c_mask, uint32_t rsvdp= _mask, + uint32_t rsvdz_mask); int __must_check vpci_remove_register(struct vpci *vpci, unsigned int offs= et, unsigned int size); =20 @@ -50,6 +57,8 @@ uint32_t cf_check vpci_hw_read16( const struct pci_dev *pdev, unsigned int reg, void *data); uint32_t cf_check vpci_hw_read32( const struct pci_dev *pdev, unsigned int reg, void *data); +void cf_check vpci_hw_write16( + const struct pci_dev *pdev, unsigned int reg, uint32_t val, void *data= ); =20 /* * Check for pending vPCI operations on this vcpu. Returns true if the vcpu --=20 2.43.0 From nobody Tue Feb 10 02:00:22 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; arc=pass (i=1 dmarc=pass fromdomain=amd.com); dmarc=pass(p=quarantine dis=none) header.from=amd.com ARC-Seal: i=2; a=rsa-sha256; t=1701200726; cv=pass; d=zohomail.com; s=zohoarc; b=Hp6kkYfDv19AsDFRYROmabe2Vca8b7ivUI2Bt2Pq0XdOustnJ//K4GWfSlQ6V+dmW2OBBFA/+TcoZDM4WN6A7jSnmfMC80253bXO5fXNwJeuNCUqPi/OJh9f8Qn5Ryd5Cp+5p6kaPeZWMDznpSmnv7pQlKpBPCsGIOMAWVC9lmE= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1701200726; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=T17tiKHmcbaQx3dRFOfzBGEbQzMo3quHzHjXP9wNTwk=; b=DMwsssGzzh/183VeekQvJq8tnbYsMUy9tmEk0aUylbhsWL7VBY1ChzvtYP7V19IOao28yC1BDWJCefSmHA5pY41BLCLosKzGHHQ6qkHDnr7yDE8mj7WuuKqoL7+M+2lQ+4mXPrWMj/FkZZ5Lufv5tyPlcg3spvQGlWhwCOYKQp4= ARC-Authentication-Results: i=2; mx.zohomail.com; dkim=pass; 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; arc=pass (i=1 dmarc=pass fromdomain=amd.com); dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1701200726767995.756311426342; Tue, 28 Nov 2023 11:45:26 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.643449.1003691 (Exim 4.92) (envelope-from ) id 1r8416-0005qG-Ud; Tue, 28 Nov 2023 19:45:08 +0000 Received: by outflank-mailman (output) from mailman id 643449.1003691; Tue, 28 Nov 2023 19:45:08 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r8416-0005q9-Re; Tue, 28 Nov 2023 19:45:08 +0000 Received: by outflank-mailman (input) for mailman id 643449; Tue, 28 Nov 2023 19:45:07 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r8415-0005Sf-PM for xen-devel@lists.xenproject.org; Tue, 28 Nov 2023 19:45:07 +0000 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2060e.outbound.protection.outlook.com [2a01:111:f400:7e8d::60e]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id a123b2d4-8e26-11ee-98e3-6d05b1d4d9a1; Tue, 28 Nov 2023 20:45:06 +0100 (CET) Received: from CH2PR19CA0022.namprd19.prod.outlook.com (2603:10b6:610:4d::32) by SJ0PR12MB7007.namprd12.prod.outlook.com (2603:10b6:a03:486::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7046.22; Tue, 28 Nov 2023 19:45:03 +0000 Received: from DS3PEPF000099DC.namprd04.prod.outlook.com (2603:10b6:610:4d:cafe::2a) by CH2PR19CA0022.outlook.office365.com (2603:10b6:610:4d::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7025.29 via Frontend Transport; Tue, 28 Nov 2023 19:45:02 +0000 Received: from SATLEXMB03.amd.com (165.204.84.17) by DS3PEPF000099DC.mail.protection.outlook.com (10.167.17.198) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7046.17 via Frontend Transport; Tue, 28 Nov 2023 19:45:02 +0000 Received: from SATLEXMB05.amd.com (10.181.40.146) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Tue, 28 Nov 2023 13:45:02 -0600 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB05.amd.com (10.181.40.146) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Tue, 28 Nov 2023 13:45:02 -0600 Received: from ubuntu.mshome.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.34 via Frontend Transport; Tue, 28 Nov 2023 13:45:00 -0600 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: a123b2d4-8e26-11ee-98e3-6d05b1d4d9a1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=C9jJx0U7xUyCSBPuALz5j967Lp373WB0YsmwFLTmzOmqyQh794WHFRGlSjhMhkU7YSZ/+TAqjFiM0LtUTbSlCFUVg0c0VxNWTvqUgr7sVN41gcJVglwB55m+nFyigXi+Spgceokl9SRBKlO8uJepZZG5CzlFSCcZ9E+Jg0/2vJbSaGPRReNoSitt5LiK08Jje2WQSAgmzIpw1vzGjOT4ojnZ63VqlXUoW+1upIsFm8JRNudyPickS66UdevVh+5PoNb9WdxcPKBhsiqdQGLQbmrfJUnEeCsrgVK2Ue2qteGnBUyUOO2GLk1lVD/Rv9WLS8uWWw79wLPG6KXGG0aogQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=T17tiKHmcbaQx3dRFOfzBGEbQzMo3quHzHjXP9wNTwk=; b=VKDvhmO4UVOdtYsu8re6rQGDCwBhqBVHx2YxZL3HldV9Y0w0T3QhPRKRwpnxklHQ0c6aqcvOneMYBIYhOJN1PvPjPXZdd07LFB1xv4QJ0PIKvmU30ceaF3bn9m45CWrO6st+tykg6Bxevcw8gp890w1qYrq+o2Wc7U7GV4oiYTT1yKX1nNjzPHb2mbG7PX8LhzJRhuptve4F7hYQ+6fopocAxmb2epCbAisaEf9Pkf+ypIsRziosHX33bOPLyfEUiThRIhYR/XPPqga6waSQqBr2ua0DmXJSRAIQpMUF7gCLMW1gZras2ewQqzyDj7Irt5qv0IjAOzBpRVv8J1ilYA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=T17tiKHmcbaQx3dRFOfzBGEbQzMo3quHzHjXP9wNTwk=; b=SjAtIes9axaG2EefjIYUpgRa4m9SB12cfbFXXWTj8vNRgkSRODlKd8sSBygIDbVtVzxh/vlg5YRnuKwciSeLcKDnGCEm3yREF0Joq+zpna0nEmf5mDNa/RwOchlWGeZf73vzt8kLMWCXBJGnS7h+5D+/u6rA5z+CxfRcJiPd3V8= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; 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; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C From: Stewart Hildebrand To: CC: Stewart Hildebrand , Andrew Cooper , George Dunlap , "Jan Beulich" , Julien Grall , "Stefano Stabellini" , Wei Liu , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Subject: [PATCH v8 2/2] xen/vpci: header: filter PCI capabilities Date: Tue, 28 Nov 2023 14:44:25 -0500 Message-ID: <20231128194427.2513249-3-stewart.hildebrand@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231128194427.2513249-1-stewart.hildebrand@amd.com> References: <20231128194427.2513249-1-stewart.hildebrand@amd.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS3PEPF000099DC:EE_|SJ0PR12MB7007:EE_ X-MS-Office365-Filtering-Correlation-Id: aceae3dc-5184-454e-3b0a-08dbf04a838d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ++Fu4rSkApoRGpEGKYPrHWeaYJC4I0D9zLdQ3mGNNzmalc9MbuzpLTMp62yNtO8sUNhokCRWVJv2etb4AG48OKbPHYTJt65jYjQkrl+I6RQL4B+pICK71ORLrgWhGoJn8SnSPxW3Qycij5A2DgAZtuLWWGLuTtkhVY9aT14IlGf9Udsk4b2x527ty+Y+5v8y6SgYlF6u7rkUoTbPCrJDSawVNw+/+nbgK+jJki/u7sppq0B2cFAwuMd8Q5Gn9m1LrKSHfD191KruqEpRUMB7phuLfodJz1Zincovl8/fXeomsXXjo9+KsIg+eYuOzYDhxTZmWNBdi2uI5/bFceLkXD/2lBMrQ7IgoDyVWVjQucU6tSBWCCvc+IGbZT9ejOcRfmVhEGfqyl0NfKH/BufJiUPxJDQ4baB2Nude6SJy2trjI6/zZx8ZviClqYC+QlSEFE1sVR2lNLsakc3oltcQowjp+7IL4JgUir+Rbf1S0APr4mNUT3UDyam1BvFC5Vqzb1Y5oVDgslxVABB5cosCelELRx0S55XtIupch5sn80DdS2g8nY4T+ekYNQAvZbZ0WqyskhNf3IFfb4hH8XCa+Mez1oe5UALkvtuCOekCpdH4f6/ClXOyuvgFQJZTeBXfZwLZvDcfvebDmS0EzVBxq3I2qGcSL0Yo05E99xHfMwoFlESOq3/So8bLRv5onGVoWTWC/JYyqi1Us5j5riLBt+9gTrQaABO1RUz0Tqv731mOpPYeLoxEwWKVhGcMheE7vj1XY3SIjNrQfLUcrkOf0w== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(396003)(136003)(376002)(346002)(39860400002)(230922051799003)(82310400011)(1800799012)(186009)(64100799003)(451199024)(40470700004)(46966006)(36840700001)(2906002)(40480700001)(5660300002)(40460700003)(44832011)(36860700001)(6666004)(478600001)(8676002)(8936002)(41300700001)(4326008)(36756003)(81166007)(356005)(82740400003)(426003)(336012)(47076005)(2616005)(83380400001)(6916009)(86362001)(70206006)(70586007)(54906003)(316002)(1076003)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Nov 2023 19:45:02.7749 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: aceae3dc-5184-454e-3b0a-08dbf04a838d X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DS3PEPF000099DC.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB7007 X-ZohoMail-DKIM: pass (identity @amd.com) X-ZM-MESSAGEID: 1701200726990000001 Content-Type: text/plain; charset="utf-8" Currently, Xen vPCI only supports virtualizing the MSI and MSI-X capabiliti= es. Hide all other PCI capabilities (including extended capabilities) from domU= s for now, even though there may be certain devices/drivers that depend on being = able to discover certain capabilities. We parse the physical PCI capabilities linked list and add vPCI register handlers for the next elements, inserting our own next value, thus presenti= ng a modified linked list to the domU. Introduce helper functions vpci_hw_read8 and vpci_read_val. The vpci_read_v= al helper function returns a fixed value, which may be used for RAZ registers,= or registers whose value doesn't change. Introduce pci_find_next_cap_ttl() helper while adapting the logic from pci_find_next_cap() to suit our needs, and implement the existing pci_find_next_cap() in terms of the new helper. Signed-off-by: Stewart Hildebrand Reviewed-by: Roger Pau Monn=C3=A9 --- v7->v8: * use to array instead of match function * include lib.h for ARRAY_SIZE * don't emulate PCI_CAPABILITY_LIST register if PCI_STATUS_CAP_LIST bit is = not set in hardware * spell out RAZ/WI acronym * dropped R-b tag since the patch has changed moderately since the last rev v6->v7: * no change v5->v6: * add register handlers before status register handler in init_bars() * s/header->mask_cap_list/mask_cap_list/ v4->v5: * use more appropriate types, continued * get rid of unnecessary hook function * add Jan's R-b v3->v4: * move mask_cap_list setting to this patch * leave pci_find_next_cap signature alone * use more appropriate types v2->v3: * get rid of > 0 in loop condition * implement pci_find_next_cap in terms of new pci_find_next_cap_ttl functio= n so that hypothetical future callers wouldn't be required to pass &ttl. * change NULL to (void *)0 for RAZ value passed to vpci_read_val * change type of ttl to unsigned int * remember to mask off the low 2 bits of next in the initial loop iteration * change return type of pci_find_next_cap and pci_find_next_cap_ttl * avoid wrapping the PCI_STATUS_CAP_LIST condition by using ! instead of = =3D=3D 0 v1->v2: * change type of ttl to int * use switch statement instead of if/else * adapt existing pci_find_next_cap helper instead of rolling our own * pass ttl as in/out * "pass through" the lower 2 bits of the next pointer * squash helper functions into this patch to avoid transient dead code situ= ation * extended capabilities RAZ/WI --- xen/drivers/pci/pci.c | 31 ++++++++++++------- xen/drivers/vpci/header.c | 63 +++++++++++++++++++++++++++++++++++++++ xen/drivers/vpci/vpci.c | 12 ++++++++ xen/include/xen/pci.h | 3 ++ xen/include/xen/vpci.h | 5 ++++ 5 files changed, 104 insertions(+), 10 deletions(-) diff --git a/xen/drivers/pci/pci.c b/xen/drivers/pci/pci.c index 3569ccb24e9e..1645b3118220 100644 --- a/xen/drivers/pci/pci.c +++ b/xen/drivers/pci/pci.c @@ -39,31 +39,42 @@ unsigned int pci_find_cap_offset(pci_sbdf_t sbdf, unsig= ned int cap) return 0; } =20 -unsigned int pci_find_next_cap(pci_sbdf_t sbdf, unsigned int pos, - unsigned int cap) +unsigned int pci_find_next_cap_ttl(pci_sbdf_t sbdf, unsigned int pos, + unsigned int *cap, unsigned int n, + unsigned int *ttl) { - u8 id; - int ttl =3D 48; + unsigned int id, i; =20 - while ( ttl-- ) + while ( (*ttl)-- ) { pos =3D pci_conf_read8(sbdf, pos); if ( pos < 0x40 ) break; =20 - pos &=3D ~3; - id =3D pci_conf_read8(sbdf, pos + PCI_CAP_LIST_ID); + id =3D pci_conf_read8(sbdf, (pos & ~3) + PCI_CAP_LIST_ID); =20 if ( id =3D=3D 0xff ) break; - if ( id =3D=3D cap ) - return pos; + for ( i =3D 0; i < n; i++ ) + { + if ( id =3D=3D cap[i] ) + return pos; + } =20 - pos +=3D PCI_CAP_LIST_NEXT; + pos =3D (pos & ~3) + PCI_CAP_LIST_NEXT; } + return 0; } =20 +unsigned int pci_find_next_cap(pci_sbdf_t sbdf, unsigned int pos, + unsigned int cap) +{ + unsigned int ttl =3D 48; + + return pci_find_next_cap_ttl(sbdf, pos, &cap, 1, &ttl) & ~3; +} + /** * pci_find_ext_capability - Find an extended capability * @sbdf: PCI device to query diff --git a/xen/drivers/vpci/header.c b/xen/drivers/vpci/header.c index 351318121e48..d7dc0c82a6ba 100644 --- a/xen/drivers/vpci/header.c +++ b/xen/drivers/vpci/header.c @@ -18,6 +18,7 @@ */ =20 #include +#include #include #include #include @@ -545,6 +546,68 @@ static int cf_check init_bars(struct pci_dev *pdev) if ( rc ) return rc; =20 + if ( !is_hardware_domain(pdev->domain) ) + { + if ( pci_conf_read16(pdev->sbdf, PCI_STATUS) & PCI_STATUS_CAP_LIST= ) + { + /* Only expose capabilities to the guest that vPCI can handle.= */ + unsigned int next, ttl =3D 48; + unsigned int supported_caps[] =3D { + PCI_CAP_ID_MSI, + PCI_CAP_ID_MSIX, + }; + + next =3D pci_find_next_cap_ttl(pdev->sbdf, PCI_CAPABILITY_LIST, + supported_caps, + ARRAY_SIZE(supported_caps), &ttl); + + rc =3D vpci_add_register(pdev->vpci, vpci_read_val, NULL, + PCI_CAPABILITY_LIST, 1, + (void *)(uintptr_t)next); + if ( rc ) + return rc; + + next &=3D ~3; + + if ( !next ) + /* + * If we don't have any supported capabilities to expose t= o the + * guest, mask the PCI_STATUS_CAP_LIST bit in the status + * register. + */ + mask_cap_list =3D true; + + while ( next && ttl ) + { + unsigned int pos =3D next; + + next =3D pci_find_next_cap_ttl(pdev->sbdf, + pos + PCI_CAP_LIST_NEXT, + supported_caps, + ARRAY_SIZE(supported_caps), &= ttl); + + rc =3D vpci_add_register(pdev->vpci, vpci_hw_read8, NULL, + pos + PCI_CAP_LIST_ID, 1, NULL); + if ( rc ) + return rc; + + rc =3D vpci_add_register(pdev->vpci, vpci_read_val, NULL, + pos + PCI_CAP_LIST_NEXT, 1, + (void *)(uintptr_t)next); + if ( rc ) + return rc; + + next &=3D ~3; + } + } + + /* Extended capabilities read as zero, write ignore */ + rc =3D vpci_add_register(pdev->vpci, vpci_read_val, NULL, 0x100, 4, + (void *)0); + if ( rc ) + return rc; + } + /* Utilize rsvdp_mask to hide PCI_STATUS_CAP_LIST from the guest. */ rc =3D vpci_add_register_mask(pdev->vpci, vpci_hw_read16, vpci_hw_writ= e16, PCI_STATUS, 2, NULL, diff --git a/xen/drivers/vpci/vpci.c b/xen/drivers/vpci/vpci.c index 96187b70141b..99307e310bbb 100644 --- a/xen/drivers/vpci/vpci.c +++ b/xen/drivers/vpci/vpci.c @@ -137,6 +137,18 @@ static void cf_check vpci_ignored_write( { } =20 +uint32_t cf_check vpci_read_val( + const struct pci_dev *pdev, unsigned int reg, void *data) +{ + return (uintptr_t)data; +} + +uint32_t cf_check vpci_hw_read8( + const struct pci_dev *pdev, unsigned int reg, void *data) +{ + return pci_conf_read8(pdev->sbdf, reg); +} + uint32_t cf_check vpci_hw_read16( const struct pci_dev *pdev, unsigned int reg, void *data) { diff --git a/xen/include/xen/pci.h b/xen/include/xen/pci.h index 50d7dfb2a2fd..b2dcef01a1cf 100644 --- a/xen/include/xen/pci.h +++ b/xen/include/xen/pci.h @@ -205,6 +205,9 @@ int pci_mmcfg_read(unsigned int seg, unsigned int bus, int pci_mmcfg_write(unsigned int seg, unsigned int bus, unsigned int devfn, int reg, int len, u32 value); unsigned int pci_find_cap_offset(pci_sbdf_t sbdf, unsigned int cap); +unsigned int pci_find_next_cap_ttl(pci_sbdf_t sbdf, unsigned int pos, + unsigned int *cap, unsigned int n, + unsigned int *ttl); unsigned int pci_find_next_cap(pci_sbdf_t sbdf, unsigned int pos, unsigned int cap); unsigned int pci_find_ext_capability(pci_sbdf_t sbdf, unsigned int cap); diff --git a/xen/include/xen/vpci.h b/xen/include/xen/vpci.h index 8e8e42372ec1..3c14a74d6255 100644 --- a/xen/include/xen/vpci.h +++ b/xen/include/xen/vpci.h @@ -52,7 +52,12 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, un= signed int size); void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size, uint32_t data); =20 +uint32_t cf_check vpci_read_val( + const struct pci_dev *pdev, unsigned int reg, void *data); + /* Passthrough handlers. */ +uint32_t cf_check vpci_hw_read8( + const struct pci_dev *pdev, unsigned int reg, void *data); uint32_t cf_check vpci_hw_read16( const struct pci_dev *pdev, unsigned int reg, void *data); uint32_t cf_check vpci_hw_read32( --=20 2.43.0