diff --git a/sys/amd64/acpica/acpi_wakeup.c b/sys/amd64/acpica/acpi_wakeup.c new file mode 100644 index 000000000000..2b10978e493c --- /dev/null +++ b/sys/amd64/acpica/acpi_wakeup.c @@ -0,0 +1,328 @@ +/*- + * Copyright (c) 2001 Takanori Watanabe + * Copyright (c) 2001 Mitsuru IWASAKI + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include "acpi.h" + +#include + +#include "acpi_wakecode.h" + +extern void initializecpu(void); + +struct region_descriptor r_idt, r_gdt, *p_gdt; +static u_int16_t r_ldt; + +static u_int32_t r_eax, r_ebx, r_ecx, r_edx, r_ebp, r_esi, r_edi, + r_efl, r_cr0, r_cr2, r_cr3, r_cr4, ret_addr; + +static u_int16_t r_cs, r_ds, r_es, r_fs, r_gs, r_ss, r_tr; +static u_int32_t r_esp = 0; + +static int debug_wakeup = 1; +SYSCTL_INT(_debug, OID_AUTO, acpi_wakeup, CTLFLAG_RW, &debug_wakeup, 0, "") + +static void acpi_printcpu(void); +static void acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, + int nsegs, int error); +static void acpi_alloc_wakeup_handler(void); + +/* XXX shut gcc up */ +extern int acpi_savecpu(void); +extern int acpi_restorecpu(void); + +__asm__(" + .text + .p2align 2, 0x90 + .type acpi_restorecpu, @function +acpi_restorecpu: + .align 4 + movl r_eax,%eax + movl r_ebx,%ebx + movl r_ecx,%ecx + movl r_edx,%edx + movl r_ebp,%ebp + movl r_esi,%esi + movl r_edi,%edi + movl r_esp,%esp + + pushl r_efl + popfl + + pushl ret_addr + xorl %eax,%eax + ret + + .text + .p2align 2, 0x90 + .type acpi_savecpu, @function +acpi_savecpu: + movw %cs,r_cs + movw %ds,r_ds + movw %es,r_es + movw %fs,r_fs + movw %gs,r_gs + movw %ss,r_ss + + movl %eax,r_eax + movl %ebx,r_ebx + movl %ecx,r_ecx + movl %edx,r_edx + movl %ebp,r_ebp + movl %esi,r_esi + movl %edi,r_edi + + movl %cr0,%eax + movl %eax,r_cr0 + movl %cr2,%eax + movl %eax,r_cr2 + movl %cr3,%eax + movl %eax,r_cr3 + movl %cr4,%eax + movl %eax,r_cr4 + + pushfl + popl r_efl + + movl %esp,r_esp + + sgdt r_gdt + sidt r_idt + sldt r_ldt + str r_tr + + movl (%esp),%eax + movl %eax,ret_addr + movl $1,%eax + ret +"); + +static void +acpi_printcpu(void) +{ + + printf("======== acpi_printcpu() debug dump ========\n"); + printf("gdt[%04x:%08x] idt[%04x:%08x] ldt[%04x] tr[%04x] efl[%08x]\n", + r_gdt.rd_limit, r_gdt.rd_base, r_idt.rd_limit, r_idt.rd_base, + r_ldt, r_tr, r_efl); + printf("eax[%08x] ebx[%08x] ecx[%08x] edx[%08x]\n", + r_eax, r_ebx, r_ecx, r_edx); + printf("esi[%08x] edi[%08x] ebp[%08x] esp[%08x]\n", + r_esi, r_edi, r_ebp, r_esp); + printf("cr0[%08x] cr2[%08x] cr3[%08x] cr4[%08x]\n", + r_cr0, r_cr2, r_cr3, r_cr4); + printf("cs[%04x] ds[%04x] es[%04x] fs[%04x] gs[%04x] ss[%04x]\n", + r_cs, r_ds, r_es, r_fs, r_gs, r_ss); +} + +int +acpi_sleep_machdep(struct acpi_softc *sc, int state) +{ + ACPI_STATUS status; + void **addr; + vm_offset_t oldphys; + struct pmap *pm; + vm_page_t page; + static vm_page_t opage = NULL; + int ret = 0; + u_long ef; + + if (sc->acpi_wakeaddr == NULL) { + return (0); + } + + AcpiSetFirmwareWakingVector(sc->acpi_wakephys); + + ef = read_eflags(); + disable_intr(); + + /* Create Identity Mapping */ + pm = &CURPROC->p_vmspace->vm_pmap; + oldphys = pmap_extract(pm, sc->acpi_wakephys); + if (oldphys) { + opage = PHYS_TO_VM_PAGE(oldphys); + } + page = PHYS_TO_VM_PAGE(sc->acpi_wakephys); + pmap_enter(pm, sc->acpi_wakephys, page, + VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE, 1); + + ret_addr = 0; + if (acpi_savecpu()) { + /* Execute Sleep */ + p_gdt = (struct region_descriptor *)(sc->acpi_wakeaddr + physical_gdt); + p_gdt->rd_limit = r_gdt.rd_limit; + p_gdt->rd_base = vtophys(r_gdt.rd_base); + addr = (void **)(sc->acpi_wakeaddr + physical_esp); + (u_int32_t *)*addr = vtophys(r_esp); + addr = (void **)(sc->acpi_wakeaddr + previous_cr0); + (u_int32_t *)*addr = r_cr0; + addr = (void **)(sc->acpi_wakeaddr + previous_cr2); + (u_int32_t *)*addr = r_cr2; + addr = (void **)(sc->acpi_wakeaddr + previous_cr3); + (u_int32_t *)*addr = r_cr3; + addr = (void **)(sc->acpi_wakeaddr + previous_cr4); + (u_int32_t *)*addr = r_cr4; + + addr = (void **)(sc->acpi_wakeaddr + previous_tr); + (u_int16_t *)*addr = r_tr; + addr = (void **)(sc->acpi_wakeaddr + previous_gdt); + bcopy(&r_gdt, addr, sizeof(r_gdt)); + addr = (void **)(sc->acpi_wakeaddr + previous_ldt); + (u_int16_t *)*addr = r_ldt; + addr = (void **)(sc->acpi_wakeaddr + previous_idt); + bcopy(&r_idt, addr, sizeof(r_idt)); + addr = (void **)(sc->acpi_wakeaddr + where_to_recover); + *addr = acpi_restorecpu; + addr = (void **)(sc->acpi_wakeaddr + previous_ds); + (u_int16_t *)*addr = r_ds; + addr = (void **)(sc->acpi_wakeaddr + previous_es); + (u_int16_t *)*addr = r_es; + addr = (void **)(sc->acpi_wakeaddr + previous_fs); + (u_int16_t *)*addr = r_fs; + addr = (void **)(sc->acpi_wakeaddr + previous_gs); + (u_int16_t *)*addr = r_gs; + addr = (void **)(sc->acpi_wakeaddr + previous_ss); + (u_int16_t *)*addr = r_ss; + + if (debug_wakeup) { + acpi_printcpu(); + } + + if ((status = AcpiEnterSleepState(state)) != AE_OK) { + device_printf(sc->acpi_dev, + "AcpiEnterSleepState failed - %s\n", + acpi_strerror(status)); + ret = -1; + goto out; + } + wbinvd(); + + for (;;) ; + } else { + /* Execute Wakeup */ +#if 0 + initializecpu(); +#endif + icu_reinit(); + + pmap_remove(pm, sc->acpi_wakephys, + sc->acpi_wakephys + PAGE_SIZE); + if (opage) { + pmap_enter(pm, sc->acpi_wakephys, page, + VM_PROT_READ | VM_PROT_WRITE, 0); + } + + if (debug_wakeup) { + acpi_savecpu(); + acpi_printcpu(); + } + } + +out: + write_eflags(ef); + + return (ret); +} + +static bus_dma_tag_t acpi_waketag; +static bus_dmamap_t acpi_wakemap; +static vm_offset_t acpi_wakeaddr = 0; + +static void +acpi_alloc_wakeup_handler(void) +{ + + if (bus_dma_tag_create(/* parent */ NULL, /* alignment */ 2, 0, + /* lowaddr below 1MB */ 0x9ffff, + /* highaddr */ BUS_SPACE_MAXADDR, NULL, NULL, + PAGE_SIZE, 1, PAGE_SIZE, 0, &acpi_waketag) != 0) { + printf("acpi_alloc_wakeup_handler: unable to create wake tag\n"); + return; + } + + if (bus_dmamem_alloc(acpi_waketag, (void **)&acpi_wakeaddr, + BUS_DMA_NOWAIT, &acpi_wakemap)) { + printf("acpi_alloc_wakeup_handler: unable to allocate wake memory\n"); + return; + } +} + +SYSINIT(acpiwakeup, SI_SUB_KMEM, SI_ORDER_ANY, acpi_alloc_wakeup_handler, 0) + +static void +acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, int nsegs, int error) +{ + struct acpi_softc *sc = arg; + u_int32_t *addr; + + addr = (u_int32_t *)&wakecode[wakeup_sw32 + 2]; + *addr = segs[0].ds_addr + wakeup_32; + bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode)); + sc->acpi_wakephys = segs[0].ds_addr; +} + +void +acpi_install_wakeup_handler(struct acpi_softc *sc) +{ + + if (acpi_wakeaddr == 0) { + return; + } + + sc->acpi_waketag = acpi_waketag; + sc->acpi_wakeaddr = acpi_wakeaddr; + sc->acpi_wakemap = acpi_wakemap; + + bus_dmamap_load(sc->acpi_waketag, sc->acpi_wakemap, + (void *)sc->acpi_wakeaddr, PAGE_SIZE, + acpi_realmodeinst, sc, 0); +} + diff --git a/sys/amd64/isa/intr_machdep.c b/sys/amd64/isa/intr_machdep.c index 503163f9dbde..48555d3be315 100644 --- a/sys/amd64/isa/intr_machdep.c +++ b/sys/amd64/isa/intr_machdep.c @@ -135,6 +135,7 @@ static driver_intr_t isa_strayintr; static void ithds_init(void *dummy); static void ithread_enable(int vector); static void ithread_disable(int vector); +static void init_i8259(void); #ifdef PC98 #define NMI_PARITY 0x04 @@ -281,6 +282,22 @@ isa_nmi(cd) return(retval); } +/* + * ICU reinitialize when ICU configuration has lost. + */ +void icu_reinit() +{ + int i; + u_int32_t eflags; + eflags = read_eflags(); + disable_intr(); + init_i8259(); + for(i=0;iacpi_dev = dev; + acpi_install_wakeup_handler(sc); + #ifdef ENABLE_DEBUGGER if (debugpoint && !strcmp(debugpoint, "spaces")) acpi_EnterDebugger(); @@ -959,7 +961,6 @@ acpi_FindIndexedResource(ACPI_RESOURCE *resbuf, int index, ACPI_RESOURCE **resp) static ACPI_STATUS __inline acpi_wakeup(UINT8 state) { - UINT16 Count; ACPI_STATUS Status; ACPI_OBJECT_LIST Arg_list; ACPI_OBJECT Arg; @@ -968,21 +969,6 @@ acpi_wakeup(UINT8 state) FUNCTION_TRACE_U32(__func__, state); ACPI_ASSERTLOCK; - - /* wait for the WAK_STS bit */ - Count = 0; - while (!(AcpiHwRegisterBitAccess(ACPI_READ, ACPI_MTX_LOCK, WAK_STS))) { - AcpiOsSleepUsec(1000); - /* - * Some BIOSes don't set WAK_STS at all, - * give up waiting for wakeup if we time out. - */ - if (Count > 1000) { - printf("ACPI: timed out waiting for WAK_STS, continuing\n"); - break; /* giving up */ - } - Count++; - } /* * Evaluate the _WAK method @@ -1056,6 +1042,7 @@ ACPI_STATUS acpi_SetSleepState(struct acpi_softc *sc, int state) { ACPI_STATUS status = AE_OK; + UINT16 Count; FUNCTION_TRACE_U32(__func__, state); ACPI_ASSERTLOCK; @@ -1069,6 +1056,9 @@ acpi_SetSleepState(struct acpi_softc *sc, int state) break; case ACPI_STATE_S1: + case ACPI_STATE_S2: + case ACPI_STATE_S3: + case ACPI_STATE_S4: /* * Inform all devices that we are going to sleep. */ @@ -1084,10 +1074,37 @@ acpi_SetSleepState(struct acpi_softc *sc, int state) return_ACPI_STATUS(AE_ERROR); } sc->acpi_sstate = state; - status = AcpiEnterSleepState((UINT8)state); - if (status != AE_OK) { - device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", acpi_strerror(status)); - break; + + if (state != ACPI_STATE_S1) { + acpi_sleep_machdep(sc, state); + + /* AcpiEnterSleepState() maybe incompleted, unlock here. */ + AcpiUtReleaseMutex(ACPI_MTX_HARDWARE); + + /* Re-enable ACPI hardware on wakeup from sleep state 4. */ + if (state >= ACPI_STATE_S4) { + acpi_Disable(sc); + acpi_Enable(sc); + } + } else { + status = AcpiEnterSleepState((UINT8)state); + if (status != AE_OK) { + device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", acpi_strerror(status)); + break; + } + /* wait for the WAK_STS bit */ + Count = 0; + while (!(AcpiHwRegisterBitAccess(ACPI_READ, ACPI_MTX_LOCK, WAK_STS))) { + AcpiOsSleepUsec(1000); + /* + * Some BIOSes don't set WAK_STS at all, + * give up waiting for wakeup if we time out. + */ + if (Count > 1000) { + break; /* giving up */ + } + Count++; + } } acpi_wakeup((UINT8)state); DEVICE_RESUME(root_bus); @@ -1095,9 +1112,6 @@ acpi_SetSleepState(struct acpi_softc *sc, int state) acpi_enable_fixed_events(sc); break; - case ACPI_STATE_S3: - acpi_off_state = ACPI_STATE_S3; - /* FALLTHROUGH */ case ACPI_STATE_S5: /* * Shut down cleanly and power off. This will call us back through the diff --git a/sys/dev/acpica/acpivar.h b/sys/dev/acpica/acpivar.h index a8bc37b66dd3..5a6f73752c7e 100644 --- a/sys/dev/acpica/acpivar.h +++ b/sys/dev/acpica/acpivar.h @@ -32,6 +32,9 @@ #include #include +#include +#include + extern devclass_t acpi_devclass; struct acpi_softc { @@ -54,6 +57,11 @@ struct acpi_softc { int acpi_sleep_button_sx; int acpi_lid_switch_sx; + bus_dma_tag_t acpi_waketag; + bus_dmamap_t acpi_wakemap; + vm_offset_t acpi_wakeaddr; + vm_offset_t acpi_wakephys; + struct sysctl_ctx_list acpi_battery_sysctl_ctx; struct sysctl_oid *acpi_battery_sysctl_tree; }; @@ -267,6 +275,9 @@ extern char *acpi_name(ACPI_HANDLE handle); extern int acpi_avoid(ACPI_HANDLE handle); extern int acpi_disabled(char *subsys); +extern void acpi_install_wakeup_handler(struct acpi_softc *sc); +extern int acpi_sleep_machdep(struct acpi_softc *sc, int state); + /* * Battery Abstruction. */ diff --git a/sys/i386/acpica/Makefile b/sys/i386/acpica/Makefile new file mode 100644 index 000000000000..287529280c47 --- /dev/null +++ b/sys/i386/acpica/Makefile @@ -0,0 +1,24 @@ +# $FreeBSD$ + +# Correct path for kernel builds +# Don't rely on the kernel's .depend file +.ifdef MAKESRCPATH +.PATH: ${MAKESRCPATH} +DEPENDFILE= +.else +MAKESRCPATH= ${.CURDIR} +CLEANFILES= acpi_wakecode.h acpi_wakecode.bin acpi_wakecode.o +.endif + +all: acpi_wakecode.h + +acpi_wakecode.o: acpi_wakecode.S + +acpi_wakecode.bin: acpi_wakecode.o + objcopy -S -O binary acpi_wakecode.o acpi_wakecode.bin + +acpi_wakecode.h: acpi_wakecode.bin acpi_wakecode.o + perl ${MAKESRCPATH}/genwakecode.pl > acpi_wakecode.h + +.include + diff --git a/sys/i386/acpica/acpi_wakecode.S b/sys/i386/acpica/acpi_wakecode.S new file mode 100644 index 000000000000..22268b18b4d8 --- /dev/null +++ b/sys/i386/acpica/acpi_wakecode.S @@ -0,0 +1,218 @@ +/*- + * Copyright (c) 2001 Takanori Watanabe + * Copyright (c) 2001 Mitsuru IWASAKI + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#define LOCORE + +#include + + .align 4 + .code16 +wakeup_16: + nop + cli + + /* Set up segment registers for real mode */ + movw %cs,%ax + movw %ax,%ds + movw %ax,%ss + + /* Load GDT for real mode */ + lgdt physical_gdt + + /* Restore CR2, CR3 and CR4 */ + mov previous_cr2,%eax + mov %eax,%cr2 + mov previous_cr3,%eax + mov %eax,%cr3 + mov previous_cr4,%eax + mov %eax,%cr4 + + /* Transfer some values to protected mode */ +#define NVALUES 9 +#define TRANSFER_STACK32(val, idx) \ + mov val,%eax; \ + mov %eax,wakeup_32stack+(idx+1)+(idx*4); + + TRANSFER_STACK32(previous_ss, (NVALUES - 9)) + TRANSFER_STACK32(previous_fs, (NVALUES - 8)) + TRANSFER_STACK32(previous_ds, (NVALUES - 7)) + TRANSFER_STACK32(physical_gdt+2, (NVALUES - 6)) + TRANSFER_STACK32(where_to_recover, (NVALUES - 5)) + TRANSFER_STACK32(previous_idt+2, (NVALUES - 4)) + TRANSFER_STACK32(previous_ldt, (NVALUES - 3)) + TRANSFER_STACK32(previous_gdt+2, (NVALUES - 2)) + TRANSFER_STACK32(previous_tr, (NVALUES - 1)) + TRANSFER_STACK32(previous_cr0, (NVALUES - 0)) + + mov physical_esp,%esi /* to be used in 32bit code */ + + /* Enable protected mode */ + mov %cr0,%eax + orl $(CR0_PE),%eax + mov %eax,%cr0 + +wakeup_sw32: + /* Switch to protected mode by intersegmental jump */ + ljmpl $0x8,$0x12345678 /* Code location, to be replaced */ + + .code32 +wakeup_32: + /* + * Switched to protected mode w/o paging + * %esi: KERNEL stack pointer (physical address) + */ + + nop + + /* Set up segment registers for protected mode */ + movw $0x10,%ax /* KDSEL to segment registers */ + movw %ax,%ds + movw %ax,%es + movw %ax,%gs + movw %ax,%ss + movw $0x18,%ax /* KPSEL to %fs */ + movw %ax,%fs + movl %esi,%esp /* physical address stack pointer */ + +wakeup_32stack: + /* Operands are overwritten in 16bit code */ + pushl $0xabcdef09 /* ss + dummy */ + pushl $0xabcdef08 /* fs + gs */ + pushl $0xabcdef07 /* ds + es */ + pushl $0xabcdef06 /* gdt:base (physical address) */ + pushl $0xabcdef05 /* recover address */ + pushl $0xabcdef04 /* idt:base */ + pushl $0xabcdef03 /* ldt + idt:limit */ + pushl $0xabcdef02 /* gdt:base */ + pushl $0xabcdef01 /* TR + gdt:limit */ + pushl $0xabcdef00 /* CR0 */ + + movl %esp,%ebp +#define CR0_REGISTER 0(%ebp) +#define TASK_REGISTER 4(%ebp) +#define PREVIOUS_GDT 6(%ebp) +#define PREVIOUS_LDT 12(%ebp) +#define PREVIOUS_IDT 14(%ebp) +#define RECOVER_ADDR 20(%ebp) +#define PHYSICAL_GDT_BASE 24(%ebp) +#define PREVIOUS_DS 28(%ebp) +#define PREVIOUS_ES 30(%ebp) +#define PREVIOUS_FS 32(%ebp) +#define PREVIOUS_GS 34(%ebp) +#define PREVIOUS_SS 36(%ebp) + + /* Fixup TSS type field */ +#define TSS_TYPEFIX_MASK 0xf9 + xorl %esi,%esi + movl PHYSICAL_GDT_BASE,%ebx + movw TASK_REGISTER,%si + leal (%ebx,%esi),%eax /* get TSS segment descriptor */ + andb $TSS_TYPEFIX_MASK,5(%eax) + + /* Prepare to return to sleep/wakeup code point */ + lgdt PREVIOUS_GDT + lidt PREVIOUS_IDT + + xorl %eax,%eax + movl %eax,%ebx + movl %eax,%ecx + movl %eax,%edx + movl %eax,%esi + movl %eax,%edi + movl PREVIOUS_DS,%ebx + movl PREVIOUS_FS,%ecx + movl PREVIOUS_SS,%edx + movw TASK_REGISTER,%si + shll $16,%esi + movw PREVIOUS_LDT,%si + movl RECOVER_ADDR,%edi + + /* Enable paging and etc. */ + movl CR0_REGISTER,%eax + movl %eax,%cr0 + + /* Flush the prefetch queue */ + jmp 1f +1: jmp 1f +1: + /* + * Now that we are in kernel virtual memory addressing + * %ebx: ds + es + * %ecx: fs + gs + * %edx: ss + dummy + * %esi: LDTR + TR + * %edi: recover address + */ + + nop + + movl %esi,%eax /* LDTR + TR */ + lldt %ax /* load LDT register */ + shrl $16,%eax + ltr %ax /* load task register */ + + /* Restore segment registers */ + movl %ebx,%eax /* ds + es */ + movw %ax,%ds + shrl $16,%eax + movw %ax,%es + movl %ecx,%eax /* fs + gs */ + movw %ax,%fs + shrl $16,%eax + movw %ax,%gs + movl %edx,%eax /* ss */ + movw %ax,%ss + + /* Jump to acpi_restorecpu() */ + jmp *%edi + +/* used in real mode */ +physical_gdt: .word 0 + .long 0 +physical_esp: .long 0 +previous_cr2: .long 0 +previous_cr3: .long 0 +previous_cr4: .long 0 + +/* transfer from real mode to protected mode */ +previous_cr0: .long 0 +previous_tr: .word 0 +previous_gdt: .word 0 + .long 0 +previous_ldt: .word 0 +previous_idt: .word 0 + .long 0 +where_to_recover: .long 0 +previous_ds: .word 0 +previous_es: .word 0 +previous_fs: .word 0 +previous_gs: .word 0 +previous_ss: .word 0 +dummy: .word 0 + diff --git a/sys/i386/acpica/acpi_wakeup.c b/sys/i386/acpica/acpi_wakeup.c new file mode 100644 index 000000000000..2b10978e493c --- /dev/null +++ b/sys/i386/acpica/acpi_wakeup.c @@ -0,0 +1,328 @@ +/*- + * Copyright (c) 2001 Takanori Watanabe + * Copyright (c) 2001 Mitsuru IWASAKI + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include "acpi.h" + +#include + +#include "acpi_wakecode.h" + +extern void initializecpu(void); + +struct region_descriptor r_idt, r_gdt, *p_gdt; +static u_int16_t r_ldt; + +static u_int32_t r_eax, r_ebx, r_ecx, r_edx, r_ebp, r_esi, r_edi, + r_efl, r_cr0, r_cr2, r_cr3, r_cr4, ret_addr; + +static u_int16_t r_cs, r_ds, r_es, r_fs, r_gs, r_ss, r_tr; +static u_int32_t r_esp = 0; + +static int debug_wakeup = 1; +SYSCTL_INT(_debug, OID_AUTO, acpi_wakeup, CTLFLAG_RW, &debug_wakeup, 0, "") + +static void acpi_printcpu(void); +static void acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, + int nsegs, int error); +static void acpi_alloc_wakeup_handler(void); + +/* XXX shut gcc up */ +extern int acpi_savecpu(void); +extern int acpi_restorecpu(void); + +__asm__(" + .text + .p2align 2, 0x90 + .type acpi_restorecpu, @function +acpi_restorecpu: + .align 4 + movl r_eax,%eax + movl r_ebx,%ebx + movl r_ecx,%ecx + movl r_edx,%edx + movl r_ebp,%ebp + movl r_esi,%esi + movl r_edi,%edi + movl r_esp,%esp + + pushl r_efl + popfl + + pushl ret_addr + xorl %eax,%eax + ret + + .text + .p2align 2, 0x90 + .type acpi_savecpu, @function +acpi_savecpu: + movw %cs,r_cs + movw %ds,r_ds + movw %es,r_es + movw %fs,r_fs + movw %gs,r_gs + movw %ss,r_ss + + movl %eax,r_eax + movl %ebx,r_ebx + movl %ecx,r_ecx + movl %edx,r_edx + movl %ebp,r_ebp + movl %esi,r_esi + movl %edi,r_edi + + movl %cr0,%eax + movl %eax,r_cr0 + movl %cr2,%eax + movl %eax,r_cr2 + movl %cr3,%eax + movl %eax,r_cr3 + movl %cr4,%eax + movl %eax,r_cr4 + + pushfl + popl r_efl + + movl %esp,r_esp + + sgdt r_gdt + sidt r_idt + sldt r_ldt + str r_tr + + movl (%esp),%eax + movl %eax,ret_addr + movl $1,%eax + ret +"); + +static void +acpi_printcpu(void) +{ + + printf("======== acpi_printcpu() debug dump ========\n"); + printf("gdt[%04x:%08x] idt[%04x:%08x] ldt[%04x] tr[%04x] efl[%08x]\n", + r_gdt.rd_limit, r_gdt.rd_base, r_idt.rd_limit, r_idt.rd_base, + r_ldt, r_tr, r_efl); + printf("eax[%08x] ebx[%08x] ecx[%08x] edx[%08x]\n", + r_eax, r_ebx, r_ecx, r_edx); + printf("esi[%08x] edi[%08x] ebp[%08x] esp[%08x]\n", + r_esi, r_edi, r_ebp, r_esp); + printf("cr0[%08x] cr2[%08x] cr3[%08x] cr4[%08x]\n", + r_cr0, r_cr2, r_cr3, r_cr4); + printf("cs[%04x] ds[%04x] es[%04x] fs[%04x] gs[%04x] ss[%04x]\n", + r_cs, r_ds, r_es, r_fs, r_gs, r_ss); +} + +int +acpi_sleep_machdep(struct acpi_softc *sc, int state) +{ + ACPI_STATUS status; + void **addr; + vm_offset_t oldphys; + struct pmap *pm; + vm_page_t page; + static vm_page_t opage = NULL; + int ret = 0; + u_long ef; + + if (sc->acpi_wakeaddr == NULL) { + return (0); + } + + AcpiSetFirmwareWakingVector(sc->acpi_wakephys); + + ef = read_eflags(); + disable_intr(); + + /* Create Identity Mapping */ + pm = &CURPROC->p_vmspace->vm_pmap; + oldphys = pmap_extract(pm, sc->acpi_wakephys); + if (oldphys) { + opage = PHYS_TO_VM_PAGE(oldphys); + } + page = PHYS_TO_VM_PAGE(sc->acpi_wakephys); + pmap_enter(pm, sc->acpi_wakephys, page, + VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE, 1); + + ret_addr = 0; + if (acpi_savecpu()) { + /* Execute Sleep */ + p_gdt = (struct region_descriptor *)(sc->acpi_wakeaddr + physical_gdt); + p_gdt->rd_limit = r_gdt.rd_limit; + p_gdt->rd_base = vtophys(r_gdt.rd_base); + addr = (void **)(sc->acpi_wakeaddr + physical_esp); + (u_int32_t *)*addr = vtophys(r_esp); + addr = (void **)(sc->acpi_wakeaddr + previous_cr0); + (u_int32_t *)*addr = r_cr0; + addr = (void **)(sc->acpi_wakeaddr + previous_cr2); + (u_int32_t *)*addr = r_cr2; + addr = (void **)(sc->acpi_wakeaddr + previous_cr3); + (u_int32_t *)*addr = r_cr3; + addr = (void **)(sc->acpi_wakeaddr + previous_cr4); + (u_int32_t *)*addr = r_cr4; + + addr = (void **)(sc->acpi_wakeaddr + previous_tr); + (u_int16_t *)*addr = r_tr; + addr = (void **)(sc->acpi_wakeaddr + previous_gdt); + bcopy(&r_gdt, addr, sizeof(r_gdt)); + addr = (void **)(sc->acpi_wakeaddr + previous_ldt); + (u_int16_t *)*addr = r_ldt; + addr = (void **)(sc->acpi_wakeaddr + previous_idt); + bcopy(&r_idt, addr, sizeof(r_idt)); + addr = (void **)(sc->acpi_wakeaddr + where_to_recover); + *addr = acpi_restorecpu; + addr = (void **)(sc->acpi_wakeaddr + previous_ds); + (u_int16_t *)*addr = r_ds; + addr = (void **)(sc->acpi_wakeaddr + previous_es); + (u_int16_t *)*addr = r_es; + addr = (void **)(sc->acpi_wakeaddr + previous_fs); + (u_int16_t *)*addr = r_fs; + addr = (void **)(sc->acpi_wakeaddr + previous_gs); + (u_int16_t *)*addr = r_gs; + addr = (void **)(sc->acpi_wakeaddr + previous_ss); + (u_int16_t *)*addr = r_ss; + + if (debug_wakeup) { + acpi_printcpu(); + } + + if ((status = AcpiEnterSleepState(state)) != AE_OK) { + device_printf(sc->acpi_dev, + "AcpiEnterSleepState failed - %s\n", + acpi_strerror(status)); + ret = -1; + goto out; + } + wbinvd(); + + for (;;) ; + } else { + /* Execute Wakeup */ +#if 0 + initializecpu(); +#endif + icu_reinit(); + + pmap_remove(pm, sc->acpi_wakephys, + sc->acpi_wakephys + PAGE_SIZE); + if (opage) { + pmap_enter(pm, sc->acpi_wakephys, page, + VM_PROT_READ | VM_PROT_WRITE, 0); + } + + if (debug_wakeup) { + acpi_savecpu(); + acpi_printcpu(); + } + } + +out: + write_eflags(ef); + + return (ret); +} + +static bus_dma_tag_t acpi_waketag; +static bus_dmamap_t acpi_wakemap; +static vm_offset_t acpi_wakeaddr = 0; + +static void +acpi_alloc_wakeup_handler(void) +{ + + if (bus_dma_tag_create(/* parent */ NULL, /* alignment */ 2, 0, + /* lowaddr below 1MB */ 0x9ffff, + /* highaddr */ BUS_SPACE_MAXADDR, NULL, NULL, + PAGE_SIZE, 1, PAGE_SIZE, 0, &acpi_waketag) != 0) { + printf("acpi_alloc_wakeup_handler: unable to create wake tag\n"); + return; + } + + if (bus_dmamem_alloc(acpi_waketag, (void **)&acpi_wakeaddr, + BUS_DMA_NOWAIT, &acpi_wakemap)) { + printf("acpi_alloc_wakeup_handler: unable to allocate wake memory\n"); + return; + } +} + +SYSINIT(acpiwakeup, SI_SUB_KMEM, SI_ORDER_ANY, acpi_alloc_wakeup_handler, 0) + +static void +acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, int nsegs, int error) +{ + struct acpi_softc *sc = arg; + u_int32_t *addr; + + addr = (u_int32_t *)&wakecode[wakeup_sw32 + 2]; + *addr = segs[0].ds_addr + wakeup_32; + bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode)); + sc->acpi_wakephys = segs[0].ds_addr; +} + +void +acpi_install_wakeup_handler(struct acpi_softc *sc) +{ + + if (acpi_wakeaddr == 0) { + return; + } + + sc->acpi_waketag = acpi_waketag; + sc->acpi_wakeaddr = acpi_wakeaddr; + sc->acpi_wakemap = acpi_wakemap; + + bus_dmamap_load(sc->acpi_waketag, sc->acpi_wakemap, + (void *)sc->acpi_wakeaddr, PAGE_SIZE, + acpi_realmodeinst, sc, 0); +} + diff --git a/sys/i386/acpica/genwakecode.pl b/sys/i386/acpica/genwakecode.pl new file mode 100644 index 000000000000..1f5aede59309 --- /dev/null +++ b/sys/i386/acpica/genwakecode.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl +# $FreeBSD$ +print "static char wakecode[] = {\n"; +open(BIN, "hexdump -Cv acpi_wakecode.bin|"); +while () { + s/^[0-9a-f]+//; + s/\|.*$//; + foreach (split()) { + print "0x$_,"; + } + print "\n"; +} +print "};\n"; +close(BIN); + +open(NM, "nm -n acpi_wakecode.o|"); +while () { + split; + print "#define $_[2] 0x$_[0]\n"; +} +close(NM); + diff --git a/sys/i386/isa/intr_machdep.c b/sys/i386/isa/intr_machdep.c index 503163f9dbde..48555d3be315 100644 --- a/sys/i386/isa/intr_machdep.c +++ b/sys/i386/isa/intr_machdep.c @@ -135,6 +135,7 @@ static driver_intr_t isa_strayintr; static void ithds_init(void *dummy); static void ithread_enable(int vector); static void ithread_disable(int vector); +static void init_i8259(void); #ifdef PC98 #define NMI_PARITY 0x04 @@ -281,6 +282,22 @@ isa_nmi(cd) return(retval); } +/* + * ICU reinitialize when ICU configuration has lost. + */ +void icu_reinit() +{ + int i; + u_int32_t eflags; + eflags = read_eflags(); + disable_intr(); + init_i8259(); + for(i=0;iopt_ddb.h +acpi_wakecode.h: acpi_wakecode.S + ${MAKE} -f ${.CURDIR}/../../${MACHINE}/acpica/Makefile MAKESRCPATH=${.CURDIR}/../../${MACHINE}/acpica + .include diff --git a/sys/modules/acpica/Makefile b/sys/modules/acpica/Makefile index 4932b50be03b..375c356642c8 100644 --- a/sys/modules/acpica/Makefile +++ b/sys/modules/acpica/Makefile @@ -2,7 +2,7 @@ .PATH: ${.CURDIR}/../../contrib/dev/acpica \ ${.CURDIR}/../../pci ${.CURDIR}/../../dev/acpica \ - ${.CURDIR}/../../dev/acpica/Osd + ${.CURDIR}/../../dev/acpica/Osd ${.CURDIR}/../../${MACHINE}/acpica KMOD= acpica @@ -30,6 +30,7 @@ SRCS+= utglobal.c utinit.c utmisc.c utobject.c utxface.c SRCS+= acpi.c acpi_acad.c acpi_battery.c acpi_button.c acpi_cmbat.c acpi_cpu.c SRCS+= acpi_ec.c acpi_isa.c acpi_lid.c acpi_pcib.c acpi_powerprofile.c SRCS+= acpi_powerres.c acpi_resource.c acpi_thermal.c acpi_timer.c +SRCS+= acpi_wakecode.h acpi_wakeup.c SRCS+= OsdDebug.c SRCS+= OsdHardware.c OsdInterrupt.c OsdMemory.c OsdSchedule.c SRCS+= OsdStream.c OsdSynch.c OsdEnvironment.c @@ -37,8 +38,12 @@ SRCS+= opt_acpi.h opt_ddb.h SRCS+= device_if.h bus_if.h pci_if.h pcib_if.h isa_if.h CFLAGS+= -I${.CURDIR}/../../contrib/dev/acpica +CLEANFILES+= acpi_wakecode.h acpi_wakecode.o acpi_wakecode.bin opt_ddb.h: Makefile echo '#define DDB 1' >opt_ddb.h +acpi_wakecode.h: acpi_wakecode.S + ${MAKE} -f ${.CURDIR}/../../${MACHINE}/acpica/Makefile MAKESRCPATH=${.CURDIR}/../../${MACHINE}/acpica + .include