From nobody Sun Feb 8 05:40:35 2026 Received: from DM1PR04CU001.outbound.protection.outlook.com (mail-centralusazon11010067.outbound.protection.outlook.com [52.101.61.67]) (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 ADE3E2F6577; Fri, 31 Oct 2025 13:40:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.61.67 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761918021; cv=fail; b=oMrkJ4QRw6C8Ij6CLs/DokXu4XMrouRYR/emuRoEMNYR++Fdd+mok4luF+oub1AaLf4E8zEZ86EbN43/zubeZETBPc1/VL11qgaE59afpwl+gQIl5xGckOnAnQhuA9mPjbePM2hYaYm3PUgxYrJ1GTqODHVT+vveFAcDdecEWDQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761918021; c=relaxed/simple; bh=62O6nVVE+gwZNyHku9oYjGj9NCgvoSfnyUWC6wTBPE8=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=fkidWTgPr+Xe9y9jyvqhkEBGQtx6SrV6Ab5sADLueMHxLFoIp1V2882m0mmQJH50yOXrmzeEzqflBPXOClqkIx2dnsS0Y9LVS1sbwQRIgYbHkNQ5xkaMImpdvoWJN6eVjnbOQRKv6ZhueMb2nj0mDbJz+5xZj3NOn+1KDadfEzA= 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=lyJWnBvO; arc=fail smtp.client-ip=52.101.61.67 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="lyJWnBvO" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=gnqoYZJkKkgz4PYsGI9/DiOwycd8hoN6PwqQALscnSZ3uvF1Qek+BLwJh1j/RzpfSdYO8mMXGdhkaZOfgk3AvGdo1d8BZSW31NTJ/8NtSMUSKcwqCxLUONQo7uhQyuA1YZvBr6WmdfcmouR+NVgA6tO1qRRRLfHHCMTtQysbHPvJ+qSA5xILTndND8a5NWIebThXrZwuRtyA+ki+K5Zq/LY3ky/xlOmSZ52Z9CJKeqWuqJ1E27Me96UXHnq/u3P2zsWiLbbHDTICBL1zgy8ESeiH5kNvqv1GMeM4tTgAo0ia0XYOm3gQUnmXCMYS/lhQNGlTO2Ebq4Ns5GeDtDMDyw== 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=QdsZdTOsSnNBTXBjHF3k39FpCLdVxDPHbFPLiAWnEmU=; b=rbQQpfs6xl8J10y2HQ5gduOA79trv85WcjU6IXuvDNwNWZgYKiSRRdoMGpvcVclAoGId8872uxfvd/NqK434KQ5ErW2oMG9VNwPUpMl5bAVVaryYS/MLD1gvi/pXqNQNOKBD5FZJfZsaQZr4Mgt7ZgEyiATay1H2RVQLA6WcM8dEPac2xx97i1/Y/iAzTptU/z+DdSM3h1pGm1yFhWZKQJ5JEX3MfQuzrcGJjhqccfzEF48KgfBZv+zn/3pigJN+mQI/uQ5GONbwW0230MrH0on349sOeWAkKlowqvGlFEN4m3zqhs7o7UVT9VJVO7TldA1HAt08OkExW0hrpZGZxw== 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=QdsZdTOsSnNBTXBjHF3k39FpCLdVxDPHbFPLiAWnEmU=; b=lyJWnBvOuMYuKrF8q5pd9x0TAEQ2sILm3KjsHMQbZ39i6DichBTjSi7c+hQzI3uoT7lZJg83tkrJwJIfZaAtKSMe8lGkbl9MqOw3gppP93jt5pI7NFEpNT7SNleeEwNRscy8qLhfc3iMT/YZ/kZop/UrUM51gSkJ91qo4lDx0zWLQ/YOmxkBe3viP0oC3GLWNYWlsySbQmGSt+0BtgDk6oVY7vfk+k4dzWUocjvyp0pz9XNODCI+2TJ6V4B1TNPZjwEy+F0iso1sHlfmiViw5Yl2mfttlgTZ2x54ouwSjTah9NCT1d1T27XL+qTF3mK0TNfKR2b2mAQZu59vV6KVeg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH2PR12MB3990.namprd12.prod.outlook.com (2603:10b6:610:28::18) by CH3PR12MB8211.namprd12.prod.outlook.com (2603:10b6:610:125::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9275.15; Fri, 31 Oct 2025 13:40:09 +0000 Received: from CH2PR12MB3990.namprd12.prod.outlook.com ([fe80::7de1:4fe5:8ead:5989]) by CH2PR12MB3990.namprd12.prod.outlook.com ([fe80::7de1:4fe5:8ead:5989%6]) with mapi id 15.20.9275.013; Fri, 31 Oct 2025 13:40:09 +0000 From: Alexandre Courbot Date: Fri, 31 Oct 2025 22:39:57 +0900 Subject: [PATCH 1/2] rust: add BitInt integer wrapping type Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251031-bounded_ints-v1-1-e2dbcd8fda71@nvidia.com> References: <20251031-bounded_ints-v1-0-e2dbcd8fda71@nvidia.com> In-Reply-To: <20251031-bounded_ints-v1-0-e2dbcd8fda71@nvidia.com> To: Alice Ryhl , Danilo Krummrich , Miguel Ojeda , Joel Fernandes , Yury Norov , Jesung Yang , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Alexandre Courbot X-Mailer: b4 0.14.3 X-ClientProxiedBy: TYCP286CA0246.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:456::12) To CH2PR12MB3990.namprd12.prod.outlook.com (2603:10b6:610:28::18) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR12MB3990:EE_|CH3PR12MB8211:EE_ X-MS-Office365-Filtering-Correlation-Id: 572fcb2f-babd-4b75-1df3-08de1883028c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|7416014|376014|10070799003|921020; X-Microsoft-Antispam-Message-Info: =?utf-8?B?Z252emlzQmJhMVFUZC9SeHpoRVVYOFpQZ05nN0VsbW5USGJWaktuc3dMc09Z?= =?utf-8?B?TWFHcW1JSnIxYXdud2ttV0hOVGtKTUJBS2YrVjlqY2NnMWtFM0o3YVloWHBK?= =?utf-8?B?MDFNZmJmdDlYcFVpaUpIejRZSHhDNUtNN1psV1hiNzlnR2ZzYWhMTldkRjlQ?= =?utf-8?B?eit3aUwzYTVJY1NaN2ZEN25KWkdIOUlEdDdSdUZxUjNodEpVUlFHYWhwUVRG?= =?utf-8?B?ZU40TlJlNDlyNFRlZGJYWFV1N0VqUVUrbzNaMWlSNHcwQ1llbUJoWWlVREpp?= =?utf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:NSPM;H:CH2PR12MB3990.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(7416014)(376014)(10070799003)(921020);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?K3IvdFhiZ2dzc3NDMjBhYldRb0FzaEVhaFd2RUlPUUREaXBSamUreGRRTmdD?= =?utf-8?B?SHJYTGtNUkZrbmpGTzF1K1dpYU5CdW9QUXhDSmh3RGxtNUhRellqOUZnaTBw?= =?utf-8?B?US9FWmVMb09UV25pdW9ReTBYa2d2WlVEY1VqSFJJNStXeWxNejNIdmo5OFhp?= =?utf-8?B?SElqYWRjem1DQmVNdUhTWlQycmxURzBzQ0s5c2hlTEtsRVVyMXFoYkdjTWQv?= =?utf-8?B?SGlONkc0Y1REMVMwWUxkOTd3V2ZWTGNPYzNzZmVQTTlvWGZ3cUpHMElFMTF6?= =?utf-8?B?MHFESVVvTHJYM0cvTzQwd3ZOWXZzb0xlUlVKTHN2RVZIdXE2ZzFVb1RKZU16?= =?utf-8?B?SStCQ0VxR3RTSm9HTWhBOFdyUmx0RW5hTGFZVWQ4N2ZTVW9FR0I1SmR0UEhQ?= =?utf-8?B?bWVMSmxmcElpWFdVU3NxckxyZWV0cFBNYmZNRm5JMjRvNHZXNDlQajkremxE?= =?utf-8?B?UEU4Yk9QRHZ3Z3RpT3NieWlKRWg5UW5uM0lMMzhmUDdHNjlBbngvTzVNNm5y?= =?utf-8?B?U20vNEhJTHJTNzdTbVNjQUhYUUgrTVB3VzVoc3ZYby9OczBrWnZzWFlwTUh6?= =?utf-8?B?a0E1Sk1PNDNMaGZhbmY2OFFscGl6a2RYVTFmRHFNVE9WZ2Y4R1ZWUEsvb292?= =?utf-8?B?UWZxTkozTmJpL1Z2QWU2amNRcFZZVWxVbUdCNnJMUStUVzUrVWdRMVp3aThl?= =?utf-8?B?QkVrQkR0U2hWWVBKT0tKd1pJaG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YlVKZTZ2M3ZWNjJEZ2o1V0FLR3d0S3cyREpMaTJ4NGk0SHl2?= =?utf-8?B?RU9uTHNnLzVwT2lrUFoxcVloRnY2VDZ2T0Z6ZjdLK1JrQk5XTlZWWEtBU2xB?= =?utf-8?B?Yk4yaU8wL05TR2hYbzZaMkFtOXBUQXo5SGZpS0ZPQmdUaGh1Wi9SNVVwZUlE?= =?utf-8?B?WVNGelpzczNSQks3eHkyeGJYb1NyOWpzVTl2dGFVaXRyc3pWUitNL0NUUGZ4?= =?utf-8?B?REJPR3ZhUVhRbTBLMFl3bDBjVzZMZlVINEZOZmsrRmYyTmgzL1R5Z0hRVGx0?= =?utf-8?B?QlJyNTNORzd1Rmw5U3VTcG9lcFpCcHpKb21CN0wyUnFWaHRaanFvTnd4ZXFM?= =?utf-8?B?Z3Jyd2FrcWFXcVpEOGNQdFh3Q0tYMU1pQlNRZjdUM294TGY5dTJRQVFMc2dx?= =?utf-8?B?UitGdWJUeW9qOVQ4bGhhRnNIVFVITytTNHNyUnNhN2M5a0YvWTM1MmRGVkM5?= =?utf-8?B?LzdIMDBLOE52UGFPU0t2MGFHRGRmM1BQVk40enFvMjY5aWp6dXlISTR1TmVC?= =?utf-8?Q?5dXpvXGMRrGfmCVz3W6L7ydHYqsjaKbr7Ji0EdW36sey?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 572fcb2f-babd-4b75-1df3-08de1883028c X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB3990.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Oct 2025 13:40:09.8552 (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: N04rhIQkZAZIQS2uWfMDfVd4s9KQEuBcyt2jMBMFq5JDIKH1tv1yCPQQ2KHEiUAW5gfdmDl0qxGWqAkRxMzsSw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8211 Add the `BitInt` type, which is an integer on which the number of bits allowed to be used is restricted, capping its maximal value below that of primitive type is wraps. This is useful to e.g. enforce guarantees when working with bit fields. Alongside this type, provide many `From` and `TryFrom` implementations are to reduce friction when using with regular integer types. Proxy implementations of common integer traits are also provided. Signed-off-by: Alexandre Courbot --- rust/kernel/lib.rs | 1 + rust/kernel/num.rs | 75 ++++ rust/kernel/num/bitint.rs | 896 ++++++++++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 972 insertions(+) diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 3dd7bebe7888..235d0d8b1eff 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -109,6 +109,7 @@ pub mod mm; #[cfg(CONFIG_NET)] pub mod net; +pub mod num; pub mod of; #[cfg(CONFIG_PM_OPP)] pub mod opp; diff --git a/rust/kernel/num.rs b/rust/kernel/num.rs new file mode 100644 index 000000000000..4e81879d88f7 --- /dev/null +++ b/rust/kernel/num.rs @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Numerical features for the kernel. + +pub mod bitint; +pub use bitint::*; + +/// Type used to designate unsigned primitive types. +pub struct Unsigned; + +/// Type used to designate signed primitive types. +pub struct Signed; + +/// Trait describing properties of integer types. +pub trait Integer { + /// Whether this type is [`Signed`] or [`Unsigned`]. + type Signedness; + + /// Number of bits used for value representation. + const BITS: u32; +} + +impl Integer for bool { + type Signedness =3D Unsigned; + + const BITS: u32 =3D 1; +} + +impl Integer for u8 { + type Signedness =3D Unsigned; + + const BITS: u32 =3D u8::BITS; +} + +impl Integer for u16 { + type Signedness =3D Unsigned; + + const BITS: u32 =3D u16::BITS; +} + +impl Integer for u32 { + type Signedness =3D Unsigned; + + const BITS: u32 =3D u32::BITS; +} + +impl Integer for u64 { + type Signedness =3D Unsigned; + + const BITS: u32 =3D u64::BITS; +} + +impl Integer for i8 { + type Signedness =3D Signed; + + const BITS: u32 =3D i8::BITS; +} + +impl Integer for i16 { + type Signedness =3D Signed; + + const BITS: u32 =3D i16::BITS; +} + +impl Integer for i32 { + type Signedness =3D Signed; + + const BITS: u32 =3D i32::BITS; +} + +impl Integer for i64 { + type Signedness =3D Signed; + + const BITS: u32 =3D i64::BITS; +} diff --git a/rust/kernel/num/bitint.rs b/rust/kernel/num/bitint.rs new file mode 100644 index 000000000000..5af1447347a8 --- /dev/null +++ b/rust/kernel/num/bitint.rs @@ -0,0 +1,896 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! [`BitInt`], a primitive integer type with a limited set of bits usable= to represent values. + +use core::ops::Deref; + +use kernel::num::{Integer, Signed, Unsigned}; +use kernel::prelude::*; + +/// Evaluates to `true` if `$value` can be represented using at most `$num= _bits` on `$type`. +/// +/// Can be used in const context. +macro_rules! fits_within { + ($value:expr, $type:ty, $num_bits:expr) =3D> {{ + // Any attempt to create a `BitInt` with more bits used for repres= entation than its backing + // type (i.e. create an invalid `BitInt`) must be aborted at build= time. + build_assert!( + <$type>::BITS >=3D $num_bits, + "Number of bits requested for representation is larger than ba= cking type." + ); + + let shift: u32 =3D <$type>::BITS - $num_bits; + let v =3D $value; + + // The value fits within `NUM_BITS` if shifting it left by the num= ber of unused bits, then + // right by the same number, doesn't change the value. + // + // This method has the benefit of working with both unsigned and s= igned integers. + (v << shift) >> shift =3D=3D v + }}; +} + +/// Trait for primitive integer types that can be used to back a [`BitInt`= ]. +/// +/// This is mostly used to lock all the operations we need for [`BitInt`] = in a single trait. +pub trait Boundable +where + Self: Integer + + Sized + + Copy + + core::ops::Shl + + core::ops::Shr + + core::cmp::PartialEq, + Self: TryInto + TryInto + TryInto + TryInto, + Self: TryInto + TryInto + TryInto + TryInto, +{ + /// Returns `true` if `value` can be represented with at most `NUM_BIT= S` on `T`. + fn fits_within(value: Self, num_bits: u32) -> bool { + fits_within!(value, Self, num_bits) + } +} + +/// Inplement `Boundable` for all integers types. +impl Boundable for T +where + T: Integer + + Sized + + Copy + + core::ops::Shl + + core::ops::Shr + + core::cmp::PartialEq, + Self: TryInto + TryInto + TryInto + TryInto, + Self: TryInto + TryInto + TryInto + TryInto, +{ +} + +/// Integer type for which only the bits `0..NUM_BITS` are valid. +/// +/// # Invariants +/// +/// Stored values are represented with at most `NUM_BITS` bits. +/// +/// # Examples +/// +/// ``` +/// use kernel::num::BitInt; +/// +/// // An unsigned 8-bit integer, of which only the 4 LSBs can ever be set. +/// // The value `15` is statically validated to fit within the specified = number of bits. +/// let v =3D BitInt::::new::<15>(); +/// assert_eq!(v.get(), 15); +/// +/// let v =3D BitInt::::new::<-8>(); +/// assert_eq!(v.get(), -8); +/// +/// // This doesn't build: a `u8` is smaller than the requested 9 bits. +/// // let _ =3D BitInt::::new::<10>(); +/// +/// // This also doesn't build: the requested value doesn't fit within the= requested bits. +/// // let _ =3D BitInt::::new::<8>(); +/// +/// // Values can also be validated at runtime. This succeeds because `15`= can be represented +/// // with 4 bits. +/// assert!(BitInt::::try_new(15).is_some()); +/// // This fails because `16` cannot be represented with 4 bits. +/// assert!(BitInt::::try_new(16).is_none()); +/// +/// // Non-constant expressions can also be validated at build-time thanks= to compiler +/// // optimizations. This should be used as a last resort though. +/// let v =3D BitInt::::from_expr(15); +/// +/// // Common operations are supported against the backing type. +/// assert_eq!(v + 5, 20); +/// assert_eq!(v / 3, 5); +/// +/// // The backing type can be changed while preserving the number of bits= used for representation. +/// assert_eq!(v.cast::(), BitInt::::new::<15>()); +/// +/// // We can safely extend the number of bits... +/// assert_eq!(v.extend::<5>(), BitInt::::new::<15>()); +/// // ... but reducing the number of bits fails here as the value doesn't= fit anymore. +/// assert_eq!(v.try_shrink::<3>(), None); +/// +/// // Conversion into primitive types is dependent on the number of usefu= l bits, not the backing +/// // type. +/// // +/// // Event though its backing type is `u32`, this `BitInt` only uses 8 b= its and thus can safely +/// // be converted to a `u8`. +/// assert_eq!(u8::from(BitInt::::new::<128>()), 128u8); +/// +/// // The same applies to signed values. +/// assert_eq!(i8::from(BitInt::::new::<127>()), 127i8); +/// +/// // This however is not allowed, as 10 bits won't fit into a `u8` (rega= rdless of the actually +/// // contained value). +/// // let _ =3D u8::from(BitInt::::new::<10>()); +/// +/// // Conversely, `BitInt` types large enough to contain a given primitiv= e type can be created +/// // directly from it: +/// // +/// // This `BitInt` has 8 bits, so it can represent any `u8`. +/// let _ =3D BitInt::::from(128u8); +/// +/// // This `BitInt` has 8 bits, so it can represent any `i8`. +/// let _ =3D BitInt::::from(127i8); +/// +/// // This is not allowed, as this 6-bit `BitInt` does not have enough ca= pacity to represent a +/// // `u8` (regardless of the passed value). +/// // let _ =3D BitInt::::from(10u8); +/// +/// // A `u8` can be converted to an `i16` if we allow at least 9 bits to = be used. +/// let _ =3D BitInt::::from(255u8); +/// // ... but 8 bits aren't enough as the MSB is used for the sign, so th= is doesn't build. +/// // let _ =3D BitInt::::from(255u8); +/// ``` +#[repr(transparent)] +#[derive(Clone, Copy, Debug, Default, Hash)] +pub struct BitInt(T); + +/// Validating the value as a const expression cannot be done as a regular= method, as the +/// arithmetic expressions we rely on to check the bounds are not const. T= hus, implement +/// [`BitInt::new`] using a macro. +macro_rules! impl_const_new { + ($($type:ty)*) =3D> { + $( + impl BitInt<$type, NUM_BITS> { + /// Creates a [`BitInt`] for the constant `VALUE`. + /// + /// Fails at build time if `VALUE` cannot be represented with = `NUM_BITS`. + /// + /// This method should be used preferred to [`Self::from_expr`= ] whenever possible. + /// + /// # Examples + /// ``` + /// use kernel::num::BitInt; + /// + #[doc =3D ::core::concat!( + "let v =3D BitInt::<", + ::core::stringify!($type), + ", 4>::new::<7>();")] + /// assert_eq!(v.get(), 7); + /// ``` + pub const fn new() -> Self { + build_assert!( + fits_within!(VALUE, $type, NUM_BITS), + "Requested value larger than maximal representable val= ue." + ); + + // INVARIANT: `fits_within` confirmed that `value` can be = represented within + // `NUM_BITS`. + Self(VALUE) + } + } + )* + }; +} + +impl_const_new!(u8 u16 u32 u64); +impl_const_new!(i8 i16 i32 i64); + +impl BitInt +where + T: Boundable, +{ + /// Attempts to convert `value` into a `BitInt` using `NUM_BITS`. + /// + /// Returns [`None`] if `value` doesn't fit within `NUM_BITS`. + /// + /// # Examples + /// + /// ``` + /// use kernel::num::BitInt; + /// + /// assert_eq!(BitInt::::try_new(1).as_deref(), Some(&1)); + /// assert_eq!(BitInt::::try_new(0xff).as_deref(), Some(&0xff)= ); + /// + /// // `0x1ff` doesn't fit into 8 bits. + /// assert_eq!(BitInt::::try_new(0x1ff), None); + /// + /// // Signed types can also be used. + /// assert_eq!(BitInt::::try_new(-2).as_deref(), Some(&-2)); + /// assert_eq!(BitInt::::try_new(7).as_deref(), Some(&7)); + /// + /// // `8` doesn't fit into 4 signed bits. + /// assert_eq!(BitInt::::try_new(8), None); + /// + /// // `-9` doesn't fit into 4 signed bits. + /// assert_eq!(BitInt::::try_new(-9), None); + /// ``` + pub fn try_new(value: T) -> Option { + if !T::fits_within(value, NUM_BITS) { + None + } else { + // INVARIANT: `fits_within` confirmed that `value` can be repr= esented within `NUM_BITS`. + Some(Self(value)) + } + } + + /// Checks that `expr` is valid for this type at compile-time and buil= d a new value. + /// + /// This relies on [`build_assert!`] to perform validation at compile-= time. If `expr` cannot + /// be inferred to be in bounds at compile-time, use the fallible [`Se= lf::try_new`] instead. + /// + /// When possible, use one of the [`Self::new`] constructors instead o= f this one as it + /// statically validates `expr` instead of relying on the compiler's o= ptimizations. + /// + /// # Examples + /// + /// ``` + /// use kernel::num::BitInt; + /// + /// # fn some_number() -> u32 { 0xffffffff } + /// + /// assert_eq!(BitInt::::from_expr(1).get(), 1); + /// assert_eq!(BitInt::::from_expr(0xff).get(), 0xff); + /// + /// // Triggers a build error as `0x1ff` doesn't fit into 8 bits. + /// // assert_eq!(BitInt::::from_expr(0x1ff).get(), 0x1ff); + /// + /// // Signed types can also be used. + /// assert_eq!(BitInt::::from_expr(-2).get(), -2); + /// assert_eq!(BitInt::::from_expr(7).get(), 7); + /// + /// // Triggers a build error as `8` cannot be represented with 4 sign= ed bits. + /// // assert_eq!(BitInt::::from_expr(8).get(), 8); + /// + /// // Triggers a build error as `-9` cannot be represented with 4 sig= ned bits. + /// // assert_eq!(BitInt::::from_expr(-9).get(), -9); + /// + /// let v: u32 =3D some_number(); + /// // Triggers a build error as `v` cannot be asserted to fit within = 4 bits... + /// // let _ =3D BitInt::::from_expr(v); + /// // ... but this works as the compiler can assert the range from th= e mask. + /// let _ =3D BitInt::::from_expr(v & 0xf); + /// ``` + pub fn from_expr(expr: T) -> Self { + crate::build_assert!( + T::fits_within(expr, NUM_BITS), + "Requested value larger than maximal representable value." + ); + + // INVARIANT: `fits_within` confirmed that `expr` can be represent= ed within `NUM_BITS`. + Self(expr) + } + + /// Returns the contained value as the backing type. + /// + /// # Examples + /// + /// ``` + /// use kernel::num::BitInt; + /// + /// let v =3D BitInt::::new::<7>(); + /// assert_eq!(v.get(), 7u32); + /// ``` + pub fn get(self) -> T { + *self.deref() + } + + /// Increase the number of bits usable for `self`. + /// + /// This operation cannot fail. + /// + /// # Examples + /// + /// ``` + /// use kernel::num::BitInt; + /// + /// let v =3D BitInt::::new::<7>(); + /// let larger_v =3D v.extend::<12>(); + /// // The contained values are equal even though `larger_v` has a big= ger capacity. + /// assert_eq!(larger_v, v); + /// ``` + pub const fn extend(self) -> BitInt { + build_assert!( + NEW_NUM_BITS >=3D NUM_BITS, + "Requested number of bits is less than the current representat= ion." + ); + + // INVARIANT: the value did fit within `NUM_BITS`, so it will all = the more fit within + // the larger `NEW_NUM_BITS`. + BitInt(self.0) + } + + /// Shrink the number of bits usable for `self`. + /// + /// Returns [`None`] if the value of `self` cannot be represented with= in `NEW_NUM_BITS`. + /// + /// # Examples + /// + /// ``` + /// use kernel::num::BitInt; + /// + /// let v =3D BitInt::::new::<7>(); + /// let smaller_v =3D v.try_shrink::<4>().ok_or(EOVERFLOW)?; + /// // The contained values are equal even though `smaller_v` has a sm= aller capacity. + /// assert_eq!(smaller_v, v); + /// + /// # Ok::<(), Error>(()) + /// ``` + pub fn try_shrink(self) -> Option> { + BitInt::::try_new(self.get()) + } + + /// Casts `self` into a [`BitInt`] backed by a different storage type,= but using the same + /// number of bits for value representation. + /// + /// This method cannot fail as the number of bits used for representat= ion doesn't change. + /// + /// # Examples + /// + /// ``` + /// use kernel::num::BitInt; + /// + /// let v =3D BitInt::::new::<-3>(); + /// let u: BitInt =3D v.cast(); + /// // The contained values are equal even though `u` has a smaller st= orage type. + /// assert_eq!(i32::from(u.get()), v.get()); + /// + /// let v =3D BitInt::::new::<127>(); + /// // This doesn't build: signedness cannot be safely changed when ca= sting. + /// // let _: BitInt =3D v.cast(); + /// ``` + pub fn cast(self) -> BitInt + where + U: TryFrom + Boundable, + T: Integer, + // Only allow casting from types of same signedness. + U: Integer, + { + // Make sure that the target type can contain the required number = of bits. + build_assert!(U::BITS >=3D NUM_BITS, "Cast to incompatible target = type."); + + // SAFETY: the contained value is represented using `NUM_BITS`, `U= ` is larger than + // `NUM_BITS`, and `U` and `T` have the same sign, hence this conv= ersion cannot fail. + let value =3D unsafe { U::try_from(self.0).unwrap_unchecked() }; + + // INVARIANT: although the storage type has changed, the value is = still represented within + // `NUM_BITS`, and with the same signedness. + BitInt(value) + } +} + +impl Deref for BitInt +where + T: Boundable, +{ + type Target =3D T; + + fn deref(&self) -> &Self::Target { + if !T::fits_within(self.0, NUM_BITS) { + // SAFETY: Per the `BitInt` invariants, `fits_within` can neve= r return `false`. + unsafe { core::hint::unreachable_unchecked() } + } + &self.0 + } +} + +/// Trait similar to [`TryInto`] but for `BitInt`, to avoid conflicting im= plementations errors. +/// +/// # Examples +/// +/// ``` +/// use kernel::num::{BitInt, TryIntoBitInt}; +/// +/// // Succeed because `128` fits into 8 bits. +/// assert_eq!(128u32.try_into_bitint(), Some(BitInt::::new::<128>= ())); +/// // Fails because `128` doesn't fits into 6 bits. +/// assert_eq!(TryIntoBitInt::::try_into_bitint(128u32), None); +/// ``` +pub trait TryIntoBitInt { + /// Attempts to convert `self` into a [`BitInt`] using `NUM_BITS`. + fn try_into_bitint(self) -> Option>; +} + +/// Any value can be attempted to be converted into a [`BitInt`] of any si= ze. +impl TryIntoBitInt for U +where + T: Boundable, + U: TryInto, +{ + fn try_into_bitint(self) -> Option> { + self.try_into().ok().and_then(BitInt::try_new) + } +} + +/// Allows comparison between [`BitInt`]s, even if their number of valid b= its differ. +/// +/// # Examples +/// +/// ``` +/// use kernel::num::BitInt; +/// +/// let v1 =3D BitInt::::new::<15>(); +/// let v2 =3D BitInt::::new::<15>(); +/// assert_eq!(v1, v2); +/// ``` +impl PartialEq> + for BitInt +where + T: Boundable, + U: Boundable, + T: PartialEq, +{ + fn eq(&self, other: &BitInt) -> bool { + self.get() =3D=3D other.get() + } +} + +impl Eq for BitInt where T: Boundable= {} + +/// Allows partial ordering between [`BitInt`]s, even if their number of v= alid bits differ. +/// +/// # Examples +/// +/// ``` +/// use kernel::num::BitInt; +/// +/// let v1 =3D BitInt::::new::<4>(); +/// let v2 =3D BitInt::::new::<15>(); +/// assert!(v1 < v2); +/// ``` +impl PartialOrd> + for BitInt +where + T: Boundable, + U: Boundable, + T: PartialOrd, +{ + fn partial_cmp(&self, other: &BitInt) -> Option { + self.get().partial_cmp(&other.get()) + } +} + +/// Allows ordering between [`BitInt`]s. +/// +/// # Examples +/// +/// ``` +/// use core::cmp::Ordering; +/// use kernel::num::BitInt; +/// +/// let v1 =3D BitInt::::new::<4>(); +/// let v2 =3D BitInt::::new::<15>(); +/// assert_eq!(v1.cmp(&v2), Ordering::Less); +/// ``` +impl Ord for BitInt +where + T: Boundable, + T: Ord, +{ + fn cmp(&self, other: &Self) -> core::cmp::Ordering { + self.get().cmp(&other.get()) + } +} + +/// Allows comparison between a [`BitInt`] and its backing type. +/// +/// # Examples +/// +/// ``` +/// use kernel::num::BitInt; +/// +/// let v =3D BitInt::::new::<15>(); +/// assert_eq!(v, 15); +/// ``` +impl PartialEq for BitInt +where + T: Boundable, + T: PartialEq, +{ + fn eq(&self, other: &T) -> bool { + self.get() =3D=3D *other + } +} + +/// Allows partial ordering between a [`BitInt`] and its backing type. +/// +/// # Examples +/// +/// ``` +/// use kernel::num::BitInt; +/// +/// let v =3D BitInt::::new::<4>(); +/// assert!(v < 15); +/// ``` +impl PartialOrd for BitInt +where + T: Boundable, + T: PartialOrd, +{ + fn partial_cmp(&self, other: &T) -> Option { + self.get().partial_cmp(other) + } +} + +/// Allows adding a [`BitInt`] to its backing type. +/// +/// # Examples +/// +/// ``` +/// use kernel::num::BitInt; +/// +/// let v =3D BitInt::::new::<15>(); +/// assert_eq!(v + 5, 20); +/// ``` +impl core::ops::Add for BitInt +where + T: Boundable, + T: core::ops::Add, +{ + type Output =3D T; + + fn add(self, rhs: T) -> Self::Output { + *self + rhs + } +} + +/// Allows subtracting a [`BitInt`] to its backing type. +/// +/// # Examples +/// +/// ``` +/// use kernel::num::BitInt; +/// +/// let v =3D BitInt::::new::<15>(); +/// assert_eq!(v - 5, 10); +/// ``` +impl core::ops::Sub for BitInt +where + T: Boundable, + T: core::ops::Sub, +{ + type Output =3D T; + + fn sub(self, rhs: T) -> Self::Output { + *self - rhs + } +} + +/// Allows multiplying a [`BitInt`] to its backing type. +/// +/// # Examples +/// +/// ``` +/// use kernel::num::BitInt; +/// +/// let v =3D BitInt::::new::<15>(); +/// assert_eq!(v * 10, 150); +/// ``` +impl core::ops::Mul for BitInt +where + T: Boundable, + T: core::ops::Mul, +{ + type Output =3D T; + + fn mul(self, rhs: T) -> Self::Output { + *self * rhs + } +} + +/// Allows dividing a [`BitInt`] to its backing type. +/// +/// # Examples +/// +/// ``` +/// use kernel::num::BitInt; +/// +/// let v =3D BitInt::::new::<15>(); +/// assert_eq!(v / 3, 5); +/// ``` +impl core::ops::Div for BitInt +where + T: Boundable, + T: core::ops::Div, +{ + type Output =3D T; + + fn div(self, rhs: T) -> Self::Output { + *self / rhs + } +} + +/// Allows getting a remainder of a [`BitInt`] when divided by its backing= type. +/// +/// # Examples +/// +/// ``` +/// use kernel::num::BitInt; +/// +/// let v =3D BitInt::::new::<15>(); +/// assert_eq!(v % 10, 5); +/// ``` +impl core::ops::Rem for BitInt +where + T: Boundable, + T: core::ops::Rem, +{ + type Output =3D T; + + fn rem(self, rhs: T) -> Self::Output { + *self % rhs + } +} + +// TODO: implement more of std::ops. + +impl core::fmt::Display for BitInt +where + T: Boundable, + T: core::fmt::Display, +{ + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + self.0.fmt(f) + } +} + +impl core::fmt::LowerHex for BitInt +where + T: Boundable, + T: core::fmt::LowerHex, +{ + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + self.0.fmt(f) + } +} + +impl core::fmt::UpperHex for BitInt +where + T: Boundable, + T: core::fmt::UpperHex, +{ + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + self.0.fmt(f) + } +} + +/// Declares a new `$trait` and implements it for all [`BitInt`] types rep= resented using +/// `$num_bits`. +/// +/// This is used to declare properties as traits that we can use in later = implementations. +macro_rules! impl_size_rule { + ($trait:ident, $($num_bits:literal)*) =3D> { + trait $trait {} + + $( + impl $trait for BitInt where T: Boundable {} + )* + }; + + // Variant declaring and implementing two traits, for signed and unsig= ned integers. + ($u_trait:ident, $i_trait:ident, $($num_bits:literal)*) =3D> { + trait $u_trait {} + trait $i_trait {} + + $( + impl $u_trait for BitInt + where + T: Boundable + Integer + { + } + + impl $i_trait for BitInt + where + T: Boundable + Integer + { + } + )* + }; +} + +/// Implementations for infallibly converting a primitive type into a `Bit= Int` that can contain it. +/// +/// # Examples +/// +/// ``` +/// use kernel::num::BitInt; +/// +/// // This `BitInt` has 8 bits, so it can represent any `u8`. +/// let _ =3D BitInt::::from(128u8); +/// +/// // This `BitInt` has 8 bits, so it can represent any `i8`. +/// let _ =3D BitInt::::from(127i8); +/// +/// // This is not allowed, as this 6-bit `BitInt` does not have enough ca= pacity to represent a +/// // `u8` (regardless of the passed value). +/// // let _ =3D BitInt::::from(10u8); +/// +/// // A `u8` can be converted to an `i16` if we allow at least 9 bits to = be used. +/// let _ =3D BitInt::::from(255u8); +/// // ... but 8 bits aren't enough as the MSB is used for the sign, so th= is doesn't build. +/// // let _ =3D BitInt::::from(255u8); +/// ``` +mod atleast_impls { + use super::*; + + // Number of bits at least as large as `64`. + impl_size_rule!(AtLeast64Bits, 64); + + // Number of bits at least as large as `32`. + impl_size_rule!(AtLeast32Bits, + 32 33 34 35 36 37 38 39 + 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 + 56 57 58 59 60 61 62 63 + ); + // Anything larger than `64` is also larger than `32`. + impl AtLeast32Bits for T where T: AtLeast64Bits {} + + // Number of bits at least as large as `16`. + impl_size_rule!(AtLeast16Bits, + 16 17 18 19 20 21 22 23 + 24 25 26 27 28 29 30 31 + ); + // Anything larger than `32` is also larger than `16`. + impl AtLeast16Bits for T where T: AtLeast32Bits {} + + // Number of bits at least as large as a `8`. + impl_size_rule!(AtLeast8Bits, 8 9 10 11 12 13 14 15); + // Anything larger than `16` is also larger than `8`. + impl AtLeast8Bits for T where T: AtLeast16Bits {} + + // Number of bits at least as large as `1`. + impl_size_rule!(AtLeast1Bit, 1 2 3 4 5 6 7); + // Anything larger than `8` is also larger than `1`. + impl AtLeast1Bit for T where T: AtLeast8Bits {} + + /// Generates `From` implementations from a primitive type into a [`Bi= tInt`] that is + /// guaranteed to being able to contain it. + macro_rules! impl_from_primitive { + ($($type:ty =3D> $trait:ident),*) =3D> { + $( + impl From<$type> for BitInt + where + Self: $trait, + T: From<$type> + Boundable, + { + fn from(value: $type) -> Self { + build_assert!( + T::BITS >=3D NUM_BITS, + "Number of bits requested for representation is la= rger than backing type." + ); + // INVARIANT: the impl constraints guarantee that the = source type is smaller + // than `NUM_BITS`, and the `build_assert` above that = the backing type can + // contain `NUM_BITS`. + Self(T::from(value)) + } + } + )* + } + } + + impl_from_primitive!( + bool =3D> AtLeast1Bit, + u8 =3D> AtLeast8Bits, + i8 =3D> AtLeast8Bits, + u16 =3D> AtLeast16Bits, + i16 =3D> AtLeast16Bits, + u32 =3D> AtLeast32Bits, + i32 =3D> AtLeast32Bits, + u64 =3D>AtLeast64Bits, + i64 =3D>AtLeast64Bits + ); +} + +/// Implementations for infallibly converting a [`BitInt`] into a primitiv= e type that can contain +/// it. +/// +/// # Examples +/// +/// ``` +/// use kernel::num::BitInt; +/// +/// // Event though its backing type is `u32`, this `BitInt` only uses 8 b= its and thus can safely +/// // be converted to a `u8`. +/// assert_eq!(u8::from(BitInt::::new::<128>()), 128u8); +/// +/// // The same applies to signed values. +/// assert_eq!(i8::from(BitInt::::new::<127>()), 127i8); +/// +/// // This however is not allowed, as 10 bits won't fit into a `u8` (rega= rdless of the actually +/// // contained value). +/// // let _ =3D u8::from(BitInt::::new::<10>()); +/// ``` +mod fits_impls { + use super::*; + + // Number of bits that can be converted to a `bool`. + impl_size_rule!(IntoBool, 1); + + // Number of bits that can be converted to a `u8`. + impl_size_rule!(IntoU8, IntoI8, 2 3 4 5 6 7 8); + // Anything that fits into `1` bit also fits into `8`. + impl IntoU8 for T where T: IntoBool {} + + // Number of bits that can be converted to a `u16`. + impl_size_rule!(IntoU16, IntoI16, 9 10 11 12 13 14 15 16); + // Anything that fits into `8` bits also fits into `16`. + impl IntoU16 for T where T: IntoU8 {} + impl IntoI16 for T where T: IntoI8 {} + + // Number of bits that can be converted into a `u32`. + impl_size_rule!(IntoU32, IntoI32, + 17 18 19 20 21 22 23 24 + 25 26 27 28 29 30 31 32 + ); + // Anything that fits into `16` bits also fits into `32`. + impl IntoU32 for T where T: IntoU16 {} + impl IntoI32 for T where T: IntoI16 {} + + // Number of bits that can be converted into a `u64`. + impl_size_rule!(IntoU64, IntoI64, + 33 34 35 36 37 38 39 40 + 41 42 43 44 45 46 47 48 + 49 50 51 52 53 54 55 56 + 57 58 59 60 61 62 63 64 + ); + // Anything that fits into `32` bits also fits into `64`. + impl IntoU64 for T where T: IntoU32 {} + impl IntoI64 for T where T: IntoI32 {} + + /// Generates [`From`] implementations from a [`BitInt`] into a primit= ive type that is + /// guaranteed to contain it. + macro_rules! impl_into_primitive { + ($($trait:ident =3D> $type:ty),*) =3D> { + $( + impl From> for $ty= pe + where + T: Boundable, + BitInt: $trait + { + fn from(value: BitInt) -> $type { + // SAFETY: the impl constraints ensure that `NUM_BITS`= can be converted into + // the destination type, so this conversion cannot fai= l. + unsafe { value.get().try_into().unwrap_unchecked() } + } + } + )* + } + } + + impl_into_primitive!( + IntoU8 =3D> u8, + IntoI8 =3D> i8, + IntoU16 =3D> u16, + IntoI16 =3D> i16, + IntoU32 =3D> u32, + IntoI32 =3D> i32, + IntoU64 =3D> u64, + IntoI64 =3D> i64 + ); + + // Conversion to boolean must be handled separately as it does not hav= e a [`TryFrom`] + // implementation from integers. + impl From> for bool + where + T: Boundable, + BitInt: IntoBool, + T: PartialEq + Zeroable, + { + fn from(value: BitInt) -> Self { + value.get() !=3D Zeroable::zeroed() + } + } +} --=20 2.51.0 From nobody Sun Feb 8 05:40:35 2026 Received: from DM1PR04CU001.outbound.protection.outlook.com (mail-centralusazon11010067.outbound.protection.outlook.com [52.101.61.67]) (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 7B8DD22A4E5; Fri, 31 Oct 2025 13:40:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.61.67 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761918024; cv=fail; b=chiX5xP+hkYR9dFZBTuzJLKlDK4tGRI0NRCR9Kn3a6/Cq8jmT48Z81AbXum5ocaxzWqjfo/DCF7VjvVgge3G8NSkBUsQQB5d81jTRdX2IXOAw7mD0vSW18QfO0oAhwv2XV97cAwzI7BURmYm+BL4ufj2RroVJqdaSU7JhFg1c5c= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761918024; c=relaxed/simple; bh=gj2y4AdcIvOe4Qy9sKKvr28bUsvM/lsLPgg5j4WTsQU=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=YDcakFAygpk1uNaL0rdaCnTgiT8VJwde9FgtGPhmoFDR5+lmREikRAH+PIHzCfnxADQ4uctnlp1xvuFM9VuisqwJQljaAGEFxDiwWzsch94n23WZyN3bVqVa2YgUc8SfMcVFwOC6eVoupHggxCX0rjWLY33eur/3M/1s3sMyWoY= 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=ZsSj2xfZ; arc=fail smtp.client-ip=52.101.61.67 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="ZsSj2xfZ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=MoHSBEBlHFfpbp4Uk/kCavLbVn/VEafgxCOmC1cuRCFD0S87fOTPxp9h2m3svwq1tB/CMQv/FlEyAHtIoJWIhhWjNfV/7YK/7Tj0aszogAPa7OJCZ/a6mh4XMy5jp8nsTidV2IhzkKmU9+AT/n7oGYo4GRPeBOQNlIPIxOncctuQWC0S+e0oETPNpddkCdn1OTh7LB8L9xCPaTi1i9o5d3bFWsdQRxR6rqjhPsWt3hHWgoz7l1ap8kgTslVFFhVi60bCAB9++Z/pjaF2Uu7lwHgiUzivj1rEw793Ngq1dtTtrm+9SHfaYEPqqreI/sSJYyoKhfMoUzu1nDjWVoAc+g== 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=DcEIy8GK95nks7lv6EqLFx13YTW8r6iD00Jay5XJvtg=; b=Jl2BhKrIwWkUHdli8XOHpnjTDNrqlq63t+8e80L548x+bVYVcoOjfMPloxN83RdVyVUmtHHB5s7wkCZbFX37si89nW4cGrjVaIA4LoIBEf7ihWWF3TBfVBtbpFadXjQ/d62EgTCZA6hL8+bmwl+mqzrROe9mLKZSBl9pJqrQOkIyiCuIYHGHxWepTIfh24z4dnx7QBMLZa1HakyiC+IqYqp11HfmZetL1mAwF6otDEhNwvcWWC44vrfUM+tECgicqeWLe25xY5Vc3CC0hUQX3ESqj+eOiaqxxTztB087Pm6BasvILfNQ6dSGKf6rdzGALrWSrtUc2Ym3JItfAuEbYw== 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=DcEIy8GK95nks7lv6EqLFx13YTW8r6iD00Jay5XJvtg=; b=ZsSj2xfZpVoxKPZrUGgljwlz6EBaU1PYk5tOAuIu+EkNNmX23Qq5DASqv1zvoUSv6SGPSab3+61SBhd0fbJjlADslu0VDWNy8BzhRK4nRnQjLh+zoq++jw06K3w6m528kAPZTrjQ1gZUhwdraZc/0kLpHqEMaoGiOZhuM0zj8bYbJzTuh/M2Grv6isGkFFV5vFGVtICzpIvroP0YhxPh+fy2JNA6fzywDneqFrGMnc3nHv+lz4HYhkSxyEirP/BzDrw/uS8pm8TG1RsF1rfUx5sjWx3NhmXxR6pD56r3+rzHNeqK418iLu9R8qECR4ZOa4vwNRgxgtEbv2qgjWVPYQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH2PR12MB3990.namprd12.prod.outlook.com (2603:10b6:610:28::18) by CH3PR12MB8211.namprd12.prod.outlook.com (2603:10b6:610:125::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9275.15; Fri, 31 Oct 2025 13:40:13 +0000 Received: from CH2PR12MB3990.namprd12.prod.outlook.com ([fe80::7de1:4fe5:8ead:5989]) by CH2PR12MB3990.namprd12.prod.outlook.com ([fe80::7de1:4fe5:8ead:5989%6]) with mapi id 15.20.9275.013; Fri, 31 Oct 2025 13:40:13 +0000 From: Alexandre Courbot Date: Fri, 31 Oct 2025 22:39:58 +0900 Subject: [PATCH FOR REFERENCE 2/2] gpu: nova-core: use BitInt for bitfields Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251031-bounded_ints-v1-2-e2dbcd8fda71@nvidia.com> References: <20251031-bounded_ints-v1-0-e2dbcd8fda71@nvidia.com> In-Reply-To: <20251031-bounded_ints-v1-0-e2dbcd8fda71@nvidia.com> To: Alice Ryhl , Danilo Krummrich , Miguel Ojeda , Joel Fernandes , Yury Norov , Jesung Yang , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Alexandre Courbot X-Mailer: b4 0.14.3 X-ClientProxiedBy: TYCP286CA0281.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:3c9::14) To CH2PR12MB3990.namprd12.prod.outlook.com (2603:10b6:610:28::18) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR12MB3990:EE_|CH3PR12MB8211:EE_ X-MS-Office365-Filtering-Correlation-Id: 546b8c4c-0f50-4575-ed66-08de188304af X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|7416014|376014|10070799003|921020; X-Microsoft-Antispam-Message-Info: =?utf-8?B?SDlidVpaUDJrbEpsQ1MzNGU5T0JYc215aEFWazhHTlltRDdSeUxmS05PMWp0?= =?utf-8?B?OGtScGpUSW80Q3BDV2dYY1BQR3NHbU1iMGprV0xRN1hUa2dYOEZLNlFRTGVX?= =?utf-8?B?Q0NXSmh2d2gxam1xeHd2RGV5WUlFV09XZDV4YVBNN0ZBN29IQmQ1MndLMlQ5?= =?utf-8?B?bFVQcXp4ZDM0T2tHUWw4Y241bGx4Vm5YK2V6NE5vZ1Y4aW8zcGg1cjRxRlNY?= =?utf-8?B?b1kzV1VaUEpXWW1qcnl1VEtQOU03b0VYczJZSjU4OG12UXc1WFZjNkF0eTdT?= =?utf-8?B?YjVuNkwvQk1hZEFadUg1SkNoc0toU3JWRFBGMDVrTWgvVzVva2FBcmI0Ynlh?= =?utf-8?B?eEdXOHBWMFFiS3YzeG9SRnJNa00zaENuenNjWHZKdkc5dEhuTFRCcStSOGhR?= =?utf-8?B?NHU2OWdtdFcxTkhVZ1p2MUd6ejdSTUZCS0wyR0lXUnZLMzhNdzRSeXRFemE3?= =?utf-8?B?eGI1VVA2cldXZXpjVXdsNHpCTGxwK0lFYmc3b2o4QkNRRjBQU0F1azVsTEJs?= =?utf-8?B?VFAvR0w4MDRldW00YWJhK0YwU0F0T1g2YzJTd1NkdGlJOVNZQXZvcXJoc0Np?= =?utf-8?B?ZzIrdk1qYUJMRmI2VXdVSkdtNXhSTWdGTGVlVDlSRGlEU2FQM1pZeGdEVnhl?= =?utf-8?B?NHZ3akVEZEs0QW9IWWpXZXNXbGxIQmpjWjMyb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ejd0K09CNmxxdHZ5STNjMDRYUnJSaUkyNktK?= =?utf-8?B?a3NtWHhtYk1kVWF4K0lKbnhPZi9lczRxVGJZSmFPQnZKTjl0ODB3RWRYQWZx?= =?utf-8?B?TVp2b04xV0hLbElwRG1UL2YybysxSVAxMmtNQTVDTnhnejV0OTlncXVOS3Vz?= =?utf-8?B?ZUo5WmhEV0k4aWdORjFhY2NWTDlXdTIvQkdIZ091Z3pOTCs4Skt2YmMySTNq?= =?utf-8?B?S1hYRFhIeUM4cEkwbUh2RlVycFRVUWdzMmh2b01ydXdpY2JWNXBTcDRmMHNG?= =?utf-8?B?SmVJU1k4STlyV0hCQ0F5RDRrOFJvMHhKTUYwZ2ZGYy9XQTBsOWIzOHJXOHlH?= =?utf-8?B?ZjR6dTJOWjRYY0tTd1paTVpKalVLcmdjMFJnUUVrZDQ1ZDVKRW1Ma09WbGdz?= =?utf-8?B?U2VZL3gya2dYTmRiS3g4SXVtTzFrYlR5SklGa3BLTmhjbXRDcjJkSGZMUk5D?= =?utf-8?Q?AF6S2l?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR12MB3990.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(7416014)(376014)(10070799003)(921020);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?QzlScmY0bDBSa3lWOEZIbzY4K3d0NzlMQ0lCNDFJaXNLSm9QQjNiZzJPNmhi?= =?utf-8?B?ZXRJTk54SUZuV2pLdS9yQi9qSGpvTi9JeDZFZDRHWDMxUkxZVTNDcWVvbFhF?= =?utf-8?B?bjk3SFVPRnQ2WWdNZFlmd2gyZGE3aDB4K3ByWmNPd0VzQWxBbzVXdko3Q0Qx?= =?utf-8?B?QSt5SGhMN1htTG1SZWxtRzlxUll0bVZibHFvTDVYQzdE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mFhZVY5?= =?utf-8?B?QXpHcHRqenkvSmdER2d0UWRYZTZOaTZwa3pMd3NnVnA5Q2kvTVRmdVVHYnM1?= =?utf-8?B?MVEvTUpWbjFZZlFrK1ovMXNlR1kzOWVleUlXbDVXMms0dXB3QzhQNmw3WG5Y?= =?utf-8?B?ZTRlZTlsc2hieER5T0pKS1NkSHRZUFhUUXhzKzlzZzZ6U3V2Ykd2SDV5SjhJ?= =?utf-8?Q?My5o+YZnk9XQy9v8XOWHY6n/73fQ1ydkJWEiSnby30mx?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 546b8c4c-0f50-4575-ed66-08de188304af X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB3990.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Oct 2025 13:40:13.4358 (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: tO7HZvBiZswOXw4UUEkVxGDxhUpnNvD/iyJ5w+sRIXNi6UHXqsXJ2NC768gtu4LS/GhfIkXBBYZ5HNujnt32jA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8211 Use BitInt with the bitfield!() and register!() macros and adapt the nova-core code accordingly. This makes it impossible to trim values when setting a register field, because either the value of the field has been inferred at compile-time to fit within the bounds of the field, or the user has been forced to check at runtime that it does indeed fit. The use of BitInt actually simplifies register fields definitions, as they don't need an intermediate storage type (the "as ..." part of fields definitions). Instead, the internal storage type for each field is now the bounded integer of its width in bits, which can optionally be converted to another type that implements `From`` or `TryFrom`` for that bounded integer type. This means that something like register!(NV_PDISP_VGA_WORKSPACE_BASE @ 0x00625f04 { 3:3 status_valid as bool, 31:8 addr as u32, }); Now becomes register!(NV_PDISP_VGA_WORKSPACE_BASE @ 0x00625f04 { 3:3 status_valid =3D> bool, 31:8 addr, }); (here `status_valid` is infallibly converted to a bool for convenience and to remain compatible with the previous semantics) The field setter/getters are also simplified. If a field has no target type, then its setter expects any type that implements `Into` to the field's bounded integer type. Due to the many `From` implementations for primitive types, this means that most calls can be left unchanged. If the caller passes a value that is potentially larger than the field's capacity, it must use the `try_` variant of the setter, which returns an error if the value cannot be converted at runtime. For fields that use `=3D>` to convert to another type, both setter and getter are always infallible. For fields that use `?=3D>` to fallibly convert to another type, only the getter needs to be fallible as the setter always provide valid values by design. Outside of the register macro, the biggest changes occur in `falcon.rs`, which defines many enums for fields - their conversion implementations need to be changed from the original primitive type of the field to the new corresponding bounded int type. Hopefully the TryFrom/Into derive macros [1] can take care of implementing these, but it will need to be adapted to support bounded integers... :/ But overall, I am rather happy at how simple it was to convert the whole of nova-core to this. Note: This RFC uses nova-core's register!() macro for practical purposes, but the hope is to move this patch on top of the bitfield macro after it is split out [2]. [1] https://lore.kernel.org/rust-for-linux/cover.1755235180.git.y.j3ms.n@gmail.= com/ [2] https://lore.kernel.org/rust-for-linux/20251003154748.1687160-1-joelagnelf@= nvidia.com/ Signed-off-by: Alexandre Courbot --- drivers/gpu/nova-core/bitfield.rs | 366 ++++++++++++++++----------= ---- drivers/gpu/nova-core/falcon.rs | 134 ++++++----- drivers/gpu/nova-core/falcon/hal/ga102.rs | 5 +- drivers/gpu/nova-core/fb/hal/ga100.rs | 3 +- drivers/gpu/nova-core/gpu.rs | 9 +- drivers/gpu/nova-core/regs.rs | 139 ++++++------ 6 files changed, 353 insertions(+), 303 deletions(-) diff --git a/drivers/gpu/nova-core/bitfield.rs b/drivers/gpu/nova-core/bitf= ield.rs index 16e143658c51..c75d95ef1ae9 100644 --- a/drivers/gpu/nova-core/bitfield.rs +++ b/drivers/gpu/nova-core/bitfield.rs @@ -19,21 +19,21 @@ /// Auto =3D 2, /// } /// -/// impl TryFrom for Mode { -/// type Error =3D u8; -/// fn try_from(value: u8) -> Result { -/// match value { +/// impl TryFrom> for Mode { +/// type Error =3D u32; +/// fn try_from(value: BitInt) -> Result { +/// match *value { /// 0 =3D> Ok(Mode::Low), /// 1 =3D> Ok(Mode::High), /// 2 =3D> Ok(Mode::Auto), -/// _ =3D> Err(value), +/// value =3D> Err(value), /// } /// } /// } /// -/// impl From for u8 { -/// fn from(mode: Mode) -> u8 { -/// mode as u8 +/// impl From for BitInt { +/// fn from(mode: Mode) -> BitInt { +/// BitInt::from_expr(mode as u32) /// } /// } /// @@ -44,25 +44,29 @@ /// Active =3D 1, /// } /// -/// impl From for State { -/// fn from(value: bool) -> Self { -/// if value { State::Active } else { State::Inactive } +/// impl From> for State { +/// fn from(value: BitInt) -> Self { +/// if bool::from(value) { +/// State::Active +/// } else { +/// State::Inactive +/// } /// } /// } /// -/// impl From for bool { -/// fn from(state: State) -> bool { +/// impl From for BitInt { +/// fn from(state: State) -> BitInt { /// match state { -/// State::Inactive =3D> false, -/// State::Active =3D> true, +/// State::Inactive =3D> false.into(), +/// State::Active =3D> true.into(), /// } /// } /// } /// /// bitfield! { /// pub struct ControlReg(u32) { -/// 7:7 state as bool =3D> State; -/// 3:0 mode as u8 ?=3D> Mode; +/// 7:7 state =3D> State; +/// 3:0 mode ?=3D> Mode; /// } /// } /// ``` @@ -112,12 +116,9 @@ fn from(val: $name) -> $storage { 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. + // Dispatch fields depending on the syntax used. (@fields_dispatcher $vis:vis $name:ident $storage:ty { - $($hi:tt:$lo:tt $field:ident as $type:tt + $($hi:tt:$lo:tt $field:ident $(?=3D> $try_into_type:ty)? $(=3D> $into_type:ty)? $(, $comment:literal)? @@ -125,173 +126,208 @@ fn from(val: $name) -> $storage { )* } ) =3D> { - bitfield!(@field_accessors $vis $name $storage { - $( - $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 $vis:vis $name:ident $storage:ty { - $($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 $vis $name $storage, $hi:$lo $field = as $type - $(?=3D> $try_into_type)? - $(=3D> $into_type)? - $(, $comment)? - ; - ); - )* + $( + bitfield!(@private_field_accessors $name $storage : $hi:$lo $field= ); + bitfield!(@public_field_accessors $vis $name $storage : $hi:$lo $f= ield + $(?=3D> $try_into_type)? + $(=3D> $into_type)? + $(, $comment)? + ); + )* } + + bitfield!(@debug $name { $($field;)* }); + bitfield!(@default $name { $($field;)* }); + }; =20 - // 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 $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 $vis $name $storage, $hi:$lo $field - { |f| <$into_type>::from(f !=3D 0) } - bool $into_type =3D> $into_type $(, $comment)?; - ); - }; - - // Shortcut for fields defined as `bool` without the `=3D>` syntax. - ( - @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 bool = =3D> bool $(, $comment)?; - ); - }; - - // Catches the `?=3D>` syntax for non-boolean fields. - ( - @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 - { |f| <$try_into_type>::try_from(f as $type) } $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 $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 - { |f| <$into_type>::from(f as $type) } $type $into_type =3D> $= into_type $(, $comment)?;); - }; - - // Shortcut for non-boolean fields defined without the `=3D>` or `?=3D= >` syntax. - ( - @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)?; - ); - }; - - // Generates the accessor methods for a single field. - ( - @leaf_accessor $vis:vis $name:ident $storage:ty, $hi:tt:$lo:tt $fi= eld:ident - { $process:expr } $prim_type:tt $to_type:ty =3D> $res_type:ty = $(, $comment:literal)?; + @private_field_accessors $name:ident $storage:ty : $hi:tt:$lo:tt $= field:ident ) =3D> { ::kernel::macros::paste!( const [<$field:upper _RANGE>]: ::core::ops::RangeInclusive =3D= $lo..=3D$hi; - 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 _MASK>]: u32 =3D ((((1 << $hi) - 1) << 1) + 1= ) - ((1 << $lo) - 1); const [<$field:upper _SHIFT>]: u32 =3D $lo; ); =20 + ::kernel::macros::paste!( + fn [<$field _internal>](self) -> + ::kernel::num::BitInt<$storage, { $hi + 1 - $lo }> { + const MASK: u32 =3D $name::[<$field:upper _MASK>]; + const SHIFT: u32 =3D $name::[<$field:upper _SHIFT>]; + + let field =3D ((self.0 & MASK) >> SHIFT); + + ::kernel::num::BitInt::<$storage, { $hi + 1 - $lo }>::from_exp= r(field) + } + + fn []( + mut self, + value: ::kernel::num::BitInt<$storage, { $hi + 1 - $lo }>, + ) -> Self + { + const MASK: u32 =3D $name::[<$field:upper _MASK>]; + const SHIFT: u32 =3D $name::[<$field:upper _SHIFT>]; + + let value =3D (value.get() << SHIFT) & MASK; + self.0 =3D (self.0 & !MASK) | value; + + self + } + + fn []( + mut self, + value: T, + ) -> ::kernel::error::Result + where T: ::kernel::num::TryIntoBitInt<$storage, { $hi + 1 - $l= o }>, + { + const MASK: u32 =3D $name::[<$field:upper _MASK>]; + const SHIFT: u32 =3D $name::[<$field:upper _SHIFT>]; + + let value =3D ( + value.try_into_bitint().ok_or(::kernel::error::code::EOVER= FLOW)?.get() << SHIFT + ) & MASK; + + self.0 =3D (self.0 & !MASK) | value; + + Ok(self) + } + ); + }; + + // Generates the public accessors for fields infallibly (`=3D>`) conve= rted to a type. + ( + @public_field_accessors $vis:vis $name:ident $storage:ty : $hi:tt:= $lo:tt $field:ident + =3D> $into_type:ty $(, $comment:literal)? + ) =3D> { + ::kernel::macros::paste!( + $( #[doc=3D"Returns the value of this field:"] #[doc=3D$comment] )? #[inline(always)] - $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>]; - ); - let field =3D ((self.0 & MASK) >> SHIFT); - - $process(field) + $vis fn $field(self) -> $into_type + { + self.[<$field _internal>]().into() } =20 - ::kernel::macros::paste!( $( #[doc=3D"Sets the value of this field:"] #[doc=3D$comment] )? #[inline(always)] - $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($prim_type::from(value)) << SHIF= T) & MASK; - self.0 =3D (self.0 & !MASK) | value; - - self + $vis fn [](self, value: $into_type) -> Self + { + self.[](value.into()) } + + /// Private method, for use in the [`Default`] implementation. + fn [<$field _default>]() -> $into_type { + Default::default() + } + + ); + }; + + // Generates the public accessors for fields fallibly (`?=3D>`) conver= ted to a type. + ( + @public_field_accessors $vis:vis $name:ident $storage:ty : $hi:tt:= $lo:tt $field:ident + ?=3D> $try_into_type:ty $(, $comment:literal)? + ) =3D> { + ::kernel::macros::paste!( + + $( + #[doc=3D"Returns the value of this field:"] + #[doc=3D$comment] + )? + #[inline(always)] + $vis fn $field(self) -> + Result< + $try_into_type, + <$try_into_type as ::core::convert::TryFrom< + ::kernel::num::BitInt<$storage, { $hi + 1 - $lo }> + >>::Error + > + { + self.[<$field _internal>]().try_into() + } + + $( + #[doc=3D"Sets the value of this field:"] + #[doc=3D$comment] + )? + #[inline(always)] + $vis fn [](self, value: $try_into_type) -> Self + { + self.[](value.into()) + } + + /// Private method, for use in the [`Default`] implementation. + fn [<$field _default>]() -> $try_into_type { + Default::default() + } + + ); + }; + + // Generates the public accessors for fields not converted to a type. + ( + @public_field_accessors $vis:vis $name:ident $storage:ty : $hi:tt:= $lo:tt $field:ident + $(, $comment:literal)? + ) =3D> { + ::kernel::macros::paste!( + + $( + #[doc=3D"Returns the value of this field:"] + #[doc=3D$comment] + )? + #[inline(always)] + $vis fn $field(self) -> + ::kernel::num::BitInt<$storage, { $hi + 1 - $lo }> + { + self.[<$field _internal>]() + } + + $( + #[doc=3D"Sets the value of this field:"] + #[doc=3D$comment] + )? + #[inline(always)] + $vis fn []( + self, + value: T, + ) -> Self + where T: Into<::kernel::num::BitInt<$storage, { $hi + 1 - $lo = }>>, + { + self.[](value.into()) + } + + $( + #[doc=3D"Attempts to set the value of this field:"] + #[doc=3D$comment] + )? + #[inline(always)] + $vis fn []( + self, + value: T, + ) -> ::kernel::error::Result + where T: ::kernel::num::TryIntoBitInt<$storage, { $hi + 1 - $l= o }>, + { + Ok( + self.[]( + value.try_into_bitint().ok_or(::kernel::error::code::E= OVERFLOW)? + ) + ) + } + + /// Private method, for use in the [`Default`] implementation. + fn [<$field _default>]() -> ::kernel::num::BitInt<$storage, { $hi = + 1 - $lo }> { + Default::default() + } + ); }; =20 @@ -319,7 +355,7 @@ fn default() -> Self { =20 ::kernel::macros::paste!( $( - value.[](Default::default()); + value.[](Self::[<$field _default>]()); )* ); =20 diff --git a/drivers/gpu/nova-core/falcon.rs b/drivers/gpu/nova-core/falcon= .rs index fb3561cc9746..7d85a01ea06e 100644 --- a/drivers/gpu/nova-core/falcon.rs +++ b/drivers/gpu/nova-core/falcon.rs @@ -7,6 +7,7 @@ use kernel::device; use kernel::dma::DmaAddress; use kernel::io::poll::read_poll_timeout; +use kernel::num::{self, BitInt}; use kernel::prelude::*; use kernel::sync::aref::ARef; use kernel::time::delay::fsleep; @@ -23,11 +24,14 @@ pub(crate) mod sec2; =20 // TODO[FPRI]: Replace with `ToPrimitive`. -macro_rules! impl_from_enum_to_u8 { - ($enum_type:ty) =3D> { - impl From<$enum_type> for u8 { +macro_rules! impl_from_enum_to_bounded { + ($enum_type:ty, $length:literal) =3D> { + impl From<$enum_type> for BitInt + where + T: From + num::Boundable, + { fn from(value: $enum_type) -> Self { - value as u8 + BitInt::from_expr(T::from(value as u8)) } } }; @@ -47,16 +51,19 @@ pub(crate) enum FalconCoreRev { Rev6 =3D 6, Rev7 =3D 7, } -impl_from_enum_to_u8!(FalconCoreRev); +impl_from_enum_to_bounded!(FalconCoreRev, 4); =20 // TODO[FPRI]: replace with `FromPrimitive`. -impl TryFrom for FalconCoreRev { +impl TryFrom> for FalconCoreRev +where + T: num::Boundable + num::Integer, +{ type Error =3D Error; =20 - fn try_from(value: u8) -> Result { + fn try_from(value: BitInt) -> Result { use FalconCoreRev::*; =20 - let rev =3D match value { + let rev =3D match u8::from(value) { 1 =3D> Rev1, 2 =3D> Rev2, 3 =3D> Rev3, @@ -82,24 +89,25 @@ pub(crate) enum FalconCoreRevSubversion { Subversion2 =3D 2, Subversion3 =3D 3, } -impl_from_enum_to_u8!(FalconCoreRevSubversion); +impl_from_enum_to_bounded!(FalconCoreRevSubversion, 2); =20 // TODO[FPRI]: replace with `FromPrimitive`. -impl TryFrom for FalconCoreRevSubversion { - type Error =3D Error; - - fn try_from(value: u8) -> Result { +impl From> for FalconCoreRevSubversion +where + T: num::Boundable + num::Integer, +{ + fn from(value: BitInt) -> Self { use FalconCoreRevSubversion::*; =20 - let sub_version =3D match value & 0b11 { + match u8::from(value) { 0 =3D> Subversion0, 1 =3D> Subversion1, 2 =3D> Subversion2, 3 =3D> Subversion3, - _ =3D> return Err(EINVAL), - }; - - Ok(sub_version) + // TODO: somehow the compiler cannot infer that `value` cannot= be > 3. Find a way to + // handle this gracefully, or switch back to fallible ops. + _ =3D> panic!(), + } } } =20 @@ -126,16 +134,19 @@ pub(crate) enum FalconSecurityModel { /// Also known as High-Secure, Privilege Level 3 or PL3. Heavy =3D 3, } -impl_from_enum_to_u8!(FalconSecurityModel); +impl_from_enum_to_bounded!(FalconSecurityModel, 2); =20 // TODO[FPRI]: replace with `FromPrimitive`. -impl TryFrom for FalconSecurityModel { +impl TryFrom> for FalconSecurityModel +where + T: num::Boundable + num::Integer, +{ type Error =3D Error; =20 - fn try_from(value: u8) -> Result { + fn try_from(value: BitInt) -> Result { use FalconSecurityModel::*; =20 - let sec_model =3D match value { + let sec_model =3D match u8::from(value) { 0 =3D> None, 2 =3D> Light, 3 =3D> Heavy, @@ -158,14 +169,17 @@ pub(crate) enum FalconModSelAlgo { #[default] Rsa3k =3D 1, } -impl_from_enum_to_u8!(FalconModSelAlgo); +impl_from_enum_to_bounded!(FalconModSelAlgo, 8); =20 // TODO[FPRI]: replace with `FromPrimitive`. -impl TryFrom for FalconModSelAlgo { +impl TryFrom> for FalconModSelAlgo +where + T: num::Boundable + num::Integer, +{ type Error =3D Error; =20 - fn try_from(value: u8) -> Result { - match value { + fn try_from(value: BitInt) -> Result { + match u8::from(value) { 1 =3D> Ok(FalconModSelAlgo::Rsa3k), _ =3D> Err(EINVAL), } @@ -180,14 +194,17 @@ pub(crate) enum DmaTrfCmdSize { #[default] Size256B =3D 0x6, } -impl_from_enum_to_u8!(DmaTrfCmdSize); +impl_from_enum_to_bounded!(DmaTrfCmdSize, 3); =20 // TODO[FPRI]: replace with `FromPrimitive`. -impl TryFrom for DmaTrfCmdSize { +impl TryFrom> for DmaTrfCmdSize +where + T: num::Boundable + num::Integer, +{ type Error =3D Error; =20 - fn try_from(value: u8) -> Result { - match value { + fn try_from(value: BitInt) -> Result { + match u8::from(value) { 0x6 =3D> Ok(Self::Size256B), _ =3D> Err(EINVAL), } @@ -203,25 +220,20 @@ pub(crate) enum PeregrineCoreSelect { /// RISC-V core is active. Riscv =3D 1, } +impl_from_enum_to_bounded!(PeregrineCoreSelect, 1); =20 -impl From for PeregrineCoreSelect { - fn from(value: bool) -> Self { - match value { +impl From> for PeregrineCoreSelect +where + T: num::Boundable + Zeroable, +{ + fn from(value: BitInt) -> Self { + match bool::from(value) { false =3D> PeregrineCoreSelect::Falcon, true =3D> PeregrineCoreSelect::Riscv, } } } =20 -impl From for bool { - fn from(value: PeregrineCoreSelect) -> Self { - match value { - PeregrineCoreSelect::Falcon =3D> false, - PeregrineCoreSelect::Riscv =3D> true, - } - } -} - /// Different types of memory present in a falcon core. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) enum FalconMem { @@ -245,14 +257,17 @@ pub(crate) enum FalconFbifTarget { /// Non-coherent system memory (System DRAM). NoncoherentSysmem =3D 2, } -impl_from_enum_to_u8!(FalconFbifTarget); +impl_from_enum_to_bounded!(FalconFbifTarget, 2); =20 // TODO[FPRI]: replace with `FromPrimitive`. -impl TryFrom for FalconFbifTarget { +impl TryFrom> for FalconFbifTarget +where + T: num::Boundable + num::Integer, +{ type Error =3D Error; =20 - fn try_from(value: u8) -> Result { - let res =3D match value { + fn try_from(value: BitInt) -> Result { + let res =3D match u8::from(value) { 0 =3D> Self::LocalFb, 1 =3D> Self::CoherentSysmem, 2 =3D> Self::NoncoherentSysmem, @@ -272,26 +287,21 @@ pub(crate) enum FalconFbifMemType { /// Physical memory addresses. Physical =3D 1, } +impl_from_enum_to_bounded!(FalconFbifMemType, 1); =20 /// Conversion from a single-bit register field. -impl From for FalconFbifMemType { - fn from(value: bool) -> Self { - match value { +impl From> for FalconFbifMemType +where + T: num::Boundable + Zeroable, +{ + fn from(value: BitInt) -> Self { + match bool::from(value) { false =3D> Self::Virtual, true =3D> Self::Physical, } } } =20 -impl From for bool { - fn from(value: FalconFbifMemType) -> Self { - match value { - FalconFbifMemType::Virtual =3D> false, - FalconFbifMemType::Physical =3D> true, - } - } -} - /// Type used to represent the `PFALCON` registers address base for a give= n falcon engine. pub(crate) struct PFalconBase(()); =20 @@ -414,7 +424,7 @@ pub(crate) fn reset(&self, bar: &Bar0) -> Result { self.reset_wait_mem_scrubbing(bar)?; =20 regs::NV_PFALCON_FALCON_RM::default() - .set_value(regs::NV_PMC_BOOT_0::read(bar).into()) + .set_value(u32::from(regs::NV_PMC_BOOT_0::read(bar))) .write(bar, &E::ID); =20 Ok(()) @@ -481,18 +491,18 @@ fn dma_wr>( .set_base((dma_start >> 8) as u32) .write(bar, &E::ID); regs::NV_PFALCON_FALCON_DMATRFBASE1::default() - .set_base((dma_start >> 40) as u16) + .try_set_base(dma_start >> 40)? .write(bar, &E::ID); =20 let cmd =3D regs::NV_PFALCON_FALCON_DMATRFCMD::default() .set_size(DmaTrfCmdSize::Size256B) .set_imem(target_mem =3D=3D FalconMem::Imem) - .set_sec(if sec { 1 } else { 0 }); + .set_sec(BitInt::from_expr(if sec { 1 } else { 0 })); =20 for pos in (0..num_transfers).map(|i| i * DMA_LEN) { // Perform a transfer of size `DMA_LEN`. regs::NV_PFALCON_FALCON_DMATRFMOFFS::default() - .set_offs(load_offsets.dst_start + pos) + .try_set_offs(load_offsets.dst_start + pos)? .write(bar, &E::ID); regs::NV_PFALCON_FALCON_DMATRFFBOFFS::default() .set_offs(src_start + pos) diff --git a/drivers/gpu/nova-core/falcon/hal/ga102.rs b/drivers/gpu/nova-c= ore/falcon/hal/ga102.rs index afed353b24d2..c43e48823eff 100644 --- a/drivers/gpu/nova-core/falcon/hal/ga102.rs +++ b/drivers/gpu/nova-core/falcon/hal/ga102.rs @@ -51,7 +51,7 @@ fn signature_reg_fuse_version_ga102( =20 // `ucode_idx` is guaranteed to be in the range [0..15], making the `r= ead` calls provable valid // at build-time. - let reg_fuse_version =3D if engine_id_mask & 0x0001 !=3D 0 { + let reg_fuse_version: u16 =3D if engine_id_mask & 0x0001 !=3D 0 { regs::NV_FUSE_OPT_FPF_SEC2_UCODE1_VERSION::read(bar, ucode_idx).da= ta() } else if engine_id_mask & 0x0004 !=3D 0 { regs::NV_FUSE_OPT_FPF_NVDEC_UCODE1_VERSION::read(bar, ucode_idx).d= ata() @@ -60,7 +60,8 @@ fn signature_reg_fuse_version_ga102( } else { dev_err!(dev, "unexpected engine_id_mask {:#x}", engine_id_mask); return Err(EINVAL); - }; + } + .into(); =20 // TODO[NUMM]: replace with `last_set_bit` once it lands. Ok(u16::BITS - reg_fuse_version.leading_zeros()) diff --git a/drivers/gpu/nova-core/fb/hal/ga100.rs b/drivers/gpu/nova-core/= fb/hal/ga100.rs index 871c42bf033a..5b55ca8aaddb 100644 --- a/drivers/gpu/nova-core/fb/hal/ga100.rs +++ b/drivers/gpu/nova-core/fb/hal/ga100.rs @@ -2,6 +2,7 @@ =20 struct Ga100; =20 +use kernel::num::BitInt; use kernel::prelude::*; =20 use crate::driver::Bar0; @@ -18,7 +19,7 @@ pub(super) fn read_sysmem_flush_page_ga100(bar: &Bar0) ->= u64 { =20 pub(super) fn write_sysmem_flush_page_ga100(bar: &Bar0, addr: u64) { regs::NV_PFB_NISO_FLUSH_SYSMEM_ADDR_HI::default() - .set_adr_63_40((addr >> FLUSH_SYSMEM_ADDR_SHIFT_HI) as u32) + .set_adr_63_40(BitInt::from_expr(addr >> FLUSH_SYSMEM_ADDR_SHIFT_H= I).cast()) .write(bar); regs::NV_PFB_NISO_FLUSH_SYSMEM_ADDR::default() .set_adr_39_08((addr >> FLUSH_SYSMEM_ADDR_SHIFT) as u32) diff --git a/drivers/gpu/nova-core/gpu.rs b/drivers/gpu/nova-core/gpu.rs index 9d182bffe8b4..2db3e48ea59f 100644 --- a/drivers/gpu/nova-core/gpu.rs +++ b/drivers/gpu/nova-core/gpu.rs @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 =20 +use kernel::num::BitInt; use kernel::{device, devres::Devres, error::code::*, fmt, pci, prelude::*,= sync::Arc}; =20 use crate::driver::Bar0; @@ -130,15 +131,15 @@ fn try_from(value: u8) -> Result { } =20 pub(crate) struct Revision { - major: u8, - minor: u8, + major: BitInt, + minor: BitInt, } =20 impl Revision { fn from_boot0(boot0: regs::NV_PMC_BOOT_0) -> Self { Self { - major: boot0.major_revision(), - minor: boot0.minor_revision(), + major: boot0.major_revision().cast(), + minor: boot0.minor_revision().cast(), } } } diff --git a/drivers/gpu/nova-core/regs.rs b/drivers/gpu/nova-core/regs.rs index 206dab2e1335..1542d72e4a65 100644 --- a/drivers/gpu/nova-core/regs.rs +++ b/drivers/gpu/nova-core/regs.rs @@ -17,18 +17,19 @@ // PMC =20 register!(NV_PMC_BOOT_0 @ 0x00000000, "Basic revision information about th= e GPU" { - 3:0 minor_revision as u8, "Minor revision of the chip"; - 7:4 major_revision as u8, "Major revision of the chip"; - 8:8 architecture_1 as u8, "MSB of the architecture"; - 23:20 implementation as u8, "Implementation version of the architect= ure"; - 28:24 architecture_0 as u8, "Lower bits of the architecture"; + 3:0 minor_revision, "Minor revision of the chip"; + 7:4 major_revision, "Major revision of the chip"; + 8:8 architecture_1, "MSB of the architecture"; + 23:20 implementation, "Implementation version of the architecture"; + 28:24 architecture_0, "Lower bits of the architecture"; }); =20 impl NV_PMC_BOOT_0 { /// Combines `architecture_0` and `architecture_1` to obtain the archi= tecture of the chip. pub(crate) fn architecture(self) -> Result { Architecture::try_from( - self.architecture_0() | (self.architecture_1() << Self::ARCHIT= ECTURE_0_RANGE.len()), + u8::from(self.architecture_0()) + | (u8::from(self.architecture_1()) << Self::ARCHITECTURE_0= _RANGE.len()), ) } =20 @@ -49,7 +50,7 @@ pub(crate) fn chipset(self) -> Result { =20 register!(NV_PBUS_SW_SCRATCH_0E_FRTS_ERR =3D> NV_PBUS_SW_SCRATCH[0xe], "scratch register 0xe used as FRTS firmware error code" { - 31:16 frts_err_code as u16; + 31:16 frts_err_code; }); =20 // PFB @@ -58,17 +59,17 @@ pub(crate) fn chipset(self) -> Result { // GPU to perform sysmembar operations (see `fb::SysmemFlush`). =20 register!(NV_PFB_NISO_FLUSH_SYSMEM_ADDR @ 0x00100c10 { - 31:0 adr_39_08 as u32; + 31:0 adr_39_08; }); =20 register!(NV_PFB_NISO_FLUSH_SYSMEM_ADDR_HI @ 0x00100c40 { - 23:0 adr_63_40 as u32; + 23:0 adr_63_40; }); =20 register!(NV_PFB_PRI_MMU_LOCAL_MEMORY_RANGE @ 0x00100ce0 { - 3:0 lower_scale as u8; - 9:4 lower_mag as u8; - 30:30 ecc_mode_enabled as bool; + 3:0 lower_scale; + 9:4 lower_mag; + 30:30 ecc_mode_enabled =3D> bool; }); =20 impl NV_PFB_PRI_MMU_LOCAL_MEMORY_RANGE { @@ -87,7 +88,7 @@ pub(crate) fn usable_fb_size(self) -> u64 { } =20 register!(NV_PFB_PRI_MMU_WPR2_ADDR_LO@0x001fa824 { - 31:4 lo_val as u32, "Bits 12..40 of the lower (inclusive) bound of = the WPR2 region"; + 31:4 lo_val, "Bits 12..40 of the lower (inclusive) bound of the WPR= 2 region"; }); =20 impl NV_PFB_PRI_MMU_WPR2_ADDR_LO { @@ -98,7 +99,7 @@ pub(crate) fn lower_bound(self) -> u64 { } =20 register!(NV_PFB_PRI_MMU_WPR2_ADDR_HI@0x001fa828 { - 31:4 hi_val as u32, "Bits 12..40 of the higher (exclusive) bound of= the WPR2 region"; + 31:4 hi_val, "Bits 12..40 of the higher (exclusive) bound of the WP= R2 region"; }); =20 impl NV_PFB_PRI_MMU_WPR2_ADDR_HI { @@ -123,7 +124,7 @@ pub(crate) fn higher_bound(self) -> u64 { // `PGC6_AON_SECURE_SCRATCH_GROUP_05` register (which it needs to read GFW= _BOOT). register!(NV_PGC6_AON_SECURE_SCRATCH_GROUP_05_PRIV_LEVEL_MASK @ 0x00118128, "Privilege level mask register" { - 0:0 read_protection_level0 as bool, "Set after FWSEC lowers its pr= otection level"; + 0:0 read_protection_level0 =3D> bool, "Set after FWSEC lowers its = protection level"; }); =20 // OpenRM defines this as a register array, but doesn't specify its size a= nd only uses its first @@ -133,7 +134,7 @@ pub(crate) fn higher_bound(self) -> u64 { register!( NV_PGC6_AON_SECURE_SCRATCH_GROUP_05_0_GFW_BOOT =3D> NV_PGC6_AON_SECURE= _SCRATCH_GROUP_05[0], "Scratch group 05 register 0 used as GFW boot progress indicator" { - 7:0 progress as u8, "Progress of GFW boot (0xff means completed= )"; + 7:0 progress, "Progress of GFW boot (0xff means completed)"; } ); =20 @@ -145,13 +146,13 @@ pub(crate) fn completed(self) -> bool { } =20 register!(NV_PGC6_AON_SECURE_SCRATCH_GROUP_42 @ 0x001183a4 { - 31:0 value as u32; + 31:0 value; }); =20 register!( NV_USABLE_FB_SIZE_IN_MB =3D> NV_PGC6_AON_SECURE_SCRATCH_GROUP_42, "Scratch group 42 register used as framebuffer size" { - 31:0 value as u32, "Usable framebuffer size, in megabytes"; + 31:0 value, "Usable framebuffer size, in megabytes"; } ); =20 @@ -165,8 +166,8 @@ pub(crate) fn usable_fb_size(self) -> u64 { // PDISP =20 register!(NV_PDISP_VGA_WORKSPACE_BASE @ 0x00625f04 { - 3:3 status_valid as bool, "Set if the `addr` field is valid"; - 31:8 addr as u32, "VGA workspace base address divided by 0x10000"; + 3:3 status_valid =3D> bool, "Set if the `addr` field is valid"; + 31:8 addr, "VGA workspace base address divided by 0x10000"; }); =20 impl NV_PDISP_VGA_WORKSPACE_BASE { @@ -185,40 +186,40 @@ pub(crate) fn vga_workspace_addr(self) -> Option= { pub(crate) const NV_FUSE_OPT_FPF_SIZE: usize =3D 16; =20 register!(NV_FUSE_OPT_FPF_NVDEC_UCODE1_VERSION @ 0x00824100[NV_FUSE_OPT_FP= F_SIZE] { - 15:0 data as u16; + 15:0 data; }); =20 register!(NV_FUSE_OPT_FPF_SEC2_UCODE1_VERSION @ 0x00824140[NV_FUSE_OPT_FPF= _SIZE] { - 15:0 data as u16; + 15:0 data; }); =20 register!(NV_FUSE_OPT_FPF_GSP_UCODE1_VERSION @ 0x008241c0[NV_FUSE_OPT_FPF_= SIZE] { - 15:0 data as u16; + 15:0 data; }); =20 // PFALCON =20 register!(NV_PFALCON_FALCON_IRQSCLR @ PFalconBase[0x00000004] { - 4:4 halt as bool; - 6:6 swgen0 as bool; + 4:4 halt =3D> bool; + 6:6 swgen0 =3D> bool; }); =20 register!(NV_PFALCON_FALCON_MAILBOX0 @ PFalconBase[0x00000040] { - 31:0 value as u32; + 31:0 value =3D> u32; }); =20 register!(NV_PFALCON_FALCON_MAILBOX1 @ PFalconBase[0x00000044] { - 31:0 value as u32; + 31:0 value =3D> u32; }); =20 register!(NV_PFALCON_FALCON_RM @ PFalconBase[0x00000084] { - 31:0 value as u32; + 31:0 value =3D> u32; }); =20 register!(NV_PFALCON_FALCON_HWCFG2 @ PFalconBase[0x000000f4] { - 10:10 riscv as bool; - 12:12 mem_scrubbing as bool, "Set to 0 after memory scrubbing is com= pleted"; - 31:31 reset_ready as bool, "Signal indicating that reset is complete= d (GA102+)"; + 10:10 riscv =3D> bool; + 12:12 mem_scrubbing =3D> bool, "Set to 0 after memory scrubbing is c= ompleted"; + 31:31 reset_ready =3D> bool, "Signal indicating that reset is comple= ted (GA102+)"; }); =20 impl NV_PFALCON_FALCON_HWCFG2 { @@ -229,101 +230,101 @@ pub(crate) fn mem_scrubbing_done(self) -> bool { } =20 register!(NV_PFALCON_FALCON_CPUCTL @ PFalconBase[0x00000100] { - 1:1 startcpu as bool; - 4:4 halted as bool; - 6:6 alias_en as bool; + 1:1 startcpu =3D> bool; + 4:4 halted =3D> bool; + 6:6 alias_en =3D> bool; }); =20 register!(NV_PFALCON_FALCON_BOOTVEC @ PFalconBase[0x00000104] { - 31:0 value as u32; + 31:0 value =3D> u32; }); =20 register!(NV_PFALCON_FALCON_DMACTL @ PFalconBase[0x0000010c] { - 0:0 require_ctx as bool; - 1:1 dmem_scrubbing as bool; - 2:2 imem_scrubbing as bool; - 6:3 dmaq_num as u8; - 7:7 secure_stat as bool; + 0:0 require_ctx =3D> bool; + 1:1 dmem_scrubbing =3D> bool; + 2:2 imem_scrubbing =3D> bool; + 6:3 dmaq_num; + 7:7 secure_stat =3D> bool; }); =20 register!(NV_PFALCON_FALCON_DMATRFBASE @ PFalconBase[0x00000110] { - 31:0 base as u32; + 31:0 base =3D> u32; }); =20 register!(NV_PFALCON_FALCON_DMATRFMOFFS @ PFalconBase[0x00000114] { - 23:0 offs as u32; + 23:0 offs; }); =20 register!(NV_PFALCON_FALCON_DMATRFCMD @ PFalconBase[0x00000118] { - 0:0 full as bool; - 1:1 idle as bool; - 3:2 sec as u8; - 4:4 imem as bool; - 5:5 is_write as bool; - 10:8 size as u8 ?=3D> DmaTrfCmdSize; - 14:12 ctxdma as u8; - 16:16 set_dmtag as u8; + 0:0 full =3D> bool; + 1:1 idle =3D> bool; + 3:2 sec; + 4:4 imem =3D> bool; + 5:5 is_write =3D> bool; + 10:8 size ?=3D> DmaTrfCmdSize; + 14:12 ctxdma; + 16:16 set_dmtag; }); =20 register!(NV_PFALCON_FALCON_DMATRFFBOFFS @ PFalconBase[0x0000011c] { - 31:0 offs as u32; + 31:0 offs =3D> u32; }); =20 register!(NV_PFALCON_FALCON_DMATRFBASE1 @ PFalconBase[0x00000128] { - 8:0 base as u16; + 8:0 base; }); =20 register!(NV_PFALCON_FALCON_HWCFG1 @ PFalconBase[0x0000012c] { - 3:0 core_rev as u8 ?=3D> FalconCoreRev, "Core revision"; - 5:4 security_model as u8 ?=3D> FalconSecurityModel, "Security mode= l"; - 7:6 core_rev_subversion as u8 ?=3D> FalconCoreRevSubversion, "Core= revision subversion"; + 3:0 core_rev ?=3D> FalconCoreRev, "Core revision"; + 5:4 security_model ?=3D> FalconSecurityModel, "Security model"; + 7:6 core_rev_subversion =3D> FalconCoreRevSubversion, "Core revisi= on subversion"; }); =20 register!(NV_PFALCON_FALCON_CPUCTL_ALIAS @ PFalconBase[0x00000130] { - 1:1 startcpu as bool; + 1:1 startcpu =3D> bool; }); =20 // Actually known as `NV_PSEC_FALCON_ENGINE` and `NV_PGSP_FALCON_ENGINE` d= epending on the falcon // instance. register!(NV_PFALCON_FALCON_ENGINE @ PFalconBase[0x000003c0] { - 0:0 reset as bool; + 0:0 reset =3D> bool; }); =20 register!(NV_PFALCON_FBIF_TRANSCFG @ PFalconBase[0x00000600[8]] { - 1:0 target as u8 ?=3D> FalconFbifTarget; - 2:2 mem_type as bool =3D> FalconFbifMemType; + 1:0 target ?=3D> FalconFbifTarget; + 2:2 mem_type =3D> FalconFbifMemType; }); =20 register!(NV_PFALCON_FBIF_CTL @ PFalconBase[0x00000624] { - 7:7 allow_phys_no_ctx as bool; + 7:7 allow_phys_no_ctx =3D> bool; }); =20 /* PFALCON2 */ =20 register!(NV_PFALCON2_FALCON_MOD_SEL @ PFalcon2Base[0x00000180] { - 7:0 algo as u8 ?=3D> FalconModSelAlgo; + 7:0 algo ?=3D> FalconModSelAlgo; }); =20 register!(NV_PFALCON2_FALCON_BROM_CURR_UCODE_ID @ PFalcon2Base[0x00000198]= { - 7:0 ucode_id as u8; + 7:0 ucode_id =3D> u8; }); =20 register!(NV_PFALCON2_FALCON_BROM_ENGIDMASK @ PFalcon2Base[0x0000019c] { - 31:0 value as u32; + 31:0 value =3D> u32; }); =20 // OpenRM defines this as a register array, but doesn't specify its size a= nd only uses its first // element. Be conservative until we know the actual size or need to use m= ore registers. register!(NV_PFALCON2_FALCON_BROM_PARAADDR @ PFalcon2Base[0x00000210[1]] { - 31:0 value as u32; + 31:0 value =3D> u32; }); =20 // PRISCV =20 register!(NV_PRISCV_RISCV_BCR_CTRL @ PFalconBase[0x00001668] { - 0:0 valid as bool; - 4:4 core_select as bool =3D> PeregrineCoreSelect; - 8:8 br_fetch as bool; + 0:0 valid =3D> bool; + 4:4 core_select =3D> PeregrineCoreSelect; + 8:8 br_fetch =3D> bool; }); =20 // The modules below provide registers that are not identical on all suppo= rted chips. They should @@ -333,7 +334,7 @@ pub(crate) mod gm107 { // FUSE =20 register!(NV_FUSE_STATUS_OPT_DISPLAY @ 0x00021c04 { - 0:0 display_disabled as bool; + 0:0 display_disabled =3D> bool; }); } =20 @@ -341,6 +342,6 @@ pub(crate) mod ga100 { // FUSE =20 register!(NV_FUSE_STATUS_OPT_DISPLAY @ 0x00820c04 { - 0:0 display_disabled as bool; + 0:0 display_disabled =3D> bool; }); } --=20 2.51.0