windfarm_pm81.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Windfarm PowerMac thermal control. iMac G5
  4. *
  5. * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
  6. * <benh@kernel.crashing.org>
  7. *
  8. * The algorithm used is the PID control algorithm, used the same
  9. * way the published Darwin code does, using the same values that
  10. * are present in the Darwin 8.2 snapshot property lists (note however
  11. * that none of the code has been re-used, it's a complete re-implementation
  12. *
  13. * The various control loops found in Darwin config file are:
  14. *
  15. * PowerMac8,1 and PowerMac8,2
  16. * ===========================
  17. *
  18. * System Fans control loop. Different based on models. In addition to the
  19. * usual PID algorithm, the control loop gets 2 additional pairs of linear
  20. * scaling factors (scale/offsets) expressed as 4.12 fixed point values
  21. * signed offset, unsigned scale)
  22. *
  23. * The targets are modified such as:
  24. * - the linked control (second control) gets the target value as-is
  25. * (typically the drive fan)
  26. * - the main control (first control) gets the target value scaled with
  27. * the first pair of factors, and is then modified as below
  28. * - the value of the target of the CPU Fan control loop is retrieved,
  29. * scaled with the second pair of factors, and the max of that and
  30. * the scaled target is applied to the main control.
  31. *
  32. * # model_id: 2
  33. * controls : system-fan, drive-bay-fan
  34. * sensors : hd-temp
  35. * PID params : G_d = 0x15400000
  36. * G_p = 0x00200000
  37. * G_r = 0x000002fd
  38. * History = 2 entries
  39. * Input target = 0x3a0000
  40. * Interval = 5s
  41. * linear-factors : offset = 0xff38 scale = 0x0ccd
  42. * offset = 0x0208 scale = 0x07ae
  43. *
  44. * # model_id: 3
  45. * controls : system-fan, drive-bay-fan
  46. * sensors : hd-temp
  47. * PID params : G_d = 0x08e00000
  48. * G_p = 0x00566666
  49. * G_r = 0x0000072b
  50. * History = 2 entries
  51. * Input target = 0x350000
  52. * Interval = 5s
  53. * linear-factors : offset = 0xff38 scale = 0x0ccd
  54. * offset = 0x0000 scale = 0x0000
  55. *
  56. * # model_id: 5
  57. * controls : system-fan
  58. * sensors : hd-temp
  59. * PID params : G_d = 0x15400000
  60. * G_p = 0x00233333
  61. * G_r = 0x000002fd
  62. * History = 2 entries
  63. * Input target = 0x3a0000
  64. * Interval = 5s
  65. * linear-factors : offset = 0x0000 scale = 0x1000
  66. * offset = 0x0091 scale = 0x0bae
  67. *
  68. * CPU Fan control loop. The loop is identical for all models. it
  69. * has an additional pair of scaling factor. This is used to scale the
  70. * systems fan control loop target result (the one before it gets scaled
  71. * by the System Fans control loop itself). Then, the max value of the
  72. * calculated target value and system fan value is sent to the fans
  73. *
  74. * controls : cpu-fan
  75. * sensors : cpu-temp cpu-power
  76. * PID params : From SMU sdb partition
  77. * linear-factors : offset = 0xfb50 scale = 0x1000
  78. *
  79. * CPU Slew control loop. Not implemented. The cpufreq driver in linux is
  80. * completely separate for now, though we could find a way to link it, either
  81. * as a client reacting to overtemp notifications, or directling monitoring
  82. * the CPU temperature
  83. *
  84. * WARNING ! The CPU control loop requires the CPU tmax for the current
  85. * operating point. However, we currently are completely separated from
  86. * the cpufreq driver and thus do not know what the current operating
  87. * point is. Fortunately, we also do not have any hardware supporting anything
  88. * but operating point 0 at the moment, thus we just peek that value directly
  89. * from the SDB partition. If we ever end up with actually slewing the system
  90. * clock and thus changing operating points, we'll have to find a way to
  91. * communicate with the CPU freq driver;
  92. */
  93. #include <linux/types.h>
  94. #include <linux/errno.h>
  95. #include <linux/kernel.h>
  96. #include <linux/delay.h>
  97. #include <linux/slab.h>
  98. #include <linux/init.h>
  99. #include <linux/spinlock.h>
  100. #include <linux/wait.h>
  101. #include <linux/kmod.h>
  102. #include <linux/device.h>
  103. #include <linux/platform_device.h>
  104. #include <linux/of.h>
  105. #include <asm/machdep.h>
  106. #include <asm/io.h>
  107. #include <asm/sections.h>
  108. #include <asm/smu.h>
  109. #include "windfarm.h"
  110. #include "windfarm_pid.h"
  111. #define VERSION "0.4"
  112. #undef DEBUG
  113. #ifdef DEBUG
  114. #define DBG(args...) printk(args)
  115. #else
  116. #define DBG(args...) do { } while(0)
  117. #endif
  118. /* define this to force CPU overtemp to 74 degree, useful for testing
  119. * the overtemp code
  120. */
  121. #undef HACKED_OVERTEMP
  122. static int wf_smu_mach_model; /* machine model id */
  123. /* Controls & sensors */
  124. static struct wf_sensor *sensor_cpu_power;
  125. static struct wf_sensor *sensor_cpu_temp;
  126. static struct wf_sensor *sensor_hd_temp;
  127. static struct wf_control *fan_cpu_main;
  128. static struct wf_control *fan_hd;
  129. static struct wf_control *fan_system;
  130. static struct wf_control *cpufreq_clamp;
  131. /* Set to kick the control loop into life */
  132. static int wf_smu_all_controls_ok, wf_smu_all_sensors_ok;
  133. static bool wf_smu_started;
  134. /* Failure handling.. could be nicer */
  135. #define FAILURE_FAN 0x01
  136. #define FAILURE_SENSOR 0x02
  137. #define FAILURE_OVERTEMP 0x04
  138. static unsigned int wf_smu_failure_state;
  139. static int wf_smu_readjust, wf_smu_skipping;
  140. static bool wf_smu_overtemp;
  141. /*
  142. * ****** System Fans Control Loop ******
  143. *
  144. */
  145. /* Parameters for the System Fans control loop. Parameters
  146. * not in this table such as interval, history size, ...
  147. * are common to all versions and thus hard coded for now.
  148. */
  149. struct wf_smu_sys_fans_param {
  150. int model_id;
  151. s32 itarget;
  152. s32 gd, gp, gr;
  153. s16 offset0;
  154. u16 scale0;
  155. s16 offset1;
  156. u16 scale1;
  157. };
  158. #define WF_SMU_SYS_FANS_INTERVAL 5
  159. #define WF_SMU_SYS_FANS_HISTORY_SIZE 2
  160. /* State data used by the system fans control loop
  161. */
  162. struct wf_smu_sys_fans_state {
  163. int ticks;
  164. s32 sys_setpoint;
  165. s32 hd_setpoint;
  166. s16 offset0;
  167. u16 scale0;
  168. s16 offset1;
  169. u16 scale1;
  170. struct wf_pid_state pid;
  171. };
  172. /*
  173. * Configs for SMU System Fan control loop
  174. */
  175. static struct wf_smu_sys_fans_param wf_smu_sys_all_params[] = {
  176. /* Model ID 2 */
  177. {
  178. .model_id = 2,
  179. .itarget = 0x3a0000,
  180. .gd = 0x15400000,
  181. .gp = 0x00200000,
  182. .gr = 0x000002fd,
  183. .offset0 = 0xff38,
  184. .scale0 = 0x0ccd,
  185. .offset1 = 0x0208,
  186. .scale1 = 0x07ae,
  187. },
  188. /* Model ID 3 */
  189. {
  190. .model_id = 3,
  191. .itarget = 0x350000,
  192. .gd = 0x08e00000,
  193. .gp = 0x00566666,
  194. .gr = 0x0000072b,
  195. .offset0 = 0xff38,
  196. .scale0 = 0x0ccd,
  197. .offset1 = 0x0000,
  198. .scale1 = 0x0000,
  199. },
  200. /* Model ID 5 */
  201. {
  202. .model_id = 5,
  203. .itarget = 0x3a0000,
  204. .gd = 0x15400000,
  205. .gp = 0x00233333,
  206. .gr = 0x000002fd,
  207. .offset0 = 0x0000,
  208. .scale0 = 0x1000,
  209. .offset1 = 0x0091,
  210. .scale1 = 0x0bae,
  211. },
  212. };
  213. #define WF_SMU_SYS_FANS_NUM_CONFIGS ARRAY_SIZE(wf_smu_sys_all_params)
  214. static struct wf_smu_sys_fans_state *wf_smu_sys_fans;
  215. /*
  216. * ****** CPU Fans Control Loop ******
  217. *
  218. */
  219. #define WF_SMU_CPU_FANS_INTERVAL 1
  220. #define WF_SMU_CPU_FANS_MAX_HISTORY 16
  221. #define WF_SMU_CPU_FANS_SIBLING_SCALE 0x00001000
  222. #define WF_SMU_CPU_FANS_SIBLING_OFFSET 0xfffffb50
  223. /* State data used by the cpu fans control loop
  224. */
  225. struct wf_smu_cpu_fans_state {
  226. int ticks;
  227. s32 cpu_setpoint;
  228. s32 scale;
  229. s32 offset;
  230. struct wf_cpu_pid_state pid;
  231. };
  232. static struct wf_smu_cpu_fans_state *wf_smu_cpu_fans;
  233. /*
  234. * ***** Implementation *****
  235. *
  236. */
  237. static void wf_smu_create_sys_fans(void)
  238. {
  239. struct wf_smu_sys_fans_param *param = NULL;
  240. struct wf_pid_param pid_param;
  241. int i;
  242. /* First, locate the params for this model */
  243. for (i = 0; i < WF_SMU_SYS_FANS_NUM_CONFIGS; i++)
  244. if (wf_smu_sys_all_params[i].model_id == wf_smu_mach_model) {
  245. param = &wf_smu_sys_all_params[i];
  246. break;
  247. }
  248. /* No params found, put fans to max */
  249. if (param == NULL) {
  250. printk(KERN_WARNING "windfarm: System fan config not found "
  251. "for this machine model, max fan speed\n");
  252. goto fail;
  253. }
  254. /* Alloc & initialize state */
  255. wf_smu_sys_fans = kmalloc_obj(struct wf_smu_sys_fans_state);
  256. if (wf_smu_sys_fans == NULL) {
  257. printk(KERN_WARNING "windfarm: Memory allocation error"
  258. " max fan speed\n");
  259. goto fail;
  260. }
  261. wf_smu_sys_fans->ticks = 1;
  262. wf_smu_sys_fans->scale0 = param->scale0;
  263. wf_smu_sys_fans->offset0 = param->offset0;
  264. wf_smu_sys_fans->scale1 = param->scale1;
  265. wf_smu_sys_fans->offset1 = param->offset1;
  266. /* Fill PID params */
  267. pid_param.gd = param->gd;
  268. pid_param.gp = param->gp;
  269. pid_param.gr = param->gr;
  270. pid_param.interval = WF_SMU_SYS_FANS_INTERVAL;
  271. pid_param.history_len = WF_SMU_SYS_FANS_HISTORY_SIZE;
  272. pid_param.itarget = param->itarget;
  273. pid_param.min = wf_control_get_min(fan_system);
  274. pid_param.max = wf_control_get_max(fan_system);
  275. if (fan_hd) {
  276. pid_param.min =
  277. max(pid_param.min, wf_control_get_min(fan_hd));
  278. pid_param.max =
  279. min(pid_param.max, wf_control_get_max(fan_hd));
  280. }
  281. wf_pid_init(&wf_smu_sys_fans->pid, &pid_param);
  282. DBG("wf: System Fan control initialized.\n");
  283. DBG(" itarged=%d.%03d, min=%d RPM, max=%d RPM\n",
  284. FIX32TOPRINT(pid_param.itarget), pid_param.min, pid_param.max);
  285. return;
  286. fail:
  287. if (fan_system)
  288. wf_control_set_max(fan_system);
  289. if (fan_hd)
  290. wf_control_set_max(fan_hd);
  291. }
  292. static void wf_smu_sys_fans_tick(struct wf_smu_sys_fans_state *st)
  293. {
  294. s32 new_setpoint, temp, scaled, cputarget;
  295. int rc;
  296. if (--st->ticks != 0) {
  297. if (wf_smu_readjust)
  298. goto readjust;
  299. return;
  300. }
  301. st->ticks = WF_SMU_SYS_FANS_INTERVAL;
  302. rc = wf_sensor_get(sensor_hd_temp, &temp);
  303. if (rc) {
  304. printk(KERN_WARNING "windfarm: HD temp sensor error %d\n",
  305. rc);
  306. wf_smu_failure_state |= FAILURE_SENSOR;
  307. return;
  308. }
  309. DBG("wf_smu: System Fans tick ! HD temp: %d.%03d\n",
  310. FIX32TOPRINT(temp));
  311. if (temp > (st->pid.param.itarget + 0x50000))
  312. wf_smu_failure_state |= FAILURE_OVERTEMP;
  313. new_setpoint = wf_pid_run(&st->pid, temp);
  314. DBG("wf_smu: new_setpoint: %d RPM\n", (int)new_setpoint);
  315. scaled = ((((s64)new_setpoint) * (s64)st->scale0) >> 12) + st->offset0;
  316. DBG("wf_smu: scaled setpoint: %d RPM\n", (int)scaled);
  317. cputarget = wf_smu_cpu_fans ? wf_smu_cpu_fans->pid.target : 0;
  318. cputarget = ((((s64)cputarget) * (s64)st->scale1) >> 12) + st->offset1;
  319. scaled = max(scaled, cputarget);
  320. scaled = max(scaled, st->pid.param.min);
  321. scaled = min(scaled, st->pid.param.max);
  322. DBG("wf_smu: adjusted setpoint: %d RPM\n", (int)scaled);
  323. if (st->sys_setpoint == scaled && new_setpoint == st->hd_setpoint)
  324. return;
  325. st->sys_setpoint = scaled;
  326. st->hd_setpoint = new_setpoint;
  327. readjust:
  328. if (fan_system && wf_smu_failure_state == 0) {
  329. rc = wf_control_set(fan_system, st->sys_setpoint);
  330. if (rc) {
  331. printk(KERN_WARNING "windfarm: Sys fan error %d\n",
  332. rc);
  333. wf_smu_failure_state |= FAILURE_FAN;
  334. }
  335. }
  336. if (fan_hd && wf_smu_failure_state == 0) {
  337. rc = wf_control_set(fan_hd, st->hd_setpoint);
  338. if (rc) {
  339. printk(KERN_WARNING "windfarm: HD fan error %d\n",
  340. rc);
  341. wf_smu_failure_state |= FAILURE_FAN;
  342. }
  343. }
  344. }
  345. static void wf_smu_create_cpu_fans(void)
  346. {
  347. struct wf_cpu_pid_param pid_param;
  348. const struct smu_sdbp_header *hdr;
  349. struct smu_sdbp_cpupiddata *piddata;
  350. struct smu_sdbp_fvt *fvt;
  351. s32 tmax, tdelta, maxpow, powadj;
  352. /* First, locate the PID params in SMU SBD */
  353. hdr = smu_get_sdb_partition(SMU_SDB_CPUPIDDATA_ID, NULL);
  354. if (!hdr) {
  355. printk(KERN_WARNING "windfarm: CPU PID fan config not found "
  356. "max fan speed\n");
  357. goto fail;
  358. }
  359. piddata = (struct smu_sdbp_cpupiddata *)&hdr[1];
  360. /* Get the FVT params for operating point 0 (the only supported one
  361. * for now) in order to get tmax
  362. */
  363. hdr = smu_get_sdb_partition(SMU_SDB_FVT_ID, NULL);
  364. if (hdr) {
  365. fvt = (struct smu_sdbp_fvt *)&hdr[1];
  366. tmax = ((s32)fvt->maxtemp) << 16;
  367. } else
  368. tmax = 0x5e0000; /* 94 degree default */
  369. /* Alloc & initialize state */
  370. wf_smu_cpu_fans = kmalloc_obj(struct wf_smu_cpu_fans_state);
  371. if (wf_smu_cpu_fans == NULL)
  372. goto fail;
  373. wf_smu_cpu_fans->ticks = 1;
  374. wf_smu_cpu_fans->scale = WF_SMU_CPU_FANS_SIBLING_SCALE;
  375. wf_smu_cpu_fans->offset = WF_SMU_CPU_FANS_SIBLING_OFFSET;
  376. /* Fill PID params */
  377. pid_param.interval = WF_SMU_CPU_FANS_INTERVAL;
  378. pid_param.history_len = piddata->history_len;
  379. if (pid_param.history_len > WF_CPU_PID_MAX_HISTORY) {
  380. printk(KERN_WARNING "windfarm: History size overflow on "
  381. "CPU control loop (%d)\n", piddata->history_len);
  382. pid_param.history_len = WF_CPU_PID_MAX_HISTORY;
  383. }
  384. pid_param.gd = piddata->gd;
  385. pid_param.gp = piddata->gp;
  386. pid_param.gr = piddata->gr / pid_param.history_len;
  387. tdelta = ((s32)piddata->target_temp_delta) << 16;
  388. maxpow = ((s32)piddata->max_power) << 16;
  389. powadj = ((s32)piddata->power_adj) << 16;
  390. pid_param.tmax = tmax;
  391. pid_param.ttarget = tmax - tdelta;
  392. pid_param.pmaxadj = maxpow - powadj;
  393. pid_param.min = wf_control_get_min(fan_cpu_main);
  394. pid_param.max = wf_control_get_max(fan_cpu_main);
  395. wf_cpu_pid_init(&wf_smu_cpu_fans->pid, &pid_param);
  396. DBG("wf: CPU Fan control initialized.\n");
  397. DBG(" ttarget=%d.%03d, tmax=%d.%03d, min=%d RPM, max=%d RPM\n",
  398. FIX32TOPRINT(pid_param.ttarget), FIX32TOPRINT(pid_param.tmax),
  399. pid_param.min, pid_param.max);
  400. return;
  401. fail:
  402. printk(KERN_WARNING "windfarm: CPU fan config not found\n"
  403. "for this machine model, max fan speed\n");
  404. if (cpufreq_clamp)
  405. wf_control_set_max(cpufreq_clamp);
  406. if (fan_cpu_main)
  407. wf_control_set_max(fan_cpu_main);
  408. }
  409. static void wf_smu_cpu_fans_tick(struct wf_smu_cpu_fans_state *st)
  410. {
  411. s32 new_setpoint, temp, power, systarget;
  412. int rc;
  413. if (--st->ticks != 0) {
  414. if (wf_smu_readjust)
  415. goto readjust;
  416. return;
  417. }
  418. st->ticks = WF_SMU_CPU_FANS_INTERVAL;
  419. rc = wf_sensor_get(sensor_cpu_temp, &temp);
  420. if (rc) {
  421. printk(KERN_WARNING "windfarm: CPU temp sensor error %d\n",
  422. rc);
  423. wf_smu_failure_state |= FAILURE_SENSOR;
  424. return;
  425. }
  426. rc = wf_sensor_get(sensor_cpu_power, &power);
  427. if (rc) {
  428. printk(KERN_WARNING "windfarm: CPU power sensor error %d\n",
  429. rc);
  430. wf_smu_failure_state |= FAILURE_SENSOR;
  431. return;
  432. }
  433. DBG("wf_smu: CPU Fans tick ! CPU temp: %d.%03d, power: %d.%03d\n",
  434. FIX32TOPRINT(temp), FIX32TOPRINT(power));
  435. #ifdef HACKED_OVERTEMP
  436. if (temp > 0x4a0000)
  437. wf_smu_failure_state |= FAILURE_OVERTEMP;
  438. #else
  439. if (temp > st->pid.param.tmax)
  440. wf_smu_failure_state |= FAILURE_OVERTEMP;
  441. #endif
  442. new_setpoint = wf_cpu_pid_run(&st->pid, power, temp);
  443. DBG("wf_smu: new_setpoint: %d RPM\n", (int)new_setpoint);
  444. systarget = wf_smu_sys_fans ? wf_smu_sys_fans->pid.target : 0;
  445. systarget = ((((s64)systarget) * (s64)st->scale) >> 12)
  446. + st->offset;
  447. new_setpoint = max(new_setpoint, systarget);
  448. new_setpoint = max(new_setpoint, st->pid.param.min);
  449. new_setpoint = min(new_setpoint, st->pid.param.max);
  450. DBG("wf_smu: adjusted setpoint: %d RPM\n", (int)new_setpoint);
  451. if (st->cpu_setpoint == new_setpoint)
  452. return;
  453. st->cpu_setpoint = new_setpoint;
  454. readjust:
  455. if (fan_cpu_main && wf_smu_failure_state == 0) {
  456. rc = wf_control_set(fan_cpu_main, st->cpu_setpoint);
  457. if (rc) {
  458. printk(KERN_WARNING "windfarm: CPU main fan"
  459. " error %d\n", rc);
  460. wf_smu_failure_state |= FAILURE_FAN;
  461. }
  462. }
  463. }
  464. /*
  465. * ****** Setup / Init / Misc ... ******
  466. *
  467. */
  468. static void wf_smu_tick(void)
  469. {
  470. unsigned int last_failure = wf_smu_failure_state;
  471. unsigned int new_failure;
  472. if (!wf_smu_started) {
  473. DBG("wf: creating control loops !\n");
  474. wf_smu_create_sys_fans();
  475. wf_smu_create_cpu_fans();
  476. wf_smu_started = true;
  477. }
  478. /* Skipping ticks */
  479. if (wf_smu_skipping && --wf_smu_skipping)
  480. return;
  481. wf_smu_failure_state = 0;
  482. if (wf_smu_sys_fans)
  483. wf_smu_sys_fans_tick(wf_smu_sys_fans);
  484. if (wf_smu_cpu_fans)
  485. wf_smu_cpu_fans_tick(wf_smu_cpu_fans);
  486. wf_smu_readjust = 0;
  487. new_failure = wf_smu_failure_state & ~last_failure;
  488. /* If entering failure mode, clamp cpufreq and ramp all
  489. * fans to full speed.
  490. */
  491. if (wf_smu_failure_state && !last_failure) {
  492. if (cpufreq_clamp)
  493. wf_control_set_max(cpufreq_clamp);
  494. if (fan_system)
  495. wf_control_set_max(fan_system);
  496. if (fan_cpu_main)
  497. wf_control_set_max(fan_cpu_main);
  498. if (fan_hd)
  499. wf_control_set_max(fan_hd);
  500. }
  501. /* If leaving failure mode, unclamp cpufreq and readjust
  502. * all fans on next iteration
  503. */
  504. if (!wf_smu_failure_state && last_failure) {
  505. if (cpufreq_clamp)
  506. wf_control_set_min(cpufreq_clamp);
  507. wf_smu_readjust = 1;
  508. }
  509. /* Overtemp condition detected, notify and start skipping a couple
  510. * ticks to let the temperature go down
  511. */
  512. if (new_failure & FAILURE_OVERTEMP) {
  513. wf_set_overtemp();
  514. wf_smu_skipping = 2;
  515. wf_smu_overtemp = true;
  516. }
  517. /* We only clear the overtemp condition if overtemp is cleared
  518. * _and_ no other failure is present. Since a sensor error will
  519. * clear the overtemp condition (can't measure temperature) at
  520. * the control loop levels, but we don't want to keep it clear
  521. * here in this case
  522. */
  523. if (!wf_smu_failure_state && wf_smu_overtemp) {
  524. wf_clear_overtemp();
  525. wf_smu_overtemp = false;
  526. }
  527. }
  528. static void wf_smu_new_control(struct wf_control *ct)
  529. {
  530. if (wf_smu_all_controls_ok)
  531. return;
  532. if (fan_cpu_main == NULL && !strcmp(ct->name, "cpu-fan")) {
  533. if (wf_get_control(ct) == 0)
  534. fan_cpu_main = ct;
  535. }
  536. if (fan_system == NULL && !strcmp(ct->name, "system-fan")) {
  537. if (wf_get_control(ct) == 0)
  538. fan_system = ct;
  539. }
  540. if (cpufreq_clamp == NULL && !strcmp(ct->name, "cpufreq-clamp")) {
  541. if (wf_get_control(ct) == 0)
  542. cpufreq_clamp = ct;
  543. }
  544. /* Darwin property list says the HD fan is only for model ID
  545. * 0, 1, 2 and 3
  546. */
  547. if (wf_smu_mach_model > 3) {
  548. if (fan_system && fan_cpu_main && cpufreq_clamp)
  549. wf_smu_all_controls_ok = 1;
  550. return;
  551. }
  552. if (fan_hd == NULL && !strcmp(ct->name, "drive-bay-fan")) {
  553. if (wf_get_control(ct) == 0)
  554. fan_hd = ct;
  555. }
  556. if (fan_system && fan_hd && fan_cpu_main && cpufreq_clamp)
  557. wf_smu_all_controls_ok = 1;
  558. }
  559. static void wf_smu_new_sensor(struct wf_sensor *sr)
  560. {
  561. if (wf_smu_all_sensors_ok)
  562. return;
  563. if (sensor_cpu_power == NULL && !strcmp(sr->name, "cpu-power")) {
  564. if (wf_get_sensor(sr) == 0)
  565. sensor_cpu_power = sr;
  566. }
  567. if (sensor_cpu_temp == NULL && !strcmp(sr->name, "cpu-temp")) {
  568. if (wf_get_sensor(sr) == 0)
  569. sensor_cpu_temp = sr;
  570. }
  571. if (sensor_hd_temp == NULL && !strcmp(sr->name, "hd-temp")) {
  572. if (wf_get_sensor(sr) == 0)
  573. sensor_hd_temp = sr;
  574. }
  575. if (sensor_cpu_power && sensor_cpu_temp && sensor_hd_temp)
  576. wf_smu_all_sensors_ok = 1;
  577. }
  578. static int wf_smu_notify(struct notifier_block *self,
  579. unsigned long event, void *data)
  580. {
  581. switch(event) {
  582. case WF_EVENT_NEW_CONTROL:
  583. DBG("wf: new control %s detected\n",
  584. ((struct wf_control *)data)->name);
  585. wf_smu_new_control(data);
  586. wf_smu_readjust = 1;
  587. break;
  588. case WF_EVENT_NEW_SENSOR:
  589. DBG("wf: new sensor %s detected\n",
  590. ((struct wf_sensor *)data)->name);
  591. wf_smu_new_sensor(data);
  592. break;
  593. case WF_EVENT_TICK:
  594. if (wf_smu_all_controls_ok && wf_smu_all_sensors_ok)
  595. wf_smu_tick();
  596. }
  597. return 0;
  598. }
  599. static struct notifier_block wf_smu_events = {
  600. .notifier_call = wf_smu_notify,
  601. };
  602. static int wf_init_pm(void)
  603. {
  604. const struct smu_sdbp_header *hdr;
  605. hdr = smu_get_sdb_partition(SMU_SDB_SENSORTREE_ID, NULL);
  606. if (hdr) {
  607. struct smu_sdbp_sensortree *st =
  608. (struct smu_sdbp_sensortree *)&hdr[1];
  609. wf_smu_mach_model = st->model_id;
  610. }
  611. printk(KERN_INFO "windfarm: Initializing for iMacG5 model ID %d\n",
  612. wf_smu_mach_model);
  613. return 0;
  614. }
  615. static int wf_smu_probe(struct platform_device *ddev)
  616. {
  617. wf_register_client(&wf_smu_events);
  618. return 0;
  619. }
  620. static void wf_smu_remove(struct platform_device *ddev)
  621. {
  622. wf_unregister_client(&wf_smu_events);
  623. /* XXX We don't have yet a guarantee that our callback isn't
  624. * in progress when returning from wf_unregister_client, so
  625. * we add an arbitrary delay. I'll have to fix that in the core
  626. */
  627. msleep(1000);
  628. /* Release all sensors */
  629. /* One more crappy race: I don't think we have any guarantee here
  630. * that the attribute callback won't race with the sensor beeing
  631. * disposed of, and I'm not 100% certain what best way to deal
  632. * with that except by adding locks all over... I'll do that
  633. * eventually but heh, who ever rmmod this module anyway ?
  634. */
  635. if (sensor_cpu_power)
  636. wf_put_sensor(sensor_cpu_power);
  637. if (sensor_cpu_temp)
  638. wf_put_sensor(sensor_cpu_temp);
  639. if (sensor_hd_temp)
  640. wf_put_sensor(sensor_hd_temp);
  641. /* Release all controls */
  642. if (fan_cpu_main)
  643. wf_put_control(fan_cpu_main);
  644. if (fan_hd)
  645. wf_put_control(fan_hd);
  646. if (fan_system)
  647. wf_put_control(fan_system);
  648. if (cpufreq_clamp)
  649. wf_put_control(cpufreq_clamp);
  650. /* Destroy control loops state structures */
  651. kfree(wf_smu_sys_fans);
  652. kfree(wf_smu_cpu_fans);
  653. }
  654. static struct platform_driver wf_smu_driver = {
  655. .probe = wf_smu_probe,
  656. .remove = wf_smu_remove,
  657. .driver = {
  658. .name = "windfarm",
  659. },
  660. };
  661. static int __init wf_smu_init(void)
  662. {
  663. int rc = -ENODEV;
  664. if (of_machine_is_compatible("PowerMac8,1") ||
  665. of_machine_is_compatible("PowerMac8,2"))
  666. rc = wf_init_pm();
  667. if (rc == 0) {
  668. #ifdef MODULE
  669. request_module("windfarm_smu_controls");
  670. request_module("windfarm_smu_sensors");
  671. request_module("windfarm_lm75_sensor");
  672. request_module("windfarm_cpufreq_clamp");
  673. #endif /* MODULE */
  674. platform_driver_register(&wf_smu_driver);
  675. }
  676. return rc;
  677. }
  678. static void __exit wf_smu_exit(void)
  679. {
  680. platform_driver_unregister(&wf_smu_driver);
  681. }
  682. module_init(wf_smu_init);
  683. module_exit(wf_smu_exit);
  684. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  685. MODULE_DESCRIPTION("Thermal control logic for iMac G5");
  686. MODULE_LICENSE("GPL");
  687. MODULE_ALIAS("platform:windfarm");