acpi_wakeup.c revision 86133
1105004Smp/*- 2190827Semaste * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org> 3190827Semaste * Copyright (c) 2001 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 4190827Semaste * All rights reserved. 5190827Semaste * 6190827Semaste * Redistribution and use in source and binary forms, with or without 7190827Semaste * modification, are permitted provided that the following conditions 8190827Semaste * are met: 9190827Semaste * 1. Redistributions of source code must retain the above copyright 10190827Semaste * notice, this list of conditions and the following disclaimer. 11190827Semaste * 2. Redistributions in binary form must reproduce the above copyright 12190827Semaste * notice, this list of conditions and the following disclaimer in the 13190827Semaste * documentation and/or other materials provided with the distribution. 14190827Semaste * 15190827Semaste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16190827Semaste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17190827Semaste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18190827Semaste * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19190827Semaste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20190827Semaste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21190827Semaste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22190827Semaste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23190827Semaste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24190827Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25190827Semaste * SUCH DAMAGE. 26190827Semaste * 27190827Semaste * $FreeBSD: head/sys/i386/acpica/acpi_wakeup.c 86133 2001-11-06 15:00:30Z iwasaki $ 28190827Semaste */ 29190827Semaste 30190827Semaste#include <sys/param.h> 31190827Semaste#include <sys/systm.h> 32190827Semaste#include <sys/kernel.h> 33190827Semaste#include <sys/bus.h> 34190827Semaste#include <sys/lock.h> 35190827Semaste#include <sys/mutex.h> 36190827Semaste#include <sys/proc.h> 37190827Semaste#include <sys/sysctl.h> 38190827Semaste 39190827Semaste#include <vm/vm.h> 40190827Semaste#include <vm/vm_param.h> 41190827Semaste#include <vm/pmap.h> 42190827Semaste#include <vm/vm_page.h> 43190827Semaste#include <vm/vm_map.h> 44190827Semaste#include <vm/vm_kern.h> 45190827Semaste#include <vm/vm_extern.h> 46190827Semaste 47190827Semaste#include <machine/bus.h> 48190827Semaste#include <machine/cpufunc.h> 49190827Semaste#include <machine/segments.h> 50190827Semaste 51190827Semaste#include <i386/isa/intr_machdep.h> 52190827Semaste 53190827Semaste#include "acpi.h" 54190827Semaste 55190827Semaste#include <dev/acpica/acpica_support.h> 56190827Semaste 57190827Semaste#include <dev/acpica/acpivar.h> 58190827Semaste 59190827Semaste#include "acpi_wakecode.h" 60190827Semaste 61190827Semasteextern void initializecpu(void); 62190827Semaste 63190827Semastestruct region_descriptor r_idt, r_gdt, *p_gdt; 64130807Smarcelstatic u_int16_t r_ldt; 65130807Smarcel 66190827Semastestatic u_int32_t r_eax, r_ebx, r_ecx, r_edx, r_ebp, r_esi, r_edi, 67130807Smarcel r_efl, r_cr0, r_cr2, r_cr3, r_cr4, ret_addr; 68130807Smarcel 69130807Smarcelstatic u_int16_t r_cs, r_ds, r_es, r_fs, r_gs, r_ss, r_tr; 70130807Smarcelstatic u_int32_t r_esp = 0; 71130807Smarcel 72130807Smarcelstatic int debug_wakeup = 1; 73130807SmarcelSYSCTL_INT(_debug, OID_AUTO, acpi_wakeup, CTLFLAG_RW, &debug_wakeup, 0, "") 74130807Smarcel 75190827Semastestatic void acpi_printcpu(void); 76130807Smarcelstatic void acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, 77190827Semaste int nsegs, int error); 78static void acpi_alloc_wakeup_handler(void); 79 80/* XXX shut gcc up */ 81extern int acpi_savecpu(void); 82extern int acpi_restorecpu(void); 83 84__asm__(" 85 .text 86 .p2align 2, 0x90 87 .type acpi_restorecpu, @function 88acpi_restorecpu: 89 .align 4 90 movl r_eax,%eax 91 movl r_ebx,%ebx 92 movl r_ecx,%ecx 93 movl r_edx,%edx 94 movl r_ebp,%ebp 95 movl r_esi,%esi 96 movl r_edi,%edi 97 movl r_esp,%esp 98 99 pushl r_efl 100 popfl 101 102 pushl ret_addr 103 xorl %eax,%eax 104 ret 105 106 .text 107 .p2align 2, 0x90 108 .type acpi_savecpu, @function 109acpi_savecpu: 110 movw %cs,r_cs 111 movw %ds,r_ds 112 movw %es,r_es 113 movw %fs,r_fs 114 movw %gs,r_gs 115 movw %ss,r_ss 116 117 movl %eax,r_eax 118 movl %ebx,r_ebx 119 movl %ecx,r_ecx 120 movl %edx,r_edx 121 movl %ebp,r_ebp 122 movl %esi,r_esi 123 movl %edi,r_edi 124 125 movl %cr0,%eax 126 movl %eax,r_cr0 127 movl %cr2,%eax 128 movl %eax,r_cr2 129 movl %cr3,%eax 130 movl %eax,r_cr3 131 movl %cr4,%eax 132 movl %eax,r_cr4 133 134 pushfl 135 popl r_efl 136 137 movl %esp,r_esp 138 139 sgdt r_gdt 140 sidt r_idt 141 sldt r_ldt 142 str r_tr 143 144 movl (%esp),%eax 145 movl %eax,ret_addr 146 movl $1,%eax 147 ret 148"); 149 150static void 151acpi_printcpu(void) 152{ 153 154 printf("======== acpi_printcpu() debug dump ========\n"); 155 printf("gdt[%04x:%08x] idt[%04x:%08x] ldt[%04x] tr[%04x] efl[%08x]\n", 156 r_gdt.rd_limit, r_gdt.rd_base, r_idt.rd_limit, r_idt.rd_base, 157 r_ldt, r_tr, r_efl); 158 printf("eax[%08x] ebx[%08x] ecx[%08x] edx[%08x]\n", 159 r_eax, r_ebx, r_ecx, r_edx); 160 printf("esi[%08x] edi[%08x] ebp[%08x] esp[%08x]\n", 161 r_esi, r_edi, r_ebp, r_esp); 162 printf("cr0[%08x] cr2[%08x] cr3[%08x] cr4[%08x]\n", 163 r_cr0, r_cr2, r_cr3, r_cr4); 164 printf("cs[%04x] ds[%04x] es[%04x] fs[%04x] gs[%04x] ss[%04x]\n", 165 r_cs, r_ds, r_es, r_fs, r_gs, r_ss); 166} 167 168int 169acpi_sleep_machdep(struct acpi_softc *sc, int state) 170{ 171 ACPI_STATUS status; 172 void **addr; 173 vm_offset_t oldphys; 174 struct pmap *pm; 175 vm_page_t page; 176 static vm_page_t opage = NULL; 177 int ret = 0; 178 u_long ef; 179 180 if (sc->acpi_wakeaddr == NULL) { 181 return (0); 182 } 183 184 AcpiSetFirmwareWakingVector(sc->acpi_wakephys); 185 186 ef = read_eflags(); 187 disable_intr(); 188 189 /* Create Identity Mapping */ 190 pm = &CURPROC->p_vmspace->vm_pmap; 191 oldphys = pmap_extract(pm, sc->acpi_wakephys); 192 if (oldphys) { 193 opage = PHYS_TO_VM_PAGE(oldphys); 194 } 195 page = PHYS_TO_VM_PAGE(sc->acpi_wakephys); 196 pmap_enter(pm, sc->acpi_wakephys, page, 197 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE, 1); 198 199 ret_addr = 0; 200 if (acpi_savecpu()) { 201 /* Execute Sleep */ 202 p_gdt = (struct region_descriptor *)(sc->acpi_wakeaddr + physical_gdt); 203 p_gdt->rd_limit = r_gdt.rd_limit; 204 p_gdt->rd_base = vtophys(r_gdt.rd_base); 205 addr = (void **)(sc->acpi_wakeaddr + physical_esp); 206 (u_int32_t *)*addr = vtophys(r_esp); 207 addr = (void **)(sc->acpi_wakeaddr + previous_cr0); 208 (u_int32_t *)*addr = r_cr0; 209 addr = (void **)(sc->acpi_wakeaddr + previous_cr2); 210 (u_int32_t *)*addr = r_cr2; 211 addr = (void **)(sc->acpi_wakeaddr + previous_cr3); 212 (u_int32_t *)*addr = r_cr3; 213 addr = (void **)(sc->acpi_wakeaddr + previous_cr4); 214 (u_int32_t *)*addr = r_cr4; 215 216 addr = (void **)(sc->acpi_wakeaddr + previous_tr); 217 (u_int16_t *)*addr = r_tr; 218 addr = (void **)(sc->acpi_wakeaddr + previous_gdt); 219 bcopy(&r_gdt, addr, sizeof(r_gdt)); 220 addr = (void **)(sc->acpi_wakeaddr + previous_ldt); 221 (u_int16_t *)*addr = r_ldt; 222 addr = (void **)(sc->acpi_wakeaddr + previous_idt); 223 bcopy(&r_idt, addr, sizeof(r_idt)); 224 addr = (void **)(sc->acpi_wakeaddr + where_to_recover); 225 *addr = acpi_restorecpu; 226 addr = (void **)(sc->acpi_wakeaddr + previous_ds); 227 (u_int16_t *)*addr = r_ds; 228 addr = (void **)(sc->acpi_wakeaddr + previous_es); 229 (u_int16_t *)*addr = r_es; 230 addr = (void **)(sc->acpi_wakeaddr + previous_fs); 231 (u_int16_t *)*addr = r_fs; 232 addr = (void **)(sc->acpi_wakeaddr + previous_gs); 233 (u_int16_t *)*addr = r_gs; 234 addr = (void **)(sc->acpi_wakeaddr + previous_ss); 235 (u_int16_t *)*addr = r_ss; 236 237 if (debug_wakeup) { 238 acpi_printcpu(); 239 } 240 241 if (state == ACPI_STATE_S4 && sc->acpi_s4bios) { 242 status = AcpiEnterSleepStateS4Bios(); 243 } else { 244 status = AcpiEnterSleepState(state); 245 } 246 247 if (status != AE_OK) { 248 device_printf(sc->acpi_dev, 249 "AcpiEnterSleepState failed - %s\n", 250 AcpiFormatException(status)); 251 ret = -1; 252 goto out; 253 } 254 wbinvd(); 255 256 for (;;) ; 257 } else { 258 /* Execute Wakeup */ 259#if 0 260 initializecpu(); 261#endif 262 icu_reinit(); 263 264 if (debug_wakeup) { 265 acpi_savecpu(); 266 acpi_printcpu(); 267 } 268 } 269 270out: 271 pmap_remove(pm, sc->acpi_wakephys, sc->acpi_wakephys + PAGE_SIZE); 272 if (opage) { 273 pmap_enter(pm, sc->acpi_wakephys, page, 274 VM_PROT_READ | VM_PROT_WRITE, 0); 275 } 276 277 write_eflags(ef); 278 279 return (ret); 280} 281 282static bus_dma_tag_t acpi_waketag; 283static bus_dmamap_t acpi_wakemap; 284static vm_offset_t acpi_wakeaddr = 0; 285 286static void 287acpi_alloc_wakeup_handler(void) 288{ 289 290 if (bus_dma_tag_create(/* parent */ NULL, /* alignment */ 2, 0, 291 /* lowaddr below 1MB */ 0x9ffff, 292 /* highaddr */ BUS_SPACE_MAXADDR, NULL, NULL, 293 PAGE_SIZE, 1, PAGE_SIZE, 0, &acpi_waketag) != 0) { 294 printf("acpi_alloc_wakeup_handler: unable to create wake tag\n"); 295 return; 296 } 297 298 if (bus_dmamem_alloc(acpi_waketag, (void **)&acpi_wakeaddr, 299 BUS_DMA_NOWAIT, &acpi_wakemap)) { 300 printf("acpi_alloc_wakeup_handler: unable to allocate wake memory\n"); 301 return; 302 } 303} 304 305SYSINIT(acpiwakeup, SI_SUB_KMEM, SI_ORDER_ANY, acpi_alloc_wakeup_handler, 0) 306 307static void 308acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 309{ 310 struct acpi_softc *sc = arg; 311 u_int32_t *addr; 312 313 addr = (u_int32_t *)&wakecode[wakeup_sw32 + 2]; 314 *addr = segs[0].ds_addr + wakeup_32; 315 bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode)); 316 sc->acpi_wakephys = segs[0].ds_addr; 317} 318 319void 320acpi_install_wakeup_handler(struct acpi_softc *sc) 321{ 322 323 if (acpi_wakeaddr == 0) { 324 return; 325 } 326 327 sc->acpi_waketag = acpi_waketag; 328 sc->acpi_wakeaddr = acpi_wakeaddr; 329 sc->acpi_wakemap = acpi_wakemap; 330 331 bus_dmamap_load(sc->acpi_waketag, sc->acpi_wakemap, 332 (void *)sc->acpi_wakeaddr, PAGE_SIZE, 333 acpi_realmodeinst, sc, 0); 334} 335 336