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