blob: 7249ba2b7a2740affaee5ba54d82ca4d27c2cc1c [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
26extern void do_suspend_lowlevel_s4bios(void);
27extern void do_suspend_lowlevel(void);
28
29static u32 acpi_suspend_states[] = {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050030 [PM_SUSPEND_ON] = ACPI_STATE_S0,
31 [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
32 [PM_SUSPEND_MEM] = ACPI_STATE_S3,
33 [PM_SUSPEND_DISK] = ACPI_STATE_S4,
34 [PM_SUSPEND_MAX] = ACPI_STATE_S5
Linus Torvalds1da177e2005-04-16 15:20:36 -070035};
36
37static int init_8259A_after_S1;
38
39/**
40 * acpi_pm_prepare - Do preliminary suspend work.
41 * @pm_state: suspend state we're entering.
42 *
43 * Make sure we support the state. If we do, and we need it, set the
44 * firmware waking vector and do arch-specific nastiness to get the
45 * wakeup code to the waking vector.
46 */
47
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050048extern int acpi_sleep_prepare(u32 acpi_state);
49extern void acpi_power_off(void);
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static int acpi_pm_prepare(suspend_state_t pm_state)
52{
53 u32 acpi_state = acpi_suspend_states[pm_state];
54
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050055 if (!sleep_states[acpi_state]) {
56 printk("acpi_pm_prepare does not support %d \n", pm_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050059 return acpi_sleep_prepare(acpi_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/**
63 * acpi_pm_enter - Actually enter a sleep state.
64 * @pm_state: State we're entering.
65 *
66 * Flush caches and go to sleep. For STR or STD, we have to call
67 * arch-specific assembly, which in turn call acpi_enter_sleep_state().
68 * It's unfortunate, but it works. Please fix if you're feeling frisky.
69 */
70
71static int acpi_pm_enter(suspend_state_t pm_state)
72{
73 acpi_status status = AE_OK;
74 unsigned long flags = 0;
75 u32 acpi_state = acpi_suspend_states[pm_state];
76
77 ACPI_FLUSH_CPU_CACHE();
78
79 /* Do arch specific saving of state. */
80 if (pm_state > PM_SUSPEND_STANDBY) {
81 int error = acpi_save_state_mem();
82 if (error)
83 return error;
84 }
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 local_irq_save(flags);
87 acpi_enable_wakeup_device(acpi_state);
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -050088 switch (pm_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 case PM_SUSPEND_STANDBY:
90 barrier();
91 status = acpi_enter_sleep_state(acpi_state);
92 break;
93
94 case PM_SUSPEND_MEM:
95 do_suspend_lowlevel();
96 break;
97
98 case PM_SUSPEND_DISK:
99 if (acpi_pm_ops.pm_disk_mode == PM_DISK_PLATFORM)
100 status = acpi_enter_sleep_state(acpi_state);
101 else
102 do_suspend_lowlevel_s4bios();
103 break;
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500104 case PM_SUSPEND_MAX:
105 acpi_power_off();
106 break;
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 default:
109 return -EINVAL;
110 }
111 local_irq_restore(flags);
112 printk(KERN_DEBUG "Back to C!\n");
113
114 /* restore processor state
115 * We should only be here if we're coming back from STR or STD.
116 * And, in the case of the latter, the memory image should have already
117 * been loaded from disk.
118 */
119 if (pm_state > PM_SUSPEND_STANDBY)
120 acpi_restore_state_mem();
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/**
126 * acpi_pm_finish - Finish up suspend sequence.
127 * @pm_state: State we're coming out of.
128 *
129 * This is called after we wake back up (or if entering the sleep state
130 * failed).
131 */
132
133static int acpi_pm_finish(suspend_state_t pm_state)
134{
135 u32 acpi_state = acpi_suspend_states[pm_state];
136
137 acpi_leave_sleep_state(acpi_state);
138 acpi_disable_wakeup_device(acpi_state);
139
140 /* reset firmware waking vector */
141 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
142
143 if (init_8259A_after_S1) {
144 printk("Broken toshiba laptop -> kicking interrupts\n");
145 init_8259A(0);
146 }
147 return 0;
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150int acpi_suspend(u32 acpi_state)
151{
152 suspend_state_t states[] = {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500153 [1] = PM_SUSPEND_STANDBY,
154 [3] = PM_SUSPEND_MEM,
155 [4] = PM_SUSPEND_DISK,
156 [5] = PM_SUSPEND_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 };
158
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500159 if (acpi_state < 6 && states[acpi_state])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return pm_suspend(states[acpi_state]);
161 return -EINVAL;
162}
163
164static struct pm_ops acpi_pm_ops = {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500165 .prepare = acpi_pm_prepare,
166 .enter = acpi_pm_enter,
167 .finish = acpi_pm_finish,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168};
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/*
171 * Toshiba fails to preserve interrupts over S1, reinitialization
172 * of 8259 is needed after S1 resume.
173 */
174static int __init init_ints_after_s1(struct dmi_system_id *d)
175{
176 printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
177 init_8259A_after_S1 = 1;
178 return 0;
179}
180
181static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
182 {
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500183 .callback = init_ints_after_s1,
184 .ident = "Toshiba Satellite 4030cdt",
185 .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
186 },
187 {},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
190static int __init acpi_sleep_init(void)
191{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500192 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 dmi_check_system(acpisleep_dmi_table);
195
196 if (acpi_disabled)
197 return 0;
198
199 printk(KERN_INFO PREFIX "(supports");
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500200 for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 acpi_status status;
202 u8 type_a, type_b;
203 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
204 if (ACPI_SUCCESS(status)) {
205 sleep_states[i] = 1;
206 printk(" S%d", i);
207 }
208 if (i == ACPI_STATE_S4) {
209 if (acpi_gbl_FACS->S4bios_f) {
210 sleep_states[i] = 1;
211 printk(" S4bios");
212 acpi_pm_ops.pm_disk_mode = PM_DISK_FIRMWARE;
213 }
214 if (sleep_states[i])
215 acpi_pm_ops.pm_disk_mode = PM_DISK_PLATFORM;
216 }
217 }
218 printk(")\n");
219
220 pm_set_ops(&acpi_pm_ops);
221 return 0;
222}
223
224late_initcall(acpi_sleep_init);