diff --git a/Documentation/hwmon/macsmc-hwmon.rst b/Documentation/hwmon/macsmc-hwmon.rst index 46a6d504b7f5c..eded79f67d421 100644 --- a/Documentation/hwmon/macsmc-hwmon.rst +++ b/Documentation/hwmon/macsmc-hwmon.rst @@ -49,7 +49,9 @@ the ``temperature-*`` children, in list order, with labels ``SoC die ``. Only readable four-byte floating-point keys are used. When thermal support is enabled, their hottest reading supplies the ``macsmc_soc_die`` thermal zone. A failed or implausible reading fails the zone read rather than reporting a -partial result. Keys absent from the firmware are skipped. +partial result. Keys absent from the firmware are skipped. On the MacBook Air +(15-inch, M3, ``apple,j615``) the zone is registered only when every listed +key is present. With ``apple,discover-temperatures`` the listed keys add no channels: the discovered channel of each listed key, labelled with the key, joins the zone. diff --git a/arch/arm64/boot/dts/apple/t8122-j504.dts b/arch/arm64/boot/dts/apple/t8122-j504.dts index f8258ba3a2f54..f290f01120677 100644 --- a/arch/arm64/boot/dts/apple/t8122-j504.dts +++ b/arch/arm64/boot/dts/apple/t8122-j504.dts @@ -39,13 +39,23 @@ &dcp { panel: panel { compatible = "apple,panel-j504", "apple,panel-mini-led", "apple,panel"; + /* + * The J514S panel: 3024x1964 at 254 ppi; 189 mm high without the + * 74 notch rows. The J504 boot framebuffer is 3024x1890 below them. + */ width-mm = <302>; height-mm = <196>; adj-height-mm = <189>; - apple,max-brightness = <600>; + apple,max-brightness = <500>; }; }; +/* The boot framebuffer is the same panel, as on the J613 (HiDPI scale for simpledrm). */ +&framebuffer0 { + panel = <&panel>; + post-init-providers = <&panel>; +}; + &wifi0 { brcm,board-type = "apple,tresco"; }; diff --git a/arch/arm64/boot/dts/apple/t8122-j615.dts b/arch/arm64/boot/dts/apple/t8122-j615.dts index 8c3add040f8d9..8d92960130239 100644 --- a/arch/arm64/boot/dts/apple/t8122-j615.dts +++ b/arch/arm64/boot/dts/apple/t8122-j615.dts @@ -11,10 +11,322 @@ #include "t8122.dtsi" #include "t8122-j613-j615.dtsi" +#include / { compatible = "apple,j615", "apple,t8122", "apple,arm-platform"; model = "Apple MacBook Air (15-inch, M3, 2024)"; + + /* + * The J615 is fanless like the J613, on the same die with the same + * six CPU die sensors, so it takes the J613's zones unchanged: one + * zone per sensor, each throttling both clusters through the power + * allocator, E cluster weighted three to one and capped at cooling + * state 3 (2004 MHz); switch-on 58 C, control 95 C, critical 110 C. + * + * Without zones nothing limits the CPU: eight busy cores passed 105 C + * in 150 s, still rising, and a -j8 kernel build held 110-114.6 C for + * 22 minutes. sustainable-power was checked on a J615: with 6 W eight + * busy cores peaked at 87.7 C with the P cores at 3.1-3.6 GHz, and a + * 20 minute -j8 kernel build held a level 91-92 C at 2.9 GHz (peak + * 97.2 C, 6 of 545 samples at or above 95 C). 8 W peaked at 96.3 C + * under the eight-core load with no gain in P-core frequency. + */ + thermal-zones { + cpu-e0-thermal { + polling-delay-passive = <500>; + polling-delay = <500>; + thermal-sensors = <&smc_hwmon 0>; /* Te05 */ + sustainable-power = <6000>; + + trips { + cpu_e0_alert: trip-alert { + temperature = <58000>; + hysteresis = <2000>; + type = "passive"; + }; + + cpu_e0_control: trip-control { + temperature = <95000>; + hysteresis = <2000>; + type = "passive"; + }; + + trip-critical { + temperature = <110000>; + hysteresis = <0>; + type = "critical"; + }; + }; + + cooling-maps { + map0 { + trip = <&cpu_e0_control>; + cooling-device = + <&cpu_p0 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>, + <&cpu_p1 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>, + <&cpu_p2 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>, + <&cpu_p3 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>; + contribution = <1024>; + }; + + map1 { + trip = <&cpu_e0_control>; + cooling-device = + <&cpu_e0 THERMAL_NO_LIMIT 3>, + <&cpu_e1 THERMAL_NO_LIMIT 3>, + <&cpu_e2 THERMAL_NO_LIMIT 3>, + <&cpu_e3 THERMAL_NO_LIMIT 3>; + contribution = <3072>; + }; + }; + }; + + cpu-e1-thermal { + polling-delay-passive = <500>; + polling-delay = <500>; + thermal-sensors = <&smc_hwmon 1>; /* Te0P */ + sustainable-power = <6000>; + + trips { + cpu_e1_alert: trip-alert { + temperature = <58000>; + hysteresis = <2000>; + type = "passive"; + }; + + cpu_e1_control: trip-control { + temperature = <95000>; + hysteresis = <2000>; + type = "passive"; + }; + + trip-critical { + temperature = <110000>; + hysteresis = <0>; + type = "critical"; + }; + }; + + cooling-maps { + map0 { + trip = <&cpu_e1_control>; + cooling-device = + <&cpu_p0 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>, + <&cpu_p1 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>, + <&cpu_p2 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>, + <&cpu_p3 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>; + contribution = <1024>; + }; + + map1 { + trip = <&cpu_e1_control>; + cooling-device = + <&cpu_e0 THERMAL_NO_LIMIT 3>, + <&cpu_e1 THERMAL_NO_LIMIT 3>, + <&cpu_e2 THERMAL_NO_LIMIT 3>, + <&cpu_e3 THERMAL_NO_LIMIT 3>; + contribution = <3072>; + }; + }; + }; + + cpu-p0-thermal { + polling-delay-passive = <500>; + polling-delay = <500>; + thermal-sensors = <&smc_hwmon 2>; /* Tp05 */ + sustainable-power = <6000>; + + trips { + cpu_p0_alert: trip-alert { + temperature = <58000>; + hysteresis = <2000>; + type = "passive"; + }; + + cpu_p0_control: trip-control { + temperature = <95000>; + hysteresis = <2000>; + type = "passive"; + }; + + trip-critical { + temperature = <110000>; + hysteresis = <0>; + type = "critical"; + }; + }; + + cooling-maps { + map0 { + trip = <&cpu_p0_control>; + cooling-device = + <&cpu_p0 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>, + <&cpu_p1 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>, + <&cpu_p2 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>, + <&cpu_p3 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>; + contribution = <1024>; + }; + + map1 { + trip = <&cpu_p0_control>; + cooling-device = + <&cpu_e0 THERMAL_NO_LIMIT 3>, + <&cpu_e1 THERMAL_NO_LIMIT 3>, + <&cpu_e2 THERMAL_NO_LIMIT 3>, + <&cpu_e3 THERMAL_NO_LIMIT 3>; + contribution = <3072>; + }; + }; + }; + + cpu-p1-thermal { + polling-delay-passive = <500>; + polling-delay = <500>; + thermal-sensors = <&smc_hwmon 3>; /* Tp0L */ + sustainable-power = <6000>; + + trips { + cpu_p1_alert: trip-alert { + temperature = <58000>; + hysteresis = <2000>; + type = "passive"; + }; + + cpu_p1_control: trip-control { + temperature = <95000>; + hysteresis = <2000>; + type = "passive"; + }; + + trip-critical { + temperature = <110000>; + hysteresis = <0>; + type = "critical"; + }; + }; + + cooling-maps { + map0 { + trip = <&cpu_p1_control>; + cooling-device = + <&cpu_p0 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>, + <&cpu_p1 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>, + <&cpu_p2 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>, + <&cpu_p3 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>; + contribution = <1024>; + }; + + map1 { + trip = <&cpu_p1_control>; + cooling-device = + <&cpu_e0 THERMAL_NO_LIMIT 3>, + <&cpu_e1 THERMAL_NO_LIMIT 3>, + <&cpu_e2 THERMAL_NO_LIMIT 3>, + <&cpu_e3 THERMAL_NO_LIMIT 3>; + contribution = <3072>; + }; + }; + }; + + cpu-p2-thermal { + polling-delay-passive = <500>; + polling-delay = <500>; + thermal-sensors = <&smc_hwmon 4>; /* Tp0b */ + sustainable-power = <6000>; + + trips { + cpu_p2_alert: trip-alert { + temperature = <58000>; + hysteresis = <2000>; + type = "passive"; + }; + + cpu_p2_control: trip-control { + temperature = <95000>; + hysteresis = <2000>; + type = "passive"; + }; + + trip-critical { + temperature = <110000>; + hysteresis = <0>; + type = "critical"; + }; + }; + + cooling-maps { + map0 { + trip = <&cpu_p2_control>; + cooling-device = + <&cpu_p0 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>, + <&cpu_p1 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>, + <&cpu_p2 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>, + <&cpu_p3 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>; + contribution = <1024>; + }; + + map1 { + trip = <&cpu_p2_control>; + cooling-device = + <&cpu_e0 THERMAL_NO_LIMIT 3>, + <&cpu_e1 THERMAL_NO_LIMIT 3>, + <&cpu_e2 THERMAL_NO_LIMIT 3>, + <&cpu_e3 THERMAL_NO_LIMIT 3>; + contribution = <3072>; + }; + }; + }; + + cpu-p3-thermal { + polling-delay-passive = <500>; + polling-delay = <500>; + thermal-sensors = <&smc_hwmon 5>; /* Tp0r */ + sustainable-power = <6000>; + + trips { + cpu_p3_alert: trip-alert { + temperature = <58000>; + hysteresis = <2000>; + type = "passive"; + }; + + cpu_p3_control: trip-control { + temperature = <95000>; + hysteresis = <2000>; + type = "passive"; + }; + + trip-critical { + temperature = <110000>; + hysteresis = <0>; + type = "critical"; + }; + }; + + cooling-maps { + map0 { + trip = <&cpu_p3_control>; + cooling-device = + <&cpu_p0 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>, + <&cpu_p1 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>, + <&cpu_p2 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>, + <&cpu_p3 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>; + contribution = <1024>; + }; + + map1 { + trip = <&cpu_p3_control>; + cooling-device = + <&cpu_e0 THERMAL_NO_LIMIT 3>, + <&cpu_e1 THERMAL_NO_LIMIT 3>, + <&cpu_e2 THERMAL_NO_LIMIT 3>, + <&cpu_e3 THERMAL_NO_LIMIT 3>; + contribution = <3072>; + }; + }; + }; + }; }; &dcp { diff --git a/drivers/gpu/drm/apple/iomfb_v14_7.c b/drivers/gpu/drm/apple/iomfb_v14_7.c index a99bfed67183f..b520b0a2c3afa 100644 --- a/drivers/gpu/drm/apple/iomfb_v14_7.c +++ b/drivers/gpu/drm/apple/iomfb_v14_7.c @@ -140,6 +140,31 @@ static const struct dcp_v14_board dcp_v14_board_j615 = { .ctm_get = A(420), }; +/* + * J504 (MacBook Pro 14", M3): the J613 DCP image on the same T8122 DCP (the + * J504 ADT names it), driving the 14" mini-LED panel of the J514S: 3024x1964 + * with 74 notch rows above the 3024x1890 boot framebuffer, at 60 or 120 Hz. + * The colour matrix methods are this image's, and so is the panel level + * block the J613 qualified (brightness in nits, 500 at most here). Its swaps + * keep zero timestamps as on the Airs. Every panel timing and the + * display clock are logged for the first boots. + */ +static const struct dcp_v14_board dcp_v14_board_j504 = { + .name = "J504", + .debugfs = "dcp-j504", + .dcp_compatible = "apple,t8122-dcp", + .machine = "apple,j504", + .firmware_uuid = DCP_V14_J613_FIRMWARE_UUID, + .handoff = "apple,t8122-handoff", + .panel_width = 3024, + .panel_height = 1964, + .promotion = true, + .log_bringup = true, + .panel_nits = true, + .ctm_set = A(421), + .ctm_get = A(420), +}; + /* * J516C (MacBook Pro 16", M3 Max): the J516S panel, 3456x2234 with 74 notch * rows above the 3456x2160 boot framebuffer, at 60 or 120 Hz, on the T6031 @@ -193,6 +218,7 @@ static const struct dcp_v14_board *const dcp_v14_boards[] = { &dcp_v14_board_t6030, &dcp_v14_board_j613, &dcp_v14_board_j615, + &dcp_v14_board_j504, &dcp_v14_board_j516c, &dcp_v14_board_j514c, }; @@ -1694,7 +1720,8 @@ static bool dcp_v14_panel_nits_admit(const struct dcp_v14_board *board, { return board && board->panel_nits && (machine_is(ctx, "apple,j514s") || machine_is(ctx, "apple,j516s") || - machine_is(ctx, "apple,j516c") || machine_is(ctx, "apple,j613")); + machine_is(ctx, "apple,j516c") || machine_is(ctx, "apple,j613") || + machine_is(ctx, "apple,j504")); } const char *iomfb_v14_7_board_name(const struct dcp_v14_board *board) diff --git a/drivers/gpu/drm/apple/iomfb_v14_7_backlight_test.c b/drivers/gpu/drm/apple/iomfb_v14_7_backlight_test.c index 3c1fc2f45d534..6f1b6ad2bfe96 100644 --- a/drivers/gpu/drm/apple/iomfb_v14_7_backlight_test.c +++ b/drivers/gpu/drm/apple/iomfb_v14_7_backlight_test.c @@ -122,13 +122,14 @@ static bool v14_bl_ctx_machine_is(const void *ctx, const char *compat) return !strcmp(((const char *const *)ctx)[1], compat); } -/* Only admitted T6030, J516C and J613 internal panels carry levels. */ +/* Only admitted T6030, J516C, J613 and J504 internal panels carry levels. */ static void v14_bl_boards(struct kunit *test) { static const char *const t6030_j516s[] = { "apple,t6030-dcp", "apple,j516s" }; static const char *const t6030_j514s[] = { "apple,t6030-dcp", "apple,j514s" }; static const char *const j613[] = { "apple,t8122-dcp", "apple,j613" }; static const char *const j615[] = { "apple,t8122-dcp", "apple,j615" }; + static const char *const j504[] = { "apple,t8122-dcp", "apple,j504" }; static const char *const j516c[] = { "apple,t6031-dcp", "apple,j516c" }; static const char *const other_t6030[] = { "apple,t6030-dcp", "apple,other" }; const struct dcp_v14_board *board; @@ -156,6 +157,11 @@ static void v14_bl_boards(struct kunit *test) board = dcp_v14_board_select(v14_bl_ctx_dcp_is, v14_bl_ctx_machine_is, j615, &known); KUNIT_ASSERT_NOT_NULL(test, board); KUNIT_EXPECT_FALSE(test, board->panel_nits); + board = dcp_v14_board_select(v14_bl_ctx_dcp_is, v14_bl_ctx_machine_is, j504, &known); + KUNIT_ASSERT_NOT_NULL(test, board); + KUNIT_EXPECT_TRUE(test, board->panel_nits); + KUNIT_EXPECT_TRUE(test, dcp_v14_panel_nits_admit(board, v14_bl_ctx_machine_is, j504)); + KUNIT_EXPECT_FALSE(test, dcp_v14_panel_nits_admit(board, v14_bl_ctx_machine_is, j615)); board = dcp_v14_board_select(v14_bl_ctx_dcp_is, v14_bl_ctx_machine_is, j516c, &known); KUNIT_ASSERT_NOT_NULL(test, board); KUNIT_EXPECT_TRUE(test, board->panel_nits); diff --git a/drivers/gpu/drm/apple/v14-board-test.c b/drivers/gpu/drm/apple/v14-board-test.c index f9b84ab36a7cc..d79c2b79a9cbc 100644 --- a/drivers/gpu/drm/apple/v14-board-test.c +++ b/drivers/gpu/drm/apple/v14-board-test.c @@ -54,6 +54,8 @@ static const char *const j514c[] = { "apple,j514c", "apple,t6031", "apple,arm-pl static const char *const j516m[] = { "apple,j516m", "apple,t6034", "apple,arm-platform", NULL }; static const char *const j516s[] = { "apple,j516s", "apple,t6030", "apple,arm-platform", NULL }; static const char *const j613[] = { "apple,j613", "apple,t8122", "apple,arm-platform", NULL }; +static const char *const j504[] = { "apple,j504", "apple,t8122", "apple,arm-platform", NULL }; +static const char *const j433[] = { "apple,j433", "apple,t8122", "apple,arm-platform", NULL }; static void v14_board_j516c(struct kunit *test) { @@ -247,7 +249,46 @@ static void v14_swap_timestamps_encoding(struct kunit *test) } } +/* + * J504 (14" M3 MacBook Pro): the J613 image on the T8122 DCP, its own 3024x1964 + * ProMotion panel, the qualified level block (brightness) and unstamped swaps. The other T8122 + * Macs (J433, J434) have no record. + */ +static void v14_board_j504(struct kunit *test) +{ + const struct dcp_v14_board *b, *air; + bool known; + + b = board_for("apple,t8122-dcp", j504, &known); + KUNIT_ASSERT_NOT_NULL(test, b); + air = board_for("apple,t8122-dcp", j613, &known); + KUNIT_ASSERT_NOT_NULL(test, air); + KUNIT_EXPECT_PTR_NE(test, b, air); + KUNIT_EXPECT_STREQ(test, b->name, "J504"); + KUNIT_EXPECT_STREQ(test, b->machine, "apple,j504"); + KUNIT_EXPECT_STREQ(test, b->handoff, "apple,t8122-handoff"); + KUNIT_EXPECT_STREQ(test, b->firmware_uuid, J613_DCP_IMAGE); + KUNIT_EXPECT_EQ(test, b->panel_width, 3024); + KUNIT_EXPECT_EQ(test, b->panel_height, 1964); + KUNIT_EXPECT_TRUE(test, b->promotion); + KUNIT_EXPECT_TRUE(test, b->log_bringup); + KUNIT_EXPECT_TRUE(test, b->panel_nits); + KUNIT_EXPECT_FALSE(test, b->base_edid); + KUNIT_EXPECT_EQ(test, b->ctm_set, air->ctm_set); + KUNIT_EXPECT_EQ(test, b->ctm_get, air->ctm_get); + KUNIT_EXPECT_TRUE(test, dcp_v14_board_admits(b, J613_DCP_IMAGE)); + KUNIT_EXPECT_FALSE(test, dcp_v14_board_admits(b, T6030_DCP_IMAGE)); + KUNIT_EXPECT_FALSE(test, dcp_v14_board_admits(b, J516C_DCP_IMAGE)); + KUNIT_EXPECT_FALSE(test, dcp_v14_board_stamps_swaps(b, false)); + + KUNIT_EXPECT_NULL(test, board_for("apple,t8122-dcp", j433, &known)); + KUNIT_EXPECT_TRUE(test, known); + /* A J504 root with another SoC's DCP gets no J504 record. */ + KUNIT_EXPECT_NULL(test, board_for("apple,t6031-dcp", j504, &known)); +} + static struct kunit_case v14_board_cases[] = { + KUNIT_CASE(v14_board_j504), KUNIT_CASE(v14_board_j516c), KUNIT_CASE(v14_board_j514c), KUNIT_CASE(v14_board_t6031_other_boards), diff --git a/drivers/gpu/drm/asahi/Kconfig b/drivers/gpu/drm/asahi/Kconfig index d383802ee8825..0df72bf32ae5b 100644 --- a/drivers/gpu/drm/asahi/Kconfig +++ b/drivers/gpu/drm/asahi/Kconfig @@ -79,11 +79,21 @@ config DRM_ASAHI_DEBUG_ALLOCATOR Say N unless you are debugging firmware structures or porting to a new firmware version. +config DRM_ASAHI_UAT_ADOPTION + bool + help + The firmware UAT handoff adoption shared by the G15 backend and the + T8122 runtime (g15/uat.rs, g15/regs.rs). + config DRM_ASAHI_M3_T8122 bool "M3 MacBook Air GPU runtime (T8122, 14.8.3 firmware)" depends on DRM_ASAHI - # The runtime adopts the firmware's UAT through the G15 backend's handoff helpers. - depends on DRM_ASAHI_G15 + # The runtime adopts the firmware's UAT as the G15 backend does, without + # needing that backend: M3 Pro/Max keep their own setting. + # Its PMP votes call apple-pmp directly (m3_device.rs). + depends on APPLE_PMP=y || (APPLE_PMP=m && DRM_ASAHI=m) + select DRM_ASAHI_UAT_ADOPTION + select APPLE_T8122_GPU_PMP_LINK select CRYPTO_LIB_SHA256 default n help @@ -101,6 +111,7 @@ config DRM_ASAHI_G15 depends on DRM_APPLE=y || (DRM_APPLE=m && DRM_ASAHI=m) select APPLE_T6030_GPU_PMP_LINK select CRYPTO_LIB_SHA256 + select DRM_ASAHI_UAT_ADOPTION default y help Backend for the M3 Pro (T6030) and M3 Max (T6031) GPUs on the diff --git a/drivers/gpu/drm/asahi/Makefile b/drivers/gpu/drm/asahi/Makefile index 890d47c40af6b..b39d2552dd900 100644 --- a/drivers/gpu/drm/asahi/Makefile +++ b/drivers/gpu/drm/asahi/Makefile @@ -5,6 +5,7 @@ aurora-y := asahi_core.o fwid.o legacy_sched.o sysfs.o g17/sync_input.o g17/sync CFLAGS_g17/trace.o := -I$(src)/g17 aurora-$(CONFIG_DRM_ASAHI_G15) += g15/glue.o +aurora-$(CONFIG_DRM_ASAHI_UAT_ADOPTION) += g15/uat_glue.o aurora-$(CONFIG_DRM_ASAHI_G16) += g16/glue.o diff --git a/drivers/gpu/drm/asahi/asahi_core.rs b/drivers/gpu/drm/asahi/asahi_core.rs index b970691651032..bd69152c3ce75 100644 --- a/drivers/gpu/drm/asahi/asahi_core.rs +++ b/drivers/gpu/drm/asahi/asahi_core.rs @@ -17,6 +17,10 @@ mod fw; mod fwid; #[cfg(CONFIG_DRM_ASAHI_G15)] mod g15; +// Without the G15 backend, only the UAT adoption the T8122 runtime shares with it. +#[cfg(all(CONFIG_DRM_ASAHI_UAT_ADOPTION, not(CONFIG_DRM_ASAHI_G15)))] +#[path = "g15/adopt.rs"] +mod g15; mod g17; mod gem; diff --git a/drivers/gpu/drm/asahi/g15/adopt.rs b/drivers/gpu/drm/asahi/g15/adopt.rs new file mode 100644 index 0000000000000..5dd54b04aaec6 --- /dev/null +++ b/drivers/gpu/drm/asahi/g15/adopt.rs @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT + +//! Without the G15 backend (`CONFIG_DRM_ASAHI_G15=n`), the parts of it the T8122 runtime uses to +//! adopt the firmware's UAT: the handoff window ([`uat`]), its mappings ([`regs`]) and the +//! hardware constants they name ([`hw`]). Nothing here probes a device. + +// Only the adoption is used; the rest of these files serves the G15 backend. +#![allow(dead_code)] + +// hw.rs's `t6031` submodule is cfg-gated on G15; were it built here, a #[path]-loaded file +// would look for it at g15/t6031.rs, not g15/hw/t6031.rs. +#[path = "hw.rs"] +pub(crate) mod hw; +#[path = "regs.rs"] +pub(crate) mod regs; +#[path = "uat.rs"] +pub(crate) mod uat; + +use kernel::prelude::*; + +extern "C" { + fn asahi_g15_memory_mode(start: u64, size: u64) -> u32; +} + +/// WB for complete reserved RAM, WC for aligned non-RAM carveouts; reject everything else. +pub(crate) fn memory_mode(start: u64, size: u64) -> Result { + // SAFETY: Only queries resources and page metadata; it does not create a mapping. + match unsafe { asahi_g15_memory_mode(start, size) } { + 0 => Err(EINVAL), + mode => Ok(mode), + } +} diff --git a/drivers/gpu/drm/asahi/g15/glue.c b/drivers/gpu/drm/asahi/g15/glue.c index 2a0346228c1e5..a12a5e6fa2fe7 100644 --- a/drivers/gpu/drm/asahi/g15/glue.c +++ b/drivers/gpu/drm/asahi/g15/glue.c @@ -29,9 +29,6 @@ void asahi_g15_release_power(struct device *dev); void asahi_g15_reprobe(void); void asahi_g15_report_loader_gpu(void); int asahi_g15_adt_phram(u64 *start, u64 *size); -bool asahi_g15_ram_disjoint(u64 start, u64 size); -bool asahi_g15_ram_within(u64 start, u64 size); -unsigned int asahi_g15_memory_mode(u64 start, u64 size); void __iomem *asahi_g15_ioremap_wc(u64 start, size_t size); bool asahi_g15_dma_coherent(struct device *dev); int asahi_g15_pmp_prepare(struct device *dev); @@ -275,13 +272,6 @@ int asahi_g15_adt_phram(u64 *start, u64 *size) return 0; } -/* The range is disjoint from System RAM (ADT region admission). */ -bool asahi_g15_ram_disjoint(u64 start, u64 size) -{ - return region_intersects(start, size, IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE) == - REGION_DISJOINT; -} - /* * Whether DMA mappings of the device skip cache maintenance. The G15 firmware * objects rely on the DMA mapping cleaning their pages to the point of @@ -292,42 +282,6 @@ bool asahi_g15_dma_coherent(struct device *dev) return dev_is_dma_coherent(dev); } -/* - * Every page must be valid, System RAM and still reserved from the allocator. - * REGION_INTERSECTS alone also accepts RAM mixed with undefined memory holes. - */ -bool asahi_g15_ram_within(u64 start, u64 size) -{ - u64 end, pa; - - if (!size || !IS_ALIGNED(start, PAGE_SIZE) || - !IS_ALIGNED(size, PAGE_SIZE) || check_add_overflow(start, size, &end)) - return false; - for (pa = start; pa < end; pa += PAGE_SIZE) { - unsigned long pfn = PHYS_PFN(pa); - - if (!pfn_valid(pfn) || !page_is_ram(pfn) || - !PageReserved(pfn_to_page(pfn))) - return false; - } - return true; -} - -/* One classification for every adopted-UAT and firmware-region CPU mapping. */ -unsigned int asahi_g15_memory_mode(u64 start, u64 size) -{ - u64 end; - - if (!size || !IS_ALIGNED(start, PAGE_SIZE) || - !IS_ALIGNED(size, PAGE_SIZE) || check_add_overflow(start, size, &end)) - return 0; - if (asahi_g15_ram_within(start, size)) - return MEMREMAP_WB; - if (asahi_g15_ram_disjoint(start, size)) - return MEMREMAP_WC; - return 0; -} - /* The inherited RTKit buffer inside firmware DATA. */ void __iomem *asahi_g15_ioremap_wc(u64 start, size_t size) { diff --git a/drivers/gpu/drm/asahi/g15/hw.rs b/drivers/gpu/drm/asahi/g15/hw.rs index b211088310210..cac11c55d3ca0 100644 --- a/drivers/gpu/drm/asahi/g15/hw.rs +++ b/drivers/gpu/drm/asahi/g15/hw.rs @@ -7,6 +7,7 @@ use core::ops::Range; +#[cfg(CONFIG_DRM_ASAHI_G15)] pub(crate) mod t6031; /// Identification register values of a qualified G15S. @@ -91,6 +92,7 @@ pub(crate) struct ComputeRegs { } /// The T6030 configuration. +#[cfg(CONFIG_DRM_ASAHI_G15)] pub(crate) mod t6030 { use super::super::Config; use super::{ComputeRegs, FirmwareBuild, HwId, Layout}; @@ -175,6 +177,69 @@ pub(crate) mod t6030 { 0xe7, 0x9b, 0xb9, 0xb0, 0x22, 0x09, 0xdb, 0x53, ], }, + // J514S firmware measured on the 14.7 boot chain; desktop rendering + // was verified with the bring-up prefix override (issue #247). + FirmwareBuild { + name: "J514S cd05 firmware (14.7 boot chain)", + machine: Some(kernel::c_str!("apple,j514s")), + text_size: 0x5c000, + data_size: 0x114000, + marker: 0x58e88, + text_sha256: [ + 0xcd, 0x05, 0xaf, 0x6a, 0x82, 0x8b, 0x52, 0xe4, + 0xc2, 0xfe, 0xd2, 0x00, 0x86, 0xd3, 0xd1, 0x8a, + 0x7d, 0xb4, 0x2e, 0x03, 0xa8, 0xfd, 0x6f, 0x83, + 0x54, 0xf9, 0xf0, 0x6e, 0x0a, 0x61, 0x36, 0x3e, + ], + }, + // Some J516S units on the same 14.7 boot chain load a different B0 + // TEXT image with identical sizes and marker. Pin its complete + // measured digest for the J516S only. + FirmwareBuild { + name: "J516S cd05 firmware (14.7 boot chain)", + machine: Some(kernel::c_str!("apple,j516s")), + text_size: 0x5c000, + data_size: 0x114000, + marker: 0x58e88, + text_sha256: [ + 0xcd, 0x05, 0xaf, 0x6a, 0x82, 0x8b, 0x52, 0xe4, + 0xc2, 0xfe, 0xd2, 0x00, 0x86, 0xd3, 0xd1, 0x8a, + 0x7d, 0xb4, 0x2e, 0x03, 0xa8, 0xfd, 0x6f, 0x83, + 0x54, 0xf9, 0xf0, 0x6e, 0x0a, 0x61, 0x36, 0x3e, + ], + }, + // J514S (14-inch) firmware measured on the 14.7 boot chain with + // system firmware 26.6.2; the GPU reached READY with the bring-up + // prefix override (issue #247). + FirmwareBuild { + name: "J514S 4118 firmware (14.7 boot chain)", + machine: Some(kernel::c_str!("apple,j514s")), + text_size: 0x5c000, + data_size: 0x114000, + marker: 0x58e88, + text_sha256: [ + 0x41, 0x18, 0x33, 0xa7, 0xde, 0xd7, 0x73, 0xfe, + 0xea, 0xc4, 0xa1, 0x81, 0x55, 0x2e, 0x3f, 0xf5, + 0x58, 0x2e, 0xaf, 0x44, 0x35, 0xf2, 0xdd, 0x8f, + 0x62, 0x0c, 0x1e, 0xd0, 0xb3, 0x1d, 0xf1, 0xb0, + ], + }, + // J516S (16-inch) firmware measured on the 14.7 boot chain with + // system firmware 26.6.2; the GPU reached READY with the bring-up + // prefix override (issue #247). + FirmwareBuild { + name: "J516S 4118 firmware (14.7 boot chain)", + machine: Some(kernel::c_str!("apple,j516s")), + text_size: 0x5c000, + data_size: 0x114000, + marker: 0x58e88, + text_sha256: [ + 0x41, 0x18, 0x33, 0xa7, 0xde, 0xd7, 0x73, 0xfe, + 0xea, 0xc4, 0xa1, 0x81, 0x55, 0x2e, 0x3f, 0xf5, + 0x58, 0x2e, 0xaf, 0x44, 0x35, 0xf2, 0xdd, 0x8f, + 0x62, 0x0c, 0x1e, 0xd0, 0xb3, 0x1d, 0xf1, 0xb0, + ], + }, ], machine: kernel::c_str!("apple,t6030"), platform: super::Platform::AdtWithPmp, diff --git a/drivers/gpu/drm/asahi/g15/mod.rs b/drivers/gpu/drm/asahi/g15/mod.rs index 4cb221fb0a5df..9553dc46f3c55 100644 --- a/drivers/gpu/drm/asahi/g15/mod.rs +++ b/drivers/gpu/drm/asahi/g15/mod.rs @@ -202,8 +202,11 @@ impl platform::Driver for Driver<'_> { // after probe_g15 started firmware would return an unbound device. try_pin_init!(Driver { _driver: { + // Not EPROBE_DEFER: a deferred device stays listed in devices_deferred until the + // deferred-probe work runs, even after activate()'s probe fails for good. Activation + // reprobes every unbound device; a device added later sees MODULE_READY set. if !MODULE_READY.load(Ordering::Acquire) { - return Err(EPROBE_DEFER); + return Err(ENODEV); } let cfg = *info.ok_or(ENODEV)?; dev_info!(pdev.as_ref(), "G15: probing ({})\n", cfg.name); diff --git a/drivers/gpu/drm/asahi/g15/regs.rs b/drivers/gpu/drm/asahi/g15/regs.rs index 05a1b390b3dff..98e7d4c8cf78a 100644 --- a/drivers/gpu/drm/asahi/g15/regs.rs +++ b/drivers/gpu/drm/asahi/g15/regs.rs @@ -125,7 +125,7 @@ impl Mapping { if size == 0 { return Err(EINVAL); } - let mode = super::boot::glue::memory_mode(phys, size as u64)?; + let mode = super::memory_mode(phys, size as u64)?; if mode == bindings::MEMREMAP_WC as u32 { return Self::write_combined(phys, size); } @@ -252,6 +252,7 @@ impl Regs { /// Reads the identification words and the enabled-core mask (once the GPU is powered). A /// SoC with more than 32 core slots carries the mask in two words. + #[cfg(CONFIG_DRM_ASAHI_G15)] pub(crate) fn identity(&self, cfg: &super::Config) -> Result { let version = self.sgx.io().try_read32(SGX_ID)?; let counts = self.sgx.io().try_read32(SGX_ID + 0x10)?; diff --git a/drivers/gpu/drm/asahi/g15/uat.rs b/drivers/gpu/drm/asahi/g15/uat.rs index 0f91f7c9f0610..0f0aa01e23517 100644 --- a/drivers/gpu/drm/asahi/g15/uat.rs +++ b/drivers/gpu/drm/asahi/g15/uat.rs @@ -38,12 +38,14 @@ use kernel::{ bindings, device, io::Io, prelude::*, - sync::Arc, time::{delay::fsleep, Delta, Instant, Monotonic}, }; +#[cfg(CONFIG_DRM_ASAHI_G15)] +use kernel::sync::Arc; use super::hw; use super::regs::{mb, rmb, wmb, Mapping}; +#[cfg(CONFIG_DRM_ASAHI_G15)] use crate::driver::AsahiDrmDevice as AsahiDevice; use crate::{mmu, pgtable}; @@ -69,6 +71,7 @@ const fn indices(va: u64) -> [usize; 3] { } /// Creates the adopted UAT over the ADT regions (step 2 above). +#[cfg(CONFIG_DRM_ASAHI_G15)] pub(crate) fn adopt( dev: &AsahiDevice, cfg: &'static super::Config, @@ -201,6 +204,7 @@ pub(crate) struct Window { impl Window { /// Maps the UAT handoff regions ([`Mapping::region`]). + #[cfg(CONFIG_DRM_ASAHI_G15)] pub(crate) fn new(adt: &super::adt::GpuAdt) -> Result { Self::from_regions(adt.handoff.start, adt.ttbs.start, adt.tables.start, adt.shared_l2.start) } diff --git a/drivers/gpu/drm/asahi/g15/uat_glue.c b/drivers/gpu/drm/asahi/g15/uat_glue.c new file mode 100644 index 0000000000000..134ff4bb28ab6 --- /dev/null +++ b/drivers/gpu/drm/asahi/g15/uat_glue.c @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * C helpers of the UAT adoption (g15/uat.rs, g15/regs.rs), built for the G15 + * backend and for the T8122 runtime, which adopts the firmware's UAT the same + * way (CONFIG_DRM_ASAHI_UAT_ADOPTION). + */ +#include +#include +#include +#include +#include +#include + +bool asahi_g15_ram_disjoint(u64 start, u64 size); +bool asahi_g15_ram_within(u64 start, u64 size); +unsigned int asahi_g15_memory_mode(u64 start, u64 size); + +/* The range is disjoint from System RAM (ADT region admission). */ +bool asahi_g15_ram_disjoint(u64 start, u64 size) +{ + return region_intersects(start, size, IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE) == + REGION_DISJOINT; +} + +/* + * Every page must be valid, System RAM and still reserved from the allocator. + * REGION_INTERSECTS alone also accepts RAM mixed with undefined memory holes. + */ +bool asahi_g15_ram_within(u64 start, u64 size) +{ + u64 end, pa; + + if (!size || !IS_ALIGNED(start, PAGE_SIZE) || + !IS_ALIGNED(size, PAGE_SIZE) || check_add_overflow(start, size, &end)) + return false; + for (pa = start; pa < end; pa += PAGE_SIZE) { + unsigned long pfn = PHYS_PFN(pa); + + if (!pfn_valid(pfn) || !page_is_ram(pfn) || + !PageReserved(pfn_to_page(pfn))) + return false; + } + return true; +} + +/* One classification for every adopted-UAT and firmware-region CPU mapping. */ +unsigned int asahi_g15_memory_mode(u64 start, u64 size) +{ + u64 end; + + if (!size || !IS_ALIGNED(start, PAGE_SIZE) || + !IS_ALIGNED(size, PAGE_SIZE) || check_add_overflow(start, size, &end)) + return 0; + if (asahi_g15_ram_within(start, size)) + return MEMREMAP_WB; + if (asahi_g15_ram_disjoint(start, size)) + return MEMREMAP_WC; + return 0; +} diff --git a/drivers/gpu/drm/asahi/m3/m3_adt_config.rs b/drivers/gpu/drm/asahi/m3/m3_adt_config.rs index d78b7bf2a0ddb..a9bc90db04d10 100644 --- a/drivers/gpu/drm/asahi/m3/m3_adt_config.rs +++ b/drivers/gpu/drm/asahi/m3/m3_adt_config.rs @@ -21,7 +21,8 @@ use crate::{ m3::m3_init_layout as init, m3::m3_init_storage as storage, m3::m3_memory::Buffer, - m3::m3_params::{self, G15Debug, InitDataParam, ThermalMode}, + m3::m3_params::{self, G15Debug, InitDataParam}, + m3::m3_thermal_policy::ThermalMode, m3::m3_soc::{IoMapping, PstateTable, Soc}, m3::t8122_hwdata, m3::t8122_start::Experiment, @@ -137,12 +138,6 @@ const _: () = assert!(storage::same_iomaps( &storage::T6030_IOMAPS )); -/// Highest performance state device-tree InitData may use without the driver's thermal limit -/// (`asahi.m3_thermal=off`), and the runtime cap the thermal limit starts at and falls back to -/// (the "safe cap"). The InitData's fast-die temperature controller words stay 0, so the -/// firmware does not limit the die temperature by itself. -pub(crate) const ADT_MAX_PSTATE_LIMIT: u32 = 5; - /// The performance states the runtime backend lets the firmware use. #[derive(Copy, Clone, Debug)] pub(crate) struct PstatePolicy { @@ -182,9 +177,10 @@ impl PstatePolicy { } /// Clamp the `asahi.m3_max_pstate` and `asahi.m3_boot_pstate` settings to the published table - /// of `hwdata` (the generated HwData object image), and to `ceiling` when given (the T8122 - /// start experiment's `asahi.t8122_pstate_cap`). - fn new(dev: &device::Device, hwdata: &[u8], ceiling: Option) -> Result { + /// of `hwdata` (the generated HwData object image), and to the T8122 start experiment's + /// `asahi.t8122_pstate_cap` when given, as admitted for the thermal mode + /// (`t8122_knobs::admitted_cap`). + fn new(dev: &device::Device, hwdata: &[u8], experiment: Option<&Experiment>) -> Result { let hwb_max = offset_of!(raw::HwDataBG15V14_8_3, max_pstate); let hwb_freq = offset_of!(raw::HwDataBG15V14_8_3, frequencies); let table_max = read_u32(hwdata, hwb_max)?; @@ -194,48 +190,41 @@ impl PstatePolicy { let freq = |i: u32| read_u32(hwdata, hwb_freq + 4 * i as usize); // Unset: the thermal limit is on. let thermal = m3_params::thermal_param().unwrap_or(ThermalMode::On); + let ceiling = experiment.map(|e| e.admitted_cap(thermal)); // Unset: the whole table when the thermal limit bounds the runtime cap, the default cap // otherwise. let requested = m3_params::max_pstate_param().unwrap_or(match thermal { ThermalMode::Hold | ThermalMode::On => u64::from(table_max), ThermalMode::Off => m3_params::MAX_PSTATE_DEFAULT, }); - let mut max = requested.clamp(1, u64::from(table_max)) as u32; - if u64::from(max) != requested { + let table_cap = requested.clamp(1, u64::from(table_max)) as u32; + let (max, safe) = thermal.pstate_caps(table_cap, ceiling); + if u64::from(table_cap) != requested { dev_info!( dev, "M3: asahi.m3_max_pstate={} clamped to {} (the table has states 1..={})\n", requested, - max, + table_cap, table_max ); } - if thermal == ThermalMode::Off && max > ADT_MAX_PSTATE_LIMIT { - dev_info!( - dev, - "M3: performance cap {} lowered to {}: device-tree InitData leaves the fast-die temperature controller off and the thermal limit is off (asahi.m3_thermal=off)\n", - max, - ADT_MAX_PSTATE_LIMIT - ); - max = ADT_MAX_PSTATE_LIMIT; - } - if let Some(ceiling) = ceiling { - if max > ceiling { + if max < table_cap { + if thermal == ThermalMode::Off { + dev_info!( + dev, + "M3: performance cap {} lowered to {}: device-tree InitData leaves the fast-die temperature controller off and the thermal limit is off (asahi.m3_thermal=off)\n", + table_cap, + max + ); + } else { dev_info!( dev, "M3 G15G start: performance cap {} lowered to {} (asahi.t8122_pstate_cap)\n", - max, - ceiling + table_cap, + max ); - max = ceiling; } } - // Without the thermal limit the runtime cap is the ceiling; with it, the runtime cap - // starts at (and never goes below) the cap used without it. - let safe = match thermal { - ThermalMode::Off => max, - ThermalMode::Hold | ThermalMode::On => max.min(ADT_MAX_PSTATE_LIMIT), - }; let boot = m3_params::boot_pstate_param() .unwrap_or(u64::from(safe)) .clamp(1, u64::from(safe)) as u32; @@ -324,7 +313,27 @@ impl Contents { storage::validate_iomaps(iomaps).map_err(|_| EINVAL)?; let images = build_images(dev, firmware, soc, experiment, iomaps)?; let hwdata = images.get(HWDATA).and_then(|i| i.as_deref()).ok_or(EINVAL)?; - let pstates = PstatePolicy::new(dev, hwdata, experiment.map(|e| e.pstate_cap()))?; + let pstates = PstatePolicy::new(dev, hwdata, experiment)?; + // The T8122 start experiment's cap: what was computed above must stay within what + // `t8122_knobs::pstate_cap_use` (which the host tests drive) gives for it. A state above + // the no-feedback limit is the firmware's only with the thermal limit on, and the + // runtime cap rises above its fallback only with it on. + if let Some(e) = experiment { + let ranges = pstates.thermal == ThermalMode::On && pstates.max > pstates.safe; + let u = e.cap_use(pstates.thermal); + let within = + pstates.max <= u.ceiling && pstates.safe <= u.fallback && (u.ranges || !ranges); + if !within { + dev_err!( + dev, + "M3 G15G start: refused: performance cap {} with runtime cap {} is more than asahi.t8122_pstate_cap={} allows\n", + pstates.max, + pstates.safe, + e.pstate_cap() + ); + return Err(EINVAL); + } + } let table = match soc.pstates { PstateTable::Fixed { states, top_mhz } => (states, top_mhz), PstateTable::DeviceTree => crate::m3::m3_board::opp_table_shape(pdev, soc).ok_or_else(|| { diff --git a/drivers/gpu/drm/asahi/m3/m3_config.rs b/drivers/gpu/drm/asahi/m3/m3_config.rs index 02b847e210dc9..0e25a449a1478 100644 --- a/drivers/gpu/drm/asahi/m3/m3_config.rs +++ b/drivers/gpu/drm/asahi/m3/m3_config.rs @@ -246,6 +246,22 @@ impl Config { self.thermal.busy(now); Ok(()) } + /// Whether [`Self::check_pstate`] must run at every retirement with no later batch in + /// flight, whatever `asahi.m3_retire_mmio` says: the firmware's ceiling is above the runtime + /// cap's fallback, so the runtime cap is what bounds it by temperature + /// (`m3_thermal::Governor::enforces`). + pub(crate) fn pstate_check_required(&self) -> bool { + self.thermal.enforces() + } + /// Whether a render batch may be published behind batches still in flight. Always, unless + /// [`Self::pstate_check_required`]; then only within 500 ms of the last + /// [`Self::check_pstate`], which runs only at a retirement with no later batch in flight. + pub(crate) fn overlap_allowed(&self) -> bool { + if !self.thermal.enforces() { + return true; + } + self.thermal.overlap_allowed(kernel::time::Instant::::now()) + } pub(crate) fn render_stats(&self) -> Result<[init::Region;2]> { if self.profile==super::m3_profile::Profile::Legacy { let value=self.stats_region()?;return Ok([value;2]); diff --git a/drivers/gpu/drm/asahi/m3/m3_params.rs b/drivers/gpu/drm/asahi/m3/m3_params.rs index 85033c88e4192..45b630cdef5b3 100644 --- a/drivers/gpu/drm/asahi/m3/m3_params.rs +++ b/drivers/gpu/drm/asahi/m3/m3_params.rs @@ -22,6 +22,8 @@ use kernel::{ prelude::*, // }; +use crate::m3::m3_thermal_policy::ThermalMode; + /// Parse a decimal or `0x`-prefixed hexadecimal `u64`. fn parse_u64(text: &str) -> Option { let text = text.trim(); @@ -516,6 +518,9 @@ fn parse_retire_mmio(text: &str) -> Option { /// job before retiring it. Bit 0 reads the engine-busy registers (SGX+0xc020/0xc120), bit 1 /// selects and reads each cluster's fault bank (0xd800/0xd8c0), bit 2 reads the performance /// state (0xe01000). Unset: the SoC's default ([`crate::m3::m3_soc::Soc::retire_mmio`]). +/// While the thermal limit's ceiling is above its safe cap the performance state is read and +/// the runtime cap moved at every retirement with no later batch in flight even without bit 2 +/// (`m3_thermal`); the first such retirement of a module load says so in the log. static M3_RETIRE_MMIO: AtomicU64 = AtomicU64::new(RETIRE_MMIO_UNSET); m3_param!("m3_retire_mmio", M3_RETIRE_MMIO, parse_retire_mmio, 0o644, Some(get_atomic_param)); @@ -545,21 +550,6 @@ fn parse_thermal(text: &str) -> Option { static M3_THERMAL: AtomicU64 = AtomicU64::new(THERMAL_UNSET); m3_param!("m3_thermal", M3_THERMAL, parse_thermal); -/// How the runtime backend limits the GPU performance state by temperature. -#[derive(Copy, Clone, PartialEq, Eq, Debug)] -pub(crate) enum ThermalMode { - /// Today's fixed cap: the firmware is given the cap as its highest state, and nothing - /// changes it at runtime. - Off, - /// The firmware is given the whole table as its highest state, and the runtime cap words - /// hold the fixed cap for the whole boot. The same performance as `Off`, if the firmware - /// follows the runtime cap: this checks that it does. - Hold, - /// As `Hold`, and the runtime cap follows the SoC die temperature, up to the whole table - /// while the die is cool. - On, -} - /// The `asahi.m3_thermal` setting, if given. pub(crate) fn thermal_param() -> Option { match M3_THERMAL.load(Ordering::Relaxed) { @@ -570,28 +560,37 @@ pub(crate) fn thermal_param() -> Option { } } -/// Default and bounds of `asahi.m3_thermal_hot`, in degrees Celsius. -pub(crate) const THERMAL_HOT_DEFAULT: u64 = 80; -pub(crate) const THERMAL_HOT_MIN: u64 = 40; -pub(crate) const THERMAL_HOT_MAX: u64 = 85; +pub(crate) use crate::m3::m3_thermal_policy::{ + COOL_GAP_DEFAULT, COOL_GAP_MAX, COOL_GAP_MIN, THERMAL_HOT_DEFAULT, THERMAL_HOT_MAX, + THERMAL_HOT_MIN, +}; /// `asahi.m3_thermal_hot`: the SoC die temperature (degrees Celsius) at or above which the thermal -/// limit lowers the runtime cap, clamped to [`THERMAL_HOT_MIN`]..=[`THERMAL_HOT_MAX`]. +/// limit lowers the runtime cap, clamped to [`THERMAL_HOT_MIN`]..=[`THERMAL_HOT_MAX`]. Writable +/// at runtime through sysfs. The thermal limit picks a new value up at its next update. static M3_THERMAL_HOT: AtomicU64 = AtomicU64::new(THERMAL_HOT_DEFAULT); -m3_param!("m3_thermal_hot", M3_THERMAL_HOT, parse_u64); +m3_param!("m3_thermal_hot", M3_THERMAL_HOT, parse_u64, 0o644, Some(get_atomic_param)); + +/// `asahi.m3_thermal_cool_gap`: how far below `hot` (degrees Celsius) the die must be before the +/// thermal limit raises the runtime cap again, clamped to [`COOL_GAP_MIN`]..=[`COOL_GAP_MAX`]. +/// Writable at runtime through sysfs, like `asahi.m3_thermal_hot`. +static M3_THERMAL_COOL_GAP: AtomicU64 = AtomicU64::new(COOL_GAP_DEFAULT); +m3_param!("m3_thermal_cool_gap", M3_THERMAL_COOL_GAP, parse_u64, 0o644, Some(get_atomic_param)); -/// The `asahi.m3_thermal_hot` setting and whether it was clamped. -pub(crate) fn thermal_hot_param() -> (u64, bool) { - let value = M3_THERMAL_HOT.load(Ordering::Relaxed); - let clamped = value.clamp(THERMAL_HOT_MIN, THERMAL_HOT_MAX); - (clamped, clamped != value) +/// The requested `asahi.m3_thermal_hot` and `asahi.m3_thermal_cool_gap`, in degrees Celsius +/// and not yet clamped (`m3_thermal_policy::clamp_thresholds` does that). +pub(crate) fn thermal_threshold_params() -> (u64, u64) { + ( + M3_THERMAL_HOT.load(Ordering::Relaxed), + M3_THERMAL_COOL_GAP.load(Ordering::Relaxed), + ) } /// Whether to start the runtime backend on a T8122 (`asahi.m3_backend`), and say why not. /// -/// The runtime starts only on the boards the T8122 table allows (the M3 MacBook Airs), with -/// `auto` and `runtime` alike: it has not run on any T8122, so nothing forces it onto another -/// board. The G15 manager backend is T6030-only. +/// The runtime starts only on the boards the T8122 table allows (the M3 MacBook Airs and the +/// 14" M3 MacBook Pro, `t8122_admission::T8122_BOARDS`), with `auto` and `runtime` alike: +/// nothing forces it onto another board. The G15 manager backend is T6030-only. pub(crate) fn t8122_backend(pdev: &platform::Device>) -> bool { let dev = pdev.as_ref(); match M3_BACKEND.load(Ordering::Relaxed) { @@ -610,7 +609,7 @@ pub(crate) fn t8122_backend(pdev: &platform::Device>) -> bool { _ => { dev_info!( dev, - "M3: no GPU backend on this T8122 board: the runtime backend may start only on the M3 MacBook Airs (J613, J615)\n" + "M3: no GPU backend on this T8122 board: the runtime backend may start only on the M3 MacBook Airs (J613, J615) and the 14\" M3 MacBook Pro (J504)\n" ); false } @@ -661,7 +660,10 @@ m3_param!("t8122_unit_mask_a", T8122_UNIT_MASK_A, parse_t8122_number); /// `asahi.t8122_unit_mask_b=`: HwDataB unit mask B (+0x17c8). static T8122_UNIT_MASK_B: AtomicU64 = AtomicU64::new(T8122_UNSET); m3_param!("t8122_unit_mask_b", T8122_UNIT_MASK_B, parse_t8122_number); -/// `asahi.t8122_pstate_cap=N`: the highest performance state the T8122 firmware may use. +/// `asahi.t8122_pstate_cap=N`: the highest performance state the T8122 firmware may use, 1 to 8. +/// The runtime cap starts at and falls back to 2; with `asahi.m3_thermal=on` it may rise to the +/// cap on usable die temperatures. 6 to 8 need `on`: with `hold` or `off` they are lowered to 5 +/// (`t8122_knobs::pstate_cap_use`). static T8122_PSTATE_CAP: AtomicU64 = AtomicU64::new(T8122_UNSET); m3_param!("t8122_pstate_cap", T8122_PSTATE_CAP, parse_t8122_number); diff --git a/drivers/gpu/drm/asahi/m3/m3_runtime.rs b/drivers/gpu/drm/asahi/m3/m3_runtime.rs index 762c8bc26e75b..0249696409772 100644 --- a/drivers/gpu/drm/asahi/m3/m3_runtime.rs +++ b/drivers/gpu/drm/asahi/m3/m3_runtime.rs @@ -280,6 +280,11 @@ impl Inner { fn pipeline_slot(&mut self)->Result> { let Some(last)=self.active.last() else {return Ok(Some((0,crate::m3::m3_pass_layout::SLOTS)));}; if !self.render_initialized {return Ok(None);} + // While the firmware's ceiling is above the runtime cap's fallback, the thermal limit + // moves and checks the cap only at a retirement with no later batch in flight. Let the + // pipeline drain once the last check is 500 ms old, so a client that keeps it full + // cannot hold the cap where it is. + if !self.config.overlap_allowed() {return Ok(None);} let Some((packet,next))=self.pending.first() else {return Ok(None);}; if !matches!(packet.commands[*next],crate::m3::m3_submit::Command::Render{..}) || self.active.iter().any(|b| b.kind!=0) || !packet.vm.same(&last.entries[0].0.vm) {return Ok(None);} @@ -821,7 +826,15 @@ impl Inner { let tick=physical_counter(); if let Some(batch)=self.active.first_mut() {batch.pipes_idle_tick=tick;} } - if retire_mmio&crate::m3::m3_params::RETIRE_MMIO_PSTATE!=0 { + // With the firmware's ceiling above the runtime cap's fallback, this check + // is what moves and enforces the runtime cap: it then runs whatever + // asahi.m3_retire_mmio says, and the first time it overrides the + // parameter the log says so. + let read_pstate=retire_mmio&crate::m3::m3_params::RETIRE_MMIO_PSTATE!=0; + if read_pstate || self.config.pstate_check_required() { + if !read_pstate && !FORCED_PSTATE_CHECK_LOGGED.swap(true,Ordering::Relaxed) { + dev_info!(self.drm.as_ref(),"M3: asahi.m3_retire_mmio={} leaves out the performance-state read (bit 2); it runs anyway at every retirement with no later batch in flight while the firmware's highest state is above the runtime cap's fallback\n",retire_mmio); + } if let Err(e)=self.config.check_pstate(&self.drm,&self.device,"after a job") { if crate::m3::t8122_start::is_t8122(self.device.soc()) { crate::m3::t8122_start::cap_violated_verdict(self.drm.as_ref().as_ref(),e); @@ -1017,6 +1030,9 @@ impl Inner { pub(crate) struct Runtime { inner: ManuallyDrop> } /// Claimed before this boot's first ASC start. static FIRMWARE_START: policy::StartLatch = policy::StartLatch::new(); +/// Whether a retirement has run the performance-state check against `asahi.m3_retire_mmio` +/// (see `poll_active`); logged once per module load. +static FORCED_PSTATE_CHECK_LOGGED: core::sync::atomic::AtomicBool = core::sync::atomic::AtomicBool::new(false); /// Takes a module reference, unless the module is already being removed. #[cfg(CONFIG_MODULE_UNLOAD)] fn pin_module() -> bool { @@ -1411,7 +1427,7 @@ fn adopt_uat(drm: &driver::AsahiDrmDevice, soc: &'static crate::m3::m3_soc::Soc, let range = |r: crate::m3::agx_resources::Region| r.base..r.base + r.size; let geometry = mmu::UatGeometry { ias: cfg.uat_ias.into(), oas: cfg.uat_oas }; let regions = mmu::AdoptedRegions { handoff: range(handoff), ttbs: range(ttbs), kernel_root: tables.base }; - let uat = mmu::Uat::new_adopted_with_user_upper(drm, geometry, ®ions, + let uat = mmu::Uat::new_adopted_ram_with_user_upper(drm, geometry, ®ions, profile == super::m3_profile::Profile::Legacy)?; let mut window = crate::g15::uat::Window::from_regions(handoff.base, ttbs.base, tables.base, shared_l2.base)?; window.bootstrap(drm.as_ref().as_ref(), &uat)?; diff --git a/drivers/gpu/drm/asahi/m3/m3_soc.rs b/drivers/gpu/drm/asahi/m3/m3_soc.rs index 7500822c4124c..3dcd15c896a48 100644 --- a/drivers/gpu/drm/asahi/m3/m3_soc.rs +++ b/drivers/gpu/drm/asahi/m3/m3_soc.rs @@ -284,9 +284,10 @@ pub(crate) static T8122: Soc = Soc { gpu_revision_id: hw::GpuRevisionID::C0, board: "apple,t8122", gpu: "apple,agx-t8122", - // The M3 MacBook Airs (13" J613, 15" J615). Allowed, not validated: the runtime has not run - // on either, and starts only behind a boot loader that hands the GPU over. - validated_boards: &[b"apple,j613", b"apple,j615"], + // The M3 MacBook Airs (13" J613, 15" J615) and the 14" M3 MacBook Pro (J504), which loads + // the same G15G image (`t8122_admission::T8122_BOARDS`). The runtime starts only behind a + // boot loader that hands the GPU over. + validated_boards: &crate::m3::t8122_admission::T8122_BOARDS, clusters: 1, cores_per_cluster: 10, mailbox_compatibles: &[ diff --git a/drivers/gpu/drm/asahi/m3/m3_thermal.rs b/drivers/gpu/drm/asahi/m3/m3_thermal.rs index e30f8fe011332..69d236f2a94a1 100644 --- a/drivers/gpu/drm/asahi/m3/m3_thermal.rs +++ b/drivers/gpu/drm/asahi/m3/m3_thermal.rs @@ -7,9 +7,12 @@ //! J516S) and nothing watches temperatures. The firmware's own die temperature controller is //! off in the InitData this driver builds. With device-tree InitData the default is `on`; //! `asahi.m3_thermal=off` restores the fixed cap exactly. +//! On T8122 the fixed cap is state 2 (or a lower requested ceiling). A higher +//! `asahi.t8122_pstate_cap` changes only the monitored ceiling, never this fallback. //! -//! With it, the InitData gives the firmware the whole table as its highest state (the -//! "ceiling"), and what bounds the firmware is the runtime cap: the Globals performance-state +//! With it, the InitData gives the firmware the requested, table-clamped highest state (the +//! "ceiling", also bounded by the T8122 experiment), and what bounds the firmware is the +//! runtime cap: the Globals performance-state //! cap and power-interface state targets, which the firmware keeps reading while it runs (a //! host moves the cap with a single store, with no message). The runtime cap starts at the //! fixed cap of `off` (the "safe cap") and never goes below it, so at worst the GPU runs as it @@ -22,10 +25,10 @@ //! 500 ms while the GPU is busy (every 2 s when idle) from the thermal zones in [`ZONES`] //! that exist: the SMC's die sensors and the PMP's CPU cluster sensors (see [`ZONES`]): //! * at or above `critical` (`hot` + 8 C): the safe cap at once; -//! * at or above `hot` (`asahi.m3_thermal_hot`, default 80 C): one state lower, at most every -//! 500 ms and once per reading; -//! * at or below `cool` (`hot` - 10 C): one state higher, at most every 2 s and once per -//! reading, up to the ceiling; +//! * at or above `hot` (`asahi.m3_thermal_hot`, default 80 C, at most 92 C): one state +//! lower, at most every 500 ms and once per reading; +//! * at or below `cool` (`hot` minus `asahi.m3_thermal_cool_gap`, default 10 C, 3 to 10 C): +//! one state higher, at most every 2 s and once per reading, up to the ceiling; //! * no zone, a zone that cannot be read, a reading outside 10..=125 C, one older than 3 s //! or one that has not changed at all for 60 s: the safe cap at once. //! @@ -35,11 +38,20 @@ //! reacts in steps of hundreds of milliseconds while the firmware changes state within the cap //! much faster. //! +//! `asahi.m3_thermal_hot` and `asahi.m3_thermal_cool_gap` are writable at runtime in +//! /sys/module/asahi/parameters. The next update after a write applies the clamped values and +//! logs them; the runtime cap itself only moves with the usual pacing. +//! //! The cap is changed only by the job thread, after a job has retired, right before the state -//! the firmware reports is checked. After a lowering the previous cap is tolerated for 1 s. A -//! reported state above the runtime cap in 20 consecutive checks spanning at least 3 s means -//! the firmware does not follow the runtime cap, so this limit cannot work: the GPU is then -//! marked failed, as for a state above the ceiling. +//! the firmware reports is checked. That check runs at a retirement with no later batch in +//! flight. While the ceiling is above the safe cap it runs at every such retirement whatever +//! `asahi.m3_retire_mmio` says, and a batch is published behind batches still in flight +//! (`asahi.m3_pipeline_depth` above 1) only within 500 ms of the last check, so that a client +//! that keeps the pipeline full cannot keep the check from running. +//! After a lowering the previous cap is tolerated for 1 s. A reported state above the runtime +//! cap in 20 consecutive checks spanning at least 3 s means the firmware does not follow the +//! runtime cap, so this limit cannot work: the GPU is then marked failed, as for a state above +//! the ceiling. //! //! The decisions are in `m3_thermal_policy`, which host tests drive; this module reads the //! sensors, publishes the cap and logs. @@ -59,8 +71,8 @@ use kernel::{ use crate::{ m3::m3_adt_config::PstatePolicy, - m3::m3_params::{self, ThermalMode}, - m3::m3_thermal_policy::{Change, Policy, Reading, Source, Why, S}, + m3::m3_params, + m3::m3_thermal_policy::{clamp_thresholds, Change, Policy, Reading, Source, ThermalMode, Why, S}, }; /// The thermal zones read, each the hottest of a set of SoC die sensors: the SMC's die sensors @@ -229,22 +241,28 @@ pub(crate) struct Governor { /// The highest state the firmware reported since the last summary. peak: u32, lines: u32, + /// The `asahi.m3_thermal_hot` and `asahi.m3_thermal_cool_gap` values last applied, as + /// written (before clamping). + requested: (u64, u64), } impl Governor { /// The governor for `policy`. Starts the sensor work item unless the limit is off. pub(crate) fn new(dev: &device::Device, pstates: &PstatePolicy) -> Result { - let (hot_c, clamped) = m3_params::thermal_hot_param(); + let requested = m3_params::thermal_threshold_params(); + let (hot, gap, clamped) = clamp_thresholds(requested.0, requested.1); let base = Time::now(); + let mut policy = Policy::new( + pstates.thermal == ThermalMode::On, + pstates.safe, + pstates.max, + hot, + ); + policy.set_thresholds(hot, gap); let mut governor = Governor { mode: pstates.thermal, fixed: pstates.max, - policy: Policy::new( - pstates.thermal == ThermalMode::On, - pstates.safe, - pstates.max, - hot_c as i32 * 1000, - ), + policy, freqs: pstates.freqs, base, sensor: None, @@ -252,18 +270,13 @@ impl Governor { last_summary: None, peak: 0, lines: 0, + requested, }; if governor.mode == ThermalMode::Off { return Ok(governor); } if clamped { - dev_info!( - dev, - "M3: asahi.m3_thermal_hot clamped to {} C ({}..={} C)\n", - hot_c, - m3_params::THERMAL_HOT_MIN, - m3_params::THERMAL_HOT_MAX - ); + governor.log_clamped(dev); } let sensor = Arc::pin_init( pin_init!(Sensor { @@ -293,7 +306,7 @@ impl Governor { ), _ => dev_info!( dev, - "M3: thermal limit on (asahi.m3_thermal=on): runtime cap {}..={} ({}..={} MHz) from the hottest SoC die temperature of zones macsmc_soc_die and apple_pmp_hotspot: up while at or below {}, down at or above {}, cap {} at or above {} or without a fresh reading\n", + "M3: thermal limit on (asahi.m3_thermal=on): runtime cap {}..={} ({}..={} MHz) from the hottest SoC die temperature of zones macsmc_soc_die and apple_pmp_hotspot: up while at or below {}, down at or above {}, cap {} at or above {} or without a fresh reading (asahi.m3_thermal_hot {}, asahi.m3_thermal_cool_gap {}, both writable at runtime)\n", p.safe(), p.ceiling(), governor.mhz(p.safe()), @@ -301,12 +314,60 @@ impl Governor { Celsius(cool), Celsius(hot), p.safe(), - Celsius(critical) + Celsius(critical), + Celsius(hot), + Celsius(hot - cool) ), } Ok(governor) } + /// Log that the requested thresholds were clamped, and to what. + fn log_clamped(&self, dev: &device::Device) { + let (cool, hot, _) = self.policy.thresholds(); + dev_info!( + dev, + "M3: asahi.m3_thermal_hot {} and asahi.m3_thermal_cool_gap {} clamped to {} and {} ({}..={} C and {}..={} C)\n", + self.requested.0, + self.requested.1, + Celsius(hot), + Celsius(hot - cool), + m3_params::THERMAL_HOT_MIN, + m3_params::THERMAL_HOT_MAX, + m3_params::COOL_GAP_MIN, + m3_params::COOL_GAP_MAX + ); + } + + /// Apply `asahi.m3_thermal_hot` and `asahi.m3_thermal_cool_gap` if either was written since + /// they were last applied, and log the thresholds now in use. + fn reread_thresholds(&mut self, dev: &device::Device) { + let requested = m3_params::thermal_threshold_params(); + if requested == self.requested { + return; + } + self.requested = requested; + let (hot, gap, clamped) = clamp_thresholds(requested.0, requested.1); + if clamped { + self.log_clamped(dev); + } + if !self.policy.set_thresholds(hot, gap) { + return; + } + let (cool, hot, critical) = self.policy.thresholds(); + let safe = self.policy.safe(); + dev_info!( + dev, + "M3: thermal: thresholds changed at runtime: up while at or below {}, down at or above {}, cap {} ({} MHz) at or above {}; runtime cap {} now\n", + Celsius(cool), + Celsius(hot), + safe, + self.mhz(safe), + Celsius(critical), + self.policy.cap() + ); + } + fn mhz(&self, state: u32) -> u32 { self.freqs.get(state as usize).copied().unwrap_or(0) } @@ -336,13 +397,29 @@ impl Governor { } else { "" }; - dev_info!( - dev, - "M3: thermal: SoC die temperature {} from zones:{}{}\n", - Celsius(t), - smc, - pmp - ) + // With a ceiling above the safe cap, a usable reading is what lets the runtime + // cap rise above it: say so, so the log shows when states above it are allowed. + if self.mode == ThermalMode::On && self.policy.above_safe() { + let ceiling = self.policy.ceiling(); + dev_info!( + dev, + "M3: thermal: SoC die temperature {} from zones:{}{}; the runtime cap may rise above {} up to {} ({} MHz)\n", + Celsius(t), + smc, + pmp, + safe, + ceiling, + self.mhz(ceiling) + ) + } else { + dev_info!( + dev, + "M3: thermal: SoC die temperature {} from zones:{}{}\n", + Celsius(t), + smc, + pmp + ) + } } (Source::Failed(e), _) => dev_info!( dev, @@ -439,6 +516,7 @@ impl Governor { return None; }; let reading = sensor.state.lock().reading; + self.reread_thresholds(dev); let now = ns(self.base, now); let (usable, change) = self.policy.update(now, reading); let temp = usable.ok().map(|(t, _, _)| t); @@ -487,6 +565,20 @@ impl Governor { Ok(()) } + /// Whether the firmware's ceiling is above the safe cap, so that only the runtime cap keeps + /// it within what the temperature allows: the caller must then move and check the cap at + /// every retirement with no later batch in flight. False with the limit off, where the cap + /// is fixed. + pub(crate) fn enforces(&self) -> bool { + self.mode != ThermalMode::Off && self.policy.above_safe() + } + + /// Whether a batch may be published now behind batches still in flight: always unless + /// [`Self::enforces`], then only within 500 ms of the last check. + pub(crate) fn overlap_allowed(&self, now: Time) -> bool { + !self.enforces() || self.policy.overlap_allowed(ns(self.base, now)) + } + /// Note that the GPU retired a job, so the sensor is read at the busy rate. pub(crate) fn busy(&self, now: Time) { if let Some(sensor) = &self.sensor { diff --git a/drivers/gpu/drm/asahi/m3/m3_thermal_policy.rs b/drivers/gpu/drm/asahi/m3/m3_thermal_policy.rs index 2ae70634ff556..ac77c5c299bf9 100644 --- a/drivers/gpu/drm/asahi/m3/m3_thermal_policy.rs +++ b/drivers/gpu/drm/asahi/m3/m3_thermal_policy.rs @@ -1,11 +1,45 @@ // SPDX-License-Identifier: GPL-2.0-only OR MIT //! Decisions of the M3 thermal limit (see `m3_thermal`), kept free of kernel dependencies so -//! that host tests drive exactly this code (`tools/asahi/m3-thermal-policy-tests.rs`). +//! that host tests drive exactly this code (`tools/asahi/m3-runtime-tests.rs`). //! //! Times are monotonic nanoseconds from any fixed origin, temperatures millidegrees Celsius, and //! states indices of the published performance-state table. +/// Highest fixed cap for non-T8122 device-tree InitData, whose firmware die-temperature +/// controller is disabled. T8122 retains its lower default cap without temperature feedback. +/// The T8122 start experiment admits a ceiling above it only with the thermal limit on +/// (`t8122_knobs::PSTATE_CAP_NO_FEEDBACK_MAX` is this number). +pub(crate) const ADT_MAX_PSTATE_LIMIT: u32 = 5; +/// T8122's unmonitored cap, independent of any experiment ceiling or future default change +/// (`t8122_knobs::PSTATE_FALLBACK` is this number). +pub(crate) const T8122_SAFE_PSTATE: u32 = 2; + +/// How the runtime backend limits the GPU performance state by temperature. +#[derive(Copy, Clone, PartialEq, Eq, Debug)] +pub(crate) enum ThermalMode { + /// A fixed ceiling no higher than the safe cap, without temperature feedback. + Off, + /// Publish the requested ceiling, but hold the runtime cap at the safe cap. + Hold, + /// Allow the runtime cap to follow the die temperature up to the requested ceiling. + On, +} + +impl ThermalMode { + /// Return (firmware ceiling, boot/failure cap) for a table-clamped request. A T8122 + /// experiment ceiling permits higher states only with working temperature feedback; + /// it must not also raise the cap used before a reading or after a sensor failure. + pub(crate) fn pstate_caps(self, requested: u32, t8122_ceiling: Option) -> (u32, u32) { + let (max, limit) = match t8122_ceiling { + Some(ceiling) => (requested.min(ceiling), T8122_SAFE_PSTATE), + None => (requested, ADT_MAX_PSTATE_LIMIT), + }; + let safe = max.min(limit); + (if self == Self::Off { safe } else { max }, safe) + } +} + /// Required thermal-zone bits: SMC die (bit 0), PMP hotspot (bit 1). /// A missing or malformed compatible list requires both zones. pub(crate) fn required_zones(compatible: &[u8]) -> u32 { @@ -42,10 +76,36 @@ pub(crate) const GRACE_NS: i64 = S; /// firmware does not follow the runtime cap. pub(crate) const IGNORED_CHECKS: u32 = 20; pub(crate) const IGNORED_NS: i64 = 3 * S; -/// Distance of `cool` below and `critical` above `hot`. +/// While the ceiling is above the safe cap, a batch is published behind batches still in flight +/// only within this time of the last check. The cap is moved and the reported state checked +/// only at a retirement with no later batch in flight, so a client that kept the pipeline full +/// would otherwise keep the cap wherever it was. The sensor is read every 500 ms while the GPU +/// is busy, so more frequent checks would not see newer readings. +pub(crate) const CHECK_EVERY_NS: i64 = 500 * MS; +/// Default distance of `cool` below `hot` (`asahi.m3_thermal_cool_gap`) and the fixed distance +/// of `critical` above it. pub(crate) const COOL_BELOW_MC: i32 = 10_000; pub(crate) const CRITICAL_ABOVE_MC: i32 = 8_000; +/// Default and bounds of `asahi.m3_thermal_hot`, in degrees Celsius. At the top `critical` is +/// 100 C, still below the 110 C critical trip of the J613 and J615 CPU thermal zones. +pub(crate) const THERMAL_HOT_DEFAULT: u64 = 80; +pub(crate) const THERMAL_HOT_MIN: u64 = 40; +pub(crate) const THERMAL_HOT_MAX: u64 = 92; +/// Default and bounds of `asahi.m3_thermal_cool_gap`, in degrees Celsius. The default is the +/// distance used before the gap could be set. +pub(crate) const COOL_GAP_DEFAULT: u64 = 10; +pub(crate) const COOL_GAP_MIN: u64 = 3; +pub(crate) const COOL_GAP_MAX: u64 = 10; + +/// `hot` and the cool gap in millidegrees Celsius for the requested values in degrees Celsius, +/// each clamped to its bounds, and whether either was clamped. +pub(crate) fn clamp_thresholds(hot_c: u64, gap_c: u64) -> (i32, i32, bool) { + let hot = hot_c.clamp(THERMAL_HOT_MIN, THERMAL_HOT_MAX); + let gap = gap_c.clamp(COOL_GAP_MIN, COOL_GAP_MAX); + (hot as i32 * 1000, gap as i32 * 1000, hot != hot_c || gap != gap_c) +} + /// The latest sensor reading. #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub(crate) enum Reading { @@ -110,6 +170,8 @@ pub(crate) struct Policy { /// Since when, and in how many consecutive checks, the firmware reported a state above the /// allowed cap. over: Option<(i64, u32)>, + /// When the reported state was last checked. + checked: Option, raised: u32, lowered_count: u32, } @@ -133,6 +195,7 @@ impl Policy { last_change: None, lowered: None, over: None, + checked: None, raised: 0, lowered_count: 0, } @@ -150,10 +213,41 @@ impl Policy { pub(crate) fn thresholds(&self) -> (i32, i32, i32) { (self.cool, self.hot, self.critical) } + + /// Move `hot` to `hot` millidegrees Celsius, `cool` to `gap` below it and `critical` to + /// [`CRITICAL_ABOVE_MC`] above it. The runtime cap stays where it is. The next update applies + /// the new thresholds with the usual pacing (critical at once). Returns whether anything + /// changed. + pub(crate) fn set_thresholds(&mut self, hot: i32, gap: i32) -> bool { + let (cool, critical) = (hot - gap, hot + CRITICAL_ABOVE_MC); + if (self.cool, self.hot, self.critical) == (cool, hot, critical) { + return false; + } + (self.cool, self.hot, self.critical) = (cool, hot, critical); + true + } pub(crate) fn counts(&self) -> (u32, u32) { (self.raised, self.lowered_count) } + /// Whether the ceiling is above the safe cap, so that the runtime cap is what keeps the + /// firmware within what the temperature allows. The cap is then moved and the reported + /// state checked at every retirement with no later batch in flight, whatever + /// `asahi.m3_retire_mmio` says (`m3_runtime`). False with the thermal limit off, where the + /// safe cap is the ceiling. + pub(crate) fn above_safe(&self) -> bool { + self.ceiling > self.safe + } + + /// Whether a batch may be published at `now` behind batches still in flight. Always while + /// the ceiling is at the safe cap. Above it, only within [`CHECK_EVERY_NS`] of the last + /// check: later batches are held back until the pipeline has drained, so the retirement + /// that checks comes at most that long, plus the run time of the batches then in flight, + /// after the last one. + pub(crate) fn overlap_allowed(&self, now: i64) -> bool { + !self.above_safe() || self.checked.is_some_and(|t| now - t < CHECK_EVERY_NS) + } + /// The temperature of `reading` if it may be used at `now` (with when it was taken and its /// zones), or why not. pub(crate) fn usable(&mut self, now: i64, reading: Reading) -> Result<(i32, i64, u32), Source> { @@ -246,6 +340,7 @@ impl Policy { /// [`Self::allowed`]. Returns the number of consecutive checks and their span when they /// show that the firmware does not follow the runtime cap. pub(crate) fn note_check(&mut self, now: i64, over: bool) -> Option<(u32, i64)> { + self.checked = Some(now); if !over { self.over = None; return None; @@ -259,6 +354,117 @@ impl Policy { } } +#[cfg(test)] +mod cap_tests { + use super::*; + + fn temp(mc: i32, at: i64) -> Reading { + Reading::Temp { mc, at, zones: 1 } + } + + fn boosted_air() -> Policy { + let (ceiling, safe) = ThermalMode::On.pstate_caps(8, Some(5)); + let mut policy = Policy::new(true, safe, ceiling, 80_000); + assert_eq!(policy.cap(), 2); + for (at, mc, cap) in [(0, 60_000, 3), (2 * S, 60_001, 4), (4 * S, 60_002, 5)] { + policy.update(at, temp(mc, at)).0.unwrap(); + assert_eq!(policy.cap(), cap); + } + policy + } + + #[test] + fn air_ceiling_never_raises_the_unmonitored_cap() { + assert_eq!(ThermalMode::On.pstate_caps(8, Some(5)), (5, 2)); + assert_eq!(ThermalMode::Hold.pstate_caps(8, Some(5)), (5, 2)); + assert_eq!(ThermalMode::Off.pstate_caps(8, Some(5)), (2, 2)); + for mode in [ThermalMode::On, ThermalMode::Hold, ThermalMode::Off] { + assert_eq!(mode.pstate_caps(1, Some(5)), (1, 1)); + assert_eq!(mode.pstate_caps(8, Some(1)), (1, 1)); + } + } + + #[test] + fn non_air_caps_keep_the_existing_safe_limit() { + assert_eq!(ThermalMode::On.pstate_caps(8, None), (8, 5)); + assert_eq!(ThermalMode::Hold.pstate_caps(8, None), (8, 5)); + assert_eq!(ThermalMode::Off.pstate_caps(8, None), (5, 5)); + } + + #[test] + fn failed_missing_stale_and_implausible_readings_drop_air_to_two() { + for (reading, source) in [ + (Reading::None, Source::NoReading), + (Reading::Failed { errno: -5, at: 5 * S }, Source::Failed(-5)), + (temp(60_000, S), Source::Stale), + (temp(9_999, 5 * S), Source::Implausible), + (temp(125_001, 5 * S), Source::Implausible), + ] { + let mut policy = boosted_air(); + let (usable, change) = policy.update(5 * S, reading); + assert_eq!(usable, Err(source)); + assert_eq!(change, Some(Change { from: 5, to: 2, why: Why::NoTemperature(source) })); + policy.update(7 * S, temp(60_003, 7 * S)).0.unwrap(); + assert_eq!(policy.cap(), 3); + } + } + + #[test] + fn air_boost_requires_fresh_paced_readings_and_respects_the_ceiling() { + let (ceiling, safe) = ThermalMode::On.pstate_caps(8, Some(5)); + let mut policy = Policy::new(true, safe, ceiling, 80_000); + policy.update(0, temp(60_000, 0)).0.unwrap(); + assert_eq!(policy.cap(), 3); + policy.update(S, temp(60_001, S)).0.unwrap(); + assert_eq!(policy.cap(), 3); + policy.update(2 * S, temp(60_000, 0)).0.unwrap(); + assert_eq!(policy.cap(), 3); + policy.update(2 * S, temp(60_002, 2 * S)).0.unwrap(); + assert_eq!(policy.cap(), 4); + policy.update(4 * S, temp(60_003, 4 * S)).0.unwrap(); + policy.update(6 * S, temp(60_004, 6 * S)).0.unwrap(); + assert_eq!(policy.cap(), 5); + } + + #[test] + fn air_hot_and_critical_readings_reduce_the_cap() { + let mut policy = boosted_air(); + policy.update(4 * S + 500 * MS, temp(80_000, 4 * S + 500 * MS)).0.unwrap(); + assert_eq!(policy.cap(), 4); + policy.update(5 * S, temp(80_000, 4 * S + 500 * MS)).0.unwrap(); + assert_eq!(policy.cap(), 4); + policy.update(5 * S, temp(80_001, 5 * S)).0.unwrap(); + assert_eq!(policy.cap(), 3); + policy.update(5 * S + MS, temp(88_000, 5 * S + MS)).0.unwrap(); + assert_eq!(policy.cap(), 2); + assert_eq!(policy.allowed(5 * S + 2 * MS), 5); + assert_eq!(policy.allowed(6 * S + MS), 2); + } + + #[test] + fn unchanged_air_temperature_cannot_sustain_boost_forever() { + let (ceiling, safe) = ThermalMode::On.pstate_caps(8, Some(5)); + let mut policy = Policy::new(true, safe, ceiling, 80_000); + for at in [0, 2 * S, 4 * S] { + policy.update(at, temp(60_000, at)).0.unwrap(); + } + assert_eq!(policy.cap(), 5); + let (usable, _) = policy.update(STUCK_NS, temp(60_000, STUCK_NS)); + assert_eq!(usable, Err(Source::Stuck)); + assert_eq!(policy.cap(), 2); + } + + #[test] + fn hold_mode_never_boosts_the_air() { + let (ceiling, safe) = ThermalMode::Hold.pstate_caps(8, Some(5)); + let mut policy = Policy::new(false, safe, ceiling, 80_000); + for at in [0, 2 * S, 4 * S] { + policy.update(at, temp(60_000, at)).0.unwrap(); + } + assert_eq!(policy.cap(), 2); + } +} + #[cfg(test)] mod zone_tests { use super::required_zones; @@ -271,6 +477,7 @@ mod zone_tests { } assert_eq!(required_zones(b"apple,j613\0"), 1); assert_eq!(required_zones(b"apple,j615\0"), 1); + assert_eq!(required_zones(b"apple,j504\0apple,t8122\0"), 1); } #[test] @@ -283,3 +490,560 @@ mod zone_tests { assert_eq!(required_zones(b"apple,t81220\0"), 0); } } + +#[cfg(test)] +mod range_tests { + use super::*; + + /// `hot` of the tests: the default of `asahi.m3_thermal_hot`. Cool is 70 C, critical 88 C. + const HOT: i32 = 80_000; + + fn temp(mc: i32, at: i64) -> Reading { + Reading::Temp { mc, at, zones: 1 } + } + + /// A policy that has climbed from `safe` to `ceiling` on cool readings, the last at + /// `2 * (ceiling - safe - 1)` s; returns it with the time of that last reading. + fn raised(safe: u32, ceiling: u32) -> (Policy, i64) { + let mut p = Policy::new(true, safe, ceiling, HOT); + let mut now = 0; + for step in 0..ceiling - safe { + now = i64::from(step) * UP_PACE_NS; + let (usable, change) = p.update(now, temp(50_000 + step as i32, now)); + assert!(usable.is_ok()); + assert_eq!(change.map(|c| (c.from, c.to, c.why)), Some((safe + step, safe + step + 1, Why::Cool))); + } + assert_eq!(p.cap(), ceiling); + (p, now) + } + + #[test] + fn the_cap_starts_at_the_safe_cap_and_climbs_one_state_per_two_seconds_to_the_ceiling() { + for ceiling in 6..=8 { + let mut p = Policy::new(true, 5, ceiling, HOT); + assert_eq!((p.cap(), p.safe(), p.ceiling()), (5, 5, ceiling)); + assert_eq!(p.thresholds(), (70_000, 80_000, 88_000)); + // No reading yet: the safe cap. + assert_eq!(p.update(0, Reading::None), (Err(Source::NoReading), None)); + assert_eq!(p.cap(), 5); + // A cool reading every 500 ms: one state every 2 s, never above the ceiling. + let mut caps = [0u32; 21]; + for (i, slot) in caps.iter_mut().enumerate() { + let now = i as i64 * 500 * MS; + let _ = p.update(now, temp(60_000 + i as i32, now)); + *slot = p.cap(); + assert!(p.cap() <= ceiling && p.cap() >= 5); + } + // t = 0, 2, 4 s: 6, 7, 8 (as far as the ceiling allows). + assert_eq!(caps[0], 6); + assert_eq!(caps[3], 6); + assert_eq!(caps[4], 7.min(ceiling)); + assert_eq!(caps[7], 7.min(ceiling)); + assert_eq!(caps[8], 8.min(ceiling)); + assert_eq!(caps[20], ceiling); + assert_eq!(p.counts(), (ceiling - 5, 0)); + // Exactly cool (70.0 C) still raises; anything above it does not. + let (mut p, t) = raised(5, ceiling); + let _ = p.update(t + 500 * MS, temp(HOT, t + 500 * MS)); + assert_eq!(p.cap(), ceiling - 1); + let _ = p.update(t + 3 * S, temp(70_001, t + 3 * S)); + assert_eq!(p.cap(), ceiling - 1); + let _ = p.update(t + 4 * S, temp(70_000, t + 4 * S)); + assert_eq!(p.cap(), ceiling); + } + } + + #[test] + fn between_cool_and_hot_the_cap_holds() { + for ceiling in 6..=8 { + let (mut p, t) = raised(5, ceiling); + for (i, mc) in [70_001, 75_000, 79_999].into_iter().enumerate() { + let now = t + (i as i64 + 1) * 3 * S; + assert_eq!(p.update(now, temp(mc, now)).1, None); + assert_eq!(p.cap(), ceiling); + } + } + } + + #[test] + fn hot_lowers_one_state_per_fresh_reading_every_500_ms_and_stops_at_the_safe_cap() { + for ceiling in 6..=8 { + let (mut p, t) = raised(5, ceiling); + // Less than 500 ms after the last change: not yet. + let now = t + 499 * MS; + assert_eq!(p.update(now, temp(HOT, now)).1, None); + let mut now = t + 500 * MS; + for expected in (5..ceiling).rev() { + let change = p.update(now, temp(HOT + (now / MS) as i32 % 7, now)).1.unwrap(); + assert_eq!((change.to, change.why), (expected, Why::Hot)); + // The same reading again, however much later, is not acted on twice. + assert_eq!(p.update(now + 400 * MS, temp(HOT, now)).1, None); + now += 500 * MS; + } + assert_eq!(p.cap(), 5); + // Hot at the safe cap: nothing lower to go to. + for i in 0..4 { + let at = now + i * 500 * MS; + assert_eq!(p.update(at, temp(HOT + 1_000 + i as i32, at)).1, None); + assert_eq!(p.cap(), 5); + } + assert_eq!(p.counts(), (ceiling - 5, ceiling - 5)); + } + } + + #[test] + fn critical_and_unusable_readings_restore_the_safe_cap_at_once() { + for ceiling in 6..=8 { + let (_, t) = raised(5, ceiling); + // Right after the last raise, so no pacing would allow a step yet. + let now = t + MS; + let cases = [ + (temp(88_000, now), Why::Critical), + (temp(110_000, now), Why::Critical), + (Reading::None, Why::NoTemperature(Source::NoReading)), + (Reading::Failed { errno: -19, at: now }, Why::NoTemperature(Source::Failed(-19))), + (Reading::Failed { errno: -5, at: now }, Why::NoTemperature(Source::Failed(-5))), + // Older than 3 s, as a temperature and as a failure. + (temp(50_000, now - STALE_NS - 1), Why::NoTemperature(Source::Stale)), + (Reading::Failed { errno: -5, at: now - STALE_NS - 1 }, Why::NoTemperature(Source::Stale)), + // Outside 10..=125 C. The SMC's reading of a powered-down sensor is one of them. + (temp(9_999, now), Why::NoTemperature(Source::Implausible)), + (temp(-2_849, now), Why::NoTemperature(Source::Implausible)), + (temp(125_001, now), Why::NoTemperature(Source::Implausible)), + ]; + for (reading, why) in cases { + let (mut p, _) = raised(5, ceiling); + let change = p.update(now, reading).1.unwrap(); + assert_eq!((change.from, change.to, change.why), (ceiling, 5, why), "{reading:?}"); + // The previous cap is tolerated for the grace period only. + assert_eq!(p.allowed(now + GRACE_NS - 1), ceiling); + assert_eq!(p.allowed(now + GRACE_NS), 5); + } + // A reading at exactly 3 s is still used. + let (mut p, _) = raised(5, ceiling); + assert_eq!(p.update(now, temp(60_000, now - STALE_NS)).1, None); + assert_eq!(p.cap(), ceiling); + // A reading that has not changed for 60 s is stuck, even while fresh. + let (mut p, t) = raised(5, ceiling); + let since = t + S; + assert_eq!(p.update(since, temp(61_234, since)).1, None); + assert_eq!(p.update(since + STUCK_NS - 1, temp(61_234, since + STUCK_NS - 1)).1, None); + assert_eq!(p.cap(), ceiling); + let change = p.update(since + STUCK_NS, temp(61_234, since + STUCK_NS)).1.unwrap(); + assert_eq!((change.to, change.why), (5, Why::NoTemperature(Source::Stuck))); + } + } + + #[test] + fn after_a_fallback_the_cap_climbs_again_one_state_per_two_seconds() { + for ceiling in 6..=8 { + let (mut p, t) = raised(5, ceiling); + let _ = p.update(t + S, Reading::Failed { errno: -19, at: t + S }); + assert_eq!(p.cap(), 5); + // A usable reading right away does not raise before 2 s have passed since the fall. + let _ = p.update(t + 2 * S, temp(55_000, t + 2 * S)); + assert_eq!(p.cap(), 5); + for step in 0..ceiling - 5 { + let now = t + 3 * S + i64::from(step) * UP_PACE_NS; + let _ = p.update(now, temp(55_001 + step as i32, now)); + assert_eq!(p.cap(), 6 + step); + } + assert_eq!(p.cap(), ceiling); + } + } + + #[test] + fn hold_never_leaves_the_safe_cap() { + for ceiling in 6..=8 { + let mut p = Policy::new(false, 5, ceiling, HOT); + for i in 0..40 { + let now = i * S; + let reading = match i % 4 { + 0 => temp(40_000 + i as i32, now), + 1 => temp(85_000 + i as i32, now), + 2 => temp(95_000 + i as i32, now), + _ => Reading::Failed { errno: -19, at: now }, + }; + assert_eq!(p.update(now, reading).1, None); + assert_eq!((p.cap(), p.allowed(now)), (5, 5)); + } + assert_eq!(p.ceiling(), ceiling); + assert_eq!(p.counts(), (0, 0)); + } + } + + #[test] + fn a_ceiling_at_the_safe_cap_never_moves() { + // Today's boots: asahi.t8122_pstate_cap of 1 to 5 gives safe == ceiling. + for cap in 1..=5 { + let mut p = Policy::new(true, cap, cap, HOT); + for i in 0..40 { + let now = i * S; + let reading = match i % 5 { + 0 => temp(40_000 + i as i32, now), + 1 => temp(81_000 + i as i32, now), + 2 => temp(99_000 + i as i32, now), + 3 => Reading::None, + _ => Reading::Failed { errno: -19, at: now }, + }; + assert_eq!(p.update(now, reading).1, None); + assert_eq!((p.cap(), p.allowed(now)), (cap, cap)); + } + assert_eq!(p.counts(), (0, 0)); + } + // A ceiling below the safe cap is raised to it, not the other way round. + let p = Policy::new(true, 5, 3, HOT); + assert_eq!((p.safe(), p.ceiling(), p.cap()), (5, 5, 5)); + } + + #[test] + fn the_per_job_check_is_required_only_while_the_ceiling_is_above_the_safe_cap() { + for ceiling in 6..=8 { + // On and hold: only the runtime cap keeps the firmware below its ceiling. + assert!(Policy::new(true, 5, ceiling, HOT).above_safe()); + assert!(Policy::new(false, 5, ceiling, HOT).above_safe()); + // Raised, lowered or fallen back, the requirement stays for the whole boot. + let (mut p, t) = raised(5, ceiling); + assert!(p.above_safe()); + let _ = p.update(t + S, Reading::None); + assert_eq!(p.cap(), 5); + assert!(p.above_safe()); + } + // Today's boots (a ceiling of 1 to 5 equals the safe cap) and the limit off (the + // governor gets the fixed cap as both) keep asahi.m3_retire_mmio in charge. + for cap in 1..=5 { + assert!(!Policy::new(true, cap, cap, HOT).above_safe()); + assert!(!Policy::new(false, cap, cap, HOT).above_safe()); + } + assert!(!Policy::new(true, 5, 3, HOT).above_safe()); + } + + #[test] + fn a_firmware_that_ignores_a_lowered_cap_is_found_after_twenty_checks_over_three_seconds() { + let (mut p, t) = raised(5, 8); + let fell = t + S; + let _ = p.update(fell, Reading::None); + assert_eq!((p.cap(), p.allowed(fell)), (5, 8)); + // Within the grace period a state of 8 is not "over". + assert!(p.note_check(fell + 500 * MS, false).is_none()); + // Afterwards: 20 checks in less than 3 s are not enough; the next one 3 s after the + // first is. + let start = fell + GRACE_NS; + for i in 0..19 { + assert!(p.note_check(start + i * 100 * MS, true).is_none()); + } + assert!(p.note_check(start + IGNORED_NS - 1, true).is_none()); + assert_eq!(p.note_check(start + IGNORED_NS, true), Some((21, IGNORED_NS))); + // One check within the cap starts the count again. + let again = start + IGNORED_NS + MS; + assert!(p.note_check(again, false).is_none()); + // 19 checks are not enough however long they span; the 20th then is. + for i in 0..19 { + assert!(p.note_check(again + (i + 1) * S, true).is_none()); + } + assert_eq!(p.note_check(again + 20 * S, true), Some((20, 19 * S))); + } +} + +#[cfg(test)] +mod pipeline_tests { + use super::*; + use std::collections::VecDeque; + + const HOT: i32 = 80_000; + const US: i64 = 1_000; + + /// One check: when, the runtime cap after it, and whether it found that the firmware + /// does not follow the runtime cap. + #[derive(Copy, Clone, Debug, PartialEq, Eq)] + struct Check { + at: i64, + cap: u32, + ignored: bool, + } + + /// A client that keeps the GPU's one lane busy with render batches of `batch` ns, as the + /// runtime publishes them (`m3_runtime::Inner::publish_next` and `pipeline_slot`): one + /// batch when none is in flight, more behind it up to `depth` while `bounded` is false or + /// [`Policy::overlap_allowed`] allows it. The oldest batch retires when it ends; only a + /// retirement with no later batch in flight runs the check (`Config::check_pstate`): the + /// governor's update with the latest sensor reading, then [`Policy::note_check`]. The + /// sensor is read every 500 ms; `temp` gives the die temperature at a time and `over` + /// whether the firmware then reports a state above the allowed cap. Returns every check, + /// the first being the one after boot at 0. + fn run( + p: &mut Policy, + depth: usize, + batch: i64, + until: i64, + bounded: bool, + temp: impl Fn(i64) -> i32, + over: impl Fn(i64) -> bool, + ) -> Vec { + let check = |p: &mut Policy, now: i64| { + let at = now - now % (500 * MS); + // A small wobble keeps a constant temperature from reading as stuck. + let mc = temp(at) + (at / (500 * MS) % 3) as i32; + let _ = p.update(now, Reading::Temp { mc, at, zones: 1 }); + let ignored = p.note_check(now, over(now)).is_some(); + Check { at: now, cap: p.cap(), ignored } + }; + let mut checks = vec![check(p, 0)]; + // When each batch in flight ends, oldest first. + let mut active: VecDeque = VecDeque::new(); + let mut now = 0; + while now < until { + while active.len() < depth && (active.is_empty() || !bounded || p.overlap_allowed(now)) { + let start = active.back().copied().unwrap_or(now).max(now); + active.push_back(start + batch); + } + now = active.pop_front().unwrap(); + if active.is_empty() { + checks.push(check(p, now)); + } + } + checks + } + + fn longest_gap(checks: &[Check], until: i64) -> i64 { + let mut gap = 0; + for pair in checks.windows(2) { + gap = gap.max(pair[1].at - pair[0].at); + } + gap.max(until - checks.last().unwrap().at) + } + + fn cool(_: i64) -> i32 { + 55_000 + } + + fn obeys(_: i64) -> bool { + false + } + + /// Critical (90 C) from 10 s on. + fn hot_from_10_s(at: i64) -> i32 { + if at >= 10 * S { 90_000 } else { 55_000 } + } + + #[test] + fn without_the_bound_a_full_pipeline_never_checks_and_the_cap_stays_up() { + // Today's pipelining with a ceiling of 8: after the check at boot, a client that keeps + // two batches in flight is never checked again, so 90 C does not lower the cap. + let mut p = Policy::new(true, 5, 8, HOT); + for i in 0..4 { + let at = i * UP_PACE_NS; + let _ = p.update(at, Reading::Temp { mc: 50_000 + i as i32, at, zones: 1 }); + } + assert_eq!(p.cap(), 8); + let checks = run(&mut p, 2, 2 * MS, 20 * S, false, hot_from_10_s, obeys); + assert_eq!(checks.len(), 1); + assert_eq!(p.cap(), 8); + } + + #[test] + fn with_the_bound_a_full_pipeline_is_checked_every_500_ms_plus_its_batches() { + for depth in 1..=4usize { + for batch in [100 * US, 2 * MS, 4 * MS, 50 * MS] { + let until = 20 * S; + let mut p = Policy::new(true, 5, 8, HOT); + let checks = run(&mut p, depth, batch, until, true, cool, obeys); + let bound = CHECK_EVERY_NS + depth as i64 * batch; + let gap = longest_gap(&checks, until); + assert!(gap <= bound, "depth {depth} batch {batch}: gap {gap} > {bound}"); + if depth == 1 { + // No pipelining: every retirement checks, as today. + assert_eq!(checks.len() as i64, until / batch + 1); + } else { + // A drain per 500 ms or so, not one per batch. + assert!((checks.len() as i64) < 2 * until / CHECK_EVERY_NS + 2); + } + } + } + } + + #[test] + fn with_the_bound_a_full_pipeline_cannot_hold_a_raised_cap_when_hot() { + for depth in 2..=4usize { + for batch in [100 * US, 2 * MS, 4 * MS] { + let mut p = Policy::new(true, 5, 8, HOT); + let checks = run(&mut p, depth, batch, 20 * S, true, hot_from_10_s, obeys); + // The cap rose to 8 while cool. + assert!(checks.iter().any(|c| c.at < 10 * S && c.cap == 8)); + // The sensor reading taken at 10 s is critical. The first check after it comes + // within the bound and falls straight to 5, where the cap stays. + let deadline = 10 * S + CHECK_EVERY_NS + depth as i64 * batch; + let fell = checks.iter().find(|c| c.at >= 10 * S).unwrap(); + assert!(fell.at <= deadline, "depth {depth} batch {batch}: {} > {deadline}", fell.at); + assert_eq!(fell.cap, 5); + assert!(checks.iter().filter(|c| c.at >= fell.at).all(|c| c.cap == 5)); + } + } + } + + #[test] + fn with_the_bound_a_firmware_that_ignores_the_cap_is_found_in_a_full_pipeline() { + // Hold with a ceiling of 8 (the first qualification step): the runtime cap stays 5. A + // firmware that reports a state above it after boot is found after 20 checks spanning + // 3 s, about 10 s into a full pipeline. Without the bound it is never found. + for bounded in [true, false] { + let mut p = Policy::new(false, 5, 8, HOT); + let checks = run(&mut p, 2, 2 * MS, 20 * S, bounded, cool, |now| now > 0); + let found = checks.iter().find(|c| c.ignored); + if bounded { + let at = found.expect("never found").at; + assert!(at <= 20 * (CHECK_EVERY_NS + 2 * 2 * MS), "found at {at}"); + } else { + assert_eq!(found, None); + } + assert!(checks.iter().all(|c| c.cap == 5)); + } + } + + #[test] + fn a_ceiling_at_the_safe_cap_keeps_todays_pipelining() { + // Every boot today: the bound never holds a batch back. + for cap in 1..=5 { + for depth in 1..=4usize { + let mut p = Policy::new(true, cap, cap, HOT); + let bounded = run(&mut p, depth, 2 * MS, 5 * S, true, hot_from_10_s, obeys); + let mut q = Policy::new(true, cap, cap, HOT); + let today = run(&mut q, depth, 2 * MS, 5 * S, false, hot_from_10_s, obeys); + assert_eq!(bounded, today); + for now in [0, CHECK_EVERY_NS, 100 * S] { + assert!(p.overlap_allowed(now)); + } + } + } + // Without any check yet, a ceiling above the safe cap allows no overlap; after one, + // for 500 ms. + assert!(!Policy::new(true, 5, 8, HOT).overlap_allowed(0)); + let mut p = Policy::new(false, 5, 6, HOT); + let _ = p.note_check(S, false); + assert!(p.overlap_allowed(S + CHECK_EVERY_NS - 1)); + assert!(!p.overlap_allowed(S + CHECK_EVERY_NS)); + } +} + +#[cfg(test)] +mod tuning_tests { + use super::*; + + fn temp(mc: i32, at: i64) -> Reading { + Reading::Temp { mc, at, zones: 1 } + } + + /// A policy with `hot` and `gap` in degrees Celsius that has climbed from 5 to 8 on cool + /// readings; returns it with the time of the last raise. + fn raised(hot_c: u64, gap_c: u64) -> (Policy, i64) { + let (hot, gap, _) = clamp_thresholds(hot_c, gap_c); + let mut p = Policy::new(true, 5, 8, hot); + p.set_thresholds(hot, gap); + let mut now = 0; + for step in 0..3 { + now = i64::from(step) * UP_PACE_NS; + let _ = p.update(now, temp(50_000 + step, now)); + } + assert_eq!(p.cap(), 8); + (p, now) + } + + #[test] + fn the_parameters_are_clamped_to_their_bounds() { + assert_eq!(clamp_thresholds(THERMAL_HOT_DEFAULT, COOL_GAP_DEFAULT), (80_000, 10_000, false)); + assert_eq!(clamp_thresholds(85, 10), (85_000, 10_000, false)); + assert_eq!(clamp_thresholds(92, 3), (92_000, 3_000, false)); + assert_eq!(clamp_thresholds(93, 3), (92_000, 3_000, true)); + assert_eq!(clamp_thresholds(150, 0), (92_000, 3_000, true)); + assert_eq!(clamp_thresholds(39, 11), (40_000, 10_000, true)); + assert_eq!(clamp_thresholds(40, 10), (40_000, 10_000, false)); + } + + #[test] + fn the_default_gap_keeps_the_thresholds_of_new() { + for hot_c in [THERMAL_HOT_MIN, THERMAL_HOT_DEFAULT, 85, THERMAL_HOT_MAX] { + let (hot, gap, _) = clamp_thresholds(hot_c, COOL_GAP_DEFAULT); + let mut p = Policy::new(true, 5, 8, hot); + assert!(!p.set_thresholds(hot, gap)); + assert_eq!(p.thresholds(), (hot - COOL_BELOW_MC, hot, hot + CRITICAL_ABOVE_MC)); + } + // At the top of both ranges it is up at or below 89 C, down at or above 92 C, safe at 100 C. + let (hot, gap, _) = clamp_thresholds(THERMAL_HOT_MAX, COOL_GAP_MIN); + let mut p = Policy::new(true, 5, 8, hot); + assert!(p.set_thresholds(hot, gap)); + assert_eq!(p.thresholds(), (89_000, 92_000, 100_000)); + assert!(!p.set_thresholds(hot, gap)); + } + + #[test] + fn a_narrower_gap_raises_the_cap_again_at_a_higher_temperature() { + for (gap_c, raises_at_75) in [(10, false), (5, true), (3, true)] { + let (mut p, t) = raised(80, gap_c); + // One step down at 80 C. + let now = t + 500 * MS; + assert_eq!(p.update(now, temp(80_000, now)).1.map(|c| c.to), Some(7)); + // 75 C, 2 s later: up again only when 75 C is at or below 80 C minus the gap. + let now = now + UP_PACE_NS; + let change = p.update(now, temp(75_000, now)).1; + assert_eq!(change.is_some(), raises_at_75, "gap {gap_c}"); + assert_eq!(p.cap(), if raises_at_75 { 8 } else { 7 }); + } + // With a gap of 3, 77.001 C holds and 77 C raises. + let (mut p, t) = raised(80, 3); + let now = t + 500 * MS; + let _ = p.update(now, temp(80_000, now)); + assert_eq!(p.cap(), 7); + assert_eq!(p.update(now + UP_PACE_NS, temp(77_001, now + UP_PACE_NS)).1, None); + let later = now + 2 * UP_PACE_NS; + assert_eq!(p.update(later, temp(77_000, later)).1.map(|c| (c.to, c.why)), Some((8, Why::Cool))); + } + + #[test] + fn a_higher_hot_keeps_the_cap_where_the_default_would_lower_it() { + for (hot_c, lowers_at_84) in [(80, true), (85, false), (88, false), (92, false)] { + let (mut p, t) = raised(hot_c, 10); + let now = t + 500 * MS; + assert_eq!(p.update(now, temp(84_000, now)).1.is_some(), lowers_at_84, "hot {hot_c}"); + } + // Critical follows hot: 88 C is critical at the default, merely hot at 85 and nothing + // at 92. + for (hot_c, cap) in [(80, 5), (85, 7), (92, 8)] { + let (mut p, t) = raised(hot_c, 10); + let now = t + 500 * MS; + let _ = p.update(now, temp(88_000, now)); + assert_eq!(p.cap(), cap, "hot {hot_c}"); + } + } + + #[test] + fn thresholds_changed_at_runtime_apply_from_the_next_reading_with_the_usual_pacing() { + // Running at 8 with hot 92, 86 C holds. + let (mut p, t) = raised(92, 10); + let now = t + 500 * MS; + assert_eq!(p.update(now, temp(86_000, now)).1, None); + // Hot back to 85 at runtime. The cap does not move by itself. + let (hot, gap, _) = clamp_thresholds(85, 10); + assert!(p.set_thresholds(hot, gap)); + assert_eq!(p.cap(), 8); + // The next update lowers one state on that reading, which was not acted on yet. Acted + // on once, it is not used again. The next fresh reading 500 ms later lowers one more. + let (_, change) = p.update(now + MS, temp(86_000, now)); + assert_eq!(change.map(|c| (c.to, c.why)), Some((7, Why::Hot))); + assert_eq!(p.update(now + 501 * MS, temp(86_000, now)).1, None); + let next = now + 501 * MS; + assert_eq!(p.update(next, temp(86_001, next)).1.map(|c| (c.to, c.why)), Some((6, Why::Hot))); + // Hot 80 at runtime. 88.5 C is now critical, so the safe cap at once without pacing. + let (hot, gap, _) = clamp_thresholds(80, 10); + assert!(p.set_thresholds(hot, gap)); + let soon = next + MS; + assert_eq!(p.update(soon, temp(88_500, soon)).1.map(|c| (c.to, c.why)), Some((5, Why::Critical))); + // A gap of 3 at runtime. 77 C now raises again after 2 s. + let (hot, gap, _) = clamp_thresholds(80, 3); + assert!(p.set_thresholds(hot, gap)); + let up = soon + UP_PACE_NS; + assert_eq!(p.update(up, temp(77_000, up)).1.map(|c| (c.to, c.why)), Some((6, Why::Cool))); + // Unusable readings still drop to the safe cap whatever the thresholds. + let gone = up + MS; + assert_eq!(p.update(gone, Reading::None).1.map(|c| c.to), Some(5)); + } +} diff --git a/drivers/gpu/drm/asahi/m3/t8122_admission.rs b/drivers/gpu/drm/asahi/m3/t8122_admission.rs index fb280ebcde520..ff6d098f50a3c 100644 --- a/drivers/gpu/drm/asahi/m3/t8122_admission.rs +++ b/drivers/gpu/drm/asahi/m3/t8122_admission.rs @@ -4,6 +4,12 @@ use super::m3_profile::Profile; +/// The T8122 boards the M3 runtime starts on by default: the M3 MacBook Airs (13" J613, 15" J615) +/// and the 14" M3 MacBook Pro (J504). All three load the same 14.8.3 G15G image (UUID +/// `DF697F05-…`, checked separately by firmware admission). The other T8122 Macs (the J433 and +/// J434 iMacs) are not listed. +pub(crate) const T8122_BOARDS: [&[u8]; 3] = [b"apple,j613", b"apple,j615", b"apple,j504"]; + #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub(crate) struct Identity { pub(crate) active_cores: u32, @@ -195,6 +201,17 @@ pub(crate) fn read_identity_for( #[cfg(test)] mod tests { + use super::T8122_BOARDS; + + #[test] + fn the_t8122_runtime_boards_are_exactly_the_airs_and_the_14_inch_pro() { + // Exact: dropping the J504 or adding another T8122 Mac (J433, J434) must fail here. + assert_eq!(T8122_BOARDS, [&b"apple,j613"[..], b"apple,j615", b"apple,j504"]); + for other in [&b"apple,j433"[..], b"apple,j434", b"apple,t8122"] { + assert!(!T8122_BOARDS.contains(&other)); + } + } + use super::*; const G15G_C0: u32 = 0x0702_2000; diff --git a/drivers/gpu/drm/asahi/m3/t8122_knobs.rs b/drivers/gpu/drm/asahi/m3/t8122_knobs.rs index ca0f8a6d499cc..b7390eb11ecdc 100644 --- a/drivers/gpu/drm/asahi/m3/t8122_knobs.rs +++ b/drivers/gpu/drm/asahi/m3/t8122_knobs.rs @@ -27,10 +27,80 @@ pub(crate) const UNIT_MASK_A_LIMIT: u64 = 0x7_0000_0003; pub(crate) const UNIT_MASK_B_LIMIT: u64 = 0x7; /// The default performance-state ceiling: the lowest two states. pub(crate) const PSTATE_CAP: u32 = 2; -/// The highest performance-state ceiling the experiment accepts. The same as the driver's own -/// limit when the fast-die temperature controller is off (`m3_adt_config::ADT_MAX_PSTATE_LIMIT`): -/// J613/J615 have no SoC die temperature zone yet, so there is no thermal feedback above it. -pub(crate) const PSTATE_CAP_MAX: u32 = 5; +/// The runtime cap every T8122 boot starts at and falls back to, whatever the ceiling: at +/// boot, before a usable SoC die temperature, without one, and at the critical temperature. A +/// lower ceiling still wins. `m3_thermal_policy::T8122_SAFE_PSTATE`, which `t8122_start` +/// asserts is the same number. +pub(crate) const PSTATE_FALLBACK: u32 = 2; +/// The highest ceiling admitted without the thermal limit on: the driver's own limit when the +/// fast-die temperature controller is off (`m3_thermal_policy::ADT_MAX_PSTATE_LIMIT`, which +/// `t8122_start` asserts is the same number). A higher cap with `asahi.m3_thermal=off` or `hold` +/// is lowered to it, so the GPU still starts. +pub(crate) const PSTATE_CAP_NO_FEEDBACK_MAX: u32 = 5; +/// The highest performance-state ceiling the experiment accepts: the eight states of the M3 +/// Airs' table. A ceiling above [`PSTATE_CAP_NO_FEEDBACK_MAX`] needs the thermal limit on +/// (`m3_thermal`) and is only how far it may raise the runtime cap while it reads a usable SoC +/// die temperature; see [`pstate_cap_use`]. +pub(crate) const PSTATE_CAP_MAX: u32 = 8; + +/// How the thermal limit is set for this boot (`asahi.m3_thermal`), as far as the ceiling cares. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub(crate) enum Feedback { + /// `off`: nothing reads a temperature, and the cap is fixed. + Off, + /// `hold`: the temperature is read and logged, the runtime cap stays at its fallback. + Hold, + /// `on` (the default): the runtime cap follows the temperature. + On, +} + +/// What an accepted ceiling of `asahi.t8122_pstate_cap` gives the firmware. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub(crate) struct CapUse { + /// The highest state the firmware is told about (the InitData's highest-state words). + pub(crate) ceiling: u32, + /// The runtime cap at boot, and whenever there is no usable temperature or it is critical. + pub(crate) fallback: u32, + /// Whether the runtime cap may rise above `fallback` at all this boot. + pub(crate) ranges: bool, +} + +/// The cap `asahi.t8122_pstate_cap` (1..=[`PSTATE_CAP_MAX`]) leaves with the thermal limit set +/// to `feedback`: itself, except that a cap above [`PSTATE_CAP_NO_FEEDBACK_MAX`] needs the limit +/// on and is otherwise lowered to that limit. This is the ceiling `m3_adt_config::PstatePolicy` +/// is given. +pub(crate) fn admitted_cap(cap: u32, feedback: Feedback) -> u32 { + if cap > PSTATE_CAP_NO_FEEDBACK_MAX && feedback != Feedback::On { + PSTATE_CAP_NO_FEEDBACK_MAX + } else { + cap + } +} + +/// The use of the ceiling `cap` (1..=[`PSTATE_CAP_MAX`], as [`resolve`] accepts it) with the +/// thermal limit set to `feedback`. +/// +/// The runtime cap starts at [`PSTATE_FALLBACK`] (or a lower `cap`) and falls back to it in +/// every mode. `On` lets the thermal limit raise it up to the ceiling `cap` while it reads a +/// usable SoC die temperature; only `On` admits a cap above [`PSTATE_CAP_NO_FEEDBACK_MAX`]. +/// `Hold` gives the firmware the ceiling but keeps the runtime cap at the fallback (to check that +/// the firmware follows a runtime cap below its ceiling), and lowers a cap above the no-feedback +/// limit to that limit first. `Off` has no temperature feedback, so the fallback is also the +/// ceiling (a cap above the no-feedback limit is lowered first): the GPU starts rather than not +/// at all. +/// +/// This is what `m3_adt_config::PstatePolicy::new` computes (`ThermalMode::pstate_caps` with +/// [`admitted_cap`]) while `asahi.m3_max_pstate` is not given (that parameter can only lower the +/// ceiling further); `m3_adt_config::Contents::select` refuses the start if it ever computes more. +pub(crate) fn pstate_cap_use(cap: u32, feedback: Feedback) -> CapUse { + let ceiling = admitted_cap(cap, feedback); + let fallback = ceiling.min(PSTATE_FALLBACK); + match feedback { + Feedback::Off => CapUse { ceiling: fallback, fallback, ranges: false }, + Feedback::Hold => CapUse { ceiling, fallback, ranges: false }, + Feedback::On => CapUse { ceiling, fallback, ranges: ceiling > fallback }, + } +} /// Parse a decimal or `0x`-prefixed hexadecimal `u64`. A leading sign is not accepted. fn number(text: &str) -> Option { @@ -208,7 +278,7 @@ pub(crate) fn resolve(raw: &Raw) -> Result { .ok_or(refuse("t8122_unit_mask_b", "nonzero, no bits outside 0x7"))? as u32; let pstate_cap = given(raw.pstate_cap, PSTATE_CAP as u64) .filter(|v| (1..=u64::from(PSTATE_CAP_MAX)).contains(v)) - .ok_or(refuse("t8122_pstate_cap", "1 to 5"))? as u32; + .ok_or(refuse("t8122_pstate_cap", "1 to 8"))? as u32; Ok(Values { initdata_version, fender, @@ -310,7 +380,7 @@ mod tests { (|r| r.unit_mask_b = parse_number("0xf"), "t8122_unit_mask_b"), (|r| r.unit_mask_b = 0, "t8122_unit_mask_b"), (|r| r.pstate_cap = 0, "t8122_pstate_cap"), - (|r| r.pstate_cap = 6, "t8122_pstate_cap"), + (|r| r.pstate_cap = 9, "t8122_pstate_cap"), (|r| r.pstate_cap = 16, "t8122_pstate_cap"), (|r| r.pstate_cap = parse_number("-1"), "t8122_pstate_cap"), ]; @@ -319,15 +389,76 @@ mod tests { } } + const FEEDBACK: [Feedback; 3] = [Feedback::Off, Feedback::Hold, Feedback::On]; + #[test] - fn pstate_cap_accepts_one_to_five_only() { + fn pstate_cap_accepts_one_to_eight_only() { for cap in 1..=PSTATE_CAP_MAX { assert_eq!(resolve(&with(|r| r.pstate_cap = u64::from(cap))).unwrap().pstate_cap, cap); } - for cap in [0u64, 6, 8, 15, 16] { + for cap in [0u64, 9, 15, 16, u64::from(u32::MAX) + 6] { assert!(resolve(&with(|r| r.pstate_cap = cap)).is_err(), "cap {cap}"); } - assert_eq!(PSTATE_CAP_MAX, 5); + assert_eq!(PSTATE_CAP_MAX, 8); + assert_eq!(PSTATE_CAP_NO_FEEDBACK_MAX, 5); + // The default is unchanged and needs no feedback. + let default = resolve(&with(|_| {})).unwrap().pstate_cap; + assert_eq!(default, 2); + for feedback in FEEDBACK { + assert_eq!( + pstate_cap_use(default, feedback), + CapUse { ceiling: 2, fallback: 2, ranges: false } + ); + } + } + + #[test] + fn caps_up_to_five_keep_the_state_two_fallback() { + for cap in 1..=PSTATE_CAP_NO_FEEDBACK_MAX { + let fallback = cap.min(2); + for feedback in FEEDBACK { + assert_eq!(admitted_cap(cap, feedback), cap, "cap {cap} {feedback:?}"); + } + // On: the runtime cap ranges from 2 (or a lower cap) to the ceiling. + assert_eq!( + pstate_cap_use(cap, Feedback::On), + CapUse { ceiling: cap, fallback, ranges: cap > 2 } + ); + // Hold: the firmware knows the ceiling, the runtime cap stays at the fallback. + assert_eq!( + pstate_cap_use(cap, Feedback::Hold), + CapUse { ceiling: cap, fallback, ranges: false } + ); + // Off: a fixed cap of 2 (or lower). + assert_eq!( + pstate_cap_use(cap, Feedback::Off), + CapUse { ceiling: fallback, fallback, ranges: false } + ); + } + } + + #[test] + fn caps_six_to_eight_need_the_thermal_limit_on_and_fall_back_to_two() { + for cap in 6..=PSTATE_CAP_MAX { + // On: the runtime cap ranges from 2 to the ceiling. + assert_eq!(admitted_cap(cap, Feedback::On), cap); + assert_eq!( + pstate_cap_use(cap, Feedback::On), + CapUse { ceiling: cap, fallback: 2, ranges: true } + ); + // Hold: lowered to 5; the firmware knows 5, the runtime cap stays 2. + assert_eq!(admitted_cap(cap, Feedback::Hold), 5); + assert_eq!( + pstate_cap_use(cap, Feedback::Hold), + CapUse { ceiling: 5, fallback: 2, ranges: false } + ); + // Off: lowered to 5, and without temperature feedback the GPU starts at a fixed 2. + assert_eq!(admitted_cap(cap, Feedback::Off), 5); + assert_eq!( + pstate_cap_use(cap, Feedback::Off), + CapUse { ceiling: 2, fallback: 2, ranges: false } + ); + } } #[test] diff --git a/drivers/gpu/drm/asahi/m3/t8122_start.rs b/drivers/gpu/drm/asahi/m3/t8122_start.rs index 737034dd6f8c1..8ceeeef3b25c9 100644 --- a/drivers/gpu/drm/asahi/m3/t8122_start.rs +++ b/drivers/gpu/drm/asahi/m3/t8122_start.rs @@ -16,7 +16,14 @@ //! | `asahi.t8122_sgx_setup` | `t6030`: SGX+0xd14000 = 0x70001 | `none` | //! | `asahi.t8122_unit_mask_a` | `0x700000003` | nonzero, within `0x700000003` | //! | `asahi.t8122_unit_mask_b` | `0x7` | nonzero, within `0x7` | -//! | `asahi.t8122_pstate_cap` | `2` | 1 to 5 | +//! | `asahi.t8122_pstate_cap` | `2` | 1 to 5; 6 to 8 with `asahi.m3_thermal=on` | +//! +//! A cap above 2 is the thermal limit's ceiling (`m3_thermal`), not a fixed cap: the runtime +//! cap starts at 2 and rises above it only while `asahi.m3_thermal` is on (the default) and the +//! limit reads a usable SoC die temperature; it is back at 2 without one or at the critical +//! temperature. With `hold` it stays at 2; with `off` the cap is a fixed 2. A cap of 6 to 8 +//! needs `on`: with `hold` or `off` it is lowered to 5 first, with one boot line, and the GPU +//! still starts (`t8122_knobs::pstate_cap_use`). //! //! The defaults are the T8122 table's values. A parameter given with a value it does not accept //! refuses the start. Unlisted boards remain refused by the existing backend admission. @@ -32,10 +39,11 @@ use crate::{ m3::m3_adt_config::T8122_IO_MAPPINGS, m3::m3_init_storage::{self as storage, IoMap}, m3::m3_soc::{IoMapping, Soc, T8122_HWDATA_B}, + m3::m3_thermal_policy::ThermalMode, pgtable::{prot, Prot}, m3::t8122_knobs::{ - self as knobs, ClockGen, Start, Values, CLOCK_GEN_E1C, CLOCK_GEN_E5C, FENDER_ADT, - FENDER_RULE, + self as knobs, CapUse, ClockGen, Feedback, Start, Values, CLOCK_GEN_E1C, CLOCK_GEN_E5C, + FENDER_ADT, FENDER_RULE, }, }; @@ -138,6 +146,16 @@ const _: () = { assert!(knobs::UNIT_MASK_B as u64 & !knobs::UNIT_MASK_B_LIMIT == 0); assert!(offset_of!(raw::HwDataBG15V14_8_3, unit_mask_a) == 0x17c0); assert!(offset_of!(raw::HwDataBG15V14_8_3, unit_mask_b) == 0x17c8); + // The ceiling the experiment accepts without the thermal limit on is the driver's own + // no-feedback limit, the fallback is the T8122 safe cap that `PstatePolicy::new` computes + // (`ThermalMode::pstate_caps`), and the default ceiling needs no feedback. + assert!( + knobs::PSTATE_CAP_NO_FEEDBACK_MAX == crate::m3::m3_thermal_policy::ADT_MAX_PSTATE_LIMIT + ); + assert!(knobs::PSTATE_FALLBACK == crate::m3::m3_thermal_policy::T8122_SAFE_PSTATE); + assert!(knobs::PSTATE_FALLBACK <= knobs::PSTATE_CAP); + assert!(knobs::PSTATE_CAP <= knobs::PSTATE_CAP_NO_FEEDBACK_MAX); + assert!(knobs::PSTATE_CAP_NO_FEEDBACK_MAX <= knobs::PSTATE_CAP_MAX); // An armed start skips `require_complete`. The only `unported` item of the T8122 table is the // opt-in itself; if another is added, this fails the build until the start accounts for it. assert!(crate::m3::m3_soc::T8122.unported.len() == 1); @@ -224,6 +242,32 @@ impl Experiment { pub(crate) fn pstate_cap(&self) -> u32 { self.0.pstate_cap } + + /// What that cap gives the firmware with the thermal limit in `mode`: the ceiling, the + /// runtime cap's fallback and whether the runtime cap may rise above it. + pub(crate) fn cap_use(&self, mode: ThermalMode) -> CapUse { + knobs::pstate_cap_use(self.0.pstate_cap, feedback(mode)) + } + + /// The ceiling `m3_adt_config::PstatePolicy::new` is given with the thermal limit in + /// `mode`: the cap, lowered to 5 if it is above 5 and the limit is not on. + pub(crate) fn admitted_cap(&self, mode: ThermalMode) -> u32 { + knobs::admitted_cap(self.0.pstate_cap, feedback(mode)) + } +} + +/// The thermal limit's mode as the ceiling sees it. +fn feedback(mode: ThermalMode) -> Feedback { + match mode { + ThermalMode::Off => Feedback::Off, + ThermalMode::Hold => Feedback::Hold, + ThermalMode::On => Feedback::On, + } +} + +/// `asahi.m3_thermal`; unset is on, as in `m3_adt_config::PstatePolicy::new`. +fn thermal_mode() -> ThermalMode { + crate::m3::m3_params::thermal_param().unwrap_or(ThermalMode::On) } /// Firmware page protection of the IO map of `slot`, with the read-only slots `read_only`. @@ -280,6 +324,10 @@ pub(crate) fn arm(dev: &device::Device, soc: &Soc) -> Result> ); ENODEV })?; + // A cap above the no-feedback limit is the thermal limit's ceiling. Without the limit on it + // is lowered to the no-feedback limit (below), so the GPU still starts. + let mode = thermal_mode(); + let u = knobs::pstate_cap_use(v.pstate_cap, feedback(mode)); let e = Experiment(v); dev_warn!( dev, @@ -301,17 +349,59 @@ pub(crate) fn arm(dev: &device::Device, soc: &Soc) -> Result> e.mapping_count(), storage::IOMAP_BASE ); - // J613/J615 have no SoC die temperature zone yet, so a cap above the no-feedback limit runs - // the fanless GPU without any temperature feedback (`m3_thermal` holds the cap but gets no - // reading). The runtime cap and the firmware ceiling stay equal, because the cap is at most - // the no-feedback limit. - if v.pstate_cap > knobs::PSTATE_CAP { + // The runtime cap starts at the fallback (2) and the firmware may use states up to the + // ceiling only as far as the runtime cap allows. Whether a SoC die temperature zone exists is + // not known here: the SMC hwmon driver that registers it may probe seconds after the GPU + // (7.6 s against 1.6 s on a J615). With the thermal limit on or holding (the default), + // `m3_thermal` says so when its reading finds no zone. `asahi.m3_max_pstate` can lower the + // ceiling further (`PstatePolicy::new`); the "firmware highest state" line then has the + // result. + let lowered = if crate::m3::m3_params::max_pstate_param().is_some() { + " (asahi.m3_max_pstate can lower that; see the firmware highest state line)" + } else { + "" + }; + let admitted = e.admitted_cap(mode); + // The print macros take plain expressions only, so the mode's name is chosen here. + let mode_name = if mode == ThermalMode::Off { "off" } else { "hold" }; + if admitted < v.pstate_cap { + dev_warn!( + dev, + "M3 G15G start: pstate_cap={} lowered to {}: states above {} need asahi.m3_thermal=on (the default); with asahi.m3_thermal={} the firmware gets states up to {} and the runtime cap stays at {}\n", + v.pstate_cap, + admitted, + knobs::PSTATE_CAP_NO_FEEDBACK_MAX, + mode_name, + u.ceiling, + u.fallback + ); + } else if u.ranges { + dev_warn!( + dev, + "M3 G15G start: pstate_cap={} is above {}: the runtime cap starts at {} and rises to at most {}{} only while the thermal limit reads a usable SoC die temperature (asahi.m3_thermal=on)\n", + v.pstate_cap, + u.fallback, + u.fallback, + u.ceiling, + lowered + ); + } else if u.ceiling > u.fallback { + dev_warn!( + dev, + "M3 G15G start: pstate_cap={} is above {}: asahi.m3_thermal=hold gives the firmware states up to {}{} and keeps the runtime cap at {} for this boot\n", + v.pstate_cap, + u.fallback, + u.ceiling, + lowered, + u.fallback + ); + } else if v.pstate_cap > u.ceiling { dev_warn!( dev, - "M3 G15G start: pstate_cap={} is above {}: no SoC die temperature zone on this board, so states up to {} run with no temperature feedback\n", + "M3 G15G start: pstate_cap={} with the thermal limit off (asahi.m3_thermal=off): states above {} need temperature feedback, so the firmware gets states up to {}\n", v.pstate_cap, - knobs::PSTATE_CAP, - v.pstate_cap + u.ceiling, + u.ceiling ); } Ok(Some(e)) diff --git a/drivers/gpu/drm/asahi/mmu.rs b/drivers/gpu/drm/asahi/mmu.rs index 4337024993025..34c697be9bdb7 100644 --- a/drivers/gpu/drm/asahi/mmu.rs +++ b/drivers/gpu/drm/asahi/mmu.rs @@ -505,10 +505,10 @@ fn lock_vm( Ok(VmExecGuard(unsafe { gpuvm.exec_lock(obj, interruptible)? })) } -#[cfg(any(CONFIG_DRM_ASAHI_G15, CONFIG_DRM_ASAHI_G16))] +#[cfg(any(CONFIG_DRM_ASAHI_UAT_ADOPTION, CONFIG_DRM_ASAHI_G16))] pub(crate) struct VmReadGuard(VmExecGuard); -#[cfg(any(CONFIG_DRM_ASAHI_G15, CONFIG_DRM_ASAHI_G16))] +#[cfg(any(CONFIG_DRM_ASAHI_UAT_ADOPTION, CONFIG_DRM_ASAHI_G16))] impl VmReadGuard { #[cfg(CONFIG_DRM_ASAHI_G15)] pub(crate) fn covers_range(&mut self, address: u64, size: u64, read: bool, write: bool) -> bool { @@ -2596,7 +2596,7 @@ impl Vm { /// Attach a status and job lifetime tracking before sharing a newly created user VM with /// queues, without the G17 canonical context views, shared-object footprints and idle /// residency reclaim of [`Self::with_status`]. Used by G15, whose user objects stay pinned. - #[cfg(any(CONFIG_DRM_ASAHI_G15, CONFIG_DRM_ASAHI_G16))] + #[cfg(any(CONFIG_DRM_ASAHI_UAT_ADOPTION, CONFIG_DRM_ASAHI_G16))] pub(crate) fn with_tracked_status(mut self) -> Result { self.status = Some(Arc::pin_init( crate::g17::status::VmStatus::new(), @@ -2624,17 +2624,17 @@ impl Vm { /// and invalidated to the point of coherency first (never only invalidated: CPU-written lines /// may be dirty), so the copy sees what the GPU or a write-combined CPU view stored. The /// mappings cannot change during the copy (GPUVM exec lock). - #[cfg(any(CONFIG_DRM_ASAHI_G15, CONFIG_DRM_ASAHI_G16))] + #[cfg(any(CONFIG_DRM_ASAHI_UAT_ADOPTION, CONFIG_DRM_ASAHI_G16))] pub(crate) fn read_user(&self, va: u64, buf: &mut [u8]) -> Result { self.read_lock()?.read_user(va, buf) } - #[cfg(any(CONFIG_DRM_ASAHI_G15, CONFIG_DRM_ASAHI_G16))] + #[cfg(any(CONFIG_DRM_ASAHI_UAT_ADOPTION, CONFIG_DRM_ASAHI_G16))] pub(crate) fn read_lock(&self) -> Result { Ok(VmReadGuard(lock_vm(&self.gpuvm, None, false)?)) } - #[cfg(any(CONFIG_DRM_ASAHI_G15, CONFIG_DRM_ASAHI_G16))] + #[cfg(any(CONFIG_DRM_ASAHI_UAT_ADOPTION, CONFIG_DRM_ASAHI_G16))] fn read_user_locked(inner: &mut VmInner, va: u64, buf: &mut [u8]) -> Result { let mut done = 0usize; while done < buf.len() { @@ -3477,7 +3477,7 @@ impl Drop for VmInner { /// Physical regions of a UAT whose handoff the generation has already established itself /// ([`Uat::new_adopted`]). None of them needs to be in the kernel linear map. -#[cfg(any(CONFIG_DRM_ASAHI_G15, CONFIG_DRM_ASAHI_G16))] +#[cfg(any(CONFIG_DRM_ASAHI_UAT_ADOPTION, CONFIG_DRM_ASAHI_G16))] pub(crate) struct AdoptedRegions { /// The handoff region (PPL magic, lock and flush requests). pub(crate) handoff: Range, @@ -3500,7 +3500,7 @@ impl Uat { }) } - #[cfg(any(CONFIG_DRM_ASAHI_G15, CONFIG_DRM_ASAHI_G16))] + #[cfg(any(CONFIG_DRM_ASAHI_UAT_ADOPTION, CONFIG_DRM_ASAHI_G16))] /// Map a region given by its physical range, write-combined. fn map_physical( range: &Range, @@ -3521,7 +3521,7 @@ impl Uat { let res: io::resource::Resource = unsafe { core::mem::transmute(kernel::types::Opaque::new(raw)) }; let flags = io::mem::MemFlag::WC; - #[cfg(CONFIG_DRM_ASAHI_G15)] + #[cfg(CONFIG_DRM_ASAHI_UAT_ADOPTION)] let flags = if reserved_ram { // G15 admits only whole reserved RAM pages or wholly non-RAM carveouts. if crate::g15::memory_mode(range.start, size as u64)? @@ -3534,7 +3534,7 @@ impl Uat { } else { flags }; - #[cfg(not(CONFIG_DRM_ASAHI_G15))] + #[cfg(not(CONFIG_DRM_ASAHI_UAT_ADOPTION))] let _ = reserved_ram; // SAFETY: The UAT regions are only accessed through this mapping by the CPU; // the GPU reaches them through its own translation. @@ -3545,7 +3545,7 @@ impl Uat { }) } - #[cfg(any(CONFIG_DRM_ASAHI_G15, CONFIG_DRM_ASAHI_G16))] + #[cfg(any(CONFIG_DRM_ASAHI_UAT_ADOPTION, CONFIG_DRM_ASAHI_G16))] /// Creates a `Uat` for a handoff that the caller establishes itself (G15). /// /// This only maps the handoff and the TTBAT (write-combined); neither is written here. The @@ -3571,7 +3571,7 @@ impl Uat { /// Creates the same owned-RAM adopted UAT, choosing whether application contexts also /// name the firmware upper root. M4 qualifies only context 0 for firmware-upper access; /// its application TTBAT upper words stay zero. G15 preserves its existing shared upper. - #[cfg(any(CONFIG_DRM_ASAHI_G16, CONFIG_DRM_ASAHI_M3_T8122))] + #[cfg(CONFIG_DRM_ASAHI_G16)] pub(crate) fn new_adopted_with_user_upper( dev: &driver::AsahiDrmDevice, geometry: UatGeometry, @@ -3581,7 +3581,21 @@ impl Uat { Self::new_adopted_inner(dev, geometry, regions, map_kernel_to_user, false) } - #[cfg(any(CONFIG_DRM_ASAHI_G15, CONFIG_DRM_ASAHI_G16))] + /// As [`Self::new_adopted_with_user_upper`], for regions that may be reserved System RAM: + /// each is mapped write-back when it is RAM and write-combined when it is a carveout, as + /// [`Self::new_adopted`] does. The M3 boot loaders leave the handoff and TTBAT regions in + /// the kernel's memory map as reserved RAM, which a write-combined mapping is refused for. + #[cfg(CONFIG_DRM_ASAHI_M3_T8122)] + pub(crate) fn new_adopted_ram_with_user_upper( + dev: &driver::AsahiDrmDevice, + geometry: UatGeometry, + regions: &AdoptedRegions, + map_kernel_to_user: bool, + ) -> Result { + Self::new_adopted_inner(dev, geometry, regions, map_kernel_to_user, true) + } + + #[cfg(any(CONFIG_DRM_ASAHI_UAT_ADOPTION, CONFIG_DRM_ASAHI_G16))] fn new_adopted_inner( dev: &driver::AsahiDrmDevice, geometry: UatGeometry, @@ -3672,13 +3686,13 @@ impl Uat { }) } - #[cfg(any(CONFIG_DRM_ASAHI_G15, CONFIG_DRM_ASAHI_G16))] + #[cfg(any(CONFIG_DRM_ASAHI_UAT_ADOPTION, CONFIG_DRM_ASAHI_G16))] /// Root of the kernel lower `Vm`, for TTBAT context 0 of an adopted handoff. pub(crate) fn kernel_lower_root(&self) -> PhysicalAddr { self.kernel_lower_vm.ttb() } - #[cfg(any(CONFIG_DRM_ASAHI_G15, CONFIG_DRM_ASAHI_G16))] + #[cfg(any(CONFIG_DRM_ASAHI_UAT_ADOPTION, CONFIG_DRM_ASAHI_G16))] /// Root whose driver-window entry names the second-level table of [`Self::kernel_vm`]'s /// driver window. With [`Self::new_adopted`] this is the shadow root that no context names. pub(crate) fn kernel_window_root(&self) -> PhysicalAddr { diff --git a/drivers/gpu/drm/asahi/pgtable.rs b/drivers/gpu/drm/asahi/pgtable.rs index 9fb2d0652e01e..0cc2fac3b340a 100644 --- a/drivers/gpu/drm/asahi/pgtable.rs +++ b/drivers/gpu/drm/asahi/pgtable.rs @@ -369,7 +369,7 @@ impl Prot { /// The protection bits of a leaf with this protection (no address, table type or non-global /// bits), for checking that a firmware leaf word round-trips through [`Self::from_pte`]. - #[cfg(any(CONFIG_DRM_ASAHI_G15, CONFIG_DRM_ASAHI_G16))] + #[cfg(any(CONFIG_DRM_ASAHI_UAT_ADOPTION, CONFIG_DRM_ASAHI_G16))] pub(crate) const fn leaf_bits(&self) -> u64 { self.as_pte() } diff --git a/drivers/hwmon/macsmc-hwmon.c b/drivers/hwmon/macsmc-hwmon.c index c519c70755c00..4392192dbe3be 100644 --- a/drivers/hwmon/macsmc-hwmon.c +++ b/drivers/hwmon/macsmc-hwmon.c @@ -1416,6 +1416,18 @@ static int macsmc_hwmon_die_register(struct macsmc_hwmon *hwmon) hwmon->die_listed); return 0; } + /* + * MacBook Air (15-inch, M3): all twelve keys are readable floats on + * J615. The Tg readings rose during a GPU workload and fell after + * it stopped. Require the complete CPU/GPU set, not a CPU-only zone + * if discovery of a GPU key fails. + */ + if (of_machine_is_compatible("apple,j615") && + hwmon->die_count != hwmon->die_listed) { + dev_warn(hwmon->dev, "no SoC die temperature zone: only %u of %u required keys is present\n", + hwmon->die_count, hwmon->die_listed); + return 0; + } for (i = 0; i < hwmon->temp.count && len + 6 <= sizeof(keys); i++) { smc_key key = hwmon->temp.sensors[i].macsmc_key; diff --git a/drivers/pmdomain/apple/pmp-report.c b/drivers/pmdomain/apple/pmp-report.c index 42bff817bfa78..b375a83b9e0a2 100644 --- a/drivers/pmdomain/apple/pmp-report.c +++ b/drivers/pmdomain/apple/pmp-report.c @@ -687,6 +687,44 @@ static int apple_pmp_report_wait_t6030_14(struct device *supplier, unsigned int return ret; } +/* + * The T8122 (M3 MacBook Air) GPU on its 14.8.3 firmware, for the M3 runtime + * (APPLE_T8122_GPU_PMP_LINK): the unique T8122 report linked to this PMP, and + * its display request (DISP, reg 7) READY and acknowledged before AGX, as on + * the 7.1.12 kernel that runtime was qualified with. + */ +static int apple_pmp_report_wait_t8122_14(struct device *supplier, unsigned int timeout_ms) +{ + struct device_node *np, *report = NULL, *pmp, *entry; + int ret; + + if (!IS_ENABLED(CONFIG_APPLE_T8122_GPU_PMP_LINK) || + !of_machine_is_compatible("apple,t8122") || + !of_device_is_compatible(supplier->of_node, "apple,t8122-pmp-v2")) + return -ENODEV; + for_each_compatible_node(np, NULL, "apple,t8122-pmp-v2-report") { + if (report) { + of_node_put(np); + of_node_put(report); + return -ENODEV; + } + report = of_node_get(np); + } + if (!report || !of_device_is_available(report) || + of_property_count_u32_elems(report, "apple,pmp") != 1) { + of_node_put(report); + return -ENODEV; + } + pmp = of_parse_phandle(report, "apple,pmp", 0); + entry = apple_pmp_report_t6030_entry(report); + ret = pmp == supplier->of_node && entry ? + apple_pmp_report_wait_ready(entry, msecs_to_jiffies(timeout_ms)) : -ENODEV; + of_node_put(entry); + of_node_put(pmp); + of_node_put(report); + return ret; +} + /* The native 25G83 PMP starts through its supplier topology, after requests * are seeded. Its report is pinned against removal while readiness is read. */ @@ -701,8 +739,11 @@ int apple_pmp_report_wait_supplier_ready(struct device *supplier, unsigned int t int ret; /* A declared report always takes the existing native25 path below. */ - if (!np && !of_property_present(supplier->of_node, "apple,pmp-report")) + if (!np && !of_property_present(supplier->of_node, "apple,pmp-report")) { + if (of_machine_is_compatible("apple,t8122")) + return apple_pmp_report_wait_t8122_14(supplier, timeout_ms); return apple_pmp_report_wait_t6030_14(supplier, timeout_ms); + } if (!np || !of_device_is_compatible(np, t8132 ? "apple,t8132-pmp-v2-report" : "apple,j613-25g83-pmp-report")) { of_node_put(np); diff --git a/drivers/soc/apple/Kconfig b/drivers/soc/apple/Kconfig index c8b50840a7dda..3f6db5ef17ad8 100644 --- a/drivers/soc/apple/Kconfig +++ b/drivers/soc/apple/Kconfig @@ -368,6 +368,15 @@ config APPLE_DISPLAY_GATE Say 'y' if you want the native display on M3 Pro, M3 Max, M3 and M4 Pro Macs. +config APPLE_T8122_GPU_PMP_LINK + bool + depends on APPLE_PMP_REPORT + default n + help + Let T8122 (M3 MacBook Air) GPU power commands wait for the display + request the PMP report acknowledges on the 14.8.3 firmware, as the + M3 runtime of DRM_ASAHI_M3_T8122 requires. + config APPLE_T6030_GPU_PMP_LINK bool depends on APPLE_DISPLAY_GATE diff --git a/tools/asahi/dcp14-panel-tests.py b/tools/asahi/dcp14-panel-tests.py index 0abcd1e89d3f9..19c02cafd8201 100755 --- a/tools/asahi/dcp14-panel-tests.py +++ b/tools/asahi/dcp14-panel-tests.py @@ -87,7 +87,25 @@ machine="apple,j613";const struct dcp_v14_board*j=iomfb_v14_7_board(&dev); assert(!IS_ERR(j)&&j->panel_width==2560 && j->panel_height==1664); machine="apple,j700";assert(IS_ERR(iomfb_v14_7_board(&dev))); - machine="apple,j504";assert(IS_ERR(iomfb_v14_7_board(&dev))); + /* J504 (14" M3 MacBook Pro): the J613 image, the J514 panel, 60 or 120 Hz. */ + machine="apple,j504";const struct dcp_v14_board*p14=iomfb_v14_7_board(&dev); + assert(!IS_ERR(p14)&&p14&&!strcmp(p14->name,"J504")&&!strcmp(p14->debugfs,"dcp-j504")); + assert(p14->panel_width==3024&&p14->panel_height==1964&&p14->promotion&&p14->log_bringup); + assert(!strcmp(p14->firmware_uuid,"90F849E1-B422-367E-B389-50246F8DEC47")); + assert(!strcmp(p14->handoff,"apple,t8122-handoff")&&p14->panel_nits&&!p14->swap_timestamps); + assert(p14->ctm_set==A(421)&&p14->ctm_get==A(420)); + image_uuid=p14->firmware_uuid;assert(probe_gate(&dev,p14)==0); + image_uuid="C042E95C-B9D8-3F0E-94B3-582A08AA6FDD";assert(probe_gate(&dev,p14)==-ENODEV); + image_uuid="F493F03E-CC40-3D89-9A08-5F259BA2B246";assert(probe_gate(&dev,p14)==-ENODEV); + image_uuid=NULL;assert(probe_gate(&dev,p14)==-ENODEV); + image_uuid=p14->firmware_uuid;marker_value=0;assert(probe_gate(&dev,p14)==-ENODEV);marker_value=1; + {struct apple_dcp_v14 q={.dev=&dev,.board=p14};width=3024;height=1890;stride=width*4;notch=74; + assert(dcp_v14_geometry(&q)==0&&q.panel_height==1964&&q.notch_rows==74); + height=1964;notch=0;assert(dcp_v14_geometry(&q)==0&&q.panel_height==1964); + width=2560;height=1600;stride=width*4;notch=64;assert(dcp_v14_geometry(&q)==-EINVAL);} + /* Other T8122 Macs (J433/J434 iMacs) have no record. */ + machine="apple,j433";assert(IS_ERR(iomfb_v14_7_board(&dev))); + machine="apple,j434";assert(IS_ERR(iomfb_v14_7_board(&dev))); dcp.compatible="apple,t6030-dcp";machine="apple,j516s";assert(iomfb_v14_7_board(&dev)==&dcp_v14_board_t6030); dcp.compatible="apple,t8122-dcp"; struct apple_dcp_v14 v={.dev=&dev,.board=b};width=2880;height=1800;stride=width*4;notch=64; @@ -139,7 +157,7 @@ machine="apple,j613";assert(IS_ERR(iomfb_v14_7_board(&dev))); machine="apple,j516m";assert(IS_ERR(iomfb_v14_7_board(&dev))); dcp.compatible="apple,t6030-dcp";machine="apple,j516s";assert(iomfb_v14_7_board(&dev)==&dcp_v14_board_t6030); - puts("current J615/J613/T6030/J516C/J514C admission + hidden/full notch, wrong geometry controls: PASS"); + puts("current J615/J613/J504/T6030/J516C/J514C admission + hidden/full notch, wrong geometry controls: PASS"); #endif } ''' diff --git a/tools/asahi/g15-maintained-handoff-tests.py b/tools/asahi/g15-maintained-handoff-tests.py index fdafd799645cb..738facb331e52 100644 --- a/tools/asahi/g15-maintained-handoff-tests.py +++ b/tools/asahi/g15-maintained-handoff-tests.py @@ -137,12 +137,71 @@ def item(s, marker): OWNER_CALLS.set(0); let bytes = fixture(kind); assert!(parse(&dev, &Adt { data: &bytes }).is_err()); assert_eq!(OWNER_CALLS.get(), 0); } - assert_eq!(BUILDS.len(), 3, "maintained B0 firmware row must not require an absent profile symbol"); + assert_eq!(BUILDS.len(), 7, "maintained B0 firmware row must not require an absent profile symbol"); for (row, machine) in [(1, &b"apple,j516s"[..]), (2, &b"apple,j514s"[..])] { assert_eq!(BUILDS[row].machine.unwrap().to_bytes(), machine); assert_eq!((BUILDS[row].text_size, BUILDS[row].data_size, BUILDS[row].marker), (0x5c000, 0x114000, 0x58e88)); assert_eq!(BUILDS[row].text_sha256, [0xda,0x26,0xec,0xca,0xb1,0xf8,0x59,0x8d,0xa8,0x0c,0x85,0x5e,0xca,0x91,0x65,0x20,0x40,0x6d,0x1d,0x06,0x70,0xf1,0xd4,0x70,0xe7,0x9b,0xb9,0xb0,0x22,0x09,0xdb,0x53]); } + assert_eq!(BUILDS[4].machine.unwrap().to_bytes(), b"apple,j516s"); + assert_eq!((BUILDS[4].text_size, BUILDS[4].data_size, BUILDS[4].marker), (0x5c000, 0x114000, 0x58e88)); + assert_eq!(BUILDS[4].text_sha256, [0xcd,0x05,0xaf,0x6a,0x82,0x8b,0x52,0xe4,0xc2,0xfe,0xd2,0x00,0x86,0xd3,0xd1,0x8a,0x7d,0xb4,0x2e,0x03,0xa8,0xfd,0x6f,0x83,0x54,0xf9,0xf0,0x6e,0x0a,0x61,0x36,0x3e]); + // Issue #247: this J514S build must pin without the bring-up override. + use identity::lookup::{Measured, Verdict, verdict}; + let measured = Measured { + text_size: 0x5c000, data_size: 0x114000, marker: 0x58e88, + sha256: [0xcd,0x05,0xaf,0x6a,0x82,0x8b,0x52,0xe4, + 0xc2,0xfe,0xd2,0x00,0x86,0xd3,0xd1,0x8a, + 0x7d,0xb4,0x2e,0x03,0xa8,0xfd,0x6f,0x83, + 0x54,0xf9,0xf0,0x6e,0x0a,0x61,0x36,0x3e], + }; + let identify = |m: &Measured, machine: &[u8]| verdict(m, BUILDS.iter().map(|b| ( + Measured { text_size: b.text_size, data_size: b.data_size, + marker: b.marker, sha256: b.text_sha256 }, + b.machine.map_or(true, |board| board.to_bytes() == machine), + )), 0); + assert!(matches!(identify(&measured, b"apple,j514s"), Verdict::Pinned(3)), + "J514S cd05 firmware must pin without a prefix override"); + assert_eq!(identify(&measured, b"apple,j516s"), Verdict::Pinned(4)); + let mut changed = measured; + changed.sha256[31] ^= 1; + assert_eq!(identify(&changed, b"apple,j514s"), Verdict::Refused); + changed = measured; changed.text_size += 0x1000; + assert_eq!(identify(&changed, b"apple,j514s"), Verdict::Refused); + changed = measured; changed.data_size += 0x1000; + assert_eq!(identify(&changed, b"apple,j514s"), Verdict::Refused); + changed = measured; changed.marker += 8; + assert_eq!(identify(&changed, b"apple,j514s"), Verdict::Refused); + // Issue #247: the 4118 build pins on the J514S and the J516S, each to + // its own row, without the bring-up override. + let digest = [0x41,0x18,0x33,0xa7,0xde,0xd7,0x73,0xfe, + 0xea,0xc4,0xa1,0x81,0x55,0x2e,0x3f,0xf5, + 0x58,0x2e,0xaf,0x44,0x35,0xf2,0xdd,0x8f, + 0x62,0x0c,0x1e,0xd0,0xb3,0x1d,0xf1,0xb0]; + for (row, machine) in [(5, &b"apple,j514s"[..]), (6, &b"apple,j516s"[..])] { + assert_eq!(BUILDS[row].machine.unwrap().to_bytes(), machine); + assert_eq!((BUILDS[row].text_size, BUILDS[row].data_size, BUILDS[row].marker), (0x5c000, 0x114000, 0x58e88)); + assert_eq!(BUILDS[row].text_sha256, digest); + } + let measured = Measured { text_size: 0x5c000, data_size: 0x114000, marker: 0x58e88, sha256: digest }; + assert_eq!(identify(&measured, b"apple,j514s"), Verdict::Pinned(5), + "J514S 4118 firmware must pin without a prefix override"); + assert_eq!(identify(&measured, b"apple,j516s"), Verdict::Pinned(6), + "J516S 4118 firmware must pin without a prefix override"); + for other in [&b"apple,j514c"[..], b"apple,j516c", b"apple,j514m", b"apple,j516m", b"apple,t6030"] { + assert_eq!(identify(&measured, other), Verdict::Refused); + } + for machine in [&b"apple,j514s"[..], b"apple,j516s"] { + let mut changed = measured; + changed.sha256[31] ^= 1; + assert_eq!(identify(&changed, machine), Verdict::Refused); + changed = measured; changed.text_size += 0x1000; + assert_eq!(identify(&changed, machine), Verdict::Refused); + changed = measured; changed.data_size += 0x1000; + assert_eq!(identify(&changed, machine), Verdict::Refused); + changed = measured; changed.marker += 8; + assert_eq!(identify(&changed, machine), Verdict::Refused); + } for error in [0, -517, -12] { INIT_ERROR.set(error); let shared = Arc::new(Shared); let result = Rtkit::new(&dev, &shared); assert_eq!(result.is_ok(), error == 0); @@ -167,7 +226,8 @@ def main(): while depth: depth+=(hw[end]=='[')-(hw[end]==']'); end+=1 firmware='static BUILDS: &[FirmwareBuild] = &'+hw[start:end]+';\n'+item(hw,'pub(crate) struct FirmwareBuild') constructor=item(boot,' fn new(dev: &device::Device, shared: &Arc)') - source=PREFIX+'\nmod hw {\n'+constants+'\n}\nmod record {\n'+record_constants+'\n}\n'+helpers+'\n'+firmware+'\nimpl Rtkit {\n'+constructor+'\n}\n'+CASES + identity='mod identity {\n'+''.join(f'#[path = "{G15 / "identity" / (name + ".rs")}"] pub(crate) mod {name};\n' for name in ['matching', 'lookup'])+'}\n' + source=PREFIX+'\nmod hw {\n'+constants+'\n}\nmod record {\n'+record_constants+'\n}\n'+helpers+'\n'+firmware+'\nimpl Rtkit {\n'+constructor+'\n}\n'+identity+CASES variants={'actual':source} if a.mutations: variants['skip-owner-check']=source.replace('if !glue::reserved_uat(i as u32, r.start, r.end - r.start) {','if false {') diff --git a/tools/asahi/g15-probe-init-tests.py b/tools/asahi/g15-probe-init-tests.py index a7b9b900b7af5..133df94793352 100644 --- a/tools/asahi/g15-probe-init-tests.py +++ b/tools/asahi/g15-probe-init-tests.py @@ -82,6 +82,7 @@ def item(text, marker): static EXEC_SHARE_READY: Cell = const { Cell::new(false) }; static ATTACH_ERROR: Cell = const { Cell::new(false) }; static PROBE_ERROR: Cell = const { Cell::new(0) }; + static PRE_ACTIVATION: Cell = const { Cell::new(0) }; } struct Core<'a>(PhantomData<&'a ()>); struct Device { data: Cell<*mut core::ffi::c_void> } @@ -184,7 +185,16 @@ def item(text, marker): }; // Model driver_attach and a late driver_add_groups failure. A deferred // worker can also retry G15 while the other registration is in progress. - let _ = callback::>(&PDEV, Some(&&CFG)); + let ready = MODULE_READY.load(Ordering::Acquire); + let started = PROBES.get(); + let result = callback::>(&PDEV, Some(&&CFG)); + // A probe that started firmware here is the readiness-gate failure, checked below. + if !ready && PROBES.get() == started { + // A deferral would keep the GPU in devices_deferred after a later + // terminal failure; before activation the probe must decline outright. + assert_eq!(result, Err(ENODEV), "pre-activation probe deferred instead of ENODEV"); + PRE_ACTIVATION.set(PRE_ACTIVATION.get() + 1); + } if FAIL_REGISTRATION.get() == stage { return Err(-5); } COMPLETED_REGISTRATION.set(stage); stage @@ -252,7 +262,7 @@ def item(text, marker): PDEV.base.data.set(core::ptr::null_mut()); MODULE_READY.store(false, Ordering::Release); COMPLETED_REGISTRATION.set(0); FAIL_REGISTRATION.set(failure); - ACTIVATED.set(false); ATTACH_ERROR.set(failure == 0); + ACTIVATED.set(false); ATTACH_ERROR.set(failure == 0); PRE_ACTIVATION.set(0); let init = ::init(&MODULE); assert_eq!(PROBES.get(), 0, "module initializer eagerly started firmware"); let result = KBox::pin_init(init, GFP_KERNEL); @@ -269,6 +279,12 @@ def item(text, marker): assert!(!PDEV.base.data.get().is_null()); unsafe { drop(Box::from_raw(PDEV.base.data.get().cast::>())); } } + // Every probe before activation declined: one per completed or failing registration. + // The registrations run G15 (1), the generic driver (2), then G16 (3) where it is built. + let expected = if cfg!(CONFIG_DRM_ASAHI_G16) { + match failure { 1 => 1, 2 => 2, _ => 3 } + } else if failure == 1 { 1 } else { 2 }; + assert_eq!(PRE_ACTIVATION.get(), expected, "pre-activation probes"); println!("module-registration-{failure}: PASS"); } } @@ -415,9 +431,10 @@ def main(): needle = " None\n" assert probe.count(needle) == 1 variants["retained-error-unbinds"] = (probe.replace(needle, " return Err(error);\n"), module_code) - gate = " if !MODULE_READY.load(Ordering::Acquire) {\n return Err(EPROBE_DEFER);\n }\n" + gate = " if !MODULE_READY.load(Ordering::Acquire) {\n return Err(ENODEV);\n }\n" assert probe.count(gate) == 1 variants["no-registration-readiness-gate"] = (probe.replace(gate, ""), module_code) + variants["pre-activation-defers"] = (probe.replace(gate, gate.replace("ENODEV", "EPROBE_DEFER")), module_code) late = " #[cfg(CONFIG_DRM_ASAHI_G15)]\n _g15_ready: g15::activate(),\n" assert module_code.count(late) == 1 early = module_code.replace(late, "").replace(" _driver <-", late + " _driver <-", 1) @@ -446,7 +463,7 @@ def main(): else: receipt["mutants"][label] = result if r.returncode == 0: raise SystemExit("SURVIVED: " + name) - intended = {"eager-probe-before-allocation": "allocation-before-start: firmware probe before drvdata allocation", "retained-error-unbinds": "retained-start-failure-binds: callback result", "no-registration-readiness-gate": "late registration failure started firmware", "activation-before-last-registration": "activation before all registrations completed", "activation-before-g16-registration": "activation before all registrations completed"}[name] + intended = {"pre-activation-defers": "pre-activation probe deferred instead of ENODEV", "eager-probe-before-allocation": "allocation-before-start: firmware probe before drvdata allocation", "retained-error-unbinds": "retained-start-failure-binds: callback result", "no-registration-readiness-gate": "late registration failure started firmware", "activation-before-last-registration": "activation before all registrations completed", "activation-before-g16-registration": "activation before all registrations completed"}[name] result["intended_failure"] = intended in r.stderr if not result["intended_failure"]: raise SystemExit("INVALID unrelated failure: " + name + "\n" + r.stderr) (out / "result.json").write_text(json.dumps(receipt, indent=2) + "\n") diff --git a/tools/asahi/g15-zero-words-tests.py b/tools/asahi/g15-zero-words-tests.py index c6e32d76cb415..f4d18bc9685ac 100644 --- a/tools/asahi/g15-zero-words-tests.py +++ b/tools/asahi/g15-zero-words-tests.py @@ -15,12 +15,17 @@ (one #[cfg] line; the tables are unchanged), and when the initial PB posting and the idle reclaim became fixed tuning constants instead of module parameters (four constants; the tables are unchanged), and when the two stamp -renumbering margins became tuning constants (the tables are unchanged). Its T6030 +renumbering margins became tuning constants (the tables are unchanged), and when +the T6030/T6031 tables were gated on CONFIG_DRM_ASAHI_G15 for the T8122 runtime's +UAT adoption (two #[cfg] lines; the tables are unchanged). Its T6030 configuration module is independently pinned to the original bytes, -except for the J514S B0 FirmwareBuild row added to its firmware table: the -module is the original pinned bytes (0079ae08) plus that one 15-line row, and -the whole hw.rs pin was refreshed with it. prepare_board() is pinned too; +except for the J514S B0 FirmwareBuild row, the J514S cd05 and J516S cd05 +FirmwareBuild rows and the J514S 4118 and J516S 4118 FirmwareBuild rows added +to its firmware table: the module is the original pinned bytes (0079ae08) plus +those five rows, and the whole hw.rs pin was refreshed with each (with the 4118 +rows removed, both pins are their earlier values fb2118b6 and 2667963a; with +the cd05 rows also removed, e9b80be9 and cab592b5). prepare_board() is pinned too; it was refreshed for the same Board change (its T6030 arm is unchanged). """ @@ -37,11 +42,11 @@ GOLDEN_FILES = { "board/data.rs": "7c5ff1077ca3296cbfc9e7144294162b6dea78a3774c38eeed4b0f59a14d7a5c", "board/template.rs": "286872f5474a67534f214e83e659253299f85143f3dc59035fa80894022e2549", - "hw.rs": "c54a4f8342fb466f82f4b9cfed6a137ee1ca1f4d80ede4e1b7f397d39223558e", + "hw.rs": "9fa2d6e5f5d14127574448cafd6523d2151dab77c66d16e74500fafde21e8d92", "power/idle.rs": "4928ecf7699b3ca85ff5143eb6e996efd40d47321e29a238a02c8873bc9f102c", } GOLDEN_FUNCTIONS = { - ("hw.rs", "pub(crate) mod t6030 {"): "cab592b5d776be9ede93b96b1b0526c1d838ef2948a5e4a86caa4ec71f71c2ca", + ("hw.rs", "pub(crate) mod t6030 {"): "4016ec0d46831482382a92791b625e10fb2f6e4e28115a9498fdde0776f82e52", ("initdata.rs", "fn templates("): "c3115c9926d3d7d93317cb8510e93b04f30b0c256811d7137568e822dd93b8ee", ("initdata.rs", "pub(crate) fn prepare_board("): "ebef64ab87ef7b1f994eb7fa2a2424d05bc4fc9e88cbfbf18e51570bdfc4222d", ("board.rs", "pub(crate) fn from_adt("): "cecd0b28db5bd94d3b75c1bbb5f35204e8bf0699f74e5b3d08228c59025f6ac2", diff --git a/tools/asahi/m3-runtime-tests.rs b/tools/asahi/m3-runtime-tests.rs index 0d6d4e5841194..cf74936f343a1 100644 --- a/tools/asahi/m3-runtime-tests.rs +++ b/tools/asahi/m3-runtime-tests.rs @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-only OR MIT -//! Host tests for the production M3 admission, IO ownership, thermal policy and the T8122 -//! HwDataA +0x418c block. +//! Host tests for the production M3 admission, IO ownership, thermal policy, the T8122 start +//! parameters and the T8122 HwDataA +0x418c block. //! Run: rustc --edition=2021 --test tools/asahi/m3-runtime-tests.rs -o /tmp/m3-runtime-tests #![allow(dead_code)] @@ -109,3 +109,173 @@ fn extended_parameter_aliases_are_gpu_only_and_legacy_is_unchanged() { #[path = "../../drivers/gpu/drm/asahi/m3/m3_completion_policy.rs"] mod m3_completion_policy; + +// `t8122_knobs` takes its default InitData version from `m3_firmware`, which needs the kernel. +// Only that constant is stood in for (`defaults_are_the_plan_baseline` checks its value); the +// parameter parsing and validation are the production module. +mod m3 { + pub mod m3_firmware { + pub const G15_V14_8_3_INITDATA: u64 = 0x0c08_e21e_8380_0490; + } +} +#[path = "../../drivers/gpu/drm/asahi/m3/t8122_knobs.rs"] +mod t8122_knobs; + +/// `asahi.t8122_pstate_cap` through the thermal limit: what the parameter resolves to, what +/// `pstate_cap_use` makes of it with each `asahi.m3_thermal` mode, and what the governor's +/// decisions then let the firmware use. +#[cfg(test)] +mod t8122_pstate_cap { + use super::m3_thermal_policy::{Policy, Reading, ThermalMode, S}; + use super::t8122_knobs::{self as knobs, CapUse, Feedback, Raw, Refusal}; + + const MODES: [Feedback; 3] = [Feedback::Off, Feedback::Hold, Feedback::On]; + + fn cap_use(cap: u64, feedback: Feedback) -> Result { + let raw = Raw { start: 1, pstate_cap: cap, ..Raw::NONE }; + Ok(knobs::pstate_cap_use(knobs::resolve(&raw)?.pstate_cap, feedback)) + } + + /// The governor of `m3_thermal` for `u` (`Governor::new`): it follows the temperature + /// only with `On`. + fn policy(u: CapUse, feedback: Feedback) -> Policy { + Policy::new(feedback == Feedback::On, u.fallback, u.ceiling, 80_000) + } + + /// The highest runtime cap in 30 s of cool readings from `from` on, one every 500 ms. + fn highest_when_cool(p: &mut Policy, from: i64) -> u32 { + let mut highest = p.cap(); + for i in 0..60 { + let now = from + i * S / 2; + let _ = p.update(now, Reading::Temp { mc: 45_000 + i as i32, at: now, zones: 1 }); + highest = highest.max(p.cap()); + assert!(p.allowed(now) <= p.ceiling()); + } + highest + } + + #[test] + fn states_above_five_are_reached_only_with_the_thermal_limit_on_and_a_usable_reading() { + assert_eq!(knobs::PSTATE_CAP_MAX, 8); + for cap in 1..=8u64 { + for feedback in MODES { + let u = cap_use(cap, feedback).unwrap(); + // Without the thermal limit on the firmware never gets a state above 5. + if feedback != Feedback::On { + assert!(u.ceiling <= knobs::PSTATE_CAP_NO_FEEDBACK_MAX, "cap {cap}"); + } + let mut p = policy(u, feedback); + // Before any reading the runtime cap is the fallback, at most 2. + assert_eq!(p.cap(), u.fallback); + assert!(u.fallback <= knobs::PSTATE_FALLBACK); + assert_eq!(p.update(0, Reading::None).1, None); + assert_eq!(p.cap(), u.fallback); + // Without a usable reading (here: no zone) the cap never rises. + for i in 1..20 { + let now = i * S / 2; + let _ = p.update(now, Reading::Failed { errno: -19, at: now }); + assert_eq!(p.cap(), u.fallback); + } + let highest = highest_when_cool(&mut p, 10 * S); + let expected = if feedback == Feedback::On { cap as u32 } else { (cap as u32).min(2) }; + assert_eq!(highest, expected, "cap {cap} {feedback:?}"); + assert_eq!(u.ranges, highest > u.fallback, "cap {cap} {feedback:?}"); + } + } + } + + #[test] + fn caps_six_to_eight_without_the_thermal_limit_on_still_start_the_gpu() { + for cap in 6..=8u64 { + // A cap left on the command line from a ranging boot must not cost the GPU when the + // thermal limit is switched off or held: it is lowered to 5, and the boot starts at 2. + let u = cap_use(cap, Feedback::Off).unwrap(); + assert_eq!(u, CapUse { ceiling: 2, fallback: 2, ranges: false }); + let mut p = policy(u, Feedback::Off); + assert!(!p.above_safe()); + assert_eq!(highest_when_cool(&mut p, 0), 2); + let u = cap_use(cap, Feedback::Hold).unwrap(); + assert_eq!(u, CapUse { ceiling: 5, fallback: 2, ranges: false }); + let mut p = policy(u, Feedback::Hold); + assert!(p.above_safe()); + assert_eq!(highest_when_cool(&mut p, 0), 2); + // With the limit on, the firmware is told about the ceiling and starts at 2. + let u = cap_use(cap, Feedback::On).unwrap(); + assert_eq!(u, CapUse { ceiling: cap as u32, fallback: 2, ranges: true }); + } + for cap in [0u64, 9, 10, 16, 255] { + for feedback in MODES { + assert_eq!(cap_use(cap, feedback).unwrap_err().name, "t8122_pstate_cap"); + } + } + } + + #[test] + fn every_loss_of_feedback_returns_a_raised_cap_to_two() { + for cap in 3..=8u64 { + let u = cap_use(cap, Feedback::On).unwrap(); + let losses = [ + Reading::None, + Reading::Failed { errno: -19, at: 30 * S }, + Reading::Failed { errno: -61, at: 30 * S }, + Reading::Temp { mc: 46_000, at: 26 * S, zones: 1 }, + Reading::Temp { mc: -2_849, at: 30 * S, zones: 1 }, + Reading::Temp { mc: 88_000, at: 30 * S, zones: 1 }, + ]; + for reading in losses { + let mut p = policy(u, Feedback::On); + assert_eq!(highest_when_cool(&mut p, 0), cap as u32); + let _ = p.update(30 * S, reading); + assert_eq!(p.cap(), 2, "cap {cap} {reading:?}"); + } + } + } + + #[test] + fn caps_one_and_two_never_move_whatever_the_thermal_limit() { + for cap in 1..=2u64 { + for feedback in MODES { + let u = cap_use(cap, feedback).unwrap(); + assert_eq!(u, CapUse { ceiling: cap as u32, fallback: cap as u32, ranges: false }); + let mut p = policy(u, feedback); + assert!(!p.above_safe()); + assert_eq!(highest_when_cool(&mut p, 0), cap as u32); + let _ = p.update(30 * S, Reading::Temp { mc: 95_000, at: 30 * S, zones: 1 }); + assert_eq!(p.cap(), cap as u32); + assert_eq!(p.counts(), (0, 0)); + } + } + } + + /// `ThermalMode` as `t8122_start::feedback` maps it. + fn mode(feedback: Feedback) -> ThermalMode { + match feedback { + Feedback::Off => ThermalMode::Off, + Feedback::Hold => ThermalMode::Hold, + Feedback::On => ThermalMode::On, + } + } + + #[test] + fn pstate_cap_use_is_what_pstate_policy_computes() { + // `PstatePolicy::new` with `asahi.m3_max_pstate` unset: the whole table (8) with the + // limit on or holding, the default of 5 with it off, then `pstate_caps` on the admitted + // cap. `Contents::select` refuses the start unless the result is within `pstate_cap_use`; + // without `asahi.m3_max_pstate` the two must be equal. + for cap in 1..=8u32 { + for feedback in MODES { + let requested = if feedback == Feedback::Off { 5 } else { 8 }; + let admitted = knobs::admitted_cap(cap, feedback); + let (max, safe) = mode(feedback).pstate_caps(requested, Some(admitted)); + let u = knobs::pstate_cap_use(cap, feedback); + assert_eq!((max, safe), (u.ceiling, u.fallback), "cap {cap} {feedback:?}"); + assert_eq!(u.ranges, feedback == Feedback::On && max > safe); + // A lower asahi.m3_max_pstate stays within it as well. + for lower in 1..requested { + let (max, safe) = mode(feedback).pstate_caps(lower, Some(admitted)); + assert!(max <= u.ceiling && safe <= u.fallback, "cap {cap} {feedback:?} {lower}"); + } + } + } + } +} diff --git a/tools/asahi/t6030-pmp-graph-tests.c b/tools/asahi/t6030-pmp-graph-tests.c index e89d982d2d0a6..89a6ac0efc8e7 100644 --- a/tools/asahi/t6030-pmp-graph-tests.c +++ b/tools/asahi/t6030-pmp-graph-tests.c @@ -20,6 +20,7 @@ typedef uint8_t u8; #define BIT(n) (1U << (n)) #define IS_ENABLED(x) (x) #define CONFIG_APPLE_T6030_GPU_PMP_LINK 1 +#define CONFIG_APPLE_T8122_GPU_PMP_LINK 0 #define PMP_REPORT_READY 1 #define GFP_KERNEL 0 #define GENPD_FLAG_ACTIVE_WAKEUP 1 diff --git a/tools/asahi/t6030-pmp-graph-tests.py b/tools/asahi/t6030-pmp-graph-tests.py index 4c85001acf1a8..c8a58b302e727 100644 --- a/tools/asahi/t6030-pmp-graph-tests.py +++ b/tools/asahi/t6030-pmp-graph-tests.py @@ -60,6 +60,12 @@ def main(): 'static int apple_pmp_report_entry_resume_noirq('] offsets = re.search(r'static const struct apple_pmp_report_offsets apple_pmp_offsets_t6030 = \{.*?\n\};', source, re.S)[0] functions = '\n'.join(extract.function(source, n) for n in names) + # The T8122 14.x wait (CONFIG_APPLE_T8122_GPU_PMP_LINK=0 here) precedes the dispatch; it is + # added after the mutations are placed, so they target the T6030 path only. + t8122 = extract.function(source, 'static int apple_pmp_report_wait_t8122_14(') + '\n' + dispatch = 'int apple_pmp_report_wait_supplier_ready(' + def with_t8122(text): + return text.replace(dispatch, t8122 + dispatch, 1) validation = re.sub(r'^#include .*\n', '', (ROOT / 'drivers/pmdomain/apple/pmp-report-validation.h').read_text(), flags=re.M) dram_type = re.search(r'struct apple_pmp_dram \{.*?\n\};', (ROOT / 'drivers/pmdomain/apple/pmp-dram.h').read_text(), re.S)[0] @@ -78,7 +84,7 @@ def main(): '\n' + dram_attrs + '\n' + functions) # The actual overlay must still describe a halted supplier and disabled DMA/mailbox. assert overlay.count('status = \"disabled\";') >= 3 - (args.output / 'production.h').write_text(code) + (args.output / 'production.h').write_text(with_t8122(code)) (args.output / 'overlay.h').write_text('static const struct overlay_entry overlay_entries[] = {\n' + entry_data + '\n};\n') compiler = shlex.split(os.environ.get('CC', 'clang')) command = compiler + ['-std=gnu11', '-g', '-O1', '-Wall', '-Wextra', '-Werror', @@ -133,13 +139,13 @@ def main(): ] for name, old, new in controls: assert code.count(old) == 1, (name, code.count(old)) - (args.output / 'production.h').write_text(code.replace(old, new)) + (args.output / 'production.h').write_text(with_t8122(code.replace(old, new))) built = subprocess.run(command, capture_output=True, text=True) # A mutant must compile; compilation failure is not regression evidence. ran = subprocess.run([str(args.output / 'graph')], capture_output=True, text=True) if not built.returncode else None (args.output / (name + '.log')).write_text(built.stdout + built.stderr + (ran.stdout + ran.stderr if ran else '')) mutations.append({'name': name, 'build_rc': built.returncode, 'run_rc': ran.returncode if ran else None}) - (args.output / 'production.h').write_text(code) + (args.output / 'production.h').write_text(with_t8122(code)) assert all(m['build_rc'] == 0 and m['run_rc'] != 0 for m in mutations), mutations (args.output / 'receipt.json').write_text(json.dumps({'source_ref': args.ref, 'source_sha256': hashlib.sha256(code.encode()).hexdigest(), 'overlay_sha256': hashlib.sha256(overlay.encode()).hexdigest(), 'overlay_entries': entries, diff --git a/tools/asahi/t6030-pmp-supplier-tests.c b/tools/asahi/t6030-pmp-supplier-tests.c index 59c2c0d3cd430..f9ad353872de1 100644 --- a/tools/asahi/t6030-pmp-supplier-tests.c +++ b/tools/asahi/t6030-pmp-supplier-tests.c @@ -489,6 +489,17 @@ static void reset(void) lockable = present_device = link_ok = 1; status_word = PMP_REPORT_READY; } +/* The T8122 (M3 Air) 14.x graph: the same report shape, with T8122 compatibles. */ +static void t8122(void) +{ + machine = "apple,t8122"; + gate_soc = &gate_t8122; + supplier_node.compatible = "apple,t8122-pmp-v2"; + other_node.compatible = "apple,t8122-pmp-v2"; + gpu.compatible = "apple,agx-t8122"; + report14.compatible = "apple,t8122-pmp-v2-report"; + apple_pmp_starts.first = &managed_start; +} static int power(void) { return apple_pmp_set_device_power(0x0f, 5, 1); @@ -518,6 +529,7 @@ int main(void) (void)gate_find_one; (void)apple_pmp_report_t6030_entry; (void)apple_pmp_report_wait_t6030_14; + (void)apple_pmp_report_wait_t8122_14; reset(); assert(apple_pmp_offsets_t6030.starts_pmp == !!CONFIG_APPLE_T6030_GPU_PMP_LINK); @@ -574,9 +586,50 @@ int main(void) RUN((void)0, -ENODEV, 0, 0); assert(!find_calls); #endif - /* T8122 current14 remains unsupported by this supplier API. */ + /* A T8122 supplier never takes the T6030 report. */ RUN(machine = "apple,t8122"; supplier_node.compatible = "apple,t8122-pmp-v2", -ENODEV, 0, 0); +#if CONFIG_APPLE_T8122_GPU_PMP_LINK + /* The T8122 14.x wait: one available report linked to this PMP, DISP (reg 7) acked. */ + RUN(t8122(), 0, 0, 1); + RUN(t8122(); report14.child = &entry7; entry7.sibling = &entry17; + entry17.sibling = NULL, 0, 0, 1); + RUN(t8122(); apple_pmp_starts.first = NULL, -ENODEV, 0, 0); + RUN(t8122(); apple_pmp_starts.first = NULL; + apple_pmp_reports.first = NULL, -EPROBE_DEFER, 0, 0); + RUN(t8122(); managed_start.stopping = true, -ENODEV, 0, 0); + RUN(t8122(); ready_result = -ETIMEDOUT, -ETIMEDOUT, 0, 0); + RUN(t8122(); passive_words[apple_pmp_offsets_t6030.status / 8] = 0, -EIO, + 0, 0); + RUN(t8122(); passive_words[apple_pmp_offsets_t6030.actual / 8] = 0, -EIO, + 0, 0); + RUN(t8122(); power_result = -EIO, -EIO, 0, 1); + RUN(t8122(); machine = "apple,t6030", -ENODEV, 0, 0); + RUN(t8122(); machine = "apple,t8112", -ENODEV, 0, 0); + RUN(t8122(); supplier_node.compatible = "apple,t6030-pmp-v2", -ENODEV, 0, + 0); + RUN(t8122(); report14.compatible = NULL, -ENODEV, 0, 0); + RUN(t8122(); report14.compatible = "apple,t6030-pmp-v2-report", -ENODEV, + 0, 0); + RUN(t8122(); extra_report = report14, -ENODEV, 0, 0); + RUN(t8122(); extra_report = report14; extra_report.available = false, + -ENODEV, 0, 0); + RUN(t8122(); report14.available = false, -ENODEV, 0, 0); + RUN(t8122(); report14.has_pmp = false, -ENODEV, 0, 0); + RUN(t8122(); report14.pmp_cells = 0, -ENODEV, 0, 0); + RUN(t8122(); report14.pmp_cells = 2, -ENODEV, 0, 0); + RUN(t8122(); report14.pmp = NULL, -ENODEV, 0, 0); + RUN(t8122(); report14.pmp = &other_node, -ENODEV, 0, 0); + RUN(t8122(); entry17.sibling = NULL, -ENODEV, 0, 0); + RUN(t8122(); entry7.reg = 8, -ENODEV, 0, 0); + RUN(t8122(); entry7.available = false, -ENODEV, 0, 0); + RUN(t8122(); entry7.compatible = "apple,other", -ENODEV, 0, 0); + RUN(t8122(); entry7.no_ack = true, -ENODEV, 0, 0); + RUN(t8122(); entry17.reg = 7, -ENODEV, 0, 0); +#else + /* Without APPLE_T8122_GPU_PMP_LINK, even a well-formed T8122 graph is refused. */ + RUN(t8122(), -ENODEV, 0, 0); +#endif /* A declared report, including malformed/unresolved data, never falls back. */ RUN(supplier_node.has_report = true; supplier_node.report_cells = 0, -ENODEV, 0, 0); @@ -716,7 +769,8 @@ int main(void) assert(apple_pmp_link_device(&consumer) == -EINVAL); balanced(); case_count++; - printf("PASS: %d T6030 PMP cases, config=%d; reference, lock and command boundaries checked\n", - case_count, CONFIG_APPLE_T6030_GPU_PMP_LINK); + printf("PASS: %d T6030/T8122 PMP cases, config=%d/%d; reference, lock and command boundaries checked\n", + case_count, CONFIG_APPLE_T6030_GPU_PMP_LINK, + CONFIG_APPLE_T8122_GPU_PMP_LINK); return 0; } diff --git a/tools/asahi/t6030-pmp-supplier-tests.py b/tools/asahi/t6030-pmp-supplier-tests.py index 269006b36aa4f..aee98f9c59953 100755 --- a/tools/asahi/t6030-pmp-supplier-tests.py +++ b/tools/asahi/t6030-pmp-supplier-tests.py @@ -46,16 +46,23 @@ def no_core(): resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) -def run(out, name, code, compiler, configs=(0, 1)): +# (CONFIG_APPLE_T6030_GPU_PMP_LINK, CONFIG_APPLE_T8122_GPU_PMP_LINK): off, G15 only, and G15 +# with the M3 runtime (DRM_ASAHI_M3_T8122 depends on DRM_ASAHI_G15). +CONFIGS = ((0, 0), (1, 0), (1, 1)) + + +def run(out, name, code, compiler, configs=CONFIGS): dest = out / name dest.mkdir() (dest / "t6030-pmp-production.h").write_text(code) results = [] - for config in configs: + for t6030, t8122 in configs: + config = f"{t6030}{t8122}" binary = dest / f"config-{config}" command = compiler + ["-std=gnu11", "-g", "-O1", "-Wall", "-Wextra", "-Werror", "-fsanitize=address,undefined", "-fno-omit-frame-pointer", - f"-DCONFIG_APPLE_T6030_GPU_PMP_LINK={config}", + f"-DCONFIG_APPLE_T6030_GPU_PMP_LINK={t6030}", + f"-DCONFIG_APPLE_T8122_GPU_PMP_LINK={t8122}", "-I", str(dest), str(ROOT / "tools/asahi/t6030-pmp-supplier-tests.c"), "-o", str(binary)] @@ -148,9 +155,43 @@ def main(): assert code.count(old) == 1, (name, code.count(old)) variants.append((name, code.replace(old, new))) variants.append(("control-after", code)) + # The T8122 14.x wait mirrors the T6030 one, so it is added to every variant after the + # T6030 mutations are placed; its own mutants change only its text. + dispatch = "int apple_pmp_report_wait_supplier_ready(" + t8122 = function(report, "static int apple_pmp_report_wait_t8122_14(") + "\n" + def with_t8122(source, text=t8122): + if dispatch not in source: + return source + return source.replace(dispatch, text + dispatch, 1) + variants = [(name, with_t8122(source)) for name, source in variants] + if args.mutations: + t8122_mutations = [ + ("t8122-config-scope", "if (!IS_ENABLED(CONFIG_APPLE_T8122_GPU_PMP_LINK) ||", "if (false ||"), + ("t8122-supplier-compat", '!of_device_is_compatible(supplier->of_node, "apple,t8122-pmp-v2"))', "false)"), + ("t8122-report-compat", 'for_each_compatible_node(np, NULL, "apple,t8122-pmp-v2-report")', + 'for_each_compatible_node(np, NULL, "apple,t6030-pmp-v2-report")'), + ("t8122-duplicate-report", "\t\tif (report) {", "\t\tif (false) {"), + ("t8122-disabled-report", "!of_device_is_available(report) ||", "false ||"), + ("t8122-report-phandle-shape", 'of_property_count_u32_elems(report, "apple,pmp") != 1', "false"), + ("t8122-supplier-identity", "pmp == supplier->of_node && entry ?", "entry ?"), + ("t8122-readiness-error", "apple_pmp_report_wait_ready(entry, msecs_to_jiffies(timeout_ms)) : -ENODEV;", + "(apple_pmp_report_wait_ready(entry, msecs_to_jiffies(timeout_ms)), 0) : -ENODEV;"), + ("t8122-entry-reference", "\tof_node_put(entry);\n\tof_node_put(pmp);", "\tof_node_put(pmp);"), + ] + for name, old, new in t8122_mutations: + assert t8122.count(old) == 1, (name, t8122.count(old)) + variants.insert(-1, (name, with_t8122(code, t8122.replace(old, new)))) + # The dispatch owns the machine scope: T8122 takes the T8122 wait, every other SoC the + # T6030 one. (The wait's own machine check repeats it and cannot be reached otherwise.) + machine = 'if (of_machine_is_compatible("apple,t8122"))' + assert code.count(machine) == 1 + for name, new in (("t8122-dispatch-never", "if (false)"), ("t8122-dispatch-always", "if (true)")): + variants.insert(-1, (name, with_t8122(code.replace(machine, new)))) + if baseline_code and "apple_pmp_report_wait_t8122_14(" in baseline_code: + baseline_code = with_t8122(baseline_code) results = [run(args.output, name, source, compiler) for name, source in variants] if baseline_code: - results.append(run(args.output, "control-prior-config-off", baseline_code, compiler, (0,))) + results.append(run(args.output, "control-prior-config-off", baseline_code, compiler, ((0, 0),))) valid = True for item in results: control = item["name"].startswith("control-") diff --git a/tools/aurora-sep/M3-GPU.md b/tools/aurora-sep/M3-GPU.md index 821d37b66f984..aaa4c9e59318b 100644 --- a/tools/aurora-sep/M3-GPU.md +++ b/tools/aurora-sep/M3-GPU.md @@ -109,3 +109,39 @@ to this Mac; neither the installer nor this option migrates firmware or stage1. J615 hardware qualification is pending. The retained **Aurora previous (GPU off)** entry remains available. After reboot and desktop login, run `aurora-m3-gpu-check --details` and collect the installer's `--m3-report`. + +## J613/J615 temperature-guarded performance ceiling + +The T8122 default ceiling remains state 2. With the state-2 fallback repair in +this driver, an explicit `asahi.t8122_pstate_cap=5 asahi.m3_thermal=on` permits +states up to 5 only while the governor has usable die-temperature readings. +Do not apply this higher ceiling to an older kernel: its implementation can +also raise the boot and sensor-failure cap. + +The repaired driver starts at state 2 and immediately returns there for missing, +failed, stale, stuck or implausible temperature readings, or at the critical +temperature. A lower requested ceiling still wins. `m3_thermal=off` and `hold` +do not permit states above 2. Non-T8122 cap selection is unchanged. + +J615 now supplies `macsmc_soc_die` from the six Tg GPU-family keys and the six +CPU die keys also used on J613. All twelve must be discovered before the J615 +zone is registered; a later read failure in any sensor fails the whole reading. +Do not shorten the `apple,soc-die-keys` list in `t8122.dtsi`. These are raw sensor +readings, not a calibration or a hardware safety certification. + +Caps 6 to 8 (up to 1338 MHz, the top of the T8122 table) are an experiment on top +of this. They are admitted only with `asahi.m3_thermal=on`, which is also the +default. With `off` or `hold` the driver lowers them to 5 with one boot line and +the GPU still starts. Boot, missing readings and the critical temperature stay at +state 2. The runtime range is 2 to the cap, and the firmware's highest state is +the cap. Values of 9 or more are refused. While the firmware's highest state is +above 2 the driver checks the reported state at every retirement with no later +batch in flight, whatever `asahi.m3_retire_mmio` says, and lets the pipeline +drain once the last check is 500 ms old. `air-gpu-oneshot.sh` still offers 1 to 5. + +First boot with `asahi.t8122_pstate_cap=5 asahi.m3_thermal=hold` and confirm that +the firmware respects the runtime cap of 2 under GPU load. Before relying on +`m3_thermal=on`, check the boot log for all twelve keys, a runtime range of 2..5 +and usable temperatures during GPU work. Keep a bootable previous kernel. +Verify rendering and temperature response on the running kernel; passing the +host policy tests alone does not qualify hardware behavior. diff --git a/tools/aurora-sep/air-gpu/air-gpu-oneshot.sh b/tools/aurora-sep/air-gpu/air-gpu-oneshot.sh index 2231a78b2a346..b460c151ea1fb 100755 --- a/tools/aurora-sep/air-gpu/air-gpu-oneshot.sh +++ b/tools/aurora-sep/air-gpu/air-gpu-oneshot.sh @@ -40,7 +40,7 @@ t8122_clkgen e1c|e5c|none clock-generator slot: t8122_sgx_setup none|t6030 SGX write before the firmware starts: none (default) or T6030 0x70001 t8122_unit_mask_a 0x[0-9a-fA-F]{1,9}|[1-9][0-9]{0,10} HwDataB unit mask A: nonzero, within 0x700000001 (default) t8122_unit_mask_b 0x[1-7]|[1-7] HwDataB unit mask B: nonzero, within 0x7 (default 0x3) -t8122_pstate_cap [1-5] highest GPU performance state, 1 to 5 (default 2; above 2 has no temperature feedback yet) +t8122_pstate_cap [1-5] highest GPU performance state, 1 to 5 (default 2; higher states require thermal feedback and the state-2 fallback repair) m3_expose -1|0|1 render node: -1 auto (on for J613/J615, the default), 0 off, 1 on m3_timeout_nohang 0|1 on a job timeout, fail the job without the hang path ' diff --git a/tools/aurora-sep/install-aurora-sep.sh b/tools/aurora-sep/install-aurora-sep.sh index 778bf309e518e..fde119d0ec9d7 100755 --- a/tools/aurora-sep/install-aurora-sep.sh +++ b/tools/aurora-sep/install-aurora-sep.sh @@ -678,7 +678,7 @@ M3_PERSISTENT_TRANSACTION_ACTIVE=0 M3_PERSISTENT_MAIN_ARMED=0 M3_GPU_BIN=/usr/local/bin M3_GPU_SCRIPTS=( - "air-gpu-oneshot.sh d0259869b8519439a4dbcdae08ef1b0e17fdcaf84ac746fca61febf6a325f8aa" + "air-gpu-oneshot.sh 3a7a4c4e4dff8a9b5a281f56c2b2ad264d465e16e4f1e01741bc5a60e8f593c6" "air-gpu-collect.sh face8fac811b59c31a8e98e6ca704a2cb33eceada8bc37986397cb393b050b30" "air-gpu-job.sh 9a0eb9aab52fd099488054e4ed4846c579b19fc9f78144245242b6975a40da1d" ) diff --git a/tools/testing/selftests/drivers/gpu/asahi-g15/run.py b/tools/testing/selftests/drivers/gpu/asahi-g15/run.py index b0c3105235f7b..80b7174635b5d 100644 --- a/tools/testing/selftests/drivers/gpu/asahi-g15/run.py +++ b/tools/testing/selftests/drivers/gpu/asahi-g15/run.py @@ -260,13 +260,15 @@ def main(): with tempfile.TemporaryDirectory(prefix="asahi-g15-host-") as temp: temp = Path(temp) glue = (G15 / "glue.c").read_text() + # The UAT adoption's helpers, shared with the T8122 runtime. + uat_glue = (G15 / "uat_glue.c").read_text() functions = [] for signature in ["bool asahi_g15_ram_disjoint", "bool asahi_g15_ram_within", "unsigned int asahi_g15_memory_mode"]: marker = signature + "(u64 start, u64 size)\n{" - start = glue.index(marker) - end = glue.index("\n}", start) + 2 - functions.append(glue[start:end]) + start = uat_glue.index(marker) + end = uat_glue.index("\n}", start) + 2 + functions.append(uat_glue[start:end]) c_source = temp / "memory.c" c_source.write_text(MEMORY_PREFIX + "\n".join(functions) + MEMORY_CASES) subprocess.run([os.environ.get("HOSTCC", "cc"), "-std=gnu11", "-Wall", "-Wextra",