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