windfarm_core.c 8.9 KB

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