111 #include <linux/types.h>
112 #include <linux/module.h>
113 #include <linux/errno.h>
114 #include <linux/kernel.h>
116 #include <linux/sched.h>
119 #include <linux/wait.h>
120 #include <linux/reboot.h>
122 #include <linux/i2c.h>
127 #include <asm/prom.h>
128 #include <asm/machdep.h>
130 #include <asm/sections.h>
135 #define VERSION "1.3"
140 #define DBG(args...) printk(args)
142 #define DBG(args...) do { } while(0)
163 static int cpu_pid_type;
166 static int critical_state;
168 static s32 dimm_output_clamp;
169 static int fcu_rpm_shift;
170 static int fcu_tickle_ticks;
182 #define CPU_PID_TYPE_SPLIT 0
183 #define CPU_PID_TYPE_COMBINED 1
184 #define CPU_PID_TYPE_RACKMAC 2
198 #define FCU_FAN_RPM 0
199 #define FCU_FAN_PWM 1
201 #define FCU_FAN_ABSENT_ID -1
203 #define FCU_FAN_COUNT ARRAY_SIZE(fcu_fans)
207 .loc =
"BACKSIDE,SYS CTRLR FAN",
217 .loc =
"SLOT,PCI FAN",
222 .loc =
"CPU A INTAKE",
227 .loc =
"CPU A EXHAUST",
232 .loc =
"CPU B INTAKE",
237 .loc =
"CPU B EXHAUST",
293 static struct i2c_client *attach_i2c_chip(
int id,
const char *
name)
309 info.addr = (
id >> 1) & 0x7f;
313 printk(
KERN_ERR "therm_pm72: Failed to attach to i2c ID 0x%x\n",
id);
373 buf[1] = (state->
adc_config & 0x1f) | (chan << 5);
388 data = ((
u16)buf[0]) << 8 | (
u16)buf[1];
391 DBG(
"Error reading ADC, retrying...\n");
416 DBG(
"Error reading LM87, retrying...\n");
425 static int fan_read_reg(
int reg,
unsigned char *buf,
int nb)
433 if (nw > 0 || (nw < 0 && nw != -
EIO) || tries >= 100)
445 if (nr > 0 || (nr < 0 && nr != -
ENODEV) || tries >= 100)
455 static int fan_write_reg(
int reg,
const unsigned char *
ptr,
int nb)
458 unsigned char buf[16];
466 if (nw > 0 || (nw < 0 && nw != -
EIO) || tries >= 100)
476 static int start_fcu(
void)
478 unsigned char buf = 0xff;
481 rc = fan_write_reg(0
xe, &buf, 1);
484 rc = fan_write_reg(0x2e, &buf, 1);
487 rc = fan_read_reg(0, &buf, 1);
490 fcu_rpm_shift = (buf == 1) ? 2 : 3;
497 static int set_rpm_fan(
int fan_index,
int rpm)
499 unsigned char buf[2];
504 id = fcu_fans[fan_index].
id;
508 min = 2400 >> fcu_rpm_shift;
509 max = 56000 >> fcu_rpm_shift;
515 buf[0] = rpm >> (8 - fcu_rpm_shift);
516 buf[1] = rpm << fcu_rpm_shift;
517 rc = fan_write_reg(0x10 + (
id * 2), buf, 2);
523 static int get_rpm_fan(
int fan_index,
int programmed)
525 unsigned char failure;
527 unsigned char buf[2];
528 int rc,
id, reg_base;
532 id = fcu_fans[fan_index].
id;
536 rc = fan_read_reg(0
xb, &failure, 1);
539 if ((failure & (1 <<
id)) != 0)
541 rc = fan_read_reg(0xd, &active, 1);
544 if ((active & (1 <<
id)) == 0)
548 reg_base = programmed ? 0x10 : 0x11;
549 rc = fan_read_reg(reg_base + (
id * 2), buf, 2);
553 return (buf[0] << (8 - fcu_rpm_shift)) | buf[1] >> fcu_rpm_shift;
556 static int set_pwm_fan(
int fan_index,
int pwm)
558 unsigned char buf[2];
563 id = fcu_fans[fan_index].
id;
571 pwm = (pwm * 2559) / 1000;
573 rc = fan_write_reg(0x30 + (
id * 2), buf, 1);
579 static int get_pwm_fan(
int fan_index)
581 unsigned char failure;
583 unsigned char buf[2];
588 id = fcu_fans[fan_index].
id;
592 rc = fan_read_reg(0x2b, &failure, 1);
595 if ((failure & (1 <<
id)) != 0)
597 rc = fan_read_reg(0x2d, &active, 1);
600 if ((active & (1 <<
id)) == 0)
604 rc = fan_read_reg(0x30 + (
id * 2), buf, 1);
608 return (buf[0] * 1000) / 2559;
611 static void tickle_fcu(
void)
617 DBG(
"FCU Tickle, slots fan is: %d\n", pwm);
646 sprintf(nodename,
"/u3@0,f8000000/i2c@f8001000/cpuid@a%d", cpu ? 2 : 0);
649 printk(
KERN_ERR "therm_pm72: Failed to retrieve cpuid node from device-tree\n");
654 printk(
KERN_ERR "therm_pm72: Failed to retrieve cpuid property from device-tree\n");
664 static void fetch_cpu_pumps_minmax(
void)
673 memcpy(&tmp, &state0->
mpu.processor_part_num, 8);
674 if (tmp[0] != 0xffff && tmp[1] != 0xffff) {
675 pump_min =
max(pump_min, tmp[0]);
678 if (tmp[2] != 0xffff && tmp[3] != 0xffff) {
679 pump_min =
max(pump_min, tmp[2]);
704 #define BUILD_SHOW_FUNC_FIX(name, data) \
705 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
708 mutex_lock(&driver_lock); \
709 r = sprintf(buf, "%d.%03d", FIX32TOPRINT(data)); \
710 mutex_unlock(&driver_lock); \
713 #define BUILD_SHOW_FUNC_INT(name, data) \
714 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
716 return sprintf(buf, "%d", data); \
743 static
DEVICE_ATTR(cpu0_voltage,S_IRUGO,show_cpu0_voltage,NULL);
744 static
DEVICE_ATTR(cpu0_current,S_IRUGO,show_cpu0_current,NULL);
745 static
DEVICE_ATTR(cpu0_exhaust_fan_rpm,S_IRUGO,show_cpu0_exhaust_fan_rpm,NULL);
746 static
DEVICE_ATTR(cpu0_intake_fan_rpm,S_IRUGO,show_cpu0_intake_fan_rpm,NULL);
748 static
DEVICE_ATTR(cpu1_temperature,S_IRUGO,show_cpu1_temperature,NULL);
749 static
DEVICE_ATTR(cpu1_voltage,S_IRUGO,show_cpu1_voltage,NULL);
750 static
DEVICE_ATTR(cpu1_current,S_IRUGO,show_cpu1_current,NULL);
751 static
DEVICE_ATTR(cpu1_exhaust_fan_rpm,S_IRUGO,show_cpu1_exhaust_fan_rpm,NULL);
752 static
DEVICE_ATTR(cpu1_intake_fan_rpm,S_IRUGO,show_cpu1_intake_fan_rpm,NULL);
754 static
DEVICE_ATTR(backside_temperature,S_IRUGO,show_backside_temperature,NULL);
755 static
DEVICE_ATTR(backside_fan_pwm,S_IRUGO,show_backside_fan_pwm,NULL);
757 static
DEVICE_ATTR(drives_temperature,S_IRUGO,show_drives_temperature,NULL);
758 static
DEVICE_ATTR(drives_fan_rpm,S_IRUGO,show_drives_fan_rpm,NULL);
760 static
DEVICE_ATTR(slots_temperature,S_IRUGO,show_slots_temperature,NULL);
761 static
DEVICE_ATTR(slots_fan_pwm,S_IRUGO,show_slots_fan_pwm,NULL);
763 static
DEVICE_ATTR(dimms_temperature,S_IRUGO,show_dimms_temperature,NULL);
771 s32 ltemp, volts, amps;
775 *temp = state->cur_temp;
776 *power = state->cur_power;
779 index = (state->index == 0) ?
782 index = (state->index == 0) ?
791 DBG(
" cpu %d, fan reading error !\n", state->index);
794 DBG(
" cpu %d, exhaust RPM: %d\n", state->index, state->rpm);
798 ltemp = read_smon_adc(state, 1);
804 DBG(
" cpu %d, temp reading error !\n", state->index);
808 DBG(
" cpu %d, temp raw: %04x, m_diode: %04x, b_diode: %04x\n",
810 ltemp, state->mpu.mdiode, state->mpu.bdiode);
811 *temp = ((
s32)ltemp * (
s32)state->mpu.mdiode + ((
s32)state->mpu.bdiode << 12)) >> 2;
812 state->last_temp = *
temp;
819 volts = read_smon_adc(state, 3);
820 amps = read_smon_adc(state, 4);
827 *power = (((
u64)volts) * ((
u64)amps)) >> 16;
828 state->voltage = volts;
829 state->current_a = amps;
830 state->last_power = *power;
832 DBG(
" cpu %d, current: %d.%03d, voltage: %d.%03d, power: %d.%03d W\n",
841 s32 power_target, integral, derivative, proportional, adj_in_target, sval;
842 s64 integ_p, deriv_p, prop_p,
sum;
848 power_target = ((
u32)(state->
mpu.pmaxh - state->
mpu.padjmax)) << 16;
849 DBG(
" power target: %d.%03d, error: %d.%03d\n",
879 DBG(
" integral: %08x\n", integral);
888 integ_p = ((
s64)state->
mpu.pid_gr) * (
s64)integral;
889 DBG(
" integ_p: %d\n", (
int)(integ_p >> 36));
890 sval = (state->
mpu.tmax << 16) - ((integ_p >> 20) & 0xffffffff);
891 adj_in_target = (state->
mpu.ttarget << 16);
892 if (adj_in_target > sval)
893 adj_in_target = sval;
894 DBG(
" adj_in_target: %d.%03d, ttarget: %d\n",
FIX32TOPRINT(adj_in_target),
902 deriv_p = ((
s64)state->
mpu.pid_gd) * (
s64)derivative;
903 DBG(
" deriv_p: %d\n", (
int)(deriv_p >> 36));
907 proportional = temp - adj_in_target;
908 prop_p = ((
s64)state->
mpu.pid_gp) * (
s64)proportional;
909 DBG(
" prop_p: %d\n", (
int)(prop_p >> 36));
915 DBG(
" sum: %d\n", (
int)sum);
919 static void do_monitor_cpu_combined(
void)
924 s32 temp_combi, power_combi;
925 int rc, intake, pump;
927 rc = do_read_one_cpu_values(state0, &temp0, &power0);
932 rc = do_read_one_cpu_values(state1, &temp1, &power1);
939 temp_combi =
max(temp0, temp1);
940 power_combi =
max(power0, power1);
945 if (temp_combi >= ((state0->
mpu.tmax + 8) << 16)) {
949 }
else if (temp_combi > (state0->
mpu.tmax << 16)) {
952 temp_combi >> 16, state0->
mpu.tmax, state0->
overtemp);
962 state0->
rpm = state0->
mpu.rmaxn_exhaust_fan;
969 do_cpu_pid(state0, temp_combi, power_combi);
972 state0->
rpm =
max(state0->
rpm, (
int)state0->
mpu.rminn_exhaust_fan);
973 state0->
rpm =
min(state0->
rpm, (
int)state0->
mpu.rmaxn_exhaust_fan);
977 intake =
max(intake, (
int)state0->
mpu.rminn_intake_fan);
978 intake =
min(intake, (
int)state0->
mpu.rmaxn_intake_fan);
983 state0->
mpu.rmaxn_exhaust_fan;
989 state1->
rpm = state0->
rpm;
992 DBG(
"** CPU %d RPM: %d Ex, %d, Pump: %d, In, overtemp: %d\n",
1011 static void do_monitor_cpu_split(
struct cpu_pid_state *state)
1017 rc = do_read_one_cpu_values(state, &temp, &power);
1025 if (temp >= ((state->
mpu.tmax + 8) << 16)) {
1028 state->
index, temp >> 16);
1030 }
else if (temp > (state->
mpu.tmax << 16)) {
1037 state->
index, temp >> 16);
1043 state->
rpm = state->
mpu.rmaxn_exhaust_fan;
1049 do_cpu_pid(state, temp, power);
1052 state->
rpm =
max(state->
rpm, (
int)state->
mpu.rminn_exhaust_fan);
1053 state->
rpm =
min(state->
rpm, (
int)state->
mpu.rmaxn_exhaust_fan);
1057 intake =
max(intake, (
int)state->
mpu.rminn_intake_fan);
1058 intake =
min(intake, (
int)state->
mpu.rmaxn_intake_fan);
1062 DBG(
"** CPU %d RPM: %d Ex, %d In, overtemp: %d\n",
1070 if (state->
index == 0) {
1079 static void do_monitor_cpu_rack(
struct cpu_pid_state *state)
1085 rc = do_read_one_cpu_values(state, &temp, &power);
1093 if (temp >= ((state->
mpu.tmax + 8) << 16)) {
1096 state->
index, temp >> 16);
1098 }
else if (temp > (state->
mpu.tmax << 16)) {
1105 state->
index, temp >> 16);
1116 do_cpu_pid(state, temp, power);
1119 fan_min = dimm_output_clamp;
1120 fan_min =
max(fan_min, (
int)state->
mpu.rminn_intake_fan);
1122 DBG(
" CPU min mpu = %d, min dimm = %d\n",
1123 state->
mpu.rminn_intake_fan, dimm_output_clamp);
1125 state->
rpm =
max(state->
rpm, (
int)fan_min);
1126 state->
rpm =
min(state->
rpm, (
int)state->
mpu.rmaxn_intake_fan);
1130 DBG(
"** CPU %d RPM: %d overtemp: %d\n",
1138 if (state->
index == 0) {
1165 else if (index == 1)
1178 DBG(
"CPU %d Using %d power history entries\n", index, state->
count_power);
1195 "files for CPU %d\n", index);
1207 static void dispose_processor_state(
struct cpu_pid_state *state)
1212 if (state->
index == 0) {
1234 s32 temp, integral, derivative, fan_min;
1235 s64 integ_p, deriv_p, prop_p,
sum;
1238 if (--state->
ticks != 0)
1240 state->
ticks = backside_params.interval;
1251 DBG(
" current pwm: %d\n", state->
pwm);
1271 temp - backside_params.input_target;
1281 integral *= backside_params.interval;
1282 DBG(
" integral: %08x\n", integral);
1283 integ_p = ((
s64)backside_params.G_r) * (
s64)integral;
1284 DBG(
" integ_p: %d\n", (
int)(integ_p >> 36));
1290 % BACKSIDE_PID_HISTORY_SIZE];
1291 derivative /= backside_params.interval;
1292 deriv_p = ((
s64)backside_params.G_d) * (
s64)derivative;
1293 DBG(
" deriv_p: %d\n", (
int)(deriv_p >> 36));
1298 DBG(
" prop_p: %d\n", (
int)(prop_p >> 36));
1304 DBG(
" sum: %d\n", (
int)sum);
1305 if (backside_params.additive)
1311 fan_min = (dimm_output_clamp * 100) / 14000;
1312 fan_min =
max(fan_min, backside_params.output_min);
1315 state->
pwm =
min(state->
pwm, backside_params.output_max);
1317 DBG(
"** BACKSIDE PWM: %d\n", (
int)state->
pwm);
1338 if (((*vers) & 0x3f) < 0x34)
1351 backside_params.additive = 0;
1360 backside_params.additive = 1;
1369 backside_params.additive = 1;
1384 " for backside fan\n");
1408 s32 temp, integral, derivative;
1409 s64 integ_p, deriv_p, prop_p,
sum;
1412 if (--state->
ticks != 0)
1425 DBG(
" current rpm: %d\n", state->
rpm);
1457 DBG(
" integral: %08x\n", integral);
1459 DBG(
" integ_p: %d\n", (
int)(integ_p >> 36));
1465 % DRIVES_PID_HISTORY_SIZE];
1468 DBG(
" deriv_p: %d\n", (
int)(deriv_p >> 36));
1473 DBG(
" prop_p: %d\n", (
int)(prop_p >> 36));
1479 DBG(
" sum: %d\n", (
int)sum);
1485 DBG(
"** DRIVES RPM: %d\n", (
int)state->
rpm);
1508 " for drives bay fan\n");
1532 s32 temp, integral, derivative, fan_min;
1533 s64 integ_p, deriv_p, prop_p,
sum;
1536 if (--state->
ticks != 0)
1542 DBG(
" current value: %d\n", state->
output);
1575 DBG(
" integral: %08x\n", integral);
1577 DBG(
" integ_p: %d\n", (
int)(integ_p >> 36));
1583 % DIMM_PID_HISTORY_SIZE];
1586 DBG(
" deriv_p: %d\n", (
int)(deriv_p >> 36));
1591 DBG(
" prop_p: %d\n", (
int)(prop_p >> 36));
1597 DBG(
" sum: %d\n", (
int)sum);
1601 dimm_output_clamp = state->
output;
1603 DBG(
"** DIMM clamp value: %d\n", (
int)state->
output);
1606 fan_min = (dimm_output_clamp * 100) / 14000;
1607 fan_min =
max(fan_min, backside_params.output_min);
1608 if (backside_state.pwm < fan_min) {
1609 backside_state.pwm = fan_min;
1610 DBG(
" -> applying clamp to backside fan now: %d !\n", fan_min);
1630 " for DIMM temperature\n");
1653 s32 temp, integral, derivative;
1654 s64 integ_p, deriv_p, prop_p,
sum;
1657 if (--state->
ticks != 0)
1670 DBG(
" current pwm: %d\n", state->
pwm);
1702 DBG(
" integral: %08x\n", integral);
1704 DBG(
" integ_p: %d\n", (
int)(integ_p >> 36));
1710 % SLOTS_PID_HISTORY_SIZE];
1713 DBG(
" deriv_p: %d\n", (
int)(deriv_p >> 36));
1718 DBG(
" prop_p: %d\n", (
int)(prop_p >> 36));
1724 DBG(
" sum: %d\n", (
int)sum);
1730 DBG(
"** DRIVES PWM: %d\n", (
int)state->
pwm);
1753 " for slots bay fan\n");
1773 static int call_critical_overtemp(
void)
1775 char *argv[] = { critical_overtemp_path,
NULL };
1776 static char *envp[] = {
"HOME=/",
1778 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
1781 return call_usermodehelper(critical_overtemp_path,
1789 static int main_control_loop(
void *
x)
1791 DBG(
"main_control_loop started\n");
1795 if (start_fcu() < 0) {
1806 initialize_adc(&processor_state[0]);
1807 if (processor_state[1].monitor !=
NULL)
1808 initialize_adc(&processor_state[1]);
1815 unsigned long elapsed,
start;
1822 if (--fcu_tickle_ticks < 0) {
1829 do_monitor_dimms(&dimms_state);
1833 do_monitor_cpu_combined();
1835 do_monitor_cpu_rack(&processor_state[0]);
1836 if (processor_state[1].monitor !=
NULL)
1837 do_monitor_cpu_rack(&processor_state[1]);
1840 do_monitor_cpu_split(&processor_state[0]);
1841 if (processor_state[1].monitor !=
NULL)
1842 do_monitor_cpu_split(&processor_state[1]);
1846 do_monitor_backside(&backside_state);
1848 do_monitor_slots(&slots_state);
1850 do_monitor_drives(&drives_state);
1853 if (critical_state == 1) {
1856 if (call_critical_overtemp()) {
1858 critical_overtemp_path);
1862 if (critical_state > 0)
1876 DBG(
"main_control_loop ended\n");
1885 static void dispose_control_loops(
void)
1887 dispose_processor_state(&processor_state[0]);
1888 dispose_processor_state(&processor_state[1]);
1889 dispose_backside_state(&backside_state);
1890 dispose_drives_state(&drives_state);
1891 dispose_slots_state(&slots_state);
1892 dispose_dimms_state(&dimms_state);
1899 static int create_control_loops(
void)
1910 DBG(
"counted %d CPUs in the device-tree\n",
cpu_count);
1922 printk(
KERN_INFO "Liquid cooling pumps detected, using new algorithm !\n");
1930 if (init_processor_state(&processor_state[0], 0))
1933 fetch_cpu_pumps_minmax();
1935 if (
cpu_count > 1 && init_processor_state(&processor_state[1], 1))
1937 if (init_backside_state(&backside_state))
1939 if (rackmac && init_dimms_state(&dimms_state))
1941 if (rackmac && init_slots_state(&slots_state))
1943 if (!rackmac && init_drives_state(&drives_state))
1946 DBG(
"all control loops up !\n");
1951 DBG(
"failure creating control loops, disposing\n");
1953 dispose_control_loops();
1962 static void start_control_loops(
void)
1964 init_completion(&ctrl_complete);
1972 static void stop_control_loops(
void)
1981 static int attach_fcu(
void)
1987 DBG(
"FCU attached\n");
1995 static void detach_fcu(
void)
2020 DBG(
"found U3-0\n");
2022 if (create_control_loops())
2026 DBG(
"found U3-1, attaching FCU\n");
2032 if (u3_0 && rackmac)
2033 if (create_control_loops())
2037 if (u3_0 !=
NULL && u3_1 !=
NULL && (k2 || !rackmac)) {
2038 DBG(
"everything up, starting control loops\n");
2040 start_control_loops();
2068 DBG(
"stopping control loops\n");
2070 stop_control_loops();
2074 DBG(
"lost U3-0, disposing control loops\n");
2075 dispose_control_loops();
2080 DBG(
"lost U3-1, detaching FCU\n");
2102 {
"therm_pm72", 0 },
2106 static struct i2c_driver therm_pm72_driver = {
2108 .name =
"therm_pm72",
2110 .attach_adapter = therm_pm72_attach,
2111 .probe = therm_pm72_probe,
2112 .remove = therm_pm72_remove,
2113 .id_table = therm_pm72_id,
2116 static int fan_check_loc_match(
const char *
loc,
int fan)
2121 strlcpy(tmp, fcu_fans[fan].loc, 64);
2137 static void fcu_lookup_fans(
struct device_node *fcu_node)
2148 DBG(
"Looking up FCU controls in device-tree...\n");
2155 DBG(
" control: %s, type: %s\n", np->
name, np->
type);
2173 DBG(
" matching location: %s, reg: 0x%08x\n", loc, *reg);
2178 if (!fan_check_loc_match(loc, i))
2180 DBG(
" location match, index: %d\n", i);
2182 if (type != fcu_fans[i].type) {
2184 "in device-tree for %s\n", np->
full_name);
2188 fan_id = ((*reg) - 0x10) / 2;
2190 fan_id = ((*reg) - 0x30) / 2;
2193 "fan ID in device-tree for %s\n", np->
full_name);
2196 DBG(
" fan id -> %d, type -> %d\n", fan_id, type);
2197 fcu_fans[
i].
id = fan_id;
2208 fcu_fans[i].
id, fcu_fans[i].loc);
2220 fcu_lookup_fans(dev->
dev.of_node);
2223 return i2c_add_driver(&therm_pm72_driver);
2245 .name =
"temperature",
2247 .of_match_table = fcu_match,
2249 .probe = fcu_of_probe,
2250 .remove = fcu_of_remove
2256 static int __init therm_pm72_init(
void)
2268 static void __exit therm_pm72_exit(
void)