gpu: nova-core: allow register aliases

Some registers (notably scratch registers) don't have a definitive
purpose, but need to be interpreted differently depending on context.

Expand the register!() macro to support a syntax indicating that a
register type should be at the same offset as another one, but under a
different name, and with different fields and documentation.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
Link: https://lore.kernel.org/r/20250619-nova-frts-v6-9-ecf41ef99252@nvidia.com
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
This commit is contained in:
Alexandre Courbot 2025-06-19 22:23:53 +09:00 committed by Danilo Krummrich
parent cdfe233ee6
commit e66aaaffe0

View File

@ -71,6 +71,20 @@
/// pr_info!("CPU CTL: {:#x}", cpuctl);
/// cpuctl.set_start(true).write(&bar, CPU_BASE);
/// ```
///
/// It is also possible to create a alias register by using the `=> ALIAS` syntax. This is useful
/// for cases where a register's interpretation depends on the context:
///
/// ```no_run
/// register!(SCRATCH_0 @ 0x0000100, "Scratch register 0" {
/// 31:0 value as u32, "Raw value";
///
/// register!(SCRATCH_0_BOOT_STATUS => SCRATCH_0, "Boot status of the firmware" {
/// 0:0 completed as bool, "Whether the firmware has completed booting";
/// ```
///
/// In this example, `SCRATCH_0_BOOT_STATUS` uses the same I/O address as `SCRATCH_0`, while also
/// providing its own `completed` method.
macro_rules! register {
// Creates a register at a fixed offset of the MMIO space.
(
@ -83,6 +97,17 @@ macro_rules! register {
register!(@io $name @ $offset);
};
// Creates a alias register of fixed offset register `alias` with its own fields.
(
$name:ident => $alias:ident $(, $comment:literal)? {
$($fields:tt)*
}
) => {
register!(@common $name @ $alias::OFFSET $(, $comment)?);
register!(@field_accessors $name { $($fields)* });
register!(@io $name @ $alias::OFFSET);
};
// Creates a register at a relative offset from a base address.
(
$name:ident @ + $offset:literal $(, $comment:literal)? {
@ -94,11 +119,22 @@ macro_rules! register {
register!(@io$name @ + $offset);
};
// Creates a alias register of relative offset register `alias` with its own fields.
(
$name:ident => + $alias:ident $(, $comment:literal)? {
$($fields:tt)*
}
) => {
register!(@common $name @ $alias::OFFSET $(, $comment)?);
register!(@field_accessors $name { $($fields)* });
register!(@io $name @ + $alias::OFFSET);
};
// All rules below are helpers.
// Defines the wrapper `$name` type, as well as its relevant implementations (`Debug`, `BitOr`,
// and conversion to regular `u32`).
(@common $name:ident @ $offset:literal $(, $comment:literal)?) => {
(@common $name:ident @ $offset:expr $(, $comment:literal)?) => {
$(
#[doc=$comment]
)?
@ -280,7 +316,7 @@ pub(crate) fn [<set_ $field>](mut self, value: $to_type) -> Self {
};
// Creates the IO accessors for a fixed offset register.
(@io $name:ident @ $offset:literal) => {
(@io $name:ident @ $offset:expr) => {
#[allow(dead_code)]
impl $name {
#[inline]