From nobody Thu Apr 2 22:05:40 2026 Received: from BN8PR05CU002.outbound.protection.outlook.com (mail-eastus2azon11011059.outbound.protection.outlook.com [52.101.57.59]) (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 280AC2517A5; Sat, 14 Mar 2026 01:06:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.57.59 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773450417; cv=fail; b=dVhA32GBqgFu3eaL1u4V86lhFIFI1DCrMyvSCYeLYj2HbdMA/P1PgamiGpFd5oN7aergh7cW2Qfak9W56bQRqwacoElfSd8OxvYNWdgSeswFH3ZXkhpMvQcKk/R+wAL1XmmZGjdtWCi2NOFhtR1VnrBk1xFAh4iSbxICcs6TC+g= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773450417; c=relaxed/simple; bh=8se6tM1SnAQB4KBKef+i0ndmBW3rc5y+8v/P03v7LLM=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=XEaDjWQUzDHg9dbYinX42EojeypRVIGsbcNRR0IMRcezLzLnmFat1ZJx6iFRi2rXgUX1TS9N4oqdplzT8G+Wx7D575SzRYX9QbuMcZrD0mlCgclodScDBqnQIBJGvALzkMQHuNlX/l7PKrOlx3jgendcqivGwMlHdxIqdzeQ7MA= 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=L+fvQPrA; arc=fail smtp.client-ip=52.101.57.59 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="L+fvQPrA" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=r51qDny8Lz0vtE9yKW/3bzqcP2FigS8+2gIq73Bxh2ZH516IU2Pt7LBm/3Nbss58KcWTxlyRm58mLKjq+UWjFd2iJQ913zPbYOyaw3Wp2OJXYyh1wnuNxrQghvG8UPypamtp0TGSIadiEPJ47BkUkM9MgUjOjUW5SjjxzjCBGxbTjCLtYx6/pcMnbWNBIAv1VESnaI4kZ+hUA88NHWR9inA7bqkzSI1zY1ux6EETYkiwvAxrT4EhyjsMTUKNXrsXWlUcsFu3TyhWKcqD48nQ9yG4NhmsLvaFy7b+GVW32QvLNd9r66dNXXZIjJ1hZ8BDupdsZSiu5YbOTGMkpkKRSA== 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=plts2w1j13B/V544+z+lhv+EUY6uBSOLs+JhOcBxnMM=; b=DRxhphWkaconNh27Vuie7CNnKUkWy8t4JwnU7FZ3va/VnEaqy9dmde4SegDIKvPB3t+fS1nWz9SBpQYNxo88zZrHD9rTq9oxSe3ayY1xSfQ0eFlxhBgqWSaODS0aIIrvhDVUd8r4QqT3tp4N9hULk0VlKsnbsoLzdH5JItR1pTVs+O4MiRU/R6cRJO3+m6nf39acXm2HSAyqeug99CmNxadcyw2/SJ0sUBijyR3w8IOMNFymKzL/9xGnCqsM2LIXvQKI6SOnW9JhgqRYir0Ap5E9zwzqr6AAHQcrDvpbCqATpng6QQDGXs4fu5ribHf62VG/jq+UF+MFf9KJgh0aOw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=plts2w1j13B/V544+z+lhv+EUY6uBSOLs+JhOcBxnMM=; b=L+fvQPrA5tmXo90HCzKz1CFwjMk+jedkPu8IbxCk0XIWNr7p5Vok4mzRLoATLTHlanZZSKcbnObTdIWwiKT7j8KgK0ZmP/tw/IAnZOy9wA4O/b+brbURg/KihBFPtT2sGsFtoFSDzWtUe1Ip8hcx3BRhPNd9wxFnj8RmR69puB3M7xKL7CFs1xhIRSpgqqfagh3iMBubg9ySNwESGpGyFJrSXN1mYIXmB1mSnVMFz1V+pNZS9IpAtMmoKkopC7l2nQ+4/68Pn81npBL4tfNDChjqJ+qzEAbp/+HjN4xvfwS9NvPdAnf69NLXFvWbhlzwQ8txnyriHGvYqEkP5IOqag== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH2PR12MB3990.namprd12.prod.outlook.com (2603:10b6:610:28::18) by BY5PR12MB4033.namprd12.prod.outlook.com (2603:10b6:a03:213::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9723.6; Sat, 14 Mar 2026 01:06:51 +0000 Received: from CH2PR12MB3990.namprd12.prod.outlook.com ([fe80::7de1:4fe5:8ead:5989]) by CH2PR12MB3990.namprd12.prod.outlook.com ([fe80::7de1:4fe5:8ead:5989%6]) with mapi id 15.20.9723.000; Sat, 14 Mar 2026 01:06:51 +0000 From: Alexandre Courbot Date: Sat, 14 Mar 2026 10:06:16 +0900 Subject: [PATCH v9 06/10] rust: io: use generic read/write accessors for primitive accesses Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260314-register-v9-6-86805b2f7e9d@nvidia.com> References: <20260314-register-v9-0-86805b2f7e9d@nvidia.com> In-Reply-To: <20260314-register-v9-0-86805b2f7e9d@nvidia.com> To: Danilo Krummrich , Alice Ryhl , Daniel Almeida , Miguel Ojeda , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross , Boqun Feng Cc: Yury Norov , John Hubbard , Alistair Popple , Joel Fernandes , Timur Tabi , Edwin Peer , Eliot Courtney , Dirk Behme , Steven Price , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Courbot X-Mailer: b4 0.14.3 X-ClientProxiedBy: TY4P301CA0071.JPNP301.PROD.OUTLOOK.COM (2603:1096:405:36f::10) To CH2PR12MB3990.namprd12.prod.outlook.com (2603:10b6:610:28::18) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR12MB3990:EE_|BY5PR12MB4033:EE_ X-MS-Office365-Filtering-Correlation-Id: 96e362c3-83bd-4f90-1b0c-08de8165f9b1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|376014|7416014|10070799003|921020|22082099003|56012099003|18002099003; X-Microsoft-Antispam-Message-Info: 2ag4lAl9I4fCgka5M7o5eHR2lu8pO5MB7qDt+NByd+p7XsPB5N4uflA/u9jGmpNtVde2mrMOvM+cmvj0ezEnbJWIy0HmBny/Xi4sHS3LmESpbANedqoKp2g/h1wXK+nqqz/N69RWcll00V7sfJQwVX/PF2vUZoeBFKJ9qwxIhTvp0EaOV9TRPU0F3z5ZkiW7Lo8vcJqpuBmMRBTjrygF4poZgTwfxsKcNWUGZpaDg4/zT0UvBDIsx3RV0nTk71TlwY0grf5xHCffjyy8wmzp2AS39PO8KMhf9qRNY79IvbW/lcAhMuzwZH42C9Lt8fbk+DzBoaESSV/or80AxyEwlNLCUFIx++9QG6v208mdOUHV5M0mAMZeJ2oocfMG0KUNUpfsKtabJv6lpVGaGqSBo2G/Y1RMCEQcp1L7XzZCJ0RZD3NybTEokv0A2yl3qC658mbVF4EkowavHlb6tC73Ntt/+iaz4CrnqK47SIKJ14HwIqKlmJGgRGyzKAl2V7O94/E/mO1D85ynSVh/r2ZkUczRNo6El5zBSVm/C15AJpUpp3f+AJiG78+Cc2fRMs0vJ9+h9Qo2t/Ngs0PlyuMwi3StKbB/7MQY7mOsIAA6Mg3voHOuWcRB269WknurxL90HStZh//6pEqgCsIM0f8rzPaU4DXW3fI5g6t/6z/aZKgSXh6k032gi20O/q6twBdpv80rIKTE1VAVjuq7ppXe8H9P61EYCH7aubuDXqQEuRuu4lj7f1+eWWerX69FhgshRfiafMWZ4zSVVa1A6MGBmA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR12MB3990.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(376014)(7416014)(10070799003)(921020)(22082099003)(56012099003)(18002099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 2 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?bFA5Z0dXVzltbDVHNURCcWY0VkI4YW5qenAxOVJkWEd0VkRyc1E3U1NJeWRk?= =?utf-8?B?Zk9oZ2p3d0pUOU9EQnkwTlliQWljSk1DR1F6MVlvN2RaZ3pub04vbUlKeVls?= =?utf-8?B?OVlPbUdZOGNrMFpSYTNzb0NtOXdybWx5eStKOXdPMGVVd0MvUGFKY2RmUXRB?= =?utf-8?B?TjJTbElta2N4dVk1ZUc2VWJqZ1YrNkFtUU9uSXFoaDRiK1d6MlRabUp4V29S?= =?utf-8?B?Z2l0WWFoclMxRzNnMG5VemgxcHdmRGROR3gycFJ2anRKT2luS1NrVnRuMXV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VVhMGkwcHJCOGgrNjdpZWd0elk5TG82TTRO?= =?utf-8?B?Y0FVbHFCd3JCZ2l3bFpBUHZEOHFvKzNLM1YvVkhyaG1VZVpjbTlKTW9maFlC?= =?utf-8?B?KzVUVUNvc3AxNFlhMXRWUHNLNHJFSUx4eENEWUhZRFFoV05YYTRBSHY3SUlu?= =?utf-8?B?dTBCQTJndXZwMkQwSG9ZeVBnOFdZNENHWm9QQXFRTi9iMXhiUk1RZVJsY0NR?= =?utf-8?B?YUpTMVl3eVdmWlZoaXdHS2oxZkdiK09uY2F6MlFJWDAyemRkRUp5ZVg5Q3Fr?= =?utf-8?Q?PxNViNL4SE1peyZoLr+GDxtUAJWbvYKjVslxCKdyDEa+r?= X-MS-Exchange-AntiSpam-MessageData-1: Hx9E79Et+zQW9g== X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 96e362c3-83bd-4f90-1b0c-08de8165f9b1 X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB3990.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Mar 2026 01:06:51.7231 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 1CjHsWLaeKReVANOml1HAtHNTkPPLy55Q+NUjR9TAnxTEw7PeiJAohU8MHq7I0rXL4X6ufAm+hja8MS6n7H7Gg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4033 By providing the required `IoLoc` implementations on `usize`, we can leverage the generic accessors and reduce the number of unsafe blocks in the module. This also allows us to directly call the generic `read/write/update` methods with primitive types, so add examples illustrating this. Signed-off-by: Alexandre Courbot Reviewed-by: Gary Guo --- rust/kernel/io.rs | 218 +++++++++++++++++++++++++++++++++++++-------------= ---- 1 file changed, 150 insertions(+), 68 deletions(-) diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs index e7e32f921efb..c9d43b1372ab 100644 --- a/rust/kernel/io.rs +++ b/rust/kernel/io.rs @@ -197,6 +197,26 @@ pub trait IoLoc { fn offset(self) -> usize; } =20 +/// Implements [`IoLoc<$ty>`] for [`usize`], allowing to use `usize` as a = parameter of +/// [`Io::read`] and [`Io::write`]. +macro_rules! impl_usize_ioloc { + ($($ty:ty),*) =3D> { + $( + impl IoLoc<$ty> for usize { + type IoType =3D $ty; + + #[inline(always)] + fn offset(self) -> usize { + self + } + } + )* + } +} + +// Provide the ability to read any primitive type from a [`usize`]. +impl_usize_ioloc!(u8, u16, u32, u64); + /// Types implementing this trait (e.g. MMIO BARs or PCI config regions) /// can perform I/O operations on regions of memory. /// @@ -241,10 +261,7 @@ fn try_read8(&self, offset: usize) -> Result where Self: IoCapable, { - let address =3D self.io_addr::(offset)?; - - // SAFETY: `address` has been validated by `io_addr`. - Ok(unsafe { self.io_read(address) }) + self.try_read(offset) } =20 /// Fallible 16-bit read with runtime bounds check. @@ -253,10 +270,7 @@ fn try_read16(&self, offset: usize) -> Result where Self: IoCapable, { - let address =3D self.io_addr::(offset)?; - - // SAFETY: `address` has been validated by `io_addr`. - Ok(unsafe { self.io_read(address) }) + self.try_read(offset) } =20 /// Fallible 32-bit read with runtime bounds check. @@ -265,10 +279,7 @@ fn try_read32(&self, offset: usize) -> Result where Self: IoCapable, { - let address =3D self.io_addr::(offset)?; - - // SAFETY: `address` has been validated by `io_addr`. - Ok(unsafe { self.io_read(address) }) + self.try_read(offset) } =20 /// Fallible 64-bit read with runtime bounds check. @@ -277,10 +288,7 @@ fn try_read64(&self, offset: usize) -> Result where Self: IoCapable, { - let address =3D self.io_addr::(offset)?; - - // SAFETY: `address` has been validated by `io_addr`. - Ok(unsafe { self.io_read(address) }) + self.try_read(offset) } =20 /// Fallible 8-bit write with runtime bounds check. @@ -289,11 +297,7 @@ fn try_write8(&self, value: u8, offset: usize) -> Resu= lt where Self: IoCapable, { - let address =3D self.io_addr::(offset)?; - - // SAFETY: `address` has been validated by `io_addr`. - unsafe { self.io_write(value, address) }; - Ok(()) + self.try_write(offset, value) } =20 /// Fallible 16-bit write with runtime bounds check. @@ -302,11 +306,7 @@ fn try_write16(&self, value: u16, offset: usize) -> Re= sult where Self: IoCapable, { - let address =3D self.io_addr::(offset)?; - - // SAFETY: `address` has been validated by `io_addr`. - unsafe { self.io_write(value, address) }; - Ok(()) + self.try_write(offset, value) } =20 /// Fallible 32-bit write with runtime bounds check. @@ -315,11 +315,7 @@ fn try_write32(&self, value: u32, offset: usize) -> Re= sult where Self: IoCapable, { - let address =3D self.io_addr::(offset)?; - - // SAFETY: `address` has been validated by `io_addr`. - unsafe { self.io_write(value, address) }; - Ok(()) + self.try_write(offset, value) } =20 /// Fallible 64-bit write with runtime bounds check. @@ -328,11 +324,7 @@ fn try_write64(&self, value: u64, offset: usize) -> Re= sult where Self: IoCapable, { - let address =3D self.io_addr::(offset)?; - - // SAFETY: `address` has been validated by `io_addr`. - unsafe { self.io_write(value, address) }; - Ok(()) + self.try_write(offset, value) } =20 /// Infallible 8-bit read with compile-time bounds check. @@ -341,10 +333,7 @@ fn read8(&self, offset: usize) -> u8 where Self: IoKnownSize + IoCapable, { - let address =3D self.io_addr_assert::(offset); - - // SAFETY: `address` has been validated by `io_addr_assert`. - unsafe { self.io_read(address) } + self.read(offset) } =20 /// Infallible 16-bit read with compile-time bounds check. @@ -353,10 +342,7 @@ fn read16(&self, offset: usize) -> u16 where Self: IoKnownSize + IoCapable, { - let address =3D self.io_addr_assert::(offset); - - // SAFETY: `address` has been validated by `io_addr_assert`. - unsafe { self.io_read(address) } + self.read(offset) } =20 /// Infallible 32-bit read with compile-time bounds check. @@ -365,10 +351,7 @@ fn read32(&self, offset: usize) -> u32 where Self: IoKnownSize + IoCapable, { - let address =3D self.io_addr_assert::(offset); - - // SAFETY: `address` has been validated by `io_addr_assert`. - unsafe { self.io_read(address) } + self.read(offset) } =20 /// Infallible 64-bit read with compile-time bounds check. @@ -377,10 +360,7 @@ fn read64(&self, offset: usize) -> u64 where Self: IoKnownSize + IoCapable, { - let address =3D self.io_addr_assert::(offset); - - // SAFETY: `address` has been validated by `io_addr_assert`. - unsafe { self.io_read(address) } + self.read(offset) } =20 /// Infallible 8-bit write with compile-time bounds check. @@ -389,10 +369,7 @@ fn write8(&self, value: u8, offset: usize) where Self: IoKnownSize + IoCapable, { - let address =3D self.io_addr_assert::(offset); - - // SAFETY: `address` has been validated by `io_addr_assert`. - unsafe { self.io_write(value, address) } + self.write(offset, value) } =20 /// Infallible 16-bit write with compile-time bounds check. @@ -401,10 +378,7 @@ fn write16(&self, value: u16, offset: usize) where Self: IoKnownSize + IoCapable, { - let address =3D self.io_addr_assert::(offset); - - // SAFETY: `address` has been validated by `io_addr_assert`. - unsafe { self.io_write(value, address) } + self.write(offset, value) } =20 /// Infallible 32-bit write with compile-time bounds check. @@ -413,10 +387,7 @@ fn write32(&self, value: u32, offset: usize) where Self: IoKnownSize + IoCapable, { - let address =3D self.io_addr_assert::(offset); - - // SAFETY: `address` has been validated by `io_addr_assert`. - unsafe { self.io_write(value, address) } + self.write(offset, value) } =20 /// Infallible 64-bit write with compile-time bounds check. @@ -425,13 +396,31 @@ fn write64(&self, value: u64, offset: usize) where Self: IoKnownSize + IoCapable, { - let address =3D self.io_addr_assert::(offset); - - // SAFETY: `address` has been validated by `io_addr_assert`. - unsafe { self.io_write(value, address) } + self.write(offset, value) } =20 /// Generic fallible read with runtime bounds check. + /// + /// # Examples + /// + /// Read a primitive type from an I/O address: + /// + /// ```no_run + /// use kernel::io::{ + /// Io, + /// Mmio, + /// }; + /// + /// fn do_reads(io: &Mmio) -> Result { + /// // 32-bit read from address `0x10`. + /// let v: u32 =3D io.try_read(0x10)?; + /// + /// // 8-bit read from address `0xfff`. + /// let v: u8 =3D io.try_read(0xfff)?; + /// + /// Ok(()) + /// } + /// ``` #[inline(always)] fn try_read(&self, location: L) -> Result where @@ -445,6 +434,27 @@ fn try_read(&self, location: L) -> Result } =20 /// Generic fallible write with runtime bounds check. + /// + /// # Examples + /// + /// Write a primitive type to an I/O address: + /// + /// ```no_run + /// use kernel::io::{ + /// Io, + /// Mmio, + /// }; + /// + /// fn do_writes(io: &Mmio) -> Result { + /// // 32-bit write of value `1` at address `0x10`. + /// io.try_write(0x10, 1u32)?; + /// + /// // 8-bit write of value `0xff` at address `0xfff`. + /// io.try_write(0xfff, 0xffu8)?; + /// + /// Ok(()) + /// } + /// ``` #[inline(always)] fn try_write(&self, location: L, value: T) -> Result where @@ -464,6 +474,23 @@ fn try_write(&self, location: L, value: T) -> Re= sult /// /// Note: this does not perform any synchronization. The caller is res= ponsible for ensuring /// exclusive access if required. + /// + /// # Examples + /// + /// Read the u32 value at address `0x10`, increment it, and store the = updated value back: + /// + /// ```no_run + /// use kernel::io::{ + /// Io, + /// Mmio, + /// }; + /// + /// fn do_update(io: &Mmio<0x1000>) -> Result { + /// io.try_update(0x10, |v: u32| { + /// v + 1 + /// }) + /// } + /// ``` #[inline(always)] fn try_update(&self, location: L, f: F) -> Result where @@ -484,6 +511,25 @@ fn try_update(&self, location: L, f: F) -> Re= sult } =20 /// Generic infallible read with compile-time bounds check. + /// + /// # Examples + /// + /// Read a primitive type from an I/O address: + /// + /// ```no_run + /// use kernel::io::{ + /// Io, + /// Mmio, + /// }; + /// + /// fn do_reads(io: &Mmio<0x1000>) { + /// // 32-bit read from address `0x10`. + /// let v: u32 =3D io.read(0x10); + /// + /// // 8-bit read from the top of the I/O space. + /// let v: u8 =3D io.read(0xfff); + /// } + /// ``` #[inline(always)] fn read(&self, location: L) -> T where @@ -497,6 +543,25 @@ fn read(&self, location: L) -> T } =20 /// Generic infallible write with compile-time bounds check. + /// + /// # Examples + /// + /// Write a primitive type to an I/O address: + /// + /// ```no_run + /// use kernel::io::{ + /// Io, + /// Mmio, + /// }; + /// + /// fn do_writes(io: &Mmio<0x1000>) { + /// // 32-bit write of value `1` at address `0x10`. + /// io.write(0x10, 1u32); + /// + /// // 8-bit write of value `0xff` at the top of the I/O space. + /// io.write(0xfff, 0xffu8); + /// } + /// ``` #[inline(always)] fn write(&self, location: L, value: T) where @@ -514,6 +579,23 @@ fn write(&self, location: L, value: T) /// /// Note: this does not perform any synchronization. The caller is res= ponsible for ensuring /// exclusive access if required. + /// + /// # Examples + /// + /// Read the u32 value at address `0x10`, increment it, and store the = updated value back: + /// + /// ```no_run + /// use kernel::io::{ + /// Io, + /// Mmio, + /// }; + /// + /// fn do_update(io: &Mmio<0x1000>) { + /// io.update(0x10, |v: u32| { + /// v + 1 + /// }) + /// } + /// ``` #[inline(always)] fn update(&self, location: L, f: F) where --=20 2.53.0