xref: /netbsd-src/sys/arch/x86/acpi/acpi_wakeup.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: acpi_wakeup.c,v 1.38 2014/02/25 18:30:08 pooka Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002, 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Takuya SHIOZAKI.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: acpi_wakeup.c,v 1.38 2014/02/25 18:30:08 pooka Exp $");
34 
35 /*-
36  * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org>
37  * Copyright (c) 2001 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
38  * All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *      FreeBSD: src/sys/i386/acpica/acpi_wakeup.c,v 1.9 2002/01/10 03:26:46 wes Exp
62  */
63 
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: acpi_wakeup.c,v 1.38 2014/02/25 18:30:08 pooka Exp $");
66 
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/bus.h>
71 #include <sys/cpu.h>
72 #include <sys/kcpuset.h>
73 #include <sys/sysctl.h>
74 
75 #include <uvm/uvm_extern.h>
76 #include <uvm/uvm_page.h>
77 
78 #ifdef __i386__
79 #include "opt_mtrr.h"
80 #endif
81 #include "ioapic.h"
82 #include "lapic.h"
83 
84 #if NLAPIC > 0
85 #include <machine/i82489var.h>
86 #endif
87 #if NIOAPIC > 0
88 #include <machine/i82093var.h>
89 #endif
90 #include <machine/i8259.h>
91 
92 #include "acpica.h"
93 
94 #include <dev/ic/i8253reg.h>
95 #include <dev/acpi/acpica.h>
96 #include <dev/acpi/acpivar.h>
97 #define ACPI_MACHDEP_PRIVATE
98 #include <machine/acpi_machdep.h>
99 #include <machine/cpu.h>
100 #include <machine/mtrr.h>
101 
102 #include <x86/cpuvar.h>
103 #include <x86/x86/tsc.h>
104 #include <x86/fpu.h>
105 
106 #include "opt_vga.h"
107 
108 #include "acpi_wakecode.h"
109 
110 /* Address is also hard-coded in acpi_wakecode.S */
111 static paddr_t acpi_wakeup_paddr = 3 * PAGE_SIZE;
112 static vaddr_t acpi_wakeup_vaddr;
113 
114 int acpi_md_vbios_reset = 1; /* Referenced by dev/pci/vga_pci.c */
115 int acpi_md_vesa_modenum = 0; /* Referenced by arch/x86/x86/genfb_machdep.c */
116 static int acpi_md_beep_on_reset = 0;
117 
118 static int	acpi_md_s4bios(void);
119 static int	sysctl_md_acpi_vbios_reset(SYSCTLFN_ARGS);
120 static int	sysctl_md_acpi_beep_on_reset(SYSCTLFN_ARGS);
121 
122 /* Implemented in acpi_wakeup_low.S. */
123 int	acpi_md_sleep_prepare(int);
124 int	acpi_md_sleep_exit(int);
125 
126 /* Referenced by acpi_wakeup_low.S. */
127 void	acpi_md_sleep_enter(int);
128 
129 #ifdef MULTIPROCESSOR
130 /* Referenced in ipifuncs.c. */
131 void	acpi_cpu_sleep(struct cpu_info *);
132 #endif
133 
134 static void
135 acpi_md_sleep_patch(struct cpu_info *ci)
136 {
137 #define WAKECODE_FIXUP(offset, type, val) do	{		\
138 	type	*addr;						\
139 	addr = (type *)(acpi_wakeup_vaddr + offset);		\
140 	*addr = val;						\
141 } while (0)
142 
143 #define WAKECODE_BCOPY(offset, type, val) do	{		\
144 	void	**addr;						\
145 	addr = (void **)(acpi_wakeup_vaddr + offset);		\
146 	memcpy(addr, &(val), sizeof(type));			\
147 } while (0)
148 
149 	paddr_t				tmp_pdir;
150 
151 	tmp_pdir = pmap_init_tmp_pgtbl(acpi_wakeup_paddr);
152 
153 	/* Execute Sleep */
154 	memcpy((void *)acpi_wakeup_vaddr, wakecode, sizeof(wakecode));
155 
156 	if (CPU_IS_PRIMARY(ci)) {
157 		WAKECODE_FIXUP(WAKEUP_vesa_modenum, uint16_t, acpi_md_vesa_modenum);
158 		WAKECODE_FIXUP(WAKEUP_vbios_reset, uint8_t, acpi_md_vbios_reset);
159 		WAKECODE_FIXUP(WAKEUP_beep_on_reset, uint8_t, acpi_md_beep_on_reset);
160 	} else {
161 		WAKECODE_FIXUP(WAKEUP_vesa_modenum, uint16_t, 0);
162 		WAKECODE_FIXUP(WAKEUP_vbios_reset, uint8_t, 0);
163 		WAKECODE_FIXUP(WAKEUP_beep_on_reset, uint8_t, 0);
164 	}
165 
166 #ifdef __i386__
167 	WAKECODE_FIXUP(WAKEUP_r_cr4, uint32_t, ci->ci_suspend_cr4);
168 #else
169 	WAKECODE_FIXUP(WAKEUP_efer, uint32_t, ci->ci_suspend_efer);
170 #endif
171 
172 	WAKECODE_FIXUP(WAKEUP_curcpu, void *, ci);
173 #ifdef __i386__
174 	WAKECODE_FIXUP(WAKEUP_r_cr3, uint32_t, tmp_pdir);
175 #else
176 	WAKECODE_FIXUP(WAKEUP_r_cr3, uint64_t, tmp_pdir);
177 #endif
178 	WAKECODE_FIXUP(WAKEUP_restorecpu, void *, acpi_md_sleep_exit);
179 #undef WAKECODE_FIXUP
180 #undef WAKECODE_BCOPY
181 }
182 
183 static int
184 acpi_md_s4bios(void)
185 {
186 	ACPI_TABLE_FACS *facs;
187 	ACPI_STATUS rv;
188 
189 	rv = AcpiGetTable(ACPI_SIG_FACS, 0, (ACPI_TABLE_HEADER **)&facs);
190 
191 	if (ACPI_FAILURE(rv) || facs == NULL)
192 		return 0;
193 
194 	if ((facs->Flags & ACPI_FACS_S4_BIOS_PRESENT) == 0)
195 		return 0;
196 
197 	return 1;
198 }
199 
200 void
201 acpi_md_sleep_enter(int state)
202 {
203 	static int s4bios = -1;
204 	struct cpu_info *ci;
205 	ACPI_STATUS rv;
206 
207 	ci = curcpu();
208 
209 #ifdef MULTIPROCESSOR
210 	if (!CPU_IS_PRIMARY(ci)) {
211 		atomic_and_32(&ci->ci_flags, ~CPUF_RUNNING);
212 		kcpuset_atomic_clear(kcpuset_running, cpu_index(ci));
213 
214 		ACPI_FLUSH_CPU_CACHE();
215 
216 		for (;;)
217 			x86_hlt();
218 	}
219 #endif
220 
221 	acpi_md_sleep_patch(ci);
222 
223 	ACPI_FLUSH_CPU_CACHE();
224 
225 	switch (state) {
226 
227 	case ACPI_STATE_S4:
228 
229 		if (s4bios < 0)
230 			s4bios = acpi_md_s4bios();
231 
232 		if (s4bios == 0) {
233 			aprint_error("acpi0: S4 not supported\n");
234 			return;
235 		}
236 
237 		rv = AcpiEnterSleepStateS4bios();
238 		break;
239 
240 	default:
241 		rv = AcpiEnterSleepState(state);
242 		break;
243 	}
244 
245 	if (ACPI_FAILURE(rv)) {
246 		aprint_error("acpi0: failed to enter S%d\n", state);
247 		return;
248 	}
249 
250 	for (;;)
251 		x86_hlt();
252 }
253 
254 #ifdef MULTIPROCESSOR
255 void
256 acpi_cpu_sleep(struct cpu_info *ci)
257 {
258 	KASSERT(!CPU_IS_PRIMARY(ci));
259 	KASSERT(ci == curcpu());
260 
261 	x86_disable_intr();
262 
263 	if (acpi_md_sleep_prepare(-1))
264 		return;
265 
266 	/* Execute Wakeup */
267 #ifndef __i386__
268 	cpu_init_msrs(ci, false);
269 #endif
270 	fpuinit(ci);
271 #if NLAPIC > 0
272 	lapic_enable();
273 	lapic_set_lvt();
274 	lapic_initclocks();
275 #endif
276 
277 	atomic_or_32(&ci->ci_flags, CPUF_RUNNING);
278 	kcpuset_atomic_set(kcpuset_running, cpu_index(ci));
279 	tsc_sync_ap(ci);
280 
281 	x86_enable_intr();
282 }
283 #endif
284 
285 int
286 acpi_md_sleep(int state)
287 {
288 	int s, ret = 0;
289 #ifdef MULTIPROCESSOR
290 	struct cpu_info *ci;
291 	CPU_INFO_ITERATOR cii;
292 	cpuid_t cid;
293 #endif
294 
295 	KASSERT(acpi_wakeup_paddr != 0);
296 	KASSERT(sizeof(wakecode) <= PAGE_SIZE);
297 
298 	if (!CPU_IS_PRIMARY(curcpu())) {
299 		printf("acpi0: WARNING: ignoring sleep from secondary CPU\n");
300 		return -1;
301 	}
302 
303 	AcpiSetFirmwareWakingVector(acpi_wakeup_paddr);
304 
305 	s = splhigh();
306 	fpusave_cpu(true);
307 	x86_disable_intr();
308 
309 #ifdef MULTIPROCESSOR
310 	/* Save and suspend Application Processors. */
311 	x86_broadcast_ipi(X86_IPI_ACPI_CPU_SLEEP);
312 	cid = cpu_index(curcpu());
313 	while (kcpuset_isotherset(kcpuset_running, cid)) {
314 		delay(1);
315 	}
316 #endif
317 
318 	if (acpi_md_sleep_prepare(state))
319 		goto out;
320 
321 	/* Execute Wakeup */
322 #ifndef __i386__
323 	cpu_init_msrs(&cpu_info_primary, false);
324 #endif
325 	fpuinit(&cpu_info_primary);
326 	i8259_reinit();
327 #if NLAPIC > 0
328 	lapic_enable();
329 	lapic_set_lvt();
330 	lapic_initclocks();
331 #endif
332 #if NIOAPIC > 0
333 	ioapic_reenable();
334 #endif
335 
336 	initrtclock(TIMER_FREQ);
337 	inittodr(time_second);
338 
339 	/*
340 	 * The BIOS should always re-enable the SCI upon
341 	 * resume from the S3 state. The following is a
342 	 * workaround for systems that fail to do this.
343 	 */
344 	(void)AcpiWriteBitRegister(ACPI_BITREG_SCI_ENABLE, 1);
345 
346 	/*
347 	 * Clear fixed events (see e.g. ACPI 3.0, p. 62).
348 	 * Also prevent GPEs from misfiring by disabling
349 	 * all GPEs before interrupts are enabled. The
350 	 * AcpiLeaveSleepState() function will enable
351 	 * and handle the general purpose events later.
352 	 */
353 	(void)AcpiClearEvent(ACPI_EVENT_PMTIMER);
354 	(void)AcpiClearEvent(ACPI_EVENT_GLOBAL);
355 	(void)AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
356 	(void)AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
357 	(void)AcpiClearEvent(ACPI_EVENT_RTC);
358 	(void)AcpiHwDisableAllGpes();
359 
360 	acpi_pci_link_resume();
361 
362 out:
363 
364 #ifdef MULTIPROCESSOR
365 	for (CPU_INFO_FOREACH(cii, ci)) {
366 		if (CPU_IS_PRIMARY(ci))
367 			continue;
368 		acpi_md_sleep_patch(ci);
369 
370 		CPU_STARTUP(ci, acpi_wakeup_paddr);
371 		CPU_START_CLEANUP(ci);
372 
373 		while ((ci->ci_flags & CPUF_RUNNING) == 0)
374 			x86_pause();
375 
376 		tsc_sync_bp(ci);
377 	}
378 #endif
379 
380 	x86_enable_intr();
381 	splx(s);
382 
383 #ifdef MTRR
384 	if (mtrr_funcs != NULL)
385 		mtrr_commit();
386 #endif
387 
388 	return (ret);
389 }
390 
391 void
392 acpi_md_sleep_init(void)
393 {
394 	/* Map ACPI wakecode */
395 	acpi_wakeup_vaddr = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
396 	    UVM_KMF_VAONLY);
397 	if (acpi_wakeup_vaddr == 0)
398 		panic("acpi: can't allocate address for wakecode.\n");
399 
400 	pmap_kenter_pa(acpi_wakeup_vaddr, acpi_wakeup_paddr,
401 	    VM_PROT_READ | VM_PROT_WRITE, 0);
402 	pmap_update(pmap_kernel());
403 }
404 
405 SYSCTL_SETUP(sysctl_md_acpi_setup, "ACPI x86 sysctl setup")
406 {
407 	const struct sysctlnode *rnode;
408 	int err;
409 
410 	err = sysctl_createv(clog, 0, NULL, &rnode,
411 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "acpi", NULL,
412 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
413 
414 	if (err != 0)
415 		return;
416 
417 	err = sysctl_createv(clog, 0, &rnode, &rnode,
418 	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
419 	    "sleep", SYSCTL_DESCR("ACPI sleep"),
420 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
421 
422 	if (err != 0)
423 		return;
424 
425 	(void)sysctl_createv(NULL, 0, &rnode, NULL,
426 	    CTLFLAG_READWRITE, CTLTYPE_BOOL, "beep",
427 	    NULL, sysctl_md_acpi_beep_on_reset,
428 	    0, NULL, 0, CTL_CREATE, CTL_EOL);
429 
430 	(void)sysctl_createv(NULL, 0, &rnode, NULL,
431 	    CTLFLAG_READWRITE, CTLTYPE_INT, "vbios",
432 	    NULL, sysctl_md_acpi_vbios_reset,
433 	    0, NULL, 0, CTL_CREATE, CTL_EOL);
434 }
435 
436 static int
437 sysctl_md_acpi_vbios_reset(SYSCTLFN_ARGS)
438 {
439 	int error, t;
440 	struct sysctlnode node;
441 
442 	node = *rnode;
443 	t = acpi_md_vbios_reset;
444 	node.sysctl_data = &t;
445 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
446 	if (error || newp == NULL)
447 		return error;
448 
449 	if (t < 0 || t > 2)
450 		return EINVAL;
451 
452 #ifndef VGA_POST
453 	if (t == 2) {
454 		aprint_error("WARNING: hw.acpi.sleep.vbios=2 "
455 		    "unsupported (no option VGA_POST in kernel config)\n");
456 		return EINVAL;
457 	}
458 #endif
459 
460 	acpi_md_vbios_reset = t;
461 
462 	return 0;
463 }
464 
465 static int
466 sysctl_md_acpi_beep_on_reset(SYSCTLFN_ARGS)
467 {
468 	int error, t;
469 	struct sysctlnode node;
470 
471 	node = *rnode;
472 	t = acpi_md_beep_on_reset;
473 	node.sysctl_data = &t;
474 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
475 	if (error || newp == NULL)
476 		return error;
477 
478 	if (t < 0 || t > 1)
479 		return EINVAL;
480 
481 	acpi_md_beep_on_reset = t;
482 
483 	return 0;
484 }
485