2 * Windfarm PowerMac thermal control.
3 * Control loops for RackMack3,1 (Xserve G5)
5 * Copyright (C) 2012 Benjamin Herrenschmidt, IBM Corp.
7 * Use and redistribute under the terms of the GNU GPL v2.
9 #include <linux/types.h>
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/device.h>
13 #include <linux/platform_device.h>
14 #include <linux/reboot.h>
19 #include "windfarm_pid.h"
20 #include "windfarm_mpu.h"
28 #define DBG(args...) printk(args)
30 #define DBG(args...) do { } while(0)
34 #define DBG_LOTS(args...) printk(args)
36 #define DBG_LOTS(args...) do { } while(0)
39 /* define this to force CPU overtemp to 60 degree, useful for testing
42 #undef HACKED_OVERTEMP
44 /* We currently only handle 2 chips */
46 #define NR_CPU_FANS 3 * NR_CHIPS
48 /* Controls and sensors */
49 static struct wf_sensor
*sens_cpu_temp
[NR_CHIPS
];
50 static struct wf_sensor
*sens_cpu_volts
[NR_CHIPS
];
51 static struct wf_sensor
*sens_cpu_amps
[NR_CHIPS
];
52 static struct wf_sensor
*backside_temp
;
53 static struct wf_sensor
*slots_temp
;
54 static struct wf_sensor
*dimms_temp
;
56 static struct wf_control
*cpu_fans
[NR_CHIPS
][3];
57 static struct wf_control
*backside_fan
;
58 static struct wf_control
*slots_fan
;
59 static struct wf_control
*cpufreq_clamp
;
61 /* We keep a temperature history for average calculation of 180s */
62 #define CPU_TEMP_HIST_SIZE 180
65 static const struct mpu_data
*cpu_mpu_data
[NR_CHIPS
];
66 static struct wf_cpu_pid_state cpu_pid
[NR_CHIPS
];
67 static u32 cpu_thist
[CPU_TEMP_HIST_SIZE
];
68 static int cpu_thist_pt
;
69 static s64 cpu_thist_total
;
70 static s32 cpu_all_tmax
= 100 << 16;
71 static struct wf_pid_state backside_pid
;
72 static int backside_tick
;
73 static struct wf_pid_state slots_pid
;
74 static int slots_tick
;
75 static int slots_speed
;
76 static struct wf_pid_state dimms_pid
;
77 static int dimms_output_clamp
;
80 static bool have_all_controls
;
81 static bool have_all_sensors
;
84 static int failure_state
;
85 #define FAILURE_SENSOR 1
87 #define FAILURE_PERM 4
88 #define FAILURE_LOW_OVERTEMP 8
89 #define FAILURE_HIGH_OVERTEMP 16
92 #define LOW_OVER_AVERAGE 0
93 #define LOW_OVER_IMMEDIATE (10 << 16)
94 #define LOW_OVER_CLEAR ((-10) << 16)
95 #define HIGH_OVER_IMMEDIATE (14 << 16)
96 #define HIGH_OVER_AVERAGE (10 << 16)
97 #define HIGH_OVER_IMMEDIATE (14 << 16)
100 static void cpu_max_all_fans(void)
104 /* We max all CPU fans in case of a sensor error. We also do the
105 * cpufreq clamping now, even if it's supposedly done later by the
106 * generic code anyway, we do it earlier here to react faster
109 wf_control_set_max(cpufreq_clamp
);
110 for (i
= 0; i
< nr_chips
; i
++) {
112 wf_control_set_max(cpu_fans
[i
][0]);
114 wf_control_set_max(cpu_fans
[i
][1]);
116 wf_control_set_max(cpu_fans
[i
][2]);
120 static int cpu_check_overtemp(s32 temp
)
124 static bool first
= true;
126 /* First check for immediate overtemps */
127 if (temp
>= (cpu_all_tmax
+ LOW_OVER_IMMEDIATE
)) {
128 new_state
|= FAILURE_LOW_OVERTEMP
;
129 if ((failure_state
& FAILURE_LOW_OVERTEMP
) == 0)
130 printk(KERN_ERR
"windfarm: Overtemp due to immediate CPU"
133 if (temp
>= (cpu_all_tmax
+ HIGH_OVER_IMMEDIATE
)) {
134 new_state
|= FAILURE_HIGH_OVERTEMP
;
135 if ((failure_state
& FAILURE_HIGH_OVERTEMP
) == 0)
136 printk(KERN_ERR
"windfarm: Critical overtemp due to"
137 " immediate CPU temperature !\n");
141 * The first time around, initialize the array with the first
142 * temperature reading
148 for (i
= 0; i
< CPU_TEMP_HIST_SIZE
; i
++) {
150 cpu_thist_total
+= temp
;
156 * We calculate a history of max temperatures and use that for the
157 * overtemp management
159 t_old
= cpu_thist
[cpu_thist_pt
];
160 cpu_thist
[cpu_thist_pt
] = temp
;
161 cpu_thist_pt
= (cpu_thist_pt
+ 1) % CPU_TEMP_HIST_SIZE
;
162 cpu_thist_total
-= t_old
;
163 cpu_thist_total
+= temp
;
164 t_avg
= cpu_thist_total
/ CPU_TEMP_HIST_SIZE
;
166 DBG_LOTS(" t_avg = %d.%03d (out: %d.%03d, in: %d.%03d)\n",
167 FIX32TOPRINT(t_avg
), FIX32TOPRINT(t_old
), FIX32TOPRINT(temp
));
169 /* Now check for average overtemps */
170 if (t_avg
>= (cpu_all_tmax
+ LOW_OVER_AVERAGE
)) {
171 new_state
|= FAILURE_LOW_OVERTEMP
;
172 if ((failure_state
& FAILURE_LOW_OVERTEMP
) == 0)
173 printk(KERN_ERR
"windfarm: Overtemp due to average CPU"
176 if (t_avg
>= (cpu_all_tmax
+ HIGH_OVER_AVERAGE
)) {
177 new_state
|= FAILURE_HIGH_OVERTEMP
;
178 if ((failure_state
& FAILURE_HIGH_OVERTEMP
) == 0)
179 printk(KERN_ERR
"windfarm: Critical overtemp due to"
180 " average CPU temperature !\n");
183 /* Now handle overtemp conditions. We don't currently use the windfarm
184 * overtemp handling core as it's not fully suited to the needs of those
185 * new machine. This will be fixed later.
188 /* High overtemp -> immediate shutdown */
189 if (new_state
& FAILURE_HIGH_OVERTEMP
)
191 if ((failure_state
& new_state
) != new_state
)
193 failure_state
|= new_state
;
194 } else if ((failure_state
& FAILURE_LOW_OVERTEMP
) &&
195 (temp
< (cpu_all_tmax
+ LOW_OVER_CLEAR
))) {
196 printk(KERN_ERR
"windfarm: Overtemp condition cleared !\n");
197 failure_state
&= ~FAILURE_LOW_OVERTEMP
;
200 return failure_state
& (FAILURE_LOW_OVERTEMP
| FAILURE_HIGH_OVERTEMP
);
203 static int read_one_cpu_vals(int cpu
, s32
*temp
, s32
*power
)
205 s32 dtemp
, volts
, amps
;
208 /* Get diode temperature */
209 rc
= wf_sensor_get(sens_cpu_temp
[cpu
], &dtemp
);
211 DBG(" CPU%d: temp reading error !\n", cpu
);
214 DBG_LOTS(" CPU%d: temp = %d.%03d\n", cpu
, FIX32TOPRINT((dtemp
)));
218 rc
= wf_sensor_get(sens_cpu_volts
[cpu
], &volts
);
220 DBG(" CPU%d, volts reading error !\n", cpu
);
223 DBG_LOTS(" CPU%d: volts = %d.%03d\n", cpu
, FIX32TOPRINT((volts
)));
226 rc
= wf_sensor_get(sens_cpu_amps
[cpu
], &s
);
228 DBG(" CPU%d, current reading error !\n", cpu
);
231 DBG_LOTS(" CPU%d: amps = %d.%03d\n", cpu
, FIX32TOPRINT((amps
)));
233 /* Calculate power */
235 /* Scale voltage and current raw sensor values according to fixed scales
236 * obtained in Darwin and calculate power from I and V
238 *power
= (((u64
)volts
) * ((u64
)amps
)) >> 16;
240 DBG_LOTS(" CPU%d: power = %d.%03d\n", cpu
, FIX32TOPRINT((*power
)));
246 static void cpu_fans_tick(void)
249 s32 speed
, temp
, power
, t_max
= 0;
251 DBG_LOTS("* cpu fans_tick_split()\n");
253 for (cpu
= 0; cpu
< nr_chips
; ++cpu
) {
254 struct wf_cpu_pid_state
*sp
= &cpu_pid
[cpu
];
256 /* Read current speed */
257 wf_control_get(cpu_fans
[cpu
][0], &sp
->target
);
259 err
= read_one_cpu_vals(cpu
, &temp
, &power
);
261 failure_state
|= FAILURE_SENSOR
;
266 /* Keep track of highest temp */
267 t_max
= max(t_max
, temp
);
269 /* Handle possible overtemps */
270 if (cpu_check_overtemp(t_max
))
274 wf_cpu_pid_run(sp
, power
, temp
);
276 DBG_LOTS(" CPU%d: target = %d RPM\n", cpu
, sp
->target
);
278 /* Apply DIMMs clamp */
279 speed
= max(sp
->target
, dimms_output_clamp
);
281 /* Apply result to all cpu fans */
282 for (i
= 0; i
< 3; i
++) {
283 err
= wf_control_set(cpu_fans
[cpu
][i
], speed
);
285 pr_warning("wf_rm31: Fan %s reports error %d\n",
286 cpu_fans
[cpu
][i
]->name
, err
);
287 failure_state
|= FAILURE_FAN
;
293 /* Implementation... */
294 static int cpu_setup_pid(int cpu
)
296 struct wf_cpu_pid_param pid
;
297 const struct mpu_data
*mpu
= cpu_mpu_data
[cpu
];
298 s32 tmax
, ttarget
, ptarget
;
299 int fmin
, fmax
, hsize
;
301 /* Get PID params from the appropriate MPU EEPROM */
302 tmax
= mpu
->tmax
<< 16;
303 ttarget
= mpu
->ttarget
<< 16;
304 ptarget
= ((s32
)(mpu
->pmaxh
- mpu
->padjmax
)) << 16;
306 DBG("wf_72: CPU%d ttarget = %d.%03d, tmax = %d.%03d\n",
307 cpu
, FIX32TOPRINT(ttarget
), FIX32TOPRINT(tmax
));
309 /* We keep a global tmax for overtemp calculations */
310 if (tmax
< cpu_all_tmax
)
313 /* Set PID min/max by using the rear fan min/max */
314 fmin
= wf_control_get_min(cpu_fans
[cpu
][0]);
315 fmax
= wf_control_get_max(cpu_fans
[cpu
][0]);
316 DBG("wf_72: CPU%d max RPM range = [%d..%d]\n", cpu
, fmin
, fmax
);
319 hsize
= min_t(int, mpu
->tguardband
, WF_PID_MAX_HISTORY
);
320 DBG("wf_72: CPU%d history size = %d\n", cpu
, hsize
);
322 /* Initialize PID loop */
323 pid
.interval
= 1; /* seconds */
324 pid
.history_len
= hsize
;
325 pid
.gd
= mpu
->pid_gd
;
326 pid
.gp
= mpu
->pid_gp
;
327 pid
.gr
= mpu
->pid_gr
;
329 pid
.ttarget
= ttarget
;
330 pid
.pmaxadj
= ptarget
;
334 wf_cpu_pid_init(&cpu_pid
[cpu
], &pid
);
335 cpu_pid
[cpu
].target
= 4000;
340 /* Backside/U3 fan */
341 static struct wf_pid_param backside_param
= {
353 /* DIMMs temperature (clamp the backside fan) */
354 static struct wf_pid_param dimms_param
= {
366 static void backside_fan_tick(void)
369 int speed
, dspeed
, fan_min
;
372 if (!backside_fan
|| !backside_temp
|| !dimms_temp
|| !backside_tick
)
374 if (--backside_tick
> 0)
376 backside_tick
= backside_pid
.param
.interval
;
378 DBG_LOTS("* backside fans tick\n");
380 /* Update fan speed from actual fans */
381 err
= wf_control_get(backside_fan
, &speed
);
383 backside_pid
.target
= speed
;
385 err
= wf_sensor_get(backside_temp
, &temp
);
387 printk(KERN_WARNING
"windfarm: U3 temp sensor error %d\n",
389 failure_state
|= FAILURE_SENSOR
;
390 wf_control_set_max(backside_fan
);
393 speed
= wf_pid_run(&backside_pid
, temp
);
395 DBG_LOTS("backside PID temp=%d.%.3d speed=%d\n",
396 FIX32TOPRINT(temp
), speed
);
398 err
= wf_sensor_get(dimms_temp
, &dtemp
);
400 printk(KERN_WARNING
"windfarm: DIMMs temp sensor error %d\n",
402 failure_state
|= FAILURE_SENSOR
;
403 wf_control_set_max(backside_fan
);
406 dspeed
= wf_pid_run(&dimms_pid
, dtemp
);
407 dimms_output_clamp
= dspeed
;
409 fan_min
= (dspeed
* 100) / 14000;
410 fan_min
= max(fan_min
, backside_param
.min
);
411 speed
= max(speed
, fan_min
);
413 err
= wf_control_set(backside_fan
, speed
);
415 printk(KERN_WARNING
"windfarm: backside fan error %d\n", err
);
416 failure_state
|= FAILURE_FAN
;
420 static void backside_setup_pid(void)
422 /* first time initialize things */
423 s32 fmin
= wf_control_get_min(backside_fan
);
424 s32 fmax
= wf_control_get_max(backside_fan
);
425 struct wf_pid_param param
;
427 param
= backside_param
;
428 param
.min
= max(param
.min
, fmin
);
429 param
.max
= min(param
.max
, fmax
);
430 wf_pid_init(&backside_pid
, ¶m
);
433 wf_pid_init(&dimms_pid
, ¶m
);
437 pr_info("wf_rm31: Backside control loop started.\n");
441 static const struct wf_pid_param slots_param
= {
453 static void slots_fan_tick(void)
459 if (!slots_fan
|| !slots_temp
|| !slots_tick
)
461 if (--slots_tick
> 0)
463 slots_tick
= slots_pid
.param
.interval
;
465 DBG_LOTS("* slots fans tick\n");
467 err
= wf_sensor_get(slots_temp
, &temp
);
469 pr_warning("wf_rm31: slots temp sensor error %d\n", err
);
470 failure_state
|= FAILURE_SENSOR
;
471 wf_control_set_max(slots_fan
);
474 speed
= wf_pid_run(&slots_pid
, temp
);
476 DBG_LOTS("slots PID temp=%d.%.3d speed=%d\n",
477 FIX32TOPRINT(temp
), speed
);
480 err
= wf_control_set(slots_fan
, speed
);
482 printk(KERN_WARNING
"windfarm: slots bay fan error %d\n", err
);
483 failure_state
|= FAILURE_FAN
;
487 static void slots_setup_pid(void)
489 /* first time initialize things */
490 s32 fmin
= wf_control_get_min(slots_fan
);
491 s32 fmax
= wf_control_get_max(slots_fan
);
492 struct wf_pid_param param
= slots_param
;
494 param
.min
= max(param
.min
, fmin
);
495 param
.max
= min(param
.max
, fmax
);
496 wf_pid_init(&slots_pid
, ¶m
);
499 pr_info("wf_rm31: Slots control loop started.\n");
502 static void set_fail_state(void)
507 wf_control_set_max(backside_fan
);
509 wf_control_set_max(slots_fan
);
512 static void rm31_tick(void)
518 printk(KERN_INFO
"windfarm: CPUs control loops started.\n");
519 for (i
= 0; i
< nr_chips
; ++i
) {
520 if (cpu_setup_pid(i
) < 0) {
521 failure_state
= FAILURE_PERM
;
526 DBG_LOTS("cpu_all_tmax=%d.%03d\n", FIX32TOPRINT(cpu_all_tmax
));
528 backside_setup_pid();
531 #ifdef HACKED_OVERTEMP
532 cpu_all_tmax
= 60 << 16;
536 /* Permanent failure, bail out */
537 if (failure_state
& FAILURE_PERM
)
541 * Clear all failure bits except low overtemp which will be eventually
542 * cleared by the control loop itself
544 last_failure
= failure_state
;
545 failure_state
&= FAILURE_LOW_OVERTEMP
;
549 /* We do CPUs last because they can be clamped high by
554 DBG_LOTS(" last_failure: 0x%x, failure_state: %x\n",
555 last_failure
, failure_state
);
557 /* Check for failures. Any failure causes cpufreq clamping */
558 if (failure_state
&& last_failure
== 0 && cpufreq_clamp
)
559 wf_control_set_max(cpufreq_clamp
);
560 if (failure_state
== 0 && last_failure
&& cpufreq_clamp
)
561 wf_control_set_min(cpufreq_clamp
);
563 /* That's it for now, we might want to deal with other failures
564 * differently in the future though
568 static void rm31_new_control(struct wf_control
*ct
)
572 if (!strcmp(ct
->name
, "cpu-fan-a-0"))
574 else if (!strcmp(ct
->name
, "cpu-fan-b-0"))
576 else if (!strcmp(ct
->name
, "cpu-fan-c-0"))
578 else if (!strcmp(ct
->name
, "cpu-fan-a-1"))
580 else if (!strcmp(ct
->name
, "cpu-fan-b-1"))
582 else if (!strcmp(ct
->name
, "cpu-fan-c-1"))
584 else if (!strcmp(ct
->name
, "backside-fan"))
586 else if (!strcmp(ct
->name
, "slots-fan"))
588 else if (!strcmp(ct
->name
, "cpufreq-clamp"))
602 have_all_controls
= all_controls
;
606 static void rm31_new_sensor(struct wf_sensor
*sr
)
610 if (!strcmp(sr
->name
, "cpu-diode-temp-0"))
611 sens_cpu_temp
[0] = sr
;
612 else if (!strcmp(sr
->name
, "cpu-diode-temp-1"))
613 sens_cpu_temp
[1] = sr
;
614 else if (!strcmp(sr
->name
, "cpu-voltage-0"))
615 sens_cpu_volts
[0] = sr
;
616 else if (!strcmp(sr
->name
, "cpu-voltage-1"))
617 sens_cpu_volts
[1] = sr
;
618 else if (!strcmp(sr
->name
, "cpu-current-0"))
619 sens_cpu_amps
[0] = sr
;
620 else if (!strcmp(sr
->name
, "cpu-current-1"))
621 sens_cpu_amps
[1] = sr
;
622 else if (!strcmp(sr
->name
, "backside-temp"))
624 else if (!strcmp(sr
->name
, "slots-temp"))
626 else if (!strcmp(sr
->name
, "dimms-temp"))
642 have_all_sensors
= all_sensors
;
645 static int rm31_wf_notify(struct notifier_block
*self
,
646 unsigned long event
, void *data
)
649 case WF_EVENT_NEW_SENSOR
:
650 rm31_new_sensor(data
);
652 case WF_EVENT_NEW_CONTROL
:
653 rm31_new_control(data
);
656 if (have_all_controls
&& have_all_sensors
)
662 static struct notifier_block rm31_events
= {
663 .notifier_call
= rm31_wf_notify
,
666 static int wf_rm31_probe(struct platform_device
*dev
)
668 wf_register_client(&rm31_events
);
672 static int wf_rm31_remove(struct platform_device
*dev
)
674 wf_unregister_client(&rm31_events
);
676 /* should release all sensors and controls */
680 static struct platform_driver wf_rm31_driver
= {
681 .probe
= wf_rm31_probe
,
682 .remove
= wf_rm31_remove
,
688 static int __init
wf_rm31_init(void)
690 struct device_node
*cpu
;
693 if (!of_machine_is_compatible("RackMac3,1"))
696 /* Count the number of CPU cores */
698 for_each_node_by_type(cpu
, "cpu")
700 if (nr_chips
> NR_CHIPS
)
703 pr_info("windfarm: Initializing for desktop G5 with %d chips\n",
706 /* Get MPU data for each CPU */
707 for (i
= 0; i
< nr_chips
; i
++) {
708 cpu_mpu_data
[i
] = wf_get_mpu(i
);
709 if (!cpu_mpu_data
[i
]) {
710 pr_err("wf_rm31: Failed to find MPU data for CPU %d\n", i
);
716 request_module("windfarm_fcu_controls");
717 request_module("windfarm_lm75_sensor");
718 request_module("windfarm_lm87_sensor");
719 request_module("windfarm_ad7417_sensor");
720 request_module("windfarm_max6690_sensor");
721 request_module("windfarm_cpufreq_clamp");
724 platform_driver_register(&wf_rm31_driver
);
728 static void __exit
wf_rm31_exit(void)
730 platform_driver_unregister(&wf_rm31_driver
);
733 module_init(wf_rm31_init
);
734 module_exit(wf_rm31_exit
);
736 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
737 MODULE_DESCRIPTION("Thermal control for Xserve G5");
738 MODULE_LICENSE("GPL");
739 MODULE_ALIAS("platform:windfarm");