]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/acpi.c
e15f077c29e65debcd15c0ca6f50aae272504f45
[apple/xnu.git] / osfmk / i386 / acpi.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30
31 #include <i386/misc_protos.h>
32 #include <i386/proc_reg.h>
33 #include <i386/pmap.h>
34 #include <i386/mtrr.h>
35 #include <i386/acpi.h>
36 #include <i386/fpu.h>
37 #include <i386/mp.h>
38 #include <i386/mp_desc.h>
39
40 #include <kern/cpu_data.h>
41
42 #include <IOKit/IOHibernatePrivate.h>
43 #include <IOKit/IOPlatformExpert.h>
44
45 extern void acpi_sleep_cpu(acpi_sleep_callback, void * refcon);
46 extern char acpi_wake_start[];
47 extern char acpi_wake_end[];
48
49 extern int serial_init(void);
50 extern unsigned int disableSerialOuput;
51
52 extern void set_kbd_leds(int leds);
53
54 extern void fpinit(void);
55
56 vm_offset_t
57 acpi_install_wake_handler(void)
58 {
59 /* copy wake code to ACPI_WAKE_ADDR in low memory */
60 bcopy_phys(kvtophys((vm_offset_t)acpi_wake_start),
61 (addr64_t) ACPI_WAKE_ADDR,
62 acpi_wake_end - acpi_wake_start);
63
64 /* flush cache */
65 wbinvd();
66
67 /* return physical address of the wakeup code */
68 return ACPI_WAKE_ADDR;
69 }
70
71 typedef struct acpi_hibernate_callback_data {
72 acpi_sleep_callback func;
73 void *refcon;
74 } acpi_hibernate_callback_data;
75
76 static void
77 acpi_hibernate(void *refcon)
78 {
79 boolean_t dohalt;
80
81 acpi_hibernate_callback_data *data = (acpi_hibernate_callback_data *)refcon;
82
83 if (current_cpu_datap()->cpu_hibernate) {
84
85 dohalt = hibernate_write_image();
86 if (dohalt)
87 {
88 // off
89 HIBLOG("power off\n");
90 if (PE_halt_restart)
91 (*PE_halt_restart)(kPEHaltCPU);
92 }
93 else
94 {
95 // sleep
96 HIBLOG("sleep\n");
97
98 // should we come back via regular wake, set the state in memory.
99 cpu_datap(0)->cpu_hibernate = 0;
100 }
101 }
102
103 (data->func)(data->refcon);
104
105 /* should never get here! */
106 }
107
108 void
109 acpi_sleep_kernel(acpi_sleep_callback func, void *refcon)
110 {
111 acpi_hibernate_callback_data data;
112 boolean_t did_hibernate;
113
114 kprintf("acpi_sleep_kernel hib=%d\n", current_cpu_datap()->cpu_hibernate);
115
116 /* shutdown local APIC before passing control to BIOS */
117 lapic_shutdown();
118
119 data.func = func;
120 data.refcon = refcon;
121
122 /* Save HPET state */
123 hpet_save();
124
125 /*
126 * If we're in 64-bit mode, drop back into legacy mode during sleep.
127 */
128 if (cpu_mode_is64bit()) {
129 cpu_IA32e_disable(current_cpu_datap());
130 kprintf("acpi_sleep_kernel legacy mode re-entered\n");
131 }
132
133 /*
134 * Save master CPU state and sleep platform.
135 * Will not return until platform is woken up,
136 * or if sleep failed.
137 */
138 acpi_sleep_cpu(acpi_hibernate, &data);
139
140 /* reset UART if kprintf is enabled */
141 if (FALSE == disableSerialOuput)
142 serial_init();
143
144 kprintf("ret from acpi_sleep_cpu hib=%d\n", current_cpu_datap()->cpu_hibernate);
145
146 if (current_cpu_datap()->cpu_hibernate) {
147 int i;
148 for (i=0; i<PMAP_NWINDOWS; i++) {
149 *current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP = 0;
150 }
151 current_cpu_datap()->cpu_hibernate = 0;
152 did_hibernate = TRUE;
153
154 } else {
155 did_hibernate = FALSE;
156 }
157
158 /* Re-enable 64-bit mode if necessary. */
159 if (cpu_mode_is64bit()) {
160 cpu_IA32e_enable(current_cpu_datap());
161 cpu_desc_load64(current_cpu_datap());
162 kprintf("acpi_sleep_kernel 64-bit mode re-enabled\n");
163 fast_syscall_init64();
164 } else {
165 fast_syscall_init();
166 }
167
168 /* restore MTRR settings */
169 mtrr_update_cpu();
170
171 /* set up PAT following boot processor power up */
172 pat_init();
173
174 if (did_hibernate) {
175 hibernate_machine_init();
176 }
177
178 /* re-enable and re-init local apic */
179 if (lapic_probe())
180 lapic_init();
181
182 /* Restore HPET state */
183 hpet_restore();
184
185 /* let the realtime clock reset */
186 rtc_sleep_wakeup();
187
188 fpinit();
189 clear_fpu();
190
191 if (did_hibernate) {
192 enable_preemption();
193 }
194 }