windfarm_pm72.c 20 KB

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