2 * acpi_system.c - ACPI System Driver ($Revision: 57 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 /* Modified for ReactOS and latest ACPICA
27 * Copyright (C)2009 Samuel Serapion
35 ACPI_STATUS
acpi_system_save_state(UINT32
);
37 #define _COMPONENT ACPI_SYSTEM_COMPONENT
38 ACPI_MODULE_NAME ("acpi_system")
40 #define PREFIX "ACPI: "
42 static int acpi_system_add (struct acpi_device
*device
);
43 static int acpi_system_remove (struct acpi_device
*device
, int type
);
45 ACPI_STATUS
acpi_suspend (UINT32 state
);
47 static struct acpi_driver acpi_system_driver
= {
49 ACPI_SYSTEM_DRIVER_NAME
,
54 {acpi_system_add
, acpi_system_remove
}
60 UINT8 states
[ACPI_S_STATE_COUNT
];
66 struct acpi_device
*device
)
69 ACPI_STATUS status
= AE_OK
;
70 struct acpi_system
*system
= NULL
;
73 ACPI_FUNCTION_TRACE("acpi_system_add");
78 system
= ExAllocatePoolWithTag(NonPagedPool
,sizeof(struct acpi_system
),'IPCA');
81 memset(system
, 0, sizeof(struct acpi_system
));
83 system
->handle
= device
->handle
;
84 sprintf(acpi_device_name(device
), "%s", ACPI_SYSTEM_DEVICE_NAME
);
85 sprintf(acpi_device_class(device
), "%s", ACPI_SYSTEM_CLASS
);
86 acpi_driver_data(device
) = system
;
88 DPRINT("%s [%s] (supports",
89 acpi_device_name(device
), acpi_device_bid(device
));
90 for (i
=0; i
<ACPI_S_STATE_COUNT
; i
++) {
92 status
= AcpiGetSleepTypeData(i
, &type_a
, &type_b
);
95 if (/*AcpiGbl_FACS->S4bios_f &&*/
96 0 != AcpiGbl_FADT
.SmiCommand
) {
98 system
->states
[i
] = 1;
102 if (ACPI_SUCCESS(status
)) {
103 system
->states
[i
] = 1;
110 // /* Install the soft-off (S5) handler. */
111 // if (system->states[ACPI_STATE_S5]) {
112 // pm_power_off = acpi_power_off;
113 // register_sysrq_key('o', &sysrq_acpi_poweroff_op);
118 ExFreePoolWithTag(system
, 'IPCA');
120 return_VALUE(result
);
125 struct acpi_device
*device
,
128 struct acpi_system
*system
= NULL
;
130 ACPI_FUNCTION_TRACE("acpi_system_remove");
132 if (!device
|| !acpi_driver_data(device
))
135 system
= (struct acpi_system
*) acpi_driver_data(device
);
138 // /* Remove the soft-off (S5) handler. */
139 // if (system->states[ACPI_STATE_S5]) {
140 // unregister_sysrq_key('o', &sysrq_acpi_poweroff_op);
141 // pm_power_off = NULL;
146 ExFreePoolWithTag(system
, 'IPCA');
152 * acpi_system_restore_state - OS-specific restoration of state
153 * @state: sleep state we're exiting
155 * Note that if we're coming back from S4, the memory image should have
156 * already been loaded from the disk and is already in place. (Otherwise how
157 * else would we be here?).
160 acpi_system_restore_state(
164 * We should only be here if we're coming back from STR or STD.
165 * And, in the case of the latter, the memory image should have already
166 * been loaded from disk.
168 if (state
> ACPI_STATE_S1
) {
169 //acpi_restore_state_mem();
171 /* Do _early_ resume for irqs. Required by
174 /* TBD: call arch dependant reinitialization of the
180 /* wait for power to come back */
181 KeStallExecutionProcessor(100);
185 /* Be really sure that irqs are disabled. */
186 //ACPI_DISABLE_IRQS();
188 /* Wait a little again, just in case... */
189 KeStallExecutionProcessor(10);
191 /* enable interrupts once again */
192 //ACPI_ENABLE_IRQS();
194 /* turn all the devices back on */
195 //if (state > ACPI_STATE_S1)
196 //pm_send_all(PM_RESUME, (void *)0);
203 * acpi_system_save_state - save OS specific state and power down devices
204 * @state: sleep state we're entering.
206 * This handles saving all context to memory, and possibly disk.
207 * First, we call to the device driver layer to save device state.
208 * Once we have that, we save whatevery processor and kernel state we
210 * If we're entering S4, we then write the memory image to disk.
212 * Only then it is safe for us to power down devices, since we may need
213 * the disks and upstream buses to write to.
216 acpi_system_save_state(
221 /* Send notification to devices that they will be suspended.
222 * If any device or driver cannot make the transition, either up
223 * or down, we'll get an error back.
225 /*if (state > ACPI_STATE_S1) {
226 error = pm_send_all(PM_SAVE_STATE, (void *)3);
231 //if (state <= ACPI_STATE_S5) {
232 // /* Tell devices to stop I/O and actually save their state.
233 // * It is theoretically possible that something could fail,
234 // * so handle that gracefully..
236 // if (state > ACPI_STATE_S1 && state != ACPI_STATE_S5) {
237 // error = pm_send_all(PM_SUSPEND, (void *)3);
239 // /* Tell devices to restore state if they have
240 // * it saved and to start taking I/O requests.
242 // pm_send_all(PM_RESUME, (void *)0);
248 ACPI_FLUSH_CPU_CACHE();
250 /* Do arch specific saving of state. */
251 if (state
> ACPI_STATE_S1
) {
252 error
= 0;//acpi_save_state_mem();
254 /* TBD: if no s4bios, write codes for
255 * acpi_save_state_disk()...
258 if (!error
&& (state
== ACPI_STATE_S4
))
259 error
= acpi_save_state_disk();
262 pm_send_all(PM_RESUME, (void *)0);
267 /* disable interrupts
268 * Note that acpi_suspend -- our caller -- will do this once we return.
269 * But, we want it done early, so we don't get any suprises during
270 * the device suspend sequence.
272 //ACPI_DISABLE_IRQS();
274 /* Unconditionally turn off devices.
275 * Obvious if we enter a sleep state.
276 * If entering S5 (soft off), this should put devices in a
280 //if (state > ACPI_STATE_S1) {
281 // error = pm_send_all(PM_SUSPEND, (void *)3);
283 // /* We're pretty screwed if we got an error from this.
284 // * We try to recover by simply calling our own restore_state
285 // * function; see above for definition.
287 // * If it's S5 though, go through with it anyway..
289 // if (error && state != ACPI_STATE_S5)
290 // acpi_system_restore_state(state);
292 return error
? AE_ERROR
: AE_OK
;
296 /****************************************************************************
298 * FUNCTION: acpi_system_suspend
300 * PARAMETERS: %state: Sleep state to enter.
302 * RETURN: ACPI_STATUS, whether or not we successfully entered and
305 * DESCRIPTION: Perform OS-specific action to enter sleep state.
306 * This is the final step in going to sleep, per spec. If we
307 * know we're coming back (i.e. not entering S5), we save the
308 * processor flags. [ We'll have to save and restore them anyway,
309 * so we use the arch-agnostic save_flags and restore_flags
310 * here.] We then set the place to return to in arch-specific
311 * globals using arch_set_return_point. Finally, we call the
312 * ACPI function to write the proper values to I/O ports.
314 ****************************************************************************/
320 ACPI_STATUS status
= AE_ERROR
;
321 //unsigned long flags = 0;
323 //local_irq_save(flags);
324 /* kernel_fpu_begin(); */
330 status
= AcpiEnterSleepState(state
);
333 //do_suspend_lowlevel_s4bios(0);
337 /* kernel_fpu_end(); */
338 //local_irq_restore(flags);
346 * acpi_suspend - OS-agnostic system suspend/resume support (S? states)
347 * @state: state we're entering
356 /* only support S1 and S5 on kernel 2.4 */
357 //if (state != ACPI_STATE_S1 && state != ACPI_STATE_S4
358 // && state != ACPI_STATE_S5)
362 //if (ACPI_STATE_S4 == state) {
363 // /* For s4bios, we need a wakeup address. */
364 // if (1 == AcpiGbl_FACS->S4bios_f &&
365 // 0 != AcpiGbl_FADT->smi_cmd) {
366 // if (!acpi_wakeup_address)
368 // AcpiSetFirmwareWakingVector((acpi_physical_address) acpi_wakeup_address);
370 // /* We don't support S4 under 2.4. Give up */
373 AcpiEnterSleepStatePrep(state
);
375 status
= AcpiEnterSleepState(state
);
376 if (!ACPI_SUCCESS(status
) && state
!= ACPI_STATE_S5
)
379 /* disable interrupts and flush caches */
381 ACPI_FLUSH_CPU_CACHE();
383 /* perform OS-specific sleep actions */
384 status
= acpi_system_suspend(state
);
386 /* Even if we failed to go to sleep, all of the devices are in an suspended
387 * mode. So, we run these unconditionaly to make sure we have a usable system
390 AcpiLeaveSleepState(state
);
391 acpi_system_restore_state(state
);
393 /* make sure interrupts are enabled */
396 /* reset firmware waking vector */
397 AcpiSetFirmwareWakingVector(0, 0);
403 acpi_system_init (void)
407 ACPI_FUNCTION_TRACE("acpi_system_init");
409 result
= acpi_bus_register_driver(&acpi_system_driver
);
411 return_VALUE(AE_NOT_FOUND
);
418 acpi_system_exit (void)
420 ACPI_FUNCTION_TRACE("acpi_system_exit");
421 acpi_bus_unregister_driver(&acpi_system_driver
);