2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <i386/pmap.h>
30 #include <i386/proc_reg.h>
31 #include <i386/mp_desc.h>
32 #include <i386/misc_protos.h>
34 #include <i386/cpu_data.h>
36 #include <i386/mtrr.h>
39 #include <kern/hv_support.h>
42 #include <i386/vmx/vmx_cpu.h>
44 #include <i386/ucode.h>
45 #include <i386/acpi.h>
47 #include <i386/lapic.h>
49 #include <i386/mp_desc.h>
50 #include <i386/serial_io.h>
52 #include <i386/machine_check.h>
54 #include <i386/pmCPU.h>
58 #include <kern/cpu_data.h>
59 #include <kern/machine.h>
60 #include <kern/timer_queue.h>
61 #include <console/serial_protos.h>
62 #include <machine/pal_routines.h>
63 #include <vm/vm_page.h>
66 #include <IOKit/IOHibernatePrivate.h>
68 #include <IOKit/IOPlatformExpert.h>
69 #include <sys/kdebug.h>
72 #include <kern/monotonic.h>
73 #endif /* MONOTONIC */
76 extern void acpi_sleep_cpu(acpi_sleep_callback
, void * refcon
);
77 extern void acpi_wake_prot(void);
79 extern kern_return_t
IOCPURunPlatformQuiesceActions(void);
80 extern kern_return_t
IOCPURunPlatformActiveActions(void);
81 extern kern_return_t
IOCPURunPlatformHaltRestartActions(uint32_t message
);
83 extern void fpinit(void);
86 acpi_install_wake_handler(void)
89 install_real_mode_bootstrap(acpi_wake_prot
);
90 return REAL_MODE_BOOTSTRAP_OFFSET
;
98 unsigned int save_kdebug_enable
= 0;
99 static uint64_t acpi_sleep_abstime
;
100 static uint64_t acpi_idle_abstime
;
101 static uint64_t acpi_wake_abstime
, acpi_wake_postrebase_abstime
;
102 boolean_t deep_idle_rebase
= TRUE
;
105 struct acpi_hibernate_callback_data
{
106 acpi_sleep_callback func
;
109 typedef struct acpi_hibernate_callback_data acpi_hibernate_callback_data_t
;
112 acpi_hibernate(void *refcon
)
116 acpi_hibernate_callback_data_t
*data
=
117 (acpi_hibernate_callback_data_t
*)refcon
;
119 if (current_cpu_datap()->cpu_hibernate
)
121 mode
= hibernate_write_image();
123 if( mode
== kIOHibernatePostWriteHalt
)
126 HIBLOG("power off\n");
127 IOCPURunPlatformHaltRestartActions(kPEHaltCPU
);
128 if (PE_halt_restart
) (*PE_halt_restart
)(kPEHaltCPU
);
130 else if( mode
== kIOHibernatePostWriteRestart
)
134 IOCPURunPlatformHaltRestartActions(kPERestartCPU
);
135 if (PE_halt_restart
) (*PE_halt_restart
)(kPERestartCPU
);
142 // should we come back via regular wake, set the state in memory.
143 cpu_datap(0)->cpu_hibernate
= 0;
153 IOCPURunPlatformQuiesceActions();
155 acpi_sleep_abstime
= mach_absolute_time();
157 (data
->func
)(data
->refcon
);
159 /* should never get here! */
161 #endif /* HIBERNATION */
162 #endif /* CONFIG_SLEEP */
164 extern void slave_pstart(void);
165 extern void hibernate_rebuild_vm_structs(void);
167 extern unsigned int wake_nkdbufs
;
170 acpi_sleep_kernel(acpi_sleep_callback func
, void *refcon
)
173 acpi_hibernate_callback_data_t data
;
175 boolean_t did_hibernate
;
176 cpu_data_t
*cdp
= current_cpu_datap();
181 uint64_t elapsed
= 0;
182 uint64_t elapsed_trace_start
= 0;
184 my_cpu
= cpu_number();
185 kprintf("acpi_sleep_kernel hib=%d, cpu=%d\n", cdp
->cpu_hibernate
,
188 /* Get all CPUs to be in the "off" state */
189 for (cpu
= 0; cpu
< real_ncpus
; cpu
+= 1) {
192 rc
= pmCPUExitHaltToOff(cpu
);
193 if (rc
!= KERN_SUCCESS
)
194 panic("Error %d trying to transition CPU %d to OFF",
198 /* shutdown local APIC before passing control to firmware */
203 data
.refcon
= refcon
;
208 #endif /* MONOTONIC */
210 /* Save power management timer state */
214 /* Notify hypervisor that we are about to sleep */
219 * Enable FPU/SIMD unit for potential hibernate acceleration
223 KDBG(IOKDBG_CODE(DBG_HIBERNATE
, 0) | DBG_FUNC_START
);
225 save_kdebug_enable
= kdebug_enable
;
228 acpi_sleep_abstime
= mach_absolute_time();
232 * Save master CPU state and sleep platform.
233 * Will not return until platform is woken up,
234 * or if sleep failed.
236 uint64_t old_cr3
= x86_64_pre_sleep();
238 acpi_sleep_cpu(acpi_hibernate
, &data
);
243 acpi_sleep_cpu(func
, refcon
);
246 acpi_wake_abstime
= mach_absolute_time();
247 /* Rebase TSC->absolute time conversion, using timestamp
248 * recorded before sleep.
250 rtc_nanotime_init(acpi_sleep_abstime
);
251 acpi_wake_postrebase_abstime
= start
= mach_absolute_time();
252 assert(start
>= acpi_sleep_abstime
);
254 x86_64_post_sleep(old_cr3
);
256 #endif /* CONFIG_SLEEP */
258 /* Reset UART if kprintf is enabled.
259 * However kprintf should not be used before rtc_sleep_wakeup()
260 * for compatibility with firewire kprintf.
263 if (FALSE
== disable_serial_output
)
267 if (current_cpu_datap()->cpu_hibernate
) {
268 did_hibernate
= TRUE
;
273 did_hibernate
= FALSE
;
276 /* Re-enable fast syscall */
277 cpu_syscall_init(current_cpu_datap());
280 /* Re-enable machine check handling */
285 /* restore MTRR settings */
289 /* update CPU microcode */
293 /* set up PAT following boot processor power up */
301 vmx_resume(did_hibernate
);
305 * Go through all of the CPUs and mark them as requiring
311 /* re-enable and re-init local apic (prior to starting timers) */
317 * The sleep implementation uses indirect noreturn calls, so we miss stack
318 * unpoisoning. Do it explicitly.
320 __asan_handle_no_return();
324 hibernate_rebuild_vm_structs();
327 elapsed
+= mach_absolute_time() - start
;
329 rtc_decrementer_configure();
330 kdebug_enable
= save_kdebug_enable
;
332 if (kdebug_enable
== 0) {
334 start
= mach_absolute_time();
335 kdebug_trace_start(wake_nkdbufs
, NULL
, TRUE
);
336 elapsed_trace_start
+= mach_absolute_time() - start
;
339 start
= mach_absolute_time();
341 /* Reconfigure FP/SIMD unit */
345 IOCPURunPlatformActiveActions();
349 KDBG(IOKDBG_CODE(DBG_HIBERNATE
, 2) | DBG_FUNC_START
);
350 hibernate_machine_init();
351 KDBG(IOKDBG_CODE(DBG_HIBERNATE
, 2) | DBG_FUNC_END
);
353 current_cpu_datap()->cpu_hibernate
= 0;
355 #endif /* HIBERNATION */
357 KDBG(IOKDBG_CODE(DBG_HIBERNATE
, 0) | DBG_FUNC_END
, start
, elapsed
,
358 elapsed_trace_start
, acpi_wake_abstime
);
360 /* Restore power management register state */
361 pmCPUMarkRunning(current_cpu_datap());
363 /* Restore power management timer state */
366 /* Restart timer interrupts */
371 kprintf("ret from acpi_sleep_cpu hib=%d\n", did_hibernate
);
375 /* Becase we don't save the bootstrap page, and we share it
376 * between sleep and mp slave init, we need to recreate it
377 * after coming back from sleep or hibernate */
378 install_real_mode_bootstrap(slave_pstart
);
383 * acpi_idle_kernel is called by the ACPI Platform kext to request the kernel
384 * to idle the boot processor in the deepest C-state for S0 sleep. All slave
385 * processors are expected already to have been offlined in the deepest C-state.
387 * The contract with ACPI is that although the kernel is called with interrupts
388 * disabled, interrupts may need to be re-enabled to dismiss any pending timer
389 * interrupt. However, the callback function will be called once this has
390 * occurred and interrupts are guaranteed to be disabled at that time,
391 * and to remain disabled during C-state entry, exit (wake) and return
392 * from acpi_idle_kernel.
395 acpi_idle_kernel(acpi_sleep_callback func
, void *refcon
)
397 boolean_t istate
= ml_get_interrupts_enabled();
399 kprintf("acpi_idle_kernel, cpu=%d, interrupts %s\n",
400 cpu_number(), istate
? "enabled" : "disabled");
402 assert(cpu_number() == master_cpu
);
404 /* Cancel any pending deadline */
406 while (lapic_is_interrupting(LAPIC_TIMER_VECTOR
)) {
407 (void) ml_set_interrupts_enabled(TRUE
);
409 ml_set_interrupts_enabled(FALSE
);
413 * Call back to caller to indicate that interrupts will remain
414 * disabled while we deep idle, wake and return.
416 IOCPURunPlatformQuiesceActions();
420 acpi_idle_abstime
= mach_absolute_time();
422 KERNEL_DEBUG_CONSTANT(
423 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_DEEP_IDLE
) | DBG_FUNC_START
,
424 acpi_idle_abstime
, deep_idle_rebase
, 0, 0, 0);
427 * Disable tracing during S0-sleep
428 * unless overridden by sysctl -w tsc.deep_idle_rebase=0
430 if (deep_idle_rebase
) {
431 save_kdebug_enable
= kdebug_enable
;
436 * Call into power-management to enter the lowest C-state.
437 * Note when called on the boot processor this routine will
438 * return directly when awoken.
440 pmCPUHalt(PM_HALT_SLEEP
);
443 * Get wakeup time relative to the TSC which has progressed.
444 * Then rebase nanotime to reflect time not progressing over sleep
445 * - unless overriden so that tracing can occur during deep_idle.
447 acpi_wake_abstime
= mach_absolute_time();
448 if (deep_idle_rebase
) {
449 rtc_sleep_wakeup(acpi_idle_abstime
);
450 kdebug_enable
= save_kdebug_enable
;
452 acpi_wake_postrebase_abstime
= mach_absolute_time();
453 assert(mach_absolute_time() >= acpi_idle_abstime
);
455 KERNEL_DEBUG_CONSTANT(
456 MACHDBG_CODE(DBG_MACH_SCHED
, MACH_DEEP_IDLE
) | DBG_FUNC_END
,
457 acpi_wake_abstime
, acpi_wake_abstime
- acpi_idle_abstime
, 0, 0, 0);
459 /* Like S3 sleep, turn on tracing if trace_wake boot-arg is present */
460 if (kdebug_enable
== 0) {
462 __kdebug_only
uint64_t start
= mach_absolute_time();
463 kdebug_trace_start(wake_nkdbufs
, NULL
, TRUE
);
464 KDBG(IOKDBG_CODE(DBG_HIBERNATE
, 15), start
);
468 IOCPURunPlatformActiveActions();
470 /* Restart timer interrupts */
474 extern char real_mode_bootstrap_end
[];
475 extern char real_mode_bootstrap_base
[];
478 install_real_mode_bootstrap(void *prot_entry
)
481 * Copy the boot entry code to the real-mode vector area REAL_MODE_BOOTSTRAP_OFFSET.
482 * This is in page 1 which has been reserved for this purpose by
483 * machine_startup() from the boot processor.
484 * The slave boot code is responsible for switching to protected
485 * mode and then jumping to the common startup, _start().
487 bcopy_phys(kvtophys((vm_offset_t
) real_mode_bootstrap_base
),
488 (addr64_t
) REAL_MODE_BOOTSTRAP_OFFSET
,
489 real_mode_bootstrap_end
-real_mode_bootstrap_base
);
492 * Set the location at the base of the stack to point to the
493 * common startup entry.
496 PROT_MODE_START
+REAL_MODE_BOOTSTRAP_OFFSET
,
497 (unsigned int)kvtophys((vm_offset_t
)prot_entry
));
504 ml_recent_wake(void) {
505 uint64_t ctime
= mach_absolute_time();
506 assert(ctime
> acpi_wake_postrebase_abstime
);
507 return ((ctime
- acpi_wake_postrebase_abstime
) < 5 * NSEC_PER_SEC
);