From nobody Thu Dec 18 07:30:51 2025 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2047.outbound.protection.outlook.com [40.107.243.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 629D0267731; Thu, 13 Mar 2025 14:48:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.243.47 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741877318; cv=fail; b=S9qTt7fHR0sLFlNmnbRiwX5NNCLhUopMdtYFxmd7neOhZyJceiZXAsQqv6Th59OOWbgiUjmdqN5E4NQyarQZLNvcek1mlfcedlvCAYQZNk38mJovaK2scQWhMGEPiihl+1gPaFNY1Kc+PGODDOfaSbHw8CcS16IKRTeGLK5gYCc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741877318; c=relaxed/simple; bh=1+j5QqX3VxR9+oKSdttCQ43irLdYdvsgx8RQA5l8/Ts=; h=From:Date:Subject:Content-Type:Message-Id:To:Cc:MIME-Version; b=AmNdkqnQUcegNJVSVMXXRWnwUQNTLaSVdnYszz2pzF37d8FK5pNwMEhahxKSd3Jus3AFN+Vcc4M0mDPvXyJMZPlAOYQxfQ3zuquV5DbWXL2+qESfSundOe7ZJV4wjprzvMFpgmMCyi+QDkszxFbhqVRzILYnssNwj+DtsHQmnyY= 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=Fna3KOAT; arc=fail smtp.client-ip=40.107.243.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="Fna3KOAT" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=G8s9BVvGAyjU8J4ZbatXKyW6pCkPy1pRd+ueNsAC2EVDi5DaMF3WRv0HDCpapxO70TAZRHsyrJSEDi8W6SMBQfZErbVxoDVyUM94I8EK2C1M2s+HIN+G3FDeifZbj0EPAOjQk4N5TT2igJMKX61S9WbdZ0NQRkmRQ9NVINRuLUojfwHGW0+uMvtVlUborMywRqXRbPkb29IcznHQQOomrgPigiiTH5qpRjHvlpm29HyZ1R7rKIhw3oIg+VEFWQbLXyniu92AkqgXaDE4AKsCkmMR8GgUuwE//D+sZE5DXZLHUjxEGEi+F/DBH/i+3PEeIxRlhAGY/y5pFtALTN4ssg== 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=tCh6GKjGmZ/SVveGEnnrEXwk9AYaR/XECAzPWdYw4ZY=; b=XqZA4rgV+h50Uf9WjNHsTIY+D4wULq/9r7DPj6f1EA7Cp/ftAxBwvKC9mMZLiiLnQCBrTgKsa7ip5K2ZqSFOQLO6u9NR5zWlcioA2C4UujkIX6JiApi5TBYu4WwLsPOWaa3hau9nxCl15CDxdQ1yopRGQTszUuVaIIN6wDoLSNQCBiN5k5Aagc0YOyKxnLT29y4pPOQmGwt0LiJe1rps8kcKZw9DhISEF/c9KJDg0gDfzTvfUntwhR2P8EW1bhgg/gKhDIX/bia7Cbq1u+qc6955Pi0HarAWu0gzSLsX6goBb6yAGfWb7EyNdIAWfm9f/QqKmsGNkUBS8arDMw4sKw== 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=tCh6GKjGmZ/SVveGEnnrEXwk9AYaR/XECAzPWdYw4ZY=; b=Fna3KOATXBF8JQbtFGY6UTcKwgzmnJaDLr9EyAGl44f57cJ2y0B0gnB9Wdi7uUaNRupUyQ4mjtErp+P65fDPFLNmaC40RBee+l54CHOeWwmbSw/iTce+npJ2xc4/4e57kEDfIsZataq/GT3hQJV3/KfkCqUHuE9BMe6eu38PZe11UbI79OEaYmLJw+u5IH2NauxNpvwyDZ1LrAhCTIkHPWMRSg1Jo4efVXDKUgd+RUheiqJIRFsIlTeMlc46GwFG5bVxtQYe8dupMD3QJJRvghALCKDZqRt5AKtCpU3eyhvqIDJ1SPAyDUEuf8sByknv69sQCG6xZgsFm50BO4L46A== 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 CH1PPF711010B62.namprd12.prod.outlook.com (2603:10b6:61f:fc00::614) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8511.28; Thu, 13 Mar 2025 14:48:32 +0000 Received: from CH2PR12MB3990.namprd12.prod.outlook.com ([fe80::6e37:569f:82ee:3f99]) by CH2PR12MB3990.namprd12.prod.outlook.com ([fe80::6e37:569f:82ee:3f99%6]) with mapi id 15.20.8511.026; Thu, 13 Mar 2025 14:48:32 +0000 From: Alexandre Courbot Date: Thu, 13 Mar 2025 23:48:25 +0900 Subject: [PATCH RFC] rust: add macros to define registers layout Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250313-registers-v1-1-8d498537e8b2@nvidia.com> X-B4-Tracking: v=1; b=H4sIADjw0mcC/6tWKk4tykwtVrJSqFYqSi3LLM7MzwNyDHUUlJIzE vPSU3UzU4B8JSMDI1MDY0Nj3aLU9MziktSiYl3ztJTkJOMUSyOzJAMloPqCotS0zAqwWdGxtbU AHuMk2VsAAAA= X-Change-ID: 20250313-registers-7fdcb3d926b0 To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Alexandre Courbot X-Mailer: b4 0.14.2 X-ClientProxiedBy: TY2PR04CA0018.apcprd04.prod.outlook.com (2603:1096:404:f6::30) 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_|CH1PPF711010B62:EE_ X-MS-Office365-Filtering-Correlation-Id: 4b349fe1-20dd-4aa2-48df-08dd623e203c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|10070799003|7416014|376014|921020; X-Microsoft-Antispam-Message-Info: =?utf-8?B?V1podkZoZDFOUTJPdlVnN0o4UU1mWUIweFlsV2YxbVI4STRhVlQwNWhBTjBC?= =?utf-8?B?bE5zNFVKajJGaXorNHExNFk5dnVNWENyUmRmR0pGSGU1WXF2M0VHUU9VNWQ0?= =?utf-8?B?bzQ1NnRtYjJIdWI4ZXFCd0JNNVBJUG9zUGZvcjJhT0xINElsRVV5eEI0OWhw?= =?utf-8?B?c3VhU2NHN29XVHV5ZHkyeTdHK090Y0kwc2U3L1FxbFhaUGp2OGF4TnJMY3BF?= =?utf-8?B?OGlZSGFpW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PR12MB3990.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(10070799003)(7416014)(376014)(921020);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?OEsvSDEzRm9hSE5aNWlEZEdQN0M0U2hETmlmN3pNSjMzaVFJNzRoVDVkeEM1?= =?utf-8?B?amd5NVdnS2hncE1rTkRJMVBZMmV2Q3grbUtuOXgrdFRZbDZuS0FkZHJXZGlw?= =?utf-8?B?UTlET2ZHemlsbk83MEtsUm4yd3ROTG5jMGJOd3lYMVEwY2UyOGdaZkUxYWMr?= =?utf-8?B?Z2JwVTluNDE2RXRQTHZ6OUZxaTUrMGlNcWx2MGpYclhteHdVVHd0TW9qVUtG?= =?utf-8?B?UEVzZDZJdTA1QVE4VThXTWdsK3pZbDI1RkdmTERLL3ZlUlNGMUMrN0lvNXFp?= =?utf-8?B?NEUwQ2g3dnhETFI1TW9ablZoU3o4dWEvUUI4aDlPYnFycE5JRnFKMkg3UVE2?= =?utf-8?B?dzRtTzNYN2xNb0ZtZ3ZxVEt4bE9xbTYvNDZ4QW0xb1c2WjMwMERpVCtDZjB3?= =?utf-8?B?U3EyTVV1UVNHUFpqSmY1a1hsZWNBZ2creDQxbEZVc3o4T1VVSTA2WnNFUWVz?= =?utf-8?B?YXNCeHVFNmpmWWxGd2hyZko3VnBQMUJKWTA0TnpvdlI3M1hnNGNqODJoYlll?= =?utf-8?B?aklqWTByUUJjVmpWc0tlQkRab3d0NW9TaEx3RHBxYithVjEzN2k0U3dKNERm?= =?utf-8?B?S0ZUSHF6YWptemNpY29MeG9YUHhYSWVrcVY5QXlrcjFmNzRMaU9kY04xOE1D?= =?utf-8?B?UUhheUNGVzB0SS9vVmdWTkxDZ3hROVR3Z0IxZHQxbmZNekl0RzBqTjh2NEdX?= =?utf-8?B?YTJ6WjR1dnNjbGVucnNCYUtPUEpiS1NNVFJlVU9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yNmZaaENkRi9Yazh6WmRaVFdZ?= =?utf-8?B?T1Z0V1ZZeDRWeHVVdzcyRHlmdGQrZU4weHAxRGQwcnpxcHQzbmZ1S0FXbUdh?= =?utf-8?B?MVJyVEJxWWwweS9JS3BkL29OemZLTE1MQWZaSEZXM3QrSy9kUnQwTTJXL3Z1?= =?utf-8?B?bzFFWFFFVXNvaVA0TGZCS0RnTFBFUzc2amgrMVBPZ1pQaHgwZjlLRWM3Y3BY?= =?utf-8?B?S3VsTXBiSlFXRzZhRHVnSEhHUnRESlFZNDQ5WkxFd2c4TWhkRTNNOUJYd1U1?= =?utf-8?B?U003amM1cGVpNHU3aUYvZmlZK2J1L2VBZm1hcXFHNVFPNzFjU05qamlKZmNW?= =?utf-8?B?cUVzUzM3bE5zUkdTWmJPVFJIclFxVnBENktXRDVlbzVvaGx1ZEFkUUJhUlgv?= =?utf-8?B?WVRqT0ZqQlFtNlhzMGxMTFRDMEg4RUxkcy9UMWFrdHhmSjdzT3hKL1BXZkh5?= =?utf-8?B?MHFKNElONUpCREFObVM0YUk1R3V5aDE5cU11NjIzb0xUcjdYYnhneHNNbUNo?= =?utf-8?Q?eP0xd4qOuSnHbfX7UhtiGVscFVGW6nT1dFDBavfbieep?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4b349fe1-20dd-4aa2-48df-08dd623e203c X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB3990.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Mar 2025 14:48:32.6036 (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: VdQkis0oMJ8rD7J2yzTL4aG8Y1JN6hvSs/B2gNF51H3ThOkDygEO3VijT0hL6fGcEsInESG2B/zME117GnkStQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH1PPF711010B62 Add two macros, reg_def!() and reg_def_rel!(), that define a given register's layout and provide accessors for absolute or relative offsets, respectively. The following example (taken from the rustdoc) helps understanding how they are used: reg_def!(Boot0@0x00000100, "Basic revision information about the chip"; 3:0 minor_rev =3D> as u8, "minor revision of the chip"; 7:4 major_rev =3D> as u8, "major revision of the chip"; 28:20 chipset =3D> try_into Chipset, "chipset model" ); This defines a `Boot0` type which can be read or written from offset `0x100` of an `Io` region. It is composed of 3 fields, for instance `minor_rev` is made of the 4 less significant bits of the register. Each field can be accessed and modified using helper methods: // Read from offset `0x100`. let boot0 =3D Boot0.read(&bar); pr_info!("chip revision: {}.{}", boot0.major_rev(), boot0.minor_rev()); // `Chipset::try_from` will be called with the value of the field and // returns an error if the value is invalid. let chipset =3D boot0.chipset()?; // Update some fields and write the value back. boot0.set_major_rev(3).set_minor_rev(10).write(&bar); Fields are made accessible using one of the following strategies: - `as ` simply casts the field value to the requested type. - `as_bit ` turns the field into a boolean and calls ::from()` with the obtained value. To be used with single-bit fields. - `into ` calls `::from()` on the value of the field. It is expected to handle all the possible values for the bit range selected. - `try_into ` calls `::try_from()` on the value of the field and returns its result. The documentation strings are optional. If present, they will be added to the type or the field getter and setter methods they are attached to. Signed-off-by: Alexandre Courbot --- I have written these initially for the nova-core driver, then it has been suggested that they might be useful outside of it as well, so here goes. This is my first serious attempt at writing Rust macros and I am sure there is a lot that is wrong with them, but I'd like to get early feedback and see whether this is actually something we want for the kernel in general. The following in particular needs to be improved, suggestions are welcome: - Inner types other than `u32` need to be supported - this can probably just be an extra parameter of the macro. - The syntax can certainly be improved. I've tried to some with something that makes the register layout obvious, while fitting within the expectations of the Rust macro parser, but my lack of experience certainly shows here. - We probably need an option to make some fields or whole registers read-only. - The I/O offset and read/write methods should be optional, so the layout part can be used for things that are not registers. - The visibility of the helper macros is a bit of a headache - I haven't found a way to completely hide them to the outside, so I have prefixed them with `__` for now. - Formatting - there are some pretty long lines, not sure how to break them in an idiomatic way. Sorry if this is still a bit rough around the edges, but hopefully the potential benefit is properly conveyed. --- rust/kernel/lib.rs | 1 + rust/kernel/reg.rs | 284 +++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 285 insertions(+) diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 398242f92a961c3a445d681c65449047a847968a..d610199f6675d22fa01d4db524d= 9989581f7b646 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -69,6 +69,7 @@ pub mod prelude; pub mod print; pub mod rbtree; +mod reg; pub mod revocable; pub mod security; pub mod seq_file; diff --git a/rust/kernel/reg.rs b/rust/kernel/reg.rs new file mode 100644 index 0000000000000000000000000000000000000000..3f0bad18b4f757fb3e7d45f2fde= 6c3214fa957c8 --- /dev/null +++ b/rust/kernel/reg.rs @@ -0,0 +1,284 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Types and macros to define register layout and accessors. +//! +//! A single register typically includes several fields, which are accesse= d through a combination +//! of bit-shift and mask operations that introduce a class of potential m= istakes, notably because +//! not all possible field values are necessarily valid. +//! +//! The macros in this module allow to define, using an intruitive and rea= dable syntax, a dedicated +//! type for each register with its own field accessors that can return an= error is a field's value +//! is invalid. They also provide a builder type allowing to construct a r= egister value to be +//! written by combining valid values for its fields. + +/// Helper macro for the `reg_def` family of macros. +/// +/// Defines the wrapper `$name` type, as well as its relevant implementati= ons (`Debug`, `BitOr`, +/// and conversion to regular `u32`). +#[macro_export] +macro_rules! __reg_def_common { + ($name:ident $(, $type_comment:expr)?) =3D> { + $( + #[doc=3D$type_comment] + )? + #[repr(transparent)] + #[derive(Clone, Copy, Default)] + pub(crate) struct $name(u32); + + // TODO: should we display the raw hex value, then the value of al= l its fields? + impl ::core::fmt::Debug for $name { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::= Result { + f.debug_tuple(stringify!($name)) + .field(&format_args!("0x{0:x}", &self.0)) + .finish() + } + } + + impl core::ops::BitOr for $name { + type Output =3D Self; + + fn bitor(self, rhs: Self) -> Self::Output { + Self(self.0 | rhs.0) + } + } + + impl From<$name> for u32 { + fn from(reg: $name) -> u32 { + reg.0 + } + } + }; +} + +/// Helper macro for the `reg_def` family of macros. +/// +/// Defines the getter method for $field. +#[macro_export] +macro_rules! __reg_def_field_getter { + ( + $hi:tt:$lo:tt $field:ident + $(=3D> as $as_type:ty)? + $(=3D> as_bit $bit_type:ty)? + $(=3D> into $type:ty)? + $(=3D> try_into $try_type:ty)? + $(, $comment:expr)? + ) =3D> { + $( + #[doc=3Dconcat!("Returns the ", $comment)] + )? + #[inline] + pub(crate) fn $field(self) -> $( $as_type )? $( $bit_type )? $( $t= ype )? $( core::result::Result<$try_type, <$try_type as TryFrom>::Erro= r> )? { + const MASK: u32 =3D ((((1 << $hi) - 1) << 1) + 1) - ((1 << $lo= ) - 1); + const SHIFT: u32 =3D MASK.trailing_zeros(); + let field =3D (self.0 & MASK) >> SHIFT; + + $( field as $as_type )? + $( + // TODO: it would be nice to throw a compile-time error if $hi= !=3D $lo as this means we + // are considering more than one bit but returning a bool... + <$bit_type>::from(if field !=3D 0 { true } else { false }) as = $bit_type + )? + $( <$type>::from(field) )? + $( <$try_type>::try_from(field) )? + } + } +} + +/// Helper macro for the `reg_def` family of macros. +/// +/// Defines all the field getter methods for `$name`. +#[macro_export] +macro_rules! __reg_def_getters { + ( + $name:ident + $(; $hi:tt:$lo:tt $field:ident + $(=3D> as $as_type:ty)? + $(=3D> as_bit $bit_type:ty)? + $(=3D> into $type:ty)? + $(=3D> try_into $try_type:ty)? + $(, $field_comment:expr)?)* $(;)? + ) =3D> { + #[allow(dead_code)] + impl $name { + $( + ::kernel::__reg_def_field_getter!($hi:$lo $field $(=3D> as $as= _type)? $(=3D> as_bit $bit_type)? $(=3D> into $type)? $(=3D> try_into $try_= type)? $(, $field_comment)?); + )* + } + }; +} + +/// Helper macro for the `reg_def` family of macros. +/// +/// Defines the setter method for $field. +#[macro_export] +macro_rules! __reg_def_field_setter { + ( + $hi:tt:$lo:tt $field:ident + $(=3D> as $as_type:ty)? + $(=3D> as_bit $bit_type:ty)? + $(=3D> into $type:ty)? + $(=3D> try_into $try_type:ty)? + $(, $comment:expr)? + ) =3D> { + kernel::macros::paste! { + $( + #[doc=3Dconcat!("Sets the ", $comment)] + )? + #[inline] + pub(crate) fn [](mut self, value: $( $as_type)? $( $b= it_type )? $( $type )? $( $try_type)? ) -> Self { + const MASK: u32 =3D ((((1 << $hi) - 1) << 1) + 1) - ((1 << $lo= ) - 1); + const SHIFT: u32 =3D MASK.trailing_zeros(); + + let value =3D ((value as u32) << SHIFT) & MASK; + self.0 =3D self.0 | value; + self + } + } + }; +} + +/// Helper macro for the `reg_def` family of macros. +/// +/// Defines all the field setter methods for `$name`. +#[macro_export] +macro_rules! __reg_def_setters { + ( + $name:ident + $(; $hi:tt:$lo:tt $field:ident + $(=3D> as $as_type:ty)? + $(=3D> as_bit $bit_type:ty)? + $(=3D> into $type:ty)? + $(=3D> try_into $try_type:ty)? + $(, $field_comment:expr)?)* $(;)? + ) =3D> { + #[allow(dead_code)] + impl $name { + $( + ::kernel::__reg_def_field_setter!($hi:$lo $field $(=3D> as $as= _type)? $(=3D> as_bit $bit_type)? $(=3D> into $type)? $(=3D> try_into $try_= type)? $(, $field_comment)?); + )* + } + }; +} + +/// Defines a dedicated type for a register with an absolute offset, along= side with getter and +/// setter methods for its fields and methods to read and write it from an= `Io` region. +/// +/// Example: +/// +/// ```no_run +/// reg_def!(Boot0@0x00000100, "Basic revision information about the chip"; +/// 3:0 minor_rev =3D> as u8, "minor revision of the chip"; +/// 7:4 major_rev =3D> as u8, "major revision of the chip"; +/// 28:20 chipset =3D> try_into Chipset, "chipset model" +/// ); +/// ``` +/// +/// This defines a `Boot0` type which can be read or written from offset `= 0x100` of an `Io` region. +/// It is composed of 3 fields, for instance `minor_rev` is made of the 4 = less significant bits of +/// the register. Each field can be accessed and modified using helper met= hods: +/// +/// ```no_run +/// // Read from offset 0x100. +/// let boot0 =3D Boot0.read(&bar); +/// pr_info!("chip revision: {}.{}", boot0.major_rev(), boot0.minor_rev()); +/// +/// // `Chipset::try_from` will be called with the value of the field and = returns an error if the +/// // value is invalid. +/// let chipset =3D boot0.chipset()?; +/// +/// // Update some fields and write the value back. +/// boot0.set_major_rev(3).set_minor_rev(10).write(&bar); +/// ``` +/// +/// Fields are made accessible using one of the following strategies: +/// +/// - `as ` simply casts the field value to the requested type. +/// - `as_bit ` turns the field into a boolean and calls `::fr= om()` with the obtained +/// value. To be used with single-bit fields. +/// - `into ` calls `::from()` on the value of the field. It i= s expected to handle all +/// the possible values for the bit range selected. +/// - `try_into ` calls `::try_from()` on the value of the fie= ld and returns its +/// result. +/// +/// The documentation strings are optional. If present, they will be added= to the type or the field +/// getter and setter methods they are attached to. +#[macro_export] +macro_rules! reg_def { + ( + $name:ident@$offset:expr $(, $type_comment:expr)? + $(; $hi:tt:$lo:tt $field:ident + $(=3D> as $as_type:ty)? + $(=3D> as_bit $bit_type:ty)? + $(=3D> into $type:ty)? + $(=3D> try_into $try_type:ty)? + $(, $field_comment:expr)?)* $(;)? + ) =3D> { + ::kernel::__reg_def_common!($name); + + #[allow(dead_code)] + impl $name { + #[inline] + pub(crate) fn read>>(bar: &T) -> Self { + Self(bar.readl($offset)) + } + + #[inline] + pub(crate) fn write>>(self, bar: &T) { + bar.writel(self.0, $offset) + } + } + + ::kernel::__reg_def_getters!($name; $( $hi:$lo $field $(=3D> as $a= s_type)? $(=3D> as_bit $bit_type)? $(=3D> into $type)? $(=3D> try_into $try= _type)? $(, $field_comment)? );*); + + ::kernel::__reg_def_setters!($name; $( $hi:$lo $field $(=3D> as $a= s_type)? $(=3D> as_bit $bit_type)? $(=3D> into $type)? $(=3D> try_into $try= _type)? $(, $field_comment)? );*); + }; +} + +/// Defines a dedicated type for a register with a relative offset, alongs= ide with getter and +/// setter methods for its fields and methods to read and write it from an= `Io` region. +/// +/// See the documentation for [`reg_def`] for more details. This macro wor= ks similarly to +/// `reg_def`, with the exception that the `read` and `write` methods take= a `base` argument that +/// is added to the offset of the register before access, and the `try_rea= d` and `try_write` +/// methods are added to allow access with offsets unknown at compile-time. +#[macro_export] +macro_rules! reg_def_rel { + ( + $name:ident@$offset:expr $(, $type_comment:expr)? + $(; $hi:tt:$lo:tt $field:ident + $(=3D> as $as_type:ty)? + $(=3D> as_bit $bit_type:ty)? + $(=3D> into $type:ty)? + $(=3D> try_into $try_type:ty)? + $(, $field_comment:expr)?)* $(;)? + ) =3D> { + ::kernel::__reg_def_common!($name); + + #[allow(dead_code)] + impl $name { + #[inline] + pub(crate) fn read>>(bar: &T, base: usize) -> Self { + Self(bar.readl(base + $offset)) + } + + #[inline] + pub(crate) fn write>>(self, bar: &T, base: usize) { + bar.writel(self.0, base + $offset) + } + + #[inline] + pub(crate) fn try_read>>(bar: &T, base: usize) -> ::kernel::error::Result { + bar.try_readl(base + $offset).map(Self) + } + + #[inline] + pub(crate) fn try_write>>(self, bar: &T, base: usize) -> ::kernel::error::Result<()> { + bar.try_writel(self.0, base + $offset) + } + } + + ::kernel::__reg_def_getters!($name; $( $hi:$lo $field $(=3D> as $a= s_type)? $(=3D> as_bit $bit_type)? $(=3D> into $type)? $(=3D> try_into $try= _type)? $(, $field_comment)? );*); + + ::kernel::__reg_def_setters!($name; $( $hi:$lo $field $(=3D> as $a= s_type)? $(=3D> as_bit $bit_type)? $(=3D> into $type)? $(=3D> try_into $try= _type)? $(, $field_comment)? );*); + }; +} --- base-commit: 4d872d51bc9d7b899c1f61534e3dbde72613f627 change-id: 20250313-registers-7fdcb3d926b0 Best regards, --=20 Alexandre Courbot