Merge branches 'regmap-core', 'regmap-mmio' and 'regmap-naming' into regmap-stride
[deliverable/linux.git] / drivers / acpi / sleep.c
CommitLineData
1da177e4
LT
1/*
2 * sleep.c - ACPI sleep support.
3 *
e2a5b420 4 * Copyright (c) 2005 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
1da177e4
LT
5 * Copyright (c) 2004 David Shaohua Li <shaohua.li@intel.com>
6 * Copyright (c) 2000-2003 Patrick Mochel
7 * Copyright (c) 2003 Open Source Development Lab
8 *
9 * This file is released under the GPLv2.
10 *
11 */
12
13#include <linux/delay.h>
14#include <linux/irq.h>
15#include <linux/dmi.h>
16#include <linux/device.h>
17#include <linux/suspend.h>
e49f711c 18#include <linux/reboot.h>
d1ee4335 19#include <linux/acpi.h>
a2ef5c4f 20#include <linux/module.h>
b24e5098 21#include <linux/pm_runtime.h>
f216cc37
AS
22
23#include <asm/io.h>
24
1da177e4
LT
25#include <acpi/acpi_bus.h>
26#include <acpi/acpi_drivers.h>
e60cc7a6
BH
27
28#include "internal.h"
1da177e4
LT
29#include "sleep.h"
30
a2ef5c4f
LM
31static unsigned int gts, bfs;
32module_param(gts, uint, 0644);
33module_param(bfs, uint, 0644);
34MODULE_PARM_DESC(gts, "Enable evaluation of _GTS on suspend.");
35MODULE_PARM_DESC(bfs, "Enable evaluation of _BFS on resume".);
36
37static u8 wake_sleep_flags(void)
38{
39 u8 flags = ACPI_NO_OPTIONAL_METHODS;
40
41 if (gts)
42 flags |= ACPI_EXECUTE_GTS;
43 if (bfs)
44 flags |= ACPI_EXECUTE_BFS;
45
46 return flags;
47}
48
01eac60b 49static u8 sleep_states[ACPI_S_STATE_COUNT];
1da177e4 50
e49f711c
ZY
51static void acpi_sleep_tts_switch(u32 acpi_state)
52{
53 union acpi_object in_arg = { ACPI_TYPE_INTEGER };
54 struct acpi_object_list arg_list = { 1, &in_arg };
55 acpi_status status = AE_OK;
56
57 in_arg.integer.value = acpi_state;
58 status = acpi_evaluate_object(NULL, "\\_TTS", &arg_list, NULL);
59 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
60 /*
61 * OS can't evaluate the _TTS object correctly. Some warning
62 * message will be printed. But it won't break anything.
63 */
64 printk(KERN_NOTICE "Failure in evaluating _TTS object\n");
65 }
66}
67
68static int tts_notify_reboot(struct notifier_block *this,
69 unsigned long code, void *x)
70{
71 acpi_sleep_tts_switch(ACPI_STATE_S5);
72 return NOTIFY_DONE;
73}
74
75static struct notifier_block tts_notifier = {
76 .notifier_call = tts_notify_reboot,
77 .next = NULL,
78 .priority = 0,
79};
80
c9b6c8f6 81static int acpi_sleep_prepare(u32 acpi_state)
2f3f2226
AS
82{
83#ifdef CONFIG_ACPI_SLEEP
84 /* do we have a wakeup address for S2 and S3? */
85 if (acpi_state == ACPI_STATE_S3) {
86 if (!acpi_wakeup_address) {
87 return -EFAULT;
88 }
4b4f7280
PA
89 acpi_set_firmware_waking_vector(
90 (acpi_physical_address)acpi_wakeup_address);
2f3f2226
AS
91
92 }
93 ACPI_FLUSH_CPU_CACHE();
2f3f2226 94#endif
c9b6c8f6
RW
95 printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
96 acpi_state);
78f5f023 97 acpi_enable_wakeup_devices(acpi_state);
2f3f2226
AS
98 acpi_enter_sleep_state_prep(acpi_state);
99 return 0;
100}
101
5d1e072b 102#ifdef CONFIG_ACPI_SLEEP
091aad6a
JB
103static u32 acpi_target_sleep_state = ACPI_STATE_S0;
104
72ad5d77
RW
105/*
106 * The ACPI specification wants us to save NVS memory regions during hibernation
107 * and to restore them during the subsequent resume. Windows does that also for
108 * suspend to RAM. However, it is known that this mechanism does not work on
109 * all machines, so we allow the user to disable it with the help of the
110 * 'acpi_sleep=nonvs' kernel command line option.
111 */
112static bool nvs_nosave;
113
114void __init acpi_nvs_nosave(void)
115{
116 nvs_nosave = true;
117}
118
d8f3de0d
RW
119/*
120 * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
121 * user to request that behavior by using the 'acpi_old_suspend_ordering'
122 * kernel command line option that causes the following variable to be set.
123 */
124static bool old_suspend_ordering;
125
126void __init acpi_old_suspend_ordering(void)
127{
128 old_suspend_ordering = true;
129}
130
131/**
d5a64513 132 * acpi_pm_freeze - Disable the GPEs and suspend EC transactions.
d8f3de0d 133 */
d5a64513 134static int acpi_pm_freeze(void)
d8f3de0d 135{
3d97e426 136 acpi_disable_all_gpes();
d5a64513 137 acpi_os_wait_events_complete(NULL);
fe955682 138 acpi_ec_block_transactions();
d8f3de0d
RW
139 return 0;
140}
141
c5f7a1bb
RW
142/**
143 * acpi_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS.
144 */
145static int acpi_pm_pre_suspend(void)
146{
147 acpi_pm_freeze();
26fcaf60 148 return suspend_nvs_save();
c5f7a1bb
RW
149}
150
d8f3de0d
RW
151/**
152 * __acpi_pm_prepare - Prepare the platform to enter the target state.
153 *
154 * If necessary, set the firmware waking vector and do arch-specific
155 * nastiness to get the wakeup code to the waking vector.
156 */
157static int __acpi_pm_prepare(void)
158{
159 int error = acpi_sleep_prepare(acpi_target_sleep_state);
d8f3de0d
RW
160 if (error)
161 acpi_target_sleep_state = ACPI_STATE_S0;
c5f7a1bb 162
d8f3de0d
RW
163 return error;
164}
165
166/**
167 * acpi_pm_prepare - Prepare the platform to enter the target sleep
168 * state and disable the GPEs.
169 */
170static int acpi_pm_prepare(void)
171{
172 int error = __acpi_pm_prepare();
d8f3de0d 173 if (!error)
26fcaf60 174 error = acpi_pm_pre_suspend();
d5a64513 175
d8f3de0d
RW
176 return error;
177}
178
179/**
180 * acpi_pm_finish - Instruct the platform to leave a sleep state.
181 *
182 * This is called after we wake back up (or if entering the sleep state
183 * failed).
184 */
185static void acpi_pm_finish(void)
186{
187 u32 acpi_state = acpi_target_sleep_state;
188
42de5532 189 acpi_ec_unblock_transactions();
d551d81d 190 suspend_nvs_free();
2a6b6976 191
d8f3de0d
RW
192 if (acpi_state == ACPI_STATE_S0)
193 return;
1da177e4 194
d8f3de0d
RW
195 printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
196 acpi_state);
78f5f023 197 acpi_disable_wakeup_devices(acpi_state);
d8f3de0d
RW
198 acpi_leave_sleep_state(acpi_state);
199
200 /* reset firmware waking vector */
201 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
202
203 acpi_target_sleep_state = ACPI_STATE_S0;
204}
205
206/**
207 * acpi_pm_end - Finish up suspend sequence.
208 */
209static void acpi_pm_end(void)
210{
211 /*
212 * This is necessary in case acpi_pm_finish() is not called during a
213 * failing transition to a sleep state.
214 */
215 acpi_target_sleep_state = ACPI_STATE_S0;
e49f711c 216 acpi_sleep_tts_switch(acpi_target_sleep_state);
d8f3de0d 217}
92daa7b5
RW
218#else /* !CONFIG_ACPI_SLEEP */
219#define acpi_target_sleep_state ACPI_STATE_S0
5d1e072b 220#endif /* CONFIG_ACPI_SLEEP */
1da177e4 221
d8f3de0d 222#ifdef CONFIG_SUSPEND
1da177e4 223static u32 acpi_suspend_states[] = {
e2a5b420
AS
224 [PM_SUSPEND_ON] = ACPI_STATE_S0,
225 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
226 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
e2a5b420 227 [PM_SUSPEND_MAX] = ACPI_STATE_S5
1da177e4
LT
228};
229
e9b3aba8 230/**
2c6e33c3 231 * acpi_suspend_begin - Set the target system sleep state to the state
e9b3aba8
RW
232 * associated with given @pm_state, if supported.
233 */
2c6e33c3 234static int acpi_suspend_begin(suspend_state_t pm_state)
e9b3aba8
RW
235{
236 u32 acpi_state = acpi_suspend_states[pm_state];
237 int error = 0;
238
72ad5d77 239 error = nvs_nosave ? 0 : suspend_nvs_alloc();
2a6b6976
MG
240 if (error)
241 return error;
242
e9b3aba8
RW
243 if (sleep_states[acpi_state]) {
244 acpi_target_sleep_state = acpi_state;
e49f711c 245 acpi_sleep_tts_switch(acpi_target_sleep_state);
e9b3aba8
RW
246 } else {
247 printk(KERN_ERR "ACPI does not support this state: %d\n",
248 pm_state);
249 error = -ENOSYS;
250 }
251 return error;
252}
253
1da177e4 254/**
2c6e33c3 255 * acpi_suspend_enter - Actually enter a sleep state.
e9b3aba8 256 * @pm_state: ignored
1da177e4 257 *
50ad147a
RW
258 * Flush caches and go to sleep. For STR we have to call arch-specific
259 * assembly, which in turn call acpi_enter_sleep_state().
1da177e4
LT
260 * It's unfortunate, but it works. Please fix if you're feeling frisky.
261 */
2c6e33c3 262static int acpi_suspend_enter(suspend_state_t pm_state)
1da177e4
LT
263{
264 acpi_status status = AE_OK;
e9b3aba8 265 u32 acpi_state = acpi_target_sleep_state;
a2ef5c4f 266 u8 flags = wake_sleep_flags();
979f11b0 267 int error;
1da177e4
LT
268
269 ACPI_FLUSH_CPU_CACHE();
270
e9b3aba8
RW
271 switch (acpi_state) {
272 case ACPI_STATE_S1:
1da177e4 273 barrier();
a2ef5c4f 274 status = acpi_enter_sleep_state(acpi_state, flags);
1da177e4
LT
275 break;
276
e9b3aba8 277 case ACPI_STATE_S3:
f1a2003e 278 error = acpi_suspend_lowlevel();
979f11b0
RW
279 if (error)
280 return error;
7a63f08b 281 pr_info(PREFIX "Low-level resume complete\n");
1da177e4 282 break;
1da177e4 283 }
872d83d0 284
b6dacf63
MG
285 /* This violates the spec but is required for bug compatibility. */
286 acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
65df7847 287
c95d47a8 288 /* Reprogram control registers and execute _BFS */
a2ef5c4f 289 acpi_leave_sleep_state_prep(acpi_state, flags);
c95d47a8 290
23b168d4 291 /* ACPI 3.0 specs (P62) says that it's the responsibility
872d83d0
AP
292 * of the OSPM to clear the status bit [ implying that the
293 * POWER_BUTTON event should not reach userspace ]
294 */
295 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
296 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
297
a3627f67
SL
298 /*
299 * Disable and clear GPE status before interrupt is enabled. Some GPEs
300 * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
301 * acpi_leave_sleep_state will reenable specific GPEs later
302 */
3d97e426 303 acpi_disable_all_gpes();
d5a64513 304 /* Allow EC transactions to happen. */
fe955682 305 acpi_ec_unblock_transactions_early();
a3627f67 306
2a6b6976
MG
307 suspend_nvs_restore();
308
1da177e4
LT
309 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
310}
311
2c6e33c3 312static int acpi_suspend_state_valid(suspend_state_t pm_state)
eb9289eb 313{
e8c9c502 314 u32 acpi_state;
eb9289eb 315
e8c9c502
JB
316 switch (pm_state) {
317 case PM_SUSPEND_ON:
318 case PM_SUSPEND_STANDBY:
319 case PM_SUSPEND_MEM:
320 acpi_state = acpi_suspend_states[pm_state];
321
322 return sleep_states[acpi_state];
323 default:
324 return 0;
325 }
eb9289eb
SL
326}
327
2f55ac07 328static const struct platform_suspend_ops acpi_suspend_ops = {
2c6e33c3
LB
329 .valid = acpi_suspend_state_valid,
330 .begin = acpi_suspend_begin,
6a7c7eaf 331 .prepare_late = acpi_pm_prepare,
2c6e33c3 332 .enter = acpi_suspend_enter,
618d7fd0 333 .wake = acpi_pm_finish,
d8f3de0d
RW
334 .end = acpi_pm_end,
335};
336
337/**
338 * acpi_suspend_begin_old - Set the target system sleep state to the
339 * state associated with given @pm_state, if supported, and
340 * execute the _PTS control method. This function is used if the
341 * pre-ACPI 2.0 suspend ordering has been requested.
342 */
343static int acpi_suspend_begin_old(suspend_state_t pm_state)
344{
345 int error = acpi_suspend_begin(pm_state);
d8f3de0d
RW
346 if (!error)
347 error = __acpi_pm_prepare();
c5f7a1bb 348
d8f3de0d
RW
349 return error;
350}
351
352/*
353 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
354 * been requested.
355 */
2f55ac07 356static const struct platform_suspend_ops acpi_suspend_ops_old = {
d8f3de0d
RW
357 .valid = acpi_suspend_state_valid,
358 .begin = acpi_suspend_begin_old,
c5f7a1bb 359 .prepare_late = acpi_pm_pre_suspend,
2c6e33c3 360 .enter = acpi_suspend_enter,
618d7fd0 361 .wake = acpi_pm_finish,
d8f3de0d
RW
362 .end = acpi_pm_end,
363 .recover = acpi_pm_finish,
1da177e4 364};
e41fb7c5
CC
365
366static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
367{
368 old_suspend_ordering = true;
369 return 0;
370}
371
53998648
RW
372static int __init init_nvs_nosave(const struct dmi_system_id *d)
373{
374 acpi_nvs_nosave();
375 return 0;
376}
377
e41fb7c5
CC
378static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
379 {
380 .callback = init_old_suspend_ordering,
381 .ident = "Abit KN9 (nForce4 variant)",
382 .matches = {
383 DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
384 DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
385 },
386 },
4fb507b6
RW
387 {
388 .callback = init_old_suspend_ordering,
389 .ident = "HP xw4600 Workstation",
390 .matches = {
391 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
392 DMI_MATCH(DMI_PRODUCT_NAME, "HP xw4600 Workstation"),
393 },
394 },
65df7847 395 {
a1404495
AW
396 .callback = init_old_suspend_ordering,
397 .ident = "Asus Pundit P1-AH2 (M2N8L motherboard)",
398 .matches = {
399 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."),
400 DMI_MATCH(DMI_BOARD_NAME, "M2N8L"),
401 },
402 },
45e77988 403 {
2a9ef8e1
ZY
404 .callback = init_old_suspend_ordering,
405 .ident = "Panasonic CF51-2L",
406 .matches = {
407 DMI_MATCH(DMI_BOARD_VENDOR,
408 "Matsushita Electric Industrial Co.,Ltd."),
409 DMI_MATCH(DMI_BOARD_NAME, "CF51-2L"),
410 },
411 },
53998648
RW
412 {
413 .callback = init_nvs_nosave,
d11c78e9
DJ
414 .ident = "Sony Vaio VGN-FW21E",
415 .matches = {
416 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
417 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21E"),
418 },
419 },
420 {
421 .callback = init_nvs_nosave,
ddf6ce45
DJ
422 .ident = "Sony Vaio VPCEB17FX",
423 .matches = {
424 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
425 DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB17FX"),
426 },
427 },
428 {
429 .callback = init_nvs_nosave,
53998648
RW
430 .ident = "Sony Vaio VGN-SR11M",
431 .matches = {
432 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
433 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR11M"),
434 },
435 },
436 {
437 .callback = init_nvs_nosave,
438 .ident = "Everex StepNote Series",
439 .matches = {
440 DMI_MATCH(DMI_SYS_VENDOR, "Everex Systems, Inc."),
441 DMI_MATCH(DMI_PRODUCT_NAME, "Everex StepNote Series"),
442 },
443 },
af48931c
RW
444 {
445 .callback = init_nvs_nosave,
446 .ident = "Sony Vaio VPCEB1Z1E",
447 .matches = {
448 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
449 DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1Z1E"),
450 },
451 },
291a73c9
RW
452 {
453 .callback = init_nvs_nosave,
454 .ident = "Sony Vaio VGN-NW130D",
455 .matches = {
456 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
457 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-NW130D"),
458 },
459 },
7b330707
RW
460 {
461 .callback = init_nvs_nosave,
93f77084
LT
462 .ident = "Sony Vaio VPCCW29FX",
463 .matches = {
464 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
465 DMI_MATCH(DMI_PRODUCT_NAME, "VPCCW29FX"),
466 },
467 },
468 {
469 .callback = init_nvs_nosave,
7b330707
RW
470 .ident = "Averatec AV1020-ED2",
471 .matches = {
472 DMI_MATCH(DMI_SYS_VENDOR, "AVERATEC"),
473 DMI_MATCH(DMI_PRODUCT_NAME, "1000 Series"),
474 },
475 },
bb0c5ed6
ZR
476 {
477 .callback = init_old_suspend_ordering,
478 .ident = "Asus A8N-SLI DELUXE",
479 .matches = {
480 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
481 DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI DELUXE"),
482 },
483 },
484 {
485 .callback = init_old_suspend_ordering,
486 .ident = "Asus A8N-SLI Premium",
487 .matches = {
488 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
489 DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI Premium"),
490 },
491 },
89e8ea12
RW
492 {
493 .callback = init_nvs_nosave,
494 .ident = "Sony Vaio VGN-SR26GN_P",
495 .matches = {
496 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
497 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR26GN_P"),
498 },
499 },
731b25a4
BR
500 {
501 .callback = init_nvs_nosave,
502 .ident = "Sony Vaio VGN-FW520F",
503 .matches = {
504 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
505 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW520F"),
506 },
507 },
5a50a7c3
KYL
508 {
509 .callback = init_nvs_nosave,
510 .ident = "Asus K54C",
511 .matches = {
512 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
513 DMI_MATCH(DMI_PRODUCT_NAME, "K54C"),
514 },
515 },
516 {
517 .callback = init_nvs_nosave,
518 .ident = "Asus K54HR",
519 .matches = {
520 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
521 DMI_MATCH(DMI_PRODUCT_NAME, "K54HR"),
522 },
523 },
e41fb7c5
CC
524 {},
525};
296699de
RW
526#endif /* CONFIG_SUSPEND */
527
b0cb1a19 528#ifdef CONFIG_HIBERNATION
bdfe6b7c
SL
529static unsigned long s4_hardware_signature;
530static struct acpi_table_facs *facs;
531static bool nosigcheck;
532
533void __init acpi_no_s4_hw_signature(void)
534{
535 nosigcheck = true;
536}
537
caea99ef 538static int acpi_hibernation_begin(void)
74f270af 539{
3f4b0ef7
RW
540 int error;
541
72ad5d77 542 error = nvs_nosave ? 0 : suspend_nvs_alloc();
3f4b0ef7
RW
543 if (!error) {
544 acpi_target_sleep_state = ACPI_STATE_S4;
545 acpi_sleep_tts_switch(acpi_target_sleep_state);
546 }
547
548 return error;
549}
550
a3d25c27
RW
551static int acpi_hibernation_enter(void)
552{
a2ef5c4f 553 u8 flags = wake_sleep_flags();
a3d25c27 554 acpi_status status = AE_OK;
a3d25c27
RW
555
556 ACPI_FLUSH_CPU_CACHE();
557
a3d25c27 558 /* This shouldn't return. If it returns, we have a problem */
a2ef5c4f 559 status = acpi_enter_sleep_state(ACPI_STATE_S4, flags);
c95d47a8 560 /* Reprogram control registers and execute _BFS */
a2ef5c4f 561 acpi_leave_sleep_state_prep(ACPI_STATE_S4, flags);
a3d25c27
RW
562
563 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
564}
565
c7e0831d
RW
566static void acpi_hibernation_leave(void)
567{
a2ef5c4f
LM
568 u8 flags = wake_sleep_flags();
569
c7e0831d
RW
570 /*
571 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
572 * enable it here.
573 */
574 acpi_enable();
c95d47a8 575 /* Reprogram control registers and execute _BFS */
a2ef5c4f 576 acpi_leave_sleep_state_prep(ACPI_STATE_S4, flags);
bdfe6b7c
SL
577 /* Check the hardware signature */
578 if (facs && s4_hardware_signature != facs->hardware_signature) {
579 printk(KERN_EMERG "ACPI: Hardware changed while hibernated, "
580 "cannot resume!\n");
581 panic("ACPI S4 hardware signature mismatch");
582 }
3f4b0ef7 583 /* Restore the NVS memory area */
dd4c4f17 584 suspend_nvs_restore();
d5a64513 585 /* Allow EC transactions to happen. */
fe955682 586 acpi_ec_unblock_transactions_early();
c7e0831d
RW
587}
588
d5a64513 589static void acpi_pm_thaw(void)
f6bb13aa 590{
fe955682 591 acpi_ec_unblock_transactions();
3d97e426 592 acpi_enable_all_runtime_gpes();
a3d25c27
RW
593}
594
073ef1f6 595static const struct platform_hibernation_ops acpi_hibernation_ops = {
d8f3de0d
RW
596 .begin = acpi_hibernation_begin,
597 .end = acpi_pm_end,
c5f7a1bb 598 .pre_snapshot = acpi_pm_prepare,
2a6b6976 599 .finish = acpi_pm_finish,
d8f3de0d
RW
600 .prepare = acpi_pm_prepare,
601 .enter = acpi_hibernation_enter,
602 .leave = acpi_hibernation_leave,
d5a64513
RW
603 .pre_restore = acpi_pm_freeze,
604 .restore_cleanup = acpi_pm_thaw,
d8f3de0d 605};
caea99ef 606
d8f3de0d
RW
607/**
608 * acpi_hibernation_begin_old - Set the target system sleep state to
609 * ACPI_STATE_S4 and execute the _PTS control method. This
610 * function is used if the pre-ACPI 2.0 suspend ordering has been
611 * requested.
612 */
613static int acpi_hibernation_begin_old(void)
a634cc10 614{
e49f711c
ZY
615 int error;
616 /*
617 * The _TTS object should always be evaluated before the _PTS object.
618 * When the old_suspended_ordering is true, the _PTS object is
619 * evaluated in the acpi_sleep_prepare.
620 */
621 acpi_sleep_tts_switch(ACPI_STATE_S4);
622
623 error = acpi_sleep_prepare(ACPI_STATE_S4);
a634cc10 624
3f4b0ef7 625 if (!error) {
72ad5d77 626 if (!nvs_nosave)
dd4c4f17 627 error = suspend_nvs_alloc();
3f4b0ef7
RW
628 if (!error)
629 acpi_target_sleep_state = ACPI_STATE_S4;
630 }
631 return error;
632}
633
d8f3de0d
RW
634/*
635 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
636 * been requested.
637 */
073ef1f6 638static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
d8f3de0d
RW
639 .begin = acpi_hibernation_begin_old,
640 .end = acpi_pm_end,
c5f7a1bb 641 .pre_snapshot = acpi_pm_pre_suspend,
d5a64513 642 .prepare = acpi_pm_freeze,
2a6b6976 643 .finish = acpi_pm_finish,
a3d25c27 644 .enter = acpi_hibernation_enter,
c7e0831d 645 .leave = acpi_hibernation_leave,
d5a64513
RW
646 .pre_restore = acpi_pm_freeze,
647 .restore_cleanup = acpi_pm_thaw,
d8f3de0d 648 .recover = acpi_pm_finish,
a3d25c27 649};
d8f3de0d 650#endif /* CONFIG_HIBERNATION */
a3d25c27 651
296699de
RW
652int acpi_suspend(u32 acpi_state)
653{
654 suspend_state_t states[] = {
655 [1] = PM_SUSPEND_STANDBY,
656 [3] = PM_SUSPEND_MEM,
657 [5] = PM_SUSPEND_MAX
658 };
659
660 if (acpi_state < 6 && states[acpi_state])
661 return pm_suspend(states[acpi_state]);
662 if (acpi_state == 4)
663 return hibernate();
664 return -EINVAL;
665}
666
aa338601 667#ifdef CONFIG_PM
fd4aff1a
SL
668/**
669 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
670 * in the system sleep state given by %acpi_target_sleep_state
2fe2de5f
DB
671 * @dev: device to examine; its driver model wakeup flags control
672 * whether it should be able to wake up the system
fd4aff1a
SL
673 * @d_min_p: used to store the upper limit of allowed states range
674 * Return value: preferred power state of the device on success, -ENODEV on
675 * failure (ie. if there's no 'struct acpi_device' for @dev)
676 *
677 * Find the lowest power (highest number) ACPI device power state that
678 * device @dev can be in while the system is in the sleep state represented
679 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
680 * able to wake up the system from this sleep state. If @d_min_p is set,
681 * the highest power (lowest number) device power state of @dev allowed
682 * in this system sleep state is stored at the location pointed to by it.
683 *
684 * The caller must ensure that @dev is valid before using this function.
685 * The caller is also responsible for figuring out if the device is
686 * supposed to be able to wake up the system and passing this information
687 * via @wake.
688 */
689
2fe2de5f 690int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
fd4aff1a
SL
691{
692 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
693 struct acpi_device *adev;
694 char acpi_method[] = "_SxD";
27663c58 695 unsigned long long d_min, d_max;
fd4aff1a
SL
696
697 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
ead77594 698 printk(KERN_DEBUG "ACPI handle has no context!\n");
fd4aff1a
SL
699 return -ENODEV;
700 }
701
702 acpi_method[2] = '0' + acpi_target_sleep_state;
703 /*
704 * If the sleep state is S0, we will return D3, but if the device has
705 * _S0W, we will use the value from _S0W
706 */
707 d_min = ACPI_STATE_D0;
708 d_max = ACPI_STATE_D3;
709
710 /*
711 * If present, _SxD methods return the minimum D-state (highest power
712 * state) we can use for the corresponding S-states. Otherwise, the
713 * minimum D-state is D0 (ACPI 3.x).
714 *
715 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
716 * provided -- that's our fault recovery, we ignore retval.
717 */
718 if (acpi_target_sleep_state > ACPI_STATE_S0)
719 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
720
721 /*
722 * If _PRW says we can wake up the system from the target sleep state,
723 * the D-state returned by _SxD is sufficient for that (we assume a
724 * wakeup-aware driver if wake is set). Still, if _SxW exists
725 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
726 * can wake the system. _S0W may be valid, too.
727 */
728 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
761afb86 729 (device_may_wakeup(dev) &&
fd4aff1a 730 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
ad3399c3
RW
731 acpi_status status;
732
fd4aff1a 733 acpi_method[3] = 'W';
ad3399c3
RW
734 status = acpi_evaluate_integer(handle, acpi_method, NULL,
735 &d_max);
736 if (ACPI_FAILURE(status)) {
761afb86
RW
737 if (acpi_target_sleep_state != ACPI_STATE_S0 ||
738 status != AE_NOT_FOUND)
739 d_max = d_min;
ad3399c3
RW
740 } else if (d_max < d_min) {
741 /* Warn the user of the broken DSDT */
742 printk(KERN_WARNING "ACPI: Wrong value from %s\n",
743 acpi_method);
744 /* Sanitize it */
fd4aff1a 745 d_min = d_max;
ad3399c3 746 }
fd4aff1a
SL
747 }
748
749 if (d_min_p)
750 *d_min_p = d_min;
751 return d_max;
752}
aa338601 753#endif /* CONFIG_PM */
eb9d0fe4 754
761afb86 755#ifdef CONFIG_PM_SLEEP
b24e5098
LM
756/**
757 * acpi_pm_device_run_wake - Enable/disable wake-up for given device.
758 * @phys_dev: Device to enable/disable the platform to wake-up the system for.
759 * @enable: Whether enable or disable the wake-up functionality.
760 *
761 * Find the ACPI device object corresponding to @pci_dev and try to
762 * enable/disable the GPE associated with it.
763 */
764int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
765{
766 struct acpi_device *dev;
767 acpi_handle handle;
768
769 if (!device_run_wake(phys_dev))
770 return -EINVAL;
771
772 handle = DEVICE_ACPI_HANDLE(phys_dev);
773 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev))) {
774 dev_dbg(phys_dev, "ACPI handle has no context in %s!\n",
775 __func__);
776 return -ENODEV;
777 }
778
779 if (enable) {
780 acpi_enable_wakeup_device_power(dev, ACPI_STATE_S0);
781 acpi_enable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number);
782 } else {
783 acpi_disable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number);
784 acpi_disable_wakeup_device_power(dev);
785 }
786
787 return 0;
788}
789
eb9d0fe4
RW
790/**
791 * acpi_pm_device_sleep_wake - enable or disable the system wake-up
792 * capability of given device
793 * @dev: device to handle
794 * @enable: 'true' - enable, 'false' - disable the wake-up capability
795 */
796int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
797{
798 acpi_handle handle;
799 struct acpi_device *adev;
df8db91f 800 int error;
eb9d0fe4 801
0baed8da 802 if (!device_can_wakeup(dev))
eb9d0fe4
RW
803 return -EINVAL;
804
805 handle = DEVICE_ACPI_HANDLE(dev);
806 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
df8db91f 807 dev_dbg(dev, "ACPI handle has no context in %s!\n", __func__);
eb9d0fe4
RW
808 return -ENODEV;
809 }
810
e8b6f970
RW
811 error = enable ?
812 acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) :
813 acpi_disable_wakeup_device_power(adev);
df8db91f
RW
814 if (!error)
815 dev_info(dev, "wake-up capability %s by ACPI\n",
816 enable ? "enabled" : "disabled");
817
818 return error;
eb9d0fe4 819}
761afb86 820#endif /* CONFIG_PM_SLEEP */
fd4aff1a 821
f216cc37
AS
822static void acpi_power_off_prepare(void)
823{
824 /* Prepare to power off the system */
825 acpi_sleep_prepare(ACPI_STATE_S5);
3d97e426 826 acpi_disable_all_gpes();
f216cc37
AS
827}
828
829static void acpi_power_off(void)
830{
a2ef5c4f
LM
831 u8 flags = wake_sleep_flags();
832
f216cc37 833 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
4d939155 834 printk(KERN_DEBUG "%s called\n", __func__);
f216cc37 835 local_irq_disable();
a2ef5c4f 836 acpi_enter_sleep_state(ACPI_STATE_S5, flags);
f216cc37
AS
837}
838
96f15efc
LB
839/*
840 * ACPI 2.0 created the optional _GTS and _BFS,
841 * but industry adoption has been neither rapid nor broad.
842 *
843 * Linux gets into trouble when it executes poorly validated
844 * paths through the BIOS, so disable _GTS and _BFS by default,
845 * but do speak up and offer the option to enable them.
846 */
01eac60b 847static void __init acpi_gts_bfs_check(void)
96f15efc
LB
848{
849 acpi_handle dummy;
850
4efeeecd 851 if (ACPI_SUCCESS(acpi_get_handle(ACPI_ROOT_OBJECT, METHOD_PATHNAME__GTS, &dummy)))
96f15efc
LB
852 {
853 printk(KERN_NOTICE PREFIX "BIOS offers _GTS\n");
854 printk(KERN_NOTICE PREFIX "If \"acpi.gts=1\" improves suspend, "
855 "please notify linux-acpi@vger.kernel.org\n");
856 }
4efeeecd 857 if (ACPI_SUCCESS(acpi_get_handle(ACPI_ROOT_OBJECT, METHOD_PATHNAME__BFS, &dummy)))
96f15efc
LB
858 {
859 printk(KERN_NOTICE PREFIX "BIOS offers _BFS\n");
860 printk(KERN_NOTICE PREFIX "If \"acpi.bfs=1\" improves resume, "
861 "please notify linux-acpi@vger.kernel.org\n");
862 }
863}
864
aafbcd16 865int __init acpi_sleep_init(void)
1da177e4 866{
296699de
RW
867 acpi_status status;
868 u8 type_a, type_b;
869#ifdef CONFIG_SUSPEND
e2a5b420 870 int i = 0;
e41fb7c5
CC
871
872 dmi_check_system(acpisleep_dmi_table);
296699de 873#endif
1da177e4
LT
874
875 if (acpi_disabled)
876 return 0;
877
5a50fe70
FP
878 sleep_states[ACPI_STATE_S0] = 1;
879 printk(KERN_INFO PREFIX "(supports S0");
880
296699de 881#ifdef CONFIG_SUSPEND
5a50fe70 882 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
1da177e4
LT
883 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
884 if (ACPI_SUCCESS(status)) {
885 sleep_states[i] = 1;
886 printk(" S%d", i);
887 }
1da177e4 888 }
1da177e4 889
d8f3de0d
RW
890 suspend_set_ops(old_suspend_ordering ?
891 &acpi_suspend_ops_old : &acpi_suspend_ops);
296699de 892#endif
a3d25c27 893
b0cb1a19 894#ifdef CONFIG_HIBERNATION
296699de
RW
895 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
896 if (ACPI_SUCCESS(status)) {
d8f3de0d
RW
897 hibernation_set_ops(old_suspend_ordering ?
898 &acpi_hibernation_ops_old : &acpi_hibernation_ops);
296699de 899 sleep_states[ACPI_STATE_S4] = 1;
f216cc37 900 printk(" S4");
bdfe6b7c 901 if (!nosigcheck) {
3d97e426 902 acpi_get_table(ACPI_SIG_FACS, 1,
bdfe6b7c
SL
903 (struct acpi_table_header **)&facs);
904 if (facs)
905 s4_hardware_signature =
906 facs->hardware_signature;
907 }
296699de 908 }
a3d25c27 909#endif
f216cc37
AS
910 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
911 if (ACPI_SUCCESS(status)) {
912 sleep_states[ACPI_STATE_S5] = 1;
913 printk(" S5");
914 pm_power_off_prepare = acpi_power_off_prepare;
915 pm_power_off = acpi_power_off;
916 }
917 printk(")\n");
e49f711c
ZY
918 /*
919 * Register the tts_notifier to reboot notifier list so that the _TTS
920 * object can also be evaluated when the system enters S5.
921 */
922 register_reboot_notifier(&tts_notifier);
96f15efc 923 acpi_gts_bfs_check();
1da177e4
LT
924 return 0;
925}
This page took 1.063128 seconds and 5 git commands to generate.