windfarm_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Windfarm PowerMac thermal control. Core
  3. *
  4. * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
  5. * <benh@kernel.crashing.org>
  6. *
  7. * Released under the term of the GNU GPL v2.
  8. *
  9. * This core code tracks the list of sensors & controls, register
  10. * clients, and holds the kernel thread used for control.
  11. *
  12. * TODO:
  13. *
  14. * Add some information about sensor/control type and data format to
  15. * sensors/controls, and have the sysfs attribute stuff be moved
  16. * generically here instead of hard coded in the platform specific
  17. * driver as it us currently
  18. *
  19. * This however requires solving some annoying lifetime issues with
  20. * sysfs which doesn't seem to have lifetime rules for struct attribute,
  21. * I may have to create full features kobjects for every sensor/control
  22. * instead which is a bit of an overkill imho
  23. */
  24. #include <linux/types.h>
  25. #include <linux/errno.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/init.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/kthread.h>
  31. #include <linux/jiffies.h>
  32. #include <linux/reboot.h>
  33. #include <linux/device.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/mutex.h>
  36. #include <linux/freezer.h>
  37. #include <asm/prom.h>
  38. #include "windfarm.h"
  39. #define VERSION "0.2"
  40. #undef DEBUG
  41. #ifdef DEBUG
  42. #define DBG(args...) printk(args)
  43. #else
  44. #define DBG(args...) do { } while(0)
  45. #endif
  46. static LIST_HEAD(wf_controls);
  47. static LIST_HEAD(wf_sensors);
  48. static DEFINE_MUTEX(wf_lock);
  49. static BLOCKING_NOTIFIER_HEAD(wf_client_list);
  50. static int wf_client_count;
  51. static unsigned int wf_overtemp;
  52. static unsigned int wf_overtemp_counter;
  53. struct task_struct *wf_thread;
  54. static struct platform_device wf_platform_device = {
  55. .name = "windfarm",
  56. };
  57. /*
  58. * Utilities & tick thread
  59. */
  60. static inline void wf_notify(int event, void *param)
  61. {
  62. blocking_notifier_call_chain(&wf_client_list, event, param);
  63. }
  64. int wf_critical_overtemp(void)
  65. {
  66. static char * critical_overtemp_path = "/sbin/critical_overtemp";
  67. char *argv[] = { critical_overtemp_path, NULL };
  68. static char *envp[] = { "HOME=/",
  69. "TERM=linux",
  70. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  71. NULL };
  72. return call_usermodehelper(critical_overtemp_path,
  73. argv, envp, UMH_WAIT_EXEC);
  74. }
  75. EXPORT_SYMBOL_GPL(wf_critical_overtemp);
  76. static int wf_thread_func(void *data)
  77. {
  78. unsigned long next, delay;
  79. next = jiffies;
  80. DBG("wf: thread started\n");
  81. set_freezable();
  82. while (!kthread_should_stop()) {
  83. try_to_freeze();
  84. if (time_after_eq(jiffies, next)) {
  85. wf_notify(WF_EVENT_TICK, NULL);
  86. if (wf_overtemp) {
  87. wf_overtemp_counter++;
  88. /* 10 seconds overtemp, notify userland */
  89. if (wf_overtemp_counter > 10)
  90. wf_critical_overtemp();
  91. /* 30 seconds, shutdown */
  92. if (wf_overtemp_counter > 30) {
  93. printk(KERN_ERR "windfarm: Overtemp "
  94. "for more than 30"
  95. " seconds, shutting down\n");
  96. machine_power_off();
  97. }
  98. }
  99. next += HZ;
  100. }
  101. delay = next - jiffies;
  102. if (delay <= HZ)
  103. schedule_timeout_interruptible(delay);
  104. }
  105. DBG("wf: thread stopped\n");
  106. return 0;
  107. }
  108. static void wf_start_thread(void)
  109. {
  110. wf_thread = kthread_run(wf_thread_func, NULL, "kwindfarm");
  111. if (IS_ERR(wf_thread)) {
  112. printk(KERN_ERR "windfarm: failed to create thread,err %ld\n",
  113. PTR_ERR(wf_thread));
  114. wf_thread = NULL;
  115. }
  116. }
  117. static void wf_stop_thread(void)
  118. {
  119. if (wf_thread)
  120. kthread_stop(wf_thread);
  121. wf_thread = NULL;
  122. }
  123. /*
  124. * Controls
  125. */
  126. static void wf_control_release(struct kref *kref)
  127. {
  128. struct wf_control *ct = container_of(kref, struct wf_control, ref);
  129. DBG("wf: Deleting control %s\n", ct->name);
  130. if (ct->ops && ct->ops->release)
  131. ct->ops->release(ct);
  132. else
  133. kfree(ct);
  134. }
  135. static ssize_t wf_show_control(struct device *dev,
  136. struct device_attribute *attr, char *buf)
  137. {
  138. struct wf_control *ctrl = container_of(attr, struct wf_control, attr);
  139. const char *typestr;
  140. s32 val = 0;
  141. int err;
  142. err = ctrl->ops->get_value(ctrl, &val);
  143. if (err < 0)
  144. return err;
  145. switch(ctrl->type) {
  146. case WF_CONTROL_RPM_FAN:
  147. typestr = " RPM";
  148. break;
  149. case WF_CONTROL_PWM_FAN:
  150. typestr = " %";
  151. break;
  152. default:
  153. typestr = "";
  154. }
  155. return sprintf(buf, "%d%s\n", val, typestr);
  156. }
  157. /* This is really only for debugging... */
  158. static ssize_t wf_store_control(struct device *dev,
  159. struct device_attribute *attr,
  160. const char *buf, size_t count)
  161. {
  162. struct wf_control *ctrl = container_of(attr, struct wf_control, attr);
  163. int val;
  164. int err;
  165. char *endp;
  166. val = simple_strtoul(buf, &endp, 0);
  167. while (endp < buf + count && (*endp == ' ' || *endp == '\n'))
  168. ++endp;
  169. if (endp - buf < count)
  170. return -EINVAL;
  171. err = ctrl->ops->set_value(ctrl, val);
  172. if (err < 0)
  173. return err;
  174. return count;
  175. }
  176. int wf_register_control(struct wf_control *new_ct)
  177. {
  178. struct wf_control *ct;
  179. mutex_lock(&wf_lock);
  180. list_for_each_entry(ct, &wf_controls, link) {
  181. if (!strcmp(ct->name, new_ct->name)) {
  182. printk(KERN_WARNING "windfarm: trying to register"
  183. " duplicate control %s\n", ct->name);
  184. mutex_unlock(&wf_lock);
  185. return -EEXIST;
  186. }
  187. }
  188. kref_init(&new_ct->ref);
  189. list_add(&new_ct->link, &wf_controls);
  190. sysfs_attr_init(&new_ct->attr.attr);
  191. new_ct->attr.attr.name = new_ct->name;
  192. new_ct->attr.attr.mode = 0644;
  193. new_ct->attr.show = wf_show_control;
  194. new_ct->attr.store = wf_store_control;
  195. if (device_create_file(&wf_platform_device.dev, &new_ct->attr))
  196. printk(KERN_WARNING "windfarm: device_create_file failed"
  197. " for %s\n", new_ct->name);
  198. /* the subsystem still does useful work without the file */
  199. DBG("wf: Registered control %s\n", new_ct->name);
  200. wf_notify(WF_EVENT_NEW_CONTROL, new_ct);
  201. mutex_unlock(&wf_lock);
  202. return 0;
  203. }
  204. EXPORT_SYMBOL_GPL(wf_register_control);
  205. void wf_unregister_control(struct wf_control *ct)
  206. {
  207. mutex_lock(&wf_lock);
  208. list_del(&ct->link);
  209. mutex_unlock(&wf_lock);
  210. DBG("wf: Unregistered control %s\n", ct->name);
  211. kref_put(&ct->ref, wf_control_release);
  212. }
  213. EXPORT_SYMBOL_GPL(wf_unregister_control);
  214. struct wf_control * wf_find_control(const char *name)
  215. {
  216. struct wf_control *ct;
  217. mutex_lock(&wf_lock);
  218. list_for_each_entry(ct, &wf_controls, link) {
  219. if (!strcmp(ct->name, name)) {
  220. if (wf_get_control(ct))
  221. ct = NULL;
  222. mutex_unlock(&wf_lock);
  223. return ct;
  224. }
  225. }
  226. mutex_unlock(&wf_lock);
  227. return NULL;
  228. }
  229. EXPORT_SYMBOL_GPL(wf_find_control);
  230. int wf_get_control(struct wf_control *ct)
  231. {
  232. if (!try_module_get(ct->ops->owner))
  233. return -ENODEV;
  234. kref_get(&ct->ref);
  235. return 0;
  236. }
  237. EXPORT_SYMBOL_GPL(wf_get_control);
  238. void wf_put_control(struct wf_control *ct)
  239. {
  240. struct module *mod = ct->ops->owner;
  241. kref_put(&ct->ref, wf_control_release);
  242. module_put(mod);
  243. }
  244. EXPORT_SYMBOL_GPL(wf_put_control);
  245. /*
  246. * Sensors
  247. */
  248. static void wf_sensor_release(struct kref *kref)
  249. {
  250. struct wf_sensor *sr = container_of(kref, struct wf_sensor, ref);
  251. DBG("wf: Deleting sensor %s\n", sr->name);
  252. if (sr->ops && sr->ops->release)
  253. sr->ops->release(sr);
  254. else
  255. kfree(sr);
  256. }
  257. static ssize_t wf_show_sensor(struct device *dev,
  258. struct device_attribute *attr, char *buf)
  259. {
  260. struct wf_sensor *sens = container_of(attr, struct wf_sensor, attr);
  261. s32 val = 0;
  262. int err;
  263. err = sens->ops->get_value(sens, &val);
  264. if (err < 0)
  265. return err;
  266. return sprintf(buf, "%d.%03d\n", FIX32TOPRINT(val));
  267. }
  268. int wf_register_sensor(struct wf_sensor *new_sr)
  269. {
  270. struct wf_sensor *sr;
  271. mutex_lock(&wf_lock);
  272. list_for_each_entry(sr, &wf_sensors, link) {
  273. if (!strcmp(sr->name, new_sr->name)) {
  274. printk(KERN_WARNING "windfarm: trying to register"
  275. " duplicate sensor %s\n", sr->name);
  276. mutex_unlock(&wf_lock);
  277. return -EEXIST;
  278. }
  279. }
  280. kref_init(&new_sr->ref);
  281. list_add(&new_sr->link, &wf_sensors);
  282. sysfs_attr_init(&new_sr->attr.attr);
  283. new_sr->attr.attr.name = new_sr->name;
  284. new_sr->attr.attr.mode = 0444;
  285. new_sr->attr.show = wf_show_sensor;
  286. new_sr->attr.store = NULL;
  287. if (device_create_file(&wf_platform_device.dev, &new_sr->attr))
  288. printk(KERN_WARNING "windfarm: device_create_file failed"
  289. " for %s\n", new_sr->name);
  290. /* the subsystem still does useful work without the file */
  291. DBG("wf: Registered sensor %s\n", new_sr->name);
  292. wf_notify(WF_EVENT_NEW_SENSOR, new_sr);
  293. mutex_unlock(&wf_lock);
  294. return 0;
  295. }
  296. EXPORT_SYMBOL_GPL(wf_register_sensor);
  297. void wf_unregister_sensor(struct wf_sensor *sr)
  298. {
  299. mutex_lock(&wf_lock);
  300. list_del(&sr->link);
  301. mutex_unlock(&wf_lock);
  302. DBG("wf: Unregistered sensor %s\n", sr->name);
  303. wf_put_sensor(sr);
  304. }
  305. EXPORT_SYMBOL_GPL(wf_unregister_sensor);
  306. struct wf_sensor * wf_find_sensor(const char *name)
  307. {
  308. struct wf_sensor *sr;
  309. mutex_lock(&wf_lock);
  310. list_for_each_entry(sr, &wf_sensors, link) {
  311. if (!strcmp(sr->name, name)) {
  312. if (wf_get_sensor(sr))
  313. sr = NULL;
  314. mutex_unlock(&wf_lock);
  315. return sr;
  316. }
  317. }
  318. mutex_unlock(&wf_lock);
  319. return NULL;
  320. }
  321. EXPORT_SYMBOL_GPL(wf_find_sensor);
  322. int wf_get_sensor(struct wf_sensor *sr)
  323. {
  324. if (!try_module_get(sr->ops->owner))
  325. return -ENODEV;
  326. kref_get(&sr->ref);
  327. return 0;
  328. }
  329. EXPORT_SYMBOL_GPL(wf_get_sensor);
  330. void wf_put_sensor(struct wf_sensor *sr)
  331. {
  332. struct module *mod = sr->ops->owner;
  333. kref_put(&sr->ref, wf_sensor_release);
  334. module_put(mod);
  335. }
  336. EXPORT_SYMBOL_GPL(wf_put_sensor);
  337. /*
  338. * Client & notification
  339. */
  340. int wf_register_client(struct notifier_block *nb)
  341. {
  342. int rc;
  343. struct wf_control *ct;
  344. struct wf_sensor *sr;
  345. mutex_lock(&wf_lock);
  346. rc = blocking_notifier_chain_register(&wf_client_list, nb);
  347. if (rc != 0)
  348. goto bail;
  349. wf_client_count++;
  350. list_for_each_entry(ct, &wf_controls, link)
  351. wf_notify(WF_EVENT_NEW_CONTROL, ct);
  352. list_for_each_entry(sr, &wf_sensors, link)
  353. wf_notify(WF_EVENT_NEW_SENSOR, sr);
  354. if (wf_client_count == 1)
  355. wf_start_thread();
  356. bail:
  357. mutex_unlock(&wf_lock);
  358. return rc;
  359. }
  360. EXPORT_SYMBOL_GPL(wf_register_client);
  361. int wf_unregister_client(struct notifier_block *nb)
  362. {
  363. mutex_lock(&wf_lock);
  364. blocking_notifier_chain_unregister(&wf_client_list, nb);
  365. wf_client_count++;
  366. if (wf_client_count == 0)
  367. wf_stop_thread();
  368. mutex_unlock(&wf_lock);
  369. return 0;
  370. }
  371. EXPORT_SYMBOL_GPL(wf_unregister_client);
  372. void wf_set_overtemp(void)
  373. {
  374. mutex_lock(&wf_lock);
  375. wf_overtemp++;
  376. if (wf_overtemp == 1) {
  377. printk(KERN_WARNING "windfarm: Overtemp condition detected !\n");
  378. wf_overtemp_counter = 0;
  379. wf_notify(WF_EVENT_OVERTEMP, NULL);
  380. }
  381. mutex_unlock(&wf_lock);
  382. }
  383. EXPORT_SYMBOL_GPL(wf_set_overtemp);
  384. void wf_clear_overtemp(void)
  385. {
  386. mutex_lock(&wf_lock);
  387. WARN_ON(wf_overtemp == 0);
  388. if (wf_overtemp == 0) {
  389. mutex_unlock(&wf_lock);
  390. return;
  391. }
  392. wf_overtemp--;
  393. if (wf_overtemp == 0) {
  394. printk(KERN_WARNING "windfarm: Overtemp condition cleared !\n");
  395. wf_notify(WF_EVENT_NORMALTEMP, NULL);
  396. }
  397. mutex_unlock(&wf_lock);
  398. }
  399. EXPORT_SYMBOL_GPL(wf_clear_overtemp);
  400. int wf_is_overtemp(void)
  401. {
  402. return (wf_overtemp != 0);
  403. }
  404. EXPORT_SYMBOL_GPL(wf_is_overtemp);
  405. static int __init windfarm_core_init(void)
  406. {
  407. DBG("wf: core loaded\n");
  408. /* Don't register on old machines that use therm_pm72 for now */
  409. if (of_machine_is_compatible("PowerMac7,2") ||
  410. of_machine_is_compatible("PowerMac7,3") ||
  411. of_machine_is_compatible("RackMac3,1"))
  412. return -ENODEV;
  413. platform_device_register(&wf_platform_device);
  414. return 0;
  415. }
  416. static void __exit windfarm_core_exit(void)
  417. {
  418. BUG_ON(wf_client_count != 0);
  419. DBG("wf: core unloaded\n");
  420. platform_device_unregister(&wf_platform_device);
  421. }
  422. module_init(windfarm_core_init);
  423. module_exit(windfarm_core_exit);
  424. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  425. MODULE_DESCRIPTION("Core component of PowerMac thermal control");
  426. MODULE_LICENSE("GPL");