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
)
68 ACPI_STATUS status
= AE_OK
;
69 struct acpi_system
*system
= NULL
;
72 ACPI_FUNCTION_TRACE("acpi_system_add");
77 system
= ExAllocatePoolWithTag(NonPagedPool
,sizeof(struct acpi_system
),'IPCA');
80 memset(system
, 0, sizeof(struct acpi_system
));
82 system
->handle
= device
->handle
;
83 sprintf(acpi_device_name(device
), "%s", ACPI_SYSTEM_DEVICE_NAME
);
84 sprintf(acpi_device_class(device
), "%s", ACPI_SYSTEM_CLASS
);
85 acpi_driver_data(device
) = system
;
87 DPRINT("%s [%s] (supports",
88 acpi_device_name(device
), acpi_device_bid(device
));
89 for (i
=0; i
<ACPI_S_STATE_COUNT
; i
++) {
91 status
= AcpiGetSleepTypeData(i
, &type_a
, &type_b
);
94 if (/*AcpiGbl_FACS->S4bios_f &&*/
95 0 != AcpiGbl_FADT
.SmiCommand
) {
97 system
->states
[i
] = 1;
101 if (ACPI_SUCCESS(status
)) {
102 system
->states
[i
] = 1;
109 // /* Install the soft-off (S5) handler. */
110 // if (system->states[ACPI_STATE_S5]) {
111 // pm_power_off = acpi_power_off;
112 // register_sysrq_key('o', &sysrq_acpi_poweroff_op);
121 struct acpi_device
*device
,
124 struct acpi_system
*system
= NULL
;
126 ACPI_FUNCTION_TRACE("acpi_system_remove");
128 if (!device
|| !acpi_driver_data(device
))
131 system
= (struct acpi_system
*) acpi_driver_data(device
);
134 // /* Remove the soft-off (S5) handler. */
135 // if (system->states[ACPI_STATE_S5]) {
136 // unregister_sysrq_key('o', &sysrq_acpi_poweroff_op);
137 // pm_power_off = NULL;
142 ExFreePoolWithTag(system
, 'IPCA');
148 * acpi_system_restore_state - OS-specific restoration of state
149 * @state: sleep state we're exiting
151 * Note that if we're coming back from S4, the memory image should have
152 * already been loaded from the disk and is already in place. (Otherwise how
153 * else would we be here?).
156 acpi_system_restore_state(
160 * We should only be here if we're coming back from STR or STD.
161 * And, in the case of the latter, the memory image should have already
162 * been loaded from disk.
164 if (state
> ACPI_STATE_S1
) {
165 //acpi_restore_state_mem();
167 /* Do _early_ resume for irqs. Required by
170 /* TBD: call arch dependant reinitialization of the
176 /* wait for power to come back */
177 KeStallExecutionProcessor(100);
181 /* Be really sure that irqs are disabled. */
182 //ACPI_DISABLE_IRQS();
184 /* Wait a little again, just in case... */
185 KeStallExecutionProcessor(10);
187 /* enable interrupts once again */
188 //ACPI_ENABLE_IRQS();
190 /* turn all the devices back on */
191 //if (state > ACPI_STATE_S1)
192 //pm_send_all(PM_RESUME, (void *)0);
199 * acpi_system_save_state - save OS specific state and power down devices
200 * @state: sleep state we're entering.
202 * This handles saving all context to memory, and possibly disk.
203 * First, we call to the device driver layer to save device state.
204 * Once we have that, we save whatevery processor and kernel state we
206 * If we're entering S4, we then write the memory image to disk.
208 * Only then it is safe for us to power down devices, since we may need
209 * the disks and upstream buses to write to.
212 acpi_system_save_state(
217 /* Send notification to devices that they will be suspended.
218 * If any device or driver cannot make the transition, either up
219 * or down, we'll get an error back.
221 /*if (state > ACPI_STATE_S1) {
222 error = pm_send_all(PM_SAVE_STATE, (void *)3);
227 //if (state <= ACPI_STATE_S5) {
228 // /* Tell devices to stop I/O and actually save their state.
229 // * It is theoretically possible that something could fail,
230 // * so handle that gracefully..
232 // if (state > ACPI_STATE_S1 && state != ACPI_STATE_S5) {
233 // error = pm_send_all(PM_SUSPEND, (void *)3);
235 // /* Tell devices to restore state if they have
236 // * it saved and to start taking I/O requests.
238 // pm_send_all(PM_RESUME, (void *)0);
244 ACPI_FLUSH_CPU_CACHE();
246 /* Do arch specific saving of state. */
247 if (state
> ACPI_STATE_S1
) {
248 error
= 0;//acpi_save_state_mem();
250 /* TBD: if no s4bios, write codes for
251 * acpi_save_state_disk()...
254 if (!error
&& (state
== ACPI_STATE_S4
))
255 error
= acpi_save_state_disk();
258 pm_send_all(PM_RESUME, (void *)0);
263 /* disable interrupts
264 * Note that acpi_suspend -- our caller -- will do this once we return.
265 * But, we want it done early, so we don't get any surprises during
266 * the device suspend sequence.
268 //ACPI_DISABLE_IRQS();
270 /* Unconditionally turn off devices.
271 * Obvious if we enter a sleep state.
272 * If entering S5 (soft off), this should put devices in a
276 //if (state > ACPI_STATE_S1) {
277 // error = pm_send_all(PM_SUSPEND, (void *)3);
279 // /* We're pretty screwed if we got an error from this.
280 // * We try to recover by simply calling our own restore_state
281 // * function; see above for definition.
283 // * If it's S5 though, go through with it anyway..
285 // if (error && state != ACPI_STATE_S5)
286 // acpi_system_restore_state(state);
288 return error
? AE_ERROR
: AE_OK
;
292 /****************************************************************************
294 * FUNCTION: acpi_system_suspend
296 * PARAMETERS: %state: Sleep state to enter.
298 * RETURN: ACPI_STATUS, whether or not we successfully entered and
301 * DESCRIPTION: Perform OS-specific action to enter sleep state.
302 * This is the final step in going to sleep, per spec. If we
303 * know we're coming back (i.e. not entering S5), we save the
304 * processor flags. [ We'll have to save and restore them anyway,
305 * so we use the arch-agnostic save_flags and restore_flags
306 * here.] We then set the place to return to in arch-specific
307 * globals using arch_set_return_point. Finally, we call the
308 * ACPI function to write the proper values to I/O ports.
310 ****************************************************************************/
316 ACPI_STATUS status
= AE_ERROR
;
317 //unsigned long flags = 0;
319 //local_irq_save(flags);
320 /* kernel_fpu_begin(); */
326 status
= AcpiEnterSleepState(state
);
329 //do_suspend_lowlevel_s4bios(0);
333 /* kernel_fpu_end(); */
334 //local_irq_restore(flags);
342 * acpi_suspend - OS-agnostic system suspend/resume support (S? states)
343 * @state: state we're entering
352 /* only support S1 and S5 on kernel 2.4 */
353 //if (state != ACPI_STATE_S1 && state != ACPI_STATE_S4
354 // && state != ACPI_STATE_S5)
358 //if (ACPI_STATE_S4 == state) {
359 // /* For s4bios, we need a wakeup address. */
360 // if (1 == AcpiGbl_FACS->S4bios_f &&
361 // 0 != AcpiGbl_FADT->smi_cmd) {
362 // if (!acpi_wakeup_address)
364 // AcpiSetFirmwareWakingVector((acpi_physical_address) acpi_wakeup_address);
366 // /* We don't support S4 under 2.4. Give up */
369 AcpiEnterSleepStatePrep(state
);
371 status
= AcpiEnterSleepState(state
);
372 if (!ACPI_SUCCESS(status
) && state
!= ACPI_STATE_S5
)
375 /* disable interrupts and flush caches */
377 ACPI_FLUSH_CPU_CACHE();
379 /* perform OS-specific sleep actions */
380 status
= acpi_system_suspend(state
);
382 /* Even if we failed to go to sleep, all of the devices are in an suspended
383 * mode. So, we run these unconditionally to make sure we have a usable system
386 AcpiLeaveSleepState(state
);
387 acpi_system_restore_state(state
);
389 /* make sure interrupts are enabled */
392 /* reset firmware waking vector */
393 AcpiSetFirmwareWakingVector(0, 0);
399 acpi_system_init (void)
403 ACPI_FUNCTION_TRACE("acpi_system_init");
405 result
= acpi_bus_register_driver(&acpi_system_driver
);
407 return_VALUE(AE_NOT_FOUND
);
414 acpi_system_exit (void)
416 ACPI_FUNCTION_TRACE("acpi_system_exit");
417 acpi_bus_unregister_driver(&acpi_system_driver
);