From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from BL0PR03CU003.outbound.protection.outlook.com (mail-eastusazon11012048.outbound.protection.outlook.com [52.101.53.48]) (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 035D4377ECA; Mon, 28 Sep 2026 08:43:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.53.48 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790585000; cv=fail; b=WaB+EQDFKKZNaf/uGTiqzUiB21qJUyNHkoJpKBL/O1OcfjxmJLSoXKGOErNyBlv26mAKwYl/YC1oT7tcppXnPpRyGSzrZpasCrzw5P9b0g5MHHr/PQTG3TFMiVxuZZFAyzwcy8TGyf6jYzJ+elSq0olC3LsqrWuvRu8VrCOGKDg= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790585000; c=relaxed/simple; bh=N1+1QdzF0147TR9pjiReVPFo7PeCGIPCUhzl14a/iEw=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=q/78ZxW5QuyHCJK0BmQv5Vg6Cj6AJ+Wy36taWIys1pGJkl1bi+3s5Pp7YLXacRW0hPhEXU/J771c96xssfu7QmU5MeurDH2dEsd2MCYFajcm3A6zzdkAcYKatDlxk+xEFzcu8+GhOR6im8wKvOyor9Pp6pS+UXAC3Gi/WUAeDLg= 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=hyuMyI2t; arc=fail smtp.client-ip=52.101.53.48 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="hyuMyI2t" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=vcjpFjuk1heijbN4YPL440zABJWW+8fUib48hlozXuUEkEF/qrQ9/QcKqPYOlw03SMvZJ/3t26gCvSIq2ZSzKYide8cgDiQwqshiDuYMtyJhasrAyoYKx4J4rD7v9/heQ9JlnsNdC01PdSuzKxtOED5RDpGzYpwl4EjSgCjUdoCpXbz50dJkjfBZIHXVADMY/G1x5J65yafuXiLngc72vrMH2DhvJSpFc7XQyj1VT63wMGtND4nOau7C+LP+awA8lHPPmmpb0HhLl70zPwLx5sKv4qxD3Bu2if5azVLvTm09aHRh1Yxg5rE/jUaA9Bpf3HTLX0b4TY9eiP/ybt5RhQ== 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=9bQZ4Pl+C2/kVysjclV8MULoP6A6723vSBikcamVv9Y=; b=aPaYk8koj0EU89ju3BK0ma6Ffn8UiVJk9e3qRj/9D3aZ+TdZEFMFIDe5P9RUuSR7COVrKil9none+/v/zQQd21Ao3P0MWwTLMjZtq4cLOZBNLElKpdi42JvKChq8NU9+P1kdRKxwh0v4RvxFOG6ZftsnT1DrulJvOo7sVptf8TkFOOhq4o0zuF/iVaC7qtVkvK+eewY55BGEeBQ7dd075Hpi+T62FspRpxuGtOQvS+9e/pSAZis/ISiIFH6mEjz/vfEi3knDCS2ckzGjoysgNG2xw1HcLTlxUzhpwWw3S/lMyhhSK6kIEhUIkoCFE8R9Ie0boMMeZc+ygDOp7U0ZSQ== 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=9bQZ4Pl+C2/kVysjclV8MULoP6A6723vSBikcamVv9Y=; b=hyuMyI2t8lAR7kdFMKs2/rw66/dtFW1XERs9byJ1ZqPm5oWUXhTNufeJ+Fy6+dDBZlOlXm2dL2cFfIwZwNJXLPIuRCckrX74oniFzkzJj4YW+QNjEs5EOWYKCNrr/xShH0OZqq2ozP+7dmiP7d8XBtiGGYFM6ArOL2DHJQbeJk0zFtrK/8/EFEFlYTlvYm0DEsiYnDSuxCX8gQ9yc+HtxB8HgO5jMGFcCGWVBh3ksMN1u5sYjnbuqAJ2BQqpmWyf8D5KsdX2YsyPv8jzXNFyH4WTytWM9A1AhEII8JR+8CfqTi+itz6Pg8/aMO4XxFhlcYP3gBTkNO3kgBHjfdjVqg== Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DS0PR12MB6413.namprd12.prod.outlook.com (2603:10b6:8:ce::10) by CY8PR12MB8065.namprd12.prod.outlook.com (2603:10b6:930:73::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.24; Mon, 28 Sep 2026 08:43:13 +0000 Received: from DS0PR12MB6413.namprd12.prod.outlook.com ([fe80::e82a:6673:4142:37fa]) by DS0PR12MB6413.namprd12.prod.outlook.com ([fe80::e82a:6673:4142:37fa%5]) with mapi id 15.21.0451.022; Mon, 28 Sep 2026 08:43:13 +0000 From: Eliot Courtney Date: Mon, 28 Sep 2026 17:42:43 +0900 Subject: [PATCH v3 4/8] gpu: nova-core: add NVKV encoder Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260928-b4-nvkv-v3-4-f04504c262c2@nvidia.com> References: <20260928-b4-nvkv-v3-0-f04504c262c2@nvidia.com> In-Reply-To: <20260928-b4-nvkv-v3-0-f04504c262c2@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: TY6P301CA0006.JPNP301.PROD.OUTLOOK.COM (2603:1096:405:3be::7) To DS0PR12MB6413.namprd12.prod.outlook.com (2603:10b6:8:ce::10) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS0PR12MB6413:EE_|CY8PR12MB8065:EE_ X-MS-Office365-Filtering-Correlation-Id: 0780275d-3497-469e-0dce-08df1d3c880e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|10070799003|23010399003|7416014|376014|1800799024|366016|10067099003|56012099006|11063799006|921020|3023799007|6133799003|18002099003|22082099003; X-Microsoft-Antispam-Message-Info: GJS9DGw/zhovl6BQhtcUwhM3qHVPTw3RKiuqKRGdN1aIuC+oqGPoK/NAk2IfKbGx5ExmrkWBZMp0Qbr5l4aI/a83lKo/RtOK4cxE+Th0zYAB3kvYvIc3wfgpemV3RD+UGsNdzbYUxWuLyC0ch4wJjfY8ops2G4OxJ3ji90l5Bp8ToZU7VbKVjUIIlnqKm+FneKvm6fVCGLH6WYB/yeS2JT9m8L2+txsX3imGHA830Eah6fgpsr2RODg16kqtfYXe5P0Ku4GWMLtCA57yPHMP+wyAA0wOyz26H4sqisjq5Drw8xvlDN3gNAFlVqVc7FK2D1hk3qS0fS22jDCibDsetKdolodhmjhLDeQrx0HT/v+Z1zfZJJzQ6o02Pt6xjSk/TY2F7WCG3JOog0aIOMv67udMmCPbtoUO2fheFPjHYL9vXnl+/ErFhxshnQ1oVgkpGQ3gSmbkJsNl5NIdyerqPqFB7cWXePuOPjXfA/qABpFzJVaOMgtOtfPcj7RmSVF3t1zkhqupNSzkXsAPmHcD48fNKP/0BVbyXaNvztdRDSIrmc1ByTlihyt3LMgTcHOYzrbmANjR2MIjMhpBAYl9O/22p/0vyrjF8dBdtnEMIaqMVJxKGxay1eQjXnWB4+JRbxNjy9BzkoVBA+RuwBcMlrrqHblFExS7tsAegQeoYwiX/YDbYfMSG2f7N6MSzud9IUFWAV3VpYarg0mSPyeUOA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS0PR12MB6413.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(10070799003)(23010399003)(7416014)(376014)(1800799024)(366016)(10067099003)(56012099006)(11063799006)(921020)(3023799007)(6133799003)(18002099003)(22082099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 2 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?Ulpxa3pva0hWTHJwWHZzSUZLOVNCem9ha05PemhQdFFveml4STZQTzRGbkhC?= =?utf-8?B?MGx6ZElBYlQ5M0VGN1RKcVFWaGlOTmgrY1JPd1hXbDFIaVh4Nmg2K2lSU01r?= =?utf-8?B?SHNrWnB3ZldadGY0b3BheVBHMTZmVXFqK3F2NTN0ZFlrbGFFcXlIRnljbG8w?= =?utf-8?B?KzkvSTl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alplblBPYS8zT1lOakVKWFRjUHJSejZR?= =?utf-8?B?NE1IeFFiT2JGYXk4QWl6aWdVeWZJWVJRNzFmL25YSEhTejQzN1RUOEJQMW5U?= =?utf-8?B?MDhzcnBySWF6VUk3QloyWFRjbVZrUHJIRlJNM2R0Szl2dkE3OFkzYkxNSWtw?= =?utf-8?B?TFpaTEZUV3FibnhwcGI4WGZ1R0VOQmYvY3BydFBjcExuNGtYOTk0WjF2dndu?= =?utf-8?B?T01MY1ljeEZzR0ZLNHQzeWJoaUduVDY5dXF0bkpQR3hlb1ZmaXAxclFyaTZB?= =?utf-8?B?MHRaclptQ2QwdjVINy9aWXBGbTAvVGVWVDNPbTBkcHFjRGNjc1JIRmxnMVpa?= =?utf-8?B?Z1B2TXpKcGZjUUdBT2ZxcEJVVTFlQ2NOWTVjeXZjWXJ5V1FscDBIYm9VeWdQ?= =?utf-8?Q?tGwtJ6pHrYIYLnjrvWBi17IqEbtpdGhttRXr6yh7NmEXJ?= X-MS-Exchange-AntiSpam-MessageData-1: j9fl+feSpfKNAA== X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0780275d-3497-469e-0dce-08df1d3c880e X-MS-Exchange-CrossTenant-AuthSource: DS0PR12MB6413.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Sep 2026 08:43:13.0092 (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: KubIG4GOZPChFQbP8RnGk/Xlb/rvpb9D9U4uMw2gaJz/N+3sQ2u+L/jd51+STwKtUQguWNpchWXuN9iLJErCdQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB8065 Add an encoder for NVKV, which is the wire format for GMCAPI. The encoded stream is a sequence of 64-bit values. The first 64-bit value encodes an op word which describes the function of the next N values. Essentially, the format encodes a sequence of calls to some function f(key, index, value), where value is a [u8], u32, u64, [u32], or a [u64]. The key is a u16 and the index is a 12 bit integer. The interpretation of these function calls is per GMCAPI. Add tests for the wire encoding for each primitive. Co-developed-by: Danilo Krummrich Signed-off-by: Danilo Krummrich Signed-off-by: Eliot Courtney --- drivers/gpu/nova-core/gsp.rs | 1 + drivers/gpu/nova-core/gsp/nvkv.rs | 122 ++++++++++++++++++ drivers/gpu/nova-core/gsp/nvkv/encode.rs | 210 +++++++++++++++++++++++++++++++ 3 files changed, 333 insertions(+) diff --git a/drivers/gpu/nova-core/gsp.rs b/drivers/gpu/nova-core/gsp.rs index 25ea43f1cbe9..7b1bf74464e2 100644 --- a/drivers/gpu/nova-core/gsp.rs +++ b/drivers/gpu/nova-core/gsp.rs @@ -24,6 +24,7 @@ pub(crate) mod cmdq; pub(crate) mod commands; mod fw; +mod nvkv; mod regs; mod sequencer; diff --git a/drivers/gpu/nova-core/gsp/nvkv.rs b/drivers/gpu/nova-core/gsp/nvkv.rs new file mode 100644 index 000000000000..0957dce92f96 --- /dev/null +++ b/drivers/gpu/nova-core/gsp/nvkv.rs @@ -0,0 +1,122 @@ +// SPDX-License-Identifier: GPL-2.0 +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + +//! Codec for NVKV, the binary key-value format of GMCAPI. +//! +//! Essentially, the format encodes a sequence of calls to some function f(key, index, value), +//! where value is a [u8], u32, u64, [u32], or a [u64]. The key is a u16 and the index is a 12 bit +//! integer. The interpretation of these function calls is per GMCAPI. Generally speaking, the +//! 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)] + +use core::ops::Deref; + +use kernel::{ + alloc::{ + allocator::KVmalloc, + Allocator, // + }, + bitfield, + num::Bounded, + prelude::*, // +}; +use zerocopy::Immutable; + +use crate::bounded_enum; + +mod encode; +pub(crate) use encode::*; + +/// The allocator backing [`EncodedStream`]. +type StreamAllocator = KVmalloc; + +/// An encoded NVKV byte stream. +/// +/// # Invariants +/// +/// The byte length is always a multiple of `size_of::()`. +pub(crate) struct EncodedStream(Vec); + +impl EncodedStream { + /// Creates an empty stream. + fn new() -> Self { + // INVARIANT: An empty stream's byte length is 0, a multiple of `size_of::()`. + Self(Vec::new()) + } + + /// Appends a single `u64` to the stream. + fn push_u64(&mut self, value: u64) -> Result { + // INVARIANT: Appending `size_of::()` bytes keeps the byte length a multiple of + // `size_of::()`. + Ok(self.0.extend_from_slice(&value.to_ne_bytes(), GFP_KERNEL)?) + } + + /// Appends `data` as bytes to the stream, zero-padded to a `u64` boundary. + fn extend_with_padding(&mut self, data: &T) -> Result { + let bytes = data.as_bytes(); + let padded = bytes.len().next_multiple_of(size_of::()); + // Reserve so that a failed allocation can't leave the invariant violated. + self.0.reserve(padded, GFP_KERNEL)?; + self.0.extend_from_slice(bytes, GFP_KERNEL)?; + // INVARIANT: The padding ensures the total length remains a multiple of + // `size_of::()`. + Ok(self.0.extend_with(padded - bytes.len(), 0u8, GFP_KERNEL)?) + } +} + +// The Deref to `&[u64]` relies on this alignment guarantee. +static_assert!(align_of::() <= StreamAllocator::MIN_ALIGN); + +impl Deref for EncodedStream { + type Target = [u64]; + + fn deref(&self) -> &Self::Target { + // An empty `Vec`'s pointer isn't necessarily aligned by `StreamAllocator::MIN_ALIGN`. + if self.0.is_empty() { + return &[]; + } + + // PANIC: By the type invariants the byte length is a multiple of `size_of::()`, and + // the backing buffer of a non-empty vector has at least `u64` alignment per + // `StreamAllocator`'s minimum alignment. + <[u64]>::ref_from_bytes(&self.0).expect("EncodedStream invariant violated") + } +} + +/// The identifier of an NVKV key. +pub(crate) type KeyId = u16; + +/// The index of an NVKV value. +pub(crate) type Index = Bounded; + +bitfield! { + /// The op word that starts each NVKV operation. + struct Op(u64) { + 15:0 key; + 27:16 index => Index; + 31:28 opcode ?=> Opcode; + 63:32 value; + } +} + +bounded_enum! { + /// Describes the format of the following NVKV operation. + #[derive(Debug, Copy, Clone, PartialEq, Eq)] + #[repr(u8)] + enum Opcode with TryFrom> { + /// A 32-bit value in the op word. + Imm32 = 0, + /// 32-bit values for consecutive keys, starting at the op word's key. + Seq32 = 1, + /// 64-bit values for consecutive keys, starting at the op word's key. + Seq64 = 2, + /// An array of bytes. + Array8 = 3, + /// An array of 32-bit elements. + Array32 = 4, + /// An array of 64-bit elements. + Array64 = 5, + } +} diff --git a/drivers/gpu/nova-core/gsp/nvkv/encode.rs b/drivers/gpu/nova-core/gsp/nvkv/encode.rs new file mode 100644 index 000000000000..8b66ddb15189 --- /dev/null +++ b/drivers/gpu/nova-core/gsp/nvkv/encode.rs @@ -0,0 +1,210 @@ +// SPDX-License-Identifier: GPL-2.0 +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + +#![cfg_attr(not(CONFIG_KUNIT), expect(dead_code))] + +use kernel::prelude::*; + +use super::{ + EncodedStream, + Index, + KeyId, + Op, + Opcode, // +}; + +/// An encoder for an NVKV stream. +pub(crate) struct Encoder { + stream: EncodedStream, +} + +impl Encoder { + /// Creates an empty encoder. + pub(crate) fn new() -> Self { + Self { + stream: EncodedStream::new(), + } + } + + /// Returns the encoded data. + #[must_use = "encoded stream must be consumed"] + pub(crate) fn finish(self) -> EncodedStream { + self.stream + } + + #[inline] + fn encode_op(&mut self, op: Op) -> Result { + self.stream.push_u64(op.into_raw()) + } + + /// Encodes a 32-bit value as an IMM32 pair, with the value in the op word. + #[inline] + pub(crate) fn encode_u32(&mut self, key: KeyId, index: Index, value: u32) -> Result { + // TODO: Consider automatically merging sequential keys. + self.encode_op( + Op::zeroed() + .with_key(key) + .with_index(index) + .with_opcode(Opcode::Imm32) + .with_value(value), + ) + } + + /// Encodes a 64-bit value as a single-element SEQ64 pair. + #[inline] + pub(crate) fn encode_u64(&mut self, key: KeyId, index: Index, value: u64) -> Result { + // TODO: Consider automatically merging sequential keys. + const KEY_COUNT: u32 = 1; + self.encode_op( + Op::zeroed() + .with_key(key) + .with_index(index) + .with_opcode(Opcode::Seq64) + .with_value(KEY_COUNT), + )?; + self.stream.push_u64(value) + } + + /// Encodes a byte array as an ARRAY8 pair, zero-padded to a multiple of 8 bytes. + #[inline] + pub(crate) fn encode_array8(&mut self, key: KeyId, index: Index, array: &[u8]) -> Result { + let value_count = u32::try_from(array.len()).map_err(|_| EMSGSIZE)?; + self.encode_op( + Op::zeroed() + .with_key(key) + .with_index(index) + .with_opcode(Opcode::Array8) + .with_value(value_count), + )?; + self.stream.extend_with_padding(array) + } + + /// Encodes a 32-bit array as an ARRAY32 pair, zero-padded to a multiple of 8 bytes. + #[inline] + pub(crate) fn encode_array32(&mut self, key: KeyId, index: Index, array: &[u32]) -> Result { + let value_count = u32::try_from(array.len()).map_err(|_| EMSGSIZE)?; + self.encode_op( + Op::zeroed() + .with_key(key) + .with_index(index) + .with_opcode(Opcode::Array32) + .with_value(value_count), + )?; + self.stream.extend_with_padding(array) + } + + /// Encodes a 64-bit array as an ARRAY64 pair. + #[inline] + pub(crate) fn encode_array64(&mut self, key: KeyId, index: Index, array: &[u64]) -> Result { + let value_count = u32::try_from(array.len()).map_err(|_| EMSGSIZE)?; + self.encode_op( + Op::zeroed() + .with_key(key) + .with_index(index) + .with_opcode(Opcode::Array64) + .with_value(value_count), + )?; + self.stream.extend_with_padding(array) + } +} + +#[kunit_tests(nova_core_nvkv_encode)] +mod tests { + use super::*; + + // Tests that each kind of value is encoded to NVKV wire format properly. + #[test] + fn encode_all_value_kinds() -> Result { + // All keys, indexes, and values are distinct but arbitrary values to make it easier for the + // test to catch bugs in the encoded output. + const U32_KEY: KeyId = 0x1001; + const U64_KEY: KeyId = 0x1002; + const ARRAY8_KEY: KeyId = 0x1003; + const ARRAY32_KEY: KeyId = 0x1004; + const ARRAY64_KEY: KeyId = 0x1005; + + const U32_VALUE: u32 = 0x1111_2222; + const U64_VALUE: u64 = 0x3333_4444_5555_6666; + const ARRAY8_VALUE: &[u8] = &[0xaa, 0xbb, 0xcc]; + const ARRAY32_VALUE: &[u32] = &[0xbbbb_cccc, 0xdddd_eeee]; + const ARRAY64_VALUE: &[u64] = &[0x0123_4567_89ab_cdef, 0xfedc_ba98_7654_3210]; + + let mut encoder = Encoder::new(); + encoder.encode_u32(U32_KEY, Index::new::<0>(), U32_VALUE)?; + encoder.encode_u64(U64_KEY, Index::new::<1>(), U64_VALUE)?; + encoder.encode_array8(ARRAY8_KEY, Index::new::<2>(), ARRAY8_VALUE)?; + encoder.encode_array32(ARRAY32_KEY, Index::new::<3>(), ARRAY32_VALUE)?; + encoder.encode_array64(ARRAY64_KEY, Index::new::<4>(), ARRAY64_VALUE)?; + + let encoded = encoder.finish(); + assert_eq!(encoded.len(), 10); + + // IMM32 has its value in the op word. + assert_eq!( + encoded[0], + Op::zeroed() + .with_key(U32_KEY) + .with_index(Index::new::<0>()) + .with_opcode(Opcode::Imm32) + .with_value(U32_VALUE) + .into_raw() + ); + + // The SEQ64 op word followed by the value. + assert_eq!( + encoded[1], + Op::zeroed() + .with_key(U64_KEY) + .with_index(Index::new::<1>()) + .with_opcode(Opcode::Seq64) + .with_value(1u32) + .into_raw() + ); + assert_eq!(encoded[2], U64_VALUE); + + // The ARRAY8 op word has the byte count. The bytes follow, padded out to a whole word. + assert_eq!( + encoded[3], + Op::zeroed() + .with_key(ARRAY8_KEY) + .with_index(Index::new::<2>()) + .with_opcode(Opcode::Array8) + .with_value(3u32) + .into_raw() + ); + assert_eq!( + encoded[4], + u64::from_le_bytes([0xaa, 0xbb, 0xcc, 0, 0, 0, 0, 0]) + ); + + // The ARRAY32 op word has the element count. The two elements follow in little endian. + assert_eq!( + encoded[5], + Op::zeroed() + .with_key(ARRAY32_KEY) + .with_index(Index::new::<3>()) + .with_opcode(Opcode::Array32) + .with_value(2u32) + .into_raw() + ); + assert_eq!( + encoded[6], + u64::from(ARRAY32_VALUE[1]) << 32 | u64::from(ARRAY32_VALUE[0]) + ); + + // The ARRAY64 op word has the element count with the two elements after. + assert_eq!( + encoded[7], + Op::zeroed() + .with_key(ARRAY64_KEY) + .with_index(Index::new::<4>()) + .with_opcode(Opcode::Array64) + .with_value(2u32) + .into_raw() + ); + assert_eq!(encoded[8], ARRAY64_VALUE[0]); + assert_eq!(encoded[9], ARRAY64_VALUE[1]); + + Ok(()) + } +} -- 2.55.0