2 * Windfarm PowerMac thermal control.
3 * Control loops for machines with SMU and PPC970MP processors.
5 * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org>
6 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
8 * Use and redistribute under the terms of the GNU GPL v2.
10 #include <linux/types.h>
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/device.h>
14 #include <linux/platform_device.h>
15 #include <linux/reboot.h>
16 #include <linux/slab.h>
21 #include "windfarm_pid.h"
29 #define DBG(args...) printk(args)
31 #define DBG(args...) do { } while(0)
35 #define DBG_LOTS(args...) printk(args)
37 #define DBG_LOTS(args...) do { } while(0)
40 /* define this to force CPU overtemp to 60 degree, useful for testing
43 #undef HACKED_OVERTEMP
45 /* We currently only handle 2 chips, 4 cores... */
48 #define NR_CPU_FANS 3 * NR_CHIPS
50 /* Controls and sensors */
51 static struct wf_sensor *sens_cpu_temp[NR_CORES];
52 static struct wf_sensor *sens_cpu_power[NR_CORES];
53 static struct wf_sensor *hd_temp;
54 static struct wf_sensor *slots_power;
55 static struct wf_sensor *u4_temp;
57 static struct wf_control *cpu_fans[NR_CPU_FANS];
58 static char *cpu_fan_names[NR_CPU_FANS] = {
66 static struct wf_control *cpufreq_clamp;
68 /* Second pump isn't required (and isn't actually present) */
69 #define CPU_FANS_REQD (NR_CPU_FANS - 2)
73 /* We keep a temperature history for average calculation of 180s */
74 #define CPU_TEMP_HIST_SIZE 180
76 /* Scale factor for fan speed, *100 */
77 static int cpu_fan_scale[NR_CPU_FANS] = {
80 97, /* inlet fans run at 97% of exhaust fan */
82 100, /* updated later */
83 100, /* updated later */
86 static struct wf_control *backside_fan;
87 static struct wf_control *slots_fan;
88 static struct wf_control *drive_bay_fan;
91 static struct wf_cpu_pid_state cpu_pid[NR_CORES];
92 static u32 cpu_thist[CPU_TEMP_HIST_SIZE];
93 static int cpu_thist_pt;
94 static s64 cpu_thist_total;
95 static s32 cpu_all_tmax = 100 << 16;
96 static int cpu_last_target;
97 static struct wf_pid_state backside_pid;
98 static int backside_tick;
99 static struct wf_pid_state slots_pid;
100 static int slots_started;
101 static struct wf_pid_state drive_bay_pid;
102 static int drive_bay_tick;
105 static int have_all_controls;
106 static int have_all_sensors;
109 static int failure_state;
110 #define FAILURE_SENSOR 1
111 #define FAILURE_FAN 2
112 #define FAILURE_PERM 4
113 #define FAILURE_LOW_OVERTEMP 8
114 #define FAILURE_HIGH_OVERTEMP 16
116 /* Overtemp values */
117 #define LOW_OVER_AVERAGE 0
118 #define LOW_OVER_IMMEDIATE (10 << 16)
119 #define LOW_OVER_CLEAR ((-10) << 16)
120 #define HIGH_OVER_IMMEDIATE (14 << 16)
121 #define HIGH_OVER_AVERAGE (10 << 16)
122 #define HIGH_OVER_IMMEDIATE (14 << 16)
125 /* Implementation... */
126 static int create_cpu_loop(int cpu)
130 struct smu_sdbp_header *hdr;
131 struct smu_sdbp_cpupiddata *piddata;
132 struct wf_cpu_pid_param pid;
133 struct wf_control *main_fan = cpu_fans[0];
137 /* Get FVT params to get Tmax; if not found, assume default */
138 hdr = smu_sat_get_sdb_partition(chip, 0xC4 + core, NULL);
140 struct smu_sdbp_fvt *fvt = (struct smu_sdbp_fvt *)&hdr[1];
141 tmax = fvt->maxtemp << 16;
143 tmax = 95 << 16; /* default to 95 degrees C */
145 /* We keep a global tmax for overtemp calculations */
146 if (tmax < cpu_all_tmax)
151 /* Get PID params from the appropriate SAT */
152 hdr = smu_sat_get_sdb_partition(chip, 0xC8 + core, NULL);
154 printk(KERN_WARNING"windfarm: can't get CPU PID fan config\n");
157 piddata = (struct smu_sdbp_cpupiddata *)&hdr[1];
160 * Darwin has a minimum fan speed of 1000 rpm for the 4-way and
161 * 515 for the 2-way. That appears to be overkill, so for now,
162 * impose a minimum of 750 or 515.
164 fmin = (nr_cores > 2) ? 750 : 515;
166 /* Initialize PID loop */
167 pid.interval = 1; /* seconds */
168 pid.history_len = piddata->history_len;
169 pid.gd = piddata->gd;
170 pid.gp = piddata->gp;
171 pid.gr = piddata->gr / piddata->history_len;
172 pid.pmaxadj = (piddata->max_power << 16) - (piddata->power_adj << 8);
173 pid.ttarget = tmax - (piddata->target_temp_delta << 16);
175 pid.min = main_fan->ops->get_min(main_fan);
176 pid.max = main_fan->ops->get_max(main_fan);
180 wf_cpu_pid_init(&cpu_pid[cpu], &pid);
187 static void cpu_max_all_fans(void)
191 /* We max all CPU fans in case of a sensor error. We also do the
192 * cpufreq clamping now, even if it's supposedly done later by the
193 * generic code anyway, we do it earlier here to react faster
196 wf_control_set_max(cpufreq_clamp);
197 for (i = 0; i < NR_CPU_FANS; ++i)
199 wf_control_set_max(cpu_fans[i]);
202 static int cpu_check_overtemp(s32 temp)
207 /* First check for immediate overtemps */
208 if (temp >= (cpu_all_tmax + LOW_OVER_IMMEDIATE)) {
209 new_state |= FAILURE_LOW_OVERTEMP;
210 if ((failure_state & FAILURE_LOW_OVERTEMP) == 0)
211 printk(KERN_ERR "windfarm: Overtemp due to immediate CPU"
214 if (temp >= (cpu_all_tmax + HIGH_OVER_IMMEDIATE)) {
215 new_state |= FAILURE_HIGH_OVERTEMP;
216 if ((failure_state & FAILURE_HIGH_OVERTEMP) == 0)
217 printk(KERN_ERR "windfarm: Critical overtemp due to"
218 " immediate CPU temperature !\n");
221 /* We calculate a history of max temperatures and use that for the
222 * overtemp management
224 t_old = cpu_thist[cpu_thist_pt];
225 cpu_thist[cpu_thist_pt] = temp;
226 cpu_thist_pt = (cpu_thist_pt + 1) % CPU_TEMP_HIST_SIZE;
227 cpu_thist_total -= t_old;
228 cpu_thist_total += temp;
229 t_avg = cpu_thist_total / CPU_TEMP_HIST_SIZE;
231 DBG_LOTS("t_avg = %d.%03d (out: %d.%03d, in: %d.%03d)\n",
232 FIX32TOPRINT(t_avg), FIX32TOPRINT(t_old), FIX32TOPRINT(temp));
234 /* Now check for average overtemps */
235 if (t_avg >= (cpu_all_tmax + LOW_OVER_AVERAGE)) {
236 new_state |= FAILURE_LOW_OVERTEMP;
237 if ((failure_state & FAILURE_LOW_OVERTEMP) == 0)
238 printk(KERN_ERR "windfarm: Overtemp due to average CPU"
241 if (t_avg >= (cpu_all_tmax + HIGH_OVER_AVERAGE)) {
242 new_state |= FAILURE_HIGH_OVERTEMP;
243 if ((failure_state & FAILURE_HIGH_OVERTEMP) == 0)
244 printk(KERN_ERR "windfarm: Critical overtemp due to"
245 " average CPU temperature !\n");
248 /* Now handle overtemp conditions. We don't currently use the windfarm
249 * overtemp handling core as it's not fully suited to the needs of those
250 * new machine. This will be fixed later.
253 /* High overtemp -> immediate shutdown */
254 if (new_state & FAILURE_HIGH_OVERTEMP)
256 if ((failure_state & new_state) != new_state)
258 failure_state |= new_state;
259 } else if ((failure_state & FAILURE_LOW_OVERTEMP) &&
260 (temp < (cpu_all_tmax + LOW_OVER_CLEAR))) {
261 printk(KERN_ERR "windfarm: Overtemp condition cleared !\n");
262 failure_state &= ~FAILURE_LOW_OVERTEMP;
265 return failure_state & (FAILURE_LOW_OVERTEMP | FAILURE_HIGH_OVERTEMP);
268 static void cpu_fans_tick(void)
271 s32 greatest_delta = 0;
272 s32 temp, power, t_max = 0;
273 int i, t, target = 0;
274 struct wf_sensor *sr;
275 struct wf_control *ct;
276 struct wf_cpu_pid_state *sp;
278 DBG_LOTS(KERN_DEBUG);
279 for (cpu = 0; cpu < nr_cores; ++cpu) {
280 /* Get CPU core temperature */
281 sr = sens_cpu_temp[cpu];
282 err = sr->ops->get_value(sr, &temp);
285 printk(KERN_WARNING "windfarm: CPU %d temperature "
286 "sensor error %d\n", cpu, err);
287 failure_state |= FAILURE_SENSOR;
292 /* Keep track of highest temp */
293 t_max = max(t_max, temp);
296 sr = sens_cpu_power[cpu];
297 err = sr->ops->get_value(sr, &power);
300 printk(KERN_WARNING "windfarm: CPU %d power "
301 "sensor error %d\n", cpu, err);
302 failure_state |= FAILURE_SENSOR;
309 t = wf_cpu_pid_run(sp, power, temp);
311 if (cpu == 0 || sp->last_delta > greatest_delta) {
312 greatest_delta = sp->last_delta;
315 DBG_LOTS("[%d] P=%d.%.3d T=%d.%.3d ",
316 cpu, FIX32TOPRINT(power), FIX32TOPRINT(temp));
318 DBG_LOTS("fans = %d, t_max = %d.%03d\n", target, FIX32TOPRINT(t_max));
320 /* Darwin limits decrease to 20 per iteration */
321 if (target < (cpu_last_target - 20))
322 target = cpu_last_target - 20;
323 cpu_last_target = target;
324 for (cpu = 0; cpu < nr_cores; ++cpu)
325 cpu_pid[cpu].target = target;
327 /* Handle possible overtemps */
328 if (cpu_check_overtemp(t_max))
332 for (i = 0; i < NR_CPU_FANS; ++i) {
336 err = ct->ops->set_value(ct, target * cpu_fan_scale[i] / 100);
338 printk(KERN_WARNING "windfarm: fan %s reports "
339 "error %d\n", ct->name, err);
340 failure_state |= FAILURE_FAN;
346 /* Backside/U4 fan */
347 static struct wf_pid_param backside_param = {
357 static void backside_fan_tick(void)
363 if (!backside_fan || !u4_temp)
365 if (!backside_tick) {
366 /* first time; initialize things */
367 printk(KERN_INFO "windfarm: Backside control loop started.\n");
368 backside_param.min = backside_fan->ops->get_min(backside_fan);
369 backside_param.max = backside_fan->ops->get_max(backside_fan);
370 wf_pid_init(&backside_pid, &backside_param);
373 if (--backside_tick > 0)
375 backside_tick = backside_pid.param.interval;
377 err = u4_temp->ops->get_value(u4_temp, &temp);
379 printk(KERN_WARNING "windfarm: U4 temp sensor error %d\n",
381 failure_state |= FAILURE_SENSOR;
382 wf_control_set_max(backside_fan);
385 speed = wf_pid_run(&backside_pid, temp);
386 DBG_LOTS("backside PID temp=%d.%.3d speed=%d\n",
387 FIX32TOPRINT(temp), speed);
389 err = backside_fan->ops->set_value(backside_fan, speed);
391 printk(KERN_WARNING "windfarm: backside fan error %d\n", err);
392 failure_state |= FAILURE_FAN;
397 static struct wf_pid_param drive_bay_prm = {
407 static void drive_bay_fan_tick(void)
413 if (!drive_bay_fan || !hd_temp)
415 if (!drive_bay_tick) {
416 /* first time; initialize things */
417 printk(KERN_INFO "windfarm: Drive bay control loop started.\n");
418 drive_bay_prm.min = drive_bay_fan->ops->get_min(drive_bay_fan);
419 drive_bay_prm.max = drive_bay_fan->ops->get_max(drive_bay_fan);
420 wf_pid_init(&drive_bay_pid, &drive_bay_prm);
423 if (--drive_bay_tick > 0)
425 drive_bay_tick = drive_bay_pid.param.interval;
427 err = hd_temp->ops->get_value(hd_temp, &temp);
429 printk(KERN_WARNING "windfarm: drive bay temp sensor "
431 failure_state |= FAILURE_SENSOR;
432 wf_control_set_max(drive_bay_fan);
435 speed = wf_pid_run(&drive_bay_pid, temp);
436 DBG_LOTS("drive_bay PID temp=%d.%.3d speed=%d\n",
437 FIX32TOPRINT(temp), speed);
439 err = drive_bay_fan->ops->set_value(drive_bay_fan, speed);
441 printk(KERN_WARNING "windfarm: drive bay fan error %d\n", err);
442 failure_state |= FAILURE_FAN;
446 /* PCI slots area fan */
447 /* This makes the fan speed proportional to the power consumed */
448 static struct wf_pid_param slots_param = {
459 static void slots_fan_tick(void)
465 if (!slots_fan || !slots_power)
467 if (!slots_started) {
468 /* first time; initialize things */
469 printk(KERN_INFO "windfarm: Slots control loop started.\n");
470 wf_pid_init(&slots_pid, &slots_param);
474 err = slots_power->ops->get_value(slots_power, &power);
476 printk(KERN_WARNING "windfarm: slots power sensor error %d\n",
478 failure_state |= FAILURE_SENSOR;
479 wf_control_set_max(slots_fan);
482 speed = wf_pid_run(&slots_pid, power);
483 DBG_LOTS("slots PID power=%d.%.3d speed=%d\n",
484 FIX32TOPRINT(power), speed);
486 err = slots_fan->ops->set_value(slots_fan, speed);
488 printk(KERN_WARNING "windfarm: slots fan error %d\n", err);
489 failure_state |= FAILURE_FAN;
493 static void set_fail_state(void)
498 wf_control_set_max(cpufreq_clamp);
499 for (i = 0; i < NR_CPU_FANS; ++i)
501 wf_control_set_max(cpu_fans[i]);
503 wf_control_set_max(backside_fan);
505 wf_control_set_max(slots_fan);
507 wf_control_set_max(drive_bay_fan);
510 static void pm112_tick(void)
516 printk(KERN_INFO "windfarm: CPUs control loops started.\n");
517 for (i = 0; i < nr_cores; ++i) {
518 if (create_cpu_loop(i) < 0) {
519 failure_state = FAILURE_PERM;
524 DBG_LOTS("cpu_all_tmax=%d.%03d\n", FIX32TOPRINT(cpu_all_tmax));
526 #ifdef HACKED_OVERTEMP
527 cpu_all_tmax = 60 << 16;
531 /* Permanent failure, bail out */
532 if (failure_state & FAILURE_PERM)
534 /* Clear all failure bits except low overtemp which will be eventually
535 * cleared by the control loop itself
537 last_failure = failure_state;
538 failure_state &= FAILURE_LOW_OVERTEMP;
542 drive_bay_fan_tick();
544 DBG_LOTS("last_failure: 0x%x, failure_state: %x\n",
545 last_failure, failure_state);
547 /* Check for failures. Any failure causes cpufreq clamping */
548 if (failure_state && last_failure == 0 && cpufreq_clamp)
549 wf_control_set_max(cpufreq_clamp);
550 if (failure_state == 0 && last_failure && cpufreq_clamp)
551 wf_control_set_min(cpufreq_clamp);
553 /* That's it for now, we might want to deal with other failures
554 * differently in the future though
558 static void pm112_new_control(struct wf_control *ct)
562 if (cpufreq_clamp == NULL && !strcmp(ct->name, "cpufreq-clamp")) {
563 if (wf_get_control(ct) == 0)
567 for (i = 0; i < NR_CPU_FANS; ++i) {
568 if (!strcmp(ct->name, cpu_fan_names[i])) {
569 if (cpu_fans[i] == NULL && wf_get_control(ct) == 0)
574 if (i >= NR_CPU_FANS) {
575 /* not a CPU fan, try the others */
576 if (!strcmp(ct->name, "backside-fan")) {
577 if (backside_fan == NULL && wf_get_control(ct) == 0)
579 } else if (!strcmp(ct->name, "slots-fan")) {
580 if (slots_fan == NULL && wf_get_control(ct) == 0)
582 } else if (!strcmp(ct->name, "drive-bay-fan")) {
583 if (drive_bay_fan == NULL && wf_get_control(ct) == 0)
589 for (i = 0; i < CPU_FANS_REQD; ++i)
590 if (cpu_fans[i] == NULL)
593 /* work out pump scaling factors */
594 max_exhaust = cpu_fans[0]->ops->get_max(cpu_fans[0]);
595 for (i = FIRST_PUMP; i <= LAST_PUMP; ++i)
596 if ((ct = cpu_fans[i]) != NULL)
598 ct->ops->get_max(ct) * 100 / max_exhaust;
600 have_all_controls = 1;
603 static void pm112_new_sensor(struct wf_sensor *sr)
607 if (!strncmp(sr->name, "cpu-temp-", 9)) {
608 i = sr->name[9] - '0';
609 if (sr->name[10] == 0 && i < NR_CORES &&
610 sens_cpu_temp[i] == NULL && wf_get_sensor(sr) == 0)
611 sens_cpu_temp[i] = sr;
613 } else if (!strncmp(sr->name, "cpu-power-", 10)) {
614 i = sr->name[10] - '0';
615 if (sr->name[11] == 0 && i < NR_CORES &&
616 sens_cpu_power[i] == NULL && wf_get_sensor(sr) == 0)
617 sens_cpu_power[i] = sr;
618 } else if (!strcmp(sr->name, "hd-temp")) {
619 if (hd_temp == NULL && wf_get_sensor(sr) == 0)
621 } else if (!strcmp(sr->name, "slots-power")) {
622 if (slots_power == NULL && wf_get_sensor(sr) == 0)
624 } else if (!strcmp(sr->name, "backside-temp")) {
625 if (u4_temp == NULL && wf_get_sensor(sr) == 0)
630 /* check if we have all the sensors we need */
631 for (i = 0; i < nr_cores; ++i)
632 if (sens_cpu_temp[i] == NULL || sens_cpu_power[i] == NULL)
635 have_all_sensors = 1;
638 static int pm112_wf_notify(struct notifier_block *self,
639 unsigned long event, void *data)
642 case WF_EVENT_NEW_SENSOR:
643 pm112_new_sensor(data);
645 case WF_EVENT_NEW_CONTROL:
646 pm112_new_control(data);
649 if (have_all_controls && have_all_sensors)
655 static struct notifier_block pm112_events = {
656 .notifier_call = pm112_wf_notify,
659 static int wf_pm112_probe(struct platform_device *dev)
661 wf_register_client(&pm112_events);
665 static int wf_pm112_remove(struct platform_device *dev)
667 wf_unregister_client(&pm112_events);
668 /* should release all sensors and controls */
672 static struct platform_driver wf_pm112_driver = {
673 .probe = wf_pm112_probe,
674 .remove = wf_pm112_remove,
677 .owner = THIS_MODULE,
681 static int __init wf_pm112_init(void)
683 struct device_node *cpu;
685 if (!of_machine_is_compatible("PowerMac11,2"))
688 /* Count the number of CPU cores */
690 for_each_node_by_type(cpu, "cpu")
693 printk(KERN_INFO "windfarm: initializing for dual-core desktop G5\n");
696 request_module("windfarm_smu_controls");
697 request_module("windfarm_smu_sensors");
698 request_module("windfarm_smu_sat");
699 request_module("windfarm_lm75_sensor");
700 request_module("windfarm_max6690_sensor");
701 request_module("windfarm_cpufreq_clamp");
705 platform_driver_register(&wf_pm112_driver);
709 static void __exit wf_pm112_exit(void)
711 platform_driver_unregister(&wf_pm112_driver);
714 module_init(wf_pm112_init);
715 module_exit(wf_pm112_exit);
717 MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
718 MODULE_DESCRIPTION("Thermal control for PowerMac11,2");
719 MODULE_LICENSE("GPL");
720 MODULE_ALIAS("platform:windfarm");