2 * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org>
3 * Copyright (c) 2001 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * $FreeBSD: src/sys/i386/acpica/acpi_wakeup.c,v 1.33 2004/05/06 02:18:58 njl Exp $
28 * $DragonFly: src/sys/i386/acpica5/Attic/acpi_wakeup.c,v 1.3 2004/07/05 00:07:35 dillon Exp $
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
37 #include <sys/sysctl.h>
41 #include <vm/vm_object.h>
42 #include <vm/vm_page.h>
43 #include <vm/vm_map.h>
45 #include <machine/bus.h>
46 #include <machine/cpufunc.h>
47 #include <machine/segments.h>
50 #include <dev/acpica/acpivar.h>
52 #include "acpi_wakecode.h"
54 #if __FreeBSD_version < 500000
55 #define vm_page_lock_queues()
56 #define vm_page_unlock_queues()
59 extern uint32_t acpi_reset_video;
60 extern void initializecpu(void);
62 static struct region_descriptor r_idt, r_gdt, *p_gdt;
63 static uint16_t r_ldt;
65 static uint32_t r_eax, r_ebx, r_ecx, r_edx, r_ebp, r_esi, r_edi,
66 r_efl, r_cr0, r_cr2, r_cr3, r_cr4, ret_addr;
68 static uint16_t r_cs, r_ds, r_es, r_fs, r_gs, r_ss, r_tr;
69 static uint32_t r_esp = 0;
71 static void acpi_printcpu(void);
72 static void acpi_realmodeinst(void *arg, bus_dma_segment_t *segs,
73 int nsegs, int error);
74 static void acpi_alloc_wakeup_handler(void);
77 extern int acpi_savecpu(void);
78 extern int acpi_restorecpu(void);
80 #if defined(__GNUC__) || defined(__INTEL_COMPILER)
84 .type acpi_restorecpu, @function\n\
99 movl ret_addr,%eax \n\
106 .type acpi_savecpu, @function \n\
143 movl %eax,ret_addr \n\
147 #endif /* __GNUC__ || __INTEL_COMPILER */
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,
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);
166 #define WAKECODE_FIXUP(offset, type, val) do { \
168 addr = (void **)(sc->acpi_wakeaddr + offset); \
169 (type *)*addr = val; \
172 #define WAKECODE_BCOPY(offset, type, val) do { \
174 addr = (void **)(sc->acpi_wakeaddr + offset); \
175 bcopy(&(val), addr, sizeof(type)); \
179 acpi_sleep_machdep(struct acpi_softc *sc, int state)
185 static vm_page_t opage = NULL;
191 if (sc->acpi_wakeaddr == 0)
194 AcpiSetFirmwareWakingVector(sc->acpi_wakephys);
199 /* Create Identity Mapping */
200 if ((p = curproc) == NULL)
202 pm = vmspace_pmap(p->p_vmspace);
205 load_cr3(vtophys(pm->pm_pdpt));
207 load_cr3(vtophys(pm->pm_pdir));
210 oldphys = pmap_extract(pm, sc->acpi_wakephys);
212 opage = PHYS_TO_VM_PAGE(oldphys);
213 page = PHYS_TO_VM_PAGE(sc->acpi_wakephys);
214 pmap_enter(pm, sc->acpi_wakephys, page,
215 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE, 1);
218 if (acpi_savecpu()) {
222 p_gdt = (struct region_descriptor *)
223 (sc->acpi_wakeaddr + physical_gdt);
224 p_gdt->rd_limit = r_gdt.rd_limit;
225 p_gdt->rd_base = vtophys(r_gdt.rd_base);
227 WAKECODE_FIXUP(physical_esp, uint32_t, vtophys(r_esp));
228 WAKECODE_FIXUP(previous_cr0, uint32_t, r_cr0);
229 WAKECODE_FIXUP(previous_cr2, uint32_t, r_cr2);
230 WAKECODE_FIXUP(previous_cr3, uint32_t, r_cr3);
231 WAKECODE_FIXUP(previous_cr4, uint32_t, r_cr4);
233 WAKECODE_FIXUP(reset_video, uint32_t, acpi_reset_video);
235 WAKECODE_FIXUP(previous_tr, uint16_t, r_tr);
236 WAKECODE_BCOPY(previous_gdt, struct region_descriptor, r_gdt);
237 WAKECODE_FIXUP(previous_ldt, uint16_t, r_ldt);
238 WAKECODE_BCOPY(previous_idt, struct region_descriptor, r_idt);
240 WAKECODE_FIXUP(where_to_recover, void, acpi_restorecpu);
242 WAKECODE_FIXUP(previous_ds, uint16_t, r_ds);
243 WAKECODE_FIXUP(previous_es, uint16_t, r_es);
244 WAKECODE_FIXUP(previous_fs, uint16_t, r_fs);
245 WAKECODE_FIXUP(previous_gs, uint16_t, r_gs);
246 WAKECODE_FIXUP(previous_ss, uint16_t, r_ss);
251 /* Call ACPICA to enter the desired sleep state */
252 if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
253 status = AcpiEnterSleepStateS4bios();
255 status = AcpiEnterSleepState(state);
257 if (status != AE_OK) {
258 device_printf(sc->acpi_dev,
259 "AcpiEnterSleepState failed - %s\n",
260 AcpiFormatException(status));
280 vm_page_lock_queues();
281 pmap_remove(pm, sc->acpi_wakephys, sc->acpi_wakephys + PAGE_SIZE);
282 vm_page_unlock_queues();
284 pmap_enter(pm, sc->acpi_wakephys, page,
285 VM_PROT_READ | VM_PROT_WRITE, 0);
295 static bus_dma_tag_t acpi_waketag;
296 static bus_dmamap_t acpi_wakemap;
297 static vm_offset_t acpi_wakeaddr = 0;
300 acpi_alloc_wakeup_handler(void)
305 if (bus_dma_tag_create(/* parent */ NULL, /* alignment */ 2, 0,
306 /* lowaddr below 1MB */ 0x9ffff,
307 /* highaddr */ BUS_SPACE_MAXADDR, NULL, NULL,
308 PAGE_SIZE, 1, PAGE_SIZE, 0, &acpi_waketag) != 0) {
309 printf("acpi_alloc_wakeup_handler: can't create wake tag\n");
313 if (bus_dmamem_alloc(acpi_waketag, (void **)&acpi_wakeaddr,
314 BUS_DMA_NOWAIT, &acpi_wakemap)) {
315 printf("acpi_alloc_wakeup_handler: can't alloc wake memory\n");
320 SYSINIT(acpiwakeup, SI_SUB_KMEM, SI_ORDER_ANY, acpi_alloc_wakeup_handler, 0)
323 acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
325 struct acpi_softc *sc = arg;
328 addr = (uint32_t *)&wakecode[wakeup_sw32 + 2];
329 *addr = segs[0].ds_addr + wakeup_32;
330 bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode));
331 sc->acpi_wakephys = segs[0].ds_addr;
335 acpi_install_wakeup_handler(struct acpi_softc *sc)
337 if (acpi_wakeaddr == 0)
340 sc->acpi_waketag = acpi_waketag;
341 sc->acpi_wakeaddr = acpi_wakeaddr;
342 sc->acpi_wakemap = acpi_wakemap;
344 bus_dmamap_load(sc->acpi_waketag, sc->acpi_wakemap,
345 (void *)sc->acpi_wakeaddr, PAGE_SIZE,
346 acpi_realmodeinst, sc, 0);