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