Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Documentation/devicetree/bindings/gpu/apple,agx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,25 @@ properties:
backend extends the firmware's native delay with its host idle-hold
control; an absent property preserves the native policy.

apple,fast-die0-target-temp:
$ref: /schemas/types.yaml#/definitions/uint32
minimum: 85
maximum: 110
description:
Die temperature, in degrees Celsius, that the GPU firmware's die
temperature limiter holds the GPU below. A board whose enclosure must
run the GPU cooler than the firmware default sets it. It can only
lower the firmware default (110 C on the G17 family); absent, the
default applies.

apple,fast-die0-release-temp:
$ref: /schemas/types.yaml#/definitions/uint32
minimum: 40
description:
Die temperature, in degrees Celsius, below which the GPU firmware's die
temperature limiter releases the GPU. Must be below the target. It can
only lower the firmware default; absent, the default applies.

apple,pmp:
$ref: /schemas/types.yaml#/definitions/phandle
description: PMP firmware provider owning the GPU power vote and telemetry
Expand Down
9 changes: 9 additions & 0 deletions arch/arm64/boot/dts/apple/t8140-j700.dts
Original file line number Diff line number Diff line change
Expand Up @@ -2179,6 +2179,15 @@

&gpu {
status = "okay";
/*
* Our own measurements on this machine, 10 min sustained GPU load:
* macOS holds the hottest GPU die key at or below 105.5 C. Linux
* with the firmware limiter's default 110 C target peaks at 107.75 C
* (base600 row); a 108 C target peaks at 105.78 C, a 107 C target at
* 104.73 C (die107-600 row, 417 fps vs 460 fps). 107 C is the
* highest target that stays within the macOS level.
*/
apple,fast-die0-target-temp = <107>;
};

#include "hwmon-common.dtsi"
Expand Down
209 changes: 209 additions & 0 deletions drivers/gpu/drm/asahi/g17/die_limit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT

//! Per-board die temperature limit of the G17-family firmware builds.
//!
//! Every G17 build runs a die temperature limiter in the firmware: above its target the firmware
//! lowers the GPU state on its own, below its release temperature it lets go. Each build's tables
//! carry a default. A board whose enclosure must stay cooler describes its limit on the GPU node
//! with `apple,fast-die0-target-temp` and `apple,fast-die0-release-temp`, in degrees Celsius like
//! the older generations' `apple,fast-die0-release-temp`. Both are optional; a board value only
//! ever lowers a build's default, so a node without them runs every build as before.

use kernel::{c_str, device, prelude::*};

/// Highest accepted target, in 0.01 °C: the 26A428 default, which no board may raise.
const TARGET_MAX_CC: u32 = super::fw::initdata::DIE_LIMIT.target_cc;
/// Lowest accepted target, in 0.01 °C. A lower limit would hold the GPU at its lowest state at
/// ordinary load temperatures; such a value is a mistake, not a policy.
const TARGET_MIN_CC: u32 = 8_500;
/// Lowest accepted release temperature, in 0.01 °C.
const RELEASE_MIN_CC: u32 = 4_000;
/// Smallest gap kept between a lowered target and the release temperature, in 0.01 °C.
const MIN_GAP_CC: u32 = 100;

/// Limiter temperatures as one firmware table holds them, in 0.01 °C.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Limits {
pub(crate) target_cc: u32,
pub(crate) release_cc: u32,
}

/// The board's limit from the device tree, validated; `None` where the board sets nothing.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct DieLimit {
target_cc: Option<u32>,
release_cc: Option<u32>,
}

/// A device-tree value the driver did not take as written.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Problem {
/// Target above [`TARGET_MAX_CC`], clamped to it.
TargetClamped(u32),
/// Target below [`TARGET_MIN_CC`], ignored.
TargetIgnored(u32),
/// Release temperature below [`RELEASE_MIN_CC`] or not below the target, ignored.
ReleaseIgnored(u32),
}

