1 // SPDX-License-Identifier: GPL-2.0
3 * temp.c Thermal management for cpu's with Thermal Assist Units
5 * Written by Troy Benjegerdes <hozer@drgw.net>
8 * dynamic power management to limit peak CPU temp (using ICTC)
11 * Silly, crazy ideas: use cpu load (from scheduler) and ICTC to extend battery
12 * life in portables, and add a 'performance/watt' metric somewhere in /proc
15 #include <linux/errno.h>
16 #include <linux/jiffies.h>
17 #include <linux/kernel.h>
18 #include <linux/param.h>
19 #include <linux/string.h>
21 #include <linux/interrupt.h>
22 #include <linux/init.h>
26 #include <asm/nvram.h>
27 #include <asm/cache.h>
28 #include <asm/8xx_immap.h>
29 #include <asm/machdep.h>
31 static struct tau_temp
39 struct timer_list tau_timer;
41 /* TODO: put these in a /proc interface, with some sanity checks, and maybe
42 * dynamic adjustment to minimize # of interrupts */
43 /* configurable values for step size and how much to expand the window when
44 * we get an interrupt. These are based on the limit that was out of range */
45 #define step_size 2 /* step size when temp goes out of range */
46 #define window_expand 1 /* expand the window by this much */
47 /* configurable values for shrinking the window */
48 #define shrink_timer 2*HZ /* period between shrinking the window */
49 #define min_window 2 /* minimum window size, degrees C */
51 void set_thresholds(unsigned long cpu)
56 * threshold, valid bit, enable interrupts, interrupt when below threshold
58 mtspr(SPRN_THRM1, THRM1_THRES(tau[cpu].low) | THRM1_V | THRM1_TIE | THRM1_TID);
61 * threshold, valid bit, enable interrupts, interrupt when above threshold
63 mtspr (SPRN_THRM2, THRM1_THRES(tau[cpu].high) | THRM1_V | THRM1_TIE);
65 /* same thing but don't enable interrupts */
66 mtspr(SPRN_THRM1, THRM1_THRES(tau[cpu].low) | THRM1_V | THRM1_TID);
67 mtspr(SPRN_THRM2, THRM1_THRES(tau[cpu].high) | THRM1_V);
71 void TAUupdate(int cpu)
74 u32 bits = THRM1_TIV | THRM1_TIN | THRM1_V;
76 /* if both thresholds are crossed, the step_sizes cancel out
77 * and the window winds up getting expanded twice. */
78 thrm = mfspr(SPRN_THRM1);
79 if ((thrm & bits) == bits) {
82 if (tau[cpu].low >= step_size) {
83 tau[cpu].low -= step_size;
84 tau[cpu].high -= (step_size - window_expand);
87 pr_debug("%s: low threshold crossed\n", __func__);
89 thrm = mfspr(SPRN_THRM2);
90 if ((thrm & bits) == bits) {
93 if (tau[cpu].high <= 127 - step_size) {
94 tau[cpu].low += (step_size - window_expand);
95 tau[cpu].high += step_size;
98 pr_debug("%s: high threshold crossed\n", __func__);
102 #ifdef CONFIG_TAU_INT
104 * TAU interrupts - called when we have a thermal assist unit interrupt
105 * with interrupts disabled
108 void TAUException(struct pt_regs * regs)
110 int cpu = smp_processor_id();
113 tau[cpu].interrupts++;
119 #endif /* CONFIG_TAU_INT */
121 static void tau_timeout(void * info)
127 cpu = smp_processor_id();
129 #ifndef CONFIG_TAU_INT
133 /* Stop thermal sensor comparisons and interrupts */
134 mtspr(SPRN_THRM3, 0);
136 size = tau[cpu].high - tau[cpu].low;
137 if (size > min_window && ! tau[cpu].grew) {
138 /* do an exponential shrink of half the amount currently over size */
139 shrink = (2 + size - min_window) / 4;
141 tau[cpu].low += shrink;
142 tau[cpu].high -= shrink;
143 } else { /* size must have been min_window + 1 */
146 if ((tau[cpu].high - tau[cpu].low) != min_window){
147 printk(KERN_ERR "temp.c: line %d, logic error\n", __LINE__);
157 /* Restart thermal sensor comparisons and interrupts.
158 * The "PowerPC 740 and PowerPC 750 Microprocessor Datasheet"
159 * recommends that "the maximum value be set in THRM3 under all
162 mtspr(SPRN_THRM3, THRM3_SITV(0x1fff) | THRM3_E);
165 static void tau_timeout_smp(unsigned long unused)
168 /* schedule ourselves to be run again */
169 mod_timer(&tau_timer, jiffies + shrink_timer) ;
170 on_each_cpu(tau_timeout, NULL, 0);
176 * Set things up to use THRM1 as a temperature lower bound, and THRM2 as an upper bound.
180 int tau_initialized = 0;
182 void __init TAU_init_smp(void * info)
184 unsigned long cpu = smp_processor_id();
186 /* set these to a reasonable value and let the timer shrink the
194 int __init TAU_init(void)
196 /* We assume in SMP that if one CPU has TAU support, they
199 if (!cpu_has_feature(CPU_FTR_TAU)) {
200 printk("Thermal assist unit not available\n");
206 /* first, set up the window shrinking timer */
207 init_timer(&tau_timer);
208 tau_timer.function = tau_timeout_smp;
209 tau_timer.expires = jiffies + shrink_timer;
210 add_timer(&tau_timer);
212 on_each_cpu(TAU_init_smp, NULL, 0);
214 printk("Thermal assist unit ");
215 #ifdef CONFIG_TAU_INT
216 printk("using interrupts, ");
218 printk("using timers, ");
220 printk("shrink_timer: %d jiffies\n", shrink_timer);
226 __initcall(TAU_init);
229 * return current temp
232 u32 cpu_temp_both(unsigned long cpu)
234 return ((tau[cpu].high << 16) | tau[cpu].low);
237 int cpu_temp(unsigned long cpu)
239 return ((tau[cpu].high + tau[cpu].low) / 2);
242 int tau_interrupts(unsigned long cpu)
244 return (tau[cpu].interrupts);