windfarm_pm72.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /*
  2. * Windfarm PowerMac thermal control.
  3. * Control loops for PowerMac7,2 and 7,3
  4. *
  5. * Copyright (C) 2012 Benjamin Herrenschmidt, IBM Corp.
  6. *
  7. * Use and redistribute under the terms of the GNU GPL v2.
  8. */
  9. #include <linux/types.h>
  10. #include <linux/errno.h>
  11. #include <linux/kernel.h>
  12. #include <linux/device.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/reboot.h>
  15. #include <asm/prom.h>
  16. #include <asm/smu.h>
  17. #include "windfarm.h"
  18. #include "windfarm_pid.h"
  19. #include "windfarm_mpu.h"
  20. #define VERSION "1.0"
  21. #undef DEBUG
  22. #undef LOTSA_DEBUG
  23. #ifdef DEBUG
  24. #define DBG(args...) printk(args)
  25. #else
  26. #define DBG(args...) do { } while(0)
  27. #endif
  28. #ifdef LOTSA_DEBUG
  29. #define DBG_LOTS(args...) printk(args)
  30. #else
  31. #define DBG_LOTS(args...) do { } while(0)
  32. #endif
  33. /* define this to force CPU overtemp to 60 degree, useful for testing
  34. * the overtemp code
  35. */
  36. #undef HACKED_OVERTEMP
  37. /* We currently only handle 2 chips */
  38. #define NR_CHIPS 2
  39. #define NR_CPU_FANS 3 * NR_CHIPS
  40. /* Controls and sensors */
  41. static struct wf_sensor *sens_cpu_temp[NR_CHIPS];
  42. static struct wf_sensor *sens_cpu_volts[NR_CHIPS];
  43. static struct wf_sensor *sens_cpu_amps[NR_CHIPS];
  44. static struct wf_sensor *backside_temp;
  45. static struct wf_sensor *drives_temp;
  46. static struct wf_control *cpu_front_fans[NR_CHIPS];
  47. static struct wf_control *cpu_rear_fans[NR_CHIPS];
  48. static struct wf_control *cpu_pumps[NR_CHIPS];
  49. static struct wf_control *backside_fan;
  50. static struct wf_control *drives_fan;
  51. static struct wf_control *slots_fan;
  52. static struct wf_control *cpufreq_clamp;
  53. /* We keep a temperature history for average calculation of 180s */
  54. #define CPU_TEMP_HIST_SIZE 180
  55. /* Fixed speed for slot fan */
  56. #define SLOTS_FAN_DEFAULT_PWM 40
  57. /* Scale value for CPU intake fans */
  58. #define CPU_INTAKE_SCALE 0x0000f852
  59. /* PID loop state */
  60. static const struct mpu_data *cpu_mpu_data[NR_CHIPS];
  61. static struct wf_cpu_pid_state cpu_pid[NR_CHIPS];
  62. static bool cpu_pid_combined;
  63. static u32 cpu_thist[CPU_TEMP_HIST_SIZE];
  64. static int cpu_thist_pt;
  65. static s64 cpu_thist_total;
  66. static s32 cpu_all_tmax = 100 << 16;
  67. static struct wf_pid_state backside_pid;
  68. static int backside_tick;
  69. static struct wf_pid_state drives_pid;
  70. static int drives_tick;
  71. static int nr_chips;
  72. static bool have_all_controls;
  73. static bool have_all_sensors;
  74. static bool started;
  75. static int failure_state;
  76. #define FAILURE_SENSOR 1
  77. #define FAILURE_FAN 2
  78. #define FAILURE_PERM 4
  79. #define FAILURE_LOW_OVERTEMP 8
  80. #define FAILURE_HIGH_OVERTEMP 16
  81. /* Overtemp values */
  82. #define LOW_OVER_AVERAGE 0
  83. #define LOW_OVER_IMMEDIATE (10 << 16)
  84. #define LOW_OVER_CLEAR ((-10) << 16)
  85. #define HIGH_OVER_IMMEDIATE (14 << 16)
  86. #define HIGH_OVER_AVERAGE (10 << 16)
  87. #define HIGH_OVER_IMMEDIATE (14 << 16)
  88. static void cpu_max_all_fans(void)
  89. {
  90. int i;
  91. /* We max all CPU fans in case of a sensor error. We also do the
  92. * cpufreq clamping now, even if it's supposedly done later by the
  93. * generic code anyway, we do it earlier here to react faster
  94. */
  95. if (cpufreq_clamp)
  96. wf_control_set_max(cpufreq_clamp);
  97. for (i = 0; i < nr_chips; i++) {
  98. if (cpu_front_fans[i])
  99. wf_control_set_max(cpu_front_fans[i]);
  100. if (cpu_rear_fans[i])
  101. wf_control_set_max(cpu_rear_fans[i]);
  102. if (cpu_pumps[i])
  103. wf_control_set_max(cpu_pumps[i]);
  104. }
  105. }
  106. static int cpu_check_overtemp(s32 temp)
  107. {
  108. int new_state = 0;
  109. s32 t_avg, t_old;
  110. static bool first = true;
  111. /* First check for immediate overtemps */
  112. if (temp >= (cpu_all_tmax + LOW_OVER_IMMEDIATE)) {
  113. new_state |= FAILURE_LOW_OVERTEMP;
  114. if ((failure_state & FAILURE_LOW_OVERTEMP) == 0)
  115. printk(KERN_ERR "windfarm: Overtemp due to immediate CPU"
  116. " temperature !\n");
  117. }
  118. if (temp >= (cpu_all_tmax + HIGH_OVER_IMMEDIATE)) {
  119. new_state |= FAILURE_HIGH_OVERTEMP;
  120. if ((failure_state & FAILURE_HIGH_OVERTEMP) == 0)
  121. printk(KERN_ERR "windfarm: Critical overtemp due to"
  122. " immediate CPU temperature !\n");
  123. }
  124. /*
  125. * The first time around, initialize the array with the first
  126. * temperature reading
  127. */
  128. if (first) {
  129. int i;
  130. cpu_thist_total = 0;
  131. for (i = 0; i < CPU_TEMP_HIST_SIZE; i++) {
  132. cpu_thist[i] = temp;
  133. cpu_thist_total += temp;
  134. }
  135. first = false;
  136. }
  137. /*
  138. * We calculate a history of max temperatures and use that for the
  139. * overtemp management
  140. */
  141. t_old = cpu_thist[cpu_thist_pt];
  142. cpu_thist[cpu_thist_pt] = temp;
  143. cpu_thist_pt = (cpu_thist_pt + 1) % CPU_TEMP_HIST_SIZE;
  144. cpu_thist_total -= t_old;
  145. cpu_thist_total += temp;
  146. t_avg = cpu_thist_total / CPU_TEMP_HIST_SIZE;
  147. DBG_LOTS(" t_avg = %d.%03d (out: %d.%03d, in: %d.%03d)\n",
  148. FIX32TOPRINT(t_avg), FIX32TOPRINT(t_old), FIX32TOPRINT(temp));
  149. /* Now check for average overtemps */
  150. if (t_avg >= (cpu_all_tmax + LOW_OVER_AVERAGE)) {
  151. new_state |= FAILURE_LOW_OVERTEMP;
  152. if ((failure_state & FAILURE_LOW_OVERTEMP) == 0)
  153. printk(KERN_ERR "windfarm: Overtemp due to average CPU"
  154. " temperature !\n");
  155. }
  156. if (t_avg >= (cpu_all_tmax + HIGH_OVER_AVERAGE)) {
  157. new_state |= FAILURE_HIGH_OVERTEMP;
  158. if ((failure_state & FAILURE_HIGH_OVERTEMP) == 0)
  159. printk(KERN_ERR "windfarm: Critical overtemp due to"
  160. " average CPU temperature !\n");
  161. }
  162. /* Now handle overtemp conditions. We don't currently use the windfarm
  163. * overtemp handling core as it's not fully suited to the needs of those
  164. * new machine. This will be fixed later.
  165. */
  166. if (new_state) {
  167. /* High overtemp -> immediate shutdown */
  168. if (new_state & FAILURE_HIGH_OVERTEMP)
  169. machine_power_off();
  170. if ((failure_state & new_state) != new_state)
  171. cpu_max_all_fans();
  172. failure_state |= new_state;
  173. } else if ((failure_state & FAILURE_LOW_OVERTEMP) &&
  174. (temp < (cpu_all_tmax + LOW_OVER_CLEAR))) {
  175. printk(KERN_ERR "windfarm: Overtemp condition cleared !\n");
  176. failure_state &= ~FAILURE_LOW_OVERTEMP;
  177. }
  178. return failure_state & (FAILURE_LOW_OVERTEMP | FAILURE_HIGH_OVERTEMP);
  179. }
  180. static int read_one_cpu_vals(int cpu, s32 *temp, s32 *power)
  181. {
  182. s32 dtemp, volts, amps;
  183. int rc;
  184. /* Get diode temperature */
  185. rc = wf_sensor_get(sens_cpu_temp[cpu], &dtemp);
  186. if (rc) {
  187. DBG(" CPU%d: temp reading error !\n", cpu);
  188. return -EIO;
  189. }
  190. DBG_LOTS(" CPU%d: temp = %d.%03d\n", cpu, FIX32TOPRINT((dtemp)));
  191. *temp = dtemp;
  192. /* Get voltage */
  193. rc = wf_sensor_get(sens_cpu_volts[cpu], &volts);
  194. if (rc) {
  195. DBG(" CPU%d, volts reading error !\n", cpu);
  196. return -EIO;
  197. }
  198. DBG_LOTS(" CPU%d: volts = %d.%03d\n", cpu, FIX32TOPRINT((volts)));
  199. /* Get current */
  200. rc = wf_sensor_get(sens_cpu_amps[cpu], &amps);
  201. if (rc) {
  202. DBG(" CPU%d, current reading error !\n", cpu);
  203. return -EIO;
  204. }
  205. DBG_LOTS(" CPU%d: amps = %d.%03d\n", cpu, FIX32TOPRINT((amps)));
  206. /* Calculate power */
  207. /* Scale voltage and current raw sensor values according to fixed scales
  208. * obtained in Darwin and calculate power from I and V
  209. */
  210. *power = (((u64)volts) * ((u64)amps)) >> 16;
  211. DBG_LOTS(" CPU%d: power = %d.%03d\n", cpu, FIX32TOPRINT((*power)));
  212. return 0;
  213. }
  214. static void cpu_fans_tick_split(void)
  215. {
  216. int err, cpu;
  217. s32 intake, temp, power, t_max = 0;
  218. DBG_LOTS("* cpu fans_tick_split()\n");
  219. for (cpu = 0; cpu < nr_chips; ++cpu) {
  220. struct wf_cpu_pid_state *sp = &cpu_pid[cpu];
  221. /* Read current speed */
  222. wf_control_get(cpu_rear_fans[cpu], &sp->target);
  223. DBG_LOTS(" CPU%d: cur_target = %d RPM\n", cpu, sp->target);
  224. err = read_one_cpu_vals(cpu, &temp, &power);
  225. if (err) {
  226. failure_state |= FAILURE_SENSOR;
  227. cpu_max_all_fans();
  228. return;
  229. }
  230. /* Keep track of highest temp */
  231. t_max = max(t_max, temp);
  232. /* Handle possible overtemps */
  233. if (cpu_check_overtemp(t_max))
  234. return;
  235. /* Run PID */
  236. wf_cpu_pid_run(sp, power, temp);
  237. DBG_LOTS(" CPU%d: target = %d RPM\n", cpu, sp->target);
  238. /* Apply result directly to exhaust fan */
  239. err = wf_control_set(cpu_rear_fans[cpu], sp->target);
  240. if (err) {
  241. pr_warning("wf_pm72: Fan %s reports error %d\n",
  242. cpu_rear_fans[cpu]->name, err);
  243. failure_state |= FAILURE_FAN;
  244. break;
  245. }
  246. /* Scale result for intake fan */
  247. intake = (sp->target * CPU_INTAKE_SCALE) >> 16;
  248. DBG_LOTS(" CPU%d: intake = %d RPM\n", cpu, intake);
  249. err = wf_control_set(cpu_front_fans[cpu], intake);
  250. if (err) {
  251. pr_warning("wf_pm72: Fan %s reports error %d\n",
  252. cpu_front_fans[cpu]->name, err);
  253. failure_state |= FAILURE_FAN;
  254. break;
  255. }
  256. }
  257. }
  258. static void cpu_fans_tick_combined(void)
  259. {
  260. s32 temp0, power0, temp1, power1, t_max = 0;
  261. s32 temp, power, intake, pump;
  262. struct wf_control *pump0, *pump1;
  263. struct wf_cpu_pid_state *sp = &cpu_pid[0];
  264. int err, cpu;
  265. DBG_LOTS("* cpu fans_tick_combined()\n");
  266. /* Read current speed from cpu 0 */
  267. wf_control_get(cpu_rear_fans[0], &sp->target);
  268. DBG_LOTS(" CPUs: cur_target = %d RPM\n", sp->target);
  269. /* Read values for both CPUs */
  270. err = read_one_cpu_vals(0, &temp0, &power0);
  271. if (err) {
  272. failure_state |= FAILURE_SENSOR;
  273. cpu_max_all_fans();
  274. return;
  275. }
  276. err = read_one_cpu_vals(1, &temp1, &power1);
  277. if (err) {
  278. failure_state |= FAILURE_SENSOR;
  279. cpu_max_all_fans();
  280. return;
  281. }
  282. /* Keep track of highest temp */
  283. t_max = max(t_max, max(temp0, temp1));
  284. /* Handle possible overtemps */
  285. if (cpu_check_overtemp(t_max))
  286. return;
  287. /* Use the max temp & power of both */
  288. temp = max(temp0, temp1);
  289. power = max(power0, power1);
  290. /* Run PID */
  291. wf_cpu_pid_run(sp, power, temp);
  292. /* Scale result for intake fan */
  293. intake = (sp->target * CPU_INTAKE_SCALE) >> 16;
  294. /* Same deal with pump speed */
  295. pump0 = cpu_pumps[0];
  296. pump1 = cpu_pumps[1];
  297. if (!pump0) {
  298. pump0 = pump1;
  299. pump1 = NULL;
  300. }
  301. pump = (sp->target * wf_control_get_max(pump0)) /
  302. cpu_mpu_data[0]->rmaxn_exhaust_fan;
  303. DBG_LOTS(" CPUs: target = %d RPM\n", sp->target);
  304. DBG_LOTS(" CPUs: intake = %d RPM\n", intake);
  305. DBG_LOTS(" CPUs: pump = %d RPM\n", pump);
  306. for (cpu = 0; cpu < nr_chips; cpu++) {
  307. err = wf_control_set(cpu_rear_fans[cpu], sp->target);
  308. if (err) {
  309. pr_warning("wf_pm72: Fan %s reports error %d\n",
  310. cpu_rear_fans[cpu]->name, err);
  311. failure_state |= FAILURE_FAN;
  312. }
  313. err = wf_control_set(cpu_front_fans[cpu], intake);
  314. if (err) {
  315. pr_warning("wf_pm72: Fan %s reports error %d\n",
  316. cpu_front_fans[cpu]->name, err);
  317. failure_state |= FAILURE_FAN;
  318. }
  319. err = 0;
  320. if (cpu_pumps[cpu])
  321. err = wf_control_set(cpu_pumps[cpu], pump);
  322. if (err) {
  323. pr_warning("wf_pm72: Pump %s reports error %d\n",
  324. cpu_pumps[cpu]->name, err);
  325. failure_state |= FAILURE_FAN;
  326. }
  327. }
  328. }
  329. /* Implementation... */
  330. static int cpu_setup_pid(int cpu)
  331. {
  332. struct wf_cpu_pid_param pid;
  333. const struct mpu_data *mpu = cpu_mpu_data[cpu];
  334. s32 tmax, ttarget, ptarget;
  335. int fmin, fmax, hsize;
  336. /* Get PID params from the appropriate MPU EEPROM */
  337. tmax = mpu->tmax << 16;
  338. ttarget = mpu->ttarget << 16;
  339. ptarget = ((s32)(mpu->pmaxh - mpu->padjmax)) << 16;
  340. DBG("wf_72: CPU%d ttarget = %d.%03d, tmax = %d.%03d\n",
  341. cpu, FIX32TOPRINT(ttarget), FIX32TOPRINT(tmax));
  342. /* We keep a global tmax for overtemp calculations */
  343. if (tmax < cpu_all_tmax)
  344. cpu_all_tmax = tmax;
  345. /* Set PID min/max by using the rear fan min/max */
  346. fmin = wf_control_get_min(cpu_rear_fans[cpu]);
  347. fmax = wf_control_get_max(cpu_rear_fans[cpu]);
  348. DBG("wf_72: CPU%d max RPM range = [%d..%d]\n", cpu, fmin, fmax);
  349. /* History size */
  350. hsize = min_t(int, mpu->tguardband, WF_PID_MAX_HISTORY);
  351. DBG("wf_72: CPU%d history size = %d\n", cpu, hsize);
  352. /* Initialize PID loop */
  353. pid.interval = 1; /* seconds */
  354. pid.history_len = hsize;
  355. pid.gd = mpu->pid_gd;
  356. pid.gp = mpu->pid_gp;
  357. pid.gr = mpu->pid_gr;
  358. pid.tmax = tmax;
  359. pid.ttarget = ttarget;
  360. pid.pmaxadj = ptarget;
  361. pid.min = fmin;
  362. pid.max = fmax;
  363. wf_cpu_pid_init(&cpu_pid[cpu], &pid);
  364. cpu_pid[cpu].target = 1000;
  365. return 0;
  366. }
  367. /* Backside/U3 fan */
  368. static struct wf_pid_param backside_u3_param = {
  369. .interval = 5,
  370. .history_len = 2,
  371. .gd = 40 << 20,
  372. .gp = 5 << 20,
  373. .gr = 0,
  374. .itarget = 65 << 16,
  375. .additive = 1,
  376. .min = 20,
  377. .max = 100,
  378. };
  379. static struct wf_pid_param backside_u3h_param = {
  380. .interval = 5,
  381. .history_len = 2,
  382. .gd = 20 << 20,
  383. .gp = 5 << 20,
  384. .gr = 0,
  385. .itarget = 75 << 16,
  386. .additive = 1,
  387. .min = 20,
  388. .max = 100,
  389. };
  390. static void backside_fan_tick(void)
  391. {
  392. s32 temp;
  393. int speed;
  394. int err;
  395. if (!backside_fan || !backside_temp || !backside_tick)
  396. return;
  397. if (--backside_tick > 0)
  398. return;
  399. backside_tick = backside_pid.param.interval;
  400. DBG_LOTS("* backside fans tick\n");
  401. /* Update fan speed from actual fans */
  402. err = wf_control_get(backside_fan, &speed);
  403. if (!err)
  404. backside_pid.target = speed;
  405. err = wf_sensor_get(backside_temp, &temp);
  406. if (err) {
  407. printk(KERN_WARNING "windfarm: U4 temp sensor error %d\n",
  408. err);
  409. failure_state |= FAILURE_SENSOR;
  410. wf_control_set_max(backside_fan);
  411. return;
  412. }
  413. speed = wf_pid_run(&backside_pid, temp);
  414. DBG_LOTS("backside PID temp=%d.%.3d speed=%d\n",
  415. FIX32TOPRINT(temp), speed);
  416. err = wf_control_set(backside_fan, speed);
  417. if (err) {
  418. printk(KERN_WARNING "windfarm: backside fan error %d\n", err);
  419. failure_state |= FAILURE_FAN;
  420. }
  421. }
  422. static void backside_setup_pid(void)
  423. {
  424. /* first time initialize things */
  425. s32 fmin = wf_control_get_min(backside_fan);
  426. s32 fmax = wf_control_get_max(backside_fan);
  427. struct wf_pid_param param;
  428. struct device_node *u3;
  429. int u3h = 1; /* conservative by default */
  430. u3 = of_find_node_by_path("/u3@0,f8000000");
  431. if (u3 != NULL) {
  432. const u32 *vers = of_get_property(u3, "device-rev", NULL);
  433. if (vers)
  434. if (((*vers) & 0x3f) < 0x34)
  435. u3h = 0;
  436. of_node_put(u3);
  437. }
  438. param = u3h ? backside_u3h_param : backside_u3_param;
  439. param.min = max(param.min, fmin);
  440. param.max = min(param.max, fmax);
  441. wf_pid_init(&backside_pid, &param);
  442. backside_tick = 1;
  443. pr_info("wf_pm72: Backside control loop started.\n");
  444. }
  445. /* Drive bay fan */
  446. static const struct wf_pid_param drives_param = {
  447. .interval = 5,
  448. .history_len = 2,
  449. .gd = 30 << 20,
  450. .gp = 5 << 20,
  451. .gr = 0,
  452. .itarget = 40 << 16,
  453. .additive = 1,
  454. .min = 300,
  455. .max = 4000,
  456. };
  457. static void drives_fan_tick(void)
  458. {
  459. s32 temp;
  460. int speed;
  461. int err;
  462. if (!drives_fan || !drives_temp || !drives_tick)
  463. return;
  464. if (--drives_tick > 0)
  465. return;
  466. drives_tick = drives_pid.param.interval;
  467. DBG_LOTS("* drives fans tick\n");
  468. /* Update fan speed from actual fans */
  469. err = wf_control_get(drives_fan, &speed);
  470. if (!err)
  471. drives_pid.target = speed;
  472. err = wf_sensor_get(drives_temp, &temp);
  473. if (err) {
  474. pr_warning("wf_pm72: drive bay temp sensor error %d\n", err);
  475. failure_state |= FAILURE_SENSOR;
  476. wf_control_set_max(drives_fan);
  477. return;
  478. }
  479. speed = wf_pid_run(&drives_pid, temp);
  480. DBG_LOTS("drives PID temp=%d.%.3d speed=%d\n",
  481. FIX32TOPRINT(temp), speed);
  482. err = wf_control_set(drives_fan, speed);
  483. if (err) {
  484. printk(KERN_WARNING "windfarm: drive bay fan error %d\n", err);
  485. failure_state |= FAILURE_FAN;
  486. }
  487. }
  488. static void drives_setup_pid(void)
  489. {
  490. /* first time initialize things */
  491. s32 fmin = wf_control_get_min(drives_fan);
  492. s32 fmax = wf_control_get_max(drives_fan);
  493. struct wf_pid_param param = drives_param;
  494. param.min = max(param.min, fmin);
  495. param.max = min(param.max, fmax);
  496. wf_pid_init(&drives_pid, &param);
  497. drives_tick = 1;
  498. pr_info("wf_pm72: Drive bay control loop started.\n");
  499. }
  500. static void set_fail_state(void)
  501. {
  502. cpu_max_all_fans();
  503. if (backside_fan)
  504. wf_control_set_max(backside_fan);
  505. if (slots_fan)
  506. wf_control_set_max(slots_fan);
  507. if (drives_fan)
  508. wf_control_set_max(drives_fan);
  509. }
  510. static void pm72_tick(void)
  511. {
  512. int i, last_failure;
  513. if (!started) {
  514. started = 1;
  515. printk(KERN_INFO "windfarm: CPUs control loops started.\n");
  516. for (i = 0; i < nr_chips; ++i) {
  517. if (cpu_setup_pid(i) < 0) {
  518. failure_state = FAILURE_PERM;
  519. set_fail_state();
  520. break;
  521. }
  522. }
  523. DBG_LOTS("cpu_all_tmax=%d.%03d\n", FIX32TOPRINT(cpu_all_tmax));
  524. backside_setup_pid();
  525. drives_setup_pid();
  526. /*
  527. * We don't have the right stuff to drive the PCI fan
  528. * so we fix it to a default value
  529. */
  530. wf_control_set(slots_fan, SLOTS_FAN_DEFAULT_PWM);
  531. #ifdef HACKED_OVERTEMP
  532. cpu_all_tmax = 60 << 16;
  533. #endif
  534. }
  535. /* Permanent failure, bail out */
  536. if (failure_state & FAILURE_PERM)
  537. return;
  538. /*
  539. * Clear all failure bits except low overtemp which will be eventually
  540. * cleared by the control loop itself
  541. */
  542. last_failure = failure_state;
  543. failure_state &= FAILURE_LOW_OVERTEMP;
  544. if (cpu_pid_combined)
  545. cpu_fans_tick_combined();
  546. else
  547. cpu_fans_tick_split();
  548. backside_fan_tick();
  549. drives_fan_tick();
  550. DBG_LOTS(" last_failure: 0x%x, failure_state: %x\n",
  551. last_failure, failure_state);
  552. /* Check for failures. Any failure causes cpufreq clamping */
  553. if (failure_state && last_failure == 0 && cpufreq_clamp)
  554. wf_control_set_max(cpufreq_clamp);
  555. if (failure_state == 0 && last_failure && cpufreq_clamp)
  556. wf_control_set_min(cpufreq_clamp);
  557. /* That's it for now, we might want to deal with other failures
  558. * differently in the future though
  559. */
  560. }
  561. static void pm72_new_control(struct wf_control *ct)
  562. {
  563. bool all_controls;
  564. bool had_pump = cpu_pumps[0] || cpu_pumps[1];
  565. if (!strcmp(ct->name, "cpu-front-fan-0"))
  566. cpu_front_fans[0] = ct;
  567. else if (!strcmp(ct->name, "cpu-front-fan-1"))
  568. cpu_front_fans[1] = ct;
  569. else if (!strcmp(ct->name, "cpu-rear-fan-0"))
  570. cpu_rear_fans[0] = ct;
  571. else if (!strcmp(ct->name, "cpu-rear-fan-1"))
  572. cpu_rear_fans[1] = ct;
  573. else if (!strcmp(ct->name, "cpu-pump-0"))
  574. cpu_pumps[0] = ct;
  575. else if (!strcmp(ct->name, "cpu-pump-1"))
  576. cpu_pumps[1] = ct;
  577. else if (!strcmp(ct->name, "backside-fan"))
  578. backside_fan = ct;
  579. else if (!strcmp(ct->name, "slots-fan"))
  580. slots_fan = ct;
  581. else if (!strcmp(ct->name, "drive-bay-fan"))
  582. drives_fan = ct;
  583. else if (!strcmp(ct->name, "cpufreq-clamp"))
  584. cpufreq_clamp = ct;
  585. all_controls =
  586. cpu_front_fans[0] &&
  587. cpu_rear_fans[0] &&
  588. backside_fan &&
  589. slots_fan &&
  590. drives_fan;
  591. if (nr_chips > 1)
  592. all_controls &=
  593. cpu_front_fans[1] &&
  594. cpu_rear_fans[1];
  595. have_all_controls = all_controls;
  596. if ((cpu_pumps[0] || cpu_pumps[1]) && !had_pump) {
  597. pr_info("wf_pm72: Liquid cooling pump(s) detected,"
  598. " using new algorithm !\n");
  599. cpu_pid_combined = true;
  600. }
  601. }
  602. static void pm72_new_sensor(struct wf_sensor *sr)
  603. {
  604. bool all_sensors;
  605. if (!strcmp(sr->name, "cpu-diode-temp-0"))
  606. sens_cpu_temp[0] = sr;
  607. else if (!strcmp(sr->name, "cpu-diode-temp-1"))
  608. sens_cpu_temp[1] = sr;
  609. else if (!strcmp(sr->name, "cpu-voltage-0"))
  610. sens_cpu_volts[0] = sr;
  611. else if (!strcmp(sr->name, "cpu-voltage-1"))
  612. sens_cpu_volts[1] = sr;
  613. else if (!strcmp(sr->name, "cpu-current-0"))
  614. sens_cpu_amps[0] = sr;
  615. else if (!strcmp(sr->name, "cpu-current-1"))
  616. sens_cpu_amps[1] = sr;
  617. else if (!strcmp(sr->name, "backside-temp"))
  618. backside_temp = sr;
  619. else if (!strcmp(sr->name, "hd-temp"))
  620. drives_temp = sr;
  621. all_sensors =
  622. sens_cpu_temp[0] &&
  623. sens_cpu_volts[0] &&
  624. sens_cpu_amps[0] &&
  625. backside_temp &&
  626. drives_temp;
  627. if (nr_chips > 1)
  628. all_sensors &=
  629. sens_cpu_temp[1] &&
  630. sens_cpu_volts[1] &&
  631. sens_cpu_amps[1];
  632. have_all_sensors = all_sensors;
  633. }
  634. static int pm72_wf_notify(struct notifier_block *self,
  635. unsigned long event, void *data)
  636. {
  637. switch (event) {
  638. case WF_EVENT_NEW_SENSOR:
  639. pm72_new_sensor(data);
  640. break;
  641. case WF_EVENT_NEW_CONTROL:
  642. pm72_new_control(data);
  643. break;
  644. case WF_EVENT_TICK:
  645. if (have_all_controls && have_all_sensors)
  646. pm72_tick();
  647. }
  648. return 0;
  649. }
  650. static struct notifier_block pm72_events = {
  651. .notifier_call = pm72_wf_notify,
  652. };
  653. static int wf_pm72_probe(struct platform_device *dev)
  654. {
  655. wf_register_client(&pm72_events);
  656. return 0;
  657. }
  658. static int wf_pm72_remove(struct platform_device *dev)
  659. {
  660. wf_unregister_client(&pm72_events);
  661. /* should release all sensors and controls */
  662. return 0;
  663. }
  664. static struct platform_driver wf_pm72_driver = {
  665. .probe = wf_pm72_probe,
  666. .remove = wf_pm72_remove,
  667. .driver = {
  668. .name = "windfarm",
  669. },
  670. };
  671. static int __init wf_pm72_init(void)
  672. {
  673. struct device_node *cpu;
  674. int i;
  675. if (!of_machine_is_compatible("PowerMac7,2") &&
  676. !of_machine_is_compatible("PowerMac7,3"))
  677. return -ENODEV;
  678. /* Count the number of CPU cores */
  679. nr_chips = 0;
  680. for_each_node_by_type(cpu, "cpu")
  681. ++nr_chips;
  682. if (nr_chips > NR_CHIPS)
  683. nr_chips = NR_CHIPS;
  684. pr_info("windfarm: Initializing for desktop G5 with %d chips\n",
  685. nr_chips);
  686. /* Get MPU data for each CPU */
  687. for (i = 0; i < nr_chips; i++) {
  688. cpu_mpu_data[i] = wf_get_mpu(i);
  689. if (!cpu_mpu_data[i]) {
  690. pr_err("wf_pm72: Failed to find MPU data for CPU %d\n", i);
  691. return -ENXIO;
  692. }
  693. }
  694. #ifdef MODULE
  695. request_module("windfarm_fcu_controls");
  696. request_module("windfarm_lm75_sensor");
  697. request_module("windfarm_ad7417_sensor");
  698. request_module("windfarm_max6690_sensor");
  699. request_module("windfarm_cpufreq_clamp");
  700. #endif /* MODULE */
  701. platform_driver_register(&wf_pm72_driver);
  702. return 0;
  703. }
  704. static void __exit wf_pm72_exit(void)
  705. {
  706. platform_driver_unregister(&wf_pm72_driver);
  707. }
  708. module_init(wf_pm72_init);
  709. module_exit(wf_pm72_exit);
  710. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  711. MODULE_DESCRIPTION("Thermal control for AGP PowerMac G5s");
  712. MODULE_LICENSE("GPL");
  713. MODULE_ALIAS("platform:windfarm");