From nobody Sun Feb 8 15:01:11 2026 Received: from SN4PR0501CU005.outbound.protection.outlook.com (mail-southcentralusazon11011034.outbound.protection.outlook.com [40.93.194.34]) (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 2546F2BE7CD; Sat, 8 Nov 2025 02:24:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.93.194.34 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762568669; cv=fail; b=OkAK8bJ9wlSBnOcsuNqrsdqie1RUBYKR8L8gFSD1AR0G+VFHFQ5D9hI4fGfy/8S/1EOZtIkAlvjxcnlIXAuJYGY5B7Okg91XwyEsRMbmfaY3Tp9Zucok4UjU6YW7WGCNAIAlOXTokXE6Rh0z1zRPJ4Q1lNhlCGBaA3V24TLuQ+A= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762568669; c=relaxed/simple; bh=Be46L1a4in2dntzwbEEoYGQaw7hjSDxTjTLGAOL9eek=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=uiTwKaOG47svIRCEJ78Ji7dVKSHjX+VmYIqRsGt7UpQC5rcKyRNMTFSzTsi/QBoe6t2ctWagCuVtBeukumlZ+BAOlnjPqlpUvN7w5zA2sGxO6nHT2e8OyfKcRrXEF7LWuhFug2Rtm/D58qsUrBVUZgmf/L0SMXUuqXTguE/XXWI= 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=QBmSx6VU; arc=fail smtp.client-ip=40.93.194.34 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="QBmSx6VU" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=TFa9OHrMw26iq1Vf0ZN/CV2HRGn3MDj4FOb3YnzcnWKUr9vFQ55mbtlQC0YwRxYjUJKGumAGixLjW/9uLWpT/agO3jKtJV0tMmM6QfTEBXSM7eau0tKbsumfTeMiUvr31ifxfDHuZ+evnM/S4lqsRhxsAzDaCHziZVm5bZ7fZb/urV2DsAHPSViC4i8dT8pJzrKAWwIi78ZBHfPaF8Hn8CCneHr/8jDGAQnLEIEATQZLa+aAws/p6XxQ/H92xq/XoIB437noDzeO6NC+eEkD5cndM+cIJAcGoMj6xlf4ldMBreoLrmWkrqG2FPPLOP05OgGv69YDPgF0sGQDplixgA== 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=E+oGofHLK6cwft5AVPzLl/T3zAbIgZ2buxX45oDcgIY=; b=x1cXIK7ukUQ4pndVmWXoQYC0gbx2inxX0F4ZquuxxKmHHI2KKb3GuoTG5l6DBGbJFxSOdK6/6DkDSpi1lZwbzn2Q7EGV8LMgY0P4fXCwv0z+qBCQyhrxXqmJhONONCnPpDeOsTKceclDHXJNo3oALB16IeI/1WaDqJFM+pMmZkNVKQfpIj08uGPOYEloqb2AHABw6Bz0J+mtgGSw5Uztey9TrSPe4R6sxM765kZT2rNneEFpon4dBxNscpuWevq/19FHH51bpsLRiQSHwoTX/K6pWK/z6srA9zQDmkRhQe/rvNudCH+rIBpPXlmA398nhyf9RvNRiuV9WPSfBlFNuw== 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=E+oGofHLK6cwft5AVPzLl/T3zAbIgZ2buxX45oDcgIY=; b=QBmSx6VUb7XRpoqxpUrzidwC/6LJwLS7ASTIfzz1vOMWn2Y1nsJYGOAlQmvSeT6sV53rqJ+NiD2N+uBg57Ai14LG8Bjy0lY6tCySnNc8UPFTsNj/e88B+Gb13dUHNmmnRv+s3f23gFdVLQnTC25h9r043hts+tDmtugYryHV+RbUqFAE1EIVbyr62xZBQ0OfAC6Etd+ntNtzF40ZadEO2Dd19GqJavRs1I2I3xkY3fWofM7wdZs0rdD8+ZwTszO9hmngGR8xJYl1l/VamJnLRDn1CVood1IzYS4V/8QUQ2I5xP6tfS2ZjckC3APSS4oq5Y/8j9AY+QYS44WZNZsSfA== 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 MN0PR12MB6149.namprd12.prod.outlook.com (2603:10b6:208:3c7::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9275.16; Sat, 8 Nov 2025 02:24:18 +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.9298.012; Sat, 8 Nov 2025 02:24:18 +0000 From: Alexandre Courbot Date: Sat, 08 Nov 2025 11:23:50 +0900 Subject: [PATCH FOR REFERENCE v4 4/4] gpu: nova-core: use BitInt for bitfields Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251108-bounded_ints-v4-4-c9342ac7ebd1@nvidia.com> References: <20251108-bounded_ints-v4-0-c9342ac7ebd1@nvidia.com> In-Reply-To: <20251108-bounded_ints-v4-0-c9342ac7ebd1@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: TYCPR01CA0098.jpnprd01.prod.outlook.com (2603:1096:405:4::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_|MN0PR12MB6149:EE_ X-MS-Office365-Filtering-Correlation-Id: ef56f6e0-3ad4-426d-ea01-08de1e6deb77 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|10070799003|7416014|376014|1800799024|366016|921020; X-Microsoft-Antispam-Message-Info: =?utf-8?B?TFRUTHhZTE1xMmpKaVcyaldLU1N5R0g4VktKYngybW56VW9uUDlOS0EzeDBm?= =?utf-8?B?YjdoQXd3YU9EUytuVFViV3ZTWG51WnFuL055REw1anEvd1pCNXNXejdmNm5U?= =?utf-8?B?TWVKZ1p6MGE3a1BiNUl1SG9mZ0VHZWRvTm5ya0RUUHltYm42M3dHVERla2JM?= =?utf-8?B?TEtyQzhUT0o1VjVmS1RSNGxPSDhFTTFPVDh2OUlPOForcjhSSTVUbjN5czNn?= =?utf-8?B?dVRVaEhZT3ZyT2VzMGYzUms4Zm9ycDRKVEY1NWZWRUo0b3kzYTl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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)(10070799003)(7416014)(376014)(1800799024)(366016)(921020);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?M3NQZ1dzN3hZU3RaemtjUVdxbVVQU1ljYUJSdENvdlhONVJWSnYwazZQNE1p?= =?utf-8?B?MmpsaEE5N0FSQWlMWlVLd0FoalJSWGV6V215TVlHSVorQVQ1TEVzeTN4di8x?= =?utf-8?B?Y2dtdERQQVRVaFZnQUtDRFg2cjd4SFlSakY4blU3cHlGUFBwWjVBNVY1a2gr?= =?utf-8?B?aWdtRVViNkVIRGVvcmN0UXJlYU8wRk5FUE0wa2NoNFRCdTVWNitzd1BpRENl?= =?utf-8?B?c0xQQ2xhSm9zQTZ0cTM0ZkVDcVk2eUdGRkxCMkM3V0tHeHJLVkhUUWhwcUVX?= =?utf-8?B?cnV2ajJPeWJMdzUxZFVWdTl5L3JnTEVFVC85Y1pSSVhhZm8wMzkxdHBBWTVx?= =?utf-8?B?VUtQREowTVdmajdYYjllU1BLeFJ6U0dJTklrN3J2ZVNYNUlLZGcxV3NxS0to?= =?utf-8?B?YTV1dmlEY08xWXFqNWRjTkgxNVZwK0RGLzVmS2l0dDFKV0k2Y0xYaSttbUVv?= =?utf-8?B?NUZHTTlGSXUwQ2JWZWlMbW5yVkpBb2pzd2V0QjlVMHNKTTVpeGp2c1I3eG5j?= =?utf-8?B?eU1GdytsOTBjVndmN21aRXBCUDJwTlhaWmFSSFBnRkFNaHdmTmY3bFQ4bFFH?= =?utf-8?B?RjRBWkkvRitsTnNyNVhMU2JYOC9RQjFkaGpsRnB3OE5yL3d1N1R6Vzd4VU5o?= =?utf-8?B?SndidUxKenc1RzUzTnB0L3YrUXdNVW9LTGptYXp4RkNTQ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VFhGa2lCdFgwUUV3WTNmNFBhNDZEekdSbURq?= =?utf-8?B?U3VPNWZSZWMzN09jckR2YzRLV3laWkh4NUtqdFhiakNDcWNJbkZ0UnhYQjZl?= =?utf-8?B?bldIQ29hbHBSRm5teFNxcjJVaTdTNEdUWUdmSFhmbTMvQnVhVzhMK2c0SDVK?= =?utf-8?B?eldva2xhTW13bHhkMDZ2TkZDd05xNE5xNER2ZDBaTU5pVEk5Y2J2NC9XRGhq?= =?utf-8?B?ckpOV2crNDJOY0dFbU9TaVUveGdEbndNa1ZicUNBaStpelB0cnFnRzk4TTR2?= =?utf-8?B?aXR1eXZMT3FqWFJrZEdCT28xc1Z5VEw4Y0ErbVBJT1RxdTFRbUk1NHRFamhx?= =?utf-8?B?YUpHV1VmS3NlSno2eGFUejJRaHBGaGhhaGF4bTlSZkFVRERPZzRoTDdOemth?= =?utf-8?B?VjUwNHhaamR2a1ZCTW1XNGlFdmZQRDIvQUo2UnZtNld6cDdQOHQwRjBybThU?= =?utf-8?B?dkoxNzVVaENQMkgwajNXSmtWSXRZbDFNM0pqK09WYm90Mjh0UllPUjVTK1ph?= =?utf-8?B?anFLRkJkNzUwdG9YcHVqUERRbGxCL3IrMEZOd0FOVFVodGRWeWJCcGpCclR6?= =?utf-8?Q?t2p30lRDS4SYv2JYhpsSLa8cbI+xem6IvZWVs19SxrrY?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: ef56f6e0-3ad4-426d-ea01-08de1e6deb77 X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB3990.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2025 02:24:18.6944 (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: ikZnHs4ViwNvFPPFJVWEmUzFkrqdlzgzVAZKsyIZUei3RPJmVj9pEkIgxjNVcyUd6H8JZEkNArr3Io2JXgIb6A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6149 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 | 145 ++++++------ drivers/gpu/nova-core/falcon/hal/ga102.rs | 5 +- drivers/gpu/nova-core/fb/hal/ga100.rs | 9 +- drivers/gpu/nova-core/gpu.rs | 9 +- drivers/gpu/nova-core/regs.rs | 139 ++++++------ 6 files changed, 365 insertions(+), 308 deletions(-) diff --git a/drivers/gpu/nova-core/bitfield.rs b/drivers/gpu/nova-core/bitf= ield.rs index 16e143658c51..be4d54feb38c 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: Bounded) -> 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 Bounded { +/// fn from(mode: Mode) -> Bounded { +/// Bounded::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: Bounded) -> Self { +/// if bool::from(value) { +/// State::Active +/// } else { +/// State::Inactive +/// } /// } /// } /// -/// impl From for bool { -/// fn from(state: State) -> bool { +/// impl From for Bounded { +/// fn from(state: State) -> Bounded { /// 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::Bounded<$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::Bounded::<$storage, { $hi + 1 - $lo }>::from_ex= pr(field) + } + + fn []( + mut self, + value: ::kernel::num::Bounded<$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::TryIntoBounded<$storage, { $hi + 1 - $= lo }>, + { + 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::Bounded<$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::Bounded<$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::Bounded<$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::TryIntoBounded<$storage, { $hi + 1 - $= lo }>, + { + 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::Bounded<$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 fe5b3256d972..f74770703c0f 100644 --- a/drivers/gpu/nova-core/falcon.rs +++ b/drivers/gpu/nova-core/falcon.rs @@ -10,6 +10,10 @@ device, dma::DmaAddress, io::poll::read_poll_timeout, + num::{ + self, + Bounded, // + }, prelude::*, sync::aref::ARef, time::{ @@ -35,11 +39,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 Bounded + where + T: From + num::Integer, + { fn from(value: $enum_type) -> Self { - value as u8 + Bounded::from_expr(T::from(value as u8)) } } }; @@ -59,16 +66,20 @@ 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::Integer, + u8: From>, +{ type Error =3D Error; =20 - fn try_from(value: u8) -> Result { + fn try_from(value: Bounded) -> 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, @@ -94,24 +105,26 @@ 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::Integer, + u8: From>, +{ + fn from(value: Bounded) -> 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) + // SAFETY: `value` comes from a 2-bit `Bounded`, and we just c= hecked all possible + // values. + _ =3D> unsafe { core::hint::unreachable_unchecked() }, + } } } =20 @@ -138,16 +151,20 @@ 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::Integer, + u8: From>, +{ type Error =3D Error; =20 - fn try_from(value: u8) -> Result { + fn try_from(value: Bounded) -> 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, @@ -170,14 +187,18 @@ 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::Integer, + u8: From>, +{ type Error =3D Error; =20 - fn try_from(value: u8) -> Result { - match value { + fn try_from(value: Bounded) -> Result { + match u8::from(value) { 1 =3D> Ok(FalconModSelAlgo::Rsa3k), _ =3D> Err(EINVAL), } @@ -192,14 +213,18 @@ 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::Integer, + u8: From>, +{ type Error =3D Error; =20 - fn try_from(value: u8) -> Result { - match value { + fn try_from(value: Bounded) -> Result { + match u8::from(value) { 0x6 =3D> Ok(Self::Size256B), _ =3D> Err(EINVAL), } @@ -215,25 +240,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::Integer + Zeroable, +{ + fn from(value: Bounded) -> 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 { @@ -257,14 +277,18 @@ 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::Integer, + u8: From>, +{ type Error =3D Error; =20 - fn try_from(value: u8) -> Result { - let res =3D match value { + fn try_from(value: Bounded) -> Result { + let res =3D match u8::from(value) { 0 =3D> Self::LocalFb, 1 =3D> Self::CoherentSysmem, 2 =3D> Self::NoncoherentSysmem, @@ -284,26 +308,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::Integer + Zeroable, +{ + fn from(value: Bounded) -> 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 @@ -426,7 +445,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(()) @@ -495,20 +514,18 @@ fn dma_wr>( .set_base((dma_start >> 8) as u32) .write(bar, &E::ID); regs::NV_PFALCON_FALCON_DMATRFBASE1::default() - // CAST: `as u16` is used on purpose since the remaining bits = are guaranteed to fit - // within a `u16`. - .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(sec); =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 69a7a95cac16..72afbd9101cf 100644 --- a/drivers/gpu/nova-core/falcon/hal/ga102.rs +++ b/drivers/gpu/nova-core/falcon/hal/ga102.rs @@ -59,7 +59,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() @@ -68,7 +68,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 e0acc41aa7cd..acf46ad0dba1 100644 --- a/drivers/gpu/nova-core/fb/hal/ga100.rs +++ b/drivers/gpu/nova-core/fb/hal/ga100.rs @@ -1,6 +1,9 @@ // SPDX-License-Identifier: GPL-2.0 =20 -use kernel::prelude::*; +use kernel::{ + num::Bounded, + prelude::*, // +}; =20 use crate::{ driver::Bar0, @@ -20,9 +23,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() - // CAST: `as u32` is used on purpose since the remaining bits are = guaranteed to fit within - // a `u32`. - .set_adr_63_40((addr >> FLUSH_SYSMEM_ADDR_SHIFT_HI) as u32) + .set_adr_63_40(Bounded::from_expr(addr >> FLUSH_SYSMEM_ADDR_SHIFT_= HI).cast()) .write(bar); regs::NV_PFB_NISO_FLUSH_SYSMEM_ADDR::default() // CAST: `as u32` is used on purpose since we want to strip the up= per bits that have been diff --git a/drivers/gpu/nova-core/gpu.rs b/drivers/gpu/nova-core/gpu.rs index 802e71e4f97d..faa1ab58b4fe 100644 --- a/drivers/gpu/nova-core/gpu.rs +++ b/drivers/gpu/nova-core/gpu.rs @@ -4,6 +4,7 @@ device, devres::Devres, fmt, + num::Bounded, pci, prelude::*, sync::Arc, // @@ -143,15 +144,15 @@ fn try_from(value: u8) -> Result { } =20 pub(crate) struct Revision { - major: u8, - minor: u8, + major: Bounded, + minor: Bounded, } =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 934003cab8a8..cb712c95b0bd 100644 --- a/drivers/gpu/nova-core/regs.rs +++ b/drivers/gpu/nova-core/regs.rs @@ -32,18 +32,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 @@ -64,7 +65,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 @@ -73,17 +74,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 { @@ -102,7 +103,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 { @@ -113,7 +114,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 { @@ -138,7 +139,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 @@ -148,7 +149,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 @@ -160,13 +161,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 @@ -180,8 +181,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 { @@ -200,40 +201,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 { @@ -244,101 +245,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 @@ -348,7 +349,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 @@ -356,6 +357,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.2