power.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * HP PARISC soft power switch driver
  4. *
  5. * Copyright (c) 2001-2023 Helge Deller <deller@gmx.de>
  6. *
  7. * HINT:
  8. * Support of the soft power switch button may be enabled or disabled at
  9. * runtime through the "/proc/sys/kernel/power" procfs entry.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/panic_notifier.h>
  15. #include <linux/reboot.h>
  16. #include <linux/sched/signal.h>
  17. #include <linux/kthread.h>
  18. #include <linux/pm.h>
  19. #include <asm/pdc.h>
  20. #include <asm/io.h>
  21. #include <asm/led.h>
  22. #define DRIVER_NAME "powersw"
  23. #define KTHREAD_NAME "kpowerswd"
  24. /* how often should the power button be polled ? */
  25. #define POWERSWITCH_POLL_PER_SEC 2
  26. /* how long does the power button needs to be down until we react ? */
  27. #define POWERSWITCH_DOWN_SEC 2
  28. /* assembly code to access special registers */
  29. /* taken from PCXL ERS page 82 */
  30. #define DIAG_CODE(code) (0x14000000 + ((code)<<5))
  31. #define MFCPU_X(rDiagReg, t_ch, t_th, code) \
  32. (DIAG_CODE(code) + ((rDiagReg)<<21) + ((t_ch)<<16) + ((t_th)<<0) )
  33. #define MTCPU(dr, gr) MFCPU_X(dr, gr, 0, 0x12) /* move value of gr to dr[dr] */
  34. #define MFCPU_C(dr, gr) MFCPU_X(dr, gr, 0, 0x30) /* for dr0 and dr8 only ! */
  35. #define MFCPU_T(dr, gr) MFCPU_X(dr, 0, gr, 0xa0) /* all dr except dr0 and dr8 */
  36. #define __getDIAG(dr) ( { \
  37. register unsigned long __res asm("r28");\
  38. __asm__ __volatile__ ( \
  39. ".word %1" : "=&r" (__res) : "i" (MFCPU_T(dr,28) ) \
  40. ); \
  41. __res; \
  42. } )
  43. /* local shutdown counter */
  44. static int shutdown_timer __read_mostly;
  45. /* check, give feedback and start shutdown after one second */
  46. static void process_shutdown(void)
  47. {
  48. if (shutdown_timer == 0)
  49. printk(KERN_ALERT KTHREAD_NAME ": Shutdown requested...\n");
  50. shutdown_timer++;
  51. /* wait until the button was pressed for 1 second */
  52. if (shutdown_timer == (POWERSWITCH_DOWN_SEC*POWERSWITCH_POLL_PER_SEC)) {
  53. static const char msg[] = "Shutting down...";
  54. printk(KERN_INFO KTHREAD_NAME ": %s\n", msg);
  55. lcd_print(msg);
  56. /* send kill signal */
  57. if (kill_cad_pid(SIGINT, 1)) {
  58. /* just in case killing init process failed */
  59. machine_power_off();
  60. }
  61. }
  62. }
  63. /* main power switch task struct */
  64. static struct task_struct *power_task;
  65. /* filename in /proc which can be used to enable/disable the power switch */
  66. #define SYSCTL_FILENAME "sys/kernel/power"
  67. /* soft power switch enabled/disabled */
  68. static int pwrsw_enabled __read_mostly = 1;
  69. static const struct ctl_table power_sysctl_table[] = {
  70. {
  71. .procname = "soft-power",
  72. .data = &pwrsw_enabled,
  73. .maxlen = sizeof(int),
  74. .mode = 0644,
  75. .proc_handler = proc_dointvec,
  76. },
  77. };
  78. static int __init init_power_sysctl(void)
  79. {
  80. register_sysctl_init("kernel", power_sysctl_table);
  81. return 0;
  82. }
  83. arch_initcall(init_power_sysctl);
  84. /* main kernel thread worker. It polls the button state */
  85. static int kpowerswd(void *param)
  86. {
  87. __set_current_state(TASK_RUNNING);
  88. do {
  89. int button_not_pressed;
  90. unsigned long soft_power_reg = (unsigned long) param;
  91. schedule_timeout_interruptible(pwrsw_enabled ? HZ : HZ/POWERSWITCH_POLL_PER_SEC);
  92. if (unlikely(!pwrsw_enabled))
  93. continue;
  94. if (soft_power_reg) {
  95. /*
  96. * Non-Gecko-style machines:
  97. * Check the power switch status which is read from the
  98. * real I/O location at soft_power_reg.
  99. * Bit 31 ("the lowest bit) is the status of the power switch.
  100. * This bit is "1" if the button is NOT pressed.
  101. */
  102. button_not_pressed = (gsc_readl(soft_power_reg) & 0x1);
  103. } else {
  104. /*
  105. * On gecko style machines (e.g. 712/xx and 715/xx)
  106. * the power switch status is stored in Bit 0 ("the highest bit")
  107. * of CPU diagnose register 25.
  108. * Warning: Some machines never reset the DIAG flag, even if
  109. * the button has been released again.
  110. */
  111. button_not_pressed = (__getDIAG(25) & 0x80000000);
  112. }
  113. if (likely(button_not_pressed)) {
  114. if (unlikely(shutdown_timer && /* avoid writing if not necessary */
  115. shutdown_timer < (POWERSWITCH_DOWN_SEC*POWERSWITCH_POLL_PER_SEC))) {
  116. shutdown_timer = 0;
  117. printk(KERN_INFO KTHREAD_NAME ": Shutdown request aborted.\n");
  118. }
  119. } else
  120. process_shutdown();
  121. } while (!kthread_should_stop());
  122. return 0;
  123. }
  124. /*
  125. * powerfail interruption handler (irq IRQ_FROM_REGION(CPU_IRQ_REGION)+2)
  126. */
  127. #if 0
  128. static void powerfail_interrupt(int code, void *x)
  129. {
  130. printk(KERN_CRIT "POWERFAIL INTERRUPTION !\n");
  131. poweroff();
  132. }
  133. #endif
  134. /*
  135. * parisc_panic_event() is called by the panic handler.
  136. *
  137. * As soon as a panic occurs, our tasklets above will not
  138. * be executed any longer. This function then re-enables
  139. * the soft-power switch and allows the user to switch off
  140. * the system. We rely in pdc_soft_power_button_panic()
  141. * since this version spin_trylocks (instead of regular
  142. * spinlock), preventing deadlocks on panic path.
  143. */
  144. static int parisc_panic_event(struct notifier_block *this,
  145. unsigned long event, void *ptr)
  146. {
  147. /* re-enable the soft-power switch */
  148. pdc_soft_power_button_panic(0);
  149. return NOTIFY_DONE;
  150. }
  151. static struct notifier_block parisc_panic_block = {
  152. .notifier_call = parisc_panic_event,
  153. .priority = INT_MAX,
  154. };
  155. /* qemu soft power-off function */
  156. static int qemu_power_off(struct sys_off_data *data)
  157. {
  158. /* this turns the system off via SeaBIOS */
  159. gsc_writel(0, (unsigned long) data->cb_data);
  160. pdc_soft_power_button(1);
  161. return NOTIFY_DONE;
  162. }
  163. static int __init power_init(void)
  164. {
  165. unsigned long ret;
  166. unsigned long soft_power_reg;
  167. #if 0
  168. request_irq( IRQ_FROM_REGION(CPU_IRQ_REGION)+2, &powerfail_interrupt,
  169. 0, "powerfail", NULL);
  170. #endif
  171. /* enable the soft power switch if possible */
  172. ret = pdc_soft_power_info(&soft_power_reg);
  173. if (ret == PDC_OK)
  174. ret = pdc_soft_power_button(1);
  175. if (ret != PDC_OK)
  176. soft_power_reg = -1UL;
  177. switch (soft_power_reg) {
  178. case 0: printk(KERN_INFO DRIVER_NAME ": Gecko-style soft power switch enabled.\n");
  179. break;
  180. case -1UL: printk(KERN_INFO DRIVER_NAME ": Soft power switch support not available.\n");
  181. return -ENODEV;
  182. default: printk(KERN_INFO DRIVER_NAME ": Soft power switch at 0x%08lx enabled.\n",
  183. soft_power_reg);
  184. }
  185. power_task = NULL;
  186. if (running_on_qemu && soft_power_reg)
  187. register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, SYS_OFF_PRIO_DEFAULT,
  188. qemu_power_off, (void *)soft_power_reg);
  189. if (!running_on_qemu || soft_power_reg)
  190. power_task = kthread_run(kpowerswd, (void*)soft_power_reg,
  191. KTHREAD_NAME);
  192. if (IS_ERR(power_task)) {
  193. printk(KERN_ERR DRIVER_NAME ": thread creation failed. Driver not loaded.\n");
  194. pdc_soft_power_button(0);
  195. return -EIO;
  196. }
  197. /* Register a call for panic conditions. */
  198. atomic_notifier_chain_register(&panic_notifier_list,
  199. &parisc_panic_block);
  200. return 0;
  201. }
  202. static void __exit power_exit(void)
  203. {
  204. kthread_stop(power_task);
  205. atomic_notifier_chain_unregister(&panic_notifier_list,
  206. &parisc_panic_block);
  207. pdc_soft_power_button(0);
  208. }
  209. arch_initcall(power_init);
  210. module_exit(power_exit);
  211. MODULE_AUTHOR("Helge Deller <deller@gmx.de>");
  212. MODULE_DESCRIPTION("Soft power switch driver");
  213. MODULE_LICENSE("Dual BSD/GPL");