From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from SA9PR02CU001.outbound.protection.outlook.com (mail-southcentralusazon11013000.outbound.protection.outlook.com [40.93.196.0]) (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 EDD6B41F5FD for ; Mon, 17 Aug 2026 12:59:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.93.196.0 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786971571; cv=fail; b=ozwAWN7kaUdSyEg68xIIqvUERTi6F8AAAWVawG5T7X2gTgclkwkpTun1RoHrWoDKAstbPs2ScvKoDhrc+gsjw/BB4Nty0i8f5zbQT87UJZNvqOrDnru4iIa4U2zhBohnhcnd3XEQAMZUl0/CGG0qFYqXPMoHAYN1Ck0YUFQB1AE= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786971571; c=relaxed/simple; bh=7P4wAOJnHrEoOvFZYLEnYTcAJcHCi4+BYcf20KLmK5w=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=FvXH9o3XuWZBxcvedp/r0A4Ndk/uCYA2EFfnkqKCo0Z+CRHk6RXK9B/DzR0RxBY06K7lcyERX9qVYuFI97kIHv+X4WsU6YVduDyMwWy9bvrlpQPx1Ahrhy05Y4YKM84yL3GfMpdUtQxT9OsGWgDaQo8gmFRu6DJ6yvdMZCt1bVU= 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=ks7mGRYv; arc=fail smtp.client-ip=40.93.196.0 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="ks7mGRYv" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=wCmEK4MWJH8a7JPbGq8x25DZhevpXNlynPdc206F2UGHkrnjagt7I5Gp3AP5SCUqIz3oZCVpuo9CSSZMD7NE78WMuu7v3Gm23MMy0manV45ROUxeiROwOkdCqsRiyeBi4h/HdvYCwoNVgJq/h8b9jnTt5hRy5j+aOdZsSb2CfzAWzMMte7B4F3vDDWZnV5MNAv0rlTdKqPyagKr+bKNcHpRtUC/4MMKpC4+5IbZ1OvkREN+zsZsOqE8Kqfrur6h9bgdPm5zhQDPyoLr/Mj6Qm52kXTSFNF3ZBIO8FQMdFlhsyubJD7/ZyL435fwpCBLrqR56Ui8XVK1KoGgV4bVvrQ== 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=Dzo4kPKqaJ73zkodzxKW8/KYHl95STGO1/blrVcrVrU=; b=nObFTN71vsaCrhT1iZ1Mnfx245ogn5C5d3xaYD6YTU0qUJzC1ZTp58sOqYfnRC1L9Z/6Uav+02cYYgq64Vgzw8S6Dh3CVg/NvbcbTTTwROqwAgQ9GNzaIMvFP4W13faj5b0VOaC6XJ/Nn62Z7bR3wSL1Btxi9qfGWpeiXQ6zUnKeI4l89oMFhIrHQ9+Rg0EW9G6LC8goLbMua1NyAAwQdiqs/+PvjHkA3hjfkwsSMINTqUbdjMz0taFvD89L1BAMqw5A/Bb6hK05dbMqecR1J9FDpvbh60oWYr7tEKCd45LFXIu05Uw8ptAjwRM5e9f24FHadC46dFE+FMzoUbhB4Q== 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=Dzo4kPKqaJ73zkodzxKW8/KYHl95STGO1/blrVcrVrU=; b=ks7mGRYvYz9iAtC0kQKbBMg/Y1iqbUNOThQawE4sXPWtMiknF2oOxIfAScKrmsa7TBNyoHq/O+WEogD4iWNuQVcVgtfalcuvNO7heqw72IKfARy/APuEhUHNwMDSzwBKRbeQpOfHcUduh2FO0UomwCxljkdtHU0rC8hkwtnYcVFs6b+j7koEyHAD+UUrETNjkYlEjl3xvcd2UugITIteaL5kUzwwHY88UEk5YPLtLgRKAvL8BKofPcKhxi5CLc+VRCH06fKRgRBnnaFT23QHbrZZtLfd1nHVWb0zs6Ip982Q068Kqp8Mgwyx2IzoyB+FTUimxEIfY4UVIsRyNfDqIg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from BL0PR12MB2353.namprd12.prod.outlook.com (2603:10b6:207:4c::31) by PH7PR12MB6833.namprd12.prod.outlook.com (2603:10b6:510:1af::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.315.13; Mon, 17 Aug 2026 12:59:24 +0000 Received: from BL0PR12MB2353.namprd12.prod.outlook.com ([fe80::99b:dcff:8d6d:78e0]) by BL0PR12MB2353.namprd12.prod.outlook.com ([fe80::99b:dcff:8d6d:78e0%4]) with mapi id 15.21.0315.016; Mon, 17 Aug 2026 12:59:24 +0000 From: Eliot Courtney Date: Mon, 17 Aug 2026 21:56:40 +0900 Subject: [PATCH 5/6] gpu: nova-core: add NVKV typed decoding Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260817-b4-nvkv-v1-5-b84db5e84b67@nvidia.com> References: <20260817-b4-nvkv-v1-0-b84db5e84b67@nvidia.com> In-Reply-To: <20260817-b4-nvkv-v1-0-b84db5e84b67@nvidia.com> To: Danilo Krummrich , Lorenzo Stoakes , Vlastimil Babka , "Liam R. Howlett" , Uladzislau Rezki , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , David Airlie , Simona Vetter Cc: John Hubbard , Alistair Popple , Timur Tabi , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org, Eliot Courtney X-Mailer: b4 0.15.2 X-ClientProxiedBy: TYCPR01CA0168.jpnprd01.prod.outlook.com (2603:1096:400:2b2::8) To BL0PR12MB2353.namprd12.prod.outlook.com (2603:10b6:207:4c::31) Precedence: bulk X-Mailing-List: nova-gpu@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL0PR12MB2353:EE_|PH7PR12MB6833:EE_ X-MS-Office365-Filtering-Correlation-Id: 20dfecff-5ab1-4a67-b550-08defc5f5cb3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|23010399003|1800799024|366016|7416014|10070799003|18002099003|921020|22082099003|56012099006|10067099003|11063799006|3023799007; X-Microsoft-Antispam-Message-Info: +p/qsUk8VfshCiLM+Vo61JjlZKZkQ2RMIr9Pc9t3pKJL7aO0FEyrbldn0Qfp8Vt/+jhompaiMgXXnR4FbgAvZ5FtF1YpbwXphVext4rUYFPQoWqYxX28nx86SerQHto0Wjr49yJfP989/lrzCkRgu1tqMWd9sAGuZpnMW9mW4LUIFP4h+UretTLT+qy5/fIaHqXkgJAb7+g6WC6n0o/NKiwkvor1KfWPI8vPRKnoiEu/nA3ZYram2nrzGrUIOJxGh/1Mh0EX1kIwqjW96QKOg4WY/EMNbAotOdpOQKhR9rZk6r0bdM1cXIQjR1f97RgbX2nV/2JlIRyUeQo2P4c6kJv1FAeahj59oiDYj0xjLLRx3agISGCOdmHAfK83ssbdFIFnq3tbCcG5ywE0dwfwev9viFZ6qV7OQziff9L2yDDlH7JToxLC9J7R8DSGInX5yL0CH9slokx541ZgvWprksr/N1T4NVPIFBgD2EEBsttjE9U3cD8Rla/hJSDApGVDmM9VidLgUmfHFsWU11ol6HTUnGCs/imNM0Uzip4hdWGfEidHaKRQ/1gjFg2RSLZ6zOBWXZCexNHbvalUTJMUbQB/qyCFpIBgFiYqDGiYnBuHJt2tRnuNwIpg81DVqEjoPcconTLcmi7KkqTUdIYiZURrPN49USzKrz2bRxNqj0WNTA4Q+yBxCPFIbvb+6RCAnsoL0WlCAnjs2TiS8f1MfQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BL0PR12MB2353.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(376014)(23010399003)(1800799024)(366016)(7416014)(10070799003)(18002099003)(921020)(22082099003)(56012099006)(10067099003)(11063799006)(3023799007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 2 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?ZHAvT3V0aUkrZG9GdFN2eG5WUkZkOUFFM0xud0tjOWJlNVp1NEgxNjlnVDRW?= =?utf-8?B?ZFJabTFaM0ZxY3V0WGFkc1BuTE04ZDcrekN2dVdoMkpOcENrOGs5aTA4NU1n?= =?utf-8?B?VFJWMW8xRTdLVmdUQUUwY0phKzk0Q1pZRHFKU3FhSUhRdjQzNnZpUDFnWFFh?= =?utf-8?B?RkFyRVFq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=?utf-8?B?TkhDVHhPNlZCUnRMQ3l2KzlYVHV0ZFdFVXBLZTBVUG10bVJhaHpkNElmaVFq?= =?utf-8?B?ME1lSTNzRjBBMmtTUlZyZ1orS05mcFhQTXNOck1UQ1BTaEtlV0pvcVVqYVFF?= =?utf-8?B?ZDl4dFFaM0RHMzkxVmVud2xPZTRMbm0yZ29pM1JNSUNZVHB5MnE2UmErM2t5?= =?utf-8?B?R3l5NWpiS2xtRnFIWTBiY3JFZHJ2NU54dGswQWZ0cDV6ak1YTHk4V0NOT2xi?= =?utf-8?B?NS96YzdZVHdYaVlEVHlYSGNZY2Y3SE5rSDdPVC9Xb0NyLzVjVFRqc2d1Yy93?= =?utf-8?B?a1Z6YjBZemsxVFYzbVdZcnFjZnlTUVBqNyt4Vmg0SzlGRTRxeGpHNXJsVjY0?= =?utf-8?Q?Qr/ZATv5XFD8zC7tCgMKUtGQ9fLQn085OzJ3tZHmU5MYW?= X-MS-Exchange-AntiSpam-MessageData-1: 2YgEKMDAkEb/dA== X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 20dfecff-5ab1-4a67-b550-08defc5f5cb3 X-MS-Exchange-CrossTenant-AuthSource: BL0PR12MB2353.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Aug 2026 12:59:24.1474 (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: imtEZldSga7aUr36CsmDTYQMKKFT/8y3iLNmD4wtOm7wqvzklgUqleoI8zuGKbiVOo8nw0lc4W2f7f1Z2Vhq8Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6833 Similar to the typed encoding layer, add some decoding type machinery. Add a simple macro `nvkv_decode!` which implements `Schema` for a struct by composing visit calls to each member. Add some common `Schema` kinds, such as `Array` which collects an array value into a fixed maximum size array, and `Required` which fails a decode if the value is not sent. Signed-off-by: Eliot Courtney --- drivers/gpu/nova-core/gsp/nvkv.rs | 60 ++++- drivers/gpu/nova-core/gsp/nvkv/decode.rs | 393 +++++++++++++++++++++++++++++++ 2 files changed, 452 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/nova-core/gsp/nvkv.rs b/drivers/gpu/nova-core/gsp/nvkv.rs index bf6500d54b21..a0068847bb80 100644 --- a/drivers/gpu/nova-core/gsp/nvkv.rs +++ b/drivers/gpu/nova-core/gsp/nvkv.rs @@ -9,7 +9,7 @@ //! function calls will map to some struct - for example, f(GPU_NAME_STRING_KEY, 0, b"some gpu") //! naturally maps to storing a &str with the GPU name. -#![expect(unused_imports)] +#![cfg_attr(not(CONFIG_KUNIT), expect(unused_imports))] #![cfg_attr(not(CONFIG_KUNIT), expect(unused_macros))] use core::marker::PhantomData; @@ -78,6 +78,64 @@ fn default() -> Self { } } +/// A fixed capacity vector that holds at most `N` elements. +#[derive(Debug, Copy, Clone, PartialEq, Eq, Zeroable)] +pub(crate) struct ArrayVec { + data: [T; N], + len: usize, +} + +impl ArrayVec { + /// Replaces the contents with a copy of `slice`. + /// + /// Fails with `EMSGSIZE` if `slice` is longer than the capacity. + pub(crate) fn set_from_slice(&mut self, slice: &[T]) -> Result + where + T: Copy, + { + let Some(dst) = self.data.get_mut(..slice.len()) else { + return Err(EMSGSIZE); + }; + + dst.copy_from_slice(slice); + self.len = slice.len(); + + Ok(()) + } + + /// Returns the initialized elements as a slice. + #[inline] + pub(crate) fn as_slice(&self) -> &[T] { + // PANIC: `len` is bounded by `N`. + &self.data[..self.len] + } +} + +impl Default for ArrayVec { + fn default() -> Self { + Self { + data: [T::default(); N], + len: 0, + } + } +} + +impl Deref for ArrayVec { + type Target = [T]; + + #[inline] + fn deref(&self) -> &Self::Target { + self.as_slice() + } +} + +/// A schema field for an array value under the NVKV key `KEY_ID`. +#[derive(Default)] +#[repr(transparent)] +pub(crate) struct Array( + pub(crate) ArrayVec, +); + bitfield! { /// The op word that starts each NVKV operation. struct Op(u64) { diff --git a/drivers/gpu/nova-core/gsp/nvkv/decode.rs b/drivers/gpu/nova-core/gsp/nvkv/decode.rs index ee8b6ab5a3a4..9112dcf1aaca 100644 --- a/drivers/gpu/nova-core/gsp/nvkv/decode.rs +++ b/drivers/gpu/nova-core/gsp/nvkv/decode.rs @@ -3,16 +3,311 @@ #![cfg_attr(not(CONFIG_KUNIT), expect(dead_code))] +use core::marker::PhantomData; + use kernel::prelude::*; use crate::gsp::nvkv::{ + Array, + ArrayVec, Index, + Key, KeyId, Op, Opcode, // }; use crate::num; +/// Defines a schema struct together with its [`Schema`] implementation that decodes into `$target`. +/// +/// Each member of the struct should implement `Schema`. For every (key, index, value) triple +/// decoded from the NVKV stream, the generated parent `Schema` implementation will call each member +/// in declaration order with that triple. If a member consumes that triple, it will stop there. +/// Otherwise it will keep going until all members are tried. +/// +/// The schema struct holds the state required by the schema implementation to do the decode. It's +/// recommended to use one of the existing Schema kinds (`Required`, `Accumulated`, `Key`, `Array`, +/// `Indexed`) for each member. +/// +/// # Examples +/// +/// ``` +/// nvkv_decode! { +/// #[derive(Default)] +/// struct RequestSchema => Request { +/// id: Required, +/// name: Array, +/// } +/// } +/// ``` +macro_rules! nvkv_decode { + ( + $(#[$attr:meta])* + $vis:vis struct $name:ident => $target: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::Schema for $name { + type Target = $target; + + fn visit( + &mut self, + key: $crate::gsp::nvkv::KeyId, + index: $crate::gsp::nvkv::Index, + value: $crate::gsp::nvkv::DecoderValue<'_>, + ) -> ::kernel::error::Result { + Ok(false + $( || $crate::gsp::nvkv::Schema::visit(&mut self.$field, key, index, value)? )*) + } + + #[inline(always)] + fn finish(self) -> impl ::kernel::prelude::Init { + ::kernel::try_init!(Self::Target { + $( $field <- $crate::gsp::nvkv::Schema::finish(self.$field), )* + }? ::kernel::error::Error) + } + } + }; +} +pub(crate) use nvkv_decode; + +impl TryFrom, Error = Error> + Default, const KEY_ID: KeyId> Schema + for Key +{ + type Target = T; + + #[inline(always)] + fn visit<'a>(&mut self, key: KeyId, index: Index, value: DecoderValue<'a>) -> Result { + if key != KEY_ID { + Ok(false) + } else if index != Index::new::<0>() { + // Single values being set must be at index 0. + Err(EINVAL) + } else { + // Overwrite and take the latest value here. + self.0 = value.try_into()?; + Ok(true) + } + } + + #[inline(always)] + fn finish(self) -> impl Init { + Ok(self.0) + } +} + +impl TryFrom, Error = Error>, const KEY_ID: KeyId> Schema + for Key, KEY_ID> +{ + type Target = Option; + + #[inline(always)] + fn visit<'a>(&mut self, key: KeyId, index: Index, value: DecoderValue<'a>) -> Result { + if key != KEY_ID { + Ok(false) + } else if index != Index::new::<0>() { + // Single values being set must be at index 0. + Err(EINVAL) + } else { + // Overwrite and take the latest value here. + self.0 = Some(value.try_into()?); + Ok(true) + } + } + + #[inline(always)] + fn finish(self) -> impl Init { + Ok(self.0) + } +} + +impl Schema for Array +where + for<'a> &'a [T]: TryFrom, Error = Error>, +{ + type Target = ArrayVec; + + fn visit<'a>(&mut self, key: KeyId, index: Index, value: DecoderValue<'a>) -> Result { + if key != KEY_ID { + return Ok(false); + } + // Require to be at index 0 + if index != Index::new::<0>() { + return Err(EINVAL); + } + // Reject oversized and take the latest value. + self.0.set_from_slice(value.try_into()?)?; + Ok(true) + } + + #[inline(always)] + fn finish(self) -> impl Init { + Ok(self.0) + } +} + +/// A schema field for a key that must be present. +/// +/// `finish` fails with `EINVAL` if no value arrived for the key. +#[repr(transparent)] +pub(crate) struct Required(Key, KEY_ID>); + +impl TryFrom, Error = Error>, const KEY_ID: KeyId> Schema + for Required +{ + type Target = T; + + #[inline(always)] + fn visit<'a>(&mut self, key: KeyId, index: Index, value: DecoderValue<'a>) -> Result { + self.0.visit(key, index, value) + } + + #[inline(always)] + fn finish(self) -> impl Init { + (self.0).0.ok_or(EINVAL) + } +} + +impl Default for Required { + fn default() -> Self { + Self(None.into()) + } +} + +/// Expects objects specified sequentially with index starting from zero. +pub(crate) struct Accumulated { + current_index: Index, + current: S, + current_started: bool, + next: S, + accumulated: KVVec, +} + +impl Accumulated { + /// Creates an empty accumulator. + pub(crate) fn new() -> Self { + Self { + current_index: Index::new::<0>(), + current: S::default(), + current_started: false, + next: S::default(), + accumulated: KVVec::new(), + } + } + + fn into_vec(mut self) -> Result> { + if self.current_started { + let done = core::mem::take(&mut self.current); + self.accumulated.push_init(done.finish(), GFP_KERNEL)?; + } + Ok(self.accumulated) + } +} + +impl Schema for Accumulated { + type Target = KVVec; + + fn visit<'a>(&mut self, key: KeyId, index: Index, value: DecoderValue<'a>) -> Result { + if index != self.current_index { + if !self.next.visit(key, Index::new::<0>(), value)? { + // Unrelated key to us. + return Ok(false); + } + + // Require that objects at index k have all their keys sent before the k + 1 th object + // can be completed. Require that objects are sent contiguously in order from index 0. + if !self.current_started || index != self.current_index + 1 { + return Err(EINVAL); + } + + // The current value must be finished. Finish it and start working on `next`. + let done = core::mem::replace(&mut self.current, core::mem::take(&mut self.next)); + self.accumulated.push_init(done.finish(), GFP_KERNEL)?; + self.current_started = true; + self.current_index = index; + Ok(true) + } else { + let consumed = self.current.visit(key, Index::new::<0>(), value)?; + self.current_started |= consumed; + Ok(consumed) + } + } + + #[inline(always)] + fn finish(self) -> impl Init { + self.into_vec() + } +} + +impl Default for Accumulated { + fn default() -> Self { + Self::new() + } +} + +/// A schema field that scatters indexed values into an array of `N` slots. +#[repr(transparent)] +pub(crate) struct Indexed([T; N], PhantomData); + +/// Copies `elems`, converted to `T`, into `slots` at `start`. +/// +/// Fails with `EINVAL` if the window does not fit in `slots`. +fn scatter_window, As: Copy>(slots: &mut [T], start: usize, elems: &[As]) -> Result { + let end = start.checked_add(elems.len()).ok_or(EINVAL)?; + // Reject indices outside of the declared array size. + let dst = slots.get_mut(start..end).ok_or(EINVAL)?; + for (d, &e) in dst.iter_mut().zip(elems) { + *d = T::from(e); + } + Ok(()) +} + +impl Schema for Indexed +where + T: From, + As: Copy + for<'a> TryFrom, Error = Error>, + for<'a> &'a [As]: TryFrom, Error = Error>, +{ + type Target = [T; N]; + + fn visit<'a>(&mut self, key: KeyId, index: Index, value: DecoderValue<'a>) -> Result { + if key != KEY_ID { + return Ok(false); + } + let start = index.cast::().get(); + // Accept both scalar vs scattered array setting for flexibility. + match <&[As]>::try_from(value) { + Ok(elems) => scatter_window(&mut self.0, start, elems)?, + Err(_) => scatter_window(&mut self.0, start, &[As::try_from(value)?])?, + } + Ok(true) + } + + #[inline(always)] + fn finish(self) -> impl Init { + Ok(self.0) + } +} + +impl Default + for Indexed +{ + fn default() -> Self { + Self([T::default(); N], PhantomData) + } +} + /// A decoded NVKV value. #[derive(Copy, Clone)] pub(crate) enum DecoderValue<'a> { @@ -255,4 +550,102 @@ fn finish(self) -> impl Init { Ok(()) } + + // Tests that decoding via the `nvkv_decode!` macro works correctly. + #[test] + fn decode_typed_struct() -> Result { + const SCALAR32_KEY: KeyId = 0x1234; + const SCALAR64_KEY: KeyId = 0x1235; + const ARRAY8_KEY: KeyId = 0x1236; + const ARRAY32_KEY: KeyId = 0x1237; + const ARRAY64_KEY: KeyId = 0x1238; + const OPT_PRESENT_KEY: KeyId = 0x1239; + const OPT_ABSENT_KEY: KeyId = 0x123a; + const X_KEY: KeyId = 0x0100; + const Y_KEY: KeyId = 0x0101; + const SLOT_KEY: KeyId = 0x0200; + + const SCALAR32_VALUE: u32 = 0x89ab_cdef; + const SCALAR64_VALUE: u64 = 0x0123_4567_89ab_cdef; + const ARRAY8_VALUE: &[u8] = &[0x12, 0x34, 0x56]; + const ARRAY32_VALUE: &[u32] = &[0x0123_4567, 0x89ab_cdef]; + const ARRAY64_VALUE: &[u64] = &[0x0123_4567_89ab_cdef, 0xfedc_ba98_7654_3210]; + const OPT_PRESENT_VALUE: u32 = 0x55; + + nvkv_decode! { + #[derive(Default)] + struct PairSchema => Pair { + x: Required, + y: Required, + } + } + + struct Pair { + x: u32, + y: u32, + } + + nvkv_decode! { + #[derive(Default)] + struct TestSchema => TestDecodeable { + scalar32: Required, + scalar64: Required, + array8: Array, + array32: Array, + array64: Array, + opt_present: Key, { OPT_PRESENT_KEY }>, + opt_absent: Key, { OPT_ABSENT_KEY }>, + pairs: Accumulated, + slots: Indexed, + } + } + + struct TestDecodeable { + scalar32: u32, + scalar64: u64, + array8: ArrayVec, + array32: ArrayVec, + array64: ArrayVec, + opt_present: Option, + opt_absent: Option, + pairs: KVVec, + slots: [u32; 4], + } + + let index0 = Index::new::<0>(); + let index1 = Index::new::<1>(); + let mut encoder = Encoder::new(); + encoder.encode_u32(SCALAR32_KEY, index0, SCALAR32_VALUE)?; + encoder.encode_u64(SCALAR64_KEY, index0, SCALAR64_VALUE)?; + encoder.encode_array8(ARRAY8_KEY, index0, ARRAY8_VALUE)?; + encoder.encode_array32(ARRAY32_KEY, index0, ARRAY32_VALUE)?; + encoder.encode_array64(ARRAY64_KEY, index0, ARRAY64_VALUE)?; + encoder.encode_u32(OPT_PRESENT_KEY, index0, OPT_PRESENT_VALUE)?; + encoder.encode_u32(X_KEY, index0, 1)?; + encoder.encode_u32(Y_KEY, index0, 2)?; + encoder.encode_u32(SLOT_KEY, index1, 20)?; + encoder.encode_u32(X_KEY, index1, 3)?; + encoder.encode_u32(Y_KEY, index1, 4)?; + encoder.encode_u32(SLOT_KEY, index0, 10)?; + let serialized = encoder.finish(); + + let decoder = Decoder::new(&serialized, UnknownKeyPolicy::Error); + let decoded = KBox::try_init(decoder.decode(TestSchema::default())?, GFP_KERNEL)?; + + assert_eq!(decoded.scalar32, SCALAR32_VALUE); + assert_eq!(decoded.scalar64, SCALAR64_VALUE); + assert_eq!(*decoded.array8, *ARRAY8_VALUE); + assert_eq!(*decoded.array32, *ARRAY32_VALUE); + assert_eq!(*decoded.array64, *ARRAY64_VALUE); + assert_eq!(decoded.opt_present, Some(OPT_PRESENT_VALUE)); + assert_eq!(decoded.opt_absent, None); + assert_eq!(decoded.pairs.len(), 2); + assert_eq!(decoded.pairs[0].x, 1); + assert_eq!(decoded.pairs[0].y, 2); + assert_eq!(decoded.pairs[1].x, 3); + assert_eq!(decoded.pairs[1].y, 4); + assert_eq!(decoded.slots, [10, 20, 0, 0]); + + Ok(()) + } } -- 2.55.0