impl DieLimit {
/// Reads and validates the GPU node's limit. Values out of range are clamped or ignored
/// with a warning; an unreadable node leaves every build's default.
pub(crate) fn read(dev: &device::Device) -> Self {
let Some(node) = dev.of_node() else {
return Self::default();
};
let read = |name: &'static CStr| match node.get_property::<u32>(name) {
Ok(value) => Some(value),
Err(ENOENT) => None,
Err(error) => {
dev_warn!(dev, "Ignoring unreadable GPU {:?}: {:?}\n", name, error);
None
}
};
let target = read(c_str!("apple,fast-die0-target-temp"));
let release = read(c_str!("apple,fast-die0-release-temp"));
let limit = Self::from_celsius(target, release, |problem| match problem {
Problem::TargetClamped(t) => dev_warn!(
dev,
"GPU die target {} C above the firmware default, using {} C\n",
t,
TARGET_MAX_CC / 100
),
Problem::TargetIgnored(t) => dev_warn!(
dev,
"GPU die target {} C below {} C, ignored\n",
t,
TARGET_MIN_CC / 100
),
Problem::ReleaseIgnored(r) => {
dev_warn!(dev, "GPU die release temperature {} C out of range, ignored\n", r)
}
});
if let Some(target_cc) = limit.target_cc {
dev_info!(dev, "GPU die limiter target {} C from the device tree\n", target_cc / 100);
}
limit
}

/// Validates device-tree values in degrees Celsius.
fn from_celsius(
target: Option<u32>,
release: Option<u32>,
mut problem: impl FnMut(Problem),
) -> Self {
let target_cc = target.and_then(|t| match t.saturating_mul(100) {
cc if cc > TARGET_MAX_CC => {
problem(Problem::TargetClamped(t));
Some(TARGET_MAX_CC)
}
cc if cc < TARGET_MIN_CC => {
problem(Problem::TargetIgnored(t));
None
}
cc => Some(cc),
});
let ceiling = target_cc.unwrap_or(TARGET_MAX_CC);
let release_cc = release.and_then(|r| match r.saturating_mul(100) {
cc if (RELEASE_MIN_CC..ceiling).contains(&cc) => Some(cc),
_ => {
problem(Problem::ReleaseIgnored(r));
None
}
});
Self { target_cc, release_cc }
}

/// Whether the board sets any limit.
pub(crate) fn is_set(&self) -> bool {
*self != Self::default()
}

/// The limiter temperatures for a firmware table whose build default is `default`: the
/// board's where set, never above the build's, with the release below the target.
pub(crate) fn apply(&self, default: Limits) -> Limits {
if !self.is_set() {
return default;
}
let target_cc = self.target_cc.map_or(default.target_cc, |t| t.min(default.target_cc));
let release_cc = self
.release_cc
.map_or(default.release_cc, |r| r.min(default.release_cc))
.min(target_cc.saturating_sub(MIN_GAP_CC));
Limits { target_cc, release_cc }
}

/// Writes the limiter temperatures for `default` at `target` and `release`, byte offsets
/// of 32-bit little-endian words in `bytes`.
pub(crate) fn write(&self, bytes: &mut [u8], default: Limits, target: usize, release: usize) {
let limits = self.apply(default);
bytes[target..target + 4].copy_from_slice(&limits.target_cc.to_le_bytes());
bytes[release..release + 4].copy_from_slice(&limits.release_cc.to_le_bytes());
}
}

#[kunit_tests(asahi_g17_die_limit)]
mod tests {
use super::*;

const BUILD: Limits = Limits { target_cc: 11_000, release_cc: 8_000 };
const STATE_25G83: Limits = Limits { target_cc: 10_100, release_cc: 9_600 };

fn parse(target: Option<u32>, release: Option<u32>) -> (DieLimit, KVec<Problem>) {
let mut problems = KVec::new();
let limit = DieLimit::from_celsius(target, release, |p| {
let _ = problems.push(p, GFP_KERNEL);
});
(limit, problems)
}

#[test]
fn absent_keeps_every_build_default() {
let (limit, problems) = parse(None, None);
assert!(problems.is_empty());
assert!(!limit.is_set());
assert_eq!(limit.apply(BUILD), BUILD);
assert_eq!(limit.apply(STATE_25G83), STATE_25G83);
}

#[test]
fn board_target_lowers_but_never_raises() {
let (limit, problems) = parse(Some(108), None);
assert!(problems.is_empty());
assert_eq!(limit.apply(BUILD), Limits { target_cc: 10_800, release_cc: 8_000 });
// A build whose own default is lower keeps it.
assert_eq!(limit.apply(STATE_25G83), STATE_25G83);
}

#[test]
fn nonsense_is_clamped_or_ignored() {
let (limit, problems) = parse(Some(150), None);
assert_eq!(&problems[..], &[Problem::TargetClamped(150)]);
assert_eq!(limit.apply(BUILD), BUILD);

let (limit, problems) = parse(Some(20), None);
assert_eq!(&problems[..], &[Problem::TargetIgnored(20)]);
assert!(!limit.is_set());

let (limit, problems) = parse(Some(u32::MAX), Some(u32::MAX));
assert_eq!(
&problems[..],
&[Problem::TargetClamped(u32::MAX), Problem::ReleaseIgnored(u32::MAX)]
);
assert_eq!(limit.apply(BUILD), BUILD);

let (_, problems) = parse(Some(100), Some(100));
assert_eq!(&problems[..], &[Problem::ReleaseIgnored(100)]);
}

#[test]
fn release_stays_below_a_lowered_target() {
let (limit, problems) = parse(Some(95), Some(70));
assert!(problems.is_empty());
assert_eq!(limit.apply(BUILD), Limits { target_cc: 9_500, release_cc: 7_000 });
// 25G83 power state: target 95, its 96 C release pulled below the target.
assert_eq!(limit.apply(STATE_25G83), Limits { target_cc: 9_500, release_cc: 7_000 });
let (limit, _) = parse(Some(95), None);
assert_eq!(limit.apply(STATE_25G83), Limits { target_cc: 9_500, release_cc: 9_400 });
}
}
43 changes: 27 additions & 16 deletions drivers/gpu/drm/asahi/g17/fw/initdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use kernel::prelude::*;

use super::clear;
use crate::g17::die_limit::{DieLimit, Limits};

pub(crate) mod v25g83;

Expand Down Expand Up @@ -1558,14 +1559,14 @@ impl PrimaryStatusB {
pub(crate) const POWER_STATE: usize = 0xe440;

/// Initializes the primary status B; `fwctl_va` is the zeroed
/// firmware-control page ([`FWCTL_SIZE`] bytes).
pub(crate) fn init(&mut self, fwctl_va: u64) {
/// firmware-control page ([`FWCTL_SIZE`] bytes), `limit` the board's die limit.
pub(crate) fn init(&mut self, fwctl_va: u64, limit: &DieLimit) {
clear(self);
self.status = StatusBlock::new();
self.fwctl_state_va = fwctl_va;
self.fwctl_ring_va = fwctl_va + FWCTL_RING;
self.unk_e434 = 1;
self.power.init();
self.power.init(limit);
}
}

Expand Down Expand Up @@ -1604,10 +1605,18 @@ static_assert!(private::STATUS_B + size_of::<SecondaryConfig>() == private::SECO

/// Die temperature sensors read by the die temperature limiter.
const TEMP_SENSOR_MASK: u64 = 0x82a;
/// Die temperature the limiter holds the GPU below, in 0.01 °C.
const TEMP_TARGET: u32 = 11_000;
/// Die temperature below which the limiter releases, in 0.01 °C.
const TEMP_RELEASE: u32 = 8_000;
/// Default die temperatures of the limiter, in 0.01 °C: it holds the GPU below the target and
/// releases below the release temperature. A board may lower them
/// ([`DieLimit`](crate::g17::die_limit::DieLimit)).
pub(crate) const DIE_LIMIT: Limits = Limits { target_cc: 11_000, release_cc: 8_000 };
const TEMP_TARGET: u32 = DIE_LIMIT.target_cc;
const TEMP_RELEASE: u32 = DIE_LIMIT.release_cc;
/// Offsets of the limiter target and release in the [`PowerConfigTail`].
const TAIL_TEMP_TARGET: usize = 0x5ac;
const TAIL_TEMP_RELEASE: usize = 0x558;
/// Offsets of the limiter target and release in the [`PowerState`].
const STATE_TEMP_TARGET: usize = 0xc8;
const STATE_TEMP_RELEASE: usize = 0xcc;
/// Proportional gain of the limiter: 20.34 as an IEEE-754 single.
const TEMP_KP: u32 = 0x41a2_b852;
/// Integral gain of the limiter, per control period: 9.376 as an IEEE-754
Expand Down Expand Up @@ -1715,15 +1724,15 @@ const POWER_CONFIG_TAIL_WORDS: Words = &[
(0x50c, 0x3f4ccccd, 1),
(0x510, 0x3e4ccccd, 1),
(0x550, TEMP_SENSOR_MASK as u32, 1),
(0x558, TEMP_RELEASE, 1),
(TAIL_TEMP_RELEASE, TEMP_RELEASE, 1),
(0x560, 4, 1),
(0x574, 0x3f800000, 1),
(0x57c, TEMP_KI, 1),
(0x588, 0x47800000, 1),
(0x58c, TEMP_KP, 1),
(0x598, 0x28, 1),
(0x59c, 0x3e8, 2),
(0x5ac, TEMP_TARGET, 1),
(TAIL_TEMP_TARGET, TEMP_TARGET, 1),
(0x5e0, 0x3e8, 1),
(0x7fc, 0x42000006, 1),
(0xa10, 0xb8520000, 1),
Expand Down Expand Up @@ -1805,8 +1814,8 @@ const POWER_STATE_WORDS: Words = &[
(0x18, 0x40880000, 1),
(0x1c, 0x28, 1),
(0x20, 1, 1),
(0xc8, TEMP_TARGET, 1),
(0xcc, TEMP_RELEASE, 1),
(STATE_TEMP_TARGET, TEMP_TARGET, 1),
(STATE_TEMP_RELEASE, TEMP_RELEASE, 1),
(0xd8, TEMP_KP, 1),
(0xdc, TEMP_KI, 1),
(0xf0, 0x26ac, 1),
Expand Down Expand Up @@ -1905,13 +1914,14 @@ pub(crate) use super::power::PowerState;
unsafe impl Zeroable for PowerState {}

impl PowerState {
fn init(&mut self) {
fn init(&mut self, limit: &DieLimit) {
// SAFETY: The repr(C) object consists entirely of initialized integer
// and byte-array storage; every bit pattern is valid.
let bytes = unsafe {
core::slice::from_raw_parts_mut(core::ptr::from_mut(self).cast(), size_of::<Self>())
};
fill_words(bytes, POWER_STATE_WORDS);
limit.write(bytes, DIE_LIMIT, STATE_TEMP_TARGET, STATE_TEMP_RELEASE);
self.ppm_enabled = 1;
self.ppm_target_mw = 9750;
}
Expand Down Expand Up @@ -1957,8 +1967,9 @@ pub(crate) struct PowerConfigTail(pub(crate) [u8; 0x1000]);
unsafe impl Zeroable for PowerConfigTail {}

impl PowerConfigTail {
fn init(&mut self) {
fn init(&mut self, limit: &DieLimit) {
fill_words(&mut self.0, POWER_CONFIG_TAIL_WORDS);
limit.write(&mut self.0, DIE_LIMIT, TAIL_TEMP_TARGET, TAIL_TEMP_RELEASE);
}
}

Expand Down Expand Up @@ -1998,10 +2009,10 @@ static_assert!(bundle::VIEWS[2] + 0xe40 == bundle::POWER_CONFIG);
unsafe impl Zeroable for PowerConfig {}

impl PowerConfig {
/// Initializes the power configuration.
pub(crate) fn init(&mut self) {
/// Initializes the power configuration with the board's die limit.
pub(crate) fn init(&mut self, limit: &DieLimit) {
self.head.init();
self.tail.init();
self.tail.init(limit);
}
}

Expand Down
Loading