From nobody Tue Dec 16 21:51:00 2025 Received: from DM5PR21CU001.outbound.protection.outlook.com (mail-centralusazon11011038.outbound.protection.outlook.com [52.101.62.38]) (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 65D2228727D; Fri, 3 Oct 2025 15:48:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.62.38 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759506491; cv=fail; b=JCtWpZwq25jeIpFg/DFhBNS9wF21CqqlBIm4XI+oi4+dBcn7+MBX9DDr/15hq8Xv1kIARbfegcTjt+CekRIEMKi23doNGj9kxX3vEjjII6Q0seABxXPyzD4Rx7jK7V3HCe4lrKbuLm09rQBJzlI9R68603xT83iTJ/cdQkHK1Kw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759506491; c=relaxed/simple; bh=TnB6Ezt22MrS++9N+lZpiOdl0AC6/XoL+NtfNcPLgP4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Content-Type:MIME-Version; b=Z29muk84nzKCyORiO2mslBYNQ9q+k/DD+Epm23/sp+AoSk5YkK9RByOy2bdg1nzgktOJk5KfXmQMwFCEG500K6SELJuI14bKU5DDFKhAFkfXO+BZ49bonTeePGuYdxqZ59CThLZlMdWEzdGsoVD18RErCuaOelt0wpfThVY7ApM= 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=EJUBFZSE; arc=fail smtp.client-ip=52.101.62.38 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="EJUBFZSE" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ENA3jw1TnnUJ1xafBjxkjf2Ny0OofbrY7YJwV18SKCHSPEQPWS9KC0eMlri4jWJv5VSg5esBKEmirQxGuV1qhFxwuyh5BX8K2i7v3Z7qBBNmNM9ZgvV0aW2d7b7T9CzcsWTOnZckHCLjd2MwVk76DSGtvpzHo1vDKTlHM54YltAW9P5ozMoRFYHri+SNtEpsLCrglX8OO5XXXjQ2XKACC1viM+0S+tY4CLjerVqgolIK+L812dcx7U5ONmbI6aqeyQLQTPtnJlGnhxkyU7MoPjAD4eB6Uf/wKa8lHb5OMZKM0gyASv/3fYKFntYTRgmZYGdLjpOkK/RnOF/Id75a+A== 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=9Zu4WnKxS81fwCLW/6lfcz7srFLvC/+CJNK2p1fwATo=; b=i6v9hds9BaC9qamAIIOPu56XiwK4XL0jdeit5P+zt/wSvzGc0QIO8qPr6G+o5JyHjf9J+vmQhJPMhYBfkkeqxbXHzmi4nGgj9oBxKsYrkBHZJWlrsWrs6SUKB70FiOcAGF2iWSsyqQ8Td6pVrVxFo7vU/73dSJeL3R4qnTsNv9Wx0QXMyWE/QpVmKaIoLeBXrOnoL2VYfar7uzmaK875zBJDCgRrYEv4BKHfFPSWKxS2dqA4EfV2U2374hi6n2cYWJnjhcU/KT08PIwQPFizeyE9L1gBV/NdYqdD4O7808yphfBpdP/n+pJUc5fa6QcdJbAeYKz4y8URhHCmHc9COw== 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=9Zu4WnKxS81fwCLW/6lfcz7srFLvC/+CJNK2p1fwATo=; b=EJUBFZSEM46tXW0Gl5ypnEPgezCQS+9P84GvslHGNlpF0Cpn6crC48XSXTUgMf69mAx7ck1sjzItE0i3yrF09QsvzGFo/ZTicDr4DNzTGUeglxfTxeVA8CSJocBFbopiOGAuDqSaH3LIQydrm1MPPegbrhEro5trfgtIhR8UAVqALWd30TJ5245zQ+CVqEnQ5J2kYWPt+IAboxwqWZ4amGcCkdAWSlQu7NURpjnhhsCwQVpn3PCXi8QQWGB0Z/DfCdFm8YpArf+BfMdoURmplY2CNHD/pErvqLArP1IixDANSP9RDJzhD4UOOUHNAILQRcLdPwAoGL4c5KE9uw8qaA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from SN7PR12MB8059.namprd12.prod.outlook.com (2603:10b6:806:32b::7) by PH0PR12MB8152.namprd12.prod.outlook.com (2603:10b6:510:292::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9182.16; Fri, 3 Oct 2025 15:47:55 +0000 Received: from SN7PR12MB8059.namprd12.prod.outlook.com ([fe80::4ee2:654e:1fe8:4b91]) by SN7PR12MB8059.namprd12.prod.outlook.com ([fe80::4ee2:654e:1fe8:4b91%2]) with mapi id 15.20.9160.017; Fri, 3 Oct 2025 15:47:55 +0000 From: Joel Fernandes To: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org, dakr@kernel.org, acourbot@nvidia.com Cc: Alistair Popple , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , bjorn3_gh@protonmail.com, Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , David Airlie , Simona Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , John Hubbard , Joel Fernandes , Timur Tabi , joel@joelfernandes.org, Elle Rhumsaa , Yury Norov , Daniel Almeida , Andrea Righi , nouveau@lists.freedesktop.org Subject: [PATCH v6 1/5] nova-core: bitfield: Move bitfield-specific code from register! into new macro Date: Fri, 3 Oct 2025 11:47:44 -0400 Message-Id: <20251003154748.1687160-2-joelagnelf@nvidia.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251003154748.1687160-1-joelagnelf@nvidia.com> References: <20251003154748.1687160-1-joelagnelf@nvidia.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: BN0PR04CA0029.namprd04.prod.outlook.com (2603:10b6:408:ee::34) To SN7PR12MB8059.namprd12.prod.outlook.com (2603:10b6:806:32b::7) 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: SN7PR12MB8059:EE_|PH0PR12MB8152:EE_ X-MS-Office365-Filtering-Correlation-Id: f252f711-0c4c-45ae-bbb2-08de029437d2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|7416014|376014|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?xlwTS7hDRpp4WcFn++vUp8wPXAxmRaV41S+tt8QFZ/q5bLwXgy3IOLdIb9vE?= =?us-ascii?Q?GJ5KuSLyPRV+90OLPwipGqAoGmSDY50mExAGIvB014YPQoVytF/eJZtYLw4u?= =?us-ascii?Q?NKI17iPD0Lc7NgjG3sRU6ZLHyNBBRA6oWRMHruwCO/+WfHwc+kBxZRc1/XUF?= =?us-ascii?Q?OQwkYQ9bcsDGwvszFh5T1WCoaoSYsQyXCHWyGGEU4dlAqkA60DAgQb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WToRm14cYcl6VQSbdWq5FhMq2eCShbV?= =?us-ascii?Q?lomUjtx5DdSsD2B2VEeVl41O+4/JkH83HFegLH+RwE6JaYSUSNit3VhuhQIx?= =?us-ascii?Q?arBjXREUV36LslZXIJVNk7onFL1aJe79utjPeMXafdOduhSpPpfAjJa1HWUp?= =?us-ascii?Q?pjuiUflT5+JH++1RhnNl5l4NCrLy0TyTKUnyNQmjUPx3jQQtg1LBU+IEsnfd?= =?us-ascii?Q?RVtg9fGbgOfytZmW0/zxAGDrObtuaOWFOaUiABoVDur3/RUGmKHwuYmxNetx?= =?us-ascii?Q?SfxiVpkqj1XXmOs8MpOttTu/eY4FlCu84rHioH13CLYf+VQr7eXG5TqSABzS?= =?us-ascii?Q?fxfiJSeIPUwXCK3mkt10yPbJDQ5AyakT?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN7PR12MB8059.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(7416014)(376014)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?TxOJyjtgeCt208FW88INEC90LI3X4mh8zVoWEQq+0Bdhb3gmVFF02PiyQ81Q?= =?us-ascii?Q?47QiPDN2MCmOUrTPwNLIRjApl/qDpJGKy3kMVEdbMiDi1uxpV3u06cvxwzyM?= =?us-ascii?Q?KeukABLurnKHZ/+FKITELuMLII5YTouhnTDE7Ahkks7CFo5W+A5kKo/HauEv?= =?us-ascii?Q?SrUD2OUErY29VuhhsStpopKq+b19I5pUkc3AwlNxhTcyL/zXsJgUgl3yYHFM?= =?us-ascii?Q?OtafuruQG+Pm9Llbpa9dZtP8A6JQIy4HZEOber2GcHWIp3qesj+M5KtMYlrT?= =?us-ascii?Q?Adqlu6cr36xPlF869//ss9AWjpv689zUojbW3FaCR7NTKFZz8TRqcgtPTwx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bE1XedHIW25Msj2Aji5Fe69eay0dVXv?= =?us-ascii?Q?hlgUlZgtCP9pb5U/fc2PoZRf64RGuumPgOEhr3wBLYSfG2bwr65/tm3X2vX/?= =?us-ascii?Q?NrKyNPNkLPPYY9XalqZMQYMbKWX7y7mBp8US0ddLGkJjWXaL0ahJREYu2Rw6?= =?us-ascii?Q?zCn3pmEfus3FYU81aF1NLtVSwmNoj+XsmZtbBNJeJ7Xng8Ze+NSfasmF6GYF?= =?us-ascii?Q?cIGOrLNxI7mSXIPBTy2xDqr9KJqHf1N1ZWGLkOE9/8pcSFEM77eVrqncYPbA?= =?us-ascii?Q?oQquvdYYXWVRRdkyXplsw92uSQZR6dn65Mi1kA2RceSoyGVvtJ7dS7HydBtz?= =?us-ascii?Q?C2BS9jtYdbytP5TAkpGiOokJXHFge+KJDVMne9iq?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: f252f711-0c4c-45ae-bbb2-08de029437d2 X-MS-Exchange-CrossTenant-AuthSource: SN7PR12MB8059.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Oct 2025 15:47:55.0304 (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: I5CTIsFmhoY9+yBpjkn1OS7JzKDijEUbPLRaNsL4ZVdZH2BkkX6G6Dko95NVWeSVjm62/Xj6rOsoerzeNHWHUw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB8152 Content-Type: text/plain; charset="utf-8" The bitfield-specific into new macro. This will be used to define structs with bitfields, similar to C language. Reviewed-by: Elle Rhumsaa Reviewed-by: Alexandre Courbot Signed-off-by: Joel Fernandes Reviewed-by: Edwin Peer --- drivers/gpu/nova-core/bitfield.rs | 316 +++++++++++++++++++++++++++ drivers/gpu/nova-core/nova_core.rs | 3 + drivers/gpu/nova-core/regs/macros.rs | 259 +--------------------- 3 files changed, 329 insertions(+), 249 deletions(-) create mode 100644 drivers/gpu/nova-core/bitfield.rs diff --git a/drivers/gpu/nova-core/bitfield.rs b/drivers/gpu/nova-core/bitf= ield.rs new file mode 100644 index 000000000000..dd0ef2016ff8 --- /dev/null +++ b/drivers/gpu/nova-core/bitfield.rs @@ -0,0 +1,316 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Bitfield library for Rust structures +//! +//! Support for defining bitfields in Rust structures. Also used by the [`= register!`] macro. + +/// Defines a struct with accessors to access bits within an inner unsigne= d integer. +/// +/// # Syntax +/// +/// ```rust +/// use nova_core::bitfield; +/// +/// #[derive(Debug, Clone, Copy, Default)] +/// enum Mode { +/// #[default] +/// Low =3D 0, +/// High =3D 1, +/// Auto =3D 2, +/// } +/// +/// impl TryFrom for Mode { +/// type Error =3D u8; +/// fn try_from(value: u8) -> Result { +/// match value { +/// 0 =3D> Ok(Mode::Low), +/// 1 =3D> Ok(Mode::High), +/// 2 =3D> Ok(Mode::Auto), +/// _ =3D> Err(value), +/// } +/// } +/// } +/// +/// impl From for u32 { +/// fn from(mode: Mode) -> u32 { +/// mode as u32 +/// } +/// } +/// +/// #[derive(Debug, Clone, Copy, Default)] +/// enum State { +/// #[default] +/// Inactive =3D 0, +/// Active =3D 1, +/// } +/// +/// impl From for State { +/// fn from(value: bool) -> Self { +/// if value { State::Active } else { State::Inactive } +/// } +/// } +/// +/// impl From for u32 { +/// fn from(state: State) -> u32 { +/// state as u32 +/// } +/// } +/// +/// bitfield! { +/// struct ControlReg { +/// 3:0 mode as u8 ?=3D> Mode; +/// 7:7 state as bool =3D> State; +/// } +/// } +/// ``` +/// +/// This generates a struct with: +/// - Field accessors: `mode()`, `state()`, etc. +/// - Field setters: `set_mode()`, `set_state()`, etc. (supports chaining = with builder pattern). +/// - Debug and Default implementations. +/// +/// Fields are defined as follows: +/// +/// - `as ` simply returns the field value casted to , typical= ly `u32`, `u16`, `u8` or +/// `bool`. Note that `bool` fields must have a range of 1 bit. +/// - `as =3D> ` calls ``'s `From::<>` = implementation and returns +/// the result. +/// - `as ?=3D> ` calls ``'s `TryFrom= ::<>` implementation +/// and returns the result. This is useful with fields for which not all= values are valid. +macro_rules! bitfield { + // Main entry point - defines the bitfield struct with fields + (struct $name:ident $(, $comment:literal)? { $($fields:tt)* }) =3D> { + bitfield!(@core $name $(, $comment)? { $($fields)* }); + }; + + // All rules below are helpers. + + // Defines the wrapper `$name` type, as well as its relevant implement= ations (`Debug`, + // `Default`, `BitOr`, and conversion to the value type) and field acc= essor methods. + (@core $name:ident $(, $comment:literal)? { $($fields:tt)* }) =3D> { + $( + #[doc=3D$comment] + )? + #[repr(transparent)] + #[derive(Clone, Copy)] + pub(crate) struct $name(u32); + + impl ::core::ops::BitOr for $name { + type Output =3D Self; + + fn bitor(self, rhs: Self) -> Self::Output { + Self(self.0 | rhs.0) + } + } + + impl ::core::convert::From<$name> for u32 { + fn from(val: $name) -> u32 { + val.0 + } + } + + bitfield!(@fields_dispatcher $name { $($fields)* }); + }; + + // Captures the fields and passes them to all the implementers that re= quire field information. + // + // Used to simplify the matching rules for implementers, so they don't= need to match the entire + // complex fields rule even though they only make use of part of it. + (@fields_dispatcher $name:ident { + $($hi:tt:$lo:tt $field:ident as $type:tt + $(?=3D> $try_into_type:ty)? + $(=3D> $into_type:ty)? + $(, $comment:literal)? + ; + )* + } + ) =3D> { + bitfield!(@field_accessors $name { + $( + $hi:$lo $field as $type + $(?=3D> $try_into_type)? + $(=3D> $into_type)? + $(, $comment)? + ; + )* + }); + bitfield!(@debug $name { $($field;)* }); + bitfield!(@default $name { $($field;)* }); + }; + + // Defines all the field getter/setter methods for `$name`. + ( + @field_accessors $name:ident { + $($hi:tt:$lo:tt $field:ident as $type:tt + $(?=3D> $try_into_type:ty)? + $(=3D> $into_type:ty)? + $(, $comment:literal)? + ; + )* + } + ) =3D> { + $( + bitfield!(@check_field_bounds $hi:$lo $field as $type); + )* + + #[allow(dead_code)] + impl $name { + $( + bitfield!(@field_accessor $name $hi:$lo $field as $type + $(?=3D> $try_into_type)? + $(=3D> $into_type)? + $(, $comment)? + ; + ); + )* + } + }; + + // Boolean fields must have `$hi =3D=3D $lo`. + (@check_field_bounds $hi:tt:$lo:tt $field:ident as bool) =3D> { + #[allow(clippy::eq_op)] + const _: () =3D { + ::kernel::build_assert!( + $hi =3D=3D $lo, + concat!("boolean field `", stringify!($field), "` covers m= ore than one bit") + ); + }; + }; + + // Non-boolean fields must have `$hi >=3D $lo`. + (@check_field_bounds $hi:tt:$lo:tt $field:ident as $type:tt) =3D> { + #[allow(clippy::eq_op)] + const _: () =3D { + ::kernel::build_assert!( + $hi >=3D $lo, + concat!("field `", stringify!($field), "`'s MSB is smaller= than its LSB") + ); + }; + }; + + // Catches fields defined as `bool` and convert them into a boolean va= lue. + ( + @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as bool =3D= > $into_type:ty + $(, $comment:literal)?; + ) =3D> { + bitfield!( + @leaf_accessor $name $hi:$lo $field + { |f| <$into_type>::from(if f !=3D 0 { true } else { false }) } + $into_type =3D> $into_type $(, $comment)?; + ); + }; + + // Shortcut for fields defined as `bool` without the `=3D>` syntax. + ( + @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as bool $(,= $comment:literal)?; + ) =3D> { + bitfield!(@field_accessor $name $hi:$lo $field as bool =3D> bool $= (, $comment)?;); + }; + + // Catches the `?=3D>` syntax for non-boolean fields. + ( + @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as $type:tt= ?=3D> $try_into_type:ty + $(, $comment:literal)?; + ) =3D> { + bitfield!(@leaf_accessor $name $hi:$lo $field + { |f| <$try_into_type>::try_from(f as $type) } $try_into_type = =3D> + ::core::result::Result< + $try_into_type, + <$try_into_type as ::core::convert::TryFrom<$type>>::Error + > + $(, $comment)?;); + }; + + // Catches the `=3D>` syntax for non-boolean fields. + ( + @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as $type:tt= =3D> $into_type:ty + $(, $comment:literal)?; + ) =3D> { + bitfield!(@leaf_accessor $name $hi:$lo $field + { |f| <$into_type>::from(f as $type) } $into_type =3D> $into_t= ype $(, $comment)?;); + }; + + // Shortcut for non-boolean fields defined without the `=3D>` or `?=3D= >` syntax. + ( + @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as $type:tt + $(, $comment:literal)?; + ) =3D> { + bitfield!(@field_accessor $name $hi:$lo $field as $type =3D> $type= $(, $comment)?;); + }; + + // Generates the accessor methods for a single field. + ( + @leaf_accessor $name:ident $hi:tt:$lo:tt $field:ident + { $process:expr } $to_type:ty =3D> $res_type:ty $(, $comment:l= iteral)?; + ) =3D> { + ::kernel::macros::paste!( + const [<$field:upper _RANGE>]: ::core::ops::RangeInclusive =3D= $lo..=3D$hi; + const [<$field:upper _MASK>]: u32 =3D ((((1 << $hi) - 1) << 1) + 1= ) - ((1 << $lo) - 1); + const [<$field:upper _SHIFT>]: u32 =3D Self::[<$field:upper _MASK>= ].trailing_zeros(); + ); + + $( + #[doc=3D"Returns the value of this field:"] + #[doc=3D$comment] + )? + #[inline(always)] + pub(crate) fn $field(self) -> $res_type { + ::kernel::macros::paste!( + const MASK: u32 =3D $name::[<$field:upper _MASK>]; + const SHIFT: u32 =3D $name::[<$field:upper _SHIFT>]; + ); + let field =3D ((self.0 & MASK) >> SHIFT); + + $process(field) + } + + ::kernel::macros::paste!( + $( + #[doc=3D"Sets the value of this field:"] + #[doc=3D$comment] + )? + #[inline(always)] + pub(crate) fn [](mut self, value: $to_type) -> Self { + const MASK: u32 =3D $name::[<$field:upper _MASK>]; + const SHIFT: u32 =3D $name::[<$field:upper _SHIFT>]; + let value =3D (u32::from(value) << SHIFT) & MASK; + self.0 =3D (self.0 & !MASK) | value; + + self + } + ); + }; + + // Generates the `Debug` implementation for `$name`. + (@debug $name:ident { $($field:ident;)* }) =3D> { + impl ::core::fmt::Debug for $name { + fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::f= mt::Result { + f.debug_struct(stringify!($name)) + .field("", &format_args!("{:#x}", &self.0)) + $( + .field(stringify!($field), &self.$field()) + )* + .finish() + } + } + }; + + // Generates the `Default` implementation for `$name`. + (@default $name:ident { $($field:ident;)* }) =3D> { + /// Returns a value for the bitfield where all fields are set to t= heir default value. + impl ::core::default::Default for $name { + fn default() -> Self { + #[allow(unused_mut)] + let mut value =3D Self(Default::default()); + + ::kernel::macros::paste!( + $( + value.[](Default::default()); + )* + ); + + value + } + } + }; +} diff --git a/drivers/gpu/nova-core/nova_core.rs b/drivers/gpu/nova-core/nov= a_core.rs index fffcaee2249f..112277c7921e 100644 --- a/drivers/gpu/nova-core/nova_core.rs +++ b/drivers/gpu/nova-core/nova_core.rs @@ -2,6 +2,9 @@ =20 //! Nova Core GPU Driver =20 +#[macro_use] +mod bitfield; + mod dma; mod driver; mod falcon; diff --git a/drivers/gpu/nova-core/regs/macros.rs b/drivers/gpu/nova-core/r= egs/macros.rs index 754c14ee7f40..945d15a2c529 100644 --- a/drivers/gpu/nova-core/regs/macros.rs +++ b/drivers/gpu/nova-core/regs/macros.rs @@ -8,7 +8,8 @@ //! //! The `register!` macro in this module provides an intuitive and readabl= e syntax for defining a //! dedicated type for each register. Each such type comes with its own fi= eld accessors that can -//! return an error if a field's value is invalid. +//! return an error if a field's value is invalid. Please look at the [`bi= tfield`] macro for the +//! complete syntax of fields definitions. =20 /// Trait providing a base address to be added to the offset of a relative= register to obtain /// its actual offset. @@ -54,15 +55,6 @@ pub(crate) trait RegisterBase { /// BOOT_0::alter(&bar, |r| r.set_major_revision(3).set_minor_revision(10)= ); /// ``` /// -/// Fields are defined as follows: -/// -/// - `as ` simply returns the field value casted to , typical= ly `u32`, `u16`, `u8` or -/// `bool`. Note that `bool` fields must have a range of 1 bit. -/// - `as =3D> ` calls ``'s `From::<>` = implementation and returns -/// the result. -/// - `as ?=3D> ` calls ``'s `TryFrom= ::<>` implementation -/// and returns the result. This is useful with fields for which not all= values are valid. -/// /// The documentation strings are optional. If present, they will be added= to the type's /// definition, or the field getter and setter methods they are attached t= o. /// @@ -284,25 +276,25 @@ pub(crate) trait RegisterBase { macro_rules! register { // Creates a register at a fixed offset of the MMIO space. ($name:ident @ $offset:literal $(, $comment:literal)? { $($fields:tt)*= } ) =3D> { - register!(@core $name $(, $comment)? { $($fields)* } ); + bitfield!(struct $name $(, $comment)? { $($fields)* } ); register!(@io_fixed $name @ $offset); }; =20 // Creates an alias register of fixed offset register `alias` with its= own fields. ($name:ident =3D> $alias:ident $(, $comment:literal)? { $($fields:tt)*= } ) =3D> { - register!(@core $name $(, $comment)? { $($fields)* } ); + bitfield!(struct $name $(, $comment)? { $($fields)* } ); register!(@io_fixed $name @ $alias::OFFSET); }; =20 // Creates a register at a relative offset from a base address provide= r. ($name:ident @ $base:ty [ $offset:literal ] $(, $comment:literal)? { $= ($fields:tt)* } ) =3D> { - register!(@core $name $(, $comment)? { $($fields)* } ); + bitfield!(struct $name $(, $comment)? { $($fields)* } ); register!(@io_relative $name @ $base [ $offset ]); }; =20 // Creates an alias register of relative offset register `alias` with = its own fields. ($name:ident =3D> $base:ty [ $alias:ident ] $(, $comment:literal)? { $= ($fields:tt)* }) =3D> { - register!(@core $name $(, $comment)? { $($fields)* } ); + bitfield!(struct $name $(, $comment)? { $($fields)* } ); register!(@io_relative $name @ $base [ $alias::OFFSET ]); }; =20 @@ -313,7 +305,7 @@ macro_rules! register { } ) =3D> { static_assert!(::core::mem::size_of::() <=3D $stride); - register!(@core $name $(, $comment)? { $($fields)* } ); + bitfield!(struct $name $(, $comment)? { $($fields)* } ); register!(@io_array $name @ $offset [ $size ; $stride ]); }; =20 @@ -334,7 +326,7 @@ macro_rules! register { $(, $comment:literal)? { $($fields:tt)* } ) =3D> { static_assert!(::core::mem::size_of::() <=3D $stride); - register!(@core $name $(, $comment)? { $($fields)* } ); + bitfield!(struct $name $(, $comment)? { $($fields)* } ); register!(@io_relative_array $name @ $base [ $offset [ $size ; $st= ride ] ]); }; =20 @@ -356,7 +348,7 @@ macro_rules! register { } ) =3D> { static_assert!($idx < $alias::SIZE); - register!(@core $name $(, $comment)? { $($fields)* } ); + bitfield!(struct $name $(, $comment)? { $($fields)* } ); register!(@io_relative $name @ $base [ $alias::OFFSET + $idx * $al= ias::STRIDE ] ); }; =20 @@ -365,241 +357,10 @@ macro_rules! register { // to avoid it being interpreted in place of the relative register arr= ay alias rule. ($name:ident =3D> $alias:ident [ $idx:expr ] $(, $comment:literal)? { = $($fields:tt)* }) =3D> { static_assert!($idx < $alias::SIZE); - register!(@core $name $(, $comment)? { $($fields)* } ); + bitfield!(struct $name $(, $comment)? { $($fields)* } ); register!(@io_fixed $name @ $alias::OFFSET + $idx * $alias::STRIDE= ); }; =20 - // All rules below are helpers. - - // Defines the wrapper `$name` type, as well as its relevant implement= ations (`Debug`, - // `Default`, `BitOr`, and conversion to the value type) and field acc= essor methods. - (@core $name:ident $(, $comment:literal)? { $($fields:tt)* }) =3D> { - $( - #[doc=3D$comment] - )? - #[repr(transparent)] - #[derive(Clone, Copy)] - pub(crate) struct $name(u32); - - impl ::core::ops::BitOr for $name { - type Output =3D Self; - - fn bitor(self, rhs: Self) -> Self::Output { - Self(self.0 | rhs.0) - } - } - - impl ::core::convert::From<$name> for u32 { - fn from(reg: $name) -> u32 { - reg.0 - } - } - - register!(@fields_dispatcher $name { $($fields)* }); - }; - - // Captures the fields and passes them to all the implementers that re= quire field information. - // - // Used to simplify the matching rules for implementers, so they don't= need to match the entire - // complex fields rule even though they only make use of part of it. - (@fields_dispatcher $name:ident { - $($hi:tt:$lo:tt $field:ident as $type:tt - $(?=3D> $try_into_type:ty)? - $(=3D> $into_type:ty)? - $(, $comment:literal)? - ; - )* - } - ) =3D> { - register!(@field_accessors $name { - $( - $hi:$lo $field as $type - $(?=3D> $try_into_type)? - $(=3D> $into_type)? - $(, $comment)? - ; - )* - }); - register!(@debug $name { $($field;)* }); - register!(@default $name { $($field;)* }); - }; - - // Defines all the field getter/methods methods for `$name`. - ( - @field_accessors $name:ident { - $($hi:tt:$lo:tt $field:ident as $type:tt - $(?=3D> $try_into_type:ty)? - $(=3D> $into_type:ty)? - $(, $comment:literal)? - ; - )* - } - ) =3D> { - $( - register!(@check_field_bounds $hi:$lo $field as $type); - )* - - #[allow(dead_code)] - impl $name { - $( - register!(@field_accessor $name $hi:$lo $field as $type - $(?=3D> $try_into_type)? - $(=3D> $into_type)? - $(, $comment)? - ; - ); - )* - } - }; - - // Boolean fields must have `$hi =3D=3D $lo`. - (@check_field_bounds $hi:tt:$lo:tt $field:ident as bool) =3D> { - #[allow(clippy::eq_op)] - const _: () =3D { - ::kernel::build_assert!( - $hi =3D=3D $lo, - concat!("boolean field `", stringify!($field), "` covers m= ore than one bit") - ); - }; - }; - - // Non-boolean fields must have `$hi >=3D $lo`. - (@check_field_bounds $hi:tt:$lo:tt $field:ident as $type:tt) =3D> { - #[allow(clippy::eq_op)] - const _: () =3D { - ::kernel::build_assert!( - $hi >=3D $lo, - concat!("field `", stringify!($field), "`'s MSB is smaller= than its LSB") - ); - }; - }; - - // Catches fields defined as `bool` and convert them into a boolean va= lue. - ( - @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as bool =3D= > $into_type:ty - $(, $comment:literal)?; - ) =3D> { - register!( - @leaf_accessor $name $hi:$lo $field - { |f| <$into_type>::from(if f !=3D 0 { true } else { false }) } - $into_type =3D> $into_type $(, $comment)?; - ); - }; - - // Shortcut for fields defined as `bool` without the `=3D>` syntax. - ( - @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as bool $(,= $comment:literal)?; - ) =3D> { - register!(@field_accessor $name $hi:$lo $field as bool =3D> bool $= (, $comment)?;); - }; - - // Catches the `?=3D>` syntax for non-boolean fields. - ( - @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as $type:tt= ?=3D> $try_into_type:ty - $(, $comment:literal)?; - ) =3D> { - register!(@leaf_accessor $name $hi:$lo $field - { |f| <$try_into_type>::try_from(f as $type) } $try_into_type = =3D> - ::core::result::Result< - $try_into_type, - <$try_into_type as ::core::convert::TryFrom<$type>>::Error - > - $(, $comment)?;); - }; - - // Catches the `=3D>` syntax for non-boolean fields. - ( - @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as $type:tt= =3D> $into_type:ty - $(, $comment:literal)?; - ) =3D> { - register!(@leaf_accessor $name $hi:$lo $field - { |f| <$into_type>::from(f as $type) } $into_type =3D> $into_t= ype $(, $comment)?;); - }; - - // Shortcut for non-boolean fields defined without the `=3D>` or `?=3D= >` syntax. - ( - @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as $type:tt - $(, $comment:literal)?; - ) =3D> { - register!(@field_accessor $name $hi:$lo $field as $type =3D> $type= $(, $comment)?;); - }; - - // Generates the accessor methods for a single field. - ( - @leaf_accessor $name:ident $hi:tt:$lo:tt $field:ident - { $process:expr } $to_type:ty =3D> $res_type:ty $(, $comment:l= iteral)?; - ) =3D> { - ::kernel::macros::paste!( - const [<$field:upper _RANGE>]: ::core::ops::RangeInclusive =3D= $lo..=3D$hi; - const [<$field:upper _MASK>]: u32 =3D ((((1 << $hi) - 1) << 1) + 1= ) - ((1 << $lo) - 1); - const [<$field:upper _SHIFT>]: u32 =3D Self::[<$field:upper _MASK>= ].trailing_zeros(); - ); - - $( - #[doc=3D"Returns the value of this field:"] - #[doc=3D$comment] - )? - #[inline(always)] - pub(crate) fn $field(self) -> $res_type { - ::kernel::macros::paste!( - const MASK: u32 =3D $name::[<$field:upper _MASK>]; - const SHIFT: u32 =3D $name::[<$field:upper _SHIFT>]; - ); - let field =3D ((self.0 & MASK) >> SHIFT); - - $process(field) - } - - ::kernel::macros::paste!( - $( - #[doc=3D"Sets the value of this field:"] - #[doc=3D$comment] - )? - #[inline(always)] - pub(crate) fn [](mut self, value: $to_type) -> Self { - const MASK: u32 =3D $name::[<$field:upper _MASK>]; - const SHIFT: u32 =3D $name::[<$field:upper _SHIFT>]; - let value =3D (u32::from(value) << SHIFT) & MASK; - self.0 =3D (self.0 & !MASK) | value; - - self - } - ); - }; - - // Generates the `Debug` implementation for `$name`. - (@debug $name:ident { $($field:ident;)* }) =3D> { - impl ::core::fmt::Debug for $name { - fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::f= mt::Result { - f.debug_struct(stringify!($name)) - .field("", &format_args!("{:#x}", &self.0)) - $( - .field(stringify!($field), &self.$field()) - )* - .finish() - } - } - }; - - // Generates the `Default` implementation for `$name`. - (@default $name:ident { $($field:ident;)* }) =3D> { - /// Returns a value for the register where all fields are set to t= heir default value. - impl ::core::default::Default for $name { - fn default() -> Self { - #[allow(unused_mut)] - let mut value =3D Self(Default::default()); - - ::kernel::macros::paste!( - $( - value.[](Default::default()); - )* - ); - - value - } - } - }; - // Generates the IO accessors for a fixed offset register. (@io_fixed $name:ident @ $offset:expr) =3D> { #[allow(dead_code)] --=20 2.34.1 From nobody Tue Dec 16 21:51:00 2025 Received: from DM5PR21CU001.outbound.protection.outlook.com (mail-centralusazon11011038.outbound.protection.outlook.com [52.101.62.38]) (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 30C5528725F; Fri, 3 Oct 2025 15:48:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.62.38 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759506489; cv=fail; b=n1KyRyk7DTpeo3ipUXJVHw3dD08wW/m9cRgQERGUsh41SPUh5/zA3+nnaM/pbCEmdITK2SalO9070TnwN+p4tnwEa4BQphotsI8t20xaXHKNlyAnBCo8ItOagxeyS1E8DiA1DZ9ZhQRKn6vY7y8Y9hWvLCcL2rbOsOc9nDrMTu8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759506489; c=relaxed/simple; bh=gDWZAbE7PGpkyI9O9e7BBDbRtANlHqX354rzjqLDSfM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Content-Type:MIME-Version; b=m8HcmhLNDZqcoN7jX8Mr3B8gyuaga5MhLmciW4PPc1Sy9DegKRLn5ui82ckOaQkb2XKgAuPd2lD7l2gLK1lswWH4e4WxQgu+Mrlmw7Jh+VNWG9dz42AUEwItVR0LG/mNNwRORYpRDBviZRMn88vPt7ch8rtgaVKHP9JZXgvh16k= 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=pgU1ep49; arc=fail smtp.client-ip=52.101.62.38 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="pgU1ep49" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=f5ateWKaZpKywhgrTmslP/WYUBXC4wDWEUrbygQKpoWUrqrORzPfe7Ux5Qk4UTLDyiySXwBwwHJJA8u/tXnqcHqY04K2wdTjiZaVqtcvdm3YtW/XiWxYqmw973HhZcy8nE7ngAryTg9OZMG+XWzhEb0pS/mw6KWVIp70thl0g3UXVx3Xq243KNhvR89q2gtEu1YhiATBYj5/dq5ftp03GgJUWSfv0/KcvJQTGc2Jfdgu7HLk6vj5ZsE7plKDc3ury3GSpIBQEenju9ZpRD4fSU91wvcq7ZSRierKvHdZG6AKEVAvvOrOYVtkI9v8YV0hOT/xje5qdHeTCA1p82aApA== 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=77FTNcbClWOmC09KHfDOzykWUuFW3CunLJr7B7Oaw3E=; b=mOupyrm7nGYcs4cyyc/90zlMyNJ0+fdlJ0JhMzBZosWGN3dZ5urS9LP7DKrgFEm8if77Hyj2KtJDrkIB78jlEB0uKnnDpMl+0noTLCfhuCATpGd70LyvAO0O+jhvD051k4XAzKAPHJahikhWOf9TFBZjH3KzuoUiB7xyXdwg52zuChO1zwY9ycB8p0px7gXCK8GdWqgBLYsM/HplnCnStI5rSRJJZFEdMXjkREY4srpAtcTRtZIuo5+tNJ9/xCT6MWyD9aMPJdXTtfX+hgdubwKfDcrxLT0nlsoT/KTZdr9EvbjsGnYBj/nVRHFzbeJ8hUCD9QFZ3oFTX2BM0aFOaA== 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=77FTNcbClWOmC09KHfDOzykWUuFW3CunLJr7B7Oaw3E=; b=pgU1ep49A2g2Io4Z6kOyvKCEgTjCZwat7mlE/hOB3du0Ya8YEp4kfJtLkKj/JV51jdR0tskVqy2Il03soK1vxI7kHhfIF5i3ssmkvq54jCSw8NKAyiJzYNMoSlgZ4sqtB43hJJ9iYq79nlywoIE9lbQxmPWoiGhuliBrY7uVbuMWan5JbeAXvyoU8EWo6yGnqrVs04m0ZPRL/lCXXG0LMWLbqadKY6lZAqlWdkby9M/MXkxIgtjqrm/LD981EQQKgmHVPT00Yl7FfT/LKHPXZ4w6HVgqEdV/IGDVyDfrDa/WJcQck94CO8B1oophHl8+lFbWS/WxN6pOs5A9EpfBSw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from SN7PR12MB8059.namprd12.prod.outlook.com (2603:10b6:806:32b::7) by PH0PR12MB8152.namprd12.prod.outlook.com (2603:10b6:510:292::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9182.16; Fri, 3 Oct 2025 15:47:57 +0000 Received: from SN7PR12MB8059.namprd12.prod.outlook.com ([fe80::4ee2:654e:1fe8:4b91]) by SN7PR12MB8059.namprd12.prod.outlook.com ([fe80::4ee2:654e:1fe8:4b91%2]) with mapi id 15.20.9160.017; Fri, 3 Oct 2025 15:47:57 +0000 From: Joel Fernandes To: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org, dakr@kernel.org, acourbot@nvidia.com Cc: Alistair Popple , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , bjorn3_gh@protonmail.com, Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , David Airlie , Simona Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , John Hubbard , Joel Fernandes , Timur Tabi , joel@joelfernandes.org, Elle Rhumsaa , Yury Norov , Daniel Almeida , Andrea Righi , nouveau@lists.freedesktop.org Subject: [PATCH v6 2/5] nova-core: bitfield: Add support for different storage widths Date: Fri, 3 Oct 2025 11:47:45 -0400 Message-Id: <20251003154748.1687160-3-joelagnelf@nvidia.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251003154748.1687160-1-joelagnelf@nvidia.com> References: <20251003154748.1687160-1-joelagnelf@nvidia.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: BN9PR03CA0766.namprd03.prod.outlook.com (2603:10b6:408:13a::21) To SN7PR12MB8059.namprd12.prod.outlook.com (2603:10b6:806:32b::7) 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: SN7PR12MB8059:EE_|PH0PR12MB8152:EE_ X-MS-Office365-Filtering-Correlation-Id: d8216167-4dd7-431d-bf33-08de02943921 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|7416014|376014|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?9YTsrBYkh9yzJFVUz6B2UuYbndv6/iLx7ChP2rw4kRyTe/5yGWHzzSaV+f50?= =?us-ascii?Q?+xV0Am9Si4O8UxaLXtJStlzderoy5X2iVJ5NSFtHzWakgDh3xE4t8xmS7UIn?= =?us-ascii?Q?o3xoyMfEww1u6wRrTJjSSXmQbrVigIGe6x75pA+qhWPdzOP4ZOgpNIKntLFA?= =?us-ascii?Q?q9oGAFzsQ8C0OB0PkFhgvStFRxWcLsEu5WSNUca4h2tO5kJyfvmEam/WGLie?= =?us-ascii?Q?gMPIc9rxa9dXYVAhcN+VKmucoJ6tju984pwp3fOkKvp+xgr/aFg1CA0NJZNs?= =?us-ascii?Q?kMjZbWQ6cTzeCiS6rHQNHZlhX8BX0k2hKlL2umAa0Xxp0ixxpmsMOzI3knww?= =?us-ascii?Q?PiIgVjTLlyTEWejq4IwhbYIxcM6A9TQC+p8k/HEP5zjxeTBlfynZJa/F9UXH?= =?us-ascii?Q?2WrYjhwp4vRca3jJVmHnTBhgrqRWpzkB9bdmMXDjcodIznRM5KQaPU5yYydX?= =?us-ascii?Q?oWsk6Smsw39lIehEPjntsfircBVxGVvHiqUGN/seA+ukT4Y3QBxd7y+r0Up4?= =?us-ascii?Q?WRiTBD8t9vJLGrEo1vu1vlsijrJsDjXWE7zSLVYA9TBW/LkSAS0GsM0/y07l?= =?us-ascii?Q?cMi/vkUKt7GqciQOZI12n4Vuz5QQ3Z4Mb3V1I7I+ewZtD55TsizOcHpBLk+L?= =?us-ascii?Q?OfOzGO3dIGF1G+qfNoR+tB6N3wFjXnO0KKvYowC16laxF7yD7L9PZX/dG1DL?= =?us-ascii?Q?Xy6QsmBC/N4vDSsKloHnRitu8+z/luJX60y2uCcI5R86+NuqZlQPQ+AInfWN?= =?us-ascii?Q?bUEW+GPaBp6xOjmcZPkSCbgZq9TloUBGPdDvd21H+gWDC3nGBUd10+Zqay9/?= =?us-ascii?Q?G2+kpfLH5dYlOnYlyFLl3VwB1QMJ+Hfh9bXYapT5KIW7EnSCNdf361l5S5dY?= =?us-ascii?Q?lbhnWvqPatWyBbaWntHBXG2qltkb/R+x0lb2c+q82Cse8oCr21tKOm91gyeh?= =?us-ascii?Q?YB849NDgZ2P4KZeFSc/9aWhU+MhkwcSJ7dqcNH9G7ndWgqRI7/c+Gm/xDwEs?= =?us-ascii?Q?ibDJ67s95b7FLBkd3B/nF00LhaJVHpKTnOcq4zBszqLBOG7RdcWbCVhXklMi?= =?us-ascii?Q?VLQDrBaazFCQq6zRWrZTjcrXveoXlkKxrcygBDoQ1ydQEV9rMLDtkLZNwS+h?= =?us-ascii?Q?yV4LiF7WmnqywOWK/VRA1zyl2qOLtUmyIaKQcJbbjfOvmDX3HxHJcoBrQySa?= =?us-ascii?Q?lAIiuvJAgXQBiGUs9a0ykBIFS8ZK3TmQv0GBdCFTdxwoPuVNXCR8mUHuU8XH?= =?us-ascii?Q?Lfvt4aR9E4p4UTYvTjPSKktmhGwgIs7rvoHkvaXBg626Nz5SC8IAOKpxjQql?= =?us-ascii?Q?DaPA6XjmlZEVKwVWE0TUGMeqxW8FTdc6Tcw38EpB8yoFRtMD5XIiadpRNj7s?= =?us-ascii?Q?5JjQ4dMpKkmMfYBj1quaR6LdCByk8euTmYJhxi3/+1fQhqz1iKPLMseJ18rb?= =?us-ascii?Q?w0y6CaebaBMvJvfJ9NkgSOsD+kzi1KPV?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN7PR12MB8059.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(7416014)(376014)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?CRkRrhAjSA7P2MpYHpUlWLfuTSUVrUTb//901zcTMX/wXT/mp3E/MnP+ST74?= =?us-ascii?Q?bYZzGvJNsP5jfDAGvNBfQOSfwFt1HlTuJgGF237atp87MS9NlRUNBboD9vyT?= =?us-ascii?Q?pwty53RlHdQqFW9bWrFINCW/t3K/MHuSIFC/xfH2bwEwBh5RpoR99DeSmtLa?= =?us-ascii?Q?azuWIUuLsWUQy2kbanWwmhjDiSw337cEZHC/zo+sUl5aGz1e7uiKEKLEzwQU?= =?us-ascii?Q?YZhT8cMLgTNKC15pgTuCxL7USIQ1zjYvVap/rOQ5GzzQCfV0SythcYKNx78k?= =?us-ascii?Q?7DNpm2Vvobx3znxrEUzOE8PDqC7BwLrd7lSD2up1BK5/QpbmMVQ8wCThqbgI?= =?us-ascii?Q?brs8ZNfAjKfxKlrp5T2HFeyv5PmByHT9xaraAR4BLNPl1FXxXtaPbbHS9QDo?= =?us-ascii?Q?VQPEfMfGei+X3voh+s5nQ9KAbRxIQakTQOWbZ+LvvU9We9V6V3QYfiURRzqv?= =?us-ascii?Q?MpnU1jGbhy5BuN2e5RKhoWBhYfzfweqwe/kUlxK8dVlO1RxfYqdjQ4otfsEK?= =?us-ascii?Q?iv0hSU7NCyyqi60gNfPOs/jfhcxmhfo03JN1G7NE+bYazsLpXSy0dW86Mkud?= =?us-ascii?Q?OEox/R2CZp4KNSJMMgLZY7KCTBHoprygjj6rHqtowykzY7AaOJJ/mJ9UPKUB?= =?us-ascii?Q?2TkFmz4xoAMaP6E7QnxMxpC3DN6nPpRYann2xQvMQdlrJeQNAtcF7ipdOoWM?= =?us-ascii?Q?ywCLj6yyhacnCQuOQlrhoBMcjmQhtnAHs4Su+yuLk9o1K1/0TP/PbA15y3Fu?= =?us-ascii?Q?H9leGil7Vqt0Pf1wM+8iWaTtJ0x8tAKuPVRBFhlGJ9Iubb4M6cAu8P4H/Lme?= =?us-ascii?Q?gf3WmpwbnVTVm/1XioCta+vftSUbqQl9UXOl93xiAWqHLf6HrEkMksOSlo9k?= =?us-ascii?Q?OMCZj71IMOUhRf71iik2Q85iigMQFs57QWFokNZ8zjmMhl8WkQf1ET1W18ZL?= =?us-ascii?Q?JDFHPNa2PrHKjdyYU5ipeNWu9DttMpIEO1L7XVdt7ZQg1/vPrFEBsYt9HzUo?= =?us-ascii?Q?cp5/y/R8SUbPbYhrrPK7TeIJSElu+5oCJOszf74mzdeM15NEfQOAAmg0G5d0?= =?us-ascii?Q?qZjTua25tW3/xeOY/94TQXeN0Sg03DXz4FdCfBcVdCZCIt6rLbGxKTdZnX+C?= =?us-ascii?Q?RDT2GSYdgRCHnvifRHps+uGS3plXhaIIsPHjraX0q6FWe0GSxpf2bFoPavpf?= =?us-ascii?Q?4/8fqr1V+CERfns8euUHCMxCZTwoCD/savl09sq1Gov3s1D522Qaxsrz5IAa?= =?us-ascii?Q?6D0b3WTBFT/1ONsE9z4sucfzQQdkgxhGlnRqjuQcOqpKuODXvmO6tEe+MsP5?= =?us-ascii?Q?el5K4GzKZ7Bn7/6VACaSkChULg5Lmyz9gM7DT3QeDLvDUzzD3exvwl47rQ4N?= =?us-ascii?Q?RwmtzTwld2j/ZAxFSQuU7DXxs79G5jkBpr75t5CFEyRiEaN674KXdzWQdUqX?= =?us-ascii?Q?DDwwnhv3Vr/vakEYszc6gfyG/k1lvUbweoWl19i1gHttgempnOyWVYYEMr2a?= =?us-ascii?Q?Sjsmp/WE/41zmw7thNK1ERZHWT7ZvhHDHpqowGSgmRgzUtkcW6Z4irXlKQZ0?= =?us-ascii?Q?IjvbJn+VHrsKgNO3slZEubmql6DONHT7nAHB2iP6?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d8216167-4dd7-431d-bf33-08de02943921 X-MS-Exchange-CrossTenant-AuthSource: SN7PR12MB8059.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Oct 2025 15:47:57.1800 (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: +NbSCBJlZgDehdxA4sSUk/04hXA7NLnSuONWM7Gb/wswXcbh6Fsfcdhg5xaKlMCueH85JD54qZ1VA/IomtOt1g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB8152 Content-Type: text/plain; charset="utf-8" Previously, bitfields were hardcoded to use u32 as the underlying storage type. Add support for different storage types (u8, u16, u32, u64) to the bitfield macro. New syntax is: struct Name() { ... } Reviewed-by: Alexandre Courbot Reviewed-by: Elle Rhumsaa Signed-off-by: Joel Fernandes Reviewed-by: Edwin Peer --- drivers/gpu/nova-core/bitfield.rs | 65 ++++++++++++++++------------ drivers/gpu/nova-core/regs/macros.rs | 16 +++---- 2 files changed, 46 insertions(+), 35 deletions(-) diff --git a/drivers/gpu/nova-core/bitfield.rs b/drivers/gpu/nova-core/bitf= ield.rs index dd0ef2016ff8..75de1c0fcb3b 100644 --- a/drivers/gpu/nova-core/bitfield.rs +++ b/drivers/gpu/nova-core/bitfield.rs @@ -57,7 +57,7 @@ /// } /// /// bitfield! { -/// struct ControlReg { +/// struct ControlReg(u32) { /// 3:0 mode as u8 ?=3D> Mode; /// 7:7 state as bool =3D> State; /// } @@ -67,6 +67,8 @@ /// This generates a struct with: /// - Field accessors: `mode()`, `state()`, etc. /// - Field setters: `set_mode()`, `set_state()`, etc. (supports chaining = with builder pattern). +/// Note that the compiler will error out if the size of the setter's ar= g exceeds the +/// struct's storage size. /// - Debug and Default implementations. /// /// Fields are defined as follows: @@ -79,21 +81,21 @@ /// and returns the result. This is useful with fields for which not all= values are valid. macro_rules! bitfield { // Main entry point - defines the bitfield struct with fields - (struct $name:ident $(, $comment:literal)? { $($fields:tt)* }) =3D> { - bitfield!(@core $name $(, $comment)? { $($fields)* }); + (struct $name:ident($storage:ty) $(, $comment:literal)? { $($fields:tt= )* }) =3D> { + bitfield!(@core $name $storage $(, $comment)? { $($fields)* }); }; =20 // All rules below are helpers. =20 // Defines the wrapper `$name` type, as well as its relevant implement= ations (`Debug`, // `Default`, `BitOr`, and conversion to the value type) and field acc= essor methods. - (@core $name:ident $(, $comment:literal)? { $($fields:tt)* }) =3D> { + (@core $name:ident $storage:ty $(, $comment:literal)? { $($fields:tt)*= }) =3D> { $( #[doc=3D$comment] )? #[repr(transparent)] #[derive(Clone, Copy)] - pub(crate) struct $name(u32); + pub(crate) struct $name($storage); =20 impl ::core::ops::BitOr for $name { type Output =3D Self; @@ -103,20 +105,20 @@ fn bitor(self, rhs: Self) -> Self::Output { } } =20 - impl ::core::convert::From<$name> for u32 { - fn from(val: $name) -> u32 { + impl ::core::convert::From<$name> for $storage { + fn from(val: $name) -> $storage { val.0 } } =20 - bitfield!(@fields_dispatcher $name { $($fields)* }); + bitfield!(@fields_dispatcher $name $storage { $($fields)* }); }; =20 // Captures the fields and passes them to all the implementers that re= quire field information. // // Used to simplify the matching rules for implementers, so they don't= need to match the entire // complex fields rule even though they only make use of part of it. - (@fields_dispatcher $name:ident { + (@fields_dispatcher $name:ident $storage:ty { $($hi:tt:$lo:tt $field:ident as $type:tt $(?=3D> $try_into_type:ty)? $(=3D> $into_type:ty)? @@ -125,7 +127,7 @@ fn from(val: $name) -> u32 { )* } ) =3D> { - bitfield!(@field_accessors $name { + bitfield!(@field_accessors $name $storage { $( $hi:$lo $field as $type $(?=3D> $try_into_type)? @@ -140,7 +142,7 @@ fn from(val: $name) -> u32 { =20 // Defines all the field getter/setter methods for `$name`. ( - @field_accessors $name:ident { + @field_accessors $name:ident $storage:ty { $($hi:tt:$lo:tt $field:ident as $type:tt $(?=3D> $try_into_type:ty)? $(=3D> $into_type:ty)? @@ -156,7 +158,7 @@ fn from(val: $name) -> u32 { #[allow(dead_code)] impl $name { $( - bitfield!(@field_accessor $name $hi:$lo $field as $type + bitfield!(@field_accessor $name $storage, $hi:$lo $field as $t= ype $(?=3D> $try_into_type)? $(=3D> $into_type)? $(, $comment)? @@ -190,11 +192,11 @@ impl $name { =20 // Catches fields defined as `bool` and convert them into a boolean va= lue. ( - @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as bool =3D= > $into_type:ty + @field_accessor $name:ident $storage:ty, $hi:tt:$lo:tt $field:iden= t as bool =3D> $into_type:ty $(, $comment:literal)?; ) =3D> { bitfield!( - @leaf_accessor $name $hi:$lo $field + @leaf_accessor $name $storage, $hi:$lo $field { |f| <$into_type>::from(if f !=3D 0 { true } else { false }) } $into_type =3D> $into_type $(, $comment)?; ); @@ -202,17 +204,17 @@ impl $name { =20 // Shortcut for fields defined as `bool` without the `=3D>` syntax. ( - @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as bool $(,= $comment:literal)?; + @field_accessor $name:ident $storage:ty, $hi:tt:$lo:tt $field:iden= t as bool $(, $comment:literal)?; ) =3D> { - bitfield!(@field_accessor $name $hi:$lo $field as bool =3D> bool $= (, $comment)?;); + bitfield!(@field_accessor $name $storage, $hi:$lo $field as bool = =3D> bool $(, $comment)?;); }; =20 // Catches the `?=3D>` syntax for non-boolean fields. ( - @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as $type:tt= ?=3D> $try_into_type:ty + @field_accessor $name:ident $storage:ty, $hi:tt:$lo:tt $field:iden= t as $type:tt ?=3D> $try_into_type:ty $(, $comment:literal)?; ) =3D> { - bitfield!(@leaf_accessor $name $hi:$lo $field + bitfield!(@leaf_accessor $name $storage, $hi:$lo $field { |f| <$try_into_type>::try_from(f as $type) } $try_into_type = =3D> ::core::result::Result< $try_into_type, @@ -223,29 +225,38 @@ impl $name { =20 // Catches the `=3D>` syntax for non-boolean fields. ( - @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as $type:tt= =3D> $into_type:ty + @field_accessor $name:ident $storage:ty, $hi:tt:$lo:tt $field:iden= t as $type:tt =3D> $into_type:ty $(, $comment:literal)?; ) =3D> { - bitfield!(@leaf_accessor $name $hi:$lo $field + bitfield!(@leaf_accessor $name $storage, $hi:$lo $field { |f| <$into_type>::from(f as $type) } $into_type =3D> $into_t= ype $(, $comment)?;); }; =20 // Shortcut for non-boolean fields defined without the `=3D>` or `?=3D= >` syntax. ( - @field_accessor $name:ident $hi:tt:$lo:tt $field:ident as $type:tt + @field_accessor $name:ident $storage:ty, $hi:tt:$lo:tt $field:iden= t as $type:tt $(, $comment:literal)?; ) =3D> { - bitfield!(@field_accessor $name $hi:$lo $field as $type =3D> $type= $(, $comment)?;); + bitfield!(@field_accessor $name $storage, $hi:$lo $field as $type = =3D> $type $(, $comment)?;); }; =20 // Generates the accessor methods for a single field. ( - @leaf_accessor $name:ident $hi:tt:$lo:tt $field:ident + @leaf_accessor $name:ident $storage:ty, $hi:tt:$lo:tt $field:ident { $process:expr } $to_type:ty =3D> $res_type:ty $(, $comment:l= iteral)?; ) =3D> { ::kernel::macros::paste!( const [<$field:upper _RANGE>]: ::core::ops::RangeInclusive =3D= $lo..=3D$hi; - const [<$field:upper _MASK>]: u32 =3D ((((1 << $hi) - 1) << 1) + 1= ) - ((1 << $lo) - 1); + const [<$field:upper _MASK>]: $storage =3D { + // Generate mask for shifting + match ::core::mem::size_of::<$storage>() { + 1 =3D> ::kernel::bits::genmask_u8($lo..=3D$hi) as $storage, + 2 =3D> ::kernel::bits::genmask_u16($lo..=3D$hi) as $storag= e, + 4 =3D> ::kernel::bits::genmask_u32($lo..=3D$hi) as $storag= e, + 8 =3D> ::kernel::bits::genmask_u64($lo..=3D$hi) as $storag= e, + _ =3D> ::kernel::build_error!("Unsupported storage type si= ze") + } + }; const [<$field:upper _SHIFT>]: u32 =3D Self::[<$field:upper _MASK>= ].trailing_zeros(); ); =20 @@ -256,7 +267,7 @@ impl $name { #[inline(always)] pub(crate) fn $field(self) -> $res_type { ::kernel::macros::paste!( - const MASK: u32 =3D $name::[<$field:upper _MASK>]; + const MASK: $storage =3D $name::[<$field:upper _MASK>]; const SHIFT: u32 =3D $name::[<$field:upper _SHIFT>]; ); let field =3D ((self.0 & MASK) >> SHIFT); @@ -271,9 +282,9 @@ pub(crate) fn $field(self) -> $res_type { )? #[inline(always)] pub(crate) fn [](mut self, value: $to_type) -> Self { - const MASK: u32 =3D $name::[<$field:upper _MASK>]; + const MASK: $storage =3D $name::[<$field:upper _MASK>]; const SHIFT: u32 =3D $name::[<$field:upper _SHIFT>]; - let value =3D (u32::from(value) << SHIFT) & MASK; + let value =3D (<$storage>::from(value) << SHIFT) & MASK; self.0 =3D (self.0 & !MASK) | value; =20 self diff --git a/drivers/gpu/nova-core/regs/macros.rs b/drivers/gpu/nova-core/r= egs/macros.rs index 945d15a2c529..ffd7d5cb73bb 100644 --- a/drivers/gpu/nova-core/regs/macros.rs +++ b/drivers/gpu/nova-core/regs/macros.rs @@ -276,25 +276,25 @@ pub(crate) trait RegisterBase { macro_rules! register { // Creates a register at a fixed offset of the MMIO space. ($name:ident @ $offset:literal $(, $comment:literal)? { $($fields:tt)*= } ) =3D> { - bitfield!(struct $name $(, $comment)? { $($fields)* } ); + bitfield!(struct $name(u32) $(, $comment)? { $($fields)* } ); register!(@io_fixed $name @ $offset); }; =20 // Creates an alias register of fixed offset register `alias` with its= own fields. ($name:ident =3D> $alias:ident $(, $comment:literal)? { $($fields:tt)*= } ) =3D> { - bitfield!(struct $name $(, $comment)? { $($fields)* } ); + bitfield!(struct $name(u32) $(, $comment)? { $($fields)* } ); register!(@io_fixed $name @ $alias::OFFSET); }; =20 // Creates a register at a relative offset from a base address provide= r. ($name:ident @ $base:ty [ $offset:literal ] $(, $comment:literal)? { $= ($fields:tt)* } ) =3D> { - bitfield!(struct $name $(, $comment)? { $($fields)* } ); + bitfield!(struct $name(u32) $(, $comment)? { $($fields)* } ); register!(@io_relative $name @ $base [ $offset ]); }; =20 // Creates an alias register of relative offset register `alias` with = its own fields. ($name:ident =3D> $base:ty [ $alias:ident ] $(, $comment:literal)? { $= ($fields:tt)* }) =3D> { - bitfield!(struct $name $(, $comment)? { $($fields)* } ); + bitfield!(struct $name(u32) $(, $comment)? { $($fields)* } ); register!(@io_relative $name @ $base [ $alias::OFFSET ]); }; =20 @@ -305,7 +305,7 @@ macro_rules! register { } ) =3D> { static_assert!(::core::mem::size_of::() <=3D $stride); - bitfield!(struct $name $(, $comment)? { $($fields)* } ); + bitfield!(struct $name(u32) $(, $comment)? { $($fields)* } ); register!(@io_array $name @ $offset [ $size ; $stride ]); }; =20 @@ -326,7 +326,7 @@ macro_rules! register { $(, $comment:literal)? { $($fields:tt)* } ) =3D> { static_assert!(::core::mem::size_of::() <=3D $stride); - bitfield!(struct $name $(, $comment)? { $($fields)* } ); + bitfield!(struct $name(u32) $(, $comment)? { $($fields)* } ); register!(@io_relative_array $name @ $base [ $offset [ $size ; $st= ride ] ]); }; =20 @@ -348,7 +348,7 @@ macro_rules! register { } ) =3D> { static_assert!($idx < $alias::SIZE); - bitfield!(struct $name $(, $comment)? { $($fields)* } ); + bitfield!(struct $name(u32) $(, $comment)? { $($fields)* } ); register!(@io_relative $name @ $base [ $alias::OFFSET + $idx * $al= ias::STRIDE ] ); }; =20 @@ -357,7 +357,7 @@ macro_rules! register { // to avoid it being interpreted in place of the relative register arr= ay alias rule. ($name:ident =3D> $alias:ident [ $idx:expr ] $(, $comment:literal)? { = $($fields:tt)* }) =3D> { static_assert!($idx < $alias::SIZE); - bitfield!(struct $name $(, $comment)? { $($fields)* } ); + bitfield!(struct $name(u32) $(, $comment)? { $($fields)* } ); register!(@io_fixed $name @ $alias::OFFSET + $idx * $alias::STRIDE= ); }; =20 --=20 2.34.1 From nobody Tue Dec 16 21:51:00 2025 Received: from DM5PR21CU001.outbound.protection.outlook.com (mail-centralusazon11011038.outbound.protection.outlook.com [52.101.62.38]) (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 EFCEA28751B; Fri, 3 Oct 2025 15:48:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.62.38 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759506493; cv=fail; b=CaGY+Xwbo0pj6T/qKVtvu+XBkIYfgh3AR+yR51jpDMJqo3z75ulclcsrvon9UkRyucjHIXWLxfEjVzxaYw071Ee3jkHxoMgfNNnqLLorq94fN/5J6ydaStuJtGh+3ECTwIFkhk+OcVpgLvx83xtfSxoNS1mWQ9GxLzoCv0XF46w= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759506493; c=relaxed/simple; bh=nOnRd2EX88BCR4MT9YisoEXoBcPGYW4ZBGP6WnoXDE0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Content-Type:MIME-Version; b=PUb1G84UjKlTwbWIHb9g7toAQIVD+N/iq2DdQvrS3aTjNag+0efuUYx7JWtBZna1QiTMQDCu4JhLBrULvd9XABBiTj0gaJu5m7A/6MFeblH4QXPzIGZP6WqAr23qAin0vcNnWPUnC2pOpKX6jsoxHU97fyzkkhdEC/HWag49ahs= 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=lz/ZBOUC; arc=fail smtp.client-ip=52.101.62.38 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="lz/ZBOUC" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=D+8LvHn89dENH8Q/e+XYDFEfnepJ/fBFBauLKPEnMdrlXtJajXu5AKY15KJWVQ3osth3yJTSbo68XKqn/a7o8rjpQt3f8Y5NnBVinFZ8rQF/ccNEqoU9kABmsxXJY78KRHi8DPxywCq99Ecy02vqGIUrZdlDgIYTqmPSdsRk4N+99aiukE+JfauxscV3lDTy+jTu3vHooVzeJSYPNqYGsr4cIl98BtcoPDq7YF4HP8QEmU6KPo9ufS2BlUbTDl/tKoNa/Zqw4Yh0nn6mvAW5Nd1+VuofuoPA4IGbrM5JqhyCSKHpmab4J5PmbSECk5tiuN7h3rf15O9HdmKqnh3fQg== 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=7WtHuucVr0mioG8hMK3CYr/acoECPocCMbDaWYL6yQc=; b=NS8tGEAcNWrYwbB3Joglp7GHOmWt/BOccINr3SX5s/Tc7nvBClgW0AfW93BViCvcq38ErW8lOhkJvhIIF90oqjJ40gLLgzIURW1fUMPPRbCIdAhxpy5o4rgFEBdUdjrtWodfUrwBFgKyMZ3WsTV9EpCaVM3A0c8YGtYEZgndyC+B1C5X/YLaEHkCWu0vjPgB8HnPuoxzaH7LTWg/N6kapLE16ysDt5Z83EURwrI3jsuTZ2ppp1u19OnTgVxuHUXXRKW3kTe8eXHnT+yWDZhh3E4QfaafRmzLvvoRr/cQs57DlJKZLGGD7KlLdW4t4JJgL7Uq+xeUpX4xMpvoe+enKw== 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=7WtHuucVr0mioG8hMK3CYr/acoECPocCMbDaWYL6yQc=; b=lz/ZBOUCAuPfuRb0uzs45Ofhz1J5oWFhBwB39Ig8zWlVJNR38elMwqZ/ziw/VXzoQ55D2JOd4QZb9vYyrFDgMPJNMFmmo53o1Ox9WGgDQnCaky0nBtwMVtsnYZrLuQf1GaDNZgiWe6LtDn+usmQVWxFMjjrwsXxzJTZFy/RVCTeQr+CfoPv79bUM1T3JZBU6hib4h/EaGq2Y8H15vunaxWMTMBCqToy74axwatsJcuqOJw3sK1XHPJg68mS+jQ7rulP714NNJHM/YX84FGucnXgwJEloPiC0jRY+aODj+OEvu3U3YP3sc4cWNG1UFtR6JDpnkMr8eW3aw2WKuBA92A== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from SN7PR12MB8059.namprd12.prod.outlook.com (2603:10b6:806:32b::7) by PH0PR12MB8152.namprd12.prod.outlook.com (2603:10b6:510:292::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9182.16; Fri, 3 Oct 2025 15:47:59 +0000 Received: from SN7PR12MB8059.namprd12.prod.outlook.com ([fe80::4ee2:654e:1fe8:4b91]) by SN7PR12MB8059.namprd12.prod.outlook.com ([fe80::4ee2:654e:1fe8:4b91%2]) with mapi id 15.20.9160.017; Fri, 3 Oct 2025 15:47:59 +0000 From: Joel Fernandes To: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org, dakr@kernel.org, acourbot@nvidia.com Cc: Alistair Popple , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , bjorn3_gh@protonmail.com, Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , David Airlie , Simona Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , John Hubbard , Joel Fernandes , Timur Tabi , joel@joelfernandes.org, Elle Rhumsaa , Yury Norov , Daniel Almeida , Andrea Righi , nouveau@lists.freedesktop.org Subject: [PATCH v6 3/5] nova-core: bitfield: Add support for custom visiblity Date: Fri, 3 Oct 2025 11:47:46 -0400 Message-Id: <20251003154748.1687160-4-joelagnelf@nvidia.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251003154748.1687160-1-joelagnelf@nvidia.com> References: <20251003154748.1687160-1-joelagnelf@nvidia.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: BN9P223CA0024.NAMP223.PROD.OUTLOOK.COM (2603:10b6:408:10b::29) To SN7PR12MB8059.namprd12.prod.outlook.com (2603:10b6:806:32b::7) 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: SN7PR12MB8059:EE_|PH0PR12MB8152:EE_ X-MS-Office365-Filtering-Correlation-Id: 088bd8cd-c219-4822-ee74-08de02943a55 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|7416014|376014|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?0j6PKnmBa6hyVeLasbw/li9Qpi4T9BcoFIRL+dsVRxqJQy1w+qHSclDbgujk?= =?us-ascii?Q?LC5ny2L0byG4PreHHNcdcchHbfDPDrXuw0KtdpcTpgh+6po+GeQ4vyV3cJtk?= =?us-ascii?Q?JICPgKMiT2U1utvXQv+6VcKu05rhrviBD20CxfliXGE+qPsd4T37DSGIJJtK?= =?us-ascii?Q?hqIzWEUWfTjyXYuXbfkXbOT04FDRnH/yRUnzdPG/YYW4XfxsmHfh4w9x+W5S?= =?us-ascii?Q?U7pSW5FDp0vHn5zN0PZnXtF0wE6a/Kog9CzD41pkbahmKLeNJBZbWWTFjOKo?= =?us-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PONC7dl5k7JeBiQUJdq8XdhLaKh?= =?us-ascii?Q?atVIg5eLPKK22o7PVdYbKo9Ri1FhXrp0r2l8RcRUJb5GhVO/0G28CiR8VO+3?= =?us-ascii?Q?x1IFssGfldNkOOGnPLZibQovrKbAI8vJa79BHIr9Jno44hk0Mz6EcGFkWwSG?= =?us-ascii?Q?ZADHFFSOUhvV5q6NbR1pZRA0qeUDYDEZTWe9ARSBoiIa1e3pDbR03KxspdYN?= =?us-ascii?Q?KvZwPvd4JV9qL2Mf6JRKxeBQ1T5urRhxX28CCNkmg1NIrk4/4LxO4m4jWUT+?= =?us-ascii?Q?GrE2qcjRqKNPtghUvJnUd/NKOgftSTx7?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN7PR12MB8059.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(7416014)(376014)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?e+zkdSFWI1WB/Q3OUsGiHwjV5sNUe3sdBC8TzFgXeMXa9M+3C8kmrWb6btnf?= =?us-ascii?Q?Dw5KNpncWoueL0C1/M6S952AtUmU7k079B0E7lOuqcayAuGmh3FNGFFDzr5p?= =?us-ascii?Q?DBQ8ThS4FZtPj2LxFcsN6J+ZsLfnth+9uWTkkBZX7Qwsq/UEs/rTxn+J88Dd?= =?us-ascii?Q?j4L3wtYu9buF5ZMW8pezY3Ag8R5W/HJBZZM2yT1r2o+YCxuIC+nV4lQlqGAO?= =?us-ascii?Q?d4v1x5BbDEeIziZlztwYcRHxJUtawxVMGshsk5vSI2miEClKJNU1i0uxDw9Y?= =?us-ascii?Q?8ybZ8y3uTeFdWLrbQ9jfqQ8DeU8fhbpXHBjsMPlO0WTU8JG6peCAmUOKPNF1?= =?us-ascii?Q?phYOA0VbZItE+pu2C6BG4ZC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?= =?us-ascii?Q?TCyOS3VPzjXu9+XXzl6vy4JcHkZDFNB1S7JRzRNzDEoa8eKOKGMV2Q2ro8yU?= =?us-ascii?Q?vWy3auREiQrzn3fQ+5pk5etx7RGtlETbBZQD+2tHS18wXSu5jUR4lTjRwJRQ?= =?us-ascii?Q?oC8RGWLw6abqwywb2xkJd2gbJz2bUXzl4v6MqlCszb4is3jzWZ/nBPqwhX4F?= =?us-ascii?Q?1NhbjyP2vxZ4uTPdMdMRmrW7r+1LtF/GS2YWYf55WXY4/VSffbL4uHjWiMwO?= =?us-ascii?Q?8uSgYPZL7q7GcFuPuSr6r601mJOq8u3SUq7dOa8Z+Q3YVhEqYQf+BkjMa/75?= =?us-ascii?Q?8ZqmQX1dLAK5BBshA9VCFr1OyAGVTcrcJrowNBow?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 088bd8cd-c219-4822-ee74-08de02943a55 X-MS-Exchange-CrossTenant-AuthSource: SN7PR12MB8059.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Oct 2025 15:47:59.2386 (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: aP1Zl45JViJHhmgKJmwIxLbnp1A5b8Y5C9C27qvfVVpnKsaJ7HZAEm97H4a0sxPfXzYZudyluq/jMpSN2Md2CA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB8152 Content-Type: text/plain; charset="utf-8" Add support for custom visiblity to allow for users to control visibility of the structure and helpers. Reviewed-by: Alexandre Courbot Reviewed-by: Elle Rhumsaa Signed-off-by: Joel Fernandes Reviewed-by: Edwin Peer --- drivers/gpu/nova-core/bitfield.rs | 49 +++++++++++++++------------- drivers/gpu/nova-core/regs/macros.rs | 16 ++++----- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/drivers/gpu/nova-core/bitfield.rs b/drivers/gpu/nova-core/bitf= ield.rs index 75de1c0fcb3b..cbedbb0078f6 100644 --- a/drivers/gpu/nova-core/bitfield.rs +++ b/drivers/gpu/nova-core/bitfield.rs @@ -57,7 +57,7 @@ /// } /// /// bitfield! { -/// struct ControlReg(u32) { +/// pub struct ControlReg(u32) { /// 3:0 mode as u8 ?=3D> Mode; /// 7:7 state as bool =3D> State; /// } @@ -71,6 +71,9 @@ /// struct's storage size. /// - Debug and Default implementations. /// +/// Note: Field accessors and setters inherit the same visibility as the s= truct itself. +/// In the example above, both `mode()` and `set_mode()` methods will be `= pub`. +/// /// Fields are defined as follows: /// /// - `as ` simply returns the field value casted to , typical= ly `u32`, `u16`, `u8` or @@ -81,21 +84,21 @@ /// and returns the result. This is useful with fields for which not all= values are valid. macro_rules! bitfield { // Main entry point - defines the bitfield struct with fields - (struct $name:ident($storage:ty) $(, $comment:literal)? { $($fields:tt= )* }) =3D> { - bitfield!(@core $name $storage $(, $comment)? { $($fields)* }); + ($vis:vis struct $name:ident($storage:ty) $(, $comment:literal)? { $($= fields:tt)* }) =3D> { + bitfield!(@core $vis $name $storage $(, $comment)? { $($fields)* }= ); }; =20 // All rules below are helpers. =20 // Defines the wrapper `$name` type, as well as its relevant implement= ations (`Debug`, // `Default`, `BitOr`, and conversion to the value type) and field acc= essor methods. - (@core $name:ident $storage:ty $(, $comment:literal)? { $($fields:tt)*= }) =3D> { + (@core $vis:vis $name:ident $storage:ty $(, $comment:literal)? { $($fi= elds:tt)* }) =3D> { $( #[doc=3D$comment] )? #[repr(transparent)] #[derive(Clone, Copy)] - pub(crate) struct $name($storage); + $vis struct $name($storage); =20 impl ::core::ops::BitOr for $name { type Output =3D Self; @@ -111,14 +114,14 @@ fn from(val: $name) -> $storage { } } =20 - bitfield!(@fields_dispatcher $name $storage { $($fields)* }); + bitfield!(@fields_dispatcher $vis $name $storage { $($fields)* }); }; =20 // Captures the fields and passes them to all the implementers that re= quire field information. // // Used to simplify the matching rules for implementers, so they don't= need to match the entire // complex fields rule even though they only make use of part of it. - (@fields_dispatcher $name:ident $storage:ty { + (@fields_dispatcher $vis:vis $name:ident $storage:ty { $($hi:tt:$lo:tt $field:ident as $type:tt $(?=3D> $try_into_type:ty)? $(=3D> $into_type:ty)? @@ -127,7 +130,7 @@ fn from(val: $name) -> $storage { )* } ) =3D> { - bitfield!(@field_accessors $name $storage { + bitfield!(@field_accessors $vis $name $storage { $( $hi:$lo $field as $type $(?=3D> $try_into_type)? @@ -142,7 +145,7 @@ fn from(val: $name) -> $storage { =20 // Defines all the field getter/setter methods for `$name`. ( - @field_accessors $name:ident $storage:ty { + @field_accessors $vis:vis $name:ident $storage:ty { $($hi:tt:$lo:tt $field:ident as $type:tt $(?=3D> $try_into_type:ty)? $(=3D> $into_type:ty)? @@ -158,7 +161,7 @@ fn from(val: $name) -> $storage { #[allow(dead_code)] impl $name { $( - bitfield!(@field_accessor $name $storage, $hi:$lo $field as $t= ype + bitfield!(@field_accessor $vis $name $storage, $hi:$lo $field = as $type $(?=3D> $try_into_type)? $(=3D> $into_type)? $(, $comment)? @@ -192,11 +195,11 @@ impl $name { =20 // Catches fields defined as `bool` and convert them into a boolean va= lue. ( - @field_accessor $name:ident $storage:ty, $hi:tt:$lo:tt $field:iden= t as bool =3D> $into_type:ty + @field_accessor $vis:vis $name:ident $storage:ty, $hi:tt:$lo:tt $f= ield:ident as bool =3D> $into_type:ty $(, $comment:literal)?; ) =3D> { bitfield!( - @leaf_accessor $name $storage, $hi:$lo $field + @leaf_accessor $vis $name $storage, $hi:$lo $field { |f| <$into_type>::from(if f !=3D 0 { true } else { false }) } $into_type =3D> $into_type $(, $comment)?; ); @@ -204,17 +207,17 @@ impl $name { =20 // Shortcut for fields defined as `bool` without the `=3D>` syntax. ( - @field_accessor $name:ident $storage:ty, $hi:tt:$lo:tt $field:iden= t as bool $(, $comment:literal)?; + @field_accessor $vis:vis $name:ident $storage:ty, $hi:tt:$lo:tt $f= ield:ident as bool $(, $comment:literal)?; ) =3D> { - bitfield!(@field_accessor $name $storage, $hi:$lo $field as bool = =3D> bool $(, $comment)?;); + bitfield!(@field_accessor $vis $name $storage, $hi:$lo $field as b= ool =3D> bool $(, $comment)?;); }; =20 // Catches the `?=3D>` syntax for non-boolean fields. ( - @field_accessor $name:ident $storage:ty, $hi:tt:$lo:tt $field:iden= t as $type:tt ?=3D> $try_into_type:ty + @field_accessor $vis:vis $name:ident $storage:ty, $hi:tt:$lo:tt $f= ield:ident as $type:tt ?=3D> $try_into_type:ty $(, $comment:literal)?; ) =3D> { - bitfield!(@leaf_accessor $name $storage, $hi:$lo $field + bitfield!(@leaf_accessor $vis $name $storage, $hi:$lo $field { |f| <$try_into_type>::try_from(f as $type) } $try_into_type = =3D> ::core::result::Result< $try_into_type, @@ -225,24 +228,24 @@ impl $name { =20 // Catches the `=3D>` syntax for non-boolean fields. ( - @field_accessor $name:ident $storage:ty, $hi:tt:$lo:tt $field:iden= t as $type:tt =3D> $into_type:ty + @field_accessor $vis:vis $name:ident $storage:ty, $hi:tt:$lo:tt $f= ield:ident as $type:tt =3D> $into_type:ty $(, $comment:literal)?; ) =3D> { - bitfield!(@leaf_accessor $name $storage, $hi:$lo $field + bitfield!(@leaf_accessor $vis $name $storage, $hi:$lo $field { |f| <$into_type>::from(f as $type) } $into_type =3D> $into_t= ype $(, $comment)?;); }; =20 // Shortcut for non-boolean fields defined without the `=3D>` or `?=3D= >` syntax. ( - @field_accessor $name:ident $storage:ty, $hi:tt:$lo:tt $field:iden= t as $type:tt + @field_accessor $vis:vis $name:ident $storage:ty, $hi:tt:$lo:tt $f= ield:ident as $type:tt $(, $comment:literal)?; ) =3D> { - bitfield!(@field_accessor $name $storage, $hi:$lo $field as $type = =3D> $type $(, $comment)?;); + bitfield!(@field_accessor $vis $name $storage, $hi:$lo $field as $= type =3D> $type $(, $comment)?;); }; =20 // Generates the accessor methods for a single field. ( - @leaf_accessor $name:ident $storage:ty, $hi:tt:$lo:tt $field:ident + @leaf_accessor $vis:vis $name:ident $storage:ty, $hi:tt:$lo:tt $fi= eld:ident { $process:expr } $to_type:ty =3D> $res_type:ty $(, $comment:l= iteral)?; ) =3D> { ::kernel::macros::paste!( @@ -265,7 +268,7 @@ impl $name { #[doc=3D$comment] )? #[inline(always)] - pub(crate) fn $field(self) -> $res_type { + $vis fn $field(self) -> $res_type { ::kernel::macros::paste!( const MASK: $storage =3D $name::[<$field:upper _MASK>]; const SHIFT: u32 =3D $name::[<$field:upper _SHIFT>]; @@ -281,7 +284,7 @@ pub(crate) fn $field(self) -> $res_type { #[doc=3D$comment] )? #[inline(always)] - pub(crate) fn [](mut self, value: $to_type) -> Self { + $vis fn [](mut self, value: $to_type) -> Self { const MASK: $storage =3D $name::[<$field:upper _MASK>]; const SHIFT: u32 =3D $name::[<$field:upper _SHIFT>]; let value =3D (<$storage>::from(value) << SHIFT) & MASK; diff --git a/drivers/gpu/nova-core/regs/macros.rs b/drivers/gpu/nova-core/r= egs/macros.rs index ffd7d5cb73bb..c0a5194e8d97 100644 --- a/drivers/gpu/nova-core/regs/macros.rs +++ b/drivers/gpu/nova-core/regs/macros.rs @@ -276,25 +276,25 @@ pub(crate) trait RegisterBase { macro_rules! register { // Creates a register at a fixed offset of the MMIO space. ($name:ident @ $offset:literal $(, $comment:literal)? { $($fields:tt)*= } ) =3D> { - bitfield!(struct $name(u32) $(, $comment)? { $($fields)* } ); + bitfield!(pub(crate) struct $name(u32) $(, $comment)? { $($fields)= * } ); register!(@io_fixed $name @ $offset); }; =20 // Creates an alias register of fixed offset register `alias` with its= own fields. ($name:ident =3D> $alias:ident $(, $comment:literal)? { $($fields:tt)*= } ) =3D> { - bitfield!(struct $name(u32) $(, $comment)? { $($fields)* } ); + bitfield!(pub(crate) struct $name(u32) $(, $comment)? { $($fields)= * } ); register!(@io_fixed $name @ $alias::OFFSET); }; =20 // Creates a register at a relative offset from a base address provide= r. ($name:ident @ $base:ty [ $offset:literal ] $(, $comment:literal)? { $= ($fields:tt)* } ) =3D> { - bitfield!(struct $name(u32) $(, $comment)? { $($fields)* } ); + bitfield!(pub(crate) struct $name(u32) $(, $comment)? { $($fields)= * } ); register!(@io_relative $name @ $base [ $offset ]); }; =20 // Creates an alias register of relative offset register `alias` with = its own fields. ($name:ident =3D> $base:ty [ $alias:ident ] $(, $comment:literal)? { $= ($fields:tt)* }) =3D> { - bitfield!(struct $name(u32) $(, $comment)? { $($fields)* } ); + bitfield!(pub(crate) struct $name(u32) $(, $comment)? { $($fields)= * } ); register!(@io_relative $name @ $base [ $alias::OFFSET ]); }; =20 @@ -305,7 +305,7 @@ macro_rules! register { } ) =3D> { static_assert!(::core::mem::size_of::() <=3D $stride); - bitfield!(struct $name(u32) $(, $comment)? { $($fields)* } ); + bitfield!(pub(crate) struct $name(u32) $(, $comment)? { $($fields)= * } ); register!(@io_array $name @ $offset [ $size ; $stride ]); }; =20 @@ -326,7 +326,7 @@ macro_rules! register { $(, $comment:literal)? { $($fields:tt)* } ) =3D> { static_assert!(::core::mem::size_of::() <=3D $stride); - bitfield!(struct $name(u32) $(, $comment)? { $($fields)* } ); + bitfield!(pub(crate) struct $name(u32) $(, $comment)? { $($fields)= * } ); register!(@io_relative_array $name @ $base [ $offset [ $size ; $st= ride ] ]); }; =20 @@ -348,7 +348,7 @@ macro_rules! register { } ) =3D> { static_assert!($idx < $alias::SIZE); - bitfield!(struct $name(u32) $(, $comment)? { $($fields)* } ); + bitfield!(pub(crate) struct $name(u32) $(, $comment)? { $($fields)= * } ); register!(@io_relative $name @ $base [ $alias::OFFSET + $idx * $al= ias::STRIDE ] ); }; =20 @@ -357,7 +357,7 @@ macro_rules! register { // to avoid it being interpreted in place of the relative register arr= ay alias rule. ($name:ident =3D> $alias:ident [ $idx:expr ] $(, $comment:literal)? { = $($fields:tt)* }) =3D> { static_assert!($idx < $alias::SIZE); - bitfield!(struct $name(u32) $(, $comment)? { $($fields)* } ); + bitfield!(pub(crate) struct $name(u32) $(, $comment)? { $($fields)= * } ); register!(@io_fixed $name @ $alias::OFFSET + $idx * $alias::STRIDE= ); }; =20 --=20 2.34.1 From nobody Tue Dec 16 21:51:00 2025 Received: from DM5PR21CU001.outbound.protection.outlook.com (mail-centralusazon11011038.outbound.protection.outlook.com [52.101.62.38]) (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 16EE32882AC; Fri, 3 Oct 2025 15:48:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.62.38 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759506496; cv=fail; b=jhvFkO3E+s8Qm3iVbwgMmtSfqZnWlMbB7laELQBPxy+Pm2CzPyXmXXaDtmyrTMCUuvDld3cTVt90xcVnLD+nfzLiWrz+BJSVFGDTgl68ENNmfxf2BVXjVmK5xOOiVW07KazEioXvXoaRyxuqxZ0i7gfBHXWIHtGdmUtZFkQWXHY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759506496; c=relaxed/simple; bh=8ERO1rd0nrFfQf7BiM4W4thjP2qdF0lFQu4NQ7P4N84=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Content-Type:MIME-Version; b=jlwWDDcA8kI2aLSNLSXF7m9z1h4gFd/JkJRDfbUTGlDCQ09X606DjnznHgCXhie3oj18DLPbsrRqYddccQ5RoN/GTe6OAl2DO0ivi1LFblHFxE7vp3VUK9NzxHGvH4TNT4lguL7BAvPwaHGI83nrXC1zMSfzaanHUR2pGRs+PmU= 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=dmqhBFSb; arc=fail smtp.client-ip=52.101.62.38 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="dmqhBFSb" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=j68Es+gorUStgmlcSPyYwhep6mwccuc1EyemYvd5gStBcSzXDwSvUD/IfDnMkiVwXMXs2RVSpiWiZFeAVjcc6aWhdJ03cS62sQWPC00qhpg1mzk/vTdVvv62/EOvJ3GF0B6t+V3O477ApF09en6FZxp44CdjkkDKJHCEBNCaRWqXdM7A/+KP83w3A0qo0qwAFWTEWIjv0WlmNF+2SiDtKbUj5z5aKiGIbKl4KTRZ5Nt/GBh1ILnKA11igdNdjDUNln4F0KOpyBJaAavol7+ksUtFiIFuu30DgSjiON6+0vQOxux1HP2H1X1I+0J/l6GzNwBu0kM/kOXg1zPgGziZYA== 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=HYkGffdK/7EZ4eJV/r/RfWgSSUu2+ttVya1KH/5bFNc=; b=I/qwEFqVetOuSfDTwqYU3T7ovlllELjd0yhwCI0qQ+DTfpc16nwBAmqbwWA2FW4SlNeULNdlep6G39DElYFnVty0H2RpNP5a5XdZlHt8489fJO/dD32wIibl6QN16dB4xlRAzimpeofx91xzUsSTlpezbEkIr2Lcl7Xfx2UMcxhyg4ErkEUegjtFTgQnzcfdrRi9WrH71EN/rEHEO7FtWjTKadXcn/BrJ84etDsjBnFrxAc3sNvk8YpPMP6J7x4xZe2dfqLWI/cpRfB/+YejPpdUSJwttNvVXx4nx5tx/RxSGAwGZVHPg0BcgIgrnCayzTZuuq/yPSWhBocegEl4QA== 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=HYkGffdK/7EZ4eJV/r/RfWgSSUu2+ttVya1KH/5bFNc=; b=dmqhBFSbNKo0Ah+ShIhCOygmu7SiNVjy+pJgg8VfZm5mVss+EhzbmnMUCkIuo+olRgJ4YAd/iWg+l2RcQREdhNg0vLZbJrSQAsmHnemCyOj5KdKOBCJ9qzECNY8SSmeF9H+PF6O5Uy+9UBJQf5/j0P5MeHH3W0lKlsfChM+kRgxhgBkAe3mZf4R70d+MBq0cH2hAk7jMpF4HXUmXjI8hi9Mlgvolo1OACHyrDB819MAM1Z5RcUpXTwNpKjqNkgMoRmJn1xt7j2EZSQOAnn9i9qB1ukeVkLr/Jua4jRGlSMlDseF7P+w8bHtm2jOzGLV+VpB+BDJhCdVEhWgx/I31pQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from SN7PR12MB8059.namprd12.prod.outlook.com (2603:10b6:806:32b::7) by PH0PR12MB8152.namprd12.prod.outlook.com (2603:10b6:510:292::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9182.16; Fri, 3 Oct 2025 15:48:01 +0000 Received: from SN7PR12MB8059.namprd12.prod.outlook.com ([fe80::4ee2:654e:1fe8:4b91]) by SN7PR12MB8059.namprd12.prod.outlook.com ([fe80::4ee2:654e:1fe8:4b91%2]) with mapi id 15.20.9160.017; Fri, 3 Oct 2025 15:48:01 +0000 From: Joel Fernandes To: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org, dakr@kernel.org, acourbot@nvidia.com Cc: Alistair Popple , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , bjorn3_gh@protonmail.com, Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , David Airlie , Simona Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , John Hubbard , Joel Fernandes , Timur Tabi , joel@joelfernandes.org, Elle Rhumsaa , Yury Norov , Daniel Almeida , Andrea Righi , nouveau@lists.freedesktop.org Subject: [PATCH v6 4/5] rust: Move register and bitfield macros out of Nova Date: Fri, 3 Oct 2025 11:47:47 -0400 Message-Id: <20251003154748.1687160-5-joelagnelf@nvidia.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251003154748.1687160-1-joelagnelf@nvidia.com> References: <20251003154748.1687160-1-joelagnelf@nvidia.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: BN9PR03CA0652.namprd03.prod.outlook.com (2603:10b6:408:13b::27) To SN7PR12MB8059.namprd12.prod.outlook.com (2603:10b6:806:32b::7) 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: SN7PR12MB8059:EE_|PH0PR12MB8152:EE_ X-MS-Office365-Filtering-Correlation-Id: cd4abf4c-ca11-4fe5-959b-08de02943b79 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|7416014|376014|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?Cpa2eAIIOBrJuvUe1vOCMmr/dcOFjYttdH/MS4bJgJ4Z33qF7ccxTyRpGtwn?= =?us-ascii?Q?X6huhSZoHwNrfYdl47iwwSpyAFIzRJzm7qY1qJP1j+konI7xrIGlMb2QuHhP?= =?us-ascii?Q?sZIg1skt3irFcyn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ANV?= =?us-ascii?Q?C4/HMHMEOzg+6u9KRXNND12ngEsnpydT62OSUY+KGpKE1IHfE/ZcCxcoyQBF?= =?us-ascii?Q?aOaMzLiZAH7T1sDEhVZTAVkQa8tTOZXepnRfP/8JzNXDT1paSIc4UCOi5p6X?= =?us-ascii?Q?zdENsBjgb/nOyGg0GAGE0oH6lFIKglskBmiidYFzxc77XtkWDTXP4IH73Ujj?= =?us-ascii?Q?BTvtw/86qEdhLAQNknYwEIDaIaM0BNzwGmtyKbsuquqC79e49y//1fOGDDaa?= =?us-ascii?Q?owMB85k7b4xPFmUid1IkUwD+kYBAlFdOb8tfN0h0eHchmX3bz6C1AX6zuBTi?= =?us-ascii?Q?G0pvOJ8TJWB9ChcV71WWktFKxkILg+xWmtT9l7Y5YO5SIa/kBleN1JNBIuy1?= =?us-ascii?Q?0FDAp+64gce8j2TSCcRIrq+Cw3mz0mh/7WrsWLhcAJo+vxzUyFi2qGzs5pJi?= =?us-ascii?Q?SZ3qIqjFjIshcOpbpCml5qmGYsq8SNA4?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN7PR12MB8059.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(7416014)(376014)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?464XkfJWsTLuM1aqGX1YHMPjB9S6cJWz/xVYc6ps+nIxmwekQQWjwOpfr6qU?= =?us-ascii?Q?zxqD9GFI/I6EapijRq1oXOu7Qf7TKjfJUOHWoLzXfi7E3YvraOCK6oRFsZO+?= =?us-ascii?Q?44NUMWpKxgEyYObHv6dARWbvnVW04uf+WxU9z7SyLCLzGXm93ycWegQEHB7H?= =?us-ascii?Q?iTXK4/lQT07GKxn6E1IhU4nxKWl+7GZQDR3PmVvu8frN8gBe+vRgnG4v1sI3?= =?us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AsWk3pvjD6niTN?= =?us-ascii?Q?8WLI4HuSOm3Oyrl78FqvTRcVbB93r77UzBMk/GOpoYunuJU0to1fiJmwr1Xm?= =?us-ascii?Q?pW1jdIwwUvjHnyx+vEYC/YerwdAElrON0hh72XFGkPVeNwkt5btuq2EvbePI?= =?us-ascii?Q?sdIWTI1TDm5HcLg382yuzHddbqNLhLTqux1xokLzr99LJye7DV7+BUTDToW8?= =?us-ascii?Q?s3KgGedi/nhW5xQCw0VsFQYJpY6J4gHCufQvwEay3gi3+r6PNRKrthUhg0Pe?= =?us-ascii?Q?Kj09dmXx4od8tsY24pGReCjOhwcmc3EiDCLQWtNaFoNdOfph0FuteWh2xF5E?= =?us-ascii?Q?xO7qcJUjsDiDkwLAmfaw9RWTqZwzSpjnQe7iNuXNq1ruDHRefxoUn0LZrKSk?= =?us-ascii?Q?H/TKYRFx8heQRoD1XOnPg73uLCnweY2AqRnjfgRunTovJGxbe1F7bcT9A/nM?= =?us-ascii?Q?gf0BH1FsbH/7QyP76IEz4dwsM9gYQcUdj8h2s0o3?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: cd4abf4c-ca11-4fe5-959b-08de02943b79 X-MS-Exchange-CrossTenant-AuthSource: SN7PR12MB8059.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Oct 2025 15:48:01.1758 (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: JnEbOVLydpvy5tAMKW48cKMixicDlpN8GGTKpuJgUqs1srXbspvyrYkUdJMSViTWeZD8lO4JS2+ajmuP7/ROVg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB8152 Content-Type: text/plain; charset="utf-8" Out of broad need for the register and bitfield macros in Rust, move them out of nova into the kernel crate. Several usecases need them (Nova is already using these and Tyr developers said they need them). bitfield moved into kernel crate - defines bitfields in Rust. register moved into io module - defines hardware registers and accessors. Reviewed-by: Alexandre Courbot Reviewed-by: Elle Rhumsaa Signed-off-by: Joel Fernandes Reviewed-by: Edwin Peer --- drivers/gpu/nova-core/falcon.rs | 2 +- drivers/gpu/nova-core/falcon/gsp.rs | 4 +- drivers/gpu/nova-core/falcon/sec2.rs | 2 +- drivers/gpu/nova-core/nova_core.rs | 3 - drivers/gpu/nova-core/regs.rs | 6 +- .../gpu/nova-core =3D> rust/kernel}/bitfield.rs | 27 ++++----- rust/kernel/io.rs | 1 + .../macros.rs =3D> rust/kernel/io/register.rs | 58 ++++++++++--------- rust/kernel/lib.rs | 1 + 9 files changed, 54 insertions(+), 50 deletions(-) rename {drivers/gpu/nova-core =3D> rust/kernel}/bitfield.rs (91%) rename drivers/gpu/nova-core/regs/macros.rs =3D> rust/kernel/io/register.r= s (93%) diff --git a/drivers/gpu/nova-core/falcon.rs b/drivers/gpu/nova-core/falcon= .rs index 37e6298195e4..a15fa98c8614 100644 --- a/drivers/gpu/nova-core/falcon.rs +++ b/drivers/gpu/nova-core/falcon.rs @@ -6,6 +6,7 @@ use hal::FalconHal; use kernel::device; use kernel::dma::DmaAddress; +use kernel::io::register::RegisterBase; use kernel::prelude::*; use kernel::sync::aref::ARef; use kernel::time::Delta; @@ -14,7 +15,6 @@ use crate::driver::Bar0; use crate::gpu::Chipset; use crate::regs; -use crate::regs::macros::RegisterBase; use crate::util; =20 pub(crate) mod gsp; diff --git a/drivers/gpu/nova-core/falcon/gsp.rs b/drivers/gpu/nova-core/fa= lcon/gsp.rs index f17599cb49fa..cd4960e997c8 100644 --- a/drivers/gpu/nova-core/falcon/gsp.rs +++ b/drivers/gpu/nova-core/falcon/gsp.rs @@ -1,9 +1,11 @@ // SPDX-License-Identifier: GPL-2.0 =20 +use kernel::io::register::RegisterBase; + use crate::{ driver::Bar0, falcon::{Falcon, FalconEngine, PFalcon2Base, PFalconBase}, - regs::{self, macros::RegisterBase}, + regs::self, }; =20 /// Type specifying the `Gsp` falcon engine. Cannot be instantiated. diff --git a/drivers/gpu/nova-core/falcon/sec2.rs b/drivers/gpu/nova-core/f= alcon/sec2.rs index 815786c8480d..81717868a8a8 100644 --- a/drivers/gpu/nova-core/falcon/sec2.rs +++ b/drivers/gpu/nova-core/falcon/sec2.rs @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 =20 use crate::falcon::{FalconEngine, PFalcon2Base, PFalconBase}; -use crate::regs::macros::RegisterBase; +use kernel::io::register::RegisterBase; =20 /// Type specifying the `Sec2` falcon engine. Cannot be instantiated. pub(crate) struct Sec2(()); diff --git a/drivers/gpu/nova-core/nova_core.rs b/drivers/gpu/nova-core/nov= a_core.rs index 112277c7921e..fffcaee2249f 100644 --- a/drivers/gpu/nova-core/nova_core.rs +++ b/drivers/gpu/nova-core/nova_core.rs @@ -2,9 +2,6 @@ =20 //! Nova Core GPU Driver =20 -#[macro_use] -mod bitfield; - mod dma; mod driver; mod falcon; diff --git a/drivers/gpu/nova-core/regs.rs b/drivers/gpu/nova-core/regs.rs index 206dab2e1335..1f08e6d4045a 100644 --- a/drivers/gpu/nova-core/regs.rs +++ b/drivers/gpu/nova-core/regs.rs @@ -4,15 +4,13 @@ // but are mapped to types. #![allow(non_camel_case_types)] =20 -#[macro_use] -pub(crate) mod macros; - use crate::falcon::{ DmaTrfCmdSize, FalconCoreRev, FalconCoreRevSubversion, FalconFbifMemTy= pe, FalconFbifTarget, FalconModSelAlgo, FalconSecurityModel, PFalcon2Base, PFalconBase, Pere= grineCoreSelect, }; use crate::gpu::{Architecture, Chipset}; use kernel::prelude::*; +use kernel::register; =20 // PMC =20 @@ -331,6 +329,7 @@ pub(crate) fn mem_scrubbing_done(self) -> bool { =20 pub(crate) mod gm107 { // FUSE + use kernel::register; =20 register!(NV_FUSE_STATUS_OPT_DISPLAY @ 0x00021c04 { 0:0 display_disabled as bool; @@ -339,6 +338,7 @@ pub(crate) mod gm107 { =20 pub(crate) mod ga100 { // FUSE + use kernel::register; =20 register!(NV_FUSE_STATUS_OPT_DISPLAY @ 0x00820c04 { 0:0 display_disabled as bool; diff --git a/drivers/gpu/nova-core/bitfield.rs b/rust/kernel/bitfield.rs similarity index 91% rename from drivers/gpu/nova-core/bitfield.rs rename to rust/kernel/bitfield.rs index cbedbb0078f6..09cd5741598c 100644 --- a/drivers/gpu/nova-core/bitfield.rs +++ b/rust/kernel/bitfield.rs @@ -9,7 +9,7 @@ /// # Syntax /// /// ```rust -/// use nova_core::bitfield; +/// use kernel::bitfield; /// /// #[derive(Debug, Clone, Copy, Default)] /// enum Mode { @@ -82,10 +82,11 @@ /// the result. /// - `as ?=3D> ` calls ``'s `TryFrom= ::<>` implementation /// and returns the result. This is useful with fields for which not all= values are valid. +#[macro_export] macro_rules! bitfield { // Main entry point - defines the bitfield struct with fields ($vis:vis struct $name:ident($storage:ty) $(, $comment:literal)? { $($= fields:tt)* }) =3D> { - bitfield!(@core $vis $name $storage $(, $comment)? { $($fields)* }= ); + ::kernel::bitfield!(@core $vis $name $storage $(, $comment)? { $($= fields)* }); }; =20 // All rules below are helpers. @@ -114,7 +115,7 @@ fn from(val: $name) -> $storage { } } =20 - bitfield!(@fields_dispatcher $vis $name $storage { $($fields)* }); + ::kernel::bitfield!(@fields_dispatcher $vis $name $storage { $($fi= elds)* }); }; =20 // Captures the fields and passes them to all the implementers that re= quire field information. @@ -130,7 +131,7 @@ fn from(val: $name) -> $storage { )* } ) =3D> { - bitfield!(@field_accessors $vis $name $storage { + ::kernel::bitfield!(@field_accessors $vis $name $storage { $( $hi:$lo $field as $type $(?=3D> $try_into_type)? @@ -139,8 +140,8 @@ fn from(val: $name) -> $storage { ; )* }); - bitfield!(@debug $name { $($field;)* }); - bitfield!(@default $name { $($field;)* }); + ::kernel::bitfield!(@debug $name { $($field;)* }); + ::kernel::bitfield!(@default $name { $($field;)* }); }; =20 // Defines all the field getter/setter methods for `$name`. @@ -155,13 +156,13 @@ fn from(val: $name) -> $storage { } ) =3D> { $( - bitfield!(@check_field_bounds $hi:$lo $field as $type); + ::kernel::bitfield!(@check_field_bounds $hi:$lo $field as $typ= e); )* =20 #[allow(dead_code)] impl $name { $( - bitfield!(@field_accessor $vis $name $storage, $hi:$lo $field = as $type + ::kernel::bitfield!(@field_accessor $vis $name $storage, $hi:$= lo $field as $type $(?=3D> $try_into_type)? $(=3D> $into_type)? $(, $comment)? @@ -198,7 +199,7 @@ impl $name { @field_accessor $vis:vis $name:ident $storage:ty, $hi:tt:$lo:tt $f= ield:ident as bool =3D> $into_type:ty $(, $comment:literal)?; ) =3D> { - bitfield!( + ::kernel::bitfield!( @leaf_accessor $vis $name $storage, $hi:$lo $field { |f| <$into_type>::from(if f !=3D 0 { true } else { false }) } $into_type =3D> $into_type $(, $comment)?; @@ -209,7 +210,7 @@ impl $name { ( @field_accessor $vis:vis $name:ident $storage:ty, $hi:tt:$lo:tt $f= ield:ident as bool $(, $comment:literal)?; ) =3D> { - bitfield!(@field_accessor $vis $name $storage, $hi:$lo $field as b= ool =3D> bool $(, $comment)?;); + ::kernel::bitfield!(@field_accessor $vis $name $storage, $hi:$lo $= field as bool =3D> bool $(, $comment)?;); }; =20 // Catches the `?=3D>` syntax for non-boolean fields. @@ -217,7 +218,7 @@ impl $name { @field_accessor $vis:vis $name:ident $storage:ty, $hi:tt:$lo:tt $f= ield:ident as $type:tt ?=3D> $try_into_type:ty $(, $comment:literal)?; ) =3D> { - bitfield!(@leaf_accessor $vis $name $storage, $hi:$lo $field + ::kernel::bitfield!(@leaf_accessor $vis $name $storage, $hi:$lo $f= ield { |f| <$try_into_type>::try_from(f as $type) } $try_into_type = =3D> ::core::result::Result< $try_into_type, @@ -231,7 +232,7 @@ impl $name { @field_accessor $vis:vis $name:ident $storage:ty, $hi:tt:$lo:tt $f= ield:ident as $type:tt =3D> $into_type:ty $(, $comment:literal)?; ) =3D> { - bitfield!(@leaf_accessor $vis $name $storage, $hi:$lo $field + ::kernel::bitfield!(@leaf_accessor $vis $name $storage, $hi:$lo $f= ield { |f| <$into_type>::from(f as $type) } $into_type =3D> $into_t= ype $(, $comment)?;); }; =20 @@ -240,7 +241,7 @@ impl $name { @field_accessor $vis:vis $name:ident $storage:ty, $hi:tt:$lo:tt $f= ield:ident as $type:tt $(, $comment:literal)?; ) =3D> { - bitfield!(@field_accessor $vis $name $storage, $hi:$lo $field as $= type =3D> $type $(, $comment)?;); + ::kernel::bitfield!(@field_accessor $vis $name $storage, $hi:$lo $= field as $type =3D> $type $(, $comment)?;); }; =20 // Generates the accessor methods for a single field. diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs index 03b467722b86..a79b603604b1 100644 --- a/rust/kernel/io.rs +++ b/rust/kernel/io.rs @@ -8,6 +8,7 @@ use crate::{bindings, build_assert, ffi::c_void}; =20 pub mod mem; +pub mod register; pub mod resource; =20 pub use resource::Resource; diff --git a/drivers/gpu/nova-core/regs/macros.rs b/rust/kernel/io/register= .rs similarity index 93% rename from drivers/gpu/nova-core/regs/macros.rs rename to rust/kernel/io/register.rs index c0a5194e8d97..c24d956f122f 100644 --- a/drivers/gpu/nova-core/regs/macros.rs +++ b/rust/kernel/io/register.rs @@ -17,7 +17,8 @@ /// The `T` generic argument is used to distinguish which base to use, in = case a type provides /// several bases. It is given to the `register!` macro to restrict the us= e of the register to /// implementors of this particular variant. -pub(crate) trait RegisterBase { +pub trait RegisterBase { + /// The base address for the register. const BASE: usize; } =20 @@ -26,7 +27,7 @@ pub(crate) trait RegisterBase { /// /// Example: /// -/// ```no_run +/// ```ignore /// register!(BOOT_0 @ 0x00000100, "Basic revision information about the G= PU" { /// 3:0 minor_revision as u8, "Minor revision of the chip"; /// 7:4 major_revision as u8, "Major revision of the chip"; @@ -39,7 +40,7 @@ pub(crate) trait RegisterBase { /// significant bits of the register. Each field can be accessed and modif= ied using accessor /// methods: /// -/// ```no_run +/// ```ignore /// // Read from the register's defined offset (0x100). /// let boot0 =3D BOOT_0::read(&bar); /// pr_info!("chip revision: {}.{}", boot0.major_revision(), boot0.minor_r= evision()); @@ -61,7 +62,7 @@ pub(crate) trait RegisterBase { /// It is also possible to create a alias register by using the `=3D> ALIA= S` syntax. This is useful /// for cases where a register's interpretation depends on the context: /// -/// ```no_run +/// ```ignore /// register!(SCRATCH @ 0x00000200, "Scratch register" { /// 31:0 value as u32, "Raw value"; /// }); @@ -111,7 +112,7 @@ pub(crate) trait RegisterBase { /// this register needs to implement `RegisterBase`. Here is the abo= ve example translated /// into code: /// -/// ```no_run +/// ```ignore /// // Type used to identify the base. /// pub(crate) struct CpuCtlBase; /// @@ -162,7 +163,7 @@ pub(crate) trait RegisterBase { /// compile-time or runtime bound checking. Simply define their address as= `Address[Size]`, and add /// an `idx` parameter to their `read`, `write` and `alter` methods: /// -/// ```no_run +/// ```ignore /// # fn no_run() -> Result<(), Error> { /// # fn get_scratch_idx() -> usize { /// # 0x15 @@ -211,7 +212,7 @@ pub(crate) trait RegisterBase { /// Combining the two features described in the sections above, arrays of = registers accessible from /// a base can also be defined: /// -/// ```no_run +/// ```ignore /// # fn no_run() -> Result<(), Error> { /// # fn get_scratch_idx() -> usize { /// # 0x15 @@ -273,28 +274,29 @@ pub(crate) trait RegisterBase { /// # Ok(()) /// # } /// ``` +#[macro_export] macro_rules! register { // Creates a register at a fixed offset of the MMIO space. ($name:ident @ $offset:literal $(, $comment:literal)? { $($fields:tt)*= } ) =3D> { - bitfield!(pub(crate) struct $name(u32) $(, $comment)? { $($fields)= * } ); + ::kernel::bitfield!(pub(crate) struct $name(u32) $(, $comment)? { = $($fields)* } ); register!(@io_fixed $name @ $offset); }; =20 // Creates an alias register of fixed offset register `alias` with its= own fields. ($name:ident =3D> $alias:ident $(, $comment:literal)? { $($fields:tt)*= } ) =3D> { - bitfield!(pub(crate) struct $name(u32) $(, $comment)? { $($fields)= * } ); + ::kernel::bitfield!(pub(crate) struct $name(u32) $(, $comment)? { = $($fields)* } ); register!(@io_fixed $name @ $alias::OFFSET); }; =20 // Creates a register at a relative offset from a base address provide= r. ($name:ident @ $base:ty [ $offset:literal ] $(, $comment:literal)? { $= ($fields:tt)* } ) =3D> { - bitfield!(pub(crate) struct $name(u32) $(, $comment)? { $($fields)= * } ); + ::kernel::bitfield!(pub(crate) struct $name(u32) $(, $comment)? { = $($fields)* } ); register!(@io_relative $name @ $base [ $offset ]); }; =20 // Creates an alias register of relative offset register `alias` with = its own fields. ($name:ident =3D> $base:ty [ $alias:ident ] $(, $comment:literal)? { $= ($fields:tt)* }) =3D> { - bitfield!(pub(crate) struct $name(u32) $(, $comment)? { $($fields)= * } ); + ::kernel::bitfield!(pub(crate) struct $name(u32) $(, $comment)? { = $($fields)* } ); register!(@io_relative $name @ $base [ $alias::OFFSET ]); }; =20 @@ -305,7 +307,7 @@ macro_rules! register { } ) =3D> { static_assert!(::core::mem::size_of::() <=3D $stride); - bitfield!(pub(crate) struct $name(u32) $(, $comment)? { $($fields)= * } ); + ::kernel::bitfield!(pub(crate) struct $name(u32) $(, $comment)? { = $($fields)* } ); register!(@io_array $name @ $offset [ $size ; $stride ]); }; =20 @@ -326,7 +328,7 @@ macro_rules! register { $(, $comment:literal)? { $($fields:tt)* } ) =3D> { static_assert!(::core::mem::size_of::() <=3D $stride); - bitfield!(pub(crate) struct $name(u32) $(, $comment)? { $($fields)= * } ); + ::kernel::bitfield!(pub(crate) struct $name(u32) $(, $comment)? { = $($fields)* } ); register!(@io_relative_array $name @ $base [ $offset [ $size ; $st= ride ] ]); }; =20 @@ -348,7 +350,7 @@ macro_rules! register { } ) =3D> { static_assert!($idx < $alias::SIZE); - bitfield!(pub(crate) struct $name(u32) $(, $comment)? { $($fields)= * } ); + ::kernel::bitfield!(pub(crate) struct $name(u32) $(, $comment)? { = $($fields)* } ); register!(@io_relative $name @ $base [ $alias::OFFSET + $idx * $al= ias::STRIDE ] ); }; =20 @@ -357,7 +359,7 @@ macro_rules! register { // to avoid it being interpreted in place of the relative register arr= ay alias rule. ($name:ident =3D> $alias:ident [ $idx:expr ] $(, $comment:literal)? { = $($fields:tt)* }) =3D> { static_assert!($idx < $alias::SIZE); - bitfield!(pub(crate) struct $name(u32) $(, $comment)? { $($fields)= * } ); + ::kernel::bitfield!(pub(crate) struct $name(u32) $(, $comment)? { = $($fields)* } ); register!(@io_fixed $name @ $alias::OFFSET + $idx * $alias::STRIDE= ); }; =20 @@ -414,12 +416,12 @@ pub(crate) fn read( base: &B, ) -> Self where T: ::core::ops::Deref>, - B: crate::regs::macros::RegisterBase<$base>, + B: ::kernel::io::register::RegisterBase<$base>, { const OFFSET: usize =3D $name::OFFSET; =20 let value =3D io.read32( - >::BASE = + OFFSET + >::BA= SE + OFFSET ); =20 Self(value) @@ -435,13 +437,13 @@ pub(crate) fn write( base: &B, ) where T: ::core::ops::Deref>, - B: crate::regs::macros::RegisterBase<$base>, + B: ::kernel::io::register::RegisterBase<$base>, { const OFFSET: usize =3D $name::OFFSET; =20 io.write32( self.0, - >::BASE = + OFFSET + >::BA= SE + OFFSET ); } =20 @@ -455,7 +457,7 @@ pub(crate) fn alter( f: F, ) where T: ::core::ops::Deref>, - B: crate::regs::macros::RegisterBase<$base>, + B: ::kernel::io::register::RegisterBase<$base>, F: ::core::ops::FnOnce(Self) -> Self, { let reg =3D f(Self::read(io, base)); @@ -600,11 +602,11 @@ pub(crate) fn read( idx: usize, ) -> Self where T: ::core::ops::Deref>, - B: crate::regs::macros::RegisterBase<$base>, + B: ::kernel::io::register::RegisterBase<$base>, { build_assert!(idx < Self::SIZE); =20 - let offset =3D >::BASE + + let offset =3D >::BASE + Self::OFFSET + (idx * Self::STRIDE); let value =3D io.read32(offset); =20 @@ -622,11 +624,11 @@ pub(crate) fn write( idx: usize ) where T: ::core::ops::Deref>, - B: crate::regs::macros::RegisterBase<$base>, + B: ::kernel::io::register::RegisterBase<$base>, { build_assert!(idx < Self::SIZE); =20 - let offset =3D >::BASE + + let offset =3D >::BASE + Self::OFFSET + (idx * Self::STRIDE); =20 io.write32(self.0, offset); @@ -643,7 +645,7 @@ pub(crate) fn alter( f: F, ) where T: ::core::ops::Deref>, - B: crate::regs::macros::RegisterBase<$base>, + B: ::kernel::io::register::RegisterBase<$base>, F: ::core::ops::FnOnce(Self) -> Self, { let reg =3D f(Self::read(io, base, idx)); @@ -662,7 +664,7 @@ pub(crate) fn try_read( idx: usize, ) -> ::kernel::error::Result where T: ::core::ops::Deref>, - B: crate::regs::macros::RegisterBase<$base>, + B: ::kernel::io::register::RegisterBase<$base>, { if idx < Self::SIZE { Ok(Self::read(io, base, idx)) @@ -684,7 +686,7 @@ pub(crate) fn try_write( idx: usize, ) -> ::kernel::error::Result where T: ::core::ops::Deref>, - B: crate::regs::macros::RegisterBase<$base>, + B: ::kernel::io::register::RegisterBase<$base>, { if idx < Self::SIZE { Ok(self.write(io, base, idx)) @@ -707,7 +709,7 @@ pub(crate) fn try_alter( f: F, ) -> ::kernel::error::Result where T: ::core::ops::Deref>, - B: crate::regs::macros::RegisterBase<$base>, + B: ::kernel::io::register::RegisterBase<$base>, F: ::core::ops::FnOnce(Self) -> Self, { if idx < Self::SIZE { diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index fcffc3988a90..8f8260090c02 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -63,6 +63,7 @@ pub mod alloc; #[cfg(CONFIG_AUXILIARY_BUS)] pub mod auxiliary; +pub mod bitfield; pub mod bits; #[cfg(CONFIG_BLOCK)] pub mod block; --=20 2.34.1 From nobody Tue Dec 16 21:51:00 2025 Received: from DM5PR21CU001.outbound.protection.outlook.com (mail-centralusazon11011038.outbound.protection.outlook.com [52.101.62.38]) (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 98CF228A704; Fri, 3 Oct 2025 15:48:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.62.38 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759506498; cv=fail; b=KijlGEiILVVVs/IvyboM6QA7CxfKKFoa0K2BoxYk6V0EAuYK7s+UfHPIXJq2sc6UtqkDQNsZzb0R7dmc1vcX0L9CMq10ttOyRf/GnbFgWjC3uNdnAS7Q4f1rZc8cx4Kmz9BfMi8L5AKBOrBRcICLXjfk+ih1SLVTDV6jy+Oq7/8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759506498; c=relaxed/simple; bh=S0A7ZsNgYcHDBNLp0ctpTugXZvGZ9TCo2UwPcV0Yx9E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Content-Type:MIME-Version; b=ogSfiyoz7JeHdz5ARS2cgrLnRKSui4A7QUsSljWmkmajNk3WdcQMP5D8ER+PGZQqhV9o7WWV+uXgkXtX48ZMu64KN+bmWj80NDUCjViaT9riActW9XAXrKZ+XfkvuE87FPilTPJ+oStX6jVRcpoU3JJZRafdIx9UwhkPYXwuUnk= 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=dVwjQyLk; arc=fail smtp.client-ip=52.101.62.38 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="dVwjQyLk" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ROjrY+EwwIVQncMrNEiznxkAm9GR/VYz7uRkX24ET0MF74b6EGSw9XtaD9wQVwJ4gRV3ApmTf53jsvC3lRC4UZBmtLJrz1dkiUybyW7fIkwamLjfRS5AF56zHiahjpFnJoY9mYFa/bDjKmFe72eqB4PThWl9HYBV69cRDVVfvr4i7zd8u0XLWPeD5YLmPc2zB99Iqz4O/RttZLQxUQku3WH2Aqk61K2yVoITgq5nnk0ViyYAVX97u8UehytmANk2z91avI0bjSWbE6FW+IXExa8+HvdiZUImxuOZ8iqS1S0vQiFPbek1PnTE0nNGhmC/ujwVZeSaTzk81XC1W2nG9A== 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=irXmGebk8jrwl3Ma14svHRfkrqEosrVsZTlmxLLa1/M=; b=vu76BHqJwHD9VdgnsuLDtOZjomTkvoeVfCn/KCLPFhq8vPeb5ZlJUyKVMSQae1RrCGWbtbqkdFK30yxg533BykiLoNZbm9StTj2EQ0pvJ8LG8CfQYQ1cFMKwkARs7JDeUmRCnPTyEU4L1x3T4tZYY+j2ZGzcE3oFsRcnBS6tZ8U4VqQlIPgtZ2f9RypuKKtEEAHcyn0WrRlS+UchWdUbi4afC49GSwg7zs0AsOqxOYR5FGI2GLUlPGcEcPnpCScVKAYpNydqwU0XKMP44yuMJHsC0n6IUHD11N1uG8+XVKASdzHkLfLicTIibduWgeLt0kU3T2WD8xsA/kpIPhJs7g== 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=irXmGebk8jrwl3Ma14svHRfkrqEosrVsZTlmxLLa1/M=; b=dVwjQyLk3+OcXH4dLpf5s+xwEz0zoFW7EV8etetutw4SCwBPukhA/cp76TXN0rbWLIXMPb3jzFvfV2vxvobv54lI1myVauUVgHkmIqqBil4/oSzsXGCrZ9YCReD8IU4D6NrYjp7/ClNhB7pHUXuPf9kz5CPASNaTtXF3qab4hDoD3lVq2wHHxKIyVLtpNAe+qMQ0t4CMZnUEcROuZ8+mgJRj0wTU21jb7V8a2kJt4SsONsilgHXF4rNOnpvQUCFopuAbAI9VPIZ5LxNo6bS0mF/ETRF1fa7vMzcWhAdkL3yQFm6MyYHMmnkGKE0LKAEnogCznO7X5RdjqCwL93q58Q== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from SN7PR12MB8059.namprd12.prod.outlook.com (2603:10b6:806:32b::7) by PH0PR12MB8152.namprd12.prod.outlook.com (2603:10b6:510:292::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9182.16; Fri, 3 Oct 2025 15:48:03 +0000 Received: from SN7PR12MB8059.namprd12.prod.outlook.com ([fe80::4ee2:654e:1fe8:4b91]) by SN7PR12MB8059.namprd12.prod.outlook.com ([fe80::4ee2:654e:1fe8:4b91%2]) with mapi id 15.20.9160.017; Fri, 3 Oct 2025 15:48:03 +0000 From: Joel Fernandes To: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org, dakr@kernel.org, acourbot@nvidia.com Cc: Alistair Popple , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , bjorn3_gh@protonmail.com, Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , David Airlie , Simona Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , John Hubbard , Joel Fernandes , Timur Tabi , joel@joelfernandes.org, Elle Rhumsaa , Yury Norov , Daniel Almeida , Andrea Righi , nouveau@lists.freedesktop.org Subject: [PATCH v6 5/5] rust: bitfield: Add KUNIT tests for bitfield Date: Fri, 3 Oct 2025 11:47:48 -0400 Message-Id: <20251003154748.1687160-6-joelagnelf@nvidia.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251003154748.1687160-1-joelagnelf@nvidia.com> References: <20251003154748.1687160-1-joelagnelf@nvidia.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: BL1PR13CA0184.namprd13.prod.outlook.com (2603:10b6:208:2be::9) To SN7PR12MB8059.namprd12.prod.outlook.com (2603:10b6:806:32b::7) 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: SN7PR12MB8059:EE_|PH0PR12MB8152:EE_ X-MS-Office365-Filtering-Correlation-Id: 3b2b54b2-c44d-4b95-540e-08de02943cd8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|7416014|376014|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?MMIH3PiYeidaBSVs9EvupD9icVX+LOT6TETWYce1stjNwuwFA/axL2tGuMKn?= =?us-ascii?Q?1uTUfw0Rj1zPCar6llcNY56hq/jNFq3AAurF3M0TRvhp/cKqeUxa9qcpRKi6?= =?us-ascii?Q?qmTIHy/hbKJ2/OW6/+9uyVLZnVuBZrj96tkAo+0idZFlO1ac2AofS23GmqJo?= =?us-ascii?Q?bTjUxuX9iOjO7cz5vQOH3XwFoCTK9XKUFilwoHbkYiNRju9GXtMnKUTpJISG?= =?us-ascii?Q?+F1d0zT5swiq04kjCUKus8BxPQhUcLcxrzMBziTFCNKQE4UhNc71fls/ukDZ?= =?us-ascii?Q?R9Dk7mtbGNgxSDgHuklF9qCMgYaX3heVxH9tQ6t434Gpybjykw7+EkVv+H5k?= =?us-ascii?Q?QxHUIkPg0rDd9YPieZYCgILHdM5/7Q1Te/CL27s5wzocqG2I6UQGT1loy/qn?= =?us-ascii?Q?RdMgFXJ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K+pZ1pu/GArWW9/yfmEENk1nu3R9?= =?us-ascii?Q?QmkCSJ3X6XQAIrcTFQ1aB7LwLd9lZNArf9Q0jqZUEe3jfOCEyYBZKO/KxX8G?= =?us-ascii?Q?H+RqKlVHJ3Nmqy8JlgHSYsO2WanJkVlKyd9YtmlrrYSn7qRgOg8yC2P2Vs3h?= =?us-ascii?Q?T6chkkkO5Txq7JF0No8NHjn3tyM4GGSE?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN7PR12MB8059.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(7416014)(376014)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?nqhoUpzVbWQ9X/ZNVlmzMznkuJU2DtwoJv44ZzHbgvR5JyxZQHgTRd5IWxUn?= =?us-ascii?Q?8D1SWVnH2e6hp4sc8uIit1dmx6Dz7kwDJnvHuTJg/GSL+uqC6ogQx9dHC5F6?= =?us-ascii?Q?+JzZilKKGCgbJ6SA51GVtNtLiV0HeX6ZYTHuVWPtVbqHG6O2WxLSC+DH+hOm?= =?us-ascii?Q?OcKaHwXLm7YGB96Ph9No41ecbviQYvMgdVt4EWvbs7NBLm5xhEl6P3irUich?= =?us-ascii?Q?ZfWhQ7gum4jR8VJCne1z3TgR7MhuQGM2jr31yPbSJtju0ITqyj9I1tg37JqG?= =?us-ascii?Q?h1Xrj43bRwb+g0JHF6MUwtIQuxp6rE7WU2fnJIWDeWZVgEsl5DIZh6MMWZ4C?= =?us-ascii?Q?J4aRYvjdBluKwfpLyQYbHm45+8LCoJv+2DwjSvG81KpbkpG5HFO/zGR7oB5y?= =?us-ascii?Q?WVCnaOJVKC6e2QwFXeu5picgBdYS/vJsmFH5i73JztvPf3Fi1KmNV8Ep+asH?= =?us-ascii?Q?XTBoolgd18SBK+LS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t?= =?us-ascii?Q?zBQ3FV/rhYGQLYBPNdOJyP6K2J3i4e3goHj4lsgoTawgQ72JI6FscwtiR6Nw?= =?us-ascii?Q?Kiu87XrHlZHKOIBN90Lb960rCyoCQzNes8I6xj9Yei6La1Hfn/pMDT8sMox6?= =?us-ascii?Q?IoNmFcgyKHWV3LZ8VdV0ubEOcVsRTdU0y+mQWVPzV37OCvMtcoZ8H+f+9o4v?= =?us-ascii?Q?UqZlKyUM2+QIVvALcc7qOqajtvGvKzNXUFlWQGex?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3b2b54b2-c44d-4b95-540e-08de02943cd8 X-MS-Exchange-CrossTenant-AuthSource: SN7PR12MB8059.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Oct 2025 15:48:03.4504 (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: ITk4Bd6otR/hKDJixZawEq19LFei639EhU8CXtdSku2Bg2p5kxsVWiwMMdH8xKqBAWRIlW2M0Kkvr1Gp0LYVsg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB8152 Content-Type: text/plain; charset="utf-8" Add KUNIT tests to make sure the macro is working correctly. Reviewed-by: Alexandre Courbot Signed-off-by: Joel Fernandes Reviewed-by: Edwin Peer --- rust/kernel/bitfield.rs | 323 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 323 insertions(+) diff --git a/rust/kernel/bitfield.rs b/rust/kernel/bitfield.rs index 09cd5741598c..f0e341a1a979 100644 --- a/rust/kernel/bitfield.rs +++ b/rust/kernel/bitfield.rs @@ -329,3 +329,326 @@ fn default() -> Self { } }; } + +#[::kernel::macros::kunit_tests(kernel_bitfield)] +mod tests { + use core::convert::TryFrom; + + // Enum types for testing =3D> and ?=3D> conversions + #[derive(Debug, Default, Clone, Copy, PartialEq)] + enum MemoryType { + #[default] + Unmapped =3D 0, + Normal =3D 1, + Device =3D 2, + Reserved =3D 3, + } + + impl TryFrom for MemoryType { + type Error =3D u8; + fn try_from(value: u8) -> Result { + match value { + 0 =3D> Ok(MemoryType::Unmapped), + 1 =3D> Ok(MemoryType::Normal), + 2 =3D> Ok(MemoryType::Device), + 3 =3D> Ok(MemoryType::Reserved), + _ =3D> Err(value), + } + } + } + + impl From for u64 { + fn from(mt: MemoryType) -> u64 { + mt as u64 + } + } + + #[derive(Debug, Default, Clone, Copy, PartialEq)] + enum Priority { + #[default] + Low =3D 0, + Medium =3D 1, + High =3D 2, + Critical =3D 3, + } + + impl From for Priority { + fn from(value: u8) -> Self { + match value & 0x3 { + 0 =3D> Priority::Low, + 1 =3D> Priority::Medium, + 2 =3D> Priority::High, + _ =3D> Priority::Critical, + } + } + } + + impl From for u16 { + fn from(p: Priority) -> u16 { + p as u16 + } + } + + bitfield! { + struct TestPageTableEntry(u64) { + 0:0 present as bool; + 1:1 writable as bool; + 11:9 available as u8; + 13:12 mem_type as u8 ?=3D> MemoryType; + 17:14 extended_type as u8 ?=3D> MemoryType; // For testin= g failures + 51:12 pfn as u64; + 51:12 pfn_overlap as u64; + 61:52 available2 as u16; + } + } + + bitfield! { + struct TestControlRegister(u16) { + 0:0 enable as bool; + 3:1 mode as u8; + 5:4 priority as u8 =3D> Priority; + 7:4 priority_nibble as u8; + 15:8 channel as u8; + } + } + + bitfield! { + struct TestStatusRegister(u8) { + 0:0 ready as bool; + 1:1 error as bool; + 3:2 state as u8; + 7:4 reserved as u8; + 7:0 full_byte as u8; // For entire register + } + } + + #[test] + fn test_single_bits() { + let mut pte =3D TestPageTableEntry::default(); + + assert!(!pte.present()); + assert!(!pte.writable()); + assert_eq!(u64::from(pte), 0x0); + + pte =3D pte.set_present(true); + assert!(pte.present()); + assert_eq!(u64::from(pte), 0x1); + + pte =3D pte.set_writable(true); + assert!(pte.writable()); + assert_eq!(u64::from(pte), 0x3); + + pte =3D pte.set_writable(false); + assert!(!pte.writable()); + assert_eq!(u64::from(pte), 0x1); + + assert_eq!(pte.available(), 0); + pte =3D pte.set_available(0x5); + assert_eq!(pte.available(), 0x5); + assert_eq!(u64::from(pte), 0xA01); + } + + #[test] + fn test_range_fields() { + let mut pte =3D TestPageTableEntry::default(); + assert_eq!(u64::from(pte), 0x0); + + pte =3D pte.set_pfn(0x123456); + assert_eq!(pte.pfn(), 0x123456); + // Test overlapping field reads same value + assert_eq!(pte.pfn_overlap(), 0x123456); + assert_eq!(u64::from(pte), 0x123456000); + + pte =3D pte.set_available(0x7); + assert_eq!(pte.available(), 0x7); + assert_eq!(u64::from(pte), 0x123456E00); + + pte =3D pte.set_available2(0x3FF); + assert_eq!(pte.available2(), 0x3FF); + assert_eq!(u64::from(pte), 0x3FF0000123456E00); + + // Test TryFrom with ?=3D> for MemoryType + pte =3D pte.set_mem_type(MemoryType::Device); + assert_eq!(pte.mem_type(), Ok(MemoryType::Device)); + assert_eq!(u64::from(pte), 0x3FF0000123456E00); + + pte =3D pte.set_mem_type(MemoryType::Normal); + assert_eq!(pte.mem_type(), Ok(MemoryType::Normal)); + assert_eq!(u64::from(pte), 0x3FF0000123455E00); + + // Test all valid values for mem_type + pte =3D pte.set_mem_type(MemoryType::Reserved); + assert_eq!(pte.mem_type(), Ok(MemoryType::Reserved)); + assert_eq!(u64::from(pte), 0x3FF0000123457E00); + + // Test failure case using extended_type field which has 4 bits (0= -15) + // MemoryType only handles 0-3, so values 4-15 should return Err + let mut raw =3D pte.into(); + // Set bits 17:14 to 7 (invalid for MemoryType) + raw =3D (raw & !::kernel::bits::genmask_u64(14..=3D17)) | (0x7 << = 14); + let invalid_pte =3D TestPageTableEntry(raw); + // Should return Err with the invalid value + assert_eq!(invalid_pte.extended_type(), Err(0x7)); + + // Test a valid value after testing invalid to ensure both cases w= ork + // Set bits 17:14 to 2 (valid: Device) + raw =3D (raw & !::kernel::bits::genmask_u64(14..=3D17)) | (0x2 << = 14); + let valid_pte =3D TestPageTableEntry(raw); + assert_eq!(valid_pte.extended_type(), Ok(MemoryType::Device)); + + let max_pfn =3D ::kernel::bits::genmask_u64(0..=3D39); + pte =3D pte.set_pfn(max_pfn); + assert_eq!(pte.pfn(), max_pfn); + assert_eq!(pte.pfn_overlap(), max_pfn); + } + + #[test] + fn test_builder_pattern() { + let pte =3D TestPageTableEntry::default() + .set_present(true) + .set_writable(true) + .set_available(0x7) + .set_pfn(0xABCDEF) + .set_mem_type(MemoryType::Reserved) + .set_available2(0x3FF); + + assert!(pte.present()); + assert!(pte.writable()); + assert_eq!(pte.available(), 0x7); + assert_eq!(pte.pfn(), 0xABCDEF); + assert_eq!(pte.pfn_overlap(), 0xABCDEF); + assert_eq!(pte.mem_type(), Ok(MemoryType::Reserved)); + assert_eq!(pte.available2(), 0x3FF); + } + + #[test] + fn test_raw_operations() { + let raw_value =3D 0x3FF0000003123E03u64; + + let pte =3D TestPageTableEntry(raw_value); + assert_eq!(u64::from(pte), raw_value); + + assert!(pte.present()); + assert!(pte.writable()); + assert_eq!(pte.available(), 0x7); + assert_eq!(pte.pfn(), 0x3123); + assert_eq!(pte.pfn_overlap(), 0x3123); + assert_eq!(pte.mem_type(), Ok(MemoryType::Reserved)); + assert_eq!(pte.available2(), 0x3FF); + + // Test using direct constructor syntax TestStruct(value) + let pte2 =3D TestPageTableEntry(raw_value); + assert_eq!(u64::from(pte2), raw_value); + } + + #[test] + fn test_u16_bitfield() { + let mut ctrl =3D TestControlRegister::default(); + + assert!(!ctrl.enable()); + assert_eq!(ctrl.mode(), 0); + assert_eq!(ctrl.priority(), Priority::Low); + assert_eq!(ctrl.priority_nibble(), 0); + assert_eq!(ctrl.channel(), 0); + + ctrl =3D ctrl.set_enable(true); + assert!(ctrl.enable()); + + ctrl =3D ctrl.set_mode(0x5); + assert_eq!(ctrl.mode(), 0x5); + + // Test From conversion with =3D> + ctrl =3D ctrl.set_priority(Priority::High); + assert_eq!(ctrl.priority(), Priority::High); + assert_eq!(ctrl.priority_nibble(), 0x2); // High =3D 2 in bits 5:4 + + ctrl =3D ctrl.set_channel(0xAB); + assert_eq!(ctrl.channel(), 0xAB); + + // Test overlapping fields + ctrl =3D ctrl.set_priority_nibble(0xF); + assert_eq!(ctrl.priority_nibble(), 0xF); + assert_eq!(ctrl.priority(), Priority::Critical); // bits 5:4 =3D 0= x3 + + let ctrl2 =3D TestControlRegister::default() + .set_enable(true) + .set_mode(0x3) + .set_priority(Priority::Medium) + .set_channel(0x42); + + assert!(ctrl2.enable()); + assert_eq!(ctrl2.mode(), 0x3); + assert_eq!(ctrl2.priority(), Priority::Medium); + assert_eq!(ctrl2.channel(), 0x42); + + let raw_value: u16 =3D 0x4217; + let ctrl3 =3D TestControlRegister(raw_value); + assert_eq!(u16::from(ctrl3), raw_value); + assert!(ctrl3.enable()); + assert_eq!(ctrl3.priority(), Priority::Medium); + assert_eq!(ctrl3.priority_nibble(), 0x1); + assert_eq!(ctrl3.channel(), 0x42); + } + + #[test] + fn test_u8_bitfield() { + let mut status =3D TestStatusRegister::default(); + + assert!(!status.ready()); + assert!(!status.error()); + assert_eq!(status.state(), 0); + assert_eq!(status.reserved(), 0); + assert_eq!(status.full_byte(), 0); + + status =3D status.set_ready(true); + assert!(status.ready()); + assert_eq!(status.full_byte(), 0x01); + + status =3D status.set_error(true); + assert!(status.error()); + assert_eq!(status.full_byte(), 0x03); + + status =3D status.set_state(0x3); + assert_eq!(status.state(), 0x3); + assert_eq!(status.full_byte(), 0x0F); + + status =3D status.set_reserved(0xA); + assert_eq!(status.reserved(), 0xA); + assert_eq!(status.full_byte(), 0xAF); + + // Test overlapping field + status =3D status.set_full_byte(0x55); + assert_eq!(status.full_byte(), 0x55); + assert!(status.ready()); + assert!(!status.error()); + assert_eq!(status.state(), 0x1); + assert_eq!(status.reserved(), 0x5); + + let status2 =3D TestStatusRegister::default() + .set_ready(true) + .set_state(0x2) + .set_reserved(0x5); + + assert!(status2.ready()); + assert!(!status2.error()); + assert_eq!(status2.state(), 0x2); + assert_eq!(status2.reserved(), 0x5); + assert_eq!(status2.full_byte(), 0x59); + + let raw_value: u8 =3D 0x59; + let status3 =3D TestStatusRegister(raw_value); + assert_eq!(u8::from(status3), raw_value); + assert!(status3.ready()); + assert!(!status3.error()); + assert_eq!(status3.state(), 0x2); + assert_eq!(status3.reserved(), 0x5); + assert_eq!(status3.full_byte(), 0x59); + + let status4 =3D TestStatusRegister(0xFF); + assert!(status4.ready()); + assert!(status4.error()); + assert_eq!(status4.state(), 0x3); + assert_eq!(status4.reserved(), 0xF); + assert_eq!(status4.full_byte(), 0xFF); + } +} --=20 2.34.1