acpi_wakeup.c revision 126891
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 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD: head/sys/i386/acpica/acpi_wakeup.c 126891 2004-03-12 21:45:33Z trhodes $"); 30 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> 38 39#include <vm/vm.h> 40#include <vm/vm_param.h> 41#include <vm/pmap.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> 47 48#include <machine/bus.h> 49#include <machine/cpufunc.h> 50#include <machine/intr_machdep.h> 51#include <machine/segments.h> 52 53#include "acpi.h" 54#include <dev/acpica/acpivar.h> 55 56#include "acpi_wakecode.h" 57 58#if __FreeBSD_version < 500000 59#define vm_page_lock_queues() 60#define vm_page_unlock_queues() 61#endif 62 63extern uint32_t acpi_reset_video; 64extern void initializecpu(void); 65 66static struct region_descriptor r_idt, r_gdt, *p_gdt; 67static u_int16_t r_ldt; 68 69static u_int32_t r_eax, r_ebx, r_ecx, r_edx, r_ebp, r_esi, r_edi, 70 r_efl, r_cr0, r_cr2, r_cr3, r_cr4, ret_addr; 71 72static u_int16_t r_cs, r_ds, r_es, r_fs, r_gs, r_ss, r_tr; 73static u_int32_t r_esp = 0; 74 75static void acpi_printcpu(void); 76static void acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, 77 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#if defined(__GNUC__) || defined(__INTEL_COMPILER) 85__asm__(" \n\ 86 .text \n\ 87 .p2align 2, 0x90 \n\ 88 .type acpi_restorecpu, @function\n\ 89acpi_restorecpu: \n\ 90 .align 4 \n\ 91 movl r_eax,%eax \n\ 92 movl r_ebx,%ebx \n\ 93 movl r_ecx,%ecx \n\ 94 movl r_edx,%edx \n\ 95 movl r_ebp,%ebp \n\ 96 movl r_esi,%esi \n\ 97 movl r_edi,%edi \n\ 98 movl r_esp,%esp \n\ 99 \n\ 100 pushl r_efl \n\ 101 popfl \n\ 102 \n\ 103 movl ret_addr,%eax \n\ 104 movl %eax,(%esp) \n\ 105 xorl %eax,%eax \n\ 106 ret \n\ 107 \n\ 108 .text \n\ 109 .p2align 2, 0x90 \n\ 110 .type acpi_savecpu, @function \n\ 111acpi_savecpu: \n\ 112 movw %cs,r_cs \n\ 113 movw %ds,r_ds \n\ 114 movw %es,r_es \n\ 115 movw %fs,r_fs \n\ 116 movw %gs,r_gs \n\ 117 movw %ss,r_ss \n\ 118 \n\ 119 movl %eax,r_eax \n\ 120 movl %ebx,r_ebx \n\ 121 movl %ecx,r_ecx \n\ 122 movl %edx,r_edx \n\ 123 movl %ebp,r_ebp \n\ 124 movl %esi,r_esi \n\ 125 movl %edi,r_edi \n\ 126 \n\ 127 movl %cr0,%eax \n\ 128 movl %eax,r_cr0 \n\ 129 movl %cr2,%eax \n\ 130 movl %eax,r_cr2 \n\ 131 movl %cr3,%eax \n\ 132 movl %eax,r_cr3 \n\ 133 movl %cr4,%eax \n\ 134 movl %eax,r_cr4 \n\ 135 \n\ 136 pushfl \n\ 137 popl r_efl \n\ 138 \n\ 139 movl %esp,r_esp \n\ 140 \n\ 141 sgdt r_gdt \n\ 142 sidt r_idt \n\ 143 sldt r_ldt \n\ 144 str r_tr \n\ 145 \n\ 146 movl (%esp),%eax \n\ 147 movl %eax,ret_addr \n\ 148 movl $1,%eax \n\ 149 ret \n\ 150"); 151#endif /* __GNUC__ || __INTEL_COMPILER */ 152 153static void 154acpi_printcpu(void) 155{ 156 printf("======== acpi_printcpu() debug dump ========\n"); 157 printf("gdt[%04x:%08x] idt[%04x:%08x] ldt[%04x] tr[%04x] efl[%08x]\n", 158 r_gdt.rd_limit, r_gdt.rd_base, r_idt.rd_limit, r_idt.rd_base, 159 r_ldt, r_tr, r_efl); 160 printf("eax[%08x] ebx[%08x] ecx[%08x] edx[%08x]\n", 161 r_eax, r_ebx, r_ecx, r_edx); 162 printf("esi[%08x] edi[%08x] ebp[%08x] esp[%08x]\n", 163 r_esi, r_edi, r_ebp, r_esp); 164 printf("cr0[%08x] cr2[%08x] cr3[%08x] cr4[%08x]\n", 165 r_cr0, r_cr2, r_cr3, r_cr4); 166 printf("cs[%04x] ds[%04x] es[%04x] fs[%04x] gs[%04x] ss[%04x]\n", 167 r_cs, r_ds, r_es, r_fs, r_gs, r_ss); 168} 169 170#define WAKECODE_FIXUP(offset, type, val) do { \ 171 void **addr; \ 172 addr = (void **)(sc->acpi_wakeaddr + offset); \ 173 (type *)*addr = val; \ 174} while (0) 175 176#define WAKECODE_BCOPY(offset, type, val) do { \ 177 void **addr; \ 178 addr = (void **)(sc->acpi_wakeaddr + offset); \ 179 bcopy(&(val), addr, sizeof(type)); \ 180} while (0) 181 182int 183acpi_sleep_machdep(struct acpi_softc *sc, int state) 184{ 185 ACPI_STATUS status; 186 vm_paddr_t oldphys; 187 struct pmap *pm; 188 vm_page_t page; 189 static vm_page_t opage = NULL; 190 int ret = 0; 191 u_int32_t cr3; 192 u_long ef; 193 struct proc *p; 194 195 if (sc->acpi_wakeaddr == 0) 196 return (0); 197 198 AcpiSetFirmwareWakingVector(sc->acpi_wakephys); 199 200 ef = read_eflags(); 201 ACPI_DISABLE_IRQS(); 202 203 /* Create Identity Mapping */ 204 if ((p = curproc) == NULL) 205 p = &proc0; 206 pm = vmspace_pmap(p->p_vmspace); 207 cr3 = rcr3(); 208#ifdef PAE 209 load_cr3(vtophys(pm->pm_pdpt)); 210#else 211 load_cr3(vtophys(pm->pm_pdir)); 212#endif 213 214 oldphys = pmap_extract(pm, sc->acpi_wakephys); 215 if (oldphys) 216 opage = PHYS_TO_VM_PAGE(oldphys); 217 page = PHYS_TO_VM_PAGE(sc->acpi_wakephys); 218 pmap_enter(pm, sc->acpi_wakephys, page, 219 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE, 1); 220 221 ret_addr = 0; 222 if (acpi_savecpu()) { 223 /* Execute Sleep */ 224 intr_suspend(); 225 226 p_gdt = (struct region_descriptor *) 227 (sc->acpi_wakeaddr + physical_gdt); 228 p_gdt->rd_limit = r_gdt.rd_limit; 229 p_gdt->rd_base = vtophys(r_gdt.rd_base); 230 231 WAKECODE_FIXUP(physical_esp, u_int32_t, vtophys(r_esp)); 232 WAKECODE_FIXUP(previous_cr0, u_int32_t, r_cr0); 233 WAKECODE_FIXUP(previous_cr2, u_int32_t, r_cr2); 234 WAKECODE_FIXUP(previous_cr3, u_int32_t, r_cr3); 235 WAKECODE_FIXUP(previous_cr4, u_int32_t, r_cr4); 236 237 WAKECODE_FIXUP(reset_video, uint32_t, acpi_reset_video); 238 239 WAKECODE_FIXUP(previous_tr, u_int16_t, r_tr); 240 WAKECODE_BCOPY(previous_gdt, struct region_descriptor, r_gdt); 241 WAKECODE_FIXUP(previous_ldt, u_int16_t, r_ldt); 242 WAKECODE_BCOPY(previous_idt, struct region_descriptor, r_idt); 243 244 WAKECODE_FIXUP(where_to_recover, void, acpi_restorecpu); 245 246 WAKECODE_FIXUP(previous_ds, u_int16_t, r_ds); 247 WAKECODE_FIXUP(previous_es, u_int16_t, r_es); 248 WAKECODE_FIXUP(previous_fs, u_int16_t, r_fs); 249 WAKECODE_FIXUP(previous_gs, u_int16_t, r_gs); 250 WAKECODE_FIXUP(previous_ss, u_int16_t, r_ss); 251 252 if (acpi_get_verbose(sc)) 253 acpi_printcpu(); 254 255 /* Call ACPICA to enter the desired sleep state */ 256 if (state == ACPI_STATE_S4 && sc->acpi_s4bios) 257 status = AcpiEnterSleepStateS4bios(); 258 else 259 status = AcpiEnterSleepState(state); 260 261 if (status != AE_OK) { 262 device_printf(sc->acpi_dev, 263 "AcpiEnterSleepState failed - %s\n", 264 AcpiFormatException(status)); 265 ret = -1; 266 goto out; 267 } 268 269 for (;;) ; 270 } else { 271 /* Execute Wakeup */ 272#if 0 273 initializecpu(); 274#endif 275 intr_resume(); 276 277 if (acpi_get_verbose(sc)) { 278 acpi_savecpu(); 279 acpi_printcpu(); 280 } 281 } 282 283out: 284 vm_page_lock_queues(); 285 pmap_remove(pm, sc->acpi_wakephys, sc->acpi_wakephys + PAGE_SIZE); 286 vm_page_unlock_queues(); 287 if (opage) { 288 pmap_enter(pm, sc->acpi_wakephys, page, 289 VM_PROT_READ | VM_PROT_WRITE, 0); 290 } 291 292 load_cr3(cr3); 293 294 write_eflags(ef); 295 296 return (ret); 297} 298 299static bus_dma_tag_t acpi_waketag; 300static bus_dmamap_t acpi_wakemap; 301static vm_offset_t acpi_wakeaddr = 0; 302 303static void 304acpi_alloc_wakeup_handler(void) 305{ 306 if (!cold) 307 return; 308 309 if (bus_dma_tag_create(/* parent */ NULL, /* alignment */ 2, 0, 310 /* lowaddr below 1MB */ 0x9ffff, 311 /* highaddr */ BUS_SPACE_MAXADDR, NULL, NULL, 312 PAGE_SIZE, 1, PAGE_SIZE, 0, busdma_lock_mutex, 313 &Giant, &acpi_waketag) != 0) { 314 printf("acpi_alloc_wakeup_handler: can't create wake tag\n"); 315 return; 316 } 317 318 if (bus_dmamem_alloc(acpi_waketag, (void **)&acpi_wakeaddr, 319 BUS_DMA_NOWAIT, &acpi_wakemap)) { 320 printf("acpi_alloc_wakeup_handler: can't alloc wake memory\n"); 321 return; 322 } 323} 324 325SYSINIT(acpiwakeup, SI_SUB_KMEM, SI_ORDER_ANY, acpi_alloc_wakeup_handler, 0) 326 327static void 328acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 329{ 330 struct acpi_softc *sc = arg; 331 u_int32_t *addr; 332 333 addr = (u_int32_t *)&wakecode[wakeup_sw32 + 2]; 334 *addr = segs[0].ds_addr + wakeup_32; 335 bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode)); 336 sc->acpi_wakephys = segs[0].ds_addr; 337} 338 339void 340acpi_install_wakeup_handler(struct acpi_softc *sc) 341{ 342 if (acpi_wakeaddr == 0) 343 return; 344 345 sc->acpi_waketag = acpi_waketag; 346 sc->acpi_wakeaddr = acpi_wakeaddr; 347 sc->acpi_wakemap = acpi_wakemap; 348 349 bus_dmamap_load(sc->acpi_waketag, sc->acpi_wakemap, 350 (void *)sc->acpi_wakeaddr, PAGE_SIZE, 351 acpi_realmodeinst, sc, 0); 352} 353