xref: /openbmc/linux/arch/powerpc/kernel/tau_6xx.c (revision b8265621)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * temp.c	Thermal management for cpu's with Thermal Assist Units
4  *
5  * Written by Troy Benjegerdes <hozer@drgw.net>
6  *
7  * TODO:
8  * dynamic power management to limit peak CPU temp (using ICTC)
9  * calibration???
10  *
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
13  */
14 
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>
20 #include <linux/mm.h>
21 #include <linux/interrupt.h>
22 #include <linux/init.h>
23 
24 #include <asm/io.h>
25 #include <asm/reg.h>
26 #include <asm/nvram.h>
27 #include <asm/cache.h>
28 #include <asm/8xx_immap.h>
29 #include <asm/machdep.h>
30 #include <asm/asm-prototypes.h>
31 
32 #include "setup.h"
33 
34 static struct tau_temp
35 {
36 	int interrupts;
37 	unsigned char low;
38 	unsigned char high;
39 	unsigned char grew;
40 } tau[NR_CPUS];
41 
42 struct timer_list tau_timer;
43 
44 #undef DEBUG
45 
46 /* TODO: put these in a /proc interface, with some sanity checks, and maybe
47  * dynamic adjustment to minimize # of interrupts */
48 /* configurable values for step size and how much to expand the window when
49  * we get an interrupt. These are based on the limit that was out of range */
50 #define step_size		2	/* step size when temp goes out of range */
51 #define window_expand		1	/* expand the window by this much */
52 /* configurable values for shrinking the window */
53 #define shrink_timer	2*HZ	/* period between shrinking the window */
54 #define min_window	2	/* minimum window size, degrees C */
55 
56 static void set_thresholds(unsigned long cpu)
57 {
58 #ifdef CONFIG_TAU_INT
59 	/*
60 	 * setup THRM1,
61 	 * threshold, valid bit, enable interrupts, interrupt when below threshold
62 	 */
63 	mtspr(SPRN_THRM1, THRM1_THRES(tau[cpu].low) | THRM1_V | THRM1_TIE | THRM1_TID);
64 
65 	/* setup THRM2,
66 	 * threshold, valid bit, enable interrupts, interrupt when above threshold
67 	 */
68 	mtspr (SPRN_THRM2, THRM1_THRES(tau[cpu].high) | THRM1_V | THRM1_TIE);
69 #else
70 	/* same thing but don't enable interrupts */
71 	mtspr(SPRN_THRM1, THRM1_THRES(tau[cpu].low) | THRM1_V | THRM1_TID);
72 	mtspr(SPRN_THRM2, THRM1_THRES(tau[cpu].high) | THRM1_V);
73 #endif
74 }
75 
76 static void TAUupdate(int cpu)
77 {
78 	unsigned thrm;
79 
80 #ifdef DEBUG
81 	printk("TAUupdate ");
82 #endif
83 
84 	/* if both thresholds are crossed, the step_sizes cancel out
85 	 * and the window winds up getting expanded twice. */
86 	if((thrm = mfspr(SPRN_THRM1)) & THRM1_TIV){ /* is valid? */
87 		if(thrm & THRM1_TIN){ /* crossed low threshold */
88 			if (tau[cpu].low >= step_size){
89 				tau[cpu].low -= step_size;
90 				tau[cpu].high -= (step_size - window_expand);
91 			}
92 			tau[cpu].grew = 1;
93 #ifdef DEBUG
94 			printk("low threshold crossed ");
95 #endif
96 		}
97 	}
98 	if((thrm = mfspr(SPRN_THRM2)) & THRM1_TIV){ /* is valid? */
99 		if(thrm & THRM1_TIN){ /* crossed high threshold */
100 			if (tau[cpu].high <= 127-step_size){
101 				tau[cpu].low += (step_size - window_expand);
102 				tau[cpu].high += step_size;
103 			}
104 			tau[cpu].grew = 1;
105 #ifdef DEBUG
106 			printk("high threshold crossed ");
107 #endif
108 		}
109 	}
110 
111 #ifdef DEBUG
112 	printk("grew = %d\n", tau[cpu].grew);
113 #endif
114 
115 #ifndef CONFIG_TAU_INT /* tau_timeout will do this if not using interrupts */
116 	set_thresholds(cpu);
117 #endif
118 
119 }
120 
121 #ifdef CONFIG_TAU_INT
122 /*
123  * TAU interrupts - called when we have a thermal assist unit interrupt
124  * with interrupts disabled
125  */
126 
127 void TAUException(struct pt_regs * regs)
128 {
129 	int cpu = smp_processor_id();
130 
131 	irq_enter();
132 	tau[cpu].interrupts++;
133 
134 	TAUupdate(cpu);
135 
136 	irq_exit();
137 }
138 #endif /* CONFIG_TAU_INT */
139 
140 static void tau_timeout(void * info)
141 {
142 	int cpu;
143 	unsigned long flags;
144 	int size;
145 	int shrink;
146 
147 	/* disabling interrupts *should* be okay */
148 	local_irq_save(flags);
149 	cpu = smp_processor_id();
150 
151 #ifndef CONFIG_TAU_INT
152 	TAUupdate(cpu);
153 #endif
154 
155 	size = tau[cpu].high - tau[cpu].low;
156 	if (size > min_window && ! tau[cpu].grew) {
157 		/* do an exponential shrink of half the amount currently over size */
158 		shrink = (2 + size - min_window) / 4;
159 		if (shrink) {
160 			tau[cpu].low += shrink;
161 			tau[cpu].high -= shrink;
162 		} else { /* size must have been min_window + 1 */
163 			tau[cpu].low += 1;
164 #if 1 /* debug */
165 			if ((tau[cpu].high - tau[cpu].low) != min_window){
166 				printk(KERN_ERR "temp.c: line %d, logic error\n", __LINE__);
167 			}
168 #endif
169 		}
170 	}
171 
172 	tau[cpu].grew = 0;
173 
174 	set_thresholds(cpu);
175 
176 	/*
177 	 * Do the enable every time, since otherwise a bunch of (relatively)
178 	 * complex sleep code needs to be added. One mtspr every time
179 	 * tau_timeout is called is probably not a big deal.
180 	 *
181 	 * Enable thermal sensor and set up sample interval timer
182 	 * need 20 us to do the compare.. until a nice 'cpu_speed' function
183 	 * call is implemented, just assume a 500 mhz clock. It doesn't really
184 	 * matter if we take too long for a compare since it's all interrupt
185 	 * driven anyway.
186 	 *
187 	 * use a extra long time.. (60 us @ 500 mhz)
188 	 */
189 	mtspr(SPRN_THRM3, THRM3_SITV(500*60) | THRM3_E);
190 
191 	local_irq_restore(flags);
192 }
193 
194 static void tau_timeout_smp(struct timer_list *unused)
195 {
196 
197 	/* schedule ourselves to be run again */
198 	mod_timer(&tau_timer, jiffies + shrink_timer) ;
199 	on_each_cpu(tau_timeout, NULL, 0);
200 }
201 
202 /*
203  * setup the TAU
204  *
205  * Set things up to use THRM1 as a temperature lower bound, and THRM2 as an upper bound.
206  * Start off at zero
207  */
208 
209 int tau_initialized = 0;
210 
211 static void __init TAU_init_smp(void *info)
212 {
213 	unsigned long cpu = smp_processor_id();
214 
215 	/* set these to a reasonable value and let the timer shrink the
216 	 * window */
217 	tau[cpu].low = 5;
218 	tau[cpu].high = 120;
219 
220 	set_thresholds(cpu);
221 }
222 
223 static int __init TAU_init(void)
224 {
225 	/* We assume in SMP that if one CPU has TAU support, they
226 	 * all have it --BenH
227 	 */
228 	if (!cpu_has_feature(CPU_FTR_TAU)) {
229 		printk("Thermal assist unit not available\n");
230 		tau_initialized = 0;
231 		return 1;
232 	}
233 
234 
235 	/* first, set up the window shrinking timer */
236 	timer_setup(&tau_timer, tau_timeout_smp, 0);
237 	tau_timer.expires = jiffies + shrink_timer;
238 	add_timer(&tau_timer);
239 
240 	on_each_cpu(TAU_init_smp, NULL, 0);
241 
242 	printk("Thermal assist unit ");
243 #ifdef CONFIG_TAU_INT
244 	printk("using interrupts, ");
245 #else
246 	printk("using timers, ");
247 #endif
248 	printk("shrink_timer: %d jiffies\n", shrink_timer);
249 	tau_initialized = 1;
250 
251 	return 0;
252 }
253 
254 __initcall(TAU_init);
255 
256 /*
257  * return current temp
258  */
259 
260 u32 cpu_temp_both(unsigned long cpu)
261 {
262 	return ((tau[cpu].high << 16) | tau[cpu].low);
263 }
264 
265 u32 cpu_temp(unsigned long cpu)
266 {
267 	return ((tau[cpu].high + tau[cpu].low) / 2);
268 }
269 
270 u32 tau_interrupts(unsigned long cpu)
271 {
272 	return (tau[cpu].interrupts);
273 }
274