KVM: VMX: Fix reporting of unhandled EPT violations
[linux-2.6/btrfs-unstable.git] / kernel / power / main.c
blobf710e36930cc33ecceac7f5455de6d86fedbd718
1 /*
2 * kernel/power/main.c - PM subsystem core functionality.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 *
7 * This file is released under the GPLv2
9 */
11 #include <linux/kobject.h>
12 #include <linux/string.h>
13 #include <linux/resume-trace.h>
15 #include "power.h"
17 DEFINE_MUTEX(pm_mutex);
19 unsigned int pm_flags;
20 EXPORT_SYMBOL(pm_flags);
22 #ifdef CONFIG_PM_SLEEP
24 /* Routines for PM-transition notifications */
26 static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
28 int register_pm_notifier(struct notifier_block *nb)
30 return blocking_notifier_chain_register(&pm_chain_head, nb);
32 EXPORT_SYMBOL_GPL(register_pm_notifier);
34 int unregister_pm_notifier(struct notifier_block *nb)
36 return blocking_notifier_chain_unregister(&pm_chain_head, nb);
38 EXPORT_SYMBOL_GPL(unregister_pm_notifier);
40 int pm_notifier_call_chain(unsigned long val)
42 return (blocking_notifier_call_chain(&pm_chain_head, val, NULL)
43 == NOTIFY_BAD) ? -EINVAL : 0;
46 #ifdef CONFIG_PM_DEBUG
47 int pm_test_level = TEST_NONE;
49 static const char * const pm_tests[__TEST_AFTER_LAST] = {
50 [TEST_NONE] = "none",
51 [TEST_CORE] = "core",
52 [TEST_CPUS] = "processors",
53 [TEST_PLATFORM] = "platform",
54 [TEST_DEVICES] = "devices",
55 [TEST_FREEZER] = "freezer",
58 static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
59 char *buf)
61 char *s = buf;
62 int level;
64 for (level = TEST_FIRST; level <= TEST_MAX; level++)
65 if (pm_tests[level]) {
66 if (level == pm_test_level)
67 s += sprintf(s, "[%s] ", pm_tests[level]);
68 else
69 s += sprintf(s, "%s ", pm_tests[level]);
72 if (s != buf)
73 /* convert the last space to a newline */
74 *(s-1) = '\n';
76 return (s - buf);
79 static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
80 const char *buf, size_t n)
82 const char * const *s;
83 int level;
84 char *p;
85 int len;
86 int error = -EINVAL;
88 p = memchr(buf, '\n', n);
89 len = p ? p - buf : n;
91 mutex_lock(&pm_mutex);
93 level = TEST_FIRST;
94 for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
95 if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
96 pm_test_level = level;
97 error = 0;
98 break;
101 mutex_unlock(&pm_mutex);
103 return error ? error : n;
106 power_attr(pm_test);
107 #endif /* CONFIG_PM_DEBUG */
109 #endif /* CONFIG_PM_SLEEP */
111 struct kobject *power_kobj;
114 * state - control system power state.
116 * show() returns what states are supported, which is hard-coded to
117 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
118 * 'disk' (Suspend-to-Disk).
120 * store() accepts one of those strings, translates it into the
121 * proper enumerated value, and initiates a suspend transition.
123 static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
124 char *buf)
126 char *s = buf;
127 #ifdef CONFIG_SUSPEND
128 int i;
130 for (i = 0; i < PM_SUSPEND_MAX; i++) {
131 if (pm_states[i] && valid_state(i))
132 s += sprintf(s,"%s ", pm_states[i]);
134 #endif
135 #ifdef CONFIG_HIBERNATION
136 s += sprintf(s, "%s\n", "disk");
137 #else
138 if (s != buf)
139 /* convert the last space to a newline */
140 *(s-1) = '\n';
141 #endif
142 return (s - buf);
145 static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
146 const char *buf, size_t n)
148 #ifdef CONFIG_SUSPEND
149 suspend_state_t state = PM_SUSPEND_STANDBY;
150 const char * const *s;
151 #endif
152 char *p;
153 int len;
154 int error = -EINVAL;
156 p = memchr(buf, '\n', n);
157 len = p ? p - buf : n;
159 /* First, check if we are requested to hibernate */
160 if (len == 4 && !strncmp(buf, "disk", len)) {
161 error = hibernate();
162 goto Exit;
165 #ifdef CONFIG_SUSPEND
166 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
167 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
168 break;
170 if (state < PM_SUSPEND_MAX && *s)
171 error = enter_state(state);
172 #endif
174 Exit:
175 return error ? error : n;
178 power_attr(state);
180 #ifdef CONFIG_PM_TRACE
181 int pm_trace_enabled;
183 static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
184 char *buf)
186 return sprintf(buf, "%d\n", pm_trace_enabled);
189 static ssize_t
190 pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
191 const char *buf, size_t n)
193 int val;
195 if (sscanf(buf, "%d", &val) == 1) {
196 pm_trace_enabled = !!val;
197 return n;
199 return -EINVAL;
202 power_attr(pm_trace);
203 #endif /* CONFIG_PM_TRACE */
205 static struct attribute * g[] = {
206 &state_attr.attr,
207 #ifdef CONFIG_PM_TRACE
208 &pm_trace_attr.attr,
209 #endif
210 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PM_DEBUG)
211 &pm_test_attr.attr,
212 #endif
213 NULL,
216 static struct attribute_group attr_group = {
217 .attrs = g,
220 static int __init pm_init(void)
222 power_kobj = kobject_create_and_add("power", NULL);
223 if (!power_kobj)
224 return -ENOMEM;
225 return sysfs_create_group(power_kobj, &attr_group);
228 core_initcall(pm_init);