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.40 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796444145; b=gWY+Xv/eZr0gpFmH3+IvFbKlWt5nONbHNpEFgkr0egEpElNz3bEuHGqiIKfoVcKLcoyO WHpkANRmZFsvoOVh1K+NnDKv0uN9Rc+c2XYa8dvgwYueE9P96mwtanxGNAsn1Z2c2V+Ng ifhZa8ksWrc+UerDo2kuDIT2osUA8yQtFKdgZDTt2rLh/n8GQXMasztWSoRvnYWA/MRdc l1KqRKZ5qnXCPhTg3UCUI+LbxNk5cRHQ2qvhccEBxAzl6wdogJx6EAGHnlIXO7shPAJzK po0UAKs8HU8ocLnMqlROGd0CR/b0dBeQS/tvcAUA4pHWO5BG2ZlIl9eqywy9ujvER9A== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796444145; 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=/R1FC1XLp34flRxAroDVhKCWYU+H0egpYVDcT9hVor4=; b=GmztX8krP5njrp6pad0+UvcT6PWdlgRL55soO0yYy4oJ7Q/CFr4U6gUZIFSiABwfwP/J S/FJwnHHTF1BBwwBZjxQF1vg7n99lpR2pKuUQsZzvyzCdZfsKsRycWb+uKK5wJ0HW0qxY z/Mcgd8l5/mXbj+O0sLBjNXoChviEOdyHHuFfXZIypDDcbk4LFv5gKszXX9ukBwKJl4Ct 6yeHwWgZS/x0+JgYAfeASjDqhmBsgmdnYZA5GP8tljCpaE2HagsqZg/+Pn/5IQEwNo5kB bux37ZjOM52fun3dOsQp4KkN62gmZ6qHRlPr1X22sGnJK7/I5YloZB+6OjXz2wDUd1A== 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=BWHLOt7r; arc=pass header.oldest-pass=0 smtp.remote-ip=40.93.201.40 Received: from CY3PR05CU001.outbound.protection.outlook.com (mail-westcentralusazon11013040.outbound.protection.outlook.com [40.93.201.40]) (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 E3440606A682; Sat, 5 Dec 2026 04:15:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E3440606A682 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=BWHLOt7r ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=gubmRwfRYrdD0p3JiSQQXzgtW4tGyz4QAmC9VKnlVPYLHzsfYiEIZUja8F8RyEcmpTgC8k9Qj0zP019NApVp8kt1HviLIgcMnY+3DNKGm0XWeehNzSPvk2CwTaoEsch/bP73sackZ9tageq8gfxayW4c14qVbVwqm3u7DtW1lzwLXZC9YYbUyAonT/x+l6o2El09bABoAI9DAI2O+FQxTA5X2VP2xhTzJ7sE89I96HeV8754OCxTYupX2vbSwVl1SUFFDijlHsVdL9dcE4Zus/UNHU6d0l+Y2zfQ4LKOykVBkLzXbEftz2kHFc8ydI3dah9WUIcYkpys4nKRYF5JFg== 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=/R1FC1XLp34flRxAroDVhKCWYU+H0egpYVDcT9hVor4=; b=D4X0mHUkSXQwGbIpRBahpuwDu0zvw0ekgZx3bHzkHSBI1QdFo2gpiYmLnC/HExYegTH2m1McNDk41sYOiJOeZPs0u5rqVuQg9V0ZMRTSDgvW7UJmhngx2ipZVkLRQbx7YR46NBG6TguXd7P8WdSh/jCXx/ouaS/dHMO3ofvRZEr6PNSJUjNPWcAk/ADWLe499mpnC0UH+Pyzib/+S4ipagUTQBGzOjmdPM6EZDONrKbQyXLNn4VSugTXHC/QlU26PDC6gaYQqI8Mu24I0WbgEoKNbQ3/n/giNIqYiINnkoIYLhXRWOvSEC4P9o+R5uqI8S0Muy6MiQ+dBltg9xtjjw== 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=/R1FC1XLp34flRxAroDVhKCWYU+H0egpYVDcT9hVor4=; b=BWHLOt7rFZ9rCAK2zjhPXc09B2NKcBEwfVy01JJXGCIAueTS0Ja/uSjqojKsLbQE72GSJcrE6yOi46eHxXt2NlH2KfBKGC2ZBT6LJ+v6N+DdmZi/qkPAFT1zaqu/E2+7Z2zytd54hjN5LaPn2Gt/nyFKtN/J4wc4Hub5giM4oJcLlwMqcyJFLqz1brukDJWJvbzvS5oigCrq6VKjGJH+wdSpRCnZnEKFvlHmR9z+s7QFR7kv/l2o5Glab0aUFipcL236QELzvpanogUXQhhKB9Stbs0nhqEA2g7aya+lP+xuNDdv8aJKkHohnADTqkUT57nXEv+2dSnhhUwZWIqI0g== 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:40 +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:40 +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 09/19] cxl: Refresh cached PCI HDM decoder settings Date: Sat, 5 Dec 2026 04:14:43 +0000 Message-ID: <20261205041453.3946529-10-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: SJ0PR05CA0141.namprd05.prod.outlook.com (2603:10b6:a03:33d::26) To DSVPR12MB827618.namprd12.prod.outlook.com (2603:10b6:8:3e5::24) X-Mailing-List: linux-cxl@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: 4fdae1ac-cc15-42f5-6db8-08df52866c56 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|261022105027099003|56012099006|261102123227099003|261021121427099003|261021123027099003|261102130727099003|261009223027099003|261026121527099003|261022075727099003|261026140227099003|261028144727099003|18002099003|6133799003|22082099003|3023799007; X-Microsoft-Antispam-Message-Info: ztHU47sfB9eFYVIjiU2Xlg038QeOXfNQxLppNOdIKPITcsXthPL9cwKqM5ZsG+qz2E6FQmRMsYh5SoOp3X07NDcLK4VMX82cqZSZf/tcLD7DKWP1uyhxrnX/KOCWzXeSNAf+Lb8fyv54t/HjPm4XsXH4AdSyxh8gA1W1dnAgysZxzadLqq8KDyU5z0qHH8JTad1I2sLG1xOJpyQKK8SOJNEPpPJ6f9p8Ylo+X8ICCrt2+QLBly5GHSfOnRLDlBgnPHLfPo9jm74rV2L8dQT0edIwP4ZkbjM8vYL+/9tgWOZOZEGQCDV7vQJgO96sXhddsUF359aLbzorJLpEO5FpFdy/JLyPykQmT4DlFcUZ9hzsm3qXmuY6my32Itj6WykA9zr2ebQE1UBV28rnc2Yns2JVHb/gLlhOcpX7W4d/BVkGTVDnmeOGFa13BaqLnZwkbNrjKPuKZ9Mlpne7PZjHWwBoauRHi5uRulztoNFvwBtt9gel/1VdTQXD7pTcR91iA6kw+cj/CT/orukA5RMQmD1+oWXtyL7zwiNK1aYBy4yPDQ/aXZvrbWF7Qfip2zyRbofbv8JcSUV5wFnLb38VATlmwWYNpZxDoeRalK3zGgmKIdVgyHt6YNGQPOOJhRYOL81trrKFljhx5yxcnIQjLcHUH2S5Ff7K1uZI4rsVFFMydR8BI47i8MuyYTHDUpY0XeeA19RUsavjEzaKPxW4OQ== 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)(261022105027099003)(56012099006)(261102123227099003)(261021121427099003)(261021123027099003)(261102130727099003)(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?invN+UKdKIxxZ98D/n4FywgDVfIJ/6tzLUL1ZHz934fpeIKVaCrDSiQPiRcE?= =?us-ascii?Q?M4Onn3ACgOCfKpMjZXIh7FczdhcygBS3DEH75WPUiHkva1B9i/kJluxMPu+x?= =?us-ascii?Q?0mOGzBvsdRJJ7WdZDbThJlE0GgC2zqN96dR0tixNrG2BLFiuZgGtgWpg4Y5b?= =?us-ascii?Q?S0GX7tqiF37K11kgnliwNKnzezV0GIGNuaPFsKj9BFcQ3hqUi0IUxuZ6BzAc?= =?us-ascii?Q?sjxbhqUPq1smIlsvZaLBoVmj8yaf21UTC5R3fnh9tqGQ+fYVszc1fpmD+DdW?= =?us-ascii?Q?gXXwad7ykdB21ffQfnYAllEoCfp4vyl/k0CRol4Y5FWF9W6i+dNcM8UTVddT?= =?us-ascii?Q?Z/CC24w1d2dajtxD7zFka1c2J95830Z9+RwWJmOYwjaVQRtMmQBaGAj76umY?= =?us-ascii?Q?X2/Yu7QFuUnJCou9QinC4SNikGFMBabNV4tUh+ql8WY6vpqFvnp7m8E9009/?= =?us-ascii?Q?K5NT1dU+6aLJNDzfCEe8xo0+fzg5BIx4xHy/7oF6MkaZhfYOLkx5Z2uJulnU?= =?us-ascii?Q?q4dW0vSrFpM0bQMR2LGxj8aJGmKlXjUSlTwbhC68V3oqATOsY20GJuHflRYS?= =?us-ascii?Q?zWesh+D+bRAM/HWCHUiBDYDvDNhgEtjPO4njZGiCaOhwfyPCOJ0uDH904tKI?= =?us-ascii?Q?sF+5xhsYvoZGoOmxA+n1erOU0O7Wfedy8Q8wyzOTWuOatGBgwah2CM0CdtA9?= =?us-ascii?Q?IdcZy5NOozCI3kFl4K9kjw41Asgb/tzhX9FQzegvwDWMqArUMberM+ttUuj9?= =?us-ascii?Q?NN4wb7JhQIrM6QZsSHR2Th4cl65MdJB2cHtId+y2z/BBJ+sQIfdNcBkRQABt?= =?us-ascii?Q?Ev5qCUO+i9GgC7AEnIiUnpLH1cIANwsqHPHGsGL46RPCAEA0kupJ7MRxEZiw?= =?us-ascii?Q?pZeuadup3qySIhI+/IMrNJOvD2fD5NiG1519efpYbVuBZzeW+LqbOhyl9/D4?= =?us-ascii?Q?I/chxlSkkEWXG6XyroP+5BpsnYsRJUQV5meCo52BZaUHWr2INkf2CYDpIGS3?= =?us-ascii?Q?Vo4uKFY4/aEGqOa1HX0b3MBT1JpYlBpaiwegMSWhnqXncFNROTMsZYlvLEjL?= =?us-ascii?Q?YwYqF61A0Jhy399uDVenQ+DYkzAYKnBoLwQDk5k/LBmySx/9sAc1gzJtQrPe?= =?us-ascii?Q?RZhUS57sbrS2GrWmEb34pg2g7NjI3K+LMD5HfMvd7OXP1oMhBeHQCeOIxOvj?= =?us-ascii?Q?swMAA9BW0ToL5T6sr5/Kixtqt0W2Q7sAIAYHRLTusdVEaOwd5msVmN170nO9?= =?us-ascii?Q?LizEmO47dHKO/4IbCJB2kd6hvV8GKcOduvxY4zHXLK5BUKho86Xd6TUG+f/j?= =?us-ascii?Q?BAjJDO3wlw2NUb1kVDregNgSJ/AIBdqkFrT5S1JkuHE7g08hjcjNO/fbCD5L?= =?us-ascii?Q?AKGyjdlSN7TCPnyv/3X6OKsrMAz7kAAYtgCAtffY4uIyjOYWp/BwtTb+r9I8?= =?us-ascii?Q?+xDPDHyDuqWZFUVgMOem1YQ3ZvL7Msn8+NT8mb6nGztNH4Kuu5waMI0nK4bd?= =?us-ascii?Q?8HaSTUUxkF6g2jH+zWNnuj24ZTd7mJZ4Ha4z+GkqxW8/xfHWeO3FkQSrikLW?= =?us-ascii?Q?rnfMMnG/oTvXqVs68F7eEkH9ysIgBoH9/smNrybyocQqmhkzPVXGULqnkjbW?= =?us-ascii?Q?dPpOWWYyAhHgQdoT+Uo3VAz38jsmiTytGFEI/96RDjXYckJ2EhXIsHRNBJ7+?= =?us-ascii?Q?+Gvw6Z9Y0YFzdz8xArKG2STZag+SApMiFsjYhS0nAMVO3II7i5seUTrjuB4F?= =?us-ascii?Q?1Qjt5LhbFg=3D=3D?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4fdae1ac-cc15-42f5-6db8-08df52866c56 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:40.7649 (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: Nd8fx2GLrbKsXWwKW0pYiuwBoJptDwV6rOVx3+AcSQphIB/nLHHS7nVkkm0EZiX1gNyChGZPvBvhipM6qez6hQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB5602 Early PCI discovery creates the HDM cache, while later CXL enumeration and decoder operations provide updated programming state. Refresh the PCI snapshot when decoders are enumerated, committed, or reset so reset recovery need not walk the CXL topology. Keep cache access and locking in the built-in HDM state helpers. Ignore updates when no cache exists and reject decoder-count mismatches. Use CXL_RESET for the PCI HDM cache. Shared HDM register helpers do not require it. Skip cache checks and updates for DVSEC-emulated decoders, which do not use HDM registers. Keep cached global control when MMIO reads return all ones. Non-PCI mock ports have no upstream PCI device, so the cache helpers ignore them. This avoids test wrappers and a circular dependency between cxl_core and cxl_mock. Signed-off-by: Srirangan Madhavan Reviewed-by: Richard Cheng --- Changes in v18: - Declare the pci_dev reference at its initializer, add kernel-doc for the exported cache helpers, make the decoder-count check return bool as cxl_hdm_cache_decoder_count_match(), and drop an unused forward declaration (Dave Jiang) Reference branch: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/commit/?id=50a88f5194e5df38590d413f925d8d5afa8a8488 --- drivers/cxl/Kconfig | 5 +++ drivers/cxl/core/core.h | 1 + drivers/cxl/core/hdm.c | 30 ++++++++++++++ drivers/cxl/core/hdm_state.c | 78 ++++++++++++++++++++++++++++++++++++ drivers/cxl/core/pci.c | 27 +++++++++++++ include/cxl/hdm.h | 32 +++++++++++++++ include/linux/pci.h | 6 +++ 7 files changed, 179 insertions(+) diff --git a/drivers/cxl/Kconfig b/drivers/cxl/Kconfig index fdd57a4118db..37bfa6689b04 100644 --- a/drivers/cxl/Kconfig +++ b/drivers/cxl/Kconfig @@ -6,6 +6,7 @@ menuconfig CXL_BUS select FW_UPLOAD select PCI_DOE select FIRMWARE_TABLE + select CXL_RESET select NUMA_KEEP_MEMINFO if NUMA_MEMBLKS select FWCTL if CXL_FEATURES help @@ -260,3 +261,7 @@ config CXL_KUNIT_TEST If unsure, say N. endif + +# CXL_BUS requires the PCI HDM cache and built-in state helpers, even when modular. +config CXL_RESET + bool diff --git a/drivers/cxl/core/core.h b/drivers/cxl/core/core.h index 7af8d380c8ac..715097894144 100644 --- a/drivers/cxl/core/core.h +++ b/drivers/cxl/core/core.h @@ -220,6 +220,7 @@ static inline void devm_cxl_dport_ras_setup(struct cxl_dport *dport) { } int cxl_gpf_port_setup(struct cxl_dport *dport); struct cxl_hdm; +int cxl_hdm_check_decoder_count(struct cxl_hdm *cxlhdm); int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm, struct cxl_endpoint_dvsec_info *info); int cxl_port_get_possible_dports(struct cxl_port *port); diff --git a/drivers/cxl/core/hdm.c b/drivers/cxl/core/hdm.c index 1fabbd4a63a3..f88418177582 100644 --- a/drivers/cxl/core/hdm.c +++ b/drivers/cxl/core/hdm.c @@ -750,6 +750,24 @@ static void cxlsd_set_targets(struct cxl_switch_decoder *cxlsd, u64 *tgt) *tgt |= FIELD_PREP(GENMASK_ULL(63, 56), t[7]->port_id); } +static void cxl_hdm_refresh_decoder(struct cxl_hdm *cxlhdm, + struct cxl_decoder *cxld) +{ + void __iomem *hdm = cxlhdm->regs.hdm_decoder; + + if (!hdm) + return; + if (!is_endpoint_decoder(&cxld->dev)) + return; + + struct pci_dev *pdev __free(pci_dev_put) = + cxl_port_get_uport_pci_dev(cxlhdm->port); + if (!pdev) + return; + cxl_hdm_cache_refresh_decoder(pdev, hdm, + to_cxl_endpoint_decoder(&cxld->dev)); +} + static void cxl_endpoint_decoder_snapshot(struct cxl_endpoint_decoder *cxled, struct cxl_endpoint_decoder_settings *settings) @@ -830,6 +848,7 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld) } port->commit_end++; cxld->config.flags |= CXL_DECODER_F_ENABLE; + cxl_hdm_refresh_decoder(cxlhdm, cxld); return 0; } @@ -902,6 +921,7 @@ static void cxl_decoder_reset(struct cxl_decoder *cxld) writel(0, hdm + CXL_HDM_DECODER0_BASE_LOW_OFFSET(id)); cxld->config.flags &= ~CXL_DECODER_F_ENABLE; + cxl_hdm_refresh_decoder(cxlhdm, cxld); /* Userspace is now responsible for reconfiguring this decoder */ if (is_endpoint_decoder(&cxld->dev)) { @@ -1121,6 +1141,8 @@ static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm, { void __iomem *hdm = cxlhdm->regs.hdm_decoder; struct cxl_port *port = cxlhdm->port; + /* DVSEC-emulated decoders do not describe the cached HDM registers. */ + bool emulated = should_emulate_decoders(info); u64 dpa_base = 0; int i, rc; @@ -1136,6 +1158,12 @@ static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm, return -ENXIO; } + if (is_cxl_endpoint(port) && hdm && !emulated) { + rc = cxl_hdm_check_decoder_count(cxlhdm); + if (rc) + return rc; + } + cxl_settle_decoders(cxlhdm); /* @@ -1182,6 +1210,8 @@ static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm, put_device(&cxld->dev); return rc; } + if (!emulated) + cxl_hdm_refresh_decoder(cxlhdm, cxld); rc = add_hdm_decoder(port, cxld); if (rc) { dev_warn(&port->dev, diff --git a/drivers/cxl/core/hdm_state.c b/drivers/cxl/core/hdm_state.c index c9cc019902ef..ec335ac21ad4 100644 --- a/drivers/cxl/core/hdm_state.c +++ b/drivers/cxl/core/hdm_state.c @@ -1,6 +1,8 @@ // SPDX-License-Identifier: GPL-2.0-only /* Copyright (c) 2026 NVIDIA Corporation & Affiliates */ #include +#include +#include #include #include @@ -13,3 +15,79 @@ struct cxl_rwsem cxl_rwsem = { .dpa = __RWSEM_INITIALIZER(cxl_rwsem.dpa), }; EXPORT_SYMBOL_FOR_MODULES(cxl_rwsem, "cxl_core"); + +/** + * cxl_hdm_cache_refresh_decoder() - update the PCI HDM cache from a decoder + * @pdev: PCI device that owns the HDM cache + * @hdm: mapped HDM decoder register block, for the global control register + * @cxled: endpoint decoder whose state is recorded + * + * Record the decoder's current configuration, or just its id when it is + * disabled, and refresh the cached global control register unless the read + * returns all ones. Does nothing if @pdev has no cache or the decoder id is + * outside the cached count. Takes cxl_rwsem.dpa for write. + */ +void cxl_hdm_cache_refresh_decoder(struct pci_dev *pdev, void __iomem *hdm, + struct cxl_endpoint_decoder *cxled) +{ + struct cxl_endpoint_decoder_settings *settings; + struct cxl_decoder *cxld = &cxled->cxld; + struct cxl_hdm_info *info; + u32 global_ctrl; + int id; + + guard(rwsem_write)(&cxl_rwsem.dpa); + id = cxld->config.id; + info = pdev->hdm; + if (!info) + return; + if (id < 0 || id >= info->decoder_count) { + pci_warn(pdev, "CXL HDM decoder %d exceeds cached count %d\n", + id, info->decoder_count); + return; + } + + /* Preserve the last good value when the device does not respond. */ + global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET); + if (global_ctrl != U32_MAX) + info->global_ctrl = global_ctrl; + settings = &info->settings[id]; + + /* + * A disabled decoder's software object may retain its old range and + * skip state. Leave only the decoder id in the cached settings so stale + * state is not restored as an enabled decode. + */ + *settings = (struct cxl_endpoint_decoder_settings) { + .config.id = id, + }; + if (cxld->config.flags & CXL_DECODER_F_ENABLE) { + settings->config = cxld->config; + settings->skip = cxled->skip; + } +} +EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_refresh_decoder, "cxl_core"); + +/** + * cxl_hdm_cache_decoder_count_match() - compare a decoder count to the cache + * @pdev: PCI device that owns the HDM cache + * @count: decoder count found during CXL enumeration + * + * Takes cxl_rwsem.dpa for read. Warns when the counts differ. + * + * Return: true if @pdev has no cache or the cached decoder count equals + * @count, false otherwise. + */ +bool cxl_hdm_cache_decoder_count_match(struct pci_dev *pdev, int count) +{ + guard(rwsem_read)(&cxl_rwsem.dpa); + if (pdev->hdm && pdev->hdm->decoder_count != count) { + pci_warn(pdev, + "CXL HDM cache decoder count mismatch: cached=%d hdm=%d\n", + pdev->hdm->decoder_count, count); + return false; + } + + return true; +} +EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_decoder_count_match, "cxl_core"); diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c index 63dc3f39b5d8..d0da38763b4b 100644 --- a/drivers/cxl/core/pci.c +++ b/drivers/cxl/core/pci.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -180,6 +181,32 @@ int cxl_await_media_ready(struct cxl_dev_state *cxlds) } EXPORT_SYMBOL_NS_GPL(cxl_await_media_ready, "CXL"); +/** + * cxl_hdm_check_decoder_count() - check enumeration against the PCI HDM cache + * @cxlhdm: HDM capability containing the decoder count to check + * + * Take cxl_rwsem.dpa for read while comparing the cached count. Call only + * for hardware HDM decoders, since DVSEC-emulated ranges do not describe + * the HDM registers represented by the cache. + * + * Return: 0 if the counts match or no upstream PCI device or cache exists, + * or -ENXIO if the cached and enumerated decoder counts differ. + */ +int cxl_hdm_check_decoder_count(struct cxl_hdm *cxlhdm) +{ + struct pci_dev *pdev __free(pci_dev_put) = + cxl_port_get_uport_pci_dev(cxlhdm->port); + + if (!pdev) + return 0; + + if (!cxl_hdm_cache_decoder_count_match(pdev, cxlhdm->decoder_count)) + return -ENXIO; + + return 0; +} +EXPORT_SYMBOL_NS_GPL(cxl_hdm_check_decoder_count, "CXL"); + static int cxl_set_mem_enable(struct cxl_dev_state *cxlds, u16 val) { struct pci_dev *pdev = to_pci_dev(cxlds->dev); diff --git a/include/cxl/hdm.h b/include/cxl/hdm.h index 219b9bb67440..6c5b369ce490 100644 --- a/include/cxl/hdm.h +++ b/include/cxl/hdm.h @@ -6,6 +6,9 @@ #include #include +struct pci_dev; +struct cxl_endpoint_decoder; + /** * struct cxl_endpoint_decoder_settings - endpoint HDM decoder programming * @config: common decoder configuration @@ -16,4 +19,33 @@ struct cxl_endpoint_decoder_settings { u64 skip; }; +/** + * struct cxl_hdm_info - cached CXL HDM state for a PCI device + * @decoder_count: number of entries in @settings + * @hdm_bar: PCI BAR containing the HDM decoder capability + * @hdm_offset: offset of the HDM decoder capability in @hdm_bar + * @hdm_size: size of the HDM decoder register block + * @global_ctrl: HDM decoder global control register + * @dvsec_ctrl: CXL DVSEC control register + * @dvsec_ctrl_valid: whether @dvsec_ctrl contains valid state + * @settings: per-endpoint-decoder programming state + * + * Publication through pci_dev.hdm and all mutable fields are protected by + * cxl_rwsem.dpa. + */ +struct cxl_hdm_info { + int decoder_count; + int hdm_bar; + resource_size_t hdm_offset; + resource_size_t hdm_size; + u32 global_ctrl; + u16 dvsec_ctrl; + bool dvsec_ctrl_valid; + struct cxl_endpoint_decoder_settings settings[] __counted_by(decoder_count); +}; + +void cxl_hdm_cache_refresh_decoder(struct pci_dev *pdev, void __iomem *hdm, + struct cxl_endpoint_decoder *cxled); +bool cxl_hdm_cache_decoder_count_match(struct pci_dev *pdev, int count); + #endif /* __CXL_HDM_H__ */ diff --git a/include/linux/pci.h b/include/linux/pci.h index aa3765a20a96..f62aab652770 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -339,6 +339,9 @@ struct pcie_link_state; struct pci_sriov; struct pci_p2pdma; struct rcec_ea; +#ifdef CONFIG_CXL_RESET +struct cxl_hdm_info; +#endif /* struct pci_dev - describes a PCI device * @@ -567,6 +570,9 @@ struct pci_dev { #ifdef CONFIG_PCI_DOE struct xarray doe_mbs; /* Data Object Exchange mailboxes */ #endif +#ifdef CONFIG_CXL_RESET + struct cxl_hdm_info *hdm; /* CXL HDM decoder state */ +#endif #ifdef CONFIG_PCI_NPEM struct npem *npem; /* Native PCIe Enclosure Management */ #endif -- 2.43.0