From 45f238024b4c88b37c9a291187430915bf6ad61b Mon Sep 17 00:00:00 2001 From: termg Date: Fri, 20 Nov 2026 14:58:22 +0000 Subject: [PATCH 1/2] drm/asahi: g17: Log an unsupported firmware build only when none matches firmware_variant() tries the identified builds in turn, the one the boot loader's apple,firmware-version names first. Each try measured the loaded TEXT again and, when it was not that build, logged "unsupported GPU firmware build", the build it was not, and "GPU firmware image not admitted: EINVAL", even when a later build then matched. Without apple,firmware-version (the installed aura does not write it on T8142), an M5 on macOS 27.2 logs three such errors for the 26A428 build before it admits 26B123. Measure the TEXT once and try each build against that measurement (Measured::find, which leaves the copy as it found it). Only when no build matches, log the measured identity and every supported build, before the existing "Unsupported GPU firmware image" line. A matched build still logs "GPU firmware build: ...". The TEXT is now copied and hashed once per probe rather than once per candidate build. Signed-off-by: termg --- drivers/gpu/drm/asahi/g17/mod.rs | 37 +++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/drivers/gpu/drm/asahi/g17/mod.rs b/drivers/gpu/drm/asahi/g17/mod.rs index ca362da3e93f5..befcadd8d3011 100644 --- a/drivers/gpu/drm/asahi/g17/mod.rs +++ b/drivers/gpu/drm/asahi/g17/mod.rs @@ -1890,11 +1890,16 @@ impl Gpu { let others = builds .iter() .filter(|build| Some(build.os_version) != version); + // Measure the loaded TEXT once and try each build against it. A build that is not this + // image is not an error while another one may be: only when none is, say what was + // measured and what is supported. + let mut measured = Self::measure_text(dev)?; let mut identified = None; for build in Iterator::chain(preferred, others) { - if Self::identify_image(dev, build).is_err() { + let Some(image) = measured.find(build.images) else { continue; - } + }; + dev_info!(dev.as_ref(), "GPU firmware build: {}\n", image.name); let admits = build.config.is_none_or(|config| { config .core_layout @@ -1911,6 +1916,14 @@ impl Gpu { dev_info!(dev.as_ref(), "GPU firmware: {}\n", build.name); return Ok((build.variant, build.config)); } + dev_err!( + dev.as_ref(), + "unsupported GPU firmware build: {}\n", + measured.identity(builds.iter().flat_map(|build| build.images)) + ); + for image in builds.iter().flat_map(|build| build.images) { + dev_err!(dev.as_ref(), "supported: {}\n", image.name); + } dev_err!( dev.as_ref(), "Unsupported GPU firmware image (reported macOS version {:?})\n", @@ -1919,19 +1932,23 @@ impl Gpu { Err(ENODEV) } - /// Checks that the loaded TEXT (the `fw-text` region) is one of `build`'s images, with the DATA size - /// of the `fw-data` region. The version property is only a selection hint. - fn identify_image(dev: &AsahiDrmDevice, build: &'static fw::variant::Build) -> Result { + /// Measures the loaded TEXT (the `fw-text` region), with the DATA size of the `fw-data` + /// region, for [`Self::firmware_variant`] to identify among the builds. The version + /// property is only a selection hint. + fn measure_text(dev: &AsahiDrmDevice) -> Result { let node = dev.as_ref().as_ref().of_node().ok_or(EINVAL)?; let text = node.reserved_mem_region_to_resource_byname(c_str!("fw-text"))?; let data = node.reserved_mem_region_to_resource_byname(c_str!("fw-data"))?; let start = text.start(); let end = start.checked_add(text.size()).ok_or(EINVAL)?; - crate::fwid::identify(dev.as_ref().as_ref(), start..end, data.size(), build.images) - .inspect_err(|e| { - dev_err!(dev.as_ref(), "GPU firmware image not admitted: {:?}\n", e) - })?; - Ok(()) + crate::fwid::measure(start..end, data.size()).inspect_err(|e| { + if *e == EAGAIN { + dev_err!( + dev.as_ref(), + "GPU firmware TEXT changed while it was measured\n" + ); + } + }) } /// The macOS version the boot loader reports for the boot firmware that loaded the GPU From 313c264eb8020acd86b552120c513125964fd783 Mon Sep 17 00:00:00 2001 From: termg Date: Sat, 21 Nov 2026 03:12:37 +0000 Subject: [PATCH 2/2] drm/asahi: g17: Report a TEXT that cannot be measured, and test the shared measurement Follow-ups to "Log an unsupported firmware build only when none matches": - measure_text() logged only EAGAIN. A missing fw-text or fw-data region (a DT or boot loader mismatch) now returned from the probe silently, where before it printed "GPU firmware image not admitted". Log every other error too. - A measurement error (EAGAIN, ENOMEM, EINVAL) now ends the probe with that error, where before each candidate build measured again and the probe ended with ENODEV after "Unsupported GPU firmware image". So a transient EAGAIN (TEXT changed between the two copies) is no longer retried by the next candidate; it is logged instead. - "GPU firmware build: " is logged once, for the build that is taken, next to "GPU firmware: ". It was logged for every build whose image matched, so an image shared by a build whose configuration does not admit the probed cores logged it twice. - The "supported:" list walked every build's images, so an image shared by builds that differ only in their core layout (as T6041 25G83 is) was listed once per build. List each image once, comparing name and TEXT digest: the address of a const-promoted entry is not guaranteed to be shared. - g17-firmware-selection-tests.py compiles firmware_variant() against stub types, and its Build stub still had a single `image` with an identify_image() stub, so the host test stopped building (E0609 "no field images"). Model the new shape instead: Build::images, a stub measure_text() and a Measured whose find() records each image it is tried against, so the existing checks on the order of the candidates still hold. The stub dev_info!/dev_err! now build their messages, so the log lines are type-checked as well. Signed-off-by: termg --- drivers/gpu/drm/asahi/g17/mod.rs | 42 ++++++++++++----- tools/asahi/g17-firmware-selection-tests.py | 51 +++++++++++++++------ 2 files changed, 68 insertions(+), 25 deletions(-) diff --git a/drivers/gpu/drm/asahi/g17/mod.rs b/drivers/gpu/drm/asahi/g17/mod.rs index befcadd8d3011..3fbb6006507b0 100644 --- a/drivers/gpu/drm/asahi/g17/mod.rs +++ b/drivers/gpu/drm/asahi/g17/mod.rs @@ -1899,7 +1899,6 @@ impl Gpu { let Some(image) = measured.find(build.images) else { continue; }; - dev_info!(dev.as_ref(), "GPU firmware build: {}\n", image.name); let admits = build.config.is_none_or(|config| { config .core_layout @@ -1907,12 +1906,13 @@ impl Gpu { .is_none_or(|measured| measured == cores) }); if admits { - identified = Some(build); + identified = Some((build, image)); break; } - identified.get_or_insert(build); + identified.get_or_insert((build, image)); } - if let Some(build) = identified { + if let Some((build, image)) = identified { + dev_info!(dev.as_ref(), "GPU firmware build: {}\n", image.name); dev_info!(dev.as_ref(), "GPU firmware: {}\n", build.name); return Ok((build.variant, build.config)); } @@ -1921,8 +1921,16 @@ impl Gpu { "unsupported GPU firmware build: {}\n", measured.identity(builds.iter().flat_map(|build| build.images)) ); - for image in builds.iter().flat_map(|build| build.images) { - dev_err!(dev.as_ref(), "supported: {}\n", image.name); + // Builds that differ only in their core layout share an image: list each image once. + // Compare by name and digest; the address of a `const`-promoted entry is not unique. + let images = || builds.iter().flat_map(|build| build.images); + for (i, image) in images().enumerate() { + if !images() + .take(i) + .any(|seen| (seen.name, seen.text_sha256) == (image.name, image.text_sha256)) + { + dev_err!(dev.as_ref(), "supported: {}\n", image.name); + } } dev_err!( dev.as_ref(), @@ -1936,17 +1944,27 @@ impl Gpu { /// region, for [`Self::firmware_variant`] to identify among the builds. The version /// property is only a selection hint. fn measure_text(dev: &AsahiDrmDevice) -> Result { - let node = dev.as_ref().as_ref().of_node().ok_or(EINVAL)?; - let text = node.reserved_mem_region_to_resource_byname(c_str!("fw-text"))?; - let data = node.reserved_mem_region_to_resource_byname(c_str!("fw-data"))?; - let start = text.start(); - let end = start.checked_add(text.size()).ok_or(EINVAL)?; - crate::fwid::measure(start..end, data.size()).inspect_err(|e| { + let measure = || { + let node = dev.as_ref().as_ref().of_node().ok_or(EINVAL)?; + let text = node.reserved_mem_region_to_resource_byname(c_str!("fw-text"))?; + let data = node.reserved_mem_region_to_resource_byname(c_str!("fw-data"))?; + let start = text.start(); + let end = start.checked_add(text.size()).ok_or(EINVAL)?; + crate::fwid::measure(start..end, data.size()) + }; + measure().inspect_err(|e| { if *e == EAGAIN { dev_err!( dev.as_ref(), "GPU firmware TEXT changed while it was measured\n" ); + } else { + // A missing or bad fw-text/fw-data region: a DT or boot loader mismatch. + dev_err!( + dev.as_ref(), + "GPU firmware image not measured (fw-text/fw-data): {:?}\n", + e + ); } }) } diff --git a/tools/asahi/g17-firmware-selection-tests.py b/tools/asahi/g17-firmware-selection-tests.py index 6b9cc52d70ea7..43335e4e3098b 100644 --- a/tools/asahi/g17-firmware-selection-tests.py +++ b/tools/asahi/g17-firmware-selection-tests.py @@ -27,12 +27,13 @@ def method(source, signature): #![allow(dead_code)] type Result = std::result::Result; const ENODEV: i32 = -19; -macro_rules! dev_info { ($($t:tt)*) => { () }; } -macro_rules! dev_err { ($($t:tt)*) => { () }; } +// The device argument is dropped; the message and its arguments are still built. +macro_rules! dev_info { ($dev:expr, $($t:tt)*) => { { let _ = format!($($t)*); } }; } +macro_rules! dev_err { ($dev:expr, $($t:tt)*) => { { let _ = format!($($t)*); } }; } struct AsahiDrmDevice { version: Option<[u32; 3]>, image: u32, - checked: std::cell::RefCell>, + checked: std::rc::Rc>>, } struct CoreLayout { cores: Option<(u32, u32)>, @@ -41,6 +42,29 @@ def method(source, signature): id: u32, core_layout: CoreLayout, } +mod fwid { + pub struct FirmwareBuild { + pub name: &'static str, + pub image: u32, + pub text_sha256: [u8; 32], + } + // The loaded TEXT, modelled by its image number; records every image it is tried against. + pub struct Measured { + pub image: u32, + pub checked: std::rc::Rc>>, + } + impl Measured { + pub fn find(&mut self, builds: &'static [FirmwareBuild]) -> Option<&'static FirmwareBuild> { + builds.iter().find(|build| { + self.checked.borrow_mut().push(build.image); + build.image == self.image + }) + } + pub fn identity(&mut self, _: impl IntoIterator) -> u32 { + self.image + } + } +} mod fw { pub struct Variant(pub u32); pub mod variant { @@ -48,7 +72,7 @@ def method(source, signature): pub struct Build { pub os_version: [u32; 3], pub name: &'static str, - pub image: u32, + pub images: &'static [crate::fwid::FirmwareBuild], pub variant: &'static Variant, pub config: Option<&'static crate::Config>, } @@ -58,35 +82,36 @@ def method(source, signature): struct Gpu; impl Gpu { fn os_firmware_version(dev: &AsahiDrmDevice) -> Option<[u32; 3]> { dev.version } - fn identify_image(dev: &AsahiDrmDevice, build: &'static fw::variant::Build) -> Result { - dev.checked.borrow_mut().push(build.image); - if dev.image == build.image { Ok(()) } else { Err(ENODEV) } + fn measure_text(dev: &AsahiDrmDevice) -> Result { + Ok(fwid::Measured { image: dev.image, checked: dev.checked.clone() }) } ''' TESTS = r''' } +static IMAGE_1: [fwid::FirmwareBuild; 1] = [fwid::FirmwareBuild { name: "image 1", image: 1, text_sha256: [1; 32] }]; +static IMAGE_2: [fwid::FirmwareBuild; 1] = [fwid::FirmwareBuild { name: "image 2", image: 2, text_sha256: [2; 32] }]; static FIRST: fw::Variant = fw::Variant(1); static SECOND: fw::Variant = fw::Variant(2); static OWN: Config = Config { id: 7, core_layout: CoreLayout { cores: None } }; static BUILDS: [fw::variant::Build; 2] = [ - fw::variant::Build { os_version: [26,6,2], name: "first", image: 1, variant: &FIRST, config: None }, - fw::variant::Build { os_version: [27,0,0], name: "second", image: 2, variant: &SECOND, + fw::variant::Build { os_version: [26,6,2], name: "first", images: &IMAGE_1, variant: &FIRST, config: None }, + fw::variant::Build { os_version: [27,0,0], name: "second", images: &IMAGE_2, variant: &SECOND, config: Some(&OWN) }, ]; // One image admitted with the configurations of two core layouts, as T6041 25G83 is. static FORTY: Config = Config { id: 40, core_layout: CoreLayout { cores: Some((4, 40)) } }; static THIRTY_TWO: Config = Config { id: 32, core_layout: CoreLayout { cores: Some((4, 32)) } }; static SHARED: [fw::variant::Build; 3] = [ - fw::variant::Build { os_version: [26,5,2], name: "other", image: 1, variant: &FIRST, config: None }, - fw::variant::Build { os_version: [26,6,2], name: "40", image: 2, variant: &SECOND, + fw::variant::Build { os_version: [26,5,2], name: "other", images: &IMAGE_1, variant: &FIRST, config: None }, + fw::variant::Build { os_version: [26,6,2], name: "40", images: &IMAGE_2, variant: &SECOND, config: Some(&FORTY) }, - fw::variant::Build { os_version: [26,6,2], name: "32", image: 2, variant: &SECOND, + fw::variant::Build { os_version: [26,6,2], name: "32", images: &IMAGE_2, variant: &SECOND, config: Some(&THIRTY_TWO) }, ]; const CORES: (u32, u32) = (4, 40); fn device(version: Option<[u32; 3]>, image: u32) -> AsahiDrmDevice { - AsahiDrmDevice { version, image, checked: std::cell::RefCell::new(Vec::new()) } + AsahiDrmDevice { version, image, checked: Default::default() } } fn main() { use fw::variant::Firmware;