blob: c69c931818ed49b8d5d9326a50aefb8aa339ba9e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- 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 Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mm.h>
18#include <linux/kernel_stat.h>
19#include <linux/delay.h>
20#include <linux/mc146818rtc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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 Hellwigf3402a42007-04-22 20:30:43 +010026#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#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 Hellwigf3402a42007-04-22 20:30:43 +010033struct task_struct *voyager_thread;
34static __u8 set_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010036static int execute(const char *string)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
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 Molnara4ec1ef2008-01-30 13:30:10 +010053 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 Torvalds1da177e2005-04-16 15:20:36 -070057 }
58 return ret;
59}
60
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010061static void check_from_kernel(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010063 if (voyager_status.switch_off) {
64
Simon Arlott27b46d72007-10-20 01:13:56 +020065 /* FIXME: This should be configurable via proc */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 execute("umask 600; echo 0 > /etc/initrunlvl; kill -HUP 1");
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010067 } else if (voyager_status.power_fail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 VDEBUG(("Voyager daemon detected AC power failure\n"));
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 /* 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 Molnara4ec1ef2008-01-30 13:30:10 +010076static void check_continuing_condition(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010078 if (voyager_status.power_fail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 __u8 data;
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010080 voyager_cat_psi(VOYAGER_PSI_SUBREAD,
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 VOYAGER_PSI_AC_FAIL_REG, &data);
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010082 if ((data & 0x1f) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 /* all power restored */
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010084 printk(KERN_NOTICE
85 "VOYAGER AC power restored, cancelling shutdown\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 /* FIXME: should be user configureable */
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010087 execute
88 ("umask 600; echo O > /etc/powerstatus; kill -PWR 1");
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 set_timeout = 0;
90 }
91 }
92}
93
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010094static int thread(void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 printk(KERN_NOTICE "Voyager starting monitor thread\n");
97
Christoph Hellwigf3402a42007-04-22 20:30:43 +010098 for (;;) {
99 set_current_state(TASK_INTERRUPTIBLE);
100 schedule_timeout(set_timeout ? HZ : MAX_SCHEDULE_TIMEOUT);
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 VDEBUG(("Voyager Daemon awoken\n"));
Ingo Molnara4ec1ef2008-01-30 13:30:10 +0100103 if (voyager_status.request_from_kernel == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 /* probably awoken from timeout */
105 check_continuing_condition();
106 } else {
107 check_from_kernel();
108 voyager_status.request_from_kernel = 0;
109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111}
112
Ingo Molnara4ec1ef2008-01-30 13:30:10 +0100113static int __init voyager_thread_start(void)
Christoph Hellwigf3402a42007-04-22 20:30:43 +0100114{
115 voyager_thread = kthread_run(thread, NULL, "kvoyagerd");
116 if (IS_ERR(voyager_thread)) {
Ingo Molnara4ec1ef2008-01-30 13:30:10 +0100117 printk(KERN_ERR
118 "Voyager: Failed to create system monitor thread.\n");
Christoph Hellwigf3402a42007-04-22 20:30:43 +0100119 return PTR_ERR(voyager_thread);
120 }
121 return 0;
122}
123
Ingo Molnara4ec1ef2008-01-30 13:30:10 +0100124static void __exit voyager_thread_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Christoph Hellwigf3402a42007-04-22 20:30:43 +0100126 kthread_stop(voyager_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129module_init(voyager_thread_start);
Christoph Hellwigf3402a42007-04-22 20:30:43 +0100130module_exit(voyager_thread_stop);