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