From 348a490b12c1cf94f912ef7991ee741b89d4efe9 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Thu, 19 Nov 2026 23:17:28 +1100 Subject: [PATCH 1/6] dt-bindings: gpu: apple,agx: describe the GPU power-ceiling cooling device The GPU firmware takes an absolute power ceiling from the host. Allow #cooling-cells on the GPU node so thermal zones can map cooling states onto it: state N requests 100 - N percent of the initialized maximum power, bounded below at 25 percent, and state 0 leaves the firmware's own policy unchanged. Signed-off-by: Ryan Murray --- Documentation/devicetree/bindings/gpu/apple,agx.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Documentation/devicetree/bindings/gpu/apple,agx.yaml b/Documentation/devicetree/bindings/gpu/apple,agx.yaml index 7794c0e13b939..7195523c1bd79 100644 --- a/Documentation/devicetree/bindings/gpu/apple,agx.yaml +++ b/Documentation/devicetree/bindings/gpu/apple,agx.yaml @@ -136,6 +136,16 @@ properties: $ref: /schemas/types.yaml#/definitions/phandle description: PMP firmware provider owning the GPU power vote and telemetry + "#cooling-cells": + const: 2 + description: | + The GPU as a thermal cooling device: an absolute power ceiling the GPU + firmware applies. Cooling state N, for N in 0..100, requests (100 - N) + percent of the GPU's initialized maximum power, bounded below at 25 + percent. State 0 leaves the firmware's own policy unchanged. Where the + running firmware's power inputs are unknown to the driver, a GPU with + this property does not start. + operating-points-v2: true apple,csafr-min-sram-microvolt: From ff3079470a9e87b224284a42ae97a5ced960369e Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Thu, 19 Nov 2026 23:17:39 +1100 Subject: [PATCH 2/6] drm/asahi: add a GPU power-ceiling cooling device The G17-family firmware takes two absolute power inputs from the host, the maximum GPU power in Region C and the power-management budget in the primary power state. Both are written once at initialization, so nothing on the host can lower the GPU's power when a board runs hot. Add a cooling device that owns those words. Cooling state N requests 100 - N percent of the initialized maximum, never less than 25 percent. Thermal, fast-die, system and manual requesters compose by minimum, so none can relax another's ceiling. The device starts unrestricted: state 0 writes back the initialized values, and a GPU that no thermal zone references keeps the firmware's own policy. Recovery republishes the committed ceiling before queues reopen. The policy is plain C shared by every firmware build that describes the words; the build-specific part is only where the words live. It is registered when the GPU's device-tree node has #cooling-cells, with no module parameter. A read-only gpu_power_ceiling attribute and a debugfs file report the requested and written ceilings. KUnit and a host build run the same mapping, composition, failure and recovery cases. Signed-off-by: Ryan Murray --- .../testing/sysfs-driver-aurora-gpu-cooling | 18 ++ Documentation/gpu/asahi-gpu-thermal.rst | 63 +++++ Documentation/gpu/drivers.rst | 1 + drivers/gpu/drm/Makefile | 1 + drivers/gpu/drm/asahi/.kunitconfig.cooling | 3 + drivers/gpu/drm/asahi/Kconfig | 12 + drivers/gpu/drm/asahi/Makefile | 2 +- drivers/gpu/drm/asahi/cooling.c | 219 ++++++++++++++++++ drivers/gpu/drm/asahi/cooling.h | 23 ++ drivers/gpu/drm/asahi/cooling_policy.h | 137 +++++++++++ drivers/gpu/drm/asahi/cooling_test.c | 205 ++++++++++++++++ .../testing/selftests/drivers/apple/Makefile | 3 +- .../selftests/drivers/apple/gpu-cooling.py | 36 +++ 13 files changed, 721 insertions(+), 2 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-driver-aurora-gpu-cooling create mode 100644 Documentation/gpu/asahi-gpu-thermal.rst create mode 100644 drivers/gpu/drm/asahi/.kunitconfig.cooling create mode 100644 drivers/gpu/drm/asahi/cooling.c create mode 100644 drivers/gpu/drm/asahi/cooling.h create mode 100644 drivers/gpu/drm/asahi/cooling_policy.h create mode 100644 drivers/gpu/drm/asahi/cooling_test.c create mode 100644 tools/testing/selftests/drivers/apple/gpu-cooling.py diff --git a/Documentation/ABI/testing/sysfs-driver-aurora-gpu-cooling b/Documentation/ABI/testing/sysfs-driver-aurora-gpu-cooling new file mode 100644 index 0000000000000..37ccb69e059d8 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-driver-aurora-gpu-cooling @@ -0,0 +1,18 @@ +What: /sys/devices/platform/.../gpu_power_ceiling +Date: November 2026 +Contact: Asahi Linux contributors +Description: + Read-only. Present when the GPU's device-tree node is a + cooling device (#cooling-cells). One key/value pair per line: + + requested_state: thermal cooling state, 0..100. + requested_ceiling_mw: minimum of all requester ceilings. + applied_ceiling_mw: ceiling last written to the firmware. + provisional_floor_mw: lowest ceiling, 25 percent of the + initialized maximum (never zero). + achieved_ceiling_mw: always "unsupported"; the firmware does + not acknowledge the ceiling. + + State N requests max(25, 100 - N) percent of the initialized + maximum power. The debugfs file asahi--power/ceiling + adds the read-back firmware words and each requester's ceiling. diff --git a/Documentation/gpu/asahi-gpu-thermal.rst b/Documentation/gpu/asahi-gpu-thermal.rst new file mode 100644 index 0000000000000..2b75de8377b86 --- /dev/null +++ b/Documentation/gpu/asahi-gpu-thermal.rst @@ -0,0 +1,63 @@ +.. SPDX-License-Identifier: GPL-2.0-only OR MIT + +===================================== +Apple GPU power ceiling and thermals +===================================== + +The GPU firmware takes two absolute power inputs from the host: the maximum +GPU power in Region C and the power-management budget in the primary power +state. Both are written once at initialization. The driver turns them into +a thermal cooling device, so device-tree thermal zones can lower the GPU's +power the same way they lower CPU frequencies. + +Device tree +=========== + +A GPU node with ``#cooling-cells = <2>`` is a cooling device. Cooling state +``N`` (0..100) requests ``100 - N`` percent of the initialized maximum power, +never less than 25 percent. State 0 republishes the initialized values, so a +GPU that no thermal zone references keeps the firmware's own policy. The +policy itself (sensors, trips, the states each trip asks for) belongs to the +board's thermal zones. The GPU is not a power actor; under the +``power_allocator`` governor its zones are ladders of ``active`` trips, which +the governor runs bang-bang, as it runs fans. + +The firmware keeps its own die-temperature limiter, so the host ceiling is +an outer loop, not the overtemperature protection. + +Firmware builds +=============== + +The driver writes the power inputs only where the running build's layout is +described (``Variant::live_power`` in ``g17/fw/variant.rs``): the A18 Pro +(26A428) and M4 Pro (25G83) builds. A GPU the device tree makes a cooling +device does not start on any other build, rather than run with the thermal +zones silently ignored. The M5 firmware takes its host budget at a different +Region C offset, which its own governor (``g17/thermal.rs``) writes; moving +that governor onto this cooling device is separate work. + +The requesters (thermal, firmware fast-die, system, manual) compose by +minimum: one cannot relax another's ceiling. Recovery republishes the +committed ceiling before queues reopen. + +Diagnostics +=========== + +``/sys/class/thermal/cooling_device*/`` with type ``gpu-power`` shows the +requested state. The GPU device's read-only ``gpu_power_ceiling`` attribute +and ``/sys/kernel/debug/asahi--power/ceiling`` report the requested +and published ceilings and the read-back words. A published ceiling is what +the host wrote; the firmware does not acknowledge it, so measured GPU power +and clocks are the evidence that it took effect. + +Tests +===== + +``drivers/gpu/drm/asahi/.kunitconfig.cooling`` runs the mapping, composition, +failure and recovery cases under KUnit:: + + ./tools/testing/kunit/kunit.py run --arch=arm64 \ + --kunitconfig=drivers/gpu/drm/asahi/.kunitconfig.cooling + +``tools/testing/selftests/drivers/apple/gpu-cooling.py`` runs the same cases +and the typed power-state layout checks on the build host. diff --git a/Documentation/gpu/drivers.rst b/Documentation/gpu/drivers.rst index 38f99ddda7c83..d2a9ec8b75863 100644 --- a/Documentation/gpu/drivers.rst +++ b/Documentation/gpu/drivers.rst @@ -12,6 +12,7 @@ GPU Driver Documentation asahi-reclaim asahi-usage-stats asahi-m4-bringup + asahi-gpu-thermal apple-dcp-m4 i915 imagination/index diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile index cd966b1b5b47e..101756c8f632e 100644 --- a/drivers/gpu/drm/Makefile +++ b/drivers/gpu/drm/Makefile @@ -221,6 +221,7 @@ obj-$(CONFIG_DRM_PL111) += pl111/ obj-$(CONFIG_DRM_TVE200) += tve200/ obj-$(CONFIG_DRM_ADP) += adp/ obj-$(CONFIG_DRM_ASAHI) += asahi/ +obj-$(CONFIG_DRM_ASAHI_COOLING_KUNIT_TEST) += asahi/cooling_test.o obj-$(CONFIG_DRM_XEN) += xen/ obj-$(CONFIG_DRM_VBOXVIDEO) += vboxvideo/ obj-$(CONFIG_DRM_LIMA) += lima/ diff --git a/drivers/gpu/drm/asahi/.kunitconfig.cooling b/drivers/gpu/drm/asahi/.kunitconfig.cooling new file mode 100644 index 0000000000000..161a3a371082f --- /dev/null +++ b/drivers/gpu/drm/asahi/.kunitconfig.cooling @@ -0,0 +1,3 @@ +CONFIG_KUNIT=y +CONFIG_DRM=y +CONFIG_DRM_ASAHI_COOLING_KUNIT_TEST=y diff --git a/drivers/gpu/drm/asahi/Kconfig b/drivers/gpu/drm/asahi/Kconfig index d383802ee8825..91f9c26ba03ce 100644 --- a/drivers/gpu/drm/asahi/Kconfig +++ b/drivers/gpu/drm/asahi/Kconfig @@ -16,6 +16,8 @@ config DRM_ASAHI depends on MMU depends on IOMMU_SUPPORT depends on PAGE_SIZE_16KB + select THERMAL + select THERMAL_OF if OF select APPLE_PMP_REPORT select RUST_DRM_SCHED select RUST_DRM_GEM_SHMEM_HELPER @@ -111,3 +113,13 @@ config DRM_ASAHI_G15 The native display driver must be built in, or both drivers must be modules, so a modular display provider finishes initialization before the GPU module can probe through its direct helper import. + +config DRM_ASAHI_COOLING_KUNIT_TEST + tristate "Aurora GPU power-cap policy tests" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + Exercise absolute-power mapping, composition and failed publication + without a GPU or Rust toolchain. The same production mapping and + fake actuator cases also run on a host compiler. + These tests do not qualify firmware layouts or thermal response. diff --git a/drivers/gpu/drm/asahi/Makefile b/drivers/gpu/drm/asahi/Makefile index 890d47c40af6b..45aff609e2432 100644 --- a/drivers/gpu/drm/asahi/Makefile +++ b/drivers/gpu/drm/asahi/Makefile @@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_DRM_ASAHI) += aurora.o -aurora-y := asahi_core.o fwid.o legacy_sched.o sysfs.o g17/sync_input.o g17/sync_output.o g17/sync_scope.o g17/trace.o g17/clpc.o g17/fans.o g17/sleep.o g17/pmpdash.o +aurora-y := asahi_core.o fwid.o legacy_sched.o sysfs.o cooling.o g17/sync_input.o g17/sync_output.o g17/sync_scope.o g17/trace.o g17/clpc.o g17/fans.o g17/sleep.o g17/pmpdash.o CFLAGS_g17/trace.o := -I$(src)/g17 aurora-$(CONFIG_DRM_ASAHI_G15) += g15/glue.o diff --git a/drivers/gpu/drm/asahi/cooling.c b/drivers/gpu/drm/asahi/cooling.c new file mode 100644 index 0000000000000..ac1c0a449fe10 --- /dev/null +++ b/drivers/gpu/drm/asahi/cooling.c @@ -0,0 +1,219 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cooling.h" + +struct asahi_gpu_cooling { + struct device *dev; + struct mutex lock; + struct asahi_gpu_cap cap; + struct asahi_gpu_power_words words; + u32 ppm_budget_mw; + struct thermal_cooling_device *cdev; + struct device_attribute telemetry; + struct dentry *debugfs; +}; + +static void clean_word(u32 *word) +{ + dcache_clean_poc((unsigned long)word, (unsigned long)(word + 1)); +} + +/* No mailbox or ACK exists. All pointers are checked before registration. + * The cooling mutex owns every ceiling requester and both power publications; + * it never takes the GPU mutex or waits for GPU work. RegionC is write-back, + * so a WC store barrier alone would not publish this word to firmware. + */ +static int asahi_gpu_publish_cap(void *context, unsigned int mw) +{ + struct asahi_gpu_cooling *cooling = context; + struct asahi_gpu_power_words *words = &cooling->words; + u32 ppm = asahi_gpu_cap_ppm(mw, cooling->cap.max_mw, cooling->ppm_budget_mw); + + lockdep_assert_held(&cooling->lock); + /* Independent absolute-power inputs: the Region C maximum and the + * power-management budget. Neither word is a percent or an effort floor. + */ + WRITE_ONCE(*words->ppm_target_mw, ppm); + WRITE_ONCE(*words->ppm_enabled, 1); + WRITE_ONCE(*words->max_power_mw, mw); + clean_word(words->max_power_mw); + wmb(); + return 0; +} + +int asahi_gpu_cooling_request(struct asahi_gpu_cooling *cooling, + enum asahi_gpu_cap_source source, u32 mw) +{ + /* Thermal requests also need a state; only the cooling callback sets it. */ + if (source <= ASAHI_GPU_CAP_THERMAL || source >= ASAHI_GPU_CAP_SOURCES) + return -EINVAL; + guard(mutex)(&cooling->lock); + if (mw < cooling->cap.floor_mw || mw > cooling->cap.max_mw) + return -ERANGE; + return asahi_gpu_cap_request(&cooling->cap, source, mw, + asahi_gpu_publish_cap, cooling); +} + +static int asahi_gpu_cooling_max(struct thermal_cooling_device *cdev, + unsigned long *state) +{ + *state = ASAHI_GPU_COOLING_MAX; + return 0; +} + +static int asahi_gpu_cooling_get(struct thermal_cooling_device *cdev, + unsigned long *state) +{ + struct asahi_gpu_cooling *cooling = cdev->devdata; + + guard(mutex)(&cooling->lock); + *state = cooling->cap.requested_state; + return 0; +} + +static int asahi_gpu_cooling_set(struct thermal_cooling_device *cdev, + unsigned long state) +{ + struct asahi_gpu_cooling *cooling = cdev->devdata; + + if (asahi_gpu_cap_percent(state) < 0) + return -EINVAL; + guard(mutex)(&cooling->lock); + return asahi_gpu_cap_update(&cooling->cap, state, asahi_gpu_publish_cap, cooling); +} + +static const struct thermal_cooling_device_ops asahi_gpu_cooling_ops = { + .get_max_state = asahi_gpu_cooling_max, + .get_cur_state = asahi_gpu_cooling_get, + .set_cur_state = asahi_gpu_cooling_set, +}; + +static int ceiling_show(struct seq_file *m, void *unused) +{ + struct asahi_gpu_cooling *cooling = m->private; + unsigned int i; + + guard(mutex)(&cooling->lock); + seq_printf(m, "requested_state %u\nrequested_ceiling_mw %u\napplied_ceiling_mw %u\n" + "region_c_readback_mw %u\nppm_target_readback_mw %u\nppm_enabled %u\n" + "initialized_ppm_budget_mw %u\n" + "initialized_max_mw %u\nprovisional_floor_mw %u\n" + "achieved_ceiling_mw unsupported\n", + cooling->cap.requested_state, asahi_gpu_cap_effective(&cooling->cap), + cooling->cap.applied_mw, READ_ONCE(*cooling->words.max_power_mw), + READ_ONCE(*cooling->words.ppm_target_mw), + READ_ONCE(*cooling->words.ppm_enabled), cooling->ppm_budget_mw, + cooling->cap.max_mw, + cooling->cap.floor_mw); + for (i = 0; i < ASAHI_GPU_CAP_SOURCES; i++) + seq_printf(m, "request_%u_mw %u\n", i, cooling->cap.requests[i]); + return 0; +} +DEFINE_SHOW_ATTRIBUTE(ceiling); + +static ssize_t gpu_power_ceiling_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct asahi_gpu_cooling *cooling = + container_of(attr, struct asahi_gpu_cooling, telemetry); + + guard(mutex)(&cooling->lock); + return sysfs_emit(buf, "requested_state %u\nrequested_ceiling_mw %u\n" + "applied_ceiling_mw %u\nprovisional_floor_mw %u\n" + "achieved_ceiling_mw unsupported\n", + cooling->cap.requested_state, + asahi_gpu_cap_effective(&cooling->cap), cooling->cap.applied_mw, + cooling->cap.floor_mw); +} + +struct asahi_gpu_cooling *asahi_gpu_cooling_register(struct device *dev, + const struct asahi_gpu_power_words *words) +{ + struct asahi_gpu_cooling *cooling; + char *name; + int ret; + + if (!dev || !dev->of_node || !words || !words->max_power_mw || + !words->ppm_enabled || !words->ppm_target_mw || + !IS_ALIGNED((unsigned long)words->max_power_mw, sizeof(u32)) || + !IS_ALIGNED((unsigned long)words->ppm_enabled, sizeof(u32)) || + !IS_ALIGNED((unsigned long)words->ppm_target_mw, sizeof(u32)) || + words->max_power_mw == words->ppm_enabled || + words->max_power_mw == words->ppm_target_mw || + words->ppm_enabled == words->ppm_target_mw) + return ERR_PTR(-EINVAL); + cooling = kzalloc_obj(*cooling); + if (!cooling) + return ERR_PTR(-ENOMEM); + cooling->dev = dev; + cooling->words = *words; + mutex_init(&cooling->lock); + ret = asahi_gpu_cap_init(&cooling->cap, READ_ONCE(*words->max_power_mw)); + if (ret) { + ret = -EINVAL; + goto free; + } + cooling->ppm_budget_mw = cooling->cap.max_mw; + if (READ_ONCE(*words->ppm_enabled) && READ_ONCE(*words->ppm_target_mw)) + cooling->ppm_budget_mw = READ_ONCE(*words->ppm_target_mw); + if (cooling->ppm_budget_mw < cooling->cap.floor_mw) { + ret = -ERANGE; + goto free; + } + mutex_lock(&cooling->lock); + ret = asahi_gpu_cap_update(&cooling->cap, 0, asahi_gpu_publish_cap, cooling); + mutex_unlock(&cooling->lock); + if (ret) + goto free; + cooling->cdev = thermal_of_cooling_device_register(dev->of_node, 0, + "gpu-power", cooling, &asahi_gpu_cooling_ops); + if (IS_ERR(cooling->cdev)) { + ret = PTR_ERR(cooling->cdev); + goto free; + } + sysfs_attr_init(&cooling->telemetry.attr); + cooling->telemetry.attr.name = "gpu_power_ceiling"; + cooling->telemetry.attr.mode = 0444; + cooling->telemetry.show = gpu_power_ceiling_show; + ret = device_create_file(dev, &cooling->telemetry); + if (ret) { + thermal_cooling_device_unregister(cooling->cdev); + goto free; + } + name = kasprintf(GFP_KERNEL, "asahi-%s-power", dev_name(dev)); + if (name) { + cooling->debugfs = debugfs_create_dir(name, NULL); + debugfs_create_file("ceiling", 0444, cooling->debugfs, cooling, &ceiling_fops); + kfree(name); + } + return cooling; +free: + kfree(cooling); + return ERR_PTR(ret); +} + +void asahi_gpu_cooling_rearm(struct asahi_gpu_cooling *cooling) +{ + guard(mutex)(&cooling->lock); + /* Preserve every requester across recovery, before consumption resumes. */ + asahi_gpu_cap_rearm(&cooling->cap, asahi_gpu_publish_cap, cooling); +} + +void asahi_gpu_cooling_unregister(struct asahi_gpu_cooling *cooling) +{ + debugfs_remove_recursive(cooling->debugfs); + device_remove_file(cooling->dev, &cooling->telemetry); + thermal_cooling_device_unregister(cooling->cdev); + kfree(cooling); +} diff --git a/drivers/gpu/drm/asahi/cooling.h b/drivers/gpu/drm/asahi/cooling.h new file mode 100644 index 0000000000000..5c15f76347be6 --- /dev/null +++ b/drivers/gpu/drm/asahi/cooling.h @@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +#ifndef ASAHI_COOLING_H +#define ASAHI_COOLING_H +#include +#include "cooling_policy.h" +struct device; +struct asahi_gpu_cooling; + +/* Separate typed fields, retained until unregister drains every callback. + * RegionC is WB; both admitted PowerState mappings are WC. + */ +struct asahi_gpu_power_words { + u32 *max_power_mw; + u32 *ppm_enabled; + u32 *ppm_target_mw; +}; +struct asahi_gpu_cooling *asahi_gpu_cooling_register(struct device *dev, + const struct asahi_gpu_power_words *words); +int asahi_gpu_cooling_request(struct asahi_gpu_cooling *cooling, + enum asahi_gpu_cap_source source, u32 mw); +void asahi_gpu_cooling_unregister(struct asahi_gpu_cooling *cooling); +void asahi_gpu_cooling_rearm(struct asahi_gpu_cooling *cooling); +#endif diff --git a/drivers/gpu/drm/asahi/cooling_policy.h b/drivers/gpu/drm/asahi/cooling_policy.h new file mode 100644 index 0000000000000..f100a1be0f4f2 --- /dev/null +++ b/drivers/gpu/drm/asahi/cooling_policy.h @@ -0,0 +1,137 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +#ifndef ASAHI_COOLING_POLICY_H +#define ASAHI_COOLING_POLICY_H + +#define ASAHI_GPU_COOLING_MAX 100U +/* + * Lowest ceiling, as a percentage of the initialized maximum. It keeps the + * deepest state a usable GPU rather than a stop request; the firmware has no + * documented behaviour for a zero budget. + */ +#define ASAHI_GPU_MIN_PERCENT 25U + +enum asahi_gpu_cap_source { + ASAHI_GPU_CAP_THERMAL, + ASAHI_GPU_CAP_FAST_DIE, + ASAHI_GPU_CAP_SYSTEM, + ASAHI_GPU_CAP_MANUAL, + ASAHI_GPU_CAP_SOURCES, +}; + +struct asahi_gpu_cap { + unsigned int max_mw; + unsigned int floor_mw; + unsigned int requests[ASAHI_GPU_CAP_SOURCES]; + unsigned int requested_state; + unsigned int applied_mw; +}; + +static inline int asahi_gpu_cap_percent(unsigned long state) +{ + if (state > ASAHI_GPU_COOLING_MAX) + return -1; + return ASAHI_GPU_COOLING_MAX - state; +} + +static inline unsigned int asahi_gpu_cap_scale(unsigned int max_mw, + unsigned int percent) +{ + /* Divide first so even UINT_MAX never overflows. Round down. */ + return max_mw / 100 * percent + max_mw % 100 * percent / 100; +} + +static inline int asahi_gpu_cap_init(struct asahi_gpu_cap *cap, + unsigned int max_mw) +{ + unsigned int i; + + if (!max_mw) + return -1; + cap->max_mw = max_mw; + cap->floor_mw = asahi_gpu_cap_scale(max_mw, ASAHI_GPU_MIN_PERCENT); + if (!cap->floor_mw) + cap->floor_mw = 1; + for (i = 0; i < ASAHI_GPU_CAP_SOURCES; i++) + cap->requests[i] = max_mw; + /* + * Start unrestricted, as the firmware is initialized. Thermal zones that + * reference the GPU set their state as soon as the cooling device binds; + * a GPU that no zone references keeps the firmware's own policy. + */ + cap->requested_state = 0; + cap->applied_mw = 0; /* No publication yet; never a firmware zero request. */ + return 0; +} + +/* + * The firmware's power-management budget follows the ceiling below its own + * initialized value and returns to that value when the ceiling is lifted, so + * state 0 publishes exactly what initialization did. + */ +static inline unsigned int asahi_gpu_cap_ppm(unsigned int mw, unsigned int max_mw, + unsigned int ppm_budget_mw) +{ + if (mw >= max_mw) + return ppm_budget_mw; + return mw < ppm_budget_mw ? mw : ppm_budget_mw; +} + +static inline unsigned int asahi_gpu_cap_effective(const struct asahi_gpu_cap *cap) +{ + unsigned int i, limit = cap->max_mw; + + for (i = 0; i < ASAHI_GPU_CAP_SOURCES; i++) + if (cap->requests[i] < limit) + limit = cap->requests[i]; + return limit; +} + +/* Publisher validates before any store and cannot partially fail. Commit only + * after successful publication. A weaker requester never overrides another. + * Absolute requests below the provisional floor are rejected, never rounded up. + */ +static inline int asahi_gpu_cap_request(struct asahi_gpu_cap *cap, + unsigned int source, unsigned int mw, + int (*publish)(void *context, unsigned int mw), void *context) +{ + struct asahi_gpu_cap next = *cap; + unsigned int effective; + int ret; + + if (source >= ASAHI_GPU_CAP_SOURCES || mw < cap->floor_mw || mw > cap->max_mw) + return -1; + next.requests[source] = mw; + effective = asahi_gpu_cap_effective(&next); + ret = publish(context, effective); + if (ret) + return ret; + next.applied_mw = effective; + *cap = next; + return 0; +} + +static inline int asahi_gpu_cap_update(struct asahi_gpu_cap *cap, + unsigned long state, + int (*publish)(void *context, unsigned int mw), void *context) +{ + int percent = asahi_gpu_cap_percent(state); + unsigned int mw; + int ret; + + if (percent < 0) + return -1; + mw = asahi_gpu_cap_scale(cap->max_mw, percent); + if (mw < cap->floor_mw) + mw = cap->floor_mw; + ret = asahi_gpu_cap_request(cap, ASAHI_GPU_CAP_THERMAL, mw, publish, context); + if (!ret) + cap->requested_state = state; + return ret; +} +/* Restore the committed restriction before queues reopen after recovery. */ +static inline int asahi_gpu_cap_rearm(const struct asahi_gpu_cap *cap, + int (*publish)(void *context, unsigned int mw), void *context) +{ + return publish(context, cap->applied_mw); +} +#endif diff --git a/drivers/gpu/drm/asahi/cooling_test.c b/drivers/gpu/drm/asahi/cooling_test.c new file mode 100644 index 0000000000000..c4c43c2017616 --- /dev/null +++ b/drivers/gpu/drm/asahi/cooling_test.c @@ -0,0 +1,205 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT + +/* Build this same production-policy suite with ASAHI_HOST_TEST or KUnit. */ +#ifdef ASAHI_HOST_TEST +#include +#include +#include +struct kunit { int unused; }; +#define KUNIT_EXPECT_EQ(test, a, b) do { (void)(test); assert((a) == (b)); } while (0) +#define KUNIT_EXPECT_LT(test, a, b) do { (void)(test); assert((a) < (b)); } while (0) +#else +#include +#include +#include +#endif +#include "cooling_policy.h" + +struct fake_actuator { + unsigned int max_mw; + unsigned int region_c_mw; + unsigned int ppm_target_mw; + unsigned int ppm_budget_mw; + unsigned int calls; + int error; +}; + +static int fake_publish(void *context, unsigned int mw) +{ + struct fake_actuator *fake = context; + + fake->calls++; + if (fake->error) + return fake->error; /* Fail before either store. */ + fake->region_c_mw = mw; + fake->ppm_target_mw = asahi_gpu_cap_ppm(mw, fake->max_mw, fake->ppm_budget_mw); + return 0; +} + +static void cap_mapping(struct kunit *test) +{ + struct asahi_gpu_cap cap; + struct fake_actuator fake = { .max_mw = 9550, .ppm_budget_mw = 9750 }; + unsigned long state; + unsigned int previous = 9550; + + KUNIT_EXPECT_EQ(test, asahi_gpu_cap_init(&cap, 9550), 0); + for (state = 0; state <= 100; state++) { + unsigned int expected = 9550 * (100 - state) / 100; + + if (expected < 2387) + expected = 2387; + KUNIT_EXPECT_EQ(test, asahi_gpu_cap_update(&cap, state, fake_publish, &fake), 0); + KUNIT_EXPECT_EQ(test, cap.applied_mw, expected); + KUNIT_EXPECT_EQ(test, fake.region_c_mw, expected); + KUNIT_EXPECT_EQ(test, fake.ppm_target_mw, state ? expected : 9750U); + KUNIT_EXPECT_EQ(test, cap.applied_mw <= previous, 1); + previous = cap.applied_mw; + } + KUNIT_EXPECT_EQ(test, asahi_gpu_cap_percent(100), 0); + KUNIT_EXPECT_LT(test, asahi_gpu_cap_percent(101), 0); + KUNIT_EXPECT_LT(test, asahi_gpu_cap_percent(ULONG_MAX), 0); +} + +static void cap_composition(struct kunit *test) +{ + struct asahi_gpu_cap cap; + struct fake_actuator fake = { .max_mw = 10000, .ppm_budget_mw = 10000 }; + unsigned int a, b, source; + + /* Every demand pair, for every non-thermal requester, in both orders. */ + for (source = ASAHI_GPU_CAP_FAST_DIE; source < ASAHI_GPU_CAP_SOURCES; source++) { + for (a = 25; a <= 100; a++) { + for (b = 25; b <= 100; b++) { + unsigned int expected = (a < b ? a : b) * 100; + + asahi_gpu_cap_init(&cap, 10000); + KUNIT_EXPECT_EQ(test, asahi_gpu_cap_request(&cap, source, b * 100, fake_publish, &fake), 0); + KUNIT_EXPECT_EQ(test, cap.applied_mw, b * 100); + KUNIT_EXPECT_EQ(test, asahi_gpu_cap_update(&cap, 100 - a, fake_publish, &fake), 0); + KUNIT_EXPECT_EQ(test, cap.applied_mw, expected); + asahi_gpu_cap_init(&cap, 10000); + asahi_gpu_cap_update(&cap, 100 - a, fake_publish, &fake); + asahi_gpu_cap_request(&cap, source, b * 100, fake_publish, &fake); + KUNIT_EXPECT_EQ(test, cap.applied_mw, expected); + } + } + } + /* All four owners remain independent when one relaxes. */ + asahi_gpu_cap_init(&cap, 10000); + asahi_gpu_cap_update(&cap, 0, fake_publish, &fake); + asahi_gpu_cap_request(&cap, ASAHI_GPU_CAP_FAST_DIE, 3000, fake_publish, &fake); + asahi_gpu_cap_request(&cap, ASAHI_GPU_CAP_SYSTEM, 5000, fake_publish, &fake); + asahi_gpu_cap_request(&cap, ASAHI_GPU_CAP_MANUAL, 4000, fake_publish, &fake); + KUNIT_EXPECT_EQ(test, cap.applied_mw, 3000U); + asahi_gpu_cap_request(&cap, ASAHI_GPU_CAP_FAST_DIE, 10000, fake_publish, &fake); + KUNIT_EXPECT_EQ(test, cap.applied_mw, 4000U); + asahi_gpu_cap_update(&cap, 1, fake_publish, &fake); + KUNIT_EXPECT_EQ(test, cap.applied_mw, 4000U); + asahi_gpu_cap_request(&cap, ASAHI_GPU_CAP_MANUAL, 10000, fake_publish, &fake); + KUNIT_EXPECT_EQ(test, cap.applied_mw, 5000U); +} + +static void cap_startup_and_ppm(struct kunit *test) +{ + struct asahi_gpu_cap cap; + struct fake_actuator fake = { .max_mw = 9550, .ppm_budget_mw = 7000 }; + + /* Unrestricted at start: state 0 republishes the initialized words. */ + KUNIT_EXPECT_EQ(test, asahi_gpu_cap_init(&cap, 9550), 0); + KUNIT_EXPECT_EQ(test, cap.applied_mw, 0U); + KUNIT_EXPECT_EQ(test, cap.requested_state, 0U); + KUNIT_EXPECT_EQ(test, asahi_gpu_cap_update(&cap, 0, fake_publish, &fake), 0); + KUNIT_EXPECT_EQ(test, fake.region_c_mw, 9550U); + KUNIT_EXPECT_EQ(test, fake.ppm_target_mw, 7000U); + KUNIT_EXPECT_EQ(test, asahi_gpu_cap_update(&cap, 100, fake_publish, &fake), 0); + KUNIT_EXPECT_EQ(test, fake.region_c_mw, 2387U); + KUNIT_EXPECT_EQ(test, fake.ppm_target_mw, 2387U); + asahi_gpu_cap_update(&cap, 0, fake_publish, &fake); + KUNIT_EXPECT_EQ(test, fake.region_c_mw, 9550U); + KUNIT_EXPECT_EQ(test, fake.ppm_target_mw, 7000U); + KUNIT_EXPECT_EQ(test, fake.ppm_budget_mw, 7000U); +} + +static void cap_failure_and_invalid(struct kunit *test) +{ + struct asahi_gpu_cap cap; + struct fake_actuator fake = { .max_mw = 10000, .ppm_budget_mw = 10000 }; + unsigned int calls; + + asahi_gpu_cap_init(&cap, 10000); + asahi_gpu_cap_update(&cap, 70, fake_publish, &fake); + fake.error = -5; + /* Failed relaxation and failed tightening both keep the old restriction. */ + KUNIT_EXPECT_EQ(test, asahi_gpu_cap_update(&cap, 0, fake_publish, &fake), -5); + KUNIT_EXPECT_EQ(test, asahi_gpu_cap_update(&cap, 100, fake_publish, &fake), -5); + KUNIT_EXPECT_EQ(test, asahi_gpu_cap_request(&cap, ASAHI_GPU_CAP_MANUAL, 2500, fake_publish, &fake), -5); + KUNIT_EXPECT_EQ(test, cap.requested_state, 70U); + KUNIT_EXPECT_EQ(test, cap.applied_mw, 3000U); + KUNIT_EXPECT_EQ(test, cap.requests[ASAHI_GPU_CAP_MANUAL], 10000U); + KUNIT_EXPECT_EQ(test, fake.region_c_mw, 3000U); + KUNIT_EXPECT_EQ(test, fake.ppm_target_mw, 3000U); + calls = fake.calls; + KUNIT_EXPECT_LT(test, asahi_gpu_cap_update(&cap, ULONG_MAX, fake_publish, &fake), 0); + KUNIT_EXPECT_LT(test, asahi_gpu_cap_request(&cap, ASAHI_GPU_CAP_SOURCES, 3000, fake_publish, &fake), 0); + KUNIT_EXPECT_LT(test, asahi_gpu_cap_request(&cap, ASAHI_GPU_CAP_SYSTEM, 0, fake_publish, &fake), 0); + KUNIT_EXPECT_LT(test, asahi_gpu_cap_request(&cap, ASAHI_GPU_CAP_SYSTEM, 2499, fake_publish, &fake), 0); + KUNIT_EXPECT_LT(test, asahi_gpu_cap_request(&cap, ASAHI_GPU_CAP_SYSTEM, 10001, fake_publish, &fake), 0); + KUNIT_EXPECT_EQ(test, fake.calls, calls); + KUNIT_EXPECT_EQ(test, cap.applied_mw, 3000U); +} + +static void cap_recovery_and_limits(struct kunit *test) +{ + struct asahi_gpu_cap cap; + struct fake_actuator fake = { .max_mw = 1, .ppm_budget_mw = UINT_MAX }; + + KUNIT_EXPECT_LT(test, asahi_gpu_cap_init(&cap, 0), 0); + asahi_gpu_cap_init(&cap, 1); + asahi_gpu_cap_update(&cap, 100, fake_publish, &fake); + KUNIT_EXPECT_EQ(test, cap.applied_mw, 1U); + fake.max_mw = UINT_MAX; + asahi_gpu_cap_init(&cap, UINT_MAX); + asahi_gpu_cap_update(&cap, 0, fake_publish, &fake); + KUNIT_EXPECT_EQ(test, cap.applied_mw, UINT_MAX); + fake.max_mw = 10000; + asahi_gpu_cap_init(&cap, 10000); + asahi_gpu_cap_update(&cap, 0, fake_publish, &fake); + asahi_gpu_cap_request(&cap, ASAHI_GPU_CAP_MANUAL, 3000, fake_publish, &fake); + fake.region_c_mw = 10000; + fake.ppm_target_mw = 10000; + KUNIT_EXPECT_EQ(test, asahi_gpu_cap_rearm(&cap, fake_publish, &fake), 0); + KUNIT_EXPECT_EQ(test, fake.region_c_mw, 3000U); + KUNIT_EXPECT_EQ(test, fake.ppm_target_mw, 3000U); + KUNIT_EXPECT_EQ(test, cap.requested_state, 0U); +} + +#ifdef ASAHI_HOST_TEST +int main(void) +{ + struct kunit test = {}; + + cap_mapping(&test); + cap_composition(&test); + cap_startup_and_ppm(&test); + cap_failure_and_invalid(&test); + cap_recovery_and_limits(&test); + puts("PASS: mW mapping, all-owner composition, unrestricted startup, separate PPM, failure, recovery, limits"); + return 0; +} +#else +static struct kunit_case asahi_gpu_cooling_cases[] = { + KUNIT_CASE(cap_mapping), + KUNIT_CASE(cap_composition), + KUNIT_CASE(cap_startup_and_ppm), + KUNIT_CASE(cap_failure_and_invalid), + KUNIT_CASE(cap_recovery_and_limits), + {} +}; +static struct kunit_suite asahi_gpu_cooling_suite = { + .name = "asahi-gpu-cooling", + .test_cases = asahi_gpu_cooling_cases, +}; +kunit_test_suite(asahi_gpu_cooling_suite); +MODULE_LICENSE("Dual MIT/GPL"); +#endif diff --git a/tools/testing/selftests/drivers/apple/Makefile b/tools/testing/selftests/drivers/apple/Makefile index f13a2d06d9b4c..c2bf2243ab82f 100644 --- a/tools/testing/selftests/drivers/apple/Makefile +++ b/tools/testing/selftests/drivers/apple/Makefile @@ -12,7 +12,8 @@ TEST_PROGS := macsmc-j713.py atc-t8132-usb3.py pmgr-inherited.py dwc3-initial-role.py \ sn201202x-transport.py bcm4377-dma-window.py bcm4377-radio-sids.py \ bcm4377-state-wait.py bcm4377-rti-address.py aop-las.py \ - aop-motion.py centauri-t6050-probe.py sep-ioctl-fuzz.py + aop-motion.py centauri-t6050-probe.py sep-ioctl-fuzz.py \ + gpu-cooling.py TEST_FILES := j813-peripherals.sh include ../../lib.mk diff --git a/tools/testing/selftests/drivers/apple/gpu-cooling.py b/tools/testing/selftests/drivers/apple/gpu-cooling.py new file mode 100644 index 0000000000000..ffadd383ea75c --- /dev/null +++ b/tools/testing/selftests/drivers/apple/gpu-cooling.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only OR MIT +"""Run the production absolute-power cap suite on the host, without a GPU.""" +import os +from pathlib import Path +import subprocess +import tempfile + +root = Path(__file__).resolve().parents[5] +source = root / "drivers/gpu/drm/asahi/cooling_test.c" +with tempfile.TemporaryDirectory(prefix="asahi-cooling-") as work: + binary = str(Path(work) / "cap-tests") + subprocess.run([os.environ.get("HOSTCC", "cc"), "-std=gnu11", "-Wall", "-Wextra", + "-Werror", "-DASAHI_HOST_TEST", str(source), "-o", binary], check=True) + subprocess.run([binary], check=True) + + # Compile the exact typed firmware members independently of the kernel. + power = root / "drivers/gpu/drm/asahi/g17/fw/power.rs" + rust = Path(work) / "power.rs" + rust.write_text(f'''#![allow(dead_code)] +#[path = "{power}"] mod power; +fn main() {{ + assert_eq!(core::mem::offset_of!(power::PowerState, ppm_target_mw), 0x12c); + let mut p = power::PowerState {{ prefix: [0xa5; 0x128], ppm_enabled: 1, + ppm_target_mw: 9750, suffix: [0x5a; 0x590] }}; + p.ppm_target_mw = 2387; + let enabled = p.ppm_enabled; + assert_eq!(enabled, 1); + assert!(p.prefix.iter().all(|v| *v == 0xa5)); + assert!(p.suffix.iter().all(|v| *v == 0x5a)); + println!("PASS: separate typed PPM fields, canaries"); +}} +''') + subprocess.run([os.environ.get("RUSTC", "rustc"), "--edition=2021", str(rust), + "-o", binary], check=True) + subprocess.run([binary], check=True) From f2e7ee1192a234d6342c993cd1590f3ea41a40fa Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Thu, 19 Nov 2026 23:17:51 +1100 Subject: [PATCH 3/6] drm/asahi: g17: write the power ceiling on the A18 Pro and M4 Pro builds Resolve the Region C maximum-power word and the power state's power-management budget for the 26A428 (A18 Pro) and 25G83 (M4 Pro) builds, which share the layout, and hand them to the cooling device before the firmware is initialized. The power state's two budget words become typed fields with their existing initial values. A GPU the device tree makes a cooling device does not start on a build whose words are not described, rather than run with its thermal zones silently ignored. Two builds stay without them, each with the reason in the code: 25F84, whose power state is not the 25G83 one, and the M5 firmware, which takes its host budget at a different Region C offset that the M5 governor already writes. Signed-off-by: Ryan Murray --- drivers/gpu/drm/asahi/g17/cooling.rs | 109 +++++++++++++++++++ drivers/gpu/drm/asahi/g17/fw/initdata.rs | 17 ++- drivers/gpu/drm/asahi/g17/fw/mod.rs | 1 + drivers/gpu/drm/asahi/g17/fw/power.rs | 21 ++++ drivers/gpu/drm/asahi/g17/fw/variant.rs | 11 ++ drivers/gpu/drm/asahi/g17/initdata.rs | 33 +++++- drivers/gpu/drm/asahi/g17/initdata/v25g83.rs | 1 + drivers/gpu/drm/asahi/g17/mod.rs | 9 ++ drivers/gpu/drm/asahi/g17/runtime.rs | 2 + drivers/gpu/drm/asahi/g17/runtime/events.rs | 3 + 10 files changed, 199 insertions(+), 8 deletions(-) create mode 100644 drivers/gpu/drm/asahi/g17/cooling.rs create mode 100644 drivers/gpu/drm/asahi/g17/fw/power.rs diff --git a/drivers/gpu/drm/asahi/g17/cooling.rs b/drivers/gpu/drm/asahi/g17/cooling.rs new file mode 100644 index 0000000000000..c9fbe92e2cb61 --- /dev/null +++ b/drivers/gpu/drm/asahi/g17/cooling.rs @@ -0,0 +1,109 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT + +//! GPU power-ceiling cooling device of the G17-family firmware builds. +//! +//! The policy (requesters, mapping, thermal callbacks) is the shared C owner in `cooling.c`; +//! this file resolves the firmware words of the running build and registers it when the +//! device tree makes the GPU a cooling device (`#cooling-cells`). + +use super::{fw::variant::Variant, initdata::InitData}; +use core::{ffi::c_void, ptr::NonNull}; +use kernel::{c_str, device, prelude::*, sync::aref::ARef}; + +extern "C" { + fn asahi_gpu_cooling_register(dev: *mut c_void, words: *const PowerWords) -> *mut c_void; + fn asahi_gpu_cooling_unregister(cooling: *mut c_void); + fn asahi_gpu_cooling_rearm(cooling: *mut c_void); +} + +/// Where a firmware build takes the GPU's absolute power inputs. +#[derive(Clone, Copy)] +pub(crate) enum Layout { + /// A18 Pro 26A428 and M4 Pro 25G83: [`RegionC::max_power_mw`] and the primary + /// [`PowerState`]'s power-management budget. + /// + /// [`RegionC::max_power_mw`]: super::fw::initdata::RegionC::max_power_mw + /// [`PowerState`]: super::fw::power::PowerState + Common, +} + +/// C ABI; these are independent typed members, never numeric bundle offsets. +#[repr(C)] +pub(super) struct PowerWords { + pub(super) max_power_mw: *mut u32, + pub(super) ppm_enabled: *mut u32, + pub(super) ppm_target_mw: *mut u32, +} + +fn required(dev: &device::Device) -> Result { + let Some(node) = dev.of_node() else { + return Ok(false); + }; + match node.get_property::(c_str!("#cooling-cells")) { + Ok(2) => Ok(true), + Ok(_) => Err(EINVAL), + Err(ENOENT) => Ok(false), + Err(error) => Err(error), + } +} + +/// Check before starting either processor. A GPU the device tree makes a cooling device must +/// not run without its ceiling: a build whose power inputs are not described here +/// ([`Variant::live_power`] is `None`) refuses to start rather than silently +/// ignoring the thermal zones. +pub(super) fn admit(dev: &device::Device, variant: &Variant) -> Result { + if required(dev)? && variant.live_power.is_none() { + dev_err!(dev, "GPU live power layout not admitted\n"); + return Err(EOPNOTSUPP); + } + Ok(()) +} + +/// Dropped (and callbacks drained) before the firmware allocations. No C +/// callback takes the GPU mutex; thermal changes reach even a long dispatch. +pub(super) struct Cooling { + inner: NonNull, + _dev: ARef, +} + +// SAFETY: C serializes all shared access with its per-device mutex; Drop has +// unique ownership and drains thermal and sysfs callbacks. +unsafe impl Send for Cooling {} +// SAFETY: As for Send. Rearming uses the same C mutex as the callbacks. +unsafe impl Sync for Cooling {} + +impl Cooling { + pub(super) fn attach( + dev: &device::Device, + variant: &Variant, + init: &InitData, + ) -> Result> { + if !required(dev)? { + return Ok(None); + } + let layout = variant.live_power.as_ref().ok_or(EOPNOTSUPP)?; + let words = init.cooling_words(layout)?; + // SAFETY: The owner keeps all validated shared words mapped until after + // Drop drains the C callbacks. Registration republishes the initialized + // words (state 0) before INIT, port attachment or any work publication. + let inner = kernel::error::from_err_ptr(unsafe { + asahi_gpu_cooling_register(dev.as_raw().cast(), &words) + })?; + Ok(Some(Self { + inner: NonNull::new(inner).ok_or(ENOMEM)?, + _dev: dev.into(), + })) + } + + pub(super) fn rearm(&self) { + // SAFETY: The C allocation and the power mappings are still owned. + unsafe { asahi_gpu_cooling_rearm(self.inner.as_ptr()) }; + } +} + +impl Drop for Cooling { + fn drop(&mut self) { + // SAFETY: Unique owner; drains callbacks before mappings are freed. + unsafe { asahi_gpu_cooling_unregister(self.inner.as_ptr()) }; + } +} diff --git a/drivers/gpu/drm/asahi/g17/fw/initdata.rs b/drivers/gpu/drm/asahi/g17/fw/initdata.rs index caf2ba6b658e6..461e6574d461a 100644 --- a/drivers/gpu/drm/asahi/g17/fw/initdata.rs +++ b/drivers/gpu/drm/asahi/g17/fw/initdata.rs @@ -1386,6 +1386,7 @@ static_assert!(core::mem::offset_of!(RegionC, idle_power_off_delay) == 0x9bc); static_assert!(core::mem::offset_of!(RegionC, compute_watchdog) == 0x9cc); static_assert!(core::mem::offset_of!(RegionC, power_assert_tally) == 0xe51); static_assert!(size_of::() == 0x1000); +static_assert!(core::mem::offset_of!(RegionC, max_power_mw) == 0x84); // SAFETY: `RegionC` consists of integers and arrays of integers only. unsafe impl Zeroable for RegionC {} @@ -1813,8 +1814,6 @@ const POWER_STATE_WORDS: Words = &[ (0x108, 0x42c80000, 1), (0x10c, 0x43c80000, 1), (0x110, 0xc8, 1), - (0x128, 1, 1), - (0x12c, 0x2616, 1), (0x130, 0x3f000000, 1), (0x134, 0x40cccccd, 1), (0x14c, 1, 1), @@ -1900,15 +1899,21 @@ const SECONDARY_CONFIG_WORDS: Words = &[ /// Power state the primary status B carries for the secondary instance, /// named by [`SecondaryRoot::power_state_va`]. -#[repr(C)] -pub(crate) struct PowerState(pub(crate) [u8; 0x6c0]); +pub(crate) use super::power::PowerState; -// SAFETY: `PowerState` is a byte array. +// SAFETY: `PowerState` contains only integers and byte arrays. unsafe impl Zeroable for PowerState {} impl PowerState { fn init(&mut self) { - fill_words(&mut self.0, POWER_STATE_WORDS); + // 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::()) + }; + fill_words(bytes, POWER_STATE_WORDS); + self.ppm_enabled = 1; + self.ppm_target_mw = 9750; } } diff --git a/drivers/gpu/drm/asahi/g17/fw/mod.rs b/drivers/gpu/drm/asahi/g17/fw/mod.rs index df69458699b39..f1fb16950b209 100644 --- a/drivers/gpu/drm/asahi/g17/fw/mod.rs +++ b/drivers/gpu/drm/asahi/g17/fw/mod.rs @@ -19,6 +19,7 @@ pub(crate) mod context; pub(crate) mod freelist; pub(crate) mod initdata; pub(crate) mod kick; +pub(crate) mod power; // Most M5 encodings are reached only once an M5 board has a page-table boot path. #[allow(dead_code)] pub(crate) mod m5; diff --git a/drivers/gpu/drm/asahi/g17/fw/power.rs b/drivers/gpu/drm/asahi/g17/fw/power.rs new file mode 100644 index 0000000000000..80fc42a354e11 --- /dev/null +++ b/drivers/gpu/drm/asahi/g17/fw/power.rs @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT + +//! Typed absolute-power inputs. These are separate firmware objects/fields. + +/// Power state shared by the A18 Pro (26A428) and M4 Pro (25G83) builds. +/// Alignment stays one for embedding in packed PrimaryStatusB. The live +/// publisher separately validates the natural alignment of each u32 mapping. +#[repr(C, packed)] +pub(crate) struct PowerState { + pub(crate) prefix: [u8; 0x128], + pub(crate) ppm_enabled: u32, + /// Absolute ApplePPM budget in milliwatts, independent of RegionC's maximum. + pub(crate) ppm_target_mw: u32, + pub(crate) suffix: [u8; 0x590], +} + +const _: () = { + assert!(core::mem::offset_of!(PowerState, ppm_enabled) == 0x128); + assert!(core::mem::offset_of!(PowerState, ppm_target_mw) == 0x12c); + assert!(core::mem::size_of::() == 0x6c0); +}; diff --git a/drivers/gpu/drm/asahi/g17/fw/variant.rs b/drivers/gpu/drm/asahi/g17/fw/variant.rs index 7c3ee790dc941..6445e2492606a 100644 --- a/drivers/gpu/drm/asahi/g17/fw/variant.rs +++ b/drivers/gpu/drm/asahi/g17/fw/variant.rs @@ -46,6 +46,9 @@ pub(crate) enum QueueRegistration { /// The layouts and rules of one G17 firmware build. #[derive(Copy, Clone)] pub(crate) struct Variant { + /// Where the build takes the GPU's absolute power inputs, which the power-ceiling + /// cooling device writes ([`crate::g17::cooling`]); `None` where they are not described. + pub(crate) live_power: Option, /// The initialization graph. pub(crate) graph: InitGraph, /// How the firmware acknowledges its initialization. @@ -135,6 +138,7 @@ pub(crate) enum Firmware { /// The A18 Pro firmware (26A428). pub(crate) const V26A428: Variant = Variant { + live_power: Some(crate::g17::cooling::Layout::Common), graph: InitGraph::G17P, init_ack: InitAck::Message, power_on_done: true, @@ -165,6 +169,7 @@ pub(crate) const V26A428: Variant = Variant { /// The M4 Pro firmware (25G83, `mBoot-18000.161.10`). pub(crate) const V25G83: Variant = Variant { + live_power: Some(crate::g17::cooling::Layout::Common), graph: InitGraph::V25G83, init_ack: InitAck::Status, // Never sent on the M4 Pro: the firmware has retired its opening record when it accepts @@ -206,6 +211,9 @@ pub(crate) const V25G83: Variant = Variant { /// The 26A428 firmware of the M5 GPUs, which runs the M5 protocol (the M5 graph, queues and /// command set). pub(crate) const V26A428_M5: Variant = Variant { + // The M5 firmware takes its host power budget at Region C +0x88, not +0x84, and the M5 + // session never releases its storage; the M5 governor (g17/thermal.rs) owns that word. + live_power: None, m5_commands: true, // Not measured on the M5 firmware. traversal_exclusion: false, @@ -217,6 +225,9 @@ pub(crate) const V26A428_M5: Variant = Variant { }; pub(crate) const V25F84: Variant = Variant { + // The power inputs of this build have not been described: its PowerState is not the + // 25G83 one. A GPU the device tree makes a cooling device refuses to start on it. + live_power: None, qid_floor: 0, registration: QueueRegistration::Mailbox, compute: super::compute::ComputeProgram::V25F84, diff --git a/drivers/gpu/drm/asahi/g17/initdata.rs b/drivers/gpu/drm/asahi/g17/initdata.rs index c505bbd5b3b45..f1b000f4025c1 100644 --- a/drivers/gpu/drm/asahi/g17/initdata.rs +++ b/drivers/gpu/drm/asahi/g17/initdata.rs @@ -175,8 +175,9 @@ pub(super) struct InitData { /// The M5 firmware build's layout, where [`Self::m5`]. graph: &'static super::fw::m5::initdata::Graph, outer_credits: Option>, - // Live publication uses interior writes under the device mutex. This owner is Send but not - // Sync, so safe host code cannot issue those writes concurrently through shared references. + // Live effort publication uses interior writes under the device mutex. Power-ceiling + // fields are handed to the cooling owner and serialized by its separate mutex. This owner + // is Send but not Sync; safe host code cannot publish effort concurrently through &self. _single_publisher: core::marker::PhantomData>, aliases: KVec, objects: KVec, @@ -186,6 +187,7 @@ pub(super) struct InitData { cluster: usize, bundle: usize, region_c: usize, + power_state: Option<(usize, usize)>, qos: usize, pb: usize, page_pool: usize, @@ -301,6 +303,7 @@ impl InitData { cluster: 0, bundle: 0, region_c: 0, + power_state: None, qos: 0, pb: 0, page_pool: 0, @@ -1514,6 +1517,10 @@ impl InitData { .write(abi::private::PRIMARY_STATUS_A, abi::StatusBlock::new())?; self.object_mut(self.cluster)? .write(abi::private::SECONDARY_STATUS_A, abi::StatusBlock::new())?; + self.power_state = Some(( + self.cluster, + abi::private::STATUS_B + abi::PrimaryStatusB::POWER_STATE, + )); let fwctl_va = self.object(tables.fwctl)?.gpu_va(); self.object_mut(self.cluster)? .initialize::(abi::private::STATUS_B, |status| { @@ -1694,6 +1701,28 @@ impl InitData { Ok(()) } + /// Resolve typed fields from their retained objects before any store. + pub(super) fn cooling_words( + &self, + layout: &super::cooling::Layout, + ) -> Result { + let super::cooling::Layout::Common = layout; + let region = self.object(self.region_c)?; + let (index, offset) = self.power_state.ok_or(EOPNOTSUPP)?; + let power = self.object(index)?; + Ok(super::cooling::PowerWords { + max_power_mw: region + .pointer(core::mem::offset_of!(abi::RegionC, max_power_mw), 4)? + .cast(), + ppm_enabled: power + .pointer(offset + core::mem::offset_of!(abi::PowerState, ppm_enabled), 4)? + .cast(), + ppm_target_mw: power + .pointer(offset + core::mem::offset_of!(abi::PowerState, ppm_target_mw), 4)? + .cast(), + }) + } + pub(super) fn effort(&self) -> Result { Ok(self .object(self.region_c)? diff --git a/drivers/gpu/drm/asahi/g17/initdata/v25g83.rs b/drivers/gpu/drm/asahi/g17/initdata/v25g83.rs index c0485c4932e48..72d2c6d017c50 100644 --- a/drivers/gpu/drm/asahi/g17/initdata/v25g83.rs +++ b/drivers/gpu/drm/asahi/g17/initdata/v25g83.rs @@ -104,6 +104,7 @@ impl InitData { false => self.object(objects.aon)?.gpu_va(), }; let power_state_va = self.object(objects.power_state)?.gpu_va(); + self.power_state = Some((objects.power_state, 0)); let firmware_area_va = self.object(objects.firmware_area)?.gpu_va(); let firmware_area_2_va = match objects.firmware_area_2 { Some(index) => self.object(index)?.gpu_va(), diff --git a/drivers/gpu/drm/asahi/g17/mod.rs b/drivers/gpu/drm/asahi/g17/mod.rs index ca362da3e93f5..f3dc0d32e5f93 100644 --- a/drivers/gpu/drm/asahi/g17/mod.rs +++ b/drivers/gpu/drm/asahi/g17/mod.rs @@ -31,6 +31,7 @@ pub(crate) use command::Attachments; #[allow(dead_code)] mod render_resources; mod completion; +mod cooling; pub(crate) mod context; mod data_segment; mod dependency; @@ -822,6 +823,8 @@ struct Firmware { /// Render kick ring storage, kept and reused for the life of the device. render_rings: Arc, render_ids: Arc, + // Drain independent thermal callbacks before dropping firmware storage. + cooling: Option, init: initdata::InitData, _render_global: mmu::Vm, _pm_metrics: Arc, @@ -842,6 +845,7 @@ fn retain_session() -> Result { impl Drop for Firmware { fn drop(&mut self) { + drop(self.cooling.take()); if let Some(secondary) = self.secondary.as_mut() { secondary.stop(); } @@ -1629,6 +1633,7 @@ impl Gpu { Self::firmware_variant(dev, &cfg.firmware, (id.num_clusters, active_cores))?; // A build with its own configuration of the SoC runs with that one. let cfg = build_cfg.unwrap_or(cfg); + cooling::admit(dev.as_ref().as_ref(), variant)?; if let Some(calibration) = calibration { calibration.check_cores(&id.core_masks)?; calibration.check_opps(&initdata::InitData::opps(dev, cfg.opps, id.num_clusters)?)?; @@ -2099,6 +2104,7 @@ impl Gpu { pool: object::Pool::new(shared.variant.objects_after_release)?, render_rings: kick::RenderRings::new()?, render_ids: freelist::RenderIds::new()?, + cooling: None, init: initdata::InitData::new(), _render_global: render_global, _pm_metrics: session_metrics, @@ -2303,6 +2309,7 @@ impl Gpu { pool: object::Pool::new(shared.variant.objects_after_release)?, render_rings: kick::RenderRings::new()?, render_ids: freelist::RenderIds::new()?, + cooling: None, init: initdata::InitData::new(), _render_global: render_global, _pm_metrics: Arc::new(pm_metrics, GFP_KERNEL)?, @@ -2334,6 +2341,8 @@ impl Gpu { if let Some(root_entries) = root_entries { session.uat.confirm_firmware_root_entries(root_entries)?; } + session.cooling = + cooling::Cooling::attach(dev.as_ref().as_ref(), shared.variant, &session.init)?; let mailbox: *const Mailbox = &*session.primary.rtkit; session.port.attach( session.init.port_views(mailbox)?, diff --git a/drivers/gpu/drm/asahi/g17/runtime.rs b/drivers/gpu/drm/asahi/g17/runtime.rs index d193f7d64ee93..7dee01d3c206e 100644 --- a/drivers/gpu/drm/asahi/g17/runtime.rs +++ b/drivers/gpu/drm/asahi/g17/runtime.rs @@ -2661,6 +2661,7 @@ impl super::Shared { { let mut state = self.state.lock(); if let Some(firmware) = state.as_deref_mut() { + drop(firmware.cooling.take()); if let Some(secondary) = firmware.secondary.as_mut() { secondary.stop(); } @@ -2689,6 +2690,7 @@ impl super::Shared { let firmware = { let mut state = self.state.lock(); if let Some(firmware) = (*state).as_deref_mut() { + drop(firmware.cooling.take()); if let Some(secondary) = firmware.secondary.as_mut() { secondary.stop(); } diff --git a/drivers/gpu/drm/asahi/g17/runtime/events.rs b/drivers/gpu/drm/asahi/g17/runtime/events.rs index 1eca0cc036fc7..f27bbea38e08a 100644 --- a/drivers/gpu/drm/asahi/g17/runtime/events.rs +++ b/drivers/gpu/drm/asahi/g17/runtime/events.rs @@ -554,6 +554,9 @@ impl crate::g17::Shared { firmware.drain_reports(); } if recover { + if let Some(cooling) = firmware.cooling.as_ref() { + cooling.rearm(); + } firmware.service_runtime_recovery(deferred)?; if !firmware.recovery.pending() && !self.sleep_gate.load(core::sync::atomic::Ordering::Acquire) From dede88c4869cf0fed5e15cacc04eeb15d561b16c Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Thu, 19 Nov 2026 23:17:53 +1100 Subject: [PATCH 4/6] arm64: dts: apple: t8140: make the GPU a cooling device The power ceiling is a property of the GPU and its firmware, not of a board. Mark the T8140 GPU as a cooling device; boards decide whether a thermal zone uses it. Without one the GPU stays at state 0, its firmware's own policy. Signed-off-by: Ryan Murray --- arch/arm64/boot/dts/apple/t8140.dtsi | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/arm64/boot/dts/apple/t8140.dtsi b/arch/arm64/boot/dts/apple/t8140.dtsi index ccf15535d493d..e0015edb34116 100644 --- a/arch/arm64/boot/dts/apple/t8140.dtsi +++ b/arch/arm64/boot/dts/apple/t8140.dtsi @@ -1523,6 +1523,7 @@ operating-points-v2 = <&gpu_opp>; apple,clpc = <&clpc>; apple,pmp-report-entry = <&pmp_report_gfx>; + #cooling-cells = <2>; status = "disabled"; }; From 49967724b85053653120684db75977dd25acf37a Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Thu, 19 Nov 2026 23:17:56 +1100 Subject: [PATCH 5/6] arm64: dts: apple: t8140-j700: cool the GPU from its die temperatures Under a sustained GPU load macOS 26.6.2 lets the hottest GPU key reach about 107.7 C, then holds it at 104.5-106.3 C (mean 105.5 C) with the GPU at 4.3-4.7 W, down from 6.0 W. Linux has no host ceiling and sits at 107.6 C, where the firmware's die limiter holds it, losing 16% of its frame rate on the way. Give each of the eight SMC GPU die temperatures (Tg04 to Tg0e) a zone, so the hottest one decides. Each zone is a ladder of active trips from 102 C to 106 C that steps the GPU ceiling from 60 to 25 percent of its initialized maximum, with 1.5 C of hysteresis; about 104 C asks for the 4.3-4.7 W band macOS settles in. The GPU is not a power actor, so the power allocator runs these trips bang-bang, as it runs fans. There is no critical trip on these zones: the firmware's die limiter and the SMC keep their own protection, and the CPU zones keep theirs. The GPU keys read placeholders while the GPU is off, which the SMC driver reports as no data. Signed-off-by: Ryan Murray --- arch/arm64/boot/dts/apple/t8140-j700.dts | 90 +++++++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/arch/arm64/boot/dts/apple/t8140-j700.dts b/arch/arm64/boot/dts/apple/t8140-j700.dts index 4c69fcbd33063..3075d4ec8963d 100644 --- a/arch/arm64/boot/dts/apple/t8140-j700.dts +++ b/arch/arm64/boot/dts/apple/t8140-j700.dts @@ -12,6 +12,84 @@ #include "t8140.dtsi" #include +/* + * GPU die zones. The SMC publishes eight GPU die temperatures (channels + * 83-90 of smc_hwmon below); each gets a zone, so whichever is hottest + * sets the GPU's power ceiling (the deepest state any zone asks for wins). + * + * Under a sustained GPU load macOS lets the hottest GPU key reach about + * 107.7 C, then holds it at 104.5-106.3 C (mean 105.5 C) with the GPU + * drawing 4.3-4.7 W, down from 6.0 W. Without a host ceiling Linux sits + * at 107.6 C, where the firmware's own die limiter holds it. The ladder + * below steps the GPU's absolute power ceiling (state N = 100 - N percent + * of its initialized maximum power) from 102 C on, so the ceiling lands + * in the 4.3-4.7 W band near 104 C, and reaches the actuator's floor at + * 106 C. The zones are active-trip ladders: the power allocator runs + * them bang-bang, like a fan curve, since the GPU is not a power actor. + * + * The GPU keys read placeholders while the GPU is powered down; the SMC + * driver reports those as no data, and the zones skip those updates. No + * critical trip: the firmware's die limiter and the SMC keep their own + * overtemperature protection, and the CPU zones keep theirs. + */ +#define J700_GPU_ZONE(node, n, ch) \ + node { \ + polling-delay-passive = <500>; \ + polling-delay = <500>; \ + thermal-sensors = <&smc_hwmon ch>; \ + \ + trips { \ + gpu##n##_cap0: trip-cap0 { \ + temperature = <102000>; \ + hysteresis = <1500>; \ + type = "active"; \ + }; \ + gpu##n##_cap1: trip-cap1 { \ + temperature = <103000>; \ + hysteresis = <1500>; \ + type = "active"; \ + }; \ + gpu##n##_cap2: trip-cap2 { \ + temperature = <104000>; \ + hysteresis = <1500>; \ + type = "active"; \ + }; \ + gpu##n##_cap3: trip-cap3 { \ + temperature = <105000>; \ + hysteresis = <1500>; \ + type = "active"; \ + }; \ + gpu##n##_cap4: trip-cap4 { \ + temperature = <106000>; \ + hysteresis = <1500>; \ + type = "active"; \ + }; \ + }; \ + \ + cooling-maps { \ + map0 { \ + trip = <&gpu##n##_cap0>; \ + cooling-device = <&gpu 40 40>; \ + }; \ + map1 { \ + trip = <&gpu##n##_cap1>; \ + cooling-device = <&gpu 48 48>; \ + }; \ + map2 { \ + trip = <&gpu##n##_cap2>; \ + cooling-device = <&gpu 55 55>; \ + }; \ + map3 { \ + trip = <&gpu##n##_cap3>; \ + cooling-device = <&gpu 62 62>; \ + }; \ + map4 { \ + trip = <&gpu##n##_cap4>; \ + cooling-device = <&gpu 75 75>; \ + }; \ + }; \ + } + / { compatible = "apple,j700", "apple,t8140", "apple,arm-platform"; model = "Apple MacBook Neo"; @@ -87,7 +165,8 @@ }; /* - * The J700 is fanless: the CPUs are the only cooling devices. Every + * The J700 is fanless: the CPUs and the GPU are the only cooling + * devices. The GPU zones follow the CPU ones. Every CPU * die sensor gets a zone that throttles both clusters, since they share * one die and one heat spreader. The power allocator splits the budget * by the energy models, with the E cluster weighted three to one: its @@ -405,6 +484,15 @@ }; }; }; + + J700_GPU_ZONE(gpu0-thermal, gpu0, 83); /* Tg04 */ + J700_GPU_ZONE(gpu1-thermal, gpu1, 84); /* Tg05 */ + J700_GPU_ZONE(gpu2-thermal, gpu2, 85); /* Tg0C */ + J700_GPU_ZONE(gpu3-thermal, gpu3, 86); /* Tg0D */ + J700_GPU_ZONE(gpu4-thermal, gpu4, 87); /* Tg0K */ + J700_GPU_ZONE(gpu5-thermal, gpu5, 88); /* Tg0L */ + J700_GPU_ZONE(gpu6-thermal, gpu6, 89); /* Tg0d */ + J700_GPU_ZONE(gpu7-thermal, gpu7, 90); /* Tg0e */ }; /* From e9aa6282363cf2405f1ad4c57785a2c9ae426f39 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 04:21:53 +1100 Subject: [PATCH 6/6] drm/asahi: g17: keep the GPU running on firmware without a power layout The GPU firmware keeps its own die limiter, so the host ceiling is an addition to it, not the only protection. A GPU node with #cooling-cells on a firmware build whose power inputs are not described refused to start, which took the whole GPU away for a missing extra. Warn once and run without the cooling device instead; the thermal zones then have no GPU ceiling to drive. Signed-off-by: Ryan Murray --- Documentation/gpu/asahi-gpu-thermal.rst | 6 +++--- drivers/gpu/drm/asahi/g17/cooling.rs | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Documentation/gpu/asahi-gpu-thermal.rst b/Documentation/gpu/asahi-gpu-thermal.rst index 2b75de8377b86..fee1d8dec7331 100644 --- a/Documentation/gpu/asahi-gpu-thermal.rst +++ b/Documentation/gpu/asahi-gpu-thermal.rst @@ -30,9 +30,9 @@ Firmware builds The driver writes the power inputs only where the running build's layout is described (``Variant::live_power`` in ``g17/fw/variant.rs``): the A18 Pro -(26A428) and M4 Pro (25G83) builds. A GPU the device tree makes a cooling -device does not start on any other build, rather than run with the thermal -zones silently ignored. The M5 firmware takes its host budget at a different +(26A428) and M4 Pro (25G83) builds. On any other build the GPU still starts +under the firmware's own die limiter, warns once, and does not register as a +cooling device, so its thermal zones get no GPU ceiling. The M5 firmware takes its host budget at a different Region C offset, which its own governor (``g17/thermal.rs``) writes; moving that governor onto this cooling device is separate work. diff --git a/drivers/gpu/drm/asahi/g17/cooling.rs b/drivers/gpu/drm/asahi/g17/cooling.rs index c9fbe92e2cb61..1f531626dd92d 100644 --- a/drivers/gpu/drm/asahi/g17/cooling.rs +++ b/drivers/gpu/drm/asahi/g17/cooling.rs @@ -47,14 +47,12 @@ fn required(dev: &device::Device) -> Result { } } -/// Check before starting either processor. A GPU the device tree makes a cooling device must -/// not run without its ceiling: a build whose power inputs are not described here -/// ([`Variant::live_power`] is `None`) refuses to start rather than silently -/// ignoring the thermal zones. +/// Check before starting either processor. The firmware keeps its own die limiter, so a build +/// whose power inputs are not described here ([`Variant::live_power`] is `None`) still runs; the +/// GPU just does not become a cooling device and its thermal zones get no GPU ceiling. pub(super) fn admit(dev: &device::Device, variant: &Variant) -> Result { if required(dev)? && variant.live_power.is_none() { - dev_err!(dev, "GPU live power layout not admitted\n"); - return Err(EOPNOTSUPP); + dev_warn!(dev, "GPU live power layout unknown for this firmware, no GPU cooling device\n"); } Ok(()) } @@ -81,7 +79,9 @@ impl Cooling { if !required(dev)? { return Ok(None); } - let layout = variant.live_power.as_ref().ok_or(EOPNOTSUPP)?; + let Some(layout) = variant.live_power.as_ref() else { + return Ok(None); + }; let words = init.cooling_words(layout)?; // SAFETY: The owner keeps all validated shared words mapped until after // Drop drains the C callbacks. Registration republishes the initialized