acpi_wakeup.c revision 107199
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 107199 2002-11-24 02:27:07Z iwasaki $ 28 */ 29 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 61extern void initializecpu(void); 62 63static struct region_descriptor r_idt, r_gdt, *p_gdt; 64static u_int16_t r_ldt; 65 66static u_int32_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; 68 69static u_int16_t r_cs, r_ds, r_es, r_fs, r_gs, r_ss, r_tr; 70static u_int32_t r_esp = 0; 71 72static void acpi_printcpu(void); 73static void acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, 74 int nsegs, int error); 75static void acpi_alloc_wakeup_handler(void); 76 77/* XXX shut gcc up */ 78extern int acpi_savecpu(void); 79extern int acpi_restorecpu(void); 80 81#ifdef __GNUC__ 82__asm__(" \n\ 83 .text \n\ 84 .p2align 2, 0x90 \n\ 85 .type acpi_restorecpu, @function\n\ 86acpi_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\ 104 \n\ 105 .text \n\ 106 .p2align 2, 0x90 \n\ 107 .type acpi_savecpu, @function \n\ 108acpi_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\ 115 \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\ 123 \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\ 132 \n\ 133 pushfl \n\ 134 popl r_efl \n\ 135 \n\ 136 movl %esp,r_esp \n\ 137 \n\ 138 sgdt r_gdt \n\ 139 sidt r_idt \n\ 140 sldt r_ldt \n\ 141 str r_tr \n\ 142 \n\ 143 movl (%esp),%eax \n\ 144 movl %eax,ret_addr \n\ 145 movl $1,%eax \n\ 146 ret \n\ 147"); 148#endif /* __GNUC__ */ 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 168#define WAKECODE_FIXUP(offset, type, val) do { \ 169 void **addr; \ 170 addr = (void **)(sc->acpi_wakeaddr + offset); \ 171 (type *)*addr = val; \ 172} while (0) 173 174#define WAKECODE_BCOPY(offset, type, val) do { \ 175 void **addr; \ 176 addr = (void **)(sc->acpi_wakeaddr + offset); \ 177 bcopy(&(val), addr, sizeof(type)); \ 178} while (0) 179 180int 181acpi_sleep_machdep(struct acpi_softc *sc, int state) 182{ 183 ACPI_STATUS status; 184 vm_offset_t oldphys; 185 struct pmap *pm; 186 vm_page_t page; 187 static vm_page_t opage = NULL; 188 int ret = 0; 189 int pteobj_allocated = 0; 190 u_long ef; 191 struct proc *p; 192 193 if (sc->acpi_wakeaddr == 0) { 194 return (0); 195 } 196 197 AcpiSetFirmwareWakingVector(sc->acpi_wakephys); 198 199 ef = read_eflags(); 200 disable_intr(); 201 202 /* Create Identity Mapping */ 203 if ((p = curproc) == NULL) 204 p = &proc0; 205 pm = vmspace_pmap(p->p_vmspace); 206 if (pm->pm_pteobj == NULL) { 207 pm->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, PTDPTDI + 1); 208 pteobj_allocated = 1; 209 } 210 211 oldphys = pmap_extract(pm, sc->acpi_wakephys); 212 if (oldphys) { 213 opage = PHYS_TO_VM_PAGE(oldphys); 214 } 215 page = PHYS_TO_VM_PAGE(sc->acpi_wakephys); 216 pmap_enter(pm, sc->acpi_wakephys, page, 217 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE, 1); 218 219 ret_addr = 0; 220 if (acpi_savecpu()) { 221 /* Execute Sleep */ 222 p_gdt = (struct region_descriptor *)(sc->acpi_wakeaddr + physical_gdt); 223 p_gdt->rd_limit = r_gdt.rd_limit; 224 p_gdt->rd_base = vtophys(r_gdt.rd_base); 225 226 WAKECODE_FIXUP(physical_esp, u_int32_t, vtophys(r_esp)); 227 WAKECODE_FIXUP(previous_cr0, u_int32_t, r_cr0); 228 WAKECODE_FIXUP(previous_cr2, u_int32_t, r_cr2); 229 WAKECODE_FIXUP(previous_cr3, u_int32_t, r_cr3); 230 WAKECODE_FIXUP(previous_cr4, u_int32_t, r_cr4); 231 232 WAKECODE_FIXUP(previous_tr, u_int16_t, r_tr); 233 WAKECODE_BCOPY(previous_gdt, struct region_descriptor, r_gdt); 234 WAKECODE_FIXUP(previous_ldt, u_int16_t, r_ldt); 235 WAKECODE_BCOPY(previous_idt, struct region_descriptor, r_idt); 236 237 WAKECODE_FIXUP(where_to_recover, void, acpi_restorecpu); 238 239 WAKECODE_FIXUP(previous_ds, u_int16_t, r_ds); 240 WAKECODE_FIXUP(previous_es, u_int16_t, r_es); 241 WAKECODE_FIXUP(previous_fs, u_int16_t, r_fs); 242 WAKECODE_FIXUP(previous_gs, u_int16_t, r_gs); 243 WAKECODE_FIXUP(previous_ss, u_int16_t, r_ss); 244 245 if (acpi_get_verbose(sc)) { 246 acpi_printcpu(); 247 } 248 249 wbinvd(); 250 251 if (state == ACPI_STATE_S4 && sc->acpi_s4bios) { 252 status = AcpiEnterSleepStateS4Bios(); 253 } else { 254 status = AcpiEnterSleepState(state); 255 } 256 257 if (status != AE_OK) { 258 device_printf(sc->acpi_dev, 259 "AcpiEnterSleepState failed - %s\n", 260 AcpiFormatException(status)); 261 ret = -1; 262 goto out; 263 } 264 265 for (;;) ; 266 } else { 267 /* Execute Wakeup */ 268#if 0 269 initializecpu(); 270#endif 271 icu_reinit(); 272 273 if (acpi_get_verbose(sc)) { 274 acpi_savecpu(); 275 acpi_printcpu(); 276 } 277 } 278 279out: 280 pmap_remove(pm, sc->acpi_wakephys, sc->acpi_wakephys + PAGE_SIZE); 281 if (opage) { 282 pmap_enter(pm, sc->acpi_wakephys, page, 283 VM_PROT_READ | VM_PROT_WRITE, 0); 284 } 285 286 if (pteobj_allocated) { 287 vm_object_deallocate(pm->pm_pteobj); 288 pm->pm_pteobj = NULL; 289 } 290 291 write_eflags(ef); 292 293 return (ret); 294} 295 296static bus_dma_tag_t acpi_waketag; 297static bus_dmamap_t acpi_wakemap; 298static vm_offset_t acpi_wakeaddr = 0; 299 300static void 301acpi_alloc_wakeup_handler(void) 302{ 303 304 if (!cold) 305 return; 306 307 if (bus_dma_tag_create(/* parent */ NULL, /* alignment */ 2, 0, 308 /* lowaddr below 1MB */ 0x9ffff, 309 /* highaddr */ BUS_SPACE_MAXADDR, NULL, NULL, 310 PAGE_SIZE, 1, PAGE_SIZE, 0, &acpi_waketag) != 0) { 311 printf("acpi_alloc_wakeup_handler: unable to create wake tag\n"); 312 return; 313 } 314 315 if (bus_dmamem_alloc(acpi_waketag, (void **)&acpi_wakeaddr, 316 BUS_DMA_NOWAIT, &acpi_wakemap)) { 317 printf("acpi_alloc_wakeup_handler: unable to allocate wake memory\n"); 318 return; 319 } 320} 321 322SYSINIT(acpiwakeup, SI_SUB_KMEM, SI_ORDER_ANY, acpi_alloc_wakeup_handler, 0) 323 324static void 325acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 326{ 327 struct acpi_softc *sc = arg; 328 u_int32_t *addr; 329 330 addr = (u_int32_t *)&wakecode[wakeup_sw32 + 2]; 331 *addr = segs[0].ds_addr + wakeup_32; 332 bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode)); 333 sc->acpi_wakephys = segs[0].ds_addr; 334} 335 336void 337acpi_install_wakeup_handler(struct acpi_softc *sc) 338{ 339 340 if (acpi_wakeaddr == 0) { 341 return; 342 } 343 344 sc->acpi_waketag = acpi_waketag; 345 sc->acpi_wakeaddr = acpi_wakeaddr; 346 sc->acpi_wakemap = acpi_wakemap; 347 348 bus_dmamap_load(sc->acpi_waketag, sc->acpi_wakemap, 349 (void *)sc->acpi_wakeaddr, PAGE_SIZE, 350 acpi_realmodeinst, sc, 0); 351} 352 353