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