]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/acpi.c
xnu-1504.9.37.tar.gz
[apple/xnu.git] / osfmk / i386 / acpi.c
1 /*
2 * Copyright (c) 2000-2009 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/pmap.h>
30 #include <i386/proc_reg.h>
31 #include <i386/mp_desc.h>
32 #include <i386/misc_protos.h>
33 #include <i386/mp.h>
34 #include <i386/cpu_data.h>
35 #include <i386/mtrr.h>
36 #if CONFIG_VMX
37 #include <i386/vmx/vmx_cpu.h>
38 #endif
39 #include <i386/acpi.h>
40 #include <i386/fpu.h>
41 #include <i386/lapic.h>
42 #include <i386/mp.h>
43 #include <i386/mp_desc.h>
44 #include <i386/serial_io.h>
45 #if CONFIG_MCA
46 #include <i386/machine_check.h>
47 #endif
48 #include <i386/pmCPU.h>
49
50 #include <i386/tsc.h>
51
52 #include <kern/cpu_data.h>
53 #include <console/serial_protos.h>
54 #include <vm/vm_page.h>
55
56 #if HIBERNATION
57 #include <IOKit/IOHibernatePrivate.h>
58 #endif
59 #include <IOKit/IOPlatformExpert.h>
60
61 #include <sys/kdebug.h>
62
63 #if CONFIG_SLEEP
64 extern void acpi_sleep_cpu(acpi_sleep_callback, void * refcon);
65 extern void acpi_wake_prot(void);
66 #endif
67
68 extern void fpinit(void);
69
70 vm_offset_t
71 acpi_install_wake_handler(void)
72 {
73 #if CONFIG_SLEEP
74 install_real_mode_bootstrap(acpi_wake_prot);
75 return REAL_MODE_BOOTSTRAP_OFFSET;
76 #else
77 return 0;
78 #endif
79 }
80
81 #if HIBERNATION
82 struct acpi_hibernate_callback_data {
83 acpi_sleep_callback func;
84 void *refcon;
85 };
86 typedef struct acpi_hibernate_callback_data acpi_hibernate_callback_data_t;
87
88 unsigned int save_kdebug_enable = 0;
89 static uint64_t acpi_sleep_abstime;
90
91
92 #if CONFIG_SLEEP
93 static void
94 acpi_hibernate(void *refcon)
95 {
96 uint32_t mode;
97
98 acpi_hibernate_callback_data_t *data =
99 (acpi_hibernate_callback_data_t *)refcon;
100
101 if (current_cpu_datap()->cpu_hibernate)
102 {
103 #if defined(__i386__)
104 cpu_IA32e_enable(current_cpu_datap());
105 #endif
106
107 mode = hibernate_write_image();
108
109 if( mode == kIOHibernatePostWriteHalt )
110 {
111 // off
112 HIBLOG("power off\n");
113 if (PE_halt_restart) (*PE_halt_restart)(kPEHaltCPU);
114 }
115 else if( mode == kIOHibernatePostWriteRestart )
116 {
117 // restart
118 HIBLOG("restart\n");
119 if (PE_halt_restart) (*PE_halt_restart)(kPERestartCPU);
120 }
121 else
122 {
123 // sleep
124 HIBLOG("sleep\n");
125
126 // should we come back via regular wake, set the state in memory.
127 cpu_datap(0)->cpu_hibernate = 0;
128 }
129
130 #if defined(__i386__)
131 /*
132 * If we're in 64-bit mode, drop back into legacy mode during sleep.
133 */
134 cpu_IA32e_disable(current_cpu_datap());
135 #endif
136 }
137 kdebug_enable = 0;
138
139 acpi_sleep_abstime = mach_absolute_time();
140
141 (data->func)(data->refcon);
142
143 /* should never get here! */
144 }
145 #endif /* CONFIG_SLEEP */
146 #endif /* HIBERNATION */
147
148 extern void slave_pstart(void);
149
150 void
151 acpi_sleep_kernel(acpi_sleep_callback func, void *refcon)
152 {
153 #if HIBERNATION
154 acpi_hibernate_callback_data_t data;
155 #endif
156 boolean_t did_hibernate;
157 unsigned int cpu;
158 kern_return_t rc;
159 unsigned int my_cpu;
160 uint64_t now;
161 uint64_t my_tsc;
162 uint64_t my_abs;
163
164 kprintf("acpi_sleep_kernel hib=%d\n",
165 current_cpu_datap()->cpu_hibernate);
166
167 /* Get all CPUs to be in the "off" state */
168 my_cpu = cpu_number();
169 for (cpu = 0; cpu < real_ncpus; cpu += 1) {
170 if (cpu == my_cpu)
171 continue;
172 rc = pmCPUExitHaltToOff(cpu);
173 if (rc != KERN_SUCCESS)
174 panic("Error %d trying to transition CPU %d to OFF",
175 rc, cpu);
176 }
177
178 /* shutdown local APIC before passing control to BIOS */
179 lapic_shutdown();
180
181 #if HIBERNATION
182 data.func = func;
183 data.refcon = refcon;
184 #endif
185
186 /* Save power management timer state */
187 pmTimerSave();
188
189 #if CONFIG_VMX
190 /*
191 * Turn off VT, otherwise switching to legacy mode will fail
192 */
193 vmx_suspend();
194 #endif
195
196 #if defined(__i386__)
197 /*
198 * If we're in 64-bit mode, drop back into legacy mode during sleep.
199 */
200 cpu_IA32e_disable(current_cpu_datap());
201 #endif
202 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 0) | DBG_FUNC_START, 0, 0, 0, 0, 0);
203
204 save_kdebug_enable = kdebug_enable;
205 kdebug_enable = 0;
206
207 acpi_sleep_abstime = mach_absolute_time();
208
209 #if CONFIG_SLEEP
210 /*
211 * Save master CPU state and sleep platform.
212 * Will not return until platform is woken up,
213 * or if sleep failed.
214 */
215 #ifdef __x86_64__
216 uint64_t old_cr3 = x86_64_pre_sleep();
217 #endif
218 #if HIBERNATION
219 acpi_sleep_cpu(acpi_hibernate, &data);
220 #else
221 acpi_sleep_cpu(func, refcon);
222 #endif
223 #ifdef __x86_64__
224 x86_64_post_sleep(old_cr3);
225 #endif
226
227 #endif /* CONFIG_SLEEP */
228
229 /* Reset UART if kprintf is enabled.
230 * However kprintf should not be used before rtc_sleep_wakeup()
231 * for compatibility with firewire kprintf.
232 */
233
234 if (FALSE == disable_serial_output)
235 serial_init();
236
237 #if HIBERNATION
238 if (current_cpu_datap()->cpu_hibernate) {
239 #if defined(__i386__)
240 int i;
241 for (i = 0; i < PMAP_NWINDOWS; i++)
242 *current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP = 0;
243 #endif
244 did_hibernate = TRUE;
245
246 } else
247 #endif
248 {
249 did_hibernate = FALSE;
250 }
251
252 /* Re-enable mode (including 64-bit if applicable) */
253 cpu_mode_init(current_cpu_datap());
254
255 #if CONFIG_MCA
256 /* Re-enable machine check handling */
257 mca_cpu_init();
258 #endif
259
260 /* restore MTRR settings */
261 mtrr_update_cpu();
262
263 #if CONFIG_VMX
264 /*
265 * Restore VT mode
266 */
267 vmx_resume();
268 #endif
269
270 /* set up PAT following boot processor power up */
271 pat_init();
272
273 /*
274 * Go through all of the CPUs and mark them as requiring
275 * a full restart.
276 */
277 pmMarkAllCPUsOff();
278
279 ml_get_timebase(&now);
280
281 /* let the realtime clock reset */
282 rtc_sleep_wakeup(acpi_sleep_abstime);
283
284 kdebug_enable = save_kdebug_enable;
285
286 if (did_hibernate) {
287
288 my_tsc = (now >> 32) | (now << 32);
289 my_abs = tmrCvt(my_tsc, tscFCvtt2n);
290
291 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 2) | DBG_FUNC_START,
292 (uint32_t)(my_abs >> 32), (uint32_t)my_abs, 0, 0, 0);
293 hibernate_machine_init();
294 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 2) | DBG_FUNC_END, 0, 0, 0, 0, 0);
295
296 current_cpu_datap()->cpu_hibernate = 0;
297
298 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 0) | DBG_FUNC_END, 0, 0, 0, 0, 0);
299 } else
300 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 0) | DBG_FUNC_END, 0, 0, 0, 0, 0);
301
302 /* re-enable and re-init local apic */
303 if (lapic_probe())
304 lapic_configure();
305
306 /* Restore power management register state */
307 pmCPUMarkRunning(current_cpu_datap());
308
309 /* Restore power management timer state */
310 pmTimerRestore();
311
312 /* Restart tick interrupts from the LAPIC timer */
313 rtc_lapic_start_ticking();
314
315 fpinit();
316 clear_fpu();
317
318 #if HIBERNATION
319 #ifdef __i386__
320 /* The image is written out using the copy engine, which disables
321 * preemption. Since the copy engine writes out the page which contains
322 * the preemption variable when it is disabled, we need to explicitly
323 * enable it here */
324 if (did_hibernate)
325 enable_preemption();
326 #endif
327
328 kprintf("ret from acpi_sleep_cpu hib=%d\n", did_hibernate);
329 #endif
330
331 #if CONFIG_SLEEP
332 /* Becase we don't save the bootstrap page, and we share it
333 * between sleep and mp slave init, we need to recreate it
334 * after coming back from sleep or hibernate */
335 install_real_mode_bootstrap(slave_pstart);
336 #endif
337 }
338
339 extern char real_mode_bootstrap_end[];
340 extern char real_mode_bootstrap_base[];
341
342 void
343 install_real_mode_bootstrap(void *prot_entry)
344 {
345 /*
346 * Copy the boot entry code to the real-mode vector area REAL_MODE_BOOTSTRAP_OFFSET.
347 * This is in page 1 which has been reserved for this purpose by
348 * machine_startup() from the boot processor.
349 * The slave boot code is responsible for switching to protected
350 * mode and then jumping to the common startup, _start().
351 */
352 bcopy_phys(kvtophys((vm_offset_t) real_mode_bootstrap_base),
353 (addr64_t) REAL_MODE_BOOTSTRAP_OFFSET,
354 real_mode_bootstrap_end-real_mode_bootstrap_base);
355
356 /*
357 * Set the location at the base of the stack to point to the
358 * common startup entry.
359 */
360 ml_phys_write_word(
361 PROT_MODE_START+REAL_MODE_BOOTSTRAP_OFFSET,
362 (unsigned int)kvtophys((vm_offset_t)prot_entry));
363
364 /* Flush caches */
365 __asm__("wbinvd");
366 }
367