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