xref: /freebsd/sys/x86/x86/cpu_machdep.c (revision b57a73f8e758fddf66180287569506e099f55607)
1dfe7b3bfSKonstantin Belousov /*-
2dfe7b3bfSKonstantin Belousov  * Copyright (c) 2003 Peter Wemm.
3dfe7b3bfSKonstantin Belousov  * Copyright (c) 1992 Terrence R. Lambert.
4dfe7b3bfSKonstantin Belousov  * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
5dfe7b3bfSKonstantin Belousov  * All rights reserved.
6dfe7b3bfSKonstantin Belousov  *
7dfe7b3bfSKonstantin Belousov  * This code is derived from software contributed to Berkeley by
8dfe7b3bfSKonstantin Belousov  * William Jolitz.
9dfe7b3bfSKonstantin Belousov  *
10dfe7b3bfSKonstantin Belousov  * Redistribution and use in source and binary forms, with or without
11dfe7b3bfSKonstantin Belousov  * modification, are permitted provided that the following conditions
12dfe7b3bfSKonstantin Belousov  * are met:
13dfe7b3bfSKonstantin Belousov  * 1. Redistributions of source code must retain the above copyright
14dfe7b3bfSKonstantin Belousov  *    notice, this list of conditions and the following disclaimer.
15dfe7b3bfSKonstantin Belousov  * 2. Redistributions in binary form must reproduce the above copyright
16dfe7b3bfSKonstantin Belousov  *    notice, this list of conditions and the following disclaimer in the
17dfe7b3bfSKonstantin Belousov  *    documentation and/or other materials provided with the distribution.
18dfe7b3bfSKonstantin Belousov  * 3. All advertising materials mentioning features or use of this software
19dfe7b3bfSKonstantin Belousov  *    must display the following acknowledgement:
20dfe7b3bfSKonstantin Belousov  *	This product includes software developed by the University of
21dfe7b3bfSKonstantin Belousov  *	California, Berkeley and its contributors.
22dfe7b3bfSKonstantin Belousov  * 4. Neither the name of the University nor the names of its contributors
23dfe7b3bfSKonstantin Belousov  *    may be used to endorse or promote products derived from this software
24dfe7b3bfSKonstantin Belousov  *    without specific prior written permission.
25dfe7b3bfSKonstantin Belousov  *
26dfe7b3bfSKonstantin Belousov  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27dfe7b3bfSKonstantin Belousov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28dfe7b3bfSKonstantin Belousov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29dfe7b3bfSKonstantin Belousov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30dfe7b3bfSKonstantin Belousov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31dfe7b3bfSKonstantin Belousov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32dfe7b3bfSKonstantin Belousov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33dfe7b3bfSKonstantin Belousov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34dfe7b3bfSKonstantin Belousov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35dfe7b3bfSKonstantin Belousov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36dfe7b3bfSKonstantin Belousov  * SUCH DAMAGE.
37dfe7b3bfSKonstantin Belousov  *
38dfe7b3bfSKonstantin Belousov  *	from: @(#)machdep.c	7.4 (Berkeley) 6/3/91
39dfe7b3bfSKonstantin Belousov  */
40dfe7b3bfSKonstantin Belousov 
41dfe7b3bfSKonstantin Belousov #include <sys/cdefs.h>
42dfe7b3bfSKonstantin Belousov __FBSDID("$FreeBSD$");
43dfe7b3bfSKonstantin Belousov 
44dfe7b3bfSKonstantin Belousov #include "opt_atpic.h"
45dfe7b3bfSKonstantin Belousov #include "opt_compat.h"
46dfe7b3bfSKonstantin Belousov #include "opt_cpu.h"
47dfe7b3bfSKonstantin Belousov #include "opt_ddb.h"
48dfe7b3bfSKonstantin Belousov #include "opt_inet.h"
49dfe7b3bfSKonstantin Belousov #include "opt_isa.h"
50dfe7b3bfSKonstantin Belousov #include "opt_kstack_pages.h"
51dfe7b3bfSKonstantin Belousov #include "opt_maxmem.h"
52dfe7b3bfSKonstantin Belousov #include "opt_mp_watchdog.h"
53dfe7b3bfSKonstantin Belousov #include "opt_perfmon.h"
54dfe7b3bfSKonstantin Belousov #include "opt_platform.h"
55dfe7b3bfSKonstantin Belousov #ifdef __i386__
56dfe7b3bfSKonstantin Belousov #include "opt_npx.h"
57dfe7b3bfSKonstantin Belousov #include "opt_apic.h"
58dfe7b3bfSKonstantin Belousov #include "opt_xbox.h"
59dfe7b3bfSKonstantin Belousov #endif
60dfe7b3bfSKonstantin Belousov 
61dfe7b3bfSKonstantin Belousov #include <sys/param.h>
62dfe7b3bfSKonstantin Belousov #include <sys/proc.h>
63dfe7b3bfSKonstantin Belousov #include <sys/systm.h>
64dfe7b3bfSKonstantin Belousov #include <sys/bus.h>
65dfe7b3bfSKonstantin Belousov #include <sys/cpu.h>
66dfe7b3bfSKonstantin Belousov #include <sys/kdb.h>
67dfe7b3bfSKonstantin Belousov #include <sys/kernel.h>
68dfe7b3bfSKonstantin Belousov #include <sys/ktr.h>
69dfe7b3bfSKonstantin Belousov #include <sys/lock.h>
70dfe7b3bfSKonstantin Belousov #include <sys/malloc.h>
71dfe7b3bfSKonstantin Belousov #include <sys/mutex.h>
72dfe7b3bfSKonstantin Belousov #include <sys/pcpu.h>
73dfe7b3bfSKonstantin Belousov #include <sys/rwlock.h>
74dfe7b3bfSKonstantin Belousov #include <sys/sched.h>
75dfe7b3bfSKonstantin Belousov #ifdef SMP
76dfe7b3bfSKonstantin Belousov #include <sys/smp.h>
77dfe7b3bfSKonstantin Belousov #endif
78dfe7b3bfSKonstantin Belousov #include <sys/sysctl.h>
79dfe7b3bfSKonstantin Belousov 
80dfe7b3bfSKonstantin Belousov #include <machine/clock.h>
81dfe7b3bfSKonstantin Belousov #include <machine/cpu.h>
82dfe7b3bfSKonstantin Belousov #include <machine/cputypes.h>
83dfe7b3bfSKonstantin Belousov #include <machine/specialreg.h>
84dfe7b3bfSKonstantin Belousov #include <machine/md_var.h>
85dfe7b3bfSKonstantin Belousov #include <machine/mp_watchdog.h>
86dfe7b3bfSKonstantin Belousov #ifdef PERFMON
87dfe7b3bfSKonstantin Belousov #include <machine/perfmon.h>
88dfe7b3bfSKonstantin Belousov #endif
89dfe7b3bfSKonstantin Belousov #include <machine/tss.h>
90dfe7b3bfSKonstantin Belousov #ifdef SMP
91dfe7b3bfSKonstantin Belousov #include <machine/smp.h>
92dfe7b3bfSKonstantin Belousov #endif
93*b57a73f8SKonstantin Belousov #include <x86/acpica_machdep.h>
94dfe7b3bfSKonstantin Belousov 
95dfe7b3bfSKonstantin Belousov #include <vm/vm.h>
96dfe7b3bfSKonstantin Belousov #include <vm/vm_extern.h>
97dfe7b3bfSKonstantin Belousov #include <vm/vm_kern.h>
98dfe7b3bfSKonstantin Belousov #include <vm/vm_page.h>
99dfe7b3bfSKonstantin Belousov #include <vm/vm_map.h>
100dfe7b3bfSKonstantin Belousov #include <vm/vm_object.h>
101dfe7b3bfSKonstantin Belousov #include <vm/vm_pager.h>
102dfe7b3bfSKonstantin Belousov #include <vm/vm_param.h>
103dfe7b3bfSKonstantin Belousov 
104dfe7b3bfSKonstantin Belousov /*
105dfe7b3bfSKonstantin Belousov  * Machine dependent boot() routine
106dfe7b3bfSKonstantin Belousov  *
107dfe7b3bfSKonstantin Belousov  * I haven't seen anything to put here yet
108dfe7b3bfSKonstantin Belousov  * Possibly some stuff might be grafted back here from boot()
109dfe7b3bfSKonstantin Belousov  */
110dfe7b3bfSKonstantin Belousov void
111dfe7b3bfSKonstantin Belousov cpu_boot(int howto)
112dfe7b3bfSKonstantin Belousov {
113dfe7b3bfSKonstantin Belousov }
114dfe7b3bfSKonstantin Belousov 
115dfe7b3bfSKonstantin Belousov /*
116dfe7b3bfSKonstantin Belousov  * Flush the D-cache for non-DMA I/O so that the I-cache can
117dfe7b3bfSKonstantin Belousov  * be made coherent later.
118dfe7b3bfSKonstantin Belousov  */
119dfe7b3bfSKonstantin Belousov void
120dfe7b3bfSKonstantin Belousov cpu_flush_dcache(void *ptr, size_t len)
121dfe7b3bfSKonstantin Belousov {
122dfe7b3bfSKonstantin Belousov 	/* Not applicable */
123dfe7b3bfSKonstantin Belousov }
124dfe7b3bfSKonstantin Belousov 
125*b57a73f8SKonstantin Belousov void
126*b57a73f8SKonstantin Belousov acpi_cpu_c1(void)
127*b57a73f8SKonstantin Belousov {
128*b57a73f8SKonstantin Belousov 
129*b57a73f8SKonstantin Belousov 	__asm __volatile("sti; hlt");
130*b57a73f8SKonstantin Belousov }
131*b57a73f8SKonstantin Belousov 
132*b57a73f8SKonstantin Belousov void
133*b57a73f8SKonstantin Belousov acpi_cpu_idle_mwait(uint32_t mwait_hint)
134*b57a73f8SKonstantin Belousov {
135*b57a73f8SKonstantin Belousov 	int *state;
136*b57a73f8SKonstantin Belousov 
137*b57a73f8SKonstantin Belousov 	state = (int *)PCPU_PTR(monitorbuf);
138*b57a73f8SKonstantin Belousov 	/*
139*b57a73f8SKonstantin Belousov 	 * XXXKIB.  Software coordination mode should be supported,
140*b57a73f8SKonstantin Belousov 	 * but all Intel CPUs provide hardware coordination.
141*b57a73f8SKonstantin Belousov 	 */
142*b57a73f8SKonstantin Belousov 	cpu_monitor(state, 0, 0);
143*b57a73f8SKonstantin Belousov 	cpu_mwait(MWAIT_INTRBREAK, mwait_hint);
144*b57a73f8SKonstantin Belousov }
145*b57a73f8SKonstantin Belousov 
146dfe7b3bfSKonstantin Belousov /* Get current clock frequency for the given cpu id. */
147dfe7b3bfSKonstantin Belousov int
148dfe7b3bfSKonstantin Belousov cpu_est_clockrate(int cpu_id, uint64_t *rate)
149dfe7b3bfSKonstantin Belousov {
150dfe7b3bfSKonstantin Belousov 	uint64_t tsc1, tsc2;
151dfe7b3bfSKonstantin Belousov 	uint64_t acnt, mcnt, perf;
152dfe7b3bfSKonstantin Belousov 	register_t reg;
153dfe7b3bfSKonstantin Belousov 
154dfe7b3bfSKonstantin Belousov 	if (pcpu_find(cpu_id) == NULL || rate == NULL)
155dfe7b3bfSKonstantin Belousov 		return (EINVAL);
156dfe7b3bfSKonstantin Belousov #ifdef __i386__
157dfe7b3bfSKonstantin Belousov 	if ((cpu_feature & CPUID_TSC) == 0)
158dfe7b3bfSKonstantin Belousov 		return (EOPNOTSUPP);
159dfe7b3bfSKonstantin Belousov #endif
160dfe7b3bfSKonstantin Belousov 
161dfe7b3bfSKonstantin Belousov 	/*
162dfe7b3bfSKonstantin Belousov 	 * If TSC is P-state invariant and APERF/MPERF MSRs do not exist,
163dfe7b3bfSKonstantin Belousov 	 * DELAY(9) based logic fails.
164dfe7b3bfSKonstantin Belousov 	 */
165dfe7b3bfSKonstantin Belousov 	if (tsc_is_invariant && !tsc_perf_stat)
166dfe7b3bfSKonstantin Belousov 		return (EOPNOTSUPP);
167dfe7b3bfSKonstantin Belousov 
168dfe7b3bfSKonstantin Belousov #ifdef SMP
169dfe7b3bfSKonstantin Belousov 	if (smp_cpus > 1) {
170dfe7b3bfSKonstantin Belousov 		/* Schedule ourselves on the indicated cpu. */
171dfe7b3bfSKonstantin Belousov 		thread_lock(curthread);
172dfe7b3bfSKonstantin Belousov 		sched_bind(curthread, cpu_id);
173dfe7b3bfSKonstantin Belousov 		thread_unlock(curthread);
174dfe7b3bfSKonstantin Belousov 	}
175dfe7b3bfSKonstantin Belousov #endif
176dfe7b3bfSKonstantin Belousov 
177dfe7b3bfSKonstantin Belousov 	/* Calibrate by measuring a short delay. */
178dfe7b3bfSKonstantin Belousov 	reg = intr_disable();
179dfe7b3bfSKonstantin Belousov 	if (tsc_is_invariant) {
180dfe7b3bfSKonstantin Belousov 		wrmsr(MSR_MPERF, 0);
181dfe7b3bfSKonstantin Belousov 		wrmsr(MSR_APERF, 0);
182dfe7b3bfSKonstantin Belousov 		tsc1 = rdtsc();
183dfe7b3bfSKonstantin Belousov 		DELAY(1000);
184dfe7b3bfSKonstantin Belousov 		mcnt = rdmsr(MSR_MPERF);
185dfe7b3bfSKonstantin Belousov 		acnt = rdmsr(MSR_APERF);
186dfe7b3bfSKonstantin Belousov 		tsc2 = rdtsc();
187dfe7b3bfSKonstantin Belousov 		intr_restore(reg);
188dfe7b3bfSKonstantin Belousov 		perf = 1000 * acnt / mcnt;
189dfe7b3bfSKonstantin Belousov 		*rate = (tsc2 - tsc1) * perf;
190dfe7b3bfSKonstantin Belousov 	} else {
191dfe7b3bfSKonstantin Belousov 		tsc1 = rdtsc();
192dfe7b3bfSKonstantin Belousov 		DELAY(1000);
193dfe7b3bfSKonstantin Belousov 		tsc2 = rdtsc();
194dfe7b3bfSKonstantin Belousov 		intr_restore(reg);
195dfe7b3bfSKonstantin Belousov 		*rate = (tsc2 - tsc1) * 1000;
196dfe7b3bfSKonstantin Belousov 	}
197dfe7b3bfSKonstantin Belousov 
198dfe7b3bfSKonstantin Belousov #ifdef SMP
199dfe7b3bfSKonstantin Belousov 	if (smp_cpus > 1) {
200dfe7b3bfSKonstantin Belousov 		thread_lock(curthread);
201dfe7b3bfSKonstantin Belousov 		sched_unbind(curthread);
202dfe7b3bfSKonstantin Belousov 		thread_unlock(curthread);
203dfe7b3bfSKonstantin Belousov 	}
204dfe7b3bfSKonstantin Belousov #endif
205dfe7b3bfSKonstantin Belousov 
206dfe7b3bfSKonstantin Belousov 	return (0);
207dfe7b3bfSKonstantin Belousov }
208dfe7b3bfSKonstantin Belousov 
209dfe7b3bfSKonstantin Belousov /*
210dfe7b3bfSKonstantin Belousov  * Shutdown the CPU as much as possible
211dfe7b3bfSKonstantin Belousov  */
212dfe7b3bfSKonstantin Belousov void
213dfe7b3bfSKonstantin Belousov cpu_halt(void)
214dfe7b3bfSKonstantin Belousov {
215dfe7b3bfSKonstantin Belousov 	for (;;)
216dfe7b3bfSKonstantin Belousov 		halt();
217dfe7b3bfSKonstantin Belousov }
218dfe7b3bfSKonstantin Belousov 
219*b57a73f8SKonstantin Belousov bool
220*b57a73f8SKonstantin Belousov cpu_mwait_usable(void)
221*b57a73f8SKonstantin Belousov {
222*b57a73f8SKonstantin Belousov 
223*b57a73f8SKonstantin Belousov 	return ((cpu_feature2 & CPUID2_MON) != 0 && ((cpu_mon_mwait_flags &
224*b57a73f8SKonstantin Belousov 	    (CPUID5_MON_MWAIT_EXT | CPUID5_MWAIT_INTRBREAK)) ==
225*b57a73f8SKonstantin Belousov 	    (CPUID5_MON_MWAIT_EXT | CPUID5_MWAIT_INTRBREAK)));
226*b57a73f8SKonstantin Belousov }
227*b57a73f8SKonstantin Belousov 
228dfe7b3bfSKonstantin Belousov void (*cpu_idle_hook)(sbintime_t) = NULL;	/* ACPI idle hook. */
229dfe7b3bfSKonstantin Belousov static int	cpu_ident_amdc1e = 0;	/* AMD C1E supported. */
230dfe7b3bfSKonstantin Belousov static int	idle_mwait = 1;		/* Use MONITOR/MWAIT for short idle. */
231dfe7b3bfSKonstantin Belousov SYSCTL_INT(_machdep, OID_AUTO, idle_mwait, CTLFLAG_RWTUN, &idle_mwait,
232dfe7b3bfSKonstantin Belousov     0, "Use MONITOR/MWAIT for short idle");
233dfe7b3bfSKonstantin Belousov 
234dfe7b3bfSKonstantin Belousov #define	STATE_RUNNING	0x0
235dfe7b3bfSKonstantin Belousov #define	STATE_MWAIT	0x1
236dfe7b3bfSKonstantin Belousov #define	STATE_SLEEPING	0x2
237dfe7b3bfSKonstantin Belousov 
238dfe7b3bfSKonstantin Belousov #ifndef PC98
239dfe7b3bfSKonstantin Belousov static void
240dfe7b3bfSKonstantin Belousov cpu_idle_acpi(sbintime_t sbt)
241dfe7b3bfSKonstantin Belousov {
242dfe7b3bfSKonstantin Belousov 	int *state;
243dfe7b3bfSKonstantin Belousov 
244dfe7b3bfSKonstantin Belousov 	state = (int *)PCPU_PTR(monitorbuf);
245dfe7b3bfSKonstantin Belousov 	*state = STATE_SLEEPING;
246dfe7b3bfSKonstantin Belousov 
247dfe7b3bfSKonstantin Belousov 	/* See comments in cpu_idle_hlt(). */
248dfe7b3bfSKonstantin Belousov 	disable_intr();
249dfe7b3bfSKonstantin Belousov 	if (sched_runnable())
250dfe7b3bfSKonstantin Belousov 		enable_intr();
251dfe7b3bfSKonstantin Belousov 	else if (cpu_idle_hook)
252dfe7b3bfSKonstantin Belousov 		cpu_idle_hook(sbt);
253dfe7b3bfSKonstantin Belousov 	else
254*b57a73f8SKonstantin Belousov 		acpi_cpu_c1();
255dfe7b3bfSKonstantin Belousov 	*state = STATE_RUNNING;
256dfe7b3bfSKonstantin Belousov }
257dfe7b3bfSKonstantin Belousov #endif /* !PC98 */
258dfe7b3bfSKonstantin Belousov 
259dfe7b3bfSKonstantin Belousov static void
260dfe7b3bfSKonstantin Belousov cpu_idle_hlt(sbintime_t sbt)
261dfe7b3bfSKonstantin Belousov {
262dfe7b3bfSKonstantin Belousov 	int *state;
263dfe7b3bfSKonstantin Belousov 
264dfe7b3bfSKonstantin Belousov 	state = (int *)PCPU_PTR(monitorbuf);
265dfe7b3bfSKonstantin Belousov 	*state = STATE_SLEEPING;
266dfe7b3bfSKonstantin Belousov 
267dfe7b3bfSKonstantin Belousov 	/*
268dfe7b3bfSKonstantin Belousov 	 * Since we may be in a critical section from cpu_idle(), if
269dfe7b3bfSKonstantin Belousov 	 * an interrupt fires during that critical section we may have
270dfe7b3bfSKonstantin Belousov 	 * a pending preemption.  If the CPU halts, then that thread
271dfe7b3bfSKonstantin Belousov 	 * may not execute until a later interrupt awakens the CPU.
272dfe7b3bfSKonstantin Belousov 	 * To handle this race, check for a runnable thread after
273dfe7b3bfSKonstantin Belousov 	 * disabling interrupts and immediately return if one is
274dfe7b3bfSKonstantin Belousov 	 * found.  Also, we must absolutely guarentee that hlt is
275dfe7b3bfSKonstantin Belousov 	 * the next instruction after sti.  This ensures that any
276dfe7b3bfSKonstantin Belousov 	 * interrupt that fires after the call to disable_intr() will
277dfe7b3bfSKonstantin Belousov 	 * immediately awaken the CPU from hlt.  Finally, please note
278dfe7b3bfSKonstantin Belousov 	 * that on x86 this works fine because of interrupts enabled only
279dfe7b3bfSKonstantin Belousov 	 * after the instruction following sti takes place, while IF is set
280dfe7b3bfSKonstantin Belousov 	 * to 1 immediately, allowing hlt instruction to acknowledge the
281dfe7b3bfSKonstantin Belousov 	 * interrupt.
282dfe7b3bfSKonstantin Belousov 	 */
283dfe7b3bfSKonstantin Belousov 	disable_intr();
284dfe7b3bfSKonstantin Belousov 	if (sched_runnable())
285dfe7b3bfSKonstantin Belousov 		enable_intr();
286dfe7b3bfSKonstantin Belousov 	else
287*b57a73f8SKonstantin Belousov 		acpi_cpu_c1();
288dfe7b3bfSKonstantin Belousov 	*state = STATE_RUNNING;
289dfe7b3bfSKonstantin Belousov }
290dfe7b3bfSKonstantin Belousov 
291dfe7b3bfSKonstantin Belousov static void
292dfe7b3bfSKonstantin Belousov cpu_idle_mwait(sbintime_t sbt)
293dfe7b3bfSKonstantin Belousov {
294dfe7b3bfSKonstantin Belousov 	int *state;
295dfe7b3bfSKonstantin Belousov 
296dfe7b3bfSKonstantin Belousov 	state = (int *)PCPU_PTR(monitorbuf);
297dfe7b3bfSKonstantin Belousov 	*state = STATE_MWAIT;
298dfe7b3bfSKonstantin Belousov 
299dfe7b3bfSKonstantin Belousov 	/* See comments in cpu_idle_hlt(). */
300dfe7b3bfSKonstantin Belousov 	disable_intr();
301dfe7b3bfSKonstantin Belousov 	if (sched_runnable()) {
302dfe7b3bfSKonstantin Belousov 		enable_intr();
303dfe7b3bfSKonstantin Belousov 		*state = STATE_RUNNING;
304dfe7b3bfSKonstantin Belousov 		return;
305dfe7b3bfSKonstantin Belousov 	}
306dfe7b3bfSKonstantin Belousov 	cpu_monitor(state, 0, 0);
307dfe7b3bfSKonstantin Belousov 	if (*state == STATE_MWAIT)
308dfe7b3bfSKonstantin Belousov 		__asm __volatile("sti; mwait" : : "a" (MWAIT_C1), "c" (0));
309dfe7b3bfSKonstantin Belousov 	else
310dfe7b3bfSKonstantin Belousov 		enable_intr();
311dfe7b3bfSKonstantin Belousov 	*state = STATE_RUNNING;
312dfe7b3bfSKonstantin Belousov }
313dfe7b3bfSKonstantin Belousov 
314dfe7b3bfSKonstantin Belousov static void
315dfe7b3bfSKonstantin Belousov cpu_idle_spin(sbintime_t sbt)
316dfe7b3bfSKonstantin Belousov {
317dfe7b3bfSKonstantin Belousov 	int *state;
318dfe7b3bfSKonstantin Belousov 	int i;
319dfe7b3bfSKonstantin Belousov 
320dfe7b3bfSKonstantin Belousov 	state = (int *)PCPU_PTR(monitorbuf);
321dfe7b3bfSKonstantin Belousov 	*state = STATE_RUNNING;
322dfe7b3bfSKonstantin Belousov 
323dfe7b3bfSKonstantin Belousov 	/*
324dfe7b3bfSKonstantin Belousov 	 * The sched_runnable() call is racy but as long as there is
325dfe7b3bfSKonstantin Belousov 	 * a loop missing it one time will have just a little impact if any
326dfe7b3bfSKonstantin Belousov 	 * (and it is much better than missing the check at all).
327dfe7b3bfSKonstantin Belousov 	 */
328dfe7b3bfSKonstantin Belousov 	for (i = 0; i < 1000; i++) {
329dfe7b3bfSKonstantin Belousov 		if (sched_runnable())
330dfe7b3bfSKonstantin Belousov 			return;
331dfe7b3bfSKonstantin Belousov 		cpu_spinwait();
332dfe7b3bfSKonstantin Belousov 	}
333dfe7b3bfSKonstantin Belousov }
334dfe7b3bfSKonstantin Belousov 
335dfe7b3bfSKonstantin Belousov /*
336dfe7b3bfSKonstantin Belousov  * C1E renders the local APIC timer dead, so we disable it by
337dfe7b3bfSKonstantin Belousov  * reading the Interrupt Pending Message register and clearing
338dfe7b3bfSKonstantin Belousov  * both C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27).
339dfe7b3bfSKonstantin Belousov  *
340dfe7b3bfSKonstantin Belousov  * Reference:
341dfe7b3bfSKonstantin Belousov  *   "BIOS and Kernel Developer's Guide for AMD NPT Family 0Fh Processors"
342dfe7b3bfSKonstantin Belousov  *   #32559 revision 3.00+
343dfe7b3bfSKonstantin Belousov  */
344dfe7b3bfSKonstantin Belousov #define	MSR_AMDK8_IPM		0xc0010055
345dfe7b3bfSKonstantin Belousov #define	AMDK8_SMIONCMPHALT	(1ULL << 27)
346dfe7b3bfSKonstantin Belousov #define	AMDK8_C1EONCMPHALT	(1ULL << 28)
347dfe7b3bfSKonstantin Belousov #define	AMDK8_CMPHALT		(AMDK8_SMIONCMPHALT | AMDK8_C1EONCMPHALT)
348dfe7b3bfSKonstantin Belousov 
349dfe7b3bfSKonstantin Belousov void
350dfe7b3bfSKonstantin Belousov cpu_probe_amdc1e(void)
351dfe7b3bfSKonstantin Belousov {
352dfe7b3bfSKonstantin Belousov 
353dfe7b3bfSKonstantin Belousov 	/*
354dfe7b3bfSKonstantin Belousov 	 * Detect the presence of C1E capability mostly on latest
355dfe7b3bfSKonstantin Belousov 	 * dual-cores (or future) k8 family.
356dfe7b3bfSKonstantin Belousov 	 */
357dfe7b3bfSKonstantin Belousov 	if (cpu_vendor_id == CPU_VENDOR_AMD &&
358dfe7b3bfSKonstantin Belousov 	    (cpu_id & 0x00000f00) == 0x00000f00 &&
359dfe7b3bfSKonstantin Belousov 	    (cpu_id & 0x0fff0000) >=  0x00040000) {
360dfe7b3bfSKonstantin Belousov 		cpu_ident_amdc1e = 1;
361dfe7b3bfSKonstantin Belousov 	}
362dfe7b3bfSKonstantin Belousov }
363dfe7b3bfSKonstantin Belousov 
364ed95805eSJohn Baldwin #if defined(__i386__) && defined(PC98)
365dfe7b3bfSKonstantin Belousov void (*cpu_idle_fn)(sbintime_t) = cpu_idle_hlt;
366dfe7b3bfSKonstantin Belousov #else
367dfe7b3bfSKonstantin Belousov void (*cpu_idle_fn)(sbintime_t) = cpu_idle_acpi;
368dfe7b3bfSKonstantin Belousov #endif
369dfe7b3bfSKonstantin Belousov 
370dfe7b3bfSKonstantin Belousov void
371dfe7b3bfSKonstantin Belousov cpu_idle(int busy)
372dfe7b3bfSKonstantin Belousov {
373dfe7b3bfSKonstantin Belousov 	uint64_t msr;
374dfe7b3bfSKonstantin Belousov 	sbintime_t sbt = -1;
375dfe7b3bfSKonstantin Belousov 
376dfe7b3bfSKonstantin Belousov 	CTR2(KTR_SPARE2, "cpu_idle(%d) at %d",
377dfe7b3bfSKonstantin Belousov 	    busy, curcpu);
378ed95805eSJohn Baldwin #ifdef MP_WATCHDOG
379dfe7b3bfSKonstantin Belousov 	ap_watchdog(PCPU_GET(cpuid));
380dfe7b3bfSKonstantin Belousov #endif
381ed95805eSJohn Baldwin 
382dfe7b3bfSKonstantin Belousov 	/* If we are busy - try to use fast methods. */
383dfe7b3bfSKonstantin Belousov 	if (busy) {
384dfe7b3bfSKonstantin Belousov 		if ((cpu_feature2 & CPUID2_MON) && idle_mwait) {
385dfe7b3bfSKonstantin Belousov 			cpu_idle_mwait(busy);
386dfe7b3bfSKonstantin Belousov 			goto out;
387dfe7b3bfSKonstantin Belousov 		}
388dfe7b3bfSKonstantin Belousov 	}
389dfe7b3bfSKonstantin Belousov 
390dfe7b3bfSKonstantin Belousov 	/* If we have time - switch timers into idle mode. */
391dfe7b3bfSKonstantin Belousov 	if (!busy) {
392dfe7b3bfSKonstantin Belousov 		critical_enter();
393dfe7b3bfSKonstantin Belousov 		sbt = cpu_idleclock();
394dfe7b3bfSKonstantin Belousov 	}
395dfe7b3bfSKonstantin Belousov 
396dfe7b3bfSKonstantin Belousov 	/* Apply AMD APIC timer C1E workaround. */
397dfe7b3bfSKonstantin Belousov 	if (cpu_ident_amdc1e && cpu_disable_c3_sleep) {
398dfe7b3bfSKonstantin Belousov 		msr = rdmsr(MSR_AMDK8_IPM);
399dfe7b3bfSKonstantin Belousov 		if (msr & AMDK8_CMPHALT)
400dfe7b3bfSKonstantin Belousov 			wrmsr(MSR_AMDK8_IPM, msr & ~AMDK8_CMPHALT);
401dfe7b3bfSKonstantin Belousov 	}
402dfe7b3bfSKonstantin Belousov 
403dfe7b3bfSKonstantin Belousov 	/* Call main idle method. */
404dfe7b3bfSKonstantin Belousov 	cpu_idle_fn(sbt);
405dfe7b3bfSKonstantin Belousov 
406dfe7b3bfSKonstantin Belousov 	/* Switch timers back into active mode. */
407dfe7b3bfSKonstantin Belousov 	if (!busy) {
408dfe7b3bfSKonstantin Belousov 		cpu_activeclock();
409dfe7b3bfSKonstantin Belousov 		critical_exit();
410dfe7b3bfSKonstantin Belousov 	}
411dfe7b3bfSKonstantin Belousov out:
412dfe7b3bfSKonstantin Belousov 	CTR2(KTR_SPARE2, "cpu_idle(%d) at %d done",
413dfe7b3bfSKonstantin Belousov 	    busy, curcpu);
414dfe7b3bfSKonstantin Belousov }
415dfe7b3bfSKonstantin Belousov 
416dfe7b3bfSKonstantin Belousov int
417dfe7b3bfSKonstantin Belousov cpu_idle_wakeup(int cpu)
418dfe7b3bfSKonstantin Belousov {
419dfe7b3bfSKonstantin Belousov 	struct pcpu *pcpu;
420dfe7b3bfSKonstantin Belousov 	int *state;
421dfe7b3bfSKonstantin Belousov 
422dfe7b3bfSKonstantin Belousov 	pcpu = pcpu_find(cpu);
423dfe7b3bfSKonstantin Belousov 	state = (int *)pcpu->pc_monitorbuf;
424dfe7b3bfSKonstantin Belousov 	/*
425dfe7b3bfSKonstantin Belousov 	 * This doesn't need to be atomic since missing the race will
426dfe7b3bfSKonstantin Belousov 	 * simply result in unnecessary IPIs.
427dfe7b3bfSKonstantin Belousov 	 */
428dfe7b3bfSKonstantin Belousov 	if (*state == STATE_SLEEPING)
429dfe7b3bfSKonstantin Belousov 		return (0);
430dfe7b3bfSKonstantin Belousov 	if (*state == STATE_MWAIT)
431dfe7b3bfSKonstantin Belousov 		*state = STATE_RUNNING;
432dfe7b3bfSKonstantin Belousov 	return (1);
433dfe7b3bfSKonstantin Belousov }
434dfe7b3bfSKonstantin Belousov 
435dfe7b3bfSKonstantin Belousov /*
436dfe7b3bfSKonstantin Belousov  * Ordered by speed/power consumption.
437dfe7b3bfSKonstantin Belousov  */
438dfe7b3bfSKonstantin Belousov struct {
439dfe7b3bfSKonstantin Belousov 	void	*id_fn;
440dfe7b3bfSKonstantin Belousov 	char	*id_name;
441dfe7b3bfSKonstantin Belousov } idle_tbl[] = {
442dfe7b3bfSKonstantin Belousov 	{ cpu_idle_spin, "spin" },
443dfe7b3bfSKonstantin Belousov 	{ cpu_idle_mwait, "mwait" },
444dfe7b3bfSKonstantin Belousov 	{ cpu_idle_hlt, "hlt" },
445dfe7b3bfSKonstantin Belousov #if !defined(__i386__) || !defined(PC98)
446dfe7b3bfSKonstantin Belousov 	{ cpu_idle_acpi, "acpi" },
447dfe7b3bfSKonstantin Belousov #endif
448dfe7b3bfSKonstantin Belousov 	{ NULL, NULL }
449dfe7b3bfSKonstantin Belousov };
450dfe7b3bfSKonstantin Belousov 
451dfe7b3bfSKonstantin Belousov static int
452dfe7b3bfSKonstantin Belousov idle_sysctl_available(SYSCTL_HANDLER_ARGS)
453dfe7b3bfSKonstantin Belousov {
454dfe7b3bfSKonstantin Belousov 	char *avail, *p;
455dfe7b3bfSKonstantin Belousov 	int error;
456dfe7b3bfSKonstantin Belousov 	int i;
457dfe7b3bfSKonstantin Belousov 
458dfe7b3bfSKonstantin Belousov 	avail = malloc(256, M_TEMP, M_WAITOK);
459dfe7b3bfSKonstantin Belousov 	p = avail;
460dfe7b3bfSKonstantin Belousov 	for (i = 0; idle_tbl[i].id_name != NULL; i++) {
461dfe7b3bfSKonstantin Belousov 		if (strstr(idle_tbl[i].id_name, "mwait") &&
462dfe7b3bfSKonstantin Belousov 		    (cpu_feature2 & CPUID2_MON) == 0)
463dfe7b3bfSKonstantin Belousov 			continue;
464dfe7b3bfSKonstantin Belousov #if !defined(__i386__) || !defined(PC98)
465dfe7b3bfSKonstantin Belousov 		if (strcmp(idle_tbl[i].id_name, "acpi") == 0 &&
466dfe7b3bfSKonstantin Belousov 		    cpu_idle_hook == NULL)
467dfe7b3bfSKonstantin Belousov 			continue;
468dfe7b3bfSKonstantin Belousov #endif
469dfe7b3bfSKonstantin Belousov 		p += sprintf(p, "%s%s", p != avail ? ", " : "",
470dfe7b3bfSKonstantin Belousov 		    idle_tbl[i].id_name);
471dfe7b3bfSKonstantin Belousov 	}
472dfe7b3bfSKonstantin Belousov 	error = sysctl_handle_string(oidp, avail, 0, req);
473dfe7b3bfSKonstantin Belousov 	free(avail, M_TEMP);
474dfe7b3bfSKonstantin Belousov 	return (error);
475dfe7b3bfSKonstantin Belousov }
476dfe7b3bfSKonstantin Belousov 
477dfe7b3bfSKonstantin Belousov SYSCTL_PROC(_machdep, OID_AUTO, idle_available, CTLTYPE_STRING | CTLFLAG_RD,
478dfe7b3bfSKonstantin Belousov     0, 0, idle_sysctl_available, "A", "list of available idle functions");
479dfe7b3bfSKonstantin Belousov 
480dfe7b3bfSKonstantin Belousov static int
481dfe7b3bfSKonstantin Belousov idle_sysctl(SYSCTL_HANDLER_ARGS)
482dfe7b3bfSKonstantin Belousov {
483dfe7b3bfSKonstantin Belousov 	char buf[16];
484dfe7b3bfSKonstantin Belousov 	int error;
485dfe7b3bfSKonstantin Belousov 	char *p;
486dfe7b3bfSKonstantin Belousov 	int i;
487dfe7b3bfSKonstantin Belousov 
488dfe7b3bfSKonstantin Belousov 	p = "unknown";
489dfe7b3bfSKonstantin Belousov 	for (i = 0; idle_tbl[i].id_name != NULL; i++) {
490dfe7b3bfSKonstantin Belousov 		if (idle_tbl[i].id_fn == cpu_idle_fn) {
491dfe7b3bfSKonstantin Belousov 			p = idle_tbl[i].id_name;
492dfe7b3bfSKonstantin Belousov 			break;
493dfe7b3bfSKonstantin Belousov 		}
494dfe7b3bfSKonstantin Belousov 	}
495dfe7b3bfSKonstantin Belousov 	strncpy(buf, p, sizeof(buf));
496dfe7b3bfSKonstantin Belousov 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
497dfe7b3bfSKonstantin Belousov 	if (error != 0 || req->newptr == NULL)
498dfe7b3bfSKonstantin Belousov 		return (error);
499dfe7b3bfSKonstantin Belousov 	for (i = 0; idle_tbl[i].id_name != NULL; i++) {
500dfe7b3bfSKonstantin Belousov 		if (strstr(idle_tbl[i].id_name, "mwait") &&
501dfe7b3bfSKonstantin Belousov 		    (cpu_feature2 & CPUID2_MON) == 0)
502dfe7b3bfSKonstantin Belousov 			continue;
503dfe7b3bfSKonstantin Belousov #if !defined(__i386__) || !defined(PC98)
504dfe7b3bfSKonstantin Belousov 		if (strcmp(idle_tbl[i].id_name, "acpi") == 0 &&
505dfe7b3bfSKonstantin Belousov 		    cpu_idle_hook == NULL)
506dfe7b3bfSKonstantin Belousov 			continue;
507dfe7b3bfSKonstantin Belousov #endif
508dfe7b3bfSKonstantin Belousov 		if (strcmp(idle_tbl[i].id_name, buf))
509dfe7b3bfSKonstantin Belousov 			continue;
510dfe7b3bfSKonstantin Belousov 		cpu_idle_fn = idle_tbl[i].id_fn;
511dfe7b3bfSKonstantin Belousov 		return (0);
512dfe7b3bfSKonstantin Belousov 	}
513dfe7b3bfSKonstantin Belousov 	return (EINVAL);
514dfe7b3bfSKonstantin Belousov }
515dfe7b3bfSKonstantin Belousov 
516dfe7b3bfSKonstantin Belousov SYSCTL_PROC(_machdep, OID_AUTO, idle, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
517dfe7b3bfSKonstantin Belousov     idle_sysctl, "A", "currently selected idle function");
518