From nobody Sat Nov 30 06:36:31 2024 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=1726133108; cv=pass; d=zohomail.com; s=zohoarc; b=VA6ES7TPT48jqJkcXMH8TlftllaAQwSTCVzPAJAGMZlE+QZOqSWNkia3K4aI25WkAuU+EFcBdv6LnZv6mh3KccsI5m6pBLJgffXnXqEGcZRoTHjgCd80sBZVruGC0iMsWVdL9LwKS8yp2aeM5dEU11+qtl7V82U9GoR8UGLGsuQ= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1726133108; 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=K/LBDOvtpcnz21DnY0VeKywIgWINhOaisJwaW9jtGw4=; b=JUT0WCQw2MKWy5tSdai2ukCEftByoL5JtlH88x/uYb/Ne/dfwOtem9C0QbUpRYktLrG7HyLVeoWk0Z48lVori6ICX+fuX3jSfXVaIHNAbr2VYkVv5nwbjrv5c2RyS/PXRH88ag2ssR3APC+O3Bm6inzI2cTleVqQGNpMrnghVB4= 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 1726133108235458.09508171414893; Thu, 12 Sep 2024 02:25:08 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.797106.1206929 (Exim 4.92) (envelope-from ) id 1sog40-0004xk-B2; Thu, 12 Sep 2024 09:24:32 +0000 Received: by outflank-mailman (output) from mailman id 797106.1206929; Thu, 12 Sep 2024 09:24:32 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sog40-0004xd-83; Thu, 12 Sep 2024 09:24:32 +0000 Received: by outflank-mailman (input) for mailman id 797106; Thu, 12 Sep 2024 09:24:29 +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 1sog3x-0004ii-TJ for xen-devel@lists.xenproject.org; Thu, 12 Sep 2024 09:24:29 +0000 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on20610.outbound.protection.outlook.com [2a01:111:f403:2414::610]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id cf3c3753-70e8-11ef-a0b5-8be0dac302b0; Thu, 12 Sep 2024 11:24:29 +0200 (CEST) Received: from BYAPR05CA0104.namprd05.prod.outlook.com (2603:10b6:a03:e0::45) by SA1PR12MB6947.namprd12.prod.outlook.com (2603:10b6:806:24e::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.33; Thu, 12 Sep 2024 09:24:23 +0000 Received: from CO1PEPF000044F0.namprd05.prod.outlook.com (2603:10b6:a03:e0:cafe::a5) by BYAPR05CA0104.outlook.office365.com (2603:10b6:a03:e0::45) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.24 via Frontend Transport; Thu, 12 Sep 2024 09:24:23 +0000 Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1PEPF000044F0.mail.protection.outlook.com (10.167.241.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7918.13 via Frontend Transport; Thu, 12 Sep 2024 09:24:22 +0000 Received: from cjq-desktop.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Thu, 12 Sep 2024 04:24:18 -0500 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: cf3c3753-70e8-11ef-a0b5-8be0dac302b0 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=IEFK4/MYIlMH1Dks/krt6KVpWrrMGH1mJPnxIWLWZ0ZU3W/kmlzsUURrvaa9LpSYF+w498cW8lEvbE7NDPRB/nPThJorTqNt0hrtzLoxmylEv2QwHmq1lMqrFcpT0FpSey+LJIO8csWVNWHilg22Il9+l6uXewlUOjOppUuEE32Vf2LAitoNtrC402xCa3EzGSARxBmEBB3mO/NQecvc16wQIbWieksd+N3fkLiWBZZGTHx5hX3M4WPeYdFwg4eVN+vTzQ8L2H2SM3E5Y6QyXNMIUUNMn/VOvw0g9D15C5BdUbfnFvYBLkNC7pgkRdZAGfiCYS+t7QQQantfuSC3dg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=K/LBDOvtpcnz21DnY0VeKywIgWINhOaisJwaW9jtGw4=; b=vBAOUidjPneTj2FiCmSzwvGuoLjgFCWipI1YJhNQV/hfAtkD7V3DMuML8b69RPhR3VEhOdidCUFM04/nwr/dHry1HtUrXhiCL4Ga+vMWJNW0J39DNzNGqU7/2C2uu2/chf3al7PAFCVlH+ACrSXCWce/fRuC5X37J1cWAyBP1OCjddlP+c6hLrYl+vaFLWR5HAuLhWGJn1utwpxbUB5+SQ5/a5kIkV0iWUygroHU6W+5afRy0wmMWVydo2rAqB44sIT5nLatS0NinsGJnm75nesAEI99wtP6+x2Ii7KkzfaI1MKA3Lu+1YyOoY5g1IdmL8QKkPAttKXadgpWco2Ptw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=suse.com 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=K/LBDOvtpcnz21DnY0VeKywIgWINhOaisJwaW9jtGw4=; b=VXhxCrW5THFYFmNNr/v6U5q9Sb4BpB7DQaTC8Ch9uE69AXHhidRGPRrVLaDBahFyJnf8hjdhhOxj/G1QbtJvVJxUQoVf8J8zwPrjASMb3xGM89QobdDLgePX5z0dDUPnFIW8XdpLGtoaGYlJAgt/BLNCWRjnY8nhc1eozPLYG2E= 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=SATLEXMB04.amd.com; pr=C From: Jiqian Chen To: Juergen Gross , Stefano Stabellini , Bjorn Helgaas , "Rafael J . Wysocki" CC: , , , , Jiqian Chen , Huang Rui Subject: [KERNEL PATCH v9 1/3] xen/pci: Add a function to reset device for xen Date: Thu, 12 Sep 2024 17:23:49 +0800 Message-ID: <20240912092352.1602724-2-Jiqian.Chen@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240912092352.1602724-1-Jiqian.Chen@amd.com> References: <20240912092352.1602724-1-Jiqian.Chen@amd.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000044F0:EE_|SA1PR12MB6947:EE_ X-MS-Office365-Filtering-Correlation-Id: 1ff64c50-b8a6-42b4-b8ae-08dcd30cb04a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|376014|36860700013|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?+0Q6Mxj8CLJadwy6YzjJ2CBU603BXSocKkdBsduqgSj4pSWWY34lqJajyS8U?= =?us-ascii?Q?pe/G0z66BItCIYCQvXiLz/PYbTqtH+2toN6c+UMhxnwPsfCCom+Nj8fGRXGE?= =?us-ascii?Q?pgosxU+e4LzxyJm3icBcI+HNZ1sJ4FVNgV7vJBwiHpFZcOVflS0FiztDmeXr?= =?us-ascii?Q?6MLgkAxe/CuKga46COrsxW3bLijRmFb/c/IXa27MaoAlG9DlTTbOXHyL9TkN?= =?us-ascii?Q?Xqv5Ntz/3leSmHXKQeikig7QHGoeb/HglrjdugDUlmc4g2vGoWED0FkxhEu4?= =?us-ascii?Q?tKQLKFlK8ya0hlp05oJkbq8wf1BrRYS7boYHSPcSECoStx8GV+D5Cb05D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8I9FPK8eQVgiuQxhILu/PssRqKlTIUeSeLdhsLn1ePyzby?= =?us-ascii?Q?fRLKN0SkbtWQrbitY7rrx+oWy5y5WJsadBuYMIP9zjQGwvnYjRvoWPwghvK8?= =?us-ascii?Q?jY3FMUH4Cv0+eqqxWjN31wOTjh1ww6KyX8FowspvgoriTq0BifdWrsb6CWJi?= =?us-ascii?Q?diMvmKNPQLY4wl+klLlaS6dkkHrrbwK3hH/seF0H9pPPQggepx8ig70W3RBR?= =?us-ascii?Q?oPHqPAofVaqRXav5A8+GHshX/+ygrRQm?= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(82310400026)(376014)(36860700013)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Sep 2024 09:24:22.8804 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1ff64c50-b8a6-42b4-b8ae-08dcd30cb04a 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=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000044F0.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB6947 X-ZohoMail-DKIM: pass (identity @amd.com) X-ZM-MESSAGEID: 1726133108742116600 Content-Type: text/plain; charset="utf-8" When device on dom0 side has been reset, the vpci on Xen side won't get notification, so that the cached state in vpci is all out of date with the real device state. To solve that problem, add a new function to clear all vpci device state when device is reset on dom0 side. And call that function in pcistub_init_device. Because when using "pci-assignable-add" to assign a passthrough device in Xen, it will reset passthrough device and the vpci state will out of date, and then device will fail to restore bar state. Signed-off-by: Jiqian Chen Signed-off-by: Huang Rui Signed-off-by: Jiqian Chen Reviewed-by: Stefano Stabellini --- v8->v9 changes: Due to the struct and name of the hypercall changed on Xen side, I did the = corresponding changes, so removed the Reviewed-by of Stefano. But no functi= on changes actually. v5->v8 changes: No. v4->v5 changes: Added Reviewed-by of Stefano. v3->v4 changes: Changed the code comment of PHYSDEVOP_pci_device_state_reset. Used a new function pcistub_reset_device_state to wrap __pci_reset_function= _locked and xen_reset_device_state, and called pcistub_reset_device_state i= n pci_stub.c. v2->v3 changes: Added condition to limit do xen_reset_device_state for no-pv domain in pcis= tub_init_device. v1->v2 changes: New patch to add a new function to call reset hypercall. --- drivers/xen/pci.c | 13 +++++++++++++ drivers/xen/xen-pciback/pci_stub.c | 18 +++++++++++++++--- include/xen/interface/physdev.h | 17 +++++++++++++++++ include/xen/pci.h | 6 ++++++ 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/drivers/xen/pci.c b/drivers/xen/pci.c index 72d4e3f193af..bb59524b8bbd 100644 --- a/drivers/xen/pci.c +++ b/drivers/xen/pci.c @@ -177,6 +177,19 @@ static int xen_remove_device(struct device *dev) return r; } =20 +int xen_reset_device(const struct pci_dev *dev) +{ + struct pci_device_reset device =3D { + .dev.seg =3D pci_domain_nr(dev->bus), + .dev.bus =3D dev->bus->number, + .dev.devfn =3D dev->devfn, + .flags =3D PCI_DEVICE_RESET_FLR, + }; + + return HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_reset, &device); +} +EXPORT_SYMBOL_GPL(xen_reset_device); + static int xen_pci_notifier(struct notifier_block *nb, unsigned long action, void *data) { diff --git a/drivers/xen/xen-pciback/pci_stub.c b/drivers/xen/xen-pciback/p= ci_stub.c index 4faebbb84999..3e162c1753e2 100644 --- a/drivers/xen/xen-pciback/pci_stub.c +++ b/drivers/xen/xen-pciback/pci_stub.c @@ -89,6 +89,16 @@ static struct pcistub_device *pcistub_device_alloc(struc= t pci_dev *dev) return psdev; } =20 +static int pcistub_reset_device_state(struct pci_dev *dev) +{ + __pci_reset_function_locked(dev); + + if (!xen_pv_domain()) + return xen_reset_device(dev); + else + return 0; +} + /* Don't call this directly as it's called by pcistub_device_put */ static void pcistub_device_release(struct kref *kref) { @@ -107,7 +117,7 @@ static void pcistub_device_release(struct kref *kref) /* Call the reset function which does not take lock as this * is called from "unbind" which takes a device_lock mutex. */ - __pci_reset_function_locked(dev); + pcistub_reset_device_state(dev); if (dev_data && pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state)) dev_info(&dev->dev, "Could not reload PCI state\n"); @@ -284,7 +294,7 @@ void pcistub_put_pci_dev(struct pci_dev *dev) * (so it's ready for the next domain) */ device_lock_assert(&dev->dev); - __pci_reset_function_locked(dev); + pcistub_reset_device_state(dev); =20 dev_data =3D pci_get_drvdata(dev); ret =3D pci_load_saved_state(dev, dev_data->pci_saved_state); @@ -420,7 +430,9 @@ static int pcistub_init_device(struct pci_dev *dev) dev_err(&dev->dev, "Could not store PCI conf saved state!\n"); else { dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n"); - __pci_reset_function_locked(dev); + err =3D pcistub_reset_device_state(dev); + if (err) + goto config_release; pci_restore_state(dev); } /* Now disable the device (this also ensures some private device diff --git a/include/xen/interface/physdev.h b/include/xen/interface/physde= v.h index a237af867873..df74e65a884b 100644 --- a/include/xen/interface/physdev.h +++ b/include/xen/interface/physdev.h @@ -256,6 +256,13 @@ struct physdev_pci_device_add { */ #define PHYSDEVOP_prepare_msix 30 #define PHYSDEVOP_release_msix 31 +/* + * Notify the hypervisor that a PCI device has been reset, so that any + * internally cached state is regenerated. Should be called after any + * device reset performed by the hardware domain. + */ +#define PHYSDEVOP_pci_device_reset 32 + struct physdev_pci_device { /* IN */ uint16_t seg; @@ -263,6 +270,16 @@ struct physdev_pci_device { uint8_t devfn; }; =20 +struct pci_device_reset { + struct physdev_pci_device dev; +#define PCI_DEVICE_RESET_COLD 0x0 +#define PCI_DEVICE_RESET_WARM 0x1 +#define PCI_DEVICE_RESET_HOT 0x2 +#define PCI_DEVICE_RESET_FLR 0x3 +#define PCI_DEVICE_RESET_MASK 0x3 + uint32_t flags; +}; + #define PHYSDEVOP_DBGP_RESET_PREPARE 1 #define PHYSDEVOP_DBGP_RESET_DONE 2 =20 diff --git a/include/xen/pci.h b/include/xen/pci.h index b8337cf85fd1..424b8ea89ca8 100644 --- a/include/xen/pci.h +++ b/include/xen/pci.h @@ -4,10 +4,16 @@ #define __XEN_PCI_H__ =20 #if defined(CONFIG_XEN_DOM0) +int xen_reset_device(const struct pci_dev *dev); int xen_find_device_domain_owner(struct pci_dev *dev); int xen_register_device_domain_owner(struct pci_dev *dev, uint16_t domain); int xen_unregister_device_domain_owner(struct pci_dev *dev); #else +static inline int xen_reset_device(const struct pci_dev *dev) +{ + return -1; +} + static inline int xen_find_device_domain_owner(struct pci_dev *dev) { return -1; --=20 2.34.1