blob: 37a0930fc0a6ac538fbb2407e3ebb6f71a203753 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <acpi/acpi_bus.h>
19#include <acpi/acpi_drivers.h>
20#include "sleep.h"
21
22u8 sleep_states[ACPI_S_STATE_COUNT];
23
24static struct pm_ops acpi_pm_ops;
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026extern void do_suspend_lowlevel(void);
27
28static u32 acpi_suspend_states[] = {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050029 [PM_SUSPEND_ON] = ACPI_STATE_S0,
30 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
31 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
32 [PM_SUSPEND_DISK] = ACPI_STATE_S4,
33 [PM_SUSPEND_MAX] = ACPI_STATE_S5
Linus Torvalds1da177e2005-04-16 15:20:36 -070034};
35
36static int init_8259A_after_S1;
37
38/**
39 * acpi_pm_prepare - Do preliminary suspend work.
40 * @pm_state: suspend state we're entering.
41 *
42 * Make sure we support the state. If we do, and we need it, set the
43 * firmware waking vector and do arch-specific nastiness to get the
44 * wakeup code to the waking vector.
45 */
46
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050047extern int acpi_sleep_prepare(u32 acpi_state);
48extern void acpi_power_off(void);
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static int acpi_pm_prepare(suspend_state_t pm_state)
51{
52 u32 acpi_state = acpi_suspend_states[pm_state];
53
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050054 if (!sleep_states[acpi_state]) {
55 printk("acpi_pm_prepare does not support %d \n", pm_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 }
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050058 return acpi_sleep_prepare(acpi_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/**
62 * acpi_pm_enter - Actually enter a sleep state.
63 * @pm_state: State we're entering.
64 *
65 * Flush caches and go to sleep. For STR or STD, we have to call
66 * arch-specific assembly, which in turn call acpi_enter_sleep_state().
67 * It's unfortunate, but it works. Please fix if you're feeling frisky.
68 */
69
70static int acpi_pm_enter(suspend_state_t pm_state)
71{
72 acpi_status status = AE_OK;
73 unsigned long flags = 0;
74 u32 acpi_state = acpi_suspend_states[pm_state];
75
76 ACPI_FLUSH_CPU_CACHE();
77
78 /* Do arch specific saving of state. */
79 if (pm_state > PM_SUSPEND_STANDBY) {
80 int error = acpi_save_state_mem();
81 if (error)
82 return error;
83 }
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 local_irq_save(flags);
86 acpi_enable_wakeup_device(acpi_state);
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050087 switch (pm_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 case PM_SUSPEND_STANDBY:
89 barrier();
90 status = acpi_enter_sleep_state(acpi_state);
91 break;
92
93 case PM_SUSPEND_MEM:
94 do_suspend_lowlevel();
95 break;
96
97 case PM_SUSPEND_DISK:
98 if (acpi_pm_ops.pm_disk_mode == PM_DISK_PLATFORM)
99 status = acpi_enter_sleep_state(acpi_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 break;
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500101 case PM_SUSPEND_MAX:
102 acpi_power_off();
103 break;
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 default:
106 return -EINVAL;
107 }
Arnaud Patard872d83d2006-04-27 05:25:00 -0400108
109 /* ACPI 3.0 specs (P62) says that it's the responsabilty
110 * of the OSPM to clear the status bit [ implying that the
111 * POWER_BUTTON event should not reach userspace ]
112 */
113 if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
114 acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 local_irq_restore(flags);
117 printk(KERN_DEBUG "Back to C!\n");
118
119 /* restore processor state
120 * We should only be here if we're coming back from STR or STD.
121 * And, in the case of the latter, the memory image should have already
122 * been loaded from disk.
123 */
124 if (pm_state > PM_SUSPEND_STANDBY)
125 acpi_restore_state_mem();
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
128}
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/**
131 * acpi_pm_finish - Finish up suspend sequence.
132 * @pm_state: State we're coming out of.
133 *
134 * This is called after we wake back up (or if entering the sleep state
135 * failed).
136 */
137
138static int acpi_pm_finish(suspend_state_t pm_state)
139{
140 u32 acpi_state = acpi_suspend_states[pm_state];
141
142 acpi_leave_sleep_state(acpi_state);
143 acpi_disable_wakeup_device(acpi_state);
144
145 /* reset firmware waking vector */
146 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
147
148 if (init_8259A_after_S1) {
149 printk("Broken toshiba laptop -> kicking interrupts\n");
150 init_8259A(0);
151 }
152 return 0;
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155int acpi_suspend(u32 acpi_state)
156{
157 suspend_state_t states[] = {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500158 [1] = PM_SUSPEND_STANDBY,
159 [3] = PM_SUSPEND_MEM,
160 [4] = PM_SUSPEND_DISK,
161 [5] = PM_SUSPEND_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 };
163
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500164 if (acpi_state < 6 && states[acpi_state])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return pm_suspend(states[acpi_state]);
166 return -EINVAL;
167}
168
Shaohua Lieb9289e2005-10-30 15:00:01 -0800169static int acpi_pm_state_valid(suspend_state_t pm_state)
170{
171 u32 acpi_state = acpi_suspend_states[pm_state];
172
173 return sleep_states[acpi_state];
174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static struct pm_ops acpi_pm_ops = {
Shaohua Lieb9289e2005-10-30 15:00:01 -0800177 .valid = acpi_pm_state_valid,
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500178 .prepare = acpi_pm_prepare,
179 .enter = acpi_pm_enter,
180 .finish = acpi_pm_finish,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181};
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/*
184 * Toshiba fails to preserve interrupts over S1, reinitialization
185 * of 8259 is needed after S1 resume.
186 */
187static int __init init_ints_after_s1(struct dmi_system_id *d)
188{
189 printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
190 init_8259A_after_S1 = 1;
191 return 0;
192}
193
194static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
195 {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500196 .callback = init_ints_after_s1,
197 .ident = "Toshiba Satellite 4030cdt",
198 .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
199 },
200 {},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201};
202
Alexey Starikovskiyaafbcd12007-02-10 01:32:16 -0500203int __init acpi_sleep_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500205 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 dmi_check_system(acpisleep_dmi_table);
208
209 if (acpi_disabled)
210 return 0;
211
212 printk(KERN_INFO PREFIX "(supports");
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500213 for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 acpi_status status;
215 u8 type_a, type_b;
216 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
217 if (ACPI_SUCCESS(status)) {
218 sleep_states[i] = 1;
219 printk(" S%d", i);
220 }
221 if (i == ACPI_STATE_S4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (sleep_states[i])
223 acpi_pm_ops.pm_disk_mode = PM_DISK_PLATFORM;
224 }
225 }
226 printk(")\n");
227
228 pm_set_ops(&acpi_pm_ops);
229 return 0;
230}
231