Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Windfarm PowerMac thermal control.
0004  * Control loops for PowerMac7,2 and 7,3
0005  *
0006  * Copyright (C) 2012 Benjamin Herrenschmidt, IBM Corp.
0007  */
0008 #include <linux/types.h>
0009 #include <linux/errno.h>
0010 #include <linux/kernel.h>
0011 #include <linux/device.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/reboot.h>
0014 
0015 #include <asm/smu.h>
0016 
0017 #include "windfarm.h"
0018 #include "windfarm_pid.h"
0019 #include "windfarm_mpu.h"
0020 
0021 #define VERSION "1.0"
0022 
0023 #undef DEBUG
0024 #undef LOTSA_DEBUG
0025 
0026 #ifdef DEBUG
0027 #define DBG(args...)    printk(args)
0028 #else
0029 #define DBG(args...)    do { } while(0)
0030 #endif
0031 
0032 #ifdef LOTSA_DEBUG
0033 #define DBG_LOTS(args...)   printk(args)
0034 #else
0035 #define DBG_LOTS(args...)   do { } while(0)
0036 #endif
0037 
0038 /* define this to force CPU overtemp to 60 degree, useful for testing
0039  * the overtemp code
0040  */
0041 #undef HACKED_OVERTEMP
0042 
0043 /* We currently only handle 2 chips */
0044 #define NR_CHIPS    2
0045 #define NR_CPU_FANS 3 * NR_CHIPS
0046 
0047 /* Controls and sensors */
0048 static struct wf_sensor *sens_cpu_temp[NR_CHIPS];
0049 static struct wf_sensor *sens_cpu_volts[NR_CHIPS];
0050 static struct wf_sensor *sens_cpu_amps[NR_CHIPS];
0051 static struct wf_sensor *backside_temp;
0052 static struct wf_sensor *drives_temp;
0053 
0054 static struct wf_control *cpu_front_fans[NR_CHIPS];
0055 static struct wf_control *cpu_rear_fans[NR_CHIPS];
0056 static struct wf_control *cpu_pumps[NR_CHIPS];
0057 static struct wf_control *backside_fan;
0058 static struct wf_control *drives_fan;
0059 static struct wf_control *slots_fan;
0060 static struct wf_control *cpufreq_clamp;
0061 
0062 /* We keep a temperature history for average calculation of 180s */
0063 #define CPU_TEMP_HIST_SIZE  180
0064 
0065 /* Fixed speed for slot fan */
0066 #define SLOTS_FAN_DEFAULT_PWM   40
0067 
0068 /* Scale value for CPU intake fans */
0069 #define CPU_INTAKE_SCALE    0x0000f852
0070 
0071 /* PID loop state */
0072 static const struct mpu_data *cpu_mpu_data[NR_CHIPS];
0073 static struct wf_cpu_pid_state cpu_pid[NR_CHIPS];
0074 static bool cpu_pid_combined;
0075 static u32 cpu_thist[CPU_TEMP_HIST_SIZE];
0076 static int cpu_thist_pt;
0077 static s64 cpu_thist_total;
0078 static s32 cpu_all_tmax = 100 << 16;
0079 static struct wf_pid_state backside_pid;
0080 static int backside_tick;
0081 static struct wf_pid_state drives_pid;
0082 static int drives_tick;
0083 
0084 static int nr_chips;
0085 static bool have_all_controls;
0086 static bool have_all_sensors;
0087 static bool started;
0088 
0089 static int failure_state;
0090 #define FAILURE_SENSOR      1
0091 #define FAILURE_FAN     2
0092 #define FAILURE_PERM        4
0093 #define FAILURE_LOW_OVERTEMP    8
0094 #define FAILURE_HIGH_OVERTEMP   16
0095 
0096 /* Overtemp values */
0097 #define LOW_OVER_AVERAGE    0
0098 #define LOW_OVER_IMMEDIATE  (10 << 16)
0099 #define LOW_OVER_CLEAR      ((-10) << 16)
0100 #define HIGH_OVER_IMMEDIATE (14 << 16)
0101 #define HIGH_OVER_AVERAGE   (10 << 16)
0102 #define HIGH_OVER_IMMEDIATE (14 << 16)
0103 
0104 
0105 static void cpu_max_all_fans(void)
0106 {
0107     int i;
0108 
0109     /* We max all CPU fans in case of a sensor error. We also do the
0110      * cpufreq clamping now, even if it's supposedly done later by the
0111      * generic code anyway, we do it earlier here to react faster
0112      */
0113     if (cpufreq_clamp)
0114         wf_control_set_max(cpufreq_clamp);
0115     for (i = 0; i < nr_chips; i++) {
0116         if (cpu_front_fans[i])
0117             wf_control_set_max(cpu_front_fans[i]);
0118         if (cpu_rear_fans[i])
0119             wf_control_set_max(cpu_rear_fans[i]);
0120         if (cpu_pumps[i])
0121             wf_control_set_max(cpu_pumps[i]);
0122     }
0123 }
0124 
0125 static int cpu_check_overtemp(s32 temp)
0126 {
0127     int new_state = 0;
0128     s32 t_avg, t_old;
0129     static bool first = true;
0130 
0131     /* First check for immediate overtemps */
0132     if (temp >= (cpu_all_tmax + LOW_OVER_IMMEDIATE)) {
0133         new_state |= FAILURE_LOW_OVERTEMP;
0134         if ((failure_state & FAILURE_LOW_OVERTEMP) == 0)
0135             printk(KERN_ERR "windfarm: Overtemp due to immediate CPU"
0136                    " temperature !\n");
0137     }
0138     if (temp >= (cpu_all_tmax + HIGH_OVER_IMMEDIATE)) {
0139         new_state |= FAILURE_HIGH_OVERTEMP;
0140         if ((failure_state & FAILURE_HIGH_OVERTEMP) == 0)
0141             printk(KERN_ERR "windfarm: Critical overtemp due to"
0142                    " immediate CPU temperature !\n");
0143     }
0144 
0145     /*
0146      * The first time around, initialize the array with the first
0147      * temperature reading
0148      */
0149     if (first) {
0150         int i;
0151 
0152         cpu_thist_total = 0;
0153         for (i = 0; i < CPU_TEMP_HIST_SIZE; i++) {
0154             cpu_thist[i] = temp;
0155             cpu_thist_total += temp;
0156         }
0157         first = false;
0158     }
0159 
0160     /*
0161      * We calculate a history of max temperatures and use that for the
0162      * overtemp management
0163      */
0164     t_old = cpu_thist[cpu_thist_pt];
0165     cpu_thist[cpu_thist_pt] = temp;
0166     cpu_thist_pt = (cpu_thist_pt + 1) % CPU_TEMP_HIST_SIZE;
0167     cpu_thist_total -= t_old;
0168     cpu_thist_total += temp;
0169     t_avg = cpu_thist_total / CPU_TEMP_HIST_SIZE;
0170 
0171     DBG_LOTS("  t_avg = %d.%03d (out: %d.%03d, in: %d.%03d)\n",
0172          FIX32TOPRINT(t_avg), FIX32TOPRINT(t_old), FIX32TOPRINT(temp));
0173 
0174     /* Now check for average overtemps */
0175     if (t_avg >= (cpu_all_tmax + LOW_OVER_AVERAGE)) {
0176         new_state |= FAILURE_LOW_OVERTEMP;
0177         if ((failure_state & FAILURE_LOW_OVERTEMP) == 0)
0178             printk(KERN_ERR "windfarm: Overtemp due to average CPU"
0179                    " temperature !\n");
0180     }
0181     if (t_avg >= (cpu_all_tmax + HIGH_OVER_AVERAGE)) {
0182         new_state |= FAILURE_HIGH_OVERTEMP;
0183         if ((failure_state & FAILURE_HIGH_OVERTEMP) == 0)
0184             printk(KERN_ERR "windfarm: Critical overtemp due to"
0185                    " average CPU temperature !\n");
0186     }
0187 
0188     /* Now handle overtemp conditions. We don't currently use the windfarm
0189      * overtemp handling core as it's not fully suited to the needs of those
0190      * new machine. This will be fixed later.
0191      */
0192     if (new_state) {
0193         /* High overtemp -> immediate shutdown */
0194         if (new_state & FAILURE_HIGH_OVERTEMP)
0195             machine_power_off();
0196         if ((failure_state & new_state) != new_state)
0197             cpu_max_all_fans();
0198         failure_state |= new_state;
0199     } else if ((failure_state & FAILURE_LOW_OVERTEMP) &&
0200            (temp < (cpu_all_tmax + LOW_OVER_CLEAR))) {
0201         printk(KERN_ERR "windfarm: Overtemp condition cleared !\n");
0202         failure_state &= ~FAILURE_LOW_OVERTEMP;
0203     }
0204 
0205     return failure_state & (FAILURE_LOW_OVERTEMP | FAILURE_HIGH_OVERTEMP);
0206 }
0207 
0208 static int read_one_cpu_vals(int cpu, s32 *temp, s32 *power)
0209 {
0210     s32 dtemp, volts, amps;
0211     int rc;
0212 
0213     /* Get diode temperature */
0214     rc = wf_sensor_get(sens_cpu_temp[cpu], &dtemp);
0215     if (rc) {
0216         DBG("  CPU%d: temp reading error !\n", cpu);
0217         return -EIO;
0218     }
0219     DBG_LOTS("  CPU%d: temp   = %d.%03d\n", cpu, FIX32TOPRINT((dtemp)));
0220     *temp = dtemp;
0221 
0222     /* Get voltage */
0223     rc = wf_sensor_get(sens_cpu_volts[cpu], &volts);
0224     if (rc) {
0225         DBG("  CPU%d, volts reading error !\n", cpu);
0226         return -EIO;
0227     }
0228     DBG_LOTS("  CPU%d: volts  = %d.%03d\n", cpu, FIX32TOPRINT((volts)));
0229 
0230     /* Get current */
0231     rc = wf_sensor_get(sens_cpu_amps[cpu], &amps);
0232     if (rc) {
0233         DBG("  CPU%d, current reading error !\n", cpu);
0234         return -EIO;
0235     }
0236     DBG_LOTS("  CPU%d: amps   = %d.%03d\n", cpu, FIX32TOPRINT((amps)));
0237 
0238     /* Calculate power */
0239 
0240     /* Scale voltage and current raw sensor values according to fixed scales
0241      * obtained in Darwin and calculate power from I and V
0242      */
0243     *power = (((u64)volts) * ((u64)amps)) >> 16;
0244 
0245     DBG_LOTS("  CPU%d: power  = %d.%03d\n", cpu, FIX32TOPRINT((*power)));
0246 
0247     return 0;
0248 
0249 }
0250 
0251 static void cpu_fans_tick_split(void)
0252 {
0253     int err, cpu;
0254     s32 intake, temp, power, t_max = 0;
0255 
0256     DBG_LOTS("* cpu fans_tick_split()\n");
0257 
0258     for (cpu = 0; cpu < nr_chips; ++cpu) {
0259         struct wf_cpu_pid_state *sp = &cpu_pid[cpu];
0260 
0261         /* Read current speed */
0262         wf_control_get(cpu_rear_fans[cpu], &sp->target);
0263 
0264         DBG_LOTS("  CPU%d: cur_target = %d RPM\n", cpu, sp->target);
0265 
0266         err = read_one_cpu_vals(cpu, &temp, &power);
0267         if (err) {
0268             failure_state |= FAILURE_SENSOR;
0269             cpu_max_all_fans();
0270             return;
0271         }
0272 
0273         /* Keep track of highest temp */
0274         t_max = max(t_max, temp);
0275 
0276         /* Handle possible overtemps */
0277         if (cpu_check_overtemp(t_max))
0278             return;
0279 
0280         /* Run PID */
0281         wf_cpu_pid_run(sp, power, temp);
0282 
0283         DBG_LOTS("  CPU%d: target = %d RPM\n", cpu, sp->target);
0284 
0285         /* Apply result directly to exhaust fan */
0286         err = wf_control_set(cpu_rear_fans[cpu], sp->target);
0287         if (err) {
0288             pr_warn("wf_pm72: Fan %s reports error %d\n",
0289                 cpu_rear_fans[cpu]->name, err);
0290             failure_state |= FAILURE_FAN;
0291             break;
0292         }
0293 
0294         /* Scale result for intake fan */
0295         intake = (sp->target * CPU_INTAKE_SCALE) >> 16;
0296         DBG_LOTS("  CPU%d: intake = %d RPM\n", cpu, intake);
0297         err = wf_control_set(cpu_front_fans[cpu], intake);
0298         if (err) {
0299             pr_warn("wf_pm72: Fan %s reports error %d\n",
0300                 cpu_front_fans[cpu]->name, err);
0301             failure_state |= FAILURE_FAN;
0302             break;
0303         }
0304     }
0305 }
0306 
0307 static void cpu_fans_tick_combined(void)
0308 {
0309     s32 temp0, power0, temp1, power1, t_max = 0;
0310     s32 temp, power, intake, pump;
0311     struct wf_control *pump0, *pump1;
0312     struct wf_cpu_pid_state *sp = &cpu_pid[0];
0313     int err, cpu;
0314 
0315     DBG_LOTS("* cpu fans_tick_combined()\n");
0316 
0317     /* Read current speed from cpu 0 */
0318     wf_control_get(cpu_rear_fans[0], &sp->target);
0319 
0320     DBG_LOTS("  CPUs: cur_target = %d RPM\n", sp->target);
0321 
0322     /* Read values for both CPUs */
0323     err = read_one_cpu_vals(0, &temp0, &power0);
0324     if (err) {
0325         failure_state |= FAILURE_SENSOR;
0326         cpu_max_all_fans();
0327         return;
0328     }
0329     err = read_one_cpu_vals(1, &temp1, &power1);
0330     if (err) {
0331         failure_state |= FAILURE_SENSOR;
0332         cpu_max_all_fans();
0333         return;
0334     }
0335 
0336     /* Keep track of highest temp */
0337     t_max = max(t_max, max(temp0, temp1));
0338 
0339     /* Handle possible overtemps */
0340     if (cpu_check_overtemp(t_max))
0341         return;
0342 
0343     /* Use the max temp & power of both */
0344     temp = max(temp0, temp1);
0345     power = max(power0, power1);
0346 
0347     /* Run PID */
0348     wf_cpu_pid_run(sp, power, temp);
0349 
0350     /* Scale result for intake fan */
0351     intake = (sp->target * CPU_INTAKE_SCALE) >> 16;
0352 
0353     /* Same deal with pump speed */
0354     pump0 = cpu_pumps[0];
0355     pump1 = cpu_pumps[1];
0356     if (!pump0) {
0357         pump0 = pump1;
0358         pump1 = NULL;
0359     }
0360     pump = (sp->target * wf_control_get_max(pump0)) /
0361         cpu_mpu_data[0]->rmaxn_exhaust_fan;
0362 
0363     DBG_LOTS("  CPUs: target = %d RPM\n", sp->target);
0364     DBG_LOTS("  CPUs: intake = %d RPM\n", intake);
0365     DBG_LOTS("  CPUs: pump   = %d RPM\n", pump);
0366 
0367     for (cpu = 0; cpu < nr_chips; cpu++) {
0368         err = wf_control_set(cpu_rear_fans[cpu], sp->target);
0369         if (err) {
0370             pr_warn("wf_pm72: Fan %s reports error %d\n",
0371                 cpu_rear_fans[cpu]->name, err);
0372             failure_state |= FAILURE_FAN;
0373         }
0374         err = wf_control_set(cpu_front_fans[cpu], intake);
0375         if (err) {
0376             pr_warn("wf_pm72: Fan %s reports error %d\n",
0377                 cpu_front_fans[cpu]->name, err);
0378             failure_state |= FAILURE_FAN;
0379         }
0380         err = 0;
0381         if (cpu_pumps[cpu])
0382             err = wf_control_set(cpu_pumps[cpu], pump);
0383         if (err) {
0384             pr_warn("wf_pm72: Pump %s reports error %d\n",
0385                 cpu_pumps[cpu]->name, err);
0386             failure_state |= FAILURE_FAN;
0387         }
0388     }
0389 }
0390 
0391 /* Implementation... */
0392 static int cpu_setup_pid(int cpu)
0393 {
0394     struct wf_cpu_pid_param pid;
0395     const struct mpu_data *mpu = cpu_mpu_data[cpu];
0396     s32 tmax, ttarget, ptarget;
0397     int fmin, fmax, hsize;
0398 
0399     /* Get PID params from the appropriate MPU EEPROM */
0400     tmax = mpu->tmax << 16;
0401     ttarget = mpu->ttarget << 16;
0402     ptarget = ((s32)(mpu->pmaxh - mpu->padjmax)) << 16;
0403 
0404     DBG("wf_72: CPU%d ttarget = %d.%03d, tmax = %d.%03d\n",
0405         cpu, FIX32TOPRINT(ttarget), FIX32TOPRINT(tmax));
0406 
0407     /* We keep a global tmax for overtemp calculations */
0408     if (tmax < cpu_all_tmax)
0409         cpu_all_tmax = tmax;
0410 
0411     /* Set PID min/max by using the rear fan min/max */
0412     fmin = wf_control_get_min(cpu_rear_fans[cpu]);
0413     fmax = wf_control_get_max(cpu_rear_fans[cpu]);
0414     DBG("wf_72: CPU%d max RPM range = [%d..%d]\n", cpu, fmin, fmax);
0415 
0416     /* History size */
0417     hsize = min_t(int, mpu->tguardband, WF_PID_MAX_HISTORY);
0418     DBG("wf_72: CPU%d history size = %d\n", cpu, hsize);
0419 
0420     /* Initialize PID loop */
0421     pid.interval    = 1;    /* seconds */
0422     pid.history_len = hsize;
0423     pid.gd      = mpu->pid_gd;
0424     pid.gp      = mpu->pid_gp;
0425     pid.gr      = mpu->pid_gr;
0426     pid.tmax    = tmax;
0427     pid.ttarget = ttarget;
0428     pid.pmaxadj = ptarget;
0429     pid.min     = fmin;
0430     pid.max     = fmax;
0431 
0432     wf_cpu_pid_init(&cpu_pid[cpu], &pid);
0433     cpu_pid[cpu].target = 1000;
0434 
0435     return 0;
0436 }
0437 
0438 /* Backside/U3 fan */
0439 static struct wf_pid_param backside_u3_param = {
0440     .interval   = 5,
0441     .history_len    = 2,
0442     .gd     = 40 << 20,
0443     .gp     = 5 << 20,
0444     .gr     = 0,
0445     .itarget    = 65 << 16,
0446     .additive   = 1,
0447     .min        = 20,
0448     .max        = 100,
0449 };
0450 
0451 static struct wf_pid_param backside_u3h_param = {
0452     .interval   = 5,
0453     .history_len    = 2,
0454     .gd     = 20 << 20,
0455     .gp     = 5 << 20,
0456     .gr     = 0,
0457     .itarget    = 75 << 16,
0458     .additive   = 1,
0459     .min        = 20,
0460     .max        = 100,
0461 };
0462 
0463 static void backside_fan_tick(void)
0464 {
0465     s32 temp;
0466     int speed;
0467     int err;
0468 
0469     if (!backside_fan || !backside_temp || !backside_tick)
0470         return;
0471     if (--backside_tick > 0)
0472         return;
0473     backside_tick = backside_pid.param.interval;
0474 
0475     DBG_LOTS("* backside fans tick\n");
0476 
0477     /* Update fan speed from actual fans */
0478     err = wf_control_get(backside_fan, &speed);
0479     if (!err)
0480         backside_pid.target = speed;
0481 
0482     err = wf_sensor_get(backside_temp, &temp);
0483     if (err) {
0484         printk(KERN_WARNING "windfarm: U4 temp sensor error %d\n",
0485                err);
0486         failure_state |= FAILURE_SENSOR;
0487         wf_control_set_max(backside_fan);
0488         return;
0489     }
0490     speed = wf_pid_run(&backside_pid, temp);
0491 
0492     DBG_LOTS("backside PID temp=%d.%.3d speed=%d\n",
0493          FIX32TOPRINT(temp), speed);
0494 
0495     err = wf_control_set(backside_fan, speed);
0496     if (err) {
0497         printk(KERN_WARNING "windfarm: backside fan error %d\n", err);
0498         failure_state |= FAILURE_FAN;
0499     }
0500 }
0501 
0502 static void backside_setup_pid(void)
0503 {
0504     /* first time initialize things */
0505     s32 fmin = wf_control_get_min(backside_fan);
0506     s32 fmax = wf_control_get_max(backside_fan);
0507     struct wf_pid_param param;
0508     struct device_node *u3;
0509     int u3h = 1; /* conservative by default */
0510 
0511     u3 = of_find_node_by_path("/u3@0,f8000000");
0512     if (u3 != NULL) {
0513         const u32 *vers = of_get_property(u3, "device-rev", NULL);
0514         if (vers)
0515             if (((*vers) & 0x3f) < 0x34)
0516                 u3h = 0;
0517         of_node_put(u3);
0518     }
0519 
0520     param = u3h ? backside_u3h_param : backside_u3_param;
0521 
0522     param.min = max(param.min, fmin);
0523     param.max = min(param.max, fmax);
0524     wf_pid_init(&backside_pid, &param);
0525     backside_tick = 1;
0526 
0527     pr_info("wf_pm72: Backside control loop started.\n");
0528 }
0529 
0530 /* Drive bay fan */
0531 static const struct wf_pid_param drives_param = {
0532     .interval   = 5,
0533     .history_len    = 2,
0534     .gd     = 30 << 20,
0535     .gp     = 5 << 20,
0536     .gr     = 0,
0537     .itarget    = 40 << 16,
0538     .additive   = 1,
0539     .min        = 300,
0540     .max        = 4000,
0541 };
0542 
0543 static void drives_fan_tick(void)
0544 {
0545     s32 temp;
0546     int speed;
0547     int err;
0548 
0549     if (!drives_fan || !drives_temp || !drives_tick)
0550         return;
0551     if (--drives_tick > 0)
0552         return;
0553     drives_tick = drives_pid.param.interval;
0554 
0555     DBG_LOTS("* drives fans tick\n");
0556 
0557     /* Update fan speed from actual fans */
0558     err = wf_control_get(drives_fan, &speed);
0559     if (!err)
0560         drives_pid.target = speed;
0561 
0562     err = wf_sensor_get(drives_temp, &temp);
0563     if (err) {
0564         pr_warn("wf_pm72: drive bay temp sensor error %d\n", err);
0565         failure_state |= FAILURE_SENSOR;
0566         wf_control_set_max(drives_fan);
0567         return;
0568     }
0569     speed = wf_pid_run(&drives_pid, temp);
0570 
0571     DBG_LOTS("drives PID temp=%d.%.3d speed=%d\n",
0572          FIX32TOPRINT(temp), speed);
0573 
0574     err = wf_control_set(drives_fan, speed);
0575     if (err) {
0576         printk(KERN_WARNING "windfarm: drive bay fan error %d\n", err);
0577         failure_state |= FAILURE_FAN;
0578     }
0579 }
0580 
0581 static void drives_setup_pid(void)
0582 {
0583     /* first time initialize things */
0584     s32 fmin = wf_control_get_min(drives_fan);
0585     s32 fmax = wf_control_get_max(drives_fan);
0586     struct wf_pid_param param = drives_param;
0587 
0588     param.min = max(param.min, fmin);
0589     param.max = min(param.max, fmax);
0590     wf_pid_init(&drives_pid, &param);
0591     drives_tick = 1;
0592 
0593     pr_info("wf_pm72: Drive bay control loop started.\n");
0594 }
0595 
0596 static void set_fail_state(void)
0597 {
0598     cpu_max_all_fans();
0599 
0600     if (backside_fan)
0601         wf_control_set_max(backside_fan);
0602     if (slots_fan)
0603         wf_control_set_max(slots_fan);
0604     if (drives_fan)
0605         wf_control_set_max(drives_fan);
0606 }
0607 
0608 static void pm72_tick(void)
0609 {
0610     int i, last_failure;
0611 
0612     if (!started) {
0613         started = true;
0614         printk(KERN_INFO "windfarm: CPUs control loops started.\n");
0615         for (i = 0; i < nr_chips; ++i) {
0616             if (cpu_setup_pid(i) < 0) {
0617                 failure_state = FAILURE_PERM;
0618                 set_fail_state();
0619                 break;
0620             }
0621         }
0622         DBG_LOTS("cpu_all_tmax=%d.%03d\n", FIX32TOPRINT(cpu_all_tmax));
0623 
0624         backside_setup_pid();
0625         drives_setup_pid();
0626 
0627         /*
0628          * We don't have the right stuff to drive the PCI fan
0629          * so we fix it to a default value
0630          */
0631         wf_control_set(slots_fan, SLOTS_FAN_DEFAULT_PWM);
0632 
0633 #ifdef HACKED_OVERTEMP
0634         cpu_all_tmax = 60 << 16;
0635 #endif
0636     }
0637 
0638     /* Permanent failure, bail out */
0639     if (failure_state & FAILURE_PERM)
0640         return;
0641 
0642     /*
0643      * Clear all failure bits except low overtemp which will be eventually
0644      * cleared by the control loop itself
0645      */
0646     last_failure = failure_state;
0647     failure_state &= FAILURE_LOW_OVERTEMP;
0648     if (cpu_pid_combined)
0649         cpu_fans_tick_combined();
0650     else
0651         cpu_fans_tick_split();
0652     backside_fan_tick();
0653     drives_fan_tick();
0654 
0655     DBG_LOTS("  last_failure: 0x%x, failure_state: %x\n",
0656          last_failure, failure_state);
0657 
0658     /* Check for failures. Any failure causes cpufreq clamping */
0659     if (failure_state && last_failure == 0 && cpufreq_clamp)
0660         wf_control_set_max(cpufreq_clamp);
0661     if (failure_state == 0 && last_failure && cpufreq_clamp)
0662         wf_control_set_min(cpufreq_clamp);
0663 
0664     /* That's it for now, we might want to deal with other failures
0665      * differently in the future though
0666      */
0667 }
0668 
0669 static void pm72_new_control(struct wf_control *ct)
0670 {
0671     bool all_controls;
0672     bool had_pump = cpu_pumps[0] || cpu_pumps[1];
0673 
0674     if (!strcmp(ct->name, "cpu-front-fan-0"))
0675         cpu_front_fans[0] = ct;
0676     else if (!strcmp(ct->name, "cpu-front-fan-1"))
0677         cpu_front_fans[1] = ct;
0678     else if (!strcmp(ct->name, "cpu-rear-fan-0"))
0679         cpu_rear_fans[0] = ct;
0680     else if (!strcmp(ct->name, "cpu-rear-fan-1"))
0681         cpu_rear_fans[1] = ct;
0682     else if (!strcmp(ct->name, "cpu-pump-0"))
0683         cpu_pumps[0] = ct;
0684     else if (!strcmp(ct->name, "cpu-pump-1"))
0685         cpu_pumps[1] = ct;
0686     else if (!strcmp(ct->name, "backside-fan"))
0687         backside_fan = ct;
0688     else if (!strcmp(ct->name, "slots-fan"))
0689         slots_fan = ct;
0690     else if (!strcmp(ct->name, "drive-bay-fan"))
0691         drives_fan = ct;
0692     else if (!strcmp(ct->name, "cpufreq-clamp"))
0693         cpufreq_clamp = ct;
0694 
0695     all_controls =
0696         cpu_front_fans[0] &&
0697         cpu_rear_fans[0] &&
0698         backside_fan &&
0699         slots_fan &&
0700         drives_fan;
0701     if (nr_chips > 1)
0702         all_controls &=
0703             cpu_front_fans[1] &&
0704             cpu_rear_fans[1];
0705     have_all_controls = all_controls;
0706 
0707     if ((cpu_pumps[0] || cpu_pumps[1]) && !had_pump) {
0708         pr_info("wf_pm72: Liquid cooling pump(s) detected,"
0709             " using new algorithm !\n");
0710         cpu_pid_combined = true;
0711     }
0712 }
0713 
0714 
0715 static void pm72_new_sensor(struct wf_sensor *sr)
0716 {
0717     bool all_sensors;
0718 
0719     if (!strcmp(sr->name, "cpu-diode-temp-0"))
0720         sens_cpu_temp[0] = sr;
0721     else if (!strcmp(sr->name, "cpu-diode-temp-1"))
0722         sens_cpu_temp[1] = sr;
0723     else if (!strcmp(sr->name, "cpu-voltage-0"))
0724         sens_cpu_volts[0] = sr;
0725     else if (!strcmp(sr->name, "cpu-voltage-1"))
0726         sens_cpu_volts[1] = sr;
0727     else if (!strcmp(sr->name, "cpu-current-0"))
0728         sens_cpu_amps[0] = sr;
0729     else if (!strcmp(sr->name, "cpu-current-1"))
0730         sens_cpu_amps[1] = sr;
0731     else if (!strcmp(sr->name, "backside-temp"))
0732         backside_temp = sr;
0733     else if (!strcmp(sr->name, "hd-temp"))
0734         drives_temp = sr;
0735 
0736     all_sensors =
0737         sens_cpu_temp[0] &&
0738         sens_cpu_volts[0] &&
0739         sens_cpu_amps[0] &&
0740         backside_temp &&
0741         drives_temp;
0742     if (nr_chips > 1)
0743         all_sensors &=
0744             sens_cpu_temp[1] &&
0745             sens_cpu_volts[1] &&
0746             sens_cpu_amps[1];
0747 
0748     have_all_sensors = all_sensors;
0749 }
0750 
0751 static int pm72_wf_notify(struct notifier_block *self,
0752               unsigned long event, void *data)
0753 {
0754     switch (event) {
0755     case WF_EVENT_NEW_SENSOR:
0756         pm72_new_sensor(data);
0757         break;
0758     case WF_EVENT_NEW_CONTROL:
0759         pm72_new_control(data);
0760         break;
0761     case WF_EVENT_TICK:
0762         if (have_all_controls && have_all_sensors)
0763             pm72_tick();
0764     }
0765     return 0;
0766 }
0767 
0768 static struct notifier_block pm72_events = {
0769     .notifier_call = pm72_wf_notify,
0770 };
0771 
0772 static int wf_pm72_probe(struct platform_device *dev)
0773 {
0774     wf_register_client(&pm72_events);
0775     return 0;
0776 }
0777 
0778 static int wf_pm72_remove(struct platform_device *dev)
0779 {
0780     wf_unregister_client(&pm72_events);
0781 
0782     /* should release all sensors and controls */
0783     return 0;
0784 }
0785 
0786 static struct platform_driver wf_pm72_driver = {
0787     .probe  = wf_pm72_probe,
0788     .remove = wf_pm72_remove,
0789     .driver = {
0790         .name = "windfarm",
0791     },
0792 };
0793 
0794 static int __init wf_pm72_init(void)
0795 {
0796     struct device_node *cpu;
0797     int i;
0798 
0799     if (!of_machine_is_compatible("PowerMac7,2") &&
0800         !of_machine_is_compatible("PowerMac7,3"))
0801         return -ENODEV;
0802 
0803     /* Count the number of CPU cores */
0804     nr_chips = 0;
0805     for_each_node_by_type(cpu, "cpu")
0806         ++nr_chips;
0807     if (nr_chips > NR_CHIPS)
0808         nr_chips = NR_CHIPS;
0809 
0810     pr_info("windfarm: Initializing for desktop G5 with %d chips\n",
0811         nr_chips);
0812 
0813     /* Get MPU data for each CPU */
0814     for (i = 0; i < nr_chips; i++) {
0815         cpu_mpu_data[i] = wf_get_mpu(i);
0816         if (!cpu_mpu_data[i]) {
0817             pr_err("wf_pm72: Failed to find MPU data for CPU %d\n", i);
0818             return -ENXIO;
0819         }
0820     }
0821 
0822 #ifdef MODULE
0823     request_module("windfarm_fcu_controls");
0824     request_module("windfarm_lm75_sensor");
0825     request_module("windfarm_ad7417_sensor");
0826     request_module("windfarm_max6690_sensor");
0827     request_module("windfarm_cpufreq_clamp");
0828 #endif /* MODULE */
0829 
0830     platform_driver_register(&wf_pm72_driver);
0831     return 0;
0832 }
0833 
0834 static void __exit wf_pm72_exit(void)
0835 {
0836     platform_driver_unregister(&wf_pm72_driver);
0837 }
0838 
0839 module_init(wf_pm72_init);
0840 module_exit(wf_pm72_exit);
0841 
0842 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
0843 MODULE_DESCRIPTION("Thermal control for AGP PowerMac G5s");
0844 MODULE_LICENSE("GPL");
0845 MODULE_ALIAS("platform:windfarm");