]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/acpi.c
xnu-1228.0.2.tar.gz
[apple/xnu.git] / osfmk / i386 / acpi.c
CommitLineData
91447636 1/*
2d21ac55 2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
91447636 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
91447636 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
91447636
A
27 */
28
29#include <i386/misc_protos.h>
2d21ac55 30#include <i386/cpu_data.h>
91447636
A
31#include <i386/proc_reg.h>
32#include <i386/pmap.h>
33#include <i386/mtrr.h>
2d21ac55 34#include <i386/vmx/vmx_cpu.h>
91447636 35#include <i386/acpi.h>
0c530ab8 36#include <i386/fpu.h>
91447636 37#include <i386/mp.h>
0c530ab8 38#include <i386/mp_desc.h>
2d21ac55
A
39#include <i386/serial_io.h>
40#include <i386/hpet.h>
0c530ab8 41#include <i386/machine_check.h>
91447636
A
42
43#include <kern/cpu_data.h>
2d21ac55 44#include <console/serial_protos.h>
3a60a9f5 45
2d21ac55 46#if HIBERNATION
3a60a9f5 47#include <IOKit/IOHibernatePrivate.h>
2d21ac55 48#endif
91447636
A
49#include <IOKit/IOPlatformExpert.h>
50
51extern void acpi_sleep_cpu(acpi_sleep_callback, void * refcon);
52extern char acpi_wake_start[];
53extern char acpi_wake_end[];
54
91447636
A
55extern void set_kbd_leds(int leds);
56
0c530ab8
A
57extern void fpinit(void);
58
91447636
A
59vm_offset_t
60acpi_install_wake_handler(void)
61{
62 /* copy wake code to ACPI_WAKE_ADDR in low memory */
0c530ab8 63 bcopy_phys(kvtophys((vm_offset_t)acpi_wake_start),
91447636
A
64 (addr64_t) ACPI_WAKE_ADDR,
65 acpi_wake_end - acpi_wake_start);
66
67 /* flush cache */
68 wbinvd();
69
70 /* return physical address of the wakeup code */
71 return ACPI_WAKE_ADDR;
72}
73
2d21ac55
A
74#if HIBERNATION
75struct acpi_hibernate_callback_data {
76 acpi_sleep_callback func;
77 void *refcon;
78};
79typedef struct acpi_hibernate_callback_data acpi_hibernate_callback_data_t;
91447636
A
80
81static void
3a60a9f5 82acpi_hibernate(void *refcon)
91447636 83{
2d21ac55 84 uint32_t mode;
3a60a9f5 85
2d21ac55
A
86 acpi_hibernate_callback_data_t *data =
87 (acpi_hibernate_callback_data_t *)refcon;
91447636 88
2d21ac55 89 if (current_cpu_datap()->cpu_hibernate)
0c530ab8 90 {
2d21ac55
A
91 cpu_IA32e_enable(current_cpu_datap());
92
93 mode = hibernate_write_image();
94
95 if( mode == kIOHibernatePostWriteHalt )
96 {
97 // off
98 HIBLOG("power off\n");
99 if (PE_halt_restart) (*PE_halt_restart)(kPEHaltCPU);
100 }
101 else if( mode == kIOHibernatePostWriteRestart )
102 {
103 // restart
104 HIBLOG("restart\n");
105 if (PE_halt_restart) (*PE_halt_restart)(kPERestartCPU);
106 }
107 else
108 {
109 // sleep
110 HIBLOG("sleep\n");
111
112 // should we come back via regular wake, set the state in memory.
113 cpu_datap(0)->cpu_hibernate = 0;
114 }
115
116 /*
117 * If we're in 64-bit mode, drop back into legacy mode during sleep.
118 */
119 cpu_IA32e_disable(current_cpu_datap());
0c530ab8 120
0c530ab8 121 }
91447636 122
2d21ac55 123 (data->func)(data->refcon);
91447636 124
2d21ac55 125 /* should never get here! */
91447636 126}
2d21ac55 127#endif
91447636 128
0c530ab8
A
129static uint64_t acpi_sleep_abstime;
130
91447636
A
131void
132acpi_sleep_kernel(acpi_sleep_callback func, void *refcon)
133{
2d21ac55
A
134#if HIBERNATION
135 acpi_hibernate_callback_data_t data;
136 boolean_t did_hibernate;
137#endif
91447636 138
2d21ac55
A
139 kprintf("acpi_sleep_kernel hib=%d\n",
140 current_cpu_datap()->cpu_hibernate);
0c530ab8 141
2d21ac55
A
142 /* shutdown local APIC before passing control to BIOS */
143 lapic_shutdown();
91447636 144
2d21ac55
A
145#if HIBERNATION
146 data.func = func;
147 data.refcon = refcon;
148#endif
91447636 149
2d21ac55
A
150 /* Save HPET state */
151 hpet_save();
0c530ab8 152
2d21ac55
A
153 /*
154 * Turn off VT, otherwise switching to legacy mode will fail
155 */
156 vmx_suspend();
157
158 /*
159 * If we're in 64-bit mode, drop back into legacy mode during sleep.
160 */
0c530ab8 161 cpu_IA32e_disable(current_cpu_datap());
0c530ab8
A
162
163 acpi_sleep_abstime = mach_absolute_time();
2d21ac55
A
164
165 /*
166 * Save master CPU state and sleep platform.
167 * Will not return until platform is woken up,
168 * or if sleep failed.
169 */
170#if HIBERNATION
171 acpi_sleep_cpu(acpi_hibernate, &data);
172#else
173 acpi_sleep_cpu(func, refcon);
174#endif
175
176 /* reset UART if kprintf is enabled */
177 if (FALSE == disable_serial_output)
178 serial_init();
179
180#if HIBERNATION
181 if (current_cpu_datap()->cpu_hibernate) {
182 int i;
183 for (i = 0; i < PMAP_NWINDOWS; i++)
184 *current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP = 0;
185 current_cpu_datap()->cpu_hibernate = 0;
186 did_hibernate = TRUE;
187
188 } else
189#endif
190 {
191 did_hibernate = FALSE;
0c530ab8 192 }
3a60a9f5 193
2d21ac55
A
194 /* Re-enable mode (including 64-bit if applicable) */
195 cpu_mode_init(current_cpu_datap());
3a60a9f5 196
2d21ac55
A
197 /* Re-enable machine check handling */
198 mca_cpu_init();
91447636 199
2d21ac55
A
200 /* restore MTRR settings */
201 mtrr_update_cpu();
0c530ab8 202
2d21ac55
A
203 /*
204 * Restore VT mode
205 */
206 vmx_resume();
0c530ab8 207
2d21ac55
A
208 /* set up PAT following boot processor power up */
209 pat_init();
91447636 210
0c530ab8
A
211 /* let the realtime clock reset */
212 rtc_sleep_wakeup(acpi_sleep_abstime);
91447636 213
2d21ac55
A
214 if (did_hibernate)
215 hibernate_machine_init();
216
217 /* re-enable and re-init local apic */
218 if (lapic_probe())
219 lapic_init();
91447636 220
2d21ac55
A
221 /* Restore HPET state */
222 hpet_restore();
0c530ab8
A
223
224 /* Restart tick interrupts from the LAPIC timer */
225 rtc_lapic_start_ticking();
226
2d21ac55
A
227 fpinit();
228 clear_fpu();
229
230#if HIBERNATION
231 if (did_hibernate)
232 enable_preemption();
91447636 233
2d21ac55
A
234 kprintf("ret from acpi_sleep_cpu hib=%d\n", did_hibernate);
235#endif
91447636 236}