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);
71 extern kern_return_t
IOCPURunPlatformQuiesceActions(void);
72 extern kern_return_t
IOCPURunPlatformActiveActions(void);
74 extern void fpinit(void);
77 acpi_install_wake_handler(void)
80 install_real_mode_bootstrap(acpi_wake_prot
);
81 return REAL_MODE_BOOTSTRAP_OFFSET
;
88 struct acpi_hibernate_callback_data
{
89 acpi_sleep_callback func
;
92 typedef struct acpi_hibernate_callback_data acpi_hibernate_callback_data_t
;
94 unsigned int save_kdebug_enable
= 0;
95 static uint64_t acpi_sleep_abstime
;
99 acpi_hibernate(void *refcon
)
103 acpi_hibernate_callback_data_t
*data
=
104 (acpi_hibernate_callback_data_t
*)refcon
;
106 if (current_cpu_datap()->cpu_hibernate
)
108 #if defined(__i386__)
109 cpu_IA32e_enable(current_cpu_datap());
111 mode
= hibernate_write_image();
113 if( mode
== kIOHibernatePostWriteHalt
)
116 HIBLOG("power off\n");
117 if (PE_halt_restart
) (*PE_halt_restart
)(kPEHaltCPU
);
119 else if( mode
== kIOHibernatePostWriteRestart
)
123 if (PE_halt_restart
) (*PE_halt_restart
)(kPERestartCPU
);
130 // should we come back via regular wake, set the state in memory.
131 cpu_datap(0)->cpu_hibernate
= 0;
134 #if defined(__i386__)
136 * If we're in 64-bit mode, drop back into legacy mode during sleep.
138 cpu_IA32e_disable(current_cpu_datap());
143 IOCPURunPlatformQuiesceActions();
145 acpi_sleep_abstime
= mach_absolute_time();
147 (data
->func
)(data
->refcon
);
149 /* should never get here! */
151 #endif /* CONFIG_SLEEP */
152 #endif /* HIBERNATION */
154 extern void slave_pstart(void);
158 acpi_sleep_kernel(acpi_sleep_callback func
, void *refcon
)
161 acpi_hibernate_callback_data_t data
;
163 boolean_t did_hibernate
;
171 kprintf("acpi_sleep_kernel hib=%d, cpu=%d\n",
172 current_cpu_datap()->cpu_hibernate
, cpu_number());
174 /* Get all CPUs to be in the "off" state */
175 my_cpu
= cpu_number();
176 for (cpu
= 0; cpu
< real_ncpus
; cpu
+= 1) {
179 rc
= pmCPUExitHaltToOff(cpu
);
180 if (rc
!= KERN_SUCCESS
)
181 panic("Error %d trying to transition CPU %d to OFF",
185 /* shutdown local APIC before passing control to firmware */
190 data
.refcon
= refcon
;
193 /* Save power management timer state */
198 * Turn off VT, otherwise switching to legacy mode will fail
203 #if defined(__i386__)
205 * If we're in 64-bit mode, drop back into legacy mode during sleep.
207 cpu_IA32e_disable(current_cpu_datap());
210 * Enable FPU/SIMD unit for potential hibernate acceleration
214 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE
, 0) | DBG_FUNC_START
, 0, 0, 0, 0, 0);
216 save_kdebug_enable
= kdebug_enable
;
219 acpi_sleep_abstime
= mach_absolute_time();
223 * Save master CPU state and sleep platform.
224 * Will not return until platform is woken up,
225 * or if sleep failed.
228 uint64_t old_cr3
= x86_64_pre_sleep();
231 acpi_sleep_cpu(acpi_hibernate
, &data
);
233 acpi_sleep_cpu(func
, refcon
);
237 x86_64_post_sleep(old_cr3
);
240 #endif /* CONFIG_SLEEP */
242 /* Reset UART if kprintf is enabled.
243 * However kprintf should not be used before rtc_sleep_wakeup()
244 * for compatibility with firewire kprintf.
247 if (FALSE
== disable_serial_output
)
251 if (current_cpu_datap()->cpu_hibernate
) {
252 #if defined(__i386__)
254 for (i
= 0; i
< PMAP_NWINDOWS
; i
++)
255 *current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
= 0;
257 did_hibernate
= TRUE
;
262 did_hibernate
= FALSE
;
265 /* Re-enable mode (including 64-bit if applicable) */
266 cpu_mode_init(current_cpu_datap());
269 /* Re-enable machine check handling */
274 /* restore MTRR settings */
278 /* update CPU microcode */
289 /* set up PAT following boot processor power up */
294 * Go through all of the CPUs and mark them as requiring
299 ml_get_timebase(&now
);
301 /* re-enable and re-init local apic (prior to starting timers) */
305 /* let the realtime clock reset */
306 rtc_sleep_wakeup(acpi_sleep_abstime
);
308 kdebug_enable
= save_kdebug_enable
;
310 IOCPURunPlatformActiveActions();
314 my_tsc
= (now
>> 32) | (now
<< 32);
315 my_abs
= tmrCvt(my_tsc
, tscFCvtt2n
);
317 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE
, 2) | DBG_FUNC_START
,
318 (uint32_t)(my_abs
>> 32), (uint32_t)my_abs
, 0, 0, 0);
319 hibernate_machine_init();
320 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE
, 2) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
322 current_cpu_datap()->cpu_hibernate
= 0;
324 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE
, 0) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
326 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE
, 0) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
328 /* Restore power management register state */
329 pmCPUMarkRunning(current_cpu_datap());
331 /* Restore power management timer state */
334 /* Restart timer interrupts */
337 /* Reconfigure FP/SIMD unit */
342 /* The image is written out using the copy engine, which disables
343 * preemption. Since the copy engine writes out the page which contains
344 * the preemption variable when it is disabled, we need to explicitly
350 kprintf("ret from acpi_sleep_cpu hib=%d\n", did_hibernate
);
354 /* Becase we don't save the bootstrap page, and we share it
355 * between sleep and mp slave init, we need to recreate it
356 * after coming back from sleep or hibernate */
357 install_real_mode_bootstrap(slave_pstart
);
361 extern char real_mode_bootstrap_end
[];
362 extern char real_mode_bootstrap_base
[];
365 install_real_mode_bootstrap(void *prot_entry
)
368 * Copy the boot entry code to the real-mode vector area REAL_MODE_BOOTSTRAP_OFFSET.
369 * This is in page 1 which has been reserved for this purpose by
370 * machine_startup() from the boot processor.
371 * The slave boot code is responsible for switching to protected
372 * mode and then jumping to the common startup, _start().
374 bcopy_phys(kvtophys((vm_offset_t
) real_mode_bootstrap_base
),
375 (addr64_t
) REAL_MODE_BOOTSTRAP_OFFSET
,
376 real_mode_bootstrap_end
-real_mode_bootstrap_base
);
379 * Set the location at the base of the stack to point to the
380 * common startup entry.
383 PROT_MODE_START
+REAL_MODE_BOOTSTRAP_OFFSET
,
384 (unsigned int)kvtophys((vm_offset_t
)prot_entry
));