From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=40.93.196.67 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796444182; b=AtHZbdB/rIsBaD+VmKmmSZsV+/ooegKz2uvKAah9bOHlGOZiahgRdqJwFJI5l40ACY3E 5jAT5VLQErc+s08YXa2BCT5brtAAcqEXFTcobPs3O5CqBuGkzrjUFF10Shh470ClqyiW+ rGuVqgbXoeA6twOrhn4P8NDnRtTfm2oTeu1vYXNLhBOuRcy2cKh+zFVeZWq15Vm2+uaiY vdtwITj2DfAtXyhEQRzFPcWhc3CvG4n2L6kUxYmv4ehFTO+3a5ZTlMjUuQJt6Ce6Bh6Du EX42dPNq9gNlwUmCPo9Irh5okGtc3t6XrlEGV14DjZy/X7XPurnh9hJ6nuu+499KTEg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796444182; h=DMARC-Filter:DKIM-Signature:Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References: Content-Transfer-Encoding:Content-Type:X-ClientProxiedBy:MIME-Version: X-MS-PublicTrafficType:X-MS-TrafficTypeDiagnostic: X-MS-Office365-Filtering-Correlation-Id:X-MS-Exchange-SenderADCheck: X-MS-Exchange-AntiSpam-Relay:X-Microsoft-Antispam: X-Microsoft-Antispam-Message-Info:X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-OriginatorOrg: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-CrossTenant-Id:X-MS-Exchange-CrossTenant-MailboxType: X-MS-Exchange-CrossTenant-UserPrincipalName: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=mPkKCuCMgjTB04c9JW+4cTWmS0ffCyCiyxv7l8inr7w=; b=fbqpdGKpj5FtG9tFUHFABEfabzIgmb0PgJe5F5BJXrUuDeCPKRA2se/cmQGN3/83qMBh 6L2xmdvRrqlC/xeLuxbquis0pIjmjUJ/fapoeMgKOft71+1xxHByJa1xN/6B88Nsn27YA UFY3UM0RD8Yoc3g6tq79qiwIrXz7IkLyS3Oks2OES+Y1DqjL/q0l0e0Ci14CAN62iOu0W N15denZ8Rlx16eA6pNRw0fSCdi12IpLM3BTI5zdm7KTGQ7WcgG0v9ZFiYdIfGNs28V9AN lvFfdahWOSewelvx1ISEFtNyerc3sC8zlq/gtOXUNZ3Fzg98MqLzm5ISMPLnm+OQRmQ== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=Xg18E2eU; arc=pass header.oldest-pass=0 smtp.remote-ip=40.93.196.67 Received: from SA9PR02CU001.outbound.protection.outlook.com (mail-southcentralusazon11013067.outbound.protection.outlook.com [40.93.196.67]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (secp384r1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C73FD606A33A; Sat, 5 Dec 2026 04:16:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C73FD606A33A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=Xg18E2eU ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=EOiWq244/0zu+tThqtt97Y8l57Ill+vllrAqhTz+k+9B4mYj896xJ7bcq2AsukwwDR/N0AfypEVP+nJtDQLoAcGt6YNSN+OrtcXvm27mccx53lxErxvttfyxo/+wnXjIABttkc1ZqZZZ5jfwr7qfRq7E1yHCZhq8PV9Myrbkiq0nNmcQUj5R2Zq6ZrDhFnUQzN8ZNSddtSvq0Dj7TNXzMcVV/yOaatOK/01CxPq0jI5O3xCm4ekI6feo7/4K8aIcINrQXgVRdSnOjJ5XeU+/lZGDuoPiok7VCCHQU1tZbRyAkYKPAaLtIoy4GXPcs7r85eW+0fcVnTyyintJ2EBbGg== 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=mPkKCuCMgjTB04c9JW+4cTWmS0ffCyCiyxv7l8inr7w=; b=aoC96pCigxkRt0YABMDlsKoKTTlES/5w7+R3IFvdSkYskZZYsdohneJ88DGmYqLugYlRgdp2LN9m2SzueMbJckFElXfFEEz+DJ6OKSLO43yP+3w3K4TQIjWHu2O0KgqtjdFpr0QgojM6IMBQIMvP+obrCPmDj/jEIAffqWcWQxFR/WWA0AdcWY+7Lc7d+8ZvwuwcBMF5FWSwhUBl9V6+115XySU4m0XzoOigdF11dMyEeuW/yd6icnbNdJT3VBCk9QJedmRGnV9LJYQqDSe7Yg3Mr+pBE/9BqbBZSz2oXDrDpR3gZUFJnJ4bjwkDec8mqdxCxaP45pncnauanm4eqw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=mPkKCuCMgjTB04c9JW+4cTWmS0ffCyCiyxv7l8inr7w=; b=Xg18E2eUAqSSkzZ89p43bBnOYYqqfwCY8KI99KQvj0YQd78axoZ/wC50kpTqbn7C7COiF0bqU9q0/063V4kpJXovNJP4ZRLH69opBM293WaMZui1Lie5zXg+5c1oVSZ4TfBK3muoixhICs1N+ColuTyVQ5VtiRuWxfbwwqP85sC5ZT0BkzKzsU0WTvURXXXjTOomx4sDHyTqmEwMg0ZPjWXy24yjtfo0YXAnVjrm1yDvViPQ+8xd44VwIFOcGSq4f/LMPQ0kW/slQUvptfdnfuhNJmBsAQqwMD4vj0Atg35P7f9fmPlA92v7J1xQuSsGisb+LIiuuVUzEO+JAak+dg== Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DSVPR12MB827618.namprd12.prod.outlook.com (2603:10b6:8:3e5::24) by MW4PR12MB5602.namprd12.prod.outlook.com (2603:10b6:303:169::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.11; Sat, 5 Dec 2026 04:16:17 +0000 Received: from DSVPR12MB827618.namprd12.prod.outlook.com ([fe80::c673:6b00:5b48:b56f]) by DSVPR12MB827618.namprd12.prod.outlook.com ([fe80::c673:6b00:5b48:b56f%5]) with mapi id 15.21.0688.011; Sat, 5 Dec 2026 04:16:17 +0000 From: Srirangan Madhavan To: Alison Schofield , Bjorn Helgaas , Dave Jiang , Davidlohr Bueso , Ira Weiny , Jonathan Cameron , Vishal Verma , linux-cxl@vger.kernel.org, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Li Ming , Joshua Machauer , Alex Williamson , vsethi@nvidia.com, alwilliamson@nvidia.com, Sai Yashwanth Reddy Kancherla , Vishal Aslot , Manish Honap , Jiandi An , Richard Cheng , linux-tegra@vger.kernel.org, Alejandro Lucero Palau , Srirangan Madhavan Subject: [PATCH v18 18/19] PCI/CXL: Flush host and device caches for CXL bus reset Date: Sat, 5 Dec 2026 04:14:52 +0000 Message-ID: <20261205041453.3946529-19-smadhavan@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261205041453.3946529-1-smadhavan@nvidia.com> References: <20261205041453.3946529-1-smadhavan@nvidia.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: MW4PR04CA0058.namprd04.prod.outlook.com (2603:10b6:303:6a::33) To DSVPR12MB827618.namprd12.prod.outlook.com (2603:10b6:8:3e5::24) X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DSVPR12MB827618:EE_|MW4PR12MB5602:EE_ X-MS-Office365-Filtering-Correlation-Id: bde3b3c0-b749-40a5-dbc1-08df5286825f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|366016|23010399003|1800799024|921020|5023799004|10067099003|11063799006|261022105027099003|56012099006|261102123227099003|261021121427099003|261021123027099003|261009223027099003|261026121527099003|261022075727099003|261026140227099003|261028144727099003|18002099003|6133799003|22082099003; X-Microsoft-Antispam-Message-Info: jNXI1qZiIaUh1XU2hde/CpnI/Tw7UP4YO3PW6RRuAkelw6Ax93VrRy7xq+jzM5eXStaDSp0qa4RtyjSFHCeGab9PW/7vyTIbUrvukRxLWz9hBlzgbzOTGYe05/FwC7gEElsouo/uKDm/93VDf+VHCqZxUlRE9h5YIMhoWift3O+4BsTzXEDDjEui3Of4JinsUOurN6/1BhHZFWFhE2PNhaq7k54OYcVwWdbX9caBuQcY2AvBQTRQ7a+Fclkzs3VH0IVfD313vThYcSYTVRYEbs+2/jM9DwsQHRG7ja+4mmvnRiylYxtzakbKm3X6jJUS7JPd5IPBqyyY+my4jKWzKUYLuEHUVFce72n8PkX+LfXYWiP1ZxirHLuVOjRYatFCu5shBGjrvmcGWrtlj8c43kPqf1y8G2xYaT83YhR8ZPYpzhUGreeTtqnkswI3l8rTUz3+sXQIU+qpOvna9CLrs2XTNWjzNCyK5lbJkDVgqypU5QVBdyZKSfctUmrvpsUY8GMhvuT6EUtUclYNf85noiey9EOWWYojgX9GYQsFkAMlBStgIla3kieaw8Ltn7QjTcuw328ky6Dutn7jzQIePFQrfQl6PeoOLnHcyem1/A6ND7pV/vXxsLU9L0/9TPhMttwFN4tI8Mabrk/nqzJZHZaYnNzd9L86vyWdGnkMpntpgHYrIL03YanHm29h3TeP05W/DoXZTGlNJPG4bgTGIA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DSVPR12MB827618.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(376014)(7416014)(366016)(23010399003)(1800799024)(921020)(5023799004)(10067099003)(11063799006)(261022105027099003)(56012099006)(261102123227099003)(261021121427099003)(261021123027099003)(261009223027099003)(261026121527099003)(261022075727099003)(261026140227099003)(261028144727099003)(18002099003)(6133799003)(22082099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?w685zhqQEHIVUhg21cPCJiqG1NBAPb79HKp8V04A7EjZv/8Qi8jA6dVVlGRx?= =?us-ascii?Q?D6PHmf12uJlnI1971cCT7TWtlZog2vPfVHT9gwLziKNYlUAMsu5yz4Jr/Rg7?= =?us-ascii?Q?6qyLTj85rY9r+EvEyUySBV+dKuhBNmQY8pk+a049mXPpnzgIuxFTnDRVChPX?= =?us-ascii?Q?w+oc7e+8j7Wz9qz7FHx5v1cgzc1N+RZEyRh4E2Jd4dpuMd9wP7NOX5lzWTRj?= =?us-ascii?Q?kkgrw5+wNV6+6/p7SHimFZWZ4DUkZ0g9qVYInW9B25l63WDds66d9+LWMNqr?= =?us-ascii?Q?9r2++1rVUN3nBTSfrZBDD1iGn5lGGFCrVDHg9Wu+yQ5ZSP4WJHyQfJ4FHVyL?= =?us-ascii?Q?y5YxNeZu8tw4wUxDVv0nSCEYF/g4rI3oSauZBRn+bR+On8JcgN2ILAcEH3Ig?= =?us-ascii?Q?kKrXAKDbzi7NdCDyNWPdlrqR0dXp1elKtcpMm88Aoyjvfv0xenhKO/yKough?= =?us-ascii?Q?/IiKXQ+T9nezY6wEvcY1G/dEgx+y/AQBsUvS/0GXdxMwZJhcOBHxPmcDg+8m?= =?us-ascii?Q?SNZFaDR0xuYL3/LOsYNaebnBrDBDCWxd9m46NV5KiUA3unCfBvkv5DraOw+a?= =?us-ascii?Q?YRGhqNnK7BXTUnuHZiUdRQ+HvCAcexWFJIcpFcndmPaYOKsp5PQTQXDrxuae?= =?us-ascii?Q?vDNAvjbD8QNEjnssh853IS0R0K6lZtdSpTilHwLG3V2g0DIVSyyXyz9oEN5Y?= =?us-ascii?Q?3phmdPrYyl2cFbDJFSS5bZAzPWgM5RB+5bQbZk9AMyatHFvBG5WAWyebAblf?= =?us-ascii?Q?vuaanbYp2BiAStU3eRj289lN/99UVGMeRAHcDfG6AN69NKHP3aDutNa4sTxO?= =?us-ascii?Q?i+s0dVNrAHD3FRfsKQGq3YoVgTqYt02aZmU8U+NFsuRt9XRCjKCFpy5Ckm3b?= =?us-ascii?Q?9ggmiohOC4XNKWaZmJg9v590GE7J6/BwYPJJ3yfWDDhZgbmyJlrnmWoyimTP?= =?us-ascii?Q?FJ/ybO8vzEbioYK/WknIEApDWQuBb6dHGFeYDIZB2X4Zw67CgmyUG6YL2dVH?= =?us-ascii?Q?PvWiKFgi13EnzraAGWvK8g7HBmoJZkYo52yv3crV8trgUJdZ3Rlj7A9NaSwI?= =?us-ascii?Q?qKwgzlVCAHmlzYY5OzzWacblXEmE5cMyOWjaVov9iuXAM3QUO7luSvpU9MV1?= =?us-ascii?Q?GB4ff7pARfnW9DOQ8/hcJYvq6c1I/1AA/nqWN5pjZZ7V+9n2QQ2TLDlfipa+?= =?us-ascii?Q?wdWr+5m1f80F+rGQYk8LTktQomlnxqQiqG5bL/IMdqs4IfCASSjPhJyZx7xI?= =?us-ascii?Q?PQzPeEW1OdWGg/m80IawB6nhj+5fwVHcWJsmmd69CPdw6Oj7v6ajPJxg2hjm?= =?us-ascii?Q?ElFyM2GSFmTc4MOsWRmLWN+YOBqbTVSpufTVwFtAL/d5s83z5ix/PQj9tgit?= =?us-ascii?Q?P0dRmYX7FPVjj3kY9V6h6OO9tyiAn4G2Kdwcv+K6FN+j47VEVzpLct54I/mH?= =?us-ascii?Q?+gYW5naiS2fd9kcMII1gWV+sU7m+Q4TdxTGxU8QEY/yfn9ZfhEo7gbjb67M8?= =?us-ascii?Q?TTUrmkIy6zlh9IglFagVWFKDkxN7uzkEGNPPIn/35yS8ouzz0EP2imB0JDcw?= =?us-ascii?Q?UyQDTR/BniB36ZhvLafMNI0SmOcF1+CagpVjj0zhE7Wyr8sx7wSdihw4s0/2?= =?us-ascii?Q?URpWwU0IzyGMCtDUkTTWr0+6pzkq/mVSjZl6NfzzkRhJMSGo7tumBj3Y+CCx?= =?us-ascii?Q?/rNdYvigvvc4p/tO47BG7UQD0vCJg2LiIREqwKoR4AXCebV2OfFjkmFraXSD?= =?us-ascii?Q?/EXZwvxRrw=3D=3D?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: bde3b3c0-b749-40a5-dbc1-08df5286825f X-MS-Exchange-CrossTenant-AuthSource: DSVPR12MB827618.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Dec 2026 04:16:17.7393 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 9qkRA9si9vRT840oTVxRHEYklSjfyXbpVyyLbAGuhpQUT+1z0+mPpDK6wp3Yr9oZ6eyOe/34EOJ5ac9npxOuGA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB5602 CXL bus reset can reset a Type 2 device while its host and device caches still contain data for the device's HDM ranges. Flush the host caches, disable device caching, and trigger device cache writeback and invalidation when supported before reset. Invalidate the host caches again before ending IOMMU exclusion. Hold the CXL region lock only to take the snapshot and reserve the affected HPA ranges, to mark the HDM registers offline right before the reset, to restore the state after the reset, and to clear the marks once the second invalidation is done. Do the host cache flushes and the bus reset without the lock. The reserved ranges and the pending mark keep new decode from coming online in between, and a region torn down during the reset does not access the device's registers. Preserve the device's cache policy across the operation. If reset was not attempted, undo preparation without changing the saved PCI command unless cleanup fails. After an attempted reset, keep the device disabled on recovery failure and do not re-enable caching against unrestored HDM state. Enable bus mastering only after the IOMMU is restored, so that no DMA request reaches the blocking domain. If no cached HDM state exists, retain the existing CXL bus reset path. For a device with cached state, reject the reset if its cache-maintenance state cannot be safely prepared or restored. An invalidation provider is required unless the existing test-only CXL_REGION_INVALIDATION_TEST bypass is enabled. Signed-off-by: Srirangan Madhavan --- Changes in v18: - Do not hold the region lock across the bus reset. Share the prepare, offline, restore and unmark windows of the CXL Reset path, and do the host cache flushes and the bus reset without the lock (Dave) https://lore.kernel.org/linux-cxl/5616c825-454e-4eba-9352-b06d98dc123a@intel.com/ - Check that a slot or parent bus reset can run before touching the device, and that the SBR unmask took effect before resetting, so a bus reset that cannot run no longer leaves the device disabled with caching off. Any result from the bus reset itself still counts as an attempted reset (Sashiko) https://sashiko.dev/#/patchset/20261105185917.2667656-1-smadhavan@nvidia.com?part=18 --- drivers/pci/cxl.c | 164 ++++++++++++++++++++++++++++++++++++---------- drivers/pci/pci.c | 35 ++++++++-- drivers/pci/pci.h | 20 ++++-- 3 files changed, 173 insertions(+), 46 deletions(-) diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c index 0d5225e8141b..a86d56b5879b 100644 --- a/drivers/pci/cxl.c +++ b/drivers/pci/cxl.c @@ -415,6 +415,11 @@ static int pci_cxl_restore_state(struct pci_dev *pdev, return cxl_restore_state(pdev, snapshot); } +static int cxl_reset_restore_cache_policy(struct pci_dev *pdev, int dvsec, + u16 saved_ctrl2); +static int cxl_reset_cache_prepare(struct pci_dev *pdev, int dvsec, u16 cap, + struct cxl_pci_cache *cache); + /* * Prepare window. Hold the region lock only long enough to capture the HDM * state, reserve its ranges and mark the device as resetting. The caller does @@ -430,10 +435,12 @@ static struct cxl_hdm_info *cxl_reset_hdm_prepare(struct pci_dev *pdev, struct cxl_hdm_info *snapshot; int rc; - rc = cxl_pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL2, - &cache->saved_ctrl2); - if (rc) - return ERR_PTR(rc); + if (cap & PCI_DVSEC_CXL_CACHE_CAPABLE) { + rc = cxl_pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL2, + &cache->saved_ctrl2); + if (rc) + return ERR_PTR(rc); + } cxl_region_quiesce_lock(); snapshot = cxl_hdm_cache_snapshot(&pdev->hdm); @@ -443,6 +450,10 @@ static struct cxl_hdm_info *cxl_reset_hdm_prepare(struct pci_dev *pdev, cxl_region_quiesce_unlock(); return snapshot; } + if (!snapshot->dvsec_ctrl_valid) { + rc = -ENXIO; + goto free_snapshot; + } /* Judge the snapshot: the live cache can change after it was taken. */ if (cxl_hdm_snapshot_is_dvsec_decoded(snapshot)) { rc = -EOPNOTSUPP; @@ -479,14 +490,13 @@ static struct cxl_hdm_info *cxl_reset_hdm_prepare(struct pci_dev *pdev, } /* - * Restore window. Take the region lock to refresh the decoder settings in - * @snapshot from the cache and write the HDM state back. Leave @pdev disabled - * on failure. + * Restore window. Take the region lock again to bring the cached decoders up + * to date and write the HDM state back. The pending mark stays set: the caller + * does the host cache flush without the lock and then calls + * cxl_reset_hdm_unmark(). * - * Refresh the decoders now rather than rely on the copy taken before the - * reset: a region commit or teardown during the reset has updated the cache, - * and restoring older decoders would leave the device decoding ranges the - * CXL core considers free. + * A region torn down during the reset has already refreshed the cache, so the + * decoders are re-read here instead of restoring the snapshot's stale copy. */ static int cxl_reset_hdm_restore(struct pci_dev *pdev, struct cxl_hdm_info *snapshot, u16 *command) @@ -495,7 +505,9 @@ static int cxl_reset_hdm_restore(struct pci_dev *pdev, cxl_region_quiesce_lock(); rc = cxl_hdm_cache_copy_decoders(&pdev->hdm, snapshot); - if (!rc) + if (rc) + pci_err(pdev, "failed to refresh cached HDM decoders: %d\n", rc); + else rc = pci_cxl_restore_state(pdev, snapshot, command); /* The registers are usable again; later teardowns write them. */ cxl_hdm_cache_set_regs_offline(pdev, false); @@ -509,7 +521,7 @@ static int cxl_reset_hdm_restore(struct pci_dev *pdev, * teardown from here on does not access the registers of a device in reset. A * teardown before this point still writes them. */ -static void cxl_reset_regs_offline(struct pci_dev *pdev) +void cxl_reset_regs_offline(struct pci_dev *pdev) { cxl_region_quiesce_lock(); cxl_hdm_cache_set_regs_offline(pdev, true); @@ -529,63 +541,143 @@ static void cxl_reset_hdm_unmark(struct pci_dev *pdev) } /** - * pci_cxl_reset_prepare() - allocate HDM state storage ahead of a bus reset + * pci_cxl_reset_prepare() - prepare HDM state and caches before a bus reset * @pdev: device about to be reset + * @cachep: receives the cache state to pass to + * cxl_restore_state_after_pci_reset() * - * Copy the cached HDM state before the reset, so that restoring cannot fail - * for lack of memory. The decoder settings are refreshed when the state is - * restored. + * Capture HDM state and reserve its ranges under the region lock, then drop + * the lock and complete host and device cache maintenance. No region lock is + * held across the reset: the device's decoders cannot commit until + * cxl_restore_state_after_pci_reset() clears the pending mark. * - * Return: storage for cxl_restore_state_after_pci_reset(), to be freed with - * kfree(); NULL when @pdev has no cached HDM state; or an ERR_PTR() on - * failure. + * Return: snapshot to be freed with kfree(), NULL when no cached HDM state is + * present, or an ERR_PTR() on failure. A cached device without a valid DVSEC + * snapshot is rejected because its cache-maintenance scope is unknown. + * Only a successful non-NULL snapshot returns with *@cachep set. The caller + * must pass both to cxl_restore_state_after_pci_reset() even if reset is never + * attempted. The snapshot remains caller-owned. All other returns leave + * *@cachep NULL and nothing reserved or marked. */ -struct cxl_hdm_info *pci_cxl_reset_prepare(struct pci_dev *pdev) +struct cxl_hdm_info *pci_cxl_reset_prepare(struct pci_dev *pdev, + struct cxl_pci_cache **cachep) { struct cxl_hdm_info *snapshot; + struct cxl_pci_cache *cache; + u16 dvsec, cap, command; + int rc; device_lock_assert(&pdev->dev); - snapshot = cxl_hdm_cache_snapshot(&pdev->hdm); - if (IS_ERR(snapshot)) - return PTR_ERR(snapshot) == -ENXIO ? NULL : snapshot; + *cachep = NULL; + if (!pdev->hdm) + return NULL; + rc = cxl_pci_device_dvsec(pdev, &dvsec, &cap); + if (rc == -ENOTTY) + return ERR_PTR(-ENODEV); + if (rc) + return ERR_PTR(rc); /* Raw reset callers may not have saved the PCI state needed for restore. */ if (!pdev->state_saved) { pci_err(pdev, "CXL bus reset requires saved PCI state\n"); - kfree(snapshot); return ERR_PTR(-EINVAL); } + cache = kzalloc_obj(*cache); + if (!cache) + return ERR_PTR(-ENOMEM); + + snapshot = cxl_reset_hdm_prepare(pdev, dvsec, cap, cache); + if (IS_ERR(snapshot)) { + kfree(cache); + return snapshot; + } + + rc = cxl_reset_cache_prepare(pdev, dvsec, cap, cache); + if (!rc && !pci_wait_for_pending_transaction(pdev)) + rc = -ETIMEDOUT; + if (rc) { + rc = cxl_restore_state_after_pci_reset(pdev, snapshot, cache, false, + rc, &command); + kfree(snapshot); + return ERR_PTR(rc); + } + + *cachep = cache; return snapshot; } /** - * cxl_restore_state_after_pci_reset() - restore CXL state after a bus reset + * cxl_restore_state_after_pci_reset() - finish a prepared reset and release its quiesce * @pdev: device that was reset * @snapshot: storage from pci_cxl_reset_prepare() + * @cache: quiesce state from pci_cxl_reset_prepare() + * @reset_attempted: reset helper was invoked and may have affected hardware + * @rc: preparation or PCI reset result * @command: returns the saved PCI_COMMAND value to apply once the IOMMU is * restored * - * Under the region quiesce, refresh the decoder settings in @snapshot from the - * cache and restore: first the PCI configuration needed to reach HDM MMIO, - * then the HDM decoder and CXL Device DVSEC state. Leave @pdev disabled on - * failure. + * Restore state through pci_cxl_restore_state(), complete host invalidation, + * and restore the cache policy changed by planned-reset preparation. When + * @reset_attempted is false, undo preparation without changing the saved PCI + * command, unless cleanup itself fails. After an attempted reset, keep caching + * and the device disabled if state restoration or host invalidation fails. + * A reset-helper error does not prove that hardware was untouched. + * Unplanned recovery uses pci_cxl_restore_state() directly and owns its own + * quiesce and cache-maintenance sequencing. * * Bus mastering stays off, because the caller has not yet restored IOMMU * translation and a DMA request now would hit the blocking domain. The caller * calls cxl_restore_command_after_pci_reset() once the IOMMU is restored. + * + * The region lock is taken only to restore the HDM state and, after the host + * cache invalidation, to clear the pending mark that blocks decoder commits. + * Complete host cache invalidation before PCI operation resumes, then release + * the reserved ranges and free @cache on every exit. @snapshot remains + * caller-owned. */ int cxl_restore_state_after_pci_reset(struct pci_dev *pdev, struct cxl_hdm_info *snapshot, - u16 *command) + struct cxl_pci_cache *cache, + bool reset_attempted, int rc, u16 *command) { - int rc; + int rc2 = 0, policy_rc = 0; + *command = 0; + if (!cache) + return rc; device_lock_assert(&pdev->dev); - rc = cxl_reset_hdm_restore(pdev, snapshot, command); - if (rc) - cxl_reset_save_disabled_state(pdev); + /* The restore result matters only after an attempted, uncorrupted reset. */ + if (reset_attempted && !rc) + rc = cxl_reset_hdm_restore(pdev, snapshot, command); + + /* Complete host invalidation before ending IOMMU exclusion. */ + if (reset_attempted || cache->cache_disabled) { + rc2 = cxl_hdm_quiesce_flush(&cache->quiesce); + if (rc2) + pci_err(pdev, + "failed to invalidate CPU cache after CXL bus reset: %d\n", + rc2); + } + cxl_reset_hdm_unmark(pdev); + /* Undo preparation on abort; enable caching after reset only if safe. */ + if (cache->cache_disabled && (!reset_attempted || (!rc && !rc2))) + policy_rc = cxl_reset_restore_cache_policy(pdev, snapshot->dvsec, + cache->saved_ctrl2); + if (reset_attempted) { + rc = rc ?: rc2 ?: policy_rc; + if (rc) + cxl_reset_save_disabled_state(pdev); + } else { + /* An aborted reset alone must not poison the saved PCI command. */ + if (rc2 || policy_rc) + cxl_reset_save_disabled_state(pdev); + rc = rc ?: rc2 ?: policy_rc; + } + + cxl_hdm_quiesce_destroy(&cache->quiesce); + kfree(cache); return rc; } @@ -728,7 +820,7 @@ static int cxl_reset_cache_prepare(struct pci_dev *pdev, int dvsec, u16 cap, int rc; rc = cxl_hdm_quiesce_flush(&cache->quiesce); - if (rc) + if (rc || !(cap & PCI_DVSEC_CXL_CACHE_CAPABLE)) return rc; /* Track an attempted change so partial failures still restore policy. */ diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 3d9647c110dd..9fd15fdb3872 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -5060,9 +5060,11 @@ static int pci_reset_bus_function(struct pci_dev *dev, bool probe) static int cxl_reset_bus_function(struct pci_dev *dev, bool probe) { + struct cxl_pci_cache *cache; struct pci_dev *bridge; u16 dvsec, reg, val; u16 command; + bool attempted = false; int rc; /* @@ -5089,19 +5091,30 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe) if (probe) return 0; + /* + * Check that a bus reset can run before touching device state, so that + * a later -ENOTTY cannot mean "nothing was reset". + */ + rc = pci_dev_reset_slot_function(dev, PCI_RESET_PROBE); + if (rc == -ENOTTY) + rc = pci_parent_bus_reset(dev, PCI_RESET_PROBE); + if (rc) + return rc; + rc = pci_read_config_word(bridge, dvsec + PCI_DVSEC_CXL_PORT_CTL, ®); if (rc) return -ENOTTY; struct cxl_hdm_info *snapshot __free(kfree) = - pci_cxl_reset_prepare(dev); + pci_cxl_reset_prepare(dev, &cache); if (IS_ERR(snapshot)) return PTR_ERR(snapshot); rc = pci_dev_reset_iommu_prepare(dev); if (rc) { pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", rc); - return rc; + return cxl_restore_state_after_pci_reset(dev, snapshot, cache, + false, rc, &command); } if (reg & PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR) { @@ -5112,14 +5125,26 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe) val); } - rc = pci_reset_bus_function(dev, probe); + if (cxl_sbr_masked(bridge)) { + /* The unmask did not take effect, so SBR would do nothing. */ + rc = -ENOTTY; + } else { + if (snapshot) + cxl_reset_regs_offline(dev); + rc = pci_reset_bus_function(dev, probe); + /* + * A bus reset method was verified above, so any result, even + * -ENOTTY from the wait for the bus, may follow a reset. + */ + attempted = true; + } if (reg != val) pci_write_config_word(bridge, dvsec + PCI_DVSEC_CXL_PORT_CTL, reg); - if (!rc && snapshot) - rc = cxl_restore_state_after_pci_reset(dev, snapshot, &command); + rc = cxl_restore_state_after_pci_reset(dev, snapshot, cache, attempted, + rc, &command); pci_dev_reset_iommu_done(dev); diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index cdbe4a8b4050..546911ad8af4 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -737,14 +737,18 @@ static inline void pci_doe_disconnected(struct pci_dev *pdev) { } #endif struct cxl_hdm_info; +struct cxl_pci_cache; #ifdef CONFIG_CXL_RESET void pci_cxl_hdm_cache_init(struct pci_dev *pdev); void pci_cxl_hdm_cache_release(struct pci_dev *pdev); int cxl_reset_function(struct pci_dev *pdev, bool probe); -struct cxl_hdm_info *pci_cxl_reset_prepare(struct pci_dev *pdev); +struct cxl_hdm_info *pci_cxl_reset_prepare(struct pci_dev *pdev, + struct cxl_pci_cache **cachep); +void cxl_reset_regs_offline(struct pci_dev *pdev); int cxl_restore_state_after_pci_reset(struct pci_dev *pdev, struct cxl_hdm_info *snapshot, - u16 *command); + struct cxl_pci_cache *cache, + bool reset_attempted, int rc, u16 *command); int cxl_restore_command_after_pci_reset(struct pci_dev *pdev, const struct cxl_hdm_info *snapshot, u16 command); @@ -756,16 +760,22 @@ static inline int cxl_reset_function(struct pci_dev *pdev, bool probe) return -ENOTTY; } -static inline struct cxl_hdm_info *pci_cxl_reset_prepare(struct pci_dev *pdev) +static inline struct cxl_hdm_info * +pci_cxl_reset_prepare(struct pci_dev *pdev, struct cxl_pci_cache **cachep) { + *cachep = NULL; return NULL; } +static inline void cxl_reset_regs_offline(struct pci_dev *pdev) { } + static inline int cxl_restore_state_after_pci_reset(struct pci_dev *pdev, - struct cxl_hdm_info *snapshot, u16 *command) + struct cxl_hdm_info *snapshot, + struct cxl_pci_cache *cache, + bool reset_attempted, int rc, u16 *command) { - return 0; + return rc; } static inline int -- 2.43.0