Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * kernel/power/main.c - PM subsystem core functionality. |
| 3 | * |
| 4 | * Copyright (c) 2003 Patrick Mochel |
| 5 | * Copyright (c) 2003 Open Source Development Lab |
| 6 | * |
| 7 | * This file is released under the GPLv2 |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include <linux/suspend.h> |
| 12 | #include <linux/kobject.h> |
| 13 | #include <linux/string.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/pm.h> |
Andrew Morton | 6cc0719 | 2006-06-22 14:47:18 -0700 | [diff] [blame] | 18 | #include <linux/console.h> |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 19 | #include <linux/cpu.h> |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 20 | #include <linux/resume-trace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #include "power.h" |
| 23 | |
David Shaohua Li | 5ae947e | 2005-03-18 16:27:13 -0500 | [diff] [blame] | 24 | /*This is just an arbitrary number */ |
| 25 | #define FREE_PAGE_NUMBER (100) |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | DECLARE_MUTEX(pm_sem); |
| 28 | |
Pavel Machek | 123d3c1 | 2005-11-29 19:34:37 -0800 | [diff] [blame] | 29 | struct pm_ops *pm_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | suspend_disk_method_t pm_disk_mode = PM_DISK_SHUTDOWN; |
| 31 | |
| 32 | /** |
| 33 | * pm_set_ops - Set the global power method table. |
| 34 | * @ops: Pointer to ops structure. |
| 35 | */ |
| 36 | |
| 37 | void pm_set_ops(struct pm_ops * ops) |
| 38 | { |
| 39 | down(&pm_sem); |
| 40 | pm_ops = ops; |
| 41 | up(&pm_sem); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * suspend_prepare - Do prep work before entering low-power state. |
| 47 | * @state: State we're entering. |
| 48 | * |
| 49 | * This is common code that is called for each state that we're |
| 50 | * entering. Allocate a console, stop all processes, then make sure |
| 51 | * the platform can enter the requested state. |
| 52 | */ |
| 53 | |
| 54 | static int suspend_prepare(suspend_state_t state) |
| 55 | { |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 56 | int error; |
David Shaohua Li | 5ae947e | 2005-03-18 16:27:13 -0500 | [diff] [blame] | 57 | unsigned int free_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | if (!pm_ops || !pm_ops->enter) |
| 60 | return -EPERM; |
| 61 | |
| 62 | pm_prepare_console(); |
| 63 | |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 64 | error = disable_nonboot_cpus(); |
| 65 | if (error) |
Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 66 | goto Enable_cpu; |
Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | if (freeze_processes()) { |
| 69 | error = -EAGAIN; |
| 70 | goto Thaw; |
| 71 | } |
| 72 | |
David Shaohua Li | 5ae947e | 2005-03-18 16:27:13 -0500 | [diff] [blame] | 73 | if ((free_pages = nr_free_pages()) < FREE_PAGE_NUMBER) { |
| 74 | pr_debug("PM: free some memory\n"); |
| 75 | shrink_all_memory(FREE_PAGE_NUMBER - free_pages); |
| 76 | if (nr_free_pages() < FREE_PAGE_NUMBER) { |
| 77 | error = -ENOMEM; |
| 78 | printk(KERN_ERR "PM: No enough memory\n"); |
| 79 | goto Thaw; |
| 80 | } |
| 81 | } |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | if (pm_ops->prepare) { |
| 84 | if ((error = pm_ops->prepare(state))) |
| 85 | goto Thaw; |
| 86 | } |
| 87 | |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 88 | suspend_console(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | if ((error = device_suspend(PMSG_SUSPEND))) { |
| 90 | printk(KERN_ERR "Some devices failed to suspend\n"); |
| 91 | goto Finish; |
| 92 | } |
| 93 | return 0; |
| 94 | Finish: |
| 95 | if (pm_ops->finish) |
| 96 | pm_ops->finish(state); |
| 97 | Thaw: |
| 98 | thaw_processes(); |
Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 99 | Enable_cpu: |
| 100 | enable_nonboot_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | pm_restore_console(); |
| 102 | return error; |
| 103 | } |
| 104 | |
| 105 | |
Luca Tettamanti | 9b23820 | 2006-03-23 03:00:09 -0800 | [diff] [blame] | 106 | int suspend_enter(suspend_state_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
| 108 | int error = 0; |
| 109 | unsigned long flags; |
| 110 | |
| 111 | local_irq_save(flags); |
| 112 | |
| 113 | if ((error = device_power_down(PMSG_SUSPEND))) { |
| 114 | printk(KERN_ERR "Some devices failed to power down\n"); |
| 115 | goto Done; |
| 116 | } |
| 117 | error = pm_ops->enter(state); |
| 118 | device_power_up(); |
| 119 | Done: |
| 120 | local_irq_restore(flags); |
| 121 | return error; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * suspend_finish - Do final work before exiting suspend sequence. |
| 127 | * @state: State we're coming out of. |
| 128 | * |
| 129 | * Call platform code to clean up, restart processes, and free the |
| 130 | * console that we've allocated. This is not called for suspend-to-disk. |
| 131 | */ |
| 132 | |
| 133 | static void suspend_finish(suspend_state_t state) |
| 134 | { |
| 135 | device_resume(); |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 136 | resume_console(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | thaw_processes(); |
Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 138 | enable_nonboot_cpus(); |
David Shaohua Li | 1a38416 | 2005-11-23 12:36:00 -0500 | [diff] [blame] | 139 | if (pm_ops && pm_ops->finish) |
| 140 | pm_ops->finish(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | pm_restore_console(); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
Andreas Mohr | 3b364b8 | 2006-06-25 05:47:56 -0700 | [diff] [blame] | 147 | static const char * const pm_states[PM_SUSPEND_MAX] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | [PM_SUSPEND_STANDBY] = "standby", |
| 149 | [PM_SUSPEND_MEM] = "mem", |
Pavel Machek | 57c4ce3 | 2005-09-03 15:57:06 -0700 | [diff] [blame] | 150 | #ifdef CONFIG_SOFTWARE_SUSPEND |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | [PM_SUSPEND_DISK] = "disk", |
Pavel Machek | 57c4ce3 | 2005-09-03 15:57:06 -0700 | [diff] [blame] | 152 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
Pavel Machek | 123d3c1 | 2005-11-29 19:34:37 -0800 | [diff] [blame] | 155 | static inline int valid_state(suspend_state_t state) |
| 156 | { |
| 157 | /* Suspend-to-disk does not really need low-level support. |
| 158 | * It can work with reboot if needed. */ |
| 159 | if (state == PM_SUSPEND_DISK) |
| 160 | return 1; |
| 161 | |
| 162 | if (pm_ops && pm_ops->valid && !pm_ops->valid(state)) |
| 163 | return 0; |
| 164 | return 1; |
| 165 | } |
| 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | /** |
| 169 | * enter_state - Do common work of entering low-power state. |
| 170 | * @state: pm_state structure for state we're entering. |
| 171 | * |
| 172 | * Make sure we're the only ones trying to enter a sleep state. Fail |
| 173 | * if someone has beat us to it, since we don't want anything weird to |
| 174 | * happen when we wake up. |
| 175 | * Then, do the setup for suspend, enter the state, and cleaup (after |
| 176 | * we've woken up). |
| 177 | */ |
| 178 | |
| 179 | static int enter_state(suspend_state_t state) |
| 180 | { |
| 181 | int error; |
| 182 | |
Pavel Machek | 123d3c1 | 2005-11-29 19:34:37 -0800 | [diff] [blame] | 183 | if (!valid_state(state)) |
Shaohua Li | eb9289e | 2005-10-30 15:00:01 -0800 | [diff] [blame] | 184 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | if (down_trylock(&pm_sem)) |
| 186 | return -EBUSY; |
| 187 | |
| 188 | if (state == PM_SUSPEND_DISK) { |
| 189 | error = pm_suspend_disk(); |
| 190 | goto Unlock; |
| 191 | } |
| 192 | |
David Brownell | 82428b6 | 2005-05-09 08:07:00 -0700 | [diff] [blame] | 193 | pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | if ((error = suspend_prepare(state))) |
| 195 | goto Unlock; |
| 196 | |
David Brownell | 82428b6 | 2005-05-09 08:07:00 -0700 | [diff] [blame] | 197 | pr_debug("PM: Entering %s sleep\n", pm_states[state]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | error = suspend_enter(state); |
| 199 | |
David Brownell | 82428b6 | 2005-05-09 08:07:00 -0700 | [diff] [blame] | 200 | pr_debug("PM: Finishing wakeup.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | suspend_finish(state); |
| 202 | Unlock: |
| 203 | up(&pm_sem); |
| 204 | return error; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * This is main interface to the outside world. It needs to be |
| 209 | * called from process context. |
| 210 | */ |
| 211 | int software_suspend(void) |
| 212 | { |
| 213 | return enter_state(PM_SUSPEND_DISK); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | /** |
| 218 | * pm_suspend - Externally visible function for suspending system. |
| 219 | * @state: Enumarted value of state to enter. |
| 220 | * |
| 221 | * Determine whether or not value is within range, get state |
| 222 | * structure, and enter (above). |
| 223 | */ |
| 224 | |
| 225 | int pm_suspend(suspend_state_t state) |
| 226 | { |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 227 | if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | return enter_state(state); |
| 229 | return -EINVAL; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | |
| 234 | decl_subsys(power,NULL,NULL); |
| 235 | |
| 236 | |
| 237 | /** |
| 238 | * state - control system power state. |
| 239 | * |
| 240 | * show() returns what states are supported, which is hard-coded to |
| 241 | * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and |
| 242 | * 'disk' (Suspend-to-Disk). |
| 243 | * |
| 244 | * store() accepts one of those strings, translates it into the |
| 245 | * proper enumerated value, and initiates a suspend transition. |
| 246 | */ |
| 247 | |
| 248 | static ssize_t state_show(struct subsystem * subsys, char * buf) |
| 249 | { |
| 250 | int i; |
| 251 | char * s = buf; |
| 252 | |
| 253 | for (i = 0; i < PM_SUSPEND_MAX; i++) { |
Pavel Machek | 123d3c1 | 2005-11-29 19:34:37 -0800 | [diff] [blame] | 254 | if (pm_states[i] && valid_state(i)) |
| 255 | s += sprintf(s,"%s ", pm_states[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | s += sprintf(s,"\n"); |
| 258 | return (s - buf); |
| 259 | } |
| 260 | |
| 261 | static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n) |
| 262 | { |
| 263 | suspend_state_t state = PM_SUSPEND_STANDBY; |
Andreas Mohr | 3b364b8 | 2006-06-25 05:47:56 -0700 | [diff] [blame] | 264 | const char * const *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | char *p; |
| 266 | int error; |
| 267 | int len; |
| 268 | |
| 269 | p = memchr(buf, '\n', n); |
| 270 | len = p ? p - buf : n; |
| 271 | |
| 272 | for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) { |
| 273 | if (*s && !strncmp(buf, *s, len)) |
| 274 | break; |
| 275 | } |
dean gaudet | 47bb789 | 2006-04-27 18:39:17 -0700 | [diff] [blame] | 276 | if (state < PM_SUSPEND_MAX && *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | error = enter_state(state); |
| 278 | else |
| 279 | error = -EINVAL; |
| 280 | return error ? error : n; |
| 281 | } |
| 282 | |
| 283 | power_attr(state); |
| 284 | |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 285 | #ifdef CONFIG_PM_TRACE |
| 286 | int pm_trace_enabled; |
| 287 | |
| 288 | static ssize_t pm_trace_show(struct subsystem * subsys, char * buf) |
| 289 | { |
| 290 | return sprintf(buf, "%d\n", pm_trace_enabled); |
| 291 | } |
| 292 | |
| 293 | static ssize_t |
| 294 | pm_trace_store(struct subsystem * subsys, const char * buf, size_t n) |
| 295 | { |
| 296 | int val; |
| 297 | |
| 298 | if (sscanf(buf, "%d", &val) == 1) { |
| 299 | pm_trace_enabled = !!val; |
| 300 | return n; |
| 301 | } |
| 302 | return -EINVAL; |
| 303 | } |
| 304 | |
| 305 | power_attr(pm_trace); |
| 306 | |
| 307 | static struct attribute * g[] = { |
| 308 | &state_attr.attr, |
| 309 | &pm_trace_attr.attr, |
| 310 | NULL, |
| 311 | }; |
| 312 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | static struct attribute * g[] = { |
| 314 | &state_attr.attr, |
| 315 | NULL, |
| 316 | }; |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 317 | #endif /* CONFIG_PM_TRACE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
| 319 | static struct attribute_group attr_group = { |
| 320 | .attrs = g, |
| 321 | }; |
| 322 | |
| 323 | |
| 324 | static int __init pm_init(void) |
| 325 | { |
| 326 | int error = subsystem_register(&power_subsys); |
| 327 | if (!error) |
| 328 | error = sysfs_create_group(&power_subsys.kset.kobj,&attr_group); |
| 329 | return error; |
| 330 | } |
| 331 | |
| 332 | core_initcall(pm_init); |