Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* -*- mode: c; c-basic-offset: 8 -*- */ |
| 2 | |
| 3 | /* Copyright (C) 2001 |
| 4 | * |
| 5 | * Author: J.E.J.Bottomley@HansenPartnership.com |
| 6 | * |
| 7 | * linux/arch/i386/kernel/voyager_thread.c |
| 8 | * |
| 9 | * This module provides the machine status monitor thread for the |
| 10 | * voyager architecture. This allows us to monitor the machine |
| 11 | * environment (temp, voltage, fan function) and the front panel and |
| 12 | * internal UPS. If a fault is detected, this thread takes corrective |
| 13 | * action (usually just informing init) |
| 14 | * */ |
| 15 | |
| 16 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/mm.h> |
| 18 | #include <linux/kernel_stat.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/mc146818rtc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/init.h> |
| 22 | #include <linux/bootmem.h> |
| 23 | #include <linux/kmod.h> |
| 24 | #include <linux/completion.h> |
| 25 | #include <linux/sched.h> |
Christoph Hellwig | f3402a4 | 2007-04-22 20:30:43 +0100 | [diff] [blame] | 26 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <asm/desc.h> |
| 28 | #include <asm/voyager.h> |
| 29 | #include <asm/vic.h> |
| 30 | #include <asm/mtrr.h> |
| 31 | #include <asm/msr.h> |
| 32 | |
Christoph Hellwig | f3402a4 | 2007-04-22 20:30:43 +0100 | [diff] [blame] | 33 | struct task_struct *voyager_thread; |
| 34 | static __u8 set_timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Ingo Molnar | a4ec1ef | 2008-01-30 13:30:10 +0100 | [diff] [blame] | 36 | static int execute(const char *string) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
| 38 | int ret; |
| 39 | |
| 40 | char *envp[] = { |
| 41 | "HOME=/", |
| 42 | "TERM=linux", |
| 43 | "PATH=/sbin:/usr/sbin:/bin:/usr/bin", |
| 44 | NULL, |
| 45 | }; |
| 46 | char *argv[] = { |
| 47 | "/bin/bash", |
| 48 | "-c", |
| 49 | (char *)string, |
| 50 | NULL, |
| 51 | }; |
| 52 | |
Ingo Molnar | a4ec1ef | 2008-01-30 13:30:10 +0100 | [diff] [blame] | 53 | if ((ret = |
| 54 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC)) != 0) { |
| 55 | printk(KERN_ERR "Voyager failed to run \"%s\": %i\n", string, |
| 56 | ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | return ret; |
| 59 | } |
| 60 | |
Ingo Molnar | a4ec1ef | 2008-01-30 13:30:10 +0100 | [diff] [blame] | 61 | static void check_from_kernel(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
Ingo Molnar | a4ec1ef | 2008-01-30 13:30:10 +0100 | [diff] [blame] | 63 | if (voyager_status.switch_off) { |
| 64 | |
Simon Arlott | 27b46d7 | 2007-10-20 01:13:56 +0200 | [diff] [blame] | 65 | /* FIXME: This should be configurable via proc */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | execute("umask 600; echo 0 > /etc/initrunlvl; kill -HUP 1"); |
Ingo Molnar | a4ec1ef | 2008-01-30 13:30:10 +0100 | [diff] [blame] | 67 | } else if (voyager_status.power_fail) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | VDEBUG(("Voyager daemon detected AC power failure\n")); |
Ingo Molnar | a4ec1ef | 2008-01-30 13:30:10 +0100 | [diff] [blame] | 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | /* FIXME: This should be configureable via proc */ |
| 71 | execute("umask 600; echo F > /etc/powerstatus; kill -PWR 1"); |
| 72 | set_timeout = 1; |
| 73 | } |
| 74 | } |
| 75 | |
Ingo Molnar | a4ec1ef | 2008-01-30 13:30:10 +0100 | [diff] [blame] | 76 | static void check_continuing_condition(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
Ingo Molnar | a4ec1ef | 2008-01-30 13:30:10 +0100 | [diff] [blame] | 78 | if (voyager_status.power_fail) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | __u8 data; |
Ingo Molnar | a4ec1ef | 2008-01-30 13:30:10 +0100 | [diff] [blame] | 80 | voyager_cat_psi(VOYAGER_PSI_SUBREAD, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | VOYAGER_PSI_AC_FAIL_REG, &data); |
Ingo Molnar | a4ec1ef | 2008-01-30 13:30:10 +0100 | [diff] [blame] | 82 | if ((data & 0x1f) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /* all power restored */ |
Ingo Molnar | a4ec1ef | 2008-01-30 13:30:10 +0100 | [diff] [blame] | 84 | printk(KERN_NOTICE |
| 85 | "VOYAGER AC power restored, cancelling shutdown\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | /* FIXME: should be user configureable */ |
Ingo Molnar | a4ec1ef | 2008-01-30 13:30:10 +0100 | [diff] [blame] | 87 | execute |
| 88 | ("umask 600; echo O > /etc/powerstatus; kill -PWR 1"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | set_timeout = 0; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
Ingo Molnar | a4ec1ef | 2008-01-30 13:30:10 +0100 | [diff] [blame] | 94 | static int thread(void *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | printk(KERN_NOTICE "Voyager starting monitor thread\n"); |
| 97 | |
Christoph Hellwig | f3402a4 | 2007-04-22 20:30:43 +0100 | [diff] [blame] | 98 | for (;;) { |
| 99 | set_current_state(TASK_INTERRUPTIBLE); |
| 100 | schedule_timeout(set_timeout ? HZ : MAX_SCHEDULE_TIMEOUT); |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | VDEBUG(("Voyager Daemon awoken\n")); |
Ingo Molnar | a4ec1ef | 2008-01-30 13:30:10 +0100 | [diff] [blame] | 103 | if (voyager_status.request_from_kernel == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | /* probably awoken from timeout */ |
| 105 | check_continuing_condition(); |
| 106 | } else { |
| 107 | check_from_kernel(); |
| 108 | voyager_status.request_from_kernel = 0; |
| 109 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
Ingo Molnar | a4ec1ef | 2008-01-30 13:30:10 +0100 | [diff] [blame] | 113 | static int __init voyager_thread_start(void) |
Christoph Hellwig | f3402a4 | 2007-04-22 20:30:43 +0100 | [diff] [blame] | 114 | { |
| 115 | voyager_thread = kthread_run(thread, NULL, "kvoyagerd"); |
| 116 | if (IS_ERR(voyager_thread)) { |
Ingo Molnar | a4ec1ef | 2008-01-30 13:30:10 +0100 | [diff] [blame] | 117 | printk(KERN_ERR |
| 118 | "Voyager: Failed to create system monitor thread.\n"); |
Christoph Hellwig | f3402a4 | 2007-04-22 20:30:43 +0100 | [diff] [blame] | 119 | return PTR_ERR(voyager_thread); |
| 120 | } |
| 121 | return 0; |
| 122 | } |
| 123 | |
Ingo Molnar | a4ec1ef | 2008-01-30 13:30:10 +0100 | [diff] [blame] | 124 | static void __exit voyager_thread_stop(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
Christoph Hellwig | f3402a4 | 2007-04-22 20:30:43 +0100 | [diff] [blame] | 126 | kthread_stop(voyager_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | module_init(voyager_thread_start); |
Christoph Hellwig | f3402a4 | 2007-04-22 20:30:43 +0100 | [diff] [blame] | 130 | module_exit(voyager_thread_stop); |