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$ 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/mutex.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_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/acpivar.h> 56 57 #include "acpi_wakecode.h" 58 59 extern void initializecpu(void); 60 61 struct region_descriptor r_idt, r_gdt, *p_gdt; 62 static u_int16_t r_ldt; 63 64 static u_int32_t r_eax, r_ebx, r_ecx, r_edx, r_ebp, r_esi, r_edi, 65 r_efl, r_cr0, r_cr2, r_cr3, r_cr4, ret_addr; 66 67 static u_int16_t r_cs, r_ds, r_es, r_fs, r_gs, r_ss, r_tr; 68 static u_int32_t r_esp = 0; 69 70 static int debug_wakeup = 1; 71 SYSCTL_INT(_debug, OID_AUTO, acpi_wakeup, CTLFLAG_RW, &debug_wakeup, 0, "") 72 73 static void acpi_printcpu(void); 74 static void acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, 75 int nsegs, int error); 76 static void acpi_alloc_wakeup_handler(void); 77 78 /* XXX shut gcc up */ 79 extern int acpi_savecpu(void); 80 extern int acpi_restorecpu(void); 81 82 __asm__(" 83 .text 84 .p2align 2, 0x90 85 .type acpi_restorecpu, @function 86 acpi_restorecpu: 87 .align 4 88 movl r_eax,%eax 89 movl r_ebx,%ebx 90 movl r_ecx,%ecx 91 movl r_edx,%edx 92 movl r_ebp,%ebp 93 movl r_esi,%esi 94 movl r_edi,%edi 95 movl r_esp,%esp 96 97 pushl r_efl 98 popfl 99 100 pushl ret_addr 101 xorl %eax,%eax 102 ret 103 104 .text 105 .p2align 2, 0x90 106 .type acpi_savecpu, @function 107 acpi_savecpu: 108 movw %cs,r_cs 109 movw %ds,r_ds 110 movw %es,r_es 111 movw %fs,r_fs 112 movw %gs,r_gs 113 movw %ss,r_ss 114 115 movl %eax,r_eax 116 movl %ebx,r_ebx 117 movl %ecx,r_ecx 118 movl %edx,r_edx 119 movl %ebp,r_ebp 120 movl %esi,r_esi 121 movl %edi,r_edi 122 123 movl %cr0,%eax 124 movl %eax,r_cr0 125 movl %cr2,%eax 126 movl %eax,r_cr2 127 movl %cr3,%eax 128 movl %eax,r_cr3 129 movl %cr4,%eax 130 movl %eax,r_cr4 131 132 pushfl 133 popl r_efl 134 135 movl %esp,r_esp 136 137 sgdt r_gdt 138 sidt r_idt 139 sldt r_ldt 140 str r_tr 141 142 movl (%esp),%eax 143 movl %eax,ret_addr 144 movl $1,%eax 145 ret 146 "); 147 148 static void 149 acpi_printcpu(void) 150 { 151 152 printf("======== acpi_printcpu() debug dump ========\n"); 153 printf("gdt[%04x:%08x] idt[%04x:%08x] ldt[%04x] tr[%04x] efl[%08x]\n", 154 r_gdt.rd_limit, r_gdt.rd_base, r_idt.rd_limit, r_idt.rd_base, 155 r_ldt, r_tr, r_efl); 156 printf("eax[%08x] ebx[%08x] ecx[%08x] edx[%08x]\n", 157 r_eax, r_ebx, r_ecx, r_edx); 158 printf("esi[%08x] edi[%08x] ebp[%08x] esp[%08x]\n", 159 r_esi, r_edi, r_ebp, r_esp); 160 printf("cr0[%08x] cr2[%08x] cr3[%08x] cr4[%08x]\n", 161 r_cr0, r_cr2, r_cr3, r_cr4); 162 printf("cs[%04x] ds[%04x] es[%04x] fs[%04x] gs[%04x] ss[%04x]\n", 163 r_cs, r_ds, r_es, r_fs, r_gs, r_ss); 164 } 165 166 int 167 acpi_sleep_machdep(struct acpi_softc *sc, int state) 168 { 169 ACPI_STATUS status; 170 void **addr; 171 vm_offset_t oldphys; 172 struct pmap *pm; 173 vm_page_t page; 174 static vm_page_t opage = NULL; 175 int ret = 0; 176 u_long ef; 177 178 if (sc->acpi_wakeaddr == NULL) { 179 return (0); 180 } 181 182 AcpiSetFirmwareWakingVector(sc->acpi_wakephys); 183 184 ef = read_eflags(); 185 disable_intr(); 186 187 /* Create Identity Mapping */ 188 pm = &CURPROC->p_vmspace->vm_pmap; 189 oldphys = pmap_extract(pm, sc->acpi_wakephys); 190 if (oldphys) { 191 opage = PHYS_TO_VM_PAGE(oldphys); 192 } 193 page = PHYS_TO_VM_PAGE(sc->acpi_wakephys); 194 pmap_enter(pm, sc->acpi_wakephys, page, 195 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE, 1); 196 197 ret_addr = 0; 198 if (acpi_savecpu()) { 199 /* Execute Sleep */ 200 p_gdt = (struct region_descriptor *)(sc->acpi_wakeaddr + physical_gdt); 201 p_gdt->rd_limit = r_gdt.rd_limit; 202 p_gdt->rd_base = vtophys(r_gdt.rd_base); 203 addr = (void **)(sc->acpi_wakeaddr + physical_esp); 204 (u_int32_t *)*addr = vtophys(r_esp); 205 addr = (void **)(sc->acpi_wakeaddr + previous_cr0); 206 (u_int32_t *)*addr = r_cr0; 207 addr = (void **)(sc->acpi_wakeaddr + previous_cr2); 208 (u_int32_t *)*addr = r_cr2; 209 addr = (void **)(sc->acpi_wakeaddr + previous_cr3); 210 (u_int32_t *)*addr = r_cr3; 211 addr = (void **)(sc->acpi_wakeaddr + previous_cr4); 212 (u_int32_t *)*addr = r_cr4; 213 214 addr = (void **)(sc->acpi_wakeaddr + previous_tr); 215 (u_int16_t *)*addr = r_tr; 216 addr = (void **)(sc->acpi_wakeaddr + previous_gdt); 217 bcopy(&r_gdt, addr, sizeof(r_gdt)); 218 addr = (void **)(sc->acpi_wakeaddr + previous_ldt); 219 (u_int16_t *)*addr = r_ldt; 220 addr = (void **)(sc->acpi_wakeaddr + previous_idt); 221 bcopy(&r_idt, addr, sizeof(r_idt)); 222 addr = (void **)(sc->acpi_wakeaddr + where_to_recover); 223 *addr = acpi_restorecpu; 224 addr = (void **)(sc->acpi_wakeaddr + previous_ds); 225 (u_int16_t *)*addr = r_ds; 226 addr = (void **)(sc->acpi_wakeaddr + previous_es); 227 (u_int16_t *)*addr = r_es; 228 addr = (void **)(sc->acpi_wakeaddr + previous_fs); 229 (u_int16_t *)*addr = r_fs; 230 addr = (void **)(sc->acpi_wakeaddr + previous_gs); 231 (u_int16_t *)*addr = r_gs; 232 addr = (void **)(sc->acpi_wakeaddr + previous_ss); 233 (u_int16_t *)*addr = r_ss; 234 235 if (debug_wakeup) { 236 acpi_printcpu(); 237 } 238 239 if ((status = AcpiEnterSleepState(state)) != AE_OK) { 240 device_printf(sc->acpi_dev, 241 "AcpiEnterSleepState failed - %s\n", 242 AcpiFormatException(status)); 243 ret = -1; 244 goto out; 245 } 246 wbinvd(); 247 248 for (;;) ; 249 } else { 250 /* Execute Wakeup */ 251 #if 0 252 initializecpu(); 253 #endif 254 icu_reinit(); 255 256 if (debug_wakeup) { 257 acpi_savecpu(); 258 acpi_printcpu(); 259 } 260 } 261 262 out: 263 pmap_remove(pm, sc->acpi_wakephys, sc->acpi_wakephys + PAGE_SIZE); 264 if (opage) { 265 pmap_enter(pm, sc->acpi_wakephys, page, 266 VM_PROT_READ | VM_PROT_WRITE, 0); 267 } 268 269 write_eflags(ef); 270 271 return (ret); 272 } 273 274 static bus_dma_tag_t acpi_waketag; 275 static bus_dmamap_t acpi_wakemap; 276 static vm_offset_t acpi_wakeaddr = 0; 277 278 static void 279 acpi_alloc_wakeup_handler(void) 280 { 281 282 if (bus_dma_tag_create(/* parent */ NULL, /* alignment */ 2, 0, 283 /* lowaddr below 1MB */ 0x9ffff, 284 /* highaddr */ BUS_SPACE_MAXADDR, NULL, NULL, 285 PAGE_SIZE, 1, PAGE_SIZE, 0, &acpi_waketag) != 0) { 286 printf("acpi_alloc_wakeup_handler: unable to create wake tag\n"); 287 return; 288 } 289 290 if (bus_dmamem_alloc(acpi_waketag, (void **)&acpi_wakeaddr, 291 BUS_DMA_NOWAIT, &acpi_wakemap)) { 292 printf("acpi_alloc_wakeup_handler: unable to allocate wake memory\n"); 293 return; 294 } 295 } 296 297 SYSINIT(acpiwakeup, SI_SUB_KMEM, SI_ORDER_ANY, acpi_alloc_wakeup_handler, 0) 298 299 static void 300 acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 301 { 302 struct acpi_softc *sc = arg; 303 u_int32_t *addr; 304 305 addr = (u_int32_t *)&wakecode[wakeup_sw32 + 2]; 306 *addr = segs[0].ds_addr + wakeup_32; 307 bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode)); 308 sc->acpi_wakephys = segs[0].ds_addr; 309 } 310 311 void 312 acpi_install_wakeup_handler(struct acpi_softc *sc) 313 { 314 315 if (acpi_wakeaddr == 0) { 316 return; 317 } 318 319 sc->acpi_waketag = acpi_waketag; 320 sc->acpi_wakeaddr = acpi_wakeaddr; 321 sc->acpi_wakemap = acpi_wakemap; 322 323 bus_dmamap_load(sc->acpi_waketag, sc->acpi_wakemap, 324 (void *)sc->acpi_wakeaddr, PAGE_SIZE, 325 acpi_realmodeinst, sc, 0); 326 } 327 328