Alexandre Courbot
2025-May-21 06:45 UTC
[PATCH v4 08/20] 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 at nvidia.com>
---
drivers/gpu/nova-core/regs/macros.rs | 40 ++++++++++++++++++++++++++++++++++--
1 file changed, 38 insertions(+), 2 deletions(-)
diff --git a/drivers/gpu/nova-core/regs/macros.rs
b/drivers/gpu/nova-core/regs/macros.rs
index
7cd013f3c90bbd8ca437d4072cae8f11d7946fcd..64dda1d4d93d3c7022ef02b6f6fb81b58e90dd44
100644
--- a/drivers/gpu/nova-core/regs/macros.rs
+++ b/drivers/gpu/nova-core/regs/macros.rs
@@ -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 `=> PARENT`
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 `parent` with its own
fields.
+ (
+ $name:ident => $parent:ident $(, $comment:literal)? {
+ $($fields:tt)*
+ }
+ ) => {
+ register!(@common $name @ $parent::OFFSET $(, $comment)?);
+ register!(@field_accessors $name { $($fields)* });
+ register!(@io $name @ $parent::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 `parent` with its
own fields.
+ (
+ $name:ident => + $parent:ident $(, $comment:literal)? {
+ $($fields:tt)*
+ }
+ ) => {
+ register!(@common $name @ $parent::OFFSET $(, $comment)?);
+ register!(@field_accessors $name { $($fields)* });
+ register!(@io $name @ + $parent::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]
--
2.49.0
Danilo Krummrich
2025-May-21 08:37 UTC
[PATCH v4 08/20] gpu: nova-core: allow register aliases
On Wed, May 21, 2025 at 03:45:03PM +0900, Alexandre Courbot wrote:> 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 at nvidia.com> > --- > drivers/gpu/nova-core/regs/macros.rs | 40 ++++++++++++++++++++++++++++++++++-- > 1 file changed, 38 insertions(+), 2 deletions(-) > > diff --git a/drivers/gpu/nova-core/regs/macros.rs b/drivers/gpu/nova-core/regs/macros.rs > index 7cd013f3c90bbd8ca437d4072cae8f11d7946fcd..64dda1d4d93d3c7022ef02b6f6fb81b58e90dd44 100644 > --- a/drivers/gpu/nova-core/regs/macros.rs > +++ b/drivers/gpu/nova-core/regs/macros.rs > @@ -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 `=> PARENT` 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" {NIT: I'd put the arrow the other way around, i.e. SCRATCH_0_BOOT_STATUS is derived from SCRATCH_0, not the other way around.