GNU Linux-libre 4.14.332-gnu1
[releases.git] / arch / powerpc / kernel / tau_6xx.c
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
31 static struct tau_temp
32 {
33         int interrupts;
34         unsigned char low;
35         unsigned char high;
36         unsigned char grew;
37 } tau[NR_CPUS];
38
39 struct timer_list tau_timer;
40
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 */
50
51 void set_thresholds(unsigned long cpu)
52 {
53 #ifdef CONFIG_TAU_INT
54         /*
55          * setup THRM1,
56          * threshold, valid bit, enable interrupts, interrupt when below threshold
57          */
58         mtspr(SPRN_THRM1, THRM1_THRES(tau[cpu].low) | THRM1_V | THRM1_TIE | THRM1_TID);
59
60         /* setup THRM2,
61          * threshold, valid bit, enable interrupts, interrupt when above threshold
62          */
63         mtspr (SPRN_THRM2, THRM1_THRES(tau[cpu].high) | THRM1_V | THRM1_TIE);
64 #else
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);
68 #endif
69 }
70
71 void TAUupdate(int cpu)
72 {
73         u32 thrm;
74         u32 bits = THRM1_TIV | THRM1_TIN | THRM1_V;
75
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) {
80                 mtspr(SPRN_THRM1, 0);
81
82                 if (tau[cpu].low >= step_size) {
83                         tau[cpu].low -= step_size;
84                         tau[cpu].high -= (step_size - window_expand);
85                 }
86                 tau[cpu].grew = 1;
87                 pr_debug("%s: low threshold crossed\n", __func__);
88         }
89         thrm = mfspr(SPRN_THRM2);
90         if ((thrm & bits) == bits) {
91                 mtspr(SPRN_THRM2, 0);
92
93                 if (tau[cpu].high <= 127 - step_size) {
94                         tau[cpu].low += (step_size - window_expand);
95                         tau[cpu].high += step_size;
96                 }
97                 tau[cpu].grew = 1;
98                 pr_debug("%s: high threshold crossed\n", __func__);
99         }
100 }
101
102 #ifdef CONFIG_TAU_INT
103 /*
104  * TAU interrupts - called when we have a thermal assist unit interrupt
105  * with interrupts disabled
106  */
107
108 void TAUException(struct pt_regs * regs)
109 {
110         int cpu = smp_processor_id();
111
112         irq_enter();
113         tau[cpu].interrupts++;
114
115         TAUupdate(cpu);
116
117         irq_exit();
118 }
119 #endif /* CONFIG_TAU_INT */
120
121 static void tau_timeout(void * info)
122 {
123         int cpu;
124         int size;
125         int shrink;
126
127         cpu = smp_processor_id();
128
129 #ifndef CONFIG_TAU_INT
130         TAUupdate(cpu);
131 #endif
132
133         /* Stop thermal sensor comparisons and interrupts */
134         mtspr(SPRN_THRM3, 0);
135
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;
140                 if (shrink) {
141                         tau[cpu].low += shrink;
142                         tau[cpu].high -= shrink;
143                 } else { /* size must have been min_window + 1 */
144                         tau[cpu].low += 1;
145 #if 1 /* debug */
146                         if ((tau[cpu].high - tau[cpu].low) != min_window){
147                                 printk(KERN_ERR "temp.c: line %d, logic error\n", __LINE__);
148                         }
149 #endif
150                 }
151         }
152
153         tau[cpu].grew = 0;
154
155         set_thresholds(cpu);
156
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
160          * conditions."
161          */
162         mtspr(SPRN_THRM3, THRM3_SITV(0x1fff) | THRM3_E);
163 }
164
165 static void tau_timeout_smp(unsigned long unused)
166 {
167
168         /* schedule ourselves to be run again */
169         mod_timer(&tau_timer, jiffies + shrink_timer) ;
170         on_each_cpu(tau_timeout, NULL, 0);
171 }
172
173 /*
174  * setup the TAU
175  *
176  * Set things up to use THRM1 as a temperature lower bound, and THRM2 as an upper bound.
177  * Start off at zero
178  */
179
180 int tau_initialized = 0;
181
182 void __init TAU_init_smp(void * info)
183 {
184         unsigned long cpu = smp_processor_id();
185
186         /* set these to a reasonable value and let the timer shrink the
187          * window */
188         tau[cpu].low = 5;
189         tau[cpu].high = 120;
190
191         set_thresholds(cpu);
192 }
193
194 int __init TAU_init(void)
195 {
196         /* We assume in SMP that if one CPU has TAU support, they
197          * all have it --BenH
198          */
199         if (!cpu_has_feature(CPU_FTR_TAU)) {
200                 printk("Thermal assist unit not available\n");
201                 tau_initialized = 0;
202                 return 1;
203         }
204
205
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);
211
212         on_each_cpu(TAU_init_smp, NULL, 0);
213
214         printk("Thermal assist unit ");
215 #ifdef CONFIG_TAU_INT
216         printk("using interrupts, ");
217 #else
218         printk("using timers, ");
219 #endif
220         printk("shrink_timer: %d jiffies\n", shrink_timer);
221         tau_initialized = 1;
222
223         return 0;
224 }
225
226 __initcall(TAU_init);
227
228 /*
229  * return current temp
230  */
231
232 u32 cpu_temp_both(unsigned long cpu)
233 {
234         return ((tau[cpu].high << 16) | tau[cpu].low);
235 }
236
237 int cpu_temp(unsigned long cpu)
238 {
239         return ((tau[cpu].high + tau[cpu].low) / 2);
240 }
241
242 int tau_interrupts(unsigned long cpu)
243 {
244         return (tau[cpu].interrupts);
245 }