From nobody Sun Feb 8 04:57:37 2026 Received: from BL2PR02CU003.outbound.protection.outlook.com (mail-eastusazon11011043.outbound.protection.outlook.com [52.101.52.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A9A043EDADB; Wed, 21 Jan 2026 20:22:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.52.43 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769026977; cv=fail; b=HsRzP0LhaHxrgK+B/dh2dUaaDtQgEWksY+FbiELli0s3WtjA1i1YPGAwOXhQbIkF+u0NL+J6/qUg54HPtdzL1RD/Aen/uVjrjwfO4529UVSTqM15HjqXquF0XyfIm2znuY/YMa/9kQo9s33Z5PrAEH0EamcBC7k/vGFSXjn6OnU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769026977; c=relaxed/simple; bh=htpxWmumglNCX8kauWKNJF6VbHve7Dwubm2JfH/Be20=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JxnafHgjTIfFDi1K82kxLo9tZR6EB0Z5Hr0od3Tabk6lzrJFOju25q2QXcYbfBI/xbZ5sNZJ8hbQTDvVsVHpJtBrlYSNGVscVqq0sHXagK5Zy2ppkjCxnOo1Fv+Tys/qyFEb8YfOfvLEdHJE1FyxbQIrjRTYYOHTyju9iUyUI84= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=aKrfHJGF; arc=fail smtp.client-ip=52.101.52.43 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) header.d=Nvidia.com header.i=@Nvidia.com header.b="aKrfHJGF" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ZoLyV3b8m8ABj5cYbZ+e8KHnepsk/C9YGsXX4oVwqgNIZFKlM0dXaIR53Cisg6qSgeJyRa6wOabrS1EhL7+uds7wR0i1ZDkQN3zO6Y9mtFLAZsTnhLr8DuN+1ars7aAWAdgEMRaLuaz7eApajyZsqWPfYh06cZSwFeP6I7rmv8PA5HFTMsl07T16ubzr5Sd/XGVyrmkD67+5QDztQkPUsmhA6kKPHKAMRjMT+xMYP18ynfWoh/XCyqutDpf+yTVVnp8jJBk+w+c3w87LNZASyQe5uUvpUmooq87vRkmCyN4CfIL9QIDZwxQbyTvfYVz4j2KumJPtLskf9b/mpWTb2A== 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=JcRZro5+6Bfg1SZvzFK8B/Jbx0vcHMUf72GL/qqK8GM=; b=KexsfdluFkuZRRlGpltVvVG3u8GYNRKNapNttgdkJ58pcfhMLEcOBTZ3pVSp/HPsFRqxvAyfFnbxu+RTLyovsXTXcetIWynUSV9SJpKWd+zOC1Dhq0HXdnzsl6CRzL0xR7l1SWyixRC2BBZfRYuZnwABLbexnqsp10JyPXxZUopCThDPMTdyySEn/D0fJzb8Eih2zth9fUBNnkVp1bzci2yfmWswaldYQNjqIhf/AsioLbTVJ2GSK0cD9EJrOZ2tLEGu3Ye6/1X24JtF1IW7Gbr5IrCHibMC7/HSv88Xok2Xl8bx1Jr38MGWcfc56VPLhIPGmxo2RSOPsQpgRSZ2Vg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=JcRZro5+6Bfg1SZvzFK8B/Jbx0vcHMUf72GL/qqK8GM=; b=aKrfHJGFQeuYwOhcYV6P9HI9uY7qSSYQMsVsTpbIC5Pfs3o6xVwkIMaR1RkkZKajGhoXiQRx8r2RYWPh+P4aoUdglL7BpaQ98Qzmh1BxjJYnEbxfyzdSbksGDut0iTvtB7L6oTCSMlgjtwibb2EdFY5DC8snLUM0JN9HzC1EQXHyDWrgLdZ4Aldh/9r2OmMyLl/qbIOLxPzcV2rH3wJcM3N7Bp2cGjdy7SEQGUGPoXxuzH1MFgoTMmWzoAqD/xkhtWdgYQDLxKN6j9mQ2KJFzlTYuCsOGu2JXezD6zt2fIEjJYMqLQNFKkKG+NVm6OrPNTWWc+g1P6zqm69iTG/6VQ== Received: from DM6PR08CA0036.namprd08.prod.outlook.com (2603:10b6:5:80::49) by SA1PR12MB7293.namprd12.prod.outlook.com (2603:10b6:806:2b9::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9542.9; Wed, 21 Jan 2026 20:22:47 +0000 Received: from DS2PEPF0000343A.namprd02.prod.outlook.com (2603:10b6:5:80:cafe::4d) by DM6PR08CA0036.outlook.office365.com (2603:10b6:5:80::49) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9542.10 via Frontend Transport; Wed, 21 Jan 2026 20:22:47 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by DS2PEPF0000343A.mail.protection.outlook.com (10.167.18.37) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9542.4 via Frontend Transport; Wed, 21 Jan 2026 20:22:46 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Wed, 21 Jan 2026 12:22:26 -0800 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Wed, 21 Jan 2026 12:22:26 -0800 Received: from inno-vm-xubuntu (10.127.8.11) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Wed, 21 Jan 2026 12:22:20 -0800 From: Zhi Wang To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , Zhi Wang Subject: [PATCH v12 1/5] rust: devres: style for imports Date: Wed, 21 Jan 2026 22:22:07 +0200 Message-ID: <20260121202212.4438-2-zhiw@nvidia.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260121202212.4438-1-zhiw@nvidia.com> References: <20260121202212.4438-1-zhiw@nvidia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF0000343A:EE_|SA1PR12MB7293:EE_ X-MS-Office365-Filtering-Correlation-Id: 7e912918-7a4a-4a6b-0da7-08de592ad769 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|1800799024|82310400026|376014|7416014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?YXrSsaLNPvi8J9cIcdengeJnPxYprQIXqEYr5hSyaZpeWeEyJmXya1pTbyw6?= =?us-ascii?Q?X5jt/Z7qN4fDRMpXPvZNFn2KdOmpbWV3nlnSq+CCHYZBGzgB3s8bLIjDR9aG?= =?us-ascii?Q?tGTWtBE4Y6hCKhz800yLPAwmnV6kWHLkv/bSnILpJyHkOwwhk1q33i0yYktt?= =?us-ascii?Q?R0tClUXlE1NyyU7UKqBruWdBDDHGNxVzdWpsR1LsGsme9TECIp8FXtyDkP9c?= =?us-ascii?Q?t338Yh0unN6F7QwGD9v22Y83N+p6G2eP4OtIp9kq64jUCQKCIz4trHKR29Yj?= =?us-ascii?Q?DQ9OH7WaLUs0oijpFxG15IL0PMtxFKiSrLCYLrbeD/cajZmlUFB3QKtt79m0?= =?us-ascii?Q?1AlfIxMdVvuf/wC/I35sisFsFE7JuehlyGszxghRfPnsoEfg7MuKVqugd3ql?= =?us-ascii?Q?NPrAONB1x6fbX4eY+9iShys8LTzwTEaPJeF61snDExZIZPewRgo1pgfwA1LD?= =?us-ascii?Q?wH0Jqx9ND0upFGHvEDTeBz2dvC6czcpRuFXjcVgzh0jkkv35WPVA9XTjInUM?= =?us-ascii?Q?IbDagzQzbTQT6vrsIC+ya7SwLi0Tt/nhwbTGU5BiSYhRLHb4HHgfd6abZheF?= =?us-ascii?Q?xAhmzSw5+yczZZSYxm9UywuKpxTrFW+OHkJCfSblwbKQWPqJT/K9x9VcH6w4?= =?us-ascii?Q?Slo3Zrw7VC2lQlnrx3LtM9lxaObUvKAoI0fbzqyPSMUgqMKteHfYZ7MXRJq5?= =?us-ascii?Q?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k1khDZlLawOMjGj5?= =?us-ascii?Q?RhUJNo4ZbuSGc/1Vc2JvsXeCT8GpkGgR2rDiE/nxhu9XscRDbB9VdeelT3QT?= =?us-ascii?Q?/V/uXg=3D=3D?= X-Forefront-Antispam-Report: CIP:216.228.118.232;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge1.nvidia.com;CAT:NONE;SFS:(13230040)(36860700013)(1800799024)(82310400026)(376014)(7416014);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Jan 2026 20:22:46.9392 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7e912918-7a4a-4a6b-0da7-08de592ad769 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.232];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS2PEPF0000343A.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB7293 Content-Type: text/plain; charset="utf-8" Convert all imports in the devres to use "kernel vertical" style. Cc: Gary Guo Cc: Miguel Ojeda Reviewed-by: Gary Guo Signed-off-by: Zhi Wang Reviewed-by: Alexandre Courbot --- rust/kernel/devres.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/rust/kernel/devres.rs b/rust/kernel/devres.rs index db02f8b1788d..43089511bf76 100644 --- a/rust/kernel/devres.rs +++ b/rust/kernel/devres.rs @@ -254,8 +254,12 @@ pub fn device(&self) -> &Device { /// # Examples /// /// ```no_run - /// # #![cfg(CONFIG_PCI)] - /// # use kernel::{device::Core, devres::Devres, pci}; + /// #![cfg(CONFIG_PCI)] + /// use kernel::{ + /// device::Core, + /// devres::Devres, + /// pci, // + /// }; /// /// fn from_core(dev: &pci::Device, devres: Devres= >) -> Result { /// let bar =3D devres.access(dev.as_ref())?; @@ -358,7 +362,13 @@ fn register_foreign

(dev: &Device, data: P) -= > Result /// # Examples /// /// ```no_run -/// use kernel::{device::{Bound, Device}, devres}; +/// use kernel::{ +/// device::{ +/// Bound, +/// Device, // +/// }, +/// devres, // +/// }; /// /// /// Registration of e.g. a class device, IRQ, etc. /// struct Registration; --=20 2.51.0 From nobody Sun Feb 8 04:57:37 2026 Received: from PH8PR06CU001.outbound.protection.outlook.com (mail-westus3azon11012046.outbound.protection.outlook.com [40.107.209.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AC08734D4C9; Wed, 21 Jan 2026 20:23:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.209.46 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769026990; cv=fail; b=UBAtJZz+7dhI6BM20A33PnREUWn9hiGbz9VDUnZT63n8CDKuvLxoulHxlBTyMYFWTrqbcR1N3/+GwKwcEmlouLf3hCt3d6NuFgKxfUyRH3O7p6wVEtiHa0oG0MtUAoi9N8SoQ5FyrkbPoWv9uuY9rMP0neipV0LA/qsRg2/2BAg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769026990; c=relaxed/simple; bh=O4UnCzw9aRMVAK5u/xbAiPRlcYT9upcD8IcgZ8QQgk8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=D5glev916Ky//GmmxnBFjjYEWrRPObyLgpmqeREUdJspnYNvAGIdS6A1p8Ok1PmrhJaWg86APNdoVznBDuSAwnQstR0xQDQgsd4nRe0fSNo0tgo1fx+ftkP0WXbd7XFEcGjslldJuaqz48OvbkGB/tx54awvbZpKbRcrDIqa0ss= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=oPlIxM/x; arc=fail smtp.client-ip=40.107.209.46 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) header.d=Nvidia.com header.i=@Nvidia.com header.b="oPlIxM/x" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=CdGD3aN+BT5jhbXRODgXP07twc1ru89hBUHlaLZ/giJyU30SBqMVQW1P/m152WxbZiXi1OJt3+cDIhiT0mlU/obCP/hvOW3ma/RHRKA2hC7YP7iHaDDH81ARSmoJIchAoeyB4RdtNscWF/2n2VS11NwPgyaC/TFSvKjbXG8AFjo0+YOEV/stmBQk0rKK04zKXCjQ7AvFwuhdpkttAr+i3+II4s1f0AduPIOMWjKAgbN7oVqpGk/1/TYMDHpheLQruGNpQeyxDzaxfijSkAKBP7zQe03aWqkkdOQp7FpVa9GwhUodCLLwxV1V7La07DisJ8mnXxZOL11HdXjf7F3c0Q== 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=eb34WNm+DsHC3D5kTOV0NXYIhWJiB2hX8SHLExAI85A=; b=CUhTNeYLaFqiYU/2PYpHyBFzEFQSS5g4vmFABgjUwBrlsWMx6YlsQbISIeXJxMalTq+mfQ7EYDS5XmYGRmNoOW6+E7qZJLad+Iaw5spKm5i4/l2uY6I5uKCLnWU06QSyat75eM7CMRDkqs0vi8x3wlcDnNUlM4kedWbJnskWPahsB+iargL0sN7drBM+fcMJiF3UDTED4Jx2k5lZyExSPsCrmNOElH/oXl/OphGQ+yV12FM6ypSqTKYIlLK8HGFxhtVW/wgWkj2g7203+G8IHwddDj9W9QAIPAU5Xp9iqExCC/xPXV1hddXdozXoSATK4O3uwmrZwET9So2mM9JmTw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=eb34WNm+DsHC3D5kTOV0NXYIhWJiB2hX8SHLExAI85A=; b=oPlIxM/xnFvl9xrKm8G9FmpiC/tc0+6Vzz909d0KSttUOfDVOC4dvFnRXQaGnKU2ojPX2jbi8UnpWkQgy6VcezjyPwQbfxYJ3ttRiinIpasfzBp2k8XCZREyNArZ1Mj2DAvwDFFihEvlhafn4lrD62jP5+438begTyD1PNyTE9h81PI2b8dUww3Hb5cZR50oxl7POQRth1dTh+ojJJGe4p2P16lfweEK+xXg4RJN87yB3ckgenU9xsssq9jlGLtS7R3qlu/p6O3gX+8tZ8YPo53pIVeo/s0zenLpU819U7V0eqzKpXZvB95BMQQKZBmJM75ovhFk2QYEeGnwy+074w== Received: from SJ0PR03CA0170.namprd03.prod.outlook.com (2603:10b6:a03:338::25) by LV2PR12MB5966.namprd12.prod.outlook.com (2603:10b6:408:171::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9542.9; Wed, 21 Jan 2026 20:22:55 +0000 Received: from MWH0EPF000971E7.namprd02.prod.outlook.com (2603:10b6:a03:338:cafe::9a) by SJ0PR03CA0170.outlook.office365.com (2603:10b6:a03:338::25) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9520.12 via Frontend Transport; Wed, 21 Jan 2026 20:22:54 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by MWH0EPF000971E7.mail.protection.outlook.com (10.167.243.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9542.4 via Frontend Transport; Wed, 21 Jan 2026 20:22:54 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Wed, 21 Jan 2026 12:22:33 -0800 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Wed, 21 Jan 2026 12:22:32 -0800 Received: from inno-vm-xubuntu (10.127.8.11) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Wed, 21 Jan 2026 12:22:26 -0800 From: Zhi Wang To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , Zhi Wang Subject: [PATCH v12 2/5] rust: io: separate generic I/O helpers from MMIO implementation Date: Wed, 21 Jan 2026 22:22:08 +0200 Message-ID: <20260121202212.4438-3-zhiw@nvidia.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260121202212.4438-1-zhiw@nvidia.com> References: <20260121202212.4438-1-zhiw@nvidia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWH0EPF000971E7:EE_|LV2PR12MB5966:EE_ X-MS-Office365-Filtering-Correlation-Id: 4db11e06-ce0c-4ddc-fa15-08de592adbb1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|7416014|376014|82310400026|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?vX4nnrXx9iBJ/f2Mr3NzdhyazS9iWqbRBja7hWusVVKfYZ1vo+Z3iShFOsha?= =?us-ascii?Q?0ZRzZ2amE5E9lwFbJiHRoHwNhVlS3aox5o0ovjgZm7pPiXaihURq2Hl+Yjpd?= =?us-ascii?Q?BUKFq1u6CxJ1XLKIVFgvUW3sVjI52juXMQpA1NQZ4TF2LKh+cGtur+xP6q65?= =?us-ascii?Q?KpW7IAjDPyQ79eW1Kej0/CbxRZMRyC8EtRuXIIHKo5ws37bYRQMc9Zqedm3x?= =?us-ascii?Q?gnWjg0DOTULP4COaMw7cy2Qp7iLCdvTNU3zsmnlXHmOk7XAaIqW8eDBlAVwg?= =?us-ascii?Q?L/ToSf5wsvqwqNFcpZspi8p2Fo++BS9xJM11BnqFSbzdG0oO/z+2yD8oBd+M?= =?us-ascii?Q?Y0EgVpq/AIv6ZP+k15J2W5Q4Hbv3Bm5JJXPfEG9rrEo0pDArEOUXTwpC6dA1?= =?us-ascii?Q?GsOiZYEslayFK092EElhRPaI6DLrkRQLtm6d3fYgoWxIicq66tD6yLs5OcAO?= =?us-ascii?Q?j1YZRlV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us-ascii?Q?IxP6zsBEPepXNdz1+Y2U+rZEADind+wQCBTlozWPV00DG1m8B0PrVf4N0VcO?= =?us-ascii?Q?tzyeUQzdudVH/uo32lEmG775n9DclLcn+e3EWGsBRxDGG9qi8CR85il4PEqj?= =?us-ascii?Q?aitlqLQScki+6XkXpmYCBldIm7rcNGHx6Zp5N5OG2hJLdCiBhxuoZXwdcgZ/?= =?us-ascii?Q?IyD0IUoMllQC3rM9Brv0TQtq/aJHDkYtUfVzWRGJNRWNtDJ5sEYB2lPt7UFW?= =?us-ascii?Q?X4dMRul5ARqI2z2wXNKBFklcR87+C9vOpfl2mlDGcvBOR8bqlKYbLT3+TODl?= =?us-ascii?Q?dj97eg=3D=3D?= X-Forefront-Antispam-Report: CIP:216.228.118.233;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge2.nvidia.com;CAT:NONE;SFS:(13230040)(36860700013)(7416014)(376014)(82310400026)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Jan 2026 20:22:54.2190 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4db11e06-ce0c-4ddc-fa15-08de592adbb1 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.233];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: MWH0EPF000971E7.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV2PR12MB5966 Content-Type: text/plain; charset="utf-8" The previous Io type combined both the generic I/O access helpers and MMIO implementation details in a single struct. This coupling prevented reusing the I/O helpers for other backends, such as PCI configuration space. Establish a clean separation between the I/O interface and concrete backends by separating generic I/O helpers from MMIO implementation. Introduce a new trait hierarchy to handle different access capabilities: - IoCapable: A marker trait indicating that a backend supports I/O operations of a certain type (u8, u16, u32, or u64). - Io trait: Defines fallible (try_read8, try_write8, etc.) and infallibile (read8, write8, etc.) I/O methods with runtime bounds checking and compile-time bounds checking. - IoKnownSize trait: The marker trait for types support infallible I/O methods. Move the MMIO-specific logic into a dedicated Mmio type that implements the Io traits. Rename IoRaw to MmioRaw and update consumers to use the new types. Cc: Alexandre Courbot Cc: Alice Ryhl Cc: Bjorn Helgaas Cc: Gary Guo Cc: Danilo Krummrich Cc: John Hubbard Signed-off-by: Zhi Wang Reviewed-by: Alexandre Courbot Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo --- drivers/gpu/drm/tyr/regs.rs | 1 + drivers/gpu/nova-core/gsp/sequencer.rs | 5 +- drivers/gpu/nova-core/regs/macros.rs | 90 +++--- drivers/gpu/nova-core/vbios.rs | 1 + drivers/pwm/pwm_th1520.rs | 5 +- rust/kernel/devres.rs | 19 +- rust/kernel/io.rs | 398 ++++++++++++++++++++----- rust/kernel/io/mem.rs | 16 +- rust/kernel/io/poll.rs | 16 +- rust/kernel/pci/io.rs | 12 +- samples/rust/rust_driver_pci.rs | 1 + 11 files changed, 433 insertions(+), 131 deletions(-) diff --git a/drivers/gpu/drm/tyr/regs.rs b/drivers/gpu/drm/tyr/regs.rs index f46933aaa221..d3a541cb37c6 100644 --- a/drivers/gpu/drm/tyr/regs.rs +++ b/drivers/gpu/drm/tyr/regs.rs @@ -11,6 +11,7 @@ use kernel::device::Bound; use kernel::device::Device; use kernel::devres::Devres; +use kernel::io::Io; use kernel::prelude::*; =20 use crate::driver::IoMem; diff --git a/drivers/gpu/nova-core/gsp/sequencer.rs b/drivers/gpu/nova-core= /gsp/sequencer.rs index 2d0369c49092..862cf7f27143 100644 --- a/drivers/gpu/nova-core/gsp/sequencer.rs +++ b/drivers/gpu/nova-core/gsp/sequencer.rs @@ -12,7 +12,10 @@ =20 use kernel::{ device, - io::poll::read_poll_timeout, + io::{ + poll::read_poll_timeout, + Io, // + }, prelude::*, time::{ delay::fsleep, diff --git a/drivers/gpu/nova-core/regs/macros.rs b/drivers/gpu/nova-core/r= egs/macros.rs index fd1a815fa57d..ed624be1f39b 100644 --- a/drivers/gpu/nova-core/regs/macros.rs +++ b/drivers/gpu/nova-core/regs/macros.rs @@ -369,16 +369,18 @@ impl $name { =20 /// Read the register from its address in `io`. #[inline(always)] - pub(crate) fn read(io: &T) -> Self where - T: ::core::ops::Deref>, + pub(crate) fn read(io: &T) -> Self where + T: ::core::ops::Deref, + I: ::kernel::io::IoKnownSize + ::kernel::io::IoCapable, { Self(io.read32($offset)) } =20 /// Write the value contained in `self` to the register addres= s in `io`. #[inline(always)] - pub(crate) fn write(self, io: &T) where - T: ::core::ops::Deref>, + pub(crate) fn write(self, io: &T) where + T: ::core::ops::Deref, + I: ::kernel::io::IoKnownSize + ::kernel::io::IoCapable, { io.write32(self.0, $offset) } @@ -386,11 +388,12 @@ pub(crate) fn write(self, io: &= T) where /// Read the register from its address in `io` and run `f` on = its value to obtain a new /// value to write back. #[inline(always)] - pub(crate) fn update( + pub(crate) fn update( io: &T, f: F, ) where - T: ::core::ops::Deref>, + T: ::core::ops::Deref, + I: ::kernel::io::IoKnownSize + ::kernel::io::IoCapable, F: ::core::ops::FnOnce(Self) -> Self, { let reg =3D f(Self::read(io)); @@ -408,12 +411,13 @@ impl $name { /// Read the register from `io`, using the base address provid= ed by `base` and adding /// the register's offset to it. #[inline(always)] - pub(crate) fn read( + pub(crate) fn read( io: &T, #[allow(unused_variables)] base: &B, ) -> Self where - T: ::core::ops::Deref>, + T: ::core::ops::Deref, + I: ::kernel::io::IoKnownSize + ::kernel::io::IoCapable, B: crate::regs::macros::RegisterBase<$base>, { const OFFSET: usize =3D $name::OFFSET; @@ -428,13 +432,14 @@ pub(crate) fn read( /// Write the value contained in `self` to `io`, using the bas= e address provided by /// `base` and adding the register's offset to it. #[inline(always)] - pub(crate) fn write( + pub(crate) fn write( self, io: &T, #[allow(unused_variables)] base: &B, ) where - T: ::core::ops::Deref>, + T: ::core::ops::Deref, + I: ::kernel::io::IoKnownSize + ::kernel::io::IoCapable, B: crate::regs::macros::RegisterBase<$base>, { const OFFSET: usize =3D $name::OFFSET; @@ -449,12 +454,13 @@ pub(crate) fn write( /// the register's offset to it, then run `f` on its value to = obtain a new value to /// write back. #[inline(always)] - pub(crate) fn update( + pub(crate) fn update( io: &T, base: &B, f: F, ) where - T: ::core::ops::Deref>, + T: ::core::ops::Deref, + I: ::kernel::io::IoKnownSize + ::kernel::io::IoCapable, B: crate::regs::macros::RegisterBase<$base>, F: ::core::ops::FnOnce(Self) -> Self, { @@ -474,11 +480,12 @@ impl $name { =20 /// Read the array register at index `idx` from its address in= `io`. #[inline(always)] - pub(crate) fn read( + pub(crate) fn read( io: &T, idx: usize, ) -> Self where - T: ::core::ops::Deref>, + T: ::core::ops::Deref, + I: ::kernel::io::IoKnownSize + ::kernel::io::IoCapable, { build_assert!(idx < Self::SIZE); =20 @@ -490,12 +497,13 @@ pub(crate) fn read( =20 /// Write the value contained in `self` to the array register = with index `idx` in `io`. #[inline(always)] - pub(crate) fn write( + pub(crate) fn write( self, io: &T, idx: usize ) where - T: ::core::ops::Deref>, + T: ::core::ops::Deref, + I: ::kernel::io::IoKnownSize + ::kernel::io::IoCapable, { build_assert!(idx < Self::SIZE); =20 @@ -507,12 +515,13 @@ pub(crate) fn write( /// Read the array register at index `idx` in `io` and run `f`= on its value to obtain a /// new value to write back. #[inline(always)] - pub(crate) fn update( + pub(crate) fn update( io: &T, idx: usize, f: F, ) where - T: ::core::ops::Deref>, + T: ::core::ops::Deref, + I: ::kernel::io::IoKnownSize + ::kernel::io::IoCapable, F: ::core::ops::FnOnce(Self) -> Self, { let reg =3D f(Self::read(io, idx)); @@ -524,11 +533,12 @@ pub(crate) fn update( /// The validity of `idx` is checked at run-time, and `EINVAL`= is returned is the /// access was out-of-bounds. #[inline(always)] - pub(crate) fn try_read( + pub(crate) fn try_read( io: &T, idx: usize, ) -> ::kernel::error::Result where - T: ::core::ops::Deref>, + T: ::core::ops::Deref, + I: ::kernel::io::IoKnownSize + ::kernel::io::IoCapable, { if idx < Self::SIZE { Ok(Self::read(io, idx)) @@ -542,12 +552,13 @@ pub(crate) fn try_read( /// The validity of `idx` is checked at run-time, and `EINVAL`= is returned is the /// access was out-of-bounds. #[inline(always)] - pub(crate) fn try_write( + pub(crate) fn try_write( self, io: &T, idx: usize, ) -> ::kernel::error::Result where - T: ::core::ops::Deref>, + T: ::core::ops::Deref, + I: ::kernel::io::IoKnownSize + ::kernel::io::IoCapable, { if idx < Self::SIZE { Ok(self.write(io, idx)) @@ -562,12 +573,13 @@ pub(crate) fn try_write( /// The validity of `idx` is checked at run-time, and `EINVAL`= is returned is the /// access was out-of-bounds. #[inline(always)] - pub(crate) fn try_update( + pub(crate) fn try_update( io: &T, idx: usize, f: F, ) -> ::kernel::error::Result where - T: ::core::ops::Deref>, + T: ::core::ops::Deref, + I: ::kernel::io::IoKnownSize + ::kernel::io::IoCapable, F: ::core::ops::FnOnce(Self) -> Self, { if idx < Self::SIZE { @@ -593,13 +605,14 @@ impl $name { /// Read the array register at index `idx` from `io`, using th= e base address provided /// by `base` and adding the register's offset to it. #[inline(always)] - pub(crate) fn read( + pub(crate) fn read( io: &T, #[allow(unused_variables)] base: &B, idx: usize, ) -> Self where - T: ::core::ops::Deref>, + T: ::core::ops::Deref, + I: ::kernel::io::IoKnownSize + ::kernel::io::IoCapable, B: crate::regs::macros::RegisterBase<$base>, { build_assert!(idx < Self::SIZE); @@ -614,14 +627,15 @@ pub(crate) fn read( /// Write the value contained in `self` to `io`, using the bas= e address provided by /// `base` and adding the offset of array register `idx` to it. #[inline(always)] - pub(crate) fn write( + pub(crate) fn write( self, io: &T, #[allow(unused_variables)] base: &B, idx: usize ) where - T: ::core::ops::Deref>, + T: ::core::ops::Deref, + I: ::kernel::io::IoKnownSize + ::kernel::io::IoCapable, B: crate::regs::macros::RegisterBase<$base>, { build_assert!(idx < Self::SIZE); @@ -636,13 +650,14 @@ pub(crate) fn write( /// by `base` and adding the register's offset to it, then run= `f` on its value to /// obtain a new value to write back. #[inline(always)] - pub(crate) fn update( + pub(crate) fn update( io: &T, base: &B, idx: usize, f: F, ) where - T: ::core::ops::Deref>, + T: ::core::ops::Deref, + I: ::kernel::io::IoKnownSize + ::kernel::io::IoCapable, B: crate::regs::macros::RegisterBase<$base>, F: ::core::ops::FnOnce(Self) -> Self, { @@ -656,12 +671,13 @@ pub(crate) fn update( /// The validity of `idx` is checked at run-time, and `EINVAL`= is returned is the /// access was out-of-bounds. #[inline(always)] - pub(crate) fn try_read( + pub(crate) fn try_read( io: &T, base: &B, idx: usize, ) -> ::kernel::error::Result where - T: ::core::ops::Deref>, + T: ::core::ops::Deref, + I: ::kernel::io::IoKnownSize + ::kernel::io::IoCapable, B: crate::regs::macros::RegisterBase<$base>, { if idx < Self::SIZE { @@ -677,13 +693,14 @@ pub(crate) fn try_read( /// The validity of `idx` is checked at run-time, and `EINVAL`= is returned is the /// access was out-of-bounds. #[inline(always)] - pub(crate) fn try_write( + pub(crate) fn try_write( self, io: &T, base: &B, idx: usize, ) -> ::kernel::error::Result where - T: ::core::ops::Deref>, + T: ::core::ops::Deref, + I: ::kernel::io::IoKnownSize + ::kernel::io::IoCapable, B: crate::regs::macros::RegisterBase<$base>, { if idx < Self::SIZE { @@ -700,13 +717,14 @@ pub(crate) fn try_write( /// The validity of `idx` is checked at run-time, and `EINVAL`= is returned is the /// access was out-of-bounds. #[inline(always)] - pub(crate) fn try_update( + pub(crate) fn try_update( io: &T, base: &B, idx: usize, f: F, ) -> ::kernel::error::Result where - T: ::core::ops::Deref>, + T: ::core::ops::Deref, + I: ::kernel::io::IoKnownSize + ::kernel::io::IoCapable, B: crate::regs::macros::RegisterBase<$base>, F: ::core::ops::FnOnce(Self) -> Self, { diff --git a/drivers/gpu/nova-core/vbios.rs b/drivers/gpu/nova-core/vbios.rs index abf423560ff4..fe33b519e4d8 100644 --- a/drivers/gpu/nova-core/vbios.rs +++ b/drivers/gpu/nova-core/vbios.rs @@ -6,6 +6,7 @@ =20 use kernel::{ device, + io::Io, prelude::*, ptr::{ Alignable, diff --git a/drivers/pwm/pwm_th1520.rs b/drivers/pwm/pwm_th1520.rs index e3b7e77356fc..616ca398b2c5 100644 --- a/drivers/pwm/pwm_th1520.rs +++ b/drivers/pwm/pwm_th1520.rs @@ -26,7 +26,10 @@ clk::Clk, device::{Bound, Core, Device}, devres, - io::mem::IoMem, + io::{ + mem::IoMem, + Io, // + }, of, platform, prelude::*, pwm, time, diff --git a/rust/kernel/devres.rs b/rust/kernel/devres.rs index 43089511bf76..cdc49677022a 100644 --- a/rust/kernel/devres.rs +++ b/rust/kernel/devres.rs @@ -74,14 +74,17 @@ struct Inner { /// devres::Devres, /// io::{ /// Io, -/// IoRaw, -/// PhysAddr, +/// IoKnownSize, +/// Mmio, +/// MmioRaw, +/// PhysAddr, // /// }, +/// prelude::*, /// }; /// use core::ops::Deref; /// /// // See also [`pci::Bar`] for a real example. -/// struct IoMem(IoRaw); +/// struct IoMem(MmioRaw); /// /// impl IoMem { /// /// # Safety @@ -96,7 +99,7 @@ struct Inner { /// return Err(ENOMEM); /// } /// -/// Ok(IoMem(IoRaw::new(addr as usize, SIZE)?)) +/// Ok(IoMem(MmioRaw::new(addr as usize, SIZE)?)) /// } /// } /// @@ -108,11 +111,11 @@ struct Inner { /// } /// /// impl Deref for IoMem { -/// type Target =3D Io; +/// type Target =3D Mmio; /// /// fn deref(&self) -> &Self::Target { /// // SAFETY: The memory range stored in `self` has been properly= mapped in `Self::new`. -/// unsafe { Io::from_raw(&self.0) } +/// unsafe { Mmio::from_raw(&self.0) } /// } /// } /// # fn no_run(dev: &Device) -> Result<(), Error> { @@ -258,6 +261,10 @@ pub fn device(&self) -> &Device { /// use kernel::{ /// device::Core, /// devres::Devres, + /// io::{ + /// Io, + /// IoKnownSize, // + /// }, /// pci, // /// }; /// diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs index a97eb44a9a87..687d32572d6b 100644 --- a/rust/kernel/io.rs +++ b/rust/kernel/io.rs @@ -32,16 +32,16 @@ /// By itself, the existence of an instance of this structure does not pro= vide any guarantees that /// the represented MMIO region does exist or is properly mapped. /// -/// Instead, the bus specific MMIO implementation must convert this raw re= presentation into an `Io` -/// instance providing the actual memory accessors. Only by the conversion= into an `Io` structure -/// any guarantees are given. -pub struct IoRaw { +/// Instead, the bus specific MMIO implementation must convert this raw re= presentation into an +/// `Mmio` instance providing the actual memory accessors. Only by the con= version into an `Mmio` +/// structure any guarantees are given. +pub struct MmioRaw { addr: usize, maxsize: usize, } =20 -impl IoRaw { - /// Returns a new `IoRaw` instance on success, an error otherwise. +impl MmioRaw { + /// Returns a new `MmioRaw` instance on success, an error otherwise. pub fn new(addr: usize, maxsize: usize) -> Result { if maxsize < SIZE { return Err(EINVAL); @@ -81,14 +81,16 @@ pub fn maxsize(&self) -> usize { /// ffi::c_void, /// io::{ /// Io, -/// IoRaw, +/// IoKnownSize, +/// Mmio, +/// MmioRaw, /// PhysAddr, /// }, /// }; /// use core::ops::Deref; /// /// // See also `pci::Bar` for a real example. -/// struct IoMem(IoRaw); +/// struct IoMem(MmioRaw); /// /// impl IoMem { /// /// # Safety @@ -103,7 +105,7 @@ pub fn maxsize(&self) -> usize { /// return Err(ENOMEM); /// } /// -/// Ok(IoMem(IoRaw::new(addr as usize, SIZE)?)) +/// Ok(IoMem(MmioRaw::new(addr as usize, SIZE)?)) /// } /// } /// @@ -115,11 +117,11 @@ pub fn maxsize(&self) -> usize { /// } /// /// impl Deref for IoMem { -/// type Target =3D Io; +/// type Target =3D Mmio; /// /// fn deref(&self) -> &Self::Target { /// // SAFETY: The memory range stored in `self` has been properly= mapped in `Self::new`. -/// unsafe { Io::from_raw(&self.0) } +/// unsafe { Mmio::from_raw(&self.0) } /// } /// } /// @@ -133,29 +135,31 @@ pub fn maxsize(&self) -> usize { /// # } /// ``` #[repr(transparent)] -pub struct Io(IoRaw); +pub struct Mmio(MmioRaw); =20 macro_rules! define_read { - ($(#[$attr:meta])* $name:ident, $try_name:ident, $c_fn:ident -> $type_= name:ty) =3D> { + (infallible, $(#[$attr:meta])* $vis:vis $name:ident, $c_fn:ident -> $t= ype_name:ty) =3D> { /// Read IO data from a given offset known at compile time. /// /// Bound checks are performed on compile time, hence if the offse= t is not known at compile /// time, the build will fail. $(#[$attr])* #[inline] - pub fn $name(&self, offset: usize) -> $type_name { + $vis fn $name(&self, offset: usize) -> $type_name { let addr =3D self.io_addr_assert::<$type_name>(offset); =20 // SAFETY: By the type invariant `addr` is a valid address for= MMIO operations. unsafe { bindings::$c_fn(addr as *const c_void) } } + }; =20 + (fallible, $(#[$attr:meta])* $vis:vis $try_name:ident, $c_fn:ident -> = $type_name:ty) =3D> { /// Read IO data from a given offset. /// /// Bound checks are performed on runtime, it fails if the offset = (plus the type size) is /// out of bounds. $(#[$attr])* - pub fn $try_name(&self, offset: usize) -> Result<$type_name> { + $vis fn $try_name(&self, offset: usize) -> Result<$type_name> { let addr =3D self.io_addr::<$type_name>(offset)?; =20 // SAFETY: By the type invariant `addr` is a valid address for= MMIO operations. @@ -163,74 +167,95 @@ pub fn $try_name(&self, offset: usize) -> Result<$typ= e_name> { } }; } +pub(crate) use define_read; =20 macro_rules! define_write { - ($(#[$attr:meta])* $name:ident, $try_name:ident, $c_fn:ident <- $type_= name:ty) =3D> { + (infallible, $(#[$attr:meta])* $vis:vis $name:ident, $c_fn:ident <- $t= ype_name:ty) =3D> { /// Write IO data from a given offset known at compile time. /// /// Bound checks are performed on compile time, hence if the offse= t is not known at compile /// time, the build will fail. $(#[$attr])* #[inline] - pub fn $name(&self, value: $type_name, offset: usize) { + $vis fn $name(&self, value: $type_name, offset: usize) { let addr =3D self.io_addr_assert::<$type_name>(offset); =20 // SAFETY: By the type invariant `addr` is a valid address for= MMIO operations. unsafe { bindings::$c_fn(value, addr as *mut c_void) } } + }; =20 + (fallible, $(#[$attr:meta])* $vis:vis $try_name:ident, $c_fn:ident <- = $type_name:ty) =3D> { /// Write IO data from a given offset. /// /// Bound checks are performed on runtime, it fails if the offset = (plus the type size) is /// out of bounds. $(#[$attr])* - pub fn $try_name(&self, value: $type_name, offset: usize) -> Resul= t { + $vis fn $try_name(&self, value: $type_name, offset: usize) -> Resu= lt { let addr =3D self.io_addr::<$type_name>(offset)?; =20 // SAFETY: By the type invariant `addr` is a valid address for= MMIO operations. - unsafe { bindings::$c_fn(value, addr as *mut c_void) } + unsafe { bindings::$c_fn(value, addr as *mut c_void) }; Ok(()) } }; } - -impl Io { - /// Converts an `IoRaw` into an `Io` instance, providing the accessors= to the MMIO mapping. - /// - /// # Safety - /// - /// Callers must ensure that `addr` is the start of a valid I/O mapped= memory region of size - /// `maxsize`. - pub unsafe fn from_raw(raw: &IoRaw) -> &Self { - // SAFETY: `Io` is a transparent wrapper around `IoRaw`. - unsafe { &*core::ptr::from_ref(raw).cast() } +pub(crate) use define_write; + +/// Checks whether an access of type `U` at the given `offset` +/// is valid within this region. +#[inline] +const fn offset_valid(offset: usize, size: usize) -> bool { + let type_size =3D core::mem::size_of::(); + if let Some(end) =3D offset.checked_add(type_size) { + end <=3D size && offset % type_size =3D=3D 0 + } else { + false } +} + +/// Marker trait indicating that an I/O backend supports operations of a c= ertain type. +/// +/// Different I/O backends can implement this trait to expose only the ope= rations they support. +/// +/// For example, a PCI configuration space may implement `IoCapable`, = `IoCapable`, +/// and `IoCapable`, but not `IoCapable`, while an MMIO region o= n a 64-bit +/// system might implement all four. +pub trait IoCapable {} + +/// Types implementing this trait (e.g. MMIO BARs or PCI config regions) +/// can perform I/O operations on regions of memory. +/// +/// This is an abstract representation to be implemented by arbitrary I/O +/// backends (e.g. MMIO, PCI config space, etc.). +/// +/// The [`Io`] trait provides: +/// - Base address and size information +/// - Helper methods for offset validation and address calculation +/// - Fallible (runtime checked) accessors for different data widths +/// +/// Which I/O methods are available depends on which [`IoCapable`] trai= ts +/// are implemented for the type. +/// +/// # Examples +/// +/// For MMIO regions, all widths (u8, u16, u32, and u64 on 64-bit systems)= are typically +/// supported. For PCI configuration space, u8, u16, and u32 are supported= but u64 is not. +pub trait Io { + /// Minimum usable size of this region. + const MIN_SIZE: usize; =20 /// Returns the base address of this mapping. - #[inline] - pub fn addr(&self) -> usize { - self.0.addr() - } + fn addr(&self) -> usize; =20 /// Returns the maximum size of this mapping. - #[inline] - pub fn maxsize(&self) -> usize { - self.0.maxsize() - } - - #[inline] - const fn offset_valid(offset: usize, size: usize) -> bool { - let type_size =3D core::mem::size_of::(); - if let Some(end) =3D offset.checked_add(type_size) { - end <=3D size && offset % type_size =3D=3D 0 - } else { - false - } - } + fn maxsize(&self) -> usize; =20 + /// Returns the absolute I/O address for a given `offset`, + /// performing runtime bound checks. #[inline] fn io_addr(&self, offset: usize) -> Result { - if !Self::offset_valid::(offset, self.maxsize()) { + if !offset_valid::(offset, self.maxsize()) { return Err(EINVAL); } =20 @@ -239,50 +264,285 @@ fn io_addr(&self, offset: usize) -> Result= { self.addr().checked_add(offset).ok_or(EINVAL) } =20 + /// Returns the absolute I/O address for a given `offset`, + /// performing compile-time bound checks. #[inline] fn io_addr_assert(&self, offset: usize) -> usize { - build_assert!(Self::offset_valid::(offset, SIZE)); + build_assert!(offset_valid::(offset, Self::MIN_SIZE)); =20 self.addr() + offset } =20 - define_read!(read8, try_read8, readb -> u8); - define_read!(read16, try_read16, readw -> u16); - define_read!(read32, try_read32, readl -> u32); + /// Fallible 8-bit read with runtime bounds check. + #[inline(always)] + fn try_read8(&self, _offset: usize) -> Result + where + Self: IoCapable, + { + build_error!("Backend does not support fallible 8-bit read") + } + + /// Fallible 16-bit read with runtime bounds check. + #[inline(always)] + fn try_read16(&self, _offset: usize) -> Result + where + Self: IoCapable, + { + build_error!("Backend does not support fallible 16-bit read") + } + + /// Fallible 32-bit read with runtime bounds check. + #[inline(always)] + fn try_read32(&self, _offset: usize) -> Result + where + Self: IoCapable, + { + build_error!("Backend does not support fallible 32-bit read") + } + + /// Fallible 64-bit read with runtime bounds check. + #[inline(always)] + fn try_read64(&self, _offset: usize) -> Result + where + Self: IoCapable, + { + build_error!("Backend does not support fallible 64-bit read") + } + + /// Fallible 8-bit write with runtime bounds check. + #[inline(always)] + fn try_write8(&self, _value: u8, _offset: usize) -> Result + where + Self: IoCapable, + { + build_error!("Backend does not support fallible 8-bit write") + } + + /// Fallible 16-bit write with runtime bounds check. + #[inline(always)] + fn try_write16(&self, _value: u16, _offset: usize) -> Result + where + Self: IoCapable, + { + build_error!("Backend does not support fallible 16-bit write") + } + + /// Fallible 32-bit write with runtime bounds check. + #[inline(always)] + fn try_write32(&self, _value: u32, _offset: usize) -> Result + where + Self: IoCapable, + { + build_error!("Backend does not support fallible 32-bit write") + } + + /// Fallible 64-bit write with runtime bounds check. + #[inline(always)] + fn try_write64(&self, _value: u64, _offset: usize) -> Result + where + Self: IoCapable, + { + build_error!("Backend does not support fallible 64-bit write") + } + + /// Infallible 8-bit read with compile-time bounds check. + #[inline(always)] + fn read8(&self, _offset: usize) -> u8 + where + Self: IoKnownSize + IoCapable, + { + build_error!("Backend does not support infallible 8-bit read") + } + + /// Infallible 16-bit read with compile-time bounds check. + #[inline(always)] + fn read16(&self, _offset: usize) -> u16 + where + Self: IoKnownSize + IoCapable, + { + build_error!("Backend does not support infallible 16-bit read") + } + + /// Infallible 32-bit read with compile-time bounds check. + #[inline(always)] + fn read32(&self, _offset: usize) -> u32 + where + Self: IoKnownSize + IoCapable, + { + build_error!("Backend does not support infallible 32-bit read") + } + + /// Infallible 64-bit read with compile-time bounds check. + #[inline(always)] + fn read64(&self, _offset: usize) -> u64 + where + Self: IoKnownSize + IoCapable, + { + build_error!("Backend does not support infallible 64-bit read") + } + + /// Infallible 8-bit write with compile-time bounds check. + #[inline(always)] + fn write8(&self, _value: u8, _offset: usize) + where + Self: IoKnownSize + IoCapable, + { + build_error!("Backend does not support infallible 8-bit write") + } + + /// Infallible 16-bit write with compile-time bounds check. + #[inline(always)] + fn write16(&self, _value: u16, _offset: usize) + where + Self: IoKnownSize + IoCapable, + { + build_error!("Backend does not support infallible 16-bit write") + } + + /// Infallible 32-bit write with compile-time bounds check. + #[inline(always)] + fn write32(&self, _value: u32, _offset: usize) + where + Self: IoKnownSize + IoCapable, + { + build_error!("Backend does not support infallible 32-bit write") + } + + /// Infallible 64-bit write with compile-time bounds check. + #[inline(always)] + fn write64(&self, _value: u64, _offset: usize) + where + Self: IoKnownSize + IoCapable, + { + build_error!("Backend does not support infallible 64-bit write") + } +} + +/// Marker trait for types with a known size at compile time. +/// +/// This trait is implemented by I/O backends that have a compile-time kno= wn size, +/// enabling the use of infallible I/O accessors with compile-time bounds = checking. +/// +/// Types implementing this trait can use the infallible methods in [`Io`]= trait +/// (e.g., `read8`, `write32`), which require `Self: IoKnownSize` bound. +pub trait IoKnownSize: Io {} + +// MMIO regions support 8, 16, and 32-bit accesses. +impl IoCapable for Mmio {} +impl IoCapable for Mmio {} +impl IoCapable for Mmio {} + +// MMIO regions on 64-bit systems also support 64-bit accesses. +#[cfg(CONFIG_64BIT)] +impl IoCapable for Mmio {} + +impl Io for Mmio { + const MIN_SIZE: usize =3D SIZE; + + /// Returns the base address of this mapping. + #[inline] + fn addr(&self) -> usize { + self.0.addr() + } + + /// Returns the maximum size of this mapping. + #[inline] + fn maxsize(&self) -> usize { + self.0.maxsize() + } + + define_read!(fallible, try_read8, readb -> u8); + define_read!(fallible, try_read16, readw -> u16); + define_read!(fallible, try_read32, readl -> u32); define_read!( + fallible, #[cfg(CONFIG_64BIT)] - read64, try_read64, readq -> u64 ); =20 - define_read!(read8_relaxed, try_read8_relaxed, readb_relaxed -> u8); - define_read!(read16_relaxed, try_read16_relaxed, readw_relaxed -> u16); - define_read!(read32_relaxed, try_read32_relaxed, readl_relaxed -> u32); + define_write!(fallible, try_write8, writeb <- u8); + define_write!(fallible, try_write16, writew <- u16); + define_write!(fallible, try_write32, writel <- u32); + define_write!( + fallible, + #[cfg(CONFIG_64BIT)] + try_write64, + writeq <- u64 + ); + + define_read!(infallible, read8, readb -> u8); + define_read!(infallible, read16, readw -> u16); + define_read!(infallible, read32, readl -> u32); define_read!( + infallible, #[cfg(CONFIG_64BIT)] - read64_relaxed, - try_read64_relaxed, - readq_relaxed -> u64 + read64, + readq -> u64 ); =20 - define_write!(write8, try_write8, writeb <- u8); - define_write!(write16, try_write16, writew <- u16); - define_write!(write32, try_write32, writel <- u32); + define_write!(infallible, write8, writeb <- u8); + define_write!(infallible, write16, writew <- u16); + define_write!(infallible, write32, writel <- u32); define_write!( + infallible, #[cfg(CONFIG_64BIT)] write64, - try_write64, writeq <- u64 ); +} + +impl IoKnownSize for Mmio {} + +impl Mmio { + /// Converts an `MmioRaw` into an `Mmio` instance, providing the acces= sors to the MMIO mapping. + /// + /// # Safety + /// + /// Callers must ensure that `addr` is the start of a valid I/O mapped= memory region of size + /// `maxsize`. + pub unsafe fn from_raw(raw: &MmioRaw) -> &Self { + // SAFETY: `Mmio` is a transparent wrapper around `MmioRaw`. + unsafe { &*core::ptr::from_ref(raw).cast() } + } + + define_read!(infallible, pub read8_relaxed, readb_relaxed -> u8); + define_read!(infallible, pub read16_relaxed, readw_relaxed -> u16); + define_read!(infallible, pub read32_relaxed, readl_relaxed -> u32); + define_read!( + infallible, + #[cfg(CONFIG_64BIT)] + pub read64_relaxed, + readq_relaxed -> u64 + ); + + define_read!(fallible, pub try_read8_relaxed, readb_relaxed -> u8); + define_read!(fallible, pub try_read16_relaxed, readw_relaxed -> u16); + define_read!(fallible, pub try_read32_relaxed, readl_relaxed -> u32); + define_read!( + fallible, + #[cfg(CONFIG_64BIT)] + pub try_read64_relaxed, + readq_relaxed -> u64 + ); + + define_write!(infallible, pub write8_relaxed, writeb_relaxed <- u8); + define_write!(infallible, pub write16_relaxed, writew_relaxed <- u16); + define_write!(infallible, pub write32_relaxed, writel_relaxed <- u32); + define_write!( + infallible, + #[cfg(CONFIG_64BIT)] + pub write64_relaxed, + writeq_relaxed <- u64 + ); =20 - define_write!(write8_relaxed, try_write8_relaxed, writeb_relaxed <- u8= ); - define_write!(write16_relaxed, try_write16_relaxed, writew_relaxed <- = u16); - define_write!(write32_relaxed, try_write32_relaxed, writel_relaxed <- = u32); + define_write!(fallible, pub try_write8_relaxed, writeb_relaxed <- u8); + define_write!(fallible, pub try_write16_relaxed, writew_relaxed <- u16= ); + define_write!(fallible, pub try_write32_relaxed, writel_relaxed <- u32= ); define_write!( + fallible, #[cfg(CONFIG_64BIT)] - write64_relaxed, - try_write64_relaxed, + pub try_write64_relaxed, writeq_relaxed <- u64 ); } diff --git a/rust/kernel/io/mem.rs b/rust/kernel/io/mem.rs index e4878c131c6d..620022cff401 100644 --- a/rust/kernel/io/mem.rs +++ b/rust/kernel/io/mem.rs @@ -16,8 +16,8 @@ Region, Resource, // }, - Io, - IoRaw, // + Mmio, + MmioRaw, // }, prelude::*, }; @@ -212,7 +212,7 @@ pub fn new<'a>(io_request: IoRequest<'a>) -> impl PinIn= it, Error> + } =20 impl Deref for ExclusiveIoMem { - type Target =3D Io; + type Target =3D Mmio; =20 fn deref(&self) -> &Self::Target { &self.iomem @@ -226,10 +226,10 @@ fn deref(&self) -> &Self::Target { /// /// # Invariants /// -/// [`IoMem`] always holds an [`IoRaw`] instance that holds a valid pointe= r to the +/// [`IoMem`] always holds an [`MmioRaw`] instance that holds a valid poin= ter to the /// start of the I/O memory mapped region. pub struct IoMem { - io: IoRaw, + io: MmioRaw, } =20 impl IoMem { @@ -264,7 +264,7 @@ fn ioremap(resource: &Resource) -> Result { return Err(ENOMEM); } =20 - let io =3D IoRaw::new(addr as usize, size)?; + let io =3D MmioRaw::new(addr as usize, size)?; let io =3D IoMem { io }; =20 Ok(io) @@ -287,10 +287,10 @@ fn drop(&mut self) { } =20 impl Deref for IoMem { - type Target =3D Io; + type Target =3D Mmio; =20 fn deref(&self) -> &Self::Target { // SAFETY: Safe as by the invariant of `IoMem`. - unsafe { Io::from_raw(&self.io) } + unsafe { Mmio::from_raw(&self.io) } } } diff --git a/rust/kernel/io/poll.rs b/rust/kernel/io/poll.rs index b1a2570364f4..75d1b3e8596c 100644 --- a/rust/kernel/io/poll.rs +++ b/rust/kernel/io/poll.rs @@ -45,12 +45,16 @@ /// # Examples /// /// ```no_run -/// use kernel::io::{Io, poll::read_poll_timeout}; +/// use kernel::io::{ +/// Io, +/// Mmio, +/// poll::read_poll_timeout, // +/// }; /// use kernel::time::Delta; /// /// const HW_READY: u16 =3D 0x01; /// -/// fn wait_for_hardware(io: &Io) -> Result { +/// fn wait_for_hardware(io: &Mmio) -> Result { /// read_poll_timeout( /// // The `op` closure reads the value of a specific status regis= ter. /// || io.try_read16(0x1000), @@ -128,12 +132,16 @@ pub fn read_poll_timeout( /// # Examples /// /// ```no_run -/// use kernel::io::{poll::read_poll_timeout_atomic, Io}; +/// use kernel::io::{ +/// Io, +/// Mmio, +/// poll::read_poll_timeout_atomic, // +/// }; /// use kernel::time::Delta; /// /// const HW_READY: u16 =3D 0x01; /// -/// fn wait_for_hardware(io: &Io) -> Result { +/// fn wait_for_hardware(io: &Mmio) -> Result { /// read_poll_timeout_atomic( /// // The `op` closure reads the value of a specific status regis= ter. /// || io.try_read16(0x1000), diff --git a/rust/kernel/pci/io.rs b/rust/kernel/pci/io.rs index 70e3854e7d8d..e3377397666e 100644 --- a/rust/kernel/pci/io.rs +++ b/rust/kernel/pci/io.rs @@ -8,8 +8,8 @@ device, devres::Devres, io::{ - Io, - IoRaw, // + Mmio, + MmioRaw, // }, prelude::*, sync::aref::ARef, // @@ -27,7 +27,7 @@ /// memory mapped PCI BAR and its size. pub struct Bar { pdev: ARef, - io: IoRaw, + io: MmioRaw, num: i32, } =20 @@ -63,7 +63,7 @@ pub(super) fn new(pdev: &Device, num: u32, name: &CStr) -= > Result { return Err(ENOMEM); } =20 - let io =3D match IoRaw::new(ioptr, len as usize) { + let io =3D match MmioRaw::new(ioptr, len as usize) { Ok(io) =3D> io, Err(err) =3D> { // SAFETY: @@ -117,11 +117,11 @@ fn drop(&mut self) { } =20 impl Deref for Bar { - type Target =3D Io; + type Target =3D Mmio; =20 fn deref(&self) -> &Self::Target { // SAFETY: By the type invariant of `Self`, the MMIO range in `sel= f.io` is properly mapped. - unsafe { Io::from_raw(&self.io) } + unsafe { Mmio::from_raw(&self.io) } } } =20 diff --git a/samples/rust/rust_driver_pci.rs b/samples/rust/rust_driver_pci= .rs index ef04c6401e6a..38c949efce38 100644 --- a/samples/rust/rust_driver_pci.rs +++ b/samples/rust/rust_driver_pci.rs @@ -7,6 +7,7 @@ use kernel::{ device::Core, devres::Devres, + io::Io, pci, prelude::*, sync::aref::ARef, // --=20 2.51.0 From nobody Sun Feb 8 04:57:37 2026 Received: from SN4PR0501CU005.outbound.protection.outlook.com (mail-southcentralusazon11011002.outbound.protection.outlook.com [40.93.194.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DAF923EDADE; Wed, 21 Jan 2026 20:23:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.93.194.2 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769026992; cv=fail; b=AGwh71X02CENPe5ltTlRA4NbewUrhwXsnyOn68F8jMSwwEitvI+8T/4kelz1lgg3jAcUe+Pr/BTRhMVLKXy2HlLrNHauXgsYDlJawO2XfYhokFEf8+w5c1DJs27XPzjzjVi24QgzYmiGaozdwxUf9E/JCVCY3T04mKtwxptRzRY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769026992; c=relaxed/simple; bh=4KIgJAA3dK8b17oE5S3haQkZJDXVlyaNS3JUknwi3/Y=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=UsrmjeeWXeUOscKzlme0UQLuMcx7AL/QeDwVXkMrJQklbNjP6jOCIrb1dDdcOZg8ZnEkcO9d/5fgAaZw+MxUVz2j1pmqwMmlRwWLMgF2aiuiHsGR06QeD2OwiMS6mboWKqNoH80Hncl5HyEwgfnV5LnvLNxxIuUtV3BpCRTF7ck= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=VuD/2Sph; arc=fail smtp.client-ip=40.93.194.2 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) header.d=Nvidia.com header.i=@Nvidia.com header.b="VuD/2Sph" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=TkNgEaioL72PaX4tW/zRkCXscbC9CClLl/DqtEvYcUFykM49Yr3fs6BHYBxyCANl6Ri+adQscqV65RqWDZ4EtZSd2hZT6wkd+G0ATSSxT9ZBU515kVxTZEZrlc6ExecPXssADD2SMKgNZoHOxKDUnRARHxX/MPtWRyKlPVu9dYp7IV4AjKu5XyW8VCX7qsf6lDDY8VLidF7erB6UcZ/Wa2n+2kA/WmOWy4mZEcs/LuDYjdjH/v3v2CrtVHMKvJbpTNlIqiMSQs6sKmUi4ctUhzbWwl+6xboUNInfFrTWhMudMNKy0ikVAKhHoLHhOaWHa80sgAkw4cFm0ENEKRpLOA== 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=jnrRM0Bd7XcPCamofFLqcEafBu5H6TZsO23tks9hTFA=; b=Z0jDG3bSI/rh9CT62xO3p/10H4AFWluntd1ITj/QqWFe+G9p100OJv1ajsOMaEObuLelp1KijdpWUbj4gvQqAbJUFqVCylpkjcgR8qBH1AUFvPzDrfkvOSY5qW0unamGv/XT2hSZ/e0dZ0yScq7m7mQliC8L+HSiItgJP5letSkj2v5TMokLvqb4IgtIScqu0z/yBIUBg6SMBHdbDwoW4nw3hFrlFbV7wbX3PperDrq+b1dohgXQoYLH5lWnJNQN805HQlXQBcznd9eLur7VEy8DPZVqratCAi2WG8Epo2T45hSWca1BknrjDC0f75pXcg6OuwU8f8NwhGLfJyw2YA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=jnrRM0Bd7XcPCamofFLqcEafBu5H6TZsO23tks9hTFA=; b=VuD/2Sph1J1REEawjOGmVdc4xc5CTtgrFQcUOM1uP5E9Z19/RsWH0fpjb5fJmOlUTHSQQRBY6sB7ZXLM1hMn0irY417cFxqC5Ab0At3APYSxn5sdnFGX4HIteopcAMXlsTDyRlt2vq8UEA4WaXnh/YgQyhZEPLJUFRjHGtOyUBdSQvU7youQT/oxH4Z4iOBbK/zVQK5fyEtLNW8ptu2qrhBWBi6nJGezdpttaEs+7Roo0ynm8yCVhVTcExp8rj6kTHuQJUQ7CSUQZGBeYVZxrdWNNbX5J7iyL76m91nCtYOpjtfAk7LE1omTAV+fB5EzLtBCcso+ML5G/y+2FQJBgQ== Received: from DM6PR03CA0085.namprd03.prod.outlook.com (2603:10b6:5:333::18) by MN2PR12MB4472.namprd12.prod.outlook.com (2603:10b6:208:267::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9542.9; Wed, 21 Jan 2026 20:22:59 +0000 Received: from DS2PEPF0000343E.namprd02.prod.outlook.com (2603:10b6:5:333:cafe::8) by DM6PR03CA0085.outlook.office365.com (2603:10b6:5:333::18) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9542.9 via Frontend Transport; Wed, 21 Jan 2026 20:22:54 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by DS2PEPF0000343E.mail.protection.outlook.com (10.167.18.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9542.4 via Frontend Transport; Wed, 21 Jan 2026 20:22:59 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Wed, 21 Jan 2026 12:22:39 -0800 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Wed, 21 Jan 2026 12:22:39 -0800 Received: from inno-vm-xubuntu (10.127.8.11) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Wed, 21 Jan 2026 12:22:33 -0800 From: Zhi Wang To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , Zhi Wang Subject: [PATCH v12 3/5] rust: io: factor out MMIO read/write macros Date: Wed, 21 Jan 2026 22:22:09 +0200 Message-ID: <20260121202212.4438-4-zhiw@nvidia.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260121202212.4438-1-zhiw@nvidia.com> References: <20260121202212.4438-1-zhiw@nvidia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF0000343E:EE_|MN2PR12MB4472:EE_ X-MS-Office365-Filtering-Correlation-Id: 1ef89e8f-954e-4933-0c03-08de592adeeb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|7416014|376014|1800799024|82310400026; X-Microsoft-Antispam-Message-Info: =?utf-8?B?czFTOGx5czZpV0tXRmgxdjZRbHJybEh5cGhyekNwT1RjUGJIRU1BaFpLSHRZ?= =?utf-8?B?eEdrNFdHNnpYMUZKLzRzbi9FeWpMZC9mblBSb0FNV2NxZWRpT3B5NWtVdENE?= =?utf-8?B?VWJsVFBxdHJCUkh5L05RQzlmM2RTMUwzVFdUWXhYanEzaWg4T3VDdnpiMkkz?= =?utf-8?B?alROZ292ejNGRmRLbDBXeEpLOWdNeEIzL3Z4aFp6VnhpRS9iRmJndjBRa2hp?= =?utf-8?B?U0R2UUttNkMwcjV0M1d1ditlSzBtM1FtaUxpYUhGdWxlMXhBNVpLV3VpSlBZ?= =?utf-8?B?VkRDL3JDVVQxejVmUXluTFNsQ0FmajF6TDM1OWpQenNIREJkQ2lxV2haYzVE?= =?utf-8?B?UU9IMVJEUTVvNHJQM2R1VFhyY1RLNmdkTndLcFppckhEbWVzNHpXRm9oTkto?= =?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U2MvdFFpQ285UkRRVjVNTVhRenBX?= =?utf-8?B?dStuN3ROOVByZXJINHhDWkJ4QStnPT0=?= X-Forefront-Antispam-Report: CIP:216.228.118.232;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge1.nvidia.com;CAT:NONE;SFS:(13230040)(36860700013)(7416014)(376014)(1800799024)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Jan 2026 20:22:59.5697 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1ef89e8f-954e-4933-0c03-08de592adeeb X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.232];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS2PEPF0000343E.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4472 Refactor the existing MMIO accessors to use common call macros instead of inlining the bindings calls in each `define_{read,write}!` expansion. This factoring separates the common offset/bounds checks from the low-level call pattern, making it easier to add additional I/O accessor families. No functional change intended. Cc: Alexandre Courbot Signed-off-by: Zhi Wang Reviewed-by: Alexandre Courbot --- rust/kernel/io.rs | 147 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 102 insertions(+), 45 deletions(-) diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs index 687d32572d6b..fcd1b156a14a 100644 --- a/rust/kernel/io.rs +++ b/rust/kernel/io.rs @@ -137,8 +137,65 @@ pub fn maxsize(&self) -> usize { #[repr(transparent)] pub struct Mmio(MmioRaw); =20 +/// Internal helper macros used to invoke C MMIO read functions. +/// +/// This macro is intended to be used by higher-level MMIO access macros (= define_read) and provides +/// a unified expansion for infallible vs. fallible read semantics. It emi= ts a direct call into the +/// corresponding C helper and performs the required cast to the Rust retu= rn type. +/// +/// # Parameters +/// +/// * `$c_fn` =E2=80=93 The C function performing the MMIO read. +/// * `$self` =E2=80=93 The I/O backend object. +/// * `$ty` =E2=80=93 The type of the value to be read. +/// * `$addr` =E2=80=93 The MMIO address to read. +/// +/// This macro does not perform any validation; all invariants must be uph= eld by the higher-level +/// abstraction invoking it. +macro_rules! call_mmio_read { + (infallible, $c_fn:ident, $self:ident, $type:ty, $addr:expr) =3D> { + // SAFETY: By the type invariant `addr` is a valid address for MMI= O operations. + unsafe { bindings::$c_fn($addr as *const c_void) as $type } + }; + + (fallible, $c_fn:ident, $self:ident, $type:ty, $addr:expr) =3D> {{ + // SAFETY: By the type invariant `addr` is a valid address for MMI= O operations. + Ok(unsafe { bindings::$c_fn($addr as *const c_void) as $type }) + }}; +} + +/// Internal helper macros used to invoke C MMIO write functions. +/// +/// This macro is intended to be used by higher-level MMIO access macros (= define_write) and provides +/// a unified expansion for infallible vs. fallible write semantics. It em= its a direct call into the +/// corresponding C helper and performs the required cast to the Rust retu= rn type. +/// +/// # Parameters +/// +/// * `$c_fn` =E2=80=93 The C function performing the MMIO write. +/// * `$self` =E2=80=93 The I/O backend object. +/// * `$ty` =E2=80=93 The type of the written value. +/// * `$addr` =E2=80=93 The MMIO address to write. +/// * `$value` =E2=80=93 The value to write. +/// +/// This macro does not perform any validation; all invariants must be uph= eld by the higher-level +/// abstraction invoking it. +macro_rules! call_mmio_write { + (infallible, $c_fn:ident, $self:ident, $ty:ty, $addr:expr, $value:expr= ) =3D> { + // SAFETY: By the type invariant `addr` is a valid address for MMI= O operations. + unsafe { bindings::$c_fn($value, $addr as *mut c_void) } + }; + + (fallible, $c_fn:ident, $self:ident, $ty:ty, $addr:expr, $value:expr) = =3D> {{ + // SAFETY: By the type invariant `addr` is a valid address for MMI= O operations. + unsafe { bindings::$c_fn($value, $addr as *mut c_void) }; + Ok(()) + }}; +} + macro_rules! define_read { - (infallible, $(#[$attr:meta])* $vis:vis $name:ident, $c_fn:ident -> $t= ype_name:ty) =3D> { + (infallible, $(#[$attr:meta])* $vis:vis $name:ident, $call_macro:ident= ($c_fn:ident) -> + $type_name:ty) =3D> { /// Read IO data from a given offset known at compile time. /// /// Bound checks are performed on compile time, hence if the offse= t is not known at compile @@ -148,12 +205,13 @@ macro_rules! define_read { $vis fn $name(&self, offset: usize) -> $type_name { let addr =3D self.io_addr_assert::<$type_name>(offset); =20 - // SAFETY: By the type invariant `addr` is a valid address for= MMIO operations. - unsafe { bindings::$c_fn(addr as *const c_void) } + // SAFETY: By the type invariant `addr` is a valid address for= IO operations. + $call_macro!(infallible, $c_fn, self, $type_name, addr) } }; =20 - (fallible, $(#[$attr:meta])* $vis:vis $try_name:ident, $c_fn:ident -> = $type_name:ty) =3D> { + (fallible, $(#[$attr:meta])* $vis:vis $try_name:ident, $call_macro:ide= nt($c_fn:ident) -> + $type_name:ty) =3D> { /// Read IO data from a given offset. /// /// Bound checks are performed on runtime, it fails if the offset = (plus the type size) is @@ -162,15 +220,16 @@ macro_rules! define_read { $vis fn $try_name(&self, offset: usize) -> Result<$type_name> { let addr =3D self.io_addr::<$type_name>(offset)?; =20 - // SAFETY: By the type invariant `addr` is a valid address for= MMIO operations. - Ok(unsafe { bindings::$c_fn(addr as *const c_void) }) + // SAFETY: By the type invariant `addr` is a valid address for= IO operations. + $call_macro!(fallible, $c_fn, self, $type_name, addr) } }; } pub(crate) use define_read; =20 macro_rules! define_write { - (infallible, $(#[$attr:meta])* $vis:vis $name:ident, $c_fn:ident <- $t= ype_name:ty) =3D> { + (infallible, $(#[$attr:meta])* $vis:vis $name:ident, $call_macro:ident= ($c_fn:ident) <- + $type_name:ty) =3D> { /// Write IO data from a given offset known at compile time. /// /// Bound checks are performed on compile time, hence if the offse= t is not known at compile @@ -180,12 +239,12 @@ macro_rules! define_write { $vis fn $name(&self, value: $type_name, offset: usize) { let addr =3D self.io_addr_assert::<$type_name>(offset); =20 - // SAFETY: By the type invariant `addr` is a valid address for= MMIO operations. - unsafe { bindings::$c_fn(value, addr as *mut c_void) } + $call_macro!(infallible, $c_fn, self, $type_name, addr, value); } }; =20 - (fallible, $(#[$attr:meta])* $vis:vis $try_name:ident, $c_fn:ident <- = $type_name:ty) =3D> { + (fallible, $(#[$attr:meta])* $vis:vis $try_name:ident, $call_macro:ide= nt($c_fn:ident) <- + $type_name:ty) =3D> { /// Write IO data from a given offset. /// /// Bound checks are performed on runtime, it fails if the offset = (plus the type size) is @@ -194,9 +253,7 @@ macro_rules! define_write { $vis fn $try_name(&self, value: $type_name, offset: usize) -> Resu= lt { let addr =3D self.io_addr::<$type_name>(offset)?; =20 - // SAFETY: By the type invariant `addr` is a valid address for= MMIO operations. - unsafe { bindings::$c_fn(value, addr as *mut c_void) }; - Ok(()) + $call_macro!(fallible, $c_fn, self, $type_name, addr, value) } }; } @@ -451,44 +508,44 @@ fn maxsize(&self) -> usize { self.0.maxsize() } =20 - define_read!(fallible, try_read8, readb -> u8); - define_read!(fallible, try_read16, readw -> u16); - define_read!(fallible, try_read32, readl -> u32); + define_read!(fallible, try_read8, call_mmio_read(readb) -> u8); + define_read!(fallible, try_read16, call_mmio_read(readw) -> u16); + define_read!(fallible, try_read32, call_mmio_read(readl) -> u32); define_read!( fallible, #[cfg(CONFIG_64BIT)] try_read64, - readq -> u64 + call_mmio_read(readq) -> u64 ); =20 - define_write!(fallible, try_write8, writeb <- u8); - define_write!(fallible, try_write16, writew <- u16); - define_write!(fallible, try_write32, writel <- u32); + define_write!(fallible, try_write8, call_mmio_write(writeb) <- u8); + define_write!(fallible, try_write16, call_mmio_write(writew) <- u16); + define_write!(fallible, try_write32, call_mmio_write(writel) <- u32); define_write!( fallible, #[cfg(CONFIG_64BIT)] try_write64, - writeq <- u64 + call_mmio_write(writeq) <- u64 ); =20 - define_read!(infallible, read8, readb -> u8); - define_read!(infallible, read16, readw -> u16); - define_read!(infallible, read32, readl -> u32); + define_read!(infallible, read8, call_mmio_read(readb) -> u8); + define_read!(infallible, read16, call_mmio_read(readw) -> u16); + define_read!(infallible, read32, call_mmio_read(readl) -> u32); define_read!( infallible, #[cfg(CONFIG_64BIT)] read64, - readq -> u64 + call_mmio_read(readq) -> u64 ); =20 - define_write!(infallible, write8, writeb <- u8); - define_write!(infallible, write16, writew <- u16); - define_write!(infallible, write32, writel <- u32); + define_write!(infallible, write8, call_mmio_write(writeb) <- u8); + define_write!(infallible, write16, call_mmio_write(writew) <- u16); + define_write!(infallible, write32, call_mmio_write(writel) <- u32); define_write!( infallible, #[cfg(CONFIG_64BIT)] write64, - writeq <- u64 + call_mmio_write(writeq) <- u64 ); } =20 @@ -506,43 +563,43 @@ pub unsafe fn from_raw(raw: &MmioRaw) -> &Self { unsafe { &*core::ptr::from_ref(raw).cast() } } =20 - define_read!(infallible, pub read8_relaxed, readb_relaxed -> u8); - define_read!(infallible, pub read16_relaxed, readw_relaxed -> u16); - define_read!(infallible, pub read32_relaxed, readl_relaxed -> u32); + define_read!(infallible, pub read8_relaxed, call_mmio_read(readb_relax= ed) -> u8); + define_read!(infallible, pub read16_relaxed, call_mmio_read(readw_rela= xed) -> u16); + define_read!(infallible, pub read32_relaxed, call_mmio_read(readl_rela= xed) -> u32); define_read!( infallible, #[cfg(CONFIG_64BIT)] pub read64_relaxed, - readq_relaxed -> u64 + call_mmio_read(readq_relaxed) -> u64 ); =20 - define_read!(fallible, pub try_read8_relaxed, readb_relaxed -> u8); - define_read!(fallible, pub try_read16_relaxed, readw_relaxed -> u16); - define_read!(fallible, pub try_read32_relaxed, readl_relaxed -> u32); + define_read!(fallible, pub try_read8_relaxed, call_mmio_read(readb_rel= axed) -> u8); + define_read!(fallible, pub try_read16_relaxed, call_mmio_read(readw_re= laxed) -> u16); + define_read!(fallible, pub try_read32_relaxed, call_mmio_read(readl_re= laxed) -> u32); define_read!( fallible, #[cfg(CONFIG_64BIT)] pub try_read64_relaxed, - readq_relaxed -> u64 + call_mmio_read(readq_relaxed) -> u64 ); =20 - define_write!(infallible, pub write8_relaxed, writeb_relaxed <- u8); - define_write!(infallible, pub write16_relaxed, writew_relaxed <- u16); - define_write!(infallible, pub write32_relaxed, writel_relaxed <- u32); + define_write!(infallible, pub write8_relaxed, call_mmio_write(writeb_r= elaxed) <- u8); + define_write!(infallible, pub write16_relaxed, call_mmio_write(writew_= relaxed) <- u16); + define_write!(infallible, pub write32_relaxed, call_mmio_write(writel_= relaxed) <- u32); define_write!( infallible, #[cfg(CONFIG_64BIT)] pub write64_relaxed, - writeq_relaxed <- u64 + call_mmio_write(writeq_relaxed) <- u64 ); =20 - define_write!(fallible, pub try_write8_relaxed, writeb_relaxed <- u8); - define_write!(fallible, pub try_write16_relaxed, writew_relaxed <- u16= ); - define_write!(fallible, pub try_write32_relaxed, writel_relaxed <- u32= ); + define_write!(fallible, pub try_write8_relaxed, call_mmio_write(writeb= _relaxed) <- u8); + define_write!(fallible, pub try_write16_relaxed, call_mmio_write(write= w_relaxed) <- u16); + define_write!(fallible, pub try_write32_relaxed, call_mmio_write(write= l_relaxed) <- u32); define_write!( fallible, #[cfg(CONFIG_64BIT)] pub try_write64_relaxed, - writeq_relaxed <- u64 + call_mmio_write(writeq_relaxed) <- u64 ); } --=20 2.51.0 From nobody Sun Feb 8 04:57:37 2026 Received: from CH5PR02CU005.outbound.protection.outlook.com (mail-northcentralusazon11012003.outbound.protection.outlook.com [40.107.200.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5DF92381710; Wed, 21 Jan 2026 20:23:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.200.3 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769026998; cv=fail; b=ORuBySEWp8tcZapSTVoJqa/PwCA0AQbVjPxff1DjZbij/j4YmPa8rm5B5xInTAHde++P4XqjT6R2B8Sav3lorsGGcwWukDM2TwO8PAeIGcpbnfV6GJSTiWC8K7z4dsY4IIthQb8imDlNwkSF9NhE+HvzQJ9djSQ/Dl7aoVzQUR4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769026998; c=relaxed/simple; bh=ea8SfwPHfXrd9Vn0IHe8ZEfIBNmof2pO7ZliIqAN558=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=id7RImzi8qQ+UfMrDWsnlP6UiisnNNAl2zcjsQz29pYqKQJ0S2v6U8KCgkXtsenOoSLaUlXU+2lPv36F0y91npaqIp2q9uQipsXoCTikM1AcxgVDYAVQkr3cePRlNKs1fINC09y+Pjwk3TCIZH/XduDFRvfVwLztAOmrYtXQ9j8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=pVDn2KzF; arc=fail smtp.client-ip=40.107.200.3 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) header.d=Nvidia.com header.i=@Nvidia.com header.b="pVDn2KzF" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=JdeASngR8bJRv3wUfvkph5uUrOE3kuszzh2ggX1+097Mnm1dfAE22KuBI0iI+Rxri2ar/BLZwJVrCePde6YXUOYlCeACHMbiMZWVwYvM5/Deg1lJ9n9FEdqTMSLJs94dSA3on0jzVLmevZ/jPffysiCI5q5wTYn08XbFzRcJxGcnVu2GCNuCh0ZMsPpfUf8taWCCq70CQy2/R3twY4I2FBmiGt1ZTm6zTv1ELddQYMPPxyCypvDX176i0xOTPzJOS7F8M+V8p6lWtD6IXPQxK/e2EYKJRNzujSh9W2BuTzUc+4Iz1L5Uo+6HbBU655LIdmQKeFFiAWG+I+AxNJU6lA== 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=0Qe1Pdnfw0CCAjk62etWuE115kRttKl7phAQm5Sgw7k=; b=BDLZJj87Tsk4aQFFkVTk1+pd2KE6v6qhbJee4mRLrh3RnXOodytIGnpAHoGUaBPLzYLqe0xTjwRwIgMR+sOYQqZBv/eBJtxa6FaKzITyHgB+FmFI5TeTF4N113ummjXGCluuAzmsKmlO08RKNtn5OPB0X5Pk6Y2MZ7KpcX208nyEiqzN75jnGyHyhbbo3g35WO6p9CKmzAXL1lpK0W7oQQi1oJ2fDZ3rmCkDBwH/C9VlNeUBJIw7dlXlVq+PalRre8HP87mVACxCjx7YDp8uq08JnUQGznUaPwMSFdeCcDMV2SAGFujSWZCL3RYOf6pH0x2DowTlyMawT6lMHK7M2g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=0Qe1Pdnfw0CCAjk62etWuE115kRttKl7phAQm5Sgw7k=; b=pVDn2KzFSNbaDqco752s/f3WUbwuDU9okW7ayMKNaxYqZN3MnflBPzOcZgN3fosEnR0nnVsKDfM6FEEnwg7ZwyFxS7otolLH8iHwmLcQhBJ1za388HW4RDIeF/tj2XWqj990KmX1yObyQVDQ2DUuJ4GTLFwPc9AuGQ9Rf2l//O6Z30Jeh9F4kpBsgwsFNhn0VdTat5ubt5X/mwnoiiKZH+A6M5nGb2nbUc2aKnvsWagVUlt0t+JBSz7cC1wqRWxwQqqSY/L9eGR6GgPI8sJxnhurMVIvSm3/nCDOetkUmEjv06tKb+Ybq9pqzk6uI7HHwqqYQi67FS5Dv3/wNSHbvQ== Received: from CH0PR03CA0234.namprd03.prod.outlook.com (2603:10b6:610:e7::29) by PH0PR12MB8775.namprd12.prod.outlook.com (2603:10b6:510:28e::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9520.11; Wed, 21 Jan 2026 20:23:03 +0000 Received: from DS2PEPF0000343F.namprd02.prod.outlook.com (2603:10b6:610:e7:cafe::ab) by CH0PR03CA0234.outlook.office365.com (2603:10b6:610:e7::29) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9542.10 via Frontend Transport; Wed, 21 Jan 2026 20:22:54 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by DS2PEPF0000343F.mail.protection.outlook.com (10.167.18.42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9542.4 via Frontend Transport; Wed, 21 Jan 2026 20:23:03 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Wed, 21 Jan 2026 12:22:46 -0800 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Wed, 21 Jan 2026 12:22:45 -0800 Received: from inno-vm-xubuntu (10.127.8.11) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Wed, 21 Jan 2026 12:22:39 -0800 From: Zhi Wang To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , Zhi Wang Subject: [PATCH v12 4/5] rust: pci: add config space read/write support Date: Wed, 21 Jan 2026 22:22:10 +0200 Message-ID: <20260121202212.4438-5-zhiw@nvidia.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260121202212.4438-1-zhiw@nvidia.com> References: <20260121202212.4438-1-zhiw@nvidia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF0000343F:EE_|PH0PR12MB8775:EE_ X-MS-Office365-Filtering-Correlation-Id: fae0f731-5f81-474b-d952-08de592ae10b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|376014|7416014|1800799024|82310400026; X-Microsoft-Antispam-Message-Info: =?utf-8?B?UnRscVJxNkRMTDZoTW5Kd3lnaWlVQ3JXeVJkVytoSkdCTGhuS0xHVGZmTVQ0?= =?utf-8?B?dTQwUkh4eFRnWWRuMmJGaVNjT0YybUVsSVRqdUhpYUJqVDE2ZWJDU3hFNFVE?= =?utf-8?B?NjdCT08vSnFwNnlLWStOV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mM1RZ?= =?utf-8?B?Yk1ndFRGSzFLajhFWW5Xd3gvOWt4WVRHQmxhQnRyWVpYeWFuVzhWWElFdENu?= =?utf-8?B?ejdLZTdYOWVKT0x1VFRJd0h4elJONCsyc1QxNGM3azFJdEFyVXhQTUlxNUh2?= =?utf-8?B?eXdqbk5Xa1k2eHZNK1BHZTZVb3kxbnRBMENIVldMQThlajN0YnhZdjRKMWlt?= =?utf-8?B?Ny9GY1U2Wko4emlmY3Z0bEF6eEpDeWptSmt1VmJka2ZIVkpqQXhzc0VxSGsy?= =?utf-8?B?SGVNdUE0cW45Z0FkSXlZaGE2eksxV1N4UXdTVlFZSXpla2d0NGJ6RTNZN05p?= =?utf-8?B?ZGtnM25kaXpWN2pxQmRPM3pIRkJBPT0=?= X-Forefront-Antispam-Report: CIP:216.228.118.232;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge1.nvidia.com;CAT:NONE;SFS:(13230040)(36860700013)(376014)(7416014)(1800799024)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Jan 2026 20:23:03.0979 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fae0f731-5f81-474b-d952-08de592ae10b X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.232];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS2PEPF0000343F.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB8775 Drivers might need to access PCI config space for querying capability structures and access the registers inside the structures. For Rust drivers need to access PCI config space, the Rust PCI abstraction needs to support it in a way that upholds Rust's safety principles. Introduce a `ConfigSpace` wrapper in Rust PCI abstraction to provide safe accessors for PCI config space. The new type implements the `Io` trait and `IoCapable` for u8, u16, and u32 to share offset validation and bound-checking logic with other I/O backends. The `ConfigSpace` type uses marker types (`Normal` and `Extended`) to represent configuration space sizes at the type level. Cc: Alexandre Courbot Cc: Danilo Krummrich Cc: Gary Guo Cc: Joel Fernandes Signed-off-by: Zhi Wang Reviewed-by: Gary Guo --- rust/kernel/pci.rs | 7 +- rust/kernel/pci/io.rs | 167 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 172 insertions(+), 2 deletions(-) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 82e128431f08..9020959ce0c7 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -40,7 +40,12 @@ ClassMask, Vendor, // }; -pub use self::io::Bar; +pub use self::io::{ + Bar, + ConfigSpaceSize, + Extended, + Normal, // +}; pub use self::irq::{ IrqType, IrqTypes, diff --git a/rust/kernel/pci/io.rs b/rust/kernel/pci/io.rs index e3377397666e..39df41d0eaab 100644 --- a/rust/kernel/pci/io.rs +++ b/rust/kernel/pci/io.rs @@ -8,13 +8,149 @@ device, devres::Devres, io::{ + define_read, + define_write, + Io, + IoCapable, + IoKnownSize, Mmio, MmioRaw, // }, prelude::*, sync::aref::ARef, // }; -use core::ops::Deref; +use core::{ + marker::PhantomData, + ops::Deref, // +}; + +/// Marker type for normal (256-byte) PCI configuration space. +pub struct Normal; + +/// Marker type for extended (4096-byte) PCIe configuration space. +pub struct Extended; + +/// Trait for PCI configuration space size markers. +/// +/// This trait is implemented by [`Normal`] and [`Extended`] to provide +/// compile-time knowledge of the configuration space size. +pub trait ConfigSpaceSize { + /// The size of this configuration space in bytes. + const SIZE: usize; +} + +impl ConfigSpaceSize for Normal { + const SIZE: usize =3D 256; +} + +impl ConfigSpaceSize for Extended { + const SIZE: usize =3D 4096; +} + +/// The PCI configuration space of a device. +/// +/// Provides typed read and write accessors for configuration registers +/// using the standard `pci_read_config_*` and `pci_write_config_*` helper= s. +/// +/// The generic parameter `S` indicates the maximum size of the configurat= ion space. +/// Use [`Normal`] for 256-byte legacy configuration space or [`Extended`]= for +/// 4096-byte PCIe extended configuration space (default). +pub struct ConfigSpace<'a, S: ConfigSpaceSize =3D Extended> { + pub(crate) pdev: &'a Device, + _marker: PhantomData, +} + +/// Internal helper macros used to invoke C PCI configuration space read f= unctions. +/// +/// This macro is intended to be used by higher-level PCI configuration sp= ace access macros +/// (define_read) and provides a unified expansion for infallible vs. fall= ible read semantics. It +/// emits a direct call into the corresponding C helper and performs the r= equired cast to the Rust +/// return type. +/// +/// # Parameters +/// +/// * `$c_fn` =E2=80=93 The C function performing the PCI configuration sp= ace write. +/// * `$self` =E2=80=93 The I/O backend object. +/// * `$ty` =E2=80=93 The type of the value to read. +/// * `$addr` =E2=80=93 The PCI configuration space offset to read. +/// +/// This macro does not perform any validation; all invariants must be uph= eld by the higher-level +/// abstraction invoking it. +macro_rules! call_config_read { + (infallible, $c_fn:ident, $self:ident, $ty:ty, $addr:expr) =3D> {{ + let mut val: $ty =3D 0; + // SAFETY: By the type invariant `$self.pdev` is a valid address. + // CAST: The offset is cast to `i32` because the C functions expec= t a 32-bit signed offset + // parameter. PCI configuration space size is at most 4096 bytes, = so the value always fits + // within `i32` without truncation or sign change. + // Return value from C function is ignored in infallible accessors. + let _ret =3D unsafe { bindings::$c_fn($self.pdev.as_raw(), $addr a= s i32, &mut val) }; + val + }}; +} + +/// Internal helper macros used to invoke C PCI configuration space write = functions. +/// +/// This macro is intended to be used by higher-level PCI configuration sp= ace access macros +/// (define_write) and provides a unified expansion for infallible vs. fal= lible read semantics. It +/// emits a direct call into the corresponding C helper and performs the r= equired cast to the Rust +/// return type. +/// +/// # Parameters +/// +/// * `$c_fn` =E2=80=93 The C function performing the PCI configuration sp= ace write. +/// * `$self` =E2=80=93 The I/O backend object. +/// * `$ty` =E2=80=93 The type of the written value. +/// * `$addr` =E2=80=93 The configuration space offset to write. +/// * `$value` =E2=80=93 The value to write. +/// +/// This macro does not perform any validation; all invariants must be uph= eld by the higher-level +/// abstraction invoking it. +macro_rules! call_config_write { + (infallible, $c_fn:ident, $self:ident, $ty:ty, $addr:expr, $value:expr= ) =3D> { + // SAFETY: By the type invariant `$self.pdev` is a valid address. + // CAST: The offset is cast to `i32` because the C functions expec= t a 32-bit signed offset + // parameter. PCI configuration space size is at most 4096 bytes, = so the value always fits + // within `i32` without truncation or sign change. + // Return value from C function is ignored in infallible accessors. + let _ret =3D unsafe { bindings::$c_fn($self.pdev.as_raw(), $addr a= s i32, $value) }; + }; +} + +// PCI configuration space supports 8, 16, and 32-bit accesses. +impl<'a, S: ConfigSpaceSize> IoCapable for ConfigSpace<'a, S> {} +impl<'a, S: ConfigSpaceSize> IoCapable for ConfigSpace<'a, S> {} +impl<'a, S: ConfigSpaceSize> IoCapable for ConfigSpace<'a, S> {} + +impl<'a, S: ConfigSpaceSize> Io for ConfigSpace<'a, S> { + const MIN_SIZE: usize =3D S::SIZE; + + /// Returns the base address of the I/O region. It is always 0 for con= figuration space. + #[inline] + fn addr(&self) -> usize { + 0 + } + + /// Returns the maximum size of the configuration space. + #[inline] + fn maxsize(&self) -> usize { + self.pdev.cfg_size().map_or(0, |v| v) + } + + // PCI configuration space does not support fallible operations. + // The default implementations from the Io trait are not used. + + define_read!(infallible, read8, call_config_read(pci_read_config_byte)= -> u8); + define_read!(infallible, read16, call_config_read(pci_read_config_word= ) -> u16); + define_read!(infallible, read32, call_config_read(pci_read_config_dwor= d) -> u32); + + define_write!(infallible, write8, call_config_write(pci_write_config_b= yte) <- u8); + define_write!(infallible, write16, call_config_write(pci_write_config_= word) <- u16); + define_write!(infallible, write32, call_config_write(pci_write_config_= dword) <- u32); +} + +/// Marker trait indicating ConfigSpace has a known size at compile time. +impl<'a, S: ConfigSpaceSize> IoKnownSize for ConfigSpace<'a, S> {} =20 /// A PCI BAR to perform I/O-Operations on. /// @@ -144,4 +280,33 @@ pub fn iomap_region<'a>( ) -> impl PinInit, Error> + 'a { self.iomap_region_sized::<0>(bar, name) } + + /// Returns the size of configuration space in bytes. + fn cfg_size(&self) -> Result { + // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`. + let size =3D unsafe { (*self.as_raw()).cfg_size }; + match size { + 256 | 4096 =3D> Ok(size as usize), + _ =3D> { + debug_assert!(false); + Err(EINVAL) + } + } + } + + /// Return an initialized normal (256-byte) config space object. + pub fn config_space<'a>(&'a self) -> Result> { + Ok(ConfigSpace { + pdev: self, + _marker: PhantomData, + }) + } + + /// Return an initialized extended (4096-byte) config space object. + pub fn config_space_extended<'a>(&'a self) -> Result> { + Ok(ConfigSpace { + pdev: self, + _marker: PhantomData, + }) + } } --=20 2.51.0 From nobody Sun Feb 8 04:57:37 2026 Received: from CH1PR05CU001.outbound.protection.outlook.com (mail-northcentralusazon11010064.outbound.protection.outlook.com [52.101.193.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C3CCD3C1976; Wed, 21 Jan 2026 20:23:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.193.64 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769026995; cv=fail; b=AmG5DKtrqoEIEtAMvxFzZmRBGpJP6KbYYrLvak1ufUtEFlg64LcmFhVprx+wKZ8F0ogn53IXwtm6cr2xGN1XM8/7O0RtvyMXbI2/sjpy+uPV17BAG67e5r94K5gMXjSbRvKKJ50AEhWbN4TFtucEQkMg6o2bqO5zP0JOuY5LuhU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769026995; c=relaxed/simple; bh=Mgg5rRqyZwHP2sTo/X4MvWcFkp3NIcgILNd3twNdYuA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=VoeL0hNwZouV1PwxoMyfo64cfcTeGel+ImHmcGqKoXTKofxF8k+iXJQIJkEBtJGhQpwWWBWZWBmkfXbs7hmbcb+5lQOMZiu2somrPxeDvDxWIchpGqr0a5ji1VrG27a0MHdyPrRjEkBHVZLPAq0H+NTu57w2C7YQk3lxZSqsXhw= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=lfvXZSl2; arc=fail smtp.client-ip=52.101.193.64 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) header.d=Nvidia.com header.i=@Nvidia.com header.b="lfvXZSl2" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=F8NFmYQPMq8t3SpBnJYlyl8qoYlKHBsqpEjGnkZVz/XExQ6Il19H5tJbrOjaD/B/xSanMcJx8M+/fpCn0yR+ZBOVgMfOoRwA5j7nZQLOILtuOzFf/pMLhCaTn++dEfuZQVYvjI+KE3TQSsWCGXmH5XEIOPNISNA8PCmTFehTA+AyB/WO3lCWut+0xfWypWwGn0sIGijOVNDcGcq8oWOlLpj6T1wKTOXYW4aZFwEh5mLlNGMdP0FhCZEVwh4no3nJubvQg6EtiRfawGC64IBIttWO5HquydlGHZrWd/OPgbjw1DsowIdSwxFhzaFm2JiYRqCvwPfT8x1t4Y8Z2D4OZg== 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=2WESaqIq8dHbYW0/RrreUMYYjBhW5WCK172vSILfzis=; b=KJ6WUq29bV8iX0oyZjyE7K5x9aS9/4o2z3/a0bq3qTsypRXdi+edwf4wGQsWBng1dyIE5bv5uTUy+r0kMPoNbdUe7TLEBJ1XVMTDNSbnD57WaXFfUo3pt6HO6ZIBZ0d6HnfR5mznpms1gPJ8Ll+qWJgadIE9i4cSlMTRWzZXugBb3IRa+PmfDKn0UOHvXt0GKFf/44yZH39j7O7uMXvORhDnRb6srx7z6ZIcOyzMriBP4nC+f6rhdv7KowVlx/SHVSW2WHgkeAEGPYsYAIEgScxcnUfwY1/zFs8Zu8UgHKYuS9YBc7Xw03zQPaHAsKFbrwCbnvjP+kbLvz/ElqjcqQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=2WESaqIq8dHbYW0/RrreUMYYjBhW5WCK172vSILfzis=; b=lfvXZSl2CEvcwBmCQFQWEJZ/Ps17Keii+coGBr3ZYaMr2u1fpHhVIJYKwbBF+7uGTpKbCfZrK2rKhh59Mr/WSedz5DUZ6/6Z3+rcnBGz+UrIgxtcr0dCzdL9nBYqV8RElZlmh0IM8PotNcAzjdpdgERFY+K5MtILqpImEHtUIkZ6WfhHl+AvIj+eTFQ/3UKWyyR/Yw9d/gchgzqo2cHp7MqCfTZJ/bnL9+uDdnsZPFsOAeL94DCOsI87u3iglktZHk7JPgBQpHl9hD7Mhcyc7d27wRABjwRWveI0sjLqex9vYRJnESP/V0OFHdZvwh9lrRh0cju3Ari2QVLlsUSr8w== Received: from CH0PR03CA0072.namprd03.prod.outlook.com (2603:10b6:610:cc::17) by SJ5PPF6375781D1.namprd12.prod.outlook.com (2603:10b6:a0f:fc02::995) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9542.9; Wed, 21 Jan 2026 20:23:08 +0000 Received: from DS2PEPF00003440.namprd02.prod.outlook.com (2603:10b6:610:cc:cafe::16) by CH0PR03CA0072.outlook.office365.com (2603:10b6:610:cc::17) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9542.10 via Frontend Transport; Wed, 21 Jan 2026 20:22:58 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by DS2PEPF00003440.mail.protection.outlook.com (10.167.18.43) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9542.4 via Frontend Transport; Wed, 21 Jan 2026 20:23:08 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Wed, 21 Jan 2026 12:22:52 -0800 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Wed, 21 Jan 2026 12:22:52 -0800 Received: from inno-vm-xubuntu (10.127.8.11) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Wed, 21 Jan 2026 12:22:46 -0800 From: Zhi Wang To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , Zhi Wang Subject: [PATCH v12 5/5] sample: rust: pci: add tests for config space routines Date: Wed, 21 Jan 2026 22:22:11 +0200 Message-ID: <20260121202212.4438-6-zhiw@nvidia.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260121202212.4438-1-zhiw@nvidia.com> References: <20260121202212.4438-1-zhiw@nvidia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF00003440:EE_|SJ5PPF6375781D1:EE_ X-MS-Office365-Filtering-Correlation-Id: 693c3255-d074-460b-f054-08de592ae414 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|82310400026|36860700013; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?G2YW64pFQbjyaTlI4cbwGF2wFX3Sods5vz0e8oGFh5X19A/1w4bJf3rzjFjA?= =?us-ascii?Q?lP2LDTgStJKjqlkbyOYfJqJK+zgn23dK3ePJfZB6SCsWfKUjDiEpBHnCIxME?= =?us-ascii?Q?f62zA3AlTwjKXe8VzxgthA9zSM/cEIp/qk9z84BZ656RxzR3Ty+AsrOAM9sP?= =?us-ascii?Q?9l/sCJC6itwVXT5t4RuhVtmsSGBa/A/a6GFYxSEXhfcahUtYhDl0GJf1wfh0?= =?us-ascii?Q?aNmlK20BcxbMiYKy87sSi/tIKhrXQvjRIVnGSJHJT9hHjPF9Buwd9s5nk9fe?= =?us-ascii?Q?EBrZ6bZo3KHHTCat042sckNY7mw39ThVAfR0eQKZEUNBbRVusQzsiORs574i?= =?us-ascii?Q?II+DkjMAvHHuhiPvdlCQsQ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+Es2nMQE1XENx+ZMKRHtZhmWa++F1SmR85THO2zhoVpMt3AkMa7ZPsd?= =?us-ascii?Q?AvglyF2vc6RiW0zFGLAw3x4mnzn0t/7bc6sLqVsqDrAuaQ7B0DVPWcCdQGfX?= =?us-ascii?Q?MzBk+SwZgQXPIKMZsujeVvzYQLfEcNBbM0WQD18xX4eeJcNItkCq/4Uk+QZA?= =?us-ascii?Q?ZbVL2QHFBmDeCl3wM8WIYVAnu/red6g/V2Jc5j6I3sx70mju7WarqqA80y1+?= =?us-ascii?Q?brvnHcDKrXvgL60u2bznY3iKUrhSboDnC4sLATDXUNLDYn9L0QBVjOoI1Ztr?= =?us-ascii?Q?zypglSodLH1q8I/0lNhkXCXwt6ZMsozMcz7k6yTAEMlcqVSnjm/aRmucSZyx?= =?us-ascii?Q?ympGs7johgjtBSrVhE1JUavhRPLTqdlpHMtsbrICw1lOI0218qUTn+OW9/as?= =?us-ascii?Q?Hd/wKw=3D=3D?= X-Forefront-Antispam-Report: CIP:216.228.118.232;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge1.nvidia.com;CAT:NONE;SFS:(13230040)(376014)(7416014)(1800799024)(82310400026)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Jan 2026 20:23:08.1902 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 693c3255-d074-460b-f054-08de592ae414 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.232];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS2PEPF00003440.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ5PPF6375781D1 Content-Type: text/plain; charset="utf-8" Add tests exercising the PCI configuration space helpers. Suggested-by: Danilo Krummrich Signed-off-by: Zhi Wang Reviewed-by: Alexandre Courbot Reviewed-by: Gary Guo --- samples/rust/rust_driver_pci.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/samples/rust/rust_driver_pci.rs b/samples/rust/rust_driver_pci= .rs index 38c949efce38..1bc5bd1a8df5 100644 --- a/samples/rust/rust_driver_pci.rs +++ b/samples/rust/rust_driver_pci.rs @@ -5,6 +5,7 @@ //! To make this driver probe, QEMU must be run with `-device pci-testdev`. =20 use kernel::{ + device::Bound, device::Core, devres::Devres, io::Io, @@ -65,6 +66,32 @@ fn testdev(index: &TestIndex, bar: &Bar0) -> Result= { =20 Ok(bar.read32(Regs::COUNT)) } + + fn config_space(pdev: &pci::Device) -> Result { + let config =3D pdev.config_space()?; + + // TODO: use the register!() macro for defining PCI configuration = space registers once it + // has been move out of nova-core. + dev_info!( + pdev.as_ref(), + "pci-testdev config space read8 rev ID: {:x}\n", + config.read8(0x8) + ); + + dev_info!( + pdev.as_ref(), + "pci-testdev config space read16 vendor ID: {:x}\n", + config.read16(0) + ); + + dev_info!( + pdev.as_ref(), + "pci-testdev config space read32 BAR 0: {:x}\n", + config.read32(0x10) + ); + + Ok(()) + } } =20 impl pci::Driver for SampleDriver { @@ -96,6 +123,7 @@ fn probe(pdev: &pci::Device, info: &Self::IdInfo) = -> impl PinInit