windfarm_core.c 11 KB

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