]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000-2009 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
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. | |
14 | * | |
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 | |
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. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
27 | */ | |
28 | ||
29 | #include <i386/pmap.h> | |
30 | #include <i386/proc_reg.h> | |
31 | #include <i386/mp_desc.h> | |
32 | #include <i386/misc_protos.h> | |
33 | #include <i386/mp.h> | |
34 | #include <i386/cpu_data.h> | |
35 | #if CONFIG_MTRR | |
36 | #include <i386/mtrr.h> | |
37 | #endif | |
38 | #if CONFIG_VMX | |
39 | #include <i386/vmx/vmx_cpu.h> | |
40 | #endif | |
41 | #include <i386/ucode.h> | |
42 | #include <i386/acpi.h> | |
43 | #include <i386/fpu.h> | |
44 | #include <i386/lapic.h> | |
45 | #include <i386/mp.h> | |
46 | #include <i386/mp_desc.h> | |
47 | #include <i386/serial_io.h> | |
48 | #if CONFIG_MCA | |
49 | #include <i386/machine_check.h> | |
50 | #endif | |
51 | #include <i386/pmCPU.h> | |
52 | ||
53 | #include <i386/tsc.h> | |
54 | ||
55 | #include <kern/cpu_data.h> | |
56 | #include <console/serial_protos.h> | |
57 | #include <machine/pal_routines.h> | |
58 | #include <vm/vm_page.h> | |
59 | ||
60 | #if HIBERNATION | |
61 | #include <IOKit/IOHibernatePrivate.h> | |
62 | #endif | |
63 | #include <IOKit/IOPlatformExpert.h> | |
64 | ||
65 | #include <sys/kdebug.h> | |
66 | ||
67 | #if CONFIG_SLEEP | |
68 | extern void acpi_sleep_cpu(acpi_sleep_callback, void * refcon); | |
69 | extern void acpi_wake_prot(void); | |
70 | #endif | |
71 | ||
72 | extern void fpinit(void); | |
73 | ||
74 | vm_offset_t | |
75 | acpi_install_wake_handler(void) | |
76 | { | |
77 | #if CONFIG_SLEEP | |
78 | install_real_mode_bootstrap(acpi_wake_prot); | |
79 | return REAL_MODE_BOOTSTRAP_OFFSET; | |
80 | #else | |
81 | return 0; | |
82 | #endif | |
83 | } | |
84 | ||
85 | #if HIBERNATION | |
86 | struct acpi_hibernate_callback_data { | |
87 | acpi_sleep_callback func; | |
88 | void *refcon; | |
89 | }; | |
90 | typedef struct acpi_hibernate_callback_data acpi_hibernate_callback_data_t; | |
91 | ||
92 | unsigned int save_kdebug_enable = 0; | |
93 | static uint64_t acpi_sleep_abstime; | |
94 | ||
95 | ||
96 | #if CONFIG_SLEEP | |
97 | static void | |
98 | acpi_hibernate(void *refcon) | |
99 | { | |
100 | uint32_t mode; | |
101 | ||
102 | acpi_hibernate_callback_data_t *data = | |
103 | (acpi_hibernate_callback_data_t *)refcon; | |
104 | ||
105 | if (current_cpu_datap()->cpu_hibernate) | |
106 | { | |
107 | #if defined(__i386__) | |
108 | cpu_IA32e_enable(current_cpu_datap()); | |
109 | #endif | |
110 | mode = hibernate_write_image(); | |
111 | ||
112 | if( mode == kIOHibernatePostWriteHalt ) | |
113 | { | |
114 | // off | |
115 | HIBLOG("power off\n"); | |
116 | if (PE_halt_restart) (*PE_halt_restart)(kPEHaltCPU); | |
117 | } | |
118 | else if( mode == kIOHibernatePostWriteRestart ) | |
119 | { | |
120 | // restart | |
121 | HIBLOG("restart\n"); | |
122 | if (PE_halt_restart) (*PE_halt_restart)(kPERestartCPU); | |
123 | } | |
124 | else | |
125 | { | |
126 | // sleep | |
127 | HIBLOG("sleep\n"); | |
128 | ||
129 | // should we come back via regular wake, set the state in memory. | |
130 | cpu_datap(0)->cpu_hibernate = 0; | |
131 | } | |
132 | ||
133 | #if defined(__i386__) | |
134 | /* | |
135 | * If we're in 64-bit mode, drop back into legacy mode during sleep. | |
136 | */ | |
137 | cpu_IA32e_disable(current_cpu_datap()); | |
138 | #endif | |
139 | } | |
140 | kdebug_enable = 0; | |
141 | ||
142 | acpi_sleep_abstime = mach_absolute_time(); | |
143 | ||
144 | (data->func)(data->refcon); | |
145 | ||
146 | /* should never get here! */ | |
147 | } | |
148 | #endif /* CONFIG_SLEEP */ | |
149 | #endif /* HIBERNATION */ | |
150 | ||
151 | extern void slave_pstart(void); | |
152 | ||
153 | ||
154 | void | |
155 | acpi_sleep_kernel(acpi_sleep_callback func, void *refcon) | |
156 | { | |
157 | #if HIBERNATION | |
158 | acpi_hibernate_callback_data_t data; | |
159 | #endif | |
160 | boolean_t did_hibernate; | |
161 | unsigned int cpu; | |
162 | kern_return_t rc; | |
163 | unsigned int my_cpu; | |
164 | uint64_t now; | |
165 | uint64_t my_tsc; | |
166 | uint64_t my_abs; | |
167 | ||
168 | kprintf("acpi_sleep_kernel hib=%d, cpu=%d\n", | |
169 | current_cpu_datap()->cpu_hibernate, cpu_number()); | |
170 | ||
171 | /* Get all CPUs to be in the "off" state */ | |
172 | my_cpu = cpu_number(); | |
173 | for (cpu = 0; cpu < real_ncpus; cpu += 1) { | |
174 | if (cpu == my_cpu) | |
175 | continue; | |
176 | rc = pmCPUExitHaltToOff(cpu); | |
177 | if (rc != KERN_SUCCESS) | |
178 | panic("Error %d trying to transition CPU %d to OFF", | |
179 | rc, cpu); | |
180 | } | |
181 | ||
182 | /* shutdown local APIC before passing control to firmware */ | |
183 | lapic_shutdown(); | |
184 | ||
185 | #if HIBERNATION | |
186 | data.func = func; | |
187 | data.refcon = refcon; | |
188 | #endif | |
189 | ||
190 | /* Save power management timer state */ | |
191 | pmTimerSave(); | |
192 | ||
193 | #if CONFIG_VMX | |
194 | /* | |
195 | * Turn off VT, otherwise switching to legacy mode will fail | |
196 | */ | |
197 | vmx_suspend(); | |
198 | #endif | |
199 | ||
200 | #if defined(__i386__) | |
201 | /* | |
202 | * If we're in 64-bit mode, drop back into legacy mode during sleep. | |
203 | */ | |
204 | cpu_IA32e_disable(current_cpu_datap()); | |
205 | #endif | |
206 | /* | |
207 | * Enable FPU/SIMD unit for potential hibernate acceleration | |
208 | */ | |
209 | clear_ts(); | |
210 | ||
211 | KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 0) | DBG_FUNC_START, 0, 0, 0, 0, 0); | |
212 | ||
213 | save_kdebug_enable = kdebug_enable; | |
214 | kdebug_enable = 0; | |
215 | ||
216 | acpi_sleep_abstime = mach_absolute_time(); | |
217 | ||
218 | #if CONFIG_SLEEP | |
219 | /* | |
220 | * Save master CPU state and sleep platform. | |
221 | * Will not return until platform is woken up, | |
222 | * or if sleep failed. | |
223 | */ | |
224 | #ifdef __x86_64__ | |
225 | uint64_t old_cr3 = x86_64_pre_sleep(); | |
226 | #endif | |
227 | #if HIBERNATION | |
228 | acpi_sleep_cpu(acpi_hibernate, &data); | |
229 | #else | |
230 | acpi_sleep_cpu(func, refcon); | |
231 | #endif | |
232 | ||
233 | #ifdef __x86_64__ | |
234 | x86_64_post_sleep(old_cr3); | |
235 | #endif | |
236 | ||
237 | #endif /* CONFIG_SLEEP */ | |
238 | ||
239 | /* Reset UART if kprintf is enabled. | |
240 | * However kprintf should not be used before rtc_sleep_wakeup() | |
241 | * for compatibility with firewire kprintf. | |
242 | */ | |
243 | ||
244 | if (FALSE == disable_serial_output) | |
245 | pal_serial_init(); | |
246 | ||
247 | #if HIBERNATION | |
248 | if (current_cpu_datap()->cpu_hibernate) { | |
249 | #if defined(__i386__) | |
250 | int i; | |
251 | for (i = 0; i < PMAP_NWINDOWS; i++) | |
252 | *current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP = 0; | |
253 | #endif | |
254 | did_hibernate = TRUE; | |
255 | ||
256 | } else | |
257 | #endif | |
258 | { | |
259 | did_hibernate = FALSE; | |
260 | } | |
261 | ||
262 | /* Re-enable mode (including 64-bit if applicable) */ | |
263 | cpu_mode_init(current_cpu_datap()); | |
264 | ||
265 | #if CONFIG_MCA | |
266 | /* Re-enable machine check handling */ | |
267 | mca_cpu_init(); | |
268 | #endif | |
269 | ||
270 | #if CONFIG_MTRR | |
271 | /* restore MTRR settings */ | |
272 | mtrr_update_cpu(); | |
273 | #endif | |
274 | ||
275 | /* update CPU microcode */ | |
276 | ucode_update_wake(); | |
277 | ||
278 | #if CONFIG_VMX | |
279 | /* | |
280 | * Restore VT mode | |
281 | */ | |
282 | vmx_resume(); | |
283 | #endif | |
284 | ||
285 | #if CONFIG_MTRR | |
286 | /* set up PAT following boot processor power up */ | |
287 | pat_init(); | |
288 | #endif | |
289 | ||
290 | /* | |
291 | * Go through all of the CPUs and mark them as requiring | |
292 | * a full restart. | |
293 | */ | |
294 | pmMarkAllCPUsOff(); | |
295 | ||
296 | ml_get_timebase(&now); | |
297 | ||
298 | /* re-enable and re-init local apic (prior to starting timers) */ | |
299 | if (lapic_probe()) | |
300 | lapic_configure(); | |
301 | ||
302 | /* let the realtime clock reset */ | |
303 | rtc_sleep_wakeup(acpi_sleep_abstime); | |
304 | ||
305 | kdebug_enable = save_kdebug_enable; | |
306 | ||
307 | if (did_hibernate) { | |
308 | ||
309 | my_tsc = (now >> 32) | (now << 32); | |
310 | my_abs = tmrCvt(my_tsc, tscFCvtt2n); | |
311 | ||
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); | |
316 | ||
317 | current_cpu_datap()->cpu_hibernate = 0; | |
318 | ||
319 | KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 0) | DBG_FUNC_END, 0, 0, 0, 0, 0); | |
320 | } else | |
321 | KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 0) | DBG_FUNC_END, 0, 0, 0, 0, 0); | |
322 | ||
323 | /* Restore power management register state */ | |
324 | pmCPUMarkRunning(current_cpu_datap()); | |
325 | ||
326 | /* Restore power management timer state */ | |
327 | pmTimerRestore(); | |
328 | ||
329 | /* Restart timer interrupts */ | |
330 | rtc_timer_start(); | |
331 | ||
332 | /* Reconfigure FP/SIMD unit */ | |
333 | init_fpu(); | |
334 | ||
335 | #if HIBERNATION | |
336 | #ifdef __i386__ | |
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 | |
340 | * enable it here */ | |
341 | if (did_hibernate) | |
342 | enable_preemption(); | |
343 | #endif | |
344 | ||
345 | kprintf("ret from acpi_sleep_cpu hib=%d\n", did_hibernate); | |
346 | #endif | |
347 | ||
348 | #if CONFIG_SLEEP | |
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); | |
353 | #endif | |
354 | } | |
355 | ||
356 | extern char real_mode_bootstrap_end[]; | |
357 | extern char real_mode_bootstrap_base[]; | |
358 | ||
359 | void | |
360 | install_real_mode_bootstrap(void *prot_entry) | |
361 | { | |
362 | /* | |
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(). | |
368 | */ | |
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); | |
372 | ||
373 | /* | |
374 | * Set the location at the base of the stack to point to the | |
375 | * common startup entry. | |
376 | */ | |
377 | ml_phys_write_word( | |
378 | PROT_MODE_START+REAL_MODE_BOOTSTRAP_OFFSET, | |
379 | (unsigned int)kvtophys((vm_offset_t)prot_entry)); | |
380 | ||
381 | /* Flush caches */ | |
382 | __asm__("wbinvd"); | |
383 | } | |
384 |