1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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/system.h>
45#include <asm/sections.h>
46#include <asm/smu.h>
47
48#include "windfarm.h"
49#include "windfarm_pid.h"
50
51#define VERSION "0.4"
52
53#undef DEBUG
54
55#ifdef DEBUG
56#define DBG(args...) printk(args)
57#else
58#define DBG(args...) do { } while(0)
59#endif
60
61
62
63
64#undef HACKED_OVERTEMP
65
66
67static struct wf_sensor *sensor_cpu_power;
68static struct wf_sensor *sensor_cpu_temp;
69static struct wf_sensor *sensor_hd_temp;
70static struct wf_sensor *sensor_slots_power;
71static struct wf_control *fan_cpu_main;
72static struct wf_control *fan_cpu_second;
73static struct wf_control *fan_cpu_third;
74static struct wf_control *fan_hd;
75static struct wf_control *fan_slots;
76static struct wf_control *cpufreq_clamp;
77
78
79static int wf_smu_all_controls_ok, wf_smu_all_sensors_ok, wf_smu_started;
80
81
82#define FAILURE_FAN 0x01
83#define FAILURE_SENSOR 0x02
84#define FAILURE_OVERTEMP 0x04
85
86static unsigned int wf_smu_failure_state;
87static int wf_smu_readjust, wf_smu_skipping;
88
89
90
91
92
93
94
95#define WF_SMU_CPU_FANS_INTERVAL 1
96#define WF_SMU_CPU_FANS_MAX_HISTORY 16
97
98
99
100struct wf_smu_cpu_fans_state {
101 int ticks;
102 s32 cpu_setpoint;
103 struct wf_cpu_pid_state pid;
104};
105
106static struct wf_smu_cpu_fans_state *wf_smu_cpu_fans;
107
108
109
110
111
112
113
114
115struct wf_smu_drive_fans_state {
116 int ticks;
117 s32 setpoint;
118 struct wf_pid_state pid;
119};
120
121static struct wf_smu_drive_fans_state *wf_smu_drive_fans;
122
123
124
125
126
127
128struct wf_smu_slots_fans_state {
129 int ticks;
130 s32 setpoint;
131 struct wf_pid_state pid;
132};
133
134static struct wf_smu_slots_fans_state *wf_smu_slots_fans;
135
136
137
138
139
140
141
142static void wf_smu_create_cpu_fans(void)
143{
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;
149
150
151 hdr = smu_get_sdb_partition(SMU_SDB_CPUPIDDATA_ID, NULL);
152 if (hdr == 0) {
153 printk(KERN_WARNING "windfarm: CPU PID fan config not found "
154 "max fan speed\n");
155 goto fail;
156 }
157 piddata = (struct smu_sdbp_cpupiddata *)&hdr[1];
158
159
160
161
162 hdr = smu_get_sdb_partition(SMU_SDB_FVT_ID, NULL);
163 if (hdr) {
164 fvt = (struct smu_sdbp_fvt *)&hdr[1];
165 tmax = ((s32)fvt->maxtemp) << 16;
166 } else
167 tmax = 0x5e0000;
168
169
170 wf_smu_cpu_fans = kmalloc(sizeof(struct wf_smu_cpu_fans_state),
171 GFP_KERNEL);
172 if (wf_smu_cpu_fans == NULL)
173 goto fail;
174 wf_smu_cpu_fans->ticks = 1;
175
176
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;
183 }
184 pid_param.gd = piddata->gd;
185 pid_param.gp = piddata->gp;
186 pid_param.gr = piddata->gr / pid_param.history_len;
187
188 tdelta = ((s32)piddata->target_temp_delta) << 16;
189 maxpow = ((s32)piddata->max_power) << 16;
190 powadj = ((s32)piddata->power_adj) << 16;
191
192 pid_param.tmax = tmax;
193 pid_param.ttarget = tmax - tdelta;
194 pid_param.pmaxadj = maxpow - powadj;
195
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);
198
199 wf_cpu_pid_init(&wf_smu_cpu_fans->pid, &pid_param);
200
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);
205
206 return;
207
208 fail:
209 printk(KERN_WARNING "windfarm: CPU fan config not found\n"
210 "for this machine model, max fan speed\n");
211
212 if (cpufreq_clamp)
213 wf_control_set_max(cpufreq_clamp);
214 if (fan_cpu_main)
215 wf_control_set_max(fan_cpu_main);
216}
217
218static void wf_smu_cpu_fans_tick(struct wf_smu_cpu_fans_state *st)
219{
220 s32 new_setpoint, temp, power;
221 int rc;
222
223 if (--st->ticks != 0) {
224 if (wf_smu_readjust)
225 goto readjust;
226 return;
227 }
228 st->ticks = WF_SMU_CPU_FANS_INTERVAL;
229
230 rc = sensor_cpu_temp->ops->get_value(sensor_cpu_temp, &temp);
231 if (rc) {
232 printk(KERN_WARNING "windfarm: CPU temp sensor error %d\n",
233 rc);
234 wf_smu_failure_state |= FAILURE_SENSOR;
235 return;
236 }
237
238 rc = sensor_cpu_power->ops->get_value(sensor_cpu_power, &power);
239 if (rc) {
240 printk(KERN_WARNING "windfarm: CPU power sensor error %d\n",
241 rc);
242 wf_smu_failure_state |= FAILURE_SENSOR;
243 return;
244 }
245
246 DBG("wf_smu: CPU Fans tick ! CPU temp: %d.%03d, power: %d.%03d\n",
247 FIX32TOPRINT(temp), FIX32TOPRINT(power));
248
249#ifdef HACKED_OVERTEMP
250 if (temp > 0x4a0000)
251 wf_smu_failure_state |= FAILURE_OVERTEMP;
252#else
253 if (temp > st->pid.param.tmax)
254 wf_smu_failure_state |= FAILURE_OVERTEMP;
255#endif
256 new_setpoint = wf_cpu_pid_run(&st->pid, power, temp);
257
258 DBG("wf_smu: new_setpoint: %d RPM\n", (int)new_setpoint);
259
260 if (st->cpu_setpoint == new_setpoint)
261 return;
262 st->cpu_setpoint = new_setpoint;
263 readjust:
264 if (fan_cpu_main && wf_smu_failure_state == 0) {
265 rc = fan_cpu_main->ops->set_value(fan_cpu_main,
266 st->cpu_setpoint);
267 if (rc) {
268 printk(KERN_WARNING "windfarm: CPU main fan"
269 " error %d\n", rc);
270 wf_smu_failure_state |= FAILURE_FAN;
271 }
272 }
273 if (fan_cpu_second && wf_smu_failure_state == 0) {
274 rc = fan_cpu_second->ops->set_value(fan_cpu_second,
275 st->cpu_setpoint);
276 if (rc) {
277 printk(KERN_WARNING "windfarm: CPU second fan"
278 " error %d\n", rc);
279 wf_smu_failure_state |= FAILURE_FAN;
280 }
281 }
282 if (fan_cpu_third && wf_smu_failure_state == 0) {
283 rc = fan_cpu_main->ops->set_value(fan_cpu_third,
284 st->cpu_setpoint);
285 if (rc) {
286 printk(KERN_WARNING "windfarm: CPU third fan"
287 " error %d\n", rc);
288 wf_smu_failure_state |= FAILURE_FAN;
289 }
290 }
291}
292
293static void wf_smu_create_drive_fans(void)
294{
295 struct wf_pid_param param = {
296 .interval = 5,
297 .history_len = 2,
298 .gd = 0x01e00000,
299 .gp = 0x00500000,
300 .gr = 0x00000000,
301 .itarget = 0x00200000,
302 };
303
304
305 wf_smu_drive_fans = kmalloc(sizeof(struct wf_smu_drive_fans_state),
306 GFP_KERNEL);
307 if (wf_smu_drive_fans == NULL) {
308 printk(KERN_WARNING "windfarm: Memory allocation error"
309 " max fan speed\n");
310 goto fail;
311 }
312 wf_smu_drive_fans->ticks = 1;
313
314
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);
319
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);
323 return;
324
325 fail:
326 if (fan_hd)
327 wf_control_set_max(fan_hd);
328}
329
330static void wf_smu_drive_fans_tick(struct wf_smu_drive_fans_state *st)
331{
332 s32 new_setpoint, temp;
333 int rc;
334
335 if (--st->ticks != 0) {
336 if (wf_smu_readjust)
337 goto readjust;
338 return;
339 }
340 st->ticks = st->pid.param.interval;
341
342 rc = sensor_hd_temp->ops->get_value(sensor_hd_temp, &temp);
343 if (rc) {
344 printk(KERN_WARNING "windfarm: HD temp sensor error %d\n",
345 rc);
346 wf_smu_failure_state |= FAILURE_SENSOR;
347 return;
348 }
349
350 DBG("wf_smu: Drive Fans tick ! HD temp: %d.%03d\n",
351 FIX32TOPRINT(temp));
352
353 if (temp > (st->pid.param.itarget + 0x50000))
354 wf_smu_failure_state |= FAILURE_OVERTEMP;
355
356 new_setpoint = wf_pid_run(&st->pid, temp);
357
358 DBG("wf_smu: new_setpoint: %d\n", (int)new_setpoint);
359
360 if (st->setpoint == new_setpoint)
361 return;
362 st->setpoint = new_setpoint;
363 readjust:
364 if (fan_hd && wf_smu_failure_state == 0) {
365 rc = fan_hd->ops->set_value(fan_hd, st->setpoint);
366 if (rc) {
367 printk(KERN_WARNING "windfarm: HD fan error %d\n",
368 rc);
369 wf_smu_failure_state |= FAILURE_FAN;
370 }
371 }
372}
373
374static void wf_smu_create_slots_fans(void)
375{
376 struct wf_pid_param param = {
377 .interval = 1,
378 .history_len = 8,
379 .gd = 0x00000000,
380 .gp = 0x00000000,
381 .gr = 0x00020000,
382 .itarget = 0x00000000
383 };
384
385
386 wf_smu_slots_fans = kmalloc(sizeof(struct wf_smu_slots_fans_state),
387 GFP_KERNEL);
388 if (wf_smu_slots_fans == NULL) {
389 printk(KERN_WARNING "windfarm: Memory allocation error"
390 " max fan speed\n");
391 goto fail;
392 }
393 wf_smu_slots_fans->ticks = 1;
394
395
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);
400
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);
404 return;
405
406 fail:
407 if (fan_slots)
408 wf_control_set_max(fan_slots);
409}
410
411static void wf_smu_slots_fans_tick(struct wf_smu_slots_fans_state *st)
412{
413 s32 new_setpoint, power;
414 int rc;
415
416 if (--st->ticks != 0) {
417 if (wf_smu_readjust)
418 goto readjust;
419 return;
420 }
421 st->ticks = st->pid.param.interval;
422
423 rc = sensor_slots_power->ops->get_value(sensor_slots_power, &power);
424 if (rc) {
425 printk(KERN_WARNING "windfarm: Slots power sensor error %d\n",
426 rc);
427 wf_smu_failure_state |= FAILURE_SENSOR;
428 return;
429 }
430
431 DBG("wf_smu: Slots Fans tick ! Slots power: %d.%03d\n",
432 FIX32TOPRINT(power));
433
434#if 0
435 if (power > (st->pid.param.itarget + 0x50000))
436 wf_smu_failure_state |= FAILURE_OVERTEMP;
437#endif
438
439 new_setpoint = wf_pid_run(&st->pid, power);
440
441 DBG("wf_smu: new_setpoint: %d\n", (int)new_setpoint);
442
443 if (st->setpoint == new_setpoint)
444 return;
445 st->setpoint = new_setpoint;
446 readjust:
447 if (fan_slots && wf_smu_failure_state == 0) {
448 rc = fan_slots->ops->set_value(fan_slots, st->setpoint);
449 if (rc) {
450 printk(KERN_WARNING "windfarm: Slots fan error %d\n",
451 rc);
452 wf_smu_failure_state |= FAILURE_FAN;
453 }
454 }
455}
456
457
458
459
460
461
462
463static void wf_smu_tick(void)
464{
465 unsigned int last_failure = wf_smu_failure_state;
466 unsigned int new_failure;
467
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();
473 wf_smu_started = 1;
474 }
475
476
477 if (wf_smu_skipping && --wf_smu_skipping)
478 return;
479
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);
485 if (wf_smu_cpu_fans)
486 wf_smu_cpu_fans_tick(wf_smu_cpu_fans);
487
488 wf_smu_readjust = 0;
489 new_failure = wf_smu_failure_state & ~last_failure;
490
491
492
493
494 if (wf_smu_failure_state && !last_failure) {
495 if (cpufreq_clamp)
496 wf_control_set_max(cpufreq_clamp);
497 if (fan_cpu_main)
498 wf_control_set_max(fan_cpu_main);
499 if (fan_cpu_second)
500 wf_control_set_max(fan_cpu_second);
501 if (fan_cpu_third)
502 wf_control_set_max(fan_cpu_third);
503 if (fan_hd)
504 wf_control_set_max(fan_hd);
505 if (fan_slots)
506 wf_control_set_max(fan_slots);
507 }
508
509
510
511
512 if (!wf_smu_failure_state && last_failure) {
513 if (cpufreq_clamp)
514 wf_control_set_min(cpufreq_clamp);
515 wf_smu_readjust = 1;
516 }
517
518
519
520
521 if (new_failure & FAILURE_OVERTEMP) {
522 wf_set_overtemp();
523 wf_smu_skipping = 2;
524 }
525
526
527
528
529
530
531
532 if (new_failure == 0 && last_failure & FAILURE_OVERTEMP)
533 wf_clear_overtemp();
534}
535
536
537static void wf_smu_new_control(struct wf_control *ct)
538{
539 if (wf_smu_all_controls_ok)
540 return;
541
542 if (fan_cpu_main == NULL && !strcmp(ct->name, "cpu-rear-fan-0")) {
543 if (wf_get_control(ct) == 0)
544 fan_cpu_main = ct;
545 }
546
547 if (fan_cpu_second == NULL && !strcmp(ct->name, "cpu-rear-fan-1")) {
548 if (wf_get_control(ct) == 0)
549 fan_cpu_second = ct;
550 }
551
552 if (fan_cpu_third == NULL && !strcmp(ct->name, "cpu-front-fan-0")) {
553 if (wf_get_control(ct) == 0)
554 fan_cpu_third = ct;
555 }
556
557 if (cpufreq_clamp == NULL && !strcmp(ct->name, "cpufreq-clamp")) {
558 if (wf_get_control(ct) == 0)
559 cpufreq_clamp = ct;
560 }
561
562 if (fan_hd == NULL && !strcmp(ct->name, "drive-bay-fan")) {
563 if (wf_get_control(ct) == 0)
564 fan_hd = ct;
565 }
566
567 if (fan_slots == NULL && !strcmp(ct->name, "slots-fan")) {
568 if (wf_get_control(ct) == 0)
569 fan_slots = ct;
570 }
571
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;
575}
576
577static void wf_smu_new_sensor(struct wf_sensor *sr)
578{
579 if (wf_smu_all_sensors_ok)
580 return;
581
582 if (sensor_cpu_power == NULL && !strcmp(sr->name, "cpu-power")) {
583 if (wf_get_sensor(sr) == 0)
584 sensor_cpu_power = sr;
585 }
586
587 if (sensor_cpu_temp == NULL && !strcmp(sr->name, "cpu-temp")) {
588 if (wf_get_sensor(sr) == 0)
589 sensor_cpu_temp = sr;
590 }
591
592 if (sensor_hd_temp == NULL && !strcmp(sr->name, "hd-temp")) {
593 if (wf_get_sensor(sr) == 0)
594 sensor_hd_temp = sr;
595 }
596
597 if (sensor_slots_power == NULL && !strcmp(sr->name, "slots-power")) {
598 if (wf_get_sensor(sr) == 0)
599 sensor_slots_power = sr;
600 }
601
602 if (sensor_cpu_power && sensor_cpu_temp &&
603 sensor_hd_temp && sensor_slots_power)
604 wf_smu_all_sensors_ok = 1;
605}
606
607
608static int wf_smu_notify(struct notifier_block *self,
609 unsigned long event, void *data)
610{
611 switch(event) {
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);
616 wf_smu_readjust = 1;
617 break;
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);
622 break;
623 case WF_EVENT_TICK:
624 if (wf_smu_all_controls_ok && wf_smu_all_sensors_ok)
625 wf_smu_tick();
626 }
627
628 return 0;
629}
630
631static struct notifier_block wf_smu_events = {
632 .notifier_call = wf_smu_notify,
633};
634
635static int wf_init_pm(void)
636{
637 printk(KERN_INFO "windfarm: Initializing for Desktop G5 model\n");
638
639 return 0;
640}
641
642static int wf_smu_probe(struct platform_device *ddev)
643{
644 wf_register_client(&wf_smu_events);
645
646 return 0;
647}
648
649static int __devexit wf_smu_remove(struct platform_device *ddev)
650{
651 wf_unregister_client(&wf_smu_events);
652
653
654
655
656
657 msleep(1000);
658
659
660
661
662
663
664
665
666 if (sensor_cpu_power)
667 wf_put_sensor(sensor_cpu_power);
668 if (sensor_cpu_temp)
669 wf_put_sensor(sensor_cpu_temp);
670 if (sensor_hd_temp)
671 wf_put_sensor(sensor_hd_temp);
672 if (sensor_slots_power)
673 wf_put_sensor(sensor_slots_power);
674
675
676 if (fan_cpu_main)
677 wf_put_control(fan_cpu_main);
678 if (fan_cpu_second)
679 wf_put_control(fan_cpu_second);
680 if (fan_cpu_third)
681 wf_put_control(fan_cpu_third);
682 if (fan_hd)
683 wf_put_control(fan_hd);
684 if (fan_slots)
685 wf_put_control(fan_slots);
686 if (cpufreq_clamp)
687 wf_put_control(cpufreq_clamp);
688
689
690 kfree(wf_smu_slots_fans);
691 kfree(wf_smu_drive_fans);
692 kfree(wf_smu_cpu_fans);
693
694 return 0;
695}
696
697static struct platform_driver wf_smu_driver = {
698 .probe = wf_smu_probe,
699 .remove = __devexit_p(wf_smu_remove),
700 .driver = {
701 .name = "windfarm",
702 .owner = THIS_MODULE,
703 },
704};
705
706
707static int __init wf_smu_init(void)
708{
709 int rc = -ENODEV;
710
711 if (of_machine_is_compatible("PowerMac9,1"))
712 rc = wf_init_pm();
713
714 if (rc == 0) {
715#ifdef MODULE
716 request_module("windfarm_smu_controls");
717 request_module("windfarm_smu_sensors");
718 request_module("windfarm_lm75_sensor");
719 request_module("windfarm_cpufreq_clamp");
720
721#endif
722 platform_driver_register(&wf_smu_driver);
723 }
724
725 return rc;
726}
727
728static void __exit wf_smu_exit(void)
729{
730
731 platform_driver_unregister(&wf_smu_driver);
732}
733
734
735module_init(wf_smu_init);
736module_exit(wf_smu_exit);
737
738MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
739MODULE_DESCRIPTION("Thermal control logic for PowerMac9,1");
740MODULE_LICENSE("GPL");
741
742MODULE_ALIAS("platform:windfarm");
743