main.c 5.18 KB
Newer Older
1 2 3
/*
 * sleep.c - ACPI sleep support.
 *
4 5 6 7 8
 * Copyright (c) 2000-2003 Patrick Mochel
 * Copyright (c) 2003 Open Source Development Lab
 *
 * This file is released under the GPLv2.
 *
9 10 11 12
 */

#include <linux/delay.h>
#include <linux/irq.h>
13
#include <linux/dmi.h>
14
#include <linux/device.h>
15
#include <linux/suspend.h>
16
#include <acpi/acpi_bus.h>
17
#include <acpi/acpi_drivers.h>
18
#include "sleep.h"
19

20
u8 sleep_states[ACPI_S_STATE_COUNT];
21

22 23
static struct pm_ops acpi_pm_ops;

24 25
extern void do_suspend_lowlevel_s4bios(void);
extern void do_suspend_lowlevel(void);
26

27 28 29 30 31 32
static u32 acpi_suspend_states[] = {
	[PM_SUSPEND_ON]		= ACPI_STATE_S0,
	[PM_SUSPEND_STANDBY]	= ACPI_STATE_S1,
	[PM_SUSPEND_MEM]	= ACPI_STATE_S3,
	[PM_SUSPEND_DISK]	= ACPI_STATE_S4,
};
33

34 35
static int init_8259A_after_S1;

36
/**
37
 *	acpi_pm_prepare - Do preliminary suspend work.
38
 *	@pm_state:		suspend state we're entering.
39
 *
40 41 42
 *	Make sure we support the state. If we do, and we need it, set the
 *	firmware waking vector and do arch-specific nastiness to get the 
 *	wakeup code to the waking vector. 
43 44
 */

45
static int acpi_pm_prepare(u32 pm_state)
46
{
47
	u32 acpi_state = acpi_suspend_states[pm_state];
48

49 50
	if (!sleep_states[acpi_state])
		return -EPERM;
51

52 53 54
	/* do we have a wakeup address for S2 and S3? */
	/* Here, we support only S4BIOS, those we set the wakeup address */
	/* S4OS is only supported for now via swsusp.. */
55
	if (pm_state == PM_SUSPEND_MEM || pm_state == PM_SUSPEND_DISK) {
56 57 58 59
		if (!acpi_wakeup_address)
			return -EFAULT;
		acpi_set_firmware_waking_vector(
			(acpi_physical_address) acpi_wakeup_address);
60
	}
61
	ACPI_FLUSH_CPU_CACHE();
62
	acpi_enable_wakeup_device_prep(acpi_state);
63 64
	acpi_enter_sleep_state_prep(acpi_state);
	return 0;
65 66 67
}


68 69
/**
 *	acpi_pm_enter - Actually enter a sleep state.
70
 *	@pm_state:		State we're entering.
71 72 73 74 75 76
 *
 *	Flush caches and go to sleep. For STR or STD, we have to call 
 *	arch-specific assembly, which in turn call acpi_enter_sleep_state().
 *	It's unfortunate, but it works. Please fix if you're feeling frisky.
 */

77
static int acpi_pm_enter(u32 pm_state)
78
{
79 80
	acpi_status status = AE_OK;
	unsigned long flags = 0;
81
	u32 acpi_state = acpi_suspend_states[pm_state];
82

83
	ACPI_FLUSH_CPU_CACHE();
84 85

	/* Do arch specific saving of state. */
86
	if (pm_state > PM_SUSPEND_STANDBY) {
87 88 89 90 91 92
		int error = acpi_save_state_mem();
		if (error)
			return error;
	}


93
	local_irq_save(flags);
94
	acpi_enable_wakeup_device(acpi_state);
95
	switch (pm_state)
96
	{
97
	case PM_SUSPEND_STANDBY:
98
		barrier();
99
		status = acpi_enter_sleep_state(acpi_state);
100 101
		break;

102
	case PM_SUSPEND_MEM:
103
		do_suspend_lowlevel();
104
		break;
105

106
	case PM_SUSPEND_DISK:
107 108 109
		if (acpi_pm_ops.pm_disk_mode == PM_DISK_PLATFORM)
			status = acpi_enter_sleep_state(acpi_state);
		else
110
			do_suspend_lowlevel_s4bios();
111 112
		break;
	default:
113
		return -EINVAL;
114 115
	}
	local_irq_restore(flags);
116
	printk(KERN_DEBUG "Back to C!\n");
117

118 119 120 121 122
	/* restore processor state
	 * We should only be here if we're coming back from STR or STD.
	 * And, in the case of the latter, the memory image should have already
	 * been loaded from disk.
	 */
123
	if (pm_state > PM_SUSPEND_STANDBY)
124 125 126
		acpi_restore_state_mem();


127
	return ACPI_SUCCESS(status) ? 0 : -EFAULT;
128 129
}

