xref: /freebsd/sys/x86/x86/cpu_machdep.c (revision d9e8bbb64df954f974ad353fd22a7f4d8023bfb0)
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
93b57a73f8SKonstantin 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 
104*d9e8bbb6SKonstantin Belousov #define	STATE_RUNNING	0x0
105*d9e8bbb6SKonstantin Belousov #define	STATE_MWAIT	0x1
106*d9e8bbb6SKonstantin Belousov #define	STATE_SLEEPING	0x2
107*d9e8bbb6SKonstantin Belousov 
108dfe7b3bfSKonstantin Belousov /*
109dfe7b3bfSKonstantin Belousov  * Machine dependent boot() routine
110dfe7b3bfSKonstantin Belousov  *
111dfe7b3bfSKonstantin Belousov  * I haven't seen anything to put here yet
112dfe7b3bfSKonstantin Belousov  * Possibly some stuff might be grafted back here from boot()
113dfe7b3bfSKonstantin Belousov  */
114dfe7b3bfSKonstantin Belousov void
115dfe7b3bfSKonstantin Belousov cpu_boot(int howto)
116dfe7b3bfSKonstantin Belousov {
117dfe7b3bfSKonstantin Belousov }
118dfe7b3bfSKonstantin Belousov 
119dfe7b3bfSKonstantin Belousov /*
120dfe7b3bfSKonstantin Belousov  * Flush the D-cache for non-DMA I/O so that the I-cache can
121dfe7b3bfSKonstantin Belousov  * be made coherent later.
122dfe7b3bfSKonstantin Belousov  */
123dfe7b3bfSKonstantin Belousov void
124dfe7b3bfSKonstantin Belousov cpu_flush_dcache(void *ptr, size_t len)
125dfe7b3bfSKonstantin Belousov {
126dfe7b3bfSKonstantin Belousov 	/* Not applicable */
127dfe7b3bfSKonstantin Belousov }
128dfe7b3bfSKonstantin Belousov 
129b57a73f8SKonstantin Belousov void
130b57a73f8SKonstantin Belousov acpi_cpu_c1(void)
131b57a73f8SKonstantin Belousov {
132b57a73f8SKonstantin Belousov 
133b57a73f8SKonstantin Belousov 	__asm __volatile("sti; hlt");
134b57a73f8SKonstantin Belousov }
135b57a73f8SKonstantin Belousov 
136b57a73f8SKonstantin Belousov void
137b57a73f8SKonstantin Belousov acpi_cpu_idle_mwait(uint32_t mwait_hint)
138b57a73f8SKonstantin Belousov {
139b57a73f8SKonstantin Belousov 	int *state;
140b57a73f8SKonstantin Belousov 
141b57a73f8SKonstantin Belousov 	/*
142b57a73f8SKonstantin Belousov 	 * XXXKIB.  Software coordination mode should be supported,
143b57a73f8SKonstantin Belousov 	 * but all Intel CPUs provide hardware coordination.
144b57a73f8SKonstantin Belousov 	 */
145*d9e8bbb6SKonstantin Belousov 
146*d9e8bbb6SKonstantin Belousov 	state = (int *)PCPU_PTR(monitorbuf);
147*d9e8bbb6SKonstantin Belousov 	KASSERT(*state == STATE_SLEEPING,
148*d9e8bbb6SKonstantin Belousov 		("cpu_mwait_cx: wrong monitorbuf state"));
149*d9e8bbb6SKonstantin Belousov 	*state = STATE_MWAIT;
150b57a73f8SKonstantin Belousov 	cpu_monitor(state, 0, 0);
151*d9e8bbb6SKonstantin Belousov 	if (*state == STATE_MWAIT)
152b57a73f8SKonstantin Belousov 		cpu_mwait(MWAIT_INTRBREAK, mwait_hint);
153*d9e8bbb6SKonstantin Belousov 
154*d9e8bbb6SKonstantin Belousov 	/*
155*d9e8bbb6SKonstantin Belousov 	 * We should exit on any event that interrupts mwait, because
156*d9e8bbb6SKonstantin Belousov 	 * that event might be a wanted interrupt.
157*d9e8bbb6SKonstantin Belousov 	 */
158*d9e8bbb6SKonstantin Belousov 	*state = STATE_RUNNING;
159b57a73f8SKonstantin Belousov }
160b57a73f8SKonstantin Belousov 
161dfe7b3bfSKonstantin Belousov /* Get current clock frequency for the given cpu id. */
162dfe7b3bfSKonstantin Belousov int
163dfe7b3bfSKonstantin Belousov cpu_est_clockrate(int cpu_id, uint64_t *rate)
164dfe7b3bfSKonstantin Belousov {
165dfe7b3bfSKonstantin Belousov 	uint64_t tsc1, tsc2;
166dfe7b3bfSKonstantin Belousov 	uint64_t acnt, mcnt, perf;
167dfe7b3bfSKonstantin Belousov 	register_t reg;
168dfe7b3bfSKonstantin Belousov 
169dfe7b3bfSKonstantin Belousov 	if (pcpu_find(cpu_id) == NULL || rate == NULL)
170dfe7b3bfSKonstantin Belousov 		return (EINVAL);
171dfe7b3bfSKonstantin Belousov #ifdef __i386__
172dfe7b3bfSKonstantin Belousov 	if ((cpu_feature & CPUID_TSC) == 0)
173dfe7b3bfSKonstantin Belousov 		return (EOPNOTSUPP);
174dfe7b3bfSKonstantin Belousov #endif
175dfe7b3bfSKonstantin Belousov 
176dfe7b3bfSKonstantin Belousov 	/*
177dfe7b3bfSKonstantin Belousov 	 * If TSC is P-state invariant and APERF/MPERF MSRs do not exist,
178dfe7b3bfSKonstantin Belousov 	 * DELAY(9) based logic fails.
179dfe7b3bfSKonstantin Belousov 	 */
180dfe7b3bfSKonstantin Belousov 	if (tsc_is_invariant && !tsc_perf_stat)
181dfe7b3bfSKonstantin Belousov 		return (EOPNOTSUPP);
182dfe7b3bfSKonstantin Belousov 
183dfe7b3bfSKonstantin Belousov #ifdef SMP
184dfe7b3bfSKonstantin Belousov 	if (smp_cpus > 1) {
185dfe7b3bfSKonstantin Belousov 		/* Schedule ourselves on the indicated cpu. */
186dfe7b3bfSKonstantin Belousov 		thread_lock(curthread);
187dfe7b3bfSKonstantin Belousov 		sched_bind(curthread, cpu_id);
188dfe7b3bfSKonstantin Belousov 		thread_unlock(curthread);
189dfe7b3bfSKonstantin Belousov 	}
190dfe7b3bfSKonstantin Belousov #endif
191dfe7b3bfSKonstantin Belousov 
192dfe7b3bfSKonstantin Belousov 	/* Calibrate by measuring a short delay. */
193dfe7b3bfSKonstantin Belousov 	reg = intr_disable();
194dfe7b3bfSKonstantin Belousov 	if (tsc_is_invariant) {
195dfe7b3bfSKonstantin Belousov 		wrmsr(MSR_MPERF, 0);
196dfe7b3bfSKonstantin Belousov 		wrmsr(MSR_APERF, 0);
197dfe7b3bfSKonstantin Belousov 		tsc1 = rdtsc();
198dfe7b3bfSKonstantin Belousov 		DELAY(1000);
199dfe7b3bfSKonstantin Belousov 		mcnt = rdmsr(MSR_MPERF);
200dfe7b3bfSKonstantin Belousov 		acnt = rdmsr(MSR_APERF);
201dfe7b3bfSKonstantin Belousov 		tsc2 = rdtsc();
202dfe7b3bfSKonstantin Belousov 		intr_restore(reg);
203dfe7b3bfSKonstantin Belousov 		perf = 1000 * acnt / mcnt;
204dfe7b3bfSKonstantin Belousov 		*rate = (tsc2 - tsc1) * perf;
205dfe7b3bfSKonstantin Belousov 	} else {
206dfe7b3bfSKonstantin Belousov 		tsc1 = rdtsc();
207dfe7b3bfSKonstantin Belousov 		DELAY(1000);
208dfe7b3bfSKonstantin Belousov 		tsc2 = rdtsc();
209dfe7b3bfSKonstantin Belousov 		intr_restore(reg);
210dfe7b3bfSKonstantin Belousov 		*rate = (tsc2 - tsc1) * 1000;
211dfe7b3bfSKonstantin Belousov 	}
212dfe7b3bfSKonstantin Belousov 
213dfe7b3bfSKonstantin Belousov #ifdef SMP
214dfe7b3bfSKonstantin Belousov 	if (smp_cpus > 1) {
215dfe7b3bfSKonstantin Belousov 		thread_lock(curthread);
216dfe7b3bfSKonstantin Belousov 		sched_unbind(curthread);
217dfe7b3bfSKonstantin Belousov 		thread_unlock(curthread);
218dfe7b3bfSKonstantin Belousov 	}
219dfe7b3bfSKonstantin Belousov #endif
220dfe7b3bfSKonstantin Belousov 
221dfe7b3bfSKonstantin Belousov 	return (0);
222dfe7b3bfSKonstantin Belousov }
223dfe7b3bfSKonstantin Belousov 
224dfe7b3bfSKonstantin Belousov /*
225dfe7b3bfSKonstantin Belousov  * Shutdown the CPU as much as possible
226dfe7b3bfSKonstantin Belousov  */
227dfe7b3bfSKonstantin Belousov void
228dfe7b3bfSKonstantin Belousov cpu_halt(void)
229dfe7b3bfSKonstantin Belousov {
230dfe7b3bfSKonstantin Belousov 	for (;;)
231dfe7b3bfSKonstantin Belousov 		halt();
232dfe7b3bfSKonstantin Belousov }
233dfe7b3bfSKonstantin Belousov 
234b57a73f8SKonstantin Belousov bool
235b57a73f8SKonstantin Belousov cpu_mwait_usable(void)
236b57a73f8SKonstantin Belousov {
237b57a73f8SKonstantin Belousov 
238b57a73f8SKonstantin Belousov 	return ((cpu_feature2 & CPUID2_MON) != 0 && ((cpu_mon_mwait_flags &
239b57a73f8SKonstantin Belousov 	    (CPUID5_MON_MWAIT_EXT | CPUID5_MWAIT_INTRBREAK)) ==
240b57a73f8SKonstantin Belousov 	    (CPUID5_MON_MWAIT_EXT | CPUID5_MWAIT_INTRBREAK)));
241b57a73f8SKonstantin Belousov }
242b57a73f8SKonstantin Belousov 
243dfe7b3bfSKonstantin Belousov void (*cpu_idle_hook)(sbintime_t) = NULL;	/* ACPI idle hook. */
244dfe7b3bfSKonstantin Belousov static int	cpu_ident_amdc1e = 0;	/* AMD C1E supported. */
245dfe7b3bfSKonstantin Belousov static int	idle_mwait = 1;		/* Use MONITOR/MWAIT for short idle. */
246dfe7b3bfSKonstantin Belousov SYSCTL_INT(_machdep, OID_AUTO, idle_mwait, CTLFLAG_RWTUN, &idle_mwait,
247dfe7b3bfSKonstantin Belousov     0, "Use MONITOR/MWAIT for short idle");
248dfe7b3bfSKonstantin Belousov 
249dfe7b3bfSKonstantin Belousov #ifndef PC98
250dfe7b3bfSKonstantin Belousov static void
251dfe7b3bfSKonstantin Belousov cpu_idle_acpi(sbintime_t sbt)
252dfe7b3bfSKonstantin Belousov {
253dfe7b3bfSKonstantin Belousov 	int *state;
254dfe7b3bfSKonstantin Belousov 
255dfe7b3bfSKonstantin Belousov 	state = (int *)PCPU_PTR(monitorbuf);
256dfe7b3bfSKonstantin Belousov 	*state = STATE_SLEEPING;
257dfe7b3bfSKonstantin Belousov 
258dfe7b3bfSKonstantin Belousov 	/* See comments in cpu_idle_hlt(). */
259dfe7b3bfSKonstantin Belousov 	disable_intr();
260dfe7b3bfSKonstantin Belousov 	if (sched_runnable())
261dfe7b3bfSKonstantin Belousov 		enable_intr();
262dfe7b3bfSKonstantin Belousov 	else if (cpu_idle_hook)
263dfe7b3bfSKonstantin Belousov 		cpu_idle_hook(sbt);
264dfe7b3bfSKonstantin Belousov 	else
265b57a73f8SKonstantin Belousov 		acpi_cpu_c1();
266dfe7b3bfSKonstantin Belousov 	*state = STATE_RUNNING;
267dfe7b3bfSKonstantin Belousov }
268dfe7b3bfSKonstantin Belousov #endif /* !PC98 */
269dfe7b3bfSKonstantin Belousov 
270dfe7b3bfSKonstantin Belousov static void
271dfe7b3bfSKonstantin Belousov cpu_idle_hlt(sbintime_t sbt)
272dfe7b3bfSKonstantin Belousov {
273dfe7b3bfSKonstantin Belousov 	int *state;
274dfe7b3bfSKonstantin Belousov 
275dfe7b3bfSKonstantin Belousov 	state = (int *)PCPU_PTR(monitorbuf);
276dfe7b3bfSKonstantin Belousov 	*state = STATE_SLEEPING;
277dfe7b3bfSKonstantin Belousov 
278dfe7b3bfSKonstantin Belousov 	/*
279dfe7b3bfSKonstantin Belousov 	 * Since we may be in a critical section from cpu_idle(), if
280dfe7b3bfSKonstantin Belousov 	 * an interrupt fires during that critical section we may have
281dfe7b3bfSKonstantin Belousov 	 * a pending preemption.  If the CPU halts, then that thread
282dfe7b3bfSKonstantin Belousov 	 * may not execute until a later interrupt awakens the CPU.
283dfe7b3bfSKonstantin Belousov 	 * To handle this race, check for a runnable thread after
284dfe7b3bfSKonstantin Belousov 	 * disabling interrupts and immediately return if one is
285dfe7b3bfSKonstantin Belousov 	 * found.  Also, we must absolutely guarentee that hlt is
286dfe7b3bfSKonstantin Belousov 	 * the next instruction after sti.  This ensures that any
287dfe7b3bfSKonstantin Belousov 	 * interrupt that fires after the call to disable_intr() will
288dfe7b3bfSKonstantin Belousov 	 * immediately awaken the CPU from hlt.  Finally, please note
289dfe7b3bfSKonstantin Belousov 	 * that on x86 this works fine because of interrupts enabled only
290dfe7b3bfSKonstantin Belousov 	 * after the instruction following sti takes place, while IF is set
291dfe7b3bfSKonstantin Belousov 	 * to 1 immediately, allowing hlt instruction to acknowledge the
292dfe7b3bfSKonstantin Belousov 	 * interrupt.
293dfe7b3bfSKonstantin Belousov 	 */
294dfe7b3bfSKonstantin Belousov 	disable_intr();
295dfe7b3bfSKonstantin Belousov 	if (sched_runnable())
296dfe7b3bfSKonstantin Belousov 		enable_intr();
297dfe7b3bfSKonstantin Belousov 	else
298b57a73f8SKonstantin Belousov 		acpi_cpu_c1();
299dfe7b3bfSKonstantin Belousov 	*state = STATE_RUNNING;
300dfe7b3bfSKonstantin Belousov }
301dfe7b3bfSKonstantin Belousov 
302dfe7b3bfSKonstantin Belousov static void
303dfe7b3bfSKonstantin Belousov cpu_idle_mwait(sbintime_t sbt)
304dfe7b3bfSKonstantin Belousov {
305dfe7b3bfSKonstantin Belousov 	int *state;
306dfe7b3bfSKonstantin Belousov 
307dfe7b3bfSKonstantin Belousov 	state = (int *)PCPU_PTR(monitorbuf);
308dfe7b3bfSKonstantin Belousov 	*state = STATE_MWAIT;
309dfe7b3bfSKonstantin Belousov 
310dfe7b3bfSKonstantin Belousov 	/* See comments in cpu_idle_hlt(). */
311dfe7b3bfSKonstantin Belousov 	disable_intr();
312dfe7b3bfSKonstantin Belousov 	if (sched_runnable()) {
313dfe7b3bfSKonstantin Belousov 		enable_intr();
314dfe7b3bfSKonstantin Belousov 		*state = STATE_RUNNING;
315dfe7b3bfSKonstantin Belousov 		return;
316dfe7b3bfSKonstantin Belousov 	}
317dfe7b3bfSKonstantin Belousov 	cpu_monitor(state, 0, 0);
318dfe7b3bfSKonstantin Belousov 	if (*state == STATE_MWAIT)
319dfe7b3bfSKonstantin Belousov 		__asm __volatile("sti; mwait" : : "a" (MWAIT_C1), "c" (0));
320dfe7b3bfSKonstantin Belousov 	else
321dfe7b3bfSKonstantin Belousov 		enable_intr();
322dfe7b3bfSKonstantin Belousov 	*state = STATE_RUNNING;
323dfe7b3bfSKonstantin Belousov }
324dfe7b3bfSKonstantin Belousov 
325dfe7b3bfSKonstantin Belousov static void
326dfe7b3bfSKonstantin Belousov cpu_idle_spin(sbintime_t sbt)
327dfe7b3bfSKonstantin Belousov {
328dfe7b3bfSKonstantin Belousov 	int *state;
329dfe7b3bfSKonstantin Belousov 	int i;
330dfe7b3bfSKonstantin Belousov 
331dfe7b3bfSKonstantin Belousov 	state = (int *)PCPU_PTR(monitorbuf);
332dfe7b3bfSKonstantin Belousov 	*state = STATE_RUNNING;
333dfe7b3bfSKonstantin Belousov 
334dfe7b3bfSKonstantin Belousov 	/*
335dfe7b3bfSKonstantin Belousov 	 * The sched_runnable() call is racy but as long as there is
336dfe7b3bfSKonstantin Belousov 	 * a loop missing it one time will have just a little impact if any
337dfe7b3bfSKonstantin Belousov 	 * (and it is much better than missing the check at all).
338dfe7b3bfSKonstantin Belousov 	 */
339dfe7b3bfSKonstantin Belousov 	for (i = 0; i < 1000; i++) {
340dfe7b3bfSKonstantin Belousov 		if (sched_runnable())
341dfe7b3bfSKonstantin Belousov 			return;
342dfe7b3bfSKonstantin Belousov 		cpu_spinwait();
343dfe7b3bfSKonstantin Belousov 	}
344dfe7b3bfSKonstantin Belousov }
345dfe7b3bfSKonstantin Belousov 
346dfe7b3bfSKonstantin Belousov /*
347dfe7b3bfSKonstantin Belousov  * C1E renders the local APIC timer dead, so we disable it by
348dfe7b3bfSKonstantin Belousov  * reading the Interrupt Pending Message register and clearing
349dfe7b3bfSKonstantin Belousov  * both C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27).
350dfe7b3bfSKonstantin Belousov  *
351dfe7b3bfSKonstantin Belousov  * Reference:
352dfe7b3bfSKonstantin Belousov  *   "BIOS and Kernel Developer's Guide for AMD NPT Family 0Fh Processors"
353dfe7b3bfSKonstantin Belousov  *   #32559 revision 3.00+
354dfe7b3bfSKonstantin Belousov  */
355dfe7b3bfSKonstantin Belousov #define	MSR_AMDK8_IPM		0xc0010055
356dfe7b3bfSKonstantin Belousov #define	AMDK8_SMIONCMPHALT	(1ULL << 27)
357dfe7b3bfSKonstantin Belousov #define	AMDK8_C1EONCMPHALT	(1ULL << 28)
358dfe7b3bfSKonstantin Belousov #define	AMDK8_CMPHALT		(AMDK8_SMIONCMPHALT | AMDK8_C1EONCMPHALT)
359dfe7b3bfSKonstantin Belousov 
360dfe7b3bfSKonstantin Belousov void
361dfe7b3bfSKonstantin Belousov cpu_probe_amdc1e(void)
362dfe7b3bfSKonstantin Belousov {
363dfe7b3bfSKonstantin Belousov 
364dfe7b3bfSKonstantin Belousov 	/*
365dfe7b3bfSKonstantin Belousov 	 * Detect the presence of C1E capability mostly on latest
366dfe7b3bfSKonstantin Belousov 	 * dual-cores (or future) k8 family.
367dfe7b3bfSKonstantin Belousov 	 */
368dfe7b3bfSKonstantin Belousov 	if (cpu_vendor_id == CPU_VENDOR_AMD &&
369dfe7b3bfSKonstantin Belousov 	    (cpu_id & 0x00000f00) == 0x00000f00 &&
370dfe7b3bfSKonstantin Belousov 	    (cpu_id & 0x0fff0000) >=  0x00040000) {
371dfe7b3bfSKonstantin Belousov 		cpu_ident_amdc1e = 1;
372dfe7b3bfSKonstantin Belousov 	}
373dfe7b3bfSKonstantin Belousov }
374dfe7b3bfSKonstantin Belousov 
375ed95805eSJohn Baldwin #if defined(__i386__) && defined(PC98)
376dfe7b3bfSKonstantin Belousov void (*cpu_idle_fn)(sbintime_t) = cpu_idle_hlt;
377dfe7b3bfSKonstantin Belousov #else
378dfe7b3bfSKonstantin Belousov void (*cpu_idle_fn)(sbintime_t) = cpu_idle_acpi;
379dfe7b3bfSKonstantin Belousov #endif
380dfe7b3bfSKonstantin Belousov 
381dfe7b3bfSKonstantin Belousov void
382dfe7b3bfSKonstantin Belousov cpu_idle(int busy)
383dfe7b3bfSKonstantin Belousov {
384dfe7b3bfSKonstantin Belousov 	uint64_t msr;
385dfe7b3bfSKonstantin Belousov 	sbintime_t sbt = -1;
386dfe7b3bfSKonstantin Belousov 
387dfe7b3bfSKonstantin Belousov 	CTR2(KTR_SPARE2, "cpu_idle(%d) at %d",
388dfe7b3bfSKonstantin Belousov 	    busy, curcpu);
389ed95805eSJohn Baldwin #ifdef MP_WATCHDOG
390dfe7b3bfSKonstantin Belousov 	ap_watchdog(PCPU_GET(cpuid));
391dfe7b3bfSKonstantin Belousov #endif
392ed95805eSJohn Baldwin 
393dfe7b3bfSKonstantin Belousov 	/* If we are busy - try to use fast methods. */
394dfe7b3bfSKonstantin Belousov 	if (busy) {
395dfe7b3bfSKonstantin Belousov 		if ((cpu_feature2 & CPUID2_MON) && idle_mwait) {
396dfe7b3bfSKonstantin Belousov 			cpu_idle_mwait(busy);
397dfe7b3bfSKonstantin Belousov 			goto out;
398dfe7b3bfSKonstantin Belousov 		}
399dfe7b3bfSKonstantin Belousov 	}
400dfe7b3bfSKonstantin Belousov 
401dfe7b3bfSKonstantin Belousov 	/* If we have time - switch timers into idle mode. */
402dfe7b3bfSKonstantin Belousov 	if (!busy) {
403dfe7b3bfSKonstantin Belousov 		critical_enter();
404dfe7b3bfSKonstantin Belousov 		sbt = cpu_idleclock();
405dfe7b3bfSKonstantin Belousov 	}
406dfe7b3bfSKonstantin Belousov 
407dfe7b3bfSKonstantin Belousov 	/* Apply AMD APIC timer C1E workaround. */
408dfe7b3bfSKonstantin Belousov 	if (cpu_ident_amdc1e && cpu_disable_c3_sleep) {
409dfe7b3bfSKonstantin Belousov 		msr = rdmsr(MSR_AMDK8_IPM);
410dfe7b3bfSKonstantin Belousov 		if (msr & AMDK8_CMPHALT)
411dfe7b3bfSKonstantin Belousov 			wrmsr(MSR_AMDK8_IPM, msr & ~AMDK8_CMPHALT);
412dfe7b3bfSKonstantin Belousov 	}
413dfe7b3bfSKonstantin Belousov 
414dfe7b3bfSKonstantin Belousov 	/* Call main idle method. */
415dfe7b3bfSKonstantin Belousov 	cpu_idle_fn(sbt);
416dfe7b3bfSKonstantin Belousov 
417dfe7b3bfSKonstantin Belousov 	/* Switch timers back into active mode. */
418dfe7b3bfSKonstantin Belousov 	if (!busy) {
419dfe7b3bfSKonstantin Belousov 		cpu_activeclock();
420dfe7b3bfSKonstantin Belousov 		critical_exit();
421dfe7b3bfSKonstantin Belousov 	}
422dfe7b3bfSKonstantin Belousov out:
423dfe7b3bfSKonstantin Belousov 	CTR2(KTR_SPARE2, "cpu_idle(%d) at %d done",
424dfe7b3bfSKonstantin Belousov 	    busy, curcpu);
425dfe7b3bfSKonstantin Belousov }
426dfe7b3bfSKonstantin Belousov 
427dfe7b3bfSKonstantin Belousov int
428dfe7b3bfSKonstantin Belousov cpu_idle_wakeup(int cpu)
429dfe7b3bfSKonstantin Belousov {
430dfe7b3bfSKonstantin Belousov 	struct pcpu *pcpu;
431dfe7b3bfSKonstantin Belousov 	int *state;
432dfe7b3bfSKonstantin Belousov 
433dfe7b3bfSKonstantin Belousov 	pcpu = pcpu_find(cpu);
434dfe7b3bfSKonstantin Belousov 	state = (int *)pcpu->pc_monitorbuf;
435dfe7b3bfSKonstantin Belousov 	/*
436dfe7b3bfSKonstantin Belousov 	 * This doesn't need to be atomic since missing the race will
437dfe7b3bfSKonstantin Belousov 	 * simply result in unnecessary IPIs.
438dfe7b3bfSKonstantin Belousov 	 */
439dfe7b3bfSKonstantin Belousov 	if (*state == STATE_SLEEPING)
440dfe7b3bfSKonstantin Belousov 		return (0);
441dfe7b3bfSKonstantin Belousov 	if (*state == STATE_MWAIT)
442dfe7b3bfSKonstantin Belousov 		*state = STATE_RUNNING;
443dfe7b3bfSKonstantin Belousov 	return (1);
444dfe7b3bfSKonstantin Belousov }
445dfe7b3bfSKonstantin Belousov 
446dfe7b3bfSKonstantin Belousov /*
447dfe7b3bfSKonstantin Belousov  * Ordered by speed/power consumption.
448dfe7b3bfSKonstantin Belousov  */
449dfe7b3bfSKonstantin Belousov struct {
450dfe7b3bfSKonstantin Belousov 	void	*id_fn;
451dfe7b3bfSKonstantin Belousov 	char	*id_name;
452dfe7b3bfSKonstantin Belousov } idle_tbl[] = {
453dfe7b3bfSKonstantin Belousov 	{ cpu_idle_spin, "spin" },
454dfe7b3bfSKonstantin Belousov 	{ cpu_idle_mwait, "mwait" },
455dfe7b3bfSKonstantin Belousov 	{ cpu_idle_hlt, "hlt" },
456dfe7b3bfSKonstantin Belousov #if !defined(__i386__) || !defined(PC98)
457dfe7b3bfSKonstantin Belousov 	{ cpu_idle_acpi, "acpi" },
458dfe7b3bfSKonstantin Belousov #endif
459dfe7b3bfSKonstantin Belousov 	{ NULL, NULL }
460dfe7b3bfSKonstantin Belousov };
461dfe7b3bfSKonstantin Belousov 
462dfe7b3bfSKonstantin Belousov static int
463dfe7b3bfSKonstantin Belousov idle_sysctl_available(SYSCTL_HANDLER_ARGS)
464dfe7b3bfSKonstantin Belousov {
465dfe7b3bfSKonstantin Belousov 	char *avail, *p;
466dfe7b3bfSKonstantin Belousov 	int error;
467dfe7b3bfSKonstantin Belousov 	int i;
468dfe7b3bfSKonstantin Belousov 
469dfe7b3bfSKonstantin Belousov 	avail = malloc(256, M_TEMP, M_WAITOK);
470dfe7b3bfSKonstantin Belousov 	p = avail;
471dfe7b3bfSKonstantin Belousov 	for (i = 0; idle_tbl[i].id_name != NULL; i++) {
472dfe7b3bfSKonstantin Belousov 		if (strstr(idle_tbl[i].id_name, "mwait") &&
473dfe7b3bfSKonstantin Belousov 		    (cpu_feature2 & CPUID2_MON) == 0)
474dfe7b3bfSKonstantin Belousov 			continue;
475dfe7b3bfSKonstantin Belousov #if !defined(__i386__) || !defined(PC98)
476dfe7b3bfSKonstantin Belousov 		if (strcmp(idle_tbl[i].id_name, "acpi") == 0 &&
477dfe7b3bfSKonstantin Belousov 		    cpu_idle_hook == NULL)
478dfe7b3bfSKonstantin Belousov 			continue;
479dfe7b3bfSKonstantin Belousov #endif
480dfe7b3bfSKonstantin Belousov 		p += sprintf(p, "%s%s", p != avail ? ", " : "",
481dfe7b3bfSKonstantin Belousov 		    idle_tbl[i].id_name);
482dfe7b3bfSKonstantin Belousov 	}
483dfe7b3bfSKonstantin Belousov 	error = sysctl_handle_string(oidp, avail, 0, req);
484dfe7b3bfSKonstantin Belousov 	free(avail, M_TEMP);
485dfe7b3bfSKonstantin Belousov 	return (error);
486dfe7b3bfSKonstantin Belousov }
487dfe7b3bfSKonstantin Belousov 
488dfe7b3bfSKonstantin Belousov SYSCTL_PROC(_machdep, OID_AUTO, idle_available, CTLTYPE_STRING | CTLFLAG_RD,
489dfe7b3bfSKonstantin Belousov     0, 0, idle_sysctl_available, "A", "list of available idle functions");
490dfe7b3bfSKonstantin Belousov 
491dfe7b3bfSKonstantin Belousov static int
492dfe7b3bfSKonstantin Belousov idle_sysctl(SYSCTL_HANDLER_ARGS)
493dfe7b3bfSKonstantin Belousov {
494dfe7b3bfSKonstantin Belousov 	char buf[16];
495dfe7b3bfSKonstantin Belousov 	int error;
496dfe7b3bfSKonstantin Belousov 	char *p;
497dfe7b3bfSKonstantin Belousov 	int i;
498dfe7b3bfSKonstantin Belousov 
499dfe7b3bfSKonstantin Belousov 	p = "unknown";
500dfe7b3bfSKonstantin Belousov 	for (i = 0; idle_tbl[i].id_name != NULL; i++) {
501dfe7b3bfSKonstantin Belousov 		if (idle_tbl[i].id_fn == cpu_idle_fn) {
502dfe7b3bfSKonstantin Belousov 			p = idle_tbl[i].id_name;
503dfe7b3bfSKonstantin Belousov 			break;
504dfe7b3bfSKonstantin Belousov 		}
505dfe7b3bfSKonstantin Belousov 	}
506dfe7b3bfSKonstantin Belousov 	strncpy(buf, p, sizeof(buf));
507dfe7b3bfSKonstantin Belousov 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
508dfe7b3bfSKonstantin Belousov 	if (error != 0 || req->newptr == NULL)
509dfe7b3bfSKonstantin Belousov 		return (error);
510dfe7b3bfSKonstantin Belousov 	for (i = 0; idle_tbl[i].id_name != NULL; i++) {
511dfe7b3bfSKonstantin Belousov 		if (strstr(idle_tbl[i].id_name, "mwait") &&
512dfe7b3bfSKonstantin Belousov 		    (cpu_feature2 & CPUID2_MON) == 0)
513dfe7b3bfSKonstantin Belousov 			continue;
514dfe7b3bfSKonstantin Belousov #if !defined(__i386__) || !defined(PC98)
515dfe7b3bfSKonstantin Belousov 		if (strcmp(idle_tbl[i].id_name, "acpi") == 0 &&
516dfe7b3bfSKonstantin Belousov 		    cpu_idle_hook == NULL)
517dfe7b3bfSKonstantin Belousov 			continue;
518dfe7b3bfSKonstantin Belousov #endif
519dfe7b3bfSKonstantin Belousov 		if (strcmp(idle_tbl[i].id_name, buf))
520dfe7b3bfSKonstantin Belousov 			continue;
521dfe7b3bfSKonstantin Belousov 		cpu_idle_fn = idle_tbl[i].id_fn;
522dfe7b3bfSKonstantin Belousov 		return (0);
523dfe7b3bfSKonstantin Belousov 	}
524dfe7b3bfSKonstantin Belousov 	return (EINVAL);
525dfe7b3bfSKonstantin Belousov }
526dfe7b3bfSKonstantin Belousov 
527dfe7b3bfSKonstantin Belousov SYSCTL_PROC(_machdep, OID_AUTO, idle, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
528dfe7b3bfSKonstantin Belousov     idle_sysctl, "A", "currently selected idle function");
529