2 * Copyright (c) 2000-2009 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 <i386/vmx/vmx_cpu.h>
41 #include <i386/ucode.h>
42 #include <i386/acpi.h>
44 #include <i386/lapic.h>
46 #include <i386/mp_desc.h>
47 #include <i386/serial_io.h>
49 #include <i386/machine_check.h>
51 #include <i386/pmCPU.h>
55 #include <kern/cpu_data.h>
56 #include <console/serial_protos.h>
57 #include <machine/pal_routines.h>
58 #include <vm/vm_page.h>
61 #include <IOKit/IOHibernatePrivate.h>
63 #include <IOKit/IOPlatformExpert.h>
65 #include <sys/kdebug.h>
68 extern void acpi_sleep_cpu(acpi_sleep_callback
, void * refcon
);
69 extern void acpi_wake_prot(void);
72 extern void fpinit(void);
75 acpi_install_wake_handler(void)
78 install_real_mode_bootstrap(acpi_wake_prot
);
79 return REAL_MODE_BOOTSTRAP_OFFSET
;
86 struct acpi_hibernate_callback_data
{
87 acpi_sleep_callback func
;
90 typedef struct acpi_hibernate_callback_data acpi_hibernate_callback_data_t
;
92 unsigned int save_kdebug_enable
= 0;
93 static uint64_t acpi_sleep_abstime
;
98 acpi_hibernate(void *refcon
)
102 acpi_hibernate_callback_data_t
*data
=
103 (acpi_hibernate_callback_data_t
*)refcon
;
105 if (current_cpu_datap()->cpu_hibernate
)
107 #if defined(__i386__)
108 cpu_IA32e_enable(current_cpu_datap());
110 mode
= hibernate_write_image();
112 if( mode
== kIOHibernatePostWriteHalt
)
115 HIBLOG("power off\n");
116 if (PE_halt_restart
) (*PE_halt_restart
)(kPEHaltCPU
);
118 else if( mode
== kIOHibernatePostWriteRestart
)
122 if (PE_halt_restart
) (*PE_halt_restart
)(kPERestartCPU
);
129 // should we come back via regular wake, set the state in memory.
130 cpu_datap(0)->cpu_hibernate
= 0;
133 #if defined(__i386__)
135 * If we're in 64-bit mode, drop back into legacy mode during sleep.
137 cpu_IA32e_disable(current_cpu_datap());
142 acpi_sleep_abstime
= mach_absolute_time();
144 (data
->func
)(data
->refcon
);
146 /* should never get here! */
148 #endif /* CONFIG_SLEEP */
149 #endif /* HIBERNATION */
151 extern void slave_pstart(void);
155 acpi_sleep_kernel(acpi_sleep_callback func
, void *refcon
)
158 acpi_hibernate_callback_data_t data
;
160 boolean_t did_hibernate
;
168 kprintf("acpi_sleep_kernel hib=%d, cpu=%d\n",
169 current_cpu_datap()->cpu_hibernate
, cpu_number());
171 /* Get all CPUs to be in the "off" state */
172 my_cpu
= cpu_number();
173 for (cpu
= 0; cpu
< real_ncpus
; cpu
+= 1) {
176 rc
= pmCPUExitHaltToOff(cpu
);
177 if (rc
!= KERN_SUCCESS
)
178 panic("Error %d trying to transition CPU %d to OFF",
182 /* shutdown local APIC before passing control to firmware */
187 data
.refcon
= refcon
;
190 /* Save power management timer state */
195 * Turn off VT, otherwise switching to legacy mode will fail
200 #if defined(__i386__)
202 * If we're in 64-bit mode, drop back into legacy mode during sleep.
204 cpu_IA32e_disable(current_cpu_datap());
207 * Enable FPU/SIMD unit for potential hibernate acceleration
211 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE
, 0) | DBG_FUNC_START
, 0, 0, 0, 0, 0);
213 save_kdebug_enable
= kdebug_enable
;
216 acpi_sleep_abstime
= mach_absolute_time();
220 * Save master CPU state and sleep platform.
221 * Will not return until platform is woken up,
222 * or if sleep failed.
225 uint64_t old_cr3
= x86_64_pre_sleep();
228 acpi_sleep_cpu(acpi_hibernate
, &data
);
230 acpi_sleep_cpu(func
, refcon
);
234 x86_64_post_sleep(old_cr3
);
237 #endif /* CONFIG_SLEEP */
239 /* Reset UART if kprintf is enabled.
240 * However kprintf should not be used before rtc_sleep_wakeup()
241 * for compatibility with firewire kprintf.
244 if (FALSE
== disable_serial_output
)
248 if (current_cpu_datap()->cpu_hibernate
) {
249 #if defined(__i386__)
251 for (i
= 0; i
< PMAP_NWINDOWS
; i
++)
252 *current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
= 0;
254 did_hibernate
= TRUE
;
259 did_hibernate
= FALSE
;
262 /* Re-enable mode (including 64-bit if applicable) */
263 cpu_mode_init(current_cpu_datap());
266 /* Re-enable machine check handling */
271 /* restore MTRR settings */
275 /* update CPU microcode */
286 /* set up PAT following boot processor power up */
291 * Go through all of the CPUs and mark them as requiring
296 ml_get_timebase(&now
);
298 /* re-enable and re-init local apic (prior to starting timers) */
302 /* let the realtime clock reset */
303 rtc_sleep_wakeup(acpi_sleep_abstime
);
305 kdebug_enable
= save_kdebug_enable
;
309 my_tsc
= (now
>> 32) | (now
<< 32);
310 my_abs
= tmrCvt(my_tsc
, tscFCvtt2n
);
312 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE
, 2) | DBG_FUNC_START
,
313 (uint32_t)(my_abs
>> 32), (uint32_t)my_abs
, 0, 0, 0);
314 hibernate_machine_init();
315 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE
, 2) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
317 current_cpu_datap()->cpu_hibernate
= 0;
319 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE
, 0) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
321 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE
, 0) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
323 /* Restore power management register state */
324 pmCPUMarkRunning(current_cpu_datap());
326 /* Restore power management timer state */
329 /* Restart timer interrupts */
332 /* Reconfigure FP/SIMD unit */
337 /* The image is written out using the copy engine, which disables
338 * preemption. Since the copy engine writes out the page which contains
339 * the preemption variable when it is disabled, we need to explicitly
345 kprintf("ret from acpi_sleep_cpu hib=%d\n", did_hibernate
);
349 /* Becase we don't save the bootstrap page, and we share it
350 * between sleep and mp slave init, we need to recreate it
351 * after coming back from sleep or hibernate */
352 install_real_mode_bootstrap(slave_pstart
);
356 extern char real_mode_bootstrap_end
[];
357 extern char real_mode_bootstrap_base
[];
360 install_real_mode_bootstrap(void *prot_entry
)
363 * Copy the boot entry code to the real-mode vector area REAL_MODE_BOOTSTRAP_OFFSET.
364 * This is in page 1 which has been reserved for this purpose by
365 * machine_startup() from the boot processor.
366 * The slave boot code is responsible for switching to protected
367 * mode and then jumping to the common startup, _start().
369 bcopy_phys(kvtophys((vm_offset_t
) real_mode_bootstrap_base
),
370 (addr64_t
) REAL_MODE_BOOTSTRAP_OFFSET
,
371 real_mode_bootstrap_end
-real_mode_bootstrap_base
);
374 * Set the location at the base of the stack to point to the
375 * common startup entry.
378 PROT_MODE_START
+REAL_MODE_BOOTSTRAP_OFFSET
,
379 (unsigned int)kvtophys((vm_offset_t
)prot_entry
));