0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <linux/errno.h>
0016 #include <linux/kernel.h>
0017 #include <linux/param.h>
0018 #include <linux/string.h>
0019 #include <linux/mm.h>
0020 #include <linux/interrupt.h>
0021 #include <linux/init.h>
0022 #include <linux/delay.h>
0023 #include <linux/workqueue.h>
0024
0025 #include <asm/interrupt.h>
0026 #include <asm/io.h>
0027 #include <asm/reg.h>
0028 #include <asm/nvram.h>
0029 #include <asm/cache.h>
0030 #include <asm/8xx_immap.h>
0031 #include <asm/machdep.h>
0032
0033 #include "setup.h"
0034
0035 static struct tau_temp
0036 {
0037 int interrupts;
0038 unsigned char low;
0039 unsigned char high;
0040 unsigned char grew;
0041 } tau[NR_CPUS];
0042
0043 static bool tau_int_enable;
0044
0045
0046
0047
0048
0049 #define step_size 2
0050 #define window_expand 1
0051
0052 #define shrink_timer 2000
0053 #define min_window 2
0054
0055 static void set_thresholds(unsigned long cpu)
0056 {
0057 u32 maybe_tie = tau_int_enable ? THRM1_TIE : 0;
0058
0059
0060 mtspr(SPRN_THRM1, THRM1_THRES(tau[cpu].low) | THRM1_V | maybe_tie | THRM1_TID);
0061
0062
0063 mtspr(SPRN_THRM2, THRM1_THRES(tau[cpu].high) | THRM1_V | maybe_tie);
0064 }
0065
0066 static void TAUupdate(int cpu)
0067 {
0068 u32 thrm;
0069 u32 bits = THRM1_TIV | THRM1_TIN | THRM1_V;
0070
0071
0072
0073 thrm = mfspr(SPRN_THRM1);
0074 if ((thrm & bits) == bits) {
0075 mtspr(SPRN_THRM1, 0);
0076
0077 if (tau[cpu].low >= step_size) {
0078 tau[cpu].low -= step_size;
0079 tau[cpu].high -= (step_size - window_expand);
0080 }
0081 tau[cpu].grew = 1;
0082 pr_debug("%s: low threshold crossed\n", __func__);
0083 }
0084 thrm = mfspr(SPRN_THRM2);
0085 if ((thrm & bits) == bits) {
0086 mtspr(SPRN_THRM2, 0);
0087
0088 if (tau[cpu].high <= 127 - step_size) {
0089 tau[cpu].low += (step_size - window_expand);
0090 tau[cpu].high += step_size;
0091 }
0092 tau[cpu].grew = 1;
0093 pr_debug("%s: high threshold crossed\n", __func__);
0094 }
0095 }
0096
0097 #ifdef CONFIG_TAU_INT
0098
0099
0100
0101
0102
0103 DEFINE_INTERRUPT_HANDLER_ASYNC(TAUException)
0104 {
0105 int cpu = smp_processor_id();
0106
0107 tau[cpu].interrupts++;
0108
0109 TAUupdate(cpu);
0110 }
0111 #endif
0112
0113 static void tau_timeout(void * info)
0114 {
0115 int cpu;
0116 int size;
0117 int shrink;
0118
0119 cpu = smp_processor_id();
0120
0121 if (!tau_int_enable)
0122 TAUupdate(cpu);
0123
0124
0125 mtspr(SPRN_THRM3, 0);
0126
0127 size = tau[cpu].high - tau[cpu].low;
0128 if (size > min_window && ! tau[cpu].grew) {
0129
0130 shrink = (2 + size - min_window) / 4;
0131 if (shrink) {
0132 tau[cpu].low += shrink;
0133 tau[cpu].high -= shrink;
0134 } else {
0135 tau[cpu].low += 1;
0136 #if 1
0137 if ((tau[cpu].high - tau[cpu].low) != min_window){
0138 printk(KERN_ERR "temp.c: line %d, logic error\n", __LINE__);
0139 }
0140 #endif
0141 }
0142 }
0143
0144 tau[cpu].grew = 0;
0145
0146 set_thresholds(cpu);
0147
0148
0149
0150
0151
0152
0153 mtspr(SPRN_THRM3, THRM3_SITV(0x1fff) | THRM3_E);
0154 }
0155
0156 static struct workqueue_struct *tau_workq;
0157
0158 static void tau_work_func(struct work_struct *work)
0159 {
0160 msleep(shrink_timer);
0161 on_each_cpu(tau_timeout, NULL, 0);
0162
0163 queue_work(tau_workq, work);
0164 }
0165
0166 static DECLARE_WORK(tau_work, tau_work_func);
0167
0168
0169
0170
0171
0172
0173
0174
0175 int tau_initialized = 0;
0176
0177 static void __init TAU_init_smp(void *info)
0178 {
0179 unsigned long cpu = smp_processor_id();
0180
0181
0182
0183 tau[cpu].low = 5;
0184 tau[cpu].high = 120;
0185
0186 set_thresholds(cpu);
0187 }
0188
0189 static int __init TAU_init(void)
0190 {
0191
0192
0193
0194 if (!cpu_has_feature(CPU_FTR_TAU)) {
0195 printk("Thermal assist unit not available\n");
0196 tau_initialized = 0;
0197 return 1;
0198 }
0199
0200 tau_int_enable = IS_ENABLED(CONFIG_TAU_INT) &&
0201 !strcmp(cur_cpu_spec->platform, "ppc750");
0202
0203 tau_workq = alloc_workqueue("tau", WQ_UNBOUND, 1);
0204 if (!tau_workq)
0205 return -ENOMEM;
0206
0207 on_each_cpu(TAU_init_smp, NULL, 0);
0208
0209 queue_work(tau_workq, &tau_work);
0210
0211 pr_info("Thermal assist unit using %s, shrink_timer: %d ms\n",
0212 tau_int_enable ? "interrupts" : "workqueue", shrink_timer);
0213 tau_initialized = 1;
0214
0215 return 0;
0216 }
0217
0218 __initcall(TAU_init);
0219
0220
0221
0222
0223
0224 u32 cpu_temp_both(unsigned long cpu)
0225 {
0226 return ((tau[cpu].high << 16) | tau[cpu].low);
0227 }
0228
0229 u32 cpu_temp(unsigned long cpu)
0230 {
0231 return ((tau[cpu].high + tau[cpu].low) / 2);
0232 }
0233
0234 u32 tau_interrupts(unsigned long cpu)
0235 {
0236 return (tau[cpu].interrupts);
0237 }