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