]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/acpi.c
a70821010ea0e07081c6d77c59589444bdb20490
[apple/xnu.git] / osfmk / i386 / acpi.c
1 /*
2 * Copyright (c) 2000 Apple Computer, 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/misc_protos.h>
30 #include <i386/proc_reg.h>
31 #include <i386/pmap.h>
32 #include <i386/mtrr.h>
33 #include <i386/acpi.h>
34 #include <i386/fpu.h>
35 #include <i386/mp.h>
36 #include <i386/mp_desc.h>
37 #include <i386/machine_check.h>
38
39 #include <kern/cpu_data.h>
40
41 #include <IOKit/IOHibernatePrivate.h>
42 #include <IOKit/IOPlatformExpert.h>
43
44 extern void acpi_sleep_cpu(acpi_sleep_callback, void * refcon);
45 extern char acpi_wake_start[];
46 extern char acpi_wake_end[];
47
48 extern int serial_init(void);
49 extern unsigned int disableSerialOuput;
50
51 extern void set_kbd_leds(int leds);
52
53 extern void fpinit(void);
54
55 vm_offset_t
56 acpi_install_wake_handler(void)
57 {
58 /* copy wake code to ACPI_WAKE_ADDR in low memory */
59 bcopy_phys(kvtophys((vm_offset_t)acpi_wake_start),
60 (addr64_t) ACPI_WAKE_ADDR,
61 acpi_wake_end - acpi_wake_start);
62
63 /* flush cache */
64 wbinvd();
65
66 /* return physical address of the wakeup code */
67 return ACPI_WAKE_ADDR;
68 }
69
70 typedef struct acpi_hibernate_callback_data {
71 acpi_sleep_callback func;
72 void *refcon;
73 } acpi_hibernate_callback_data;
74
75 static void
76 acpi_hibernate(void *refcon)
77 {
78 boolean_t dohalt;
79
80 acpi_hibernate_callback_data *data = (acpi_hibernate_callback_data *)refcon;
81
82 if (current_cpu_datap()->cpu_hibernate) {
83
84 dohalt = hibernate_write_image();
85 if (dohalt)
86 {
87 // off
88 HIBLOG("power off\n");
89 if (PE_halt_restart)
90 (*PE_halt_restart)(kPEHaltCPU);
91 }
92 else
93 {
94 // sleep
95 HIBLOG("sleep\n");
96
97 // should we come back via regular wake, set the state in memory.
98 cpu_datap(0)->cpu_hibernate = 0;
99 }
100 }
101
102 (data->func)(data->refcon);
103
104 /* should never get here! */
105 }
106
107 void
108 acpi_sleep_kernel(acpi_sleep_callback func, void *refcon)
109 {
110 acpi_hibernate_callback_data data;
111 boolean_t did_hibernate;
112
113 kprintf("acpi_sleep_kernel hib=%d\n", current_cpu_datap()->cpu_hibernate);
114
115 /* shutdown local APIC before passing control to BIOS */
116 lapic_shutdown();
117
118 data.func = func;
119 data.refcon = refcon;
120
121 /* Save HPET state */
122 hpet_save();
123
124 /*
125 * If we're in 64-bit mode, drop back into legacy mode during sleep.
126 */
127 if (cpu_mode_is64bit()) {
128 cpu_IA32e_disable(current_cpu_datap());
129 kprintf("acpi_sleep_kernel legacy mode re-entered\n");
130 }
131
132 /*
133 * Save master CPU state and sleep platform.
134 * Will not return until platform is woken up,
135 * or if sleep failed.
136 */
137 acpi_sleep_cpu(acpi_hibernate, &data);
138
139 /* reset UART if kprintf is enabled */
140 if (FALSE == disableSerialOuput)
141 serial_init();
142
143 kprintf("ret from acpi_sleep_cpu hib=%d\n", current_cpu_datap()->cpu_hibernate);
144
145 if (current_cpu_datap()->cpu_hibernate) {
146 int i;
147 for (i=0; i<PMAP_NWINDOWS; i++) {
148 *current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP = 0;
149 }
150 current_cpu_datap()->cpu_hibernate = 0;
151 did_hibernate = TRUE;
152
153 } else {
154 did_hibernate = FALSE;
155 }
156
157 /* Re-enable 64-bit mode if necessary. */
158 if (cpu_mode_is64bit()) {
159 cpu_IA32e_enable(current_cpu_datap());
160 cpu_desc_load64(current_cpu_datap());
161 kprintf("acpi_sleep_kernel 64-bit mode re-enabled\n");
162 fast_syscall_init64();
163 } else {
164 fast_syscall_init();
165 }
166
167 /* Re-enable machine check handling */
168 mca_cpu_init();
169
170 /* restore MTRR settings */
171 mtrr_update_cpu();
172
173 /* set up PAT following boot processor power up */
174 pat_init();
175
176 if (did_hibernate) {
177 hibernate_machine_init();
178 }
179
180 /* re-enable and re-init local apic */
181 if (lapic_probe())
182 lapic_init();
183
184 /* Restore HPET state */
185 hpet_restore();
186
187 /* let the realtime clock reset */
188 rtc_sleep_wakeup();
189
190 fpinit();
191 clear_fpu();
192
193 if (did_hibernate) {
194 enable_preemption();
195 }
196 }