Deleted Added
full compact
acpi_wakeup.c (114977) acpi_wakeup.c (115681)
1/*-
2 * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org>
3 * Copyright (c) 2001 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
1/*-
2 * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org>
3 * Copyright (c) 2001 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sys/i386/acpica/acpi_wakeup.c 114977 2003-05-13 16:59:46Z jhb $
28 */
29
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/sys/i386/acpica/acpi_wakeup.c 115681 2003-06-02 06:16:45Z obrien $");
30
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#include <sys/bus.h>
34#include <sys/lock.h>
35#include <sys/proc.h>
36#include <sys/sysctl.h>
37
38#include <vm/vm.h>
39#include <vm/vm_param.h>
40#include <vm/pmap.h>
41#include <vm/vm_object.h>
42#include <vm/vm_page.h>
43#include <vm/vm_map.h>
44#include <vm/vm_kern.h>
45#include <vm/vm_extern.h>
46
47#include <machine/bus.h>
48#include <machine/cpufunc.h>
49#include <machine/segments.h>
50
51#include <i386/isa/intr_machdep.h>
52
53#include "acpi.h"
54
55#include <dev/acpica/acpica_support.h>
56
57#include <dev/acpica/acpivar.h>
58
59#include "acpi_wakecode.h"
60
61#if __FreeBSD_version < 500000
62#define vm_page_lock_queues()
63#define vm_page_unlock_queues()
64#endif
65
66extern void initializecpu(void);
67
68static struct region_descriptor r_idt, r_gdt, *p_gdt;
69static u_int16_t r_ldt;
70
71static u_int32_t r_eax, r_ebx, r_ecx, r_edx, r_ebp, r_esi, r_edi,
72 r_efl, r_cr0, r_cr2, r_cr3, r_cr4, ret_addr;
73
74static u_int16_t r_cs, r_ds, r_es, r_fs, r_gs, r_ss, r_tr;
75static u_int32_t r_esp = 0;
76
77static void acpi_printcpu(void);
78static void acpi_realmodeinst(void *arg, bus_dma_segment_t *segs,
79 int nsegs, int error);
80static void acpi_alloc_wakeup_handler(void);
81
82/* XXX shut gcc up */
83extern int acpi_savecpu(void);
84extern int acpi_restorecpu(void);
85
86#ifdef __GNUC__
87__asm__(" \n\
88 .text \n\
89 .p2align 2, 0x90 \n\
90 .type acpi_restorecpu, @function\n\
91acpi_restorecpu: \n\
92 .align 4 \n\
93 movl r_eax,%eax \n\
94 movl r_ebx,%ebx \n\
95 movl r_ecx,%ecx \n\
96 movl r_edx,%edx \n\
97 movl r_ebp,%ebp \n\
98 movl r_esi,%esi \n\
99 movl r_edi,%edi \n\
100 movl r_esp,%esp \n\
101 \n\
102 pushl r_efl \n\
103 popfl \n\
104 \n\
105 movl ret_addr,%eax \n\
106 movl %eax,(%esp) \n\
107 xorl %eax,%eax \n\
108 ret \n\
109 \n\
110 .text \n\
111 .p2align 2, 0x90 \n\
112 .type acpi_savecpu, @function \n\
113acpi_savecpu: \n\
114 movw %cs,r_cs \n\
115 movw %ds,r_ds \n\
116 movw %es,r_es \n\
117 movw %fs,r_fs \n\
118 movw %gs,r_gs \n\
119 movw %ss,r_ss \n\
120 \n\
121 movl %eax,r_eax \n\
122 movl %ebx,r_ebx \n\
123 movl %ecx,r_ecx \n\
124 movl %edx,r_edx \n\
125 movl %ebp,r_ebp \n\
126 movl %esi,r_esi \n\
127 movl %edi,r_edi \n\
128 \n\
129 movl %cr0,%eax \n\
130 movl %eax,r_cr0 \n\
131 movl %cr2,%eax \n\
132 movl %eax,r_cr2 \n\
133 movl %cr3,%eax \n\
134 movl %eax,r_cr3 \n\
135 movl %cr4,%eax \n\
136 movl %eax,r_cr4 \n\
137 \n\
138 pushfl \n\
139 popl r_efl \n\
140 \n\
141 movl %esp,r_esp \n\
142 \n\
143 sgdt r_gdt \n\
144 sidt r_idt \n\
145 sldt r_ldt \n\
146 str r_tr \n\
147 \n\
148 movl (%esp),%eax \n\
149 movl %eax,ret_addr \n\
150 movl $1,%eax \n\
151 ret \n\
152");
153#endif /* __GNUC__ */
154
155static void
156acpi_printcpu(void)
157{
158
159 printf("======== acpi_printcpu() debug dump ========\n");
160 printf("gdt[%04x:%08x] idt[%04x:%08x] ldt[%04x] tr[%04x] efl[%08x]\n",
161 r_gdt.rd_limit, r_gdt.rd_base, r_idt.rd_limit, r_idt.rd_base,
162 r_ldt, r_tr, r_efl);
163 printf("eax[%08x] ebx[%08x] ecx[%08x] edx[%08x]\n",
164 r_eax, r_ebx, r_ecx, r_edx);
165 printf("esi[%08x] edi[%08x] ebp[%08x] esp[%08x]\n",
166 r_esi, r_edi, r_ebp, r_esp);
167 printf("cr0[%08x] cr2[%08x] cr3[%08x] cr4[%08x]\n",
168 r_cr0, r_cr2, r_cr3, r_cr4);
169 printf("cs[%04x] ds[%04x] es[%04x] fs[%04x] gs[%04x] ss[%04x]\n",
170 r_cs, r_ds, r_es, r_fs, r_gs, r_ss);
171}
172
173#define WAKECODE_FIXUP(offset, type, val) do { \
174 void **addr; \
175 addr = (void **)(sc->acpi_wakeaddr + offset); \
176 (type *)*addr = val; \
177} while (0)
178
179#define WAKECODE_BCOPY(offset, type, val) do { \
180 void **addr; \
181 addr = (void **)(sc->acpi_wakeaddr + offset); \
182 bcopy(&(val), addr, sizeof(type)); \
183} while (0)
184
185int
186acpi_sleep_machdep(struct acpi_softc *sc, int state)
187{
188 ACPI_STATUS status;
189 vm_paddr_t oldphys;
190 struct pmap *pm;
191 vm_page_t page;
192 static vm_page_t opage = NULL;
193 int ret = 0;
194 int pteobj_allocated = 0;
195 u_long ef;
196 struct proc *p;
197
198 if (sc->acpi_wakeaddr == 0) {
199 return (0);
200 }
201
202 AcpiSetFirmwareWakingVector(sc->acpi_wakephys);
203
204 ef = read_eflags();
205 disable_intr();
206
207 /* Create Identity Mapping */
208 if ((p = curproc) == NULL)
209 p = &proc0;
210 pm = vmspace_pmap(p->p_vmspace);
211 if (pm->pm_pteobj == NULL) {
212 pm->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, PTDPTDI + 1);
213 pteobj_allocated = 1;
214 }
215
216 oldphys = pmap_extract(pm, sc->acpi_wakephys);
217 if (oldphys) {
218 opage = PHYS_TO_VM_PAGE(oldphys);
219 }
220 page = PHYS_TO_VM_PAGE(sc->acpi_wakephys);
221 pmap_enter(pm, sc->acpi_wakephys, page,
222 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE, 1);
223
224 ret_addr = 0;
225 if (acpi_savecpu()) {
226 /* Execute Sleep */
227 p_gdt = (struct region_descriptor *)(sc->acpi_wakeaddr + physical_gdt);
228 p_gdt->rd_limit = r_gdt.rd_limit;
229 p_gdt->rd_base = vtophys(r_gdt.rd_base);
230
231 WAKECODE_FIXUP(physical_esp, u_int32_t, vtophys(r_esp));
232 WAKECODE_FIXUP(previous_cr0, u_int32_t, r_cr0);
233 WAKECODE_FIXUP(previous_cr2, u_int32_t, r_cr2);
234 WAKECODE_FIXUP(previous_cr3, u_int32_t, r_cr3);
235 WAKECODE_FIXUP(previous_cr4, u_int32_t, r_cr4);
236
237 WAKECODE_FIXUP(previous_tr, u_int16_t, r_tr);
238 WAKECODE_BCOPY(previous_gdt, struct region_descriptor, r_gdt);
239 WAKECODE_FIXUP(previous_ldt, u_int16_t, r_ldt);
240 WAKECODE_BCOPY(previous_idt, struct region_descriptor, r_idt);
241
242 WAKECODE_FIXUP(where_to_recover, void, acpi_restorecpu);
243
244 WAKECODE_FIXUP(previous_ds, u_int16_t, r_ds);
245 WAKECODE_FIXUP(previous_es, u_int16_t, r_es);
246 WAKECODE_FIXUP(previous_fs, u_int16_t, r_fs);
247 WAKECODE_FIXUP(previous_gs, u_int16_t, r_gs);
248 WAKECODE_FIXUP(previous_ss, u_int16_t, r_ss);
249
250 if (acpi_get_verbose(sc)) {
251 acpi_printcpu();
252 }
253
254 wbinvd();
255
256 if (state == ACPI_STATE_S4 && sc->acpi_s4bios) {
257 status = AcpiEnterSleepStateS4Bios();
258 } else {
259 status = AcpiEnterSleepState(state);
260 }
261
262 if (status != AE_OK) {
263 device_printf(sc->acpi_dev,
264 "AcpiEnterSleepState failed - %s\n",
265 AcpiFormatException(status));
266 ret = -1;
267 goto out;
268 }
269
270 for (;;) ;
271 } else {
272 /* Execute Wakeup */
273#if 0
274 initializecpu();
275#endif
276 icu_reinit();
277
278 if (acpi_get_verbose(sc)) {
279 acpi_savecpu();
280 acpi_printcpu();
281 }
282 }
283
284out:
285 vm_page_lock_queues();
286 pmap_remove(pm, sc->acpi_wakephys, sc->acpi_wakephys + PAGE_SIZE);
287 vm_page_unlock_queues();
288 if (opage) {
289 pmap_enter(pm, sc->acpi_wakephys, page,
290 VM_PROT_READ | VM_PROT_WRITE, 0);
291 }
292
293 if (pteobj_allocated) {
294 vm_object_deallocate(pm->pm_pteobj);
295 pm->pm_pteobj = NULL;
296 }
297
298 write_eflags(ef);
299
300 return (ret);
301}
302
303static bus_dma_tag_t acpi_waketag;
304static bus_dmamap_t acpi_wakemap;
305static vm_offset_t acpi_wakeaddr = 0;
306
307static void
308acpi_alloc_wakeup_handler(void)
309{
310
311 if (!cold)
312 return;
313
314 if (bus_dma_tag_create(/* parent */ NULL, /* alignment */ 2, 0,
315 /* lowaddr below 1MB */ 0x9ffff,
316 /* highaddr */ BUS_SPACE_MAXADDR, NULL, NULL,
317 PAGE_SIZE, 1, PAGE_SIZE, 0, &acpi_waketag) != 0) {
318 printf("acpi_alloc_wakeup_handler: unable to create wake tag\n");
319 return;
320 }
321
322 if (bus_dmamem_alloc(acpi_waketag, (void **)&acpi_wakeaddr,
323 BUS_DMA_NOWAIT, &acpi_wakemap)) {
324 printf("acpi_alloc_wakeup_handler: unable to allocate wake memory\n");
325 return;
326 }
327}
328
329SYSINIT(acpiwakeup, SI_SUB_KMEM, SI_ORDER_ANY, acpi_alloc_wakeup_handler, 0)
330
331static void
332acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
333{
334 struct acpi_softc *sc = arg;
335 u_int32_t *addr;
336
337 addr = (u_int32_t *)&wakecode[wakeup_sw32 + 2];
338 *addr = segs[0].ds_addr + wakeup_32;
339 bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode));
340 sc->acpi_wakephys = segs[0].ds_addr;
341}
342
343void
344acpi_install_wakeup_handler(struct acpi_softc *sc)
345{
346
347 if (acpi_wakeaddr == 0) {
348 return;
349 }
350
351 sc->acpi_waketag = acpi_waketag;
352 sc->acpi_wakeaddr = acpi_wakeaddr;
353 sc->acpi_wakemap = acpi_wakemap;
354
355 bus_dmamap_load(sc->acpi_waketag, sc->acpi_wakemap,
356 (void *)sc->acpi_wakeaddr, PAGE_SIZE,
357 acpi_realmodeinst, sc, 0);
358}
359
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/kernel.h>
34#include <sys/bus.h>
35#include <sys/lock.h>
36#include <sys/proc.h>
37#include <sys/sysctl.h>
38
39#include <vm/vm.h>
40#include <vm/vm_param.h>
41#include <vm/pmap.h>
42#include <vm/vm_object.h>
43#include <vm/vm_page.h>
44#include <vm/vm_map.h>
45#include <vm/vm_kern.h>
46#include <vm/vm_extern.h>
47
48#include <machine/bus.h>
49#include <machine/cpufunc.h>
50#include <machine/segments.h>
51
52#include <i386/isa/intr_machdep.h>
53
54#include "acpi.h"
55
56#include <dev/acpica/acpica_support.h>
57
58#include <dev/acpica/acpivar.h>
59
60#include "acpi_wakecode.h"
61
62#if __FreeBSD_version < 500000
63#define vm_page_lock_queues()
64#define vm_page_unlock_queues()
65#endif
66
67extern void initializecpu(void);
68
69static struct region_descriptor r_idt, r_gdt, *p_gdt;
70static u_int16_t r_ldt;
71
72static u_int32_t r_eax, r_ebx, r_ecx, r_edx, r_ebp, r_esi, r_edi,
73 r_efl, r_cr0, r_cr2, r_cr3, r_cr4, ret_addr;
74
75static u_int16_t r_cs, r_ds, r_es, r_fs, r_gs, r_ss, r_tr;
76static u_int32_t r_esp = 0;
77
78static void acpi_printcpu(void);
79static void acpi_realmodeinst(void *arg, bus_dma_segment_t *segs,
80 int nsegs, int error);
81static void acpi_alloc_wakeup_handler(void);
82
83/* XXX shut gcc up */
84extern int acpi_savecpu(void);
85extern int acpi_restorecpu(void);
86
87#ifdef __GNUC__
88__asm__(" \n\
89 .text \n\
90 .p2align 2, 0x90 \n\
91 .type acpi_restorecpu, @function\n\
92acpi_restorecpu: \n\
93 .align 4 \n\
94 movl r_eax,%eax \n\
95 movl r_ebx,%ebx \n\
96 movl r_ecx,%ecx \n\
97 movl r_edx,%edx \n\
98 movl r_ebp,%ebp \n\
99 movl r_esi,%esi \n\
100 movl r_edi,%edi \n\
101 movl r_esp,%esp \n\
102 \n\
103 pushl r_efl \n\
104 popfl \n\
105 \n\
106 movl ret_addr,%eax \n\
107 movl %eax,(%esp) \n\
108 xorl %eax,%eax \n\
109 ret \n\
110 \n\
111 .text \n\
112 .p2align 2, 0x90 \n\
113 .type acpi_savecpu, @function \n\
114acpi_savecpu: \n\
115 movw %cs,r_cs \n\
116 movw %ds,r_ds \n\
117 movw %es,r_es \n\
118 movw %fs,r_fs \n\
119 movw %gs,r_gs \n\
120 movw %ss,r_ss \n\
121 \n\
122 movl %eax,r_eax \n\
123 movl %ebx,r_ebx \n\
124 movl %ecx,r_ecx \n\
125 movl %edx,r_edx \n\
126 movl %ebp,r_ebp \n\
127 movl %esi,r_esi \n\
128 movl %edi,r_edi \n\
129 \n\
130 movl %cr0,%eax \n\
131 movl %eax,r_cr0 \n\
132 movl %cr2,%eax \n\
133 movl %eax,r_cr2 \n\
134 movl %cr3,%eax \n\
135 movl %eax,r_cr3 \n\
136 movl %cr4,%eax \n\
137 movl %eax,r_cr4 \n\
138 \n\
139 pushfl \n\
140 popl r_efl \n\
141 \n\
142 movl %esp,r_esp \n\
143 \n\
144 sgdt r_gdt \n\
145 sidt r_idt \n\
146 sldt r_ldt \n\
147 str r_tr \n\
148 \n\
149 movl (%esp),%eax \n\
150 movl %eax,ret_addr \n\
151 movl $1,%eax \n\
152 ret \n\
153");
154#endif /* __GNUC__ */
155
156static void
157acpi_printcpu(void)
158{
159
160 printf("======== acpi_printcpu() debug dump ========\n");
161 printf("gdt[%04x:%08x] idt[%04x:%08x] ldt[%04x] tr[%04x] efl[%08x]\n",
162 r_gdt.rd_limit, r_gdt.rd_base, r_idt.rd_limit, r_idt.rd_base,
163 r_ldt, r_tr, r_efl);
164 printf("eax[%08x] ebx[%08x] ecx[%08x] edx[%08x]\n",
165 r_eax, r_ebx, r_ecx, r_edx);
166 printf("esi[%08x] edi[%08x] ebp[%08x] esp[%08x]\n",
167 r_esi, r_edi, r_ebp, r_esp);
168 printf("cr0[%08x] cr2[%08x] cr3[%08x] cr4[%08x]\n",
169 r_cr0, r_cr2, r_cr3, r_cr4);
170 printf("cs[%04x] ds[%04x] es[%04x] fs[%04x] gs[%04x] ss[%04x]\n",
171 r_cs, r_ds, r_es, r_fs, r_gs, r_ss);
172}
173
174#define WAKECODE_FIXUP(offset, type, val) do { \
175 void **addr; \
176 addr = (void **)(sc->acpi_wakeaddr + offset); \
177 (type *)*addr = val; \
178} while (0)
179
180#define WAKECODE_BCOPY(offset, type, val) do { \
181 void **addr; \
182 addr = (void **)(sc->acpi_wakeaddr + offset); \
183 bcopy(&(val), addr, sizeof(type)); \
184} while (0)
185
186int
187acpi_sleep_machdep(struct acpi_softc *sc, int state)
188{
189 ACPI_STATUS status;
190 vm_paddr_t oldphys;
191 struct pmap *pm;
192 vm_page_t page;
193 static vm_page_t opage = NULL;
194 int ret = 0;
195 int pteobj_allocated = 0;
196 u_long ef;
197 struct proc *p;
198
199 if (sc->acpi_wakeaddr == 0) {
200 return (0);
201 }
202
203 AcpiSetFirmwareWakingVector(sc->acpi_wakephys);
204
205 ef = read_eflags();
206 disable_intr();
207
208 /* Create Identity Mapping */
209 if ((p = curproc) == NULL)
210 p = &proc0;
211 pm = vmspace_pmap(p->p_vmspace);
212 if (pm->pm_pteobj == NULL) {
213 pm->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, PTDPTDI + 1);
214 pteobj_allocated = 1;
215 }
216
217 oldphys = pmap_extract(pm, sc->acpi_wakephys);
218 if (oldphys) {
219 opage = PHYS_TO_VM_PAGE(oldphys);
220 }
221 page = PHYS_TO_VM_PAGE(sc->acpi_wakephys);
222 pmap_enter(pm, sc->acpi_wakephys, page,
223 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE, 1);
224
225 ret_addr = 0;
226 if (acpi_savecpu()) {
227 /* Execute Sleep */
228 p_gdt = (struct region_descriptor *)(sc->acpi_wakeaddr + physical_gdt);
229 p_gdt->rd_limit = r_gdt.rd_limit;
230 p_gdt->rd_base = vtophys(r_gdt.rd_base);
231
232 WAKECODE_FIXUP(physical_esp, u_int32_t, vtophys(r_esp));
233 WAKECODE_FIXUP(previous_cr0, u_int32_t, r_cr0);
234 WAKECODE_FIXUP(previous_cr2, u_int32_t, r_cr2);
235 WAKECODE_FIXUP(previous_cr3, u_int32_t, r_cr3);
236 WAKECODE_FIXUP(previous_cr4, u_int32_t, r_cr4);
237
238 WAKECODE_FIXUP(previous_tr, u_int16_t, r_tr);
239 WAKECODE_BCOPY(previous_gdt, struct region_descriptor, r_gdt);
240 WAKECODE_FIXUP(previous_ldt, u_int16_t, r_ldt);
241 WAKECODE_BCOPY(previous_idt, struct region_descriptor, r_idt);
242
243 WAKECODE_FIXUP(where_to_recover, void, acpi_restorecpu);
244
245 WAKECODE_FIXUP(previous_ds, u_int16_t, r_ds);
246 WAKECODE_FIXUP(previous_es, u_int16_t, r_es);
247 WAKECODE_FIXUP(previous_fs, u_int16_t, r_fs);
248 WAKECODE_FIXUP(previous_gs, u_int16_t, r_gs);
249 WAKECODE_FIXUP(previous_ss, u_int16_t, r_ss);
250
251 if (acpi_get_verbose(sc)) {
252 acpi_printcpu();
253 }
254
255 wbinvd();
256
257 if (state == ACPI_STATE_S4 && sc->acpi_s4bios) {
258 status = AcpiEnterSleepStateS4Bios();
259 } else {
260 status = AcpiEnterSleepState(state);
261 }
262
263 if (status != AE_OK) {
264 device_printf(sc->acpi_dev,
265 "AcpiEnterSleepState failed - %s\n",
266 AcpiFormatException(status));
267 ret = -1;
268 goto out;
269 }
270
271 for (;;) ;
272 } else {
273 /* Execute Wakeup */
274#if 0
275 initializecpu();
276#endif
277 icu_reinit();
278
279 if (acpi_get_verbose(sc)) {
280 acpi_savecpu();
281 acpi_printcpu();
282 }
283 }
284
285out:
286 vm_page_lock_queues();
287 pmap_remove(pm, sc->acpi_wakephys, sc->acpi_wakephys + PAGE_SIZE);
288 vm_page_unlock_queues();
289 if (opage) {
290 pmap_enter(pm, sc->acpi_wakephys, page,
291 VM_PROT_READ | VM_PROT_WRITE, 0);
292 }
293
294 if (pteobj_allocated) {
295 vm_object_deallocate(pm->pm_pteobj);
296 pm->pm_pteobj = NULL;
297 }
298
299 write_eflags(ef);
300
301 return (ret);
302}
303
304static bus_dma_tag_t acpi_waketag;
305static bus_dmamap_t acpi_wakemap;
306static vm_offset_t acpi_wakeaddr = 0;
307
308static void
309acpi_alloc_wakeup_handler(void)
310{
311
312 if (!cold)
313 return;
314
315 if (bus_dma_tag_create(/* parent */ NULL, /* alignment */ 2, 0,
316 /* lowaddr below 1MB */ 0x9ffff,
317 /* highaddr */ BUS_SPACE_MAXADDR, NULL, NULL,
318 PAGE_SIZE, 1, PAGE_SIZE, 0, &acpi_waketag) != 0) {
319 printf("acpi_alloc_wakeup_handler: unable to create wake tag\n");
320 return;
321 }
322
323 if (bus_dmamem_alloc(acpi_waketag, (void **)&acpi_wakeaddr,
324 BUS_DMA_NOWAIT, &acpi_wakemap)) {
325 printf("acpi_alloc_wakeup_handler: unable to allocate wake memory\n");
326 return;
327 }
328}
329
330SYSINIT(acpiwakeup, SI_SUB_KMEM, SI_ORDER_ANY, acpi_alloc_wakeup_handler, 0)
331
332static void
333acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
334{
335 struct acpi_softc *sc = arg;
336 u_int32_t *addr;
337
338 addr = (u_int32_t *)&wakecode[wakeup_sw32 + 2];
339 *addr = segs[0].ds_addr + wakeup_32;
340 bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode));
341 sc->acpi_wakephys = segs[0].ds_addr;
342}
343
344void
345acpi_install_wakeup_handler(struct acpi_softc *sc)
346{
347
348 if (acpi_wakeaddr == 0) {
349 return;
350 }
351
352 sc->acpi_waketag = acpi_waketag;
353 sc->acpi_wakeaddr = acpi_wakeaddr;
354 sc->acpi_wakemap = acpi_wakemap;
355
356 bus_dmamap_load(sc->acpi_waketag, sc->acpi_wakemap,
357 (void *)sc->acpi_wakeaddr, PAGE_SIZE,
358 acpi_realmodeinst, sc, 0);
359}
360