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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
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/intr_machdep.h>
49 #include <machine/segments.h>
51 #include <contrib/dev/acpica/include/acpi.h>
53 #include <dev/acpica/acpivar.h>
55 #include "acpi_wakecode.h"
56 #include "acpi_wakedata.h"
58 /* Make sure the code is less than one page and leave room for the stack. */
59 CTASSERT(sizeof(wakecode) < PAGE_SIZE - 1024);
62 #error this file needs sys/cdefs.h as a prerequisite
65 extern uint32_t acpi_resume_beep;
66 extern uint32_t acpi_reset_video;
67 extern void initializecpu(void);
69 static struct region_descriptor __used saved_idt, saved_gdt;
70 static struct region_descriptor *p_gdt;
71 static uint16_t __used saved_ldt;
73 static uint32_t __used r_eax, r_ebx, r_ecx, r_edx, r_ebp, r_esi, r_edi,
74 r_efl, r_cr0, r_cr2, r_cr3, r_cr4, ret_addr;
76 static uint16_t __used r_cs, r_ds, r_es, r_fs, r_gs, r_ss, r_tr;
77 static uint32_t __used r_esp;
79 static void acpi_printcpu(void);
80 static void acpi_realmodeinst(void *arg, bus_dma_segment_t *segs,
81 int nsegs, int error);
82 static void acpi_alloc_wakeup_handler(void);
85 extern int acpi_savecpu(void);
86 extern int acpi_restorecpu(void);
92 .type acpi_restorecpu, @function\n\
107 movl ret_addr,%eax \n\
114 .type acpi_savecpu, @function \n\
151 movl %eax,ret_addr \n\
155 #endif /* __GNUCLIKE_ASM */
160 printf("======== acpi_printcpu() debug dump ========\n");
161 printf("gdt[%04x:%08x] idt[%04x:%08x] ldt[%04x] tr[%04x] efl[%08x]\n",
162 saved_gdt.rd_limit, saved_gdt.rd_base,
163 saved_idt.rd_limit, saved_idt.rd_base,
164 saved_ldt, r_tr, r_efl);
165 printf("eax[%08x] ebx[%08x] ecx[%08x] edx[%08x]\n",
166 r_eax, r_ebx, r_ecx, r_edx);
167 printf("esi[%08x] edi[%08x] ebp[%08x] esp[%08x]\n",
168 r_esi, r_edi, r_ebp, r_esp);
169 printf("cr0[%08x] cr2[%08x] cr3[%08x] cr4[%08x]\n",
170 r_cr0, r_cr2, r_cr3, r_cr4);
171 printf("cs[%04x] ds[%04x] es[%04x] fs[%04x] gs[%04x] ss[%04x]\n",
172 r_cs, r_ds, r_es, r_fs, r_gs, r_ss);
175 #define WAKECODE_FIXUP(offset, type, val) do { \
177 addr = (type *)(sc->acpi_wakeaddr + offset); \
181 #define WAKECODE_BCOPY(offset, type, val) do { \
183 addr = (void *)(sc->acpi_wakeaddr + offset); \
184 bcopy(&(val), addr, sizeof(type)); \
187 /* Turn off bits 1&2 of the PIT, stopping the beep. */
189 acpi_stop_beep(void *arg)
191 outb(0x61, inb(0x61) & ~0x3);
195 acpi_sleep_machdep(struct acpi_softc *sc, int state)
204 if (sc->acpi_wakeaddr == 0)
207 AcpiSetFirmwareWakingVector(sc->acpi_wakephys);
212 * Temporarily switch to the kernel pmap because it provides an
213 * identity mapping (setup at boot) for the low physical memory
214 * region containing the wakeup code.
219 load_cr3(vtophys(pm->pm_pdpt));
221 load_cr3(vtophys(pm->pm_pdir));
226 if (acpi_savecpu()) {
230 p_gdt = (struct region_descriptor *)
231 (sc->acpi_wakeaddr + physical_gdt);
232 p_gdt->rd_limit = saved_gdt.rd_limit;
233 p_gdt->rd_base = vtophys(saved_gdt.rd_base);
235 WAKECODE_FIXUP(physical_esp, uint32_t, vtophys(r_esp));
236 WAKECODE_FIXUP(previous_cr0, uint32_t, r_cr0);
237 WAKECODE_FIXUP(previous_cr2, uint32_t, r_cr2);
238 WAKECODE_FIXUP(previous_cr3, uint32_t, r_cr3);
239 WAKECODE_FIXUP(previous_cr4, uint32_t, r_cr4);
241 WAKECODE_FIXUP(resume_beep, uint32_t, acpi_resume_beep);
242 WAKECODE_FIXUP(reset_video, uint32_t, acpi_reset_video);
244 WAKECODE_FIXUP(previous_tr, uint16_t, r_tr);
245 WAKECODE_BCOPY(previous_gdt, struct region_descriptor, saved_gdt);
246 WAKECODE_FIXUP(previous_ldt, uint16_t, saved_ldt);
247 WAKECODE_BCOPY(previous_idt, struct region_descriptor, saved_idt);
249 WAKECODE_FIXUP(where_to_recover, void *, acpi_restorecpu);
251 WAKECODE_FIXUP(previous_ds, uint16_t, r_ds);
252 WAKECODE_FIXUP(previous_es, uint16_t, r_es);
253 WAKECODE_FIXUP(previous_fs, uint16_t, r_fs);
254 WAKECODE_FIXUP(previous_gs, uint16_t, r_gs);
255 WAKECODE_FIXUP(previous_ss, uint16_t, r_ss);
260 /* Call ACPICA to enter the desired sleep state */
261 if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
262 status = AcpiEnterSleepStateS4bios();
264 status = AcpiEnterSleepState(state);
266 if (status != AE_OK) {
267 device_printf(sc->acpi_dev,
268 "AcpiEnterSleepState failed - %s\n",
269 AcpiFormatException(status));
290 /* If we beeped, turn it off after a delay. */
291 if (acpi_resume_beep)
292 timeout(acpi_stop_beep, NULL, 3 * hz);
297 static bus_dma_tag_t acpi_waketag;
298 static bus_dmamap_t acpi_wakemap;
299 static vm_offset_t acpi_wakeaddr;
302 acpi_alloc_wakeup_handler(void)
310 * Specify the region for our wakeup code. We want it in the low 1 MB
311 * region, excluding video memory and above (0xa0000). We ask for
312 * it to be page-aligned, just to be safe.
314 if (bus_dma_tag_create(/*parent*/ NULL,
315 /*alignment*/ PAGE_SIZE, /*no boundary*/ 0,
316 /*lowaddr*/ 0x9ffff, /*highaddr*/ BUS_SPACE_MAXADDR, NULL, NULL,
317 /*maxsize*/ PAGE_SIZE, /*segments*/ 1, /*maxsegsize*/ PAGE_SIZE,
318 0, busdma_lock_mutex, &Giant, &acpi_waketag) != 0) {
319 printf("acpi_alloc_wakeup_handler: can't create wake tag\n");
322 if (bus_dmamem_alloc(acpi_waketag, &wakeaddr, BUS_DMA_NOWAIT,
323 &acpi_wakemap) != 0) {
324 printf("acpi_alloc_wakeup_handler: can't alloc wake memory\n");
327 acpi_wakeaddr = (vm_offset_t)wakeaddr;
330 SYSINIT(acpiwakeup, SI_SUB_KMEM, SI_ORDER_ANY, acpi_alloc_wakeup_handler, 0);
333 acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
335 struct acpi_softc *sc;
338 /* Overwrite the ljmp target with the real address */
340 sc->acpi_wakephys = segs[0].ds_addr;
341 addr = (uint32_t *)&wakecode[wakeup_sw32 + 2];
342 *addr = sc->acpi_wakephys + wakeup_32;
344 /* Copy the wake code into our low page and save its physical addr. */
345 bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode));
347 device_printf(sc->acpi_dev, "wakeup code va %#x pa %#jx\n",
348 acpi_wakeaddr, (uintmax_t)sc->acpi_wakephys);
353 acpi_install_wakeup_handler(struct acpi_softc *sc)
355 if (acpi_wakeaddr == 0)
358 sc->acpi_waketag = acpi_waketag;
359 sc->acpi_wakeaddr = acpi_wakeaddr;
360 sc->acpi_wakemap = acpi_wakemap;
362 bus_dmamap_load(sc->acpi_waketag, sc->acpi_wakemap,
363 (void *)sc->acpi_wakeaddr, PAGE_SIZE, acpi_realmodeinst, sc, 0);