- kill `unused variable' warning pointed out by sephe@
[dragonfly.git] / sys / i386 / acpica5 / acpi_wakeup.c
blobf130d56f7aff81efeac874e6389244ea94bbf0c5
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.
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.
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.
27 * $FreeBSD: src/sys/i386/acpica/acpi_wakeup.c,v 1.33 2004/05/06 02:18:58 njl Exp $
28 * $DragonFly: src/sys/i386/acpica5/Attic/acpi_wakeup.c,v 1.9 2005/10/30 04:20:49 y0netan1 Exp $
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>
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41 #include <vm/vm_object.h>
42 #include <vm/vm_page.h>
43 #include <vm/vm_map.h>
45 #include <machine/bus.h>
46 #include <machine/cpufunc.h>
47 #include <machine/segments.h>
48 #include <machine/../isa/intr_machdep.h>
50 #include "acpi.h"
51 #include <dev/acpica5/acpivar.h>
53 #include "acpi_wakecode.h"
55 #if __FreeBSD_version < 500000
56 #define vm_page_lock_queues()
57 #define vm_page_unlock_queues()
58 #endif
60 extern uint32_t acpi_reset_video;
61 extern void initializecpu(void);
63 static struct region_descriptor r_idt, r_gdt, *p_gdt;
64 static uint16_t r_ldt;
66 static uint32_t r_eax, r_ebx, r_ecx, r_edx, r_ebp, r_esi, r_edi,
67 r_efl, r_cr0, r_cr2, r_cr3, r_cr4, ret_addr;
69 static uint16_t r_cs, r_ds, r_es, r_fs, r_gs, r_ss, r_tr;
70 static uint32_t r_esp = 0;
72 static void acpi_printcpu(void);
73 static void acpi_realmodeinst(void *arg, bus_dma_segment_t *segs,
74 int nsegs, int error);
75 static void acpi_alloc_wakeup_handler(void);
77 /* XXX shut gcc up */
78 extern int acpi_savecpu(void);
79 extern int acpi_restorecpu(void);
81 #if defined(__GNUC__) || defined(__INTEL_COMPILER)
82 __asm__(" \n\
83 .text \n\
84 .p2align 2, 0x90 \n\
85 .type acpi_restorecpu, @function\n\
86 acpi_restorecpu: \n\
87 .align 4 \n\
88 movl r_eax,%eax \n\
89 movl r_ebx,%ebx \n\
90 movl r_ecx,%ecx \n\
91 movl r_edx,%edx \n\
92 movl r_ebp,%ebp \n\
93 movl r_esi,%esi \n\
94 movl r_edi,%edi \n\
95 movl r_esp,%esp \n\
96 \n\
97 pushl r_efl \n\
98 popfl \n\
99 \n\
100 movl ret_addr,%eax \n\
101 movl %eax,(%esp) \n\
102 xorl %eax,%eax \n\
103 ret \n\
105 .text \n\
106 .p2align 2, 0x90 \n\
107 .type acpi_savecpu, @function \n\
108 acpi_savecpu: \n\
109 movw %cs,r_cs \n\
110 movw %ds,r_ds \n\
111 movw %es,r_es \n\
112 movw %fs,r_fs \n\
113 movw %gs,r_gs \n\
114 movw %ss,r_ss \n\
116 movl %eax,r_eax \n\
117 movl %ebx,r_ebx \n\
118 movl %ecx,r_ecx \n\
119 movl %edx,r_edx \n\
120 movl %ebp,r_ebp \n\
121 movl %esi,r_esi \n\
122 movl %edi,r_edi \n\
124 movl %cr0,%eax \n\
125 movl %eax,r_cr0 \n\
126 movl %cr2,%eax \n\
127 movl %eax,r_cr2 \n\
128 movl %cr3,%eax \n\
129 movl %eax,r_cr3 \n\
130 movl %cr4,%eax \n\
131 movl %eax,r_cr4 \n\
133 pushfl \n\
134 popl r_efl \n\
136 movl %esp,r_esp \n\
138 sgdt r_gdt \n\
139 sidt r_idt \n\
140 sldt r_ldt \n\
141 str r_tr \n\
143 movl (%esp),%eax \n\
144 movl %eax,ret_addr \n\
145 movl $1,%eax \n\
146 ret \n\
148 #endif /* __GNUC__ || __INTEL_COMPILER */
150 static void
151 acpi_printcpu(void)
153 printf("======== acpi_printcpu() debug dump ========\n");
154 printf("gdt[%04x:%08x] idt[%04x:%08x] ldt[%04x] tr[%04x] efl[%08x]\n",
155 r_gdt.rd_limit, r_gdt.rd_base, r_idt.rd_limit, r_idt.rd_base,
156 r_ldt, r_tr, r_efl);
157 printf("eax[%08x] ebx[%08x] ecx[%08x] edx[%08x]\n",
158 r_eax, r_ebx, r_ecx, r_edx);
159 printf("esi[%08x] edi[%08x] ebp[%08x] esp[%08x]\n",
160 r_esi, r_edi, r_ebp, r_esp);
161 printf("cr0[%08x] cr2[%08x] cr3[%08x] cr4[%08x]\n",
162 r_cr0, r_cr2, r_cr3, r_cr4);
163 printf("cs[%04x] ds[%04x] es[%04x] fs[%04x] gs[%04x] ss[%04x]\n",
164 r_cs, r_ds, r_es, r_fs, r_gs, r_ss);
167 #define WAKECODE_FIXUP(offset, type, val) do { \
168 type *addr; \
169 addr = (type *)(sc->acpi_wakeaddr + offset); \
170 *addr = val; \
171 } while (0)
173 #define WAKECODE_BCOPY(offset, type, val) do { \
174 void *addr; \
175 addr = (void *)(sc->acpi_wakeaddr + offset); \
176 bcopy(&(val), addr, sizeof(type)); \
177 } while (0)
180 acpi_sleep_machdep(struct acpi_softc *sc, int state)
182 ACPI_STATUS status;
183 vm_paddr_t oldphys;
184 struct pmap *pm;
185 vm_page_t page;
186 static vm_page_t opage = NULL;
187 int ret = 0;
188 int pteobj_allocated = 0;
189 uint32_t cr3;
190 u_long ef;
191 struct proc *p;
193 if (sc->acpi_wakeaddr == 0)
194 return (0);
196 AcpiSetFirmwareWakingVector(sc->acpi_wakephys);
198 ef = read_eflags();
199 ACPI_DISABLE_IRQS();
201 /* Create Identity Mapping */
202 if ((p = curproc) == NULL)
203 p = &proc0;
204 pm = vmspace_pmap(p->p_vmspace);
205 cr3 = rcr3();
206 #ifdef PAE
207 load_cr3(vtophys(pm->pm_pdpt));
208 #else
209 load_cr3(vtophys(pm->pm_pdir));
210 #endif
212 * note: DragonFly still uses the VM object (FreeBSD-5 no longer uses
213 * the VM object).
215 if (pm->pm_pteobj == NULL) {
216 pm->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, PTDPTDI + 1);
217 pteobj_allocated = 1;
220 oldphys = pmap_extract(pm, sc->acpi_wakephys);
221 if (oldphys)
222 opage = PHYS_TO_VM_PAGE(oldphys);
223 page = PHYS_TO_VM_PAGE(sc->acpi_wakephys);
224 pmap_enter(pm, sc->acpi_wakephys, page,
225 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE, 1);
227 ret_addr = 0;
228 if (acpi_savecpu()) {
229 /* Execute Sleep */
230 cpu_disable_intr();
232 p_gdt = (struct region_descriptor *)
233 (sc->acpi_wakeaddr + physical_gdt);
234 p_gdt->rd_limit = r_gdt.rd_limit;
235 p_gdt->rd_base = vtophys(r_gdt.rd_base);
237 WAKECODE_FIXUP(physical_esp, uint32_t, vtophys(r_esp));
238 WAKECODE_FIXUP(previous_cr0, uint32_t, r_cr0);
239 WAKECODE_FIXUP(previous_cr2, uint32_t, r_cr2);
240 WAKECODE_FIXUP(previous_cr3, uint32_t, r_cr3);
241 WAKECODE_FIXUP(previous_cr4, uint32_t, r_cr4);
243 WAKECODE_FIXUP(reset_video, uint32_t, acpi_reset_video);
245 WAKECODE_FIXUP(previous_tr, uint16_t, r_tr);
246 WAKECODE_BCOPY(previous_gdt, struct region_descriptor, r_gdt);
247 WAKECODE_FIXUP(previous_ldt, uint16_t, r_ldt);
248 WAKECODE_BCOPY(previous_idt, struct region_descriptor, r_idt);
250 WAKECODE_FIXUP(where_to_recover, void *, acpi_restorecpu);
252 WAKECODE_FIXUP(previous_ds, uint16_t, r_ds);
253 WAKECODE_FIXUP(previous_es, uint16_t, r_es);
254 WAKECODE_FIXUP(previous_fs, uint16_t, r_fs);
255 WAKECODE_FIXUP(previous_gs, uint16_t, r_gs);
256 WAKECODE_FIXUP(previous_ss, uint16_t, r_ss);
258 if (bootverbose)
259 acpi_printcpu();
261 /* Call ACPICA to enter the desired sleep state */
262 if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
263 status = AcpiEnterSleepStateS4bios();
264 else
265 status = AcpiEnterSleepState(state);
267 if (status != AE_OK) {
268 device_printf(sc->acpi_dev,
269 "AcpiEnterSleepState failed - %s\n",
270 AcpiFormatException(status));
271 ret = -1;
272 goto out;
275 for (;;) ;
276 } else {
277 /* Execute Wakeup */
278 #if 0
279 initializecpu();
280 #endif
281 icu_reinit();
282 cpu_enable_intr();
284 if (bootverbose) {
285 acpi_savecpu();
286 acpi_printcpu();
290 out:
291 vm_page_lock_queues();
292 pmap_remove(pm, sc->acpi_wakephys, sc->acpi_wakephys + PAGE_SIZE);
293 vm_page_unlock_queues();
294 if (opage) {
295 pmap_enter(pm, sc->acpi_wakephys, page,
296 VM_PROT_READ | VM_PROT_WRITE, 0);
299 if (pteobj_allocated) {
300 vm_object_deallocate(pm->pm_pteobj);
301 pm->pm_pteobj = NULL;
303 load_cr3(cr3);
305 write_eflags(ef);
307 return (ret);
310 static bus_dma_tag_t acpi_waketag;
311 static bus_dmamap_t acpi_wakemap;
312 static vm_offset_t acpi_wakeaddr = 0;
314 static void
315 acpi_alloc_wakeup_handler(void)
317 if (!cold)
318 return;
320 if (bus_dma_tag_create(/* parent */ NULL, /* alignment */ 2, 0,
321 /* lowaddr below 1MB */ 0x9ffff,
322 /* highaddr */ BUS_SPACE_MAXADDR, NULL, NULL,
323 PAGE_SIZE, 1, PAGE_SIZE, 0, &acpi_waketag) != 0) {
324 printf("acpi_alloc_wakeup_handler: can't create wake tag\n");
325 return;
328 if (bus_dmamem_alloc(acpi_waketag, (void **)&acpi_wakeaddr,
329 BUS_DMA_NOWAIT, &acpi_wakemap)) {
330 printf("acpi_alloc_wakeup_handler: can't alloc wake memory\n");
331 return;
335 SYSINIT(acpiwakeup, SI_SUB_KMEM, SI_ORDER_ANY, acpi_alloc_wakeup_handler, 0)
337 static void
338 acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
340 struct acpi_softc *sc = arg;
341 uint32_t *addr;
343 addr = (uint32_t *)&wakecode[wakeup_sw32 + 2];
344 *addr = segs[0].ds_addr + wakeup_32;
345 bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode));
346 sc->acpi_wakephys = segs[0].ds_addr;
349 void
350 acpi_install_wakeup_handler(struct acpi_softc *sc)
352 if (acpi_wakeaddr == 0)
353 return;
355 sc->acpi_waketag = acpi_waketag;
356 sc->acpi_wakeaddr = acpi_wakeaddr;
357 sc->acpi_wakemap = acpi_wakemap;
359 bus_dmamap_load(sc->acpi_waketag, sc->acpi_wakemap,
360 (void *)sc->acpi_wakeaddr, PAGE_SIZE,
361 acpi_realmodeinst, sc, 0);