net: move procfs code to net/core/net-procfs.c
[linux/fpc-iii.git] / drivers / macintosh / windfarm_pm91.c
blob2eb484f213c84086655aae5adbedf9cebbb98cf0
1 /*
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:
16 * PowerMac9,1
17 * ===========
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>
41 #include <asm/prom.h>
42 #include <asm/machdep.h>
43 #include <asm/io.h>
44 #include <asm/sections.h>
45 #include <asm/smu.h>
47 #include "windfarm.h"
48 #include "windfarm_pid.h"
50 #define VERSION "0.4"
52 #undef DEBUG
54 #ifdef DEBUG
55 #define DBG(args...) printk(args)
56 #else
57 #define DBG(args...) do { } while(0)
58 #endif
60 /* define this to force CPU overtemp to 74 degree, useful for testing
61 * the overtemp code
63 #undef HACKED_OVERTEMP
65 /* Controls & sensors */
66 static struct wf_sensor *sensor_cpu_power;
67 static struct wf_sensor *sensor_cpu_temp;
68 static struct wf_sensor *sensor_hd_temp;
69 static struct wf_sensor *sensor_slots_power;
70 static struct wf_control *fan_cpu_main;
71 static struct wf_control *fan_cpu_second;
72 static struct wf_control *fan_cpu_third;
73 static struct wf_control *fan_hd;
74 static struct wf_control *fan_slots;
75 static struct wf_control *cpufreq_clamp;
77 /* Set to kick the control loop into life */
78 static int wf_smu_all_controls_ok, wf_smu_all_sensors_ok, wf_smu_started;
80 /* Failure handling.. could be nicer */
81 #define FAILURE_FAN 0x01
82 #define FAILURE_SENSOR 0x02
83 #define FAILURE_OVERTEMP 0x04
85 static unsigned int wf_smu_failure_state;
86 static int wf_smu_readjust, wf_smu_skipping;
89 * ****** CPU Fans Control Loop ******
94 #define WF_SMU_CPU_FANS_INTERVAL 1
95 #define WF_SMU_CPU_FANS_MAX_HISTORY 16
97 /* State data used by the cpu fans control loop
99 struct wf_smu_cpu_fans_state {
100 int ticks;
101 s32 cpu_setpoint;
102 struct wf_cpu_pid_state pid;
105 static struct wf_smu_cpu_fans_state *wf_smu_cpu_fans;
110 * ****** Drive Fan Control Loop ******
114 struct wf_smu_drive_fans_state {
115 int ticks;
116 s32 setpoint;
117 struct wf_pid_state pid;
120 static struct wf_smu_drive_fans_state *wf_smu_drive_fans;
123 * ****** Slots Fan Control Loop ******
127 struct wf_smu_slots_fans_state {
128 int ticks;
129 s32 setpoint;
130 struct wf_pid_state pid;
133 static struct wf_smu_slots_fans_state *wf_smu_slots_fans;
136 * ***** Implementation *****
141 static void wf_smu_create_cpu_fans(void)
143 struct wf_cpu_pid_param pid_param;
144 const struct smu_sdbp_header *hdr;
145 struct smu_sdbp_cpupiddata *piddata;
146 struct smu_sdbp_fvt *fvt;
147 s32 tmax, tdelta, maxpow, powadj;
149 /* First, locate the PID params in SMU SBD */
150 hdr = smu_get_sdb_partition(SMU_SDB_CPUPIDDATA_ID, NULL);
151 if (hdr == 0) {
152 printk(KERN_WARNING "windfarm: CPU PID fan config not found "
153 "max fan speed\n");
154 goto fail;
156 piddata = (struct smu_sdbp_cpupiddata *)&hdr[1];
158 /* Get the FVT params for operating point 0 (the only supported one
159 * for now) in order to get tmax
161 hdr = smu_get_sdb_partition(SMU_SDB_FVT_ID, NULL);
162 if (hdr) {
163 fvt = (struct smu_sdbp_fvt *)&hdr[1];
164 tmax = ((s32)fvt->maxtemp) << 16;
165 } else
166 tmax = 0x5e0000; /* 94 degree default */
168 /* Alloc & initialize state */
169 wf_smu_cpu_fans = kmalloc(sizeof(struct wf_smu_cpu_fans_state),
170 GFP_KERNEL);
171 if (wf_smu_cpu_fans == NULL)
172 goto fail;
173 wf_smu_cpu_fans->ticks = 1;
175 /* Fill PID params */
176 pid_param.interval = WF_SMU_CPU_FANS_INTERVAL;
177 pid_param.history_len = piddata->history_len;
178 if (pid_param.history_len > WF_CPU_PID_MAX_HISTORY) {
179 printk(KERN_WARNING "windfarm: History size overflow on "
180 "CPU control loop (%d)\n", piddata->history_len);
181 pid_param.history_len = WF_CPU_PID_MAX_HISTORY;
183 pid_param.gd = piddata->gd;
184 pid_param.gp = piddata->gp;
185 pid_param.gr = piddata->gr / pid_param.history_len;
187 tdelta = ((s32)piddata->target_temp_delta) << 16;
188 maxpow = ((s32)piddata->max_power) << 16;
189 powadj = ((s32)piddata->power_adj) << 16;
191 pid_param.tmax = tmax;
192 pid_param.ttarget = tmax - tdelta;
193 pid_param.pmaxadj = maxpow - powadj;
195 pid_param.min = wf_control_get_min(fan_cpu_main);
196 pid_param.max = wf_control_get_max(fan_cpu_main);
198 wf_cpu_pid_init(&wf_smu_cpu_fans->pid, &pid_param);
200 DBG("wf: CPU Fan control initialized.\n");
201 DBG(" ttarged=%d.%03d, tmax=%d.%03d, min=%d RPM, max=%d RPM\n",
202 FIX32TOPRINT(pid_param.ttarget), FIX32TOPRINT(pid_param.tmax),
203 pid_param.min, pid_param.max);
205 return;
207 fail:
208 printk(KERN_WARNING "windfarm: CPU fan config not found\n"
209 "for this machine model, max fan speed\n");
211 if (cpufreq_clamp)
212 wf_control_set_max(cpufreq_clamp);
213 if (fan_cpu_main)
214 wf_control_set_max(fan_cpu_main);
217 static void wf_smu_cpu_fans_tick(struct wf_smu_cpu_fans_state *st)
219 s32 new_setpoint, temp, power;
220 int rc;
222 if (--st->ticks != 0) {
223 if (wf_smu_readjust)
224 goto readjust;
225 return;
227 st->ticks = WF_SMU_CPU_FANS_INTERVAL;
229 rc = wf_sensor_get(sensor_cpu_temp, &temp);
230 if (rc) {
231 printk(KERN_WARNING "windfarm: CPU temp sensor error %d\n",
232 rc);
233 wf_smu_failure_state |= FAILURE_SENSOR;
234 return;
237 rc = wf_sensor_get(sensor_cpu_power, &power);
238 if (rc) {
239 printk(KERN_WARNING "windfarm: CPU power sensor error %d\n",
240 rc);
241 wf_smu_failure_state |= FAILURE_SENSOR;
242 return;
245 DBG("wf_smu: CPU Fans tick ! CPU temp: %d.%03d, power: %d.%03d\n",
246 FIX32TOPRINT(temp), FIX32TOPRINT(power));
248 #ifdef HACKED_OVERTEMP
249 if (temp > 0x4a0000)
250 wf_smu_failure_state |= FAILURE_OVERTEMP;
251 #else
252 if (temp > st->pid.param.tmax)
253 wf_smu_failure_state |= FAILURE_OVERTEMP;
254 #endif
255 new_setpoint = wf_cpu_pid_run(&st->pid, power, temp);
257 DBG("wf_smu: new_setpoint: %d RPM\n", (int)new_setpoint);
259 if (st->cpu_setpoint == new_setpoint)
260 return;
261 st->cpu_setpoint = new_setpoint;
262 readjust:
263 if (fan_cpu_main && wf_smu_failure_state == 0) {
264 rc = wf_control_set(fan_cpu_main, st->cpu_setpoint);
265 if (rc) {
266 printk(KERN_WARNING "windfarm: CPU main fan"
267 " error %d\n", rc);
268 wf_smu_failure_state |= FAILURE_FAN;
271 if (fan_cpu_second && wf_smu_failure_state == 0) {
272 rc = wf_control_set(fan_cpu_second, st->cpu_setpoint);
273 if (rc) {
274 printk(KERN_WARNING "windfarm: CPU second fan"
275 " error %d\n", rc);
276 wf_smu_failure_state |= FAILURE_FAN;
279 if (fan_cpu_third && wf_smu_failure_state == 0) {
280 rc = wf_control_set(fan_cpu_third, st->cpu_setpoint);
281 if (rc) {
282 printk(KERN_WARNING "windfarm: CPU third fan"
283 " error %d\n", rc);
284 wf_smu_failure_state |= FAILURE_FAN;
289 static void wf_smu_create_drive_fans(void)
291 struct wf_pid_param param = {
292 .interval = 5,
293 .history_len = 2,
294 .gd = 0x01e00000,
295 .gp = 0x00500000,
296 .gr = 0x00000000,
297 .itarget = 0x00200000,
300 /* Alloc & initialize state */
301 wf_smu_drive_fans = kmalloc(sizeof(struct wf_smu_drive_fans_state),
302 GFP_KERNEL);
303 if (wf_smu_drive_fans == NULL) {
304 printk(KERN_WARNING "windfarm: Memory allocation error"
305 " max fan speed\n");
306 goto fail;
308 wf_smu_drive_fans->ticks = 1;
310 /* Fill PID params */
311 param.additive = (fan_hd->type == WF_CONTROL_RPM_FAN);
312 param.min = wf_control_get_min(fan_hd);
313 param.max = wf_control_get_max(fan_hd);
314 wf_pid_init(&wf_smu_drive_fans->pid, &param);
316 DBG("wf: Drive Fan control initialized.\n");
317 DBG(" itarged=%d.%03d, min=%d RPM, max=%d RPM\n",
318 FIX32TOPRINT(param.itarget), param.min, param.max);
319 return;
321 fail:
322 if (fan_hd)
323 wf_control_set_max(fan_hd);
326 static void wf_smu_drive_fans_tick(struct wf_smu_drive_fans_state *st)
328 s32 new_setpoint, temp;
329 int rc;
331 if (--st->ticks != 0) {
332 if (wf_smu_readjust)
333 goto readjust;
334 return;
336 st->ticks = st->pid.param.interval;
338 rc = wf_sensor_get(sensor_hd_temp, &temp);
339 if (rc) {
340 printk(KERN_WARNING "windfarm: HD temp sensor error %d\n",
341 rc);
342 wf_smu_failure_state |= FAILURE_SENSOR;
343 return;
346 DBG("wf_smu: Drive Fans tick ! HD temp: %d.%03d\n",
347 FIX32TOPRINT(temp));
349 if (temp > (st->pid.param.itarget + 0x50000))
350 wf_smu_failure_state |= FAILURE_OVERTEMP;
352 new_setpoint = wf_pid_run(&st->pid, temp);
354 DBG("wf_smu: new_setpoint: %d\n", (int)new_setpoint);
356 if (st->setpoint == new_setpoint)
357 return;
358 st->setpoint = new_setpoint;
359 readjust:
360 if (fan_hd && wf_smu_failure_state == 0) {
361 rc = wf_control_set(fan_hd, st->setpoint);
362 if (rc) {
363 printk(KERN_WARNING "windfarm: HD fan error %d\n",
364 rc);
365 wf_smu_failure_state |= FAILURE_FAN;
370 static void wf_smu_create_slots_fans(void)
372 struct wf_pid_param param = {
373 .interval = 1,
374 .history_len = 8,
375 .gd = 0x00000000,
376 .gp = 0x00000000,
377 .gr = 0x00020000,
378 .itarget = 0x00000000
381 /* Alloc & initialize state */
382 wf_smu_slots_fans = kmalloc(sizeof(struct wf_smu_slots_fans_state),
383 GFP_KERNEL);
384 if (wf_smu_slots_fans == NULL) {
385 printk(KERN_WARNING "windfarm: Memory allocation error"
386 " max fan speed\n");
387 goto fail;
389 wf_smu_slots_fans->ticks = 1;
391 /* Fill PID params */
392 param.additive = (fan_slots->type == WF_CONTROL_RPM_FAN);
393 param.min = wf_control_get_min(fan_slots);
394 param.max = wf_control_get_max(fan_slots);
395 wf_pid_init(&wf_smu_slots_fans->pid, &param);
397 DBG("wf: Slots Fan control initialized.\n");
398 DBG(" itarged=%d.%03d, min=%d RPM, max=%d RPM\n",
399 FIX32TOPRINT(param.itarget), param.min, param.max);
400 return;
402 fail:
403 if (fan_slots)
404 wf_control_set_max(fan_slots);
407 static void wf_smu_slots_fans_tick(struct wf_smu_slots_fans_state *st)
409 s32 new_setpoint, power;
410 int rc;
412 if (--st->ticks != 0) {
413 if (wf_smu_readjust)
414 goto readjust;
415 return;
417 st->ticks = st->pid.param.interval;
419 rc = wf_sensor_get(sensor_slots_power, &power);
420 if (rc) {
421 printk(KERN_WARNING "windfarm: Slots power sensor error %d\n",
422 rc);
423 wf_smu_failure_state |= FAILURE_SENSOR;
424 return;
427 DBG("wf_smu: Slots Fans tick ! Slots power: %d.%03d\n",
428 FIX32TOPRINT(power));
430 #if 0 /* Check what makes a good overtemp condition */
431 if (power > (st->pid.param.itarget + 0x50000))
432 wf_smu_failure_state |= FAILURE_OVERTEMP;
433 #endif
435 new_setpoint = wf_pid_run(&st->pid, power);
437 DBG("wf_smu: new_setpoint: %d\n", (int)new_setpoint);
439 if (st->setpoint == new_setpoint)
440 return;
441 st->setpoint = new_setpoint;
442 readjust:
443 if (fan_slots && wf_smu_failure_state == 0) {
444 rc = wf_control_set(fan_slots, st->setpoint);
445 if (rc) {
446 printk(KERN_WARNING "windfarm: Slots fan error %d\n",
447 rc);
448 wf_smu_failure_state |= FAILURE_FAN;
455 * ****** Setup / Init / Misc ... ******
459 static void wf_smu_tick(void)
461 unsigned int last_failure = wf_smu_failure_state;
462 unsigned int new_failure;
464 if (!wf_smu_started) {
465 DBG("wf: creating control loops !\n");
466 wf_smu_create_drive_fans();
467 wf_smu_create_slots_fans();
468 wf_smu_create_cpu_fans();
469 wf_smu_started = 1;
472 /* Skipping ticks */
473 if (wf_smu_skipping && --wf_smu_skipping)
474 return;
476 wf_smu_failure_state = 0;
477 if (wf_smu_drive_fans)
478 wf_smu_drive_fans_tick(wf_smu_drive_fans);
479 if (wf_smu_slots_fans)
480 wf_smu_slots_fans_tick(wf_smu_slots_fans);
481 if (wf_smu_cpu_fans)
482 wf_smu_cpu_fans_tick(wf_smu_cpu_fans);
484 wf_smu_readjust = 0;
485 new_failure = wf_smu_failure_state & ~last_failure;
487 /* If entering failure mode, clamp cpufreq and ramp all
488 * fans to full speed.
490 if (wf_smu_failure_state && !last_failure) {
491 if (cpufreq_clamp)
492 wf_control_set_max(cpufreq_clamp);
493 if (fan_cpu_main)
494 wf_control_set_max(fan_cpu_main);
495 if (fan_cpu_second)
496 wf_control_set_max(fan_cpu_second);
497 if (fan_cpu_third)
498 wf_control_set_max(fan_cpu_third);
499 if (fan_hd)
500 wf_control_set_max(fan_hd);
501 if (fan_slots)
502 wf_control_set_max(fan_slots);
505 /* If leaving failure mode, unclamp cpufreq and readjust
506 * all fans on next iteration
508 if (!wf_smu_failure_state && last_failure) {
509 if (cpufreq_clamp)
510 wf_control_set_min(cpufreq_clamp);
511 wf_smu_readjust = 1;
514 /* Overtemp condition detected, notify and start skipping a couple
515 * ticks to let the temperature go down
517 if (new_failure & FAILURE_OVERTEMP) {
518 wf_set_overtemp();
519 wf_smu_skipping = 2;
522 /* We only clear the overtemp condition if overtemp is cleared
523 * _and_ no other failure is present. Since a sensor error will
524 * clear the overtemp condition (can't measure temperature) at
525 * the control loop levels, but we don't want to keep it clear
526 * here in this case
528 if (new_failure == 0 && last_failure & FAILURE_OVERTEMP)
529 wf_clear_overtemp();
533 static void wf_smu_new_control(struct wf_control *ct)
535 if (wf_smu_all_controls_ok)
536 return;
538 if (fan_cpu_main == NULL && !strcmp(ct->name, "cpu-rear-fan-0")) {
539 if (wf_get_control(ct) == 0)
540 fan_cpu_main = ct;
543 if (fan_cpu_second == NULL && !strcmp(ct->name, "cpu-rear-fan-1")) {
544 if (wf_get_control(ct) == 0)
545 fan_cpu_second = ct;
548 if (fan_cpu_third == NULL && !strcmp(ct->name, "cpu-front-fan-0")) {
549 if (wf_get_control(ct) == 0)
550 fan_cpu_third = ct;
553 if (cpufreq_clamp == NULL && !strcmp(ct->name, "cpufreq-clamp")) {
554 if (wf_get_control(ct) == 0)
555 cpufreq_clamp = ct;
558 if (fan_hd == NULL && !strcmp(ct->name, "drive-bay-fan")) {
559 if (wf_get_control(ct) == 0)
560 fan_hd = ct;
563 if (fan_slots == NULL && !strcmp(ct->name, "slots-fan")) {
564 if (wf_get_control(ct) == 0)
565 fan_slots = ct;
568 if (fan_cpu_main && (fan_cpu_second || fan_cpu_third) && fan_hd &&
569 fan_slots && cpufreq_clamp)
570 wf_smu_all_controls_ok = 1;
573 static void wf_smu_new_sensor(struct wf_sensor *sr)
575 if (wf_smu_all_sensors_ok)
576 return;
578 if (sensor_cpu_power == NULL && !strcmp(sr->name, "cpu-power")) {
579 if (wf_get_sensor(sr) == 0)
580 sensor_cpu_power = sr;
583 if (sensor_cpu_temp == NULL && !strcmp(sr->name, "cpu-temp")) {
584 if (wf_get_sensor(sr) == 0)
585 sensor_cpu_temp = sr;
588 if (sensor_hd_temp == NULL && !strcmp(sr->name, "hd-temp")) {
589 if (wf_get_sensor(sr) == 0)
590 sensor_hd_temp = sr;
593 if (sensor_slots_power == NULL && !strcmp(sr->name, "slots-power")) {
594 if (wf_get_sensor(sr) == 0)
595 sensor_slots_power = sr;
598 if (sensor_cpu_power && sensor_cpu_temp &&
599 sensor_hd_temp && sensor_slots_power)
600 wf_smu_all_sensors_ok = 1;
604 static int wf_smu_notify(struct notifier_block *self,
605 unsigned long event, void *data)
607 switch(event) {
608 case WF_EVENT_NEW_CONTROL:
609 DBG("wf: new control %s detected\n",
610 ((struct wf_control *)data)->name);
611 wf_smu_new_control(data);
612 wf_smu_readjust = 1;
613 break;
614 case WF_EVENT_NEW_SENSOR:
615 DBG("wf: new sensor %s detected\n",
616 ((struct wf_sensor *)data)->name);
617 wf_smu_new_sensor(data);
618 break;
619 case WF_EVENT_TICK:
620 if (wf_smu_all_controls_ok && wf_smu_all_sensors_ok)
621 wf_smu_tick();
624 return 0;
627 static struct notifier_block wf_smu_events = {
628 .notifier_call = wf_smu_notify,
631 static int wf_init_pm(void)
633 printk(KERN_INFO "windfarm: Initializing for Desktop G5 model\n");
635 return 0;
638 static int wf_smu_probe(struct platform_device *ddev)
640 wf_register_client(&wf_smu_events);
642 return 0;
645 static int wf_smu_remove(struct platform_device *ddev)
647 wf_unregister_client(&wf_smu_events);
649 /* XXX We don't have yet a guarantee that our callback isn't
650 * in progress when returning from wf_unregister_client, so
651 * we add an arbitrary delay. I'll have to fix that in the core
653 msleep(1000);
655 /* Release all sensors */
656 /* One more crappy race: I don't think we have any guarantee here
657 * that the attribute callback won't race with the sensor beeing
658 * disposed of, and I'm not 100% certain what best way to deal
659 * with that except by adding locks all over... I'll do that
660 * eventually but heh, who ever rmmod this module anyway ?
662 if (sensor_cpu_power)
663 wf_put_sensor(sensor_cpu_power);
664 if (sensor_cpu_temp)
665 wf_put_sensor(sensor_cpu_temp);
666 if (sensor_hd_temp)
667 wf_put_sensor(sensor_hd_temp);
668 if (sensor_slots_power)
669 wf_put_sensor(sensor_slots_power);
671 /* Release all controls */
672 if (fan_cpu_main)
673 wf_put_control(fan_cpu_main);
674 if (fan_cpu_second)
675 wf_put_control(fan_cpu_second);
676 if (fan_cpu_third)
677 wf_put_control(fan_cpu_third);
678 if (fan_hd)
679 wf_put_control(fan_hd);
680 if (fan_slots)
681 wf_put_control(fan_slots);
682 if (cpufreq_clamp)
683 wf_put_control(cpufreq_clamp);
685 /* Destroy control loops state structures */
686 kfree(wf_smu_slots_fans);
687 kfree(wf_smu_drive_fans);
688 kfree(wf_smu_cpu_fans);
690 return 0;
693 static struct platform_driver wf_smu_driver = {
694 .probe = wf_smu_probe,
695 .remove = wf_smu_remove,
696 .driver = {
697 .name = "windfarm",
698 .owner = THIS_MODULE,
703 static int __init wf_smu_init(void)
705 int rc = -ENODEV;
707 if (of_machine_is_compatible("PowerMac9,1"))
708 rc = wf_init_pm();
710 if (rc == 0) {
711 #ifdef MODULE
712 request_module("windfarm_smu_controls");
713 request_module("windfarm_smu_sensors");
714 request_module("windfarm_lm75_sensor");
715 request_module("windfarm_cpufreq_clamp");
717 #endif /* MODULE */
718 platform_driver_register(&wf_smu_driver);
721 return rc;
724 static void __exit wf_smu_exit(void)
727 platform_driver_unregister(&wf_smu_driver);
731 module_init(wf_smu_init);
732 module_exit(wf_smu_exit);
734 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
735 MODULE_DESCRIPTION("Thermal control logic for PowerMac9,1");
736 MODULE_LICENSE("GPL");
738 MODULE_ALIAS("platform:windfarm");