]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/acpi.c
xnu-3247.1.106.tar.gz
[apple/xnu.git] / osfmk / i386 / acpi.c
CommitLineData
91447636 1/*
39236c6e 2 * Copyright (c) 2000-2012 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
b0d623f7
A
29#include <i386/pmap.h>
30#include <i386/proc_reg.h>
31#include <i386/mp_desc.h>
91447636 32#include <i386/misc_protos.h>
b0d623f7 33#include <i386/mp.h>
2d21ac55 34#include <i386/cpu_data.h>
6d2010ae 35#if CONFIG_MTRR
91447636 36#include <i386/mtrr.h>
6d2010ae 37#endif
04b8595b
A
38#if HYPERVISOR
39#include <kern/hv_support.h>
40#endif
b0d623f7 41#if CONFIG_VMX
2d21ac55 42#include <i386/vmx/vmx_cpu.h>
b0d623f7 43#endif
6d2010ae 44#include <i386/ucode.h>
91447636 45#include <i386/acpi.h>
0c530ab8 46#include <i386/fpu.h>
593a1d5f 47#include <i386/lapic.h>
91447636 48#include <i386/mp.h>
0c530ab8 49#include <i386/mp_desc.h>
2d21ac55 50#include <i386/serial_io.h>
b0d623f7 51#if CONFIG_MCA
0c530ab8 52#include <i386/machine_check.h>
b0d623f7 53#endif
593a1d5f 54#include <i386/pmCPU.h>
91447636 55
0b4c1975
A
56#include <i386/tsc.h>
57
91447636 58#include <kern/cpu_data.h>
bd504ef0
A
59#include <kern/machine.h>
60#include <kern/timer_queue.h>
2d21ac55 61#include <console/serial_protos.h>
6d2010ae 62#include <machine/pal_routines.h>
0b4c1975 63#include <vm/vm_page.h>
3a60a9f5 64
2d21ac55 65#if HIBERNATION
3a60a9f5 66#include <IOKit/IOHibernatePrivate.h>
2d21ac55 67#endif
91447636 68#include <IOKit/IOPlatformExpert.h>
0b4c1975
A
69#include <sys/kdebug.h>
70
b0d623f7 71#if CONFIG_SLEEP
91447636 72extern void acpi_sleep_cpu(acpi_sleep_callback, void * refcon);
bd504ef0 73extern void acpi_wake_prot(void);
b0d623f7 74#endif
99c3a104
A
75extern kern_return_t IOCPURunPlatformQuiesceActions(void);
76extern kern_return_t IOCPURunPlatformActiveActions(void);
fe8ab488 77extern kern_return_t IOCPURunPlatformHaltRestartActions(uint32_t message);
91447636 78
0c530ab8
A
79extern void fpinit(void);
80
91447636
A
81vm_offset_t
82acpi_install_wake_handler(void)
83{
b0d623f7
A
84#if CONFIG_SLEEP
85 install_real_mode_bootstrap(acpi_wake_prot);
86 return REAL_MODE_BOOTSTRAP_OFFSET;
87#else
88 return 0;
89#endif
91447636
A
90}
91
3e170ce0 92#if CONFIG_SLEEP
91447636 93
0b4c1975
A
94unsigned int save_kdebug_enable = 0;
95static uint64_t acpi_sleep_abstime;
bd504ef0 96static uint64_t acpi_idle_abstime;
143464d5 97static uint64_t acpi_wake_abstime, acpi_wake_postrebase_abstime;
bd504ef0 98boolean_t deep_idle_rebase = TRUE;
0b4c1975 99
3e170ce0
A
100#if HIBERNATION
101struct acpi_hibernate_callback_data {
102 acpi_sleep_callback func;
103 void *refcon;
104};
105typedef struct acpi_hibernate_callback_data acpi_hibernate_callback_data_t;
106
91447636 107static void
3a60a9f5 108acpi_hibernate(void *refcon)
91447636 109{
2d21ac55 110 uint32_t mode;
3a60a9f5 111
2d21ac55
A
112 acpi_hibernate_callback_data_t *data =
113 (acpi_hibernate_callback_data_t *)refcon;
91447636 114
2d21ac55 115 if (current_cpu_datap()->cpu_hibernate)
0c530ab8 116 {
2d21ac55
A
117 mode = hibernate_write_image();
118
119 if( mode == kIOHibernatePostWriteHalt )
120 {
121 // off
122 HIBLOG("power off\n");
fe8ab488 123 IOCPURunPlatformHaltRestartActions(kPEHaltCPU);
2d21ac55
A
124 if (PE_halt_restart) (*PE_halt_restart)(kPEHaltCPU);
125 }
126 else if( mode == kIOHibernatePostWriteRestart )
127 {
128 // restart
129 HIBLOG("restart\n");
fe8ab488 130 IOCPURunPlatformHaltRestartActions(kPERestartCPU);
2d21ac55
A
131 if (PE_halt_restart) (*PE_halt_restart)(kPERestartCPU);
132 }
133 else
134 {
135 // sleep
136 HIBLOG("sleep\n");
137
138 // should we come back via regular wake, set the state in memory.
139 cpu_datap(0)->cpu_hibernate = 0;
140 }
141
0c530ab8 142 }
0b4c1975
A
143 kdebug_enable = 0;
144
99c3a104
A
145 IOCPURunPlatformQuiesceActions();
146
0b4c1975 147 acpi_sleep_abstime = mach_absolute_time();
91447636 148
2d21ac55 149 (data->func)(data->refcon);
91447636 150
2d21ac55 151 /* should never get here! */
91447636 152}
b0d623f7 153#endif /* HIBERNATION */
3e170ce0 154#endif /* CONFIG_SLEEP */
91447636 155
6d2010ae 156extern void slave_pstart(void);
39236c6e 157extern void hibernate_rebuild_vm_structs(void);
6d2010ae 158
bd504ef0 159extern unsigned int wake_nkdbufs;
0c530ab8 160
91447636
A
161void
162acpi_sleep_kernel(acpi_sleep_callback func, void *refcon)
163{
2d21ac55
A
164#if HIBERNATION
165 acpi_hibernate_callback_data_t data;
2d21ac55 166#endif
b0d623f7 167 boolean_t did_hibernate;
e2fac8b1
A
168 unsigned int cpu;
169 kern_return_t rc;
170 unsigned int my_cpu;
39236c6e
A
171 uint64_t start;
172 uint64_t elapsed = 0;
173 uint64_t elapsed_trace_start = 0;
91447636 174
6d2010ae
A
175 kprintf("acpi_sleep_kernel hib=%d, cpu=%d\n",
176 current_cpu_datap()->cpu_hibernate, cpu_number());
0c530ab8 177
b0d623f7
A
178 /* Get all CPUs to be in the "off" state */
179 my_cpu = cpu_number();
e2fac8b1
A
180 for (cpu = 0; cpu < real_ncpus; cpu += 1) {
181 if (cpu == my_cpu)
182 continue;
183 rc = pmCPUExitHaltToOff(cpu);
184 if (rc != KERN_SUCCESS)
b0d623f7
A
185 panic("Error %d trying to transition CPU %d to OFF",
186 rc, cpu);
e2fac8b1
A
187 }
188
6d2010ae 189 /* shutdown local APIC before passing control to firmware */
2d21ac55 190 lapic_shutdown();
91447636 191
2d21ac55
A
192#if HIBERNATION
193 data.func = func;
194 data.refcon = refcon;
195#endif
91447636 196
593a1d5f
A
197 /* Save power management timer state */
198 pmTimerSave();
0c530ab8 199
04b8595b
A
200#if HYPERVISOR
201 /* Notify hypervisor that we are about to sleep */
202 hv_suspend();
203#endif
204
b0d623f7 205#if CONFIG_VMX
2d21ac55
A
206 /*
207 * Turn off VT, otherwise switching to legacy mode will fail
208 */
209 vmx_suspend();
b0d623f7 210#endif
2d21ac55 211
060df5ea
A
212 /*
213 * Enable FPU/SIMD unit for potential hibernate acceleration
214 */
215 clear_ts();
216
0b4c1975
A
217 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 0) | DBG_FUNC_START, 0, 0, 0, 0, 0);
218
219 save_kdebug_enable = kdebug_enable;
220 kdebug_enable = 0;
0c530ab8
A
221
222 acpi_sleep_abstime = mach_absolute_time();
2d21ac55 223
b0d623f7 224#if CONFIG_SLEEP
2d21ac55
A
225 /*
226 * Save master CPU state and sleep platform.
227 * Will not return until platform is woken up,
228 * or if sleep failed.
229 */
b0d623f7 230 uint64_t old_cr3 = x86_64_pre_sleep();
2d21ac55
A
231#if HIBERNATION
232 acpi_sleep_cpu(acpi_hibernate, &data);
233#else
234 acpi_sleep_cpu(func, refcon);
235#endif
060df5ea 236
39236c6e
A
237 start = mach_absolute_time();
238
b0d623f7 239 x86_64_post_sleep(old_cr3);
b0d623f7
A
240
241#endif /* CONFIG_SLEEP */
2d21ac55 242
4a3eedf9
A
243 /* Reset UART if kprintf is enabled.
244 * However kprintf should not be used before rtc_sleep_wakeup()
245 * for compatibility with firewire kprintf.
246 */
247
2d21ac55 248 if (FALSE == disable_serial_output)
6d2010ae 249 pal_serial_init();
2d21ac55
A
250
251#if HIBERNATION
252 if (current_cpu_datap()->cpu_hibernate) {
2d21ac55
A
253 did_hibernate = TRUE;
254
255 } else
256#endif
257 {
258 did_hibernate = FALSE;
0c530ab8 259 }
3a60a9f5 260
2d21ac55
A
261 /* Re-enable mode (including 64-bit if applicable) */
262 cpu_mode_init(current_cpu_datap());
3a60a9f5 263
b0d623f7 264#if CONFIG_MCA
2d21ac55
A
265 /* Re-enable machine check handling */
266 mca_cpu_init();
b0d623f7 267#endif
91447636 268
6d2010ae 269#if CONFIG_MTRR
2d21ac55
A
270 /* restore MTRR settings */
271 mtrr_update_cpu();
6d2010ae
A
272#endif
273
274 /* update CPU microcode */
275 ucode_update_wake();
0c530ab8 276
b0d623f7 277#if CONFIG_VMX
2d21ac55
A
278 /*
279 * Restore VT mode
280 */
281 vmx_resume();
b0d623f7 282#endif
0c530ab8 283
6d2010ae 284#if CONFIG_MTRR
2d21ac55
A
285 /* set up PAT following boot processor power up */
286 pat_init();
6d2010ae 287#endif
91447636 288
593a1d5f
A
289 /*
290 * Go through all of the CPUs and mark them as requiring
291 * a full restart.
292 */
293 pmMarkAllCPUsOff();
294
0b4c1975 295
060df5ea
A
296 /* re-enable and re-init local apic (prior to starting timers) */
297 if (lapic_probe())
298 lapic_configure();
299
3e170ce0 300#if HIBERNATION
39236c6e 301 hibernate_rebuild_vm_structs();
3e170ce0 302#endif
39236c6e
A
303
304 elapsed += mach_absolute_time() - start;
bd504ef0
A
305 acpi_wake_abstime = mach_absolute_time();
306
0c530ab8
A
307 /* let the realtime clock reset */
308 rtc_sleep_wakeup(acpi_sleep_abstime);
143464d5 309 acpi_wake_postrebase_abstime = mach_absolute_time();
fe8ab488
A
310 assert(mach_absolute_time() >= acpi_sleep_abstime);
311
0b4c1975
A
312 kdebug_enable = save_kdebug_enable;
313
bd504ef0 314 if (kdebug_enable == 0) {
39236c6e
A
315 if (wake_nkdbufs) {
316 start = mach_absolute_time();
bd504ef0 317 start_kern_tracing(wake_nkdbufs, TRUE);
39236c6e
A
318 elapsed_trace_start += mach_absolute_time() - start;
319 }
bd504ef0 320 }
39236c6e 321 start = mach_absolute_time();
bd504ef0
A
322
323 /* Reconfigure FP/SIMD unit */
324 init_fpu();
325 clear_ts();
326
99c3a104
A
327 IOCPURunPlatformActiveActions();
328
3e170ce0 329#if HIBERNATION
0b4c1975 330 if (did_hibernate) {
39236c6e 331 elapsed += mach_absolute_time() - start;
0b4c1975 332
39236c6e 333 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 2) | DBG_FUNC_START, elapsed, elapsed_trace_start, 0, 0, 0);
2d21ac55 334 hibernate_machine_init();
0b4c1975
A
335 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 2) | DBG_FUNC_END, 0, 0, 0, 0, 0);
336
b0d623f7 337 current_cpu_datap()->cpu_hibernate = 0;
0b4c1975
A
338
339 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 0) | DBG_FUNC_END, 0, 0, 0, 0, 0);
340 } else
3e170ce0 341#endif /* HIBERNATION */
0b4c1975
A
342 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 0) | DBG_FUNC_END, 0, 0, 0, 0, 0);
343
593a1d5f
A
344 /* Restore power management register state */
345 pmCPUMarkRunning(current_cpu_datap());
91447636 346
593a1d5f
A
347 /* Restore power management timer state */
348 pmTimerRestore();
0c530ab8 349
060df5ea
A
350 /* Restart timer interrupts */
351 rtc_timer_start();
0c530ab8 352
2d21ac55 353#if HIBERNATION
91447636 354
2d21ac55
A
355 kprintf("ret from acpi_sleep_cpu hib=%d\n", did_hibernate);
356#endif
b0d623f7
A
357
358#if CONFIG_SLEEP
359 /* Becase we don't save the bootstrap page, and we share it
360 * between sleep and mp slave init, we need to recreate it
361 * after coming back from sleep or hibernate */
362 install_real_mode_bootstrap(slave_pstart);
363#endif
91447636 364}
b0d623f7 365
bd504ef0
A
366/*
367 * acpi_idle_kernel is called by the ACPI Platform kext to request the kernel
368 * to idle the boot processor in the deepest C-state for S0 sleep. All slave
369 * processors are expected already to have been offlined in the deepest C-state.
370 *
371 * The contract with ACPI is that although the kernel is called with interrupts
372 * disabled, interrupts may need to be re-enabled to dismiss any pending timer
373 * interrupt. However, the callback function will be called once this has
374 * occurred and interrupts are guaranteed to be disabled at that time,
375 * and to remain disabled during C-state entry, exit (wake) and return
376 * from acpi_idle_kernel.
377 */
378void
379acpi_idle_kernel(acpi_sleep_callback func, void *refcon)
380{
381 boolean_t istate = ml_get_interrupts_enabled();
382
383 kprintf("acpi_idle_kernel, cpu=%d, interrupts %s\n",
384 cpu_number(), istate ? "enabled" : "disabled");
385
386 assert(cpu_number() == master_cpu);
387
388 /*
389 * Effectively set the boot cpu offline.
390 * This will stop further deadlines being set.
391 */
392 cpu_datap(master_cpu)->cpu_running = FALSE;
393
394 /* Cancel any pending deadline */
395 setPop(0);
396 while (lapic_is_interrupting(LAPIC_TIMER_VECTOR)) {
397 (void) ml_set_interrupts_enabled(TRUE);
398 setPop(0);
399 ml_set_interrupts_enabled(FALSE);
400 }
401
402 /*
403 * Call back to caller to indicate that interrupts will remain
404 * disabled while we deep idle, wake and return.
405 */
406 func(refcon);
407
408 acpi_idle_abstime = mach_absolute_time();
409
410 KERNEL_DEBUG_CONSTANT(
411 MACHDBG_CODE(DBG_MACH_SCHED, MACH_DEEP_IDLE) | DBG_FUNC_START,
412 acpi_idle_abstime, deep_idle_rebase, 0, 0, 0);
413
414 /*
415 * Disable tracing during S0-sleep
416 * unless overridden by sysctl -w tsc.deep_idle_rebase=0
417 */
418 if (deep_idle_rebase) {
419 save_kdebug_enable = kdebug_enable;
420 kdebug_enable = 0;
421 }
422
423 /*
424 * Call into power-management to enter the lowest C-state.
425 * Note when called on the boot processor this routine will
426 * return directly when awoken.
427 */
428 pmCPUHalt(PM_HALT_SLEEP);
429
430 /*
431 * Get wakeup time relative to the TSC which has progressed.
432 * Then rebase nanotime to reflect time not progressing over sleep
433 * - unless overriden so that tracing can occur during deep_idle.
434 */
435 acpi_wake_abstime = mach_absolute_time();
436 if (deep_idle_rebase) {
437 rtc_sleep_wakeup(acpi_idle_abstime);
438 kdebug_enable = save_kdebug_enable;
439 }
143464d5 440 acpi_wake_postrebase_abstime = mach_absolute_time();
fe8ab488 441 assert(mach_absolute_time() >= acpi_idle_abstime);
bd504ef0
A
442 cpu_datap(master_cpu)->cpu_running = TRUE;
443
444 KERNEL_DEBUG_CONSTANT(
445 MACHDBG_CODE(DBG_MACH_SCHED, MACH_DEEP_IDLE) | DBG_FUNC_END,
446 acpi_wake_abstime, acpi_wake_abstime - acpi_idle_abstime, 0, 0, 0);
447
448 /* Like S3 sleep, turn on tracing if trace_wake boot-arg is present */
449 if (kdebug_enable == 0) {
450 if (wake_nkdbufs)
451 start_kern_tracing(wake_nkdbufs, TRUE);
452 }
453
454 IOCPURunPlatformActiveActions();
455
456 /* Restart timer interrupts */
457 rtc_timer_start();
458}
459
b0d623f7
A
460extern char real_mode_bootstrap_end[];
461extern char real_mode_bootstrap_base[];
462
463void
464install_real_mode_bootstrap(void *prot_entry)
465{
466 /*
467 * Copy the boot entry code to the real-mode vector area REAL_MODE_BOOTSTRAP_OFFSET.
468 * This is in page 1 which has been reserved for this purpose by
469 * machine_startup() from the boot processor.
470 * The slave boot code is responsible for switching to protected
471 * mode and then jumping to the common startup, _start().
472 */
473 bcopy_phys(kvtophys((vm_offset_t) real_mode_bootstrap_base),
474 (addr64_t) REAL_MODE_BOOTSTRAP_OFFSET,
475 real_mode_bootstrap_end-real_mode_bootstrap_base);
476
477 /*
478 * Set the location at the base of the stack to point to the
479 * common startup entry.
480 */
481 ml_phys_write_word(
482 PROT_MODE_START+REAL_MODE_BOOTSTRAP_OFFSET,
483 (unsigned int)kvtophys((vm_offset_t)prot_entry));
484
485 /* Flush caches */
486 __asm__("wbinvd");
487}
488
143464d5
A
489boolean_t
490ml_recent_wake(void) {
491 uint64_t ctime = mach_absolute_time();
492 assert(ctime > acpi_wake_postrebase_abstime);
493 return ((ctime - acpi_wake_postrebase_abstime) < 5 * NSEC_PER_SEC);
494}