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>
20 #include "windfarm_pid.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, 4 cores... */
47 #define NR_CPU_FANS 3 * NR_CHIPS
49 /* Controls and sensors */
50 static struct wf_sensor
*sens_cpu_temp
[NR_CORES
];
51 static struct wf_sensor
*sens_cpu_power
[NR_CORES
];
52 static struct wf_sensor
*hd_temp
;
53 static struct wf_sensor
*slots_power
;
54 static struct wf_sensor
*u4_temp
;
56 static struct wf_control
*cpu_fans
[NR_CPU_FANS
];
57 static char *cpu_fan_names
[NR_CPU_FANS
] = {
65 static struct wf_control
*cpufreq_clamp
;
67 /* Second pump isn't required (and isn't actually present) */
68 #define CPU_FANS_REQD (NR_CPU_FANS - 2)
72 /* We keep a temperature history for average calculation of 180s */
73 #define CPU_TEMP_HIST_SIZE 180
75 /* Scale factor for fan speed, *100 */
76 static int cpu_fan_scale
[NR_CPU_FANS
] = {
79 97, /* inlet fans run at 97% of exhaust fan */
81 100, /* updated later */
82 100, /* updated later */
85 static struct wf_control
*backside_fan
;
86 static struct wf_control
*slots_fan
;
87 static struct wf_control
*drive_bay_fan
;
90 static struct wf_cpu_pid_state cpu_pid
[NR_CORES
];
91 static u32 cpu_thist
[CPU_TEMP_HIST_SIZE
];
92 static int cpu_thist_pt
;
93 static s64 cpu_thist_total
;
94 static s32 cpu_all_tmax
= 100 << 16;
95 static int cpu_last_target
;
96 static struct wf_pid_state backside_pid
;
97 static int backside_tick
;
98 static struct wf_pid_state slots_pid
;
99 static bool slots_started
;
100 static struct wf_pid_state drive_bay_pid
;
101 static int drive_bay_tick
;
104 static int have_all_controls
;
105 static int have_all_sensors
;
108 static int failure_state
;
109 #define FAILURE_SENSOR 1
110 #define FAILURE_FAN 2
111 #define FAILURE_PERM 4
112 #define FAILURE_LOW_OVERTEMP 8
113 #define FAILURE_HIGH_OVERTEMP 16
115 /* Overtemp values */
116 #define LOW_OVER_AVERAGE 0
117 #define LOW_OVER_IMMEDIATE (10 << 16)
118 #define LOW_OVER_CLEAR ((-10) << 16)
119 #define HIGH_OVER_IMMEDIATE (14 << 16)
120 #define HIGH_OVER_AVERAGE (10 << 16)
121 #define HIGH_OVER_IMMEDIATE (14 << 16)
124 /* Implementation... */
125 static int create_cpu_loop(int cpu
)
129 struct smu_sdbp_header
*hdr
;
130 struct smu_sdbp_cpupiddata
*piddata
;
131 struct wf_cpu_pid_param pid
;
132 struct wf_control
*main_fan
= cpu_fans
[0];
136 /* Get PID params from the appropriate SAT */
137 hdr
= smu_sat_get_sdb_partition(chip
, 0xC8 + core
, NULL
);
139 printk(KERN_WARNING
"windfarm: can't get CPU PID fan config\n");
142 piddata
= (struct smu_sdbp_cpupiddata
*)&hdr
[1];
144 /* Get FVT params to get Tmax; if not found, assume default */
145 hdr
= smu_sat_get_sdb_partition(chip
, 0xC4 + core
, NULL
);
147 struct smu_sdbp_fvt
*fvt
= (struct smu_sdbp_fvt
*)&hdr
[1];
148 tmax
= fvt
->maxtemp
<< 16;
150 tmax
= 95 << 16; /* default to 95 degrees C */
152 /* We keep a global tmax for overtemp calculations */
153 if (tmax
< cpu_all_tmax
)
157 * Darwin has a minimum fan speed of 1000 rpm for the 4-way and
158 * 515 for the 2-way. That appears to be overkill, so for now,
159 * impose a minimum of 750 or 515.
161 fmin
= (nr_cores
> 2) ? 750 : 515;
163 /* Initialize PID loop */
164 pid
.interval
= 1; /* seconds */
165 pid
.history_len
= piddata
->history_len
;
166 pid
.gd
= piddata
->gd
;
167 pid
.gp
= piddata
->gp
;
168 pid
.gr
= piddata
->gr
/ piddata
->history_len
;
169 pid
.pmaxadj
= (piddata
->max_power
<< 16) - (piddata
->power_adj
<< 8);
170 pid
.ttarget
= tmax
- (piddata
->target_temp_delta
<< 16);
172 pid
.min
= main_fan
->ops
->get_min(main_fan
);
173 pid
.max
= main_fan
->ops
->get_max(main_fan
);
177 wf_cpu_pid_init(&cpu_pid
[cpu
], &pid
);
181 static void cpu_max_all_fans(void)
185 /* We max all CPU fans in case of a sensor error. We also do the
186 * cpufreq clamping now, even if it's supposedly done later by the
187 * generic code anyway, we do it earlier here to react faster
190 wf_control_set_max(cpufreq_clamp
);
191 for (i
= 0; i
< NR_CPU_FANS
; ++i
)
193 wf_control_set_max(cpu_fans
[i
]);
196 static int cpu_check_overtemp(s32 temp
)
201 /* First check for immediate overtemps */
202 if (temp
>= (cpu_all_tmax
+ LOW_OVER_IMMEDIATE
)) {
203 new_state
|= FAILURE_LOW_OVERTEMP
;
204 if ((failure_state
& FAILURE_LOW_OVERTEMP
) == 0)
205 printk(KERN_ERR
"windfarm: Overtemp due to immediate CPU"
208 if (temp
>= (cpu_all_tmax
+ HIGH_OVER_IMMEDIATE
)) {
209 new_state
|= FAILURE_HIGH_OVERTEMP
;
210 if ((failure_state
& FAILURE_HIGH_OVERTEMP
) == 0)
211 printk(KERN_ERR
"windfarm: Critical overtemp due to"
212 " immediate CPU temperature !\n");
215 /* We calculate a history of max temperatures and use that for the
216 * overtemp management
218 t_old
= cpu_thist
[cpu_thist_pt
];
219 cpu_thist
[cpu_thist_pt
] = temp
;
220 cpu_thist_pt
= (cpu_thist_pt
+ 1) % CPU_TEMP_HIST_SIZE
;
221 cpu_thist_total
-= t_old
;
222 cpu_thist_total
+= temp
;
223 t_avg
= cpu_thist_total
/ CPU_TEMP_HIST_SIZE
;
225 DBG_LOTS("t_avg = %d.%03d (out: %d.%03d, in: %d.%03d)\n",
226 FIX32TOPRINT(t_avg
), FIX32TOPRINT(t_old
), FIX32TOPRINT(temp
));
228 /* Now check for average overtemps */
229 if (t_avg
>= (cpu_all_tmax
+ LOW_OVER_AVERAGE
)) {
230 new_state
|= FAILURE_LOW_OVERTEMP
;
231 if ((failure_state
& FAILURE_LOW_OVERTEMP
) == 0)
232 printk(KERN_ERR
"windfarm: Overtemp due to average CPU"
235 if (t_avg
>= (cpu_all_tmax
+ HIGH_OVER_AVERAGE
)) {
236 new_state
|= FAILURE_HIGH_OVERTEMP
;
237 if ((failure_state
& FAILURE_HIGH_OVERTEMP
) == 0)
238 printk(KERN_ERR
"windfarm: Critical overtemp due to"
239 " average CPU temperature !\n");
242 /* Now handle overtemp conditions. We don't currently use the windfarm
243 * overtemp handling core as it's not fully suited to the needs of those
244 * new machine. This will be fixed later.
247 /* High overtemp -> immediate shutdown */
248 if (new_state
& FAILURE_HIGH_OVERTEMP
)
250 if ((failure_state
& new_state
) != new_state
)
252 failure_state
|= new_state
;
253 } else if ((failure_state
& FAILURE_LOW_OVERTEMP
) &&
254 (temp
< (cpu_all_tmax
+ LOW_OVER_CLEAR
))) {
255 printk(KERN_ERR
"windfarm: Overtemp condition cleared !\n");
256 failure_state
&= ~FAILURE_LOW_OVERTEMP
;
259 return failure_state
& (FAILURE_LOW_OVERTEMP
| FAILURE_HIGH_OVERTEMP
);
262 static void cpu_fans_tick(void)
265 s32 greatest_delta
= 0;
266 s32 temp
, power
, t_max
= 0;
267 int i
, t
, target
= 0;
268 struct wf_sensor
*sr
;
269 struct wf_control
*ct
;
270 struct wf_cpu_pid_state
*sp
;
272 DBG_LOTS(KERN_DEBUG
);
273 for (cpu
= 0; cpu
< nr_cores
; ++cpu
) {
274 /* Get CPU core temperature */
275 sr
= sens_cpu_temp
[cpu
];
276 err
= sr
->ops
->get_value(sr
, &temp
);
279 printk(KERN_WARNING
"windfarm: CPU %d temperature "
280 "sensor error %d\n", cpu
, err
);
281 failure_state
|= FAILURE_SENSOR
;
286 /* Keep track of highest temp */
287 t_max
= max(t_max
, temp
);
290 sr
= sens_cpu_power
[cpu
];
291 err
= sr
->ops
->get_value(sr
, &power
);
294 printk(KERN_WARNING
"windfarm: CPU %d power "
295 "sensor error %d\n", cpu
, err
);
296 failure_state
|= FAILURE_SENSOR
;
303 t
= wf_cpu_pid_run(sp
, power
, temp
);
305 if (cpu
== 0 || sp
->last_delta
> greatest_delta
) {
306 greatest_delta
= sp
->last_delta
;
309 DBG_LOTS("[%d] P=%d.%.3d T=%d.%.3d ",
310 cpu
, FIX32TOPRINT(power
), FIX32TOPRINT(temp
));
312 DBG_LOTS("fans = %d, t_max = %d.%03d\n", target
, FIX32TOPRINT(t_max
));
314 /* Darwin limits decrease to 20 per iteration */
315 if (target
< (cpu_last_target
- 20))
316 target
= cpu_last_target
- 20;
317 cpu_last_target
= target
;
318 for (cpu
= 0; cpu
< nr_cores
; ++cpu
)
319 cpu_pid
[cpu
].target
= target
;
321 /* Handle possible overtemps */
322 if (cpu_check_overtemp(t_max
))
326 for (i
= 0; i
< NR_CPU_FANS
; ++i
) {
330 err
= ct
->ops
->set_value(ct
, target
* cpu_fan_scale
[i
] / 100);
332 printk(KERN_WARNING
"windfarm: fan %s reports "
333 "error %d\n", ct
->name
, err
);
334 failure_state
|= FAILURE_FAN
;
340 /* Backside/U4 fan */
341 static struct wf_pid_param backside_param
= {
351 static void backside_fan_tick(void)
357 if (!backside_fan
|| !u4_temp
)
359 if (!backside_tick
) {
360 /* first time; initialize things */
361 printk(KERN_INFO
"windfarm: Backside control loop started.\n");
362 backside_param
.min
= backside_fan
->ops
->get_min(backside_fan
);
363 backside_param
.max
= backside_fan
->ops
->get_max(backside_fan
);
364 wf_pid_init(&backside_pid
, &backside_param
);
367 if (--backside_tick
> 0)
369 backside_tick
= backside_pid
.param
.interval
;
371 err
= u4_temp
->ops
->get_value(u4_temp
, &temp
);
373 printk(KERN_WARNING
"windfarm: U4 temp sensor error %d\n",
375 failure_state
|= FAILURE_SENSOR
;
376 wf_control_set_max(backside_fan
);
379 speed
= wf_pid_run(&backside_pid
, temp
);
380 DBG_LOTS("backside PID temp=%d.%.3d speed=%d\n",
381 FIX32TOPRINT(temp
), speed
);
383 err
= backside_fan
->ops
->set_value(backside_fan
, speed
);
385 printk(KERN_WARNING
"windfarm: backside fan error %d\n", err
);
386 failure_state
|= FAILURE_FAN
;
391 static struct wf_pid_param drive_bay_prm
= {
401 static void drive_bay_fan_tick(void)
407 if (!drive_bay_fan
|| !hd_temp
)
409 if (!drive_bay_tick
) {
410 /* first time; initialize things */
411 printk(KERN_INFO
"windfarm: Drive bay control loop started.\n");
412 drive_bay_prm
.min
= drive_bay_fan
->ops
->get_min(drive_bay_fan
);
413 drive_bay_prm
.max
= drive_bay_fan
->ops
->get_max(drive_bay_fan
);
414 wf_pid_init(&drive_bay_pid
, &drive_bay_prm
);
417 if (--drive_bay_tick
> 0)
419 drive_bay_tick
= drive_bay_pid
.param
.interval
;
421 err
= hd_temp
->ops
->get_value(hd_temp
, &temp
);
423 printk(KERN_WARNING
"windfarm: drive bay temp sensor "
425 failure_state
|= FAILURE_SENSOR
;
426 wf_control_set_max(drive_bay_fan
);
429 speed
= wf_pid_run(&drive_bay_pid
, temp
);
430 DBG_LOTS("drive_bay PID temp=%d.%.3d speed=%d\n",
431 FIX32TOPRINT(temp
), speed
);
433 err
= drive_bay_fan
->ops
->set_value(drive_bay_fan
, speed
);
435 printk(KERN_WARNING
"windfarm: drive bay fan error %d\n", err
);
436 failure_state
|= FAILURE_FAN
;
440 /* PCI slots area fan */
441 /* This makes the fan speed proportional to the power consumed */
442 static struct wf_pid_param slots_param
= {
453 static void slots_fan_tick(void)
459 if (!slots_fan
|| !slots_power
)
461 if (!slots_started
) {
462 /* first time; initialize things */
463 printk(KERN_INFO
"windfarm: Slots control loop started.\n");
464 wf_pid_init(&slots_pid
, &slots_param
);
465 slots_started
= true;
468 err
= slots_power
->ops
->get_value(slots_power
, &power
);
470 printk(KERN_WARNING
"windfarm: slots power sensor error %d\n",
472 failure_state
|= FAILURE_SENSOR
;
473 wf_control_set_max(slots_fan
);
476 speed
= wf_pid_run(&slots_pid
, power
);
477 DBG_LOTS("slots PID power=%d.%.3d speed=%d\n",
478 FIX32TOPRINT(power
), speed
);
480 err
= slots_fan
->ops
->set_value(slots_fan
, speed
);
482 printk(KERN_WARNING
"windfarm: slots fan error %d\n", err
);
483 failure_state
|= FAILURE_FAN
;
487 static void set_fail_state(void)
492 wf_control_set_max(cpufreq_clamp
);
493 for (i
= 0; i
< NR_CPU_FANS
; ++i
)
495 wf_control_set_max(cpu_fans
[i
]);
497 wf_control_set_max(backside_fan
);
499 wf_control_set_max(slots_fan
);
501 wf_control_set_max(drive_bay_fan
);
504 static void pm112_tick(void)
510 printk(KERN_INFO
"windfarm: CPUs control loops started.\n");
511 for (i
= 0; i
< nr_cores
; ++i
) {
512 if (create_cpu_loop(i
) < 0) {
513 failure_state
= FAILURE_PERM
;
518 DBG_LOTS("cpu_all_tmax=%d.%03d\n", FIX32TOPRINT(cpu_all_tmax
));
520 #ifdef HACKED_OVERTEMP
521 cpu_all_tmax
= 60 << 16;
525 /* Permanent failure, bail out */
526 if (failure_state
& FAILURE_PERM
)
528 /* Clear all failure bits except low overtemp which will be eventually
529 * cleared by the control loop itself
531 last_failure
= failure_state
;
532 failure_state
&= FAILURE_LOW_OVERTEMP
;
536 drive_bay_fan_tick();
538 DBG_LOTS("last_failure: 0x%x, failure_state: %x\n",
539 last_failure
, failure_state
);
541 /* Check for failures. Any failure causes cpufreq clamping */
542 if (failure_state
&& last_failure
== 0 && cpufreq_clamp
)
543 wf_control_set_max(cpufreq_clamp
);
544 if (failure_state
== 0 && last_failure
&& cpufreq_clamp
)
545 wf_control_set_min(cpufreq_clamp
);
547 /* That's it for now, we might want to deal with other failures
548 * differently in the future though
552 static void pm112_new_control(struct wf_control
*ct
)
556 if (cpufreq_clamp
== NULL
&& !strcmp(ct
->name
, "cpufreq-clamp")) {
557 if (wf_get_control(ct
) == 0)
561 for (i
= 0; i
< NR_CPU_FANS
; ++i
) {
562 if (!strcmp(ct
->name
, cpu_fan_names
[i
])) {
563 if (cpu_fans
[i
] == NULL
&& wf_get_control(ct
) == 0)
568 if (i
>= NR_CPU_FANS
) {
569 /* not a CPU fan, try the others */
570 if (!strcmp(ct
->name
, "backside-fan")) {
571 if (backside_fan
== NULL
&& wf_get_control(ct
) == 0)
573 } else if (!strcmp(ct
->name
, "slots-fan")) {
574 if (slots_fan
== NULL
&& wf_get_control(ct
) == 0)
576 } else if (!strcmp(ct
->name
, "drive-bay-fan")) {
577 if (drive_bay_fan
== NULL
&& wf_get_control(ct
) == 0)
583 for (i
= 0; i
< CPU_FANS_REQD
; ++i
)
584 if (cpu_fans
[i
] == NULL
)
587 /* work out pump scaling factors */
588 max_exhaust
= cpu_fans
[0]->ops
->get_max(cpu_fans
[0]);
589 for (i
= FIRST_PUMP
; i
<= LAST_PUMP
; ++i
)
590 if ((ct
= cpu_fans
[i
]) != NULL
)
592 ct
->ops
->get_max(ct
) * 100 / max_exhaust
;
594 have_all_controls
= 1;
597 static void pm112_new_sensor(struct wf_sensor
*sr
)
601 if (!strncmp(sr
->name
, "cpu-temp-", 9)) {
602 i
= sr
->name
[9] - '0';
603 if (sr
->name
[10] == 0 && i
< NR_CORES
&&
604 sens_cpu_temp
[i
] == NULL
&& wf_get_sensor(sr
) == 0)
605 sens_cpu_temp
[i
] = sr
;
607 } else if (!strncmp(sr
->name
, "cpu-power-", 10)) {
608 i
= sr
->name
[10] - '0';
609 if (sr
->name
[11] == 0 && i
< NR_CORES
&&
610 sens_cpu_power
[i
] == NULL
&& wf_get_sensor(sr
) == 0)
611 sens_cpu_power
[i
] = sr
;
612 } else if (!strcmp(sr
->name
, "hd-temp")) {
613 if (hd_temp
== NULL
&& wf_get_sensor(sr
) == 0)
615 } else if (!strcmp(sr
->name
, "slots-power")) {
616 if (slots_power
== NULL
&& wf_get_sensor(sr
) == 0)
618 } else if (!strcmp(sr
->name
, "backside-temp")) {
619 if (u4_temp
== NULL
&& wf_get_sensor(sr
) == 0)
624 /* check if we have all the sensors we need */
625 for (i
= 0; i
< nr_cores
; ++i
)
626 if (sens_cpu_temp
[i
] == NULL
|| sens_cpu_power
[i
] == NULL
)
629 have_all_sensors
= 1;
632 static int pm112_wf_notify(struct notifier_block
*self
,
633 unsigned long event
, void *data
)
636 case WF_EVENT_NEW_SENSOR
:
637 pm112_new_sensor(data
);
639 case WF_EVENT_NEW_CONTROL
:
640 pm112_new_control(data
);
643 if (have_all_controls
&& have_all_sensors
)
649 static struct notifier_block pm112_events
= {
650 .notifier_call
= pm112_wf_notify
,
653 static int wf_pm112_probe(struct platform_device
*dev
)
655 wf_register_client(&pm112_events
);
659 static int wf_pm112_remove(struct platform_device
*dev
)
661 wf_unregister_client(&pm112_events
);
662 /* should release all sensors and controls */
666 static struct platform_driver wf_pm112_driver
= {
667 .probe
= wf_pm112_probe
,
668 .remove
= wf_pm112_remove
,
674 static int __init
wf_pm112_init(void)
676 struct device_node
*cpu
;
678 if (!of_machine_is_compatible("PowerMac11,2"))
681 /* Count the number of CPU cores */
683 for_each_node_by_type(cpu
, "cpu")
686 printk(KERN_INFO
"windfarm: initializing for dual-core desktop G5\n");
689 request_module("windfarm_smu_controls");
690 request_module("windfarm_smu_sensors");
691 request_module("windfarm_smu_sat");
692 request_module("windfarm_lm75_sensor");
693 request_module("windfarm_max6690_sensor");
694 request_module("windfarm_cpufreq_clamp");
698 platform_driver_register(&wf_pm112_driver
);
702 static void __exit
wf_pm112_exit(void)
704 platform_driver_unregister(&wf_pm112_driver
);
707 module_init(wf_pm112_init
);
708 module_exit(wf_pm112_exit
);
710 MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
711 MODULE_DESCRIPTION("Thermal control for PowerMac11,2");
712 MODULE_LICENSE("GPL");
713 MODULE_ALIAS("platform:windfarm");