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