Skip to content
56 changes: 32 additions & 24 deletions drivers/gpu/drm/asahi/g16/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! holds the PMP AGX vote (25G83; 26B123 sends none), prepares owned objects and publishes
//! context0 while ASC RUN is clear.
//! It negotiates RTKit protocol12, accepts only the inherited EP1 crash buffer, then submits
//! initdata plus the consume-only NOP and policy (re-import Globals, start; 0x1a, 0x34 and
//! initdata plus the consume-only NOP and policy (re-import Globals, idle power-off; 0x1a, 0x34 and
//! 0x0a on 25G83, 0x19, 0x35 and 0x0a on 26B123). Control+0x14 and exact retired indices
//! qualify READY; no M3 firmware constants or device-control sequence are reused.
//! Firmware-reachable backing and the RTKit cookie remain retained until reboot.
Expand Down Expand Up @@ -485,7 +485,7 @@ pub(crate) struct Session {
/// The PPL handoff page, mapped here only to read the firmware's powered flag
/// ([`hw::FwLayout::powered`]); `None` when the firmware keeps the GPU powered.
handoff: Option<regs::Mapping>,
/// True once cold boot (`START`) has completed and the firmware may idle-power-off the GPU.
/// True once cold boot has completed and runtime device-control writes need the power guard.
booted: bool,
}

