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.201.65 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796444178; b=KpMNPn3YLbyF9BN8jXp8aCytxWT9w7PX3AWgc9V4JpdRecwJ+DXM+984yfJJQNFRP/bG ztabY16P43sWfZl9mPIED0FG6wP2a688JQfY/8R+mGTml36TJrLaWuHNLx5xmKbdCRbet lrpO7bYCNYgJ+PpQBMqwUXLTSeHRonxdKiHb6LgS+XuBoLv+VaNUJLBPcYIfAvX2L5H0j qjf+IShQRaE/yQlMAE1bOqtfHKD2AIqC+LEoCUbqlkgFFRcgfoxTIPLuwmWaOhrioRlQg SJqh8hY6iPhiu/p2XG2gmTujytLB95FnFd1/kkxcuY/dPPu8B6vOpo66PAgAga/Qx9Q== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796444178; 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=pjEzFBw2PSiBT7HOtD37d3RVEoReVtB/4oM8j/w/3+o=; b=mp4HxhjCO4ITtt6sPenjZHkB5F5xlMaTlUCAbv0MPVWVunvkETnEqpcJqXF0vyeHvJ8X Y/5HXftUaxPJshoV72Kq1eOondlSIKeuL0AOs6eBaHp7AwIrsm1iBGe0xVioDa3a/vKv7 tYLk51FEmdC4NF7XrmKMumyvFe4Od02G03jbCqDF4T+PWvH+ZOF1zv71MjaOtNhLBh9al Qpk9WnQudcHWRlxQ2cGIXGc6IyZJVY5m2ywF5UTGQA748xd3ZeiBR4runh5ApMbqfNIqA +6E2yJOu2C0TDmpY8BOvKXAN2D9QBmNdGgT0N+6ppY2jHh9uOZzJ4tJ26CrQfKfnWww== 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=mGOLVZXx; arc=pass header.oldest-pass=0 smtp.remote-ip=40.93.201.65 Received: from CY3PR05CU001.outbound.protection.outlook.com (mail-westcentralusazon11013065.outbound.protection.outlook.com [40.93.201.65]) (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 0D1F1606A332; Sat, 5 Dec 2026 04:16:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0D1F1606A332 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=mGOLVZXx ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=kAIWzdLeK0328mknC5Vhu7/wICjceInK3zVJWcRfgoJPR38ORmR7ZP8lZM8ti+HJQVcprYjl3D9mxhix8aSb/8uTY661TrfneNZf3uxOySL+MYpkacrK45L618hAtjVgYnpNpT1kuDWu4p6BIpGvb6AGtzqT+gJynbBS2qvFDYYmR3PHC7qjS1Gjyt7IAoJMUoKIjEcXcBvx/OLillUv9CCZbMdvgvS1NCL3aQ4DwOnNkGeydFQ3mho8GpDHdDIGuz8swqHhVrQV6dBkDgXZZ+bnT43l7jnkPMS03xfKfdFrWP83mx8vhF7s9JxbzRwbcWdM8tjUPwD7oXibNSzEMg== 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=pjEzFBw2PSiBT7HOtD37d3RVEoReVtB/4oM8j/w/3+o=; b=FeJwmQHzpeuYjbunV50r3VtvOvR9Tb4Tf+SNQEIpNTZcEN8/i167braVlerSRfCUvdQAICIehrQQXKdEaduCzjnzW5TFAJfYzuPRssEgPu7FboyIF/psrvuLxM45DuPG+t9r0pJIeP37o4Dm9GFexpFOeAbc+ycu9rC5kb1cmrOBbMZpSTYw9XXRn3e8PjUEplJm/eChTcgIkDFl1vM5akaVSEA0lhhRovLMXg/cR4U6W7E10Ih1UCF6qxQ/aggP6CnycKuhN/UrYWQHdrUmSG4mY1Kiy9Ngeof/tETorDXEaSSSOwA2S1YGyyK2djADsJxdbykNDzl+fdq/hJ7+8g== 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=pjEzFBw2PSiBT7HOtD37d3RVEoReVtB/4oM8j/w/3+o=; b=mGOLVZXxzM6Jv+t1S+SgyNpYvMCUzAu+eU/M/ODzXApVVTx6Yx5kGUoMz3+EAA61q6SY4VW+4IzrHLD96MvY0AYOcS4MBzwm2xoCp5Qecp9PvoU4a54iEIoqCmEhcM2/HrpooA8cFNj7MPdrRCMp7GCi27vFD+LYco6/L28VLC+gk5AFw9jBiqkb7jnFa0V7lDO19mduv+3L5Ro5FT7usJ8NnFxoWLsRSsBOPIRFldWNKba6E7YSHDkDsXHNT/P0vyqd0zZxe9ZTIWskzTsEIFpmrzIXx3LgkQDfllQJo3WN+MqyK5+otZYTGO9Nl7DdXPS/m9D/C4d2lEN4TVIO5Q== 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:14 +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:14 +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 17/19] PCI/CXL: Restore CXL state after CXL bus reset Date: Sat, 5 Dec 2026 04:14:51 +0000 Message-ID: <20261205041453.3946529-18-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: BY5PR04CA0008.namprd04.prod.outlook.com (2603:10b6:a03:1d0::18) To LY0PR12MB727857.namprd12.prod.outlook.com (2603:10b6:408:3ba::5) 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: 2acc02a6-1874-48d1-431e-08df52867ff8 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|261022113327099003|261026130827099003|261022105027099003|261022115727099003|261026135527099003|56012099006|261102123227099003|261021133027099003|261021135927099003|261021130227099003|261021121427099003|261021123027099003|261009223027099003|261026121527099003|261022075727099003|261026140227099003|261028144727099003|18002099003|6133799003|22082099003; X-Microsoft-Antispam-Message-Info: c+VX1pqEwgryM4hJF+HgHupSRwv59veLNr28Rzm8Gux3r6x3Xran1TUlzx9gm3HHHHy63jnCDKfHUQUmyeEehfmYua2Not/AZtY4a6Or/w8bggEvijFaOXcKHopjdtWyhYOgNLh4AGq0C38RpkNkJOoPrBk1tam/REz8aNG3gpKl+1C0Zo5IvdituwxPHYE5fbIN4xNdysXCNUhUIcwa5K36m6or+m5L4b0yFO+mQKeYMRUrKd9adHklsx0KwvlpMtyD0yaHznXx6Natw9Rr96WEy9dDmHsepBcTlQQAvR4Puly8344hW34pZ9xd/mgSJSXMAtjo3eO4rvwNTOlMVHAMlnjF1V3wZtOm1LtpEdLEPR7DgJB9h8DOImg2QZdiaHMr4KFgTbFIbEKh55/Lr97ZiB5fnJqyBLNxBoTS0lg2qFc//2urVCU6O9RS/WhZ1tDfheVdtoG1TNKQmSo/CM9hVHfazmfgfRq2HjXfTOZ+gEgV1PGRQLRN4o4gMdIWTWLnAxyw/vC5yDnYlcS+ZvX0Kh3hl+koA1CHycz/i0Dv7TeqcUnnbIq8BSKvEoirgd6xAXeqUHMV3G4NUomTt7IcL4E98INk7ArP6mPPwF9Qyh1mFU1ju4tw3VlBBNw2c2FwiuLaiOJe94TnJ7kxQ9IRKqbl0EmRkC2O2CbzimA6YsS/UBVFaS1Jtzs7zC/2XOghuaRL9pzbH2IDD+p1qQ== 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)(261022113327099003)(261026130827099003)(261022105027099003)(261022115727099003)(261026135527099003)(56012099006)(261102123227099003)(261021133027099003)(261021135927099003)(261021130227099003)(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?btQPWF5xZk/LWp6pJ9S61ad5eoy5Q1lhe2yFCVWV4ss+FuKC3vkhgrYuCUY4?= =?us-ascii?Q?8YgAtrwRHT9kFux85//sYB37K5EQUYiTYoZzd51N3mR6yCHAysAPFDnh1LZS?= =?us-ascii?Q?K/KY/TqkIgRL8nnuTB41KS8jZy2y+hglyc0jFC3AK3pJNsG2pGZ3/bzWvFYt?= =?us-ascii?Q?jhNyLCx+00vWVd+6rCFU65IpasWRByzUJv269qQoomz5RHgKFLjL0AHp4Sn+?= =?us-ascii?Q?2rbk/NapvwPebgkzdhjuuf95S5oljNuO/ck40TenlVB4+s1VNcd/9Yu/xHX4?= =?us-ascii?Q?an/4rHRUTZ7v8ZjwK4PBQ6+cBywsJlY+acCYx2geY1Rl2V3CPU9KcoHoSrFl?= =?us-ascii?Q?sVaD8AFyF/VBiwg4nuF47qO1l7EMkRQjg/YSJii2F7JzUTZ07acMcp8VJZWa?= =?us-ascii?Q?iz50/J09Ck0nrhZnyEKJIzy9t7+BPqYyVbU+Z85ZidIbpey8pHW+o0rILr6N?= =?us-ascii?Q?ByhM3gK1oUKgLSfXoXt7M4Oz4zSEfYYUpzVN9VWEcQK6peHlt4Ujl+tnR0wa?= =?us-ascii?Q?RnMhTrH3VHTTtPrOQ3gBlFshugGQXZjcGrtS5WvUjT71PeQgEnQtxl5wGNXI?= =?us-ascii?Q?qzd3SDeieivJ72TTt7wMgS3TVNx8DfYqELRypQ4Ii3/uOC46eVTcl+fLQ3sJ?= =?us-ascii?Q?M7RK1uYA8etejARWHfsjcy3TX2T1KTr623lnNnEVVSiAlJxgP2C7v4h3kYKU?= =?us-ascii?Q?gIUG+zjgT7URtoaw2zKfGkkeCJP1YCY5HkKwxTe9fatuDnRYqpdnSDwnOlVn?= =?us-ascii?Q?+BovSms2mRcO4/O3YIn12O3pefakFjuiL6gJpnPgTUOG9HxhtMgigN1ovkm7?= =?us-ascii?Q?t0f5HlnmPtTqUuNnYQMLdTlewSg5zYi85rn6u21wXdk7f/1L4Y+DtiNLHRct?= =?us-ascii?Q?41p3svU7SkznJa1EBJaHp/e9FPXums/XPa8F1SmQ6Rp/fh7tT/hEN4b6RoFp?= =?us-ascii?Q?0TEb4/bJhCTqZ49uve918nBpyVbgO5OVxfi42zoVkB3SV4AqVKyc7xbVUyTk?= =?us-ascii?Q?BI6JgaD/31Vekxzs4YFQg+RCSM82jGWy/ym1HfE6qwR1ePl7EMFIOliv6EZK?= =?us-ascii?Q?QZ/xIJS6wAl8eklCrZ2Q3MLjR2tSNobRC+0fE/oGZgDc4Qb6d8kGQigaFmtq?= =?us-ascii?Q?6hS6PyOrMGuOBpWhybps5Vq6dbsa3I54+yye7rbjXi30YUiwOTYzclJSnqPd?= =?us-ascii?Q?chD36cEpqypJvEPO8JvugRp9SAxhsZAWN+ayapu+ynyo4EWWCcYYrH3OvjR7?= =?us-ascii?Q?JZLMcqpgw6x8TGJ/EIDgR8Q6l/bZkDdoTe5i1LMGugCncS/x8HJFmMg9Ct4K?= =?us-ascii?Q?thNO6mJAuLynPJuTnNwGz1/jiYSb5BzKEmUjKmmOKD0X+q5uXPkfW6jF8DcP?= =?us-ascii?Q?cuNskX82fp4dvkG0tBdmAX2wOpdrkVQB/yVrKo7r1dqc97v+9xj+6wunaYIb?= =?us-ascii?Q?Rb/iUtGyiP0euN8OP9ZapQx+vrengV9wGCfyVEBrtvu7v6ahwDZQuwNkxkKW?= =?us-ascii?Q?ysZayK5K8Z0/Gf1uAerX21iz0G22BeppLRjixXjEKdB1YayKx+4r6R8Cu8Bp?= =?us-ascii?Q?IKKvW8Zio0P5qV69huh8KWBGRs6kENKcnphcp79J7tQ5pqZQSR8Nt3mLnWVc?= =?us-ascii?Q?sJJYcMao2GJ9tfFmp85FzS7gs1DphYOMt2TPbcc0psFdSlbirnzLmRXj8yOZ?= =?us-ascii?Q?WBU5+O+ITols9HU8euDpKc2pCi0/Dcf1kZxextdBuOpmEOChrbwZl4vBUI+y?= =?us-ascii?Q?wZ4KGH/3YQ=3D=3D?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2acc02a6-1874-48d1-431e-08df52867ff8 X-MS-Exchange-CrossTenant-AuthSource: LY0PR12MB727857.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Dec 2026 04:16:14.0078 (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: 6gfntTWvBkQnJUEq/7DMLet52eMZIiwAmQBj2ke5l+YS8e5jwIzyCFXk1UcDFQXdKf/0aOaC2irXNAc2/tzyTw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB5602 CXL bus reset can clear HDM decoder programming and CXL Device DVSEC control state. Restore the cached state after a successful cxl_bus reset while IOMMU exclusion remains active. Restore PCI configuration first so HDM MMIO is accessible, and preserve a disabled state if restoration fails. Copy the cached state before the reset, then refresh its decoder settings from the cache under the CXL region quiesce once the reset completes. A region decommit during the reset updates the cache, and restoring the earlier decoders would leave the endpoint decoding an HPA range that the CXL core considers free. Keep global and DVSEC control from before the reset, since a refresh during the reset can read them as all-ones. The prepare/restore interface and decoder-refresh structure follow Dave Jiang's cxl-type2-reset reference branch. Signed-off-by: Srirangan Madhavan Acked-by: Bjorn Helgaas # pci/pci.c --- Changes in v18: - Refresh the decoders from the cache in the shared restore window, so the CXL Reset path also restores the decoders as the CXL core last left them, now that a region can be torn down during the reset (Dave) https://lore.kernel.org/linux-cxl/5616c825-454e-4eba-9352-b06d98dc123a@intel.com/ - Turn CXL.cache back on in cxl_restore_command_after_pci_reset(), after the DVSEC Control is restored with it off, so the device does not cache again before the host caches are flushed (Sashiko) https://sashiko.dev/#/patchset/20261105185917.2667656-1-smadhavan@nvidia.com?part=18 - Declare and initialize the __free() snapshot in cxl_reset_bus_function() in one statement (Sashiko) https://sashiko.dev/#/patchset/20261105185917.2667656-1-smadhavan@nvidia.com?part=17 Reference branch: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/commit/?id=b93f0c8e67b198a63786fd3721b7177ab9df60be --- drivers/cxl/core/hdm_state.c | 31 +++++++++++ drivers/pci/cxl.c | 104 +++++++++++++++++++++++++++++++++-- drivers/pci/pci.c | 14 +++++ drivers/pci/pci.h | 28 ++++++++++ include/cxl/hdm.h | 2 + 5 files changed, 175 insertions(+), 4 deletions(-) diff --git a/drivers/cxl/core/hdm_state.c b/drivers/cxl/core/hdm_state.c index 497709bffff2..139a141c0eac 100644 --- a/drivers/cxl/core/hdm_state.c +++ b/drivers/cxl/core/hdm_state.c @@ -284,6 +284,37 @@ struct cxl_hdm_info *cxl_hdm_cache_snapshot(struct cxl_hdm_info *const *slot) return copy; } +/** + * cxl_hdm_cache_copy_decoders() - refresh the decoders of a snapshot + * @slot: cache pointer owned by the device + * @dst: snapshot of the same device from cxl_hdm_cache_snapshot() + * + * Copy only the per-decoder settings, which region commit and teardown keep + * current. Leave the rest of @dst, including global and DVSEC control, as + * captured: a refresh during a reset may have read those registers from a + * device that returned all-ones. + * + * Return: 0 on success, -ENXIO when nothing is published, or -EINVAL when + * @dst holds a different number of decoders. + */ +int cxl_hdm_cache_copy_decoders(struct cxl_hdm_info *const *slot, + struct cxl_hdm_info *dst) +{ + struct cxl_hdm_info *info; + + lockdep_assert_held_write(&cxl_rwsem.region); + guard(rwsem_read)(&cxl_rwsem.dpa); + info = *slot; + if (!info) + return -ENXIO; + if (info->decoder_count != dst->decoder_count) + return -EINVAL; + + memcpy(dst->settings, info->settings, + flex_array_size(info, settings, info->decoder_count)); + return 0; +} + void cxl_region_quiesce_lock(void) __acquires(cxl_region_quiesce) __context_unsafe(token maps to cxl_rwsem.region) diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c index cc2d5a4f67df..0d5225e8141b 100644 --- a/drivers/pci/cxl.c +++ b/drivers/pci/cxl.c @@ -479,9 +479,14 @@ static struct cxl_hdm_info *cxl_reset_hdm_prepare(struct pci_dev *pdev, } /* - * Restore window. Take the region lock again to 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(). + * 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. + * + * 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. */ static int cxl_reset_hdm_restore(struct pci_dev *pdev, struct cxl_hdm_info *snapshot, u16 *command) @@ -489,7 +494,9 @@ static int cxl_reset_hdm_restore(struct pci_dev *pdev, int rc; cxl_region_quiesce_lock(); - rc = pci_cxl_restore_state(pdev, snapshot, command); + rc = cxl_hdm_cache_copy_decoders(&pdev->hdm, snapshot); + if (!rc) + 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); cxl_region_quiesce_unlock(); @@ -521,6 +528,95 @@ static void cxl_reset_hdm_unmark(struct pci_dev *pdev) cxl_region_quiesce_unlock(); } +/** + * pci_cxl_reset_prepare() - allocate HDM state storage ahead of a bus reset + * @pdev: device about to be 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. + * + * 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. + */ +struct cxl_hdm_info *pci_cxl_reset_prepare(struct pci_dev *pdev) +{ + struct cxl_hdm_info *snapshot; + + device_lock_assert(&pdev->dev); + snapshot = cxl_hdm_cache_snapshot(&pdev->hdm); + if (IS_ERR(snapshot)) + return PTR_ERR(snapshot) == -ENXIO ? NULL : snapshot; + + /* 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); + } + + return snapshot; +} + +/** + * cxl_restore_state_after_pci_reset() - restore CXL state after a bus reset + * @pdev: device that was reset + * @snapshot: storage from pci_cxl_reset_prepare() + * @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. + * + * 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. + */ +int cxl_restore_state_after_pci_reset(struct pci_dev *pdev, + struct cxl_hdm_info *snapshot, + u16 *command) +{ + int rc; + + device_lock_assert(&pdev->dev); + rc = cxl_reset_hdm_restore(pdev, snapshot, command); + if (rc) + cxl_reset_save_disabled_state(pdev); + + return rc; +} + +/** + * cxl_restore_command_after_pci_reset() - re-enable the device after a bus reset + * @pdev: device that was reset + * @snapshot: HDM state from pci_cxl_reset_prepare() + * @command: PCI_COMMAND value from cxl_restore_state_after_pci_reset() + * + * Call after the IOMMU is restored: this can enable bus mastering. Save the + * restored PCI state, then turn CXL.cache back on, now that the host caches + * are flushed. Leave @pdev disabled on failure. + */ +int cxl_restore_command_after_pci_reset(struct pci_dev *pdev, + const struct cxl_hdm_info *snapshot, + u16 command) +{ + int rc; + + device_lock_assert(&pdev->dev); + rc = cxl_reset_save_restored_state(pdev, command); + if (rc) + return rc; + + rc = cxl_reset_enable_cache(pdev, snapshot); + if (rc) + cxl_reset_save_disabled_state(pdev); + + return rc; +} + /* * 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 diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 9ab647719c9f..3d9647c110dd 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -5062,6 +5062,7 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe) { struct pci_dev *bridge; u16 dvsec, reg, val; + u16 command; int rc; /* @@ -5092,6 +5093,11 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe) if (rc) return -ENOTTY; + struct cxl_hdm_info *snapshot __free(kfree) = + pci_cxl_reset_prepare(dev); + 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); @@ -5112,7 +5118,15 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe) 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); + pci_dev_reset_iommu_done(dev); + + /* Enable bus mastering only once the IOMMU translates again. */ + if (!rc && snapshot) + rc = cxl_restore_command_after_pci_reset(dev, snapshot, command); + return rc; } diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 5bfa77fd1d37..cdbe4a8b4050 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -736,10 +736,18 @@ static inline void pci_doe_destroy(struct pci_dev *pdev) { } static inline void pci_doe_disconnected(struct pci_dev *pdev) { } #endif +struct cxl_hdm_info; #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); +int cxl_restore_state_after_pci_reset(struct pci_dev *pdev, + struct cxl_hdm_info *snapshot, + u16 *command); +int cxl_restore_command_after_pci_reset(struct pci_dev *pdev, + const struct cxl_hdm_info *snapshot, + u16 command); #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) { } @@ -747,6 +755,26 @@ 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) +{ + return NULL; +} + +static inline int +cxl_restore_state_after_pci_reset(struct pci_dev *pdev, + struct cxl_hdm_info *snapshot, u16 *command) +{ + return 0; +} + +static inline int +cxl_restore_command_after_pci_reset(struct pci_dev *pdev, + const struct cxl_hdm_info *snapshot, + u16 command) +{ + return 0; +} #endif #ifdef CONFIG_PCI_NPEM diff --git a/include/cxl/hdm.h b/include/cxl/hdm.h index a7e342bf645e..594ee8da2c11 100644 --- a/include/cxl/hdm.h +++ b/include/cxl/hdm.h @@ -75,6 +75,8 @@ bool cxl_hdm_cache_is_regs_offline(struct pci_dev *pdev); void cxl_hdm_cache_release(struct cxl_hdm_info **slot); bool cxl_hdm_snapshot_is_dvsec_decoded(const struct cxl_hdm_info *info); struct cxl_hdm_info *cxl_hdm_cache_snapshot(struct cxl_hdm_info *const *slot); +int cxl_hdm_cache_copy_decoders(struct cxl_hdm_info *const *slot, + struct cxl_hdm_info *dst); /* * Region quiesce blocks CXL region commit and teardown, and with them decoder -- 2.43.0