1 // SPDX-License-Identifier: GPL-2.0-only
3 * Windfarm PowerMac thermal control.
4 * Control loops for RackMack3,1 (Xserve G5)
6 * Copyright (C) 2012 Benjamin Herrenschmidt, IBM Corp.
8 #include <linux/types.h>
9 #include <linux/errno.h>
10 #include <linux/kernel.h>
11 #include <linux/device.h>
12 #include <linux/platform_device.h>
13 #include <linux/reboot.h>
18 #include "windfarm_pid.h"
19 #include "windfarm_mpu.h"
27 #define DBG(args...) printk(args)
29 #define DBG(args...) do { } while(0)
33 #define DBG_LOTS(args...) printk(args)
35 #define DBG_LOTS(args...) do { } while(0)
38 /* define this to force CPU overtemp to 60 degree, useful for testing
41 #undef HACKED_OVERTEMP
43 /* We currently only handle 2 chips */
45 #define NR_CPU_FANS 3 * NR_CHIPS
47 /* Controls and sensors */
48 static struct wf_sensor
*sens_cpu_temp
[NR_CHIPS
];
49 static struct wf_sensor
*sens_cpu_volts
[NR_CHIPS
];
50 static struct wf_sensor
*sens_cpu_amps
[NR_CHIPS
];
51 static struct wf_sensor
*backside_temp
;
52 static struct wf_sensor
*slots_temp
;
53 static struct wf_sensor
*dimms_temp
;
55 static struct wf_control
*cpu_fans
[NR_CHIPS
][3];
56 static struct wf_control
*backside_fan
;
57 static struct wf_control
*slots_fan
;
58 static struct wf_control
*cpufreq_clamp
;
60 /* We keep a temperature history for average calculation of 180s */
61 #define CPU_TEMP_HIST_SIZE 180
64 static const struct mpu_data
*cpu_mpu_data
[NR_CHIPS
];
65 static struct wf_cpu_pid_state cpu_pid
[NR_CHIPS
];
66 static u32 cpu_thist
[CPU_TEMP_HIST_SIZE
];
67 static int cpu_thist_pt
;
68 static s64 cpu_thist_total
;
69 static s32 cpu_all_tmax
= 100 << 16;
70 static struct wf_pid_state backside_pid
;
71 static int backside_tick
;
72 static struct wf_pid_state slots_pid
;
73 static int slots_tick
;
74 static int slots_speed
;
75 static struct wf_pid_state dimms_pid
;
76 static int dimms_output_clamp
;
79 static bool have_all_controls
;
80 static bool have_all_sensors
;
83 static int failure_state
;
84 #define FAILURE_SENSOR 1
86 #define FAILURE_PERM 4
87 #define FAILURE_LOW_OVERTEMP 8
88 #define FAILURE_HIGH_OVERTEMP 16
91 #define LOW_OVER_AVERAGE 0
92 #define LOW_OVER_IMMEDIATE (10 << 16)
93 #define LOW_OVER_CLEAR ((-10) << 16)
94 #define HIGH_OVER_IMMEDIATE (14 << 16)
95 #define HIGH_OVER_AVERAGE (10 << 16)
96 #define HIGH_OVER_IMMEDIATE (14 << 16)
99 static void cpu_max_all_fans(void)
103 /* We max all CPU fans in case of a sensor error. We also do the
104 * cpufreq clamping now, even if it's supposedly done later by the
105 * generic code anyway, we do it earlier here to react faster
108 wf_control_set_max(cpufreq_clamp
);
109 for (i
= 0; i
< nr_chips
; i
++) {
111 wf_control_set_max(cpu_fans
[i
][0]);
113 wf_control_set_max(cpu_fans
[i
][1]);
115 wf_control_set_max(cpu_fans
[i
][2]);
119 static int cpu_check_overtemp(s32 temp
)
123 static bool first
= true;
125 /* First check for immediate overtemps */
126 if (temp
>= (cpu_all_tmax
+ LOW_OVER_IMMEDIATE
)) {
127 new_state
|= FAILURE_LOW_OVERTEMP
;
128 if ((failure_state
& FAILURE_LOW_OVERTEMP
) == 0)
129 printk(KERN_ERR
"windfarm: Overtemp due to immediate CPU"
132 if (temp
>= (cpu_all_tmax
+ HIGH_OVER_IMMEDIATE
)) {
133 new_state
|= FAILURE_HIGH_OVERTEMP
;
134 if ((failure_state
& FAILURE_HIGH_OVERTEMP
) == 0)
135 printk(KERN_ERR
"windfarm: Critical overtemp due to"
136 " immediate CPU temperature !\n");
140 * The first time around, initialize the array with the first
141 * temperature reading
147 for (i
= 0; i
< CPU_TEMP_HIST_SIZE
; i
++) {
149 cpu_thist_total
+= temp
;
155 * We calculate a history of max temperatures and use that for the
156 * overtemp management
158 t_old
= cpu_thist
[cpu_thist_pt
];
159 cpu_thist
[cpu_thist_pt
] = temp
;
160 cpu_thist_pt
= (cpu_thist_pt
+ 1) % CPU_TEMP_HIST_SIZE
;
161 cpu_thist_total
-= t_old
;
162 cpu_thist_total
+= temp
;
163 t_avg
= cpu_thist_total
/ CPU_TEMP_HIST_SIZE
;
165 DBG_LOTS(" t_avg = %d.%03d (out: %d.%03d, in: %d.%03d)\n",
166 FIX32TOPRINT(t_avg
), FIX32TOPRINT(t_old
), FIX32TOPRINT(temp
));
168 /* Now check for average overtemps */
169 if (t_avg
>= (cpu_all_tmax
+ LOW_OVER_AVERAGE
)) {
170 new_state
|= FAILURE_LOW_OVERTEMP
;
171 if ((failure_state
& FAILURE_LOW_OVERTEMP
) == 0)
172 printk(KERN_ERR
"windfarm: Overtemp due to average CPU"
175 if (t_avg
>= (cpu_all_tmax
+ HIGH_OVER_AVERAGE
)) {
176 new_state
|= FAILURE_HIGH_OVERTEMP
;
177 if ((failure_state
& FAILURE_HIGH_OVERTEMP
) == 0)
178 printk(KERN_ERR
"windfarm: Critical overtemp due to"
179 " average CPU temperature !\n");
182 /* Now handle overtemp conditions. We don't currently use the windfarm
183 * overtemp handling core as it's not fully suited to the needs of those
184 * new machine. This will be fixed later.
187 /* High overtemp -> immediate shutdown */
188 if (new_state
& FAILURE_HIGH_OVERTEMP
)
190 if ((failure_state
& new_state
) != new_state
)
192 failure_state
|= new_state
;
193 } else if ((failure_state
& FAILURE_LOW_OVERTEMP
) &&
194 (temp
< (cpu_all_tmax
+ LOW_OVER_CLEAR
))) {
195 printk(KERN_ERR
"windfarm: Overtemp condition cleared !\n");
196 failure_state
&= ~FAILURE_LOW_OVERTEMP
;
199 return failure_state
& (FAILURE_LOW_OVERTEMP
| FAILURE_HIGH_OVERTEMP
);
202 static int read_one_cpu_vals(int cpu
, s32
*temp
, s32
*power
)
204 s32 dtemp
, volts
, amps
;
207 /* Get diode temperature */
208 rc
= wf_sensor_get(sens_cpu_temp
[cpu
], &dtemp
);
210 DBG(" CPU%d: temp reading error !\n", cpu
);
213 DBG_LOTS(" CPU%d: temp = %d.%03d\n", cpu
, FIX32TOPRINT((dtemp
)));
217 rc
= wf_sensor_get(sens_cpu_volts
[cpu
], &volts
);
219 DBG(" CPU%d, volts reading error !\n", cpu
);
222 DBG_LOTS(" CPU%d: volts = %d.%03d\n", cpu
, FIX32TOPRINT((volts
)));
225 rc
= wf_sensor_get(sens_cpu_amps
[cpu
], &s
);
227 DBG(" CPU%d, current reading error !\n", cpu
);
230 DBG_LOTS(" CPU%d: amps = %d.%03d\n", cpu
, FIX32TOPRINT((amps
)));
232 /* Calculate power */
234 /* Scale voltage and current raw sensor values according to fixed scales
235 * obtained in Darwin and calculate power from I and V
237 *power
= (((u64
)volts
) * ((u64
)amps
)) >> 16;
239 DBG_LOTS(" CPU%d: power = %d.%03d\n", cpu
, FIX32TOPRINT((*power
)));
245 static void cpu_fans_tick(void)
248 s32 speed
, temp
, power
, t_max
= 0;
250 DBG_LOTS("* cpu fans_tick_split()\n");
252 for (cpu
= 0; cpu
< nr_chips
; ++cpu
) {
253 struct wf_cpu_pid_state
*sp
= &cpu_pid
[cpu
];
255 /* Read current speed */
256 wf_control_get(cpu_fans
[cpu
][0], &sp
->target
);
258 err
= read_one_cpu_vals(cpu
, &temp
, &power
);
260 failure_state
|= FAILURE_SENSOR
;
265 /* Keep track of highest temp */
266 t_max
= max(t_max
, temp
);
268 /* Handle possible overtemps */
269 if (cpu_check_overtemp(t_max
))
273 wf_cpu_pid_run(sp
, power
, temp
);
275 DBG_LOTS(" CPU%d: target = %d RPM\n", cpu
, sp
->target
);
277 /* Apply DIMMs clamp */
278 speed
= max(sp
->target
, dimms_output_clamp
);
280 /* Apply result to all cpu fans */
281 for (i
= 0; i
< 3; i
++) {
282 err
= wf_control_set(cpu_fans
[cpu
][i
], speed
);
284 pr_warn("wf_rm31: Fan %s reports error %d\n",
285 cpu_fans
[cpu
][i
]->name
, err
);
286 failure_state
|= FAILURE_FAN
;
292 /* Implementation... */
293 static int cpu_setup_pid(int cpu
)
295 struct wf_cpu_pid_param pid
;
296 const struct mpu_data
*mpu
= cpu_mpu_data
[cpu
];
297 s32 tmax
, ttarget
, ptarget
;
298 int fmin
, fmax
, hsize
;
300 /* Get PID params from the appropriate MPU EEPROM */
301 tmax
= mpu
->tmax
<< 16;
302 ttarget
= mpu
->ttarget
<< 16;
303 ptarget
= ((s32
)(mpu
->pmaxh
- mpu
->padjmax
)) << 16;
305 DBG("wf_72: CPU%d ttarget = %d.%03d, tmax = %d.%03d\n",
306 cpu
, FIX32TOPRINT(ttarget
), FIX32TOPRINT(tmax
));
308 /* We keep a global tmax for overtemp calculations */
309 if (tmax
< cpu_all_tmax
)
312 /* Set PID min/max by using the rear fan min/max */
313 fmin
= wf_control_get_min(cpu_fans
[cpu
][0]);
314 fmax
= wf_control_get_max(cpu_fans
[cpu
][0]);
315 DBG("wf_72: CPU%d max RPM range = [%d..%d]\n", cpu
, fmin
, fmax
);
318 hsize
= min_t(int, mpu
->tguardband
, WF_PID_MAX_HISTORY
);
319 DBG("wf_72: CPU%d history size = %d\n", cpu
, hsize
);
321 /* Initialize PID loop */
322 pid
.interval
= 1; /* seconds */
323 pid
.history_len
= hsize
;
324 pid
.gd
= mpu
->pid_gd
;
325 pid
.gp
= mpu
->pid_gp
;
326 pid
.gr
= mpu
->pid_gr
;
328 pid
.ttarget
= ttarget
;
329 pid
.pmaxadj
= ptarget
;
333 wf_cpu_pid_init(&cpu_pid
[cpu
], &pid
);
334 cpu_pid
[cpu
].target
= 4000;
339 /* Backside/U3 fan */
340 static const struct wf_pid_param backside_param
= {
352 /* DIMMs temperature (clamp the backside fan) */
353 static const struct wf_pid_param dimms_param
= {
365 static void backside_fan_tick(void)
368 int speed
, dspeed
, fan_min
;
371 if (!backside_fan
|| !backside_temp
|| !dimms_temp
|| !backside_tick
)
373 if (--backside_tick
> 0)
375 backside_tick
= backside_pid
.param
.interval
;
377 DBG_LOTS("* backside fans tick\n");
379 /* Update fan speed from actual fans */
380 err
= wf_control_get(backside_fan
, &speed
);
382 backside_pid
.target
= speed
;
384 err
= wf_sensor_get(backside_temp
, &temp
);
386 printk(KERN_WARNING
"windfarm: U3 temp sensor error %d\n",
388 failure_state
|= FAILURE_SENSOR
;
389 wf_control_set_max(backside_fan
);
392 speed
= wf_pid_run(&backside_pid
, temp
);
394 DBG_LOTS("backside PID temp=%d.%.3d speed=%d\n",
395 FIX32TOPRINT(temp
), speed
);
397 err
= wf_sensor_get(dimms_temp
, &dtemp
);
399 printk(KERN_WARNING
"windfarm: DIMMs temp sensor error %d\n",
401 failure_state
|= FAILURE_SENSOR
;
402 wf_control_set_max(backside_fan
);
405 dspeed
= wf_pid_run(&dimms_pid
, dtemp
);
406 dimms_output_clamp
= dspeed
;
408 fan_min
= (dspeed
* 100) / 14000;
409 fan_min
= max(fan_min
, backside_param
.min
);
410 speed
= max(speed
, fan_min
);
412 err
= wf_control_set(backside_fan
, speed
);
414 printk(KERN_WARNING
"windfarm: backside fan error %d\n", err
);
415 failure_state
|= FAILURE_FAN
;
419 static void backside_setup_pid(void)
421 /* first time initialize things */
422 s32 fmin
= wf_control_get_min(backside_fan
);
423 s32 fmax
= wf_control_get_max(backside_fan
);
424 struct wf_pid_param param
;
426 param
= backside_param
;
427 param
.min
= max(param
.min
, fmin
);
428 param
.max
= min(param
.max
, fmax
);
429 wf_pid_init(&backside_pid
, ¶m
);
432 wf_pid_init(&dimms_pid
, ¶m
);
436 pr_info("wf_rm31: Backside control loop started.\n");
440 static const struct wf_pid_param slots_param
= {
452 static void slots_fan_tick(void)
458 if (!slots_fan
|| !slots_temp
|| !slots_tick
)
460 if (--slots_tick
> 0)
462 slots_tick
= slots_pid
.param
.interval
;
464 DBG_LOTS("* slots fans tick\n");
466 err
= wf_sensor_get(slots_temp
, &temp
);
468 pr_warn("wf_rm31: slots temp sensor error %d\n", err
);
469 failure_state
|= FAILURE_SENSOR
;
470 wf_control_set_max(slots_fan
);
473 speed
= wf_pid_run(&slots_pid
, temp
);
475 DBG_LOTS("slots PID temp=%d.%.3d speed=%d\n",
476 FIX32TOPRINT(temp
), speed
);
479 err
= wf_control_set(slots_fan
, speed
);
481 printk(KERN_WARNING
"windfarm: slots bay fan error %d\n", err
);
482 failure_state
|= FAILURE_FAN
;
486 static void slots_setup_pid(void)
488 /* first time initialize things */
489 s32 fmin
= wf_control_get_min(slots_fan
);
490 s32 fmax
= wf_control_get_max(slots_fan
);
491 struct wf_pid_param param
= slots_param
;
493 param
.min
= max(param
.min
, fmin
);
494 param
.max
= min(param
.max
, fmax
);
495 wf_pid_init(&slots_pid
, ¶m
);
498 pr_info("wf_rm31: Slots control loop started.\n");
501 static void set_fail_state(void)
506 wf_control_set_max(backside_fan
);
508 wf_control_set_max(slots_fan
);
511 static void rm31_tick(void)
517 printk(KERN_INFO
"windfarm: CPUs control loops started.\n");
518 for (i
= 0; i
< nr_chips
; ++i
) {
519 if (cpu_setup_pid(i
) < 0) {
520 failure_state
= FAILURE_PERM
;
525 DBG_LOTS("cpu_all_tmax=%d.%03d\n", FIX32TOPRINT(cpu_all_tmax
));
527 backside_setup_pid();
530 #ifdef HACKED_OVERTEMP
531 cpu_all_tmax
= 60 << 16;
535 /* Permanent failure, bail out */
536 if (failure_state
& FAILURE_PERM
)
540 * Clear all failure bits except low overtemp which will be eventually
541 * cleared by the control loop itself
543 last_failure
= failure_state
;
544 failure_state
&= FAILURE_LOW_OVERTEMP
;
548 /* We do CPUs last because they can be clamped high by
553 DBG_LOTS(" last_failure: 0x%x, failure_state: %x\n",
554 last_failure
, failure_state
);
556 /* Check for failures. Any failure causes cpufreq clamping */
557 if (failure_state
&& last_failure
== 0 && cpufreq_clamp
)
558 wf_control_set_max(cpufreq_clamp
);
559 if (failure_state
== 0 && last_failure
&& cpufreq_clamp
)
560 wf_control_set_min(cpufreq_clamp
);
562 /* That's it for now, we might want to deal with other failures
563 * differently in the future though
567 static void rm31_new_control(struct wf_control
*ct
)
571 if (!strcmp(ct
->name
, "cpu-fan-a-0"))
573 else if (!strcmp(ct
->name
, "cpu-fan-b-0"))
575 else if (!strcmp(ct
->name
, "cpu-fan-c-0"))
577 else if (!strcmp(ct
->name
, "cpu-fan-a-1"))
579 else if (!strcmp(ct
->name
, "cpu-fan-b-1"))
581 else if (!strcmp(ct
->name
, "cpu-fan-c-1"))
583 else if (!strcmp(ct
->name
, "backside-fan"))
585 else if (!strcmp(ct
->name
, "slots-fan"))
587 else if (!strcmp(ct
->name
, "cpufreq-clamp"))
601 have_all_controls
= all_controls
;
605 static void rm31_new_sensor(struct wf_sensor
*sr
)
609 if (!strcmp(sr
->name
, "cpu-diode-temp-0"))
610 sens_cpu_temp
[0] = sr
;
611 else if (!strcmp(sr
->name
, "cpu-diode-temp-1"))
612 sens_cpu_temp
[1] = sr
;
613 else if (!strcmp(sr
->name
, "cpu-voltage-0"))
614 sens_cpu_volts
[0] = sr
;
615 else if (!strcmp(sr
->name
, "cpu-voltage-1"))
616 sens_cpu_volts
[1] = sr
;
617 else if (!strcmp(sr
->name
, "cpu-current-0"))
618 sens_cpu_amps
[0] = sr
;
619 else if (!strcmp(sr
->name
, "cpu-current-1"))
620 sens_cpu_amps
[1] = sr
;
621 else if (!strcmp(sr
->name
, "backside-temp"))
623 else if (!strcmp(sr
->name
, "slots-temp"))
625 else if (!strcmp(sr
->name
, "dimms-temp"))
641 have_all_sensors
= all_sensors
;
644 static int rm31_wf_notify(struct notifier_block
*self
,
645 unsigned long event
, void *data
)
648 case WF_EVENT_NEW_SENSOR
:
649 rm31_new_sensor(data
);
651 case WF_EVENT_NEW_CONTROL
:
652 rm31_new_control(data
);
655 if (have_all_controls
&& have_all_sensors
)
661 static struct notifier_block rm31_events
= {
662 .notifier_call
= rm31_wf_notify
,
665 static int wf_rm31_probe(struct platform_device
*dev
)
667 wf_register_client(&rm31_events
);
671 static int wf_rm31_remove(struct platform_device
*dev
)
673 wf_unregister_client(&rm31_events
);
675 /* should release all sensors and controls */
679 static struct platform_driver wf_rm31_driver
= {
680 .probe
= wf_rm31_probe
,
681 .remove
= wf_rm31_remove
,
687 static int __init
wf_rm31_init(void)
689 struct device_node
*cpu
;
692 if (!of_machine_is_compatible("RackMac3,1"))
695 /* Count the number of CPU cores */
697 for_each_node_by_type(cpu
, "cpu")
699 if (nr_chips
> NR_CHIPS
)
702 pr_info("windfarm: Initializing for desktop G5 with %d chips\n",
705 /* Get MPU data for each CPU */
706 for (i
= 0; i
< nr_chips
; i
++) {
707 cpu_mpu_data
[i
] = wf_get_mpu(i
);
708 if (!cpu_mpu_data
[i
]) {
709 pr_err("wf_rm31: Failed to find MPU data for CPU %d\n", i
);
715 request_module("windfarm_fcu_controls");
716 request_module("windfarm_lm75_sensor");
717 request_module("windfarm_lm87_sensor");
718 request_module("windfarm_ad7417_sensor");
719 request_module("windfarm_max6690_sensor");
720 request_module("windfarm_cpufreq_clamp");
723 platform_driver_register(&wf_rm31_driver
);
727 static void __exit
wf_rm31_exit(void)
729 platform_driver_unregister(&wf_rm31_driver
);
732 module_init(wf_rm31_init
);
733 module_exit(wf_rm31_exit
);
735 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
736 MODULE_DESCRIPTION("Thermal control for Xserve G5");
737 MODULE_LICENSE("GPL");
738 MODULE_ALIAS("platform:windfarm");