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.53 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796444155; b=0xWar7E3Ale9Rb1aiS2cooPJqAzaw74ID3qBHpwVYxiEInWkjMAxuaBEhUuEJ1DUqHll s8kkMQxNkicTu7yCaUdNAFsVCaIRmyMyncWK340x+QKJ42w8I8VI0E1HoqMcao9ujM0sW ylk5lwqCkmO9MBdeiXWT4aTcxaX8g4LllmceW8VvPczsgr1uzqcsGXjJCFuDxsCYPfLL4 PfCWPjgD4WRvv7NgYgDSosEtEey1DGaHKBAHhvC21HYm5NMHEJ4xKfNdVrDtCduN/8muT zHG0w2SPepLIZMv/Y49LAcAxdhuMN6KTdOKNHA6fmhl5DdJE8LSf4kBU86lmmn6BOlw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796444155; 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=3mfwaRv16ciHmzp85imCb4U0Erdy3Ws1BogFjHAgeOM=; b=Hbme890E1jRqgfFn8MoxINzTB6FUERRFWYHZKCLvB4h+U6zXc9ackCPfplgOjK5z4okM AtFykpZuRkO+hJa+W/xXisych6O/9dqrULeXuyHFzVeV3i5IwPC5dvsnSO2l2Jtu/pfX8 Y4DKIkk6uWtnEPK9tkfHrik63OJgENR/P8DUCPKF9W4NJnPw6/y0jUPJ3KikF/vxleif2 NuKkihzVgi2fNtXVD2Qw1zdBX8QE5MYUGvZt5a9U3Rvi4k959+z027+0a8uzRjjuS5CWU dGenjJ91bq2+7NIvaPWo5sInNq6pUWgmHZzeLA4sz+0GZGZ91G1G8noTxEg64XqEZcg== 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=udfxCqcc; arc=pass header.oldest-pass=0 smtp.remote-ip=40.93.196.53 Received: from SA9PR02CU001.outbound.protection.outlook.com (mail-southcentralusazon11013053.outbound.protection.outlook.com [40.93.196.53]) (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 6189F606A6A6; Sat, 5 Dec 2026 04:15:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6189F606A6A6 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=udfxCqcc ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=a9SyQ2fnamLjETZBSQCJ/eUdlXOaR3M+NrDRdzB0UHP8c58Pl0rjferpJ8ZdvIAIkeKYVpUyAp112wrton9fiJkzELOhdaFUnzR4VZo/hmlCPA5l+cNCvTlQCnNdnM1YK5v+ca/8ptB3tpz2BLDt1tYTelMsRJnPXAMZwPeSWabRnDrq3WT4H7NwBQIoZ+tOhycwDNPBjb9380uPD12+IvkNzfHdP8BTuw5tV99/5sP3jbvX0aaBxNPOHvAOqFeAFfwl+KoiimJRu/gzHUgmZqzqLl29N6TuKqK4TQdt4W0YWySM1mK5D73XnNNNChZ/EOP7p0Mi8zD3ODwodQgfCQ== 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=3mfwaRv16ciHmzp85imCb4U0Erdy3Ws1BogFjHAgeOM=; b=GuhPNZqJRM7ST1R1rP+XS6qxqCsb/AGMT9CCkwkE52yxHF1b15BXs/HYbTqGGh79Q3lUUKBf4309/bFdhesMt7zoR3W6ZVWLhvq1n0clYDHDkFWsGoMn9kyQjfPZun+5g9X9cdCRHkRJwcRSxaWg4RfWKl6TQiKuBi8GLzM0ixP2DbHSgTynggb1hxJASeenyVCzhb45eH9PIsbY6oxtKBodeo4gIHj6IFtZN2Pt3DiM/GShvaICCpsL4Ch4Xn2r7kfdsZtp7fhyoSiUOPulTeo45G3mZFAH1Fv7mSa4j7BoxGKaXrDSyCKqrtfA3qvwoHc9KESAPOEPOymqxkQOrw== 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=3mfwaRv16ciHmzp85imCb4U0Erdy3Ws1BogFjHAgeOM=; b=udfxCqcctwUOkKKMeJTaE4TnmQHYOrwcUaXEmdU0tNqQBVpK3AU/ziFkGjUQs/imRvvAC9yAvFokJ3TYvuS9gexug9TAzAOceNZwhcZwCLzUrlTXeW/2jbvnMC3BOOFuubWYcx3ueM16IIw9F+d2xtzA1tM3bNqpo7KHNPL3TOH9r3xcLpGawEEhu5Z6+VcZmzm8owGwBXH5WJvt8Se6PIrx0KtQCZ4VlTQHEcac9BXh4oYRfQdu9OMH16XnCFt5FscKLY2xZQ868/vLx4/C67ge4v4np9TORQWMV+EmoO8fiS4APtDsVmGBKOuDvE7f4prWzvO79RdSDDkfWaGtzw== 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:15:51 +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:15:51 +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 12/19] PCI/CXL: Add CXL Device Reset sequencing Date: Sat, 5 Dec 2026 04:14:46 +0000 Message-ID: <20261205041453.3946529-13-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: MW4PR04CA0135.namprd04.prod.outlook.com (2603:10b6:303:84::20) 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: 0f744607-385b-404f-f228-08df52867260 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|3023799007; X-Microsoft-Antispam-Message-Info: JMsx76KrYEy9i4GM/AWvRIqdSyP8v4ggrcCpWgId5zIQoRUJVQDGaPE8rmseGVHc0ug0jrnD4wn6Z8iVnJpRkCjd7KHCmF4RGhn84hqpzdCd17iFEml+NY4fAGPWP5ztbcbYdLzk/GyDTp4bYXLVxeN9vIZL7nMt+Yb0oNPCcBswnAYfLQO4Ql5c47KJLwK1J2biPQNtKC8fWaei8DrZkFppeMO0eMekR41RQlWzc3xBT8TwxW0JdX7H5uoleiCGzN0sSeBXB/NtAzTeYPBMqnmmIpSnbg2abeShRmAbNi+ZEQf1CUeTwEBft2CekdBWCyD138nESdW08cxubovtRqFU4A9ol0Oop3wcSeQUg+fvi8mQ9pS8NbA9DVCHitnTeBomSBPPyU4jns7j+5ULrrdeMtMAFS946WH8Yw3kPEPEPsmb0oIA4JtQf3cSHBwS8zo2i5cAWj7phQ/eft3CB6sxaybjtYiaqdBwa8hnaNDiZMLbaXlf2vw9bnP7tqPDMq4k6iz/4bNt8j5m+KFD8PqJKp6KW6N7455nhznb7W/y6BTC5rSoP9uzB08eVk09oreyOMzEnNYDWj4/dHdcDy2SWx1NyREPV9/FkOS7XHTxQm49IyWYYfAInZ0Za/t2gCqdywReaeQbOUeLI00xNBKj/0601Se4JBcuJPY2+qpRRaLIgLU8TtSzJ8kkzIYuBYK4qpWjkNGhqJ2spAdxAQ== 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)(3023799007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?nl16rUDqsF8fonddCAY2T33v605CIG+l2WbzU6r8Yh6g95tV3760VaC9d/lK?= =?us-ascii?Q?MLzYeon2wZBqn6oA/gFRN5EuP/68V6/aS7LogBxbZUqdx/n6pES6TjZ0Ie9X?= =?us-ascii?Q?Jvfi0Dq+FISAl3rspKnGzHrrK95VLQ/61G/WS63aLncpDvUeSTK9N1xCoGUI?= =?us-ascii?Q?Duy3f7239gtXa4pTyP/7/xbhg0a9/1Q5ARHiboDQ/rjNtzbqcAnndQ1ovNcl?= =?us-ascii?Q?3BPxPw7Vc8cwwTlJecsLPSCsK+osbcg7ZXgb19ZHPtAeaIJGj/yLDd/Asp+p?= =?us-ascii?Q?bej9qiDGL7xkylvalZk4LRdtk22HfRrvp+RdXrfdkC/IajgPTYR0Sk2qZL6p?= =?us-ascii?Q?Czru5YtygwBHM+zskZ+dKmD45CbaWhaPjttj2f+4TRF1zmEfJFU3yHNxEqS+?= =?us-ascii?Q?0/p827WhOsMC+JrVOGBk0ZVOZN2s/Lzzqa5zmQa5af/rOq7mv/aznks35LQQ?= =?us-ascii?Q?qggBsG5lGe3goWmZGmbI/ooQnJ9DRov4ZMn2k5hIE4LF85i82UQQ3RrGWYcI?= =?us-ascii?Q?UCzIG/0sPjstn1JFuaw8ifir/LJiSQTLw//2NP9cQ0MPmSlrQo3fpRANG6Ow?= =?us-ascii?Q?CGrNcZ8lWyCoeXBr8MjYPffC0rSI4rOUm08eTaEclt6jEFPLQQhuY9QcNfDl?= =?us-ascii?Q?HvCVo4/Nfn36UVnNX8AReoXhkByNcTBgu2XIIs9cKiYuNiNg1Mb8XWOQA1T3?= =?us-ascii?Q?ONXH0gWALNE4GdZXkPlhbe0/NHYlzjhiofYjChHVjptUQlA7b4Yty9l7Lcvt?= =?us-ascii?Q?6FCqfL/UrjyWKG8zjxyE84o/r7KDajCBJPR3AwILgTNVgA79znkY7eY9pmoi?= =?us-ascii?Q?vzIZOb+D/4ruG6XmqCRSaoKKK5dvyVSFHDesndlhXK3VbFoprgqMiwAwF8gC?= =?us-ascii?Q?GHp01tGKuZcwX2HZm/tN17QiOehbpsALDPt2NC7/sflV6KsrCK18/P0gbeXi?= =?us-ascii?Q?A6GuXIQdtiigwnnwt6s2yITVDLbntXhOX21Ox5dzTjLB7ZO9F1n+FnDiLpQM?= =?us-ascii?Q?5vXV1YtSY3/+bTSErakj1Qu3l1Vmsu1SheAU2aKaN34TJlcZkbBSBKA9GLJ7?= =?us-ascii?Q?R4oQU8wZeMT0viOCCpZrPr7JKbB7N0uyYeQQ6TWH2zAtzkdE6Xyd45e3tw/j?= =?us-ascii?Q?hP/w6zvCc3LACmrZnTcGWIiVxNnYgz4wNHZ2zCy0jyl1h6x1eBigJls1XWL9?= =?us-ascii?Q?ZHiMdnxqlglKnoEd3hcuBZx4DDES6ypJUwINJaYRCZJA1dfn5Pxeqjsr8MXJ?= =?us-ascii?Q?fTGbvuroQYixVdLN+hpOtOHC5lKuGWAoynXIvBI4fcSNl+yZaLdVQePqd/r9?= =?us-ascii?Q?2KbsnJY19D+sZpD2sjEBUBEa/HjFYC4IZYsLsaqocl+LznWi0tF5PJpt9w+h?= =?us-ascii?Q?bEdSz9ePr1a/hD4SiO/VkV8hpzaFVpjmW4imquJryEcHkjHSfz9sUtst35wp?= =?us-ascii?Q?kcgAOW8FY7XK/YK+wSbSzDEbFIUeYZqJxALiDUc99i+LpeIgpEQxENSHSzu8?= =?us-ascii?Q?8V5dEQTtVRW4BEvQC3fvGycw6wPV3xjcWH1ergCtOaUbbbsuAEpSI45LKU/G?= =?us-ascii?Q?PZ65Wy+nmPhOGSMw58GT8tvJs8+jR5X8LjAqiM862QrxNcqp8MKRnGR1yuMs?= =?us-ascii?Q?0agmyi0tyG3/7MNATM/FTMmOht+Zi2SiKjyPRxobdDsda3eEYjP7SQdBg0Hv?= =?us-ascii?Q?158O6yVUUmAOypIljcYmSUvsRWzsHOL4e+EDK4oA07xCF2y8BWtFXfJteytu?= =?us-ascii?Q?KpwuB9IRZg=3D=3D?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0f744607-385b-404f-f228-08df52867260 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:15:50.9066 (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: KjneTcDhWYDzYZIibGARiHItI/5zffdWZiwFDS1CsETbnTzkijpZ/tAMAxRzSoZbxkTkfjBX29cvPHXWmE8TEw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB5602 Implement CXL Device Reset for eligible Type 2 devices: disable CXL.cache, perform supported writeback, drain transactions, prepare the IOMMU, initiate reset with Memory Clear enabled, and wait for completion. Observe the 100 ms post-reset quiet period before polling Status2, and treat an all-ones read as not ready. Wait for the timeout the device advertises, up to 100 seconds, and access only Status2 until reset completion, error or expiry, as CXL 4.0 section 9.7.3 requires. Control2 cleanup runs only afterwards. Treat Control2 command bits as transient state, restore cache policy on exit, preserve the primary error, and abort when IOMMU preparation fails. Reuse Device DVSEC discovery from cache initialization and keep the reset entry point declaration private to PCI core. Signed-off-by: Srirangan Madhavan --- Changes in v18: - Reword the reset wait message to "Waiting for CXL reset to complete" and log an error when the wait times out (Li Ming) https://lore.kernel.org/linux-cxl/568d7c66-eb67-4369-8b15-5ee923471881@zohomail.com/ - Use cxl_pci_read_config_word() for the Control, Control2 and Status2 reads that reject an all-ones value, instead of open-coding the check (Li Ming) Reference branch: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/commit/?id=04f8f84816d724f8b47f4d47a77916ac37c63a49 --- drivers/pci/cxl.c | 241 +++++++++++++++++++++++++++++++++- drivers/pci/pci.h | 5 + include/uapi/linux/pci_regs.h | 12 ++ 3 files changed, 257 insertions(+), 1 deletion(-) diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c index 56119e3fd03a..4d6cfabab2c0 100644 --- a/drivers/pci/cxl.c +++ b/drivers/pci/cxl.c @@ -3,13 +3,17 @@ /* * CXL reset support in the PCI core. Cache the HDM decoder state of a CXL * memory device at enumeration, so it can be restored after a reset without - * a CXL driver. HDM register formats and programming live in drivers/cxl. + * a CXL driver, and implement CXL Device Reset. HDM register formats and + * programming live in drivers/cxl. */ #include #include +#include #include #include #include +#include +#include #include #include #include @@ -224,3 +228,238 @@ void pci_cxl_hdm_cache_release(struct pci_dev *pdev) { cxl_hdm_cache_release(&pdev->hdm); } + +/* + * CXL r4.0 sec 9.7.2 defines the reset completion timeout encodings. + * Sec 9.7.3 leaves config-space access behavior undefined for 100 ms after + * initiating CXL Reset, then limits software to CXL Status2 access until + * reset completion, timeout, or error. Wait for the full advertised timeout + * so that Control2 is not accessed while the device may still be resetting. + */ +#define CXL_RESET_RRS_WAIT_MS 100 +#define CXL_RESET_STATUS_POLL_MS 20 +static const u32 cxl_reset_timeout_ms[] = { + 10, 100, 1000, 10000, 100000, +}; + +/* + * CXL 4.0 section 9.6 permits using the device cache size to determine + * a timeout. Use a conservative fixed estimate of 100 ms. + */ +#define CXL_CACHE_WBI_TIMEOUT_US 100000 +#define CXL_CACHE_WBI_POLL_US 100 + +#define CXL_RESET_CTRL2_CMD_MASK \ + (PCI_DVSEC_CXL_INIT_CACHE_WBI | PCI_DVSEC_CXL_INIT_CXL_RST) + +static int cxl_reset_update_ctrl2(struct pci_dev *pdev, int dvsec, + u16 set, u16 clear) +{ + u16 ctrl2; + int rc; + + rc = cxl_pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL2, &ctrl2); + if (rc) + return rc; + + /* + * INIT_CACHE_WBI and INIT_CXL_RST are commands. Do not replay a command + * that remains set in the value read back while updating persistent + * Control2 state. + */ + ctrl2 &= ~CXL_RESET_CTRL2_CMD_MASK; + ctrl2 &= ~clear; + ctrl2 |= set; + + rc = pci_write_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL2, ctrl2); + if (rc) + return pcibios_err_to_errno(rc); + + return 0; +} + +static int cxl_reset_wait_cache_wbi(struct pci_dev *pdev, int dvsec) +{ + unsigned long deadline; + u16 status2; + int rc; + + rc = cxl_reset_update_ctrl2(pdev, dvsec, + PCI_DVSEC_CXL_INIT_CACHE_WBI, 0); + if (rc) + return rc; + + deadline = jiffies + usecs_to_jiffies(CXL_CACHE_WBI_TIMEOUT_US); + while (time_before(jiffies, deadline)) { + usleep_range(CXL_CACHE_WBI_POLL_US, + CXL_CACHE_WBI_POLL_US * 2); + + rc = cxl_pci_read_config_word(pdev, + dvsec + PCI_DVSEC_CXL_STATUS2, + &status2); + if (rc) + return rc; + if (status2 & PCI_DVSEC_CXL_CACHE_INV) + return 0; + } + + pci_warn(pdev, "timed out waiting for CXL cache writeback and invalidate\n"); + return -ETIMEDOUT; +} + +static int cxl_reset_disable_cache(struct pci_dev *pdev, int dvsec, u16 cap) +{ + int rc; + + rc = cxl_reset_update_ctrl2(pdev, dvsec, + PCI_DVSEC_CXL_DISABLE_CACHING, 0); + if (rc || !(cap & PCI_DVSEC_CXL_CACHE_WBI_CAPABLE)) + return rc; + + return cxl_reset_wait_cache_wbi(pdev, dvsec); +} + +static int cxl_reset_restore_cache_policy(struct pci_dev *pdev, int dvsec, + u16 saved_ctrl2) +{ + int rc; + + rc = cxl_reset_update_ctrl2(pdev, dvsec, + saved_ctrl2 & PCI_DVSEC_CXL_DISABLE_CACHING, + PCI_DVSEC_CXL_DISABLE_CACHING); + if (rc) + pci_err(pdev, "failed to restore CXL cache policy: %d\n", rc); + + return rc; +} + +static int cxl_reset_wait_done(struct pci_dev *pdev, int dvsec, u16 cap) +{ + unsigned long deadline; + u32 timeout_ms; + u16 status2; + int idx, rc; + + idx = FIELD_GET(PCI_DVSEC_CXL_RST_TIMEOUT, cap); + if (idx >= ARRAY_SIZE(cxl_reset_timeout_ms)) { + idx = ARRAY_SIZE(cxl_reset_timeout_ms) - 1; + pci_warn(pdev, + "unknown CXL reset timeout encoding; using %u ms\n", + cxl_reset_timeout_ms[idx]); + } + + /* Allow one retry after the mandatory 100 ms quiet period. */ + timeout_ms = max_t(u32, cxl_reset_timeout_ms[idx], + CXL_RESET_RRS_WAIT_MS + CXL_RESET_STATUS_POLL_MS); + pci_info(pdev, "Waiting for CXL reset to complete (max wait: %u ms)\n", + timeout_ms); + msleep(CXL_RESET_RRS_WAIT_MS); + deadline = jiffies + msecs_to_jiffies(timeout_ms - + CXL_RESET_RRS_WAIT_MS); + + for (;;) { + rc = pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_STATUS2, + &status2); + if (rc) + return pcibios_err_to_errno(rc); + if (status2 != U16_MAX) { + if (status2 & PCI_DVSEC_CXL_RST_ERR) + return -EIO; + if (status2 & PCI_DVSEC_CXL_RST_DONE) + return 0; + } + + if (time_after_eq(jiffies, deadline)) { + pci_err(pdev, "timed out waiting for CXL reset to complete\n"); + return -ETIMEDOUT; + } + + msleep(CXL_RESET_STATUS_POLL_MS); + } +} + +static int cxl_clear_memory(struct pci_dev *pdev, int dvsec, bool initiate) +{ + if (initiate) + return cxl_reset_update_ctrl2(pdev, dvsec, + PCI_DVSEC_CXL_INIT_CXL_RST | + PCI_DVSEC_CXL_RST_MEM_CLR_EN, + 0); + + return cxl_reset_update_ctrl2(pdev, dvsec, 0, + PCI_DVSEC_CXL_RST_MEM_CLR_EN); +} + +static int __cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap) +{ + int rc, rc2; + + if (!pci_wait_for_pending_transaction(pdev)) { + pci_err(pdev, "timed out waiting for pending transactions\n"); + return -ETIMEDOUT; + } + + rc = pci_dev_reset_iommu_prepare(pdev); + if (rc) { + pci_err(pdev, "failed to stop IOMMU for CXL reset: %d\n", rc); + return rc; + } + + rc = cxl_clear_memory(pdev, dvsec, true); + if (!rc) + rc = cxl_reset_wait_done(pdev, dvsec, cap); + + /* Always clear Memory Clear; successful cleanup must not hide reset failure. */ + rc2 = cxl_clear_memory(pdev, dvsec, false); + if (rc2) + pci_err(pdev, "failed to clear CXL Reset Memory Clear: %d\n", rc2); + rc = rc ?: rc2; + + pci_dev_reset_iommu_done(pdev); + return rc; +} + +static int cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap) +{ + u16 saved_ctrl2; + int rc, rc2; + + rc = cxl_pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL2, + &saved_ctrl2); + if (rc) + return rc; + + rc = cxl_reset_disable_cache(pdev, dvsec, cap); + if (!rc) + rc = __cxl_reset_execute(pdev, dvsec, cap); + /* Restore cache policy after any attempt to disable caching. */ + rc2 = cxl_reset_restore_cache_policy(pdev, dvsec, saved_ctrl2); + return rc ?: rc2; +} + +int cxl_reset_function(struct pci_dev *pdev, bool probe) +{ + u16 dvsec, cap, ctrl; + int rc; + + rc = cxl_pci_device_dvsec(pdev, &dvsec, &cap); + if (rc) + return rc; + if (!(cap & PCI_DVSEC_CXL_CACHE_CAPABLE) || + !(cap & PCI_DVSEC_CXL_MEM_CAPABLE) || + !(cap & PCI_DVSEC_CXL_RST_CAPABLE) || + !(cap & PCI_DVSEC_CXL_RST_MEM_CLR_CAPABLE)) + return -ENOTTY; + + rc = cxl_pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL, &ctrl); + if (rc) + return rc; + if (!(ctrl & PCI_DVSEC_CXL_CACHE_ENABLE) || + !(ctrl & PCI_DVSEC_CXL_MEM_ENABLE)) + return -ENOTTY; + + if (probe) + return 0; + + return cxl_reset_execute(pdev, dvsec, cap); +} diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index ea5126e86f4a..5bfa77fd1d37 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -739,9 +739,14 @@ static inline void pci_doe_disconnected(struct pci_dev *pdev) { } #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); #else static inline void pci_cxl_hdm_cache_init(struct pci_dev *pdev) { } static inline void pci_cxl_hdm_cache_release(struct pci_dev *pdev) { } +static inline int cxl_reset_function(struct pci_dev *pdev, bool probe) +{ + return -ENOTTY; +} #endif #ifdef CONFIG_PCI_NPEM diff --git a/include/uapi/linux/pci_regs.h b/include/uapi/linux/pci_regs.h index ca3e77b72d87..7224857c7b30 100644 --- a/include/uapi/linux/pci_regs.h +++ b/include/uapi/linux/pci_regs.h @@ -1352,10 +1352,22 @@ #define PCI_DVSEC_CXL_CACHE_CAPABLE _BITUL(0) #define PCI_DVSEC_CXL_MEM_CAPABLE _BITUL(2) #define PCI_DVSEC_CXL_HDM_COUNT __GENMASK(5, 4) +#define PCI_DVSEC_CXL_CACHE_WBI_CAPABLE _BITUL(6) #define PCI_DVSEC_CXL_RST_CAPABLE _BITUL(7) +#define PCI_DVSEC_CXL_RST_TIMEOUT __GENMASK(10, 8) #define PCI_DVSEC_CXL_RST_MEM_CLR_CAPABLE _BITUL(11) #define PCI_DVSEC_CXL_CTRL 0xC +#define PCI_DVSEC_CXL_CACHE_ENABLE _BITUL(0) #define PCI_DVSEC_CXL_MEM_ENABLE _BITUL(2) +#define PCI_DVSEC_CXL_CTRL2 0x10 +#define PCI_DVSEC_CXL_DISABLE_CACHING _BITUL(0) +#define PCI_DVSEC_CXL_INIT_CACHE_WBI _BITUL(1) +#define PCI_DVSEC_CXL_INIT_CXL_RST _BITUL(2) +#define PCI_DVSEC_CXL_RST_MEM_CLR_EN _BITUL(3) +#define PCI_DVSEC_CXL_STATUS2 0x12 +#define PCI_DVSEC_CXL_CACHE_INV _BITUL(0) +#define PCI_DVSEC_CXL_RST_DONE _BITUL(1) +#define PCI_DVSEC_CXL_RST_ERR _BITUL(2) #define PCI_DVSEC_CXL_RANGE_SIZE_HIGH(i) (0x18 + (i * 0x10)) #define PCI_DVSEC_CXL_RANGE_SIZE_LOW(i) (0x1C + (i * 0x10)) #define PCI_DVSEC_CXL_MEM_INFO_VALID _BITUL(0) -- 2.43.0