Expand Down Expand Up @@ -538,7 +538,8 @@ impl Session {

/// Whether the boot power policy sent in [`run`] lets the firmware power the GPU off on its
/// own while idle. False while the firmware holds the GPU powered, so it cannot cycle power
/// between commands.
/// between commands. A retained genpd vote keeps the target active; automatic mode, where
/// PMGR has it enabled, lets the firmware change the actual state behind that vote.
pub(crate) fn idle_power_off(&self) -> bool {
self.adt.layout.powered.is_some()
}
Expand Down Expand Up @@ -714,17 +715,6 @@ pub(crate) fn admit(
Ok(KBox::init(init!(Admitted { regs, adt }), GFP_KERNEL)?)
}

/// Posts one device control with only the opcode and waits for it.
fn devctl_op(
session: &mut Session,
channels: &mut Channels,
events: &mut Events,
init: &InitData,
opcode: u32,
) -> Result {
channel::devctl_submit(session, channels, events, init, &DevCtl::op(opcode), true)
}

/// Wait for one boot control to retire, requiring stable consumer+completion indices.
fn wait_boot_control(
session: &Session,
Expand Down Expand Up @@ -889,13 +879,20 @@ fn run(
let base = 1;
let scheduler = init.control.as_ref().ok_or(EINVAL)?;
let scheduler_word = init.firmware_data().scheduler;
devctl_op(session, channels, events, init, devctl.import_globals)?;
channel::devctl_submit(
session,
channels,
events,
init,
&DevCtl::op(devctl.import_globals),
true,
)?;
wait_boot_control(session, channels, events, init, base + 1, 5000)?;
let pending = scheduler.r32(scheduler_word)?;
if kind == hw::Start::MacOs27_2 {
// The InitData request (1) can still be pending here on 26B123: the firmware consumes it
// when it powers the GPU up or recovers, and the boot records need not power it up. Post
// START with it pending; never stack a second request.
// the idle policy with it pending; never stack a second request.
if pending > 1 {
channels.log_stop(
session,
Expand All @@ -914,14 +911,24 @@ fn run(
}
scheduler.w32(scheduler_word, 1)?;
}
// Bootstrap needs the GPU awake while the host publishes its channel indices.
// Release that hold only where the runtime guards powered-register accesses.
// Keep the backing policy in sync so a firmware re-import preserves idle gating.
// Held compute UMA managers follow the same policy (`Firmware::note_gpu_idle_if_any`).
let allow_idle = session.idle_power_off();
init.globals.as_ref().ok_or(ENODEV)?.w32(
super::fw::initdata::GLOBALS_IDLE_POWER_OFF,
u32::from(allow_idle),
)?;
regs::wmb();
let mut policy = DevCtl::op(devctl.start);
if session.idle_power_off() {
// Let the firmware manage idle at the normal boot policy boundary (Globals +0x30 = 1).
// Held compute UMA managers follow the same policy (`Firmware::note_gpu_idle_if_any`).
policy.words[1] = 1;
}
channel::devctl_submit(session, channels, events, init, &policy, true)?;
channel::devctl_submit(
session,
channels,
events,
init,
&DevCtl::idle_power_off(allow_idle),
true,
)?;
wait_boot_control(session, channels, events, init, base + 2, 2000)?;
let receive_kick = if g15s_channels {
hw::engine::G15S_RECEIVE_DOORBELL
Expand All @@ -942,7 +949,7 @@ fn run(
|| channels.devctl.write()? != base + 2
|| (kind == hw::Start::Wake25G83 && scheduler.r32(scheduler_word)? != 0)
{
channels.log_stop(session, init, "boot controls not settled after START");
channels.log_stop(session, init, "boot controls not settled after idle policy");
return Err(EBUSY);
}
dev_info!(
Expand Down Expand Up @@ -1124,6 +1131,7 @@ pub(crate) fn start(
cfg,
&session.regs,
id.cores.count_ones(),
session.idle_power_off(),
&mut objects,
)?);
if let Some((init, _, _)) = built.as_ref() {
Expand Down
16 changes: 10 additions & 6 deletions drivers/gpu/drm/asahi/g16/fw/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ impl SubmitRecord {
pub(crate) mod devctl {
/// Re-import Globals.
pub(crate) const IMPORT_GLOBALS: u32 = 0x34;
/// Start.
pub(crate) const START: u32 = 0x0a;
/// Set the firmware idle power-off policy.
pub(crate) const IDLE_POWER_OFF: u32 = 0x0a;
/// Initial no-op, staged before the initdata doorbell.
pub(crate) const NOP: u32 = 0x1a;
/// PB growth reply.
Expand All @@ -64,23 +64,20 @@ pub(crate) mod devctl {
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Opcodes {
pub(crate) import_globals: u32,
pub(crate) start: u32,
pub(crate) nop: u32,
pub(crate) grow_reply: u32,
}

pub(crate) const V25G83: Opcodes = Opcodes {
import_globals: IMPORT_GLOBALS,
start: START,
nop: NOP,
grow_reply: GROW_REPLY,
};

/// 26B123 device-control opcodes (re-import Globals 0x35, start 0x0a, no-op 0x19,
/// 26B123 device-control opcodes (re-import Globals 0x35, no-op 0x19,
/// PB growth reply 7).
pub(crate) const V26B123: Opcodes = Opcodes {
import_globals: 0x35,
start: 0x0a,
nop: 0x19,
grow_reply: 7,
};
Expand All @@ -102,6 +99,13 @@ impl DevCtl {
Self { words }
}

/// A zero argument wakes the GPU and holds it on. One permits firmware-managed idle.
pub(crate) fn idle_power_off(allow: bool) -> Self {
let mut record = Self::op(devctl::IDLE_POWER_OFF);
record.words[1] = u32::from(allow);
record
}

/// The opcode.
pub(crate) const fn opcode(&self) -> u32 {
self.words[0]
Expand Down
21 changes: 19 additions & 2 deletions drivers/gpu/drm/asahi/g16/fw/initdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub(crate) const OBJECT_COUNT: usize = 31;
pub(crate) const LOADER_COUNT: usize = 42;
pub(crate) const LOADER_STRIDE: usize = 0x28;

/// Globals word imported by firmware at boot and recovery. Zero holds the GPU awake.
pub(crate) const GLOBALS_IDLE_POWER_OFF: usize = 0x30;

/// Stable positions in the firmware's object inventory.
pub(crate) mod id {
pub(crate) const ROOT: usize = 0;
Expand Down Expand Up @@ -559,6 +562,20 @@ const fn runs_fit(runs: &[Run], size: usize) -> bool {
true
}

// 25G83 offsets; later layouts move both delay words together.
const CORE_IDLE_OFF_DELAY: usize = 0x9b8;
const FENDER_IDLE_OFF_DELAY: usize = 0x9bc;
const SHORT_IDLE_MS: u32 = 2;
const LONG_IDLE_MS: u32 = 40;

/// The core and Fender off timers run in sequence. Use the longer interval
/// before core power-off so a short gap between submissions can resume at once.
/// The complete domain still falls idle after the same total interval.
pub(crate) fn set_runtime_idle_delays(layout: &Layout, globals: &mut [u8]) -> Result<(), ()> {
put32(globals, remap(layout.globals, CORE_IDLE_OFF_DELAY, 4)?, LONG_IDLE_MS)?;
put32(globals, remap(layout.globals, FENDER_IDLE_OFF_DELAY, 4)?, SHORT_IDLE_MS)
}

/// Fixed 25G83 GLOBALS words remapped into every ABI; shared by `encode_for` and `layout_fits`.
const GLOBALS_FIXED: &[(usize, u32)] = &[
(0x78, 1),
Expand All @@ -567,8 +584,8 @@ const GLOBALS_FIXED: &[(usize, u32)] = &[
(0x998, 40),
(0x99c, 10),
(0x9a0, 250),
(0x9b8, 2),
(0x9bc, 40),
(CORE_IDLE_OFF_DELAY, SHORT_IDLE_MS),
(FENDER_IDLE_OFF_DELAY, LONG_IDLE_MS),
(0x9c0, 5),
// Firmware DM2 (compute) progress-watchdog thresholds (~4 ms/tick):
// - 0x9c8 (26B123 0x9cc): secondary DM2 soft-preempt watchdog, raised from 40 (~0.16 s)
Expand Down
23 changes: 22 additions & 1 deletion drivers/gpu/drm/asahi/g16/glue.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ struct asahi_g16_cooling {
long zone_critical_mc;
bool zone_limits_valid;
int zone_err;
bool startup_sampling;
};

struct asahi_g16_cooling *asahi_g16_cooling_register(struct device *dev);
Expand Down Expand Up @@ -332,6 +333,19 @@ bool asahi_g16_cooling_has_pmp(const struct asahi_g16_cooling *cooling)
return cooling->pmp_node != NULL;
}

/* Set before the GPU owner publishes its state. */
bool asahi_g16_cooling_arm_startup(struct asahi_g16_cooling *cooling)
{
cooling->startup_sampling = !!cooling->pmp_node;
return cooling->startup_sampling;
}

/* Called under the GPU owner lock. */
unsigned int asahi_g16_cooling_startup_poll_ms(const struct asahi_g16_cooling *cooling)
{
return cooling->startup_sampling ? ASAHI_G16_ZONE_POLL_FAST_MS : 0;
}

/* Serialized by the GPU owner. Provider lifetime and MMIO access stay with
* PMP: this consumer owns no SRAM mapping and cannot restart its firmware.
* An idle GPU samples about twice a second and makes no heat, so with nothing
Expand Down Expand Up @@ -396,7 +410,7 @@ int asahi_g16_cooling_sample(struct asahi_g16_cooling *cooling, u64 now, bool id
if (!ret) {
cooling->valid_ns = now;
cooling->valid_mc = *temperature;
} else if (ret == -ENODATA &&
} else if (ret == -ENODATA && !(idle && cooling->startup_sampling) &&
apple_pmp_hold_nodata(cooling->valid_ns, cooling->valid_mc,
now, idle)) {
*temperature = cooling->valid_mc;
Expand All @@ -410,6 +424,13 @@ int asahi_g16_cooling_sample(struct asahi_g16_cooling *cooling, u64 now, bool id
*power = apple_pmp_gpu_power_state(cooling->pmp);
apple_pmp_cooling_observe(&cooling->policy, now, *governor);
*backstop = apple_pmp_cooling_level(&cooling->policy);
/*
* Only live, cool PMP reads can advance the extra idle observations.
* Normal work drives later policy changes.
*/
if (cooling->startup_sampling &&
!apple_pmp_cooling_startup_pending(&cooling->policy, *temperature, ret, *governor))
cooling->startup_sampling = false;
return ret;
}

Expand Down
2 changes: 2 additions & 0 deletions drivers/gpu/drm/asahi/g16/glue.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ struct device;

u32 asahi_g16_timebase_hz(void);
bool asahi_g16_cooling_has_pmp(const struct asahi_g16_cooling *cooling);
bool asahi_g16_cooling_arm_startup(struct asahi_g16_cooling *cooling);
unsigned int asahi_g16_cooling_startup_poll_ms(const struct asahi_g16_cooling *cooling);
int asahi_g16_display_prepare(struct device *dev, const char *display_compat);

#endif /* _ASAHI_G16_GLUE_H */
4 changes: 4 additions & 0 deletions drivers/gpu/drm/asahi/g16/initdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pub(crate) fn build(
cfg: &super::Config,
regs: &super::regs::Regs,
enabled_cores: u32,
allow_idle: bool,
objects: &mut Objects,
) -> Result<(InitData, Channels, u32)> {
let abi = adt.layout.abi;
Expand Down Expand Up @@ -308,6 +309,9 @@ pub(crate) fn build(
.ok_or(EINVAL)?,
GFP_KERNEL,
)?;
if allow_idle {
wire::set_runtime_idle_delays(layout, &mut images[id::GLOBALS]).map_err(|()| EINVAL)?;
}
// Host startup policy: Main and PowerPerformance contain the top row, but the
// published selectors remain S1 until the thermal owner admits a raise.
for at in layout.pstate_selectors {
Expand Down
6 changes: 5 additions & 1 deletion drivers/gpu/drm/asahi/g16/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,9 @@ impl Gpu {
res: shared_regs::G16Resources,
) -> Result<Arc<Gpu>> {
// Admission (probe-time checks), then the firmware start to READY.
let power = power::Power::new(dev.as_ref().as_ref())?;
let mut power = power::Power::new(dev.as_ref().as_ref())?;
let admitted = boot::admit(pdev, dev, cfg, res)?;
let sample_at_startup = admitted.adt.layout.powered.is_some() && power.arm_startup_sampling();
let data = admitted.adt.data.clone();
// pin_init exposes initialized fields by name, so `dev` is shadowed after
// the platform-device field. Retain the typed DRM device before that point.
Expand Down Expand Up @@ -757,6 +758,9 @@ impl Gpu {
GFP_KERNEL,
)?;
gpu.retain()?;
if sample_at_startup {
gpu.shared.poll_now();
}
if cfg.deep_sleep {
if let Err(error) = sleep::register_pm_notify(&gpu.shared) {
dev_warn!(dev.as_ref(), "G16: no sleep PM notifier ({:?})\n", error);
Expand Down
6 changes: 5 additions & 1 deletion drivers/gpu/drm/asahi/g16/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ impl Shared {
} else if firmware.power.utilization.decaying() {
super::power::policy::WINDOW_NS
} else {
return;
let ms = firmware.power.startup_poll_ms();
if ms == 0 {
return;
}
u64::from(ms) * 1_000_000
};
self.mod_poll(kernel::time::msecs_to_jiffies((delay_ns / 1_000_000) as u32).max(1));
}
Expand Down
13 changes: 13 additions & 0 deletions drivers/gpu/drm/asahi/g16/power.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ extern "C" {
fn asahi_g16_clpc_gpu_set_busy(gpu: *mut c_void, permille: core::ffi::c_uint);
fn asahi_g16_clpc_gpu_clear_busy(gpu: *mut c_void);
fn asahi_g16_clpc_gpu_put(gpu: *mut c_void);
fn asahi_g16_cooling_arm_startup(cooling: *mut core::ffi::c_void) -> bool;
fn asahi_g16_cooling_startup_poll_ms(cooling: *const core::ffi::c_void) -> u32;
fn asahi_g16_cooling_register(dev: *mut c_void) -> *mut c_void;
fn asahi_g16_cooling_unregister(cooling: *mut c_void);
fn asahi_g16_cooling_has_pmp(cooling: *const c_void) -> bool;
Expand Down Expand Up @@ -332,6 +334,17 @@ impl Power {
self.clpc.clear_busy();
}

/// Arm the PMP startup check before autonomous GPU idle can stop its notifications.
pub(crate) fn arm_startup_sampling(&mut self) -> bool {
// SAFETY: the cooling object is live and the firmware state is still unpublished.
unsafe { asahi_g16_cooling_arm_startup(self.cooling.inner.as_ptr()) }
}

pub(crate) fn startup_poll_ms(&self) -> u32 {
// SAFETY: Cooling owns this allocation and the device mutex serializes the policy.
unsafe { asahi_g16_cooling_startup_poll_ms(self.cooling.inner.as_ptr()) }
}

/// Whether the CLPC floor is held across an idle gap awaiting expiry.
pub(crate) fn clpc_holding(&self) -> bool {
self.clpc_hold.holding()
Expand Down
20 changes: 20 additions & 0 deletions drivers/soc/apple/display-gate.c
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,21 @@ static int __init gate_gpu_pmp_link(struct of_changeset *cs, struct device_node
ret = -EINVAL;
else if (!linked)
ret = of_changeset_add_prop_u32(cs, gpu, "apple,pmp", pmp->phandle);

/*
* The G16 boot handoff removes the PMP link and forces GFX on for
* cold start. When this changeset supplies the link, let PMGR
* follow the running firmware's idle requests. Keep the policy of
* a tree that already provided its own GPU supplier.
*/
if (!ret && !linked && of_property_present(gpu, "apple,g16-handoff-version")) {
struct device_node *domain = of_parse_phandle(gpu, "power-domains", 0);
struct property *hold = of_find_property(domain, "apple,no-auto-pm", NULL);

if (hold)
ret = of_changeset_remove_property(cs, domain, hold);
of_node_put(domain);
}
of_node_put(linked);
out:
of_node_put(gpu);
Expand All @@ -949,8 +964,13 @@ static int __init gate_gpu_auto_pm(struct of_changeset *cs, struct device_node *
{
struct device_node *domain = of_parse_phandle(gpu, "power-domains", 0);
struct property *prop = domain ? of_find_property(domain, "apple,no-auto-pm", NULL) : NULL;
struct of_changeset_entry *ce;
int ret = 0;

/* gate_gpu_pmp_link() may have dropped it with the link: a second removal would fail. */
list_for_each_entry(ce, &cs->entries, node)
if (ce->action == OF_RECONFIG_REMOVE_PROPERTY && ce->prop == prop)
prop = NULL;
if (prop)
ret = of_changeset_remove_property(cs, domain, prop);
of_node_put(domain);
Expand Down
7 changes: 7 additions & 0 deletions include/linux/soc/apple/pmp-cooling.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ static inline unsigned int apple_pmp_cooling_level(const struct apple_pmp_coolin
return s->level;
}

/* Whether another valid idle observation can relax the startup backstop. */
static inline bool apple_pmp_cooling_startup_pending(const struct apple_pmp_cooling *s,
long mc, int error, unsigned int governor)
{
return !error && mc < APPLE_PMP_RELEASE_MC && (s->hard || s->level > governor);
}

/* Sample at the fast cadence whenever a decision could depend on it. */
static inline bool apple_pmp_cooling_fast(const struct apple_pmp_cooling *s,
long mc, int error)
Expand Down
Loading
Loading