blob: 8640782944cc58e02da1e2e50d27e258777aa161 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * sleep.c - ACPI sleep support.
3 *
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -05004 * Copyright (c) 2005 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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>
Zhao Yakuie49f7112008-08-12 10:20:22 +080018#include <linux/reboot.h>
H. Peter Anvind1ee4332011-02-14 15:42:46 -080019#include <linux/acpi.h>
Lin Minga2ef5c42012-03-21 10:58:46 +080020#include <linux/module.h>
Lin Mingb24e5092012-03-27 15:43:25 +080021#include <linux/pm_runtime.h>
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +040022
23#include <asm/io.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <acpi/acpi_bus.h>
26#include <acpi/acpi_drivers.h>
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060027
28#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "sleep.h"
30
Stephen Hemminger01eac602010-10-18 18:47:25 -070031static u8 sleep_states[ACPI_S_STATE_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Zhao Yakuie49f7112008-08-12 10:20:22 +080033static 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
50static 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
57static struct notifier_block tts_notifier = {
58 .notifier_call = tts_notify_reboot,
59 .next = NULL,
60 .priority = 0,
61};
62
Rafael J. Wysockic9b6c8f2008-01-08 00:10:57 +010063static int acpi_sleep_prepare(u32 acpi_state)
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020064{
65#ifdef CONFIG_ACPI_SLEEP
66 /* do we have a wakeup address for S2 and S3? */
67 if (acpi_state == ACPI_STATE_S3) {
H. Peter Anvin319b6ff2012-05-30 12:33:41 +030068 if (!acpi_wakeup_address)
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020069 return -EFAULT;
H. Peter Anvin319b6ff2012-05-30 12:33:41 +030070 acpi_set_firmware_waking_vector(acpi_wakeup_address);
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020071
72 }
73 ACPI_FLUSH_CPU_CACHE();
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020074#endif
Rafael J. Wysockic9b6c8f2008-01-08 00:10:57 +010075 printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
76 acpi_state);
Rafael J. Wysocki78f5f022010-07-06 22:09:38 -040077 acpi_enable_wakeup_devices(acpi_state);
Alexey Starikovskiy2f3f22262007-09-24 14:33:21 +020078 acpi_enter_sleep_state_prep(acpi_state);
79 return 0;
80}
81
Rafael J. Wysocki5d1e0722008-10-22 14:58:43 -040082#ifdef CONFIG_ACPI_SLEEP
Jan Beulich091aad62010-12-07 14:52:25 +000083static u32 acpi_target_sleep_state = ACPI_STATE_S0;
Rafael J. Wysockia5ca7342012-07-23 21:01:02 +020084static bool pwr_btn_event_pending;
Jan Beulich091aad62010-12-07 14:52:25 +000085
Zhang Ruid7f0eea2009-12-30 15:36:42 +080086/*
Rafael J. Wysocki72ad5d72010-07-23 22:59:09 +020087 * 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 */
93static bool nvs_nosave;
94
95void __init acpi_nvs_nosave(void)
96{
97 nvs_nosave = true;
98}
99
100/*
Kristen Carlson Accardi1bad2f12012-10-26 13:39:15 +0200101 * 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 */
108static bool nvs_nosave_s3;
109
110void __init acpi_nvs_nosave_s3(void)
111{
112 nvs_nosave_s3 = true;
113}
114
115/*
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200116 * 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 */
120static bool old_suspend_ordering;
121
122void __init acpi_old_suspend_ordering(void)
123{
124 old_suspend_ordering = true;
125}
126
127/**
Rafael J. Wysockid5a64512010-04-09 01:39:40 +0200128 * acpi_pm_freeze - Disable the GPEs and suspend EC transactions.
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200129 */
Rafael J. Wysockid5a64512010-04-09 01:39:40 +0200130static int acpi_pm_freeze(void)
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200131{
Lin Ming3d97e422008-12-16 16:57:46 +0800132 acpi_disable_all_gpes();
Lin Mingbd6f10a2012-05-22 16:43:49 +0800133 acpi_os_wait_events_complete();
Rafael J. Wysockife955682010-04-09 01:40:38 +0200134 acpi_ec_block_transactions();
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200135 return 0;
136}
137
138/**
Rafael J. Wysockic5f7a1b2010-07-02 00:14:09 +0200139 * acpi_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS.
140 */
141static int acpi_pm_pre_suspend(void)
142{
143 acpi_pm_freeze();
Jiri Slaby26fcaf62011-01-07 01:42:31 +0100144 return suspend_nvs_save();
Rafael J. Wysockic5f7a1b2010-07-02 00:14:09 +0200145}
146
147/**
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200148 * __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 */
153static int __acpi_pm_prepare(void)
154{
155 int error = acpi_sleep_prepare(acpi_target_sleep_state);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200156 if (error)
157 acpi_target_sleep_state = ACPI_STATE_S0;
Rafael J. Wysockic5f7a1b2010-07-02 00:14:09 +0200158
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200159 return error;
160}
161
162/**
163 * acpi_pm_prepare - Prepare the platform to enter the target sleep
164 * state and disable the GPEs.
165 */
166static int acpi_pm_prepare(void)
167{
168 int error = __acpi_pm_prepare();
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200169 if (!error)
Jiri Slaby26fcaf62011-01-07 01:42:31 +0100170 error = acpi_pm_pre_suspend();
Rafael J. Wysockid5a64512010-04-09 01:39:40 +0200171
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200172 return error;
173}
174
Daniel Drakec10d7a12012-05-10 00:08:43 +0100175static 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
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200183/**
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 */
189static void acpi_pm_finish(void)
190{
Daniel Drakec10d7a12012-05-10 00:08:43 +0100191 struct device *pwr_btn_dev;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200192 u32 acpi_state = acpi_target_sleep_state;
193
Len Brown42de5532010-06-12 01:15:40 -0400194 acpi_ec_unblock_transactions();
Rafael J. Wysockid551d812011-01-19 22:27:55 +0100195 suspend_nvs_free();
Matthew Garrett2a6b6972010-05-28 16:32:15 -0400196
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200197 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);
Rafael J. Wysocki78f5f022010-07-06 22:09:38 -0400202 acpi_disable_wakeup_devices(acpi_state);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200203 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;
Daniel Drakec10d7a12012-05-10 00:08:43 +0100209
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 }
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200226}
227
228/**
229 * acpi_pm_end - Finish up suspend sequence.
230 */
231static 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;
Zhao Yakuie49f7112008-08-12 10:20:22 +0800238 acpi_sleep_tts_switch(acpi_target_sleep_state);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200239}
Rafael J. Wysocki92daa7b2008-10-23 21:46:43 +0200240#else /* !CONFIG_ACPI_SLEEP */
241#define acpi_target_sleep_state ACPI_STATE_S0
Rafael J. Wysocki5d1e0722008-10-22 14:58:43 -0400242#endif /* CONFIG_ACPI_SLEEP */
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200243
244#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245static u32 acpi_suspend_states[] = {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500246 [PM_SUSPEND_ON] = ACPI_STATE_S0,
247 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
248 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500249 [PM_SUSPEND_MAX] = ACPI_STATE_S5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250};
251
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200252/**
Len Brown2c6e33c2008-04-23 18:02:52 -0400253 * acpi_suspend_begin - Set the target system sleep state to the state
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200254 * associated with given @pm_state, if supported.
255 */
Len Brown2c6e33c2008-04-23 18:02:52 -0400256static int acpi_suspend_begin(suspend_state_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 u32 acpi_state = acpi_suspend_states[pm_state];
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200259 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Kristen Carlson Accardi1bad2f12012-10-26 13:39:15 +0200261 error = (nvs_nosave || nvs_nosave_s3) ? 0 : suspend_nvs_alloc();
Matthew Garrett2a6b6972010-05-28 16:32:15 -0400262 if (error)
263 return error;
264
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200265 if (sleep_states[acpi_state]) {
266 acpi_target_sleep_state = acpi_state;
Zhao Yakuie49f7112008-08-12 10:20:22 +0800267 acpi_sleep_tts_switch(acpi_target_sleep_state);
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200268 } else {
269 printk(KERN_ERR "ACPI does not support this state: %d\n",
270 pm_state);
271 error = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200273 return error;
274}
275
276/**
Len Brown2c6e33c2008-04-23 18:02:52 -0400277 * acpi_suspend_enter - Actually enter a sleep state.
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200278 * @pm_state: ignored
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 *
Rafael J. Wysocki50ad1472007-07-24 11:58:39 +0200280 * Flush caches and go to sleep. For STR we have to call arch-specific
281 * assembly, which in turn call acpi_enter_sleep_state().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * It's unfortunate, but it works. Please fix if you're feeling frisky.
283 */
Len Brown2c6e33c2008-04-23 18:02:52 -0400284static int acpi_suspend_enter(suspend_state_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 acpi_status status = AE_OK;
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200287 u32 acpi_state = acpi_target_sleep_state;
Rafael J. Wysocki979f11b2011-02-08 23:42:09 +0100288 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 ACPI_FLUSH_CPU_CACHE();
291
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200292 switch (acpi_state) {
293 case ACPI_STATE_S1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 barrier();
Len Brown3f6f49c2012-07-26 20:08:54 -0400295 status = acpi_enter_sleep_state(acpi_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 break;
297
Rafael J. Wysockie9b3aba2007-07-17 22:40:06 +0200298 case ACPI_STATE_S3:
Rafael J. Wysockif1a20032011-02-08 23:42:22 +0100299 error = acpi_suspend_lowlevel();
Rafael J. Wysocki979f11b2011-02-08 23:42:09 +0100300 if (error)
301 return error;
Rafael J. Wysocki7a63f082011-02-08 23:41:57 +0100302 pr_info(PREFIX "Low-level resume complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Arnaud Patard872d83d2006-04-27 05:25:00 -0400305
Matthew Garrettb6dacf62010-05-11 13:49:25 -0400306 /* This violates the spec but is required for bug compatibility. */
307 acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
Rafael J. Wysocki65df7842008-11-26 17:53:13 -0500308
Len Brown3f6f49c2012-07-26 20:08:54 -0400309 /* Reprogram control registers */
310 acpi_leave_sleep_state_prep(acpi_state);
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100311
Pavel Machek23b168d2008-02-05 19:27:12 +0100312 /* ACPI 3.0 specs (P62) says that it's the responsibility
Arnaud Patard872d83d2006-04-27 05:25:00 -0400313 * of the OSPM to clear the status bit [ implying that the
314 * POWER_BUTTON event should not reach userspace ]
Daniel Drakec10d7a12012-05-10 00:08:43 +0100315 *
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.
Arnaud Patard872d83d2006-04-27 05:25:00 -0400320 */
Daniel Drakec10d7a12012-05-10 00:08:43 +0100321 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 }
Arnaud Patard872d83d2006-04-27 05:25:00 -0400332
Shaohua Lia3627f62007-06-20 09:17:58 +0800333 /*
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 */
Lin Ming3d97e422008-12-16 16:57:46 +0800338 acpi_disable_all_gpes();
Rafael J. Wysockid5a64512010-04-09 01:39:40 +0200339 /* Allow EC transactions to happen. */
Rafael J. Wysockife955682010-04-09 01:40:38 +0200340 acpi_ec_unblock_transactions_early();
Shaohua Lia3627f62007-06-20 09:17:58 +0800341
Matthew Garrett2a6b6972010-05-28 16:32:15 -0400342 suspend_nvs_restore();
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
345}
346
Len Brown2c6e33c2008-04-23 18:02:52 -0400347static int acpi_suspend_state_valid(suspend_state_t pm_state)
Shaohua Lieb9289e2005-10-30 15:00:01 -0800348{
Johannes Berge8c9c502007-04-30 15:09:54 -0700349 u32 acpi_state;
Shaohua Lieb9289e2005-10-30 15:00:01 -0800350
Johannes Berge8c9c502007-04-30 15:09:54 -0700351 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 }
Shaohua Lieb9289e2005-10-30 15:00:01 -0800361}
362
Lionel Debroux2f55ac02010-11-16 14:14:02 +0100363static const struct platform_suspend_ops acpi_suspend_ops = {
Len Brown2c6e33c2008-04-23 18:02:52 -0400364 .valid = acpi_suspend_state_valid,
365 .begin = acpi_suspend_begin,
Rafael J. Wysocki6a7c7ea2009-04-19 20:08:42 +0200366 .prepare_late = acpi_pm_prepare,
Len Brown2c6e33c2008-04-23 18:02:52 -0400367 .enter = acpi_suspend_enter,
Rafael J. Wysocki618d7fd2010-07-02 00:14:57 +0200368 .wake = acpi_pm_finish,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200369 .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 */
378static int acpi_suspend_begin_old(suspend_state_t pm_state)
379{
380 int error = acpi_suspend_begin(pm_state);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200381 if (!error)
382 error = __acpi_pm_prepare();
Rafael J. Wysockic5f7a1b2010-07-02 00:14:09 +0200383
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200384 return error;
385}
386
387/*
388 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
389 * been requested.
390 */
Lionel Debroux2f55ac02010-11-16 14:14:02 +0100391static const struct platform_suspend_ops acpi_suspend_ops_old = {
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200392 .valid = acpi_suspend_state_valid,
393 .begin = acpi_suspend_begin_old,
Rafael J. Wysockic5f7a1b2010-07-02 00:14:09 +0200394 .prepare_late = acpi_pm_pre_suspend,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200395 .enter = acpi_suspend_enter,
Rafael J. Wysocki618d7fd2010-07-02 00:14:57 +0200396 .wake = acpi_pm_finish,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200397 .end = acpi_pm_end,
398 .recover = acpi_pm_finish,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399};
Carlos Corbachoe41fb7c2008-07-23 21:28:43 -0700400
401static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
402{
403 old_suspend_ordering = true;
404 return 0;
405}
406
Rafael J. Wysocki53998642010-09-24 16:46:14 -0400407static int __init init_nvs_nosave(const struct dmi_system_id *d)
408{
409 acpi_nvs_nosave();
410 return 0;
411}
412
Carlos Corbachoe41fb7c2008-07-23 21:28:43 -0700413static 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 },
Rafael J. Wysocki4fb507b2008-10-14 22:54:06 +0200422 {
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 },
Rafael J. Wysocki65df7842008-11-26 17:53:13 -0500430 {
Andy Whitcrofta1404492009-02-11 18:11:22 +0000431 .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 },
Zhang Rui45e77982009-03-15 22:13:44 -0400438 {
Zhao Yakui2a9ef8e2009-03-18 16:36:25 +0800439 .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 },
Rafael J. Wysocki53998642010-09-24 16:46:14 -0400447 {
448 .callback = init_nvs_nosave,
Dave Jonesd11c78e2011-10-19 23:15:11 +0200449 .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,
Dave Jonesddf6ce42011-11-03 00:58:59 +0100457 .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,
Rafael J. Wysocki53998642010-09-24 16:46:14 -0400465 .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 },
Rafael J. Wysockiaf489312010-10-17 21:01:21 +0200479 {
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 },
Rafael J. Wysocki291a73c2010-12-12 21:10:42 +0100487 {
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 },
Rafael J. Wysocki7b330702011-01-06 23:37:01 +0100495 {
496 .callback = init_nvs_nosave,
Lan Tianyu93f77082012-01-21 09:23:56 +0800497 .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,
Rafael J. Wysocki7b330702011-01-06 23:37:01 +0100505 .ident = "Averatec AV1020-ED2",
506 .matches = {
507 DMI_MATCH(DMI_SYS_VENDOR, "AVERATEC"),
508 DMI_MATCH(DMI_PRODUCT_NAME, "1000 Series"),
509 },
510 },
Zhang Ruibb0c5ed2011-07-30 01:40:48 -0400511 {
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 },
Rafael J. Wysocki89e8ea12011-10-06 20:35:03 +0200527 {
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 },
Bogdan Radulescu731b25a2011-10-06 20:35:12 +0200535 {
536 .callback = init_nvs_nosave,
537 .ident = "Sony Vaio VGN-FW520F",
538 .matches = {
539 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
540 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW520F"),
541 },
542 },
Keng-Yu Lin5a50a7c2011-12-02 00:04:23 +0100543 {
544 .callback = init_nvs_nosave,
545 .ident = "Asus K54C",
546 .matches = {
547 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
548 DMI_MATCH(DMI_PRODUCT_NAME, "K54C"),
549 },
550 },
551 {
552 .callback = init_nvs_nosave,
553 .ident = "Asus K54HR",
554 .matches = {
555 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
556 DMI_MATCH(DMI_PRODUCT_NAME, "K54HR"),
557 },
558 },
Carlos Corbachoe41fb7c2008-07-23 21:28:43 -0700559 {},
560};
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200561#endif /* CONFIG_SUSPEND */
562
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200563#ifdef CONFIG_HIBERNATION
Shaohua Libdfe6b72008-07-23 21:28:41 -0700564static unsigned long s4_hardware_signature;
565static struct acpi_table_facs *facs;
566static bool nosigcheck;
567
568void __init acpi_no_s4_hw_signature(void)
569{
570 nosigcheck = true;
571}
572
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100573static int acpi_hibernation_begin(void)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700574{
Rafael J. Wysocki3f4b0ef2008-10-26 20:52:15 +0100575 int error;
576
Rafael J. Wysocki72ad5d72010-07-23 22:59:09 +0200577 error = nvs_nosave ? 0 : suspend_nvs_alloc();
Rafael J. Wysocki3f4b0ef2008-10-26 20:52:15 +0100578 if (!error) {
579 acpi_target_sleep_state = ACPI_STATE_S4;
580 acpi_sleep_tts_switch(acpi_target_sleep_state);
581 }
582
583 return error;
584}
585
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700586static int acpi_hibernation_enter(void)
587{
588 acpi_status status = AE_OK;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700589
590 ACPI_FLUSH_CPU_CACHE();
591
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700592 /* This shouldn't return. If it returns, we have a problem */
Len Brown3f6f49c2012-07-26 20:08:54 -0400593 status = acpi_enter_sleep_state(ACPI_STATE_S4);
594 /* Reprogram control registers */
595 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700596
597 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
598}
599
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700600static void acpi_hibernation_leave(void)
601{
602 /*
603 * If ACPI is not enabled by the BIOS and the boot kernel, we need to
604 * enable it here.
605 */
606 acpi_enable();
Len Brown3f6f49c2012-07-26 20:08:54 -0400607 /* Reprogram control registers */
608 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
Shaohua Libdfe6b72008-07-23 21:28:41 -0700609 /* Check the hardware signature */
610 if (facs && s4_hardware_signature != facs->hardware_signature) {
611 printk(KERN_EMERG "ACPI: Hardware changed while hibernated, "
612 "cannot resume!\n");
613 panic("ACPI S4 hardware signature mismatch");
614 }
Rafael J. Wysocki3f4b0ef2008-10-26 20:52:15 +0100615 /* Restore the NVS memory area */
Matthew Garrettdd4c4f12010-05-28 16:32:14 -0400616 suspend_nvs_restore();
Rafael J. Wysockid5a64512010-04-09 01:39:40 +0200617 /* Allow EC transactions to happen. */
Rafael J. Wysockife955682010-04-09 01:40:38 +0200618 acpi_ec_unblock_transactions_early();
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700619}
620
Rafael J. Wysockid5a64512010-04-09 01:39:40 +0200621static void acpi_pm_thaw(void)
Rafael J. Wysockif6bb13a2010-03-04 01:52:58 +0100622{
Rafael J. Wysockife955682010-04-09 01:40:38 +0200623 acpi_ec_unblock_transactions();
Lin Ming3d97e422008-12-16 16:57:46 +0800624 acpi_enable_all_runtime_gpes();
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700625}
626
Lionel Debroux073ef1f2010-11-09 21:48:49 +0100627static const struct platform_hibernation_ops acpi_hibernation_ops = {
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100628 .begin = acpi_hibernation_begin,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200629 .end = acpi_pm_end,
Rafael J. Wysockic5f7a1b2010-07-02 00:14:09 +0200630 .pre_snapshot = acpi_pm_prepare,
Matthew Garrett2a6b6972010-05-28 16:32:15 -0400631 .finish = acpi_pm_finish,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200632 .prepare = acpi_pm_prepare,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700633 .enter = acpi_hibernation_enter,
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700634 .leave = acpi_hibernation_leave,
Rafael J. Wysockid5a64512010-04-09 01:39:40 +0200635 .pre_restore = acpi_pm_freeze,
636 .restore_cleanup = acpi_pm_thaw,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700637};
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200638
639/**
640 * acpi_hibernation_begin_old - Set the target system sleep state to
641 * ACPI_STATE_S4 and execute the _PTS control method. This
642 * function is used if the pre-ACPI 2.0 suspend ordering has been
643 * requested.
644 */
645static int acpi_hibernation_begin_old(void)
646{
Zhao Yakuie49f7112008-08-12 10:20:22 +0800647 int error;
648 /*
649 * The _TTS object should always be evaluated before the _PTS object.
650 * When the old_suspended_ordering is true, the _PTS object is
651 * evaluated in the acpi_sleep_prepare.
652 */
653 acpi_sleep_tts_switch(ACPI_STATE_S4);
654
655 error = acpi_sleep_prepare(ACPI_STATE_S4);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200656
Rafael J. Wysocki3f4b0ef2008-10-26 20:52:15 +0100657 if (!error) {
Rafael J. Wysocki72ad5d72010-07-23 22:59:09 +0200658 if (!nvs_nosave)
Matthew Garrettdd4c4f12010-05-28 16:32:14 -0400659 error = suspend_nvs_alloc();
Rafael J. Wysocki3f4b0ef2008-10-26 20:52:15 +0100660 if (!error)
661 acpi_target_sleep_state = ACPI_STATE_S4;
662 }
663 return error;
664}
665
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200666/*
667 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
668 * been requested.
669 */
Lionel Debroux073ef1f2010-11-09 21:48:49 +0100670static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200671 .begin = acpi_hibernation_begin_old,
672 .end = acpi_pm_end,
Rafael J. Wysockic5f7a1b2010-07-02 00:14:09 +0200673 .pre_snapshot = acpi_pm_pre_suspend,
Rafael J. Wysockid5a64512010-04-09 01:39:40 +0200674 .prepare = acpi_pm_freeze,
Matthew Garrett2a6b6972010-05-28 16:32:15 -0400675 .finish = acpi_pm_finish,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200676 .enter = acpi_hibernation_enter,
677 .leave = acpi_hibernation_leave,
Rafael J. Wysockid5a64512010-04-09 01:39:40 +0200678 .pre_restore = acpi_pm_freeze,
679 .restore_cleanup = acpi_pm_thaw,
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200680 .recover = acpi_pm_finish,
681};
682#endif /* CONFIG_HIBERNATION */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700683
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200684int acpi_suspend(u32 acpi_state)
685{
686 suspend_state_t states[] = {
687 [1] = PM_SUSPEND_STANDBY,
688 [3] = PM_SUSPEND_MEM,
689 [5] = PM_SUSPEND_MAX
690 };
691
692 if (acpi_state < 6 && states[acpi_state])
693 return pm_suspend(states[acpi_state]);
694 if (acpi_state == 4)
695 return hibernate();
696 return -EINVAL;
697}
698
Rafael J. Wysockiaa338602011-02-11 00:06:54 +0100699#ifdef CONFIG_PM
Shaohua Lifd4aff12007-07-17 22:40:25 +0200700/**
701 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
702 * in the system sleep state given by %acpi_target_sleep_state
David Brownell2fe2de52008-06-05 01:15:40 +0200703 * @dev: device to examine; its driver model wakeup flags control
704 * whether it should be able to wake up the system
Shaohua Lifd4aff12007-07-17 22:40:25 +0200705 * @d_min_p: used to store the upper limit of allowed states range
Huang Yingee85f542012-06-23 10:23:48 +0800706 * @d_max_in: specify the lowest allowed states
707 * Return value: preferred power state of the device on success, -ENODEV
708 * (ie. if there's no 'struct acpi_device' for @dev) or -EINVAL on failure
Shaohua Lifd4aff12007-07-17 22:40:25 +0200709 *
710 * Find the lowest power (highest number) ACPI device power state that
711 * device @dev can be in while the system is in the sleep state represented
712 * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
713 * able to wake up the system from this sleep state. If @d_min_p is set,
714 * the highest power (lowest number) device power state of @dev allowed
715 * in this system sleep state is stored at the location pointed to by it.
716 *
717 * The caller must ensure that @dev is valid before using this function.
718 * The caller is also responsible for figuring out if the device is
719 * supposed to be able to wake up the system and passing this information
720 * via @wake.
721 */
722
Huang Yingee85f542012-06-23 10:23:48 +0800723int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
Shaohua Lifd4aff12007-07-17 22:40:25 +0200724{
725 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
726 struct acpi_device *adev;
727 char acpi_method[] = "_SxD";
Matthew Wilcox27663c52008-10-10 02:22:59 -0400728 unsigned long long d_min, d_max;
Shaohua Lifd4aff12007-07-17 22:40:25 +0200729
Huang Yingee85f542012-06-23 10:23:48 +0800730 if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3)
731 return -EINVAL;
Shaohua Lifd4aff12007-07-17 22:40:25 +0200732 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
Shaohua Liead77592007-08-23 15:01:13 +0800733 printk(KERN_DEBUG "ACPI handle has no context!\n");
Shaohua Lifd4aff12007-07-17 22:40:25 +0200734 return -ENODEV;
735 }
736
737 acpi_method[2] = '0' + acpi_target_sleep_state;
738 /*
Huang Yingee85f542012-06-23 10:23:48 +0800739 * If the sleep state is S0, the lowest limit from ACPI is D3,
740 * but if the device has _S0W, we will use the value from _S0W
741 * as the lowest limit from ACPI. Finally, we will constrain
742 * the lowest limit with the specified one.
Shaohua Lifd4aff12007-07-17 22:40:25 +0200743 */
744 d_min = ACPI_STATE_D0;
745 d_max = ACPI_STATE_D3;
746
747 /*
748 * If present, _SxD methods return the minimum D-state (highest power
749 * state) we can use for the corresponding S-states. Otherwise, the
750 * minimum D-state is D0 (ACPI 3.x).
751 *
752 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
753 * provided -- that's our fault recovery, we ignore retval.
754 */
755 if (acpi_target_sleep_state > ACPI_STATE_S0)
756 acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
757
758 /*
759 * If _PRW says we can wake up the system from the target sleep state,
760 * the D-state returned by _SxD is sufficient for that (we assume a
761 * wakeup-aware driver if wake is set). Still, if _SxW exists
762 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
763 * can wake the system. _S0W may be valid, too.
764 */
765 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
Rafael J. Wysockidbe9a2e2012-05-29 21:21:07 +0200766 (device_may_wakeup(dev) && adev->wakeup.flags.valid &&
767 adev->wakeup.sleep_state >= acpi_target_sleep_state)) {
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100768 acpi_status status;
769
Shaohua Lifd4aff12007-07-17 22:40:25 +0200770 acpi_method[3] = 'W';
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100771 status = acpi_evaluate_integer(handle, acpi_method, NULL,
772 &d_max);
773 if (ACPI_FAILURE(status)) {
Rafael J. Wysocki761afb82010-10-14 23:24:13 +0200774 if (acpi_target_sleep_state != ACPI_STATE_S0 ||
775 status != AE_NOT_FOUND)
776 d_max = d_min;
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100777 } else if (d_max < d_min) {
778 /* Warn the user of the broken DSDT */
779 printk(KERN_WARNING "ACPI: Wrong value from %s\n",
780 acpi_method);
781 /* Sanitize it */
Shaohua Lifd4aff12007-07-17 22:40:25 +0200782 d_min = d_max;
Rafael J. Wysockiad3399c2008-01-11 00:10:38 +0100783 }
Shaohua Lifd4aff12007-07-17 22:40:25 +0200784 }
785
Huang Yingee85f542012-06-23 10:23:48 +0800786 if (d_max_in < d_min)
787 return -EINVAL;
Shaohua Lifd4aff12007-07-17 22:40:25 +0200788 if (d_min_p)
789 *d_min_p = d_min;
Huang Yingee85f542012-06-23 10:23:48 +0800790 /* constrain d_max with specified lowest limit (max number) */
791 if (d_max > d_max_in) {
792 for (d_max = d_max_in; d_max > d_min; d_max--) {
793 if (adev->power.states[d_max].flags.valid)
794 break;
795 }
796 }
Shaohua Lifd4aff12007-07-17 22:40:25 +0200797 return d_max;
798}
Lin Ming3bd46602012-06-25 16:13:06 +0800799EXPORT_SYMBOL(acpi_pm_device_sleep_state);
Rafael J. Wysockiaa338602011-02-11 00:06:54 +0100800#endif /* CONFIG_PM */
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200801
Rafael J. Wysocki761afb82010-10-14 23:24:13 +0200802#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200803/**
Lin Mingb24e5092012-03-27 15:43:25 +0800804 * acpi_pm_device_run_wake - Enable/disable wake-up for given device.
805 * @phys_dev: Device to enable/disable the platform to wake-up the system for.
806 * @enable: Whether enable or disable the wake-up functionality.
807 *
808 * Find the ACPI device object corresponding to @pci_dev and try to
809 * enable/disable the GPE associated with it.
810 */
811int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
812{
813 struct acpi_device *dev;
814 acpi_handle handle;
815
816 if (!device_run_wake(phys_dev))
817 return -EINVAL;
818
819 handle = DEVICE_ACPI_HANDLE(phys_dev);
820 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev))) {
821 dev_dbg(phys_dev, "ACPI handle has no context in %s!\n",
822 __func__);
823 return -ENODEV;
824 }
825
826 if (enable) {
827 acpi_enable_wakeup_device_power(dev, ACPI_STATE_S0);
828 acpi_enable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number);
829 } else {
830 acpi_disable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number);
831 acpi_disable_wakeup_device_power(dev);
832 }
833
834 return 0;
835}
Lin Ming3bd46602012-06-25 16:13:06 +0800836EXPORT_SYMBOL(acpi_pm_device_run_wake);
Lin Mingb24e5092012-03-27 15:43:25 +0800837
838/**
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200839 * acpi_pm_device_sleep_wake - enable or disable the system wake-up
840 * capability of given device
841 * @dev: device to handle
842 * @enable: 'true' - enable, 'false' - disable the wake-up capability
843 */
844int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
845{
846 acpi_handle handle;
847 struct acpi_device *adev;
Rafael J. Wysockidf8db912009-09-08 23:13:49 +0200848 int error;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200849
Rafael J. Wysocki0baed8d2009-09-08 23:16:24 +0200850 if (!device_can_wakeup(dev))
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200851 return -EINVAL;
852
853 handle = DEVICE_ACPI_HANDLE(dev);
854 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
Rafael J. Wysockidf8db912009-09-08 23:13:49 +0200855 dev_dbg(dev, "ACPI handle has no context in %s!\n", __func__);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200856 return -ENODEV;
857 }
858
Rafael J. Wysockie8b6f972010-06-25 01:18:39 +0200859 error = enable ?
860 acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) :
861 acpi_disable_wakeup_device_power(adev);
Rafael J. Wysockidf8db912009-09-08 23:13:49 +0200862 if (!error)
863 dev_info(dev, "wake-up capability %s by ACPI\n",
864 enable ? "enabled" : "disabled");
865
866 return error;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200867}
Rafael J. Wysocki761afb82010-10-14 23:24:13 +0200868#endif /* CONFIG_PM_SLEEP */
Shaohua Lifd4aff12007-07-17 22:40:25 +0200869
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400870static void acpi_power_off_prepare(void)
871{
872 /* Prepare to power off the system */
873 acpi_sleep_prepare(ACPI_STATE_S5);
Lin Ming3d97e422008-12-16 16:57:46 +0800874 acpi_disable_all_gpes();
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400875}
876
877static void acpi_power_off(void)
878{
879 /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
Frank Seidel4d939152009-02-04 17:03:07 +0100880 printk(KERN_DEBUG "%s called\n", __func__);
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400881 local_irq_disable();
Len Brown3f6f49c2012-07-26 20:08:54 -0400882 acpi_enter_sleep_state(ACPI_STATE_S5);
Len Brown96f15ef2009-04-17 23:32:20 -0400883}
884
Alexey Starikovskiyaafbcd12007-02-10 01:32:16 -0500885int __init acpi_sleep_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200887 acpi_status status;
888 u8 type_a, type_b;
889#ifdef CONFIG_SUSPEND
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500890 int i = 0;
Carlos Corbachoe41fb7c2008-07-23 21:28:43 -0700891
892 dmi_check_system(acpisleep_dmi_table);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200893#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 if (acpi_disabled)
896 return 0;
897
Frans Pop5a50fe72007-09-20 22:27:44 +0200898 sleep_states[ACPI_STATE_S0] = 1;
899 printk(KERN_INFO PREFIX "(supports S0");
900
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200901#ifdef CONFIG_SUSPEND
Frans Pop5a50fe72007-09-20 22:27:44 +0200902 for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
904 if (ACPI_SUCCESS(status)) {
905 sleep_states[i] = 1;
Kay Sieversbe964472012-05-08 17:24:20 +0200906 printk(KERN_CONT " S%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200910 suspend_set_ops(old_suspend_ordering ?
911 &acpi_suspend_ops_old : &acpi_suspend_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200912#endif
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700913
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200914#ifdef CONFIG_HIBERNATION
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200915 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
916 if (ACPI_SUCCESS(status)) {
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200917 hibernation_set_ops(old_suspend_ordering ?
918 &acpi_hibernation_ops_old : &acpi_hibernation_ops);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200919 sleep_states[ACPI_STATE_S4] = 1;
Kay Sieversbe964472012-05-08 17:24:20 +0200920 printk(KERN_CONT " S4");
Shaohua Libdfe6b72008-07-23 21:28:41 -0700921 if (!nosigcheck) {
Lin Ming3d97e422008-12-16 16:57:46 +0800922 acpi_get_table(ACPI_SIG_FACS, 1,
Shaohua Libdfe6b72008-07-23 21:28:41 -0700923 (struct acpi_table_header **)&facs);
924 if (facs)
925 s4_hardware_signature =
926 facs->hardware_signature;
927 }
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200928 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700929#endif
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400930 status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
931 if (ACPI_SUCCESS(status)) {
932 sleep_states[ACPI_STATE_S5] = 1;
Kay Sieversbe964472012-05-08 17:24:20 +0200933 printk(KERN_CONT " S5");
Alexey Starikovskiyf216cc32007-09-20 21:32:35 +0400934 pm_power_off_prepare = acpi_power_off_prepare;
935 pm_power_off = acpi_power_off;
936 }
Kay Sieversbe964472012-05-08 17:24:20 +0200937 printk(KERN_CONT ")\n");
Zhao Yakuie49f7112008-08-12 10:20:22 +0800938 /*
939 * Register the tts_notifier to reboot notifier list so that the _TTS
940 * object can also be evaluated when the system enters S5.
941 */
942 register_reboot_notifier(&tts_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return 0;
944}