From: "Gary Guo" <gary@garyguo.net>
To: "Alexandre Courbot" <acourbot@nvidia.com>,
"Danilo Krummrich" <dakr@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
"Maxime Ripard" <mripard@kernel.org>,
"Thomas Zimmermann" <tzimmermann@suse.de>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>
Cc: "John Hubbard" <jhubbard@nvidia.com>,
"Alistair Popple" <apopple@nvidia.com>,
"Joel Fernandes" <joelagnelf@nvidia.com>,
"Timur Tabi" <ttabi@nvidia.com>, "Zhi Wang" <zhiw@nvidia.com>,
"Eliot Courtney" <ecourtney@nvidia.com>,
<dri-devel@lists.freedesktop.org>, <linux-kernel@vger.kernel.org>,
<linux-riscv@lists.infradead.org>, <linux-doc@vger.kernel.org>,
<rust-for-linux@vger.kernel.org>
Subject: Re: [PATCH v2 07/10] gpu: nova-core: falcon: introduce `bounded_enum` macro
Date: Fri, 20 Mar 2026 19:08:54 +0000 [thread overview]
Message-ID: <DH7UH2ITPZDT.E1DWRAW8891O@garyguo.net> (raw)
In-Reply-To: <20260320-b4-nova-register-v2-7-88fcf103e8d4@nvidia.com>
On Fri Mar 20, 2026 at 12:19 PM GMT, Alexandre Courbot wrote:
> Introduce a powered-up version of our ad-hoc `impl_from_enum_to_u8`
> macro that allows the definition of an enum type associated to a
> `Bounded` of a given width, and provides the `From` and `TryFrom`
> implementations required to use that enum as a register field member.
>
> The next patch will make use of it to convert all falcon registers to
> the kernel register macro.
>
> The macro is unused in this patch: it is introduced ahead-of-time to
> avoid diff mingling in the next patch that would make it difficult to
> review.
>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
> drivers/gpu/nova-core/falcon.rs | 82 +++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 82 insertions(+)
>
> diff --git a/drivers/gpu/nova-core/falcon.rs b/drivers/gpu/nova-core/falcon.rs
> index 5a4f7fc85160..5221e4476f90 100644
> --- a/drivers/gpu/nova-core/falcon.rs
> +++ b/drivers/gpu/nova-core/falcon.rs
> @@ -54,6 +54,88 @@ fn from(value: $enum_type) -> Self {
> };
> }
>
> +/// Creates an enum type associated to a `Bounded`, with a `From` conversion to the associated
> +/// `Bounded` and either a `TryFrom` or `From` converting from the associated `Bounded`.
> +// TODO[FPRI]: This is a temporary solution to be replaced with the corresponding derive macros
> +// once they land.
> +#[expect(unused)]
> +macro_rules! bounded_enum {
> + (
> + $(#[doc = $enum_doc:expr])*
> + enum $enum_type:ident with $from_impl:ident<Bounded<$width:ty, $length:literal>> {
> + $( $(#[doc = $variant_doc:expr])* $variant:ident = $value:expr),* $(,)*
> + }
> + ) => {
> + $(#[doc = $enum_doc])*
> + #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
> + pub(crate) enum $enum_type {
> + $(
> + $(#[doc = $variant_doc])*
> + $variant = $value
> + ),*
> + }
> +
> + impl From<$enum_type> for Bounded<$width, $length> {
> + fn from(value: $enum_type) -> Self {
> + match value {
> + $($enum_type::$variant => Bounded::<$width, _>::new::<$value>()),*
Given that you're having `$value:expr` instead of `$value:literal`, this
probably should be `::new::<{$value}>`
> + }
> + }
> + }
> +
> + bounded_enum!(@impl_from $enum_type with $from_impl<Bounded<$width, $length>> {
> + $($variant = $value),*
> + });
> + };
> +
> + // `TryFrom` implementation from associated `Bounded` to enum type.
> + (@impl_from $enum_type:ident with TryFrom<Bounded<$width:ty, $length:literal>> {
> + $($variant:ident = $value:expr),* $(,)*
> + }) => {
> + impl TryFrom<Bounded<$width, $length>> for $enum_type {
> + type Error = Error;
> +
> + fn try_from(value: Bounded<$width, $length>) -> Result<Self> {
> + match value.get() {
> + $(
> + $value => Ok($enum_type::$variant),
> + )*
> + _ => Err(EINVAL),
> + }
> + }
> + }
> + };
> +
> + // `From` implementation from associated `Bounded` to enum type. Triggers a `build_error` if
> + // all possible values of the `Bounded` are not covered by the enum type.
> + (@impl_from $enum_type:ident with From<Bounded<$width:ty, $length:literal>> {
> + $($variant:ident = $value:expr),* $(,)*
> + }) => {
> + impl From<Bounded<$width, $length>> for $enum_type {
> + fn from(value: Bounded<$width, $length>) -> Self {
> + $(
> + // Ensure all enum values fit into the `Bounded` type.
> + const { assert!(
> + $value < (1 << $length),
> + "Enum variant doesn't fit into assigned `Bounded` type."
> + ); }
> + )*
> +
> + // Makes the compiler optimizer aware of the possible range of values.
> + let value = value.get() & ((1 << $length) - 1);
> + match value {
> + $(
> + $value => $enum_type::$variant,
> + )*
> + // We land here if the match didn't cover all possible values for the
> + // `Bounded`.
> + _ => build_error!("Enum doesn't cover all values of the `Bounded` type."),
Given that the length is actually fixed, you should be able to use the
exhaustive check instead of `build_error!`:
const MAX: $width = 1 << $length;
match value {
...
MAX.. => ...,
}
This way compiler will complain about non-exhaustive patterns.
Best,
Gary
> + }
> + }
> + }
> + }
> +}
> +
> /// Revision number of a falcon core, used in the [`crate::regs::NV_PFALCON_FALCON_HWCFG1`]
> /// register.
> #[repr(u8)]
WARNING: multiple messages have this Message-ID (diff)
From: "Gary Guo" <gary@garyguo.net>
To: "Alexandre Courbot" <acourbot@nvidia.com>,
"Danilo Krummrich" <dakr@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
"Maxime Ripard" <mripard@kernel.org>,
"Thomas Zimmermann" <tzimmermann@suse.de>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>
Cc: "John Hubbard" <jhubbard@nvidia.com>,
"Alistair Popple" <apopple@nvidia.com>,
"Joel Fernandes" <joelagnelf@nvidia.com>,
"Timur Tabi" <ttabi@nvidia.com>, "Zhi Wang" <zhiw@nvidia.com>,
"Eliot Courtney" <ecourtney@nvidia.com>,
<dri-devel@lists.freedesktop.org>, <linux-kernel@vger.kernel.org>,
<linux-riscv@lists.infradead.org>, <linux-doc@vger.kernel.org>,
<rust-for-linux@vger.kernel.org>
Subject: Re: [PATCH v2 07/10] gpu: nova-core: falcon: introduce `bounded_enum` macro
Date: Fri, 20 Mar 2026 19:08:54 +0000 [thread overview]
Message-ID: <DH7UH2ITPZDT.E1DWRAW8891O@garyguo.net> (raw)
In-Reply-To: <20260320-b4-nova-register-v2-7-88fcf103e8d4@nvidia.com>
On Fri Mar 20, 2026 at 12:19 PM GMT, Alexandre Courbot wrote:
> Introduce a powered-up version of our ad-hoc `impl_from_enum_to_u8`
> macro that allows the definition of an enum type associated to a
> `Bounded` of a given width, and provides the `From` and `TryFrom`
> implementations required to use that enum as a register field member.
>
> The next patch will make use of it to convert all falcon registers to
> the kernel register macro.
>
> The macro is unused in this patch: it is introduced ahead-of-time to
> avoid diff mingling in the next patch that would make it difficult to
> review.
>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
> drivers/gpu/nova-core/falcon.rs | 82 +++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 82 insertions(+)
>
> diff --git a/drivers/gpu/nova-core/falcon.rs b/drivers/gpu/nova-core/falcon.rs
> index 5a4f7fc85160..5221e4476f90 100644
> --- a/drivers/gpu/nova-core/falcon.rs
> +++ b/drivers/gpu/nova-core/falcon.rs
> @@ -54,6 +54,88 @@ fn from(value: $enum_type) -> Self {
> };
> }
>
> +/// Creates an enum type associated to a `Bounded`, with a `From` conversion to the associated
> +/// `Bounded` and either a `TryFrom` or `From` converting from the associated `Bounded`.
> +// TODO[FPRI]: This is a temporary solution to be replaced with the corresponding derive macros
> +// once they land.
> +#[expect(unused)]
> +macro_rules! bounded_enum {
> + (
> + $(#[doc = $enum_doc:expr])*
> + enum $enum_type:ident with $from_impl:ident<Bounded<$width:ty, $length:literal>> {
> + $( $(#[doc = $variant_doc:expr])* $variant:ident = $value:expr),* $(,)*
> + }
> + ) => {
> + $(#[doc = $enum_doc])*
> + #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
> + pub(crate) enum $enum_type {
> + $(
> + $(#[doc = $variant_doc])*
> + $variant = $value
> + ),*
> + }
> +
> + impl From<$enum_type> for Bounded<$width, $length> {
> + fn from(value: $enum_type) -> Self {
> + match value {
> + $($enum_type::$variant => Bounded::<$width, _>::new::<$value>()),*
Given that you're having `$value:expr` instead of `$value:literal`, this
probably should be `::new::<{$value}>`
> + }
> + }
> + }
> +
> + bounded_enum!(@impl_from $enum_type with $from_impl<Bounded<$width, $length>> {
> + $($variant = $value),*
> + });
> + };
> +
> + // `TryFrom` implementation from associated `Bounded` to enum type.
> + (@impl_from $enum_type:ident with TryFrom<Bounded<$width:ty, $length:literal>> {
> + $($variant:ident = $value:expr),* $(,)*
> + }) => {
> + impl TryFrom<Bounded<$width, $length>> for $enum_type {
> + type Error = Error;
> +
> + fn try_from(value: Bounded<$width, $length>) -> Result<Self> {
> + match value.get() {
> + $(
> + $value => Ok($enum_type::$variant),
> + )*
> + _ => Err(EINVAL),
> + }
> + }
> + }
> + };
> +
> + // `From` implementation from associated `Bounded` to enum type. Triggers a `build_error` if
> + // all possible values of the `Bounded` are not covered by the enum type.
> + (@impl_from $enum_type:ident with From<Bounded<$width:ty, $length:literal>> {
> + $($variant:ident = $value:expr),* $(,)*
> + }) => {
> + impl From<Bounded<$width, $length>> for $enum_type {
> + fn from(value: Bounded<$width, $length>) -> Self {
> + $(
> + // Ensure all enum values fit into the `Bounded` type.
> + const { assert!(
> + $value < (1 << $length),
> + "Enum variant doesn't fit into assigned `Bounded` type."
> + ); }
> + )*
> +
> + // Makes the compiler optimizer aware of the possible range of values.
> + let value = value.get() & ((1 << $length) - 1);
> + match value {
> + $(
> + $value => $enum_type::$variant,
> + )*
> + // We land here if the match didn't cover all possible values for the
> + // `Bounded`.
> + _ => build_error!("Enum doesn't cover all values of the `Bounded` type."),
Given that the length is actually fixed, you should be able to use the
exhaustive check instead of `build_error!`:
const MAX: $width = 1 << $length;
match value {
...
MAX.. => ...,
}
This way compiler will complain about non-exhaustive patterns.
Best,
Gary
> + }
> + }
> + }
> + }
> +}
> +
> /// Revision number of a falcon core, used in the [`crate::regs::NV_PFALCON_FALCON_HWCFG1`]
> /// register.
> #[repr(u8)]
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
next prev parent reply other threads:[~2026-03-20 19:09 UTC|newest]
Thread overview: 48+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-20 12:19 [PATCH v2 00/10] gpu: nova-core: convert registers to use the kernel register macro Alexandre Courbot
2026-03-20 12:19 ` Alexandre Courbot
2026-03-20 12:19 ` [PATCH v2 01/10] gpu: nova-core: convert PMC registers to " Alexandre Courbot
2026-03-20 12:19 ` Alexandre Courbot
2026-03-20 19:09 ` Gary Guo
2026-03-20 19:09 ` Gary Guo
2026-03-23 1:41 ` Eliot Courtney
2026-03-23 1:41 ` Eliot Courtney
2026-03-20 12:19 ` [PATCH v2 02/10] gpu: nova-core: convert PBUS " Alexandre Courbot
2026-03-20 12:19 ` Alexandre Courbot
2026-03-20 12:19 ` [PATCH v2 03/10] gpu: nova-core: convert PFB " Alexandre Courbot
2026-03-20 12:19 ` Alexandre Courbot
2026-03-20 12:19 ` [PATCH v2 04/10] gpu: nova-core: convert GC6 " Alexandre Courbot
2026-03-20 12:19 ` Alexandre Courbot
2026-03-20 12:19 ` [PATCH v2 05/10] gpu: nova-core: convert FUSE " Alexandre Courbot
2026-03-20 12:19 ` Alexandre Courbot
2026-03-20 12:19 ` [PATCH v2 06/10] gpu: nova-core: convert PDISP " Alexandre Courbot
2026-03-20 12:19 ` Alexandre Courbot
2026-03-20 17:33 ` Joel Fernandes
2026-03-20 17:33 ` Joel Fernandes
2026-03-21 6:19 ` Alexandre Courbot
2026-03-21 6:19 ` Alexandre Courbot
2026-03-20 12:19 ` [PATCH v2 07/10] gpu: nova-core: falcon: introduce `bounded_enum` macro Alexandre Courbot
2026-03-20 12:19 ` Alexandre Courbot
2026-03-20 19:08 ` Gary Guo [this message]
2026-03-20 19:08 ` Gary Guo
2026-03-20 12:19 ` [PATCH v2 08/10] gpu: nova-core: convert falcon registers to kernel register macro Alexandre Courbot
2026-03-20 12:19 ` Alexandre Courbot
2026-03-20 17:38 ` Joel Fernandes
2026-03-20 17:38 ` Joel Fernandes
2026-03-20 19:52 ` John Hubbard
2026-03-20 19:52 ` John Hubbard
2026-03-20 20:07 ` Danilo Krummrich
2026-03-20 20:07 ` Danilo Krummrich
2026-03-20 20:23 ` Gary Guo
2026-03-20 20:23 ` Gary Guo
2026-03-20 20:17 ` Gary Guo
2026-03-20 20:17 ` Gary Guo
2026-03-21 6:16 ` Alexandre Courbot
2026-03-21 6:16 ` Alexandre Courbot
2026-03-20 12:19 ` [PATCH v2 09/10] gpu: nova-core: remove `io::` qualifier to register macro invocations Alexandre Courbot
2026-03-20 12:19 ` Alexandre Courbot
2026-03-23 1:40 ` Eliot Courtney
2026-03-23 1:40 ` Eliot Courtney
2026-03-20 12:19 ` [PATCH v2 10/10] Documentation: nova: remove register abstraction task Alexandre Courbot
2026-03-20 12:19 ` Alexandre Courbot
2026-03-20 19:11 ` [PATCH v2 00/10] gpu: nova-core: convert registers to use the kernel register macro Gary Guo
2026-03-20 19:11 ` Gary Guo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DH7UH2ITPZDT.E1DWRAW8891O@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=apopple@nvidia.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=ecourtney@nvidia.com \
--cc=jhubbard@nvidia.com \
--cc=joelagnelf@nvidia.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=lossin@kernel.org \
--cc=maarten.lankhorst@linux.intel.com \
--cc=mripard@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--cc=tmgross@umich.edu \
--cc=ttabi@nvidia.com \
--cc=tzimmermann@suse.de \
--cc=zhiw@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.