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