diff --git a/Documentation/devicetree/bindings/cpufreq/apple,cluster-cpufreq.yaml b/Documentation/devicetree/bindings/cpufreq/apple,cluster-cpufreq.yaml index b51913a817917..8e87c0f6d3d45 100644 --- a/Documentation/devicetree/bindings/cpufreq/apple,cluster-cpufreq.yaml +++ b/Documentation/devicetree/bindings/cpufreq/apple,cluster-cpufreq.yaml @@ -35,6 +35,7 @@ properties: - const: apple,t7000-cluster-cpufreq - const: apple,s5l8960x-cluster-cpufreq - const: apple,s5l8960x-cluster-cpufreq + - const: apple,t8140-cluster-cpufreq - items: - const: apple,t6020-cluster-cpufreq - const: apple,t8112-cluster-cpufreq diff --git a/Documentation/devicetree/bindings/hwmon/apple,smc-hwmon.yaml b/Documentation/devicetree/bindings/hwmon/apple,smc-hwmon.yaml index 2eec317bc4b3e..f3751b5af1720 100644 --- a/Documentation/devicetree/bindings/hwmon/apple,smc-hwmon.yaml +++ b/Documentation/devicetree/bindings/hwmon/apple,smc-hwmon.yaml @@ -40,6 +40,14 @@ properties: compatible: const: apple,smc-hwmon + apple,cpu-thermal-policy: + type: boolean + description: + Enable the J700 CPU thermal provider over the first six temperature + channels, which must contain Te05, Te0S, Tp01, Tp05, Tp09 and Tp0D. + Further temperature channels are reporting-only. The provider holds + CPU frequency constraints while these readings are unavailable. + patternProperties: "^current-[A-Za-z0-9]{4}$": $ref: "#/$defs/sensor" diff --git a/Documentation/devicetree/bindings/mfd/apple,smc.yaml b/Documentation/devicetree/bindings/mfd/apple,smc.yaml index d5d30c0538458..ed6b02086c33c 100644 --- a/Documentation/devicetree/bindings/mfd/apple,smc.yaml +++ b/Documentation/devicetree/bindings/mfd/apple,smc.yaml @@ -20,6 +20,7 @@ properties: - enum: - apple,t6020-smc - apple,t8122-smc + - apple,t8140-smc - const: apple,t8103-smc - items: - enum: diff --git a/Documentation/devicetree/bindings/nvmem/apple,spmi-nvmem.yaml b/Documentation/devicetree/bindings/nvmem/apple,spmi-nvmem.yaml index 4ca75ed07a54f..7a4114c7fbd19 100644 --- a/Documentation/devicetree/bindings/nvmem/apple,spmi-nvmem.yaml +++ b/Documentation/devicetree/bindings/nvmem/apple,spmi-nvmem.yaml @@ -18,6 +18,7 @@ properties: compatible: items: - enum: + - apple,baku-pmic - apple,maverick-pmic - apple,sera-pmic - apple,stowe-pmic diff --git a/Documentation/devicetree/bindings/spmi/apple,spmi.yaml b/Documentation/devicetree/bindings/spmi/apple,spmi.yaml index fceed47b2cd17..f0f7d21d031ca 100644 --- a/Documentation/devicetree/bindings/spmi/apple,spmi.yaml +++ b/Documentation/devicetree/bindings/spmi/apple,spmi.yaml @@ -25,6 +25,7 @@ properties: - apple,t8012-spmi - apple,t8015-spmi - apple,t8122-spmi + - apple,t8140-spmi - const: apple,t8103-spmi - items: - enum: diff --git a/MAINTAINERS b/MAINTAINERS index 4fd1875a9b93d..e78ba9b5a4b05 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1976,7 +1976,11 @@ F: Documentation/devicetree/bindings/serial/apple,dockchannel-uart.yaml F: Documentation/devicetree/bindings/soc/apple/apple,t8140-aop.yaml F: Documentation/devicetree/bindings/sound/apple,t8140-aop-audio.yaml F: arch/arm64/boot/dts/apple/t8140* +F: arch/arm64/include/asm/apple-idle.h +F: arch/arm64/kernel/idle-apple.S +F: drivers/thermal/apple-smc-cpu-thermal.c F: drivers/tty/serial/apple_dockchannel* +F: include/linux/soc/apple/smc-thermal.h APPLETALK NETWORK LAYER L: netdev@vger.kernel.org diff --git a/arch/arm64/include/asm/apple-idle.h b/arch/arm64/include/asm/apple-idle.h new file mode 100644 index 0000000000000..3a0a2660877f5 --- /dev/null +++ b/arch/arm64/include/asm/apple-idle.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __ASM_APPLE_IDLE_H +#define __ASM_APPLE_IDLE_H + +/* WFI that preserves x18-x30 on cores that lose them (Apple T8140). */ +void apple_cpu_wfi(void); + +#endif diff --git a/arch/arm64/include/asm/cputype.h b/arch/arm64/include/asm/cputype.h index 1b9f0cda1336d..d44795ef73e4c 100644 --- a/arch/arm64/include/asm/cputype.h +++ b/arch/arm64/include/asm/cputype.h @@ -153,6 +153,8 @@ #define APPLE_CPU_PART_M2_AVALANCHE_PRO 0x035 #define APPLE_CPU_PART_M2_BLIZZARD_MAX 0x038 #define APPLE_CPU_PART_M2_AVALANCHE_MAX 0x039 +#define APPLE_CPU_PART_TAHITI_E 0x060 +#define APPLE_CPU_PART_TAHITI_P 0x061 #define AMPERE_CPU_PART_AMPERE1 0xAC3 #define AMPERE_CPU_PART_AMPERE1A 0xAC4 @@ -246,6 +248,8 @@ #define MIDR_APPLE_M2_AVALANCHE_PRO MIDR_CPU_MODEL(ARM_CPU_IMP_APPLE, APPLE_CPU_PART_M2_AVALANCHE_PRO) #define MIDR_APPLE_M2_BLIZZARD_MAX MIDR_CPU_MODEL(ARM_CPU_IMP_APPLE, APPLE_CPU_PART_M2_BLIZZARD_MAX) #define MIDR_APPLE_M2_AVALANCHE_MAX MIDR_CPU_MODEL(ARM_CPU_IMP_APPLE, APPLE_CPU_PART_M2_AVALANCHE_MAX) +#define MIDR_APPLE_TAHITI_E MIDR_CPU_MODEL(ARM_CPU_IMP_APPLE, APPLE_CPU_PART_TAHITI_E) +#define MIDR_APPLE_TAHITI_P MIDR_CPU_MODEL(ARM_CPU_IMP_APPLE, APPLE_CPU_PART_TAHITI_P) #define MIDR_AMPERE1 MIDR_CPU_MODEL(ARM_CPU_IMP_AMPERE, AMPERE_CPU_PART_AMPERE1) #define MIDR_AMPERE1A MIDR_CPU_MODEL(ARM_CPU_IMP_AMPERE, AMPERE_CPU_PART_AMPERE1A) #define MIDR_MICROSOFT_AZURE_COBALT_100 MIDR_CPU_MODEL(ARM_CPU_IMP_MICROSOFT, MICROSOFT_CPU_PART_AZURE_COBALT_100) diff --git a/arch/arm64/kernel/Makefile b/arch/arm64/kernel/Makefile index 81df89483ba9d..2db39f6e1b007 100644 --- a/arch/arm64/kernel/Makefile +++ b/arch/arm64/kernel/Makefile @@ -37,6 +37,8 @@ obj-y := debug-monitors.o entry.o irq.o fpsimd.o \ cpufeature_impdef.o \ rsi.o jump_label.o +obj-$(CONFIG_ARCH_APPLE) += idle-apple.o + obj-$(CONFIG_COMPAT) += sys32.o signal32.o \ sys_compat.o obj-$(CONFIG_COMPAT) += sigreturn32.o diff --git a/arch/arm64/kernel/idle-apple.S b/arch/arm64/kernel/idle-apple.S new file mode 100644 index 0000000000000..02c3e4f6402ca --- /dev/null +++ b/arch/arm64/kernel/idle-apple.S @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include + +/* + * T8140 secondaries can resume from WFI with cleared general registers. + * SP and stack memory survive. Preserve the AAPCS64 callee-saved registers + * and x18 (which may hold the shadow call stack), including the return PC. + * This follows the stateless-wait approach used during m1n1 M5 Windows + * bring-up. It does not configure an Apple deep-sleep state or DVFS policy. + * + * IRQ masking/context management remains with cpu_do_idle(). The direct + * function call tells its compiler that x0-x17 and flags are clobbered. + */ + .macro apple_wait_save + stp x29, x30, [sp, #-112]! + mov x29, sp + stp x19, x20, [sp, #16] + stp x21, x22, [sp, #32] + stp x23, x24, [sp, #48] + stp x25, x26, [sp, #64] + stp x27, x28, [sp, #80] + str x18, [sp, #96] + dsb sy + .endm + + .macro apple_wait_restore + isb + ldr x18, [sp, #96] + ldp x19, x20, [sp, #16] + ldp x21, x22, [sp, #32] + ldp x23, x24, [sp, #48] + ldp x25, x26, [sp, #64] + ldp x27, x28, [sp, #80] + ldp x29, x30, [sp], #112 + ret + .endm + + .pushsection .cpuidle.text, "ax" +SYM_FUNC_START(apple_cpu_wfi) + apple_wait_save + wfi + apple_wait_restore +SYM_FUNC_END(apple_cpu_wfi) + .popsection diff --git a/arch/arm64/kernel/idle.c b/arch/arm64/kernel/idle.c index 42c5543f9589c..45bc79f128543 100644 --- a/arch/arm64/kernel/idle.c +++ b/arch/arm64/kernel/idle.c @@ -4,11 +4,16 @@ */ #include +#include #include +#include +#include #include #include #include +#include +#include #include enum { @@ -34,6 +39,41 @@ static int __init setup_idle(char *arg) } early_param("idle", setup_idle); +#ifdef CONFIG_ARCH_APPLE +/* + * Apple T8140 (A18 Pro) secondary CPUs can return from WFI with their + * general-purpose and FP/SIMD registers cleared. The key is enabled once + * at boot on those parts; everywhere else the check is a patched-out branch. + */ +static DEFINE_STATIC_KEY_FALSE(apple_wfi_loses_regs); + +static const struct midr_range apple_wfi_loses_regs_cpus[] __initconst = { + MIDR_ALL_VERSIONS(MIDR_APPLE_TAHITI_E), + MIDR_ALL_VERSIONS(MIDR_APPLE_TAHITI_P), + {} +}; + +static int __init apple_wfi_quirk_init(void) +{ + if (is_midr_in_range_list(apple_wfi_loses_regs_cpus)) { + static_branch_enable(&apple_wfi_loses_regs); + pr_info("Apple T8140: preserving registers across WFI\n"); + } + return 0; +} +early_initcall(apple_wfi_quirk_init); + +static __always_inline bool apple_wfi_needs_save(void) +{ + return static_branch_unlikely(&apple_wfi_loses_regs); +} +#else +static __always_inline bool apple_wfi_needs_save(void) +{ + return false; +} +#endif + /* * cpu_do_idle() * @@ -50,8 +90,18 @@ void __cpuidle cpu_do_idle(void) arm_cpuidle_save_irq_context(&context); if (likely(idle == ARM64_IDLE_WFI)) { - dsb(sy); - wfi(); + if (apple_wfi_needs_save()) { + /* + * The FP/SIMD registers do not survive either: drop the + * lazy ownership so the next return to userspace reloads + * them, even when the same task resumes on this CPU. + */ + fpsimd_save_and_flush_cpu_state(); + apple_cpu_wfi(); + } else { + dsb(sy); + wfi(); + } } else if (idle == ARM64_IDLE_YIELD) { dsb(sy); asm volatile("yield" ::: "memory"); diff --git a/drivers/cpufreq/apple-soc-cpufreq.c b/drivers/cpufreq/apple-soc-cpufreq.c index 60935032919d2..c37fb272ada73 100644 --- a/drivers/cpufreq/apple-soc-cpufreq.c +++ b/drivers/cpufreq/apple-soc-cpufreq.c @@ -21,6 +21,7 @@ #include #include #include +#include #define APPLE_DVFS_CMD 0x20 #define APPLE_DVFS_CMD_BUSY BIT(31) @@ -62,6 +63,8 @@ struct apple_soc_cpufreq_info { bool has_ps2; + bool verify_transition; + u64 min_pstate; u64 max_pstate; u64 cur_pstate_mask; u64 cur_pstate_shift; @@ -73,6 +76,9 @@ struct apple_cpu_priv { struct device *cpu_dev; void __iomem *reg_base; const struct apple_soc_cpufreq_info *info; + bool transition_failed; + unsigned int expected_pstate; + struct apple_smc_thermal_cpu *thermal; }; static struct cpufreq_driver apple_soc_cpufreq_driver; @@ -104,6 +110,20 @@ static const struct apple_soc_cpufreq_info soc_t8112_info = { .ps1_shift = APPLE_DVFS_CMD_PS1_SHIFT, }; +/* + * T8140 uses 5-bit PS1 state IDs starting at 1. The command register reads + * back the last accepted request, not a measured frequency; every transition + * is verified, see apple_soc_cpufreq_set_target(). Voltage, PLL and firmware + * throttling configuration are left as the bootloader set them. + */ +static const struct apple_soc_cpufreq_info soc_t8140_info = { + .verify_transition = true, + .min_pstate = 1, + .max_pstate = 31, + .ps1_mask = APPLE_DVFS_CMD_PS1, + .ps1_shift = APPLE_DVFS_CMD_PS1_SHIFT, +}; + static const struct apple_soc_cpufreq_info soc_default_info = { .has_ps2 = false, .max_pstate = 15, @@ -129,6 +149,10 @@ static const struct of_device_id apple_soc_cpufreq_of_match[] __maybe_unused = { .compatible = "apple,cluster-cpufreq", .data = &soc_default_info, }, + { + .compatible = "apple,t8140-cluster-cpufreq", + .data = &soc_t8140_info, + }, {} }; @@ -175,16 +199,39 @@ static int apple_soc_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int pstate = policy->freq_table[index].driver_data; u64 reg; - /* Fallback for newer SoCs */ - if (index > priv->info->max_pstate) - index = priv->info->max_pstate; + /* OPP level is a hardware ID, not the frequency-table array index. */ + if (pstate < priv->info->min_pstate || pstate > priv->info->max_pstate) + return -EINVAL; + if (priv->transition_failed) + return -EIO; if (readq_poll_timeout_atomic(priv->reg_base + APPLE_DVFS_CMD, reg, !(reg & APPLE_DVFS_CMD_BUSY), 2, APPLE_DVFS_TRANSITION_TIMEOUT)) { + if (priv->info->verify_transition) { + priv->transition_failed = true; + apple_smc_thermal_cpu_fault(priv->thermal); + dev_err(priv->cpu_dev, + "DVFS busy timeout, command=%#llx; stopping requests\n", reg); + } return -EIO; } + if (priv->info->verify_transition) { + unsigned int previous = FIELD_GET(APPLE_DVFS_CMD_PS1, reg); + + /* Do not fight an unexpected retained policy owner. */ + if (previous != priv->expected_pstate) { + priv->transition_failed = true; + apple_smc_thermal_cpu_fault(priv->thermal); + dev_err(priv->cpu_dev, "unexpected DVFS state %u (expected %u); stopping requests\n", + previous, priv->expected_pstate); + return -EIO; + } + if (previous == pstate) + return 0; + } + reg &= ~priv->info->ps1_mask; reg |= pstate << priv->info->ps1_shift; if (priv->info->has_ps2) { @@ -195,6 +242,21 @@ static int apple_soc_cpufreq_set_target(struct cpufreq_policy *policy, writeq_relaxed(reg, priv->reg_base + APPLE_DVFS_CMD); + if (priv->info->verify_transition && + readq_poll_timeout_atomic(priv->reg_base + APPLE_DVFS_CMD, reg, + !(reg & APPLE_DVFS_CMD_BUSY) && + FIELD_GET(APPLE_DVFS_CMD_PS1, reg) == pstate, + 2, APPLE_DVFS_TRANSITION_TIMEOUT)) { + /* Do not issue another request or guess a rollback after failure. */ + priv->transition_failed = true; + apple_smc_thermal_cpu_fault(priv->thermal); + dev_err(priv->cpu_dev, + "DVFS completion timeout, command=%#llx; stopping requests\n", reg); + return -ETIMEDOUT; + } + if (priv->info->verify_transition) + priv->expected_pstate = pstate; + return 0; } @@ -222,13 +284,15 @@ static int apple_soc_cpufreq_find_cluster(struct cpufreq_policy *policy, return ret; match = of_match_node(apple_soc_cpufreq_of_match, args.np); - of_node_put(args.np); - if (!match) + if (!match || !of_device_is_available(args.np)) { + of_node_put(args.np); return -ENODEV; + } *info = match->data; *reg_base = of_iomap(args.np, 0); + of_node_put(args.np); if (!*reg_base) return -ENOMEM; @@ -237,6 +301,7 @@ static int apple_soc_cpufreq_find_cluster(struct cpufreq_policy *policy, static int apple_soc_cpufreq_init(struct cpufreq_policy *policy) { + struct cpufreq_frequency_table *p; int ret, i; unsigned int transition_latency; void __iomem *reg_base; @@ -251,21 +316,19 @@ static int apple_soc_cpufreq_init(struct cpufreq_policy *policy) return -ENODEV; } - ret = dev_pm_opp_of_add_table(cpu_dev); - if (ret < 0) { - dev_err(cpu_dev, "%s: failed to add OPP table: %d\n", __func__, ret); - return ret; - } + priv = kzalloc_obj(*priv); + if (!priv) + return -ENOMEM; ret = apple_soc_cpufreq_find_cluster(policy, ®_base, &info); if (ret) { dev_err(cpu_dev, "%s: failed to get cluster info: %d\n", __func__, ret); - return ret; + goto out_free_priv; } - ret = dev_pm_opp_set_sharing_cpus(cpu_dev, policy->cpus); - if (ret) { - dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n", __func__, ret); + ret = dev_pm_opp_of_cpumask_add_table(policy->cpus); + if (ret < 0) { + dev_err(cpu_dev, "%s: failed to add OPP table: %d\n", __func__, ret); goto out_iounmap; } @@ -273,19 +336,13 @@ static int apple_soc_cpufreq_init(struct cpufreq_policy *policy) if (ret <= 0) { dev_dbg(cpu_dev, "OPP table is not ready, deferring probe\n"); ret = -EPROBE_DEFER; - goto out_free_opp; - } - - priv = kzalloc_obj(*priv); - if (!priv) { - ret = -ENOMEM; - goto out_free_opp; + goto out_free_table; } ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table); if (ret) { dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret); - goto out_free_priv; + goto out_free_table; } /* Get OPP levels (p-state indexes) and stash them in driver_data */ @@ -299,6 +356,33 @@ static int apple_soc_cpufreq_init(struct cpufreq_policy *policy) } freq_table[i].driver_data = dev_pm_opp_get_level(opp); dev_pm_opp_put(opp); + if (freq_table[i].driver_data < info->min_pstate || + freq_table[i].driver_data > info->max_pstate) { + ret = -EINVAL; + goto out_free_cpufreq_table; + } + } + + if (info->verify_transition) { + u64 cmd; + unsigned int state; + + /* Registration must not silently reset an unknown inherited state. */ + ret = readq_poll_timeout_atomic(reg_base + APPLE_DVFS_CMD, cmd, + !(cmd & APPLE_DVFS_CMD_BUSY), 2, + APPLE_DVFS_TRANSITION_TIMEOUT); + if (ret) + goto out_free_cpufreq_table; + state = FIELD_GET(APPLE_DVFS_CMD_PS1, cmd); + for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) + if (freq_table[i].driver_data == state) + break; + if (freq_table[i].frequency == CPUFREQ_TABLE_END) { + ret = -ERANGE; + goto out_free_cpufreq_table; + } + policy->cur = freq_table[i].frequency; + priv->expected_pstate = state; } priv->cpu_dev = cpu_dev; @@ -308,24 +392,48 @@ static int apple_soc_cpufreq_init(struct cpufreq_policy *policy) policy->freq_table = freq_table; transition_latency = dev_pm_opp_get_max_transition_latency(cpu_dev); - if (!transition_latency) + if (!transition_latency) { + /* Conservative transaction bound, not a measured transition time. */ transition_latency = APPLE_DVFS_TRANSITION_TIMEOUT * NSEC_PER_USEC; + if (info->verify_transition) + transition_latency *= 2; + } policy->cpuinfo.transition_latency = transition_latency; policy->dvfs_possible_from_any_cpu = true; - policy->fast_switch_possible = true; + policy->fast_switch_possible = !info->verify_transition; policy->suspend_freq = freq_table[0].frequency; + if (info->verify_transition) { + struct device_node *hwmon; + bool higher = false; + bool thermal; + + /* P-states above 2 are only allowed under the SMC thermal policy. */ + cpufreq_for_each_valid_entry(p, policy->freq_table) + if (p->driver_data > 2) + higher = true; + hwmon = of_find_compatible_node(NULL, NULL, "apple,smc-hwmon"); + thermal = hwmon && of_property_read_bool(hwmon, "apple,cpu-thermal-policy"); + of_node_put(hwmon); + if (higher || thermal) { + priv->thermal = apple_smc_thermal_cpu_add(policy); + if (IS_ERR(priv->thermal)) { + ret = PTR_ERR(priv->thermal); + goto out_free_cpufreq_table; + } + } + } return 0; out_free_cpufreq_table: dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table); -out_free_priv: - kfree(priv); -out_free_opp: - dev_pm_opp_remove_all_dynamic(cpu_dev); +out_free_table: + dev_pm_opp_of_cpumask_remove_table(policy->cpus); out_iounmap: iounmap(reg_base); +out_free_priv: + kfree(priv); return ret; } @@ -333,12 +441,24 @@ static void apple_soc_cpufreq_exit(struct cpufreq_policy *policy) { struct apple_cpu_priv *priv = policy->driver_data; + apple_smc_thermal_cpu_remove(priv->thermal); dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table); - dev_pm_opp_remove_all_dynamic(priv->cpu_dev); + /* + * The core clears the last CPU from policy->cpus before calling + * ->exit(), so remove the tables of every CPU the policy covered. + */ + dev_pm_opp_of_cpumask_remove_table(policy->related_cpus); iounmap(priv->reg_base); kfree(priv); } +static void apple_soc_cpufreq_ready(struct cpufreq_policy *policy) +{ + struct apple_cpu_priv *priv = policy->driver_data; + + apple_smc_thermal_cpu_ready(priv->thermal); +} + static struct cpufreq_driver apple_soc_cpufreq_driver = { .name = "apple-cpufreq", .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY | @@ -352,6 +472,7 @@ static struct cpufreq_driver apple_soc_cpufreq_driver = { .register_em = cpufreq_register_em_with_opp, .set_boost = cpufreq_boost_set_sw, .suspend = cpufreq_generic_suspend, + .ready = apple_soc_cpufreq_ready, }; static int __init apple_soc_cpufreq_module_init(void) diff --git a/drivers/hwmon/macsmc-hwmon.c b/drivers/hwmon/macsmc-hwmon.c index 1500ec2cc9f83..b41f8813d4165 100644 --- a/drivers/hwmon/macsmc-hwmon.c +++ b/drivers/hwmon/macsmc-hwmon.c @@ -22,17 +22,26 @@ #include #include +#include #include #include #include #include #include +#include #define MAX_LABEL_LENGTH 32 /* Temperature, voltage, current, power, fan(s) */ #define NUM_SENSOR_TYPES 5 +/* + * The J700 CPU thermal policy samples a fixed set of die temperatures, which + * must therefore be the first temperature channels; any further channels are + * only reported to userspace and never feed the policy. + */ +#define APPLE_SMC_CPU_POLICY_SENSORS 6 + #define FLT_EXP_BIAS 127 #define FLT_EXP_MASK GENMASK(30, 23) #define FLT_MANT_BIAS 23 @@ -86,6 +95,12 @@ struct macsmc_hwmon { struct macsmc_hwmon_sensors curr; struct macsmc_hwmon_sensors power; struct macsmc_hwmon_fans fan; + bool cpu_thermal_policy; + /* Protects the thermal_cache fields below. */ + struct mutex thermal_cache_lock; + long thermal_cache[APPLE_SMC_CPU_POLICY_SENSORS]; + unsigned long thermal_cache_time; + int thermal_cache_error; }; static int macsmc_hwmon_read_label(struct device *dev, @@ -151,31 +166,42 @@ static int macsmc_hwmon_read_f32_scaled(struct apple_smc *smc, smc_key key, if (ret < 0) return ret; - val = ((u64)((fval & FLT_MANT_MASK) | BIT(23))); - exp = ((fval >> 23) & 0xff) - FLT_EXP_BIAS - FLT_MANT_BIAS; + exp = FIELD_GET(FLT_EXP_MASK, fval); + /* NaN and infinity are unavailable readings, not extreme temperatures. */ + if (exp == 0xff) + return -ENODATA; + + val = fval & FLT_MANT_MASK; + if (exp) + val |= BIT(23); + else + exp = 1; /* Zero and subnormal values have no implicit leading one. */ + exp -= FLT_EXP_BIAS + FLT_MANT_BIAS; /* We never have negatively scaled SMC floats */ val *= scale; if (exp > 63) - val = U64_MAX; + return -ERANGE; else if (exp < -63) val = 0; else if (exp < 0) val >>= -exp; else if (exp != 0 && (val & ~((1ULL << (64 - exp)) - 1))) /* overflow */ - val = U64_MAX; + return -ERANGE; else val <<= exp; if (fval & FLT_SIGN_MASK) { if (val > (u64)LONG_MAX + 1) + return -ERANGE; + if (val == (u64)LONG_MAX + 1) *p = LONG_MIN; else *p = -(long)val; } else { if (val > (u64)LONG_MAX) - *p = LONG_MAX; + return -ERANGE; else *p = (long)val; } @@ -348,6 +374,17 @@ static int macsmc_hwmon_read(struct device *dev, enum hwmon_sensor_types type, switch (type) { case hwmon_temp: + if (hwmon->cpu_thermal_policy && channel < APPLE_SMC_CPU_POLICY_SENSORS) { + guard(mutex)(&hwmon->thermal_cache_lock); + + if (hwmon->thermal_cache_error) + return hwmon->thermal_cache_error; + if (time_after_eq(jiffies, hwmon->thermal_cache_time + + msecs_to_jiffies(APPLE_SMC_THERMAL_MAX_AGE_MS))) + return -ETIMEDOUT; + *val = hwmon->thermal_cache[channel]; + return 0; + } ret = macsmc_hwmon_read_key(hwmon->smc, &hwmon->temp.sensors[channel], 1000, val); break; @@ -771,6 +808,70 @@ static int macsmc_hwmon_create_infos(struct macsmc_hwmon *hwmon) return 0; } +static int macsmc_hwmon_cpu_sample(void *ctx, int *temp) +{ + struct macsmc_hwmon *hwmon = ctx; + unsigned long start = jiffies; + long values[APPLE_SMC_CPU_POLICY_SENSORS], maximum = LONG_MIN; + unsigned int i; + int ret = 0; + + for (i = 0; i < ARRAY_SIZE(values); i++) { + ret = macsmc_hwmon_read_key(hwmon->smc, &hwmon->temp.sensors[i], 1000, &values[i]); + if (ret) + break; + if (values[i] < INT_MIN || values[i] > INT_MAX) { + ret = -ERANGE; + break; + } + maximum = max(maximum, values[i]); + } + if (!ret && time_after_eq(jiffies, start + msecs_to_jiffies(APPLE_SMC_THERMAL_MAX_AGE_MS))) + ret = -ETIMEDOUT; + guard(mutex)(&hwmon->thermal_cache_lock); + hwmon->thermal_cache_error = ret; + if (!ret) { + memcpy(hwmon->thermal_cache, values, sizeof(values)); + hwmon->thermal_cache_time = start; + *temp = maximum; + } + return ret; +} + +static int macsmc_hwmon_cpu_policy(struct macsmc_hwmon *hwmon) +{ + static const smc_key keys[] = { + SMC_KEY(Te05), SMC_KEY(Te0S), SMC_KEY(Tp01), + SMC_KEY(Tp05), SMC_KEY(Tp09), SMC_KEY(Tp0D), + }; + unsigned int i, j, seen = 0; + + static_assert(ARRAY_SIZE(keys) == APPLE_SMC_CPU_POLICY_SENSORS); + + if (!of_property_read_bool(hwmon->dev->of_node, "apple,cpu-thermal-policy")) + return 0; + if (!of_machine_is_compatible("apple,j700") || hwmon->temp.count < ARRAY_SIZE(keys)) + return -EINVAL; + for (i = 0; i < ARRAY_SIZE(keys); i++) { + struct macsmc_hwmon_sensor *sensor = &hwmon->temp.sensors[i]; + + if (sensor->info.type_code != __SMC_KEY('f', 'l', 't', ' ') || + sensor->info.size != 4 || !(sensor->info.flags & APPLE_SMC_READABLE) || + (sensor->info.flags & APPLE_SMC_FUNCTION)) + return -EINVAL; + for (j = 0; j < ARRAY_SIZE(keys); j++) + if (sensor->macsmc_key == keys[j]) + break; + if (j == ARRAY_SIZE(keys) || seen & BIT(j)) + return -EINVAL; + seen |= BIT(j); + } + mutex_init(&hwmon->thermal_cache_lock); + hwmon->thermal_cache_error = -ENODATA; + hwmon->cpu_thermal_policy = true; + return devm_apple_smc_thermal_register(hwmon->dev, macsmc_hwmon_cpu_sample, hwmon); +} + static int macsmc_hwmon_probe(struct platform_device *pdev) { struct apple_smc *smc = dev_get_drvdata(pdev->dev.parent); @@ -810,6 +911,9 @@ static int macsmc_hwmon_probe(struct platform_device *pdev) ret = macsmc_hwmon_create_infos(hwmon); if (ret) return ret; + ret = macsmc_hwmon_cpu_policy(hwmon); + if (ret) + return dev_err_probe(hwmon->dev, ret, "CPU thermal provider setup failed\n"); hwmon->chip_info.ops = &macsmc_hwmon_ops; hwmon->chip_info.info = diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig index b10080d618604..15fc1d56d9540 100644 --- a/drivers/thermal/Kconfig +++ b/drivers/thermal/Kconfig @@ -527,4 +527,24 @@ config LOONGSON2_THERMAL is higher than the high temperature threshold or lower than the low temperature threshold, the interrupt will occur. +config APPLE_SMC_CPU_THERMAL + bool "Apple J700 SMC CPU thermal policy" + depends on ARCH_APPLE || COMPILE_TEST + depends on ARM_APPLE_SOC_CPUFREQ && SENSORS_MACSMC_HWMON + depends on CPU_FREQ_THERMAL + select THERMAL_GOV_STEP_WISE + help + CPU thermal policy for the Apple J700 (T8140). It follows the + hottest of six SMC CPU die sensors, sends both CPU clusters to + their lowest frequency through cpufreq cooling devices above a + passive trip, and holds them there through a frequency QoS + request while the readings are missing, stale or failing, while + the zone is disabled, or above a second, hard threshold. + + apple-soc-cpufreq only allows T8140 P-states above 2 when this + policy is present. The thresholds are Linux operating policy, not + Apple or silicon limits. + + Say Y for J700 kernels. + endif diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile index bb21e7ea7fc6b..e88a682c75851 100644 --- a/drivers/thermal/Makefile +++ b/drivers/thermal/Makefile @@ -68,4 +68,5 @@ obj-$(CONFIG_AMLOGIC_THERMAL) += amlogic_thermal.o obj-$(CONFIG_SPRD_THERMAL) += sprd_thermal.o obj-$(CONFIG_KHADAS_MCU_FAN_THERMAL) += khadas_mcu_fan.o obj-$(CONFIG_LOONGSON2_THERMAL) += loongson2_thermal.o +obj-$(CONFIG_APPLE_SMC_CPU_THERMAL) += apple-smc-cpu-thermal.o obj-$(CONFIG_THERMAL_CORE_TESTING) += testing/ diff --git a/drivers/thermal/apple-smc-cpu-thermal.c b/drivers/thermal/apple-smc-cpu-thermal.c new file mode 100644 index 0000000000000..8ad053205f13e --- /dev/null +++ b/drivers/thermal/apple-smc-cpu-thermal.c @@ -0,0 +1,370 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * Apple J700 CPU thermal policy over selected SMC telemetry + * + * Copyright (C) 2026 Eryk Wieliczko + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * Two controls act on the hottest of the six selected sensors: a passive + * trip bound to maximum cpufreq cooling (95 C, released below 85 C), and a + * hard tier that pins both clusters to their minimum frequency through a + * frequency-QoS guard (from 103 C, released below 98 C). The guard also + * holds the minimum while readings are missing, stale or failing, while the + * zone is disabled, and after a cpufreq DVFS fault. + * + * The band sits above the idle temperature: the original 80 C trip with a + * 70 C release latched the throttle permanently while the die idled at + * 70-77 C with the spinning idle loop, because step_wise never saw it drop + * below the release point. + * + * The thresholds are Linux operating policy, not Apple or silicon limits. + * SMC readings can lag a successful read by about a second under load, so + * faster host polling does not bound the sensor age; the 200 ms limit only + * bounds the host transaction. + */ +#define SMC_TRIP_MC 95000 +#define SMC_RELEASE_MC 85000 +#define SMC_HOT_MC 103000 +#define SMC_HOT_RELEASE_MC 98000 +#define SMC_SAMPLE_MS 25 +#define SMC_MAX_AGE_MS APPLE_SMC_THERMAL_MAX_AGE_MS +#define SMC_WATCH_MS 25 + +struct apple_smc_thermal_cpu { + struct cpufreq_policy *policy; + struct freq_qos_request guard; + struct thermal_cooling_device *cdev; + unsigned int min_freq; + bool failed; +}; + +static DEFINE_MUTEX(smc_lock); +/* Only sampling/removal take this lock: a stuck SMC read cannot block guards. */ +static DEFINE_MUTEX(smc_provider_lock); +static struct apple_smc_thermal_cpu *smc_cpus[2]; +static struct thermal_zone_device *smc_zone; +static int (*smc_read)(void *, int *); +static void *smc_ctx; +static unsigned long smc_sample_time; +static unsigned int smc_good; +static int smc_value, smc_error = -ENODATA; +static bool smc_enabled, smc_hot; +static void smc_sample_work(struct work_struct *work); +static void smc_watch_work(struct work_struct *work); +static DECLARE_DELAYED_WORK(smc_sample, smc_sample_work); +static DECLARE_DELAYED_WORK(smc_watch, smc_watch_work); + +static bool smc_fresh(void) +{ + return !smc_error && time_before(jiffies, + smc_sample_time + msecs_to_jiffies(SMC_MAX_AGE_MS)); +} + +/* smc_lock held. QoS is independent of user limits and the thermal governor. */ +static void smc_update_guards(void) +{ + bool healthy = smc_zone && smc_enabled && smc_fresh() && smc_good >= 3; + bool clamp = !healthy || smc_hot; + bool error = false; + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(smc_cpus); i++) + if (smc_cpus[i] && READ_ONCE(smc_cpus[i]->failed)) + clamp = true; + for (i = 0; i < ARRAY_SIZE(smc_cpus); i++) { + struct apple_smc_thermal_cpu *cpu = smc_cpus[i]; + int ret; + + if (!cpu) + continue; + ret = freq_qos_update_request(&cpu->guard, clamp || READ_ONCE(cpu->failed) ? + cpu->min_freq : FREQ_QOS_MAX_DEFAULT_VALUE); + if (ret < 0) { + error = true; + WRITE_ONCE(cpu->failed, true); + freq_qos_update_request(&cpu->guard, cpu->min_freq); + pr_err_ratelimited("apple-smc-thermal: CPU%u guard update failed: %d\n", + cpu->policy->cpu, ret); + } + } + if (error) + for (i = 0; i < ARRAY_SIZE(smc_cpus); i++) + if (smc_cpus[i]) + freq_qos_update_request(&smc_cpus[i]->guard, smc_cpus[i]->min_freq); +} + +static int smc_zone_temp(struct thermal_zone_device *zone, int *temp) +{ + guard(mutex)(&smc_lock); + if (!smc_fresh()) + return smc_error ?: -ETIMEDOUT; + *temp = smc_value; + return 0; +} + +static int smc_zone_mode(struct thermal_zone_device *zone, enum thermal_device_mode mode) +{ + guard(mutex)(&smc_lock); + smc_enabled = mode == THERMAL_DEVICE_ENABLED; + smc_good = 0; + smc_update_guards(); + return 0; +} + +static bool smc_zone_bind(struct thermal_zone_device *zone, const struct thermal_trip *trip, + struct thermal_cooling_device *cdev, struct cooling_spec *spec) +{ + unsigned int i; + + guard(mutex)(&smc_lock); + for (i = 0; i < ARRAY_SIZE(smc_cpus); i++) + if (smc_cpus[i] && smc_cpus[i]->cdev == cdev) { + spec->lower = cdev->max_state; + spec->upper = cdev->max_state; + return true; + } + return false; +} + +static const struct thermal_zone_device_ops smc_zone_ops = { + .get_temp = smc_zone_temp, + .change_mode = smc_zone_mode, + .should_bind = smc_zone_bind, +}; + +static const struct thermal_trip smc_trips[] = { + { .temperature = SMC_TRIP_MC, .hysteresis = SMC_TRIP_MC - SMC_RELEASE_MC, + .type = THERMAL_TRIP_PASSIVE }, +}; + +static const struct thermal_zone_params smc_params = { + .governor_name = "step_wise", + .no_hwmon = true, +}; + +static int smc_check_binding(struct thermal_trip *trip, void *data) +{ + struct thermal_zone_device *zone = data; + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(smc_cpus); i++) + if (!thermal_trip_is_bound_to_cdev(zone, trip, smc_cpus[i]->cdev)) + return -ENODEV; + return 0; +} + +/* provider_lock held; never register/update a zone while holding smc_lock. */ +static void smc_start_zone(void) +{ + struct thermal_zone_device *zone; + int ret; + + if (smc_zone || !smc_read || !smc_cpus[0] || !smc_cpus[1] || + !smc_cpus[0]->cdev || !smc_cpus[1]->cdev) + return; + zone = thermal_zone_device_register_with_trips("apple-smc-selected", + smc_trips, + ARRAY_SIZE(smc_trips), + NULL, &smc_zone_ops, + &smc_params, + SMC_SAMPLE_MS, + SMC_SAMPLE_MS); + if (IS_ERR(zone)) { + pr_err("apple-smc-thermal: zone registration failed: %ld\n", PTR_ERR(zone)); + return; + } + ret = for_each_thermal_trip(zone, smc_check_binding, zone); + if (ret) { + thermal_zone_device_unregister(zone); + pr_err("apple-smc-thermal: missing CPU cooling binding\n"); + return; + } + mutex_lock(&smc_lock); + smc_zone = zone; + mutex_unlock(&smc_lock); + ret = thermal_zone_device_enable(zone); + if (ret) + pr_err("apple-smc-thermal: zone enable failed: %d; retaining minimum caps\n", ret); +} + +static void smc_sample_work(struct work_struct *work) +{ + unsigned long start = jiffies; + int value = 0, ret; + + guard(mutex)(&smc_provider_lock); + if (!smc_read) + return; + ret = smc_read(smc_ctx, &value); + if (!ret && time_after_eq(jiffies, start + msecs_to_jiffies(SMC_MAX_AGE_MS))) + ret = -ETIMEDOUT; + mutex_lock(&smc_lock); + smc_error = ret; + if (ret) { + smc_good = 0; + } else { + smc_value = value; + smc_sample_time = start; /* Bound the age of the oldest read in the round. */ + if (smc_good < 3) + smc_good++; + if (value >= SMC_HOT_MC) + smc_hot = true; + else if (value < SMC_HOT_RELEASE_MC) + smc_hot = false; + } + smc_update_guards(); + mutex_unlock(&smc_lock); + if (smc_zone) + thermal_zone_device_update(smc_zone, THERMAL_EVENT_UNSPECIFIED); + queue_delayed_work(system_unbound_wq, &smc_sample, msecs_to_jiffies(SMC_SAMPLE_MS)); +} + +static void smc_watch_work(struct work_struct *work) +{ + guard(mutex)(&smc_lock); + if (!smc_fresh()) + smc_good = 0; + smc_update_guards(); + if (smc_cpus[0] || smc_cpus[1]) + queue_delayed_work(system_unbound_wq, &smc_watch, msecs_to_jiffies(SMC_WATCH_MS)); +} + +struct apple_smc_thermal_cpu *apple_smc_thermal_cpu_add(struct cpufreq_policy *policy) +{ + struct apple_smc_thermal_cpu *cpu; + unsigned int i; + int ret; + + if (!of_machine_is_compatible("apple,j700")) + return ERR_PTR(-ENODEV); + cpu = kzalloc_obj(*cpu); + if (!cpu) + return ERR_PTR(-ENOMEM); + cpu->policy = policy; + cpu->min_freq = policy->freq_table[0].frequency; + ret = freq_qos_add_request(&policy->constraints, &cpu->guard, FREQ_QOS_MAX, + policy->freq_table[0].frequency); + if (ret < 0) { + kfree(cpu); + return ERR_PTR(ret); + } + mutex_lock(&smc_provider_lock); + mutex_lock(&smc_lock); + for (i = 0; i < ARRAY_SIZE(smc_cpus); i++) + if (!smc_cpus[i]) { + smc_cpus[i] = cpu; + break; + } + mutex_unlock(&smc_lock); + mutex_unlock(&smc_provider_lock); + if (i == ARRAY_SIZE(smc_cpus)) { + freq_qos_remove_request(&cpu->guard); + kfree(cpu); + return ERR_PTR(-EBUSY); + } + queue_delayed_work(system_unbound_wq, &smc_watch, 0); + return cpu; +} +EXPORT_SYMBOL_GPL(apple_smc_thermal_cpu_add); + +void apple_smc_thermal_cpu_ready(struct apple_smc_thermal_cpu *cpu) +{ + if (!cpu) + return; + guard(mutex)(&smc_provider_lock); + cpu->cdev = cpufreq_cooling_register(cpu->policy); + if (IS_ERR(cpu->cdev)) { + pr_err("apple-smc-thermal: cooling registration failed: %ld\n", PTR_ERR(cpu->cdev)); + cpu->cdev = NULL; + return; + } + smc_start_zone(); +} +EXPORT_SYMBOL_GPL(apple_smc_thermal_cpu_ready); + +void apple_smc_thermal_cpu_fault(struct apple_smc_thermal_cpu *cpu) +{ + if (!cpu) + return; + /* Called inside target_index: do not recursively take the policy lock. */ + WRITE_ONCE(cpu->failed, true); + mod_delayed_work(system_unbound_wq, &smc_watch, 0); +} +EXPORT_SYMBOL_GPL(apple_smc_thermal_cpu_fault); + +void apple_smc_thermal_cpu_remove(struct apple_smc_thermal_cpu *cpu) +{ + struct thermal_zone_device *zone; + unsigned int i; + + if (!cpu) + return; + guard(mutex)(&smc_provider_lock); + mutex_lock(&smc_lock); + zone = smc_zone; + smc_zone = NULL; + smc_enabled = false; + smc_good = 0; + smc_update_guards(); + mutex_unlock(&smc_lock); + if (zone) + thermal_zone_device_unregister(zone); + cpufreq_cooling_unregister(cpu->cdev); + mutex_lock(&smc_lock); + for (i = 0; i < ARRAY_SIZE(smc_cpus); i++) + if (smc_cpus[i] == cpu) + smc_cpus[i] = NULL; + freq_qos_remove_request(&cpu->guard); + mutex_unlock(&smc_lock); + kfree(cpu); +} +EXPORT_SYMBOL_GPL(apple_smc_thermal_cpu_remove); + +static void smc_provider_remove(void *unused) +{ + mutex_lock(&smc_provider_lock); + smc_read = NULL; + smc_ctx = NULL; + mutex_lock(&smc_lock); + smc_error = -ENODEV; + smc_good = 0; + smc_update_guards(); + mutex_unlock(&smc_lock); + mutex_unlock(&smc_provider_lock); + cancel_delayed_work_sync(&smc_sample); +} + +int devm_apple_smc_thermal_register(struct device *dev, int (*read)(void *, int *), void *ctx) +{ + int ret; + + if (!of_machine_is_compatible("apple,j700")) + return -ENODEV; + mutex_lock(&smc_provider_lock); + if (smc_read) { + mutex_unlock(&smc_provider_lock); + return -EBUSY; + } + smc_ctx = ctx; + smc_read = read; + smc_start_zone(); + mutex_unlock(&smc_provider_lock); + ret = devm_add_action_or_reset(dev, smc_provider_remove, NULL); + if (!ret) + queue_delayed_work(system_unbound_wq, &smc_sample, 0); + return ret; +} +EXPORT_SYMBOL_GPL(devm_apple_smc_thermal_register); diff --git a/include/linux/soc/apple/smc-thermal.h b/include/linux/soc/apple/smc-thermal.h new file mode 100644 index 0000000000000..9cd5788e55cc8 --- /dev/null +++ b/include/linux/soc/apple/smc-thermal.h @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* + * Apple J700 CPU thermal policy: cpufreq and telemetry provider interface + * + * Copyright (C) 2026 Eryk Wieliczko + */ +#ifndef _APPLE_SMC_THERMAL_H +#define _APPLE_SMC_THERMAL_H + +#include +#include +#include + +struct cpufreq_policy; +struct device; +struct apple_smc_thermal_cpu; + +/* Maximum host transaction/cache age, NOT a firmware sensor-age guarantee. */ +#define APPLE_SMC_THERMAL_MAX_AGE_MS 200 + +#if IS_REACHABLE(CONFIG_APPLE_SMC_CPU_THERMAL) +struct apple_smc_thermal_cpu *apple_smc_thermal_cpu_add(struct cpufreq_policy *policy); +void apple_smc_thermal_cpu_ready(struct apple_smc_thermal_cpu *cpu); +void apple_smc_thermal_cpu_remove(struct apple_smc_thermal_cpu *cpu); +void apple_smc_thermal_cpu_fault(struct apple_smc_thermal_cpu *cpu); +int devm_apple_smc_thermal_register(struct device *dev, int (*read)(void *, int *), void *ctx); +#else +static inline struct apple_smc_thermal_cpu * +apple_smc_thermal_cpu_add(struct cpufreq_policy *policy) { return ERR_PTR(-ENODEV); } +static inline void apple_smc_thermal_cpu_ready(struct apple_smc_thermal_cpu *cpu) {} +static inline void apple_smc_thermal_cpu_remove(struct apple_smc_thermal_cpu *cpu) {} +static inline void apple_smc_thermal_cpu_fault(struct apple_smc_thermal_cpu *cpu) {} +static inline int devm_apple_smc_thermal_register(struct device *dev, + int (*read)(void *, int *), void *ctx) +{ return -ENODEV; } +#endif +#endif