130 131 132

/**
 *	acpi_pm_finish - Finish up suspend sequence.
133
 *	@pm_state:		State we're coming out of.
134 135 136 137 138
 *
 *	This is called after we wake back up (or if entering the sleep state
 *	failed). 
 */

139
static int acpi_pm_finish(u32 pm_state)
140
{
141 142 143
	u32 acpi_state = acpi_suspend_states[pm_state];

	acpi_leave_sleep_state(acpi_state);
144
	acpi_disable_wakeup_device(acpi_state);
145 146

	/* reset firmware waking vector */
147
	acpi_set_firmware_waking_vector((acpi_physical_address) 0);
148

149
	if (init_8259A_after_S1) {
150 151 152 153
		printk("Broken toshiba laptop -> kicking interrupts\n");
		init_8259A(0);
	}
	return 0;
154 155
}

156

157 158 159 160 161 162 163 164 165 166 167 168 169
int acpi_suspend(u32 acpi_state)
{
	u32 states[] = {
		[1]	= PM_SUSPEND_STANDBY,
		[3]	= PM_SUSPEND_MEM,
		[4]	= PM_SUSPEND_DISK,
	};

	if (acpi_state <= 4 && states[acpi_state])
		return pm_suspend(states[acpi_state]);
	return -EINVAL;
}

170 171 172 173 174 175
static struct pm_ops acpi_pm_ops = {
	.prepare	= acpi_pm_prepare,
	.enter		= acpi_pm_enter,
	.finish		= acpi_pm_finish,
};

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

/*
 * Toshiba fails to preserve interrupts over S1, reinitialization
 * of 8259 is needed after S1 resume.
 */
static int __init init_ints_after_s1(struct dmi_system_id *d)
{
	printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
	init_8259A_after_S1 = 1;
	return 0;
}

static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
	{
		.callback = init_ints_after_s1,
		.ident = "Toshiba Satellite 4030cdt",
		.matches = { DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"), },
	},
	{ },
};

197 198 199 200
static int __init acpi_sleep_init(void)
{
	int			i = 0;

201 202
	dmi_check_system(acpisleep_dmi_table);

203
	if (acpi_disabled)
204
		return 0;
205

206
	printk(KERN_INFO PREFIX "(supports");
207
	for (i=0; i < ACPI_S_STATE_COUNT; i++) {
208
		acpi_status status;
209 210 211 212 213 214
		u8 type_a, type_b;
		status = acpi_get_sleep_type_data(i, &type_a, &type_b);
		if (ACPI_SUCCESS(status)) {
			sleep_states[i] = 1;
			printk(" S%d", i);
		}
215 216 217 218 219
		if (i == ACPI_STATE_S4) {
			if (acpi_gbl_FACS->S4bios_f) {
				sleep_states[i] = 1;
				printk(" S4bios");
				acpi_pm_ops.pm_disk_mode = PM_DISK_FIRMWARE;
220 221
			}
			if (sleep_states[i])
222
				acpi_pm_ops.pm_disk_mode = PM_DISK_PLATFORM;
223
		}
224 225 226
	}
	printk(")\n");

227 228
	pm_set_ops(&acpi_pm_ops);
	return 0;
229 230
}

231
late_initcall(acpi_sleep_init);