Skip to content

Commit b345c91

Browse files
committed
gpu: nova-core: add Chipset::name() method
There are a few cases where we need the lowercase name of a given chipset, notably to resolve firmware files paths for dynamic loading or to build the module information. So far, we relied on a static `NAMES` array for the latter, and some CString hackery for the former. Replace both with a new `name` const method that returns the lowercase name of a chipset instance. We can generate it using the `paste!` macro. Using this method removes the need to create a `CString` when loading firmware, and lets us remove a couple of utility functions that now have no user. Acked-by: Danilo Krummrich <dakr@kernel.org> Link: https://lore.kernel.org/r/20250913-nova_firmware-v6-3-9007079548b0@nvidia.com Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
1 parent e7c9698 commit b345c91

3 files changed

Lines changed: 20 additions & 33 deletions

File tree

drivers/gpu/nova-core/firmware.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ pub(crate) struct Firmware {
3030

3131
impl Firmware {
3232
pub(crate) fn new(dev: &device::Device, chipset: Chipset, ver: &str) -> Result<Firmware> {
33-
let mut chip_name = CString::try_from_fmt(fmt!("{chipset}"))?;
34-
chip_name.make_ascii_lowercase();
35-
let chip_name = &*chip_name;
33+
let chip_name = chipset.name();
3634

3735
let request = |name_| {
3836
CString::try_from_fmt(fmt!("nvidia/{chip_name}/gsp/{name_}-{ver}.bin"))
@@ -180,8 +178,8 @@ impl<const N: usize> ModInfoBuilder<N> {
180178
let mut this = Self(firmware::ModInfoBuilder::new(module_name));
181179
let mut i = 0;
182180

183-
while i < gpu::Chipset::NAMES.len() {
184-
this = this.make_entry_chipset(gpu::Chipset::NAMES[i]);
181+
while i < gpu::Chipset::ALL.len() {
182+
this = this.make_entry_chipset(gpu::Chipset::ALL[i].name());
185183
i += 1;
186184
}
187185

drivers/gpu/nova-core/gpu.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::firmware::{Firmware, FIRMWARE_VERSION};
99
use crate::gfw;
1010
use crate::gsp::Gsp;
1111
use crate::regs;
12-
use crate::util;
1312
use core::fmt;
1413

1514
macro_rules! define_chipset {
@@ -26,13 +25,23 @@ macro_rules! define_chipset {
2625
$( Chipset::$variant, )*
2726
];
2827

29-
pub(crate) const NAMES: [&'static str; Self::ALL.len()] = [
30-
$( util::const_bytes_to_str(
31-
util::to_lowercase_bytes::<{ stringify!($variant).len() }>(
32-
stringify!($variant)
33-
).as_slice()
34-
), )*
35-
];
28+
::kernel::macros::paste!(
29+
/// Returns the name of this chipset, in lowercase.
30+
///
31+
/// # Examples
32+
///
33+
/// ```
34+
/// let chipset = Chipset::GA102;
35+
/// assert_eq!(chipset.name(), "ga102");
36+
/// ```
37+
pub(crate) const fn name(&self) -> &'static str {
38+
match *self {
39+
$(
40+
Chipset::$variant => stringify!([<$variant:lower>]),
41+
)*
42+
}
43+
}
44+
);
3645
}
3746

3847
// TODO[FPRI]: replace with something like derive(FromPrimitive)

drivers/gpu/nova-core/util.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,6 @@
33
use kernel::prelude::*;
44
use kernel::time::{Delta, Instant, Monotonic};
55

6-
pub(crate) const fn to_lowercase_bytes<const N: usize>(s: &str) -> [u8; N] {
7-
let src = s.as_bytes();
8-
let mut dst = [0; N];
9-
let mut i = 0;
10-
11-
while i < src.len() && i < N {
12-
dst[i] = (src[i] as char).to_ascii_lowercase() as u8;
13-
i += 1;
14-
}
15-
16-
dst
17-
}
18-
19-
pub(crate) const fn const_bytes_to_str(bytes: &[u8]) -> &str {
20-
match core::str::from_utf8(bytes) {
21-
Ok(string) => string,
22-
Err(_) => kernel::build_error!("Bytes are not valid UTF-8."),
23-
}
24-
}
25-
266
/// Wait until `cond` is true or `timeout` elapsed.
277
///
288
/// When `cond` evaluates to `Some`, its return value is returned.

0 commit comments

Comments
 (0)