From nobody Mon Feb 9 23:16:07 2026 Received: from CO1PR03CU002.outbound.protection.outlook.com (mail-westus2azon11010015.outbound.protection.outlook.com [52.101.46.15]) (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 9CE653469E9; Thu, 16 Oct 2025 15:13:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.46.15 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760627618; cv=fail; b=MVGHeOxFvymoApWhPLC+uTQuQlGyv2Zr8g5EU4gNbcwVaejpYhchh9lGNYHI6iZf8nd5CaRYBQoyk5Y5GK7o/2zIZVPt4fvsgulu7o/CQhuTRVffOgD2EjUssTvO4G6yX6q7TsA1PjopGMZtQWr6qkqTYIKhJlC0hEPPwrQ8C7I= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760627618; c=relaxed/simple; bh=6hlbJswMk7j1m70PmNFdfDHNDABkRdQyWsqD3gY78VA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Content-Type:MIME-Version; b=BVHxhXQs3wHPXszYO0WtPIea/iHRyuy2jc5p0MkX4tytLrmpHqDl15i09Q4K/rZXjYGy2cj5taPp9R3ufzqtW55hBGNtxawoNyMtzJtACnvltkHIgfqdHhXjntsFHmNSGI4zgpRjUked7fC0lI9CULUJkCNE9BmEgCfgHlCOq1c= 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=aqCQM8zJ; arc=fail smtp.client-ip=52.101.46.15 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="aqCQM8zJ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=TB3MmFQSz5pyb28gCiw8bpmesMWt6AwzIkfRdb4ATReTiOwrEjoOVCmOUBn8pjNEkAZ1OzS+KT/ssoJoyDr/tjdWYKOdNajGYj7/HVbtzoegqcipKnnPHYOX/Mf1iFxgSfZfwKvBM3dYRQBdvEi6SGhRXUdbab+Bf7SkaBh2VEpPMRRH16L/HSXmhCWP1H+M0UWgEzzdEzd9u+iBno76Itqphl5TCXExbbeyrJiByNkWjHKoiQ0IMZI1InZXalYi+JRdVGwZZkxo5KMo7F9ez++4uUUizgVhQQfOKhUNOs6pOlKTmCELD8FZVTq7/A6NkvGQNfbteQ+/p2ipYBP/bQ== 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=TazYopQhfj9fBNDwyQfsHCkjf/N0cmGySrdPC+y7YyY=; b=WJJqS34d+O7sBZA+oqR4bzFlSn9o776Z+tIXXZ2wOdt5Vw6sZsMUvohd00Iq6yjTo1QQy2keszPgOExaBg/Ds2O9BUDPjc60UKTRn8hQq/gXyfKEf4J7jcTNul36pGnd/fm2KfNhEh2DYUICpTxNjVM/OclYYo2f55oqN4dX64xhnHZwzjGKp4w9fI0LMWu1PL3lqC4f+4lbJRyG9reByMedBdlRf0ZqwrqTMoaDUJuKweE0Ml1yzn5nHfY2CQXqIDjj4dUcSGdu2wYNKQ8f08THhxB107zskXXCRMUh54RbUQUD+NFTTIggdH3O7mCF4TVS9XbqRW5sSUBXOshURw== 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=TazYopQhfj9fBNDwyQfsHCkjf/N0cmGySrdPC+y7YyY=; b=aqCQM8zJ6BoTFg+/09TNdY3gJXDgdFcOeJHGOsm+Zk8zgWU3b4OiZjMneRl6o1EVFSUJUO6N4AuRwCT3X7OqE9XGwsfClRdMIRKwzJAtYE8uvs2m6975muBAdqv0z38AGZIwuN4z0vPnlK3IPs0AGtrT8/Q9lJsgQiXYll+JLzyJr8K4T3vgHBTO29MZR3jARiCxuN/MooH+AX6j2PKyCOaJL0BiK19w8J1VOEsE4m6tShcvvNcZ5mkMgCBsjEtKf2sXqwITLGumpqiwwyxCLtoDGhMydJQv/jyq22mtDil6XW5eGMsi+RcUKtsB31kjOBlas+8+XRxam0o6lcOq4w== 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 DS0PR12MB8574.namprd12.prod.outlook.com (2603:10b6:8:166::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9228.10; Thu, 16 Oct 2025 15:13:33 +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.9228.010; Thu, 16 Oct 2025 15:13:33 +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 , nouveau@lists.freedesktop.org, Edwin Peer Subject: [PATCH v7.1 3/4] gpu: nova-core: bitfield: Add support for different storage widths Date: Thu, 16 Oct 2025 11:13:22 -0400 Message-Id: <20251016151323.1201196-4-joelagnelf@nvidia.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251016151323.1201196-1-joelagnelf@nvidia.com> References: <20251016151323.1201196-1-joelagnelf@nvidia.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: BL1PR13CA0321.namprd13.prod.outlook.com (2603:10b6:208:2c1::26) 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_|DS0PR12MB8574:EE_ X-MS-Office365-Filtering-Correlation-Id: 21011d9b-9504-46ab-b5fe-08de0cc69271 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|7416014|376014|366016; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?E3pHersiJs6n1lFW8unyY43Zjb26iFrzMpJDmbiShSpRnBV3oQxccwLOJ7ro?= =?us-ascii?Q?0vHoLjaKSUtvfK2inxmKCZYaZBe8a1UZwyQX3+UCeD4rV7LwkQEQf3rT/q0+?= =?us-ascii?Q?E+q1EaoOVuUjGAoy0oqi2xlEADafTbn8GbbdWe8Lq0rT9jKi5faODCqbZjQd?= =?us-ascii?Q?lUAss5BEcoZSl1svJf0CnCq3tC7M/1lurOKkkgb5QY7t3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Z80/gPTkhI9GkEIZVPjWM2ul8Sg11vSOfyBbg6EjTbg0S?= =?us-ascii?Q?fZa5dVd+0I325m6pe8pUoFtmpHT+ih3ySYfdlel2bOGN6CqOz11Fsa4mZp32?= =?us-ascii?Q?06raHyxkL0/j/eyQuxek33m6YLRcDLLJv10RUrst0zD5ML1/slHu2Nm4hBm1?= =?us-ascii?Q?jg3MpwjrRAs9STMg3ECDM8jRqoOZHqFY8dO3CbOc3wZNYZZNSVSFzTQhBJkh?= =?us-ascii?Q?9mfE1Vq0195OkCGUANafeDXe2DjPramfC4M9zfhgtn/MJ9iUVp5ulE1d2440?= =?us-ascii?Q?Kdn5QCpi3Npz28lTKRPknNY7KEY3D1AnOicQyr/ln27Rxa0kOGT7wtU1RwJn?= =?us-ascii?Q?1jU3Nn0xNE/gLO1Z8PpcC0EWR6AvHmRq?= 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)(1800799024)(7416014)(376014)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?2RfIT6ZVL99CDnPNIe74gyGZBmZC2kKY8U8DISu+vcKkxuyebDMHTYJfDbfG?= =?us-ascii?Q?jfp9xd0UlPdc1Ez8vg+pTCmio936NSx1uS+zPf/58oS+W9vyVLzTDCxneSNP?= =?us-ascii?Q?2QNssQiZDTcbP5tBGvccYjpddhj61zA1WoPQr+sF1MZJRmGrl/Z00u4dDTw2?= =?us-ascii?Q?SKXN1vWOzEqACIvM7m8f6b8lX+wzjBZ0/guSQl5pZ2UJyUwI9pxISBffznST?= =?us-ascii?Q?dKm3xJd42tNA1LRX7wJRUAJuW+AbumHT6lRrft5hWJa7gOohJILrArIzIusK?= =?us-ascii?Q?Ward/Hu6Wz/NnEItBaP2tkNI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Q?yTu6h4NT/O6W889yTTBsd+AydLePUE8AW+UN9SfcnNs62Oj23Rn0Dpy6/UcA?= =?us-ascii?Q?jrr4kE9jSD6lU4gPBbzDW7/HjCwxAcK33shlE5Yadiv/VwgJY9dhrZd/Mksg?= =?us-ascii?Q?ZrIApzmUvaEKGVvTPrpQ89waH5ErgU21W6G2d0X67Bbfc/aGh942i/3bXoev?= =?us-ascii?Q?a9Ey+rtAtkBxHgbCkO+Rhx+hGpwTAQ5WzTfAn+tSwlz6gxdUOSdGbZVkH5/u?= =?us-ascii?Q?gMRvRE11pcm4uxZiEBQn+79JnKdJKNsyZ96JxzYw4JoWjiYEGxETPiDjO5zb?= =?us-ascii?Q?CfAwaX7sIm3taM+d6MYU0mgjwDvjyuqfcoUxgGjnpQcBCJGAwjQXmZ2rwsMB?= =?us-ascii?Q?qiwJIG0uG8tyWq+yHoRj27g7aXD1Bhkbkfx6iAqR?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 21011d9b-9504-46ab-b5fe-08de0cc69271 X-MS-Exchange-CrossTenant-AuthSource: SN7PR12MB8059.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Oct 2025 15:13:33.5084 (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: VL/Zqe53/nc5+C28Bez9FYYgggaVEGz1z3oetrSHwPI4P174iB6xjGk64gMVGiSWBIeUN/Z7Ej0ZdfMv6loFoQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8574 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 Reviewed-by: Edwin Peer Signed-off-by: Joel Fernandes --- 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 98ccb1bd3289..998970ff0bbc 100644 --- a/drivers/gpu/nova-core/bitfield.rs +++ b/drivers/gpu/nova-core/bitfield.rs @@ -60,7 +60,7 @@ /// } /// /// bitfield! { -/// struct ControlReg { +/// struct ControlReg(u32) { /// 3:0 mode as u8 ?=3D> Mode; /// 7:7 state as bool =3D> State; /// } @@ -70,6 +70,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: @@ -82,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 $(, $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; @@ -106,20 +108,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)? @@ -128,7 +130,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)? @@ -143,7 +145,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)? @@ -159,7 +161,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)? @@ -193,11 +195,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 }) } bool $into_type =3D> $into_type $(, $comment)?; ); @@ -205,17 +207,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) } $type $try_into= _type =3D> ::core::result::Result< $try_into_type, @@ -226,29 +228,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) } $type $into_type =3D> $= into_type $(, $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 } $prim_type:tt $to_type:ty =3D> $res_type:ty = $(, $comment:literal)?; ) =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 @@ -259,7 +270,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); @@ -274,9 +285,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($prim_type::from(value)) << SHIFT) & = MASK; + let value =3D ($storage::from($prim_type::from(value)) << SHIF= T) & 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