windfarm_core.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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/smp_lock.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 "windfarm.h"
  36. #define VERSION "0.2"
  37. #undef DEBUG
  38. #ifdef DEBUG
  39. #define DBG(args...) printk(args)
  40. #else
  41. #define DBG(args...) do { } while(0)
  42. #endif
  43. static LIST_HEAD(wf_controls);
  44. static LIST_HEAD(wf_sensors);
  45. static DECLARE_MUTEX(wf_lock);
  46. static struct notifier_block *wf_client_list;
  47. static int wf_client_count;
  48. static unsigned int wf_overtemp;
  49. static unsigned int wf_overtemp_counter;
  50. struct task_struct *wf_thread;
  51. /*
  52. * Utilities & tick thread
  53. */
  54. static inline void wf_notify(int event, void *param)
  55. {
  56. notifier_call_chain(&wf_client_list, event, param);
  57. }
  58. int wf_critical_overtemp(void)
  59. {
  60. static char * critical_overtemp_path = "/sbin/critical_overtemp";
  61. char *argv[] = { critical_overtemp_path, NULL };
  62. static char *envp[] = { "HOME=/",
  63. "TERM=linux",
  64. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  65. NULL };
  66. return call_usermodehelper(critical_overtemp_path, argv, envp, 0);
  67. }
  68. EXPORT_SYMBOL_GPL(wf_critical_overtemp);
  69. static int wf_thread_func(void *data)
  70. {
  71. unsigned long next, delay;
  72. next = jiffies;
  73. DBG("wf: thread started\n");
  74. while(!kthread_should_stop()) {
  75. try_to_freeze();
  76. if (time_after_eq(jiffies, next)) {
  77. wf_notify(WF_EVENT_TICK, NULL);
  78. if (wf_overtemp) {
  79. wf_overtemp_counter++;
  80. /* 10 seconds overtemp, notify userland */
  81. if (wf_overtemp_counter > 10)
  82. wf_critical_overtemp();
  83. /* 30 seconds, shutdown */
  84. if (wf_overtemp_counter > 30) {
  85. printk(KERN_ERR "windfarm: Overtemp "
  86. "for more than 30"
  87. " seconds, shutting down\n");
  88. machine_power_off();
  89. }
  90. }
  91. next += HZ;
  92. }
  93. delay = next - jiffies;
  94. if (delay <= HZ)
  95. schedule_timeout_interruptible(delay);
  96. /* there should be no signal, but oh well */
  97. if (signal_pending(current)) {
  98. printk(KERN_WARNING "windfarm: thread got sigl !\n");
  99. break;
  100. }
  101. }
  102. DBG("wf: thread stopped\n");
  103. return 0;
  104. }
  105. static void wf_start_thread(void)
  106. {
  107. wf_thread = kthread_run(wf_thread_func, NULL, "kwindfarm");
  108. if (IS_ERR(wf_thread)) {
  109. printk(KERN_ERR "windfarm: failed to create thread,err %ld\n",
  110. PTR_ERR(wf_thread));
  111. wf_thread = NULL;
  112. }
  113. }
  114. static void wf_stop_thread(void)
  115. {
  116. if (wf_thread)
  117. kthread_stop(wf_thread);
  118. wf_thread = NULL;
  119. }
  120. /*
  121. * Controls
  122. */
  123. static void wf_control_release(struct kref *kref)
  124. {
  125. struct wf_control *ct = container_of(kref, struct wf_control, ref);
  126. DBG("wf: Deleting control %s\n", ct->name);
  127. if (ct->ops && ct->ops->release)
  128. ct->ops->release(ct);
  129. else
  130. kfree(ct);
  131. }
  132. int wf_register_control(struct wf_control *new_ct)
  133. {
  134. struct wf_control *ct;
  135. down(&wf_lock);
  136. list_for_each_entry(ct, &wf_controls, link) {
  137. if (!strcmp(ct->name, new_ct->name)) {
  138. printk(KERN_WARNING "windfarm: trying to register"
  139. " duplicate control %s\n", ct->name);
  140. up(&wf_lock);
  141. return -EEXIST;
  142. }
  143. }
  144. kref_init(&new_ct->ref);
  145. list_add(&new_ct->link, &wf_controls);
  146. DBG("wf: Registered control %s\n", new_ct->name);
  147. wf_notify(WF_EVENT_NEW_CONTROL, new_ct);
  148. up(&wf_lock);
  149. return 0;
  150. }
  151. EXPORT_SYMBOL_GPL(wf_register_control);
  152. void wf_unregister_control(struct wf_control *ct)
  153. {
  154. down(&wf_lock);
  155. list_del(&ct->link);
  156. up(&wf_lock);
  157. DBG("wf: Unregistered control %s\n", ct->name);
  158. kref_put(&ct->ref, wf_control_release);
  159. }
  160. EXPORT_SYMBOL_GPL(wf_unregister_control);
  161. struct wf_control * wf_find_control(const char *name)
  162. {
  163. struct wf_control *ct;
  164. down(&wf_lock);
  165. list_for_each_entry(ct, &wf_controls, link) {
  166. if (!strcmp(ct->name, name)) {
  167. if (wf_get_control(ct))
  168. ct = NULL;
  169. up(&wf_lock);
  170. return ct;
  171. }
  172. }
  173. up(&wf_lock);
  174. return NULL;
  175. }
  176. EXPORT_SYMBOL_GPL(wf_find_control);
  177. int wf_get_control(struct wf_control *ct)
  178. {
  179. if (!try_module_get(ct->ops->owner))
  180. return -ENODEV;
  181. kref_get(&ct->ref);
  182. return 0;
  183. }
  184. EXPORT_SYMBOL_GPL(wf_get_control);
  185. void wf_put_control(struct wf_control *ct)
  186. {
  187. struct module *mod = ct->ops->owner;
  188. kref_put(&ct->ref, wf_control_release);
  189. module_put(mod);
  190. }
  191. EXPORT_SYMBOL_GPL(wf_put_control);
  192. /*
  193. * Sensors
  194. */
  195. static void wf_sensor_release(struct kref *kref)
  196. {
  197. struct wf_sensor *sr = container_of(kref, struct wf_sensor, ref);
  198. DBG("wf: Deleting sensor %s\n", sr->name);
  199. if (sr->ops && sr->ops->release)
  200. sr->ops->release(sr);
  201. else
  202. kfree(sr);
  203. }
  204. int wf_register_sensor(struct wf_sensor *new_sr)
  205. {
  206. struct wf_sensor *sr;
  207. down(&wf_lock);
  208. list_for_each_entry(sr, &wf_sensors, link) {
  209. if (!strcmp(sr->name, new_sr->name)) {
  210. printk(KERN_WARNING "windfarm: trying to register"
  211. " duplicate sensor %s\n", sr->name);
  212. up(&wf_lock);
  213. return -EEXIST;
  214. }
  215. }
  216. kref_init(&new_sr->ref);
  217. list_add(&new_sr->link, &wf_sensors);
  218. DBG("wf: Registered sensor %s\n", new_sr->name);
  219. wf_notify(WF_EVENT_NEW_SENSOR, new_sr);
  220. up(&wf_lock);
  221. return 0;
  222. }
  223. EXPORT_SYMBOL_GPL(wf_register_sensor);
  224. void wf_unregister_sensor(struct wf_sensor *sr)
  225. {
  226. down(&wf_lock);
  227. list_del(&sr->link);
  228. up(&wf_lock);
  229. DBG("wf: Unregistered sensor %s\n", sr->name);
  230. wf_put_sensor(sr);
  231. }
  232. EXPORT_SYMBOL_GPL(wf_unregister_sensor);
  233. struct wf_sensor * wf_find_sensor(const char *name)
  234. {
  235. struct wf_sensor *sr;
  236. down(&wf_lock);
  237. list_for_each_entry(sr, &wf_sensors, link) {
  238. if (!strcmp(sr->name, name)) {
  239. if (wf_get_sensor(sr))
  240. sr = NULL;
  241. up(&wf_lock);
  242. return sr;
  243. }
  244. }
  245. up(&wf_lock);
  246. return NULL;
  247. }
  248. EXPORT_SYMBOL_GPL(wf_find_sensor);
  249. int wf_get_sensor(struct wf_sensor *sr)
  250. {
  251. if (!try_module_get(sr->ops->owner))
  252. return -ENODEV;
  253. kref_get(&sr->ref);
  254. return 0;
  255. }
  256. EXPORT_SYMBOL_GPL(wf_get_sensor);
  257. void wf_put_sensor(struct wf_sensor *sr)
  258. {
  259. struct module *mod = sr->ops->owner;
  260. kref_put(&sr->ref, wf_sensor_release);
  261. module_put(mod);
  262. }
  263. EXPORT_SYMBOL_GPL(wf_put_sensor);
  264. /*
  265. * Client & notification
  266. */
  267. int wf_register_client(struct notifier_block *nb)
  268. {
  269. int rc;
  270. struct wf_control *ct;
  271. struct wf_sensor *sr;
  272. down(&wf_lock);
  273. rc = notifier_chain_register(&wf_client_list, nb);
  274. if (rc != 0)
  275. goto bail;
  276. wf_client_count++;
  277. list_for_each_entry(ct, &wf_controls, link)
  278. wf_notify(WF_EVENT_NEW_CONTROL, ct);
  279. list_for_each_entry(sr, &wf_sensors, link)
  280. wf_notify(WF_EVENT_NEW_SENSOR, sr);
  281. if (wf_client_count == 1)
  282. wf_start_thread();
  283. bail:
  284. up(&wf_lock);
  285. return rc;
  286. }
  287. EXPORT_SYMBOL_GPL(wf_register_client);
  288. int wf_unregister_client(struct notifier_block *nb)
  289. {
  290. down(&wf_lock);
  291. notifier_chain_unregister(&wf_client_list, nb);
  292. wf_client_count++;
  293. if (wf_client_count == 0)
  294. wf_stop_thread();
  295. up(&wf_lock);
  296. return 0;
  297. }
  298. EXPORT_SYMBOL_GPL(wf_unregister_client);
  299. void wf_set_overtemp(void)
  300. {
  301. down(&wf_lock);
  302. wf_overtemp++;
  303. if (wf_overtemp == 1) {
  304. printk(KERN_WARNING "windfarm: Overtemp condition detected !\n");
  305. wf_overtemp_counter = 0;
  306. wf_notify(WF_EVENT_OVERTEMP, NULL);
  307. }
  308. up(&wf_lock);
  309. }
  310. EXPORT_SYMBOL_GPL(wf_set_overtemp);
  311. void wf_clear_overtemp(void)
  312. {
  313. down(&wf_lock);
  314. WARN_ON(wf_overtemp == 0);
  315. if (wf_overtemp == 0) {
  316. up(&wf_lock);
  317. return;
  318. }
  319. wf_overtemp--;
  320. if (wf_overtemp == 0) {
  321. printk(KERN_WARNING "windfarm: Overtemp condition cleared !\n");
  322. wf_notify(WF_EVENT_NORMALTEMP, NULL);
  323. }
  324. up(&wf_lock);
  325. }
  326. EXPORT_SYMBOL_GPL(wf_clear_overtemp);
  327. int wf_is_overtemp(void)
  328. {
  329. return (wf_overtemp != 0);
  330. }
  331. EXPORT_SYMBOL_GPL(wf_is_overtemp);
  332. static struct platform_device wf_platform_device = {
  333. .name = "windfarm",
  334. };
  335. static int __init windfarm_core_init(void)
  336. {
  337. DBG("wf: core loaded\n");
  338. platform_device_register(&wf_platform_device);
  339. return 0;
  340. }
  341. static void __exit windfarm_core_exit(void)
  342. {
  343. BUG_ON(wf_client_count != 0);
  344. DBG("wf: core unloaded\n");
  345. platform_device_unregister(&wf_platform_device);
  346. }
  347. module_init(windfarm_core_init);
  348. module_exit(windfarm_core_exit);
  349. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  350. MODULE_DESCRIPTION("Core component of PowerMac thermal control");
  351. MODULE_LICENSE("GPL");