]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/acpi.c
xnu-1228.3.13.tar.gz
[apple/xnu.git] / osfmk / i386 / acpi.c
1 /*
2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <i386/misc_protos.h>
30 #include <i386/cpu_data.h>
31 #include <i386/proc_reg.h>
32 #include <i386/pmap.h>
33 #include <i386/mtrr.h>
34 #include <i386/vmx/vmx_cpu.h>
35 #include <i386/acpi.h>
36 #include <i386/fpu.h>
37 #include <i386/mp.h>
38 #include <i386/mp_desc.h>
39 #include <i386/serial_io.h>
40 #include <i386/hpet.h>
41 #include <i386/machine_check.h>
42
43 #include <kern/cpu_data.h>
44 #include <console/serial_protos.h>
45
46 #if HIBERNATION
47 #include <IOKit/IOHibernatePrivate.h>
48 #endif
49 #include <IOKit/IOPlatformExpert.h>
50
51 extern void acpi_sleep_cpu(acpi_sleep_callback, void * refcon);
52 extern char acpi_wake_start[];
53 extern char acpi_wake_end[];
54
55 extern void set_kbd_leds(int leds);
56
57 extern void fpinit(void);
58
59 vm_offset_t
60 acpi_install_wake_handler(void)
61 {
62 /* copy wake code to ACPI_WAKE_ADDR in low memory */
63 bcopy_phys(kvtophys((vm_offset_t)acpi_wake_start),
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
74 #if HIBERNATION
75 struct acpi_hibernate_callback_data {
76 acpi_sleep_callback func;
77 void *refcon;
78 };
79 typedef struct acpi_hibernate_callback_data acpi_hibernate_callback_data_t;
80
81 static void
82 acpi_hibernate(void *refcon)
83 {
84 uint32_t mode;
85
86 acpi_hibernate_callback_data_t *data =
87 (acpi_hibernate_callback_data_t *)refcon;
88
89 if (current_cpu_datap()->cpu_hibernate)
90 {
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());
120
121 }
122
123 (data->func)(data->refcon);
124
125 /* should never get here! */
126 }
127 #endif
128
129 static uint64_t acpi_sleep_abstime;
130
131 void
132 acpi_sleep_kernel(acpi_sleep_callback func, void *refcon)
133 {
134 #if HIBERNATION
135 acpi_hibernate_callback_data_t data;
136 boolean_t did_hibernate;
137 #endif
138
139 kprintf("acpi_sleep_kernel hib=%d\n",
140 current_cpu_datap()->cpu_hibernate);
141
142 /* shutdown local APIC before passing control to BIOS */
143 lapic_shutdown();
144
145 #if HIBERNATION
146 data.func = func;
147 data.refcon = refcon;
148 #endif
149
150 /* Save HPET state */
151 hpet_save();
152
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 */
161 cpu_IA32e_disable(current_cpu_datap());
162
163 acpi_sleep_abstime = mach_absolute_time();
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 * However kprintf should not be used before rtc_sleep_wakeup()
178 * for compatibility with firewire kprintf.
179 */
180
181 if (FALSE == disable_serial_output)
182 serial_init();
183
184 #if HIBERNATION
185 if (current_cpu_datap()->cpu_hibernate) {
186 int i;
187 for (i = 0; i < PMAP_NWINDOWS; i++)
188 *current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP = 0;
189 current_cpu_datap()->cpu_hibernate = 0;
190 did_hibernate = TRUE;
191
192 } else
193 #endif
194 {
195 did_hibernate = FALSE;
196 }
197
198 /* Re-enable mode (including 64-bit if applicable) */
199 cpu_mode_init(current_cpu_datap());
200
201 /* Re-enable machine check handling */
202 mca_cpu_init();
203
204 /* restore MTRR settings */
205 mtrr_update_cpu();
206
207 /*
208 * Restore VT mode
209 */
210 vmx_resume();
211
212 /* set up PAT following boot processor power up */
213 pat_init();
214
215 /* let the realtime clock reset */
216 rtc_sleep_wakeup(acpi_sleep_abstime);
217
218 if (did_hibernate)
219 hibernate_machine_init();
220
221 /* re-enable and re-init local apic */
222 if (lapic_probe())
223 lapic_init();
224
225 /* Restore HPET state */
226 hpet_restore();
227
228 /* Restart tick interrupts from the LAPIC timer */
229 rtc_lapic_start_ticking();
230
231 fpinit();
232 clear_fpu();
233
234 #if HIBERNATION
235 if (did_hibernate)
236 enable_preemption();
237
238 kprintf("ret from acpi_sleep_cpu hib=%d\n", did_hibernate);
239 #endif
240 }