2 * Copyright (c) 2000 Apple Computer, 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@
30 #include <i386/proc_reg.h>
31 #include <i386/postcode.h>
32 #include <i386/acpi.h>
38 .align 12 /* Page align for single bcopy_phys() */
40 #define LJMP(segment, address) \
42 .long address - EXT(acpi_wake_start) ;\
45 #define PA(addr) ((addr)-KERNELBASE)
50 * The code from acpi_wake_start to acpi_wake_end is copied to
51 * memory below 1MB. The firmware waking vector is updated to
52 * point at acpi_wake_start in low memory before sleeping.
55 ENTRY(acpi_wake_start)
57 * CPU woke up from sleep, and is back in real mode.
58 * Initialize it just enough to get back to protected mode.
62 POSTCODE(ACPI_WAKE_START_ENTRY)
64 /* set up DS to match CS */
69 * Must initialize GDTR before entering protected mode.
70 * Use a temporary GDT that is 0 based, 4GB limit, code and data.
71 * Restoring the actual GDT will come later.
75 lgdt EXT(acpi_gdtr) - EXT(acpi_wake_start)
77 /* set CR0.PE to enter protected mode */
84 * Make intra-segment jump to flush pipeline and reload CS register.
85 * If GDT is bogus, it will blow up here.
88 LJMP(0x8, acpi_wake_prot + ACPI_WAKE_ADDR)
92 /* protected mode, paging disabled */
94 /* setup the protected mode segment registers */
102 /* jump back to the sleep function in the kernel */
103 movl PA(saved_eip), %eax
106 /* Segment Descriptor
109 * ------------------------------------------------------------
110 * | | |B| |A| | | |1|0|E|W|A| |
111 * | BASE 31..24 |G|/|0|V| LIMIT |P|DPL| TYPE | BASE 23:16 |
112 * | | |D| |L| 19..16| | |1|1|C|R|A| |
113 * ------------------------------------------------------------
115 * | BASE 15..0 | LIMIT 15..0 |
117 * ------------------------------------------------------------
120 .word 0, 0 /* 0x0 : null */
123 .word 0xffff, 0x0000 /* 0x8 : code */
124 .byte 0, 0x9e, 0xcf, 0
126 .word 0xffff, 0x0000 /* 0x10 : data */
127 .byte 0, 0x92, 0xcf, 0
130 .word 24 /* limit (8*3 segs) */
131 .long EXT(acpi_gdt) - EXT(acpi_wake_start) + ACPI_WAKE_ADDR
137 * acpi_sleep_cpu(acpi_sleep_callback func, void * refcon)
139 * Save CPU state before platform sleep. Restore CPU state
143 ENTRY(acpi_sleep_cpu)
150 /* save general purpose registers */
154 /* save control registers */
164 /* save segment registers */
170 /* save descriptor table registers */
177 * When system wakes up, the real mode wake handler will revert to
178 * protected mode, then jump to the address stored at saved_eip.
180 movl $(PA(wake_prot)), saved_eip
183 * Call ACPI function provided by the caller to sleep the platform.
184 * This call will not return on success.
191 /* sleep failed, no cpu context lost */
196 /* protected mode, paging disabled */
197 POSTCODE(ACPI_WAKE_PROT_ENTRY)
199 /* restore kernel GDT */
202 /* restore control registers */
203 movl PA(saved_cr2), %eax
207 movl PA(EXT(IdlePDPT)), %eax
208 movl (%eax), %esi /* save orig */
210 movl %ebx, (%eax) /* identity map low mem */
213 movl PA(saved_cr4), %eax
216 movl PA(saved_cr4), %eax
220 * Temporarily use the page tables at IdlePTD
221 * to enable paging. Copy the KPTDI entry to
222 * entry 0 in the PTD to identity map the kernel.
224 movl PA(EXT(IdlePTD)), %eax
226 addl $(KPTDI << PTEINDX), %ebx /* bytes per PDE */
227 movl (%ebx), %ebx /* IdlePTD[KPTDI] */
228 movl (%eax), %esi /* save original IdlePTD[0] */
229 movl %ebx, (%eax) /* update IdlePTD[0] */
230 movl %eax, %cr3 /* CR3 = IdlePTD */
233 /* restore CR0, paging enabled */
234 movl PA(saved_cr0), %eax
237 /* switch to kernel code segment */
238 ljmpl $(KERNEL_CS), $wake_paged
242 /* protected mode, paging enabled */
243 POSTCODE(ACPI_WAKE_PAGED_ENTRY)
245 /* switch to kernel data segment */
246 movw $(KERNEL_DS), %ax
249 /* undo changes to IdlePTD */
251 movl EXT(IdlePDPT), %eax
253 movl EXT(IdlePTD), %eax
255 addl $(KERNELBASE), %eax /* make virtual */
258 /* restore real PDE base */
263 /* restore local and interrupt descriptor tables */
267 /* restore segment registers */
274 * Restore task register. Before doing this, clear the busy flag
275 * in the TSS descriptor set by the CPU.
277 movl $saved_gdt, %eax
278 movl 2(%eax), %edx /* GDT base, skip limit word */
279 movl $(KERNEL_TSS), %eax /* TSS segment selector */
280 movb $(K_TSS), 5(%edx, %eax) /* clear busy flag */
281 ltr saved_tr /* restore TR */
285 /* restore general purpose registers */
296 .section __HIB, __text
299 .globl EXT(acpi_wake_prot_entry)
300 ENTRY(acpi_wake_prot_entry)
301 /* protected mode, paging enabled */
302 POSTCODE(ACPI_WAKE_PAGED_ENTRY)
304 /* restore kernel GDT */
308 /* restore control registers */
313 /* switch to kernel data segment */
314 movw $(KERNEL_DS), %ax
318 /* restore real PDE base */
325 /* restore local and interrupt descriptor tables */
330 /* restore segment registers */
338 * Restore task register. Before doing this, clear the busy flag
339 * in the TSS descriptor set by the CPU.
341 movl $saved_gdt, %eax
342 movl 2(%eax), %edx /* GDT base, skip limit word */
343 movl $(KERNEL_TSS), %eax /* TSS segment selector */
344 movb $(K_TSS), 5(%edx, %eax) /* clear busy flag */
345 ltr saved_tr /* restore TR */
347 /* restore general purpose registers */
354 /* make sure interrupts are disabled */
364 .section __HIB, __data
369 * CPU registers saved across sleep/wake.