2 * Windfarm PowerMac thermal control. SMU based 1 CPU desktop control loops
4 * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
5 * <benh@kernel.crashing.org>
7 * Released under the term of the GNU GPL v2.
9 * The algorithm used is the PID control algorithm, used the same
10 * way the published Darwin code does, using the same values that
11 * are present in the Darwin 8.2 snapshot property lists (note however
12 * that none of the code has been re-used, it's a complete re-implementation
14 * The various control loops found in Darwin config file are:
19 * Has 3 control loops: CPU fans is similar to PowerMac8,1 (though it doesn't
20 * try to play with other control loops fans). Drive bay is rather basic PID
21 * with one sensor and one fan. Slots area is a bit different as the Darwin
22 * driver is supposed to be capable of working in a special "AGP" mode which
23 * involves the presence of an AGP sensor and an AGP fan (possibly on the
24 * AGP card itself). I can't deal with that special mode as I don't have
25 * access to those additional sensor/fans for now (though ultimately, it would
26 * be possible to add sensor objects for them) so I'm only implementing the
27 * basic PCI slot control loop
30 #include <linux/types.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/delay.h>
34 #include <linux/slab.h>
35 #include <linux/init.h>
36 #include <linux/spinlock.h>
37 #include <linux/wait.h>
38 #include <linux/kmod.h>
39 #include <linux/device.h>
40 #include <linux/platform_device.h>
42 #include <asm/machdep.h>
44 #include <asm/system.h>
45 #include <asm/sections.h>
49 #include "windfarm_pid.h"
56 #define DBG(args...) printk(args)
58 #define DBG(args...) do { } while(0)
61 /* define this to force CPU overtemp to 74 degree, useful for testing
64 #undef HACKED_OVERTEMP
66 /* Controls & sensors */
67 static struct wf_sensor *sensor_cpu_power;
68 static struct wf_sensor *sensor_cpu_temp;
69 static struct wf_sensor *sensor_hd_temp;
70 static struct wf_sensor *sensor_slots_power;
71 static struct wf_control *fan_cpu_main;
72 static struct wf_control *fan_cpu_second;
73 static struct wf_control *fan_cpu_third;
74 static struct wf_control *fan_hd;
75 static struct wf_control *fan_slots;
76 static struct wf_control *cpufreq_clamp;
78 /* Set to kick the control loop into life */
79 static int wf_smu_all_controls_ok, wf_smu_all_sensors_ok, wf_smu_started;
81 /* Failure handling.. could be nicer */
82 #define FAILURE_FAN 0x01
83 #define FAILURE_SENSOR 0x02
84 #define FAILURE_OVERTEMP 0x04
86 static unsigned int wf_smu_failure_state;
87 static int wf_smu_readjust, wf_smu_skipping;
90 * ****** CPU Fans Control Loop ******
95 #define WF_SMU_CPU_FANS_INTERVAL 1
96 #define WF_SMU_CPU_FANS_MAX_HISTORY 16
98 /* State data used by the cpu fans control loop
100 struct wf_smu_cpu_fans_state {
103 struct wf_cpu_pid_state pid;
106 static struct wf_smu_cpu_fans_state *wf_smu_cpu_fans;
111 * ****** Drive Fan Control Loop ******
115 struct wf_smu_drive_fans_state {
118 struct wf_pid_state pid;
121 static struct wf_smu_drive_fans_state *wf_smu_drive_fans;
124 * ****** Slots Fan Control Loop ******
128 struct wf_smu_slots_fans_state {
131 struct wf_pid_state pid;
134 static struct wf_smu_slots_fans_state *wf_smu_slots_fans;
137 * ***** Implementation *****
142 static void wf_smu_create_cpu_fans(void)
144 struct wf_cpu_pid_param pid_param;
145 const struct smu_sdbp_header *hdr;
146 struct smu_sdbp_cpupiddata *piddata;
147 struct smu_sdbp_fvt *fvt;
148 s32 tmax, tdelta, maxpow, powadj;
150 /* First, locate the PID params in SMU SBD */
151 hdr = smu_get_sdb_partition(SMU_SDB_CPUPIDDATA_ID, NULL);
153 printk(KERN_WARNING "windfarm: CPU PID fan config not found "
157 piddata = (struct smu_sdbp_cpupiddata *)&hdr[1];
159 /* Get the FVT params for operating point 0 (the only supported one
160 * for now) in order to get tmax
162 hdr = smu_get_sdb_partition(SMU_SDB_FVT_ID, NULL);
164 fvt = (struct smu_sdbp_fvt *)&hdr[1];
165 tmax = ((s32)fvt->maxtemp) << 16;
167 tmax = 0x5e0000; /* 94 degree default */
169 /* Alloc & initialize state */
170 wf_smu_cpu_fans = kmalloc(sizeof(struct wf_smu_cpu_fans_state),
172 if (wf_smu_cpu_fans == NULL)
174 wf_smu_cpu_fans->ticks = 1;
176 /* Fill PID params */
177 pid_param.interval = WF_SMU_CPU_FANS_INTERVAL;
178 pid_param.history_len = piddata->history_len;
179 if (pid_param.history_len > WF_CPU_PID_MAX_HISTORY) {
180 printk(KERN_WARNING "windfarm: History size overflow on "
181 "CPU control loop (%d)\n", piddata->history_len);
182 pid_param.history_len = WF_CPU_PID_MAX_HISTORY;
184 pid_param.gd = piddata->gd;
185 pid_param.gp = piddata->gp;
186 pid_param.gr = piddata->gr / pid_param.history_len;
188 tdelta = ((s32)piddata->target_temp_delta) << 16;
189 maxpow = ((s32)piddata->max_power) << 16;
190 powadj = ((s32)piddata->power_adj) << 16;
192 pid_param.tmax = tmax;
193 pid_param.ttarget = tmax - tdelta;
194 pid_param.pmaxadj = maxpow - powadj;
196 pid_param.min = fan_cpu_main->ops->get_min(fan_cpu_main);
197 pid_param.max = fan_cpu_main->ops->get_max(fan_cpu_main);
199 wf_cpu_pid_init(&wf_smu_cpu_fans->pid, &pid_param);
201 DBG("wf: CPU Fan control initialized.\n");
202 DBG(" ttarged=%d.%03d, tmax=%d.%03d, min=%d RPM, max=%d RPM\n",
203 FIX32TOPRINT(pid_param.ttarget), FIX32TOPRINT(pid_param.tmax),
204 pid_param.min, pid_param.max);
209 printk(KERN_WARNING "windfarm: CPU fan config not found\n"
210 "for this machine model, max fan speed\n");
213 wf_control_set_max(cpufreq_clamp);
215 wf_control_set_max(fan_cpu_main);
218 static void wf_smu_cpu_fans_tick(struct wf_smu_cpu_fans_state *st)
220 s32 new_setpoint, temp, power;
223 if (--st->ticks != 0) {
228 st->ticks = WF_SMU_CPU_FANS_INTERVAL;
230 rc = sensor_cpu_temp->ops->get_value(sensor_cpu_temp, &temp);
232 printk(KERN_WARNING "windfarm: CPU temp sensor error %d\n",
234 wf_smu_failure_state |= FAILURE_SENSOR;
238 rc = sensor_cpu_power->ops->get_value(sensor_cpu_power, &power);
240 printk(KERN_WARNING "windfarm: CPU power sensor error %d\n",
242 wf_smu_failure_state |= FAILURE_SENSOR;
246 DBG("wf_smu: CPU Fans tick ! CPU temp: %d.%03d, power: %d.%03d\n",
247 FIX32TOPRINT(temp), FIX32TOPRINT(power));
249 #ifdef HACKED_OVERTEMP
251 wf_smu_failure_state |= FAILURE_OVERTEMP;
253 if (temp > st->pid.param.tmax)
254 wf_smu_failure_state |= FAILURE_OVERTEMP;
256 new_setpoint = wf_cpu_pid_run(&st->pid, power, temp);
258 DBG("wf_smu: new_setpoint: %d RPM\n", (int)new_setpoint);
260 if (st->cpu_setpoint == new_setpoint)
262 st->cpu_setpoint = new_setpoint;
264 if (fan_cpu_main && wf_smu_failure_state == 0) {
265 rc = fan_cpu_main->ops->set_value(fan_cpu_main,
268 printk(KERN_WARNING "windfarm: CPU main fan"
270 wf_smu_failure_state |= FAILURE_FAN;
273 if (fan_cpu_second && wf_smu_failure_state == 0) {
274 rc = fan_cpu_second->ops->set_value(fan_cpu_second,
277 printk(KERN_WARNING "windfarm: CPU second fan"
279 wf_smu_failure_state |= FAILURE_FAN;
282 if (fan_cpu_third && wf_smu_failure_state == 0) {
283 rc = fan_cpu_main->ops->set_value(fan_cpu_third,
286 printk(KERN_WARNING "windfarm: CPU third fan"
288 wf_smu_failure_state |= FAILURE_FAN;
293 static void wf_smu_create_drive_fans(void)
295 struct wf_pid_param param = {
301 .itarget = 0x00200000,
304 /* Alloc & initialize state */
305 wf_smu_drive_fans = kmalloc(sizeof(struct wf_smu_drive_fans_state),
307 if (wf_smu_drive_fans == NULL) {
308 printk(KERN_WARNING "windfarm: Memory allocation error"
312 wf_smu_drive_fans->ticks = 1;
314 /* Fill PID params */
315 param.additive = (fan_hd->type == WF_CONTROL_RPM_FAN);
316 param.min = fan_hd->ops->get_min(fan_hd);
317 param.max = fan_hd->ops->get_max(fan_hd);
318 wf_pid_init(&wf_smu_drive_fans->pid, ¶m);
320 DBG("wf: Drive Fan control initialized.\n");
321 DBG(" itarged=%d.%03d, min=%d RPM, max=%d RPM\n",
322 FIX32TOPRINT(param.itarget), param.min, param.max);
327 wf_control_set_max(fan_hd);
330 static void wf_smu_drive_fans_tick(struct wf_smu_drive_fans_state *st)
332 s32 new_setpoint, temp;
335 if (--st->ticks != 0) {
340 st->ticks = st->pid.param.interval;
342 rc = sensor_hd_temp->ops->get_value(sensor_hd_temp, &temp);
344 printk(KERN_WARNING "windfarm: HD temp sensor error %d\n",
346 wf_smu_failure_state |= FAILURE_SENSOR;
350 DBG("wf_smu: Drive Fans tick ! HD temp: %d.%03d\n",
353 if (temp > (st->pid.param.itarget + 0x50000))
354 wf_smu_failure_state |= FAILURE_OVERTEMP;
356 new_setpoint = wf_pid_run(&st->pid, temp);
358 DBG("wf_smu: new_setpoint: %d\n", (int)new_setpoint);
360 if (st->setpoint == new_setpoint)
362 st->setpoint = new_setpoint;
364 if (fan_hd && wf_smu_failure_state == 0) {
365 rc = fan_hd->ops->set_value(fan_hd, st->setpoint);
367 printk(KERN_WARNING "windfarm: HD fan error %d\n",
369 wf_smu_failure_state |= FAILURE_FAN;
374 static void wf_smu_create_slots_fans(void)
376 struct wf_pid_param param = {
382 .itarget = 0x00000000
385 /* Alloc & initialize state */
386 wf_smu_slots_fans = kmalloc(sizeof(struct wf_smu_slots_fans_state),
388 if (wf_smu_slots_fans == NULL) {
389 printk(KERN_WARNING "windfarm: Memory allocation error"
393 wf_smu_slots_fans->ticks = 1;
395 /* Fill PID params */
396 param.additive = (fan_slots->type == WF_CONTROL_RPM_FAN);
397 param.min = fan_slots->ops->get_min(fan_slots);
398 param.max = fan_slots->ops->get_max(fan_slots);
399 wf_pid_init(&wf_smu_slots_fans->pid, ¶m);
401 DBG("wf: Slots Fan control initialized.\n");
402 DBG(" itarged=%d.%03d, min=%d RPM, max=%d RPM\n",
403 FIX32TOPRINT(param.itarget), param.min, param.max);
408 wf_control_set_max(fan_slots);
411 static void wf_smu_slots_fans_tick(struct wf_smu_slots_fans_state *st)
413 s32 new_setpoint, power;
416 if (--st->ticks != 0) {
421 st->ticks = st->pid.param.interval;
423 rc = sensor_slots_power->ops->get_value(sensor_slots_power, &power);
425 printk(KERN_WARNING "windfarm: Slots power sensor error %d\n",
427 wf_smu_failure_state |= FAILURE_SENSOR;
431 DBG("wf_smu: Slots Fans tick ! Slots power: %d.%03d\n",
432 FIX32TOPRINT(power));
434 #if 0 /* Check what makes a good overtemp condition */
435 if (power > (st->pid.param.itarget + 0x50000))
436 wf_smu_failure_state |= FAILURE_OVERTEMP;
439 new_setpoint = wf_pid_run(&st->pid, power);
441 DBG("wf_smu: new_setpoint: %d\n", (int)new_setpoint);
443 if (st->setpoint == new_setpoint)
445 st->setpoint = new_setpoint;
447 if (fan_slots && wf_smu_failure_state == 0) {
448 rc = fan_slots->ops->set_value(fan_slots, st->setpoint);
450 printk(KERN_WARNING "windfarm: Slots fan error %d\n",
452 wf_smu_failure_state |= FAILURE_FAN;
459 * ****** Setup / Init / Misc ... ******
463 static void wf_smu_tick(void)
465 unsigned int last_failure = wf_smu_failure_state;
466 unsigned int new_failure;
468 if (!wf_smu_started) {
469 DBG("wf: creating control loops !\n");
470 wf_smu_create_drive_fans();
471 wf_smu_create_slots_fans();
472 wf_smu_create_cpu_fans();
477 if (wf_smu_skipping && --wf_smu_skipping)
480 wf_smu_failure_state = 0;
481 if (wf_smu_drive_fans)
482 wf_smu_drive_fans_tick(wf_smu_drive_fans);
483 if (wf_smu_slots_fans)
484 wf_smu_slots_fans_tick(wf_smu_slots_fans);
486 wf_smu_cpu_fans_tick(wf_smu_cpu_fans);
489 new_failure = wf_smu_failure_state & ~last_failure;
491 /* If entering failure mode, clamp cpufreq and ramp all
492 * fans to full speed.
494 if (wf_smu_failure_state && !last_failure) {
496 wf_control_set_max(cpufreq_clamp);
498 wf_control_set_max(fan_cpu_main);
500 wf_control_set_max(fan_cpu_second);
502 wf_control_set_max(fan_cpu_third);
504 wf_control_set_max(fan_hd);
506 wf_control_set_max(fan_slots);
509 /* If leaving failure mode, unclamp cpufreq and readjust
510 * all fans on next iteration
512 if (!wf_smu_failure_state && last_failure) {
514 wf_control_set_min(cpufreq_clamp);
518 /* Overtemp condition detected, notify and start skipping a couple
519 * ticks to let the temperature go down
521 if (new_failure & FAILURE_OVERTEMP) {
526 /* We only clear the overtemp condition if overtemp is cleared
527 * _and_ no other failure is present. Since a sensor error will
528 * clear the overtemp condition (can't measure temperature) at
529 * the control loop levels, but we don't want to keep it clear
532 if (new_failure == 0 && last_failure & FAILURE_OVERTEMP)
537 static void wf_smu_new_control(struct wf_control *ct)
539 if (wf_smu_all_controls_ok)
542 if (fan_cpu_main == NULL && !strcmp(ct->name, "cpu-rear-fan-0")) {
543 if (wf_get_control(ct) == 0)
547 if (fan_cpu_second == NULL && !strcmp(ct->name, "cpu-rear-fan-1")) {
548 if (wf_get_control(ct) == 0)
552 if (fan_cpu_third == NULL && !strcmp(ct->name, "cpu-front-fan-0")) {
553 if (wf_get_control(ct) == 0)
557 if (cpufreq_clamp == NULL && !strcmp(ct->name, "cpufreq-clamp")) {
558 if (wf_get_control(ct) == 0)
562 if (fan_hd == NULL && !strcmp(ct->name, "drive-bay-fan")) {
563 if (wf_get_control(ct) == 0)
567 if (fan_slots == NULL && !strcmp(ct->name, "slots-fan")) {
568 if (wf_get_control(ct) == 0)
572 if (fan_cpu_main && (fan_cpu_second || fan_cpu_third) && fan_hd &&
573 fan_slots && cpufreq_clamp)
574 wf_smu_all_controls_ok = 1;
577 static void wf_smu_new_sensor(struct wf_sensor *sr)
579 if (wf_smu_all_sensors_ok)
582 if (sensor_cpu_power == NULL && !strcmp(sr->name, "cpu-power")) {
583 if (wf_get_sensor(sr) == 0)
584 sensor_cpu_power = sr;
587 if (sensor_cpu_temp == NULL && !strcmp(sr->name, "cpu-temp")) {
588 if (wf_get_sensor(sr) == 0)
589 sensor_cpu_temp = sr;
592 if (sensor_hd_temp == NULL && !strcmp(sr->name, "hd-temp")) {
593 if (wf_get_sensor(sr) == 0)
597 if (sensor_slots_power == NULL && !strcmp(sr->name, "slots-power")) {
598 if (wf_get_sensor(sr) == 0)
599 sensor_slots_power = sr;
602 if (sensor_cpu_power && sensor_cpu_temp &&
603 sensor_hd_temp && sensor_slots_power)
604 wf_smu_all_sensors_ok = 1;
608 static int wf_smu_notify(struct notifier_block *self,
609 unsigned long event, void *data)
612 case WF_EVENT_NEW_CONTROL:
613 DBG("wf: new control %s detected\n",
614 ((struct wf_control *)data)->name);
615 wf_smu_new_control(data);
618 case WF_EVENT_NEW_SENSOR:
619 DBG("wf: new sensor %s detected\n",
620 ((struct wf_sensor *)data)->name);
621 wf_smu_new_sensor(data);
624 if (wf_smu_all_controls_ok && wf_smu_all_sensors_ok)
631 static struct notifier_block wf_smu_events = {
632 .notifier_call = wf_smu_notify,
635 static int wf_init_pm(void)
637 printk(KERN_INFO "windfarm: Initializing for Desktop G5 model\n");
642 static int wf_smu_probe(struct platform_device *ddev)
644 wf_register_client(&wf_smu_events);
649 static int __devexit wf_smu_remove(struct platform_device *ddev)
651 wf_unregister_client(&wf_smu_events);
653 /* XXX We don't have yet a guarantee that our callback isn't
654 * in progress when returning from wf_unregister_client, so
655 * we add an arbitrary delay. I'll have to fix that in the core
659 /* Release all sensors */
660 /* One more crappy race: I don't think we have any guarantee here
661 * that the attribute callback won't race with the sensor beeing
662 * disposed of, and I'm not 100% certain what best way to deal
663 * with that except by adding locks all over... I'll do that
664 * eventually but heh, who ever rmmod this module anyway ?
666 if (sensor_cpu_power)
667 wf_put_sensor(sensor_cpu_power);
669 wf_put_sensor(sensor_cpu_temp);
671 wf_put_sensor(sensor_hd_temp);
672 if (sensor_slots_power)
673 wf_put_sensor(sensor_slots_power);
675 /* Release all controls */
677 wf_put_control(fan_cpu_main);
679 wf_put_control(fan_cpu_second);
681 wf_put_control(fan_cpu_third);
683 wf_put_control(fan_hd);
685 wf_put_control(fan_slots);
687 wf_put_control(cpufreq_clamp);
689 /* Destroy control loops state structures */
690 if (wf_smu_slots_fans)
691 kfree(wf_smu_cpu_fans);
692 if (wf_smu_drive_fans)
693 kfree(wf_smu_cpu_fans);
695 kfree(wf_smu_cpu_fans);
700 static struct platform_driver wf_smu_driver = {
701 .probe = wf_smu_probe,
702 .remove = __devexit_p(wf_smu_remove),
705 .bus = &platform_bus_type,
710 static int __init wf_smu_init(void)
714 if (machine_is_compatible("PowerMac9,1"))
719 request_module("windfarm_smu_controls");
720 request_module("windfarm_smu_sensors");
721 request_module("windfarm_lm75_sensor");
722 request_module("windfarm_cpufreq_clamp");
725 platform_driver_register(&wf_smu_driver);
731 static void __exit wf_smu_exit(void)
734 platform_driver_unregister(&wf_smu_driver);
738 module_init(wf_smu_init);
739 module_exit(wf_smu_exit);
741 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
742 MODULE_DESCRIPTION("Thermal control logic for PowerMac9,1");
743 MODULE_LICENSE("GPL");