2 * Windfarm PowerMac thermal control. iMac G5
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:
16 * PowerMac8,1 and PowerMac8,2
17 * ===========================
19 * System Fans control loop. Different based on models. In addition to the
20 * usual PID algorithm, the control loop gets 2 additional pairs of linear
21 * scaling factors (scale/offsets) expressed as 4.12 fixed point values
22 * signed offset, unsigned scale)
24 * The targets are modified such as:
25 * - the linked control (second control) gets the target value as-is
26 * (typically the drive fan)
27 * - the main control (first control) gets the target value scaled with
28 * the first pair of factors, and is then modified as below
29 * - the value of the target of the CPU Fan control loop is retrieved,
30 * scaled with the second pair of factors, and the max of that and
31 * the scaled target is applied to the main control.
34 * controls : system-fan, drive-bay-fan
36 * PID params : G_d = 0x15400000
40 * Input target = 0x3a0000
42 * linear-factors : offset = 0xff38 scale = 0x0ccd
43 * offset = 0x0208 scale = 0x07ae
46 * controls : system-fan, drive-bay-fan
48 * PID params : G_d = 0x08e00000
52 * Input target = 0x350000
54 * linear-factors : offset = 0xff38 scale = 0x0ccd
55 * offset = 0x0000 scale = 0x0000
58 * controls : system-fan
60 * PID params : G_d = 0x15400000
64 * Input target = 0x3a0000
66 * linear-factors : offset = 0x0000 scale = 0x1000
67 * offset = 0x0091 scale = 0x0bae
69 * CPU Fan control loop. The loop is identical for all models. it
70 * has an additional pair of scaling factor. This is used to scale the
71 * systems fan control loop target result (the one before it gets scaled
72 * by the System Fans control loop itself). Then, the max value of the
73 * calculated target value and system fan value is sent to the fans
76 * sensors : cpu-temp cpu-power
77 * PID params : From SMU sdb partition
78 * linear-factors : offset = 0xfb50 scale = 0x1000
80 * CPU Slew control loop. Not implemented. The cpufreq driver in linux is
81 * completely separate for now, though we could find a way to link it, either
82 * as a client reacting to overtemp notifications, or directling monitoring
85 * WARNING ! The CPU control loop requires the CPU tmax for the current
86 * operating point. However, we currently are completely separated from
87 * the cpufreq driver and thus do not know what the current operating
88 * point is. Fortunately, we also do not have any hardware supporting anything
89 * but operating point 0 at the moment, thus we just peek that value directly
90 * from the SDB partition. If we ever end up with actually slewing the system
91 * clock and thus changing operating points, we'll have to find a way to
92 * communicate with the CPU freq driver;
96 #include <linux/types.h>
97 #include <linux/errno.h>
98 #include <linux/kernel.h>
99 #include <linux/delay.h>
100 #include <linux/slab.h>
101 #include <linux/init.h>
102 #include <linux/spinlock.h>
103 #include <linux/wait.h>
104 #include <linux/kmod.h>
105 #include <linux/device.h>
106 #include <linux/platform_device.h>
107 #include <asm/prom.h>
108 #include <asm/machdep.h>
110 #include <asm/sections.h>
113 #include "windfarm.h"
114 #include "windfarm_pid.h"
116 #define VERSION "0.4"
121 #define DBG(args...) printk(args)
123 #define DBG(args...) do { } while(0)
126 /* define this to force CPU overtemp to 74 degree, useful for testing
129 #undef HACKED_OVERTEMP
131 static int wf_smu_mach_model
; /* machine model id */
133 /* Controls & sensors */
134 static struct wf_sensor
*sensor_cpu_power
;
135 static struct wf_sensor
*sensor_cpu_temp
;
136 static struct wf_sensor
*sensor_hd_temp
;
137 static struct wf_control
*fan_cpu_main
;
138 static struct wf_control
*fan_hd
;
139 static struct wf_control
*fan_system
;
140 static struct wf_control
*cpufreq_clamp
;
142 /* Set to kick the control loop into life */
143 static int wf_smu_all_controls_ok
, wf_smu_all_sensors_ok
, wf_smu_started
;
145 /* Failure handling.. could be nicer */
146 #define FAILURE_FAN 0x01
147 #define FAILURE_SENSOR 0x02
148 #define FAILURE_OVERTEMP 0x04
150 static unsigned int wf_smu_failure_state
;
151 static int wf_smu_readjust
, wf_smu_skipping
;
154 * ****** System Fans Control Loop ******
158 /* Parameters for the System Fans control loop. Parameters
159 * not in this table such as interval, history size, ...
160 * are common to all versions and thus hard coded for now.
162 struct wf_smu_sys_fans_param
{
173 #define WF_SMU_SYS_FANS_INTERVAL 5
174 #define WF_SMU_SYS_FANS_HISTORY_SIZE 2
176 /* State data used by the system fans control loop
178 struct wf_smu_sys_fans_state
{
186 struct wf_pid_state pid
;
190 * Configs for SMU System Fan control loop
192 static struct wf_smu_sys_fans_param wf_smu_sys_all_params
[] = {
230 #define WF_SMU_SYS_FANS_NUM_CONFIGS ARRAY_SIZE(wf_smu_sys_all_params)
232 static struct wf_smu_sys_fans_state
*wf_smu_sys_fans
;
235 * ****** CPU Fans Control Loop ******
240 #define WF_SMU_CPU_FANS_INTERVAL 1
241 #define WF_SMU_CPU_FANS_MAX_HISTORY 16
242 #define WF_SMU_CPU_FANS_SIBLING_SCALE 0x00001000
243 #define WF_SMU_CPU_FANS_SIBLING_OFFSET 0xfffffb50
245 /* State data used by the cpu fans control loop
247 struct wf_smu_cpu_fans_state
{
252 struct wf_cpu_pid_state pid
;
255 static struct wf_smu_cpu_fans_state
*wf_smu_cpu_fans
;
260 * ***** Implementation *****
264 static void wf_smu_create_sys_fans(void)
266 struct wf_smu_sys_fans_param
*param
= NULL
;
267 struct wf_pid_param pid_param
;
270 /* First, locate the params for this model */
271 for (i
= 0; i
< WF_SMU_SYS_FANS_NUM_CONFIGS
; i
++)
272 if (wf_smu_sys_all_params
[i
].model_id
== wf_smu_mach_model
) {
273 param
= &wf_smu_sys_all_params
[i
];
277 /* No params found, put fans to max */
279 printk(KERN_WARNING
"windfarm: System fan config not found "
280 "for this machine model, max fan speed\n");
284 /* Alloc & initialize state */
285 wf_smu_sys_fans
= kmalloc(sizeof(struct wf_smu_sys_fans_state
),
287 if (wf_smu_sys_fans
== NULL
) {
288 printk(KERN_WARNING
"windfarm: Memory allocation error"
292 wf_smu_sys_fans
->ticks
= 1;
293 wf_smu_sys_fans
->scale0
= param
->scale0
;
294 wf_smu_sys_fans
->offset0
= param
->offset0
;
295 wf_smu_sys_fans
->scale1
= param
->scale1
;
296 wf_smu_sys_fans
->offset1
= param
->offset1
;
298 /* Fill PID params */
299 pid_param
.gd
= param
->gd
;
300 pid_param
.gp
= param
->gp
;
301 pid_param
.gr
= param
->gr
;
302 pid_param
.interval
= WF_SMU_SYS_FANS_INTERVAL
;
303 pid_param
.history_len
= WF_SMU_SYS_FANS_HISTORY_SIZE
;
304 pid_param
.itarget
= param
->itarget
;
305 pid_param
.min
= fan_system
->ops
->get_min(fan_system
);
306 pid_param
.max
= fan_system
->ops
->get_max(fan_system
);
309 max(pid_param
.min
,fan_hd
->ops
->get_min(fan_hd
));
311 min(pid_param
.max
,fan_hd
->ops
->get_max(fan_hd
));
313 wf_pid_init(&wf_smu_sys_fans
->pid
, &pid_param
);
315 DBG("wf: System Fan control initialized.\n");
316 DBG(" itarged=%d.%03d, min=%d RPM, max=%d RPM\n",
317 FIX32TOPRINT(pid_param
.itarget
), pid_param
.min
, pid_param
.max
);
323 wf_control_set_max(fan_system
);
325 wf_control_set_max(fan_hd
);
328 static void wf_smu_sys_fans_tick(struct wf_smu_sys_fans_state
*st
)
330 s32 new_setpoint
, temp
, scaled
, cputarget
;
333 if (--st
->ticks
!= 0) {
338 st
->ticks
= WF_SMU_SYS_FANS_INTERVAL
;
340 rc
= sensor_hd_temp
->ops
->get_value(sensor_hd_temp
, &temp
);
342 printk(KERN_WARNING
"windfarm: HD temp sensor error %d\n",
344 wf_smu_failure_state
|= FAILURE_SENSOR
;
348 DBG("wf_smu: System Fans tick ! HD temp: %d.%03d\n",
351 if (temp
> (st
->pid
.param
.itarget
+ 0x50000))
352 wf_smu_failure_state
|= FAILURE_OVERTEMP
;
354 new_setpoint
= wf_pid_run(&st
->pid
, temp
);
356 DBG("wf_smu: new_setpoint: %d RPM\n", (int)new_setpoint
);
358 scaled
= ((((s64
)new_setpoint
) * (s64
)st
->scale0
) >> 12) + st
->offset0
;
360 DBG("wf_smu: scaled setpoint: %d RPM\n", (int)scaled
);
362 cputarget
= wf_smu_cpu_fans
? wf_smu_cpu_fans
->pid
.target
: 0;
363 cputarget
= ((((s64
)cputarget
) * (s64
)st
->scale1
) >> 12) + st
->offset1
;
364 scaled
= max(scaled
, cputarget
);
365 scaled
= max(scaled
, st
->pid
.param
.min
);
366 scaled
= min(scaled
, st
->pid
.param
.max
);
368 DBG("wf_smu: adjusted setpoint: %d RPM\n", (int)scaled
);
370 if (st
->sys_setpoint
== scaled
&& new_setpoint
== st
->hd_setpoint
)
372 st
->sys_setpoint
= scaled
;
373 st
->hd_setpoint
= new_setpoint
;
375 if (fan_system
&& wf_smu_failure_state
== 0) {
376 rc
= fan_system
->ops
->set_value(fan_system
, st
->sys_setpoint
);
378 printk(KERN_WARNING
"windfarm: Sys fan error %d\n",
380 wf_smu_failure_state
|= FAILURE_FAN
;
383 if (fan_hd
&& wf_smu_failure_state
== 0) {
384 rc
= fan_hd
->ops
->set_value(fan_hd
, st
->hd_setpoint
);
386 printk(KERN_WARNING
"windfarm: HD fan error %d\n",
388 wf_smu_failure_state
|= FAILURE_FAN
;
393 static void wf_smu_create_cpu_fans(void)
395 struct wf_cpu_pid_param pid_param
;
396 const struct smu_sdbp_header
*hdr
;
397 struct smu_sdbp_cpupiddata
*piddata
;
398 struct smu_sdbp_fvt
*fvt
;
399 s32 tmax
, tdelta
, maxpow
, powadj
;
401 /* First, locate the PID params in SMU SBD */
402 hdr
= smu_get_sdb_partition(SMU_SDB_CPUPIDDATA_ID
, NULL
);
404 printk(KERN_WARNING
"windfarm: CPU PID fan config not found "
408 piddata
= (struct smu_sdbp_cpupiddata
*)&hdr
[1];
410 /* Get the FVT params for operating point 0 (the only supported one
411 * for now) in order to get tmax
413 hdr
= smu_get_sdb_partition(SMU_SDB_FVT_ID
, NULL
);
415 fvt
= (struct smu_sdbp_fvt
*)&hdr
[1];
416 tmax
= ((s32
)fvt
->maxtemp
) << 16;
418 tmax
= 0x5e0000; /* 94 degree default */
420 /* Alloc & initialize state */
421 wf_smu_cpu_fans
= kmalloc(sizeof(struct wf_smu_cpu_fans_state
),
423 if (wf_smu_cpu_fans
== NULL
)
425 wf_smu_cpu_fans
->ticks
= 1;
427 wf_smu_cpu_fans
->scale
= WF_SMU_CPU_FANS_SIBLING_SCALE
;
428 wf_smu_cpu_fans
->offset
= WF_SMU_CPU_FANS_SIBLING_OFFSET
;
430 /* Fill PID params */
431 pid_param
.interval
= WF_SMU_CPU_FANS_INTERVAL
;
432 pid_param
.history_len
= piddata
->history_len
;
433 if (pid_param
.history_len
> WF_CPU_PID_MAX_HISTORY
) {
434 printk(KERN_WARNING
"windfarm: History size overflow on "
435 "CPU control loop (%d)\n", piddata
->history_len
);
436 pid_param
.history_len
= WF_CPU_PID_MAX_HISTORY
;
438 pid_param
.gd
= piddata
->gd
;
439 pid_param
.gp
= piddata
->gp
;
440 pid_param
.gr
= piddata
->gr
/ pid_param
.history_len
;
442 tdelta
= ((s32
)piddata
->target_temp_delta
) << 16;
443 maxpow
= ((s32
)piddata
->max_power
) << 16;
444 powadj
= ((s32
)piddata
->power_adj
) << 16;
446 pid_param
.tmax
= tmax
;
447 pid_param
.ttarget
= tmax
- tdelta
;
448 pid_param
.pmaxadj
= maxpow
- powadj
;
450 pid_param
.min
= fan_cpu_main
->ops
->get_min(fan_cpu_main
);
451 pid_param
.max
= fan_cpu_main
->ops
->get_max(fan_cpu_main
);
453 wf_cpu_pid_init(&wf_smu_cpu_fans
->pid
, &pid_param
);
455 DBG("wf: CPU Fan control initialized.\n");
456 DBG(" ttarged=%d.%03d, tmax=%d.%03d, min=%d RPM, max=%d RPM\n",
457 FIX32TOPRINT(pid_param
.ttarget
), FIX32TOPRINT(pid_param
.tmax
),
458 pid_param
.min
, pid_param
.max
);
463 printk(KERN_WARNING
"windfarm: CPU fan config not found\n"
464 "for this machine model, max fan speed\n");
467 wf_control_set_max(cpufreq_clamp
);
469 wf_control_set_max(fan_cpu_main
);
472 static void wf_smu_cpu_fans_tick(struct wf_smu_cpu_fans_state
*st
)
474 s32 new_setpoint
, temp
, power
, systarget
;
477 if (--st
->ticks
!= 0) {
482 st
->ticks
= WF_SMU_CPU_FANS_INTERVAL
;
484 rc
= sensor_cpu_temp
->ops
->get_value(sensor_cpu_temp
, &temp
);
486 printk(KERN_WARNING
"windfarm: CPU temp sensor error %d\n",
488 wf_smu_failure_state
|= FAILURE_SENSOR
;
492 rc
= sensor_cpu_power
->ops
->get_value(sensor_cpu_power
, &power
);
494 printk(KERN_WARNING
"windfarm: CPU power sensor error %d\n",
496 wf_smu_failure_state
|= FAILURE_SENSOR
;
500 DBG("wf_smu: CPU Fans tick ! CPU temp: %d.%03d, power: %d.%03d\n",
501 FIX32TOPRINT(temp
), FIX32TOPRINT(power
));
503 #ifdef HACKED_OVERTEMP
505 wf_smu_failure_state
|= FAILURE_OVERTEMP
;
507 if (temp
> st
->pid
.param
.tmax
)
508 wf_smu_failure_state
|= FAILURE_OVERTEMP
;
510 new_setpoint
= wf_cpu_pid_run(&st
->pid
, power
, temp
);
512 DBG("wf_smu: new_setpoint: %d RPM\n", (int)new_setpoint
);
514 systarget
= wf_smu_sys_fans
? wf_smu_sys_fans
->pid
.target
: 0;
515 systarget
= ((((s64
)systarget
) * (s64
)st
->scale
) >> 12)
517 new_setpoint
= max(new_setpoint
, systarget
);
518 new_setpoint
= max(new_setpoint
, st
->pid
.param
.min
);
519 new_setpoint
= min(new_setpoint
, st
->pid
.param
.max
);
521 DBG("wf_smu: adjusted setpoint: %d RPM\n", (int)new_setpoint
);
523 if (st
->cpu_setpoint
== new_setpoint
)
525 st
->cpu_setpoint
= new_setpoint
;
527 if (fan_cpu_main
&& wf_smu_failure_state
== 0) {
528 rc
= fan_cpu_main
->ops
->set_value(fan_cpu_main
,
531 printk(KERN_WARNING
"windfarm: CPU main fan"
533 wf_smu_failure_state
|= FAILURE_FAN
;
539 * ****** Setup / Init / Misc ... ******
543 static void wf_smu_tick(void)
545 unsigned int last_failure
= wf_smu_failure_state
;
546 unsigned int new_failure
;
548 if (!wf_smu_started
) {
549 DBG("wf: creating control loops !\n");
550 wf_smu_create_sys_fans();
551 wf_smu_create_cpu_fans();
556 if (wf_smu_skipping
&& --wf_smu_skipping
)
559 wf_smu_failure_state
= 0;
561 wf_smu_sys_fans_tick(wf_smu_sys_fans
);
563 wf_smu_cpu_fans_tick(wf_smu_cpu_fans
);
566 new_failure
= wf_smu_failure_state
& ~last_failure
;
568 /* If entering failure mode, clamp cpufreq and ramp all
569 * fans to full speed.
571 if (wf_smu_failure_state
&& !last_failure
) {
573 wf_control_set_max(cpufreq_clamp
);
575 wf_control_set_max(fan_system
);
577 wf_control_set_max(fan_cpu_main
);
579 wf_control_set_max(fan_hd
);
582 /* If leaving failure mode, unclamp cpufreq and readjust
583 * all fans on next iteration
585 if (!wf_smu_failure_state
&& last_failure
) {
587 wf_control_set_min(cpufreq_clamp
);
591 /* Overtemp condition detected, notify and start skipping a couple
592 * ticks to let the temperature go down
594 if (new_failure
& FAILURE_OVERTEMP
) {
599 /* We only clear the overtemp condition if overtemp is cleared
600 * _and_ no other failure is present. Since a sensor error will
601 * clear the overtemp condition (can't measure temperature) at
602 * the control loop levels, but we don't want to keep it clear
605 if (new_failure
== 0 && last_failure
& FAILURE_OVERTEMP
)
609 static void wf_smu_new_control(struct wf_control
*ct
)
611 if (wf_smu_all_controls_ok
)
614 if (fan_cpu_main
== NULL
&& !strcmp(ct
->name
, "cpu-fan")) {
615 if (wf_get_control(ct
) == 0)
619 if (fan_system
== NULL
&& !strcmp(ct
->name
, "system-fan")) {
620 if (wf_get_control(ct
) == 0)
624 if (cpufreq_clamp
== NULL
&& !strcmp(ct
->name
, "cpufreq-clamp")) {
625 if (wf_get_control(ct
) == 0)
629 /* Darwin property list says the HD fan is only for model ID
633 if (wf_smu_mach_model
> 3) {
634 if (fan_system
&& fan_cpu_main
&& cpufreq_clamp
)
635 wf_smu_all_controls_ok
= 1;
639 if (fan_hd
== NULL
&& !strcmp(ct
->name
, "drive-bay-fan")) {
640 if (wf_get_control(ct
) == 0)
644 if (fan_system
&& fan_hd
&& fan_cpu_main
&& cpufreq_clamp
)
645 wf_smu_all_controls_ok
= 1;
648 static void wf_smu_new_sensor(struct wf_sensor
*sr
)
650 if (wf_smu_all_sensors_ok
)
653 if (sensor_cpu_power
== NULL
&& !strcmp(sr
->name
, "cpu-power")) {
654 if (wf_get_sensor(sr
) == 0)
655 sensor_cpu_power
= sr
;
658 if (sensor_cpu_temp
== NULL
&& !strcmp(sr
->name
, "cpu-temp")) {
659 if (wf_get_sensor(sr
) == 0)
660 sensor_cpu_temp
= sr
;
663 if (sensor_hd_temp
== NULL
&& !strcmp(sr
->name
, "hd-temp")) {
664 if (wf_get_sensor(sr
) == 0)
668 if (sensor_cpu_power
&& sensor_cpu_temp
&& sensor_hd_temp
)
669 wf_smu_all_sensors_ok
= 1;
673 static int wf_smu_notify(struct notifier_block
*self
,
674 unsigned long event
, void *data
)
677 case WF_EVENT_NEW_CONTROL
:
678 DBG("wf: new control %s detected\n",
679 ((struct wf_control
*)data
)->name
);
680 wf_smu_new_control(data
);
683 case WF_EVENT_NEW_SENSOR
:
684 DBG("wf: new sensor %s detected\n",
685 ((struct wf_sensor
*)data
)->name
);
686 wf_smu_new_sensor(data
);
689 if (wf_smu_all_controls_ok
&& wf_smu_all_sensors_ok
)
696 static struct notifier_block wf_smu_events
= {
697 .notifier_call
= wf_smu_notify
,
700 static int wf_init_pm(void)
702 const struct smu_sdbp_header
*hdr
;
704 hdr
= smu_get_sdb_partition(SMU_SDB_SENSORTREE_ID
, NULL
);
706 struct smu_sdbp_sensortree
*st
=
707 (struct smu_sdbp_sensortree
*)&hdr
[1];
708 wf_smu_mach_model
= st
->model_id
;
711 printk(KERN_INFO
"windfarm: Initializing for iMacG5 model ID %d\n",
717 static int wf_smu_probe(struct platform_device
*ddev
)
719 wf_register_client(&wf_smu_events
);
724 static int __devexit
wf_smu_remove(struct platform_device
*ddev
)
726 wf_unregister_client(&wf_smu_events
);
728 /* XXX We don't have yet a guarantee that our callback isn't
729 * in progress when returning from wf_unregister_client, so
730 * we add an arbitrary delay. I'll have to fix that in the core
734 /* Release all sensors */
735 /* One more crappy race: I don't think we have any guarantee here
736 * that the attribute callback won't race with the sensor beeing
737 * disposed of, and I'm not 100% certain what best way to deal
738 * with that except by adding locks all over... I'll do that
739 * eventually but heh, who ever rmmod this module anyway ?
741 if (sensor_cpu_power
)
742 wf_put_sensor(sensor_cpu_power
);
744 wf_put_sensor(sensor_cpu_temp
);
746 wf_put_sensor(sensor_hd_temp
);
748 /* Release all controls */
750 wf_put_control(fan_cpu_main
);
752 wf_put_control(fan_hd
);
754 wf_put_control(fan_system
);
756 wf_put_control(cpufreq_clamp
);
758 /* Destroy control loops state structures */
759 kfree(wf_smu_sys_fans
);
760 kfree(wf_smu_cpu_fans
);
765 static struct platform_driver wf_smu_driver
= {
766 .probe
= wf_smu_probe
,
767 .remove
= __devexit_p(wf_smu_remove
),
770 .owner
= THIS_MODULE
,
775 static int __init
wf_smu_init(void)
779 if (of_machine_is_compatible("PowerMac8,1") ||
780 of_machine_is_compatible("PowerMac8,2"))
785 request_module("windfarm_smu_controls");
786 request_module("windfarm_smu_sensors");
787 request_module("windfarm_lm75_sensor");
788 request_module("windfarm_cpufreq_clamp");
791 platform_driver_register(&wf_smu_driver
);
797 static void __exit
wf_smu_exit(void)
800 platform_driver_unregister(&wf_smu_driver
);
804 module_init(wf_smu_init
);
805 module_exit(wf_smu_exit
);
807 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
808 MODULE_DESCRIPTION("Thermal control logic for iMac G5");
809 MODULE_LICENSE("GPL");
810 MODULE_ALIAS("platform:windfarm");