Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * temp.c   Thermal management for cpu's with Thermal Assist Units
0004  *
0005  * Written by Troy Benjegerdes <hozer@drgw.net>
0006  *
0007  * TODO:
0008  * dynamic power management to limit peak CPU temp (using ICTC)
0009  * calibration???
0010  *
0011  * Silly, crazy ideas: use cpu load (from scheduler) and ICTC to extend battery
0012  * life in portables, and add a 'performance/watt' metric somewhere in /proc
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 /* TODO: put these in a /proc interface, with some sanity checks, and maybe
0046  * dynamic adjustment to minimize # of interrupts */
0047 /* configurable values for step size and how much to expand the window when
0048  * we get an interrupt. These are based on the limit that was out of range */
0049 #define step_size       2   /* step size when temp goes out of range */
0050 #define window_expand       1   /* expand the window by this much */
0051 /* configurable values for shrinking the window */
0052 #define shrink_timer    2000    /* period between shrinking the window */
0053 #define min_window  2   /* minimum window size, degrees C */
0054 
0055 static void set_thresholds(unsigned long cpu)
0056 {
0057     u32 maybe_tie = tau_int_enable ? THRM1_TIE : 0;
0058 
0059     /* setup THRM1, threshold, valid bit, interrupt when below threshold */
0060     mtspr(SPRN_THRM1, THRM1_THRES(tau[cpu].low) | THRM1_V | maybe_tie | THRM1_TID);
0061 
0062     /* setup THRM2, threshold, valid bit, interrupt when above threshold */
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     /* if both thresholds are crossed, the step_sizes cancel out
0072      * and the window winds up getting expanded twice. */
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  * TAU interrupts - called when we have a thermal assist unit interrupt
0100  * with interrupts disabled
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 /* CONFIG_TAU_INT */
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     /* Stop thermal sensor comparisons and interrupts */
0125     mtspr(SPRN_THRM3, 0);
0126 
0127     size = tau[cpu].high - tau[cpu].low;
0128     if (size > min_window && ! tau[cpu].grew) {
0129         /* do an exponential shrink of half the amount currently over size */
0130         shrink = (2 + size - min_window) / 4;
0131         if (shrink) {
0132             tau[cpu].low += shrink;
0133             tau[cpu].high -= shrink;
0134         } else { /* size must have been min_window + 1 */
0135             tau[cpu].low += 1;
0136 #if 1 /* debug */
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     /* Restart thermal sensor comparisons and interrupts.
0149      * The "PowerPC 740 and PowerPC 750 Microprocessor Datasheet"
0150      * recommends that "the maximum value be set in THRM3 under all
0151      * conditions."
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     /* schedule ourselves to be run again */
0163     queue_work(tau_workq, work);
0164 }
0165 
0166 static DECLARE_WORK(tau_work, tau_work_func);
0167 
0168 /*
0169  * setup the TAU
0170  *
0171  * Set things up to use THRM1 as a temperature lower bound, and THRM2 as an upper bound.
0172  * Start off at zero
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     /* set these to a reasonable value and let the timer shrink the
0182      * window */
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     /* We assume in SMP that if one CPU has TAU support, they
0192      * all have it --BenH
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  * return current temp
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 }