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>
35 #include <i386/mtrr.h>
37 #include <i386/vmx/vmx_cpu.h>
39 #include <i386/acpi.h>
41 #include <i386/lapic.h>
43 #include <i386/mp_desc.h>
44 #include <i386/serial_io.h>
46 #include <i386/machine_check.h>
48 #include <i386/pmCPU.h>
52 #include <kern/cpu_data.h>
53 #include <console/serial_protos.h>
54 #include <vm/vm_page.h>
57 #include <IOKit/IOHibernatePrivate.h>
59 #include <IOKit/IOPlatformExpert.h>
61 #include <sys/kdebug.h>
64 extern void acpi_sleep_cpu(acpi_sleep_callback
, void * refcon
);
65 extern void acpi_wake_prot(void);
68 extern void fpinit(void);
71 acpi_install_wake_handler(void)
74 install_real_mode_bootstrap(acpi_wake_prot
);
75 return REAL_MODE_BOOTSTRAP_OFFSET
;
82 struct acpi_hibernate_callback_data
{
83 acpi_sleep_callback func
;
86 typedef struct acpi_hibernate_callback_data acpi_hibernate_callback_data_t
;
88 unsigned int save_kdebug_enable
= 0;
89 static uint64_t acpi_sleep_abstime
;
94 acpi_hibernate(void *refcon
)
98 acpi_hibernate_callback_data_t
*data
=
99 (acpi_hibernate_callback_data_t
*)refcon
;
101 if (current_cpu_datap()->cpu_hibernate
)
103 #if defined(__i386__)
104 cpu_IA32e_enable(current_cpu_datap());
107 mode
= hibernate_write_image();
109 if( mode
== kIOHibernatePostWriteHalt
)
112 HIBLOG("power off\n");
113 if (PE_halt_restart
) (*PE_halt_restart
)(kPEHaltCPU
);
115 else if( mode
== kIOHibernatePostWriteRestart
)
119 if (PE_halt_restart
) (*PE_halt_restart
)(kPERestartCPU
);
126 // should we come back via regular wake, set the state in memory.
127 cpu_datap(0)->cpu_hibernate
= 0;
130 #if defined(__i386__)
132 * If we're in 64-bit mode, drop back into legacy mode during sleep.
134 cpu_IA32e_disable(current_cpu_datap());
139 acpi_sleep_abstime
= mach_absolute_time();
141 (data
->func
)(data
->refcon
);
143 /* should never get here! */
145 #endif /* CONFIG_SLEEP */
146 #endif /* HIBERNATION */
148 extern void slave_pstart(void);
151 acpi_sleep_kernel(acpi_sleep_callback func
, void *refcon
)
154 acpi_hibernate_callback_data_t data
;
156 boolean_t did_hibernate
;
164 kprintf("acpi_sleep_kernel hib=%d\n",
165 current_cpu_datap()->cpu_hibernate
);
167 /* Get all CPUs to be in the "off" state */
168 my_cpu
= cpu_number();
169 for (cpu
= 0; cpu
< real_ncpus
; cpu
+= 1) {
172 rc
= pmCPUExitHaltToOff(cpu
);
173 if (rc
!= KERN_SUCCESS
)
174 panic("Error %d trying to transition CPU %d to OFF",
178 /* shutdown local APIC before passing control to BIOS */
183 data
.refcon
= refcon
;
186 /* Save power management timer state */
191 * Turn off VT, otherwise switching to legacy mode will fail
196 #if defined(__i386__)
198 * If we're in 64-bit mode, drop back into legacy mode during sleep.
200 cpu_IA32e_disable(current_cpu_datap());
202 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE
, 0) | DBG_FUNC_START
, 0, 0, 0, 0, 0);
204 save_kdebug_enable
= kdebug_enable
;
207 acpi_sleep_abstime
= mach_absolute_time();
211 * Save master CPU state and sleep platform.
212 * Will not return until platform is woken up,
213 * or if sleep failed.
216 uint64_t old_cr3
= x86_64_pre_sleep();
219 acpi_sleep_cpu(acpi_hibernate
, &data
);
221 acpi_sleep_cpu(func
, refcon
);
224 x86_64_post_sleep(old_cr3
);
227 #endif /* CONFIG_SLEEP */
229 /* Reset UART if kprintf is enabled.
230 * However kprintf should not be used before rtc_sleep_wakeup()
231 * for compatibility with firewire kprintf.
234 if (FALSE
== disable_serial_output
)
238 if (current_cpu_datap()->cpu_hibernate
) {
239 #if defined(__i386__)
241 for (i
= 0; i
< PMAP_NWINDOWS
; i
++)
242 *current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
= 0;
244 did_hibernate
= TRUE
;
249 did_hibernate
= FALSE
;
252 /* Re-enable mode (including 64-bit if applicable) */
253 cpu_mode_init(current_cpu_datap());
256 /* Re-enable machine check handling */
260 /* restore MTRR settings */
270 /* set up PAT following boot processor power up */
274 * Go through all of the CPUs and mark them as requiring
279 ml_get_timebase(&now
);
281 /* let the realtime clock reset */
282 rtc_sleep_wakeup(acpi_sleep_abstime
);
284 kdebug_enable
= save_kdebug_enable
;
288 my_tsc
= (now
>> 32) | (now
<< 32);
289 my_abs
= tmrCvt(my_tsc
, tscFCvtt2n
);
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);
296 current_cpu_datap()->cpu_hibernate
= 0;
298 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE
, 0) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
300 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE
, 0) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
302 /* re-enable and re-init local apic */
306 /* Restore power management register state */
307 pmCPUMarkRunning(current_cpu_datap());
309 /* Restore power management timer state */
312 /* Restart tick interrupts from the LAPIC timer */
313 rtc_lapic_start_ticking();
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
328 kprintf("ret from acpi_sleep_cpu hib=%d\n", did_hibernate
);
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
);
339 extern char real_mode_bootstrap_end
[];
340 extern char real_mode_bootstrap_base
[];
343 install_real_mode_bootstrap(void *prot_entry
)
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().
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
);
357 * Set the location at the base of the stack to point to the
358 * common startup entry.
361 PROT_MODE_START
+REAL_MODE_BOOTSTRAP_OFFSET
,
362 (unsigned int)kvtophys((vm_offset_t
)prot_entry
));