windfarm_core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 "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 BLOCKING_NOTIFIER_HEAD(wf_client_list);
  48. static int wf_client_count;
  49. static unsigned int wf_overtemp;
  50. static unsigned int wf_overtemp_counter;
  51. static struct task_struct *wf_thread;
  52. static struct platform_device wf_platform_device = {
  53. .name = "windfarm",
  54. };
  55. /*
  56. * Utilities & tick thread
  57. */
  58. static inline void wf_notify(int event, void *param)
  59. {
  60. blocking_notifier_call_chain(&wf_client_list, event, param);
  61. }
  62. static int wf_critical_overtemp(void)
  63. {
  64. static char const critical_overtemp_path[] = "/sbin/critical_overtemp";
  65. char *argv[] = { (char *)critical_overtemp_path, NULL };
  66. static char *envp[] = { "HOME=/",
  67. "TERM=linux",
  68. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  69. NULL };
  70. return call_usermodehelper(critical_overtemp_path,
  71. argv, envp, UMH_WAIT_EXEC);
  72. }
  73. static int wf_thread_func(void *data)
  74. {
  75. unsigned long next, delay;
  76. next = jiffies;
  77. DBG("wf: thread started\n");
  78. set_freezable();
  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. }
  102. DBG("wf: thread stopped\n");
  103. return 0;
  104. }
  105. static void wf_start_thread(void)
  106. {
  107. wf_thread = kthread_run(wf_thread_func, NULL, "kwindfarm");
  108. if (IS_ERR(wf_thread)) {
  109. printk(KERN_ERR "windfarm: failed to create thread,err %ld\n",
  110. PTR_ERR(wf_thread));
  111. wf_thread = NULL;
  112. }
  113. }
  114. static void wf_stop_thread(void)
  115. {
  116. if (wf_thread)
  117. kthread_stop(wf_thread);
  118. wf_thread = NULL;
  119. }
  120. /*
  121. * Controls
  122. */
  123. static void wf_control_release(struct kref *kref)
  124. {
  125. struct wf_control *ct = container_of(kref, struct wf_control, ref);
  126. DBG("wf: Deleting control %s\n", ct->name);
  127. if (ct->ops && ct->ops->release)
  128. ct->ops->release(ct);
  129. else
  130. kfree(ct);
  131. }
  132. static ssize_t wf_show_control(struct device *dev,
  133. struct device_attribute *attr, char *buf)
  134. {
  135. struct wf_control *ctrl = container_of(attr, struct wf_control, attr);
  136. const char *typestr;
  137. s32 val = 0;
  138. int err;
  139. err = ctrl->ops->get_value(ctrl, &val);
  140. if (err < 0) {
  141. if (err == -EFAULT)
  142. return sprintf(buf, "<HW FAULT>\n");
  143. return err;
  144. }
  145. switch(ctrl->type) {
  146. case WF_CONTROL_RPM_FAN:
  147. typestr = " RPM";
  148. break;
  149. case WF_CONTROL_PWM_FAN:
  150. typestr = " %";
  151. break;
  152. default:
  153. typestr = "";
  154. }
  155. return sprintf(buf, "%d%s\n", val, typestr);
  156. }
  157. /* This is really only for debugging... */
  158. static ssize_t wf_store_control(struct device *dev,
  159. struct device_attribute *attr,
  160. const char *buf, size_t count)
  161. {
  162. struct wf_control *ctrl = container_of(attr, struct wf_control, attr);
  163. int val;
  164. int err;
  165. char *endp;
  166. val = simple_strtoul(buf, &endp, 0);
  167. while (endp < buf + count && (*endp == ' ' || *endp == '\n'))
  168. ++endp;
  169. if (endp - buf < count)
  170. return -EINVAL;
  171. err = ctrl->ops->set_value(ctrl, val);
  172. if (err < 0)
  173. return err;
  174. return count;
  175. }
  176. int wf_register_control(struct wf_control *new_ct)
  177. {
  178. struct wf_control *ct;
  179. mutex_lock(&wf_lock);
  180. list_for_each_entry(ct, &wf_controls, link) {
  181. if (!strcmp(ct->name, new_ct->name)) {
  182. printk(KERN_WARNING "windfarm: trying to register"
  183. " duplicate control %s\n", ct->name);
  184. mutex_unlock(&wf_lock);
  185. return -EEXIST;
  186. }
  187. }
  188. kref_init(&new_ct->ref);
  189. list_add(&new_ct->link, &wf_controls);
  190. sysfs_attr_init(&new_ct->attr.attr);
  191. new_ct->attr.attr.name = new_ct->name;
  192. new_ct->attr.attr.mode = 0644;
  193. new_ct->attr.show = wf_show_control;
  194. new_ct->attr.store = wf_store_control;
  195. if (device_create_file(&wf_platform_device.dev, &new_ct->attr))
  196. printk(KERN_WARNING "windfarm: device_create_file failed"
  197. " for %s\n", new_ct->name);
  198. /* the subsystem still does useful work without the file */
  199. DBG("wf: Registered control %s\n", new_ct->name);
  200. wf_notify(WF_EVENT_NEW_CONTROL, new_ct);
  201. mutex_unlock(&wf_lock);
  202. return 0;
  203. }
  204. EXPORT_SYMBOL_GPL(wf_register_control);
  205. void wf_unregister_control(struct wf_control *ct)
  206. {
  207. mutex_lock(&wf_lock);
  208. list_del(&ct->link);
  209. mutex_unlock(&wf_lock);
  210. DBG("wf: Unregistered control %s\n", ct->name);
  211. kref_put(&ct->ref, wf_control_release);
  212. }
  213. EXPORT_SYMBOL_GPL(wf_unregister_control);
  214. int wf_get_control(struct wf_control *ct)
  215. {
  216. if (!try_module_get(ct->ops->owner))
  217. return -ENODEV;
  218. kref_get(&ct->ref);
  219. return 0;
  220. }
  221. EXPORT_SYMBOL_GPL(wf_get_control);
  222. void wf_put_control(struct wf_control *ct)
  223. {
  224. struct module *mod = ct->ops->owner;
  225. kref_put(&ct->ref, wf_control_release);
  226. module_put(mod);
  227. }
  228. EXPORT_SYMBOL_GPL(wf_put_control);
  229. /*
  230. * Sensors
  231. */
  232. static void wf_sensor_release(struct kref *kref)
  233. {
  234. struct wf_sensor *sr = container_of(kref, struct wf_sensor, ref);
  235. DBG("wf: Deleting sensor %s\n", sr->name);
  236. if (sr->ops && sr->ops->release)
  237. sr->ops->release(sr);
  238. else
  239. kfree(sr);
  240. }
  241. static ssize_t wf_show_sensor(struct device *dev,
  242. struct device_attribute *attr, char *buf)
  243. {
  244. struct wf_sensor *sens = container_of(attr, struct wf_sensor, attr);
  245. s32 val = 0;
  246. int err;
  247. err = sens->ops->get_value(sens, &val);
  248. if (err < 0)
  249. return err;
  250. return sprintf(buf, "%d.%03d\n", FIX32TOPRINT(val));
  251. }
  252. int wf_register_sensor(struct wf_sensor *new_sr)
  253. {
  254. struct wf_sensor *sr;
  255. mutex_lock(&wf_lock);
  256. list_for_each_entry(sr, &wf_sensors, link) {
  257. if (!strcmp(sr->name, new_sr->name)) {
  258. printk(KERN_WARNING "windfarm: trying to register"
  259. " duplicate sensor %s\n", sr->name);
  260. mutex_unlock(&wf_lock);
  261. return -EEXIST;
  262. }
  263. }
  264. kref_init(&new_sr->ref);
  265. list_add(&new_sr->link, &wf_sensors);
  266. sysfs_attr_init(&new_sr->attr.attr);
  267. new_sr->attr.attr.name = new_sr->name;
  268. new_sr->attr.attr.mode = 0444;
  269. new_sr->attr.show = wf_show_sensor;
  270. new_sr->attr.store = NULL;
  271. if (device_create_file(&wf_platform_device.dev, &new_sr->attr))
  272. printk(KERN_WARNING "windfarm: device_create_file failed"
  273. " for %s\n", new_sr->name);
  274. /* the subsystem still does useful work without the file */
  275. DBG("wf: Registered sensor %s\n", new_sr->name);
  276. wf_notify(WF_EVENT_NEW_SENSOR, new_sr);
  277. mutex_unlock(&wf_lock);
  278. return 0;
  279. }
  280. EXPORT_SYMBOL_GPL(wf_register_sensor);
  281. void wf_unregister_sensor(struct wf_sensor *sr)
  282. {
  283. mutex_lock(&wf_lock);
  284. list_del(&sr->link);
  285. mutex_unlock(&wf_lock);
  286. DBG("wf: Unregistered sensor %s\n", sr->name);
  287. wf_put_sensor(sr);
  288. }
  289. EXPORT_SYMBOL_GPL(wf_unregister_sensor);
  290. int wf_get_sensor(struct wf_sensor *sr)
  291. {
  292. if (!try_module_get(sr->ops->owner))
  293. return -ENODEV;
  294. kref_get(&sr->ref);
  295. return 0;
  296. }
  297. EXPORT_SYMBOL_GPL(wf_get_sensor);
  298. void wf_put_sensor(struct wf_sensor *sr)
  299. {
  300. struct module *mod = sr->ops->owner;
  301. kref_put(&sr->ref, wf_sensor_release);
  302. module_put(mod);
  303. }
  304. EXPORT_SYMBOL_GPL(wf_put_sensor);
  305. /*
  306. * Client & notification
  307. */
  308. int wf_register_client(struct notifier_block *nb)
  309. {
  310. int rc;
  311. struct wf_control *ct;
  312. struct wf_sensor *sr;
  313. mutex_lock(&wf_lock);
  314. rc = blocking_notifier_chain_register(&wf_client_list, nb);
  315. if (rc != 0)
  316. goto bail;
  317. wf_client_count++;
  318. list_for_each_entry(ct, &wf_controls, link)
  319. wf_notify(WF_EVENT_NEW_CONTROL, ct);
  320. list_for_each_entry(sr, &wf_sensors, link)
  321. wf_notify(WF_EVENT_NEW_SENSOR, sr);
  322. if (wf_client_count == 1)
  323. wf_start_thread();
  324. bail:
  325. mutex_unlock(&wf_lock);
  326. return rc;
  327. }
  328. EXPORT_SYMBOL_GPL(wf_register_client);
  329. int wf_unregister_client(struct notifier_block *nb)
  330. {
  331. mutex_lock(&wf_lock);
  332. blocking_notifier_chain_unregister(&wf_client_list, nb);
  333. wf_client_count--;
  334. if (wf_client_count == 0)
  335. wf_stop_thread();
  336. mutex_unlock(&wf_lock);
  337. return 0;
  338. }
  339. EXPORT_SYMBOL_GPL(wf_unregister_client);
  340. void wf_set_overtemp(void)
  341. {
  342. mutex_lock(&wf_lock);
  343. wf_overtemp++;
  344. if (wf_overtemp == 1) {
  345. printk(KERN_WARNING "windfarm: Overtemp condition detected !\n");
  346. wf_overtemp_counter = 0;
  347. wf_notify(WF_EVENT_OVERTEMP, NULL);
  348. }
  349. mutex_unlock(&wf_lock);
  350. }
  351. EXPORT_SYMBOL_GPL(wf_set_overtemp);
  352. void wf_clear_overtemp(void)
  353. {
  354. mutex_lock(&wf_lock);
  355. WARN_ON(wf_overtemp == 0);
  356. if (wf_overtemp == 0) {
  357. mutex_unlock(&wf_lock);
  358. return;
  359. }
  360. wf_overtemp--;
  361. if (wf_overtemp == 0) {
  362. printk(KERN_WARNING "windfarm: Overtemp condition cleared !\n");
  363. wf_notify(WF_EVENT_NORMALTEMP, NULL);
  364. }
  365. mutex_unlock(&wf_lock);
  366. }
  367. EXPORT_SYMBOL_GPL(wf_clear_overtemp);
  368. static int __init windfarm_core_init(void)
  369. {
  370. DBG("wf: core loaded\n");
  371. platform_device_register(&wf_platform_device);
  372. return 0;
  373. }
  374. static void __exit windfarm_core_exit(void)
  375. {
  376. BUG_ON(wf_client_count != 0);
  377. DBG("wf: core unloaded\n");
  378. platform_device_unregister(&wf_platform_device);
  379. }
  380. module_init(windfarm_core_init);
  381. module_exit(windfarm_core_exit);
  382. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  383. MODULE_DESCRIPTION("Core component of PowerMac thermal control");
  384. MODULE_LICENSE("GPL");