windfarm_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. 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 non-suspend signal, but oh well */
  103. if (signal_pending(current) && !try_to_freeze()) {
  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.mode = 0644;
  184. new_ct->attr.show = wf_show_control;
  185. new_ct->attr.store = wf_store_control;
  186. if (device_create_file(&wf_platform_device.dev, &new_ct->attr))
  187. printk(KERN_WARNING "windfarm: device_create_file failed"
  188. " for %s\n", new_ct->name);
  189. /* the subsystem still does useful work without the file */
  190. DBG("wf: Registered control %s\n", new_ct->name);
  191. wf_notify(WF_EVENT_NEW_CONTROL, new_ct);
  192. mutex_unlock(&wf_lock);
  193. return 0;
  194. }
  195. EXPORT_SYMBOL_GPL(wf_register_control);
  196. void wf_unregister_control(struct wf_control *ct)
  197. {
  198. mutex_lock(&wf_lock);
  199. list_del(&ct->link);
  200. mutex_unlock(&wf_lock);
  201. DBG("wf: Unregistered control %s\n", ct->name);
  202. kref_put(&ct->ref, wf_control_release);
  203. }
  204. EXPORT_SYMBOL_GPL(wf_unregister_control);
  205. struct wf_control * wf_find_control(const char *name)
  206. {
  207. struct wf_control *ct;
  208. mutex_lock(&wf_lock);
  209. list_for_each_entry(ct, &wf_controls, link) {
  210. if (!strcmp(ct->name, name)) {
  211. if (wf_get_control(ct))
  212. ct = NULL;
  213. mutex_unlock(&wf_lock);
  214. return ct;
  215. }
  216. }
  217. mutex_unlock(&wf_lock);
  218. return NULL;
  219. }
  220. EXPORT_SYMBOL_GPL(wf_find_control);
  221. int wf_get_control(struct wf_control *ct)
  222. {
  223. if (!try_module_get(ct->ops->owner))
  224. return -ENODEV;
  225. kref_get(&ct->ref);
  226. return 0;
  227. }
  228. EXPORT_SYMBOL_GPL(wf_get_control);
  229. void wf_put_control(struct wf_control *ct)
  230. {
  231. struct module *mod = ct->ops->owner;
  232. kref_put(&ct->ref, wf_control_release);
  233. module_put(mod);
  234. }
  235. EXPORT_SYMBOL_GPL(wf_put_control);
  236. /*
  237. * Sensors
  238. */
  239. static void wf_sensor_release(struct kref *kref)
  240. {
  241. struct wf_sensor *sr = container_of(kref, struct wf_sensor, ref);
  242. DBG("wf: Deleting sensor %s\n", sr->name);
  243. if (sr->ops && sr->ops->release)
  244. sr->ops->release(sr);
  245. else
  246. kfree(sr);
  247. }
  248. static ssize_t wf_show_sensor(struct device *dev,
  249. struct device_attribute *attr, char *buf)
  250. {
  251. struct wf_sensor *sens = container_of(attr, struct wf_sensor, attr);
  252. s32 val = 0;
  253. int err;
  254. err = sens->ops->get_value(sens, &val);
  255. if (err < 0)
  256. return err;
  257. return sprintf(buf, "%d.%03d\n", FIX32TOPRINT(val));
  258. }
  259. int wf_register_sensor(struct wf_sensor *new_sr)
  260. {
  261. struct wf_sensor *sr;
  262. mutex_lock(&wf_lock);
  263. list_for_each_entry(sr, &wf_sensors, link) {
  264. if (!strcmp(sr->name, new_sr->name)) {
  265. printk(KERN_WARNING "windfarm: trying to register"
  266. " duplicate sensor %s\n", sr->name);
  267. mutex_unlock(&wf_lock);
  268. return -EEXIST;
  269. }
  270. }
  271. kref_init(&new_sr->ref);
  272. list_add(&new_sr->link, &wf_sensors);
  273. new_sr->attr.attr.name = new_sr->name;
  274. new_sr->attr.attr.mode = 0444;
  275. new_sr->attr.show = wf_show_sensor;
  276. new_sr->attr.store = NULL;
  277. if (device_create_file(&wf_platform_device.dev, &new_sr->attr))
  278. printk(KERN_WARNING "windfarm: device_create_file failed"
  279. " for %s\n", new_sr->name);
  280. /* the subsystem still does useful work without the file */
  281. DBG("wf: Registered sensor %s\n", new_sr->name);
  282. wf_notify(WF_EVENT_NEW_SENSOR, new_sr);
  283. mutex_unlock(&wf_lock);
  284. return 0;
  285. }
  286. EXPORT_SYMBOL_GPL(wf_register_sensor);
  287. void wf_unregister_sensor(struct wf_sensor *sr)
  288. {
  289. mutex_lock(&wf_lock);
  290. list_del(&sr->link);
  291. mutex_unlock(&wf_lock);
  292. DBG("wf: Unregistered sensor %s\n", sr->name);
  293. wf_put_sensor(sr);
  294. }
  295. EXPORT_SYMBOL_GPL(wf_unregister_sensor);
  296. struct wf_sensor * wf_find_sensor(const char *name)
  297. {
  298. struct wf_sensor *sr;
  299. mutex_lock(&wf_lock);
  300. list_for_each_entry(sr, &wf_sensors, link) {
  301. if (!strcmp(sr->name, name)) {
  302. if (wf_get_sensor(sr))
  303. sr = NULL;
  304. mutex_unlock(&wf_lock);
  305. return sr;
  306. }
  307. }
  308. mutex_unlock(&wf_lock);
  309. return NULL;
  310. }
  311. EXPORT_SYMBOL_GPL(wf_find_sensor);
  312. int wf_get_sensor(struct wf_sensor *sr)
  313. {
  314. if (!try_module_get(sr->ops->owner))
  315. return -ENODEV;
  316. kref_get(&sr->ref);
  317. return 0;
  318. }
  319. EXPORT_SYMBOL_GPL(wf_get_sensor);
  320. void wf_put_sensor(struct wf_sensor *sr)
  321. {
  322. struct module *mod = sr->ops->owner;
  323. kref_put(&sr->ref, wf_sensor_release);
  324. module_put(mod);
  325. }
  326. EXPORT_SYMBOL_GPL(wf_put_sensor);
  327. /*
  328. * Client & notification
  329. */
  330. int wf_register_client(struct notifier_block *nb)
  331. {
  332. int rc;
  333. struct wf_control *ct;
  334. struct wf_sensor *sr;
  335. mutex_lock(&wf_lock);
  336. rc = blocking_notifier_chain_register(&wf_client_list, nb);
  337. if (rc != 0)
  338. goto bail;
  339. wf_client_count++;
  340. list_for_each_entry(ct, &wf_controls, link)
  341. wf_notify(WF_EVENT_NEW_CONTROL, ct);
  342. list_for_each_entry(sr, &wf_sensors, link)
  343. wf_notify(WF_EVENT_NEW_SENSOR, sr);
  344. if (wf_client_count == 1)
  345. wf_start_thread();
  346. bail:
  347. mutex_unlock(&wf_lock);
  348. return rc;
  349. }
  350. EXPORT_SYMBOL_GPL(wf_register_client);
  351. int wf_unregister_client(struct notifier_block *nb)
  352. {
  353. mutex_lock(&wf_lock);
  354. blocking_notifier_chain_unregister(&wf_client_list, nb);
  355. wf_client_count++;
  356. if (wf_client_count == 0)
  357. wf_stop_thread();
  358. mutex_unlock(&wf_lock);
  359. return 0;
  360. }
  361. EXPORT_SYMBOL_GPL(wf_unregister_client);
  362. void wf_set_overtemp(void)
  363. {
  364. mutex_lock(&wf_lock);
  365. wf_overtemp++;
  366. if (wf_overtemp == 1) {
  367. printk(KERN_WARNING "windfarm: Overtemp condition detected !\n");
  368. wf_overtemp_counter = 0;
  369. wf_notify(WF_EVENT_OVERTEMP, NULL);
  370. }
  371. mutex_unlock(&wf_lock);
  372. }
  373. EXPORT_SYMBOL_GPL(wf_set_overtemp);
  374. void wf_clear_overtemp(void)
  375. {
  376. mutex_lock(&wf_lock);
  377. WARN_ON(wf_overtemp == 0);
  378. if (wf_overtemp == 0) {
  379. mutex_unlock(&wf_lock);
  380. return;
  381. }
  382. wf_overtemp--;
  383. if (wf_overtemp == 0) {
  384. printk(KERN_WARNING "windfarm: Overtemp condition cleared !\n");
  385. wf_notify(WF_EVENT_NORMALTEMP, NULL);
  386. }
  387. mutex_unlock(&wf_lock);
  388. }
  389. EXPORT_SYMBOL_GPL(wf_clear_overtemp);
  390. int wf_is_overtemp(void)
  391. {
  392. return (wf_overtemp != 0);
  393. }
  394. EXPORT_SYMBOL_GPL(wf_is_overtemp);
  395. static int __init windfarm_core_init(void)
  396. {
  397. DBG("wf: core loaded\n");
  398. /* Don't register on old machines that use therm_pm72 for now */
  399. if (machine_is_compatible("PowerMac7,2") ||
  400. machine_is_compatible("PowerMac7,3") ||
  401. machine_is_compatible("RackMac3,1"))
  402. return -ENODEV;
  403. platform_device_register(&wf_platform_device);
  404. return 0;
  405. }
  406. static void __exit windfarm_core_exit(void)
  407. {
  408. BUG_ON(wf_client_count != 0);
  409. DBG("wf: core unloaded\n");
  410. platform_device_unregister(&wf_platform_device);
  411. }
  412. module_init(windfarm_core_init);
  413. module_exit(windfarm_core_exit);
  414. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  415. MODULE_DESCRIPTION("Core component of PowerMac thermal control");
  416. MODULE_LICENSE("GPL");