[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20251029-nova-vbios-frombytes-v1-3-ac441ebc1de3@nvidia.com>
Date: Wed, 29 Oct 2025 00:07:38 +0900
From: Alexandre Courbot <acourbot@...dia.com>
To: Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <lossin@...nel.org>, Andreas Hindborg <a.hindborg@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>,
Danilo Krummrich <dakr@...nel.org>, David Airlie <airlied@...il.com>,
Simona Vetter <simona@...ll.ch>
Cc: John Hubbard <jhubbard@...dia.com>,
Alistair Popple <apopple@...dia.com>,
Joel Fernandes <joelagnelf@...dia.com>, Timur Tabi <ttabi@...dia.com>,
Edwin Peer <epeer@...dia.com>, rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org, nouveau@...ts.freedesktop.org,
dri-devel@...ts.freedesktop.org, Alexandre Courbot <acourbot@...dia.com>
Subject: [PATCH 3/5] gpu: nova-core: vbios: use FromBytes for PcirStruct
Use `from_bytes_copy_prefix` to create `PcirStruct` instead of building
it ourselves from the bytes stream. This lets us remove a few array
accesses and results in shorter code.
Signed-off-by: Alexandre Courbot <acourbot@...dia.com>
---
drivers/gpu/nova-core/vbios.rs | 40 ++++++++++++----------------------------
1 file changed, 12 insertions(+), 28 deletions(-)
diff --git a/drivers/gpu/nova-core/vbios.rs b/drivers/gpu/nova-core/vbios.rs
index b6c20627a5e3..b02a1997306f 100644
--- a/drivers/gpu/nova-core/vbios.rs
+++ b/drivers/gpu/nova-core/vbios.rs
@@ -314,45 +314,29 @@ struct PcirStruct {
max_runtime_image_len: u16,
}
+// SAFETY: all bit patterns are valid for `PcirStruct`.
+unsafe impl FromBytes for PcirStruct {}
+
impl PcirStruct {
fn new(dev: &device::Device, data: &[u8]) -> Result<Self> {
- if data.len() < core::mem::size_of::<PcirStruct>() {
- dev_err!(dev, "Not enough data for PcirStruct\n");
- return Err(EINVAL);
- }
-
- let mut signature = [0u8; 4];
- signature.copy_from_slice(&data[0..4]);
+ let pcir = PcirStruct::from_bytes_copy_prefix(data).ok_or(EINVAL)?.0;
// Signature should be "PCIR" (0x52494350) or "NPDS" (0x5344504e).
- if &signature != b"PCIR" && &signature != b"NPDS" {
- dev_err!(dev, "Invalid signature for PcirStruct: {:?}\n", signature);
+ if &pcir.signature != b"PCIR" && &pcir.signature != b"NPDS" {
+ dev_err!(
+ dev,
+ "Invalid signature for PcirStruct: {:?}\n",
+ pcir.signature
+ );
return Err(EINVAL);
}
- let mut class_code = [0u8; 3];
- class_code.copy_from_slice(&data[13..16]);
-
- let image_len = u16::from_le_bytes([data[16], data[17]]);
- if image_len == 0 {
+ if pcir.image_len == 0 {
dev_err!(dev, "Invalid image length: 0\n");
return Err(EINVAL);
}
- Ok(PcirStruct {
- signature,
- vendor_id: u16::from_le_bytes([data[4], data[5]]),
- device_id: u16::from_le_bytes([data[6], data[7]]),
- device_list_ptr: u16::from_le_bytes([data[8], data[9]]),
- pci_data_struct_len: u16::from_le_bytes([data[10], data[11]]),
- pci_data_struct_rev: data[12],
- class_code,
- image_len,
- vendor_rom_rev: u16::from_le_bytes([data[18], data[19]]),
- code_type: data[20],
- last_image: data[21],
- max_runtime_image_len: u16::from_le_bytes([data[22], data[23]]),
- })
+ Ok(pcir)
}
/// Check if this is the last image in the ROM.
--
2.51.0
Powered by blists - more mailing lists