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