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 447705dd4dSKonstantin Belousov #include "opt_acpi.h" 45dfe7b3bfSKonstantin Belousov #include "opt_atpic.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_apic.h" 57dfe7b3bfSKonstantin Belousov #endif 58dfe7b3bfSKonstantin Belousov 59dfe7b3bfSKonstantin Belousov #include <sys/param.h> 60dfe7b3bfSKonstantin Belousov #include <sys/proc.h> 61dfe7b3bfSKonstantin Belousov #include <sys/systm.h> 62dfe7b3bfSKonstantin Belousov #include <sys/bus.h> 63dfe7b3bfSKonstantin Belousov #include <sys/cpu.h> 64*7355a02bSKonstantin Belousov #include <sys/domainset.h> 65dfe7b3bfSKonstantin Belousov #include <sys/kdb.h> 66dfe7b3bfSKonstantin Belousov #include <sys/kernel.h> 67dfe7b3bfSKonstantin Belousov #include <sys/ktr.h> 68dfe7b3bfSKonstantin Belousov #include <sys/lock.h> 69dfe7b3bfSKonstantin Belousov #include <sys/malloc.h> 70dfe7b3bfSKonstantin Belousov #include <sys/mutex.h> 71dfe7b3bfSKonstantin Belousov #include <sys/pcpu.h> 72dfe7b3bfSKonstantin Belousov #include <sys/rwlock.h> 73dfe7b3bfSKonstantin Belousov #include <sys/sched.h> 74dfe7b3bfSKonstantin Belousov #include <sys/smp.h> 75dfe7b3bfSKonstantin Belousov #include <sys/sysctl.h> 76dfe7b3bfSKonstantin Belousov 77dfe7b3bfSKonstantin Belousov #include <machine/clock.h> 78dfe7b3bfSKonstantin Belousov #include <machine/cpu.h> 79dfe7b3bfSKonstantin Belousov #include <machine/cputypes.h> 80dfe7b3bfSKonstantin Belousov #include <machine/specialreg.h> 81dfe7b3bfSKonstantin Belousov #include <machine/md_var.h> 82dfe7b3bfSKonstantin Belousov #include <machine/mp_watchdog.h> 83dfe7b3bfSKonstantin Belousov #include <machine/tss.h> 84dfe7b3bfSKonstantin Belousov #ifdef SMP 85dfe7b3bfSKonstantin Belousov #include <machine/smp.h> 86dfe7b3bfSKonstantin Belousov #endif 873da25bdbSAndriy Gapon #ifdef CPU_ELAN 883da25bdbSAndriy Gapon #include <machine/elan_mmcr.h> 893da25bdbSAndriy Gapon #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 1018428d0f1SAndriy Gapon #include <isa/isareg.h> 1028428d0f1SAndriy Gapon 1037705dd4dSKonstantin Belousov #include <contrib/dev/acpica/include/acpi.h> 1047705dd4dSKonstantin Belousov 105d9e8bbb6SKonstantin Belousov #define STATE_RUNNING 0x0 106d9e8bbb6SKonstantin Belousov #define STATE_MWAIT 0x1 107d9e8bbb6SKonstantin Belousov #define STATE_SLEEPING 0x2 108d9e8bbb6SKonstantin Belousov 1098428d0f1SAndriy Gapon #ifdef SMP 1108428d0f1SAndriy Gapon static u_int cpu_reset_proxyid; 1118428d0f1SAndriy Gapon static volatile u_int cpu_reset_proxy_active; 1128428d0f1SAndriy Gapon #endif 1138428d0f1SAndriy Gapon 114665919aaSConrad Meyer /* 115665919aaSConrad Meyer * Automatically initialized per CPU errata in cpu_idle_tun below. 116665919aaSConrad Meyer */ 117665919aaSConrad Meyer bool mwait_cpustop_broken = false; 118665919aaSConrad Meyer SYSCTL_BOOL(_machdep, OID_AUTO, mwait_cpustop_broken, CTLFLAG_RDTUN, 119665919aaSConrad Meyer &mwait_cpustop_broken, 0, 120665919aaSConrad Meyer "Can not reliably wake MONITOR/MWAIT cpus without interrupts"); 1218428d0f1SAndriy Gapon 122dfe7b3bfSKonstantin Belousov /* 123dfe7b3bfSKonstantin Belousov * Machine dependent boot() routine 124dfe7b3bfSKonstantin Belousov * 125dfe7b3bfSKonstantin Belousov * I haven't seen anything to put here yet 126dfe7b3bfSKonstantin Belousov * Possibly some stuff might be grafted back here from boot() 127dfe7b3bfSKonstantin Belousov */ 128dfe7b3bfSKonstantin Belousov void 129dfe7b3bfSKonstantin Belousov cpu_boot(int howto) 130dfe7b3bfSKonstantin Belousov { 131dfe7b3bfSKonstantin Belousov } 132dfe7b3bfSKonstantin Belousov 133dfe7b3bfSKonstantin Belousov /* 134dfe7b3bfSKonstantin Belousov * Flush the D-cache for non-DMA I/O so that the I-cache can 135dfe7b3bfSKonstantin Belousov * be made coherent later. 136dfe7b3bfSKonstantin Belousov */ 137dfe7b3bfSKonstantin Belousov void 138dfe7b3bfSKonstantin Belousov cpu_flush_dcache(void *ptr, size_t len) 139dfe7b3bfSKonstantin Belousov { 140dfe7b3bfSKonstantin Belousov /* Not applicable */ 141dfe7b3bfSKonstantin Belousov } 142dfe7b3bfSKonstantin Belousov 143b57a73f8SKonstantin Belousov void 144b57a73f8SKonstantin Belousov acpi_cpu_c1(void) 145b57a73f8SKonstantin Belousov { 146b57a73f8SKonstantin Belousov 147b57a73f8SKonstantin Belousov __asm __volatile("sti; hlt"); 148b57a73f8SKonstantin Belousov } 149b57a73f8SKonstantin Belousov 15019d4720bSJonathan T. Looney /* 15119d4720bSJonathan T. Looney * Use mwait to pause execution while waiting for an interrupt or 15219d4720bSJonathan T. Looney * another thread to signal that there is more work. 15319d4720bSJonathan T. Looney * 15419d4720bSJonathan T. Looney * NOTE: Interrupts will cause a wakeup; however, this function does 15519d4720bSJonathan T. Looney * not enable interrupt handling. The caller is responsible to enable 15619d4720bSJonathan T. Looney * interrupts. 15719d4720bSJonathan T. Looney */ 158b57a73f8SKonstantin Belousov void 159b57a73f8SKonstantin Belousov acpi_cpu_idle_mwait(uint32_t mwait_hint) 160b57a73f8SKonstantin Belousov { 161b57a73f8SKonstantin Belousov int *state; 1623621ba1eSKonstantin Belousov uint64_t v; 163b57a73f8SKonstantin Belousov 164b57a73f8SKonstantin Belousov /* 165319117fdSKonstantin Belousov * A comment in Linux patch claims that 'CPUs run faster with 166319117fdSKonstantin Belousov * speculation protection disabled. All CPU threads in a core 167319117fdSKonstantin Belousov * must disable speculation protection for it to be 168319117fdSKonstantin Belousov * disabled. Disable it while we are idle so the other 169319117fdSKonstantin Belousov * hyperthread can run fast.' 170319117fdSKonstantin Belousov * 171b57a73f8SKonstantin Belousov * XXXKIB. Software coordination mode should be supported, 172b57a73f8SKonstantin Belousov * but all Intel CPUs provide hardware coordination. 173b57a73f8SKonstantin Belousov */ 174d9e8bbb6SKonstantin Belousov 17583dc49beSConrad Meyer state = &PCPU_PTR(monitorbuf)->idle_state; 176a5bd21d0SKonstantin Belousov KASSERT(atomic_load_int(state) == STATE_SLEEPING, 177d9e8bbb6SKonstantin Belousov ("cpu_mwait_cx: wrong monitorbuf state")); 178a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_MWAIT); 1793621ba1eSKonstantin Belousov if (PCPU_GET(ibpb_set) || hw_ssb_active) { 1803621ba1eSKonstantin Belousov v = rdmsr(MSR_IA32_SPEC_CTRL); 1813621ba1eSKonstantin Belousov wrmsr(MSR_IA32_SPEC_CTRL, v & ~(IA32_SPEC_CTRL_IBRS | 1823621ba1eSKonstantin Belousov IA32_SPEC_CTRL_STIBP | IA32_SPEC_CTRL_SSBD)); 1833621ba1eSKonstantin Belousov } else { 1843621ba1eSKonstantin Belousov v = 0; 1853621ba1eSKonstantin Belousov } 186b57a73f8SKonstantin Belousov cpu_monitor(state, 0, 0); 187a5bd21d0SKonstantin Belousov if (atomic_load_int(state) == STATE_MWAIT) 188b57a73f8SKonstantin Belousov cpu_mwait(MWAIT_INTRBREAK, mwait_hint); 1893621ba1eSKonstantin Belousov 1903621ba1eSKonstantin Belousov /* 1913621ba1eSKonstantin Belousov * SSB cannot be disabled while we sleep, or rather, if it was 1923621ba1eSKonstantin Belousov * disabled, the sysctl thread will bind to our cpu to tweak 1933621ba1eSKonstantin Belousov * MSR. 1943621ba1eSKonstantin Belousov */ 1953621ba1eSKonstantin Belousov if (v != 0) 1963621ba1eSKonstantin Belousov wrmsr(MSR_IA32_SPEC_CTRL, v); 197d9e8bbb6SKonstantin Belousov 198d9e8bbb6SKonstantin Belousov /* 199d9e8bbb6SKonstantin Belousov * We should exit on any event that interrupts mwait, because 200d9e8bbb6SKonstantin Belousov * that event might be a wanted interrupt. 201d9e8bbb6SKonstantin Belousov */ 202a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_RUNNING); 203b57a73f8SKonstantin Belousov } 204b57a73f8SKonstantin Belousov 205dfe7b3bfSKonstantin Belousov /* Get current clock frequency for the given cpu id. */ 206dfe7b3bfSKonstantin Belousov int 207dfe7b3bfSKonstantin Belousov cpu_est_clockrate(int cpu_id, uint64_t *rate) 208dfe7b3bfSKonstantin Belousov { 209dfe7b3bfSKonstantin Belousov uint64_t tsc1, tsc2; 210dfe7b3bfSKonstantin Belousov uint64_t acnt, mcnt, perf; 211dfe7b3bfSKonstantin Belousov register_t reg; 212dfe7b3bfSKonstantin Belousov 213dfe7b3bfSKonstantin Belousov if (pcpu_find(cpu_id) == NULL || rate == NULL) 214dfe7b3bfSKonstantin Belousov return (EINVAL); 215dfe7b3bfSKonstantin Belousov #ifdef __i386__ 216dfe7b3bfSKonstantin Belousov if ((cpu_feature & CPUID_TSC) == 0) 217dfe7b3bfSKonstantin Belousov return (EOPNOTSUPP); 218dfe7b3bfSKonstantin Belousov #endif 219dfe7b3bfSKonstantin Belousov 220dfe7b3bfSKonstantin Belousov /* 221dfe7b3bfSKonstantin Belousov * If TSC is P-state invariant and APERF/MPERF MSRs do not exist, 222dfe7b3bfSKonstantin Belousov * DELAY(9) based logic fails. 223dfe7b3bfSKonstantin Belousov */ 224dfe7b3bfSKonstantin Belousov if (tsc_is_invariant && !tsc_perf_stat) 225dfe7b3bfSKonstantin Belousov return (EOPNOTSUPP); 226dfe7b3bfSKonstantin Belousov 227dfe7b3bfSKonstantin Belousov #ifdef SMP 228dfe7b3bfSKonstantin Belousov if (smp_cpus > 1) { 229dfe7b3bfSKonstantin Belousov /* Schedule ourselves on the indicated cpu. */ 230dfe7b3bfSKonstantin Belousov thread_lock(curthread); 231dfe7b3bfSKonstantin Belousov sched_bind(curthread, cpu_id); 232dfe7b3bfSKonstantin Belousov thread_unlock(curthread); 233dfe7b3bfSKonstantin Belousov } 234dfe7b3bfSKonstantin Belousov #endif 235dfe7b3bfSKonstantin Belousov 236dfe7b3bfSKonstantin Belousov /* Calibrate by measuring a short delay. */ 237dfe7b3bfSKonstantin Belousov reg = intr_disable(); 238dfe7b3bfSKonstantin Belousov if (tsc_is_invariant) { 239dfe7b3bfSKonstantin Belousov wrmsr(MSR_MPERF, 0); 240dfe7b3bfSKonstantin Belousov wrmsr(MSR_APERF, 0); 241dfe7b3bfSKonstantin Belousov tsc1 = rdtsc(); 242dfe7b3bfSKonstantin Belousov DELAY(1000); 243dfe7b3bfSKonstantin Belousov mcnt = rdmsr(MSR_MPERF); 244dfe7b3bfSKonstantin Belousov acnt = rdmsr(MSR_APERF); 245dfe7b3bfSKonstantin Belousov tsc2 = rdtsc(); 246dfe7b3bfSKonstantin Belousov intr_restore(reg); 247dfe7b3bfSKonstantin Belousov perf = 1000 * acnt / mcnt; 248dfe7b3bfSKonstantin Belousov *rate = (tsc2 - tsc1) * perf; 249dfe7b3bfSKonstantin Belousov } else { 250dfe7b3bfSKonstantin Belousov tsc1 = rdtsc(); 251dfe7b3bfSKonstantin Belousov DELAY(1000); 252dfe7b3bfSKonstantin Belousov tsc2 = rdtsc(); 253dfe7b3bfSKonstantin Belousov intr_restore(reg); 254dfe7b3bfSKonstantin Belousov *rate = (tsc2 - tsc1) * 1000; 255dfe7b3bfSKonstantin Belousov } 256dfe7b3bfSKonstantin Belousov 257dfe7b3bfSKonstantin Belousov #ifdef SMP 258dfe7b3bfSKonstantin Belousov if (smp_cpus > 1) { 259dfe7b3bfSKonstantin Belousov thread_lock(curthread); 260dfe7b3bfSKonstantin Belousov sched_unbind(curthread); 261dfe7b3bfSKonstantin Belousov thread_unlock(curthread); 262dfe7b3bfSKonstantin Belousov } 263dfe7b3bfSKonstantin Belousov #endif 264dfe7b3bfSKonstantin Belousov 265dfe7b3bfSKonstantin Belousov return (0); 266dfe7b3bfSKonstantin Belousov } 267dfe7b3bfSKonstantin Belousov 268dfe7b3bfSKonstantin Belousov /* 269dfe7b3bfSKonstantin Belousov * Shutdown the CPU as much as possible 270dfe7b3bfSKonstantin Belousov */ 271dfe7b3bfSKonstantin Belousov void 272dfe7b3bfSKonstantin Belousov cpu_halt(void) 273dfe7b3bfSKonstantin Belousov { 274dfe7b3bfSKonstantin Belousov for (;;) 275dfe7b3bfSKonstantin Belousov halt(); 276dfe7b3bfSKonstantin Belousov } 277dfe7b3bfSKonstantin Belousov 2788428d0f1SAndriy Gapon static void 279b7b25af0SAndriy Gapon cpu_reset_real(void) 2808428d0f1SAndriy Gapon { 2818428d0f1SAndriy Gapon struct region_descriptor null_idt; 2828428d0f1SAndriy Gapon int b; 2838428d0f1SAndriy Gapon 2848428d0f1SAndriy Gapon disable_intr(); 2858428d0f1SAndriy Gapon #ifdef CPU_ELAN 2868428d0f1SAndriy Gapon if (elan_mmcr != NULL) 2878428d0f1SAndriy Gapon elan_mmcr->RESCFG = 1; 2888428d0f1SAndriy Gapon #endif 2898428d0f1SAndriy Gapon #ifdef __i386__ 2908428d0f1SAndriy Gapon if (cpu == CPU_GEODE1100) { 2918428d0f1SAndriy Gapon /* Attempt Geode's own reset */ 2928428d0f1SAndriy Gapon outl(0xcf8, 0x80009044ul); 2938428d0f1SAndriy Gapon outl(0xcfc, 0xf); 2948428d0f1SAndriy Gapon } 2958428d0f1SAndriy Gapon #endif 2968428d0f1SAndriy Gapon #if !defined(BROKEN_KEYBOARD_RESET) 2978428d0f1SAndriy Gapon /* 2988428d0f1SAndriy Gapon * Attempt to do a CPU reset via the keyboard controller, 2998428d0f1SAndriy Gapon * do not turn off GateA20, as any machine that fails 3008428d0f1SAndriy Gapon * to do the reset here would then end up in no man's land. 3018428d0f1SAndriy Gapon */ 3028428d0f1SAndriy Gapon outb(IO_KBD + 4, 0xFE); 3038428d0f1SAndriy Gapon DELAY(500000); /* wait 0.5 sec to see if that did it */ 3048428d0f1SAndriy Gapon #endif 3058428d0f1SAndriy Gapon 3068428d0f1SAndriy Gapon /* 3078428d0f1SAndriy Gapon * Attempt to force a reset via the Reset Control register at 3088428d0f1SAndriy Gapon * I/O port 0xcf9. Bit 2 forces a system reset when it 3098428d0f1SAndriy Gapon * transitions from 0 to 1. Bit 1 selects the type of reset 3108428d0f1SAndriy Gapon * to attempt: 0 selects a "soft" reset, and 1 selects a 3118428d0f1SAndriy Gapon * "hard" reset. We try a "hard" reset. The first write sets 3128428d0f1SAndriy Gapon * bit 1 to select a "hard" reset and clears bit 2. The 3138428d0f1SAndriy Gapon * second write forces a 0 -> 1 transition in bit 2 to trigger 3148428d0f1SAndriy Gapon * a reset. 3158428d0f1SAndriy Gapon */ 3168428d0f1SAndriy Gapon outb(0xcf9, 0x2); 3178428d0f1SAndriy Gapon outb(0xcf9, 0x6); 3188428d0f1SAndriy Gapon DELAY(500000); /* wait 0.5 sec to see if that did it */ 3198428d0f1SAndriy Gapon 3208428d0f1SAndriy Gapon /* 3218428d0f1SAndriy Gapon * Attempt to force a reset via the Fast A20 and Init register 3228428d0f1SAndriy Gapon * at I/O port 0x92. Bit 1 serves as an alternate A20 gate. 3238428d0f1SAndriy Gapon * Bit 0 asserts INIT# when set to 1. We are careful to only 3248428d0f1SAndriy Gapon * preserve bit 1 while setting bit 0. We also must clear bit 3258428d0f1SAndriy Gapon * 0 before setting it if it isn't already clear. 3268428d0f1SAndriy Gapon */ 3278428d0f1SAndriy Gapon b = inb(0x92); 3288428d0f1SAndriy Gapon if (b != 0xff) { 3298428d0f1SAndriy Gapon if ((b & 0x1) != 0) 3308428d0f1SAndriy Gapon outb(0x92, b & 0xfe); 3318428d0f1SAndriy Gapon outb(0x92, b | 0x1); 3328428d0f1SAndriy Gapon DELAY(500000); /* wait 0.5 sec to see if that did it */ 3338428d0f1SAndriy Gapon } 3348428d0f1SAndriy Gapon 3358428d0f1SAndriy Gapon printf("No known reset method worked, attempting CPU shutdown\n"); 3368428d0f1SAndriy Gapon DELAY(1000000); /* wait 1 sec for printf to complete */ 3378428d0f1SAndriy Gapon 3388428d0f1SAndriy Gapon /* Wipe the IDT. */ 3398428d0f1SAndriy Gapon null_idt.rd_limit = 0; 3408428d0f1SAndriy Gapon null_idt.rd_base = 0; 3418428d0f1SAndriy Gapon lidt(&null_idt); 3428428d0f1SAndriy Gapon 3438428d0f1SAndriy Gapon /* "good night, sweet prince .... <THUNK!>" */ 3448428d0f1SAndriy Gapon breakpoint(); 3458428d0f1SAndriy Gapon 3468428d0f1SAndriy Gapon /* NOTREACHED */ 3478428d0f1SAndriy Gapon while(1); 3488428d0f1SAndriy Gapon } 3498428d0f1SAndriy Gapon 3508428d0f1SAndriy Gapon #ifdef SMP 3518428d0f1SAndriy Gapon static void 352b7b25af0SAndriy Gapon cpu_reset_proxy(void) 3538428d0f1SAndriy Gapon { 3548428d0f1SAndriy Gapon 3558428d0f1SAndriy Gapon cpu_reset_proxy_active = 1; 3568428d0f1SAndriy Gapon while (cpu_reset_proxy_active == 1) 3578428d0f1SAndriy Gapon ia32_pause(); /* Wait for other cpu to see that we've started */ 3588428d0f1SAndriy Gapon 3598428d0f1SAndriy Gapon printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid); 3608428d0f1SAndriy Gapon DELAY(1000000); 3618428d0f1SAndriy Gapon cpu_reset_real(); 3628428d0f1SAndriy Gapon } 3638428d0f1SAndriy Gapon #endif 3648428d0f1SAndriy Gapon 3658428d0f1SAndriy Gapon void 366b7b25af0SAndriy Gapon cpu_reset(void) 3678428d0f1SAndriy Gapon { 3688428d0f1SAndriy Gapon #ifdef SMP 369665919aaSConrad Meyer struct monitorbuf *mb; 3708428d0f1SAndriy Gapon cpuset_t map; 3718428d0f1SAndriy Gapon u_int cnt; 3728428d0f1SAndriy Gapon 3738428d0f1SAndriy Gapon if (smp_started) { 3748428d0f1SAndriy Gapon map = all_cpus; 3758428d0f1SAndriy Gapon CPU_CLR(PCPU_GET(cpuid), &map); 3768428d0f1SAndriy Gapon CPU_NAND(&map, &stopped_cpus); 3778428d0f1SAndriy Gapon if (!CPU_EMPTY(&map)) { 3788428d0f1SAndriy Gapon printf("cpu_reset: Stopping other CPUs\n"); 3798428d0f1SAndriy Gapon stop_cpus(map); 3808428d0f1SAndriy Gapon } 3818428d0f1SAndriy Gapon 3828428d0f1SAndriy Gapon if (PCPU_GET(cpuid) != 0) { 3838428d0f1SAndriy Gapon cpu_reset_proxyid = PCPU_GET(cpuid); 3848428d0f1SAndriy Gapon cpustop_restartfunc = cpu_reset_proxy; 3858428d0f1SAndriy Gapon cpu_reset_proxy_active = 0; 3868428d0f1SAndriy Gapon printf("cpu_reset: Restarting BSP\n"); 3878428d0f1SAndriy Gapon 3888428d0f1SAndriy Gapon /* Restart CPU #0. */ 3898428d0f1SAndriy Gapon CPU_SETOF(0, &started_cpus); 390665919aaSConrad Meyer mb = &pcpu_find(0)->pc_monitorbuf; 391665919aaSConrad Meyer atomic_store_int(&mb->stop_state, 392665919aaSConrad Meyer MONITOR_STOPSTATE_RUNNING); 3938428d0f1SAndriy Gapon wmb(); 3948428d0f1SAndriy Gapon 3958428d0f1SAndriy Gapon cnt = 0; 3968428d0f1SAndriy Gapon while (cpu_reset_proxy_active == 0 && cnt < 10000000) { 3978428d0f1SAndriy Gapon ia32_pause(); 3988428d0f1SAndriy Gapon cnt++; /* Wait for BSP to announce restart */ 3998428d0f1SAndriy Gapon } 4008428d0f1SAndriy Gapon if (cpu_reset_proxy_active == 0) { 4018428d0f1SAndriy Gapon printf("cpu_reset: Failed to restart BSP\n"); 4028428d0f1SAndriy Gapon } else { 4038428d0f1SAndriy Gapon cpu_reset_proxy_active = 2; 4048428d0f1SAndriy Gapon while (1) 4058428d0f1SAndriy Gapon ia32_pause(); 4068428d0f1SAndriy Gapon /* NOTREACHED */ 4078428d0f1SAndriy Gapon } 4088428d0f1SAndriy Gapon } 4098428d0f1SAndriy Gapon 4108428d0f1SAndriy Gapon DELAY(1000000); 4118428d0f1SAndriy Gapon } 4128428d0f1SAndriy Gapon #endif 4138428d0f1SAndriy Gapon cpu_reset_real(); 4148428d0f1SAndriy Gapon /* NOTREACHED */ 4158428d0f1SAndriy Gapon } 4168428d0f1SAndriy Gapon 417b57a73f8SKonstantin Belousov bool 418b57a73f8SKonstantin Belousov cpu_mwait_usable(void) 419b57a73f8SKonstantin Belousov { 420b57a73f8SKonstantin Belousov 421b57a73f8SKonstantin Belousov return ((cpu_feature2 & CPUID2_MON) != 0 && ((cpu_mon_mwait_flags & 422b57a73f8SKonstantin Belousov (CPUID5_MON_MWAIT_EXT | CPUID5_MWAIT_INTRBREAK)) == 423b57a73f8SKonstantin Belousov (CPUID5_MON_MWAIT_EXT | CPUID5_MWAIT_INTRBREAK))); 424b57a73f8SKonstantin Belousov } 425b57a73f8SKonstantin Belousov 426dfe7b3bfSKonstantin Belousov void (*cpu_idle_hook)(sbintime_t) = NULL; /* ACPI idle hook. */ 427dfe7b3bfSKonstantin Belousov static int cpu_ident_amdc1e = 0; /* AMD C1E supported. */ 428dfe7b3bfSKonstantin Belousov static int idle_mwait = 1; /* Use MONITOR/MWAIT for short idle. */ 429dfe7b3bfSKonstantin Belousov SYSCTL_INT(_machdep, OID_AUTO, idle_mwait, CTLFLAG_RWTUN, &idle_mwait, 430dfe7b3bfSKonstantin Belousov 0, "Use MONITOR/MWAIT for short idle"); 431dfe7b3bfSKonstantin Belousov 432dfe7b3bfSKonstantin Belousov static void 433dfe7b3bfSKonstantin Belousov cpu_idle_acpi(sbintime_t sbt) 434dfe7b3bfSKonstantin Belousov { 435dfe7b3bfSKonstantin Belousov int *state; 436dfe7b3bfSKonstantin Belousov 43783dc49beSConrad Meyer state = &PCPU_PTR(monitorbuf)->idle_state; 438a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_SLEEPING); 439dfe7b3bfSKonstantin Belousov 440dfe7b3bfSKonstantin Belousov /* See comments in cpu_idle_hlt(). */ 441dfe7b3bfSKonstantin Belousov disable_intr(); 442dfe7b3bfSKonstantin Belousov if (sched_runnable()) 443dfe7b3bfSKonstantin Belousov enable_intr(); 444dfe7b3bfSKonstantin Belousov else if (cpu_idle_hook) 445dfe7b3bfSKonstantin Belousov cpu_idle_hook(sbt); 446dfe7b3bfSKonstantin Belousov else 447b57a73f8SKonstantin Belousov acpi_cpu_c1(); 448a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_RUNNING); 449dfe7b3bfSKonstantin Belousov } 450dfe7b3bfSKonstantin Belousov 451dfe7b3bfSKonstantin Belousov static void 452dfe7b3bfSKonstantin Belousov cpu_idle_hlt(sbintime_t sbt) 453dfe7b3bfSKonstantin Belousov { 454dfe7b3bfSKonstantin Belousov int *state; 455dfe7b3bfSKonstantin Belousov 45683dc49beSConrad Meyer state = &PCPU_PTR(monitorbuf)->idle_state; 457a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_SLEEPING); 458dfe7b3bfSKonstantin Belousov 459dfe7b3bfSKonstantin Belousov /* 460dfe7b3bfSKonstantin Belousov * Since we may be in a critical section from cpu_idle(), if 461dfe7b3bfSKonstantin Belousov * an interrupt fires during that critical section we may have 462dfe7b3bfSKonstantin Belousov * a pending preemption. If the CPU halts, then that thread 463dfe7b3bfSKonstantin Belousov * may not execute until a later interrupt awakens the CPU. 464dfe7b3bfSKonstantin Belousov * To handle this race, check for a runnable thread after 465dfe7b3bfSKonstantin Belousov * disabling interrupts and immediately return if one is 466dfe7b3bfSKonstantin Belousov * found. Also, we must absolutely guarentee that hlt is 467dfe7b3bfSKonstantin Belousov * the next instruction after sti. This ensures that any 468dfe7b3bfSKonstantin Belousov * interrupt that fires after the call to disable_intr() will 469dfe7b3bfSKonstantin Belousov * immediately awaken the CPU from hlt. Finally, please note 470dfe7b3bfSKonstantin Belousov * that on x86 this works fine because of interrupts enabled only 471dfe7b3bfSKonstantin Belousov * after the instruction following sti takes place, while IF is set 472dfe7b3bfSKonstantin Belousov * to 1 immediately, allowing hlt instruction to acknowledge the 473dfe7b3bfSKonstantin Belousov * interrupt. 474dfe7b3bfSKonstantin Belousov */ 475dfe7b3bfSKonstantin Belousov disable_intr(); 476dfe7b3bfSKonstantin Belousov if (sched_runnable()) 477dfe7b3bfSKonstantin Belousov enable_intr(); 478dfe7b3bfSKonstantin Belousov else 479b57a73f8SKonstantin Belousov acpi_cpu_c1(); 480a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_RUNNING); 481dfe7b3bfSKonstantin Belousov } 482dfe7b3bfSKonstantin Belousov 483dfe7b3bfSKonstantin Belousov static void 484dfe7b3bfSKonstantin Belousov cpu_idle_mwait(sbintime_t sbt) 485dfe7b3bfSKonstantin Belousov { 486dfe7b3bfSKonstantin Belousov int *state; 487dfe7b3bfSKonstantin Belousov 48883dc49beSConrad Meyer state = &PCPU_PTR(monitorbuf)->idle_state; 489a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_MWAIT); 490dfe7b3bfSKonstantin Belousov 491dfe7b3bfSKonstantin Belousov /* See comments in cpu_idle_hlt(). */ 492dfe7b3bfSKonstantin Belousov disable_intr(); 493dfe7b3bfSKonstantin Belousov if (sched_runnable()) { 494a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_RUNNING); 495dfe7b3bfSKonstantin Belousov enable_intr(); 496dfe7b3bfSKonstantin Belousov return; 497dfe7b3bfSKonstantin Belousov } 498a5bd21d0SKonstantin Belousov 499dfe7b3bfSKonstantin Belousov cpu_monitor(state, 0, 0); 500a5bd21d0SKonstantin Belousov if (atomic_load_int(state) == STATE_MWAIT) 501dfe7b3bfSKonstantin Belousov __asm __volatile("sti; mwait" : : "a" (MWAIT_C1), "c" (0)); 502dfe7b3bfSKonstantin Belousov else 503dfe7b3bfSKonstantin Belousov enable_intr(); 504a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_RUNNING); 505dfe7b3bfSKonstantin Belousov } 506dfe7b3bfSKonstantin Belousov 507dfe7b3bfSKonstantin Belousov static void 508dfe7b3bfSKonstantin Belousov cpu_idle_spin(sbintime_t sbt) 509dfe7b3bfSKonstantin Belousov { 510dfe7b3bfSKonstantin Belousov int *state; 511dfe7b3bfSKonstantin Belousov int i; 512dfe7b3bfSKonstantin Belousov 51383dc49beSConrad Meyer state = &PCPU_PTR(monitorbuf)->idle_state; 514a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_RUNNING); 515dfe7b3bfSKonstantin Belousov 516dfe7b3bfSKonstantin Belousov /* 517dfe7b3bfSKonstantin Belousov * The sched_runnable() call is racy but as long as there is 518dfe7b3bfSKonstantin Belousov * a loop missing it one time will have just a little impact if any 519dfe7b3bfSKonstantin Belousov * (and it is much better than missing the check at all). 520dfe7b3bfSKonstantin Belousov */ 521dfe7b3bfSKonstantin Belousov for (i = 0; i < 1000; i++) { 522dfe7b3bfSKonstantin Belousov if (sched_runnable()) 523dfe7b3bfSKonstantin Belousov return; 524dfe7b3bfSKonstantin Belousov cpu_spinwait(); 525dfe7b3bfSKonstantin Belousov } 526dfe7b3bfSKonstantin Belousov } 527dfe7b3bfSKonstantin Belousov 528dfe7b3bfSKonstantin Belousov /* 529dfe7b3bfSKonstantin Belousov * C1E renders the local APIC timer dead, so we disable it by 530dfe7b3bfSKonstantin Belousov * reading the Interrupt Pending Message register and clearing 531dfe7b3bfSKonstantin Belousov * both C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27). 532dfe7b3bfSKonstantin Belousov * 533dfe7b3bfSKonstantin Belousov * Reference: 534dfe7b3bfSKonstantin Belousov * "BIOS and Kernel Developer's Guide for AMD NPT Family 0Fh Processors" 535dfe7b3bfSKonstantin Belousov * #32559 revision 3.00+ 536dfe7b3bfSKonstantin Belousov */ 537dfe7b3bfSKonstantin Belousov #define MSR_AMDK8_IPM 0xc0010055 538dfe7b3bfSKonstantin Belousov #define AMDK8_SMIONCMPHALT (1ULL << 27) 539dfe7b3bfSKonstantin Belousov #define AMDK8_C1EONCMPHALT (1ULL << 28) 540dfe7b3bfSKonstantin Belousov #define AMDK8_CMPHALT (AMDK8_SMIONCMPHALT | AMDK8_C1EONCMPHALT) 541dfe7b3bfSKonstantin Belousov 542dfe7b3bfSKonstantin Belousov void 543dfe7b3bfSKonstantin Belousov cpu_probe_amdc1e(void) 544dfe7b3bfSKonstantin Belousov { 545dfe7b3bfSKonstantin Belousov 546dfe7b3bfSKonstantin Belousov /* 547dfe7b3bfSKonstantin Belousov * Detect the presence of C1E capability mostly on latest 548dfe7b3bfSKonstantin Belousov * dual-cores (or future) k8 family. 549dfe7b3bfSKonstantin Belousov */ 550dfe7b3bfSKonstantin Belousov if (cpu_vendor_id == CPU_VENDOR_AMD && 551dfe7b3bfSKonstantin Belousov (cpu_id & 0x00000f00) == 0x00000f00 && 552dfe7b3bfSKonstantin Belousov (cpu_id & 0x0fff0000) >= 0x00040000) { 553dfe7b3bfSKonstantin Belousov cpu_ident_amdc1e = 1; 554dfe7b3bfSKonstantin Belousov } 555dfe7b3bfSKonstantin Belousov } 556dfe7b3bfSKonstantin Belousov 557dfe7b3bfSKonstantin Belousov void (*cpu_idle_fn)(sbintime_t) = cpu_idle_acpi; 558dfe7b3bfSKonstantin Belousov 559dfe7b3bfSKonstantin Belousov void 560dfe7b3bfSKonstantin Belousov cpu_idle(int busy) 561dfe7b3bfSKonstantin Belousov { 562dfe7b3bfSKonstantin Belousov uint64_t msr; 563dfe7b3bfSKonstantin Belousov sbintime_t sbt = -1; 564dfe7b3bfSKonstantin Belousov 565dfe7b3bfSKonstantin Belousov CTR2(KTR_SPARE2, "cpu_idle(%d) at %d", 566dfe7b3bfSKonstantin Belousov busy, curcpu); 567ed95805eSJohn Baldwin #ifdef MP_WATCHDOG 568dfe7b3bfSKonstantin Belousov ap_watchdog(PCPU_GET(cpuid)); 569dfe7b3bfSKonstantin Belousov #endif 570ed95805eSJohn Baldwin 571dfe7b3bfSKonstantin Belousov /* If we are busy - try to use fast methods. */ 572dfe7b3bfSKonstantin Belousov if (busy) { 573dfe7b3bfSKonstantin Belousov if ((cpu_feature2 & CPUID2_MON) && idle_mwait) { 574dfe7b3bfSKonstantin Belousov cpu_idle_mwait(busy); 575dfe7b3bfSKonstantin Belousov goto out; 576dfe7b3bfSKonstantin Belousov } 577dfe7b3bfSKonstantin Belousov } 578dfe7b3bfSKonstantin Belousov 579dfe7b3bfSKonstantin Belousov /* If we have time - switch timers into idle mode. */ 580dfe7b3bfSKonstantin Belousov if (!busy) { 581dfe7b3bfSKonstantin Belousov critical_enter(); 582dfe7b3bfSKonstantin Belousov sbt = cpu_idleclock(); 583dfe7b3bfSKonstantin Belousov } 584dfe7b3bfSKonstantin Belousov 585dfe7b3bfSKonstantin Belousov /* Apply AMD APIC timer C1E workaround. */ 586dfe7b3bfSKonstantin Belousov if (cpu_ident_amdc1e && cpu_disable_c3_sleep) { 587dfe7b3bfSKonstantin Belousov msr = rdmsr(MSR_AMDK8_IPM); 588dfe7b3bfSKonstantin Belousov if (msr & AMDK8_CMPHALT) 589dfe7b3bfSKonstantin Belousov wrmsr(MSR_AMDK8_IPM, msr & ~AMDK8_CMPHALT); 590dfe7b3bfSKonstantin Belousov } 591dfe7b3bfSKonstantin Belousov 592dfe7b3bfSKonstantin Belousov /* Call main idle method. */ 593dfe7b3bfSKonstantin Belousov cpu_idle_fn(sbt); 594dfe7b3bfSKonstantin Belousov 595dfe7b3bfSKonstantin Belousov /* Switch timers back into active mode. */ 596dfe7b3bfSKonstantin Belousov if (!busy) { 597dfe7b3bfSKonstantin Belousov cpu_activeclock(); 598dfe7b3bfSKonstantin Belousov critical_exit(); 599dfe7b3bfSKonstantin Belousov } 600dfe7b3bfSKonstantin Belousov out: 601dfe7b3bfSKonstantin Belousov CTR2(KTR_SPARE2, "cpu_idle(%d) at %d done", 602dfe7b3bfSKonstantin Belousov busy, curcpu); 603dfe7b3bfSKonstantin Belousov } 604dfe7b3bfSKonstantin Belousov 6053f3937b4SKonstantin Belousov static int cpu_idle_apl31_workaround; 6063f3937b4SKonstantin Belousov SYSCTL_INT(_machdep, OID_AUTO, idle_apl31, CTLFLAG_RW, 6073f3937b4SKonstantin Belousov &cpu_idle_apl31_workaround, 0, 608160be7ccSKonstantin Belousov "Apollo Lake APL31 MWAIT bug workaround"); 6093f3937b4SKonstantin Belousov 610dfe7b3bfSKonstantin Belousov int 611dfe7b3bfSKonstantin Belousov cpu_idle_wakeup(int cpu) 612dfe7b3bfSKonstantin Belousov { 61383dc49beSConrad Meyer struct monitorbuf *mb; 614dfe7b3bfSKonstantin Belousov int *state; 615dfe7b3bfSKonstantin Belousov 61683dc49beSConrad Meyer mb = &pcpu_find(cpu)->pc_monitorbuf; 61783dc49beSConrad Meyer state = &mb->idle_state; 618a5bd21d0SKonstantin Belousov switch (atomic_load_int(state)) { 619a5bd21d0SKonstantin Belousov case STATE_SLEEPING: 620dfe7b3bfSKonstantin Belousov return (0); 621a5bd21d0SKonstantin Belousov case STATE_MWAIT: 622a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_RUNNING); 6233f3937b4SKonstantin Belousov return (cpu_idle_apl31_workaround ? 0 : 1); 624a5bd21d0SKonstantin Belousov case STATE_RUNNING: 625a5bd21d0SKonstantin Belousov return (1); 626a5bd21d0SKonstantin Belousov default: 627a5bd21d0SKonstantin Belousov panic("bad monitor state"); 628a5bd21d0SKonstantin Belousov return (1); 629a5bd21d0SKonstantin Belousov } 630dfe7b3bfSKonstantin Belousov } 631dfe7b3bfSKonstantin Belousov 632dfe7b3bfSKonstantin Belousov /* 633dfe7b3bfSKonstantin Belousov * Ordered by speed/power consumption. 634dfe7b3bfSKonstantin Belousov */ 635a5f472c5SKonstantin Belousov static struct { 636dfe7b3bfSKonstantin Belousov void *id_fn; 637dfe7b3bfSKonstantin Belousov char *id_name; 638a5f472c5SKonstantin Belousov int id_cpuid2_flag; 639dfe7b3bfSKonstantin Belousov } idle_tbl[] = { 640a5f472c5SKonstantin Belousov { .id_fn = cpu_idle_spin, .id_name = "spin" }, 641a5f472c5SKonstantin Belousov { .id_fn = cpu_idle_mwait, .id_name = "mwait", 642a5f472c5SKonstantin Belousov .id_cpuid2_flag = CPUID2_MON }, 643a5f472c5SKonstantin Belousov { .id_fn = cpu_idle_hlt, .id_name = "hlt" }, 644a5f472c5SKonstantin Belousov { .id_fn = cpu_idle_acpi, .id_name = "acpi" }, 645dfe7b3bfSKonstantin Belousov }; 646dfe7b3bfSKonstantin Belousov 647dfe7b3bfSKonstantin Belousov static int 648dfe7b3bfSKonstantin Belousov idle_sysctl_available(SYSCTL_HANDLER_ARGS) 649dfe7b3bfSKonstantin Belousov { 650dfe7b3bfSKonstantin Belousov char *avail, *p; 651dfe7b3bfSKonstantin Belousov int error; 652dfe7b3bfSKonstantin Belousov int i; 653dfe7b3bfSKonstantin Belousov 654dfe7b3bfSKonstantin Belousov avail = malloc(256, M_TEMP, M_WAITOK); 655dfe7b3bfSKonstantin Belousov p = avail; 656a5f472c5SKonstantin Belousov for (i = 0; i < nitems(idle_tbl); i++) { 657a5f472c5SKonstantin Belousov if (idle_tbl[i].id_cpuid2_flag != 0 && 658a5f472c5SKonstantin Belousov (cpu_feature2 & idle_tbl[i].id_cpuid2_flag) == 0) 659dfe7b3bfSKonstantin Belousov continue; 660dfe7b3bfSKonstantin Belousov if (strcmp(idle_tbl[i].id_name, "acpi") == 0 && 661dfe7b3bfSKonstantin Belousov cpu_idle_hook == NULL) 662dfe7b3bfSKonstantin Belousov continue; 663dfe7b3bfSKonstantin Belousov p += sprintf(p, "%s%s", p != avail ? ", " : "", 664dfe7b3bfSKonstantin Belousov idle_tbl[i].id_name); 665dfe7b3bfSKonstantin Belousov } 666dfe7b3bfSKonstantin Belousov error = sysctl_handle_string(oidp, avail, 0, req); 667dfe7b3bfSKonstantin Belousov free(avail, M_TEMP); 668dfe7b3bfSKonstantin Belousov return (error); 669dfe7b3bfSKonstantin Belousov } 670dfe7b3bfSKonstantin Belousov 671dfe7b3bfSKonstantin Belousov SYSCTL_PROC(_machdep, OID_AUTO, idle_available, CTLTYPE_STRING | CTLFLAG_RD, 672dfe7b3bfSKonstantin Belousov 0, 0, idle_sysctl_available, "A", "list of available idle functions"); 673dfe7b3bfSKonstantin Belousov 67455ba21d4SKonstantin Belousov static bool 675a5f472c5SKonstantin Belousov cpu_idle_selector(const char *new_idle_name) 67655ba21d4SKonstantin Belousov { 67755ba21d4SKonstantin Belousov int i; 67855ba21d4SKonstantin Belousov 679a5f472c5SKonstantin Belousov for (i = 0; i < nitems(idle_tbl); i++) { 680a5f472c5SKonstantin Belousov if (idle_tbl[i].id_cpuid2_flag != 0 && 681a5f472c5SKonstantin Belousov (cpu_feature2 & idle_tbl[i].id_cpuid2_flag) == 0) 68255ba21d4SKonstantin Belousov continue; 68355ba21d4SKonstantin Belousov if (strcmp(idle_tbl[i].id_name, "acpi") == 0 && 68455ba21d4SKonstantin Belousov cpu_idle_hook == NULL) 68555ba21d4SKonstantin Belousov continue; 68655ba21d4SKonstantin Belousov if (strcmp(idle_tbl[i].id_name, new_idle_name)) 68755ba21d4SKonstantin Belousov continue; 68855ba21d4SKonstantin Belousov cpu_idle_fn = idle_tbl[i].id_fn; 68955ba21d4SKonstantin Belousov if (bootverbose) 69055ba21d4SKonstantin Belousov printf("CPU idle set to %s\n", idle_tbl[i].id_name); 69155ba21d4SKonstantin Belousov return (true); 69255ba21d4SKonstantin Belousov } 69355ba21d4SKonstantin Belousov return (false); 69455ba21d4SKonstantin Belousov } 69555ba21d4SKonstantin Belousov 696dfe7b3bfSKonstantin Belousov static int 697a5f472c5SKonstantin Belousov cpu_idle_sysctl(SYSCTL_HANDLER_ARGS) 698dfe7b3bfSKonstantin Belousov { 69955ba21d4SKonstantin Belousov char buf[16], *p; 70055ba21d4SKonstantin Belousov int error, i; 701dfe7b3bfSKonstantin Belousov 702dfe7b3bfSKonstantin Belousov p = "unknown"; 703a5f472c5SKonstantin Belousov for (i = 0; i < nitems(idle_tbl); i++) { 704dfe7b3bfSKonstantin Belousov if (idle_tbl[i].id_fn == cpu_idle_fn) { 705dfe7b3bfSKonstantin Belousov p = idle_tbl[i].id_name; 706dfe7b3bfSKonstantin Belousov break; 707dfe7b3bfSKonstantin Belousov } 708dfe7b3bfSKonstantin Belousov } 709dfe7b3bfSKonstantin Belousov strncpy(buf, p, sizeof(buf)); 710dfe7b3bfSKonstantin Belousov error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 711dfe7b3bfSKonstantin Belousov if (error != 0 || req->newptr == NULL) 712dfe7b3bfSKonstantin Belousov return (error); 713a5f472c5SKonstantin Belousov return (cpu_idle_selector(buf) ? 0 : EINVAL); 714dfe7b3bfSKonstantin Belousov } 715dfe7b3bfSKonstantin Belousov 716dfe7b3bfSKonstantin Belousov SYSCTL_PROC(_machdep, OID_AUTO, idle, CTLTYPE_STRING | CTLFLAG_RW, 0, 0, 717a5f472c5SKonstantin Belousov cpu_idle_sysctl, "A", "currently selected idle function"); 718835c2787SKonstantin Belousov 71955ba21d4SKonstantin Belousov static void 720a5f472c5SKonstantin Belousov cpu_idle_tun(void *unused __unused) 72155ba21d4SKonstantin Belousov { 72255ba21d4SKonstantin Belousov char tunvar[16]; 72355ba21d4SKonstantin Belousov 72455ba21d4SKonstantin Belousov if (TUNABLE_STR_FETCH("machdep.idle", tunvar, sizeof(tunvar))) 725a5f472c5SKonstantin Belousov cpu_idle_selector(tunvar); 72645ed991dSKonstantin Belousov else if (cpu_vendor_id == CPU_VENDOR_AMD && 72745ed991dSKonstantin Belousov CPUID_TO_FAMILY(cpu_id) == 0x17 && CPUID_TO_MODEL(cpu_id) == 0x1) { 72845ed991dSKonstantin Belousov /* Ryzen erratas 1057, 1109. */ 72945ed991dSKonstantin Belousov cpu_idle_selector("hlt"); 73045ed991dSKonstantin Belousov idle_mwait = 0; 731665919aaSConrad Meyer mwait_cpustop_broken = true; 73245ed991dSKonstantin Belousov } 73345ed991dSKonstantin Belousov 7343f3937b4SKonstantin Belousov if (cpu_vendor_id == CPU_VENDOR_INTEL && cpu_id == 0x506c9) { 7353f3937b4SKonstantin Belousov /* 736160be7ccSKonstantin Belousov * Apollo Lake errata APL31 (public errata APL30). 737160be7ccSKonstantin Belousov * Stores to the armed address range may not trigger 738160be7ccSKonstantin Belousov * MWAIT to resume execution. OS needs to use 739160be7ccSKonstantin Belousov * interrupts to wake processors from MWAIT-induced 740160be7ccSKonstantin Belousov * sleep states. 7413f3937b4SKonstantin Belousov */ 7423f3937b4SKonstantin Belousov cpu_idle_apl31_workaround = 1; 743665919aaSConrad Meyer mwait_cpustop_broken = true; 7443f3937b4SKonstantin Belousov } 7453f3937b4SKonstantin Belousov TUNABLE_INT_FETCH("machdep.idle_apl31", &cpu_idle_apl31_workaround); 74655ba21d4SKonstantin Belousov } 747a5f472c5SKonstantin Belousov SYSINIT(cpu_idle_tun, SI_SUB_CPU, SI_ORDER_MIDDLE, cpu_idle_tun, NULL); 74855ba21d4SKonstantin Belousov 749295f4b6cSKonstantin Belousov static int panic_on_nmi = 1; 750295f4b6cSKonstantin Belousov SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RWTUN, 751295f4b6cSKonstantin Belousov &panic_on_nmi, 0, 752413ed27cSAndriy Gapon "Panic on NMI raised by hardware failure"); 753835c2787SKonstantin Belousov int nmi_is_broadcast = 1; 754835c2787SKonstantin Belousov SYSCTL_INT(_machdep, OID_AUTO, nmi_is_broadcast, CTLFLAG_RWTUN, 755835c2787SKonstantin Belousov &nmi_is_broadcast, 0, 756835c2787SKonstantin Belousov "Chipset NMI is broadcast"); 757835c2787SKonstantin Belousov #ifdef KDB 758835c2787SKonstantin Belousov int kdb_on_nmi = 1; 759835c2787SKonstantin Belousov SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RWTUN, 760835c2787SKonstantin Belousov &kdb_on_nmi, 0, 761413ed27cSAndriy Gapon "Go to KDB on NMI with unknown source"); 762835c2787SKonstantin Belousov #endif 763835c2787SKonstantin Belousov 764295f4b6cSKonstantin Belousov void 765295f4b6cSKonstantin Belousov nmi_call_kdb(u_int cpu, u_int type, struct trapframe *frame) 766835c2787SKonstantin Belousov { 7670fb3a72aSAndriy Gapon bool claimed = false; 768835c2787SKonstantin Belousov 7690fb3a72aSAndriy Gapon #ifdef DEV_ISA 770835c2787SKonstantin Belousov /* machine/parity/power fail/"kitchen sink" faults */ 7710fb3a72aSAndriy Gapon if (isa_nmi(frame->tf_err)) { 7720fb3a72aSAndriy Gapon claimed = true; 7730fb3a72aSAndriy Gapon if (panic_on_nmi) 7740fb3a72aSAndriy Gapon panic("NMI indicates hardware failure"); 7750fb3a72aSAndriy Gapon } 7760fb3a72aSAndriy Gapon #endif /* DEV_ISA */ 777835c2787SKonstantin Belousov #ifdef KDB 7780fb3a72aSAndriy Gapon if (!claimed && kdb_on_nmi) { 779835c2787SKonstantin Belousov /* 780835c2787SKonstantin Belousov * NMI can be hooked up to a pushbutton for debugging. 781835c2787SKonstantin Belousov */ 782835c2787SKonstantin Belousov printf("NMI/cpu%d ... going to debugger\n", cpu); 783835c2787SKonstantin Belousov kdb_trap(type, 0, frame); 784835c2787SKonstantin Belousov } 785835c2787SKonstantin Belousov #endif /* KDB */ 786295f4b6cSKonstantin Belousov } 787835c2787SKonstantin Belousov 788295f4b6cSKonstantin Belousov void 789295f4b6cSKonstantin Belousov nmi_handle_intr(u_int type, struct trapframe *frame) 790835c2787SKonstantin Belousov { 791835c2787SKonstantin Belousov 792835c2787SKonstantin Belousov #ifdef SMP 793295f4b6cSKonstantin Belousov if (nmi_is_broadcast) { 794295f4b6cSKonstantin Belousov nmi_call_kdb_smp(type, frame); 795295f4b6cSKonstantin Belousov return; 796295f4b6cSKonstantin Belousov } 797835c2787SKonstantin Belousov #endif 7981d6dfd12SKonstantin Belousov nmi_call_kdb(PCPU_GET(cpuid), type, frame); 799835c2787SKonstantin Belousov } 800319117fdSKonstantin Belousov 801319117fdSKonstantin Belousov int hw_ibrs_active; 802319117fdSKonstantin Belousov int hw_ibrs_disable = 1; 803319117fdSKonstantin Belousov 804319117fdSKonstantin Belousov SYSCTL_INT(_hw, OID_AUTO, ibrs_active, CTLFLAG_RD, &hw_ibrs_active, 0, 805b31b965eSKonstantin Belousov "Indirect Branch Restricted Speculation active"); 806319117fdSKonstantin Belousov 807319117fdSKonstantin Belousov void 808319117fdSKonstantin Belousov hw_ibrs_recalculate(void) 809319117fdSKonstantin Belousov { 810319117fdSKonstantin Belousov uint64_t v; 811319117fdSKonstantin Belousov 812319117fdSKonstantin Belousov if ((cpu_ia32_arch_caps & IA32_ARCH_CAP_IBRS_ALL) != 0) { 813319117fdSKonstantin Belousov if (hw_ibrs_disable) { 814319117fdSKonstantin Belousov v = rdmsr(MSR_IA32_SPEC_CTRL); 815c688c905SKonstantin Belousov v &= ~(uint64_t)IA32_SPEC_CTRL_IBRS; 816319117fdSKonstantin Belousov wrmsr(MSR_IA32_SPEC_CTRL, v); 817319117fdSKonstantin Belousov } else { 818319117fdSKonstantin Belousov v = rdmsr(MSR_IA32_SPEC_CTRL); 819319117fdSKonstantin Belousov v |= IA32_SPEC_CTRL_IBRS; 820319117fdSKonstantin Belousov wrmsr(MSR_IA32_SPEC_CTRL, v); 821319117fdSKonstantin Belousov } 822319117fdSKonstantin Belousov return; 823319117fdSKonstantin Belousov } 824319117fdSKonstantin Belousov hw_ibrs_active = (cpu_stdext_feature3 & CPUID_STDEXT3_IBPB) != 0 && 825319117fdSKonstantin Belousov !hw_ibrs_disable; 826319117fdSKonstantin Belousov } 827319117fdSKonstantin Belousov 828319117fdSKonstantin Belousov static int 829319117fdSKonstantin Belousov hw_ibrs_disable_handler(SYSCTL_HANDLER_ARGS) 830319117fdSKonstantin Belousov { 831319117fdSKonstantin Belousov int error, val; 832319117fdSKonstantin Belousov 833319117fdSKonstantin Belousov val = hw_ibrs_disable; 834319117fdSKonstantin Belousov error = sysctl_handle_int(oidp, &val, 0, req); 835319117fdSKonstantin Belousov if (error != 0 || req->newptr == NULL) 836319117fdSKonstantin Belousov return (error); 837319117fdSKonstantin Belousov hw_ibrs_disable = val != 0; 838319117fdSKonstantin Belousov hw_ibrs_recalculate(); 839319117fdSKonstantin Belousov return (0); 840319117fdSKonstantin Belousov } 841319117fdSKonstantin Belousov SYSCTL_PROC(_hw, OID_AUTO, ibrs_disable, CTLTYPE_INT | CTLFLAG_RWTUN | 842319117fdSKonstantin Belousov CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0, hw_ibrs_disable_handler, "I", 843b31b965eSKonstantin Belousov "Disable Indirect Branch Restricted Speculation"); 8448fbcc334SKonstantin Belousov 8453621ba1eSKonstantin Belousov int hw_ssb_active; 8463621ba1eSKonstantin Belousov int hw_ssb_disable; 8473621ba1eSKonstantin Belousov 8483621ba1eSKonstantin Belousov SYSCTL_INT(_hw, OID_AUTO, spec_store_bypass_disable_active, CTLFLAG_RD, 8493621ba1eSKonstantin Belousov &hw_ssb_active, 0, 8503621ba1eSKonstantin Belousov "Speculative Store Bypass Disable active"); 8513621ba1eSKonstantin Belousov 8523621ba1eSKonstantin Belousov static void 8533621ba1eSKonstantin Belousov hw_ssb_set_one(bool enable) 8543621ba1eSKonstantin Belousov { 8553621ba1eSKonstantin Belousov uint64_t v; 8563621ba1eSKonstantin Belousov 8573621ba1eSKonstantin Belousov v = rdmsr(MSR_IA32_SPEC_CTRL); 8583621ba1eSKonstantin Belousov if (enable) 8593621ba1eSKonstantin Belousov v |= (uint64_t)IA32_SPEC_CTRL_SSBD; 8603621ba1eSKonstantin Belousov else 8613621ba1eSKonstantin Belousov v &= ~(uint64_t)IA32_SPEC_CTRL_SSBD; 8623621ba1eSKonstantin Belousov wrmsr(MSR_IA32_SPEC_CTRL, v); 8633621ba1eSKonstantin Belousov } 8643621ba1eSKonstantin Belousov 8653621ba1eSKonstantin Belousov static void 8663621ba1eSKonstantin Belousov hw_ssb_set(bool enable, bool for_all_cpus) 8673621ba1eSKonstantin Belousov { 8683621ba1eSKonstantin Belousov struct thread *td; 8693621ba1eSKonstantin Belousov int bound_cpu, i, is_bound; 8703621ba1eSKonstantin Belousov 8713621ba1eSKonstantin Belousov if ((cpu_stdext_feature3 & CPUID_STDEXT3_SSBD) == 0) { 8723621ba1eSKonstantin Belousov hw_ssb_active = 0; 8733621ba1eSKonstantin Belousov return; 8743621ba1eSKonstantin Belousov } 8753621ba1eSKonstantin Belousov hw_ssb_active = enable; 8763621ba1eSKonstantin Belousov if (for_all_cpus) { 8773621ba1eSKonstantin Belousov td = curthread; 8783621ba1eSKonstantin Belousov thread_lock(td); 8793621ba1eSKonstantin Belousov is_bound = sched_is_bound(td); 8803621ba1eSKonstantin Belousov bound_cpu = td->td_oncpu; 8813621ba1eSKonstantin Belousov CPU_FOREACH(i) { 8823621ba1eSKonstantin Belousov sched_bind(td, i); 8833621ba1eSKonstantin Belousov hw_ssb_set_one(enable); 8843621ba1eSKonstantin Belousov } 8853621ba1eSKonstantin Belousov if (is_bound) 8863621ba1eSKonstantin Belousov sched_bind(td, bound_cpu); 8873621ba1eSKonstantin Belousov else 8883621ba1eSKonstantin Belousov sched_unbind(td); 8893621ba1eSKonstantin Belousov thread_unlock(td); 8903621ba1eSKonstantin Belousov } else { 8913621ba1eSKonstantin Belousov hw_ssb_set_one(enable); 8923621ba1eSKonstantin Belousov } 8933621ba1eSKonstantin Belousov } 8943621ba1eSKonstantin Belousov 8953621ba1eSKonstantin Belousov void 8963621ba1eSKonstantin Belousov hw_ssb_recalculate(bool all_cpus) 8973621ba1eSKonstantin Belousov { 8983621ba1eSKonstantin Belousov 8993621ba1eSKonstantin Belousov switch (hw_ssb_disable) { 9003621ba1eSKonstantin Belousov default: 9013621ba1eSKonstantin Belousov hw_ssb_disable = 0; 9023621ba1eSKonstantin Belousov /* FALLTHROUGH */ 9033621ba1eSKonstantin Belousov case 0: /* off */ 9043621ba1eSKonstantin Belousov hw_ssb_set(false, all_cpus); 9053621ba1eSKonstantin Belousov break; 9063621ba1eSKonstantin Belousov case 1: /* on */ 9073621ba1eSKonstantin Belousov hw_ssb_set(true, all_cpus); 9083621ba1eSKonstantin Belousov break; 9093621ba1eSKonstantin Belousov case 2: /* auto */ 91023437573SKonstantin Belousov hw_ssb_set((cpu_ia32_arch_caps & IA32_ARCH_CAP_SSB_NO) != 0 ? 9113621ba1eSKonstantin Belousov false : true, all_cpus); 9123621ba1eSKonstantin Belousov break; 9133621ba1eSKonstantin Belousov } 9143621ba1eSKonstantin Belousov } 9153621ba1eSKonstantin Belousov 9163621ba1eSKonstantin Belousov static int 9173621ba1eSKonstantin Belousov hw_ssb_disable_handler(SYSCTL_HANDLER_ARGS) 9183621ba1eSKonstantin Belousov { 9193621ba1eSKonstantin Belousov int error, val; 9203621ba1eSKonstantin Belousov 9213621ba1eSKonstantin Belousov val = hw_ssb_disable; 9223621ba1eSKonstantin Belousov error = sysctl_handle_int(oidp, &val, 0, req); 9233621ba1eSKonstantin Belousov if (error != 0 || req->newptr == NULL) 9243621ba1eSKonstantin Belousov return (error); 9253621ba1eSKonstantin Belousov hw_ssb_disable = val; 9263621ba1eSKonstantin Belousov hw_ssb_recalculate(true); 9273621ba1eSKonstantin Belousov return (0); 9283621ba1eSKonstantin Belousov } 9293621ba1eSKonstantin Belousov SYSCTL_PROC(_hw, OID_AUTO, spec_store_bypass_disable, CTLTYPE_INT | 9303621ba1eSKonstantin Belousov CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0, 9313621ba1eSKonstantin Belousov hw_ssb_disable_handler, "I", 9323621ba1eSKonstantin Belousov "Speculative Store Bypass Disable (0 - off, 1 - on, 2 - auto"); 9333621ba1eSKonstantin Belousov 934*7355a02bSKonstantin Belousov int hw_mds_disable; 935*7355a02bSKonstantin Belousov 936*7355a02bSKonstantin Belousov /* 937*7355a02bSKonstantin Belousov * Handler for Microarchitectural Data Sampling issues. Really not a 938*7355a02bSKonstantin Belousov * pointer to C function: on amd64 the code must not change any CPU 939*7355a02bSKonstantin Belousov * architectural state except possibly %rflags. Also, it is always 940*7355a02bSKonstantin Belousov * called with interrupts disabled. 941*7355a02bSKonstantin Belousov */ 942*7355a02bSKonstantin Belousov void (*mds_handler)(void); 943*7355a02bSKonstantin Belousov void mds_handler_void(void); 944*7355a02bSKonstantin Belousov void mds_handler_verw(void); 945*7355a02bSKonstantin Belousov void mds_handler_ivb(void); 946*7355a02bSKonstantin Belousov void mds_handler_bdw(void); 947*7355a02bSKonstantin Belousov void mds_handler_skl_sse(void); 948*7355a02bSKonstantin Belousov void mds_handler_skl_avx(void); 949*7355a02bSKonstantin Belousov void mds_handler_skl_avx512(void); 950*7355a02bSKonstantin Belousov void mds_handler_silvermont(void); 951*7355a02bSKonstantin Belousov 952*7355a02bSKonstantin Belousov static int 953*7355a02bSKonstantin Belousov sysctl_hw_mds_disable_state_handler(SYSCTL_HANDLER_ARGS) 954*7355a02bSKonstantin Belousov { 955*7355a02bSKonstantin Belousov const char *state; 956*7355a02bSKonstantin Belousov 957*7355a02bSKonstantin Belousov if (mds_handler == mds_handler_void) 958*7355a02bSKonstantin Belousov state = "inactive"; 959*7355a02bSKonstantin Belousov else if (mds_handler == mds_handler_verw) 960*7355a02bSKonstantin Belousov state = "VERW"; 961*7355a02bSKonstantin Belousov else if (mds_handler == mds_handler_ivb) 962*7355a02bSKonstantin Belousov state = "software IvyBridge"; 963*7355a02bSKonstantin Belousov else if (mds_handler == mds_handler_bdw) 964*7355a02bSKonstantin Belousov state = "software Broadwell"; 965*7355a02bSKonstantin Belousov else if (mds_handler == mds_handler_skl_sse) 966*7355a02bSKonstantin Belousov state = "software Skylake SSE"; 967*7355a02bSKonstantin Belousov else if (mds_handler == mds_handler_skl_avx) 968*7355a02bSKonstantin Belousov state = "software Skylake AVX"; 969*7355a02bSKonstantin Belousov else if (mds_handler == mds_handler_skl_avx512) 970*7355a02bSKonstantin Belousov state = "software Skylake AVX512"; 971*7355a02bSKonstantin Belousov else if (mds_handler == mds_handler_silvermont) 972*7355a02bSKonstantin Belousov state = "software Silvermont"; 973*7355a02bSKonstantin Belousov else 974*7355a02bSKonstantin Belousov state = "unknown"; 975*7355a02bSKonstantin Belousov return (SYSCTL_OUT(req, state, strlen(state))); 976*7355a02bSKonstantin Belousov } 977*7355a02bSKonstantin Belousov 978*7355a02bSKonstantin Belousov SYSCTL_PROC(_hw, OID_AUTO, mds_disable_state, 979*7355a02bSKonstantin Belousov CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 980*7355a02bSKonstantin Belousov sysctl_hw_mds_disable_state_handler, "A", 981*7355a02bSKonstantin Belousov "Microarchitectural Data Sampling Mitigation state"); 982*7355a02bSKonstantin Belousov 983*7355a02bSKonstantin Belousov _Static_assert(__offsetof(struct pcpu, pc_mds_tmp) % 64 == 0, "MDS AVX512"); 984*7355a02bSKonstantin Belousov 985*7355a02bSKonstantin Belousov void 986*7355a02bSKonstantin Belousov hw_mds_recalculate(void) 987*7355a02bSKonstantin Belousov { 988*7355a02bSKonstantin Belousov struct pcpu *pc; 989*7355a02bSKonstantin Belousov vm_offset_t b64; 990*7355a02bSKonstantin Belousov u_long xcr0; 991*7355a02bSKonstantin Belousov int i; 992*7355a02bSKonstantin Belousov 993*7355a02bSKonstantin Belousov /* 994*7355a02bSKonstantin Belousov * Allow user to force VERW variant even if MD_CLEAR is not 995*7355a02bSKonstantin Belousov * reported. For instance, hypervisor might unknowingly 996*7355a02bSKonstantin Belousov * filter the cap out. 997*7355a02bSKonstantin Belousov * For the similar reasons, and for testing, allow to enable 998*7355a02bSKonstantin Belousov * mitigation even for RDCL_NO or MDS_NO caps. 999*7355a02bSKonstantin Belousov */ 1000*7355a02bSKonstantin Belousov if (cpu_vendor_id != CPU_VENDOR_INTEL || hw_mds_disable == 0 || 1001*7355a02bSKonstantin Belousov ((cpu_ia32_arch_caps & (IA32_ARCH_CAP_RDCL_NO | 1002*7355a02bSKonstantin Belousov IA32_ARCH_CAP_MDS_NO)) != 0 && hw_mds_disable == 3)) { 1003*7355a02bSKonstantin Belousov mds_handler = mds_handler_void; 1004*7355a02bSKonstantin Belousov } else if (((cpu_stdext_feature3 & CPUID_STDEXT3_MD_CLEAR) != 0 && 1005*7355a02bSKonstantin Belousov hw_mds_disable == 3) || hw_mds_disable == 1) { 1006*7355a02bSKonstantin Belousov mds_handler = mds_handler_verw; 1007*7355a02bSKonstantin Belousov } else if (CPUID_TO_FAMILY(cpu_id) == 0x6 && 1008*7355a02bSKonstantin Belousov (CPUID_TO_MODEL(cpu_id) == 0x2e || CPUID_TO_MODEL(cpu_id) == 0x1e || 1009*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x1f || CPUID_TO_MODEL(cpu_id) == 0x1a || 1010*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x2f || CPUID_TO_MODEL(cpu_id) == 0x25 || 1011*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x2c || CPUID_TO_MODEL(cpu_id) == 0x2d || 1012*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x2a || CPUID_TO_MODEL(cpu_id) == 0x3e || 1013*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x3a) && 1014*7355a02bSKonstantin Belousov (hw_mds_disable == 2 || hw_mds_disable == 3)) { 1015*7355a02bSKonstantin Belousov /* 1016*7355a02bSKonstantin Belousov * Nehalem, SandyBridge, IvyBridge 1017*7355a02bSKonstantin Belousov */ 1018*7355a02bSKonstantin Belousov CPU_FOREACH(i) { 1019*7355a02bSKonstantin Belousov pc = pcpu_find(i); 1020*7355a02bSKonstantin Belousov if (pc->pc_mds_buf == NULL) { 1021*7355a02bSKonstantin Belousov pc->pc_mds_buf = malloc_domainset(672, M_TEMP, 1022*7355a02bSKonstantin Belousov DOMAINSET_PREF(pc->pc_domain), M_WAITOK); 1023*7355a02bSKonstantin Belousov bzero(pc->pc_mds_buf, 16); 1024*7355a02bSKonstantin Belousov } 1025*7355a02bSKonstantin Belousov } 1026*7355a02bSKonstantin Belousov mds_handler = mds_handler_ivb; 1027*7355a02bSKonstantin Belousov } else if (CPUID_TO_FAMILY(cpu_id) == 0x6 && 1028*7355a02bSKonstantin Belousov (CPUID_TO_MODEL(cpu_id) == 0x3f || CPUID_TO_MODEL(cpu_id) == 0x3c || 1029*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x45 || CPUID_TO_MODEL(cpu_id) == 0x46 || 1030*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x56 || CPUID_TO_MODEL(cpu_id) == 0x4f || 1031*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x47 || CPUID_TO_MODEL(cpu_id) == 0x3d) && 1032*7355a02bSKonstantin Belousov (hw_mds_disable == 2 || hw_mds_disable == 3)) { 1033*7355a02bSKonstantin Belousov /* 1034*7355a02bSKonstantin Belousov * Haswell, Broadwell 1035*7355a02bSKonstantin Belousov */ 1036*7355a02bSKonstantin Belousov CPU_FOREACH(i) { 1037*7355a02bSKonstantin Belousov pc = pcpu_find(i); 1038*7355a02bSKonstantin Belousov if (pc->pc_mds_buf == NULL) { 1039*7355a02bSKonstantin Belousov pc->pc_mds_buf = malloc_domainset(1536, M_TEMP, 1040*7355a02bSKonstantin Belousov DOMAINSET_PREF(pc->pc_domain), M_WAITOK); 1041*7355a02bSKonstantin Belousov bzero(pc->pc_mds_buf, 16); 1042*7355a02bSKonstantin Belousov } 1043*7355a02bSKonstantin Belousov } 1044*7355a02bSKonstantin Belousov mds_handler = mds_handler_bdw; 1045*7355a02bSKonstantin Belousov } else if (CPUID_TO_FAMILY(cpu_id) == 0x6 && 1046*7355a02bSKonstantin Belousov ((CPUID_TO_MODEL(cpu_id) == 0x55 && (cpu_id & 1047*7355a02bSKonstantin Belousov CPUID_STEPPING) <= 5) || 1048*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x4e || CPUID_TO_MODEL(cpu_id) == 0x5e || 1049*7355a02bSKonstantin Belousov (CPUID_TO_MODEL(cpu_id) == 0x8e && (cpu_id & 1050*7355a02bSKonstantin Belousov CPUID_STEPPING) <= 0xb) || 1051*7355a02bSKonstantin Belousov (CPUID_TO_MODEL(cpu_id) == 0x9e && (cpu_id & 1052*7355a02bSKonstantin Belousov CPUID_STEPPING) <= 0xc)) && 1053*7355a02bSKonstantin Belousov (hw_mds_disable == 2 || hw_mds_disable == 3)) { 1054*7355a02bSKonstantin Belousov /* 1055*7355a02bSKonstantin Belousov * Skylake, KabyLake, CoffeeLake, WhiskeyLake, 1056*7355a02bSKonstantin Belousov * CascadeLake 1057*7355a02bSKonstantin Belousov */ 1058*7355a02bSKonstantin Belousov CPU_FOREACH(i) { 1059*7355a02bSKonstantin Belousov pc = pcpu_find(i); 1060*7355a02bSKonstantin Belousov if (pc->pc_mds_buf == NULL) { 1061*7355a02bSKonstantin Belousov pc->pc_mds_buf = malloc_domainset(6 * 1024, 1062*7355a02bSKonstantin Belousov M_TEMP, DOMAINSET_PREF(pc->pc_domain), 1063*7355a02bSKonstantin Belousov M_WAITOK); 1064*7355a02bSKonstantin Belousov b64 = (vm_offset_t)malloc_domainset(64 + 63, 1065*7355a02bSKonstantin Belousov M_TEMP, DOMAINSET_PREF(pc->pc_domain), 1066*7355a02bSKonstantin Belousov M_WAITOK); 1067*7355a02bSKonstantin Belousov pc->pc_mds_buf64 = (void *)roundup2(b64, 64); 1068*7355a02bSKonstantin Belousov bzero(pc->pc_mds_buf64, 64); 1069*7355a02bSKonstantin Belousov } 1070*7355a02bSKonstantin Belousov } 1071*7355a02bSKonstantin Belousov xcr0 = rxcr(0); 1072*7355a02bSKonstantin Belousov if ((xcr0 & XFEATURE_ENABLED_ZMM_HI256) != 0 && 1073*7355a02bSKonstantin Belousov (cpu_stdext_feature2 & CPUID_STDEXT_AVX512DQ) != 0) 1074*7355a02bSKonstantin Belousov mds_handler = mds_handler_skl_avx512; 1075*7355a02bSKonstantin Belousov else if ((xcr0 & XFEATURE_ENABLED_AVX) != 0 && 1076*7355a02bSKonstantin Belousov (cpu_feature2 & CPUID2_AVX) != 0) 1077*7355a02bSKonstantin Belousov mds_handler = mds_handler_skl_avx; 1078*7355a02bSKonstantin Belousov else 1079*7355a02bSKonstantin Belousov mds_handler = mds_handler_skl_sse; 1080*7355a02bSKonstantin Belousov } else if (CPUID_TO_FAMILY(cpu_id) == 0x6 && 1081*7355a02bSKonstantin Belousov ((CPUID_TO_MODEL(cpu_id) == 0x37 || 1082*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x4a || 1083*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x4c || 1084*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x4d || 1085*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x5a || 1086*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x5d || 1087*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x6e || 1088*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x65 || 1089*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x75 || 1090*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x1c || 1091*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x26 || 1092*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x27 || 1093*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x35 || 1094*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x36 || 1095*7355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x7a))) { 1096*7355a02bSKonstantin Belousov /* Silvermont, Airmont */ 1097*7355a02bSKonstantin Belousov CPU_FOREACH(i) { 1098*7355a02bSKonstantin Belousov pc = pcpu_find(i); 1099*7355a02bSKonstantin Belousov if (pc->pc_mds_buf == NULL) 1100*7355a02bSKonstantin Belousov pc->pc_mds_buf = malloc(256, M_TEMP, M_WAITOK); 1101*7355a02bSKonstantin Belousov } 1102*7355a02bSKonstantin Belousov mds_handler = mds_handler_silvermont; 1103*7355a02bSKonstantin Belousov } else { 1104*7355a02bSKonstantin Belousov hw_mds_disable = 0; 1105*7355a02bSKonstantin Belousov mds_handler = mds_handler_void; 1106*7355a02bSKonstantin Belousov } 1107*7355a02bSKonstantin Belousov } 1108*7355a02bSKonstantin Belousov 1109*7355a02bSKonstantin Belousov static int 1110*7355a02bSKonstantin Belousov sysctl_mds_disable_handler(SYSCTL_HANDLER_ARGS) 1111*7355a02bSKonstantin Belousov { 1112*7355a02bSKonstantin Belousov int error, val; 1113*7355a02bSKonstantin Belousov 1114*7355a02bSKonstantin Belousov val = hw_mds_disable; 1115*7355a02bSKonstantin Belousov error = sysctl_handle_int(oidp, &val, 0, req); 1116*7355a02bSKonstantin Belousov if (error != 0 || req->newptr == NULL) 1117*7355a02bSKonstantin Belousov return (error); 1118*7355a02bSKonstantin Belousov if (val < 0 || val > 3) 1119*7355a02bSKonstantin Belousov return (EINVAL); 1120*7355a02bSKonstantin Belousov hw_mds_disable = val; 1121*7355a02bSKonstantin Belousov hw_mds_recalculate(); 1122*7355a02bSKonstantin Belousov return (0); 1123*7355a02bSKonstantin Belousov } 1124*7355a02bSKonstantin Belousov 1125*7355a02bSKonstantin Belousov SYSCTL_PROC(_hw, OID_AUTO, mds_disable, CTLTYPE_INT | 1126*7355a02bSKonstantin Belousov CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0, 1127*7355a02bSKonstantin Belousov sysctl_mds_disable_handler, "I", 1128*7355a02bSKonstantin Belousov "Microarchitectural Data Sampling Mitigation " 1129*7355a02bSKonstantin Belousov "(0 - off, 1 - on VERW, 2 - on SW, 3 - on AUTO"); 1130*7355a02bSKonstantin Belousov 11318fbcc334SKonstantin Belousov /* 11328fbcc334SKonstantin Belousov * Enable and restore kernel text write permissions. 11338fbcc334SKonstantin Belousov * Callers must ensure that disable_wp()/restore_wp() are executed 11348fbcc334SKonstantin Belousov * without rescheduling on the same core. 11358fbcc334SKonstantin Belousov */ 11368fbcc334SKonstantin Belousov bool 11378fbcc334SKonstantin Belousov disable_wp(void) 11388fbcc334SKonstantin Belousov { 11398fbcc334SKonstantin Belousov u_int cr0; 11408fbcc334SKonstantin Belousov 11418fbcc334SKonstantin Belousov cr0 = rcr0(); 11428fbcc334SKonstantin Belousov if ((cr0 & CR0_WP) == 0) 11438fbcc334SKonstantin Belousov return (false); 11448fbcc334SKonstantin Belousov load_cr0(cr0 & ~CR0_WP); 11458fbcc334SKonstantin Belousov return (true); 11468fbcc334SKonstantin Belousov } 11478fbcc334SKonstantin Belousov 11488fbcc334SKonstantin Belousov void 11498fbcc334SKonstantin Belousov restore_wp(bool old_wp) 11508fbcc334SKonstantin Belousov { 11518fbcc334SKonstantin Belousov 11528fbcc334SKonstantin Belousov if (old_wp) 11538fbcc334SKonstantin Belousov load_cr0(rcr0() | CR0_WP); 11548fbcc334SKonstantin Belousov } 11558fbcc334SKonstantin Belousov 11567705dd4dSKonstantin Belousov bool 11577705dd4dSKonstantin Belousov acpi_get_fadt_bootflags(uint16_t *flagsp) 11587705dd4dSKonstantin Belousov { 11597705dd4dSKonstantin Belousov #ifdef DEV_ACPI 11607705dd4dSKonstantin Belousov ACPI_TABLE_FADT *fadt; 11617705dd4dSKonstantin Belousov vm_paddr_t physaddr; 11627705dd4dSKonstantin Belousov 11637705dd4dSKonstantin Belousov physaddr = acpi_find_table(ACPI_SIG_FADT); 11647705dd4dSKonstantin Belousov if (physaddr == 0) 11657705dd4dSKonstantin Belousov return (false); 11667705dd4dSKonstantin Belousov fadt = acpi_map_table(physaddr, ACPI_SIG_FADT); 11677705dd4dSKonstantin Belousov if (fadt == NULL) 11687705dd4dSKonstantin Belousov return (false); 11697705dd4dSKonstantin Belousov *flagsp = fadt->BootFlags; 11707705dd4dSKonstantin Belousov acpi_unmap_table(fadt); 11717705dd4dSKonstantin Belousov return (true); 11727705dd4dSKonstantin Belousov #else 11737705dd4dSKonstantin Belousov return (false); 11747705dd4dSKonstantin Belousov #endif 11757705dd4dSKonstantin Belousov } 1176