xref: /onnv-gate/usr/src/uts/i86pc/os/mp_startup.c (revision 11120:fe619717975a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51455Sandrei  * Common Development and Distribution License (the "License").
61455Sandrei  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
213446Smrj 
220Sstevel@tonic-gate /*
238803SJonathan.Haslam@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/thread.h>
290Sstevel@tonic-gate #include <sys/cpuvar.h>
300Sstevel@tonic-gate #include <sys/t_lock.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/proc.h>
330Sstevel@tonic-gate #include <sys/disp.h>
340Sstevel@tonic-gate #include <sys/class.h>
350Sstevel@tonic-gate #include <sys/cmn_err.h>
360Sstevel@tonic-gate #include <sys/debug.h>
370Sstevel@tonic-gate #include <sys/asm_linkage.h>
380Sstevel@tonic-gate #include <sys/x_call.h>
390Sstevel@tonic-gate #include <sys/systm.h>
400Sstevel@tonic-gate #include <sys/var.h>
410Sstevel@tonic-gate #include <sys/vtrace.h>
420Sstevel@tonic-gate #include <vm/hat.h>
430Sstevel@tonic-gate #include <vm/as.h>
440Sstevel@tonic-gate #include <vm/seg_kmem.h>
453446Smrj #include <vm/seg_kp.h>
460Sstevel@tonic-gate #include <sys/segments.h>
470Sstevel@tonic-gate #include <sys/kmem.h>
480Sstevel@tonic-gate #include <sys/stack.h>
490Sstevel@tonic-gate #include <sys/smp_impldefs.h>
500Sstevel@tonic-gate #include <sys/x86_archext.h>
510Sstevel@tonic-gate #include <sys/machsystm.h>
520Sstevel@tonic-gate #include <sys/traptrace.h>
530Sstevel@tonic-gate #include <sys/clock.h>
540Sstevel@tonic-gate #include <sys/cpc_impl.h>
553434Sesaxe #include <sys/pg.h>
563434Sesaxe #include <sys/cmt.h>
570Sstevel@tonic-gate #include <sys/dtrace.h>
580Sstevel@tonic-gate #include <sys/archsystm.h>
590Sstevel@tonic-gate #include <sys/fp.h>
600Sstevel@tonic-gate #include <sys/reboot.h>
613446Smrj #include <sys/kdi_machimpl.h>
620Sstevel@tonic-gate #include <vm/hat_i86.h>
630Sstevel@tonic-gate #include <sys/memnode.h>
64938Sesaxe #include <sys/pci_cfgspace.h>
653446Smrj #include <sys/mach_mmu.h>
663446Smrj #include <sys/sysmacros.h>
675084Sjohnlev #if defined(__xpv)
685084Sjohnlev #include <sys/hypervisor.h>
695084Sjohnlev #endif
701414Scindi #include <sys/cpu_module.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate struct cpu	cpus[1];			/* CPU data */
730Sstevel@tonic-gate struct cpu	*cpu[NCPU] = {&cpus[0]};	/* pointers to all CPUs */
740Sstevel@tonic-gate cpu_core_t	cpu_core[NCPU];			/* cpu_core structures */
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
773446Smrj  * Useful for disabling MP bring-up on a MP capable system.
780Sstevel@tonic-gate  */
790Sstevel@tonic-gate int use_mp = 1;
800Sstevel@tonic-gate 
812006Sandrei /*
823446Smrj  * to be set by a PSM to indicate what cpus
833446Smrj  * are sitting around on the system.
842006Sandrei  */
853446Smrj cpuset_t mp_cpus;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /*
880Sstevel@tonic-gate  * This variable is used by the hat layer to decide whether or not
890Sstevel@tonic-gate  * critical sections are needed to prevent race conditions.  For sun4m,
900Sstevel@tonic-gate  * this variable is set once enough MP initialization has been done in
910Sstevel@tonic-gate  * order to allow cross calls.
920Sstevel@tonic-gate  */
933446Smrj int flushes_require_xcalls;
946336Sbholler 
956336Sbholler cpuset_t cpu_ready_set;		/* initialized in startup() */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate static 	void	mp_startup(void);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate static void cpu_sep_enable(void);
1000Sstevel@tonic-gate static void cpu_sep_disable(void);
1010Sstevel@tonic-gate static void cpu_asysc_enable(void);
1020Sstevel@tonic-gate static void cpu_asysc_disable(void);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * Init CPU info - get CPU type info for processor_info system call.
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate void
1080Sstevel@tonic-gate init_cpu_info(struct cpu *cp)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	processor_info_t *pi = &cp->cpu_type_info;
1110Sstevel@tonic-gate 	char buf[CPU_IDSTRLEN];
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	/*
1140Sstevel@tonic-gate 	 * Get clock-frequency property for the CPU.
1150Sstevel@tonic-gate 	 */
1160Sstevel@tonic-gate 	pi->pi_clock = cpu_freq;
1170Sstevel@tonic-gate 
1184667Smh27603 	/*
1194667Smh27603 	 * Current frequency in Hz.
1204667Smh27603 	 */
1214718Smh27603 	cp->cpu_curr_clock = cpu_freq_hz;
1224667Smh27603 
1239004SNapanda.Pemmaiah@Sun.COM 	/*
1249004SNapanda.Pemmaiah@Sun.COM 	 * Supported frequencies.
1259004SNapanda.Pemmaiah@Sun.COM 	 */
1269004SNapanda.Pemmaiah@Sun.COM 	if (cp->cpu_supp_freqs == NULL) {
1279004SNapanda.Pemmaiah@Sun.COM 		cpu_set_supp_freqs(cp, NULL);
1289004SNapanda.Pemmaiah@Sun.COM 	}
1299004SNapanda.Pemmaiah@Sun.COM 
1300Sstevel@tonic-gate 	(void) strcpy(pi->pi_processor_type, "i386");
1310Sstevel@tonic-gate 	if (fpu_exists)
1320Sstevel@tonic-gate 		(void) strcpy(pi->pi_fputypes, "i387 compatible");
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	(void) cpuid_getidstr(cp, buf, sizeof (buf));
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	cp->cpu_idstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
1370Sstevel@tonic-gate 	(void) strcpy(cp->cpu_idstr, buf);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	(void) cpuid_getbrandstr(cp, buf, sizeof (buf));
1400Sstevel@tonic-gate 	cp->cpu_brandstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
1410Sstevel@tonic-gate 	(void) strcpy(cp->cpu_brandstr, buf);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate  * Configure syscall support on this CPU.
1460Sstevel@tonic-gate  */
1470Sstevel@tonic-gate /*ARGSUSED*/
1485295Srandyf void
1490Sstevel@tonic-gate init_cpu_syscall(struct cpu *cp)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate 	kpreempt_disable();
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate #if defined(__amd64)
1543446Smrj 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC)) {
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate #if !defined(__lint)
1570Sstevel@tonic-gate 		/*
1580Sstevel@tonic-gate 		 * The syscall instruction imposes a certain ordering on
1590Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
1600Sstevel@tonic-gate 		 * here.
1610Sstevel@tonic-gate 		 */
1620Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
1630Sstevel@tonic-gate 		ASSERT(UDS_SEL == U32CS_SEL + 8);
1640Sstevel@tonic-gate 		ASSERT(UCS_SEL == U32CS_SEL + 16);
1650Sstevel@tonic-gate #endif
1660Sstevel@tonic-gate 		/*
1670Sstevel@tonic-gate 		 * Turn syscall/sysret extensions on.
1680Sstevel@tonic-gate 		 */
1690Sstevel@tonic-gate 		cpu_asysc_enable();
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 		/*
1720Sstevel@tonic-gate 		 * Program the magic registers ..
1730Sstevel@tonic-gate 		 */
1743446Smrj 		wrmsr(MSR_AMD_STAR,
1753446Smrj 		    ((uint64_t)(U32CS_SEL << 16 | KCS_SEL)) << 32);
176770Skucharsk 		wrmsr(MSR_AMD_LSTAR, (uint64_t)(uintptr_t)sys_syscall);
177770Skucharsk 		wrmsr(MSR_AMD_CSTAR, (uint64_t)(uintptr_t)sys_syscall32);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 		/*
1800Sstevel@tonic-gate 		 * This list of flags is masked off the incoming
1810Sstevel@tonic-gate 		 * %rfl when we enter the kernel.
1820Sstevel@tonic-gate 		 */
183770Skucharsk 		wrmsr(MSR_AMD_SFMASK, (uint64_t)(uintptr_t)(PS_IE | PS_T));
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate #endif
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	/*
1880Sstevel@tonic-gate 	 * On 32-bit kernels, we use sysenter/sysexit because it's too
1890Sstevel@tonic-gate 	 * hard to use syscall/sysret, and it is more portable anyway.
1900Sstevel@tonic-gate 	 *
1910Sstevel@tonic-gate 	 * On 64-bit kernels on Nocona machines, the 32-bit syscall
1920Sstevel@tonic-gate 	 * variant isn't available to 32-bit applications, but sysenter is.
1930Sstevel@tonic-gate 	 */
1943446Smrj 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP)) {
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate #if !defined(__lint)
1970Sstevel@tonic-gate 		/*
1980Sstevel@tonic-gate 		 * The sysenter instruction imposes a certain ordering on
1990Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
2000Sstevel@tonic-gate 		 * here. See "sysenter" in Intel document 245471-012, "IA-32
2010Sstevel@tonic-gate 		 * Intel Architecture Software Developer's Manual Volume 2:
2020Sstevel@tonic-gate 		 * Instruction Set Reference"
2030Sstevel@tonic-gate 		 */
2040Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 		ASSERT32(UCS_SEL == ((KCS_SEL + 16) | 3));
2070Sstevel@tonic-gate 		ASSERT32(UDS_SEL == UCS_SEL + 8);
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 		ASSERT64(U32CS_SEL == ((KCS_SEL + 16) | 3));
2100Sstevel@tonic-gate 		ASSERT64(UDS_SEL == U32CS_SEL + 8);
2110Sstevel@tonic-gate #endif
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 		cpu_sep_enable();
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 		/*
2160Sstevel@tonic-gate 		 * resume() sets this value to the base of the threads stack
2170Sstevel@tonic-gate 		 * via a context handler.
2180Sstevel@tonic-gate 		 */
2193446Smrj 		wrmsr(MSR_INTC_SEP_ESP, 0);
220770Skucharsk 		wrmsr(MSR_INTC_SEP_EIP, (uint64_t)(uintptr_t)sys_sysenter);
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	kpreempt_enable();
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate  * Multiprocessor initialization.
2280Sstevel@tonic-gate  *
2290Sstevel@tonic-gate  * Allocate and initialize the cpu structure, TRAPTRACE buffer, and the
2300Sstevel@tonic-gate  * startup and idle threads for the specified CPU.
2310Sstevel@tonic-gate  */
2323446Smrj struct cpu *
2330Sstevel@tonic-gate mp_startup_init(int cpun)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate 	struct cpu *cp;
2360Sstevel@tonic-gate 	kthread_id_t tp;
2370Sstevel@tonic-gate 	caddr_t	sp;
2380Sstevel@tonic-gate 	proc_t *procp;
2395084Sjohnlev #if !defined(__xpv)
2405045Sbholler 	extern int idle_cpu_prefer_mwait;
2418906SEric.Saxe@Sun.COM 	extern void cpu_idle_mwait();
2425084Sjohnlev #endif
2430Sstevel@tonic-gate 	extern void idle();
2448906SEric.Saxe@Sun.COM 	extern void cpu_idle();
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate #ifdef TRAPTRACE
2470Sstevel@tonic-gate 	trap_trace_ctl_t *ttc = &trap_trace_ctl[cpun];
2480Sstevel@tonic-gate #endif
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	ASSERT(cpun < NCPU && cpu[cpun] == NULL);
2510Sstevel@tonic-gate 
2523446Smrj 	cp = kmem_zalloc(sizeof (*cp), KM_SLEEP);
2535084Sjohnlev #if !defined(__xpv)
2548906SEric.Saxe@Sun.COM 	if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait) {
2555045Sbholler 		cp->cpu_m.mcpu_mwait = cpuid_mwait_alloc(CPU);
2568906SEric.Saxe@Sun.COM 		cp->cpu_m.mcpu_idle_cpu = cpu_idle_mwait;
2578906SEric.Saxe@Sun.COM 	} else
2585084Sjohnlev #endif
2598906SEric.Saxe@Sun.COM 		cp->cpu_m.mcpu_idle_cpu = cpu_idle;
2604481Sbholler 
2610Sstevel@tonic-gate 	procp = curthread->t_procp;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
2640Sstevel@tonic-gate 	/*
2650Sstevel@tonic-gate 	 * Initialize the dispatcher first.
2660Sstevel@tonic-gate 	 */
2670Sstevel@tonic-gate 	disp_cpu_init(cp);
2680Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
2690Sstevel@tonic-gate 
270414Skchow 	cpu_vm_data_init(cp);
271414Skchow 
2720Sstevel@tonic-gate 	/*
2730Sstevel@tonic-gate 	 * Allocate and initialize the startup thread for this CPU.
2740Sstevel@tonic-gate 	 * Interrupt and process switch stacks get allocated later
2750Sstevel@tonic-gate 	 * when the CPU starts running.
2760Sstevel@tonic-gate 	 */
2770Sstevel@tonic-gate 	tp = thread_create(NULL, 0, NULL, NULL, 0, procp,
2780Sstevel@tonic-gate 	    TS_STOPPED, maxclsyspri);
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	/*
2810Sstevel@tonic-gate 	 * Set state to TS_ONPROC since this thread will start running
2820Sstevel@tonic-gate 	 * as soon as the CPU comes online.
2830Sstevel@tonic-gate 	 *
2840Sstevel@tonic-gate 	 * All the other fields of the thread structure are setup by
2850Sstevel@tonic-gate 	 * thread_create().
2860Sstevel@tonic-gate 	 */
2870Sstevel@tonic-gate 	THREAD_ONPROC(tp, cp);
2880Sstevel@tonic-gate 	tp->t_preempt = 1;
2890Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
2900Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
2910Sstevel@tonic-gate 	tp->t_cpu = cp;
2920Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	/*
2950Sstevel@tonic-gate 	 * Setup thread to start in mp_startup.
2960Sstevel@tonic-gate 	 */
2970Sstevel@tonic-gate 	sp = tp->t_stk;
2980Sstevel@tonic-gate 	tp->t_pc = (uintptr_t)mp_startup;
2990Sstevel@tonic-gate 	tp->t_sp = (uintptr_t)(sp - MINFRAME);
3003446Smrj #if defined(__amd64)
3013446Smrj 	tp->t_sp -= STACK_ENTRY_ALIGN;		/* fake a call */
3023446Smrj #endif
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	cp->cpu_id = cpun;
3050Sstevel@tonic-gate 	cp->cpu_self = cp;
3060Sstevel@tonic-gate 	cp->cpu_thread = tp;
3070Sstevel@tonic-gate 	cp->cpu_lwp = NULL;
3080Sstevel@tonic-gate 	cp->cpu_dispthread = tp;
3090Sstevel@tonic-gate 	cp->cpu_dispatch_pri = DISP_PRIO(tp);
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	/*
3121482Ssethg 	 * cpu_base_spl must be set explicitly here to prevent any blocking
3131482Ssethg 	 * operations in mp_startup from causing the spl of the cpu to drop
3141482Ssethg 	 * to 0 (allowing device interrupts before we're ready) in resume().
3151482Ssethg 	 * cpu_base_spl MUST remain at LOCK_LEVEL until the cpu is CPU_READY.
3161482Ssethg 	 * As an extra bit of security on DEBUG kernels, this is enforced with
3171482Ssethg 	 * an assertion in mp_startup() -- before cpu_base_spl is set to its
3181482Ssethg 	 * proper value.
3191482Ssethg 	 */
3201482Ssethg 	cp->cpu_base_spl = ipltospl(LOCK_LEVEL);
3211482Ssethg 
3221482Ssethg 	/*
3230Sstevel@tonic-gate 	 * Now, initialize per-CPU idle thread for this CPU.
3240Sstevel@tonic-gate 	 */
3250Sstevel@tonic-gate 	tp = thread_create(NULL, PAGESIZE, idle, NULL, 0, procp, TS_ONPROC, -1);
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	cp->cpu_idle_thread = tp;
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	tp->t_preempt = 1;
3300Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
3310Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
3320Sstevel@tonic-gate 	tp->t_cpu = cp;
3330Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	/*
3363434Sesaxe 	 * Bootstrap the CPU's PG data
33760Sesaxe 	 */
3383434Sesaxe 	pg_cpu_bootstrap(cp);
33960Sesaxe 
34060Sesaxe 	/*
3413446Smrj 	 * Perform CPC initialization on the new CPU.
3420Sstevel@tonic-gate 	 */
3430Sstevel@tonic-gate 	kcpc_hw_init(cp);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	/*
3460Sstevel@tonic-gate 	 * Allocate virtual addresses for cpu_caddr1 and cpu_caddr2
3470Sstevel@tonic-gate 	 * for each CPU.
3480Sstevel@tonic-gate 	 */
3490Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(cp);
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	/*
3523446Smrj 	 * Allocate page for new GDT and initialize from current GDT.
3530Sstevel@tonic-gate 	 */
3543446Smrj #if !defined(__lint)
3553446Smrj 	ASSERT((sizeof (*cp->cpu_gdt) * NGDT) <= PAGESIZE);
3563446Smrj #endif
3575460Sjosephb 	cp->cpu_gdt = kmem_zalloc(PAGESIZE, KM_SLEEP);
3585460Sjosephb 	bcopy(CPU->cpu_gdt, cp->cpu_gdt, (sizeof (*cp->cpu_gdt) * NGDT));
3591626Srab 
3603446Smrj #if defined(__i386)
3610Sstevel@tonic-gate 	/*
3620Sstevel@tonic-gate 	 * setup kernel %gs.
3630Sstevel@tonic-gate 	 */
3640Sstevel@tonic-gate 	set_usegd(&cp->cpu_gdt[GDT_GS], cp, sizeof (struct cpu) -1, SDT_MEMRWA,
3650Sstevel@tonic-gate 	    SEL_KPL, 0, 1);
3663446Smrj #endif
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	/*
3690Sstevel@tonic-gate 	 * If we have more than one node, each cpu gets a copy of IDT
3700Sstevel@tonic-gate 	 * local to its node. If this is a Pentium box, we use cpu 0's
3710Sstevel@tonic-gate 	 * IDT. cpu 0's IDT has been made read-only to workaround the
3720Sstevel@tonic-gate 	 * cmpxchgl register bug
3730Sstevel@tonic-gate 	 */
3740Sstevel@tonic-gate 	if (system_hardware.hd_nodes && x86_type != X86_TYPE_P5) {
3755460Sjosephb #if !defined(__lint)
3765460Sjosephb 		ASSERT((sizeof (*CPU->cpu_idt) * NIDT) <= PAGESIZE);
3775460Sjosephb #endif
3785460Sjosephb 		cp->cpu_idt = kmem_zalloc(PAGESIZE, KM_SLEEP);
3795460Sjosephb 		bcopy(CPU->cpu_idt, cp->cpu_idt, PAGESIZE);
3803446Smrj 	} else {
3815460Sjosephb 		cp->cpu_idt = CPU->cpu_idt;
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	/*
3853446Smrj 	 * Get interrupt priority data from cpu 0.
3860Sstevel@tonic-gate 	 */
3870Sstevel@tonic-gate 	cp->cpu_pri_data = CPU->cpu_pri_data;
3880Sstevel@tonic-gate 
3893446Smrj 	/*
3903446Smrj 	 * alloc space for cpuid info
3913446Smrj 	 */
3923446Smrj 	cpuid_alloc_space(cp);
3933446Smrj 
3944581Ssherrym 	/*
3954581Ssherrym 	 * alloc space for ucode_info
3964581Ssherrym 	 */
3974581Ssherrym 	ucode_alloc_space(cp);
3989489SJoe.Bonasera@sun.com 	xc_init_cpu(cp);
3990Sstevel@tonic-gate 	hat_cpu_online(cp);
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate #ifdef TRAPTRACE
4020Sstevel@tonic-gate 	/*
4033446Smrj 	 * If this is a TRAPTRACE kernel, allocate TRAPTRACE buffers
4040Sstevel@tonic-gate 	 */
4050Sstevel@tonic-gate 	ttc->ttc_first = (uintptr_t)kmem_zalloc(trap_trace_bufsize, KM_SLEEP);
4060Sstevel@tonic-gate 	ttc->ttc_next = ttc->ttc_first;
4070Sstevel@tonic-gate 	ttc->ttc_limit = ttc->ttc_first + trap_trace_bufsize;
4080Sstevel@tonic-gate #endif
4090Sstevel@tonic-gate 	/*
4100Sstevel@tonic-gate 	 * Record that we have another CPU.
4110Sstevel@tonic-gate 	 */
4120Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
4130Sstevel@tonic-gate 	/*
4140Sstevel@tonic-gate 	 * Initialize the interrupt threads for this CPU
4150Sstevel@tonic-gate 	 */
4161455Sandrei 	cpu_intr_alloc(cp, NINTR_THREADS);
4170Sstevel@tonic-gate 	/*
4180Sstevel@tonic-gate 	 * Add CPU to list of available CPUs.  It'll be on the active list
4190Sstevel@tonic-gate 	 * after mp_startup().
4200Sstevel@tonic-gate 	 */
4210Sstevel@tonic-gate 	cpu_add_unit(cp);
4220Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
4233446Smrj 
4243446Smrj 	return (cp);
4253446Smrj }
4263446Smrj 
4273446Smrj /*
4283446Smrj  * Undo what was done in mp_startup_init
4293446Smrj  */
4303446Smrj static void
4313446Smrj mp_startup_fini(struct cpu *cp, int error)
4323446Smrj {
4333446Smrj 	mutex_enter(&cpu_lock);
4343446Smrj 
4353446Smrj 	/*
4363446Smrj 	 * Remove the CPU from the list of available CPUs.
4373446Smrj 	 */
4383446Smrj 	cpu_del_unit(cp->cpu_id);
4393446Smrj 
4403446Smrj 	if (error == ETIMEDOUT) {
4413446Smrj 		/*
4423446Smrj 		 * The cpu was started, but never *seemed* to run any
4433446Smrj 		 * code in the kernel; it's probably off spinning in its
4443446Smrj 		 * own private world, though with potential references to
4453446Smrj 		 * our kmem-allocated IDTs and GDTs (for example).
4463446Smrj 		 *
4473446Smrj 		 * Worse still, it may actually wake up some time later,
4483446Smrj 		 * so rather than guess what it might or might not do, we
4493446Smrj 		 * leave the fundamental data structures intact.
4503446Smrj 		 */
4513446Smrj 		cp->cpu_flags = 0;
4523446Smrj 		mutex_exit(&cpu_lock);
4533446Smrj 		return;
4543446Smrj 	}
4553446Smrj 
4563446Smrj 	/*
4573446Smrj 	 * At this point, the only threads bound to this CPU should
4583446Smrj 	 * special per-cpu threads: it's idle thread, it's pause threads,
4593446Smrj 	 * and it's interrupt threads.  Clean these up.
4603446Smrj 	 */
4613446Smrj 	cpu_destroy_bound_threads(cp);
4623446Smrj 	cp->cpu_idle_thread = NULL;
4633446Smrj 
4643446Smrj 	/*
4653446Smrj 	 * Free the interrupt stack.
4663446Smrj 	 */
4673446Smrj 	segkp_release(segkp,
4683446Smrj 	    cp->cpu_intr_stack - (INTR_STACK_SIZE - SA(MINFRAME)));
4693446Smrj 
4703446Smrj 	mutex_exit(&cpu_lock);
4713446Smrj 
4723446Smrj #ifdef TRAPTRACE
4733446Smrj 	/*
4743446Smrj 	 * Discard the trap trace buffer
4753446Smrj 	 */
4763446Smrj 	{
4773446Smrj 		trap_trace_ctl_t *ttc = &trap_trace_ctl[cp->cpu_id];
4783446Smrj 
4793446Smrj 		kmem_free((void *)ttc->ttc_first, trap_trace_bufsize);
4803446Smrj 		ttc->ttc_first = NULL;
4813446Smrj 	}
4823446Smrj #endif
4833446Smrj 
4843446Smrj 	hat_cpu_offline(cp);
4853446Smrj 
4863446Smrj 	cpuid_free_space(cp);
4873446Smrj 
4884581Ssherrym 	ucode_free_space(cp);
4894581Ssherrym 
4905460Sjosephb 	if (cp->cpu_idt != CPU->cpu_idt)
4915460Sjosephb 		kmem_free(cp->cpu_idt, PAGESIZE);
4925460Sjosephb 	cp->cpu_idt = NULL;
4933446Smrj 
4945460Sjosephb 	kmem_free(cp->cpu_gdt, PAGESIZE);
4955460Sjosephb 	cp->cpu_gdt = NULL;
4963446Smrj 
4973446Smrj 	teardown_vaddr_for_ppcopy(cp);
4983446Smrj 
4993446Smrj 	kcpc_hw_fini(cp);
5003446Smrj 
5013446Smrj 	cp->cpu_dispthread = NULL;
5023446Smrj 	cp->cpu_thread = NULL;	/* discarded by cpu_destroy_bound_threads() */
5033446Smrj 
5043446Smrj 	cpu_vm_data_destroy(cp);
5053446Smrj 
5063446Smrj 	mutex_enter(&cpu_lock);
5073446Smrj 	disp_cpu_fini(cp);
5083446Smrj 	mutex_exit(&cpu_lock);
5093446Smrj 
5105084Sjohnlev #if !defined(__xpv)
5115045Sbholler 	if (cp->cpu_m.mcpu_mwait != NULL)
5125045Sbholler 		cpuid_mwait_free(cp);
5135084Sjohnlev #endif
5143446Smrj 	kmem_free(cp, sizeof (*cp));
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate /*
5180Sstevel@tonic-gate  * Apply workarounds for known errata, and warn about those that are absent.
5190Sstevel@tonic-gate  *
5200Sstevel@tonic-gate  * System vendors occasionally create configurations which contain different
5210Sstevel@tonic-gate  * revisions of the CPUs that are almost but not exactly the same.  At the
5220Sstevel@tonic-gate  * time of writing, this meant that their clock rates were the same, their
5230Sstevel@tonic-gate  * feature sets were the same, but the required workaround were -not-
5240Sstevel@tonic-gate  * necessarily the same.  So, this routine is invoked on -every- CPU soon
5250Sstevel@tonic-gate  * after starting to make sure that the resulting system contains the most
5260Sstevel@tonic-gate  * pessimal set of workarounds needed to cope with *any* of the CPUs in the
5270Sstevel@tonic-gate  * system.
5280Sstevel@tonic-gate  *
529938Sesaxe  * workaround_errata is invoked early in mlsetup() for CPU 0, and in
530938Sesaxe  * mp_startup() for all slave CPUs. Slaves process workaround_errata prior
531938Sesaxe  * to acknowledging their readiness to the master, so this routine will
532938Sesaxe  * never be executed by multiple CPUs in parallel, thus making updates to
533938Sesaxe  * global data safe.
534938Sesaxe  *
535359Skucharsk  * These workarounds are based on Rev 3.57 of the Revision Guide for
536359Skucharsk  * AMD Athlon(tm) 64 and AMD Opteron(tm) Processors, August 2005.
5370Sstevel@tonic-gate  */
5380Sstevel@tonic-gate 
5393446Smrj #if defined(OPTERON_ERRATUM_88)
5403446Smrj int opteron_erratum_88;		/* if non-zero -> at least one cpu has it */
5413446Smrj #endif
5423446Smrj 
5430Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
5440Sstevel@tonic-gate int opteron_erratum_91;		/* if non-zero -> at least one cpu has it */
5450Sstevel@tonic-gate #endif
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
5480Sstevel@tonic-gate int opteron_erratum_93;		/* if non-zero -> at least one cpu has it */
5490Sstevel@tonic-gate #endif
5500Sstevel@tonic-gate 
5513446Smrj #if defined(OPTERON_ERRATUM_95)
5523446Smrj int opteron_erratum_95;		/* if non-zero -> at least one cpu has it */
5533446Smrj #endif
5543446Smrj 
5550Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
5560Sstevel@tonic-gate int opteron_erratum_100;	/* if non-zero -> at least one cpu has it */
5570Sstevel@tonic-gate #endif
5580Sstevel@tonic-gate 
5593446Smrj #if defined(OPTERON_ERRATUM_108)
5603446Smrj int opteron_erratum_108;	/* if non-zero -> at least one cpu has it */
5613446Smrj #endif
5623446Smrj 
5630Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
5640Sstevel@tonic-gate int opteron_erratum_109;	/* if non-zero -> at least one cpu has it */
5650Sstevel@tonic-gate #endif
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
5680Sstevel@tonic-gate int opteron_erratum_121;	/* if non-zero -> at least one cpu has it */
5690Sstevel@tonic-gate #endif
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
5720Sstevel@tonic-gate int opteron_erratum_122;	/* if non-zero -> at least one cpu has it */
5730Sstevel@tonic-gate #endif
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
5760Sstevel@tonic-gate int opteron_erratum_123;	/* if non-zero -> at least one cpu has it */
5770Sstevel@tonic-gate #endif
5780Sstevel@tonic-gate 
579359Skucharsk #if defined(OPTERON_ERRATUM_131)
580359Skucharsk int opteron_erratum_131;	/* if non-zero -> at least one cpu has it */
581359Skucharsk #endif
5820Sstevel@tonic-gate 
583938Sesaxe #if defined(OPTERON_WORKAROUND_6336786)
584938Sesaxe int opteron_workaround_6336786;	/* non-zero -> WA relevant and applied */
585938Sesaxe int opteron_workaround_6336786_UP = 0;	/* Not needed for UP */
586938Sesaxe #endif
587938Sesaxe 
5881582Skchow #if defined(OPTERON_WORKAROUND_6323525)
5891582Skchow int opteron_workaround_6323525;	/* if non-zero -> at least one cpu has it */
5901582Skchow #endif
5911582Skchow 
5926691Skchow #if defined(OPTERON_ERRATUM_298)
5936691Skchow int opteron_erratum_298;
5946691Skchow #endif
5956691Skchow 
5963446Smrj static void
5973446Smrj workaround_warning(cpu_t *cp, uint_t erratum)
5983446Smrj {
5993446Smrj 	cmn_err(CE_WARN, "cpu%d: no workaround for erratum %u",
6003446Smrj 	    cp->cpu_id, erratum);
6013446Smrj }
6023446Smrj 
6033446Smrj static void
6043446Smrj workaround_applied(uint_t erratum)
6053446Smrj {
6063446Smrj 	if (erratum > 1000000)
6073446Smrj 		cmn_err(CE_CONT, "?workaround applied for cpu issue #%d\n",
6083446Smrj 		    erratum);
6093446Smrj 	else
6103446Smrj 		cmn_err(CE_CONT, "?workaround applied for cpu erratum #%d\n",
6113446Smrj 		    erratum);
6123446Smrj }
6133446Smrj 
6143446Smrj static void
6153446Smrj msr_warning(cpu_t *cp, const char *rw, uint_t msr, int error)
6163446Smrj {
6173446Smrj 	cmn_err(CE_WARN, "cpu%d: couldn't %smsr 0x%x, error %d",
6183446Smrj 	    cp->cpu_id, rw, msr, error);
6193446Smrj }
6200Sstevel@tonic-gate 
6215893Sesaxe /*
6228232SEric.Saxe@Sun.COM  * Determine the number of nodes in a Hammer / Greyhound / Griffin family
6238232SEric.Saxe@Sun.COM  * system.
6245893Sesaxe  */
6255893Sesaxe static uint_t
6265893Sesaxe opteron_get_nnodes(void)
6275893Sesaxe {
6285893Sesaxe 	static uint_t nnodes = 0;
6295893Sesaxe 
6308232SEric.Saxe@Sun.COM 	if (nnodes == 0) {
6315893Sesaxe #ifdef	DEBUG
6328232SEric.Saxe@Sun.COM 		uint_t family;
6335893Sesaxe 
6348232SEric.Saxe@Sun.COM 		/*
6358232SEric.Saxe@Sun.COM 		 * This routine uses a PCI config space based mechanism
6368232SEric.Saxe@Sun.COM 		 * for retrieving the number of nodes in the system.
6378232SEric.Saxe@Sun.COM 		 * Device 24, function 0, offset 0x60 as used here is not
6388232SEric.Saxe@Sun.COM 		 * AMD processor architectural, and may not work on processor
6398232SEric.Saxe@Sun.COM 		 * families other than those listed below.
6408232SEric.Saxe@Sun.COM 		 *
6418232SEric.Saxe@Sun.COM 		 * Callers of this routine must ensure that we're running on
6428232SEric.Saxe@Sun.COM 		 * a processor which supports this mechanism.
6438232SEric.Saxe@Sun.COM 		 * The assertion below is meant to catch calls on unsupported
6448232SEric.Saxe@Sun.COM 		 * processors.
6458232SEric.Saxe@Sun.COM 		 */
6468232SEric.Saxe@Sun.COM 		family = cpuid_getfamily(CPU);
6478232SEric.Saxe@Sun.COM 		ASSERT(family == 0xf || family == 0x10 || family == 0x11);
6485893Sesaxe #endif	/* DEBUG */
6495893Sesaxe 
6505893Sesaxe 		/*
6515893Sesaxe 		 * Obtain the number of nodes in the system from
6525893Sesaxe 		 * bits [6:4] of the Node ID register on node 0.
6535893Sesaxe 		 *
6545893Sesaxe 		 * The actual node count is NodeID[6:4] + 1
6555893Sesaxe 		 *
6565893Sesaxe 		 * The Node ID register is accessed via function 0,
6575893Sesaxe 		 * offset 0x60. Node 0 is device 24.
6585893Sesaxe 		 */
6595893Sesaxe 		nnodes = ((pci_getl_func(0, 24, 0, 0x60) & 0x70) >> 4) + 1;
6605893Sesaxe 	}
6615893Sesaxe 	return (nnodes);
6625893Sesaxe }
6635893Sesaxe 
6640Sstevel@tonic-gate uint_t
6656691Skchow do_erratum_298(struct cpu *cpu)
6666691Skchow {
6676691Skchow 	static int	osvwrc = -3;
6686691Skchow 	extern int	osvw_opteron_erratum(cpu_t *, uint_t);
6696691Skchow 
6706691Skchow 	/*
6716691Skchow 	 * L2 Eviction May Occur During Processor Operation To Set
6726691Skchow 	 * Accessed or Dirty Bit.
6736691Skchow 	 */
6746691Skchow 	if (osvwrc == -3) {
6756691Skchow 		osvwrc = osvw_opteron_erratum(cpu, 298);
6766691Skchow 	} else {
6776691Skchow 		/* osvw return codes should be consistent for all cpus */
6786691Skchow 		ASSERT(osvwrc == osvw_opteron_erratum(cpu, 298));
6796691Skchow 	}
6806691Skchow 
6816691Skchow 	switch (osvwrc) {
6826691Skchow 	case 0:		/* erratum is not present: do nothing */
6836691Skchow 		break;
6846691Skchow 	case 1:		/* erratum is present: BIOS workaround applied */
6856691Skchow 		/*
6866691Skchow 		 * check if workaround is actually in place and issue warning
6876691Skchow 		 * if not.
6886691Skchow 		 */
6896691Skchow 		if (((rdmsr(MSR_AMD_HWCR) & AMD_HWCR_TLBCACHEDIS) == 0) ||
6906691Skchow 		    ((rdmsr(MSR_AMD_BU_CFG) & AMD_BU_CFG_E298) == 0)) {
6916691Skchow #if defined(OPTERON_ERRATUM_298)
6926691Skchow 			opteron_erratum_298++;
6936691Skchow #else
6946691Skchow 			workaround_warning(cpu, 298);
6956691Skchow 			return (1);
6966691Skchow #endif
6976691Skchow 		}
6986691Skchow 		break;
6996691Skchow 	case -1:	/* cannot determine via osvw: check cpuid */
7006691Skchow 		if ((cpuid_opteron_erratum(cpu, 298) > 0) &&
7016691Skchow 		    (((rdmsr(MSR_AMD_HWCR) & AMD_HWCR_TLBCACHEDIS) == 0) ||
7026691Skchow 		    ((rdmsr(MSR_AMD_BU_CFG) & AMD_BU_CFG_E298) == 0))) {
7036691Skchow #if defined(OPTERON_ERRATUM_298)
7046691Skchow 			opteron_erratum_298++;
7056691Skchow #else
7066691Skchow 			workaround_warning(cpu, 298);
7076691Skchow 			return (1);
7086691Skchow #endif
7096691Skchow 		}
7106691Skchow 		break;
7116691Skchow 	}
7126691Skchow 	return (0);
7136691Skchow }
7146691Skchow 
7156691Skchow uint_t
7160Sstevel@tonic-gate workaround_errata(struct cpu *cpu)
7170Sstevel@tonic-gate {
7180Sstevel@tonic-gate 	uint_t missing = 0;
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	ASSERT(cpu == CPU);
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	/*LINTED*/
7230Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 88) > 0) {
7240Sstevel@tonic-gate 		/*
7250Sstevel@tonic-gate 		 * SWAPGS May Fail To Read Correct GS Base
7260Sstevel@tonic-gate 		 */
7270Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_88)
7280Sstevel@tonic-gate 		/*
7290Sstevel@tonic-gate 		 * The workaround is an mfence in the relevant assembler code
7300Sstevel@tonic-gate 		 */
7313446Smrj 		opteron_erratum_88++;
7320Sstevel@tonic-gate #else
7333446Smrj 		workaround_warning(cpu, 88);
7340Sstevel@tonic-gate 		missing++;
7350Sstevel@tonic-gate #endif
7360Sstevel@tonic-gate 	}
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 91) > 0) {
7390Sstevel@tonic-gate 		/*
7400Sstevel@tonic-gate 		 * Software Prefetches May Report A Page Fault
7410Sstevel@tonic-gate 		 */
7420Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
7430Sstevel@tonic-gate 		/*
7440Sstevel@tonic-gate 		 * fix is in trap.c
7450Sstevel@tonic-gate 		 */
7460Sstevel@tonic-gate 		opteron_erratum_91++;
7470Sstevel@tonic-gate #else
7483446Smrj 		workaround_warning(cpu, 91);
7490Sstevel@tonic-gate 		missing++;
7500Sstevel@tonic-gate #endif
7510Sstevel@tonic-gate 	}
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 93) > 0) {
7540Sstevel@tonic-gate 		/*
7550Sstevel@tonic-gate 		 * RSM Auto-Halt Restart Returns to Incorrect RIP
7560Sstevel@tonic-gate 		 */
7570Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
7580Sstevel@tonic-gate 		/*
7590Sstevel@tonic-gate 		 * fix is in trap.c
7600Sstevel@tonic-gate 		 */
7610Sstevel@tonic-gate 		opteron_erratum_93++;
7620Sstevel@tonic-gate #else
7633446Smrj 		workaround_warning(cpu, 93);
7640Sstevel@tonic-gate 		missing++;
7650Sstevel@tonic-gate #endif
7660Sstevel@tonic-gate 	}
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	/*LINTED*/
7690Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 95) > 0) {
7700Sstevel@tonic-gate 		/*
7710Sstevel@tonic-gate 		 * RET Instruction May Return to Incorrect EIP
7720Sstevel@tonic-gate 		 */
7730Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_95)
7740Sstevel@tonic-gate #if defined(_LP64)
7750Sstevel@tonic-gate 		/*
7760Sstevel@tonic-gate 		 * Workaround this by ensuring that 32-bit user code and
7770Sstevel@tonic-gate 		 * 64-bit kernel code never occupy the same address
7780Sstevel@tonic-gate 		 * range mod 4G.
7790Sstevel@tonic-gate 		 */
7800Sstevel@tonic-gate 		if (_userlimit32 > 0xc0000000ul)
7810Sstevel@tonic-gate 			*(uintptr_t *)&_userlimit32 = 0xc0000000ul;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 		/*LINTED*/
7840Sstevel@tonic-gate 		ASSERT((uint32_t)COREHEAP_BASE == 0xc0000000u);
7853446Smrj 		opteron_erratum_95++;
7860Sstevel@tonic-gate #endif	/* _LP64 */
7870Sstevel@tonic-gate #else
7883446Smrj 		workaround_warning(cpu, 95);
7890Sstevel@tonic-gate 		missing++;
7903446Smrj #endif
7910Sstevel@tonic-gate 	}
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 100) > 0) {
7940Sstevel@tonic-gate 		/*
7950Sstevel@tonic-gate 		 * Compatibility Mode Branches Transfer to Illegal Address
7960Sstevel@tonic-gate 		 */
7970Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
7980Sstevel@tonic-gate 		/*
7990Sstevel@tonic-gate 		 * fix is in trap.c
8000Sstevel@tonic-gate 		 */
8010Sstevel@tonic-gate 		opteron_erratum_100++;
8020Sstevel@tonic-gate #else
8033446Smrj 		workaround_warning(cpu, 100);
8040Sstevel@tonic-gate 		missing++;
8050Sstevel@tonic-gate #endif
8060Sstevel@tonic-gate 	}
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	/*LINTED*/
8090Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 108) > 0) {
8100Sstevel@tonic-gate 		/*
8110Sstevel@tonic-gate 		 * CPUID Instruction May Return Incorrect Model Number In
8120Sstevel@tonic-gate 		 * Some Processors
8130Sstevel@tonic-gate 		 */
8140Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_108)
8150Sstevel@tonic-gate 		/*
8160Sstevel@tonic-gate 		 * (Our cpuid-handling code corrects the model number on
8170Sstevel@tonic-gate 		 * those processors)
8180Sstevel@tonic-gate 		 */
8190Sstevel@tonic-gate #else
8203446Smrj 		workaround_warning(cpu, 108);
8210Sstevel@tonic-gate 		missing++;
8220Sstevel@tonic-gate #endif
8230Sstevel@tonic-gate 	}
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	/*LINTED*/
8263446Smrj 	if (cpuid_opteron_erratum(cpu, 109) > 0) do {
8270Sstevel@tonic-gate 		/*
8289637SRandy.Fishel@Sun.COM 		 * Certain Reverse REP MOVS May Produce Unpredictable Behavior
8290Sstevel@tonic-gate 		 */
8300Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
8313446Smrj 		/*
8323446Smrj 		 * The "workaround" is to print a warning to upgrade the BIOS
8333446Smrj 		 */
8343446Smrj 		uint64_t value;
8353446Smrj 		const uint_t msr = MSR_AMD_PATCHLEVEL;
8363446Smrj 		int err;
8370Sstevel@tonic-gate 
8383446Smrj 		if ((err = checked_rdmsr(msr, &value)) != 0) {
8393446Smrj 			msr_warning(cpu, "rd", msr, err);
8403446Smrj 			workaround_warning(cpu, 109);
8413446Smrj 			missing++;
8423446Smrj 		}
8433446Smrj 		if (value == 0)
8440Sstevel@tonic-gate 			opteron_erratum_109++;
8450Sstevel@tonic-gate #else
8463446Smrj 		workaround_warning(cpu, 109);
8470Sstevel@tonic-gate 		missing++;
8480Sstevel@tonic-gate #endif
8493446Smrj 	/*CONSTANTCONDITION*/
8503446Smrj 	} while (0);
8513446Smrj 
8520Sstevel@tonic-gate 	/*LINTED*/
8530Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 121) > 0) {
8540Sstevel@tonic-gate 		/*
8550Sstevel@tonic-gate 		 * Sequential Execution Across Non_Canonical Boundary Caused
8560Sstevel@tonic-gate 		 * Processor Hang
8570Sstevel@tonic-gate 		 */
8580Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
8593446Smrj #if defined(_LP64)
8600Sstevel@tonic-gate 		/*
8610Sstevel@tonic-gate 		 * Erratum 121 is only present in long (64 bit) mode.
8620Sstevel@tonic-gate 		 * Workaround is to include the page immediately before the
8630Sstevel@tonic-gate 		 * va hole to eliminate the possibility of system hangs due to
8640Sstevel@tonic-gate 		 * sequential execution across the va hole boundary.
8650Sstevel@tonic-gate 		 */
8663446Smrj 		if (opteron_erratum_121)
8673446Smrj 			opteron_erratum_121++;
8683446Smrj 		else {
8693446Smrj 			if (hole_start) {
8703446Smrj 				hole_start -= PAGESIZE;
8713446Smrj 			} else {
8723446Smrj 				/*
8733446Smrj 				 * hole_start not yet initialized by
8743446Smrj 				 * mmu_init. Initialize hole_start
8753446Smrj 				 * with value to be subtracted.
8763446Smrj 				 */
8773446Smrj 				hole_start = PAGESIZE;
8780Sstevel@tonic-gate 			}
8793446Smrj 			opteron_erratum_121++;
8800Sstevel@tonic-gate 		}
8813446Smrj #endif	/* _LP64 */
8820Sstevel@tonic-gate #else
8833446Smrj 		workaround_warning(cpu, 121);
8840Sstevel@tonic-gate 		missing++;
8850Sstevel@tonic-gate #endif
8860Sstevel@tonic-gate 	}
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	/*LINTED*/
8893446Smrj 	if (cpuid_opteron_erratum(cpu, 122) > 0) do {
8900Sstevel@tonic-gate 		/*
8913446Smrj 		 * TLB Flush Filter May Cause Coherency Problem in
8920Sstevel@tonic-gate 		 * Multiprocessor Systems
8930Sstevel@tonic-gate 		 */
8940Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
8953446Smrj 		uint64_t value;
8963446Smrj 		const uint_t msr = MSR_AMD_HWCR;
8973446Smrj 		int error;
8983446Smrj 
8990Sstevel@tonic-gate 		/*
9000Sstevel@tonic-gate 		 * Erratum 122 is only present in MP configurations (multi-core
9010Sstevel@tonic-gate 		 * or multi-processor).
9020Sstevel@tonic-gate 		 */
9035084Sjohnlev #if defined(__xpv)
9045084Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
9055084Sjohnlev 			break;
90610175SStuart.Maybee@Sun.COM 		if (!opteron_erratum_122 && xpv_nr_phys_cpus() == 1)
9075084Sjohnlev 			break;
9085084Sjohnlev #else
9095893Sesaxe 		if (!opteron_erratum_122 && opteron_get_nnodes() == 1 &&
9103446Smrj 		    cpuid_get_ncpu_per_chip(cpu) == 1)
9113446Smrj 			break;
9125084Sjohnlev #endif
9133446Smrj 		/* disable TLB Flush Filter */
9143446Smrj 
9153446Smrj 		if ((error = checked_rdmsr(msr, &value)) != 0) {
9163446Smrj 			msr_warning(cpu, "rd", msr, error);
9173446Smrj 			workaround_warning(cpu, 122);
9183446Smrj 			missing++;
9193446Smrj 		} else {
9203446Smrj 			value |= (uint64_t)AMD_HWCR_FFDIS;
9213446Smrj 			if ((error = checked_wrmsr(msr, value)) != 0) {
9223446Smrj 				msr_warning(cpu, "wr", msr, error);
9233446Smrj 				workaround_warning(cpu, 122);
9243446Smrj 				missing++;
9253446Smrj 			}
9260Sstevel@tonic-gate 		}
9273446Smrj 		opteron_erratum_122++;
9280Sstevel@tonic-gate #else
9293446Smrj 		workaround_warning(cpu, 122);
9300Sstevel@tonic-gate 		missing++;
9310Sstevel@tonic-gate #endif
9323446Smrj 	/*CONSTANTCONDITION*/
9333446Smrj 	} while (0);
934302Skchow 
9350Sstevel@tonic-gate 	/*LINTED*/
9363446Smrj 	if (cpuid_opteron_erratum(cpu, 123) > 0) do {
9370Sstevel@tonic-gate 		/*
9380Sstevel@tonic-gate 		 * Bypassed Reads May Cause Data Corruption of System Hang in
9390Sstevel@tonic-gate 		 * Dual Core Processors
9400Sstevel@tonic-gate 		 */
9413446Smrj #if defined(OPTERON_ERRATUM_123)
9423446Smrj 		uint64_t value;
9433446Smrj 		const uint_t msr = MSR_AMD_PATCHLEVEL;
9443446Smrj 		int err;
9453446Smrj 
9460Sstevel@tonic-gate 		/*
9470Sstevel@tonic-gate 		 * Erratum 123 applies only to multi-core cpus.
9480Sstevel@tonic-gate 		 */
9493446Smrj 		if (cpuid_get_ncpu_per_chip(cpu) < 2)
9503446Smrj 			break;
9515084Sjohnlev #if defined(__xpv)
9525084Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
9535084Sjohnlev 			break;
9545084Sjohnlev #endif
9553446Smrj 		/*
9563446Smrj 		 * The "workaround" is to print a warning to upgrade the BIOS
9573446Smrj 		 */
9583446Smrj 		if ((err = checked_rdmsr(msr, &value)) != 0) {
9593446Smrj 			msr_warning(cpu, "rd", msr, err);
9603446Smrj 			workaround_warning(cpu, 123);
9613446Smrj 			missing++;
9620Sstevel@tonic-gate 		}
9633446Smrj 		if (value == 0)
9643446Smrj 			opteron_erratum_123++;
9653446Smrj #else
9663446Smrj 		workaround_warning(cpu, 123);
9673446Smrj 		missing++;
968359Skucharsk 
9693446Smrj #endif
9703446Smrj 	/*CONSTANTCONDITION*/
9713446Smrj 	} while (0);
9723446Smrj 
973359Skucharsk 	/*LINTED*/
9743446Smrj 	if (cpuid_opteron_erratum(cpu, 131) > 0) do {
975359Skucharsk 		/*
976359Skucharsk 		 * Multiprocessor Systems with Four or More Cores May Deadlock
977359Skucharsk 		 * Waiting for a Probe Response
978359Skucharsk 		 */
9793446Smrj #if defined(OPTERON_ERRATUM_131)
9803446Smrj 		uint64_t nbcfg;
9813446Smrj 		const uint_t msr = MSR_AMD_NB_CFG;
9823446Smrj 		const uint64_t wabits =
9833446Smrj 		    AMD_NB_CFG_SRQ_HEARTBEAT | AMD_NB_CFG_SRQ_SPR;
9843446Smrj 		int error;
9853446Smrj 
986359Skucharsk 		/*
987359Skucharsk 		 * Erratum 131 applies to any system with four or more cores.
988359Skucharsk 		 */
9893446Smrj 		if (opteron_erratum_131)
9903446Smrj 			break;
9915084Sjohnlev #if defined(__xpv)
9925084Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
9935084Sjohnlev 			break;
99410175SStuart.Maybee@Sun.COM 		if (xpv_nr_phys_cpus() < 4)
9955084Sjohnlev 			break;
9965084Sjohnlev #else
9975893Sesaxe 		if (opteron_get_nnodes() * cpuid_get_ncpu_per_chip(cpu) < 4)
9983446Smrj 			break;
9995084Sjohnlev #endif
10003446Smrj 		/*
10013446Smrj 		 * Print a warning if neither of the workarounds for
10023446Smrj 		 * erratum 131 is present.
10033446Smrj 		 */
10043446Smrj 		if ((error = checked_rdmsr(msr, &nbcfg)) != 0) {
10053446Smrj 			msr_warning(cpu, "rd", msr, error);
10063446Smrj 			workaround_warning(cpu, 131);
10073446Smrj 			missing++;
10083446Smrj 		} else if ((nbcfg & wabits) == 0) {
10093446Smrj 			opteron_erratum_131++;
10103446Smrj 		} else {
10113446Smrj 			/* cannot have both workarounds set */
10123446Smrj 			ASSERT((nbcfg & wabits) != wabits);
1013359Skucharsk 		}
10143446Smrj #else
10153446Smrj 		workaround_warning(cpu, 131);
10163446Smrj 		missing++;
1017359Skucharsk #endif
10183446Smrj 	/*CONSTANTCONDITION*/
10193446Smrj 	} while (0);
1020938Sesaxe 
1021938Sesaxe 	/*
10223446Smrj 	 * This isn't really an erratum, but for convenience the
1023938Sesaxe 	 * detection/workaround code lives here and in cpuid_opteron_erratum.
1024938Sesaxe 	 */
1025938Sesaxe 	if (cpuid_opteron_erratum(cpu, 6336786) > 0) {
10263446Smrj #if defined(OPTERON_WORKAROUND_6336786)
1027938Sesaxe 		/*
1028938Sesaxe 		 * Disable C1-Clock ramping on multi-core/multi-processor
1029938Sesaxe 		 * K8 platforms to guard against TSC drift.
1030938Sesaxe 		 */
1031938Sesaxe 		if (opteron_workaround_6336786) {
1032938Sesaxe 			opteron_workaround_6336786++;
10335084Sjohnlev #if defined(__xpv)
10345084Sjohnlev 		} else if ((DOMAIN_IS_INITDOMAIN(xen_info) &&
103510175SStuart.Maybee@Sun.COM 		    xpv_nr_phys_cpus() > 1) ||
10365084Sjohnlev 		    opteron_workaround_6336786_UP) {
10375084Sjohnlev 			/*
10385893Sesaxe 			 * XXPV	Hmm.  We can't walk the Northbridges on
10395084Sjohnlev 			 *	the hypervisor; so just complain and drive
10405084Sjohnlev 			 *	on.  This probably needs to be fixed in
10415084Sjohnlev 			 *	the hypervisor itself.
10425084Sjohnlev 			 */
10435084Sjohnlev 			opteron_workaround_6336786++;
10445084Sjohnlev 			workaround_warning(cpu, 6336786);
10455084Sjohnlev #else	/* __xpv */
10465893Sesaxe 		} else if ((opteron_get_nnodes() *
10475894Sesaxe 		    cpuid_get_ncpu_per_chip(cpu) > 1) ||
1048938Sesaxe 		    opteron_workaround_6336786_UP) {
10495893Sesaxe 
10505893Sesaxe 			uint_t	node, nnodes;
10513446Smrj 			uint8_t data;
10523446Smrj 
10535893Sesaxe 			nnodes = opteron_get_nnodes();
10545893Sesaxe 			for (node = 0; node < nnodes; node++) {
1055938Sesaxe 				/*
1056938Sesaxe 				 * Clear PMM7[1:0] (function 3, offset 0x87)
1057938Sesaxe 				 * Northbridge device is the node id + 24.
1058938Sesaxe 				 */
1059938Sesaxe 				data = pci_getb_func(0, node + 24, 3, 0x87);
1060938Sesaxe 				data &= 0xFC;
1061938Sesaxe 				pci_putb_func(0, node + 24, 3, 0x87, data);
1062938Sesaxe 			}
1063938Sesaxe 			opteron_workaround_6336786++;
10645084Sjohnlev #endif	/* __xpv */
1065938Sesaxe 		}
10663446Smrj #else
10673446Smrj 		workaround_warning(cpu, 6336786);
10683446Smrj 		missing++;
1069938Sesaxe #endif
10703446Smrj 	}
10711582Skchow 
10721582Skchow 	/*LINTED*/
10731582Skchow 	/*
10741582Skchow 	 * Mutex primitives don't work as expected.
10751582Skchow 	 */
10761582Skchow 	if (cpuid_opteron_erratum(cpu, 6323525) > 0) {
10773446Smrj #if defined(OPTERON_WORKAROUND_6323525)
10781582Skchow 		/*
10793446Smrj 		 * This problem only occurs with 2 or more cores. If bit in
10806691Skchow 		 * MSR_AMD_BU_CFG set, then not applicable. The workaround
10811582Skchow 		 * is to patch the semaphone routines with the lfence
10821582Skchow 		 * instruction to provide necessary load memory barrier with
10831582Skchow 		 * possible subsequent read-modify-write ops.
10841582Skchow 		 *
10851582Skchow 		 * It is too early in boot to call the patch routine so
10861582Skchow 		 * set erratum variable to be done in startup_end().
10871582Skchow 		 */
10881582Skchow 		if (opteron_workaround_6323525) {
10891582Skchow 			opteron_workaround_6323525++;
10905084Sjohnlev #if defined(__xpv)
10915084Sjohnlev 		} else if (x86_feature & X86_SSE2) {
10925084Sjohnlev 			if (DOMAIN_IS_INITDOMAIN(xen_info)) {
10935084Sjohnlev 				/*
10945084Sjohnlev 				 * XXPV	Use dom0_msr here when extended
10955084Sjohnlev 				 *	operations are supported?
10965084Sjohnlev 				 */
109710175SStuart.Maybee@Sun.COM 				if (xpv_nr_phys_cpus() > 1)
10985084Sjohnlev 					opteron_workaround_6323525++;
10995084Sjohnlev 			} else {
11005084Sjohnlev 				/*
11015084Sjohnlev 				 * We have no way to tell how many physical
11025084Sjohnlev 				 * cpus there are, or even if this processor
11035084Sjohnlev 				 * has the problem, so enable the workaround
11045084Sjohnlev 				 * unconditionally (at some performance cost).
11055084Sjohnlev 				 */
11065084Sjohnlev 				opteron_workaround_6323525++;
11075084Sjohnlev 			}
11085084Sjohnlev #else	/* __xpv */
11095893Sesaxe 		} else if ((x86_feature & X86_SSE2) && ((opteron_get_nnodes() *
11103446Smrj 		    cpuid_get_ncpu_per_chip(cpu)) > 1)) {
11118374SKit.Chow@Sun.COM 			if ((xrdmsr(MSR_AMD_BU_CFG) & (UINT64_C(1) << 33)) == 0)
11121582Skchow 				opteron_workaround_6323525++;
11135084Sjohnlev #endif	/* __xpv */
11141582Skchow 		}
11153446Smrj #else
11163446Smrj 		workaround_warning(cpu, 6323525);
11173446Smrj 		missing++;
11183446Smrj #endif
11191582Skchow 	}
11203446Smrj 
11216691Skchow 	missing += do_erratum_298(cpu);
11226691Skchow 
11235084Sjohnlev #ifdef __xpv
11245084Sjohnlev 	return (0);
11255084Sjohnlev #else
11260Sstevel@tonic-gate 	return (missing);
11275084Sjohnlev #endif
11280Sstevel@tonic-gate }
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate void
11310Sstevel@tonic-gate workaround_errata_end()
11320Sstevel@tonic-gate {
11333446Smrj #if defined(OPTERON_ERRATUM_88)
11343446Smrj 	if (opteron_erratum_88)
11353446Smrj 		workaround_applied(88);
11363446Smrj #endif
11373446Smrj #if defined(OPTERON_ERRATUM_91)
11383446Smrj 	if (opteron_erratum_91)
11393446Smrj 		workaround_applied(91);
11403446Smrj #endif
11413446Smrj #if defined(OPTERON_ERRATUM_93)
11423446Smrj 	if (opteron_erratum_93)
11433446Smrj 		workaround_applied(93);
11443446Smrj #endif
11453446Smrj #if defined(OPTERON_ERRATUM_95)
11463446Smrj 	if (opteron_erratum_95)
11473446Smrj 		workaround_applied(95);
11483446Smrj #endif
11493446Smrj #if defined(OPTERON_ERRATUM_100)
11503446Smrj 	if (opteron_erratum_100)
11513446Smrj 		workaround_applied(100);
11523446Smrj #endif
11533446Smrj #if defined(OPTERON_ERRATUM_108)
11543446Smrj 	if (opteron_erratum_108)
11553446Smrj 		workaround_applied(108);
11563446Smrj #endif
11570Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
11580Sstevel@tonic-gate 	if (opteron_erratum_109) {
1159359Skucharsk 		cmn_err(CE_WARN,
1160359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1161359Skucharsk 		    " processor\nerratum 109 was not detected; updating your"
1162359Skucharsk 		    " system's BIOS to a version\ncontaining this"
1163359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
1164359Skucharsk 		    " system\noperation may occur.\n");
11650Sstevel@tonic-gate 	}
11663446Smrj #endif
11673446Smrj #if defined(OPTERON_ERRATUM_121)
11683446Smrj 	if (opteron_erratum_121)
11693446Smrj 		workaround_applied(121);
11703446Smrj #endif
11713446Smrj #if defined(OPTERON_ERRATUM_122)
11723446Smrj 	if (opteron_erratum_122)
11733446Smrj 		workaround_applied(122);
11743446Smrj #endif
11750Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
11760Sstevel@tonic-gate 	if (opteron_erratum_123) {
1177359Skucharsk 		cmn_err(CE_WARN,
1178359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1179359Skucharsk 		    " processor\nerratum 123 was not detected; updating your"
1180359Skucharsk 		    " system's BIOS to a version\ncontaining this"
1181359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
1182359Skucharsk 		    " system\noperation may occur.\n");
11830Sstevel@tonic-gate 	}
11843446Smrj #endif
1185359Skucharsk #if defined(OPTERON_ERRATUM_131)
1186359Skucharsk 	if (opteron_erratum_131) {
1187359Skucharsk 		cmn_err(CE_WARN,
1188359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1189359Skucharsk 		    " processor\nerratum 131 was not detected; updating your"
1190359Skucharsk 		    " system's BIOS to a version\ncontaining this"
1191359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
1192359Skucharsk 		    " system\noperation may occur.\n");
1193359Skucharsk 	}
11943446Smrj #endif
11953446Smrj #if defined(OPTERON_WORKAROUND_6336786)
11963446Smrj 	if (opteron_workaround_6336786)
11973446Smrj 		workaround_applied(6336786);
11983446Smrj #endif
11993446Smrj #if defined(OPTERON_WORKAROUND_6323525)
12003446Smrj 	if (opteron_workaround_6323525)
12013446Smrj 		workaround_applied(6323525);
12023446Smrj #endif
12036691Skchow #if defined(OPTERON_ERRATUM_298)
12046691Skchow 	if (opteron_erratum_298) {
12056691Skchow 		cmn_err(CE_WARN,
12066691Skchow 		    "BIOS microcode patch for AMD 64/Opteron(tm)"
12076691Skchow 		    " processor\nerratum 298 was not detected; updating your"
12086691Skchow 		    " system's BIOS to a version\ncontaining this"
12096691Skchow 		    " microcode patch is HIGHLY recommended or erroneous"
12106691Skchow 		    " system\noperation may occur.\n");
12116691Skchow 	}
12126691Skchow #endif
12130Sstevel@tonic-gate }
12140Sstevel@tonic-gate 
12153446Smrj static cpuset_t procset;
12163446Smrj 
12173446Smrj /*
12183446Smrj  * Start a single cpu, assuming that the kernel context is available
12193446Smrj  * to successfully start another cpu.
12203446Smrj  *
12213446Smrj  * (For example, real mode code is mapped into the right place
12223446Smrj  * in memory and is ready to be run.)
12233446Smrj  */
12243446Smrj int
12253446Smrj start_cpu(processorid_t who)
12263446Smrj {
12273446Smrj 	void *ctx;
12283446Smrj 	cpu_t *cp;
12293446Smrj 	int delays;
12303446Smrj 	int error = 0;
12313446Smrj 
12323446Smrj 	ASSERT(who != 0);
12333446Smrj 
12343446Smrj 	/*
12353446Smrj 	 * Check if there's at least a Mbyte of kmem available
12363446Smrj 	 * before attempting to start the cpu.
12373446Smrj 	 */
12383446Smrj 	if (kmem_avail() < 1024 * 1024) {
12393446Smrj 		/*
12403446Smrj 		 * Kick off a reap in case that helps us with
12413446Smrj 		 * later attempts ..
12423446Smrj 		 */
12433446Smrj 		kmem_reap();
12443446Smrj 		return (ENOMEM);
12453446Smrj 	}
12463446Smrj 
12473446Smrj 	cp = mp_startup_init(who);
12483446Smrj 	if ((ctx = mach_cpucontext_alloc(cp)) == NULL ||
12493446Smrj 	    (error = mach_cpu_start(cp, ctx)) != 0) {
12503446Smrj 
12513446Smrj 		/*
12523446Smrj 		 * Something went wrong before we even started it
12533446Smrj 		 */
12543446Smrj 		if (ctx)
12553446Smrj 			cmn_err(CE_WARN,
12563446Smrj 			    "cpu%d: failed to start error %d",
12573446Smrj 			    cp->cpu_id, error);
12583446Smrj 		else
12593446Smrj 			cmn_err(CE_WARN,
12603446Smrj 			    "cpu%d: failed to allocate context", cp->cpu_id);
12610Sstevel@tonic-gate 
12623446Smrj 		if (ctx)
12633446Smrj 			mach_cpucontext_free(cp, ctx, error);
12643446Smrj 		else
12653446Smrj 			error = EAGAIN;		/* hmm. */
12663446Smrj 		mp_startup_fini(cp, error);
12673446Smrj 		return (error);
12683446Smrj 	}
12693446Smrj 
12703446Smrj 	for (delays = 0; !CPU_IN_SET(procset, who); delays++) {
12713446Smrj 		if (delays == 500) {
12723446Smrj 			/*
12733446Smrj 			 * After five seconds, things are probably looking
12743446Smrj 			 * a bit bleak - explain the hang.
12753446Smrj 			 */
12763446Smrj 			cmn_err(CE_NOTE, "cpu%d: started, "
12773446Smrj 			    "but not running in the kernel yet", who);
12783446Smrj 		} else if (delays > 2000) {
12793446Smrj 			/*
12803446Smrj 			 * We waited at least 20 seconds, bail ..
12813446Smrj 			 */
12823446Smrj 			error = ETIMEDOUT;
12833446Smrj 			cmn_err(CE_WARN, "cpu%d: timed out", who);
12843446Smrj 			mach_cpucontext_free(cp, ctx, error);
12853446Smrj 			mp_startup_fini(cp, error);
12863446Smrj 			return (error);
12873446Smrj 		}
12883446Smrj 
12893446Smrj 		/*
12903446Smrj 		 * wait at least 10ms, then check again..
12913446Smrj 		 */
12923446Smrj 		delay(USEC_TO_TICK_ROUNDUP(10000));
12933446Smrj 	}
12943446Smrj 
12953446Smrj 	mach_cpucontext_free(cp, ctx, 0);
12963446Smrj 
12975084Sjohnlev #ifndef __xpv
12983446Smrj 	if (tsc_gethrtime_enable)
12993446Smrj 		tsc_sync_master(who);
13005084Sjohnlev #endif
13013446Smrj 
13023446Smrj 	if (dtrace_cpu_init != NULL) {
13033446Smrj 		/*
13043446Smrj 		 * DTrace CPU initialization expects cpu_lock to be held.
13053446Smrj 		 */
13063446Smrj 		mutex_enter(&cpu_lock);
13073446Smrj 		(*dtrace_cpu_init)(who);
13083446Smrj 		mutex_exit(&cpu_lock);
13093446Smrj 	}
13103446Smrj 
13113446Smrj 	while (!CPU_IN_SET(cpu_ready_set, who))
13123446Smrj 		delay(1);
13133446Smrj 
13143446Smrj 	return (0);
13153446Smrj }
13163446Smrj 
13172006Sandrei 
13180Sstevel@tonic-gate /*ARGSUSED*/
13190Sstevel@tonic-gate void
13200Sstevel@tonic-gate start_other_cpus(int cprboot)
13210Sstevel@tonic-gate {
13223446Smrj 	uint_t who;
13233446Smrj 	uint_t skipped = 0;
13243446Smrj 	uint_t bootcpuid = 0;
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	/*
13270Sstevel@tonic-gate 	 * Initialize our own cpu_info.
13280Sstevel@tonic-gate 	 */
13290Sstevel@tonic-gate 	init_cpu_info(CPU);
13300Sstevel@tonic-gate 
13317656SSherry.Moore@Sun.COM 	cmn_err(CE_CONT, "?cpu%d: %s\n", CPU->cpu_id, CPU->cpu_idstr);
13327656SSherry.Moore@Sun.COM 	cmn_err(CE_CONT, "?cpu%d: %s\n", CPU->cpu_id, CPU->cpu_brandstr);
13337656SSherry.Moore@Sun.COM 
13340Sstevel@tonic-gate 	/*
13350Sstevel@tonic-gate 	 * Initialize our syscall handlers
13360Sstevel@tonic-gate 	 */
13370Sstevel@tonic-gate 	init_cpu_syscall(CPU);
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	/*
13403446Smrj 	 * Take the boot cpu out of the mp_cpus set because we know
13413446Smrj 	 * it's already running.  Add it to the cpu_ready_set for
13423446Smrj 	 * precisely the same reason.
13433446Smrj 	 */
13443446Smrj 	CPUSET_DEL(mp_cpus, bootcpuid);
13453446Smrj 	CPUSET_ADD(cpu_ready_set, bootcpuid);
13463446Smrj 
13473446Smrj 	/*
13480Sstevel@tonic-gate 	 * if only 1 cpu or not using MP, skip the rest of this
13490Sstevel@tonic-gate 	 */
13503446Smrj 	if (CPUSET_ISNULL(mp_cpus) || use_mp == 0) {
13510Sstevel@tonic-gate 		if (use_mp == 0)
13520Sstevel@tonic-gate 			cmn_err(CE_CONT, "?***** Not in MP mode\n");
13530Sstevel@tonic-gate 		goto done;
13540Sstevel@tonic-gate 	}
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	/*
13570Sstevel@tonic-gate 	 * perform such initialization as is needed
13580Sstevel@tonic-gate 	 * to be able to take CPUs on- and off-line.
13590Sstevel@tonic-gate 	 */
13600Sstevel@tonic-gate 	cpu_pause_init();
13610Sstevel@tonic-gate 
13629489SJoe.Bonasera@sun.com 	xc_init_cpu(CPU);		/* initialize processor crosscalls */
13630Sstevel@tonic-gate 
13643446Smrj 	if (mach_cpucontext_init() != 0)
13650Sstevel@tonic-gate 		goto done;
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	flushes_require_xcalls = 1;
13680Sstevel@tonic-gate 
13692575Snf202958 	/*
13702575Snf202958 	 * We lock our affinity to the master CPU to ensure that all slave CPUs
13712575Snf202958 	 * do their TSC syncs with the same CPU.
13722575Snf202958 	 */
13730Sstevel@tonic-gate 	affinity_set(CPU_CURRENT);
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	for (who = 0; who < NCPU; who++) {
13762575Snf202958 
13772006Sandrei 		if (!CPU_IN_SET(mp_cpus, who))
13782006Sandrei 			continue;
13793446Smrj 		ASSERT(who != bootcpuid);
13802006Sandrei 		if (ncpus >= max_ncpus) {
13812006Sandrei 			skipped = who;
13820Sstevel@tonic-gate 			continue;
13832006Sandrei 		}
13843446Smrj 		if (start_cpu(who) != 0)
13853446Smrj 			CPUSET_DEL(mp_cpus, who);
13868803SJonathan.Haslam@Sun.COM 
13878803SJonathan.Haslam@Sun.COM 		mutex_enter(&cpu_lock);
13888803SJonathan.Haslam@Sun.COM 		cpu_state_change_notify(who, CPU_SETUP);
13898803SJonathan.Haslam@Sun.COM 		mutex_exit(&cpu_lock);
13900Sstevel@tonic-gate 	}
13910Sstevel@tonic-gate 
13924581Ssherrym 	/* Free the space allocated to hold the microcode file */
13937605SMark.Johnson@Sun.COM 	ucode_cleanup();
13944581Ssherrym 
13950Sstevel@tonic-gate 	affinity_clear();
13960Sstevel@tonic-gate 
13972006Sandrei 	if (skipped) {
13982006Sandrei 		cmn_err(CE_NOTE,
13993446Smrj 		    "System detected %d cpus, but "
14003446Smrj 		    "only %d cpu(s) were enabled during boot.",
14012006Sandrei 		    skipped + 1, ncpus);
14022006Sandrei 		cmn_err(CE_NOTE,
14032006Sandrei 		    "Use \"boot-ncpus\" parameter to enable more CPU(s). "
14042006Sandrei 		    "See eeprom(1M).");
14052006Sandrei 	}
14062006Sandrei 
14070Sstevel@tonic-gate done:
1408*11120SMark.Johnson@Sun.COM 	if (get_hwenv() == HW_NATIVE)
1409*11120SMark.Johnson@Sun.COM 		workaround_errata_end();
14103446Smrj 	mach_cpucontext_fini();
14111642Sgavinm 
14121642Sgavinm 	cmi_post_mpstartup();
14130Sstevel@tonic-gate }
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate /*
14160Sstevel@tonic-gate  * Dummy functions - no i86pc platforms support dynamic cpu allocation.
14170Sstevel@tonic-gate  */
14180Sstevel@tonic-gate /*ARGSUSED*/
14190Sstevel@tonic-gate int
14200Sstevel@tonic-gate mp_cpu_configure(int cpuid)
14210Sstevel@tonic-gate {
14220Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
14230Sstevel@tonic-gate }
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate /*ARGSUSED*/
14260Sstevel@tonic-gate int
14270Sstevel@tonic-gate mp_cpu_unconfigure(int cpuid)
14280Sstevel@tonic-gate {
14290Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
14300Sstevel@tonic-gate }
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate /*
14330Sstevel@tonic-gate  * Startup function for 'other' CPUs (besides boot cpu).
14342985Sdmick  * Called from real_mode_start.
14351251Skchow  *
14361251Skchow  * WARNING: until CPU_READY is set, mp_startup and routines called by
14371251Skchow  * mp_startup should not call routines (e.g. kmem_free) that could call
14381251Skchow  * hat_unload which requires CPU_READY to be set.
14390Sstevel@tonic-gate  */
14400Sstevel@tonic-gate void
14410Sstevel@tonic-gate mp_startup(void)
14420Sstevel@tonic-gate {
14430Sstevel@tonic-gate 	struct cpu *cp = CPU;
14440Sstevel@tonic-gate 	uint_t new_x86_feature;
14459637SRandy.Fishel@Sun.COM 	extern void cpu_event_init_cpu(cpu_t *);
14468906SEric.Saxe@Sun.COM #ifndef __xpv
14478906SEric.Saxe@Sun.COM 	extern void cpupm_init(cpu_t *);
14488906SEric.Saxe@Sun.COM #endif
14499370SKuriakose.Kuruvilla@Sun.COM 	const char *fmt = "?cpu%d: %b\n";
14500Sstevel@tonic-gate 
14512985Sdmick 	/*
14523021Sdmick 	 * We need to get TSC on this proc synced (i.e., any delta
14533021Sdmick 	 * from cpu0 accounted for) as soon as we can, because many
14543021Sdmick 	 * many things use gethrtime/pc_gethrestime, including
14553021Sdmick 	 * interrupts, cmn_err, etc.
14563021Sdmick 	 */
14573021Sdmick 
14583021Sdmick 	/* Let cpu0 continue into tsc_sync_master() */
14593021Sdmick 	CPUSET_ATOMIC_ADD(procset, cp->cpu_id);
14603021Sdmick 
14615084Sjohnlev #ifndef __xpv
14623021Sdmick 	if (tsc_gethrtime_enable)
14633021Sdmick 		tsc_sync_slave();
14645084Sjohnlev #endif
14653021Sdmick 
14663021Sdmick 	/*
14672985Sdmick 	 * Once this was done from assembly, but it's safer here; if
14682985Sdmick 	 * it blocks, we need to be able to swtch() to and from, and
14692985Sdmick 	 * since we get here by calling t_pc, we need to do that call
14702985Sdmick 	 * before swtch() overwrites it.
14712985Sdmick 	 */
14722985Sdmick 	(void) (*ap_mlsetup)();
14732985Sdmick 
14740Sstevel@tonic-gate 	new_x86_feature = cpuid_pass1(cp);
14750Sstevel@tonic-gate 
14765084Sjohnlev #ifndef __xpv
14770Sstevel@tonic-gate 	/*
14785159Sjohnlev 	 * Program this cpu's PAT
14790Sstevel@tonic-gate 	 */
14805159Sjohnlev 	if (x86_feature & X86_PAT)
14815159Sjohnlev 		pat_sync();
14825084Sjohnlev #endif
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 	/*
14853446Smrj 	 * Set up TSC_AUX to contain the cpuid for this processor
14863446Smrj 	 * for the rdtscp instruction.
14873446Smrj 	 */
14883446Smrj 	if (x86_feature & X86_TSCP)
14893446Smrj 		(void) wrmsr(MSR_AMD_TSCAUX, cp->cpu_id);
14903446Smrj 
14913446Smrj 	/*
14920Sstevel@tonic-gate 	 * Initialize this CPU's syscall handlers
14930Sstevel@tonic-gate 	 */
14940Sstevel@tonic-gate 	init_cpu_syscall(cp);
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 	/*
14970Sstevel@tonic-gate 	 * Enable interrupts with spl set to LOCK_LEVEL. LOCK_LEVEL is the
14980Sstevel@tonic-gate 	 * highest level at which a routine is permitted to block on
14990Sstevel@tonic-gate 	 * an adaptive mutex (allows for cpu poke interrupt in case
15000Sstevel@tonic-gate 	 * the cpu is blocked on a mutex and halts). Setting LOCK_LEVEL blocks
15010Sstevel@tonic-gate 	 * device interrupts that may end up in the hat layer issuing cross
15020Sstevel@tonic-gate 	 * calls before CPU_READY is set.
15030Sstevel@tonic-gate 	 */
15043446Smrj 	splx(ipltospl(LOCK_LEVEL));
15053446Smrj 	sti();
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 	/*
15080Sstevel@tonic-gate 	 * Do a sanity check to make sure this new CPU is a sane thing
15090Sstevel@tonic-gate 	 * to add to the collection of processors running this system.
15100Sstevel@tonic-gate 	 *
15110Sstevel@tonic-gate 	 * XXX	Clearly this needs to get more sophisticated, if x86
15120Sstevel@tonic-gate 	 * systems start to get built out of heterogenous CPUs; as is
15130Sstevel@tonic-gate 	 * likely to happen once the number of processors in a configuration
15140Sstevel@tonic-gate 	 * gets large enough.
15150Sstevel@tonic-gate 	 */
15160Sstevel@tonic-gate 	if ((x86_feature & new_x86_feature) != x86_feature) {
15179370SKuriakose.Kuruvilla@Sun.COM 		cmn_err(CE_CONT, fmt, cp->cpu_id, new_x86_feature,
15189370SKuriakose.Kuruvilla@Sun.COM 		    FMT_X86_FEATURE);
15190Sstevel@tonic-gate 		cmn_err(CE_WARN, "cpu%d feature mismatch", cp->cpu_id);
15200Sstevel@tonic-gate 	}
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 	/*
15234481Sbholler 	 * We do not support cpus with mixed monitor/mwait support if the
15244481Sbholler 	 * boot cpu supports monitor/mwait.
15254481Sbholler 	 */
15264481Sbholler 	if ((x86_feature & ~new_x86_feature) & X86_MWAIT)
15274481Sbholler 		panic("unsupported mixed cpu monitor/mwait support detected");
15284481Sbholler 
15294481Sbholler 	/*
15300Sstevel@tonic-gate 	 * We could be more sophisticated here, and just mark the CPU
15310Sstevel@tonic-gate 	 * as "faulted" but at this point we'll opt for the easier
15329637SRandy.Fishel@Sun.COM 	 * answer of dying horribly.  Provided the boot cpu is ok,
15330Sstevel@tonic-gate 	 * the system can be recovered by booting with use_mp set to zero.
15340Sstevel@tonic-gate 	 */
15350Sstevel@tonic-gate 	if (workaround_errata(cp) != 0)
15360Sstevel@tonic-gate 		panic("critical workaround(s) missing for cpu%d", cp->cpu_id);
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	cpuid_pass2(cp);
15390Sstevel@tonic-gate 	cpuid_pass3(cp);
15400Sstevel@tonic-gate 	(void) cpuid_pass4(cp);
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	init_cpu_info(cp);
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
15450Sstevel@tonic-gate 
15466749Ssherrym 	cp->cpu_flags |= CPU_RUNNING | CPU_READY | CPU_EXISTS;
15472575Snf202958 
15487656SSherry.Moore@Sun.COM 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_idstr);
15497656SSherry.Moore@Sun.COM 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_brandstr);
15507656SSherry.Moore@Sun.COM 
15512575Snf202958 	if (dtrace_cpu_init != NULL) {
15522575Snf202958 		(*dtrace_cpu_init)(cp->cpu_id);
15532575Snf202958 	}
15542575Snf202958 
15554581Ssherrym 	/*
15564581Ssherrym 	 * Fill out cpu_ucode_info.  Update microcode if necessary.
15574581Ssherrym 	 */
15584581Ssherrym 	ucode_check(cp);
15594581Ssherrym 
15600Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
15610Sstevel@tonic-gate 
15628930SBill.Holler@Sun.COM 	post_startup_cpu_fixups();
15638930SBill.Holler@Sun.COM 
15643029Ssethg 	/*
15653029Ssethg 	 * Enable preemption here so that contention for any locks acquired
15663029Ssethg 	 * later in mp_startup may be preempted if the thread owning those
15679637SRandy.Fishel@Sun.COM 	 * locks is continuously executing on other CPUs (for example, this
15683029Ssethg 	 * CPU must be preemptible to allow other CPUs to pause it during their
15693029Ssethg 	 * startup phases).  It's safe to enable preemption here because the
15703029Ssethg 	 * CPU state is pretty-much fully constructed.
15713029Ssethg 	 */
15723029Ssethg 	curthread->t_preempt = 0;
15733029Ssethg 
15741482Ssethg 	/* The base spl should still be at LOCK LEVEL here */
15751482Ssethg 	ASSERT(cp->cpu_base_spl == ipltospl(LOCK_LEVEL));
15761482Ssethg 	set_base_spl();		/* Restore the spl to its proper value */
15771482Ssethg 
15789637SRandy.Fishel@Sun.COM 	cpu_event_init_cpu(cp);
15798906SEric.Saxe@Sun.COM #ifndef __xpv
15808906SEric.Saxe@Sun.COM 	cpupm_init(cp);
15818906SEric.Saxe@Sun.COM #endif
15828906SEric.Saxe@Sun.COM 
15838906SEric.Saxe@Sun.COM 	/*
15848906SEric.Saxe@Sun.COM 	 * Processor group initialization for this CPU is dependent on the
15858906SEric.Saxe@Sun.COM 	 * cpuid probing, which must be done in the context of the current
15868906SEric.Saxe@Sun.COM 	 * CPU, as well as the CPU's device node initialization (for ACPI).
15878906SEric.Saxe@Sun.COM 	 */
15888906SEric.Saxe@Sun.COM 	mutex_enter(&cpu_lock);
15898906SEric.Saxe@Sun.COM 	pghw_physid_create(cp);
15908906SEric.Saxe@Sun.COM 	pg_cpu_init(cp);
15918906SEric.Saxe@Sun.COM 	pg_cmt_cpu_startup(cp);
15928906SEric.Saxe@Sun.COM 	mutex_exit(&cpu_lock);
15938906SEric.Saxe@Sun.COM 
15946749Ssherrym 	/* Enable interrupts */
15956749Ssherrym 	(void) spl0();
15968906SEric.Saxe@Sun.COM 
15976749Ssherrym 	mutex_enter(&cpu_lock);
15986749Ssherrym 	cpu_enable_intr(cp);
15996749Ssherrym 	cpu_add_active(cp);
16006749Ssherrym 	mutex_exit(&cpu_lock);
16016749Ssherrym 
16025254Sgavinm #ifndef __xpv
16035254Sgavinm 	{
16045254Sgavinm 		/*
16055254Sgavinm 		 * Set up the CPU module for this CPU.  This can't be done
16065254Sgavinm 		 * before this CPU is made CPU_READY, because we may (in
16075254Sgavinm 		 * heterogeneous systems) need to go load another CPU module.
16085254Sgavinm 		 * The act of attempting to load a module may trigger a
16095254Sgavinm 		 * cross-call, which will ASSERT unless this cpu is CPU_READY.
16105254Sgavinm 		 */
16115254Sgavinm 		cmi_hdl_t hdl;
16121414Scindi 
16135254Sgavinm 		if ((hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
16147532SSean.Ye@Sun.COM 		    cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL) {
16155254Sgavinm 			if (x86_feature & X86_MCA)
16165254Sgavinm 				cmi_mca_init(hdl);
16175254Sgavinm 		}
16185254Sgavinm 	}
16195254Sgavinm #endif /* __xpv */
16201414Scindi 
16210Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
16223446Smrj 		kdi_cpu_init();
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 	/*
16250Sstevel@tonic-gate 	 * Setting the bit in cpu_ready_set must be the last operation in
16260Sstevel@tonic-gate 	 * processor initialization; the boot CPU will continue to boot once
16270Sstevel@tonic-gate 	 * it sees this bit set for all active CPUs.
16280Sstevel@tonic-gate 	 */
16290Sstevel@tonic-gate 	CPUSET_ATOMIC_ADD(cpu_ready_set, cp->cpu_id);
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 	/*
16320Sstevel@tonic-gate 	 * Because mp_startup() gets fired off after init() starts, we
16330Sstevel@tonic-gate 	 * can't use the '?' trick to do 'boot -v' printing - so we
16340Sstevel@tonic-gate 	 * always direct the 'cpu .. online' messages to the log.
16350Sstevel@tonic-gate 	 */
16360Sstevel@tonic-gate 	cmn_err(CE_CONT, "!cpu%d initialization complete - online\n",
16370Sstevel@tonic-gate 	    cp->cpu_id);
16380Sstevel@tonic-gate 
16399652SMichael.Corcoran@Sun.COM 	(void) mach_cpu_create_device_node(cp, NULL);
16409652SMichael.Corcoran@Sun.COM 
16410Sstevel@tonic-gate 	/*
16420Sstevel@tonic-gate 	 * Now we are done with the startup thread, so free it up.
16430Sstevel@tonic-gate 	 */
16440Sstevel@tonic-gate 	thread_exit();
16450Sstevel@tonic-gate 	panic("mp_startup: cannot return");
16460Sstevel@tonic-gate 	/*NOTREACHED*/
16470Sstevel@tonic-gate }
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate /*
16510Sstevel@tonic-gate  * Start CPU on user request.
16520Sstevel@tonic-gate  */
16530Sstevel@tonic-gate /* ARGSUSED */
16540Sstevel@tonic-gate int
16550Sstevel@tonic-gate mp_cpu_start(struct cpu *cp)
16560Sstevel@tonic-gate {
16570Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16580Sstevel@tonic-gate 	return (0);
16590Sstevel@tonic-gate }
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate /*
16620Sstevel@tonic-gate  * Stop CPU on user request.
16630Sstevel@tonic-gate  */
16640Sstevel@tonic-gate /* ARGSUSED */
16650Sstevel@tonic-gate int
16660Sstevel@tonic-gate mp_cpu_stop(struct cpu *cp)
16670Sstevel@tonic-gate {
16681389Sdmick 	extern int cbe_psm_timer_mode;
16690Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16701389Sdmick 
16715084Sjohnlev #ifdef __xpv
16725084Sjohnlev 	/*
16735084Sjohnlev 	 * We can't offline vcpu0.
16745084Sjohnlev 	 */
16755084Sjohnlev 	if (cp->cpu_id == 0)
16765084Sjohnlev 		return (EBUSY);
16775084Sjohnlev #endif
16785084Sjohnlev 
16791389Sdmick 	/*
16801389Sdmick 	 * If TIMER_PERIODIC mode is used, CPU0 is the one running it;
16811389Sdmick 	 * can't stop it.  (This is true only for machines with no TSC.)
16821389Sdmick 	 */
16831389Sdmick 
16841389Sdmick 	if ((cbe_psm_timer_mode == TIMER_PERIODIC) && (cp->cpu_id == 0))
16855084Sjohnlev 		return (EBUSY);
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	return (0);
16880Sstevel@tonic-gate }
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate /*
16910Sstevel@tonic-gate  * Take the specified CPU out of participation in interrupts.
16920Sstevel@tonic-gate  */
16930Sstevel@tonic-gate int
16940Sstevel@tonic-gate cpu_disable_intr(struct cpu *cp)
16950Sstevel@tonic-gate {
16960Sstevel@tonic-gate 	if (psm_disable_intr(cp->cpu_id) != DDI_SUCCESS)
16970Sstevel@tonic-gate 		return (EBUSY);
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 	cp->cpu_flags &= ~CPU_ENABLE;
17000Sstevel@tonic-gate 	return (0);
17010Sstevel@tonic-gate }
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate /*
17040Sstevel@tonic-gate  * Allow the specified CPU to participate in interrupts.
17050Sstevel@tonic-gate  */
17060Sstevel@tonic-gate void
17070Sstevel@tonic-gate cpu_enable_intr(struct cpu *cp)
17080Sstevel@tonic-gate {
17090Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
17100Sstevel@tonic-gate 	cp->cpu_flags |= CPU_ENABLE;
17110Sstevel@tonic-gate 	psm_enable_intr(cp->cpu_id);
17120Sstevel@tonic-gate }
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 
17155254Sgavinm /*ARGSUSED*/
17160Sstevel@tonic-gate void
17170Sstevel@tonic-gate mp_cpu_faulted_enter(struct cpu *cp)
17181414Scindi {
17195254Sgavinm #ifndef __xpv
17205254Sgavinm 	cmi_hdl_t hdl = cmi_hdl_lookup(CMI_HDL_NATIVE, cmi_ntv_hwchipid(cp),
17215254Sgavinm 	    cmi_ntv_hwcoreid(cp), cmi_ntv_hwstrandid(cp));
17225254Sgavinm 
17235254Sgavinm 	if (hdl != NULL) {
17245254Sgavinm 		cmi_faulted_enter(hdl);
17255254Sgavinm 		cmi_hdl_rele(hdl);
17265254Sgavinm 	}
17275254Sgavinm #endif
17281414Scindi }
17290Sstevel@tonic-gate 
17305254Sgavinm /*ARGSUSED*/
17310Sstevel@tonic-gate void
17320Sstevel@tonic-gate mp_cpu_faulted_exit(struct cpu *cp)
17331414Scindi {
17345254Sgavinm #ifndef __xpv
17355254Sgavinm 	cmi_hdl_t hdl = cmi_hdl_lookup(CMI_HDL_NATIVE, cmi_ntv_hwchipid(cp),
17365254Sgavinm 	    cmi_ntv_hwcoreid(cp), cmi_ntv_hwstrandid(cp));
17375254Sgavinm 
17385254Sgavinm 	if (hdl != NULL) {
17395254Sgavinm 		cmi_faulted_exit(hdl);
17405254Sgavinm 		cmi_hdl_rele(hdl);
17415254Sgavinm 	}
17425254Sgavinm #endif
17431414Scindi }
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate /*
17460Sstevel@tonic-gate  * The following two routines are used as context operators on threads belonging
17470Sstevel@tonic-gate  * to processes with a private LDT (see sysi86).  Due to the rarity of such
17480Sstevel@tonic-gate  * processes, these routines are currently written for best code readability and
17490Sstevel@tonic-gate  * organization rather than speed.  We could avoid checking x86_feature at every
17500Sstevel@tonic-gate  * context switch by installing different context ops, depending on the
17510Sstevel@tonic-gate  * x86_feature flags, at LDT creation time -- one for each combination of fast
17520Sstevel@tonic-gate  * syscall feature flags.
17530Sstevel@tonic-gate  */
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate /*ARGSUSED*/
17560Sstevel@tonic-gate void
17570Sstevel@tonic-gate cpu_fast_syscall_disable(void *arg)
17580Sstevel@tonic-gate {
17593446Smrj 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP))
17600Sstevel@tonic-gate 		cpu_sep_disable();
17613446Smrj 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC))
17620Sstevel@tonic-gate 		cpu_asysc_disable();
17630Sstevel@tonic-gate }
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate /*ARGSUSED*/
17660Sstevel@tonic-gate void
17670Sstevel@tonic-gate cpu_fast_syscall_enable(void *arg)
17680Sstevel@tonic-gate {
17693446Smrj 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP))
17700Sstevel@tonic-gate 		cpu_sep_enable();
17713446Smrj 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC))
17720Sstevel@tonic-gate 		cpu_asysc_enable();
17730Sstevel@tonic-gate }
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate static void
17760Sstevel@tonic-gate cpu_sep_enable(void)
17770Sstevel@tonic-gate {
17780Sstevel@tonic-gate 	ASSERT(x86_feature & X86_SEP);
17790Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
17800Sstevel@tonic-gate 
1781770Skucharsk 	wrmsr(MSR_INTC_SEP_CS, (uint64_t)(uintptr_t)KCS_SEL);
17820Sstevel@tonic-gate }
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate static void
17850Sstevel@tonic-gate cpu_sep_disable(void)
17860Sstevel@tonic-gate {
17870Sstevel@tonic-gate 	ASSERT(x86_feature & X86_SEP);
17880Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 	/*
17910Sstevel@tonic-gate 	 * Setting the SYSENTER_CS_MSR register to 0 causes software executing
17920Sstevel@tonic-gate 	 * the sysenter or sysexit instruction to trigger a #gp fault.
17930Sstevel@tonic-gate 	 */
17943446Smrj 	wrmsr(MSR_INTC_SEP_CS, 0);
17950Sstevel@tonic-gate }
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate static void
17980Sstevel@tonic-gate cpu_asysc_enable(void)
17990Sstevel@tonic-gate {
18000Sstevel@tonic-gate 	ASSERT(x86_feature & X86_ASYSC);
18010Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
18020Sstevel@tonic-gate 
1803770Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) |
1804770Skucharsk 	    (uint64_t)(uintptr_t)AMD_EFER_SCE);
18050Sstevel@tonic-gate }
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate static void
18080Sstevel@tonic-gate cpu_asysc_disable(void)
18090Sstevel@tonic-gate {
18100Sstevel@tonic-gate 	ASSERT(x86_feature & X86_ASYSC);
18110Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 	/*
18140Sstevel@tonic-gate 	 * Turn off the SCE (syscall enable) bit in the EFER register. Software
18150Sstevel@tonic-gate 	 * executing syscall or sysret with this bit off will incur a #ud trap.
18160Sstevel@tonic-gate 	 */
1817770Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) &
1818770Skucharsk 	    ~((uint64_t)(uintptr_t)AMD_EFER_SCE));
18190Sstevel@tonic-gate }
1820