2 * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org>
3 * Copyright (c) 2001 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
26 * $DragonFly: src/sys/platform/pc32/acpica/Attic/acpi_wakeup.c,v 1.2 2004/02/12 23:33:26 joerg Exp $
29 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
37 #include <sys/sysctl.h>
40 #include <vm/vm_param.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>
48 #include <machine/bus.h>
49 #include <machine/cpufunc.h>
50 #include <machine/segments.h>
52 #include <i386/isa/intr_machdep.h>
56 #include <dev/acpica/acpica_support.h>
58 #include <dev/acpica/acpivar.h>
60 #include "acpi_wakecode.h"
62 #if defined(__DragonFly__) || __FreeBSD_version < 500000
63 #define vm_page_lock_queues()
64 #define vm_page_unlock_queues()
67 extern void initializecpu(void);
69 static struct region_descriptor r_idt, r_gdt, *p_gdt;
70 static u_int16_t r_ldt;
72 static 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;
75 static u_int16_t r_cs, r_ds, r_es, r_fs, r_gs, r_ss, r_tr;
76 static u_int32_t r_esp = 0;
78 static void acpi_printcpu(void);
79 static void acpi_realmodeinst(void *arg, bus_dma_segment_t *segs,
80 int nsegs, int error);
81 static void acpi_alloc_wakeup_handler(void);
84 extern int acpi_savecpu(void);
85 extern int acpi_restorecpu(void);
91 .type acpi_restorecpu, @function\n\
106 movl ret_addr,%eax \n\
113 .type acpi_savecpu, @function \n\
150 movl %eax,ret_addr \n\
154 #endif /* __GNUC__ */
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,
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);
174 #define WAKECODE_FIXUP(offset, type, val) do { \
176 addr = (void **)(sc->acpi_wakeaddr + offset); \
177 (type *)*addr = val; \
180 #define WAKECODE_BCOPY(offset, type, val) do { \
182 addr = (void **)(sc->acpi_wakeaddr + offset); \
183 bcopy(&(val), addr, sizeof(type)); \
187 acpi_sleep_machdep(struct acpi_softc *sc, int state)
193 static vm_page_t opage = NULL;
195 int pteobj_allocated = 0;
199 if (sc->acpi_wakeaddr == 0) {
203 AcpiSetFirmwareWakingVector(sc->acpi_wakephys);
208 /* Create Identity Mapping */
209 if ((p = curproc) == NULL)
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;
217 oldphys = pmap_extract(pm, sc->acpi_wakephys);
219 opage = PHYS_TO_VM_PAGE(oldphys);
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);
226 if (acpi_savecpu()) {
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);
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);
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);
243 WAKECODE_FIXUP(where_to_recover, void, acpi_restorecpu);
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);
251 if (acpi_get_verbose(sc)) {
257 if (state == ACPI_STATE_S4 && sc->acpi_s4bios) {
258 status = AcpiEnterSleepStateS4Bios();
260 status = AcpiEnterSleepState(state);
263 if (status != AE_OK) {
264 device_printf(sc->acpi_dev,
265 "AcpiEnterSleepState failed - %s\n",
266 AcpiFormatException(status));
279 if (acpi_get_verbose(sc)) {
286 vm_page_lock_queues();
287 pmap_remove(pm, sc->acpi_wakephys, sc->acpi_wakephys + PAGE_SIZE);
288 vm_page_unlock_queues();
290 pmap_enter(pm, sc->acpi_wakephys, page,
291 VM_PROT_READ | VM_PROT_WRITE, 0);
294 if (pteobj_allocated) {
295 vm_object_deallocate(pm->pm_pteobj);
296 pm->pm_pteobj = NULL;
304 static bus_dma_tag_t acpi_waketag;
305 static bus_dmamap_t acpi_wakemap;
306 static vm_offset_t acpi_wakeaddr = 0;
309 acpi_alloc_wakeup_handler(void)
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");
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");
330 SYSINIT(acpiwakeup, SI_SUB_KMEM, SI_ORDER_ANY, acpi_alloc_wakeup_handler, 0)
333 acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
335 struct acpi_softc *sc = arg;
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;
345 acpi_install_wakeup_handler(struct acpi_softc *sc)
348 if (acpi_wakeaddr == 0) {
352 sc->acpi_waketag = acpi_waketag;
353 sc->acpi_wakeaddr = acpi_wakeaddr;
354 sc->acpi_wakemap = acpi_wakemap;
356 bus_dmamap_load(sc->acpi_waketag, sc->acpi_wakemap,
357 (void *)sc->acpi_wakeaddr, PAGE_SIZE,
358 acpi_realmodeinst, sc, 0);