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