diff --git a/drivers/gpu/drm/asahi/g17/mod.rs b/drivers/gpu/drm/asahi/g17/mod.rs index ca362da3e93f5..3fbb6006507b0 100644 --- a/drivers/gpu/drm/asahi/g17/mod.rs +++ b/drivers/gpu/drm/asahi/g17/mod.rs @@ -1890,11 +1890,15 @@ 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; - } + }; let admits = build.config.is_none_or(|config| { config .core_layout @@ -1902,15 +1906,32 @@ 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)); } + dev_err!( + dev.as_ref(), + "unsupported GPU firmware build: {}\n", + measured.identity(builds.iter().flat_map(|build| build.images)) + ); + // 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(), "Unsupported GPU firmware image (reported macOS version {:?})\n", @@ -1919,19 +1940,33 @@ 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 { - 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(()) + /// 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 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 + ); + } + }) } /// The macOS version the boot loader reports for the boot firmware that loaded the GPU 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;