From nobody Sun Feb 8 02:41:15 2026 Received: from PH8PR06CU001.outbound.protection.outlook.com (mail-westus3azon11012041.outbound.protection.outlook.com [40.107.209.41]) (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 D922748B39C; Wed, 21 Jan 2026 14:24:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.209.41 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769005482; cv=fail; b=ZslvGPoKmEqQDwI6nvb7oyz9wxUCP44p9KHtV/bEzMbNRQVfn46P3r0Fx9Gk83jiPYliLxJqatDspOMaXTA8GYfQDT3LpF64XG5eS84UVL1/AKmdtaIex/MVW8iSROqOaBQhv8PNDalJZG0C26OTWk35zj7Ovm48EO7dgtR5nqg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769005482; c=relaxed/simple; bh=Lxj4PkBapAtBNnB2X0r047B1aw4vPK6YYG+v7Ht85A0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=AN9ob+qI7hIS4kEnTgcLQ6aFWxlCz0YFjG9K8Z43nQFz8RY1mHmcVWcGTpqwLuhAbNayzgLriNEYsJlKZ7RtvuxmmIKOc3M68r/BfUvlK0WpcKGO4x5+/c7DTJbpzlt+HCyAMOWsRqcuRdVXLtRlXf5mfxV8nlHiy1O52oca7q8= 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=Y8tYoPJE; arc=fail smtp.client-ip=40.107.209.41 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="Y8tYoPJE" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=QixhPyaI/DnATTS5rzx5jx0S0LersfSvn4duuZCKiElqq6bACQ7Ys2FysH7i9+sX9JyL34yvH0L2mDboO3+N3hYTTYpnKGTcvb/ebhnxcbI8B+3e0+VzvlyAG2E2EeX1Dpf571fV92DcBq4rS14L4t60LA7aRk8ADJeB1ZKcFPhCjDa1PR+Vvaot0PVoDo2XBf8wExB6X/MddevfYfn+npNOuCl+0AxAjOY4qrm7L7jZHsjG0ImRr9pMg56gfzSMDN2TK9PMvVyXnXKCk1YaXCMrxHU2fIr8eP6a900lNH7T5Y2+WREpT2/3i/A3/js8SkNVUGfyGDz2K0KqNwv41w== 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=PVr6029GJBT27W7/udV0dd+wNmZk+pMBNPRUNVuJAYA=; b=OlUq2j6r0UuUtWYN+lbdflCkfmHLcdsng99K6KxNPZYvtG8a0DuM6UWYIbnLcH44Y0aZuBg/XGxceVEjQqy8bPdiGmZ2Y4kAcoCGeFV/Luli4ZE4VMEDqFratjFQZUDX3sXzCGa8GYolYhTcVmeMTTkof+gbD39o/ZjIiyy4amQ9ZFZ+nuQfE0aBb2DzD7Df2+N+yKL2TngB3FC1KoeMPLCRmZgsRp1wco4RrspauMOItLjUubsQxvf/a4EqiFRxvII21OiYK9ZcxVYsjZKS+3ZPfRJFoyxh79TmLqOoGsmXhyWVEkl/lag84tHRJUjv7HqQ43Pi4FpeMsZNDqnNvg== 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=PVr6029GJBT27W7/udV0dd+wNmZk+pMBNPRUNVuJAYA=; b=Y8tYoPJE97cV7BU52jfm7AxC23aGchsBthhdY43cK90NBoAMQH3GYNWo08ksDhxNcCmNKrEaXXn6Hmf97lGtPAcEGUsywiFnzn8EeLhE/C64vmWy6xJB5849qvxtPq5vks9AXVBgfAuCEaDOIgoZPhGuEmNQX3FXsmoB1Ow6MsDq1Rt98QYnVJnNkNxb5XldB2PGts396eaf/NvOghB0M1jEmT0hH4+R8eHuFhVThfnTRHrDnnOOjuW+zAxq7HdJFkj4fLyhY74BVG47RYLa7rCAE8VUe064Ek8Rs/GxBQODwEddoRzaGnckFy/xydyItEpHoTES6ds9VsW3k+6LIg== Received: from BYAPR11CA0067.namprd11.prod.outlook.com (2603:10b6:a03:80::44) by DS2PR12MB9664.namprd12.prod.outlook.com (2603:10b6:8:27b::10) 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 14:24:33 +0000 Received: from CO1PEPF000075EF.namprd03.prod.outlook.com (2603:10b6:a03:80:cafe::f7) by BYAPR11CA0067.outlook.office365.com (2603:10b6:a03:80::44) 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 14:24:30 +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 CO1PEPF000075EF.mail.protection.outlook.com (10.167.249.38) 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 14:24:32 +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 06:24:10 -0800 Received: from drhqmail201.nvidia.com (10.126.190.180) 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 06:24:10 -0800 Received: from inno-vm-xubuntu (10.127.8.11) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Wed, 21 Jan 2026 06:24:04 -0800 From: Zhi Wang To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , Zhi Wang , Miguel Ojeda Subject: [PATCH v11 1/5] rust: devres: style for imports Date: Wed, 21 Jan 2026 16:23:50 +0200 Message-ID: <20260121142355.4761-2-zhiw@nvidia.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260121142355.4761-1-zhiw@nvidia.com> References: <20260121142355.4761-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: CO1PEPF000075EF:EE_|DS2PR12MB9664:EE_ X-MS-Office365-Filtering-Correlation-Id: d318538a-45a1-4f6f-d46b-08de58f8cbc4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|36860700013|82310400026; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?9D+Hg4sjkx86h32PqTWJ9ErouQBj9BaEbTcm+lY2CpS3z/f+Fzdq0SFswJ2t?= =?us-ascii?Q?RGXWBw80kDBm3CCtiOWrAKLoxkNlQi6JWXsOPO2aw5g/6fc6xAMrD4ls57YU?= =?us-ascii?Q?E/u9qkHBpMEQj7qH9L7Mh0tiwaJJTmhSdJ8Lj0YL2hQlH7aLF9smXQ04lx1x?= =?us-ascii?Q?0G9jp8GdchO7939aOO8fQuJIm70rZ0ni3a4vygus00EQdhfLf7jz85uIEzYx?= =?us-ascii?Q?Qs0idhLqHDb9UjO6U/QcMINm9LzEvuyelG0fggg7p8cN3Wm4oGxn+9qbd2gy?= =?us-ascii?Q?MQUnxMwAecREjUUmKa9c9icaiQfINHiLNNbsVTFZQcipj/rJU7mp4fGApHuk?= =?us-ascii?Q?oDRJWyxMQFiFOgq4BqFROaaalyvGB/mu1jmpTfnCBw+xEsfieQgWAo9wkJpI?= =?us-ascii?Q?hI2ZpztXsJphLUNKSmKSDD5PeKXbL0p8mwd3DiqKfcUGTsHbvmRWat784BaG?= =?us-ascii?Q?oSqDFWVUIpDETYrptlDMyQvSXiYB0AmZhMlvOTtL2aDzQKSAczLml5QbFSVq?= =?us-ascii?Q?ee5aJDrP1e6AlNjVoqZMOzYxKLqfQLykAxJq+EJkDqXjE0QLylZqx9PK+Vgc?= =?us-ascii?Q?faq3/aOVljhTl0vlNJlAjvri9ndhW1BMPma9yJzu0SY0GwChIZ4oxi9Q0jjV?= =?us-ascii?Q?pC9jYoBrIUs/Mbv3nCGsMwC7+yLh8S5pv5smarr/FRRUJS7ebTFsI7zom987?= =?us-ascii?Q?v8V2kcC51a5w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Z1dZSjVTo4F1?= =?us-ascii?Q?kmS11ANvD25tB7GJsYozK5fGU8nLdvmR6hW1ifZzpolgRgFg+Suc4AC9Vy7L?= =?us-ascii?Q?vQVdhA=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)(376014)(7416014)(1800799024)(36860700013)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Jan 2026 14:24:32.6120 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d318538a-45a1-4f6f-d46b-08de58f8cbc4 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: CO1PEPF000075EF.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS2PR12MB9664 Content-Type: text/plain; charset="utf-8" Convert all imports in the devres to use "kernel vertical" style. Cc: Gary Guo Cc: Miguel Ojeda Signed-off-by: Zhi Wang --- 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 02:41:15 2026 Received: from DM1PR04CU001.outbound.protection.outlook.com (mail-centralusazon11010047.outbound.protection.outlook.com [52.101.61.47]) (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 CA987481649; Wed, 21 Jan 2026 14:24:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.61.47 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769005498; cv=fail; b=WQSzrL7/Bkkt5GYOgxXvLh05+Vhx8ufzStsWhV+Uah/haca/CaYUCsPpGMEIf/T7W2Gv4b2Knn8RibTN+GLzwvoO9/oHYhrzWpnA08KPG06bNlyuGkv7yUkcmgqYrBNMP7/zcItKPffBwnT31nTcchvMTtxQVpPziJ5ZlenkKQo= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769005498; c=relaxed/simple; bh=JRHAnejm+f6Or//nuPWpouyQgSkkcAx42dm1tNhFF+0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=BxkwXE4V68f8Je+yqIhXZH8GVTasYzh5vlu7gOGO3l4BXHc9t3m7yBE/oYSA0hZdYc1v+e0WrmtmXJ8BI0W6qJsWuJbmiFtmG0IXQwTleUlXzOi7XbxNjs7pQssmspZCfF3OUYbrI8sO4EnUiR645eU3XS5S/bjaxOM30o98LjM= 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=OcuigwCA; arc=fail smtp.client-ip=52.101.61.47 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="OcuigwCA" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Ipe1oEsOpwKPSoA1YM9ice/rz0bY/HOfwiv0ZsxyF7V5aq+0hJCfiM8xidXvENIGRmFZnIsJc/ajsbKqNXpMA/pecciw9KWEg5EIGFKsrWA9b17H93QaBAj0gZyMYZLtgxDvlblwi4Ftt3GEJ9KvAD5XZmDgLm3Qg2TIc+flfGSTDfjBi0IEeFi5gx6UwPpM2I1y3HvdGWX/1ZFnwISprz+d/U6EM4kqxIsrJT9AEHHiNdwxLH9QEIiEHfas2XwVAaN6R4/eIdT/wTKsF8u2gT3YLWDvCrK4h0l/8hv3YzLWVVCRt1DbdVf6gUOQXFvmS3uGJDbmjRueh503XYHspg== 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=40WYz7C6D8zHaZYAbZ/SenAxdOchwYn2cG/MwyyWV90=; b=ifCA6Y2DQUEaTSKPYThyEQuHRJrefkeq/PYdlgVBj9OVKQMGOvz3LeUK1PY3NZ3Zw30O+hQZ5MLYSs6quWFweB/gwwkzU/UHuxM8L13Kumvfu/4a8nlqoLe2nnvd5yS73qHj0I7mya2Z9KZ8sm7/FOtHHtHOtaLOH6Jqt5t+JKLkHNIHEG0+IRXTjv2jJV9an5y378UGwf93JLiLmDqiRulvaWzRzulQQqZgrASgyTwP+SMVp/0ztyY9lEFKpY4cnro5HS2lJaBYVUxfbanipQCwEvlCVYLk0Vv0nmWilriL7x0m1WreT0hYj2eFTbT0wE7dQjIcpMloKTSy7f8Nrw== 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=40WYz7C6D8zHaZYAbZ/SenAxdOchwYn2cG/MwyyWV90=; b=OcuigwCAHFnJtzQhO1C4LqW2udTZqX57JYGnj5VC5iWNETIb0VcLrZIWs2RtvU1PPiY158xZnFClzEw6bqQRTy1lEGqk2dr8IeJTPvz8faeJES6ia9MHs+ang9LXQ81YpAoXf6W+L8P/mxhPY9Q1ot0tN6zuiPMiSXETjSkGef6X5ojcbTgSDcrZXPkNe+WMCQO32CbEYLy4BSupL1Bej0bcDZqLqdHtem2+DhJMGcX8y/mjFRLMRf3ELLF3thcCAfUpXeUDtMXAJBZ+sN/UptiJLM8nyth9yXU+5ze1fRRlfUkH+hjoHcY8zUIv2Alsc2HsVTSdLfhL2HP4T349BQ== Received: from BYAPR06CA0007.namprd06.prod.outlook.com (2603:10b6:a03:d4::20) by SJ2PR12MB9164.namprd12.prod.outlook.com (2603:10b6:a03:556::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9520.9; Wed, 21 Jan 2026 14:24:40 +0000 Received: from CO1PEPF000075F3.namprd03.prod.outlook.com (2603:10b6:a03:d4:cafe::60) by BYAPR06CA0007.outlook.office365.com (2603:10b6:a03:d4::20) 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 14:24:39 +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 CO1PEPF000075F3.mail.protection.outlook.com (10.167.249.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 14:24:40 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) 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 06:24:17 -0800 Received: from drhqmail201.nvidia.com (10.126.190.180) 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 06:24:16 -0800 Received: from inno-vm-xubuntu (10.127.8.11) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Wed, 21 Jan 2026 06:24:10 -0800 From: Zhi Wang To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , Zhi Wang Subject: [PATCH v11 2/5] rust: io: separate generic I/O helpers from MMIO implementation Date: Wed, 21 Jan 2026 16:23:51 +0200 Message-ID: <20260121142355.4761-3-zhiw@nvidia.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260121142355.4761-1-zhiw@nvidia.com> References: <20260121142355.4761-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: CO1PEPF000075F3:EE_|SJ2PR12MB9164:EE_ X-MS-Office365-Filtering-Correlation-Id: dca8d8fb-1620-4bcd-c096-08de58f8d042 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|82310400026|376014|36860700013|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?oSinAwu1S+ke5adpyhz2xnJQ3tRXqRMODhAGiD2cDoRXBRmz3r1l/UGU2QmB?= =?us-ascii?Q?EhOOnxW/i+SVQUPazdvZkZomy27bJyMy1Y75gQuDBoxZKeTSt906seHRcRGL?= =?us-ascii?Q?iHBp5AjQNJLpwk7kxMnzJXECy3mJ4NJ09qNM/NkM+pX3ylsj16cQ2j+2KkF9?= =?us-ascii?Q?+eoXfsjL5IJF0dq7jzc320CF0RT5j4AvuZn6AqZhy7DOWnExHb6cAlE/wGY/?= =?us-ascii?Q?PQ9QkvY7f12WdmN1y9fpBFR+8dB0Sr+RCwC1D/HoLNqHJIO4zPZeptGDpM36?= =?us-ascii?Q?mJ74UkUF8+2AdfE6o07m4AMIH7bZKtNa81j86JRtnWu5ZNlLoa8e9fb09g7Z?= =?us-ascii?Q?/WvF0AYBh6cq+9a4M+VPWxsqXPahtV9KplKoUxCF9gHqiu245RDQXMZKfkc5?= =?us-ascii?Q?A4iagd+Ugq7SP2zs9DQ49J6XaXdikjihZ9MFN/b8C+waG7XPIuw/D+bChLLU?= =?us-ascii?Q?r37XzbxoW9KhHDVR7uuh7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ii?Q?2SkIGOnVR546+M06NcRknLgWgpJ6UuSawbGZKvlb3fjoGNrhPcG2aM4vZqyp?= =?us-ascii?Q?HfUFRPcNXqI8dm05wCECIgXWjuAHLBHWmDw6QoWWcqolU0hpk8JAkSc1f9sT?= =?us-ascii?Q?1811Qk+13K8OQ2CvmwnBlv7mb/j+1MQ5InZAmSIWdUdGc09ZTosI+YZ4TBM1?= =?us-ascii?Q?Eg1bbR836G3rTimb+XigejUwhQaos99VKOUUfsJ/ctVAWge6XHH0R/hlF2t7?= =?us-ascii?Q?IZHOYGLRARMGlfpiWB6EZiYTgTjxyxmwLc4ir4ISZKakK5RWEK+cgPL++UUV?= =?us-ascii?Q?kU/dPQ=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)(7416014)(82310400026)(376014)(36860700013)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Jan 2026 14:24:40.1709 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: dca8d8fb-1620-4bcd-c096-08de58f8d042 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: CO1PEPF000075F3.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9164 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 I/O methods (try_read8, try_write8, etc.) with runtime bounds checking. - IoKnownSize trait: Extends Io to define infallible I/O methods (read8, write8, etc.) with compile-time bounds checking for regions where the size is known at compile time. Move the MMIO-specific logic into a dedicated Mmio type that implements the Io and IoKnownSize 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 --- 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 | 415 +++++++++++++++++++++---- rust/kernel/io/mem.rs | 16 +- rust/kernel/io/poll.rs | 16 +- rust/kernel/pci/io.rs | 12 +- samples/rust/rust_driver_pci.rs | 4 + 11 files changed, 453 insertions(+), 131 deletions(-) diff --git a/drivers/gpu/drm/tyr/regs.rs b/drivers/gpu/drm/tyr/regs.rs index f46933aaa221..9f89bc73a775 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::IoKnownSize; 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..152afdcbaf78 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,302 @@ 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, + { + const { assert!(false, "Backend does not support fallible 8-bit re= ad") }; + unreachable!() + } + + /// Fallible 16-bit read with runtime bounds check. + #[inline(always)] + fn try_read16(&self, _offset: usize) -> Result + where + Self: IoCapable, + { + const { assert!(false, "Backend does not support fallible 16-bit r= ead") }; + unreachable!() + } + + /// Fallible 32-bit read with runtime bounds check. + #[inline(always)] + fn try_read32(&self, _offset: usize) -> Result + where + Self: IoCapable, + { + const { assert!(false, "Backend does not support fallible 32-bit r= ead") }; + unreachable!() + } + + /// Fallible 64-bit read with runtime bounds check. + #[cfg(CONFIG_64BIT)] + #[inline(always)] + fn try_read64(&self, _offset: usize) -> Result + where + Self: IoCapable, + { + const { assert!(false, "Backend does not support fallible 64-bit r= ead") }; + unreachable!() + } + + /// Fallible 8-bit write with runtime bounds check. + #[inline(always)] + fn try_write8(&self, _value: u8, _offset: usize) -> Result + where + Self: IoCapable, + { + const { assert!(false, "Backend does not support fallible 8-bit wr= ite") }; + unreachable!() + } + + /// Fallible 16-bit write with runtime bounds check. + #[inline(always)] + fn try_write16(&self, _value: u16, _offset: usize) -> Result + where + Self: IoCapable, + { + const { assert!(false, "Backend does not support fallible 16-bit w= rite") }; + unreachable!() + } + + /// Fallible 32-bit write with runtime bounds check. + #[inline(always)] + fn try_write32(&self, _value: u32, _offset: usize) -> Result + where + Self: IoCapable, + { + const { assert!(false, "Backend does not support fallible 32-bit w= rite") }; + unreachable!() + } + + /// Fallible 64-bit write with runtime bounds check. + #[cfg(CONFIG_64BIT)] + #[inline(always)] + fn try_write64(&self, _value: u64, _offset: usize) -> Result + where + Self: IoCapable, + { + const { assert!(false, "Backend does not support fallible 64-bit w= rite") }; + unreachable!() + } +} + +/// Types with a known size at compile time can provide infallible I/O acc= essors. +/// +/// This trait extends [`Io`] to provide compile-time bounds-checked I/O o= perations +/// for regions where the size is known at compile time (e.g., `Mmio= `). +pub trait IoKnownSize: Io { + /// Infallible 8-bit read with compile-time bounds check. + #[inline(always)] + fn read8(&self, _offset: usize) -> u8 + where + Self: IoCapable, + { + const { assert!(false, "Backend does not support infallible 8-bit = read") }; + unreachable!() + } + + /// Infallible 16-bit read with compile-time bounds check. + #[inline(always)] + fn read16(&self, _offset: usize) -> u16 + where + Self: IoCapable, + { + const { assert!(false, "Backend does not support infallible 16-bit= read") }; + unreachable!() + } + + /// Infallible 32-bit read with compile-time bounds check. + #[inline(always)] + fn read32(&self, _offset: usize) -> u32 + where + Self: IoCapable, + { + const { assert!(false, "Backend does not support infallible 32-bit= read") }; + unreachable!() + } + + /// Infallible 64-bit read with compile-time bounds check. + #[cfg(CONFIG_64BIT)] + #[inline(always)] + fn read64(&self, _offset: usize) -> u64 + where + Self: IoCapable, + { + const { assert!(false, "Backend does not support infallible 64-bit= read") }; + unreachable!() + } + + /// Infallible 8-bit write with compile-time bounds check. + #[inline(always)] + fn write8(&self, _value: u8, _offset: usize) + where + Self: IoCapable, + { + const { assert!(false, "Backend does not support infallible 8-bit = write") }; + unreachable!() + } + + /// Infallible 16-bit write with compile-time bounds check. + #[inline(always)] + fn write16(&self, _value: u16, _offset: usize) + where + Self: IoCapable, + { + const { assert!(false, "Backend does not support infallible 16-bit= write") }; + unreachable!() + } + + /// Infallible 32-bit write with compile-time bounds check. + #[inline(always)] + fn write32(&self, _value: u32, _offset: usize) + where + Self: IoCapable, + { + const { assert!(false, "Backend does not support infallible 32-bit= write") }; + unreachable!() + } + + /// Infallible 64-bit write with compile-time bounds check. + #[cfg(CONFIG_64BIT)] + #[inline(always)] + fn write64(&self, _value: u64, _offset: usize) + where + Self: IoCapable, + { + const { assert!(false, "Backend does not support infallible 64-bit= write") }; + unreachable!() + } +} + +// 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 + ); +} + +impl IoKnownSize for Mmio { + 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 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..bfb053059667 100644 --- a/samples/rust/rust_driver_pci.rs +++ b/samples/rust/rust_driver_pci.rs @@ -7,6 +7,10 @@ use kernel::{ device::Core, devres::Devres, + io::{ + Io, + IoKnownSize, // + }, pci, prelude::*, sync::aref::ARef, // --=20 2.51.0 From nobody Sun Feb 8 02:41:15 2026 Received: from SA9PR02CU001.outbound.protection.outlook.com (mail-southcentralusazon11013008.outbound.protection.outlook.com [40.93.196.8]) (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 339534963A3; Wed, 21 Jan 2026 14:24:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.93.196.8 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769005486; cv=fail; b=Ei+EWqFLOlRKM46XAOyhGXRpz10mod7S0+e6fQCpcYrKPTUggwm4CAywGAwJ8MUC2hWeup5HwGZeVjpvR0WmcdqcXedlt6rkqCwLDevXyxHk7bv8DRAKER8/M4dR24bLfDgO4JtG4O0Ivc6mVHotOOFoi46Z18TQLkCHprUCG+A= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769005486; c=relaxed/simple; bh=wPo7UbJ20GJM8UQfExioCiJyIk1r2OWGrEk/aOEUaC4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=RdG/DLNZPqpZpNcmsRjaSMlUm09zkjtfZMKOKkWMoTML20HTSmHkXicL9sI9oHllJeLGleu6ApwdGzf+KwdX7onnK7e3hlRxFk73lHDS9G4WINhma3QWQ1iPRKq41N8dFvGM38dm2zn1d2JWZxHISRkQjHkU0WhqucmANp12oyg= 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=cMCCVfg8; arc=fail smtp.client-ip=40.93.196.8 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="cMCCVfg8" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ThyJWNJZo3PeaM+d30uGUMvszW/Bof+yJek614eQ6av7G0UIgvOBaicJQ8Dh4ACFh/lAhez8LtSIa35DN3brNf3R1MD/QWpnLP0CA+BEPPFYVFX4x4pBGtGUNzZFEeL3+NQ29MTKI22Oe6SS9/ZLJl/EGKuqMRij+3NJZX2+I2v430bAHh7/oDWIyke2HGWP3BPW55lIUzJuYIofUolxG7l4/gKXGKn7FZbGXaLOSRM7nKphkEiJa0xqc9ONHr67lNYzawLCqqma5FOxkhxEP+OXTcfZoM7yNlWKRaL8savYLl0bHuJvYWzvOtXfbw8kxGsd4/VwJnKbQZCLvOQARA== 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=lai38gIl8gYykPx+C1o8FufpZkIQ/D4JfwW0fTgbeWM=; b=GCzGCf4yRFb+eEphP+jm9zkgLK2F0wq4ZC9De/tUyEcz+CDF1lsrRgRjhVOQo/bUqXUk0UCONk2tLflQ6JMh0r9iXX0rgU3E+oOqAhsKW/AigFdniBiJqwMpSdMCqusF2biwKq0XNzB/PwRxlhHx0vUWNV2ku/5eaqdBUwUF1sig3RtmsoAM9jQoMFSLISFkNyrMlXwR/+R52MoB7CSCWzoLYtMTZ5Qspdcyf7jjmbgenbECGUnH0fWsGkdxcCKwHnbL03oZVmXtGa41vLpBAF1589FFULQ3g8ujBav1TVBHnDKhtbxQqmwJicTDNQxm8yFtxjnAkjRnmuHzNr0p1w== 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=lai38gIl8gYykPx+C1o8FufpZkIQ/D4JfwW0fTgbeWM=; b=cMCCVfg8sUOpRx3bysR3osG5sFSvP45EpKeH3B0mPgYEBJ38ws11NLerGZWVdGaIOZ9kRtjyLHsPhPwFDcbVM15GWe5Z2Uzb1k2E+LQhl3HEHbqSqNn0pP54glu+/6B3CV27bZ4FLZXB9wjqdbMGWw1cWilbXk3ER4Y3QCk0KmW0RFyn0cW7OcZO6eAsZcb8fc089ND13lVr3cVkNGhDVPhvUFePbmeAkbkFq8nha4D9mEelfuvmENN2EOp4yUseuUt+/wLwelKkquBiCjM6nl3fzgMa78jUpdnBORyVu6Zbtbpl8OsXZ48BRPOdGGpFHePpThSLtbkHXcEMn+/v1A== Received: from BY5PR16CA0012.namprd16.prod.outlook.com (2603:10b6:a03:1a0::25) by SA0PR12MB4495.namprd12.prod.outlook.com (2603:10b6:806:70::13) 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 14:24:36 +0000 Received: from SJ5PEPF000001E8.namprd05.prod.outlook.com (2603:10b6:a03:1a0:cafe::49) by BY5PR16CA0012.outlook.office365.com (2603:10b6:a03:1a0::25) 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 14:24:36 +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 SJ5PEPF000001E8.mail.protection.outlook.com (10.167.242.196) 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 14:24:36 +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 06:24:23 -0800 Received: from drhqmail201.nvidia.com (10.126.190.180) 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 06:24:23 -0800 Received: from inno-vm-xubuntu (10.127.8.11) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Wed, 21 Jan 2026 06:24:17 -0800 From: Zhi Wang To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , Zhi Wang Subject: [PATCH v11 3/5] rust: io: factor out MMIO read/write macros Date: Wed, 21 Jan 2026 16:23:52 +0200 Message-ID: <20260121142355.4761-4-zhiw@nvidia.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260121142355.4761-1-zhiw@nvidia.com> References: <20260121142355.4761-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: SJ5PEPF000001E8:EE_|SA0PR12MB4495:EE_ X-MS-Office365-Filtering-Correlation-Id: acd5264f-08a7-4d50-5164-08de58f8ce1a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|1800799024|376014|7416014|82310400026; X-Microsoft-Antispam-Message-Info: =?utf-8?B?ZFZxQVUwa3hhaENNN0Nvc2FRNGpjdnpHQXdUNE5lTnBiMzlJeWlwMjkrSWtE?= =?utf-8?B?b09RSDBKbTVYanFXSjZHcllFcysrWHc4STRoRGMyMG9jRWk2U21kaXI5SENV?= =?utf-8?B?UXdtSC9OYkIyekRwU05ySDBySGc1NHptTWpwcVZFdVlXMGV3bldGbUtTcG11?= =?utf-8?B?TmRpazg0L25iMDB4UUFjNmxlNzJsZmN1d0ltNXcxZDZRaGUvdkRRVkdPRHhV?= =?utf-8?B?WllwVWxoSW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SWNhaVE4OUZocjFK?= =?utf-8?B?bXJzNi8xZGhaVlBlUGJZUFJkcmJxcEVsdmJvYlZVb0xXZ1BzdnZzbm5lanRn?= =?utf-8?B?U3FRVUtHRGt3clV1TDRHWjkyOTFVak9uSzJaTGt5TmlWdGhqVDRxRnVYQkhP?= =?utf-8?B?N2lBaDRkQ09UMGhaa0RRMzAza3VOWVh5N2k0RkkvZXovaVh6aUJSR3lySVNG?= =?utf-8?B?Wkx5VUpGYzlCM0ZBS0ZTUC9zS0t3PT0=?= 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)(376014)(7416014)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Jan 2026 14:24:36.5934 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: acd5264f-08a7-4d50-5164-08de58f8ce1a 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: SJ5PEPF000001E8.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4495 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 --- 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 152afdcbaf78..d62fd6714256 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) } }; } @@ -468,46 +525,46 @@ 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 impl IoKnownSize for Mmio { - 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 @@ -523,43 +580,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 02:41:15 2026 Received: from DM5PR21CU001.outbound.protection.outlook.com (mail-centralusazon11011002.outbound.protection.outlook.com [52.101.62.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 DC1FE4963A7; Wed, 21 Jan 2026 14:24:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.62.2 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769005487; cv=fail; b=i682D7LrtWbBd5ndXRISf9cTnlcf2Jda8dG+EGPMLkhft171UfpjKcLT6FhiSzuvS7SLyzdMlz7MGV/AwUtQApYww02Bi5Q2wfSJ+WmhoKN6e+4QxqI/fQPjBu1/v34qZ0MkD7qRrh17790Bm8T9UjZmLuucR45oitxtenuFoq0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769005487; c=relaxed/simple; bh=aioJkGmuKXz8SSFROhqkedNPj23M35rnthBfMlyKzII=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=dHnaR6ytSQB9Mp98RR2MQQnwIPidPnLAWLWmvKfnAbtu2c3bex8rbW7E+TQm1bectyVjEjPw1QARjliRgq1ze8MTPVN1hqml54DfQ8nLzKeqlREQvhI2IhuTjWkoyg2QV4JPA8DOKmlVFyWdAi1PnVTEhZOOLhFvDXqsPOqNtZo= 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=IMojhSJV; arc=fail smtp.client-ip=52.101.62.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="IMojhSJV" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=NtukYKydGZAmburm4qLcbc4AimZuMDmLi15xP3o9PJKYlkSeqljuJ7fQvqgTIdkwTdR3vgjbCI46HFzKRleZkqi4p6W07x1MOq2lzKaaeTxWIOd/dIcytXGToNJfiWMWAV0eYfTQ4Pq9T5vWo2SReeU/kCgKgmLtEQcLWLhR39EQz8hgbjGkkDRyB2C5puQeZYwsfWs8g1Bkn699IBKGWc+TtzlJ2JCJy9m4Vz3tnRR6ZH0yXW6SK5kBVT8MvNCeLJ63iRJVQUT46Qw9AAjR5wxgucNcVgT7OoXVxMsb8SkFYnuEkOI7SN5vP6WFsCXwgm0lYsj5g5sUqmCoGlY6eg== 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=USEyD1NagRi4PjjYms8GmHFPMyMfE9N5rrEp7v4/2t4=; b=r3qFNT0t2DqhjZJ65TMDS/Rb2Ed/3+lSJJ2XkZWcSjKHSAVuLnobHHMT9zT7xQN5vTXYG9uzzJch21UvNto4dBjTOD74Gu0FzksotOGRH23UqkOBBG2qJK5njOY7qnBFWk8q0AOvriDtdV6T+4/qnHK2k7dN8mCQ5/E53Niza9w5dX05Lxs3ymy03u0NEiRLm5InGxCEBdRyxmV/3NjVq6FsQGl4rgQ8tmaghaOns1TxF0Sik8P1TUgdczZHaPnzb0wfwlBVvIx2zvg1RwMTNXdq0p7PCubNsj08tmX//+lhPGGDfuWcj8pnde+FcGAy7TMGSp7FosgrjgbDxopIaQ== 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=USEyD1NagRi4PjjYms8GmHFPMyMfE9N5rrEp7v4/2t4=; b=IMojhSJVMJ57N7eNXy9TVWdbp8DqUC1SLywZNjOosOTgQeElKHDBFWWkjySibSDsPjpR+81VzeC6Asj24AqdsVMWr+nVqoB//Bo+UPE7NUva79w7NajL1ThSke1e5HXTnF9QN9PoO+aRkwbdF1A+6cza5NJG8FQPgqSAhzz/qmHngOc96Ezt36Y6p2wVlLZhjLvQ65AXGYRKl2FF4zco7nunpMfhcnLnQ2jTgufQfaF7LOe9hDUr9LTnTrbvBfUsmgJh1x15FfoIfifvtG7Z6buXOWyjOORj0JtY3snwNh/GxbvrfbJf8TfjCKkb+cA7Dz0x+fC2Dg0SxF6oWz2u5g== Received: from BY5PR16CA0023.namprd16.prod.outlook.com (2603:10b6:a03:1a0::36) by MN2PR12MB4488.namprd12.prod.outlook.com (2603:10b6:208:24e::19) 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 14:24:40 +0000 Received: from SJ5PEPF000001E8.namprd05.prod.outlook.com (2603:10b6:a03:1a0:cafe::92) by BY5PR16CA0023.outlook.office365.com (2603:10b6:a03:1a0::36) 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 14:24:39 +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 SJ5PEPF000001E8.mail.protection.outlook.com (10.167.242.196) 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 14:24:40 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) 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 06:24:30 -0800 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail201.nvidia.com (10.126.190.180) 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 06:24:29 -0800 Received: from inno-vm-xubuntu (10.127.8.11) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Wed, 21 Jan 2026 06:24:23 -0800 From: Zhi Wang To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , Zhi Wang Subject: [PATCH v11 4/5] rust: pci: add config space read/write support Date: Wed, 21 Jan 2026 16:23:53 +0200 Message-ID: <20260121142355.4761-5-zhiw@nvidia.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260121142355.4761-1-zhiw@nvidia.com> References: <20260121142355.4761-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: SJ5PEPF000001E8:EE_|MN2PR12MB4488:EE_ X-MS-Office365-Filtering-Correlation-Id: c489b5ab-6642-4940-6f19-08de58f8d081 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|7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a0VmSmgvblBtTDl5V0U2?= =?utf-8?B?V1VITjBQZHhLTStINVZJUHNEVW52QWtOT2E1VXB5dWdtYjdxRWRPU3hLNFdV?= =?utf-8?B?ZjlmMlVhMVpZM3hzZWxxdWNhM1pUQVBqWFdHZUd2TExlc3dNUDczQnozYTVz?= =?utf-8?B?VWtFY25xUWlBQ01hL3hhcnZpeU5rWFA1SFFWNnN3MTlCQXhlUG9UaEtwbWho?= =?utf-8?B?ekFuMjdMWTlVZ2ZqNDNnTEg1aFJjLzd3MjNrcjBCVkRwYXVQNm1hTWdwMVpu?= =?utf-8?B?SjRDeHBhdGhtaVhRbUYveDkwSjBDWkh1ZnZlNWJ3djl1MnZMSHZ3b3ZhZzk3?= =?utf-8?B?RFpyaHBJWFJyT2Vndm52a01US0V5NWhWQmlXaElMUVA1cVZjTHgwY1FSY1NE?= =?utf-8?B?RGJ5cG13cDFmS3dCbVA5VnQxcmppRWxLTHFZcnJYT2Q4N2krU3dZL0kxOFoy?= =?utf-8?B?RE01VStuMzRPdDBFd0l1VGVCSTd6KzZzcGNiSFh1Z3dCckhCNmplL3JYLzVz?= =?utf-8?B?V3dwVG0ySHpFWmlTeUFKNDhGcXlRPT0=?= 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)(82310400026)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Jan 2026 14:24:40.6334 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c489b5ab-6642-4940-6f19-08de58f8d081 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: SJ5PEPF000001E8.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4488 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. Cc: Alexandre Courbot Cc: Danilo Krummrich Cc: Joel Fernandes Signed-off-by: Zhi Wang --- rust/kernel/pci/io.rs | 161 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) diff --git a/rust/kernel/pci/io.rs b/rust/kernel/pci/io.rs index e3377397666e..e9c540fe80ac 100644 --- a/rust/kernel/pci/io.rs +++ b/rust/kernel/pci/io.rs @@ -8,6 +8,11 @@ device, devres::Devres, io::{ + define_read, + define_write, + Io, + IoCapable, + IoKnownSize, Mmio, MmioRaw, // }, @@ -16,6 +21,134 @@ }; use core::ops::Deref; =20 +/// Represents the size of a PCI configuration space. +/// +/// PCI devices can have either a *normal* (legacy) configuration space of= 256 bytes, +/// or an *extended* configuration space of 4096 bytes as defined in the P= CI Express +/// specification. +#[repr(usize)] +pub(super) enum ConfigSpaceSize { + /// 256-byte legacy PCI configuration space. + Normal =3D 256, + + /// 4096-byte PCIe extended configuration space. + Extended =3D 4096, +} + +impl ConfigSpaceSize { + /// Get the raw value of this enum. + #[inline(always)] + pub(super) const fn as_raw(self) -> usize { + // CAST: PCI configuration space size is at most 4096 bytes, so th= e value always fits + // within `usize` without truncation or sign change. + self as usize + } +} + +/// 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 const parameter `SIZE` can be used to indicate the +/// maximum size of the configuration space (e.g. `ConfigSpaceSize::Normal` +/// or `ConfigSpaceSize::Extended`). +pub struct ConfigSpace<'a, const SIZE: usize =3D { ConfigSpaceSize::Extend= ed as usize }> { + pub(crate) pdev: &'a Device, +} + +/// 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, const SIZE: usize> IoCapable for ConfigSpace<'a, SIZE> {} +impl<'a, const SIZE: usize> IoCapable for ConfigSpace<'a, SIZE> {} +impl<'a, const SIZE: usize> IoCapable for ConfigSpace<'a, SIZE> {} + +impl<'a, const SIZE: usize> Io for ConfigSpace<'a, SIZE> { + const MIN_SIZE: usize =3D 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 as usize) + } + + // PCI configuration space does not support fallible operations. + // The default implementations from the Io trait are not used. +} + +/// Implement IoKnownSize for ConfigSpace with compile-time size. +impl<'a, const SIZE: usize> IoKnownSize for ConfigSpace<'a, SIZE> { + 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); +} + /// A PCI BAR to perform I/O-Operations on. /// /// I/O backend assumes that the device is little-endian and will automati= cally @@ -144,4 +277,32 @@ pub fn iomap_region<'a>( ) -> impl PinInit, Error> + 'a { self.iomap_region_sized::<0>(bar, name) } + + /// Returns the size of configuration space. + 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 =3D> Ok(ConfigSpaceSize::Normal), + 4096 =3D> Ok(ConfigSpaceSize::Extended), + _ =3D> { + debug_assert!(false); + Err(EINVAL) + } + } + } + + /// Return an initialized config space object. + pub fn config_space<'a>( + &'a self, + ) -> Result> { + Ok(ConfigSpace { pdev: self }) + } + + /// Return an initialized config space object. + pub fn config_space_extended<'a>( + &'a self, + ) -> Result> { + Ok(ConfigSpace { pdev: self }) + } } --=20 2.51.0 From nobody Sun Feb 8 02:41:15 2026 Received: from DM5PR21CU001.outbound.protection.outlook.com (mail-centralusazon11011002.outbound.protection.outlook.com [52.101.62.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 61B34217F33; Wed, 21 Jan 2026 14:25:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.62.2 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769005505; cv=fail; b=QClawXA8d9pyQ1kMWkIXrnlTNMhwedvlxbxrJ22KXN1kt+rFgF/RrNlqgBciB0aVl3Q6yFAVRIz7QgfQRnUhboYzYHdnwpT6Xf7iVs45s63nhUZwg+W5ZlDhee3kj4UAHJ5VjMfG7Qr7KnMdq0vZd3zBGmPxzLDyuOa1T3ht4j0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769005505; c=relaxed/simple; bh=pNOxSD6yFd8ZnohsQf6idEl7i+DFCFKFyff8HtincuI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JNqvnHc2lGV9W6O/H+Z60kfq6OaiKE5fZefU+6kr0hbUT6uR38orPoDBq7q9AW/qxm345ZOzGCh3aq0ODxQjvoi9dgvVkA03UioBxYamq4lDg7XxCSgGWX/E4lh9gPNuzAGs4+eSNRoluH1LeS49HGFyi8OBrJ/dNgtq7+xERWs= 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=kpMn4256; arc=fail smtp.client-ip=52.101.62.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="kpMn4256" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=mprh8Qo93PtMXO4+U5byVcJ28QCk/ZaD0WRMZCCWxISM4t1eqnSi7TQpm1H3P4LvADREvl9EapeCI5mBZAge9ky32j1+LtTfJ8rwThKFn0zDLzhdYVxnxpSsUC+QqdhpNdJXftx8u7N1SPOaklR+hPeaM2eZxPUtXvHA/B/owH+CdPqemyi6VVAkcaav2y2IWLjZDjrmPGW3l9rvZKG3RbCQ3ncVCkrtFNgKI7PaesKdimts6SR6TAuL5aavFw/Di2dhf79f24QPPfL54p0E99GMAPJrw75dKYLenZ+glw8tc36uWmhoNFYTpGSFsvfzWXhQjsI8Z3K0xkn3uT1zRw== 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=aOWCeEUJKJc8omTvBPbqZ8eMOnLAHleQc1B8pXqNZq4=; b=UxC3DcacdZHWD0FWg6sJ3XOZxQtHJW6XjGGhTTEvIQuK2oe1bCycxC9IonISoLccmRcbdQanYpQ6W98JK7ZAjT1S4bJxl391dD3ux4S5H+91KKYRaVNP63QTniPQIKoUuyFIcit0ORiZQtmtd3y354NCPDGN5r8N/+OnS/Ba3Ie56QLtGz8Hh4Y3NLDHFytkOSabrc9w8JGHpeT936hFEUaHV4RTuAKt6od0BywFVNC7holg6SOTPCiTOmPzGXURb+Z6mkrZ8oyG9tW0+RfxUSkh9YuAgpdk7gCX4qkTJcHEJd0bo+P1f/PyWMiaqx7cEscoFuZU6U1Vecl4Kd3W4g== 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=aOWCeEUJKJc8omTvBPbqZ8eMOnLAHleQc1B8pXqNZq4=; b=kpMn4256SZ1yhWwLcfGkw3S/5yokoOXw63rc7g1DWlRD9YikKEe81FlVp7Ox2fc73GTg5/IYIQIqgmT+7MZzORSA2OeS6Gbjsvx0TK1rr1fd2gJjQful8lBGXTysVmKiGEmnEyiaUIMrd24JK+ZPSUzBQ2EpjFvA3JBAznpvwb2FztjfuEGW/A+drT9h/rmgvIlmlKhvtPipd5RndfaaCrKWxlJMSw1Qdp7PlXbtbjhUcBGUJ0Bcw0MEy3bsKLJVo8RYoP2BssGt7YRrnpRDRGJHzVQsipS6HJV9fifvVBMXrULCoFijIul529AUqTIdkY1LRwwctaTs7ttJjtN5Pg== Received: from BY5PR04CA0005.namprd04.prod.outlook.com (2603:10b6:a03:1d0::15) by SA3PR12MB7808.namprd12.prod.outlook.com (2603:10b6:806:31b::18) 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 14:24:59 +0000 Received: from CO1PEPF000075F2.namprd03.prod.outlook.com (2603:10b6:a03:1d0:cafe::31) by BY5PR04CA0005.outlook.office365.com (2603:10b6:a03:1d0::15) 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 14:24:52 +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 CO1PEPF000075F2.mail.protection.outlook.com (10.167.249.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 14:24:59 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) 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 06:24:37 -0800 Received: from drhqmail201.nvidia.com (10.126.190.180) 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 06:24:36 -0800 Received: from inno-vm-xubuntu (10.127.8.11) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Wed, 21 Jan 2026 06:24:30 -0800 From: Zhi Wang To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , Zhi Wang Subject: [PATCH v11 5/5] sample: rust: pci: add tests for config space routines Date: Wed, 21 Jan 2026 16:23:54 +0200 Message-ID: <20260121142355.4761-6-zhiw@nvidia.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260121142355.4761-1-zhiw@nvidia.com> References: <20260121142355.4761-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: CO1PEPF000075F2:EE_|SA3PR12MB7808:EE_ X-MS-Office365-Filtering-Correlation-Id: b0514011-da9f-476b-5933-08de58f8db99 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|36860700013|82310400026|376014|7416014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?ONZ3GaIVQKeWXeu5Cx5uzlL4UMer7KLLBUBOSLsDroLe6/XF6O++tGm4qp8M?= =?us-ascii?Q?a+LZHLLFl0c8t7t6v0Uaf9ccm23m7WuvFEzd8KEkV+CgddYIz7s75GaMGKjS?= =?us-ascii?Q?n0XmqjWEZWEqtAS5PrQaT2fY4/kOQzhRaknZCVPtf0Sy+qrbIKZA0tzeXAgZ?= =?us-ascii?Q?WDdJvj1BHRaFjjBitkH3aRkWjmfiIGyGEJy79RHmUuhWLqGYUDTiWlW+LvOA?= =?us-ascii?Q?WlRF7et9Q1ppG+giHxBBEaqDhyZxrnJ+8eCcKjNO6ZcVUyt8UFzBEgQdYat2?= =?us-ascii?Q?JvXwjPQIW+UkTHM+gpn4fmlkExUrGammzHnYo3KnJa88gjVClH6TuU/mUjFf?= =?us-ascii?Q?zUovo1AsNRVeyT18nN82i9p/ydhhKUX7vYdcm5JIlOOkuDxZccpfErBqqFmF?=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hf0x7NV3RpH9QElc7befbix66qJsSoeqQa/r0hvypRodfUVVO?= =?us-ascii?Q?gBXOo0ENT/SG/J7f/aCkqDjS6uCTMBzjCGLhWwuF3sGyO9aHFkgB1KVSEQWZ?= =?us-ascii?Q?14Oqpr8zD3o1gO+dNGXSspS4N4gpTFIlfylyOLlB3LBzap5BBbZNsCg9Gdvc?= =?us-ascii?Q?iIBdzjghQb8nYeVTwtu+LPVyR3d9Xk1vLDlUlWdSD03yPD0AIfYjExCXcZJR?= =?us-ascii?Q?e9XUSL0/uwV4jlcdZBr8vYbboOneeW8hKh6lnCr2VZAdJwcyW830nMYW04Xf?= =?us-ascii?Q?v1qrqDBqRZuzANNoSjPWbdJ4q/tpVajzKj6z4CeadDs8YDD5IY5gCOE212yn?= =?us-ascii?Q?PGPrpU0EHzKeoTF7Z9Ug09MDPs894LRBxeOpfGU22F+OUNhT4m0BSEMBhPL8?= =?us-ascii?Q?3e0nNQ=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)(1800799024)(36860700013)(82310400026)(376014)(7416014);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Jan 2026 14:24:59.1713 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b0514011-da9f-476b-5933-08de58f8db99 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: CO1PEPF000075F2.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA3PR12MB7808 Content-Type: text/plain; charset="utf-8" Add tests exercising the PCI configuration space helpers. Suggested-by: Danilo Krummrich Signed-off-by: Zhi Wang --- 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 bfb053059667..26f0887a74e8 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::{ @@ -68,6 +69,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 { @@ -99,6 +126,7 @@ fn probe(pdev: &pci::Device, info: &Self::IdInfo) = -> impl PinInit