From: Eliot Courtney <ecourtney@nvidia.com>
To: "Danilo Krummrich" <dakr@kernel.org>,
"Lorenzo Stoakes" <ljs@kernel.org>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
"Uladzislau Rezki" <urezki@gmail.com>,
"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>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>
Cc: John Hubbard <jhubbard@nvidia.com>,
Alistair Popple <apopple@nvidia.com>,
Timur Tabi <ttabi@nvidia.com>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org,
Eliot Courtney <ecourtney@nvidia.com>
Subject: [PATCH 4/6] gpu: nova-core: add NVKV typed encoding
Date: Mon, 17 Aug 2026 21:56:39 +0900 [thread overview]
Message-ID: <20260817-b4-nvkv-v1-4-b84db5e84b67@nvidia.com> (raw)
In-Reply-To: <20260817-b4-nvkv-v1-0-b84db5e84b67@nvidia.com>
For struct-like GMCAPI messages encoding field by field manually is
noisy. Add some type machinery and a macro to automate encoding of
struct-like messages. The `Encodeable` trait can be implemented by any
type to say that it can be encoded into an NVKV `Encoder`. Add a simple
`nvkv_encode!` macro that works on structs and encodes each field in
order. Provide some base types, such as `Key` which statically
associates a NVKV key with some value, to avoid having to make a lot of
newtypes and implement `Encodeable` on them.
Signed-off-by: Eliot Courtney <ecourtney@nvidia.com>
---
drivers/gpu/nova-core/gsp/nvkv.rs | 49 +++++++++
drivers/gpu/nova-core/gsp/nvkv/encode.rs | 178 +++++++++++++++++++++++++++++++
2 files changed, 227 insertions(+)
diff --git a/drivers/gpu/nova-core/gsp/nvkv.rs b/drivers/gpu/nova-core/gsp/nvkv.rs
index 64d8d0118452..bf6500d54b21 100644
--- a/drivers/gpu/nova-core/gsp/nvkv.rs
+++ b/drivers/gpu/nova-core/gsp/nvkv.rs
@@ -10,6 +10,13 @@
//! naturally maps to storing a &str with the GPU name.
#![expect(unused_imports)]
+#![cfg_attr(not(CONFIG_KUNIT), expect(unused_macros))]
+
+use core::marker::PhantomData;
+use core::ops::{
+ Deref,
+ DerefMut, //
+};
use kernel::{
bitfield,
@@ -29,6 +36,48 @@
/// The index of an NVKV value.
pub(crate) type Index = Bounded<u64, 12>;
+/// A static association between an NVKV key `KEY_ID` and the storage of its value.
+///
+/// Use with the encoder or decoder macros `nvkv_encode!` and `nvkv_decode!` to let them know how to
+/// map the value `Key<T, KEY_ID, As>` to/from encoded data. For brevity, `As` inserts an additional
+/// conversion (`From`) to avoid having to implement [`Encodable`] for many types. For example,
+/// enums that are easily convertible to a u32 can have `As = u32` and rely on the existing encoding
+/// for u32.
+#[repr(transparent)]
+pub(crate) struct Key<T, const KEY_ID: KeyId, As = T>(pub(crate) T, PhantomData<As>);
+
+impl<T, const KEY_ID: KeyId, As> From<T> for Key<T, KEY_ID, As> {
+ fn from(value: T) -> Self {
+ Self(value, PhantomData)
+ }
+}
+
+impl<'a, T, const KEY_ID: KeyId, As, const N: usize> From<&'a [T; N]> for Key<&'a [T], KEY_ID, As> {
+ fn from(value: &'a [T; N]) -> Self {
+ Self(&value[..], PhantomData)
+ }
+}
+
+impl<T, const KEY_ID: KeyId, As> Deref for Key<T, KEY_ID, As> {
+ type Target = T;
+
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
+
+impl<T, const KEY_ID: KeyId, As> DerefMut for Key<T, KEY_ID, As> {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.0
+ }
+}
+
+impl<T: Default, const KEY_ID: KeyId, As> Default for Key<T, KEY_ID, As> {
+ fn default() -> Self {
+ Self(T::default(), PhantomData)
+ }
+}
+
bitfield! {
/// The op word that starts each NVKV operation.
struct Op(u64) {
diff --git a/drivers/gpu/nova-core/gsp/nvkv/encode.rs b/drivers/gpu/nova-core/gsp/nvkv/encode.rs
index 6da81a371651..31ea5788e772 100644
--- a/drivers/gpu/nova-core/gsp/nvkv/encode.rs
+++ b/drivers/gpu/nova-core/gsp/nvkv/encode.rs
@@ -7,11 +7,153 @@
use super::{
Index,
+ Key,
KeyId,
Op,
Opcode, //
};
+/// A type that can encode itself into an [`Encoder`].
+pub(crate) trait Encodable {
+ /// Encodes `self` into `encoder`.
+ fn encode(&self, encoder: &mut Encoder) -> Result;
+}
+
+/// Defines a struct together with its [`Encodable`] implementation.
+///
+/// The implementation encodes each field in declaration order. Each field type must implement
+/// [`Encodable`], which is done already for types like `Key<T, KEY_ID>`.
+///
+/// # Examples
+///
+/// ```
+/// nvkv_encode! {
+/// struct Request {
+/// id: Key<u32, 0x0001>,
+/// name: Key<&'static [u8], 0x0002>,
+/// }
+/// }
+/// ```
+macro_rules! nvkv_encode {
+ (
+ $(#[$attr:meta])*
+ $vis:vis struct $name:ident {
+ $(
+ $(#[$field_attr:meta])*
+ $field_vis:vis $field:ident : $ty:ty
+ ),* $(,)?
+ }
+ ) => {
+ $(#[$attr])*
+ $vis struct $name {
+ $(
+ $(#[$field_attr])*
+ $field_vis $field: $ty,
+ )*
+ }
+
+ impl $crate::gsp::nvkv::Encodable for $name {
+ #[inline(always)]
+ fn encode(&self, encoder: &mut $crate::gsp::nvkv::Encoder) -> ::kernel::error::Result {
+ $( $crate::gsp::nvkv::Encodable::encode(&self.$field, encoder)?; )*
+ Ok(())
+ }
+ }
+ };
+}
+pub(crate) use nvkv_encode;
+
+/// A value with a specific index that encodes under the NVKV key `KEY_ID`.
+struct IndexedKey<T, const KEY_ID: KeyId> {
+ index: Index,
+ value: T,
+}
+
+impl<T, const KEY_ID: KeyId> IndexedKey<T, KEY_ID> {
+ /// Creates a key with the given index and value.
+ pub(crate) fn new(index: Index, value: T) -> Self {
+ Self { index, value }
+ }
+}
+
+impl<const KEY_ID: KeyId> Encodable for IndexedKey<u32, KEY_ID> {
+ #[inline(always)]
+ fn encode(&self, encoder: &mut Encoder) -> Result {
+ encoder.encode_u32(KEY_ID, self.index, self.value)
+ }
+}
+
+impl<const KEY_ID: KeyId> Encodable for IndexedKey<u64, KEY_ID> {
+ #[inline(always)]
+ fn encode(&self, encoder: &mut Encoder) -> Result {
+ encoder.encode_u64(KEY_ID, self.index, self.value)
+ }
+}
+
+impl<const KEY_ID: KeyId> Encodable for IndexedKey<&[u8], KEY_ID> {
+ #[inline(always)]
+ fn encode(&self, encoder: &mut Encoder) -> Result {
+ encoder.encode_array8(KEY_ID, self.index, self.value)
+ }
+}
+
+impl<const KEY_ID: KeyId> Encodable for IndexedKey<&[u32], KEY_ID> {
+ #[inline(always)]
+ fn encode(&self, encoder: &mut Encoder) -> Result {
+ encoder.encode_array32(KEY_ID, self.index, self.value)
+ }
+}
+
+impl<const KEY_ID: KeyId> Encodable for IndexedKey<&[u64], KEY_ID> {
+ #[inline(always)]
+ fn encode(&self, encoder: &mut Encoder) -> Result {
+ encoder.encode_array64(KEY_ID, self.index, self.value)
+ }
+}
+
+impl<const N: usize, const KEY_ID: KeyId> Encodable for IndexedKey<[u8; N], KEY_ID> {
+ #[inline(always)]
+ fn encode(&self, encoder: &mut Encoder) -> Result {
+ encoder.encode_array8(KEY_ID, self.index, &self.value)
+ }
+}
+
+impl<const N: usize, const KEY_ID: KeyId> Encodable for IndexedKey<[u32; N], KEY_ID> {
+ #[inline(always)]
+ fn encode(&self, encoder: &mut Encoder) -> Result {
+ encoder.encode_array32(KEY_ID, self.index, &self.value)
+ }
+}
+
+impl<const N: usize, const KEY_ID: KeyId> Encodable for IndexedKey<[u64; N], KEY_ID> {
+ #[inline(always)]
+ fn encode(&self, encoder: &mut Encoder) -> Result {
+ encoder.encode_array64(KEY_ID, self.index, &self.value)
+ }
+}
+
+impl<T, const KEY_ID: KeyId, As> Encodable for Key<T, KEY_ID, As>
+where
+ IndexedKey<As, KEY_ID>: Encodable,
+ As: From<T>,
+ T: Copy,
+{
+ #[inline(always)]
+ fn encode(&self, encoder: &mut Encoder) -> Result {
+ IndexedKey::new(Index::new::<0>(), As::from(self.0)).encode(encoder)
+ }
+}
+
+impl<T: Encodable> Encodable for Option<T> {
+ #[inline(always)]
+ fn encode(&self, encoder: &mut Encoder) -> Result {
+ if let Some(value) = self {
+ value.encode(encoder)?;
+ }
+ Ok(())
+ }
+}
+
/// An encoder for an NVKV stream.
pub(crate) struct Encoder {
backing: KVVec<u64>,
@@ -242,4 +384,40 @@ fn encode_all_value_kinds() -> Result {
Ok(())
}
+
+ // Tests that encoding via the `nvkv_encode!` macro works correctly.
+ #[test]
+ fn encode_typed_struct() -> Result {
+ const U32_KEY: KeyId = 0x0001;
+ const U64_KEY: KeyId = 0x0002;
+ const NAME_KEY: KeyId = 0x0003;
+ const FIXED_KEY: KeyId = 0x0004;
+ const OPT_KEY: KeyId = 0x0005;
+
+ nvkv_encode! {
+ struct TypedRequest {
+ a: Key<u32, { U32_KEY }>,
+ b: Key<u64, { U64_KEY }>,
+ name: Key<&'static [u8], { NAME_KEY }>,
+ fixed: Key<[u8; 4], { FIXED_KEY }>,
+ opt: Option<Key<u32, { OPT_KEY }>>,
+ }
+ }
+
+ let request = TypedRequest {
+ a: 0x89ab_cdef.into(),
+ b: 0x0123_4567_89ab_cdef.into(),
+ name: b"name\0".into(),
+ fixed: [1u8, 2, 3, 4].into(),
+ opt: None,
+ };
+
+ let mut encoder = Encoder::new();
+ request.encode(&mut encoder)?;
+ let encoded = encoder.finish();
+
+ assert_eq!(encoded.len(), 7);
+
+ Ok(())
+ }
}
--
2.55.0
next prev parent reply other threads:[~2026-08-17 12:59 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-17 12:56 [PATCH 0/6] gpu: nova-core: add NVKV codec Eliot Courtney
2026-08-17 12:56 ` [PATCH 1/6] rust: alloc: add Vec::push_init Eliot Courtney
2026-08-17 14:02 ` Gary Guo
2026-08-19 7:43 ` Eliot Courtney
2026-08-19 10:49 ` Danilo Krummrich
2026-08-19 11:23 ` Danilo Krummrich
2026-08-19 12:08 ` Gary Guo
2026-08-19 12:14 ` Gary Guo
2026-08-17 12:56 ` [PATCH 2/6] gpu: nova-core: add NVKV encoder Eliot Courtney
2026-08-19 16:32 ` Danilo Krummrich
2026-08-19 16:47 ` Danilo Krummrich
2026-08-24 12:58 ` Eliot Courtney
2026-08-17 12:56 ` [PATCH 3/6] gpu: nova-core: add NVKV decoder Eliot Courtney
2026-08-17 12:56 ` Eliot Courtney [this message]
2026-08-17 12:56 ` [PATCH 5/6] gpu: nova-core: add NVKV typed decoding Eliot Courtney
2026-08-19 18:59 ` Danilo Krummrich
2026-08-17 12:56 ` [PATCH 6/6] gpu: nova-core: add NVKV GSP_INIT schemas Eliot Courtney
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=20260817-b4-nvkv-v1-4-b84db5e84b67@nvidia.com \
--to=ecourtney@nvidia.com \
--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=daniel.almeida@collabora.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=gary@garyguo.net \
--cc=jhubbard@nvidia.com \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=lossin@kernel.org \
--cc=nova-gpu@lists.linux.dev \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=ttabi@nvidia.com \
--cc=urezki@gmail.com \
--cc=vbabka@kernel.org \
--cc=work@onurozkan.dev \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox