Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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/power/apple,pmc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,25 @@ patternProperties:
The low-power platform profile tells the SMC (apple,smc) that Low
Power Mode is on, in the record that carries High Power mode.

apple,low-power-capacity-ceiling:
type: boolean
description:
When the low-power profile holds a package power target, scale
each CPU cluster's initial frequency ceiling from
apple,low-power-frequency-max-mhz by the ratio of the fastest
cluster's capacity-dmips-mhz to that cluster's capacity-dmips-mhz.
Efficiency clusters then stay uncapped up to the same single-thread
performance level as the performance cluster at the ceiling, and
the power loop steps them down only when package power exceeds the
target.

apple,energy-monitor:
$ref: /schemas/types.yaml#/definitions/phandle
description:
Optional apple,acc-energy node providing hardware CPU and cluster
energy accumulators sampled alongside live GPU power every 100 ms
in the low-power profile, supplementing the slower SMC power key.

apple,gpu-busy-memory-states:
$ref: /schemas/types.yaml#/definitions/uint32-matrix
description:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
%YAML 1.2
---
$id: http://devicetree.org/schemas/soc/apple/apple,acc-energy.yaml#
$schema: http://devicetree.org/meta-schemas/core.yaml#

title: Apple SoC CPU/cluster hardware energy accumulators

maintainers:
- Nick <nekreshen@yandex.ru>

description: |
Memory-mapped 64-bit hardware energy accumulators in the Apple CPU complex
(per-core and cluster memory energy counters). Each reg entry covers one
16-byte accumulator pair: the primary 64-bit counter at +0x0 and the
companion SRAM 64-bit counter at +0x8, incremented in units of
apple,picojoules-per-tick. Referenced by apple,energy-monitor from a CLPC
node for 100 ms low-power package limiting.

properties:
compatible:
items:
- enum:
- apple,t8142-acc-energy
- const: apple,acc-energy

reg:
minItems: 1
maxItems: 32
description:
16-byte register windows, each containing a pair of 64-bit hardware
energy accumulators (core/cluster logic at +0x0, SRAM at +0x8).

apple,picojoules-per-tick:
$ref: /schemas/types.yaml#/definitions/uint32
minimum: 1
description:
Energy represented by one accumulator tick, in picojoules.

required:
- compatible
- reg
- apple,picojoules-per-tick

additionalProperties: false

examples:
- |
soc {
#address-cells = <2>;
#size-cells = <2>;

power-monitor@210059000 {
compatible = "apple,t8142-acc-energy", "apple,acc-energy";
reg = <0x2 0x10059000 0x0 0x10>,
<0x2 0x10e48000 0x0 0x10>,
<0x2 0x11059000 0x0 0x10>,
<0x2 0x11e48000 0x0 0x10>;
apple,picojoules-per-tick = <25>;
};
};
15 changes: 15 additions & 0 deletions Documentation/devicetree/bindings/soc/apple/apple,clpc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ properties:
The low-power platform profile tells the SMC (apple,smc) that Low Power
Mode is on, in the record that carries High Power mode.

apple,low-power-capacity-ceiling:
type: boolean
description:
When the low-power profile holds a package power target, scale each
CPU cluster's initial frequency ceiling from
apple,low-power-frequency-max-mhz by the ratio of the fastest
cluster's capacity-dmips-mhz to that cluster's capacity-dmips-mhz.

apple,energy-monitor:
$ref: /schemas/types.yaml#/definitions/phandle
description:
Optional apple,acc-energy node providing hardware CPU and cluster
energy accumulators sampled alongside live GPU power every 100 ms in
the low-power profile, supplementing the slower SMC power key.

required:
- compatible

Expand Down
19 changes: 19 additions & 0 deletions arch/arm64/boot/dts/apple/t8142-j813-j815.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,23 @@
};

&{/soc} {
cpu_energy: power-monitor@210059000 {
compatible = "apple,t8142-acc-energy", "apple,acc-energy";
reg = <0x2 0x10059000 0x0 0x10>,
<0x2 0x10159000 0x0 0x10>,
<0x2 0x10259000 0x0 0x10>,
<0x2 0x10359000 0x0 0x10>,
<0x2 0x10459000 0x0 0x10>,
<0x2 0x10559000 0x0 0x10>,
<0x2 0x10e48000 0x0 0x10>,
<0x2 0x11059000 0x0 0x10>,
<0x2 0x11159000 0x0 0x10>,
<0x2 0x11259000 0x0 0x10>,
<0x2 0x11359000 0x0 0x10>,
<0x2 0x11e48000 0x0 0x10>;
apple,picojoules-per-tick = <25>;
};

cpufreq_e: performance-controller@210e20000 {
compatible = "apple,t8142-cluster-cpufreq";
reg = <0x2 0x10e20000 0 0x1000>;
Expand Down Expand Up @@ -255,6 +272,8 @@
apple,low-power-gpu-cap;
/* Tell the SMC when the low-power platform profile is on. */
apple,low-power-smc-notice;
apple,low-power-capacity-ceiling;
apple,energy-monitor = <&cpu_energy>;
/*
* T8142 GPU busy-share memory-state ladder (shared by
* J813/J815 M5 MacBook Air): DCS 1, 2, 5, 7 and fabric
Expand Down
5 changes: 5 additions & 0 deletions drivers/gpu/drm/asahi/g17/clpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ void asahi_clpc_gpu_clear_busy(struct apple_clpc_gpu *gpu)
apple_clpc_gpu_clear_busy(gpu);
}

void asahi_clpc_gpu_set_power_mw(struct apple_clpc_gpu *gpu, unsigned int power_mw)
{
apple_clpc_gpu_set_power_mw(gpu, power_mw);
}

unsigned int asahi_clpc_gpu_power_cap(struct apple_clpc_gpu *gpu)
{
return apple_clpc_gpu_power_cap(gpu);
Expand Down
1 change: 1 addition & 0 deletions drivers/gpu/drm/asahi/g17/clpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ int asahi_clpc_gpu_get(void *dev, struct apple_clpc_gpu **out);
void asahi_clpc_gpu_set_active(struct apple_clpc_gpu *gpu, bool active);
void asahi_clpc_gpu_set_busy(struct apple_clpc_gpu *gpu, unsigned int permille);
void asahi_clpc_gpu_clear_busy(struct apple_clpc_gpu *gpu);
void asahi_clpc_gpu_set_power_mw(struct apple_clpc_gpu *gpu, unsigned int power_mw);
unsigned int asahi_clpc_gpu_power_cap(struct apple_clpc_gpu *gpu);
void asahi_clpc_gpu_put(struct apple_clpc_gpu *gpu);
#endif
7 changes: 7 additions & 0 deletions drivers/gpu/drm/asahi/g17/fw/m5/initdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2239,6 +2239,13 @@ pub(crate) const M5_STATUS_CONFIG_LINK_WORD: usize = 0x774;

pub(crate) const M5_STATUS_CONFIG_LINK_PTR: usize = 0x778;

/// Fast host power-target words in the M5 status config (inside the unshifted
/// `0x000..0x1dc` band across all M5 firmware layouts): enable flag, target in mW,
/// and policy reason word.
pub(crate) const M5_STATUS_CONFIG_POWER_TARGET_ENABLE: usize = 0x128;
pub(crate) const M5_STATUS_CONFIG_POWER_TARGET_MW: usize = 0x12c;
pub(crate) const M5_STATUS_CONFIG_POWER_TARGET_REASON: usize = 0x144;

/// Submission-history period and slot count (two u64), its generation word and its per-slot
/// bytes in the status config, at 26A428 offsets.
pub(crate) const M5_STATUS_CONFIG_HISTORY: usize = 0x73c;
Expand Down
58 changes: 58 additions & 0 deletions drivers/gpu/drm/asahi/g17/initdata/m5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,64 @@ impl InitData {
self.m5_policy = m::m5_policy(layouts);
}

/// M5 only: reads the live status-config power target word (mW).
pub(in crate::g17) fn m5_power_target(&self) -> Result<u32> {
if !self.m5 {
return Err(EINVAL);
}
let private = self.object(self.cluster)?;
let offset = self
.m5_status_config
.relocate(m::M5_STATUS_CONFIG_POWER_TARGET_MW)
.ok_or(EINVAL)?;
Ok(private
.word(self.graph.private.status_config + offset)?
.load(Ordering::Relaxed))
}

/// M5 only: publishes the fast status-config power target (mW) and policy reason.
/// Writes target and reason before re-arming the enable word, then cleans both
/// cache lines and drains with a full barrier.
pub(in crate::g17) fn publish_m5_power_target(&self, target_mw: u32, reason: u32) -> Result {
if !self.m5 {
return Err(EINVAL);
}
let private = self.object(self.cluster)?;
let base = self.graph.private.status_config;
let field = |offset| {
self.m5_status_config
.relocate(offset)
.map(|at| base + at)
.ok_or(EINVAL)
};
let enable_at = field(m::M5_STATUS_CONFIG_POWER_TARGET_ENABLE)?;
let target_at = field(m::M5_STATUS_CONFIG_POWER_TARGET_MW)?;
let reason_at = field(m::M5_STATUS_CONFIG_POWER_TARGET_REASON)?;
let enable_word = private.word(enable_at)?;
let target_word = private.word(target_at)?;
let reason_word = private.word(reason_at)?;
let target_line = private.pointer(target_at, 4)?;
let reason_line = private.pointer(reason_at, 4)?;
target_word.store(target_mw, Ordering::Relaxed);
reason_word.store(reason, Ordering::Relaxed);
fence(Ordering::Release);
enable_word.store(1, Ordering::Relaxed);
// SAFETY: both pointers lie inside the retained host mapping of the
// private cluster; cleaning their cache lines and draining publishes
// the naturally aligned stores to the firmware.
unsafe {
core::arch::asm!(
"dc cvac, {tline}",
"dc cvac, {rline}",
"dsb sy",
tline = in(reg) target_line,
rline = in(reg) reason_line,
options(nostack, preserves_flags)
)
};
Ok(())
}

/// Counts `submissions` in the firmware's submission history, in the slot of this moment.
pub(crate) fn note_m5_submission_history(&self, submissions: u8) -> Result {
if !self.m5 { return Err(EINVAL); }
Expand Down
54 changes: 46 additions & 8 deletions drivers/gpu/drm/asahi/g17/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,8 @@ struct Firmware {
hold: power::Hold,
/// M5: host GPU power budget governor (Region C's budget word).
governor: thermal::Governor,
/// M5: fast CLPC low-power target (status config `+0x12c`).
power_target: thermal::PowerTarget,
registration_power_release_pending: bool,
variant: &'static fw::Variant,
queues: runtime::Registry,
Expand Down Expand Up @@ -982,28 +984,32 @@ impl Firmware {
self.governor.note_activity(now);
}
// The low-power profile's GPU power cap (CLPC), applied while the GPU is busy.
let cap = self.port.power_cap_mw();
let busy = thermal::requested() && self.governor.recent_activity(now);
let power = if busy { self.init.m5_gpu_power_mw().unwrap_or(0) } else { 0 };
self.port.set_power_mw(power);
let raw_cap = self.port.power_cap_mw();
let active_cap = if busy { raw_cap } else { 0 };
// Prefer the fast M5 status-config power target; fall back to Region C if absent.
let cap = if self.apply_power_target(active_cap) { 0 } else { raw_cap };
// Without the budget controller (not requested, or the SoC die zone or its passive trip
// gone) only the fan assistance and the cap run (FanBoost::apply). A budget the
// controller left below the calibrated maximum (or the cap) goes back, and a failed write
// is retried.
if !thermal::enabled() {
let busy = thermal::requested() && self.governor.recent_activity(now);
let cap = if busy { cap } else { 0 };
if let Ok(current) = self.init.m5_power_budget() {
self.governor.set_maximum(current);
}
if let Some(budget) = self.governor.restore_capped(cap) {
self.publish_budget(budget);
}
let again = busy || fans || self.governor.pending();
let again = busy || fans || self.governor.pending() || self.power_target.pending();
return Some((again, thermal::CpuAdvice::Release, busy));
}
let Ok(current) = self.init.m5_power_budget() else {
return Some((false, thermal::CpuAdvice::Release, false));
return Some((self.power_target.pending(), thermal::CpuAdvice::Release, false));
};
self.governor.set_maximum(current);
let power = self.init.m5_gpu_power_mw().unwrap_or(0);
let clamped = self.init.m5_firmware_clamped().unwrap_or(false);
let (publish, again) = self.governor.tick(temp_mc, power, clamped, cap, now);
if let Some(budget) = publish {
Expand All @@ -1012,8 +1018,26 @@ impl Firmware {
let advice = self.governor.cpu_advice(power, now);
let want_fans = self.governor.fans_wanted(now);
// Keep ticking while the fans are still boosted, so the hold can expire, and while a
// budget write is owed.
Some((again || fans || self.governor.pending(), advice, want_fans))
// budget or status-config target write is owed.
Some((
again || fans || self.governor.pending() || self.power_target.pending(),
advice,
want_fans,
))
}

/// Publishes the fast M5 status-config power target when available; returns true if handled.
fn apply_power_target(&mut self, cap_mw: u32) -> bool {
let Ok(current) = self.init.m5_power_target() else {
return false;
};
self.power_target.set_maximum(current);
if let Some((target_mw, reason)) = self.power_target.apply(cap_mw) {
if self.init.publish_m5_power_target(target_mw, reason).is_err() {
self.power_target.publish_failed();
}
}
true
}

/// Publishes a budget the governor returned; a failed write stays owed for the next step.
Expand All @@ -1023,14 +1047,26 @@ impl Firmware {
}
}

/// Restore the calibrated maximum budget (shutdown).
/// Restore the calibrated maximum budget and status-config power target (shutdown).
fn governor_restore(&mut self) {
self.port.set_power_mw(0);
if let Some((target_mw, reason)) = self.power_target.restore() {
let _ = self.init.publish_m5_power_target(target_mw, reason);
}
if let Some(budget) = self.governor.restore() {
let _ = self.init.publish_m5_power_budget(budget);
}
}

fn apply_effort(&mut self, effort: u32) -> Result {
if effort == 0 {
let _ = self.apply_power_target(0);
} else {
let cap = self.port.power_cap_mw();
if cap != 0 {
let _ = self.apply_power_target(cap);
}
}
Self::apply_shared_effort(
&self.primary.state.shared,
&self.init,
Expand Down Expand Up @@ -2654,6 +2690,7 @@ impl Gpu {
effort: power::Effort::new(),
hold: power::Hold::new(),
governor: thermal::Governor::new(),
power_target: thermal::PowerTarget::new(),
registration_power_release_pending: false,
variant: shared.variant,
queues <- runtime::Registry::with_waiters(
Expand Down Expand Up @@ -2861,6 +2898,7 @@ impl Gpu {
effort: power::Effort::new(),
hold: power::Hold::new(),
governor: thermal::Governor::new(),
power_target: thermal::PowerTarget::new(),
registration_power_release_pending: false,
variant: shared.variant,
queues <- runtime::Registry::with_waiters(
Expand Down
5 changes: 5 additions & 0 deletions drivers/gpu/drm/asahi/g17/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,11 @@ impl Port {
self.effort_full.store(true, Ordering::SeqCst);
}

/// Reports live GPU power draw in mW to the CLPC low-power loop.
pub(super) fn set_power_mw(&self, power_mw: u32) {
self.demand.set_power_mw(power_mw);
}

/// The power in mW the low-power profile allows the GPU's power budget, 0 for no limit.
pub(super) fn power_cap_mw(&self) -> u32 {
self.demand.power_cap_mw()
Expand Down
6 changes: 6 additions & 0 deletions drivers/gpu/drm/asahi/g17/power.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,7 @@ unsafe extern "C" {
fn asahi_clpc_gpu_put(gpu: *mut core::ffi::c_void);
fn asahi_clpc_gpu_set_busy(gpu: *mut core::ffi::c_void, permille: core::ffi::c_uint);
fn asahi_clpc_gpu_clear_busy(gpu: *mut core::ffi::c_void);
fn asahi_clpc_gpu_set_power_mw(gpu: *mut core::ffi::c_void, power_mw: core::ffi::c_uint);
fn asahi_clpc_gpu_power_cap(gpu: *mut core::ffi::c_void) -> core::ffi::c_uint;
}

Expand Down Expand Up @@ -1216,6 +1217,11 @@ impl Demand {
unsafe { asahi_clpc_gpu_clear_busy(self.0) };
}

pub(super) fn set_power_mw(&self, power_mw: u32) {
// SAFETY: the retained reference is live; NULL denotes no DT provider.
unsafe { asahi_clpc_gpu_set_power_mw(self.0, power_mw as core::ffi::c_uint) };
}

/// The power in mW the low-power profile allows the GPU's own budget, 0 for no limit
/// (and without a provider).
pub(super) fn power_cap_mw(&self) -> u32 {
Expand Down
Loading