2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
24 #include <i386/proc_reg.h>
25 #include <i386/postcode.h>
26 #include <i386/acpi.h>
32 .align 12 /* Page align for single bcopy_phys() */
34 #define LJMP(segment, address) \
36 .long address - EXT(acpi_wake_start) ;\
39 #define PA(addr) ((addr)-KERNELBASE)
44 * The code from acpi_wake_start to acpi_wake_end is copied to
45 * memory below 1MB. The firmware waking vector is updated to
46 * point at acpi_wake_start in low memory before sleeping.
49 ENTRY(acpi_wake_start)
51 * CPU woke up from sleep, and is back in real mode.
52 * Initialize it just enough to get back to protected mode.
56 POSTCODE(ACPI_WAKE_START_ENTRY)
58 /* set up DS to match CS */
63 * Must initialize GDTR before entering protected mode.
64 * Use a temporary GDT that is 0 based, 4GB limit, code and data.
65 * Restoring the actual GDT will come later.
69 lgdt EXT(acpi_gdtr) - EXT(acpi_wake_start)
71 /* set CR0.PE to enter protected mode */
78 * Make intra-segment jump to flush pipeline and reload CS register.
79 * If GDT is bogus, it will blow up here.
82 LJMP(0x8, acpi_wake_prot + ACPI_WAKE_ADDR)
86 /* protected mode, paging disabled */
88 /* setup the protected mode segment registers */
96 /* jump back to the sleep function in the kernel */
97 movl PA(saved_eip), %eax
100 /* Segment Descriptor
103 * ------------------------------------------------------------
104 * | | |B| |A| | | |1|0|E|W|A| |
105 * | BASE 31..24 |G|/|0|V| LIMIT |P|DPL| TYPE | BASE 23:16 |
106 * | | |D| |L| 19..16| | |1|1|C|R|A| |
107 * ------------------------------------------------------------
109 * | BASE 15..0 | LIMIT 15..0 |
111 * ------------------------------------------------------------
114 .word 0, 0 /* 0x0 : null */
117 .word 0xffff, 0x0000 /* 0x8 : code */
118 .byte 0, 0x9e, 0xcf, 0
120 .word 0xffff, 0x0000 /* 0x10 : data */
121 .byte 0, 0x92, 0xcf, 0
124 .word 24 /* limit (8*3 segs) */
125 .long EXT(acpi_gdt) - EXT(acpi_wake_start) + ACPI_WAKE_ADDR
131 * acpi_sleep_cpu(acpi_sleep_callback func, void * refcon)
133 * Save CPU state before platform sleep. Restore CPU state
137 ENTRY(acpi_sleep_cpu)
144 /* save general purpose registers */
148 /* save control registers */
158 /* save segment registers */
164 /* save descriptor table registers */
171 * When system wakes up, the real mode wake handler will revert to
172 * protected mode, then jump to the address stored at saved_eip.
174 movl $(PA(wake_prot)), saved_eip
177 * Call ACPI function provided by the caller to sleep the platform.
178 * This call will not return on success.
185 /* sleep failed, no cpu context lost */
190 /* protected mode, paging disabled */
191 POSTCODE(ACPI_WAKE_PROT_ENTRY)
193 /* restore kernel GDT */
196 /* restore control registers */
197 movl PA(saved_cr2), %eax
201 movl PA(EXT(IdlePDPT)), %eax
202 movl (%eax), %esi /* save orig */
204 movl %ebx, (%eax) /* identity map low mem */
207 movl PA(saved_cr4), %eax
210 movl PA(saved_cr4), %eax
214 * Temporarily use the page tables at IdlePTD
215 * to enable paging. Copy the KPTDI entry to
216 * entry 0 in the PTD to identity map the kernel.
218 movl PA(EXT(IdlePTD)), %eax
220 addl $(KPTDI << PTEINDX), %ebx /* bytes per PDE */
221 movl (%ebx), %ebx /* IdlePTD[KPTDI] */
222 movl (%eax), %esi /* save original IdlePTD[0] */
223 movl %ebx, (%eax) /* update IdlePTD[0] */
224 movl %eax, %cr3 /* CR3 = IdlePTD */
227 /* restore CR0, paging enabled */
228 movl PA(saved_cr0), %eax
231 /* switch to kernel code segment */
232 ljmpl $(KERNEL_CS), $wake_paged
236 /* protected mode, paging enabled */
237 POSTCODE(ACPI_WAKE_PAGED_ENTRY)
239 /* switch to kernel data segment */
240 movw $(KERNEL_DS), %ax
243 /* undo changes to IdlePTD */
245 movl EXT(IdlePDPT), %eax
247 movl EXT(IdlePTD), %eax
249 addl $(KERNELBASE), %eax /* make virtual */
252 /* restore real PDE base */
257 /* restore local and interrupt descriptor tables */
261 /* restore segment registers */
268 * Restore task register. Before doing this, clear the busy flag
269 * in the TSS descriptor set by the CPU.
271 movl $saved_gdt, %eax
272 movl 2(%eax), %edx /* GDT base, skip limit word */
273 movl $(KERNEL_TSS), %eax /* TSS segment selector */
274 movb $(K_TSS), 5(%edx, %eax) /* clear busy flag */
275 ltr saved_tr /* restore TR */
279 /* restore general purpose registers */
290 .section __HIB, __text
293 .globl EXT(acpi_wake_prot_entry)
294 ENTRY(acpi_wake_prot_entry)
295 /* protected mode, paging enabled */
296 POSTCODE(ACPI_WAKE_PAGED_ENTRY)
298 /* restore kernel GDT */
302 /* restore control registers */
307 /* switch to kernel data segment */
308 movw $(KERNEL_DS), %ax
312 /* restore real PDE base */
319 /* restore local and interrupt descriptor tables */
324 /* restore segment registers */
332 * Restore task register. Before doing this, clear the busy flag
333 * in the TSS descriptor set by the CPU.
335 movl $saved_gdt, %eax
336 movl 2(%eax), %edx /* GDT base, skip limit word */
337 movl $(KERNEL_TSS), %eax /* TSS segment selector */
338 movb $(K_TSS), 5(%edx, %eax) /* clear busy flag */
339 ltr saved_tr /* restore TR */
341 /* restore general purpose registers */
348 /* make sure interrupts are disabled */
358 .section __HIB, __data
363 * CPU registers saved across sleep/wake.