#include <sys/types.h>
#include <sys/thread.h>
#include <sys/cpuvar.h>
#include <sys/cpu.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/disp.h>
#include <sys/class.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/note.h>
#include <sys/asm_linkage.h>
#include <sys/x_call.h>
#include <sys/systm.h>
#include <sys/var.h>
#include <sys/vtrace.h>
#include <vm/hat.h>
#include <vm/as.h>
#include <vm/seg_kmem.h>
#include <vm/seg_kp.h>
#include <sys/segments.h>
#include <sys/kmem.h>
#include <sys/stack.h>
#include <sys/smp_impldefs.h>
#include <sys/x86_archext.h>
#include <sys/machsystm.h>
#include <sys/traptrace.h>
#include <sys/clock.h>
#include <sys/cpc_impl.h>
#include <sys/pg.h>
#include <sys/cmt.h>
#include <sys/dtrace.h>
#include <sys/archsystm.h>
#include <sys/fp.h>
#include <sys/reboot.h>
#include <sys/kdi_machimpl.h>
#include <vm/hat_i86.h>
#include <vm/vm_dep.h>
#include <sys/memnode.h>
#include <sys/pci_cfgspace.h>
#include <sys/mach_mmu.h>
#include <sys/sysmacros.h>
#if defined(__xpv)
#include <sys/hypervisor.h>
#else
#include <sys/hma.h>
#endif
#include <sys/cpu_module.h>
#include <sys/ontrap.h>
struct cpu cpus[1] __aligned(MMU_PAGESIZE);
struct cpu *cpu[NCPU] = {&cpus[0]};
struct cpu *cpu_free_list;
cpu_core_t cpu_core[NCPU];
#define cpu_next_free cpu_prev
int use_mp = 1;
cpuset_t mp_cpus;
int flushes_require_xcalls;
cpuset_t cpu_ready_set;
static void mp_startup_boot(void);
static void mp_startup_hotplug(void);
static void cpu_sep_enable(void);
static void cpu_sep_disable(void);
static void cpu_asysc_enable(void);
static void cpu_asysc_disable(void);
void
init_cpu_info(struct cpu *cp)
{
processor_info_t *pi = &cp->cpu_type_info;
pi->pi_clock = cpu_freq;
cp->cpu_curr_clock = cpu_freq_hz;
if (cp->cpu_supp_freqs == NULL) {
cpu_set_supp_freqs(cp, NULL);
}
(void) strcpy(pi->pi_processor_type, "i386");
if (fpu_exists)
(void) strcpy(pi->pi_fputypes, "i387 compatible");
cp->cpu_idstr = kmem_zalloc(CPU_IDSTRLEN, KM_SLEEP);
cp->cpu_brandstr = kmem_zalloc(CPU_IDSTRLEN, KM_SLEEP);
(void) cpuid_getidstr(CPU, cp->cpu_idstr, CPU_IDSTRLEN);
(void) cpuid_getbrandstr(CPU, cp->cpu_brandstr, CPU_IDSTRLEN);
}
void
init_cpu_syscall(struct cpu *cp)
{
kpreempt_disable();
if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
is_x86_feature(x86_featureset, X86FSET_ASYSC)) {
uint64_t flags;
#if !defined(__xpv)
CTASSERT(KDS_SEL == KCS_SEL + 8);
CTASSERT(UDS_SEL == U32CS_SEL + 8);
CTASSERT(UCS_SEL == U32CS_SEL + 16);
#endif
cpu_asysc_enable();
wrmsr(MSR_AMD_STAR,
((uint64_t)(U32CS_SEL << 16 | KCS_SEL)) << 32);
if (kpti_enable == 1) {
wrmsr(MSR_AMD_LSTAR,
(uint64_t)(uintptr_t)tr_sys_syscall);
wrmsr(MSR_AMD_CSTAR,
(uint64_t)(uintptr_t)tr_sys_syscall32);
} else {
wrmsr(MSR_AMD_LSTAR,
(uint64_t)(uintptr_t)sys_syscall);
wrmsr(MSR_AMD_CSTAR,
(uint64_t)(uintptr_t)sys_syscall32);
}
flags = PS_IE | PS_T;
if (is_x86_feature(x86_featureset, X86FSET_SMAP) == B_TRUE)
flags |= PS_ACHK;
wrmsr(MSR_AMD_SFMASK, flags);
}
if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
is_x86_feature(x86_featureset, X86FSET_SEP)) {
#if !defined(__xpv)
CTASSERT(KDS_SEL == KCS_SEL + 8);
CTASSERT(U32CS_SEL == ((KCS_SEL + 16) | 3));
CTASSERT(UDS_SEL == U32CS_SEL + 8);
#endif
cpu_sep_enable();
wrmsr(MSR_INTC_SEP_ESP, 0);
if (kpti_enable == 1) {
wrmsr(MSR_INTC_SEP_EIP,
(uint64_t)(uintptr_t)tr_sys_sysenter);
} else {
wrmsr(MSR_INTC_SEP_EIP,
(uint64_t)(uintptr_t)sys_sysenter);
}
}
kpreempt_enable();
}
#if !defined(__xpv)
static void
init_cpu_id_gdt(struct cpu *cp)
{
set_usegd(&cp->cpu_gdt[GDT_CPUID], SDP_SHORT, NULL, cp->cpu_id,
SDT_MEMRODA, SEL_UPL, SDP_BYTES, SDP_OP32);
}
#endif
static struct cpu *
mp_cpu_configure_common(int cpun, boolean_t boot)
{
struct cpu *cp;
kthread_id_t tp;
caddr_t sp;
proc_t *procp;
#if !defined(__xpv)
extern int idle_cpu_prefer_mwait;
extern void cpu_idle_mwait();
#endif
extern void idle();
extern void cpu_idle();
#ifdef TRAPTRACE
trap_trace_ctl_t *ttc = &trap_trace_ctl[cpun];
#endif
ASSERT(MUTEX_HELD(&cpu_lock));
ASSERT(cpun < NCPU && cpu[cpun] == NULL);
if (cpu_free_list == NULL) {
cp = kmem_zalloc(sizeof (*cp), KM_SLEEP);
} else {
cp = cpu_free_list;
cpu_free_list = cp->cpu_next_free;
}
cp->cpu_m.mcpu_istamp = cpun << 16;
procp = &p0;
disp_cpu_init(cp);
cpu_vm_data_init(cp);
tp = thread_create(NULL, 0, NULL, NULL, 0, procp,
TS_STOPPED, maxclsyspri);
THREAD_ONPROC(tp, cp);
tp->t_preempt = 1;
tp->t_bound_cpu = cp;
tp->t_affinitycnt = 1;
tp->t_cpu = cp;
tp->t_disp_queue = cp->cpu_disp;
sp = tp->t_stk;
tp->t_sp = (uintptr_t)(sp - MINFRAME);
tp->t_sp -= STACK_ENTRY_ALIGN;
if (boot) {
tp->t_pc = (uintptr_t)mp_startup_boot;
} else {
tp->t_pc = (uintptr_t)mp_startup_hotplug;
}
cp->cpu_id = cpun;
cp->cpu_self = cp;
cp->cpu_thread = tp;
cp->cpu_lwp = NULL;
cp->cpu_dispthread = tp;
cp->cpu_dispatch_pri = DISP_PRIO(tp);
cp->cpu_base_spl = ipltospl(LOCK_LEVEL);
tp = thread_create(NULL, PAGESIZE, idle, NULL, 0, procp, TS_ONPROC, -1);
cp->cpu_idle_thread = tp;
tp->t_preempt = 1;
tp->t_bound_cpu = cp;
tp->t_affinitycnt = 1;
tp->t_cpu = cp;
tp->t_disp_queue = cp->cpu_disp;
pg_cpu_bootstrap(cp);
kcpc_hw_init(cp);
setup_vaddr_for_ppcopy(cp);
#if !defined(__lint)
ASSERT((sizeof (*cp->cpu_gdt) * NGDT) <= PAGESIZE);
#endif
cp->cpu_gdt = kmem_zalloc(PAGESIZE, KM_SLEEP);
bcopy(CPU->cpu_gdt, cp->cpu_gdt, (sizeof (*cp->cpu_gdt) * NGDT));
cp->cpu_m.mcpu_ldt = kmem_zalloc(LDT_CPU_SIZE, KM_SLEEP);
cp->cpu_m.mcpu_ldt_len = 0;
#if !defined(__lint)
ASSERT((sizeof (*CPU->cpu_idt) * NIDT) <= PAGESIZE);
#endif
cp->cpu_idt = kmem_alloc(PAGESIZE, KM_SLEEP);
bcopy(CPU->cpu_idt, cp->cpu_idt, PAGESIZE);
cpuid_alloc_space(cp);
#if !defined(__xpv)
if (is_x86_feature(x86_featureset, X86FSET_MWAIT) &&
idle_cpu_prefer_mwait) {
cp->cpu_m.mcpu_mwait = cpuid_mwait_alloc(cp);
cp->cpu_m.mcpu_idle_cpu = cpu_idle_mwait;
} else
#endif
cp->cpu_m.mcpu_idle_cpu = cpu_idle;
init_cpu_info(cp);
#if !defined(__xpv)
init_cpu_id_gdt(cp);
#endif
ucode_alloc_space(cp);
xc_init_cpu(cp);
hat_cpu_online(cp);
#ifdef TRAPTRACE
ttc->ttc_first = (uintptr_t)kmem_zalloc(trap_trace_bufsize, KM_SLEEP);
ttc->ttc_next = ttc->ttc_first;
ttc->ttc_limit = ttc->ttc_first + trap_trace_bufsize;
#endif
cpu_intr_alloc(cp, NINTR_THREADS);
cp->cpu_flags = CPU_OFFLINE | CPU_QUIESCED | CPU_POWEROFF;
cpu_set_state(cp);
cpu_add_unit(cp);
return (cp);
}
static void
mp_cpu_unconfigure_common(struct cpu *cp, int error)
{
ASSERT(MUTEX_HELD(&cpu_lock));
cpu_del_unit(cp->cpu_id);
if (error == ETIMEDOUT) {
cp->cpu_flags = 0;
return;
}
cpu_destroy_bound_threads(cp);
cp->cpu_idle_thread = NULL;
segkp_release(segkp,
cp->cpu_intr_stack - (INTR_STACK_SIZE - SA(MINFRAME)));
cp->cpu_intr_stack = NULL;
#ifdef TRAPTRACE
{
trap_trace_ctl_t *ttc = &trap_trace_ctl[cp->cpu_id];
kmem_free((void *)ttc->ttc_first, trap_trace_bufsize);
ttc->ttc_first = (uintptr_t)NULL;
}
#endif
hat_cpu_offline(cp);
ucode_free_space(cp);
if (cp->cpu_idstr) {
kmem_free(cp->cpu_idstr, CPU_IDSTRLEN);
cp->cpu_idstr = NULL;
}
if (cp->cpu_brandstr) {
kmem_free(cp->cpu_brandstr, CPU_IDSTRLEN);
cp->cpu_brandstr = NULL;
}
#if !defined(__xpv)
if (cp->cpu_m.mcpu_mwait != NULL) {
cpuid_mwait_free(cp);
cp->cpu_m.mcpu_mwait = NULL;
}
#endif
cpuid_free_space(cp);
if (cp->cpu_idt != CPU->cpu_idt)
kmem_free(cp->cpu_idt, PAGESIZE);
cp->cpu_idt = NULL;
kmem_free(cp->cpu_m.mcpu_ldt, LDT_CPU_SIZE);
cp->cpu_m.mcpu_ldt = NULL;
cp->cpu_m.mcpu_ldt_len = 0;
kmem_free(cp->cpu_gdt, PAGESIZE);
cp->cpu_gdt = NULL;
if (cp->cpu_supp_freqs != NULL) {
size_t len = strlen(cp->cpu_supp_freqs) + 1;
kmem_free(cp->cpu_supp_freqs, len);
cp->cpu_supp_freqs = NULL;
}
teardown_vaddr_for_ppcopy(cp);
kcpc_hw_fini(cp);
cp->cpu_dispthread = NULL;
cp->cpu_thread = NULL;
cpu_vm_data_destroy(cp);
xc_fini_cpu(cp);
disp_cpu_fini(cp);
ASSERT(cp != CPU0);
bzero(cp, sizeof (*cp));
cp->cpu_next_free = cpu_free_list;
cpu_free_list = cp;
}
#if defined(OPTERON_ERRATUM_88)
int opteron_erratum_88;
#endif
#if defined(OPTERON_ERRATUM_91)
int opteron_erratum_91;
#endif
#if defined(OPTERON_ERRATUM_93)
int opteron_erratum_93;
#endif
#if defined(OPTERON_ERRATUM_95)
int opteron_erratum_95;
#endif
#if defined(OPTERON_ERRATUM_100)
int opteron_erratum_100;
#endif
#if defined(OPTERON_ERRATUM_108)
int opteron_erratum_108;
#endif
#if defined(OPTERON_ERRATUM_109)
int opteron_erratum_109;
#endif
#if defined(OPTERON_ERRATUM_121)
int opteron_erratum_121;
#endif
#if defined(OPTERON_ERRATUM_122)
int opteron_erratum_122;
#endif
#if defined(OPTERON_ERRATUM_123)
int opteron_erratum_123;
#endif
#if defined(OPTERON_ERRATUM_131)
int opteron_erratum_131;
#endif
#if defined(OPTERON_WORKAROUND_6336786)
int opteron_workaround_6336786;
int opteron_workaround_6336786_UP = 0;
#endif
#if defined(OPTERON_ERRATUM_147)
int opteron_erratum_147;
#endif
#if defined(OPTERON_ERRATUM_298)
int opteron_erratum_298;
#endif
#if defined(OPTERON_ERRATUM_721)
int opteron_erratum_721;
#endif
static void
workaround_warning(cpu_t *cp, uint_t erratum)
{
cmn_err(CE_WARN, "cpu%d: no workaround for erratum %u",
cp->cpu_id, erratum);
}
static void
workaround_applied(uint_t erratum)
{
if (erratum > 1000000)
cmn_err(CE_CONT, "?workaround applied for cpu issue #%d\n",
erratum);
else
cmn_err(CE_CONT, "?workaround applied for cpu erratum #%d\n",
erratum);
}
static void
msr_warning(cpu_t *cp, const char *rw, uint_t msr, int error)
{
cmn_err(CE_WARN, "cpu%d: couldn't %smsr 0x%x, error %d",
cp->cpu_id, rw, msr, error);
}
static uint_t
opteron_get_nnodes(void)
{
static uint_t nnodes = 0;
if (nnodes == 0) {
#ifdef DEBUG
uint_t family;
family = cpuid_getfamily(CPU);
ASSERT(family == 0xf || family == 0x10 || family == 0x11);
#endif
nnodes = ((pci_getl_func(0, 24, 0, 0x60) & 0x70) >> 4) + 1;
}
return (nnodes);
}
uint_t
do_erratum_298(struct cpu *cpu)
{
static int osvwrc = -3;
extern int osvw_opteron_erratum(cpu_t *, uint_t);
if (osvwrc == -3) {
osvwrc = osvw_opteron_erratum(cpu, 298);
} else {
ASSERT(osvwrc == osvw_opteron_erratum(cpu, 298));
}
switch (osvwrc) {
case 0:
break;
case 1:
if (((rdmsr(MSR_AMD_HWCR) & AMD_HWCR_TLBCACHEDIS) == 0) ||
((rdmsr(MSR_AMD_BU_CFG) & AMD_BU_CFG_E298) == 0)) {
#if defined(OPTERON_ERRATUM_298)
opteron_erratum_298++;
#else
workaround_warning(cpu, 298);
return (1);
#endif
}
break;
case -1:
if ((cpuid_opteron_erratum(cpu, 298) > 0) &&
(((rdmsr(MSR_AMD_HWCR) & AMD_HWCR_TLBCACHEDIS) == 0) ||
((rdmsr(MSR_AMD_BU_CFG) & AMD_BU_CFG_E298) == 0))) {
#if defined(OPTERON_ERRATUM_298)
opteron_erratum_298++;
#else
workaround_warning(cpu, 298);
return (1);
#endif
}
break;
}
return (0);
}
uint_t
workaround_errata(struct cpu *cpu)
{
volatile uint_t missing = 0;
ASSERT(cpu == CPU);
if (cpuid_opteron_erratum(cpu, 88) > 0) {
#if defined(OPTERON_ERRATUM_88)
opteron_erratum_88++;
#else
workaround_warning(cpu, 88);
missing++;
#endif
}
if (cpuid_opteron_erratum(cpu, 91) > 0) {
#if defined(OPTERON_ERRATUM_91)
opteron_erratum_91++;
#else
workaround_warning(cpu, 91);
missing++;
#endif
}
if (cpuid_opteron_erratum(cpu, 93) > 0) {
#if defined(OPTERON_ERRATUM_93)
opteron_erratum_93++;
#else
workaround_warning(cpu, 93);
missing++;
#endif
}
if (cpuid_opteron_erratum(cpu, 95) > 0) {
#if defined(OPTERON_ERRATUM_95)
#if defined(_LP64)
if (_userlimit32 > 0xc0000000ul)
*(uintptr_t *)&_userlimit32 = 0xc0000000ul;
ASSERT((uint32_t)COREHEAP_BASE == 0xc0000000u);
opteron_erratum_95++;
#endif
#else
workaround_warning(cpu, 95);
missing++;
#endif
}
if (cpuid_opteron_erratum(cpu, 100) > 0) {
#if defined(OPTERON_ERRATUM_100)
opteron_erratum_100++;
#else
workaround_warning(cpu, 100);
missing++;
#endif
}
if (cpuid_opteron_erratum(cpu, 108) > 0) {
#if defined(OPTERON_ERRATUM_108)
#else
workaround_warning(cpu, 108);
missing++;
#endif
}
if (cpuid_opteron_erratum(cpu, 109) > 0) do {
#if defined(OPTERON_ERRATUM_109)
uint64_t value;
const uint_t msr = MSR_AMD_PATCHLEVEL;
int err;
if ((err = checked_rdmsr(msr, &value)) != 0) {
msr_warning(cpu, "rd", msr, err);
workaround_warning(cpu, 109);
missing++;
}
if (value == 0)
opteron_erratum_109++;
#else
workaround_warning(cpu, 109);
missing++;
#endif
} while (0);
if (cpuid_opteron_erratum(cpu, 121) > 0) {
#if defined(OPTERON_ERRATUM_121)
#if defined(_LP64)
if (opteron_erratum_121)
opteron_erratum_121++;
else {
if (hole_start) {
hole_start -= PAGESIZE;
} else {
hole_start = PAGESIZE;
}
opteron_erratum_121++;
}
#endif
#else
workaround_warning(cpu, 121);
missing++;
#endif
}
if (cpuid_opteron_erratum(cpu, 122) > 0) do {
#if defined(OPTERON_ERRATUM_122)
uint64_t value;
const uint_t msr = MSR_AMD_HWCR;
int error;
#if defined(__xpv)
if (!DOMAIN_IS_INITDOMAIN(xen_info))
break;
if (!opteron_erratum_122 && xpv_nr_phys_cpus() == 1)
break;
#else
if (!opteron_erratum_122 && opteron_get_nnodes() == 1 &&
cpuid_get_ncpu_per_chip(cpu) == 1)
break;
#endif
if ((error = checked_rdmsr(msr, &value)) != 0) {
msr_warning(cpu, "rd", msr, error);
workaround_warning(cpu, 122);
missing++;
} else {
value |= (uint64_t)AMD_HWCR_FFDIS;
if ((error = checked_wrmsr(msr, value)) != 0) {
msr_warning(cpu, "wr", msr, error);
workaround_warning(cpu, 122);
missing++;
}
}
opteron_erratum_122++;
#else
workaround_warning(cpu, 122);
missing++;
#endif
} while (0);
if (cpuid_opteron_erratum(cpu, 123) > 0) do {
#if defined(OPTERON_ERRATUM_123)
uint64_t value;
const uint_t msr = MSR_AMD_PATCHLEVEL;
int err;
if (cpuid_get_ncpu_per_chip(cpu) < 2)
break;
#if defined(__xpv)
if (!DOMAIN_IS_INITDOMAIN(xen_info))
break;
#endif
if ((err = checked_rdmsr(msr, &value)) != 0) {
msr_warning(cpu, "rd", msr, err);
workaround_warning(cpu, 123);
missing++;
}
if (value == 0)
opteron_erratum_123++;
#else
workaround_warning(cpu, 123);
missing++;
#endif
} while (0);
if (cpuid_opteron_erratum(cpu, 131) > 0) do {
#if defined(OPTERON_ERRATUM_131)
uint64_t nbcfg;
const uint_t msr = MSR_AMD_NB_CFG;
const uint64_t wabits =
AMD_NB_CFG_SRQ_HEARTBEAT | AMD_NB_CFG_SRQ_SPR;
int error;
if (opteron_erratum_131)
break;
#if defined(__xpv)
if (!DOMAIN_IS_INITDOMAIN(xen_info))
break;
if (xpv_nr_phys_cpus() < 4)
break;
#else
if (opteron_get_nnodes() * cpuid_get_ncpu_per_chip(cpu) < 4)
break;
#endif
if ((error = checked_rdmsr(msr, &nbcfg)) != 0) {
msr_warning(cpu, "rd", msr, error);
workaround_warning(cpu, 131);
missing++;
} else if ((nbcfg & wabits) == 0) {
opteron_erratum_131++;
} else {
ASSERT((nbcfg & wabits) != wabits);
}
#else
workaround_warning(cpu, 131);
missing++;
#endif
} while (0);
if (cpuid_opteron_erratum(cpu, 6336786) > 0) {
#if defined(OPTERON_WORKAROUND_6336786)
if (opteron_workaround_6336786) {
opteron_workaround_6336786++;
#if defined(__xpv)
} else if ((DOMAIN_IS_INITDOMAIN(xen_info) &&
xpv_nr_phys_cpus() > 1) ||
opteron_workaround_6336786_UP) {
opteron_workaround_6336786++;
workaround_warning(cpu, 6336786);
#else
} else if ((opteron_get_nnodes() *
cpuid_get_ncpu_per_chip(cpu) > 1) ||
opteron_workaround_6336786_UP) {
uint_t node, nnodes;
uint8_t data;
nnodes = opteron_get_nnodes();
for (node = 0; node < nnodes; node++) {
data = pci_getb_func(0, node + 24, 3, 0x87);
data &= 0xFC;
pci_putb_func(0, node + 24, 3, 0x87, data);
}
opteron_workaround_6336786++;
#endif
}
#else
workaround_warning(cpu, 6336786);
missing++;
#endif
}
if (cpuid_opteron_erratum(cpu, 147) > 0) {
#if defined(OPTERON_ERRATUM_147)
if (opteron_erratum_147) {
opteron_erratum_147++;
#if defined(__xpv)
} else if (is_x86_feature(x86_featureset, X86FSET_SSE2)) {
if (DOMAIN_IS_INITDOMAIN(xen_info)) {
if (xpv_nr_phys_cpus() > 1)
opteron_erratum_147++;
} else {
opteron_erratum_147++;
}
#else
} else if (is_x86_feature(x86_featureset, X86FSET_SSE2) &&
((opteron_get_nnodes() *
cpuid_get_ncpu_per_chip(cpu)) > 1)) {
if ((xrdmsr(MSR_AMD_BU_CFG) & (UINT64_C(1) << 33)) == 0)
opteron_erratum_147++;
#endif
}
#else
workaround_warning(cpu, 147);
missing++;
#endif
}
missing += do_erratum_298(cpu);
if (cpuid_opteron_erratum(cpu, 721) > 0) {
#if defined(OPTERON_ERRATUM_721)
on_trap_data_t otd;
if (!on_trap(&otd, OT_DATA_ACCESS))
wrmsr(MSR_AMD_DE_CFG,
rdmsr(MSR_AMD_DE_CFG) | AMD_DE_CFG_E721);
no_trap();
opteron_erratum_721++;
#else
workaround_warning(cpu, 721);
missing++;
#endif
}
#ifdef __xpv
return (0);
#else
return (missing);
#endif
}
void
workaround_errata_end()
{
#if defined(OPTERON_ERRATUM_88)
if (opteron_erratum_88)
workaround_applied(88);
#endif
#if defined(OPTERON_ERRATUM_91)
if (opteron_erratum_91)
workaround_applied(91);
#endif
#if defined(OPTERON_ERRATUM_93)
if (opteron_erratum_93)
workaround_applied(93);
#endif
#if defined(OPTERON_ERRATUM_95)
if (opteron_erratum_95)
workaround_applied(95);
#endif
#if defined(OPTERON_ERRATUM_100)
if (opteron_erratum_100)
workaround_applied(100);
#endif
#if defined(OPTERON_ERRATUM_108)
if (opteron_erratum_108)
workaround_applied(108);
#endif
#if defined(OPTERON_ERRATUM_109)
if (opteron_erratum_109) {
cmn_err(CE_WARN,
"BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
" processor\nerratum 109 was not detected; updating your"
" system's BIOS to a version\ncontaining this"
" microcode patch is HIGHLY recommended or erroneous"
" system\noperation may occur.\n");
}
#endif
#if defined(OPTERON_ERRATUM_121)
if (opteron_erratum_121)
workaround_applied(121);
#endif
#if defined(OPTERON_ERRATUM_122)
if (opteron_erratum_122)
workaround_applied(122);
#endif
#if defined(OPTERON_ERRATUM_123)
if (opteron_erratum_123) {
cmn_err(CE_WARN,
"BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
" processor\nerratum 123 was not detected; updating your"
" system's BIOS to a version\ncontaining this"
" microcode patch is HIGHLY recommended or erroneous"
" system\noperation may occur.\n");
}
#endif
#if defined(OPTERON_ERRATUM_131)
if (opteron_erratum_131) {
cmn_err(CE_WARN,
"BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
" processor\nerratum 131 was not detected; updating your"
" system's BIOS to a version\ncontaining this"
" microcode patch is HIGHLY recommended or erroneous"
" system\noperation may occur.\n");
}
#endif
#if defined(OPTERON_WORKAROUND_6336786)
if (opteron_workaround_6336786)
workaround_applied(6336786);
#endif
#if defined(OPTERON_ERRATUM_147)
if (opteron_erratum_147)
workaround_applied(147);
#endif
#if defined(OPTERON_ERRATUM_298)
if (opteron_erratum_298) {
cmn_err(CE_WARN,
"BIOS microcode patch for AMD 64/Opteron(tm)"
" processor\nerratum 298 was not detected; updating your"
" system's BIOS to a version\ncontaining this"
" microcode patch is HIGHLY recommended or erroneous"
" system\noperation may occur.\n");
}
#endif
#if defined(OPTERON_ERRATUM_721)
if (opteron_erratum_721)
workaround_applied(721);
#endif
}
static cpuset_t procset_slave, procset_master;
static void
mp_startup_wait(cpuset_t *sp, processorid_t cpuid)
{
cpuset_t tempset;
for (tempset = *sp; !CPU_IN_SET(tempset, cpuid);
tempset = *(volatile cpuset_t *)sp) {
SMT_PAUSE();
}
CPUSET_ATOMIC_DEL(*(cpuset_t *)sp, cpuid);
}
static void
mp_startup_signal(cpuset_t *sp, processorid_t cpuid)
{
cpuset_t tempset;
CPUSET_ATOMIC_ADD(*(cpuset_t *)sp, cpuid);
for (tempset = *sp; CPU_IN_SET(tempset, cpuid);
tempset = *(volatile cpuset_t *)sp) {
SMT_PAUSE();
}
}
int
mp_start_cpu_common(cpu_t *cp, boolean_t boot)
{
_NOTE(ARGUNUSED(boot));
void *ctx;
int delays;
int error = 0;
cpuset_t tempset;
processorid_t cpuid;
#ifndef __xpv
extern void cpupm_init(cpu_t *);
#endif
ASSERT(cp != NULL);
cpuid = cp->cpu_id;
ctx = mach_cpucontext_alloc(cp);
if (ctx == NULL) {
cmn_err(CE_WARN,
"cpu%d: failed to allocate context", cp->cpu_id);
return (EAGAIN);
}
error = mach_cpu_start(cp, ctx);
if (error != 0) {
cmn_err(CE_WARN,
"cpu%d: failed to start, error %d", cp->cpu_id, error);
mach_cpucontext_free(cp, ctx, error);
return (error);
}
for (delays = 0, tempset = procset_slave; !CPU_IN_SET(tempset, cpuid);
delays++) {
if (delays == 500) {
cmn_err(CE_NOTE, "cpu%d: started, "
"but not running in the kernel yet", cpuid);
} else if (delays > 2000) {
error = ETIMEDOUT;
cmn_err(CE_WARN, "cpu%d: timed out", cpuid);
mach_cpucontext_free(cp, ctx, error);
return (error);
}
delay(USEC_TO_TICK_ROUNDUP(10000));
tempset = *((volatile cpuset_t *)&procset_slave);
}
CPUSET_ATOMIC_DEL(procset_slave, cpuid);
mach_cpucontext_free(cp, ctx, 0);
#ifndef __xpv
if (tsc_gethrtime_enable)
tsc_sync_master(cpuid);
#endif
mp_startup_wait(&procset_slave, cpuid);
ucode_locate(cp);
mp_startup_signal(&procset_master, cpuid);
if (dtrace_cpu_init != NULL) {
(*dtrace_cpu_init)(cpuid);
}
mp_startup_wait(&procset_slave, cpuid);
#ifndef __xpv
cpupm_init(cp);
#endif
(void) pg_cpu_init(cp, B_FALSE);
cpu_set_state(cp);
mp_startup_signal(&procset_master, cpuid);
return (0);
}
int
start_cpu(processorid_t who)
{
cpu_t *cp;
int error = 0;
cpuset_t tempset;
ASSERT(who != 0);
if (kmem_avail() < 1024 * 1024) {
kmem_reap();
return (ENOMEM);
}
cp = mp_cpu_configure_common(who, B_TRUE);
ASSERT(cp != NULL);
error = mp_start_cpu_common(cp, B_TRUE);
if (error != 0) {
mp_cpu_unconfigure_common(cp, error);
return (error);
}
mutex_exit(&cpu_lock);
tempset = cpu_ready_set;
while (!CPU_IN_SET(tempset, who)) {
drv_usecwait(1);
tempset = *((volatile cpuset_t *)&cpu_ready_set);
}
mutex_enter(&cpu_lock);
return (0);
}
void
start_other_cpus(int cprboot)
{
_NOTE(ARGUNUSED(cprboot));
uint_t who;
uint_t bootcpuid = 0;
init_cpu_info(CPU);
#if !defined(__xpv)
init_cpu_id_gdt(CPU);
#endif
cmn_err(CE_CONT, "?cpu%d: %s\n", CPU->cpu_id, CPU->cpu_idstr);
cmn_err(CE_CONT, "?cpu%d: %s\n", CPU->cpu_id, CPU->cpu_brandstr);
cmn_err(CE_CONT, "?KPTI %s (PCID %s, INVPCID %s)\n",
kpti_enable ? "enabled" : "disabled",
x86_use_pcid == 1 ? "in use" :
(is_x86_feature(x86_featureset, X86FSET_PCID) ? "disabled" :
"not supported"),
x86_use_pcid == 1 && x86_use_invpcid == 1 ? "in use" :
(is_x86_feature(x86_featureset, X86FSET_INVPCID) ? "disabled" :
"not supported"));
init_cpu_syscall(CPU);
CPUSET_DEL(mp_cpus, bootcpuid);
CPUSET_ADD(cpu_ready_set, bootcpuid);
if ((CPUSET_ISNULL(mp_cpus) && plat_dr_support_cpu() == 0) ||
use_mp == 0) {
if (use_mp == 0)
cmn_err(CE_CONT, "?***** Not in MP mode\n");
goto done;
}
cpu_pause_init();
xc_init_cpu(CPU);
if (mach_cpucontext_init() != 0)
goto done;
flushes_require_xcalls = 1;
affinity_set(CPU_CURRENT);
for (who = 0; who < NCPU; who++) {
if (!CPU_IN_SET(mp_cpus, who))
continue;
ASSERT(who != bootcpuid);
mutex_enter(&cpu_lock);
if (start_cpu(who) != 0)
CPUSET_DEL(mp_cpus, who);
cpu_state_change_notify(who, CPU_SETUP);
mutex_exit(&cpu_lock);
}
ucode_cleanup();
affinity_clear();
mach_cpucontext_fini();
done:
if (get_hwenv() == HW_NATIVE)
workaround_errata_end();
cmi_post_mpstartup();
#if !defined(__xpv)
hma_init();
#endif
if (use_mp && ncpus != boot_max_ncpus) {
cmn_err(CE_NOTE,
"System detected %d cpus, but "
"only %d cpu(s) were enabled during boot.",
boot_max_ncpus, ncpus);
cmn_err(CE_NOTE,
"Use \"boot-ncpus\" parameter to enable more CPU(s). "
"See eeprom(8).");
}
}
int
mp_cpu_configure(int cpuid)
{
cpu_t *cp;
if (use_mp == 0 || plat_dr_support_cpu() == 0) {
return (ENOTSUP);
}
cp = cpu_get(cpuid);
if (cp != NULL) {
return (EALREADY);
}
if (kmem_avail() < 1024 * 1024) {
kmem_reap();
return (ENOMEM);
}
cp = mp_cpu_configure_common(cpuid, B_FALSE);
ASSERT(cp != NULL && cpu_get(cpuid) == cp);
return (cp != NULL ? 0 : EAGAIN);
}
int
mp_cpu_unconfigure(int cpuid)
{
cpu_t *cp;
if (use_mp == 0 || plat_dr_support_cpu() == 0) {
return (ENOTSUP);
} else if (cpuid < 0 || cpuid >= max_ncpus) {
return (EINVAL);
}
cp = cpu_get(cpuid);
if (cp == NULL) {
return (ENODEV);
}
mp_cpu_unconfigure_common(cp, 0);
return (0);
}
static void
mp_startup_common(boolean_t boot)
{
cpu_t *cp = CPU;
uchar_t new_x86_featureset[BT_SIZEOFMAP(NUM_X86_FEATURES)];
extern void cpu_event_init_cpu(cpu_t *);
bzero(new_x86_featureset, BT_SIZEOFMAP(NUM_X86_FEATURES));
cpuid_execpass(cp, CPUID_PASS_PRELUDE, new_x86_featureset);
cpuid_execpass(cp, CPUID_PASS_IDENT, NULL);
if (boot && get_hwenv() == HW_NATIVE &&
cpuid_getvendor(CPU) == X86_VENDOR_Intel &&
cpuid_getfamily(CPU) == 6 &&
(cpuid_getmodel(CPU) == 0x2d || cpuid_getmodel(CPU) == 0x3e) &&
is_x86_feature(x86_featureset, X86FSET_TSC)) {
(void) wrmsr(REG_TSC, 0UL);
}
mp_startup_signal(&procset_slave, cp->cpu_id);
#ifndef __xpv
if (tsc_gethrtime_enable)
tsc_sync_slave();
#endif
ucode_read_rev(cp);
mp_startup_signal(&procset_slave, cp->cpu_id);
mp_startup_wait(&procset_master, cp->cpu_id);
ucode_apply(cp);
cpuid_execpass(cp, CPUID_PASS_BASIC, new_x86_featureset);
(void) (*ap_mlsetup)();
#ifndef __xpv
pat_sync();
#endif
if (is_x86_feature(new_x86_featureset, X86FSET_TSCP))
(void) wrmsr(MSR_AMD_TSCAUX, cp->cpu_id);
init_cpu_syscall(cp);
splx(ipltospl(LOCK_LEVEL));
sti();
if (is_x86_feature(x86_featureset, X86FSET_MWAIT) !=
is_x86_feature(new_x86_featureset, X86FSET_MWAIT)) {
if (!is_x86_feature(x86_featureset, X86FSET_MWAIT)) {
remove_x86_feature(new_x86_featureset, X86FSET_MWAIT);
} else {
panic("unsupported mixed cpu mwait support detected");
}
}
if (workaround_errata(cp) != 0)
panic("critical workaround(s) missing for cpu%d", cp->cpu_id);
cp->cpu_flags &= ~(CPU_POWEROFF | CPU_QUIESCED);
enable_pcid();
if (fp_save_mech == FP_XSAVE) {
xsave_setup_msr(cp);
}
cpuid_execpass(cp, CPUID_PASS_EXTENDED, NULL);
cpuid_execpass(cp, CPUID_PASS_DYNAMIC, NULL);
cpuid_execpass(cp, CPUID_PASS_RESOLVE, NULL);
(void) cpuid_getidstr(cp, cp->cpu_idstr, CPU_IDSTRLEN);
(void) cpuid_getbrandstr(cp, cp->cpu_brandstr, CPU_IDSTRLEN);
cp->cpu_flags |= CPU_RUNNING | CPU_READY | CPU_EXISTS;
post_startup_cpu_fixups();
cpu_event_init_cpu(cp);
curthread->t_preempt = 0;
ASSERT(cp->cpu_base_spl == ipltospl(LOCK_LEVEL));
set_base_spl();
pghw_physid_create(cp);
mp_startup_signal(&procset_slave, cp->cpu_id);
mp_startup_wait(&procset_master, cp->cpu_id);
pg_cmt_cpu_startup(cp);
if (boot) {
mutex_enter(&cpu_lock);
cp->cpu_flags &= ~CPU_OFFLINE;
cpu_enable_intr(cp);
cpu_add_active(cp);
mutex_exit(&cpu_lock);
}
(void) spl0();
ucode_finish(cp);
if (compare_x86_featureset(x86_featureset, new_x86_featureset) ==
B_FALSE) {
cmn_err(CE_CONT, "cpu%d: featureset\n", cp->cpu_id);
print_x86_featureset(new_x86_featureset);
cmn_err(CE_WARN, "cpu%d feature mismatch", cp->cpu_id);
}
#ifndef __xpv
{
cmi_hdl_t hdl;
if ((hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL) {
if (is_x86_feature(x86_featureset, X86FSET_MCA))
cmi_mca_init(hdl);
cp->cpu_m.mcpu_cmi_hdl = hdl;
}
}
#endif
if (boothowto & RB_DEBUG)
kdi_cpu_init();
(void) mach_cpu_create_device_node(cp, NULL);
CPUSET_ATOMIC_ADD(cpu_ready_set, cp->cpu_id);
cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_idstr);
cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_brandstr);
cmn_err(CE_CONT, "?cpu%d initialization complete - online\n",
cp->cpu_id);
thread_exit();
}
static void
mp_startup_boot(void)
{
mp_startup_common(B_TRUE);
}
void
mp_startup_hotplug(void)
{
mp_startup_common(B_FALSE);
}
int
mp_cpu_start(struct cpu *cp)
{
ASSERT(MUTEX_HELD(&cpu_lock));
return (0);
}
int
mp_cpu_stop(struct cpu *cp)
{
extern int cbe_psm_timer_mode;
ASSERT(MUTEX_HELD(&cpu_lock));
#ifdef __xpv
if (cp->cpu_id == 0)
return (EBUSY);
#endif
if ((cbe_psm_timer_mode == TIMER_PERIODIC) && (cp->cpu_id == 0))
return (EBUSY);
return (0);
}
int
cpu_disable_intr(struct cpu *cp)
{
if (psm_disable_intr(cp->cpu_id) != DDI_SUCCESS)
return (EBUSY);
cp->cpu_flags &= ~CPU_ENABLE;
ncpus_intr_enabled--;
return (0);
}
void
cpu_enable_intr(struct cpu *cp)
{
ASSERT(MUTEX_HELD(&cpu_lock));
cp->cpu_flags |= CPU_ENABLE;
ncpus_intr_enabled++;
psm_enable_intr(cp->cpu_id);
}
void
mp_cpu_faulted_enter(struct cpu *cp)
{
#ifdef __xpv
_NOTE(ARGUNUSED(cp));
#else
cmi_hdl_t hdl = cp->cpu_m.mcpu_cmi_hdl;
if (hdl != NULL) {
cmi_hdl_hold(hdl);
} else {
hdl = cmi_hdl_lookup(CMI_HDL_NATIVE, cmi_ntv_hwchipid(cp),
cmi_ntv_hwcoreid(cp), cmi_ntv_hwstrandid(cp));
}
if (hdl != NULL) {
cmi_faulted_enter(hdl);
cmi_hdl_rele(hdl);
}
#endif
}
void
mp_cpu_faulted_exit(struct cpu *cp)
{
#ifdef __xpv
_NOTE(ARGUNUSED(cp));
#else
cmi_hdl_t hdl = cp->cpu_m.mcpu_cmi_hdl;
if (hdl != NULL) {
cmi_hdl_hold(hdl);
} else {
hdl = cmi_hdl_lookup(CMI_HDL_NATIVE, cmi_ntv_hwchipid(cp),
cmi_ntv_hwcoreid(cp), cmi_ntv_hwstrandid(cp));
}
if (hdl != NULL) {
cmi_faulted_exit(hdl);
cmi_hdl_rele(hdl);
}
#endif
}
void
cpu_fast_syscall_disable(void)
{
if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
is_x86_feature(x86_featureset, X86FSET_SEP))
cpu_sep_disable();
if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
is_x86_feature(x86_featureset, X86FSET_ASYSC))
cpu_asysc_disable();
}
void
cpu_fast_syscall_enable(void)
{
if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
is_x86_feature(x86_featureset, X86FSET_SEP))
cpu_sep_enable();
if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
is_x86_feature(x86_featureset, X86FSET_ASYSC))
cpu_asysc_enable();
}
static void
cpu_sep_enable(void)
{
ASSERT(is_x86_feature(x86_featureset, X86FSET_SEP));
ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
wrmsr(MSR_INTC_SEP_CS, (uint64_t)(uintptr_t)KCS_SEL);
CPU->cpu_m.mcpu_fast_syscall_state |= FSS_SEP_ENABLED;
}
static void
cpu_sep_disable(void)
{
ASSERT(is_x86_feature(x86_featureset, X86FSET_SEP));
ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
wrmsr(MSR_INTC_SEP_CS, 0);
CPU->cpu_m.mcpu_fast_syscall_state &= ~FSS_SEP_ENABLED;
}
static void
cpu_asysc_enable(void)
{
ASSERT(is_x86_feature(x86_featureset, X86FSET_ASYSC));
ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) |
(uint64_t)(uintptr_t)AMD_EFER_SCE);
CPU->cpu_m.mcpu_fast_syscall_state |= FSS_ASYSC_ENABLED;
}
static void
cpu_asysc_disable(void)
{
ASSERT(is_x86_feature(x86_featureset, X86FSET_ASYSC));
ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) &
~((uint64_t)(uintptr_t)AMD_EFER_SCE));
CPU->cpu_m.mcpu_fast_syscall_state &= ~FSS_ASYSC_ENABLED;
}