windfarm_core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Windfarm PowerMac thermal control. Core
  4. *
  5. * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
  6. * <benh@kernel.crashing.org>
  7. *
  8. * This core code tracks the list of sensors & controls, register
  9. * clients, and holds the kernel thread used for control.
  10. *
  11. * TODO:
  12. *
  13. * Add some information about sensor/control type and data format to
  14. * sensors/controls, and have the sysfs attribute stuff be moved
  15. * generically here instead of hard coded in the platform specific
  16. * driver as it us currently
  17. *
  18. * This however requires solving some annoying lifetime issues with
  19. * sysfs which doesn't seem to have lifetime rules for struct attribute,
  20. * I may have to create full features kobjects for every sensor/control
  21. * instead which is a bit of an overkill imho
  22. */
  23. #include <linux/types.h>
  24. #include <linux/errno.h>
  25. #include <linux/kernel.h>
  26. #include <linux/slab.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. static int wf_critical_overtemp(void)
  64. {
  65. static char const critical_overtemp_path[] = "/sbin/critical_overtemp";
  66. char *argv[] = { (char *)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. static int wf_thread_func(void *data)
  75. {
  76. unsigned long next, delay;
  77. next = jiffies;
  78. DBG("wf: thread started\n");
  79. set_freezable();
  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. }
  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. static ssize_t wf_show_control(struct device *dev,
  134. struct device_attribute *attr, char *buf)
  135. {
  136. struct wf_control *ctrl = container_of(attr, struct wf_control, attr);
  137. const char *typestr;
  138. s32 val = 0;
  139. int err;
  140. err = ctrl->ops->get_value(ctrl, &val);
  141. if (err < 0) {
  142. if (err == -EFAULT)
  143. return sprintf(buf, "<HW FAULT>\n");
  144. return err;
  145. }
  146. switch(ctrl->type) {
  147. case WF_CONTROL_RPM_FAN:
  148. typestr = " RPM";
  149. break;
  150. case WF_CONTROL_PWM_FAN:
  151. typestr = " %";
  152. break;
  153. default:
  154. typestr = "";
  155. }
  156. return sprintf(buf, "%d%s\n", val, typestr);
  157. }
  158. /* This is really only for debugging... */
  159. static ssize_t wf_store_control(struct device *dev,
  160. struct device_attribute *attr,
  161. const char *buf, size_t count)
  162. {
  163. struct wf_control *ctrl = container_of(attr, struct wf_control, attr);
  164. int val;
  165. int err;
  166. char *endp;
  167. val = simple_strtoul(buf, &endp, 0);
  168. while (endp < buf + count && (*endp == ' ' || *endp == '\n'))
  169. ++endp;
  170. if (endp - buf < count)
  171. return -EINVAL;
  172. err = ctrl->ops->set_value(ctrl, val);
  173. if (err < 0)
  174. return err;
  175. return count;
  176. }
  177. int wf_register_control(struct wf_control *new_ct)
  178. {
  179. struct wf_control *ct;
  180. mutex_lock(&wf_lock);
  181. list_for_each_entry(ct, &wf_controls, link) {
  182. if (!strcmp(ct->name, new_ct->name)) {
  183. printk(KERN_WARNING "windfarm: trying to register"
  184. " duplicate control %s\n", ct->name);
  185. mutex_unlock(&wf_lock);
  186. return -EEXIST;
  187. }
  188. }
  189. kref_init(&new_ct->ref);
  190. list_add(&new_ct->link, &wf_controls);
  191. sysfs_attr_init(&new_ct->attr.attr);
  192. new_ct->attr.attr.name = new_ct->name;
  193. new_ct->attr.attr.mode = 0644;
  194. new_ct->attr.show = wf_show_control;
  195. new_ct->attr.store = wf_store_control;
  196. if (device_create_file(&wf_platform_device.dev, &new_ct->attr))
  197. printk(KERN_WARNING "windfarm: device_create_file failed"
  198. " for %s\n", new_ct->name);
  199. /* the subsystem still does useful work without the file */
  200. DBG("wf: Registered control %s\n", new_ct->name);
  201. wf_notify(WF_EVENT_NEW_CONTROL, new_ct);
  202. mutex_unlock(&wf_lock);
  203. return 0;
  204. }
  205. EXPORT_SYMBOL_GPL(wf_register_control);
  206. void wf_unregister_control(struct wf_control *ct)
  207. {
  208. mutex_lock(&wf_lock);
  209. list_del(&ct->link);
  210. mutex_unlock(&wf_lock);
  211. DBG("wf: Unregistered control %s\n", ct->name);
  212. kref_put(&ct->ref, wf_control_release);
  213. }
  214. EXPORT_SYMBOL_GPL(wf_unregister_control);
  215. int wf_get_control(struct wf_control *ct)
  216. {
  217. if (!try_module_get(ct->ops->owner))
  218. return -ENODEV;
  219. kref_get(&ct->ref);
  220. return 0;
  221. }
  222. EXPORT_SYMBOL_GPL(wf_get_control);
  223. void wf_put_control(struct wf_control *ct)
  224. {
  225. struct module *mod = ct->ops->owner;
  226. kref_put(&ct->ref, wf_control_release);
  227. module_put(mod);
  228. }
  229. EXPORT_SYMBOL_GPL(wf_put_control);
  230. /*
  231. * Sensors
  232. */
  233. static void wf_sensor_release(struct kref *kref)
  234. {
  235. struct wf_sensor *sr = container_of(kref, struct wf_sensor, ref);
  236. DBG("wf: Deleting sensor %s\n", sr->name);
  237. if (sr->ops && sr->ops->release)
  238. sr->ops->release(sr);
  239. else
  240. kfree(sr);
  241. }
  242. static ssize_t wf_show_sensor(struct device *dev,
  243. struct device_attribute *attr, char *buf)
  244. {
  245. struct wf_sensor *sens = container_of(attr, struct wf_sensor, attr);
  246. s32 val = 0;
  247. int err;
  248. err = sens->ops->get_value(sens, &val);
  249. if (err < 0)
  250. return err;
  251. return sprintf(buf, "%d.%03d\n", FIX32TOPRINT(val));
  252. }
  253. int wf_register_sensor(struct wf_sensor *new_sr)
  254. {
  255. struct wf_sensor *sr;
  256. mutex_lock(&wf_lock);
  257. list_for_each_entry(sr, &wf_sensors, link) {
  258. if (!strcmp(sr->name, new_sr->name)) {
  259. printk(KERN_WARNING "windfarm: trying to register"
  260. " duplicate sensor %s\n", sr->name);
  261. mutex_unlock(&wf_lock);
  262. return -EEXIST;
  263. }
  264. }
  265. kref_init(&new_sr->ref);
  266. list_add(&new_sr->link, &wf_sensors);
  267. sysfs_attr_init(&new_sr->attr.attr);
  268. new_sr->attr.attr.name = new_sr->name;
  269. new_sr->attr.attr.mode = 0444;
  270. new_sr->attr.show = wf_show_sensor;
  271. new_sr->attr.store = NULL;
  272. if (device_create_file(&wf_platform_device.dev, &new_sr->attr))
  273. printk(KERN_WARNING "windfarm: device_create_file failed"
  274. " for %s\n", new_sr->name);
  275. /* the subsystem still does useful work without the file */
  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. int wf_get_sensor(struct wf_sensor *sr)
  292. {
  293. if (!try_module_get(sr->ops->owner))
  294. return -ENODEV;
  295. kref_get(&sr->ref);
  296. return 0;
  297. }
  298. EXPORT_SYMBOL_GPL(wf_get_sensor);
  299. void wf_put_sensor(struct wf_sensor *sr)
  300. {
  301. struct module *mod = sr->ops->owner;
  302. kref_put(&sr->ref, wf_sensor_release);
  303. module_put(mod);
  304. }
  305. EXPORT_SYMBOL_GPL(wf_put_sensor);
  306. /*
  307. * Client & notification
  308. */
  309. int wf_register_client(struct notifier_block *nb)
  310. {
  311. int rc;
  312. struct wf_control *ct;
  313. struct wf_sensor *sr;
  314. mutex_lock(&wf_lock);
  315. rc = blocking_notifier_chain_register(&wf_client_list, nb);
  316. if (rc != 0)
  317. goto bail;
  318. wf_client_count++;
  319. list_for_each_entry(ct, &wf_controls, link)
  320. wf_notify(WF_EVENT_NEW_CONTROL, ct);
  321. list_for_each_entry(sr, &wf_sensors, link)
  322. wf_notify(WF_EVENT_NEW_SENSOR, sr);
  323. if (wf_client_count == 1)
  324. wf_start_thread();
  325. bail:
  326. mutex_unlock(&wf_lock);
  327. return rc;
  328. }
  329. EXPORT_SYMBOL_GPL(wf_register_client);
  330. int wf_unregister_client(struct notifier_block *nb)
  331. {
  332. mutex_lock(&wf_lock);
  333. blocking_notifier_chain_unregister(&wf_client_list, nb);
  334. wf_client_count--;
  335. if (wf_client_count == 0)
  336. wf_stop_thread();
  337. mutex_unlock(&wf_lock);
  338. return 0;
  339. }
  340. EXPORT_SYMBOL_GPL(wf_unregister_client);
  341. void wf_set_overtemp(void)
  342. {
  343. mutex_lock(&wf_lock);
  344. wf_overtemp++;
  345. if (wf_overtemp == 1) {
  346. printk(KERN_WARNING "windfarm: Overtemp condition detected !\n");
  347. wf_overtemp_counter = 0;
  348. wf_notify(WF_EVENT_OVERTEMP, NULL);
  349. }
  350. mutex_unlock(&wf_lock);
  351. }
  352. EXPORT_SYMBOL_GPL(wf_set_overtemp);
  353. void wf_clear_overtemp(void)
  354. {
  355. mutex_lock(&wf_lock);
  356. WARN_ON(wf_overtemp == 0);
  357. if (wf_overtemp == 0) {
  358. mutex_unlock(&wf_lock);
  359. return;
  360. }
  361. wf_overtemp--;
  362. if (wf_overtemp == 0) {
  363. printk(KERN_WARNING "windfarm: Overtemp condition cleared !\n");
  364. wf_notify(WF_EVENT_NORMALTEMP, NULL);
  365. }
  366. mutex_unlock(&wf_lock);
  367. }
  368. EXPORT_SYMBOL_GPL(wf_clear_overtemp);
  369. static int __init windfarm_core_init(void)
  370. {
  371. DBG("wf: core loaded\n");
  372. platform_device_register(&wf_platform_device);
  373. return 0;
  374. }
  375. static void __exit windfarm_core_exit(void)
  376. {
  377. BUG_ON(wf_client_count != 0);
  378. DBG("wf: core unloaded\n");
  379. platform_device_unregister(&wf_platform_device);
  380. }
  381. module_init(windfarm_core_init);
  382. module_exit(windfarm_core_exit);
  383. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  384. MODULE_DESCRIPTION("Core component of PowerMac thermal control");
  385. MODULE_LICENSE("GPL");