• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * temp.c	Thermal management for cpu's with Thermal Assist Units
3  *
4  * Written by Troy Benjegerdes <hozer@drgw.net>
5  *
6  * TODO:
7  * dynamic power management to limit peak CPU temp (using ICTC)
8  * calibration???
9  *
10  * Silly, crazy ideas: use cpu load (from scheduler) and ICTC to extend battery
11  * life in portables, and add a 'performance/watt' metric somewhere in /proc
12  */
13 
14 #include <linux/errno.h>
15 #include <linux/jiffies.h>
16 #include <linux/kernel.h>
17 #include <linux/param.h>
18 #include <linux/string.h>
19 #include <linux/mm.h>
20 #include <linux/interrupt.h>
21 #include <linux/init.h>
22 
23 #include <asm/io.h>
24 #include <asm/reg.h>
25 #include <asm/nvram.h>
26 #include <asm/cache.h>
27 #include <asm/8xx_immap.h>
28 #include <asm/machdep.h>
29 
30 static struct tau_temp
31 {
32 	int interrupts;
33 	unsigned char low;
34 	unsigned char high;
35 	unsigned char grew;
36 } tau[NR_CPUS];
37 
38 struct timer_list tau_timer;
39 
40 /* TODO: put these in a /proc interface, with some sanity checks, and maybe
41  * dynamic adjustment to minimize # of interrupts */
42 /* configurable values for step size and how much to expand the window when
43  * we get an interrupt. These are based on the limit that was out of range */
44 #define step_size		2	/* step size when temp goes out of range */
45 #define window_expand		1	/* expand the window by this much */
46 /* configurable values for shrinking the window */
47 #define shrink_timer	2*HZ	/* period between shrinking the window */
48 #define min_window	2	/* minimum window size, degrees C */
49 
set_thresholds(unsigned long cpu)50 void set_thresholds(unsigned long cpu)
51 {
52 #ifdef CONFIG_TAU_INT
53 	/*
54 	 * setup THRM1,
55 	 * threshold, valid bit, enable interrupts, interrupt when below threshold
56 	 */
57 	mtspr(SPRN_THRM1, THRM1_THRES(tau[cpu].low) | THRM1_V | THRM1_TIE | THRM1_TID);
58 
59 	/* setup THRM2,
60 	 * threshold, valid bit, enable interrupts, interrupt when above threshold
61 	 */
62 	mtspr (SPRN_THRM2, THRM1_THRES(tau[cpu].high) | THRM1_V | THRM1_TIE);
63 #else
64 	/* same thing but don't enable interrupts */
65 	mtspr(SPRN_THRM1, THRM1_THRES(tau[cpu].low) | THRM1_V | THRM1_TID);
66 	mtspr(SPRN_THRM2, THRM1_THRES(tau[cpu].high) | THRM1_V);
67 #endif
68 }
69 
TAUupdate(int cpu)70 void TAUupdate(int cpu)
71 {
72 	u32 thrm;
73 	u32 bits = THRM1_TIV | THRM1_TIN | THRM1_V;
74 
75 	/* if both thresholds are crossed, the step_sizes cancel out
76 	 * and the window winds up getting expanded twice. */
77 	thrm = mfspr(SPRN_THRM1);
78 	if ((thrm & bits) == bits) {
79 		mtspr(SPRN_THRM1, 0);
80 
81 		if (tau[cpu].low >= step_size) {
82 			tau[cpu].low -= step_size;
83 			tau[cpu].high -= (step_size - window_expand);
84 		}
85 		tau[cpu].grew = 1;
86 		pr_debug("%s: low threshold crossed\n", __func__);
87 	}
88 	thrm = mfspr(SPRN_THRM2);
89 	if ((thrm & bits) == bits) {
90 		mtspr(SPRN_THRM2, 0);
91 
92 		if (tau[cpu].high <= 127 - step_size) {
93 			tau[cpu].low += (step_size - window_expand);
94 			tau[cpu].high += step_size;
95 		}
96 		tau[cpu].grew = 1;
97 		pr_debug("%s: high threshold crossed\n", __func__);
98 	}
99 }
100 
101 #ifdef CONFIG_TAU_INT
102 /*
103  * TAU interrupts - called when we have a thermal assist unit interrupt
104  * with interrupts disabled
105  */
106 
TAUException(struct pt_regs * regs)107 void TAUException(struct pt_regs * regs)
108 {
109 	int cpu = smp_processor_id();
110 
111 	irq_enter();
112 	tau[cpu].interrupts++;
113 
114 	TAUupdate(cpu);
115 
116 	irq_exit();
117 }
118 #endif /* CONFIG_TAU_INT */
119 
tau_timeout(void * info)120 static void tau_timeout(void * info)
121 {
122 	int cpu;
123 	int size;
124 	int shrink;
125 
126 	cpu = smp_processor_id();
127 
128 #ifndef CONFIG_TAU_INT
129 	TAUupdate(cpu);
130 #endif
131 
132 	/* Stop thermal sensor comparisons and interrupts */
133 	mtspr(SPRN_THRM3, 0);
134 
135 	size = tau[cpu].high - tau[cpu].low;
136 	if (size > min_window && ! tau[cpu].grew) {
137 		/* do an exponential shrink of half the amount currently over size */
138 		shrink = (2 + size - min_window) / 4;
139 		if (shrink) {
140 			tau[cpu].low += shrink;
141 			tau[cpu].high -= shrink;
142 		} else { /* size must have been min_window + 1 */
143 			tau[cpu].low += 1;
144 #if 1 /* debug */
145 			if ((tau[cpu].high - tau[cpu].low) != min_window){
146 				printk(KERN_ERR "temp.c: line %d, logic error\n", __LINE__);
147 			}
148 #endif
149 		}
150 	}
151 
152 	tau[cpu].grew = 0;
153 
154 	set_thresholds(cpu);
155 
156 	/* Restart thermal sensor comparisons and interrupts.
157 	 * The "PowerPC 740 and PowerPC 750 Microprocessor Datasheet"
158 	 * recommends that "the maximum value be set in THRM3 under all
159 	 * conditions."
160 	 */
161 	mtspr(SPRN_THRM3, THRM3_SITV(0x1fff) | THRM3_E);
162 }
163 
tau_timeout_smp(unsigned long unused)164 static void tau_timeout_smp(unsigned long unused)
165 {
166 
167 	/* schedule ourselves to be run again */
168 	mod_timer(&tau_timer, jiffies + shrink_timer) ;
169 	on_each_cpu(tau_timeout, NULL, 0);
170 }
171 
172 /*
173  * setup the TAU
174  *
175  * Set things up to use THRM1 as a temperature lower bound, and THRM2 as an upper bound.
176  * Start off at zero
177  */
178 
179 int tau_initialized = 0;
180 
TAU_init_smp(void * info)181 void __init TAU_init_smp(void * info)
182 {
183 	unsigned long cpu = smp_processor_id();
184 
185 	/* set these to a reasonable value and let the timer shrink the
186 	 * window */
187 	tau[cpu].low = 5;
188 	tau[cpu].high = 120;
189 
190 	set_thresholds(cpu);
191 }
192 
TAU_init(void)193 int __init TAU_init(void)
194 {
195 	/* We assume in SMP that if one CPU has TAU support, they
196 	 * all have it --BenH
197 	 */
198 	if (!cpu_has_feature(CPU_FTR_TAU)) {
199 		printk("Thermal assist unit not available\n");
200 		tau_initialized = 0;
201 		return 1;
202 	}
203 
204 
205 	/* first, set up the window shrinking timer */
206 	init_timer(&tau_timer);
207 	tau_timer.function = tau_timeout_smp;
208 	tau_timer.expires = jiffies + shrink_timer;
209 	add_timer(&tau_timer);
210 
211 	on_each_cpu(TAU_init_smp, NULL, 0);
212 
213 	printk("Thermal assist unit ");
214 #ifdef CONFIG_TAU_INT
215 	printk("using interrupts, ");
216 #else
217 	printk("using timers, ");
218 #endif
219 	printk("shrink_timer: %d jiffies\n", shrink_timer);
220 	tau_initialized = 1;
221 
222 	return 0;
223 }
224 
225 __initcall(TAU_init);
226 
227 /*
228  * return current temp
229  */
230 
cpu_temp_both(unsigned long cpu)231 u32 cpu_temp_both(unsigned long cpu)
232 {
233 	return ((tau[cpu].high << 16) | tau[cpu].low);
234 }
235 
cpu_temp(unsigned long cpu)236 int cpu_temp(unsigned long cpu)
237 {
238 	return ((tau[cpu].high + tau[cpu].low) / 2);
239 }
240 
tau_interrupts(unsigned long cpu)241 int tau_interrupts(unsigned long cpu)
242 {
243 	return (tau[cpu].interrupts);
244 }
245