powerpc: Fix G5 thermal shutdown
[deliverable/linux.git] / drivers / macintosh / therm_pm72.c
1 /*
2 * Device driver for the thermostats & fan controller of the
3 * Apple G5 "PowerMac7,2" desktop machines.
4 *
5 * (c) Copyright IBM Corp. 2003-2004
6 *
7 * Maintained by: Benjamin Herrenschmidt
8 * <benh@kernel.crashing.org>
9 *
10 *
11 * The algorithm used is the PID control algorithm, used the same
12 * way the published Darwin code does, using the same values that
13 * are present in the Darwin 7.0 snapshot property lists.
14 *
15 * As far as the CPUs control loops are concerned, I use the
16 * calibration & PID constants provided by the EEPROM,
17 * I do _not_ embed any value from the property lists, as the ones
18 * provided by Darwin 7.0 seem to always have an older version that
19 * what I've seen on the actual computers.
20 * It would be interesting to verify that though. Darwin has a
21 * version code of 1.0.0d11 for all control loops it seems, while
22 * so far, the machines EEPROMs contain a dataset versioned 1.0.0f
23 *
24 * Darwin doesn't provide source to all parts, some missing
25 * bits like the AppleFCU driver or the actual scale of some
26 * of the values returned by sensors had to be "guessed" some
27 * way... or based on what Open Firmware does.
28 *
29 * I didn't yet figure out how to get the slots power consumption
30 * out of the FCU, so that part has not been implemented yet and
31 * the slots fan is set to a fixed 50% PWM, hoping this value is
32 * safe enough ...
33 *
34 * Note: I have observed strange oscillations of the CPU control
35 * loop on a dual G5 here. When idle, the CPU exhaust fan tend to
36 * oscillates slowly (over several minutes) between the minimum
37 * of 300RPMs and approx. 1000 RPMs. I don't know what is causing
38 * this, it could be some incorrect constant or an error in the
39 * way I ported the algorithm, or it could be just normal. I
40 * don't have full understanding on the way Apple tweaked the PID
41 * algorithm for the CPU control, it is definitely not a standard
42 * implementation...
43 *
44 * TODO: - Check MPU structure version/signature
45 * - Add things like /sbin/overtemp for non-critical
46 * overtemp conditions so userland can take some policy
47 * decisions, like slewing down CPUs
48 * - Deal with fan and i2c failures in a better way
49 * - Maybe do a generic PID based on params used for
50 * U3 and Drives ? Definitely need to factor code a bit
51 * bettter... also make sensor detection more robust using
52 * the device-tree to probe for them
53 * - Figure out how to get the slots consumption and set the
54 * slots fan accordingly
55 *
56 * History:
57 *
58 * Nov. 13, 2003 : 0.5
59 * - First release
60 *
61 * Nov. 14, 2003 : 0.6
62 * - Read fan speed from FCU, low level fan routines now deal
63 * with errors & check fan status, though higher level don't
64 * do much.
65 * - Move a bunch of definitions to .h file
66 *
67 * Nov. 18, 2003 : 0.7
68 * - Fix build on ppc64 kernel
69 * - Move back statics definitions to .c file
70 * - Avoid calling schedule_timeout with a negative number
71 *
72 * Dec. 18, 2003 : 0.8
73 * - Fix typo when reading back fan speed on 2 CPU machines
74 *
75 * Mar. 11, 2004 : 0.9
76 * - Rework code accessing the ADC chips, make it more robust and
77 * closer to the chip spec. Also make sure it is configured properly,
78 * I've seen yet unexplained cases where on startup, I would have stale
79 * values in the configuration register
80 * - Switch back to use of target fan speed for PID, thus lowering
81 * pressure on i2c
82 *
83 * Oct. 20, 2004 : 1.1
84 * - Add device-tree lookup for fan IDs, should detect liquid cooling
85 * pumps when present
86 * - Enable driver for PowerMac7,3 machines
87 * - Split the U3/Backside cooling on U3 & U3H versions as Darwin does
88 * - Add new CPU cooling algorithm for machines with liquid cooling
89 * - Workaround for some PowerMac7,3 with empty "fan" node in the devtree
90 * - Fix a signed/unsigned compare issue in some PID loops
91 *
92 * Mar. 10, 2005 : 1.2
93 * - Add basic support for Xserve G5
94 * - Retreive pumps min/max from EEPROM image in device-tree (broken)
95 * - Use min/max macros here or there
96 * - Latest darwin updated U3H min fan speed to 20% PWM
97 *
98 * July. 06, 2006 : 1.3
99 * - Fix setting of RPM fans on Xserve G5 (they were going too fast)
100 * - Add missing slots fan control loop for Xserve G5
101 * - Lower fixed slots fan speed from 50% to 40% on desktop G5s. We
102 * still can't properly implement the control loop for these, so let's
103 * reduce the noise a little bit, it appears that 40% still gives us
104 * a pretty good air flow
105 * - Add code to "tickle" the FCU regulary so it doesn't think that
106 * we are gone while in fact, the machine just didn't need any fan
107 * speed change lately
108 *
109 */
110
111 #include <linux/types.h>
112 #include <linux/module.h>
113 #include <linux/errno.h>
114 #include <linux/kernel.h>
115 #include <linux/delay.h>
116 #include <linux/sched.h>
117 #include <linux/slab.h>
118 #include <linux/init.h>
119 #include <linux/spinlock.h>
120 #include <linux/wait.h>
121 #include <linux/reboot.h>
122 #include <linux/kmod.h>
123 #include <linux/i2c.h>
124 #include <linux/kthread.h>
125 #include <linux/mutex.h>
126 #include <linux/of_device.h>
127 #include <linux/of_platform.h>
128 #include <asm/prom.h>
129 #include <asm/machdep.h>
130 #include <asm/io.h>
131 #include <asm/system.h>
132 #include <asm/sections.h>
133 #include <asm/macio.h>
134
135 #include "therm_pm72.h"
136
137 #define VERSION "1.3"
138
139 #undef DEBUG
140
141 #ifdef DEBUG
142 #define DBG(args...) printk(args)
143 #else
144 #define DBG(args...) do { } while(0)
145 #endif
146
147
148 /*
149 * Driver statics
150 */
151
152 static struct of_device * of_dev;
153 static struct i2c_adapter * u3_0;
154 static struct i2c_adapter * u3_1;
155 static struct i2c_adapter * k2;
156 static struct i2c_client * fcu;
157 static struct cpu_pid_state cpu_state[2];
158 static struct basckside_pid_params backside_params;
159 static struct backside_pid_state backside_state;
160 static struct drives_pid_state drives_state;
161 static struct dimm_pid_state dimms_state;
162 static struct slots_pid_state slots_state;
163 static int state;
164 static int cpu_count;
165 static int cpu_pid_type;
166 static struct task_struct *ctrl_task;
167 static struct completion ctrl_complete;
168 static int critical_state;
169 static int rackmac;
170 static s32 dimm_output_clamp;
171 static int fcu_rpm_shift;
172 static int fcu_tickle_ticks;
173 static DEFINE_MUTEX(driver_lock);
174
175 /*
176 * We have 3 types of CPU PID control. One is "split" old style control
177 * for intake & exhaust fans, the other is "combined" control for both
178 * CPUs that also deals with the pumps when present. To be "compatible"
179 * with OS X at this point, we only use "COMBINED" on the machines that
180 * are identified as having the pumps (though that identification is at
181 * least dodgy). Ultimately, we could probably switch completely to this
182 * algorithm provided we hack it to deal with the UP case
183 */
184 #define CPU_PID_TYPE_SPLIT 0
185 #define CPU_PID_TYPE_COMBINED 1
186 #define CPU_PID_TYPE_RACKMAC 2
187
188 /*
189 * This table describes all fans in the FCU. The "id" and "type" values
190 * are defaults valid for all earlier machines. Newer machines will
191 * eventually override the table content based on the device-tree
192 */
193 struct fcu_fan_table
194 {
195 char* loc; /* location code */
196 int type; /* 0 = rpm, 1 = pwm, 2 = pump */
197 int id; /* id or -1 */
198 };
199
200 #define FCU_FAN_RPM 0
201 #define FCU_FAN_PWM 1
202
203 #define FCU_FAN_ABSENT_ID -1
204
205 #define FCU_FAN_COUNT ARRAY_SIZE(fcu_fans)
206
207 struct fcu_fan_table fcu_fans[] = {
208 [BACKSIDE_FAN_PWM_INDEX] = {
209 .loc = "BACKSIDE,SYS CTRLR FAN",
210 .type = FCU_FAN_PWM,
211 .id = BACKSIDE_FAN_PWM_DEFAULT_ID,
212 },
213 [DRIVES_FAN_RPM_INDEX] = {
214 .loc = "DRIVE BAY",
215 .type = FCU_FAN_RPM,
216 .id = DRIVES_FAN_RPM_DEFAULT_ID,
217 },
218 [SLOTS_FAN_PWM_INDEX] = {
219 .loc = "SLOT,PCI FAN",
220 .type = FCU_FAN_PWM,
221 .id = SLOTS_FAN_PWM_DEFAULT_ID,
222 },
223 [CPUA_INTAKE_FAN_RPM_INDEX] = {
224 .loc = "CPU A INTAKE",
225 .type = FCU_FAN_RPM,
226 .id = CPUA_INTAKE_FAN_RPM_DEFAULT_ID,
227 },
228 [CPUA_EXHAUST_FAN_RPM_INDEX] = {
229 .loc = "CPU A EXHAUST",
230 .type = FCU_FAN_RPM,
231 .id = CPUA_EXHAUST_FAN_RPM_DEFAULT_ID,
232 },
233 [CPUB_INTAKE_FAN_RPM_INDEX] = {
234 .loc = "CPU B INTAKE",
235 .type = FCU_FAN_RPM,
236 .id = CPUB_INTAKE_FAN_RPM_DEFAULT_ID,
237 },
238 [CPUB_EXHAUST_FAN_RPM_INDEX] = {
239 .loc = "CPU B EXHAUST",
240 .type = FCU_FAN_RPM,
241 .id = CPUB_EXHAUST_FAN_RPM_DEFAULT_ID,
242 },
243 /* pumps aren't present by default, have to be looked up in the
244 * device-tree
245 */
246 [CPUA_PUMP_RPM_INDEX] = {
247 .loc = "CPU A PUMP",
248 .type = FCU_FAN_RPM,
249 .id = FCU_FAN_ABSENT_ID,
250 },
251 [CPUB_PUMP_RPM_INDEX] = {
252 .loc = "CPU B PUMP",
253 .type = FCU_FAN_RPM,
254 .id = FCU_FAN_ABSENT_ID,
255 },
256 /* Xserve fans */
257 [CPU_A1_FAN_RPM_INDEX] = {
258 .loc = "CPU A 1",
259 .type = FCU_FAN_RPM,
260 .id = FCU_FAN_ABSENT_ID,
261 },
262 [CPU_A2_FAN_RPM_INDEX] = {
263 .loc = "CPU A 2",
264 .type = FCU_FAN_RPM,
265 .id = FCU_FAN_ABSENT_ID,
266 },
267 [CPU_A3_FAN_RPM_INDEX] = {
268 .loc = "CPU A 3",
269 .type = FCU_FAN_RPM,
270 .id = FCU_FAN_ABSENT_ID,
271 },
272 [CPU_B1_FAN_RPM_INDEX] = {
273 .loc = "CPU B 1",
274 .type = FCU_FAN_RPM,
275 .id = FCU_FAN_ABSENT_ID,
276 },
277 [CPU_B2_FAN_RPM_INDEX] = {
278 .loc = "CPU B 2",
279 .type = FCU_FAN_RPM,
280 .id = FCU_FAN_ABSENT_ID,
281 },
282 [CPU_B3_FAN_RPM_INDEX] = {
283 .loc = "CPU B 3",
284 .type = FCU_FAN_RPM,
285 .id = FCU_FAN_ABSENT_ID,
286 },
287 };
288
289 static struct i2c_driver therm_pm72_driver;
290
291 /*
292 * Utility function to create an i2c_client structure and
293 * attach it to one of u3 adapters
294 */
295 static struct i2c_client *attach_i2c_chip(int id, const char *name)
296 {
297 struct i2c_client *clt;
298 struct i2c_adapter *adap;
299 struct i2c_board_info info;
300
301 if (id & 0x200)
302 adap = k2;
303 else if (id & 0x100)
304 adap = u3_1;
305 else
306 adap = u3_0;
307 if (adap == NULL)
308 return NULL;
309
310 memset(&info, 0, sizeof(struct i2c_board_info));
311 info.addr = (id >> 1) & 0x7f;
312 strlcpy(info.type, "therm_pm72", I2C_NAME_SIZE);
313 clt = i2c_new_device(adap, &info);
314 if (!clt) {
315 printk(KERN_ERR "therm_pm72: Failed to attach to i2c ID 0x%x\n", id);
316 return NULL;
317 }
318
319 /*
320 * Let i2c-core delete that device on driver removal.
321 * This is safe because i2c-core holds the core_lock mutex for us.
322 */
323 list_add_tail(&clt->detected, &therm_pm72_driver.clients);
324 return clt;
325 }
326
327 /*
328 * Here are the i2c chip access wrappers
329 */
330
331 static void initialize_adc(struct cpu_pid_state *state)
332 {
333 int rc;
334 u8 buf[2];
335
336 /* Read ADC the configuration register and cache it. We
337 * also make sure Config2 contains proper values, I've seen
338 * cases where we got stale grabage in there, thus preventing
339 * proper reading of conv. values
340 */
341
342 /* Clear Config2 */
343 buf[0] = 5;
344 buf[1] = 0;
345 i2c_master_send(state->monitor, buf, 2);
346
347 /* Read & cache Config1 */
348 buf[0] = 1;
349 rc = i2c_master_send(state->monitor, buf, 1);
350 if (rc > 0) {
351 rc = i2c_master_recv(state->monitor, buf, 1);
352 if (rc > 0) {
353 state->adc_config = buf[0];
354 DBG("ADC config reg: %02x\n", state->adc_config);
355 /* Disable shutdown mode */
356 state->adc_config &= 0xfe;
357 buf[0] = 1;
358 buf[1] = state->adc_config;
359 rc = i2c_master_send(state->monitor, buf, 2);
360 }
361 }
362 if (rc <= 0)
363 printk(KERN_ERR "therm_pm72: Error reading ADC config"
364 " register !\n");
365 }
366
367 static int read_smon_adc(struct cpu_pid_state *state, int chan)
368 {
369 int rc, data, tries = 0;
370 u8 buf[2];
371
372 for (;;) {
373 /* Set channel */
374 buf[0] = 1;
375 buf[1] = (state->adc_config & 0x1f) | (chan << 5);
376 rc = i2c_master_send(state->monitor, buf, 2);
377 if (rc <= 0)
378 goto error;
379 /* Wait for convertion */
380 msleep(1);
381 /* Switch to data register */
382 buf[0] = 4;
383 rc = i2c_master_send(state->monitor, buf, 1);
384 if (rc <= 0)
385 goto error;
386 /* Read result */
387 rc = i2c_master_recv(state->monitor, buf, 2);
388 if (rc < 0)
389 goto error;
390 data = ((u16)buf[0]) << 8 | (u16)buf[1];
391 return data >> 6;
392 error:
393 DBG("Error reading ADC, retrying...\n");
394 if (++tries > 10) {
395 printk(KERN_ERR "therm_pm72: Error reading ADC !\n");
396 return -1;
397 }
398 msleep(10);
399 }
400 }
401
402 static int read_lm87_reg(struct i2c_client * chip, int reg)
403 {
404 int rc, tries = 0;
405 u8 buf;
406
407 for (;;) {
408 /* Set address */
409 buf = (u8)reg;
410 rc = i2c_master_send(chip, &buf, 1);
411 if (rc <= 0)
412 goto error;
413 rc = i2c_master_recv(chip, &buf, 1);
414 if (rc <= 0)
415 goto error;
416 return (int)buf;
417 error:
418 DBG("Error reading LM87, retrying...\n");
419 if (++tries > 10) {
420 printk(KERN_ERR "therm_pm72: Error reading LM87 !\n");
421 return -1;
422 }
423 msleep(10);
424 }
425 }
426
427 static int fan_read_reg(int reg, unsigned char *buf, int nb)
428 {
429 int tries, nr, nw;
430
431 buf[0] = reg;
432 tries = 0;
433 for (;;) {
434 nw = i2c_master_send(fcu, buf, 1);
435 if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100)
436 break;
437 msleep(10);
438 ++tries;
439 }
440 if (nw <= 0) {
441 printk(KERN_ERR "Failure writing address to FCU: %d", nw);
442 return -EIO;
443 }
444 tries = 0;
445 for (;;) {
446 nr = i2c_master_recv(fcu, buf, nb);
447 if (nr > 0 || (nr < 0 && nr != ENODEV) || tries >= 100)
448 break;
449 msleep(10);
450 ++tries;
451 }
452 if (nr <= 0)
453 printk(KERN_ERR "Failure reading data from FCU: %d", nw);
454 return nr;
455 }
456
457 static int fan_write_reg(int reg, const unsigned char *ptr, int nb)
458 {
459 int tries, nw;
460 unsigned char buf[16];
461
462 buf[0] = reg;
463 memcpy(buf+1, ptr, nb);
464 ++nb;
465 tries = 0;
466 for (;;) {
467 nw = i2c_master_send(fcu, buf, nb);
468 if (nw > 0 || (nw < 0 && nw != EIO) || tries >= 100)
469 break;
470 msleep(10);
471 ++tries;
472 }
473 if (nw < 0)
474 printk(KERN_ERR "Failure writing to FCU: %d", nw);
475 return nw;
476 }
477
478 static int start_fcu(void)
479 {
480 unsigned char buf = 0xff;
481 int rc;
482
483 rc = fan_write_reg(0xe, &buf, 1);
484 if (rc < 0)
485 return -EIO;
486 rc = fan_write_reg(0x2e, &buf, 1);
487 if (rc < 0)
488 return -EIO;
489 rc = fan_read_reg(0, &buf, 1);
490 if (rc < 0)
491 return -EIO;
492 fcu_rpm_shift = (buf == 1) ? 2 : 3;
493 printk(KERN_DEBUG "FCU Initialized, RPM fan shift is %d\n",
494 fcu_rpm_shift);
495
496 return 0;
497 }
498
499 static int set_rpm_fan(int fan_index, int rpm)
500 {
501 unsigned char buf[2];
502 int rc, id, min, max;
503
504 if (fcu_fans[fan_index].type != FCU_FAN_RPM)
505 return -EINVAL;
506 id = fcu_fans[fan_index].id;
507 if (id == FCU_FAN_ABSENT_ID)
508 return -EINVAL;
509
510 min = 2400 >> fcu_rpm_shift;
511 max = 56000 >> fcu_rpm_shift;
512
513 if (rpm < min)
514 rpm = min;
515 else if (rpm > max)
516 rpm = max;
517 buf[0] = rpm >> (8 - fcu_rpm_shift);
518 buf[1] = rpm << fcu_rpm_shift;
519 rc = fan_write_reg(0x10 + (id * 2), buf, 2);
520 if (rc < 0)
521 return -EIO;
522 return 0;
523 }
524
525 static int get_rpm_fan(int fan_index, int programmed)
526 {
527 unsigned char failure;
528 unsigned char active;
529 unsigned char buf[2];
530 int rc, id, reg_base;
531
532 if (fcu_fans[fan_index].type != FCU_FAN_RPM)
533 return -EINVAL;
534 id = fcu_fans[fan_index].id;
535 if (id == FCU_FAN_ABSENT_ID)
536 return -EINVAL;
537
538 rc = fan_read_reg(0xb, &failure, 1);
539 if (rc != 1)
540 return -EIO;
541 if ((failure & (1 << id)) != 0)
542 return -EFAULT;
543 rc = fan_read_reg(0xd, &active, 1);
544 if (rc != 1)
545 return -EIO;
546 if ((active & (1 << id)) == 0)
547 return -ENXIO;
548
549 /* Programmed value or real current speed */
550 reg_base = programmed ? 0x10 : 0x11;
551 rc = fan_read_reg(reg_base + (id * 2), buf, 2);
552 if (rc != 2)
553 return -EIO;
554
555 return (buf[0] << (8 - fcu_rpm_shift)) | buf[1] >> fcu_rpm_shift;
556 }
557
558 static int set_pwm_fan(int fan_index, int pwm)
559 {
560 unsigned char buf[2];
561 int rc, id;
562
563 if (fcu_fans[fan_index].type != FCU_FAN_PWM)
564 return -EINVAL;
565 id = fcu_fans[fan_index].id;
566 if (id == FCU_FAN_ABSENT_ID)
567 return -EINVAL;
568
569 if (pwm < 10)
570 pwm = 10;
571 else if (pwm > 100)
572 pwm = 100;
573 pwm = (pwm * 2559) / 1000;
574 buf[0] = pwm;
575 rc = fan_write_reg(0x30 + (id * 2), buf, 1);
576 if (rc < 0)
577 return rc;
578 return 0;
579 }
580
581 static int get_pwm_fan(int fan_index)
582 {
583 unsigned char failure;
584 unsigned char active;
585 unsigned char buf[2];
586 int rc, id;
587
588 if (fcu_fans[fan_index].type != FCU_FAN_PWM)
589 return -EINVAL;
590 id = fcu_fans[fan_index].id;
591 if (id == FCU_FAN_ABSENT_ID)
592 return -EINVAL;
593
594 rc = fan_read_reg(0x2b, &failure, 1);
595 if (rc != 1)
596 return -EIO;
597 if ((failure & (1 << id)) != 0)
598 return -EFAULT;
599 rc = fan_read_reg(0x2d, &active, 1);
600 if (rc != 1)
601 return -EIO;
602 if ((active & (1 << id)) == 0)
603 return -ENXIO;
604
605 /* Programmed value or real current speed */
606 rc = fan_read_reg(0x30 + (id * 2), buf, 1);
607 if (rc != 1)
608 return -EIO;
609
610 return (buf[0] * 1000) / 2559;
611 }
612
613 static void tickle_fcu(void)
614 {
615 int pwm;
616
617 pwm = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
618
619 DBG("FCU Tickle, slots fan is: %d\n", pwm);
620 if (pwm < 0)
621 pwm = 100;
622
623 if (!rackmac) {
624 pwm = SLOTS_FAN_DEFAULT_PWM;
625 } else if (pwm < SLOTS_PID_OUTPUT_MIN)
626 pwm = SLOTS_PID_OUTPUT_MIN;
627
628 /* That is hopefully enough to make the FCU happy */
629 set_pwm_fan(SLOTS_FAN_PWM_INDEX, pwm);
630 }
631
632
633 /*
634 * Utility routine to read the CPU calibration EEPROM data
635 * from the device-tree
636 */
637 static int read_eeprom(int cpu, struct mpu_data *out)
638 {
639 struct device_node *np;
640 char nodename[64];
641 const u8 *data;
642 int len;
643
644 /* prom.c routine for finding a node by path is a bit brain dead
645 * and requires exact @xxx unit numbers. This is a bit ugly but
646 * will work for these machines
647 */
648 sprintf(nodename, "/u3@0,f8000000/i2c@f8001000/cpuid@a%d", cpu ? 2 : 0);
649 np = of_find_node_by_path(nodename);
650 if (np == NULL) {
651 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid node from device-tree\n");
652 return -ENODEV;
653 }
654 data = of_get_property(np, "cpuid", &len);
655 if (data == NULL) {
656 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid property from device-tree\n");
657 of_node_put(np);
658 return -ENODEV;
659 }
660 memcpy(out, data, sizeof(struct mpu_data));
661 of_node_put(np);
662
663 return 0;
664 }
665
666 static void fetch_cpu_pumps_minmax(void)
667 {
668 struct cpu_pid_state *state0 = &cpu_state[0];
669 struct cpu_pid_state *state1 = &cpu_state[1];
670 u16 pump_min = 0, pump_max = 0xffff;
671 u16 tmp[4];
672
673 /* Try to fetch pumps min/max infos from eeprom */
674
675 memcpy(&tmp, &state0->mpu.processor_part_num, 8);
676 if (tmp[0] != 0xffff && tmp[1] != 0xffff) {
677 pump_min = max(pump_min, tmp[0]);
678 pump_max = min(pump_max, tmp[1]);
679 }
680 if (tmp[2] != 0xffff && tmp[3] != 0xffff) {
681 pump_min = max(pump_min, tmp[2]);
682 pump_max = min(pump_max, tmp[3]);
683 }
684
685 /* Double check the values, this _IS_ needed as the EEPROM on
686 * some dual 2.5Ghz G5s seem, at least, to have both min & max
687 * same to the same value ... (grrrr)
688 */
689 if (pump_min == pump_max || pump_min == 0 || pump_max == 0xffff) {
690 pump_min = CPU_PUMP_OUTPUT_MIN;
691 pump_max = CPU_PUMP_OUTPUT_MAX;
692 }
693
694 state0->pump_min = state1->pump_min = pump_min;
695 state0->pump_max = state1->pump_max = pump_max;
696 }
697
698 /*
699 * Now, unfortunately, sysfs doesn't give us a nice void * we could
700 * pass around to the attribute functions, so we don't really have
701 * choice but implement a bunch of them...
702 *
703 * That sucks a bit, we take the lock because FIX32TOPRINT evaluates
704 * the input twice... I accept patches :)
705 */
706 #define BUILD_SHOW_FUNC_FIX(name, data) \
707 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
708 { \
709 ssize_t r; \
710 mutex_lock(&driver_lock); \
711 r = sprintf(buf, "%d.%03d", FIX32TOPRINT(data)); \
712 mutex_unlock(&driver_lock); \
713 return r; \
714 }
715 #define BUILD_SHOW_FUNC_INT(name, data) \
716 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
717 { \
718 return sprintf(buf, "%d", data); \
719 }
720
721 BUILD_SHOW_FUNC_FIX(cpu0_temperature, cpu_state[0].last_temp)
722 BUILD_SHOW_FUNC_FIX(cpu0_voltage, cpu_state[0].voltage)
723 BUILD_SHOW_FUNC_FIX(cpu0_current, cpu_state[0].current_a)
724 BUILD_SHOW_FUNC_INT(cpu0_exhaust_fan_rpm, cpu_state[0].rpm)
725 BUILD_SHOW_FUNC_INT(cpu0_intake_fan_rpm, cpu_state[0].intake_rpm)
726
727 BUILD_SHOW_FUNC_FIX(cpu1_temperature, cpu_state[1].last_temp)
728 BUILD_SHOW_FUNC_FIX(cpu1_voltage, cpu_state[1].voltage)
729 BUILD_SHOW_FUNC_FIX(cpu1_current, cpu_state[1].current_a)
730 BUILD_SHOW_FUNC_INT(cpu1_exhaust_fan_rpm, cpu_state[1].rpm)
731 BUILD_SHOW_FUNC_INT(cpu1_intake_fan_rpm, cpu_state[1].intake_rpm)
732
733 BUILD_SHOW_FUNC_FIX(backside_temperature, backside_state.last_temp)
734 BUILD_SHOW_FUNC_INT(backside_fan_pwm, backside_state.pwm)
735
736 BUILD_SHOW_FUNC_FIX(drives_temperature, drives_state.last_temp)
737 BUILD_SHOW_FUNC_INT(drives_fan_rpm, drives_state.rpm)
738
739 BUILD_SHOW_FUNC_FIX(slots_temperature, slots_state.last_temp)
740 BUILD_SHOW_FUNC_INT(slots_fan_pwm, slots_state.pwm)
741
742 BUILD_SHOW_FUNC_FIX(dimms_temperature, dimms_state.last_temp)
743
744 static DEVICE_ATTR(cpu0_temperature,S_IRUGO,show_cpu0_temperature,NULL);
745 static DEVICE_ATTR(cpu0_voltage,S_IRUGO,show_cpu0_voltage,NULL);
746 static DEVICE_ATTR(cpu0_current,S_IRUGO,show_cpu0_current,NULL);
747 static DEVICE_ATTR(cpu0_exhaust_fan_rpm,S_IRUGO,show_cpu0_exhaust_fan_rpm,NULL);
748 static DEVICE_ATTR(cpu0_intake_fan_rpm,S_IRUGO,show_cpu0_intake_fan_rpm,NULL);
749
750 static DEVICE_ATTR(cpu1_temperature,S_IRUGO,show_cpu1_temperature,NULL);
751 static DEVICE_ATTR(cpu1_voltage,S_IRUGO,show_cpu1_voltage,NULL);
752 static DEVICE_ATTR(cpu1_current,S_IRUGO,show_cpu1_current,NULL);
753 static DEVICE_ATTR(cpu1_exhaust_fan_rpm,S_IRUGO,show_cpu1_exhaust_fan_rpm,NULL);
754 static DEVICE_ATTR(cpu1_intake_fan_rpm,S_IRUGO,show_cpu1_intake_fan_rpm,NULL);
755
756 static DEVICE_ATTR(backside_temperature,S_IRUGO,show_backside_temperature,NULL);
757 static DEVICE_ATTR(backside_fan_pwm,S_IRUGO,show_backside_fan_pwm,NULL);
758
759 static DEVICE_ATTR(drives_temperature,S_IRUGO,show_drives_temperature,NULL);
760 static DEVICE_ATTR(drives_fan_rpm,S_IRUGO,show_drives_fan_rpm,NULL);
761
762 static DEVICE_ATTR(slots_temperature,S_IRUGO,show_slots_temperature,NULL);
763 static DEVICE_ATTR(slots_fan_pwm,S_IRUGO,show_slots_fan_pwm,NULL);
764
765 static DEVICE_ATTR(dimms_temperature,S_IRUGO,show_dimms_temperature,NULL);
766
767 /*
768 * CPUs fans control loop
769 */
770
771 static int do_read_one_cpu_values(struct cpu_pid_state *state, s32 *temp, s32 *power)
772 {
773 s32 ltemp, volts, amps;
774 int index, rc = 0;
775
776 /* Default (in case of error) */
777 *temp = state->cur_temp;
778 *power = state->cur_power;
779
780 if (cpu_pid_type == CPU_PID_TYPE_RACKMAC)
781 index = (state->index == 0) ?
782 CPU_A1_FAN_RPM_INDEX : CPU_B1_FAN_RPM_INDEX;
783 else
784 index = (state->index == 0) ?
785 CPUA_EXHAUST_FAN_RPM_INDEX : CPUB_EXHAUST_FAN_RPM_INDEX;
786
787 /* Read current fan status */
788 rc = get_rpm_fan(index, !RPM_PID_USE_ACTUAL_SPEED);
789 if (rc < 0) {
790 /* XXX What do we do now ? Nothing for now, keep old value, but
791 * return error upstream
792 */
793 DBG(" cpu %d, fan reading error !\n", state->index);
794 } else {
795 state->rpm = rc;
796 DBG(" cpu %d, exhaust RPM: %d\n", state->index, state->rpm);
797 }
798
799 /* Get some sensor readings and scale it */
800 ltemp = read_smon_adc(state, 1);
801 if (ltemp == -1) {
802 /* XXX What do we do now ? */
803 state->overtemp++;
804 if (rc == 0)
805 rc = -EIO;
806 DBG(" cpu %d, temp reading error !\n", state->index);
807 } else {
808 /* Fixup temperature according to diode calibration
809 */
810 DBG(" cpu %d, temp raw: %04x, m_diode: %04x, b_diode: %04x\n",
811 state->index,
812 ltemp, state->mpu.mdiode, state->mpu.bdiode);
813 *temp = ((s32)ltemp * (s32)state->mpu.mdiode + ((s32)state->mpu.bdiode << 12)) >> 2;
814 state->last_temp = *temp;
815 DBG(" temp: %d.%03d\n", FIX32TOPRINT((*temp)));
816 }
817
818 /*
819 * Read voltage & current and calculate power
820 */
821 volts = read_smon_adc(state, 3);
822 amps = read_smon_adc(state, 4);
823
824 /* Scale voltage and current raw sensor values according to fixed scales
825 * obtained in Darwin and calculate power from I and V
826 */
827 volts *= ADC_CPU_VOLTAGE_SCALE;
828 amps *= ADC_CPU_CURRENT_SCALE;
829 *power = (((u64)volts) * ((u64)amps)) >> 16;
830 state->voltage = volts;
831 state->current_a = amps;
832 state->last_power = *power;
833
834 DBG(" cpu %d, current: %d.%03d, voltage: %d.%03d, power: %d.%03d W\n",
835 state->index, FIX32TOPRINT(state->current_a),
836 FIX32TOPRINT(state->voltage), FIX32TOPRINT(*power));
837
838 return 0;
839 }
840
841 static void do_cpu_pid(struct cpu_pid_state *state, s32 temp, s32 power)
842 {
843 s32 power_target, integral, derivative, proportional, adj_in_target, sval;
844 s64 integ_p, deriv_p, prop_p, sum;
845 int i;
846
847 /* Calculate power target value (could be done once for all)
848 * and convert to a 16.16 fp number
849 */
850 power_target = ((u32)(state->mpu.pmaxh - state->mpu.padjmax)) << 16;
851 DBG(" power target: %d.%03d, error: %d.%03d\n",
852 FIX32TOPRINT(power_target), FIX32TOPRINT(power_target - power));
853
854 /* Store temperature and power in history array */
855 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
856 state->temp_history[state->cur_temp] = temp;
857 state->cur_power = (state->cur_power + 1) % state->count_power;
858 state->power_history[state->cur_power] = power;
859 state->error_history[state->cur_power] = power_target - power;
860
861 /* If first loop, fill the history table */
862 if (state->first) {
863 for (i = 0; i < (state->count_power - 1); i++) {
864 state->cur_power = (state->cur_power + 1) % state->count_power;
865 state->power_history[state->cur_power] = power;
866 state->error_history[state->cur_power] = power_target - power;
867 }
868 for (i = 0; i < (CPU_TEMP_HISTORY_SIZE - 1); i++) {
869 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
870 state->temp_history[state->cur_temp] = temp;
871 }
872 state->first = 0;
873 }
874
875 /* Calculate the integral term normally based on the "power" values */
876 sum = 0;
877 integral = 0;
878 for (i = 0; i < state->count_power; i++)
879 integral += state->error_history[i];
880 integral *= CPU_PID_INTERVAL;
881 DBG(" integral: %08x\n", integral);
882
883 /* Calculate the adjusted input (sense value).
884 * G_r is 12.20
885 * integ is 16.16
886 * so the result is 28.36
887 *
888 * input target is mpu.ttarget, input max is mpu.tmax
889 */
890 integ_p = ((s64)state->mpu.pid_gr) * (s64)integral;
891 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
892 sval = (state->mpu.tmax << 16) - ((integ_p >> 20) & 0xffffffff);
893 adj_in_target = (state->mpu.ttarget << 16);
894 if (adj_in_target > sval)
895 adj_in_target = sval;
896 DBG(" adj_in_target: %d.%03d, ttarget: %d\n", FIX32TOPRINT(adj_in_target),
897 state->mpu.ttarget);
898
899 /* Calculate the derivative term */
900 derivative = state->temp_history[state->cur_temp] -
901 state->temp_history[(state->cur_temp + CPU_TEMP_HISTORY_SIZE - 1)
902 % CPU_TEMP_HISTORY_SIZE];
903 derivative /= CPU_PID_INTERVAL;
904 deriv_p = ((s64)state->mpu.pid_gd) * (s64)derivative;
905 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
906 sum += deriv_p;
907
908 /* Calculate the proportional term */
909 proportional = temp - adj_in_target;
910 prop_p = ((s64)state->mpu.pid_gp) * (s64)proportional;
911 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
912 sum += prop_p;
913
914 /* Scale sum */
915 sum >>= 36;
916
917 DBG(" sum: %d\n", (int)sum);
918 state->rpm += (s32)sum;
919 }
920
921 static void do_monitor_cpu_combined(void)
922 {
923 struct cpu_pid_state *state0 = &cpu_state[0];
924 struct cpu_pid_state *state1 = &cpu_state[1];
925 s32 temp0, power0, temp1, power1;
926 s32 temp_combi, power_combi;
927 int rc, intake, pump;
928
929 rc = do_read_one_cpu_values(state0, &temp0, &power0);
930 if (rc < 0) {
931 /* XXX What do we do now ? */
932 }
933 state1->overtemp = 0;
934 rc = do_read_one_cpu_values(state1, &temp1, &power1);
935 if (rc < 0) {
936 /* XXX What do we do now ? */
937 }
938 if (state1->overtemp)
939 state0->overtemp++;
940
941 temp_combi = max(temp0, temp1);
942 power_combi = max(power0, power1);
943
944 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
945 * full blown immediately and try to trigger a shutdown
946 */
947 if (temp_combi >= ((state0->mpu.tmax + 8) << 16)) {
948 printk(KERN_WARNING "Warning ! Temperature way above maximum (%d) !\n",
949 temp_combi >> 16);
950 state0->overtemp += CPU_MAX_OVERTEMP / 4;
951 } else if (temp_combi > (state0->mpu.tmax << 16)) {
952 state0->overtemp++;
953 printk(KERN_WARNING "Temperature %d above max %d. overtemp %d\n",
954 temp_combi >> 16, state0->mpu.tmax, state0->overtemp);
955 } else {
956 if (state0->overtemp)
957 printk(KERN_WARNING "Temperature back down to %d\n",
958 temp_combi >> 16);
959 state0->overtemp = 0;
960 }
961 if (state0->overtemp >= CPU_MAX_OVERTEMP)
962 critical_state = 1;
963 if (state0->overtemp > 0) {
964 state0->rpm = state0->mpu.rmaxn_exhaust_fan;
965 state0->intake_rpm = intake = state0->mpu.rmaxn_intake_fan;
966 pump = state0->pump_max;
967 goto do_set_fans;
968 }
969
970 /* Do the PID */
971 do_cpu_pid(state0, temp_combi, power_combi);
972
973 /* Range check */
974 state0->rpm = max(state0->rpm, (int)state0->mpu.rminn_exhaust_fan);
975 state0->rpm = min(state0->rpm, (int)state0->mpu.rmaxn_exhaust_fan);
976
977 /* Calculate intake fan speed */
978 intake = (state0->rpm * CPU_INTAKE_SCALE) >> 16;
979 intake = max(intake, (int)state0->mpu.rminn_intake_fan);
980 intake = min(intake, (int)state0->mpu.rmaxn_intake_fan);
981 state0->intake_rpm = intake;
982
983 /* Calculate pump speed */
984 pump = (state0->rpm * state0->pump_max) /
985 state0->mpu.rmaxn_exhaust_fan;
986 pump = min(pump, state0->pump_max);
987 pump = max(pump, state0->pump_min);
988
989 do_set_fans:
990 /* We copy values from state 0 to state 1 for /sysfs */
991 state1->rpm = state0->rpm;
992 state1->intake_rpm = state0->intake_rpm;
993
994 DBG("** CPU %d RPM: %d Ex, %d, Pump: %d, In, overtemp: %d\n",
995 state1->index, (int)state1->rpm, intake, pump, state1->overtemp);
996
997 /* We should check for errors, shouldn't we ? But then, what
998 * do we do once the error occurs ? For FCU notified fan
999 * failures (-EFAULT) we probably want to notify userland
1000 * some way...
1001 */
1002 set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
1003 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state0->rpm);
1004 set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
1005 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state0->rpm);
1006
1007 if (fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
1008 set_rpm_fan(CPUA_PUMP_RPM_INDEX, pump);
1009 if (fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
1010 set_rpm_fan(CPUB_PUMP_RPM_INDEX, pump);
1011 }
1012
1013 static void do_monitor_cpu_split(struct cpu_pid_state *state)
1014 {
1015 s32 temp, power;
1016 int rc, intake;
1017
1018 /* Read current fan status */
1019 rc = do_read_one_cpu_values(state, &temp, &power);
1020 if (rc < 0) {
1021 /* XXX What do we do now ? */
1022 }
1023
1024 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
1025 * full blown immediately and try to trigger a shutdown
1026 */
1027 if (temp >= ((state->mpu.tmax + 8) << 16)) {
1028 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
1029 " (%d) !\n",
1030 state->index, temp >> 16);
1031 state->overtemp += CPU_MAX_OVERTEMP / 4;
1032 } else if (temp > (state->mpu.tmax << 16)) {
1033 state->overtemp++;
1034 printk(KERN_WARNING "CPU %d temperature %d above max %d. overtemp %d\n",
1035 state->index, temp >> 16, state->mpu.tmax, state->overtemp);
1036 } else {
1037 if (state->overtemp)
1038 printk(KERN_WARNING "CPU %d temperature back down to %d\n",
1039 state->index, temp >> 16);
1040 state->overtemp = 0;
1041 }
1042 if (state->overtemp >= CPU_MAX_OVERTEMP)
1043 critical_state = 1;
1044 if (state->overtemp > 0) {
1045 state->rpm = state->mpu.rmaxn_exhaust_fan;
1046 state->intake_rpm = intake = state->mpu.rmaxn_intake_fan;
1047 goto do_set_fans;
1048 }
1049
1050 /* Do the PID */
1051 do_cpu_pid(state, temp, power);
1052
1053 /* Range check */
1054 state->rpm = max(state->rpm, (int)state->mpu.rminn_exhaust_fan);
1055 state->rpm = min(state->rpm, (int)state->mpu.rmaxn_exhaust_fan);
1056
1057 /* Calculate intake fan */
1058 intake = (state->rpm * CPU_INTAKE_SCALE) >> 16;
1059 intake = max(intake, (int)state->mpu.rminn_intake_fan);
1060 intake = min(intake, (int)state->mpu.rmaxn_intake_fan);
1061 state->intake_rpm = intake;
1062
1063 do_set_fans:
1064 DBG("** CPU %d RPM: %d Ex, %d In, overtemp: %d\n",
1065 state->index, (int)state->rpm, intake, state->overtemp);
1066
1067 /* We should check for errors, shouldn't we ? But then, what
1068 * do we do once the error occurs ? For FCU notified fan
1069 * failures (-EFAULT) we probably want to notify userland
1070 * some way...
1071 */
1072 if (state->index == 0) {
1073 set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
1074 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state->rpm);
1075 } else {
1076 set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
1077 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state->rpm);
1078 }
1079 }
1080
1081 static void do_monitor_cpu_rack(struct cpu_pid_state *state)
1082 {
1083 s32 temp, power, fan_min;
1084 int rc;
1085
1086 /* Read current fan status */
1087 rc = do_read_one_cpu_values(state, &temp, &power);
1088 if (rc < 0) {
1089 /* XXX What do we do now ? */
1090 }
1091
1092 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
1093 * full blown immediately and try to trigger a shutdown
1094 */
1095 if (temp >= ((state->mpu.tmax + 8) << 16)) {
1096 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
1097 " (%d) !\n",
1098 state->index, temp >> 16);
1099 state->overtemp = CPU_MAX_OVERTEMP / 4;
1100 } else if (temp > (state->mpu.tmax << 16)) {
1101 state->overtemp++;
1102 printk(KERN_WARNING "CPU %d temperature %d above max %d. overtemp %d\n",
1103 state->index, temp >> 16, state->mpu.tmax, state->overtemp);
1104 } else {
1105 if (state->overtemp)
1106 printk(KERN_WARNING "CPU %d temperature back down to %d\n",
1107 state->index, temp >> 16);
1108 state->overtemp = 0;
1109 }
1110 if (state->overtemp >= CPU_MAX_OVERTEMP)
1111 critical_state = 1;
1112 if (state->overtemp > 0) {
1113 state->rpm = state->intake_rpm = state->mpu.rmaxn_intake_fan;
1114 goto do_set_fans;
1115 }
1116
1117 /* Do the PID */
1118 do_cpu_pid(state, temp, power);
1119
1120 /* Check clamp from dimms */
1121 fan_min = dimm_output_clamp;
1122 fan_min = max(fan_min, (int)state->mpu.rminn_intake_fan);
1123
1124 DBG(" CPU min mpu = %d, min dimm = %d\n",
1125 state->mpu.rminn_intake_fan, dimm_output_clamp);
1126
1127 state->rpm = max(state->rpm, (int)fan_min);
1128 state->rpm = min(state->rpm, (int)state->mpu.rmaxn_intake_fan);
1129 state->intake_rpm = state->rpm;
1130
1131 do_set_fans:
1132 DBG("** CPU %d RPM: %d overtemp: %d\n",
1133 state->index, (int)state->rpm, state->overtemp);
1134
1135 /* We should check for errors, shouldn't we ? But then, what
1136 * do we do once the error occurs ? For FCU notified fan
1137 * failures (-EFAULT) we probably want to notify userland
1138 * some way...
1139 */
1140 if (state->index == 0) {
1141 set_rpm_fan(CPU_A1_FAN_RPM_INDEX, state->rpm);
1142 set_rpm_fan(CPU_A2_FAN_RPM_INDEX, state->rpm);
1143 set_rpm_fan(CPU_A3_FAN_RPM_INDEX, state->rpm);
1144 } else {
1145 set_rpm_fan(CPU_B1_FAN_RPM_INDEX, state->rpm);
1146 set_rpm_fan(CPU_B2_FAN_RPM_INDEX, state->rpm);
1147 set_rpm_fan(CPU_B3_FAN_RPM_INDEX, state->rpm);
1148 }
1149 }
1150
1151 /*
1152 * Initialize the state structure for one CPU control loop
1153 */
1154 static int init_cpu_state(struct cpu_pid_state *state, int index)
1155 {
1156 int err;
1157
1158 state->index = index;
1159 state->first = 1;
1160 state->rpm = (cpu_pid_type == CPU_PID_TYPE_RACKMAC) ? 4000 : 1000;
1161 state->overtemp = 0;
1162 state->adc_config = 0x00;
1163
1164
1165 if (index == 0)
1166 state->monitor = attach_i2c_chip(SUPPLY_MONITOR_ID, "CPU0_monitor");
1167 else if (index == 1)
1168 state->monitor = attach_i2c_chip(SUPPLY_MONITORB_ID, "CPU1_monitor");
1169 if (state->monitor == NULL)
1170 goto fail;
1171
1172 if (read_eeprom(index, &state->mpu))
1173 goto fail;
1174
1175 state->count_power = state->mpu.tguardband;
1176 if (state->count_power > CPU_POWER_HISTORY_SIZE) {
1177 printk(KERN_WARNING "Warning ! too many power history slots\n");
1178 state->count_power = CPU_POWER_HISTORY_SIZE;
1179 }
1180 DBG("CPU %d Using %d power history entries\n", index, state->count_power);
1181
1182 if (index == 0) {
1183 err = device_create_file(&of_dev->dev, &dev_attr_cpu0_temperature);
1184 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_voltage);
1185 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_current);
1186 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
1187 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
1188 } else {
1189 err = device_create_file(&of_dev->dev, &dev_attr_cpu1_temperature);
1190 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_voltage);
1191 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_current);
1192 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
1193 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
1194 }
1195 if (err)
1196 printk(KERN_WARNING "Failed to create some of the atribute"
1197 "files for CPU %d\n", index);
1198
1199 return 0;
1200 fail:
1201 state->monitor = NULL;
1202
1203 return -ENODEV;
1204 }
1205
1206 /*
1207 * Dispose of the state data for one CPU control loop
1208 */
1209 static void dispose_cpu_state(struct cpu_pid_state *state)
1210 {
1211 if (state->monitor == NULL)
1212 return;
1213
1214 if (state->index == 0) {
1215 device_remove_file(&of_dev->dev, &dev_attr_cpu0_temperature);
1216 device_remove_file(&of_dev->dev, &dev_attr_cpu0_voltage);
1217 device_remove_file(&of_dev->dev, &dev_attr_cpu0_current);
1218 device_remove_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
1219 device_remove_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
1220 } else {
1221 device_remove_file(&of_dev->dev, &dev_attr_cpu1_temperature);
1222 device_remove_file(&of_dev->dev, &dev_attr_cpu1_voltage);
1223 device_remove_file(&of_dev->dev, &dev_attr_cpu1_current);
1224 device_remove_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
1225 device_remove_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
1226 }
1227
1228 state->monitor = NULL;
1229 }
1230
1231 /*
1232 * Motherboard backside & U3 heatsink fan control loop
1233 */
1234 static void do_monitor_backside(struct backside_pid_state *state)
1235 {
1236 s32 temp, integral, derivative, fan_min;
1237 s64 integ_p, deriv_p, prop_p, sum;
1238 int i, rc;
1239
1240 if (--state->ticks != 0)
1241 return;
1242 state->ticks = backside_params.interval;
1243
1244 DBG("backside:\n");
1245
1246 /* Check fan status */
1247 rc = get_pwm_fan(BACKSIDE_FAN_PWM_INDEX);
1248 if (rc < 0) {
1249 printk(KERN_WARNING "Error %d reading backside fan !\n", rc);
1250 /* XXX What do we do now ? */
1251 } else
1252 state->pwm = rc;
1253 DBG(" current pwm: %d\n", state->pwm);
1254
1255 /* Get some sensor readings */
1256 temp = i2c_smbus_read_byte_data(state->monitor, MAX6690_EXT_TEMP) << 16;
1257 state->last_temp = temp;
1258 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1259 FIX32TOPRINT(backside_params.input_target));
1260
1261 /* Store temperature and error in history array */
1262 state->cur_sample = (state->cur_sample + 1) % BACKSIDE_PID_HISTORY_SIZE;
1263 state->sample_history[state->cur_sample] = temp;
1264 state->error_history[state->cur_sample] = temp - backside_params.input_target;
1265
1266 /* If first loop, fill the history table */
1267 if (state->first) {
1268 for (i = 0; i < (BACKSIDE_PID_HISTORY_SIZE - 1); i++) {
1269 state->cur_sample = (state->cur_sample + 1) %
1270 BACKSIDE_PID_HISTORY_SIZE;
1271 state->sample_history[state->cur_sample] = temp;
1272 state->error_history[state->cur_sample] =
1273 temp - backside_params.input_target;
1274 }
1275 state->first = 0;
1276 }
1277
1278 /* Calculate the integral term */
1279 sum = 0;
1280 integral = 0;
1281 for (i = 0; i < BACKSIDE_PID_HISTORY_SIZE; i++)
1282 integral += state->error_history[i];
1283 integral *= backside_params.interval;
1284 DBG(" integral: %08x\n", integral);
1285 integ_p = ((s64)backside_params.G_r) * (s64)integral;
1286 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1287 sum += integ_p;
1288
1289 /* Calculate the derivative term */
1290 derivative = state->error_history[state->cur_sample] -
1291 state->error_history[(state->cur_sample + BACKSIDE_PID_HISTORY_SIZE - 1)
1292 % BACKSIDE_PID_HISTORY_SIZE];
1293 derivative /= backside_params.interval;
1294 deriv_p = ((s64)backside_params.G_d) * (s64)derivative;
1295 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1296 sum += deriv_p;
1297
1298 /* Calculate the proportional term */
1299 prop_p = ((s64)backside_params.G_p) * (s64)(state->error_history[state->cur_sample]);
1300 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1301 sum += prop_p;
1302
1303 /* Scale sum */
1304 sum >>= 36;
1305
1306 DBG(" sum: %d\n", (int)sum);
1307 if (backside_params.additive)
1308 state->pwm += (s32)sum;
1309 else
1310 state->pwm = sum;
1311
1312 /* Check for clamp */
1313 fan_min = (dimm_output_clamp * 100) / 14000;
1314 fan_min = max(fan_min, backside_params.output_min);
1315
1316 state->pwm = max(state->pwm, fan_min);
1317 state->pwm = min(state->pwm, backside_params.output_max);
1318
1319 DBG("** BACKSIDE PWM: %d\n", (int)state->pwm);
1320 set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, state->pwm);
1321 }
1322
1323 /*
1324 * Initialize the state structure for the backside fan control loop
1325 */
1326 static int init_backside_state(struct backside_pid_state *state)
1327 {
1328 struct device_node *u3;
1329 int u3h = 1; /* conservative by default */
1330 int err;
1331
1332 /*
1333 * There are different PID params for machines with U3 and machines
1334 * with U3H, pick the right ones now
1335 */
1336 u3 = of_find_node_by_path("/u3@0,f8000000");
1337 if (u3 != NULL) {
1338 const u32 *vers = of_get_property(u3, "device-rev", NULL);
1339 if (vers)
1340 if (((*vers) & 0x3f) < 0x34)
1341 u3h = 0;
1342 of_node_put(u3);
1343 }
1344
1345 if (rackmac) {
1346 backside_params.G_d = BACKSIDE_PID_RACK_G_d;
1347 backside_params.input_target = BACKSIDE_PID_RACK_INPUT_TARGET;
1348 backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
1349 backside_params.interval = BACKSIDE_PID_RACK_INTERVAL;
1350 backside_params.G_p = BACKSIDE_PID_RACK_G_p;
1351 backside_params.G_r = BACKSIDE_PID_G_r;
1352 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1353 backside_params.additive = 0;
1354 } else if (u3h) {
1355 backside_params.G_d = BACKSIDE_PID_U3H_G_d;
1356 backside_params.input_target = BACKSIDE_PID_U3H_INPUT_TARGET;
1357 backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
1358 backside_params.interval = BACKSIDE_PID_INTERVAL;
1359 backside_params.G_p = BACKSIDE_PID_G_p;
1360 backside_params.G_r = BACKSIDE_PID_G_r;
1361 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1362 backside_params.additive = 1;
1363 } else {
1364 backside_params.G_d = BACKSIDE_PID_U3_G_d;
1365 backside_params.input_target = BACKSIDE_PID_U3_INPUT_TARGET;
1366 backside_params.output_min = BACKSIDE_PID_U3_OUTPUT_MIN;
1367 backside_params.interval = BACKSIDE_PID_INTERVAL;
1368 backside_params.G_p = BACKSIDE_PID_G_p;
1369 backside_params.G_r = BACKSIDE_PID_G_r;
1370 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1371 backside_params.additive = 1;
1372 }
1373
1374 state->ticks = 1;
1375 state->first = 1;
1376 state->pwm = 50;
1377
1378 state->monitor = attach_i2c_chip(BACKSIDE_MAX_ID, "backside_temp");
1379 if (state->monitor == NULL)
1380 return -ENODEV;
1381
1382 err = device_create_file(&of_dev->dev, &dev_attr_backside_temperature);
1383 err |= device_create_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1384 if (err)
1385 printk(KERN_WARNING "Failed to create attribute file(s)"
1386 " for backside fan\n");
1387
1388 return 0;
1389 }
1390
1391 /*
1392 * Dispose of the state data for the backside control loop
1393 */
1394 static void dispose_backside_state(struct backside_pid_state *state)
1395 {
1396 if (state->monitor == NULL)
1397 return;
1398
1399 device_remove_file(&of_dev->dev, &dev_attr_backside_temperature);
1400 device_remove_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1401
1402 state->monitor = NULL;
1403 }
1404
1405 /*
1406 * Drives bay fan control loop
1407 */
1408 static void do_monitor_drives(struct drives_pid_state *state)
1409 {
1410 s32 temp, integral, derivative;
1411 s64 integ_p, deriv_p, prop_p, sum;
1412 int i, rc;
1413
1414 if (--state->ticks != 0)
1415 return;
1416 state->ticks = DRIVES_PID_INTERVAL;
1417
1418 DBG("drives:\n");
1419
1420 /* Check fan status */
1421 rc = get_rpm_fan(DRIVES_FAN_RPM_INDEX, !RPM_PID_USE_ACTUAL_SPEED);
1422 if (rc < 0) {
1423 printk(KERN_WARNING "Error %d reading drives fan !\n", rc);
1424 /* XXX What do we do now ? */
1425 } else
1426 state->rpm = rc;
1427 DBG(" current rpm: %d\n", state->rpm);
1428
1429 /* Get some sensor readings */
1430 temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
1431 DS1775_TEMP)) << 8;
1432 state->last_temp = temp;
1433 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1434 FIX32TOPRINT(DRIVES_PID_INPUT_TARGET));
1435
1436 /* Store temperature and error in history array */
1437 state->cur_sample = (state->cur_sample + 1) % DRIVES_PID_HISTORY_SIZE;
1438 state->sample_history[state->cur_sample] = temp;
1439 state->error_history[state->cur_sample] = temp - DRIVES_PID_INPUT_TARGET;
1440
1441 /* If first loop, fill the history table */
1442 if (state->first) {
1443 for (i = 0; i < (DRIVES_PID_HISTORY_SIZE - 1); i++) {
1444 state->cur_sample = (state->cur_sample + 1) %
1445 DRIVES_PID_HISTORY_SIZE;
1446 state->sample_history[state->cur_sample] = temp;
1447 state->error_history[state->cur_sample] =
1448 temp - DRIVES_PID_INPUT_TARGET;
1449 }
1450 state->first = 0;
1451 }
1452
1453 /* Calculate the integral term */
1454 sum = 0;
1455 integral = 0;
1456 for (i = 0; i < DRIVES_PID_HISTORY_SIZE; i++)
1457 integral += state->error_history[i];
1458 integral *= DRIVES_PID_INTERVAL;
1459 DBG(" integral: %08x\n", integral);
1460 integ_p = ((s64)DRIVES_PID_G_r) * (s64)integral;
1461 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1462 sum += integ_p;
1463
1464 /* Calculate the derivative term */
1465 derivative = state->error_history[state->cur_sample] -
1466 state->error_history[(state->cur_sample + DRIVES_PID_HISTORY_SIZE - 1)
1467 % DRIVES_PID_HISTORY_SIZE];
1468 derivative /= DRIVES_PID_INTERVAL;
1469 deriv_p = ((s64)DRIVES_PID_G_d) * (s64)derivative;
1470 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1471 sum += deriv_p;
1472
1473 /* Calculate the proportional term */
1474 prop_p = ((s64)DRIVES_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1475 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1476 sum += prop_p;
1477
1478 /* Scale sum */
1479 sum >>= 36;
1480
1481 DBG(" sum: %d\n", (int)sum);
1482 state->rpm += (s32)sum;
1483
1484 state->rpm = max(state->rpm, DRIVES_PID_OUTPUT_MIN);
1485 state->rpm = min(state->rpm, DRIVES_PID_OUTPUT_MAX);
1486
1487 DBG("** DRIVES RPM: %d\n", (int)state->rpm);
1488 set_rpm_fan(DRIVES_FAN_RPM_INDEX, state->rpm);
1489 }
1490
1491 /*
1492 * Initialize the state structure for the drives bay fan control loop
1493 */
1494 static int init_drives_state(struct drives_pid_state *state)
1495 {
1496 int err;
1497
1498 state->ticks = 1;
1499 state->first = 1;
1500 state->rpm = 1000;
1501
1502 state->monitor = attach_i2c_chip(DRIVES_DALLAS_ID, "drives_temp");
1503 if (state->monitor == NULL)
1504 return -ENODEV;
1505
1506 err = device_create_file(&of_dev->dev, &dev_attr_drives_temperature);
1507 err |= device_create_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1508 if (err)
1509 printk(KERN_WARNING "Failed to create attribute file(s)"
1510 " for drives bay fan\n");
1511
1512 return 0;
1513 }
1514
1515 /*
1516 * Dispose of the state data for the drives control loop
1517 */
1518 static void dispose_drives_state(struct drives_pid_state *state)
1519 {
1520 if (state->monitor == NULL)
1521 return;
1522
1523 device_remove_file(&of_dev->dev, &dev_attr_drives_temperature);
1524 device_remove_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1525
1526 state->monitor = NULL;
1527 }
1528
1529 /*
1530 * DIMMs temp control loop
1531 */
1532 static void do_monitor_dimms(struct dimm_pid_state *state)
1533 {
1534 s32 temp, integral, derivative, fan_min;
1535 s64 integ_p, deriv_p, prop_p, sum;
1536 int i;
1537
1538 if (--state->ticks != 0)
1539 return;
1540 state->ticks = DIMM_PID_INTERVAL;
1541
1542 DBG("DIMM:\n");
1543
1544 DBG(" current value: %d\n", state->output);
1545
1546 temp = read_lm87_reg(state->monitor, LM87_INT_TEMP);
1547 if (temp < 0)
1548 return;
1549 temp <<= 16;
1550 state->last_temp = temp;
1551 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1552 FIX32TOPRINT(DIMM_PID_INPUT_TARGET));
1553
1554 /* Store temperature and error in history array */
1555 state->cur_sample = (state->cur_sample + 1) % DIMM_PID_HISTORY_SIZE;
1556 state->sample_history[state->cur_sample] = temp;
1557 state->error_history[state->cur_sample] = temp - DIMM_PID_INPUT_TARGET;
1558
1559 /* If first loop, fill the history table */
1560 if (state->first) {
1561 for (i = 0; i < (DIMM_PID_HISTORY_SIZE - 1); i++) {
1562 state->cur_sample = (state->cur_sample + 1) %
1563 DIMM_PID_HISTORY_SIZE;
1564 state->sample_history[state->cur_sample] = temp;
1565 state->error_history[state->cur_sample] =
1566 temp - DIMM_PID_INPUT_TARGET;
1567 }
1568 state->first = 0;
1569 }
1570
1571 /* Calculate the integral term */
1572 sum = 0;
1573 integral = 0;
1574 for (i = 0; i < DIMM_PID_HISTORY_SIZE; i++)
1575 integral += state->error_history[i];
1576 integral *= DIMM_PID_INTERVAL;
1577 DBG(" integral: %08x\n", integral);
1578 integ_p = ((s64)DIMM_PID_G_r) * (s64)integral;
1579 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1580 sum += integ_p;
1581
1582 /* Calculate the derivative term */
1583 derivative = state->error_history[state->cur_sample] -
1584 state->error_history[(state->cur_sample + DIMM_PID_HISTORY_SIZE - 1)
1585 % DIMM_PID_HISTORY_SIZE];
1586 derivative /= DIMM_PID_INTERVAL;
1587 deriv_p = ((s64)DIMM_PID_G_d) * (s64)derivative;
1588 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1589 sum += deriv_p;
1590
1591 /* Calculate the proportional term */
1592 prop_p = ((s64)DIMM_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1593 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1594 sum += prop_p;
1595
1596 /* Scale sum */
1597 sum >>= 36;
1598
1599 DBG(" sum: %d\n", (int)sum);
1600 state->output = (s32)sum;
1601 state->output = max(state->output, DIMM_PID_OUTPUT_MIN);
1602 state->output = min(state->output, DIMM_PID_OUTPUT_MAX);
1603 dimm_output_clamp = state->output;
1604
1605 DBG("** DIMM clamp value: %d\n", (int)state->output);
1606
1607 /* Backside PID is only every 5 seconds, force backside fan clamping now */
1608 fan_min = (dimm_output_clamp * 100) / 14000;
1609 fan_min = max(fan_min, backside_params.output_min);
1610 if (backside_state.pwm < fan_min) {
1611 backside_state.pwm = fan_min;
1612 DBG(" -> applying clamp to backside fan now: %d !\n", fan_min);
1613 set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, fan_min);
1614 }
1615 }
1616
1617 /*
1618 * Initialize the state structure for the DIMM temp control loop
1619 */
1620 static int init_dimms_state(struct dimm_pid_state *state)
1621 {
1622 state->ticks = 1;
1623 state->first = 1;
1624 state->output = 4000;
1625
1626 state->monitor = attach_i2c_chip(XSERVE_DIMMS_LM87, "dimms_temp");
1627 if (state->monitor == NULL)
1628 return -ENODEV;
1629
1630 if (device_create_file(&of_dev->dev, &dev_attr_dimms_temperature))
1631 printk(KERN_WARNING "Failed to create attribute file"
1632 " for DIMM temperature\n");
1633
1634 return 0;
1635 }
1636
1637 /*
1638 * Dispose of the state data for the DIMM control loop
1639 */
1640 static void dispose_dimms_state(struct dimm_pid_state *state)
1641 {
1642 if (state->monitor == NULL)
1643 return;
1644
1645 device_remove_file(&of_dev->dev, &dev_attr_dimms_temperature);
1646
1647 state->monitor = NULL;
1648 }
1649
1650 /*
1651 * Slots fan control loop
1652 */
1653 static void do_monitor_slots(struct slots_pid_state *state)
1654 {
1655 s32 temp, integral, derivative;
1656 s64 integ_p, deriv_p, prop_p, sum;
1657 int i, rc;
1658
1659 if (--state->ticks != 0)
1660 return;
1661 state->ticks = SLOTS_PID_INTERVAL;
1662
1663 DBG("slots:\n");
1664
1665 /* Check fan status */
1666 rc = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
1667 if (rc < 0) {
1668 printk(KERN_WARNING "Error %d reading slots fan !\n", rc);
1669 /* XXX What do we do now ? */
1670 } else
1671 state->pwm = rc;
1672 DBG(" current pwm: %d\n", state->pwm);
1673
1674 /* Get some sensor readings */
1675 temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
1676 DS1775_TEMP)) << 8;
1677 state->last_temp = temp;
1678 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1679 FIX32TOPRINT(SLOTS_PID_INPUT_TARGET));
1680
1681 /* Store temperature and error in history array */
1682 state->cur_sample = (state->cur_sample + 1) % SLOTS_PID_HISTORY_SIZE;
1683 state->sample_history[state->cur_sample] = temp;
1684 state->error_history[state->cur_sample] = temp - SLOTS_PID_INPUT_TARGET;
1685
1686 /* If first loop, fill the history table */
1687 if (state->first) {
1688 for (i = 0; i < (SLOTS_PID_HISTORY_SIZE - 1); i++) {
1689 state->cur_sample = (state->cur_sample + 1) %
1690 SLOTS_PID_HISTORY_SIZE;
1691 state->sample_history[state->cur_sample] = temp;
1692 state->error_history[state->cur_sample] =
1693 temp - SLOTS_PID_INPUT_TARGET;
1694 }
1695 state->first = 0;
1696 }
1697
1698 /* Calculate the integral term */
1699 sum = 0;
1700 integral = 0;
1701 for (i = 0; i < SLOTS_PID_HISTORY_SIZE; i++)
1702 integral += state->error_history[i];
1703 integral *= SLOTS_PID_INTERVAL;
1704 DBG(" integral: %08x\n", integral);
1705 integ_p = ((s64)SLOTS_PID_G_r) * (s64)integral;
1706 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1707 sum += integ_p;
1708
1709 /* Calculate the derivative term */
1710 derivative = state->error_history[state->cur_sample] -
1711 state->error_history[(state->cur_sample + SLOTS_PID_HISTORY_SIZE - 1)
1712 % SLOTS_PID_HISTORY_SIZE];
1713 derivative /= SLOTS_PID_INTERVAL;
1714 deriv_p = ((s64)SLOTS_PID_G_d) * (s64)derivative;
1715 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1716 sum += deriv_p;
1717
1718 /* Calculate the proportional term */
1719 prop_p = ((s64)SLOTS_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1720 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1721 sum += prop_p;
1722
1723 /* Scale sum */
1724 sum >>= 36;
1725
1726 DBG(" sum: %d\n", (int)sum);
1727 state->pwm = (s32)sum;
1728
1729 state->pwm = max(state->pwm, SLOTS_PID_OUTPUT_MIN);
1730 state->pwm = min(state->pwm, SLOTS_PID_OUTPUT_MAX);
1731
1732 DBG("** DRIVES PWM: %d\n", (int)state->pwm);
1733 set_pwm_fan(SLOTS_FAN_PWM_INDEX, state->pwm);
1734 }
1735
1736 /*
1737 * Initialize the state structure for the slots bay fan control loop
1738 */
1739 static int init_slots_state(struct slots_pid_state *state)
1740 {
1741 int err;
1742
1743 state->ticks = 1;
1744 state->first = 1;
1745 state->pwm = 50;
1746
1747 state->monitor = attach_i2c_chip(XSERVE_SLOTS_LM75, "slots_temp");
1748 if (state->monitor == NULL)
1749 return -ENODEV;
1750
1751 err = device_create_file(&of_dev->dev, &dev_attr_slots_temperature);
1752 err |= device_create_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
1753 if (err)
1754 printk(KERN_WARNING "Failed to create attribute file(s)"
1755 " for slots bay fan\n");
1756
1757 return 0;
1758 }
1759
1760 /*
1761 * Dispose of the state data for the slots control loop
1762 */
1763 static void dispose_slots_state(struct slots_pid_state *state)
1764 {
1765 if (state->monitor == NULL)
1766 return;
1767
1768 device_remove_file(&of_dev->dev, &dev_attr_slots_temperature);
1769 device_remove_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
1770
1771 state->monitor = NULL;
1772 }
1773
1774
1775 static int call_critical_overtemp(void)
1776 {
1777 char *argv[] = { critical_overtemp_path, NULL };
1778 static char *envp[] = { "HOME=/",
1779 "TERM=linux",
1780 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
1781 NULL };
1782
1783 return call_usermodehelper(critical_overtemp_path,
1784 argv, envp, UMH_WAIT_EXEC);
1785 }
1786
1787
1788 /*
1789 * Here's the kernel thread that calls the various control loops
1790 */
1791 static int main_control_loop(void *x)
1792 {
1793 DBG("main_control_loop started\n");
1794
1795 mutex_lock(&driver_lock);
1796
1797 if (start_fcu() < 0) {
1798 printk(KERN_ERR "kfand: failed to start FCU\n");
1799 mutex_unlock(&driver_lock);
1800 goto out;
1801 }
1802
1803 /* Set the PCI fan once for now on non-RackMac */
1804 if (!rackmac)
1805 set_pwm_fan(SLOTS_FAN_PWM_INDEX, SLOTS_FAN_DEFAULT_PWM);
1806
1807 /* Initialize ADCs */
1808 initialize_adc(&cpu_state[0]);
1809 if (cpu_state[1].monitor != NULL)
1810 initialize_adc(&cpu_state[1]);
1811
1812 fcu_tickle_ticks = FCU_TICKLE_TICKS;
1813
1814 mutex_unlock(&driver_lock);
1815
1816 while (state == state_attached) {
1817 unsigned long elapsed, start;
1818
1819 start = jiffies;
1820
1821 mutex_lock(&driver_lock);
1822
1823 /* Tickle the FCU just in case */
1824 if (--fcu_tickle_ticks < 0) {
1825 fcu_tickle_ticks = FCU_TICKLE_TICKS;
1826 tickle_fcu();
1827 }
1828
1829 /* First, we always calculate the new DIMMs state on an Xserve */
1830 if (rackmac)
1831 do_monitor_dimms(&dimms_state);
1832
1833 /* Then, the CPUs */
1834 if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
1835 do_monitor_cpu_combined();
1836 else if (cpu_pid_type == CPU_PID_TYPE_RACKMAC) {
1837 do_monitor_cpu_rack(&cpu_state[0]);
1838 if (cpu_state[1].monitor != NULL)
1839 do_monitor_cpu_rack(&cpu_state[1]);
1840 // better deal with UP
1841 } else {
1842 do_monitor_cpu_split(&cpu_state[0]);
1843 if (cpu_state[1].monitor != NULL)
1844 do_monitor_cpu_split(&cpu_state[1]);
1845 // better deal with UP
1846 }
1847 /* Then, the rest */
1848 do_monitor_backside(&backside_state);
1849 if (rackmac)
1850 do_monitor_slots(&slots_state);
1851 else
1852 do_monitor_drives(&drives_state);
1853 mutex_unlock(&driver_lock);
1854
1855 if (critical_state == 1) {
1856 printk(KERN_WARNING "Temperature control detected a critical condition\n");
1857 printk(KERN_WARNING "Attempting to shut down...\n");
1858 if (call_critical_overtemp()) {
1859 printk(KERN_WARNING "Can't call %s, power off now!\n",
1860 critical_overtemp_path);
1861 machine_power_off();
1862 }
1863 }
1864 if (critical_state > 0)
1865 critical_state++;
1866 if (critical_state > MAX_CRITICAL_STATE) {
1867 printk(KERN_WARNING "Shutdown timed out, power off now !\n");
1868 machine_power_off();
1869 }
1870
1871 // FIXME: Deal with signals
1872 elapsed = jiffies - start;
1873 if (elapsed < HZ)
1874 schedule_timeout_interruptible(HZ - elapsed);
1875 }
1876
1877 out:
1878 DBG("main_control_loop ended\n");
1879
1880 ctrl_task = 0;
1881 complete_and_exit(&ctrl_complete, 0);
1882 }
1883
1884 /*
1885 * Dispose the control loops when tearing down
1886 */
1887 static void dispose_control_loops(void)
1888 {
1889 dispose_cpu_state(&cpu_state[0]);
1890 dispose_cpu_state(&cpu_state[1]);
1891 dispose_backside_state(&backside_state);
1892 dispose_drives_state(&drives_state);
1893 dispose_slots_state(&slots_state);
1894 dispose_dimms_state(&dimms_state);
1895 }
1896
1897 /*
1898 * Create the control loops. U3-0 i2c bus is up, so we can now
1899 * get to the various sensors
1900 */
1901 static int create_control_loops(void)
1902 {
1903 struct device_node *np;
1904
1905 /* Count CPUs from the device-tree, we don't care how many are
1906 * actually used by Linux
1907 */
1908 cpu_count = 0;
1909 for (np = NULL; NULL != (np = of_find_node_by_type(np, "cpu"));)
1910 cpu_count++;
1911
1912 DBG("counted %d CPUs in the device-tree\n", cpu_count);
1913
1914 /* Decide the type of PID algorithm to use based on the presence of
1915 * the pumps, though that may not be the best way, that is good enough
1916 * for now
1917 */
1918 if (rackmac)
1919 cpu_pid_type = CPU_PID_TYPE_RACKMAC;
1920 else if (of_machine_is_compatible("PowerMac7,3")
1921 && (cpu_count > 1)
1922 && fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID
1923 && fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID) {
1924 printk(KERN_INFO "Liquid cooling pumps detected, using new algorithm !\n");
1925 cpu_pid_type = CPU_PID_TYPE_COMBINED;
1926 } else
1927 cpu_pid_type = CPU_PID_TYPE_SPLIT;
1928
1929 /* Create control loops for everything. If any fail, everything
1930 * fails
1931 */
1932 if (init_cpu_state(&cpu_state[0], 0))
1933 goto fail;
1934 if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
1935 fetch_cpu_pumps_minmax();
1936
1937 if (cpu_count > 1 && init_cpu_state(&cpu_state[1], 1))
1938 goto fail;
1939 if (init_backside_state(&backside_state))
1940 goto fail;
1941 if (rackmac && init_dimms_state(&dimms_state))
1942 goto fail;
1943 if (rackmac && init_slots_state(&slots_state))
1944 goto fail;
1945 if (!rackmac && init_drives_state(&drives_state))
1946 goto fail;
1947
1948 DBG("all control loops up !\n");
1949
1950 return 0;
1951
1952 fail:
1953 DBG("failure creating control loops, disposing\n");
1954
1955 dispose_control_loops();
1956
1957 return -ENODEV;
1958 }
1959
1960 /*
1961 * Start the control loops after everything is up, that is create
1962 * the thread that will make them run
1963 */
1964 static void start_control_loops(void)
1965 {
1966 init_completion(&ctrl_complete);
1967
1968 ctrl_task = kthread_run(main_control_loop, NULL, "kfand");
1969 }
1970
1971 /*
1972 * Stop the control loops when tearing down
1973 */
1974 static void stop_control_loops(void)
1975 {
1976 if (ctrl_task)
1977 wait_for_completion(&ctrl_complete);
1978 }
1979
1980 /*
1981 * Attach to the i2c FCU after detecting U3-1 bus
1982 */
1983 static int attach_fcu(void)
1984 {
1985 fcu = attach_i2c_chip(FAN_CTRLER_ID, "fcu");
1986 if (fcu == NULL)
1987 return -ENODEV;
1988
1989 DBG("FCU attached\n");
1990
1991 return 0;
1992 }
1993
1994 /*
1995 * Detach from the i2c FCU when tearing down
1996 */
1997 static void detach_fcu(void)
1998 {
1999 fcu = NULL;
2000 }
2001
2002 /*
2003 * Attach to the i2c controller. We probe the various chips based
2004 * on the device-tree nodes and build everything for the driver to
2005 * run, we then kick the driver monitoring thread
2006 */
2007 static int therm_pm72_attach(struct i2c_adapter *adapter)
2008 {
2009 mutex_lock(&driver_lock);
2010
2011 /* Check state */
2012 if (state == state_detached)
2013 state = state_attaching;
2014 if (state != state_attaching) {
2015 mutex_unlock(&driver_lock);
2016 return 0;
2017 }
2018
2019 /* Check if we are looking for one of these */
2020 if (u3_0 == NULL && !strcmp(adapter->name, "u3 0")) {
2021 u3_0 = adapter;
2022 DBG("found U3-0\n");
2023 if (k2 || !rackmac)
2024 if (create_control_loops())
2025 u3_0 = NULL;
2026 } else if (u3_1 == NULL && !strcmp(adapter->name, "u3 1")) {
2027 u3_1 = adapter;
2028 DBG("found U3-1, attaching FCU\n");
2029 if (attach_fcu())
2030 u3_1 = NULL;
2031 } else if (k2 == NULL && !strcmp(adapter->name, "mac-io 0")) {
2032 k2 = adapter;
2033 DBG("Found K2\n");
2034 if (u3_0 && rackmac)
2035 if (create_control_loops())
2036 k2 = NULL;
2037 }
2038 /* We got all we need, start control loops */
2039 if (u3_0 != NULL && u3_1 != NULL && (k2 || !rackmac)) {
2040 DBG("everything up, starting control loops\n");
2041 state = state_attached;
2042 start_control_loops();
2043 }
2044 mutex_unlock(&driver_lock);
2045
2046 return 0;
2047 }
2048
2049 static int therm_pm72_probe(struct i2c_client *client,
2050 const struct i2c_device_id *id)
2051 {
2052 /* Always succeed, the real work was done in therm_pm72_attach() */
2053 return 0;
2054 }
2055
2056 /*
2057 * Called when any of the devices which participates into thermal management
2058 * is going away.
2059 */
2060 static int therm_pm72_remove(struct i2c_client *client)
2061 {
2062 struct i2c_adapter *adapter = client->adapter;
2063
2064 mutex_lock(&driver_lock);
2065
2066 if (state != state_detached)
2067 state = state_detaching;
2068
2069 /* Stop control loops if any */
2070 DBG("stopping control loops\n");
2071 mutex_unlock(&driver_lock);
2072 stop_control_loops();
2073 mutex_lock(&driver_lock);
2074
2075 if (u3_0 != NULL && !strcmp(adapter->name, "u3 0")) {
2076 DBG("lost U3-0, disposing control loops\n");
2077 dispose_control_loops();
2078 u3_0 = NULL;
2079 }
2080
2081 if (u3_1 != NULL && !strcmp(adapter->name, "u3 1")) {
2082 DBG("lost U3-1, detaching FCU\n");
2083 detach_fcu();
2084 u3_1 = NULL;
2085 }
2086 if (u3_0 == NULL && u3_1 == NULL)
2087 state = state_detached;
2088
2089 mutex_unlock(&driver_lock);
2090
2091 return 0;
2092 }
2093
2094 /*
2095 * i2c_driver structure to attach to the host i2c controller
2096 */
2097
2098 static const struct i2c_device_id therm_pm72_id[] = {
2099 /*
2100 * Fake device name, thermal management is done by several
2101 * chips but we don't need to differentiate between them at
2102 * this point.
2103 */
2104 { "therm_pm72", 0 },
2105 { }
2106 };
2107
2108 static struct i2c_driver therm_pm72_driver = {
2109 .driver = {
2110 .name = "therm_pm72",
2111 },
2112 .attach_adapter = therm_pm72_attach,
2113 .probe = therm_pm72_probe,
2114 .remove = therm_pm72_remove,
2115 .id_table = therm_pm72_id,
2116 };
2117
2118 static int fan_check_loc_match(const char *loc, int fan)
2119 {
2120 char tmp[64];
2121 char *c, *e;
2122
2123 strlcpy(tmp, fcu_fans[fan].loc, 64);
2124
2125 c = tmp;
2126 for (;;) {
2127 e = strchr(c, ',');
2128 if (e)
2129 *e = 0;
2130 if (strcmp(loc, c) == 0)
2131 return 1;
2132 if (e == NULL)
2133 break;
2134 c = e + 1;
2135 }
2136 return 0;
2137 }
2138
2139 static void fcu_lookup_fans(struct device_node *fcu_node)
2140 {
2141 struct device_node *np = NULL;
2142 int i;
2143
2144 /* The table is filled by default with values that are suitable
2145 * for the old machines without device-tree informations. We scan
2146 * the device-tree and override those values with whatever is
2147 * there
2148 */
2149
2150 DBG("Looking up FCU controls in device-tree...\n");
2151
2152 while ((np = of_get_next_child(fcu_node, np)) != NULL) {
2153 int type = -1;
2154 const char *loc;
2155 const u32 *reg;
2156
2157 DBG(" control: %s, type: %s\n", np->name, np->type);
2158
2159 /* Detect control type */
2160 if (!strcmp(np->type, "fan-rpm-control") ||
2161 !strcmp(np->type, "fan-rpm"))
2162 type = FCU_FAN_RPM;
2163 if (!strcmp(np->type, "fan-pwm-control") ||
2164 !strcmp(np->type, "fan-pwm"))
2165 type = FCU_FAN_PWM;
2166 /* Only care about fans for now */
2167 if (type == -1)
2168 continue;
2169
2170 /* Lookup for a matching location */
2171 loc = of_get_property(np, "location", NULL);
2172 reg = of_get_property(np, "reg", NULL);
2173 if (loc == NULL || reg == NULL)
2174 continue;
2175 DBG(" matching location: %s, reg: 0x%08x\n", loc, *reg);
2176
2177 for (i = 0; i < FCU_FAN_COUNT; i++) {
2178 int fan_id;
2179
2180 if (!fan_check_loc_match(loc, i))
2181 continue;
2182 DBG(" location match, index: %d\n", i);
2183 fcu_fans[i].id = FCU_FAN_ABSENT_ID;
2184 if (type != fcu_fans[i].type) {
2185 printk(KERN_WARNING "therm_pm72: Fan type mismatch "
2186 "in device-tree for %s\n", np->full_name);
2187 break;
2188 }
2189 if (type == FCU_FAN_RPM)
2190 fan_id = ((*reg) - 0x10) / 2;
2191 else
2192 fan_id = ((*reg) - 0x30) / 2;
2193 if (fan_id > 7) {
2194 printk(KERN_WARNING "therm_pm72: Can't parse "
2195 "fan ID in device-tree for %s\n", np->full_name);
2196 break;
2197 }
2198 DBG(" fan id -> %d, type -> %d\n", fan_id, type);
2199 fcu_fans[i].id = fan_id;
2200 }
2201 }
2202
2203 /* Now dump the array */
2204 printk(KERN_INFO "Detected fan controls:\n");
2205 for (i = 0; i < FCU_FAN_COUNT; i++) {
2206 if (fcu_fans[i].id == FCU_FAN_ABSENT_ID)
2207 continue;
2208 printk(KERN_INFO " %d: %s fan, id %d, location: %s\n", i,
2209 fcu_fans[i].type == FCU_FAN_RPM ? "RPM" : "PWM",
2210 fcu_fans[i].id, fcu_fans[i].loc);
2211 }
2212 }
2213
2214 static int fcu_of_probe(struct of_device* dev, const struct of_device_id *match)
2215 {
2216 state = state_detached;
2217
2218 /* Lookup the fans in the device tree */
2219 fcu_lookup_fans(dev->node);
2220
2221 /* Add the driver */
2222 return i2c_add_driver(&therm_pm72_driver);
2223 }
2224
2225 static int fcu_of_remove(struct of_device* dev)
2226 {
2227 i2c_del_driver(&therm_pm72_driver);
2228
2229 return 0;
2230 }
2231
2232 static const struct of_device_id fcu_match[] =
2233 {
2234 {
2235 .type = "fcu",
2236 },
2237 {},
2238 };
2239
2240 static struct of_platform_driver fcu_of_platform_driver =
2241 {
2242 .name = "temperature",
2243 .match_table = fcu_match,
2244 .probe = fcu_of_probe,
2245 .remove = fcu_of_remove
2246 };
2247
2248 /*
2249 * Check machine type, attach to i2c controller
2250 */
2251 static int __init therm_pm72_init(void)
2252 {
2253 struct device_node *np;
2254
2255 rackmac = of_machine_is_compatible("RackMac3,1");
2256
2257 if (!of_machine_is_compatible("PowerMac7,2") &&
2258 !of_machine_is_compatible("PowerMac7,3") &&
2259 !rackmac)
2260 return -ENODEV;
2261
2262 printk(KERN_INFO "PowerMac G5 Thermal control driver %s\n", VERSION);
2263
2264 np = of_find_node_by_type(NULL, "fcu");
2265 if (np == NULL) {
2266 /* Some machines have strangely broken device-tree */
2267 np = of_find_node_by_path("/u3@0,f8000000/i2c@f8001000/fan@15e");
2268 if (np == NULL) {
2269 printk(KERN_ERR "Can't find FCU in device-tree !\n");
2270 return -ENODEV;
2271 }
2272 }
2273 of_dev = of_platform_device_create(np, "temperature", NULL);
2274 if (of_dev == NULL) {
2275 printk(KERN_ERR "Can't register FCU platform device !\n");
2276 return -ENODEV;
2277 }
2278
2279 of_register_platform_driver(&fcu_of_platform_driver);
2280
2281 return 0;
2282 }
2283
2284 static void __exit therm_pm72_exit(void)
2285 {
2286 of_unregister_platform_driver(&fcu_of_platform_driver);
2287
2288 if (of_dev)
2289 of_device_unregister(of_dev);
2290 }
2291
2292 module_init(therm_pm72_init);
2293 module_exit(therm_pm72_exit);
2294
2295 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
2296 MODULE_DESCRIPTION("Driver for Apple's PowerMac G5 thermal control");
2297 MODULE_LICENSE("GPL");
2298
This page took 0.083094 seconds and 5 git commands to generate.