windfarm_core.c 11 KB

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