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$"); 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/pmap.h> 41 #include <vm/vm_object.h> 42 #include <vm/vm_page.h> 43 #include <vm/vm_map.h> 44 45 #include <machine/bus.h> 46 #include <machine/cpufunc.h> 47 #include <machine/intr_machdep.h> 48 #include <x86/mca.h> 49 #include <machine/segments.h> 50 51 #include <contrib/dev/acpica/include/acpi.h> 52 53 #include <dev/acpica/acpivar.h> 54 55 #include "acpi_wakecode.h" 56 57 /* Make sure the code is less than one page and leave room for the stack. */ 58 CTASSERT(sizeof(wakecode) < PAGE_SIZE - 1024); 59 60 #ifndef _SYS_CDEFS_H_ 61 #error this file needs sys/cdefs.h as a prerequisite 62 #endif 63 64 extern uint32_t acpi_resume_beep; 65 extern uint32_t acpi_reset_video; 66 extern void initializecpu(void); 67 68 static struct region_descriptor __used saved_idt, saved_gdt; 69 static struct region_descriptor *p_gdt; 70 static uint16_t __used saved_ldt; 71 72 static uint32_t __used 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; 74 75 static uint16_t __used r_cs, r_ds, r_es, r_fs, r_gs, r_ss, r_tr; 76 static uint32_t __used r_esp; 77 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); 82 83 /* XXX shut gcc up */ 84 extern int acpi_savecpu(void); 85 extern int acpi_restorecpu(void); 86 87 #ifdef __GNUCLIKE_ASM 88 __asm__(" \n\ 89 .text \n\ 90 .p2align 2, 0x90 \n\ 91 .type acpi_restorecpu, @function\n\ 92 acpi_restorecpu: \n\ 93 .align 4 \n\ 94 movl r_eax,%eax \n\ 95 movl r_ebx,%ebx \n\ 96 movl r_ecx,%ecx \n\ 97 movl r_edx,%edx \n\ 98 movl r_ebp,%ebp \n\ 99 movl r_esi,%esi \n\ 100 movl r_edi,%edi \n\ 101 movl r_esp,%esp \n\ 102 \n\ 103 pushl r_efl \n\ 104 popfl \n\ 105 \n\ 106 movl ret_addr,%eax \n\ 107 movl %eax,(%esp) \n\ 108 xorl %eax,%eax \n\ 109 ret \n\ 110 \n\ 111 .text \n\ 112 .p2align 2, 0x90 \n\ 113 .type acpi_savecpu, @function \n\ 114 acpi_savecpu: \n\ 115 movw %cs,r_cs \n\ 116 movw %ds,r_ds \n\ 117 movw %es,r_es \n\ 118 movw %fs,r_fs \n\ 119 movw %gs,r_gs \n\ 120 movw %ss,r_ss \n\ 121 \n\ 122 movl %eax,r_eax \n\ 123 movl %ebx,r_ebx \n\ 124 movl %ecx,r_ecx \n\ 125 movl %edx,r_edx \n\ 126 movl %ebp,r_ebp \n\ 127 movl %esi,r_esi \n\ 128 movl %edi,r_edi \n\ 129 \n\ 130 movl %cr0,%eax \n\ 131 movl %eax,r_cr0 \n\ 132 movl %cr2,%eax \n\ 133 movl %eax,r_cr2 \n\ 134 movl %cr3,%eax \n\ 135 movl %eax,r_cr3 \n\ 136 movl %cr4,%eax \n\ 137 movl %eax,r_cr4 \n\ 138 \n\ 139 pushfl \n\ 140 popl r_efl \n\ 141 \n\ 142 movl %esp,r_esp \n\ 143 \n\ 144 sgdt saved_gdt \n\ 145 sidt saved_idt \n\ 146 sldt saved_ldt \n\ 147 str r_tr \n\ 148 \n\ 149 movl (%esp),%eax \n\ 150 movl %eax,ret_addr \n\ 151 movl $1,%eax \n\ 152 ret \n\ 153 "); 154 #endif /* __GNUCLIKE_ASM */ 155 156 static void 157 acpi_printcpu(void) 158 { 159 printf("======== acpi_printcpu() debug dump ========\n"); 160 printf("gdt[%04x:%08x] idt[%04x:%08x] ldt[%04x] tr[%04x] efl[%08x]\n", 161 saved_gdt.rd_limit, saved_gdt.rd_base, 162 saved_idt.rd_limit, saved_idt.rd_base, 163 saved_ldt, r_tr, r_efl); 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); 172 } 173 174 #define WAKECODE_FIXUP(offset, type, val) do { \ 175 type *addr; \ 176 addr = (type *)(sc->acpi_wakeaddr + offset); \ 177 *addr = val; \ 178 } while (0) 179 180 #define WAKECODE_BCOPY(offset, type, val) do { \ 181 void *addr; \ 182 addr = (void *)(sc->acpi_wakeaddr + offset); \ 183 bcopy(&(val), addr, sizeof(type)); \ 184 } while (0) 185 186 /* Turn off bits 1&2 of the PIT, stopping the beep. */ 187 static void 188 acpi_stop_beep(void *arg) 189 { 190 outb(0x61, inb(0x61) & ~0x3); 191 } 192 193 int 194 acpi_sleep_machdep(struct acpi_softc *sc, int state) 195 { 196 ACPI_STATUS status; 197 struct pmap *pm; 198 int ret; 199 uint32_t cr3; 200 u_long ef; 201 202 ret = 0; 203 if (sc->acpi_wakeaddr == 0) 204 return (0); 205 206 AcpiSetFirmwareWakingVector(sc->acpi_wakephys); 207 208 ef = read_eflags(); 209 210 /* 211 * Temporarily switch to the kernel pmap because it provides an 212 * identity mapping (setup at boot) for the low physical memory 213 * region containing the wakeup code. 214 */ 215 pm = kernel_pmap; 216 cr3 = rcr3(); 217 #ifdef PAE 218 load_cr3(vtophys(pm->pm_pdpt)); 219 #else 220 load_cr3(vtophys(pm->pm_pdir)); 221 #endif 222 223 ret_addr = 0; 224 ACPI_DISABLE_IRQS(); 225 if (acpi_savecpu()) { 226 /* Execute Sleep */ 227 intr_suspend(); 228 229 p_gdt = (struct region_descriptor *) 230 (sc->acpi_wakeaddr + physical_gdt); 231 p_gdt->rd_limit = saved_gdt.rd_limit; 232 p_gdt->rd_base = vtophys(saved_gdt.rd_base); 233 234 WAKECODE_FIXUP(physical_esp, uint32_t, vtophys(r_esp)); 235 WAKECODE_FIXUP(previous_cr0, uint32_t, r_cr0); 236 WAKECODE_FIXUP(previous_cr2, uint32_t, r_cr2); 237 WAKECODE_FIXUP(previous_cr3, uint32_t, r_cr3); 238 WAKECODE_FIXUP(previous_cr4, uint32_t, r_cr4); 239 240 WAKECODE_FIXUP(resume_beep, uint32_t, acpi_resume_beep); 241 WAKECODE_FIXUP(reset_video, uint32_t, acpi_reset_video); 242 243 WAKECODE_FIXUP(previous_tr, uint16_t, r_tr); 244 WAKECODE_BCOPY(previous_gdt, struct region_descriptor, saved_gdt); 245 WAKECODE_FIXUP(previous_ldt, uint16_t, saved_ldt); 246 WAKECODE_BCOPY(previous_idt, struct region_descriptor, saved_idt); 247 248 WAKECODE_FIXUP(where_to_recover, void *, acpi_restorecpu); 249 250 WAKECODE_FIXUP(previous_ds, uint16_t, r_ds); 251 WAKECODE_FIXUP(previous_es, uint16_t, r_es); 252 WAKECODE_FIXUP(previous_fs, uint16_t, r_fs); 253 WAKECODE_FIXUP(previous_gs, uint16_t, r_gs); 254 WAKECODE_FIXUP(previous_ss, uint16_t, r_ss); 255 256 if (bootverbose) 257 acpi_printcpu(); 258 259 /* Call ACPICA to enter the desired sleep state */ 260 if (state == ACPI_STATE_S4 && sc->acpi_s4bios) 261 status = AcpiEnterSleepStateS4bios(); 262 else 263 status = AcpiEnterSleepState(state); 264 265 if (status != AE_OK) { 266 device_printf(sc->acpi_dev, 267 "AcpiEnterSleepState failed - %s\n", 268 AcpiFormatException(status)); 269 ret = -1; 270 goto out; 271 } 272 273 for (;;) ; 274 } else { 275 /* Execute Wakeup */ 276 mca_resume(); 277 intr_resume(); 278 279 if (bootverbose) { 280 acpi_savecpu(); 281 acpi_printcpu(); 282 } 283 } 284 285 out: 286 load_cr3(cr3); 287 write_eflags(ef); 288 289 /* If we beeped, turn it off after a delay. */ 290 if (acpi_resume_beep) 291 timeout(acpi_stop_beep, NULL, 3 * hz); 292 293 return (ret); 294 } 295 296 static bus_dma_tag_t acpi_waketag; 297 static bus_dmamap_t acpi_wakemap; 298 static vm_offset_t acpi_wakeaddr; 299 300 static void 301 acpi_alloc_wakeup_handler(void) 302 { 303 void *wakeaddr; 304 305 if (!cold) 306 return; 307 308 /* 309 * Specify the region for our wakeup code. We want it in the low 1 MB 310 * region, excluding video memory and above (0xa0000). We ask for 311 * it to be page-aligned, just to be safe. 312 */ 313 if (bus_dma_tag_create(/*parent*/ NULL, 314 /*alignment*/ PAGE_SIZE, /*no boundary*/ 0, 315 /*lowaddr*/ 0x9ffff, /*highaddr*/ BUS_SPACE_MAXADDR, NULL, NULL, 316 /*maxsize*/ PAGE_SIZE, /*segments*/ 1, /*maxsegsize*/ PAGE_SIZE, 317 0, busdma_lock_mutex, &Giant, &acpi_waketag) != 0) { 318 printf("acpi_alloc_wakeup_handler: can't create wake tag\n"); 319 return; 320 } 321 if (bus_dmamem_alloc(acpi_waketag, &wakeaddr, BUS_DMA_NOWAIT, 322 &acpi_wakemap) != 0) { 323 printf("acpi_alloc_wakeup_handler: can't alloc wake memory\n"); 324 return; 325 } 326 acpi_wakeaddr = (vm_offset_t)wakeaddr; 327 } 328 329 SYSINIT(acpiwakeup, SI_SUB_KMEM, SI_ORDER_ANY, acpi_alloc_wakeup_handler, 0); 330 331 static void 332 acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 333 { 334 struct acpi_softc *sc; 335 uint32_t *addr; 336 337 /* Overwrite the ljmp target with the real address */ 338 sc = arg; 339 sc->acpi_wakephys = segs[0].ds_addr; 340 addr = (uint32_t *)&wakecode[wakeup_sw32 + 2]; 341 *addr = sc->acpi_wakephys + wakeup_32; 342 343 /* Copy the wake code into our low page and save its physical addr. */ 344 bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode)); 345 if (bootverbose) { 346 device_printf(sc->acpi_dev, "wakeup code va %#x pa %#jx\n", 347 acpi_wakeaddr, (uintmax_t)sc->acpi_wakephys); 348 } 349 } 350 351 void 352 acpi_install_wakeup_handler(struct acpi_softc *sc) 353 { 354 if (acpi_wakeaddr == 0) 355 return; 356 357 sc->acpi_waketag = acpi_waketag; 358 sc->acpi_wakeaddr = acpi_wakeaddr; 359 sc->acpi_wakemap = acpi_wakemap; 360 361 bus_dmamap_load(sc->acpi_waketag, sc->acpi_wakemap, 362 (void *)sc->acpi_wakeaddr, PAGE_SIZE, acpi_realmodeinst, sc, 0); 363 } 364