xref: /onnv-gate/usr/src/uts/i86pc/os/mp_startup.c (revision 13136:67d1861e02c1)
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 /*
2312826Skuriakose.kuruvilla@oracle.com  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
2512004Sjiang.liu@intel.com /*
2612004Sjiang.liu@intel.com  * Copyright (c) 2010, Intel Corporation.
2712004Sjiang.liu@intel.com  * All rights reserved.
2812004Sjiang.liu@intel.com  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/thread.h>
320Sstevel@tonic-gate #include <sys/cpuvar.h>
3312004Sjiang.liu@intel.com #include <sys/cpu.h>
340Sstevel@tonic-gate #include <sys/t_lock.h>
350Sstevel@tonic-gate #include <sys/param.h>
360Sstevel@tonic-gate #include <sys/proc.h>
370Sstevel@tonic-gate #include <sys/disp.h>
380Sstevel@tonic-gate #include <sys/class.h>
390Sstevel@tonic-gate #include <sys/cmn_err.h>
400Sstevel@tonic-gate #include <sys/debug.h>
4112004Sjiang.liu@intel.com #include <sys/note.h>
420Sstevel@tonic-gate #include <sys/asm_linkage.h>
430Sstevel@tonic-gate #include <sys/x_call.h>
440Sstevel@tonic-gate #include <sys/systm.h>
450Sstevel@tonic-gate #include <sys/var.h>
460Sstevel@tonic-gate #include <sys/vtrace.h>
470Sstevel@tonic-gate #include <vm/hat.h>
480Sstevel@tonic-gate #include <vm/as.h>
490Sstevel@tonic-gate #include <vm/seg_kmem.h>
503446Smrj #include <vm/seg_kp.h>
510Sstevel@tonic-gate #include <sys/segments.h>
520Sstevel@tonic-gate #include <sys/kmem.h>
530Sstevel@tonic-gate #include <sys/stack.h>
540Sstevel@tonic-gate #include <sys/smp_impldefs.h>
550Sstevel@tonic-gate #include <sys/x86_archext.h>
560Sstevel@tonic-gate #include <sys/machsystm.h>
570Sstevel@tonic-gate #include <sys/traptrace.h>
580Sstevel@tonic-gate #include <sys/clock.h>
590Sstevel@tonic-gate #include <sys/cpc_impl.h>
603434Sesaxe #include <sys/pg.h>
613434Sesaxe #include <sys/cmt.h>
620Sstevel@tonic-gate #include <sys/dtrace.h>
630Sstevel@tonic-gate #include <sys/archsystm.h>
640Sstevel@tonic-gate #include <sys/fp.h>
650Sstevel@tonic-gate #include <sys/reboot.h>
663446Smrj #include <sys/kdi_machimpl.h>
670Sstevel@tonic-gate #include <vm/hat_i86.h>
6812004Sjiang.liu@intel.com #include <vm/vm_dep.h>
690Sstevel@tonic-gate #include <sys/memnode.h>
70938Sesaxe #include <sys/pci_cfgspace.h>
713446Smrj #include <sys/mach_mmu.h>
723446Smrj #include <sys/sysmacros.h>
735084Sjohnlev #if defined(__xpv)
745084Sjohnlev #include <sys/hypervisor.h>
755084Sjohnlev #endif
761414Scindi #include <sys/cpu_module.h>
770Sstevel@tonic-gate 
780Sstevel@tonic-gate struct cpu	cpus[1];			/* CPU data */
790Sstevel@tonic-gate struct cpu	*cpu[NCPU] = {&cpus[0]};	/* pointers to all CPUs */
8012004Sjiang.liu@intel.com struct cpu	*cpu_free_list;			/* list for released CPUs */
810Sstevel@tonic-gate cpu_core_t	cpu_core[NCPU];			/* cpu_core structures */
820Sstevel@tonic-gate 
8312004Sjiang.liu@intel.com #define	cpu_next_free	cpu_prev
8412004Sjiang.liu@intel.com 
850Sstevel@tonic-gate /*
863446Smrj  * Useful for disabling MP bring-up on a MP capable system.
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate int use_mp = 1;
890Sstevel@tonic-gate 
902006Sandrei /*
913446Smrj  * to be set by a PSM to indicate what cpus
923446Smrj  * are sitting around on the system.
932006Sandrei  */
943446Smrj cpuset_t mp_cpus;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * This variable is used by the hat layer to decide whether or not
980Sstevel@tonic-gate  * critical sections are needed to prevent race conditions.  For sun4m,
990Sstevel@tonic-gate  * this variable is set once enough MP initialization has been done in
1000Sstevel@tonic-gate  * order to allow cross calls.
1010Sstevel@tonic-gate  */
1023446Smrj int flushes_require_xcalls;
1036336Sbholler 
1046336Sbholler cpuset_t cpu_ready_set;		/* initialized in startup() */
1050Sstevel@tonic-gate 
10612004Sjiang.liu@intel.com static void mp_startup_boot(void);
10712004Sjiang.liu@intel.com static void mp_startup_hotplug(void);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate static void cpu_sep_enable(void);
1100Sstevel@tonic-gate static void cpu_sep_disable(void);
1110Sstevel@tonic-gate static void cpu_asysc_enable(void);
1120Sstevel@tonic-gate static void cpu_asysc_disable(void);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate  * Init CPU info - get CPU type info for processor_info system call.
1160Sstevel@tonic-gate  */
1170Sstevel@tonic-gate void
init_cpu_info(struct cpu * cp)1180Sstevel@tonic-gate init_cpu_info(struct cpu *cp)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	processor_info_t *pi = &cp->cpu_type_info;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	/*
1230Sstevel@tonic-gate 	 * Get clock-frequency property for the CPU.
1240Sstevel@tonic-gate 	 */
1250Sstevel@tonic-gate 	pi->pi_clock = cpu_freq;
1260Sstevel@tonic-gate 
1274667Smh27603 	/*
1284667Smh27603 	 * Current frequency in Hz.
1294667Smh27603 	 */
1304718Smh27603 	cp->cpu_curr_clock = cpu_freq_hz;
1314667Smh27603 
1329004SNapanda.Pemmaiah@Sun.COM 	/*
1339004SNapanda.Pemmaiah@Sun.COM 	 * Supported frequencies.
1349004SNapanda.Pemmaiah@Sun.COM 	 */
1359004SNapanda.Pemmaiah@Sun.COM 	if (cp->cpu_supp_freqs == NULL) {
1369004SNapanda.Pemmaiah@Sun.COM 		cpu_set_supp_freqs(cp, NULL);
1379004SNapanda.Pemmaiah@Sun.COM 	}
1389004SNapanda.Pemmaiah@Sun.COM 
1390Sstevel@tonic-gate 	(void) strcpy(pi->pi_processor_type, "i386");
1400Sstevel@tonic-gate 	if (fpu_exists)
1410Sstevel@tonic-gate 		(void) strcpy(pi->pi_fputypes, "i387 compatible");
1420Sstevel@tonic-gate 
14312004Sjiang.liu@intel.com 	cp->cpu_idstr = kmem_zalloc(CPU_IDSTRLEN, KM_SLEEP);
14412004Sjiang.liu@intel.com 	cp->cpu_brandstr = kmem_zalloc(CPU_IDSTRLEN, KM_SLEEP);
1450Sstevel@tonic-gate 
14612004Sjiang.liu@intel.com 	/*
14712004Sjiang.liu@intel.com 	 * If called for the BSP, cp is equal to current CPU.
14812004Sjiang.liu@intel.com 	 * For non-BSPs, cpuid info of cp is not ready yet, so use cpuid info
14912004Sjiang.liu@intel.com 	 * of current CPU as default values for cpu_idstr and cpu_brandstr.
15012004Sjiang.liu@intel.com 	 * They will be corrected in mp_startup_common() after cpuid_pass1()
15112004Sjiang.liu@intel.com 	 * has been invoked on target CPU.
15212004Sjiang.liu@intel.com 	 */
15312004Sjiang.liu@intel.com 	(void) cpuid_getidstr(CPU, cp->cpu_idstr, CPU_IDSTRLEN);
15412004Sjiang.liu@intel.com 	(void) cpuid_getbrandstr(CPU, cp->cpu_brandstr, CPU_IDSTRLEN);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate  * Configure syscall support on this CPU.
1590Sstevel@tonic-gate  */
1600Sstevel@tonic-gate /*ARGSUSED*/
1615295Srandyf void
init_cpu_syscall(struct cpu * cp)1620Sstevel@tonic-gate init_cpu_syscall(struct cpu *cp)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	kpreempt_disable();
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate #if defined(__amd64)
16712826Skuriakose.kuruvilla@oracle.com 	if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
16812826Skuriakose.kuruvilla@oracle.com 	    is_x86_feature(x86_featureset, X86FSET_ASYSC)) {
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate #if !defined(__lint)
1710Sstevel@tonic-gate 		/*
1720Sstevel@tonic-gate 		 * The syscall instruction imposes a certain ordering on
1730Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
1740Sstevel@tonic-gate 		 * here.
1750Sstevel@tonic-gate 		 */
1760Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
1770Sstevel@tonic-gate 		ASSERT(UDS_SEL == U32CS_SEL + 8);
1780Sstevel@tonic-gate 		ASSERT(UCS_SEL == U32CS_SEL + 16);
1790Sstevel@tonic-gate #endif
1800Sstevel@tonic-gate 		/*
1810Sstevel@tonic-gate 		 * Turn syscall/sysret extensions on.
1820Sstevel@tonic-gate 		 */
1830Sstevel@tonic-gate 		cpu_asysc_enable();
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 		/*
1860Sstevel@tonic-gate 		 * Program the magic registers ..
1870Sstevel@tonic-gate 		 */
1883446Smrj 		wrmsr(MSR_AMD_STAR,
1893446Smrj 		    ((uint64_t)(U32CS_SEL << 16 | KCS_SEL)) << 32);
190770Skucharsk 		wrmsr(MSR_AMD_LSTAR, (uint64_t)(uintptr_t)sys_syscall);
191770Skucharsk 		wrmsr(MSR_AMD_CSTAR, (uint64_t)(uintptr_t)sys_syscall32);
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 		/*
1940Sstevel@tonic-gate 		 * This list of flags is masked off the incoming
1950Sstevel@tonic-gate 		 * %rfl when we enter the kernel.
1960Sstevel@tonic-gate 		 */
197770Skucharsk 		wrmsr(MSR_AMD_SFMASK, (uint64_t)(uintptr_t)(PS_IE | PS_T));
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate #endif
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	/*
2020Sstevel@tonic-gate 	 * On 32-bit kernels, we use sysenter/sysexit because it's too
2030Sstevel@tonic-gate 	 * hard to use syscall/sysret, and it is more portable anyway.
2040Sstevel@tonic-gate 	 *
2050Sstevel@tonic-gate 	 * On 64-bit kernels on Nocona machines, the 32-bit syscall
2060Sstevel@tonic-gate 	 * variant isn't available to 32-bit applications, but sysenter is.
2070Sstevel@tonic-gate 	 */
20812826Skuriakose.kuruvilla@oracle.com 	if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
20912826Skuriakose.kuruvilla@oracle.com 	    is_x86_feature(x86_featureset, X86FSET_SEP)) {
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate #if !defined(__lint)
2120Sstevel@tonic-gate 		/*
2130Sstevel@tonic-gate 		 * The sysenter instruction imposes a certain ordering on
2140Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
2150Sstevel@tonic-gate 		 * here. See "sysenter" in Intel document 245471-012, "IA-32
2160Sstevel@tonic-gate 		 * Intel Architecture Software Developer's Manual Volume 2:
2170Sstevel@tonic-gate 		 * Instruction Set Reference"
2180Sstevel@tonic-gate 		 */
2190Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 		ASSERT32(UCS_SEL == ((KCS_SEL + 16) | 3));
2220Sstevel@tonic-gate 		ASSERT32(UDS_SEL == UCS_SEL + 8);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 		ASSERT64(U32CS_SEL == ((KCS_SEL + 16) | 3));
2250Sstevel@tonic-gate 		ASSERT64(UDS_SEL == U32CS_SEL + 8);
2260Sstevel@tonic-gate #endif
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 		cpu_sep_enable();
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 		/*
2310Sstevel@tonic-gate 		 * resume() sets this value to the base of the threads stack
2320Sstevel@tonic-gate 		 * via a context handler.
2330Sstevel@tonic-gate 		 */
2343446Smrj 		wrmsr(MSR_INTC_SEP_ESP, 0);
235770Skucharsk 		wrmsr(MSR_INTC_SEP_EIP, (uint64_t)(uintptr_t)sys_sysenter);
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	kpreempt_enable();
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate /*
2420Sstevel@tonic-gate  * Multiprocessor initialization.
2430Sstevel@tonic-gate  *
2440Sstevel@tonic-gate  * Allocate and initialize the cpu structure, TRAPTRACE buffer, and the
2450Sstevel@tonic-gate  * startup and idle threads for the specified CPU.
24612004Sjiang.liu@intel.com  * Parameter boot is true for boot time operations and is false for CPU
24712004Sjiang.liu@intel.com  * DR operations.
2480Sstevel@tonic-gate  */
24912004Sjiang.liu@intel.com static struct cpu *
mp_cpu_configure_common(int cpun,boolean_t boot)25012004Sjiang.liu@intel.com mp_cpu_configure_common(int cpun, boolean_t boot)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate 	struct cpu *cp;
2530Sstevel@tonic-gate 	kthread_id_t tp;
2540Sstevel@tonic-gate 	caddr_t	sp;
2550Sstevel@tonic-gate 	proc_t *procp;
2565084Sjohnlev #if !defined(__xpv)
2575045Sbholler 	extern int idle_cpu_prefer_mwait;
2588906SEric.Saxe@Sun.COM 	extern void cpu_idle_mwait();
2595084Sjohnlev #endif
2600Sstevel@tonic-gate 	extern void idle();
2618906SEric.Saxe@Sun.COM 	extern void cpu_idle();
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate #ifdef TRAPTRACE
2640Sstevel@tonic-gate 	trap_trace_ctl_t *ttc = &trap_trace_ctl[cpun];
2650Sstevel@tonic-gate #endif
2660Sstevel@tonic-gate 
26712004Sjiang.liu@intel.com 	ASSERT(MUTEX_HELD(&cpu_lock));
2680Sstevel@tonic-gate 	ASSERT(cpun < NCPU && cpu[cpun] == NULL);
2690Sstevel@tonic-gate 
27012004Sjiang.liu@intel.com 	if (cpu_free_list == NULL) {
27112004Sjiang.liu@intel.com 		cp = kmem_zalloc(sizeof (*cp), KM_SLEEP);
27212004Sjiang.liu@intel.com 	} else {
27312004Sjiang.liu@intel.com 		cp = cpu_free_list;
27412004Sjiang.liu@intel.com 		cpu_free_list = cp->cpu_next_free;
27512004Sjiang.liu@intel.com 	}
2764481Sbholler 
27711330SFrank.Vanderlinden@Sun.COM 	cp->cpu_m.mcpu_istamp = cpun << 16;
27811330SFrank.Vanderlinden@Sun.COM 
27912004Sjiang.liu@intel.com 	/* Create per CPU specific threads in the process p0. */
28012004Sjiang.liu@intel.com 	procp = &p0;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	/*
2830Sstevel@tonic-gate 	 * Initialize the dispatcher first.
2840Sstevel@tonic-gate 	 */
2850Sstevel@tonic-gate 	disp_cpu_init(cp);
2860Sstevel@tonic-gate 
287414Skchow 	cpu_vm_data_init(cp);
288414Skchow 
2890Sstevel@tonic-gate 	/*
2900Sstevel@tonic-gate 	 * Allocate and initialize the startup thread for this CPU.
2910Sstevel@tonic-gate 	 * Interrupt and process switch stacks get allocated later
2920Sstevel@tonic-gate 	 * when the CPU starts running.
2930Sstevel@tonic-gate 	 */
2940Sstevel@tonic-gate 	tp = thread_create(NULL, 0, NULL, NULL, 0, procp,
2950Sstevel@tonic-gate 	    TS_STOPPED, maxclsyspri);
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	/*
2980Sstevel@tonic-gate 	 * Set state to TS_ONPROC since this thread will start running
2990Sstevel@tonic-gate 	 * as soon as the CPU comes online.
3000Sstevel@tonic-gate 	 *
3010Sstevel@tonic-gate 	 * All the other fields of the thread structure are setup by
3020Sstevel@tonic-gate 	 * thread_create().
3030Sstevel@tonic-gate 	 */
3040Sstevel@tonic-gate 	THREAD_ONPROC(tp, cp);
3050Sstevel@tonic-gate 	tp->t_preempt = 1;
3060Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
3070Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
3080Sstevel@tonic-gate 	tp->t_cpu = cp;
3090Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	/*
31212004Sjiang.liu@intel.com 	 * Setup thread to start in mp_startup_common.
3130Sstevel@tonic-gate 	 */
3140Sstevel@tonic-gate 	sp = tp->t_stk;
3150Sstevel@tonic-gate 	tp->t_sp = (uintptr_t)(sp - MINFRAME);
3163446Smrj #if defined(__amd64)
3173446Smrj 	tp->t_sp -= STACK_ENTRY_ALIGN;		/* fake a call */
3183446Smrj #endif
31912004Sjiang.liu@intel.com 	/*
32012004Sjiang.liu@intel.com 	 * Setup thread start entry point for boot or hotplug.
32112004Sjiang.liu@intel.com 	 */
32212004Sjiang.liu@intel.com 	if (boot) {
32312004Sjiang.liu@intel.com 		tp->t_pc = (uintptr_t)mp_startup_boot;
32412004Sjiang.liu@intel.com 	} else {
32512004Sjiang.liu@intel.com 		tp->t_pc = (uintptr_t)mp_startup_hotplug;
32612004Sjiang.liu@intel.com 	}
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	cp->cpu_id = cpun;
3290Sstevel@tonic-gate 	cp->cpu_self = cp;
3300Sstevel@tonic-gate 	cp->cpu_thread = tp;
3310Sstevel@tonic-gate 	cp->cpu_lwp = NULL;
3320Sstevel@tonic-gate 	cp->cpu_dispthread = tp;
3330Sstevel@tonic-gate 	cp->cpu_dispatch_pri = DISP_PRIO(tp);
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	/*
3361482Ssethg 	 * cpu_base_spl must be set explicitly here to prevent any blocking
33712004Sjiang.liu@intel.com 	 * operations in mp_startup_common from causing the spl of the cpu
33812004Sjiang.liu@intel.com 	 * to drop to 0 (allowing device interrupts before we're ready) in
33912004Sjiang.liu@intel.com 	 * resume().
3401482Ssethg 	 * cpu_base_spl MUST remain at LOCK_LEVEL until the cpu is CPU_READY.
3411482Ssethg 	 * As an extra bit of security on DEBUG kernels, this is enforced with
34212004Sjiang.liu@intel.com 	 * an assertion in mp_startup_common() -- before cpu_base_spl is set
34312004Sjiang.liu@intel.com 	 * to its proper value.
3441482Ssethg 	 */
3451482Ssethg 	cp->cpu_base_spl = ipltospl(LOCK_LEVEL);
3461482Ssethg 
3471482Ssethg 	/*
3480Sstevel@tonic-gate 	 * Now, initialize per-CPU idle thread for this CPU.
3490Sstevel@tonic-gate 	 */
3500Sstevel@tonic-gate 	tp = thread_create(NULL, PAGESIZE, idle, NULL, 0, procp, TS_ONPROC, -1);
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	cp->cpu_idle_thread = tp;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	tp->t_preempt = 1;
3550Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
3560Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
3570Sstevel@tonic-gate 	tp->t_cpu = cp;
3580Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	/*
3613434Sesaxe 	 * Bootstrap the CPU's PG data
36260Sesaxe 	 */
3633434Sesaxe 	pg_cpu_bootstrap(cp);
36460Sesaxe 
36560Sesaxe 	/*
3663446Smrj 	 * Perform CPC initialization on the new CPU.
3670Sstevel@tonic-gate 	 */
3680Sstevel@tonic-gate 	kcpc_hw_init(cp);
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	/*
3710Sstevel@tonic-gate 	 * Allocate virtual addresses for cpu_caddr1 and cpu_caddr2
3720Sstevel@tonic-gate 	 * for each CPU.
3730Sstevel@tonic-gate 	 */
3740Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(cp);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	/*
3773446Smrj 	 * Allocate page for new GDT and initialize from current GDT.
3780Sstevel@tonic-gate 	 */
3793446Smrj #if !defined(__lint)
3803446Smrj 	ASSERT((sizeof (*cp->cpu_gdt) * NGDT) <= PAGESIZE);
3813446Smrj #endif
3825460Sjosephb 	cp->cpu_gdt = kmem_zalloc(PAGESIZE, KM_SLEEP);
3835460Sjosephb 	bcopy(CPU->cpu_gdt, cp->cpu_gdt, (sizeof (*cp->cpu_gdt) * NGDT));
3841626Srab 
3853446Smrj #if defined(__i386)
3860Sstevel@tonic-gate 	/*
3870Sstevel@tonic-gate 	 * setup kernel %gs.
3880Sstevel@tonic-gate 	 */
3890Sstevel@tonic-gate 	set_usegd(&cp->cpu_gdt[GDT_GS], cp, sizeof (struct cpu) -1, SDT_MEMRWA,
3900Sstevel@tonic-gate 	    SEL_KPL, 0, 1);
3913446Smrj #endif
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	/*
3940Sstevel@tonic-gate 	 * If we have more than one node, each cpu gets a copy of IDT
3950Sstevel@tonic-gate 	 * local to its node. If this is a Pentium box, we use cpu 0's
3960Sstevel@tonic-gate 	 * IDT. cpu 0's IDT has been made read-only to workaround the
3970Sstevel@tonic-gate 	 * cmpxchgl register bug
3980Sstevel@tonic-gate 	 */
3990Sstevel@tonic-gate 	if (system_hardware.hd_nodes && x86_type != X86_TYPE_P5) {
4005460Sjosephb #if !defined(__lint)
4015460Sjosephb 		ASSERT((sizeof (*CPU->cpu_idt) * NIDT) <= PAGESIZE);
4025460Sjosephb #endif
4035460Sjosephb 		cp->cpu_idt = kmem_zalloc(PAGESIZE, KM_SLEEP);
4045460Sjosephb 		bcopy(CPU->cpu_idt, cp->cpu_idt, PAGESIZE);
4053446Smrj 	} else {
4065460Sjosephb 		cp->cpu_idt = CPU->cpu_idt;
4070Sstevel@tonic-gate 	}
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	/*
4103446Smrj 	 * Get interrupt priority data from cpu 0.
4110Sstevel@tonic-gate 	 */
4120Sstevel@tonic-gate 	cp->cpu_pri_data = CPU->cpu_pri_data;
4130Sstevel@tonic-gate 
4143446Smrj 	/*
4153446Smrj 	 * alloc space for cpuid info
4163446Smrj 	 */
4173446Smrj 	cpuid_alloc_space(cp);
41812004Sjiang.liu@intel.com #if !defined(__xpv)
41912826Skuriakose.kuruvilla@oracle.com 	if (is_x86_feature(x86_featureset, X86FSET_MWAIT) &&
42012826Skuriakose.kuruvilla@oracle.com 	    idle_cpu_prefer_mwait) {
42112004Sjiang.liu@intel.com 		cp->cpu_m.mcpu_mwait = cpuid_mwait_alloc(cp);
42212004Sjiang.liu@intel.com 		cp->cpu_m.mcpu_idle_cpu = cpu_idle_mwait;
42312004Sjiang.liu@intel.com 	} else
42412004Sjiang.liu@intel.com #endif
42512004Sjiang.liu@intel.com 		cp->cpu_m.mcpu_idle_cpu = cpu_idle;
42612004Sjiang.liu@intel.com 
42712004Sjiang.liu@intel.com 	init_cpu_info(cp);
4283446Smrj 
4294581Ssherrym 	/*
4304581Ssherrym 	 * alloc space for ucode_info
4314581Ssherrym 	 */
4324581Ssherrym 	ucode_alloc_space(cp);
4339489SJoe.Bonasera@sun.com 	xc_init_cpu(cp);
4340Sstevel@tonic-gate 	hat_cpu_online(cp);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate #ifdef TRAPTRACE
4370Sstevel@tonic-gate 	/*
4383446Smrj 	 * If this is a TRAPTRACE kernel, allocate TRAPTRACE buffers
4390Sstevel@tonic-gate 	 */
4400Sstevel@tonic-gate 	ttc->ttc_first = (uintptr_t)kmem_zalloc(trap_trace_bufsize, KM_SLEEP);
4410Sstevel@tonic-gate 	ttc->ttc_next = ttc->ttc_first;
4420Sstevel@tonic-gate 	ttc->ttc_limit = ttc->ttc_first + trap_trace_bufsize;
4430Sstevel@tonic-gate #endif
44412004Sjiang.liu@intel.com 
4450Sstevel@tonic-gate 	/*
4460Sstevel@tonic-gate 	 * Record that we have another CPU.
4470Sstevel@tonic-gate 	 */
4480Sstevel@tonic-gate 	/*
4490Sstevel@tonic-gate 	 * Initialize the interrupt threads for this CPU
4500Sstevel@tonic-gate 	 */
4511455Sandrei 	cpu_intr_alloc(cp, NINTR_THREADS);
45212004Sjiang.liu@intel.com 
45312004Sjiang.liu@intel.com 	cp->cpu_flags = CPU_OFFLINE | CPU_QUIESCED | CPU_POWEROFF;
45412004Sjiang.liu@intel.com 	cpu_set_state(cp);
45512004Sjiang.liu@intel.com 
4560Sstevel@tonic-gate 	/*
4570Sstevel@tonic-gate 	 * Add CPU to list of available CPUs.  It'll be on the active list
45812004Sjiang.liu@intel.com 	 * after mp_startup_common().
4590Sstevel@tonic-gate 	 */
4600Sstevel@tonic-gate 	cpu_add_unit(cp);
4613446Smrj 
4623446Smrj 	return (cp);
4633446Smrj }
4643446Smrj 
4653446Smrj /*
46612004Sjiang.liu@intel.com  * Undo what was done in mp_cpu_configure_common
4673446Smrj  */
4683446Smrj static void
mp_cpu_unconfigure_common(struct cpu * cp,int error)46912004Sjiang.liu@intel.com mp_cpu_unconfigure_common(struct cpu *cp, int error)
4703446Smrj {
47112004Sjiang.liu@intel.com 	ASSERT(MUTEX_HELD(&cpu_lock));
4723446Smrj 
4733446Smrj 	/*
4743446Smrj 	 * Remove the CPU from the list of available CPUs.
4753446Smrj 	 */
4763446Smrj 	cpu_del_unit(cp->cpu_id);
4773446Smrj 
4783446Smrj 	if (error == ETIMEDOUT) {
4793446Smrj 		/*
4803446Smrj 		 * The cpu was started, but never *seemed* to run any
4813446Smrj 		 * code in the kernel; it's probably off spinning in its
4823446Smrj 		 * own private world, though with potential references to
4833446Smrj 		 * our kmem-allocated IDTs and GDTs (for example).
4843446Smrj 		 *
4853446Smrj 		 * Worse still, it may actually wake up some time later,
4863446Smrj 		 * so rather than guess what it might or might not do, we
4873446Smrj 		 * leave the fundamental data structures intact.
4883446Smrj 		 */
4893446Smrj 		cp->cpu_flags = 0;
4903446Smrj 		return;
4913446Smrj 	}
4923446Smrj 
4933446Smrj 	/*
4943446Smrj 	 * At this point, the only threads bound to this CPU should
4953446Smrj 	 * special per-cpu threads: it's idle thread, it's pause threads,
4963446Smrj 	 * and it's interrupt threads.  Clean these up.
4973446Smrj 	 */
4983446Smrj 	cpu_destroy_bound_threads(cp);
4993446Smrj 	cp->cpu_idle_thread = NULL;
5003446Smrj 
5013446Smrj 	/*
5023446Smrj 	 * Free the interrupt stack.
5033446Smrj 	 */
5043446Smrj 	segkp_release(segkp,
5053446Smrj 	    cp->cpu_intr_stack - (INTR_STACK_SIZE - SA(MINFRAME)));
50612004Sjiang.liu@intel.com 	cp->cpu_intr_stack = NULL;
5073446Smrj 
5083446Smrj #ifdef TRAPTRACE
5093446Smrj 	/*
5103446Smrj 	 * Discard the trap trace buffer
5113446Smrj 	 */
5123446Smrj 	{
5133446Smrj 		trap_trace_ctl_t *ttc = &trap_trace_ctl[cp->cpu_id];
5143446Smrj 
5153446Smrj 		kmem_free((void *)ttc->ttc_first, trap_trace_bufsize);
5163446Smrj 		ttc->ttc_first = NULL;
5173446Smrj 	}
5183446Smrj #endif
5193446Smrj 
5203446Smrj 	hat_cpu_offline(cp);
5213446Smrj 
52212004Sjiang.liu@intel.com 	ucode_free_space(cp);
5233446Smrj 
52412004Sjiang.liu@intel.com 	/* Free CPU ID string and brand string. */
52512004Sjiang.liu@intel.com 	if (cp->cpu_idstr) {
52612004Sjiang.liu@intel.com 		kmem_free(cp->cpu_idstr, CPU_IDSTRLEN);
52712004Sjiang.liu@intel.com 		cp->cpu_idstr = NULL;
52812004Sjiang.liu@intel.com 	}
52912004Sjiang.liu@intel.com 	if (cp->cpu_brandstr) {
53012004Sjiang.liu@intel.com 		kmem_free(cp->cpu_brandstr, CPU_IDSTRLEN);
53112004Sjiang.liu@intel.com 		cp->cpu_brandstr = NULL;
53212004Sjiang.liu@intel.com 	}
53312004Sjiang.liu@intel.com 
53412004Sjiang.liu@intel.com #if !defined(__xpv)
53512004Sjiang.liu@intel.com 	if (cp->cpu_m.mcpu_mwait != NULL) {
53612004Sjiang.liu@intel.com 		cpuid_mwait_free(cp);
53712004Sjiang.liu@intel.com 		cp->cpu_m.mcpu_mwait = NULL;
53812004Sjiang.liu@intel.com 	}
53912004Sjiang.liu@intel.com #endif
54012004Sjiang.liu@intel.com 	cpuid_free_space(cp);
5414581Ssherrym 
5425460Sjosephb 	if (cp->cpu_idt != CPU->cpu_idt)
5435460Sjosephb 		kmem_free(cp->cpu_idt, PAGESIZE);
5445460Sjosephb 	cp->cpu_idt = NULL;
5453446Smrj 
5465460Sjosephb 	kmem_free(cp->cpu_gdt, PAGESIZE);
5475460Sjosephb 	cp->cpu_gdt = NULL;
5483446Smrj 
54912004Sjiang.liu@intel.com 	if (cp->cpu_supp_freqs != NULL) {
55012004Sjiang.liu@intel.com 		size_t len = strlen(cp->cpu_supp_freqs) + 1;
55112004Sjiang.liu@intel.com 		kmem_free(cp->cpu_supp_freqs, len);
55212004Sjiang.liu@intel.com 		cp->cpu_supp_freqs = NULL;
55312004Sjiang.liu@intel.com 	}
55412004Sjiang.liu@intel.com 
5553446Smrj 	teardown_vaddr_for_ppcopy(cp);
5563446Smrj 
5573446Smrj 	kcpc_hw_fini(cp);
5583446Smrj 
5593446Smrj 	cp->cpu_dispthread = NULL;
5603446Smrj 	cp->cpu_thread = NULL;	/* discarded by cpu_destroy_bound_threads() */
5613446Smrj 
5623446Smrj 	cpu_vm_data_destroy(cp);
5633446Smrj 
56412004Sjiang.liu@intel.com 	xc_fini_cpu(cp);
5653446Smrj 	disp_cpu_fini(cp);
5663446Smrj 
56712004Sjiang.liu@intel.com 	ASSERT(cp != CPU0);
56812004Sjiang.liu@intel.com 	bzero(cp, sizeof (*cp));
56912004Sjiang.liu@intel.com 	cp->cpu_next_free = cpu_free_list;
57012004Sjiang.liu@intel.com 	cpu_free_list = cp;
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate /*
5740Sstevel@tonic-gate  * Apply workarounds for known errata, and warn about those that are absent.
5750Sstevel@tonic-gate  *
5760Sstevel@tonic-gate  * System vendors occasionally create configurations which contain different
5770Sstevel@tonic-gate  * revisions of the CPUs that are almost but not exactly the same.  At the
5780Sstevel@tonic-gate  * time of writing, this meant that their clock rates were the same, their
5790Sstevel@tonic-gate  * feature sets were the same, but the required workaround were -not-
5800Sstevel@tonic-gate  * necessarily the same.  So, this routine is invoked on -every- CPU soon
5810Sstevel@tonic-gate  * after starting to make sure that the resulting system contains the most
5820Sstevel@tonic-gate  * pessimal set of workarounds needed to cope with *any* of the CPUs in the
5830Sstevel@tonic-gate  * system.
5840Sstevel@tonic-gate  *
585938Sesaxe  * workaround_errata is invoked early in mlsetup() for CPU 0, and in
58612004Sjiang.liu@intel.com  * mp_startup_common() for all slave CPUs. Slaves process workaround_errata
58712004Sjiang.liu@intel.com  * prior to acknowledging their readiness to the master, so this routine will
588938Sesaxe  * never be executed by multiple CPUs in parallel, thus making updates to
589938Sesaxe  * global data safe.
590938Sesaxe  *
591359Skucharsk  * These workarounds are based on Rev 3.57 of the Revision Guide for
592359Skucharsk  * AMD Athlon(tm) 64 and AMD Opteron(tm) Processors, August 2005.
5930Sstevel@tonic-gate  */
5940Sstevel@tonic-gate 
5953446Smrj #if defined(OPTERON_ERRATUM_88)
5963446Smrj int opteron_erratum_88;		/* if non-zero -> at least one cpu has it */
5973446Smrj #endif
5983446Smrj 
5990Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
6000Sstevel@tonic-gate int opteron_erratum_91;		/* if non-zero -> at least one cpu has it */
6010Sstevel@tonic-gate #endif
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
6040Sstevel@tonic-gate int opteron_erratum_93;		/* if non-zero -> at least one cpu has it */
6050Sstevel@tonic-gate #endif
6060Sstevel@tonic-gate 
6073446Smrj #if defined(OPTERON_ERRATUM_95)
6083446Smrj int opteron_erratum_95;		/* if non-zero -> at least one cpu has it */
6093446Smrj #endif
6103446Smrj 
6110Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
6120Sstevel@tonic-gate int opteron_erratum_100;	/* if non-zero -> at least one cpu has it */
6130Sstevel@tonic-gate #endif
6140Sstevel@tonic-gate 
6153446Smrj #if defined(OPTERON_ERRATUM_108)
6163446Smrj int opteron_erratum_108;	/* if non-zero -> at least one cpu has it */
6173446Smrj #endif
6183446Smrj 
6190Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
6200Sstevel@tonic-gate int opteron_erratum_109;	/* if non-zero -> at least one cpu has it */
6210Sstevel@tonic-gate #endif
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
6240Sstevel@tonic-gate int opteron_erratum_121;	/* if non-zero -> at least one cpu has it */
6250Sstevel@tonic-gate #endif
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
6280Sstevel@tonic-gate int opteron_erratum_122;	/* if non-zero -> at least one cpu has it */
6290Sstevel@tonic-gate #endif
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
6320Sstevel@tonic-gate int opteron_erratum_123;	/* if non-zero -> at least one cpu has it */
6330Sstevel@tonic-gate #endif
6340Sstevel@tonic-gate 
635359Skucharsk #if defined(OPTERON_ERRATUM_131)
636359Skucharsk int opteron_erratum_131;	/* if non-zero -> at least one cpu has it */
637359Skucharsk #endif
6380Sstevel@tonic-gate 
639938Sesaxe #if defined(OPTERON_WORKAROUND_6336786)
640938Sesaxe int opteron_workaround_6336786;	/* non-zero -> WA relevant and applied */
641938Sesaxe int opteron_workaround_6336786_UP = 0;	/* Not needed for UP */
642938Sesaxe #endif
643938Sesaxe 
6441582Skchow #if defined(OPTERON_WORKAROUND_6323525)
6451582Skchow int opteron_workaround_6323525;	/* if non-zero -> at least one cpu has it */
6461582Skchow #endif
6471582Skchow 
6486691Skchow #if defined(OPTERON_ERRATUM_298)
6496691Skchow int opteron_erratum_298;
6506691Skchow #endif
6516691Skchow 
6523446Smrj static void
workaround_warning(cpu_t * cp,uint_t erratum)6533446Smrj workaround_warning(cpu_t *cp, uint_t erratum)
6543446Smrj {
6553446Smrj 	cmn_err(CE_WARN, "cpu%d: no workaround for erratum %u",
6563446Smrj 	    cp->cpu_id, erratum);
6573446Smrj }
6583446Smrj 
6593446Smrj static void
workaround_applied(uint_t erratum)6603446Smrj workaround_applied(uint_t erratum)
6613446Smrj {
6623446Smrj 	if (erratum > 1000000)
6633446Smrj 		cmn_err(CE_CONT, "?workaround applied for cpu issue #%d\n",
6643446Smrj 		    erratum);
6653446Smrj 	else
6663446Smrj 		cmn_err(CE_CONT, "?workaround applied for cpu erratum #%d\n",
6673446Smrj 		    erratum);
6683446Smrj }
6693446Smrj 
6703446Smrj static void
msr_warning(cpu_t * cp,const char * rw,uint_t msr,int error)6713446Smrj msr_warning(cpu_t *cp, const char *rw, uint_t msr, int error)
6723446Smrj {
6733446Smrj 	cmn_err(CE_WARN, "cpu%d: couldn't %smsr 0x%x, error %d",
6743446Smrj 	    cp->cpu_id, rw, msr, error);
6753446Smrj }
6760Sstevel@tonic-gate 
6775893Sesaxe /*
6788232SEric.Saxe@Sun.COM  * Determine the number of nodes in a Hammer / Greyhound / Griffin family
6798232SEric.Saxe@Sun.COM  * system.
6805893Sesaxe  */
6815893Sesaxe static uint_t
opteron_get_nnodes(void)6825893Sesaxe opteron_get_nnodes(void)
6835893Sesaxe {
6845893Sesaxe 	static uint_t nnodes = 0;
6855893Sesaxe 
6868232SEric.Saxe@Sun.COM 	if (nnodes == 0) {
6875893Sesaxe #ifdef	DEBUG
6888232SEric.Saxe@Sun.COM 		uint_t family;
6895893Sesaxe 
6908232SEric.Saxe@Sun.COM 		/*
6918232SEric.Saxe@Sun.COM 		 * This routine uses a PCI config space based mechanism
6928232SEric.Saxe@Sun.COM 		 * for retrieving the number of nodes in the system.
6938232SEric.Saxe@Sun.COM 		 * Device 24, function 0, offset 0x60 as used here is not
6948232SEric.Saxe@Sun.COM 		 * AMD processor architectural, and may not work on processor
6958232SEric.Saxe@Sun.COM 		 * families other than those listed below.
6968232SEric.Saxe@Sun.COM 		 *
6978232SEric.Saxe@Sun.COM 		 * Callers of this routine must ensure that we're running on
6988232SEric.Saxe@Sun.COM 		 * a processor which supports this mechanism.
6998232SEric.Saxe@Sun.COM 		 * The assertion below is meant to catch calls on unsupported
7008232SEric.Saxe@Sun.COM 		 * processors.
7018232SEric.Saxe@Sun.COM 		 */
7028232SEric.Saxe@Sun.COM 		family = cpuid_getfamily(CPU);
7038232SEric.Saxe@Sun.COM 		ASSERT(family == 0xf || family == 0x10 || family == 0x11);
7045893Sesaxe #endif	/* DEBUG */
7055893Sesaxe 
7065893Sesaxe 		/*
7075893Sesaxe 		 * Obtain the number of nodes in the system from
7085893Sesaxe 		 * bits [6:4] of the Node ID register on node 0.
7095893Sesaxe 		 *
7105893Sesaxe 		 * The actual node count is NodeID[6:4] + 1
7115893Sesaxe 		 *
7125893Sesaxe 		 * The Node ID register is accessed via function 0,
7135893Sesaxe 		 * offset 0x60. Node 0 is device 24.
7145893Sesaxe 		 */
7155893Sesaxe 		nnodes = ((pci_getl_func(0, 24, 0, 0x60) & 0x70) >> 4) + 1;
7165893Sesaxe 	}
7175893Sesaxe 	return (nnodes);
7185893Sesaxe }
7195893Sesaxe 
7200Sstevel@tonic-gate uint_t
do_erratum_298(struct cpu * cpu)7216691Skchow do_erratum_298(struct cpu *cpu)
7226691Skchow {
7236691Skchow 	static int	osvwrc = -3;
7246691Skchow 	extern int	osvw_opteron_erratum(cpu_t *, uint_t);
7256691Skchow 
7266691Skchow 	/*
7276691Skchow 	 * L2 Eviction May Occur During Processor Operation To Set
7286691Skchow 	 * Accessed or Dirty Bit.
7296691Skchow 	 */
7306691Skchow 	if (osvwrc == -3) {
7316691Skchow 		osvwrc = osvw_opteron_erratum(cpu, 298);
7326691Skchow 	} else {
7336691Skchow 		/* osvw return codes should be consistent for all cpus */
7346691Skchow 		ASSERT(osvwrc == osvw_opteron_erratum(cpu, 298));
7356691Skchow 	}
7366691Skchow 
7376691Skchow 	switch (osvwrc) {
7386691Skchow 	case 0:		/* erratum is not present: do nothing */
7396691Skchow 		break;
7406691Skchow 	case 1:		/* erratum is present: BIOS workaround applied */
7416691Skchow 		/*
7426691Skchow 		 * check if workaround is actually in place and issue warning
7436691Skchow 		 * if not.
7446691Skchow 		 */
7456691Skchow 		if (((rdmsr(MSR_AMD_HWCR) & AMD_HWCR_TLBCACHEDIS) == 0) ||
7466691Skchow 		    ((rdmsr(MSR_AMD_BU_CFG) & AMD_BU_CFG_E298) == 0)) {
7476691Skchow #if defined(OPTERON_ERRATUM_298)
7486691Skchow 			opteron_erratum_298++;
7496691Skchow #else
7506691Skchow 			workaround_warning(cpu, 298);
7516691Skchow 			return (1);
7526691Skchow #endif
7536691Skchow 		}
7546691Skchow 		break;
7556691Skchow 	case -1:	/* cannot determine via osvw: check cpuid */
7566691Skchow 		if ((cpuid_opteron_erratum(cpu, 298) > 0) &&
7576691Skchow 		    (((rdmsr(MSR_AMD_HWCR) & AMD_HWCR_TLBCACHEDIS) == 0) ||
7586691Skchow 		    ((rdmsr(MSR_AMD_BU_CFG) & AMD_BU_CFG_E298) == 0))) {
7596691Skchow #if defined(OPTERON_ERRATUM_298)
7606691Skchow 			opteron_erratum_298++;
7616691Skchow #else
7626691Skchow 			workaround_warning(cpu, 298);
7636691Skchow 			return (1);
7646691Skchow #endif
7656691Skchow 		}
7666691Skchow 		break;
7676691Skchow 	}
7686691Skchow 	return (0);
7696691Skchow }
7706691Skchow 
7716691Skchow uint_t
workaround_errata(struct cpu * cpu)7720Sstevel@tonic-gate workaround_errata(struct cpu *cpu)
7730Sstevel@tonic-gate {
7740Sstevel@tonic-gate 	uint_t missing = 0;
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	ASSERT(cpu == CPU);
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	/*LINTED*/
7790Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 88) > 0) {
7800Sstevel@tonic-gate 		/*
7810Sstevel@tonic-gate 		 * SWAPGS May Fail To Read Correct GS Base
7820Sstevel@tonic-gate 		 */
7830Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_88)
7840Sstevel@tonic-gate 		/*
7850Sstevel@tonic-gate 		 * The workaround is an mfence in the relevant assembler code
7860Sstevel@tonic-gate 		 */
7873446Smrj 		opteron_erratum_88++;
7880Sstevel@tonic-gate #else
7893446Smrj 		workaround_warning(cpu, 88);
7900Sstevel@tonic-gate 		missing++;
7910Sstevel@tonic-gate #endif
7920Sstevel@tonic-gate 	}
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 91) > 0) {
7950Sstevel@tonic-gate 		/*
7960Sstevel@tonic-gate 		 * Software Prefetches May Report A Page Fault
7970Sstevel@tonic-gate 		 */
7980Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
7990Sstevel@tonic-gate 		/*
8000Sstevel@tonic-gate 		 * fix is in trap.c
8010Sstevel@tonic-gate 		 */
8020Sstevel@tonic-gate 		opteron_erratum_91++;
8030Sstevel@tonic-gate #else
8043446Smrj 		workaround_warning(cpu, 91);
8050Sstevel@tonic-gate 		missing++;
8060Sstevel@tonic-gate #endif
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 93) > 0) {
8100Sstevel@tonic-gate 		/*
8110Sstevel@tonic-gate 		 * RSM Auto-Halt Restart Returns to Incorrect RIP
8120Sstevel@tonic-gate 		 */
8130Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
8140Sstevel@tonic-gate 		/*
8150Sstevel@tonic-gate 		 * fix is in trap.c
8160Sstevel@tonic-gate 		 */
8170Sstevel@tonic-gate 		opteron_erratum_93++;
8180Sstevel@tonic-gate #else
8193446Smrj 		workaround_warning(cpu, 93);
8200Sstevel@tonic-gate 		missing++;
8210Sstevel@tonic-gate #endif
8220Sstevel@tonic-gate 	}
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	/*LINTED*/
8250Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 95) > 0) {
8260Sstevel@tonic-gate 		/*
8270Sstevel@tonic-gate 		 * RET Instruction May Return to Incorrect EIP
8280Sstevel@tonic-gate 		 */
8290Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_95)
8300Sstevel@tonic-gate #if defined(_LP64)
8310Sstevel@tonic-gate 		/*
8320Sstevel@tonic-gate 		 * Workaround this by ensuring that 32-bit user code and
8330Sstevel@tonic-gate 		 * 64-bit kernel code never occupy the same address
8340Sstevel@tonic-gate 		 * range mod 4G.
8350Sstevel@tonic-gate 		 */
8360Sstevel@tonic-gate 		if (_userlimit32 > 0xc0000000ul)
8370Sstevel@tonic-gate 			*(uintptr_t *)&_userlimit32 = 0xc0000000ul;
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 		/*LINTED*/
8400Sstevel@tonic-gate 		ASSERT((uint32_t)COREHEAP_BASE == 0xc0000000u);
8413446Smrj 		opteron_erratum_95++;
8420Sstevel@tonic-gate #endif	/* _LP64 */
8430Sstevel@tonic-gate #else
8443446Smrj 		workaround_warning(cpu, 95);
8450Sstevel@tonic-gate 		missing++;
8463446Smrj #endif
8470Sstevel@tonic-gate 	}
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 100) > 0) {
8500Sstevel@tonic-gate 		/*
8510Sstevel@tonic-gate 		 * Compatibility Mode Branches Transfer to Illegal Address
8520Sstevel@tonic-gate 		 */
8530Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
8540Sstevel@tonic-gate 		/*
8550Sstevel@tonic-gate 		 * fix is in trap.c
8560Sstevel@tonic-gate 		 */
8570Sstevel@tonic-gate 		opteron_erratum_100++;
8580Sstevel@tonic-gate #else
8593446Smrj 		workaround_warning(cpu, 100);
8600Sstevel@tonic-gate 		missing++;
8610Sstevel@tonic-gate #endif
8620Sstevel@tonic-gate 	}
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	/*LINTED*/
8650Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 108) > 0) {
8660Sstevel@tonic-gate 		/*
8670Sstevel@tonic-gate 		 * CPUID Instruction May Return Incorrect Model Number In
8680Sstevel@tonic-gate 		 * Some Processors
8690Sstevel@tonic-gate 		 */
8700Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_108)
8710Sstevel@tonic-gate 		/*
8720Sstevel@tonic-gate 		 * (Our cpuid-handling code corrects the model number on
8730Sstevel@tonic-gate 		 * those processors)
8740Sstevel@tonic-gate 		 */
8750Sstevel@tonic-gate #else
8763446Smrj 		workaround_warning(cpu, 108);
8770Sstevel@tonic-gate 		missing++;
8780Sstevel@tonic-gate #endif
8790Sstevel@tonic-gate 	}
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	/*LINTED*/
8823446Smrj 	if (cpuid_opteron_erratum(cpu, 109) > 0) do {
8830Sstevel@tonic-gate 		/*
8849637SRandy.Fishel@Sun.COM 		 * Certain Reverse REP MOVS May Produce Unpredictable Behavior
8850Sstevel@tonic-gate 		 */
8860Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
8873446Smrj 		/*
8883446Smrj 		 * The "workaround" is to print a warning to upgrade the BIOS
8893446Smrj 		 */
8903446Smrj 		uint64_t value;
8913446Smrj 		const uint_t msr = MSR_AMD_PATCHLEVEL;
8923446Smrj 		int err;
8930Sstevel@tonic-gate 
8943446Smrj 		if ((err = checked_rdmsr(msr, &value)) != 0) {
8953446Smrj 			msr_warning(cpu, "rd", msr, err);
8963446Smrj 			workaround_warning(cpu, 109);
8973446Smrj 			missing++;
8983446Smrj 		}
8993446Smrj 		if (value == 0)
9000Sstevel@tonic-gate 			opteron_erratum_109++;
9010Sstevel@tonic-gate #else
9023446Smrj 		workaround_warning(cpu, 109);
9030Sstevel@tonic-gate 		missing++;
9040Sstevel@tonic-gate #endif
9053446Smrj 	/*CONSTANTCONDITION*/
9063446Smrj 	} while (0);
9073446Smrj 
9080Sstevel@tonic-gate 	/*LINTED*/
9090Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 121) > 0) {
9100Sstevel@tonic-gate 		/*
9110Sstevel@tonic-gate 		 * Sequential Execution Across Non_Canonical Boundary Caused
9120Sstevel@tonic-gate 		 * Processor Hang
9130Sstevel@tonic-gate 		 */
9140Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
9153446Smrj #if defined(_LP64)
9160Sstevel@tonic-gate 		/*
9170Sstevel@tonic-gate 		 * Erratum 121 is only present in long (64 bit) mode.
9180Sstevel@tonic-gate 		 * Workaround is to include the page immediately before the
9190Sstevel@tonic-gate 		 * va hole to eliminate the possibility of system hangs due to
9200Sstevel@tonic-gate 		 * sequential execution across the va hole boundary.
9210Sstevel@tonic-gate 		 */
9223446Smrj 		if (opteron_erratum_121)
9233446Smrj 			opteron_erratum_121++;
9243446Smrj 		else {
9253446Smrj 			if (hole_start) {
9263446Smrj 				hole_start -= PAGESIZE;
9273446Smrj 			} else {
9283446Smrj 				/*
9293446Smrj 				 * hole_start not yet initialized by
9303446Smrj 				 * mmu_init. Initialize hole_start
9313446Smrj 				 * with value to be subtracted.
9323446Smrj 				 */
9333446Smrj 				hole_start = PAGESIZE;
9340Sstevel@tonic-gate 			}
9353446Smrj 			opteron_erratum_121++;
9360Sstevel@tonic-gate 		}
9373446Smrj #endif	/* _LP64 */
9380Sstevel@tonic-gate #else
9393446Smrj 		workaround_warning(cpu, 121);
9400Sstevel@tonic-gate 		missing++;
9410Sstevel@tonic-gate #endif
9420Sstevel@tonic-gate 	}
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	/*LINTED*/
9453446Smrj 	if (cpuid_opteron_erratum(cpu, 122) > 0) do {
9460Sstevel@tonic-gate 		/*
9473446Smrj 		 * TLB Flush Filter May Cause Coherency Problem in
9480Sstevel@tonic-gate 		 * Multiprocessor Systems
9490Sstevel@tonic-gate 		 */
9500Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
9513446Smrj 		uint64_t value;
9523446Smrj 		const uint_t msr = MSR_AMD_HWCR;
9533446Smrj 		int error;
9543446Smrj 
9550Sstevel@tonic-gate 		/*
9560Sstevel@tonic-gate 		 * Erratum 122 is only present in MP configurations (multi-core
9570Sstevel@tonic-gate 		 * or multi-processor).
9580Sstevel@tonic-gate 		 */
9595084Sjohnlev #if defined(__xpv)
9605084Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
9615084Sjohnlev 			break;
96210175SStuart.Maybee@Sun.COM 		if (!opteron_erratum_122 && xpv_nr_phys_cpus() == 1)
9635084Sjohnlev 			break;
9645084Sjohnlev #else
9655893Sesaxe 		if (!opteron_erratum_122 && opteron_get_nnodes() == 1 &&
9663446Smrj 		    cpuid_get_ncpu_per_chip(cpu) == 1)
9673446Smrj 			break;
9685084Sjohnlev #endif
9693446Smrj 		/* disable TLB Flush Filter */
9703446Smrj 
9713446Smrj 		if ((error = checked_rdmsr(msr, &value)) != 0) {
9723446Smrj 			msr_warning(cpu, "rd", msr, error);
9733446Smrj 			workaround_warning(cpu, 122);
9743446Smrj 			missing++;
9753446Smrj 		} else {
9763446Smrj 			value |= (uint64_t)AMD_HWCR_FFDIS;
9773446Smrj 			if ((error = checked_wrmsr(msr, value)) != 0) {
9783446Smrj 				msr_warning(cpu, "wr", msr, error);
9793446Smrj 				workaround_warning(cpu, 122);
9803446Smrj 				missing++;
9813446Smrj 			}
9820Sstevel@tonic-gate 		}
9833446Smrj 		opteron_erratum_122++;
9840Sstevel@tonic-gate #else
9853446Smrj 		workaround_warning(cpu, 122);
9860Sstevel@tonic-gate 		missing++;
9870Sstevel@tonic-gate #endif
9883446Smrj 	/*CONSTANTCONDITION*/
9893446Smrj 	} while (0);
990302Skchow 
9910Sstevel@tonic-gate 	/*LINTED*/
9923446Smrj 	if (cpuid_opteron_erratum(cpu, 123) > 0) do {
9930Sstevel@tonic-gate 		/*
9940Sstevel@tonic-gate 		 * Bypassed Reads May Cause Data Corruption of System Hang in
9950Sstevel@tonic-gate 		 * Dual Core Processors
9960Sstevel@tonic-gate 		 */
9973446Smrj #if defined(OPTERON_ERRATUM_123)
9983446Smrj 		uint64_t value;
9993446Smrj 		const uint_t msr = MSR_AMD_PATCHLEVEL;
10003446Smrj 		int err;
10013446Smrj 
10020Sstevel@tonic-gate 		/*
10030Sstevel@tonic-gate 		 * Erratum 123 applies only to multi-core cpus.
10040Sstevel@tonic-gate 		 */
10053446Smrj 		if (cpuid_get_ncpu_per_chip(cpu) < 2)
10063446Smrj 			break;
10075084Sjohnlev #if defined(__xpv)
10085084Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
10095084Sjohnlev 			break;
10105084Sjohnlev #endif
10113446Smrj 		/*
10123446Smrj 		 * The "workaround" is to print a warning to upgrade the BIOS
10133446Smrj 		 */
10143446Smrj 		if ((err = checked_rdmsr(msr, &value)) != 0) {
10153446Smrj 			msr_warning(cpu, "rd", msr, err);
10163446Smrj 			workaround_warning(cpu, 123);
10173446Smrj 			missing++;
10180Sstevel@tonic-gate 		}
10193446Smrj 		if (value == 0)
10203446Smrj 			opteron_erratum_123++;
10213446Smrj #else
10223446Smrj 		workaround_warning(cpu, 123);
10233446Smrj 		missing++;
1024359Skucharsk 
10253446Smrj #endif
10263446Smrj 	/*CONSTANTCONDITION*/
10273446Smrj 	} while (0);
10283446Smrj 
1029359Skucharsk 	/*LINTED*/
10303446Smrj 	if (cpuid_opteron_erratum(cpu, 131) > 0) do {
1031359Skucharsk 		/*
1032359Skucharsk 		 * Multiprocessor Systems with Four or More Cores May Deadlock
1033359Skucharsk 		 * Waiting for a Probe Response
1034359Skucharsk 		 */
10353446Smrj #if defined(OPTERON_ERRATUM_131)
10363446Smrj 		uint64_t nbcfg;
10373446Smrj 		const uint_t msr = MSR_AMD_NB_CFG;
10383446Smrj 		const uint64_t wabits =
10393446Smrj 		    AMD_NB_CFG_SRQ_HEARTBEAT | AMD_NB_CFG_SRQ_SPR;
10403446Smrj 		int error;
10413446Smrj 
1042359Skucharsk 		/*
1043359Skucharsk 		 * Erratum 131 applies to any system with four or more cores.
1044359Skucharsk 		 */
10453446Smrj 		if (opteron_erratum_131)
10463446Smrj 			break;
10475084Sjohnlev #if defined(__xpv)
10485084Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
10495084Sjohnlev 			break;
105010175SStuart.Maybee@Sun.COM 		if (xpv_nr_phys_cpus() < 4)
10515084Sjohnlev 			break;
10525084Sjohnlev #else
10535893Sesaxe 		if (opteron_get_nnodes() * cpuid_get_ncpu_per_chip(cpu) < 4)
10543446Smrj 			break;
10555084Sjohnlev #endif
10563446Smrj 		/*
10573446Smrj 		 * Print a warning if neither of the workarounds for
10583446Smrj 		 * erratum 131 is present.
10593446Smrj 		 */
10603446Smrj 		if ((error = checked_rdmsr(msr, &nbcfg)) != 0) {
10613446Smrj 			msr_warning(cpu, "rd", msr, error);
10623446Smrj 			workaround_warning(cpu, 131);
10633446Smrj 			missing++;
10643446Smrj 		} else if ((nbcfg & wabits) == 0) {
10653446Smrj 			opteron_erratum_131++;
10663446Smrj 		} else {
10673446Smrj 			/* cannot have both workarounds set */
10683446Smrj 			ASSERT((nbcfg & wabits) != wabits);
1069359Skucharsk 		}
10703446Smrj #else
10713446Smrj 		workaround_warning(cpu, 131);
10723446Smrj 		missing++;
1073359Skucharsk #endif
10743446Smrj 	/*CONSTANTCONDITION*/
10753446Smrj 	} while (0);
1076938Sesaxe 
1077938Sesaxe 	/*
10783446Smrj 	 * This isn't really an erratum, but for convenience the
1079938Sesaxe 	 * detection/workaround code lives here and in cpuid_opteron_erratum.
1080938Sesaxe 	 */
1081938Sesaxe 	if (cpuid_opteron_erratum(cpu, 6336786) > 0) {
10823446Smrj #if defined(OPTERON_WORKAROUND_6336786)
1083938Sesaxe 		/*
1084938Sesaxe 		 * Disable C1-Clock ramping on multi-core/multi-processor
1085938Sesaxe 		 * K8 platforms to guard against TSC drift.
1086938Sesaxe 		 */
1087938Sesaxe 		if (opteron_workaround_6336786) {
1088938Sesaxe 			opteron_workaround_6336786++;
10895084Sjohnlev #if defined(__xpv)
10905084Sjohnlev 		} else if ((DOMAIN_IS_INITDOMAIN(xen_info) &&
109110175SStuart.Maybee@Sun.COM 		    xpv_nr_phys_cpus() > 1) ||
10925084Sjohnlev 		    opteron_workaround_6336786_UP) {
10935084Sjohnlev 			/*
10945893Sesaxe 			 * XXPV	Hmm.  We can't walk the Northbridges on
10955084Sjohnlev 			 *	the hypervisor; so just complain and drive
10965084Sjohnlev 			 *	on.  This probably needs to be fixed in
10975084Sjohnlev 			 *	the hypervisor itself.
10985084Sjohnlev 			 */
10995084Sjohnlev 			opteron_workaround_6336786++;
11005084Sjohnlev 			workaround_warning(cpu, 6336786);
11015084Sjohnlev #else	/* __xpv */
11025893Sesaxe 		} else if ((opteron_get_nnodes() *
11035894Sesaxe 		    cpuid_get_ncpu_per_chip(cpu) > 1) ||
1104938Sesaxe 		    opteron_workaround_6336786_UP) {
11055893Sesaxe 
11065893Sesaxe 			uint_t	node, nnodes;
11073446Smrj 			uint8_t data;
11083446Smrj 
11095893Sesaxe 			nnodes = opteron_get_nnodes();
11105893Sesaxe 			for (node = 0; node < nnodes; node++) {
1111938Sesaxe 				/*
1112938Sesaxe 				 * Clear PMM7[1:0] (function 3, offset 0x87)
1113938Sesaxe 				 * Northbridge device is the node id + 24.
1114938Sesaxe 				 */
1115938Sesaxe 				data = pci_getb_func(0, node + 24, 3, 0x87);
1116938Sesaxe 				data &= 0xFC;
1117938Sesaxe 				pci_putb_func(0, node + 24, 3, 0x87, data);
1118938Sesaxe 			}
1119938Sesaxe 			opteron_workaround_6336786++;
11205084Sjohnlev #endif	/* __xpv */
1121938Sesaxe 		}
11223446Smrj #else
11233446Smrj 		workaround_warning(cpu, 6336786);
11243446Smrj 		missing++;
1125938Sesaxe #endif
11263446Smrj 	}
11271582Skchow 
11281582Skchow 	/*LINTED*/
11291582Skchow 	/*
11301582Skchow 	 * Mutex primitives don't work as expected.
11311582Skchow 	 */
11321582Skchow 	if (cpuid_opteron_erratum(cpu, 6323525) > 0) {
11333446Smrj #if defined(OPTERON_WORKAROUND_6323525)
11341582Skchow 		/*
11353446Smrj 		 * This problem only occurs with 2 or more cores. If bit in
11366691Skchow 		 * MSR_AMD_BU_CFG set, then not applicable. The workaround
11371582Skchow 		 * is to patch the semaphone routines with the lfence
11381582Skchow 		 * instruction to provide necessary load memory barrier with
11391582Skchow 		 * possible subsequent read-modify-write ops.
11401582Skchow 		 *
11411582Skchow 		 * It is too early in boot to call the patch routine so
11421582Skchow 		 * set erratum variable to be done in startup_end().
11431582Skchow 		 */
11441582Skchow 		if (opteron_workaround_6323525) {
11451582Skchow 			opteron_workaround_6323525++;
11465084Sjohnlev #if defined(__xpv)
114712826Skuriakose.kuruvilla@oracle.com 		} else if (is_x86_feature(x86_featureset, X86FSET_SSE2)) {
11485084Sjohnlev 			if (DOMAIN_IS_INITDOMAIN(xen_info)) {
11495084Sjohnlev 				/*
11505084Sjohnlev 				 * XXPV	Use dom0_msr here when extended
11515084Sjohnlev 				 *	operations are supported?
11525084Sjohnlev 				 */
115310175SStuart.Maybee@Sun.COM 				if (xpv_nr_phys_cpus() > 1)
11545084Sjohnlev 					opteron_workaround_6323525++;
11555084Sjohnlev 			} else {
11565084Sjohnlev 				/*
11575084Sjohnlev 				 * We have no way to tell how many physical
11585084Sjohnlev 				 * cpus there are, or even if this processor
11595084Sjohnlev 				 * has the problem, so enable the workaround
11605084Sjohnlev 				 * unconditionally (at some performance cost).
11615084Sjohnlev 				 */
11625084Sjohnlev 				opteron_workaround_6323525++;
11635084Sjohnlev 			}
11645084Sjohnlev #else	/* __xpv */
116512826Skuriakose.kuruvilla@oracle.com 		} else if (is_x86_feature(x86_featureset, X86FSET_SSE2) &&
116612826Skuriakose.kuruvilla@oracle.com 		    ((opteron_get_nnodes() *
11673446Smrj 		    cpuid_get_ncpu_per_chip(cpu)) > 1)) {
11688374SKit.Chow@Sun.COM 			if ((xrdmsr(MSR_AMD_BU_CFG) & (UINT64_C(1) << 33)) == 0)
11691582Skchow 				opteron_workaround_6323525++;
11705084Sjohnlev #endif	/* __xpv */
11711582Skchow 		}
11723446Smrj #else
11733446Smrj 		workaround_warning(cpu, 6323525);
11743446Smrj 		missing++;
11753446Smrj #endif
11761582Skchow 	}
11773446Smrj 
11786691Skchow 	missing += do_erratum_298(cpu);
11796691Skchow 
11805084Sjohnlev #ifdef __xpv
11815084Sjohnlev 	return (0);
11825084Sjohnlev #else
11830Sstevel@tonic-gate 	return (missing);
11845084Sjohnlev #endif
11850Sstevel@tonic-gate }
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate void
workaround_errata_end()11880Sstevel@tonic-gate workaround_errata_end()
11890Sstevel@tonic-gate {
11903446Smrj #if defined(OPTERON_ERRATUM_88)
11913446Smrj 	if (opteron_erratum_88)
11923446Smrj 		workaround_applied(88);
11933446Smrj #endif
11943446Smrj #if defined(OPTERON_ERRATUM_91)
11953446Smrj 	if (opteron_erratum_91)
11963446Smrj 		workaround_applied(91);
11973446Smrj #endif
11983446Smrj #if defined(OPTERON_ERRATUM_93)
11993446Smrj 	if (opteron_erratum_93)
12003446Smrj 		workaround_applied(93);
12013446Smrj #endif
12023446Smrj #if defined(OPTERON_ERRATUM_95)
12033446Smrj 	if (opteron_erratum_95)
12043446Smrj 		workaround_applied(95);
12053446Smrj #endif
12063446Smrj #if defined(OPTERON_ERRATUM_100)
12073446Smrj 	if (opteron_erratum_100)
12083446Smrj 		workaround_applied(100);
12093446Smrj #endif
12103446Smrj #if defined(OPTERON_ERRATUM_108)
12113446Smrj 	if (opteron_erratum_108)
12123446Smrj 		workaround_applied(108);
12133446Smrj #endif
12140Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
12150Sstevel@tonic-gate 	if (opteron_erratum_109) {
1216359Skucharsk 		cmn_err(CE_WARN,
1217359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1218359Skucharsk 		    " processor\nerratum 109 was not detected; updating your"
1219359Skucharsk 		    " system's BIOS to a version\ncontaining this"
1220359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
1221359Skucharsk 		    " system\noperation may occur.\n");
12220Sstevel@tonic-gate 	}
12233446Smrj #endif
12243446Smrj #if defined(OPTERON_ERRATUM_121)
12253446Smrj 	if (opteron_erratum_121)
12263446Smrj 		workaround_applied(121);
12273446Smrj #endif
12283446Smrj #if defined(OPTERON_ERRATUM_122)
12293446Smrj 	if (opteron_erratum_122)
12303446Smrj 		workaround_applied(122);
12313446Smrj #endif
12320Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
12330Sstevel@tonic-gate 	if (opteron_erratum_123) {
1234359Skucharsk 		cmn_err(CE_WARN,
1235359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1236359Skucharsk 		    " processor\nerratum 123 was not detected; updating your"
1237359Skucharsk 		    " system's BIOS to a version\ncontaining this"
1238359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
1239359Skucharsk 		    " system\noperation may occur.\n");
12400Sstevel@tonic-gate 	}
12413446Smrj #endif
1242359Skucharsk #if defined(OPTERON_ERRATUM_131)
1243359Skucharsk 	if (opteron_erratum_131) {
1244359Skucharsk 		cmn_err(CE_WARN,
1245359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1246359Skucharsk 		    " processor\nerratum 131 was not detected; updating your"
1247359Skucharsk 		    " system's BIOS to a version\ncontaining this"
1248359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
1249359Skucharsk 		    " system\noperation may occur.\n");
1250359Skucharsk 	}
12513446Smrj #endif
12523446Smrj #if defined(OPTERON_WORKAROUND_6336786)
12533446Smrj 	if (opteron_workaround_6336786)
12543446Smrj 		workaround_applied(6336786);
12553446Smrj #endif
12563446Smrj #if defined(OPTERON_WORKAROUND_6323525)
12573446Smrj 	if (opteron_workaround_6323525)
12583446Smrj 		workaround_applied(6323525);
12593446Smrj #endif
12606691Skchow #if defined(OPTERON_ERRATUM_298)
12616691Skchow 	if (opteron_erratum_298) {
12626691Skchow 		cmn_err(CE_WARN,
12636691Skchow 		    "BIOS microcode patch for AMD 64/Opteron(tm)"
12646691Skchow 		    " processor\nerratum 298 was not detected; updating your"
12656691Skchow 		    " system's BIOS to a version\ncontaining this"
12666691Skchow 		    " microcode patch is HIGHLY recommended or erroneous"
12676691Skchow 		    " system\noperation may occur.\n");
12686691Skchow 	}
12696691Skchow #endif
12700Sstevel@tonic-gate }
12710Sstevel@tonic-gate 
127212004Sjiang.liu@intel.com /*
127312004Sjiang.liu@intel.com  * The procset_slave and procset_master are used to synchronize
127412004Sjiang.liu@intel.com  * between the control CPU and the target CPU when starting CPUs.
127512004Sjiang.liu@intel.com  */
127612004Sjiang.liu@intel.com static cpuset_t procset_slave, procset_master;
127712004Sjiang.liu@intel.com 
127812004Sjiang.liu@intel.com static void
mp_startup_wait(cpuset_t * sp,processorid_t cpuid)127912004Sjiang.liu@intel.com mp_startup_wait(cpuset_t *sp, processorid_t cpuid)
128012004Sjiang.liu@intel.com {
128112004Sjiang.liu@intel.com 	cpuset_t tempset;
128212004Sjiang.liu@intel.com 
128312004Sjiang.liu@intel.com 	for (tempset = *sp; !CPU_IN_SET(tempset, cpuid);
128412004Sjiang.liu@intel.com 	    tempset = *(volatile cpuset_t *)sp) {
128512004Sjiang.liu@intel.com 		SMT_PAUSE();
128612004Sjiang.liu@intel.com 	}
128712004Sjiang.liu@intel.com 	CPUSET_ATOMIC_DEL(*(cpuset_t *)sp, cpuid);
128812004Sjiang.liu@intel.com }
128912004Sjiang.liu@intel.com 
129012004Sjiang.liu@intel.com static void
mp_startup_signal(cpuset_t * sp,processorid_t cpuid)129112004Sjiang.liu@intel.com mp_startup_signal(cpuset_t *sp, processorid_t cpuid)
129212004Sjiang.liu@intel.com {
129312004Sjiang.liu@intel.com 	cpuset_t tempset;
129412004Sjiang.liu@intel.com 
129512004Sjiang.liu@intel.com 	CPUSET_ATOMIC_ADD(*(cpuset_t *)sp, cpuid);
129612004Sjiang.liu@intel.com 	for (tempset = *sp; CPU_IN_SET(tempset, cpuid);
129712004Sjiang.liu@intel.com 	    tempset = *(volatile cpuset_t *)sp) {
129812004Sjiang.liu@intel.com 		SMT_PAUSE();
129912004Sjiang.liu@intel.com 	}
130012004Sjiang.liu@intel.com }
130112004Sjiang.liu@intel.com 
130212004Sjiang.liu@intel.com int
mp_start_cpu_common(cpu_t * cp,boolean_t boot)130312004Sjiang.liu@intel.com mp_start_cpu_common(cpu_t *cp, boolean_t boot)
130412004Sjiang.liu@intel.com {
130512004Sjiang.liu@intel.com 	_NOTE(ARGUNUSED(boot));
130612004Sjiang.liu@intel.com 
130712004Sjiang.liu@intel.com 	void *ctx;
130812004Sjiang.liu@intel.com 	int delays;
130912004Sjiang.liu@intel.com 	int error = 0;
131012004Sjiang.liu@intel.com 	cpuset_t tempset;
131112004Sjiang.liu@intel.com 	processorid_t cpuid;
131212004Sjiang.liu@intel.com #ifndef __xpv
131312004Sjiang.liu@intel.com 	extern void cpupm_init(cpu_t *);
131412004Sjiang.liu@intel.com #endif
131512004Sjiang.liu@intel.com 
131612004Sjiang.liu@intel.com 	ASSERT(cp != NULL);
131712004Sjiang.liu@intel.com 	cpuid = cp->cpu_id;
131812004Sjiang.liu@intel.com 	ctx = mach_cpucontext_alloc(cp);
131912004Sjiang.liu@intel.com 	if (ctx == NULL) {
132012004Sjiang.liu@intel.com 		cmn_err(CE_WARN,
132112004Sjiang.liu@intel.com 		    "cpu%d: failed to allocate context", cp->cpu_id);
132212004Sjiang.liu@intel.com 		return (EAGAIN);
132312004Sjiang.liu@intel.com 	}
132412004Sjiang.liu@intel.com 	error = mach_cpu_start(cp, ctx);
132512004Sjiang.liu@intel.com 	if (error != 0) {
132612004Sjiang.liu@intel.com 		cmn_err(CE_WARN,
132712004Sjiang.liu@intel.com 		    "cpu%d: failed to start, error %d", cp->cpu_id, error);
132812004Sjiang.liu@intel.com 		mach_cpucontext_free(cp, ctx, error);
132912004Sjiang.liu@intel.com 		return (error);
133012004Sjiang.liu@intel.com 	}
133112004Sjiang.liu@intel.com 
133212004Sjiang.liu@intel.com 	for (delays = 0, tempset = procset_slave; !CPU_IN_SET(tempset, cpuid);
133312004Sjiang.liu@intel.com 	    delays++) {
133412004Sjiang.liu@intel.com 		if (delays == 500) {
133512004Sjiang.liu@intel.com 			/*
133612004Sjiang.liu@intel.com 			 * After five seconds, things are probably looking
133712004Sjiang.liu@intel.com 			 * a bit bleak - explain the hang.
133812004Sjiang.liu@intel.com 			 */
133912004Sjiang.liu@intel.com 			cmn_err(CE_NOTE, "cpu%d: started, "
134012004Sjiang.liu@intel.com 			    "but not running in the kernel yet", cpuid);
134112004Sjiang.liu@intel.com 		} else if (delays > 2000) {
134212004Sjiang.liu@intel.com 			/*
134312004Sjiang.liu@intel.com 			 * We waited at least 20 seconds, bail ..
134412004Sjiang.liu@intel.com 			 */
134512004Sjiang.liu@intel.com 			error = ETIMEDOUT;
134612004Sjiang.liu@intel.com 			cmn_err(CE_WARN, "cpu%d: timed out", cpuid);
134712004Sjiang.liu@intel.com 			mach_cpucontext_free(cp, ctx, error);
134812004Sjiang.liu@intel.com 			return (error);
134912004Sjiang.liu@intel.com 		}
135012004Sjiang.liu@intel.com 
135112004Sjiang.liu@intel.com 		/*
135212004Sjiang.liu@intel.com 		 * wait at least 10ms, then check again..
135312004Sjiang.liu@intel.com 		 */
135412004Sjiang.liu@intel.com 		delay(USEC_TO_TICK_ROUNDUP(10000));
135512004Sjiang.liu@intel.com 		tempset = *((volatile cpuset_t *)&procset_slave);
135612004Sjiang.liu@intel.com 	}
135712004Sjiang.liu@intel.com 	CPUSET_ATOMIC_DEL(procset_slave, cpuid);
135812004Sjiang.liu@intel.com 
135912004Sjiang.liu@intel.com 	mach_cpucontext_free(cp, ctx, 0);
136012004Sjiang.liu@intel.com 
136112004Sjiang.liu@intel.com #ifndef __xpv
136212004Sjiang.liu@intel.com 	if (tsc_gethrtime_enable)
136312004Sjiang.liu@intel.com 		tsc_sync_master(cpuid);
136412004Sjiang.liu@intel.com #endif
136512004Sjiang.liu@intel.com 
136612004Sjiang.liu@intel.com 	if (dtrace_cpu_init != NULL) {
136712004Sjiang.liu@intel.com 		(*dtrace_cpu_init)(cpuid);
136812004Sjiang.liu@intel.com 	}
136912004Sjiang.liu@intel.com 
137012004Sjiang.liu@intel.com 	/*
137112004Sjiang.liu@intel.com 	 * During CPU DR operations, the cpu_lock is held by current
137212004Sjiang.liu@intel.com 	 * (the control) thread. We can't release the cpu_lock here
137312004Sjiang.liu@intel.com 	 * because that will break the CPU DR logic.
137412004Sjiang.liu@intel.com 	 * On the other hand, CPUPM and processor group initialization
137512004Sjiang.liu@intel.com 	 * routines need to access the cpu_lock. So we invoke those
137612004Sjiang.liu@intel.com 	 * routines here on behalf of mp_startup_common().
137712004Sjiang.liu@intel.com 	 *
137812004Sjiang.liu@intel.com 	 * CPUPM and processor group initialization routines depend
137912004Sjiang.liu@intel.com 	 * on the cpuid probing results. Wait for mp_startup_common()
138012004Sjiang.liu@intel.com 	 * to signal that cpuid probing is done.
138112004Sjiang.liu@intel.com 	 */
138212004Sjiang.liu@intel.com 	mp_startup_wait(&procset_slave, cpuid);
138312004Sjiang.liu@intel.com #ifndef __xpv
138412004Sjiang.liu@intel.com 	cpupm_init(cp);
138512004Sjiang.liu@intel.com #endif
138612004Sjiang.liu@intel.com 	(void) pg_cpu_init(cp, B_FALSE);
138712004Sjiang.liu@intel.com 	cpu_set_state(cp);
138812004Sjiang.liu@intel.com 	mp_startup_signal(&procset_master, cpuid);
138912004Sjiang.liu@intel.com 
139012004Sjiang.liu@intel.com 	return (0);
139112004Sjiang.liu@intel.com }
13923446Smrj 
13933446Smrj /*
13943446Smrj  * Start a single cpu, assuming that the kernel context is available
13953446Smrj  * to successfully start another cpu.
13963446Smrj  *
13973446Smrj  * (For example, real mode code is mapped into the right place
13983446Smrj  * in memory and is ready to be run.)
13993446Smrj  */
14003446Smrj int
start_cpu(processorid_t who)14013446Smrj start_cpu(processorid_t who)
14023446Smrj {
14033446Smrj 	cpu_t *cp;
14043446Smrj 	int error = 0;
140512004Sjiang.liu@intel.com 	cpuset_t tempset;
14063446Smrj 
14073446Smrj 	ASSERT(who != 0);
14083446Smrj 
14093446Smrj 	/*
14103446Smrj 	 * Check if there's at least a Mbyte of kmem available
14113446Smrj 	 * before attempting to start the cpu.
14123446Smrj 	 */
14133446Smrj 	if (kmem_avail() < 1024 * 1024) {
14143446Smrj 		/*
14153446Smrj 		 * Kick off a reap in case that helps us with
14163446Smrj 		 * later attempts ..
14173446Smrj 		 */
14183446Smrj 		kmem_reap();
14193446Smrj 		return (ENOMEM);
14203446Smrj 	}
14213446Smrj 
142212004Sjiang.liu@intel.com 	/*
142312004Sjiang.liu@intel.com 	 * First configure cpu.
142412004Sjiang.liu@intel.com 	 */
142512004Sjiang.liu@intel.com 	cp = mp_cpu_configure_common(who, B_TRUE);
142612004Sjiang.liu@intel.com 	ASSERT(cp != NULL);
14273446Smrj 
142812004Sjiang.liu@intel.com 	/*
142912004Sjiang.liu@intel.com 	 * Then start cpu.
143012004Sjiang.liu@intel.com 	 */
143112004Sjiang.liu@intel.com 	error = mp_start_cpu_common(cp, B_TRUE);
143212004Sjiang.liu@intel.com 	if (error != 0) {
143312004Sjiang.liu@intel.com 		mp_cpu_unconfigure_common(cp, error);
14343446Smrj 		return (error);
14353446Smrj 	}
14363446Smrj 
143712004Sjiang.liu@intel.com 	mutex_exit(&cpu_lock);
143812004Sjiang.liu@intel.com 	tempset = cpu_ready_set;
143912004Sjiang.liu@intel.com 	while (!CPU_IN_SET(tempset, who)) {
144012004Sjiang.liu@intel.com 		drv_usecwait(1);
144112004Sjiang.liu@intel.com 		tempset = *((volatile cpuset_t *)&cpu_ready_set);
14423446Smrj 	}
144312004Sjiang.liu@intel.com 	mutex_enter(&cpu_lock);
14443446Smrj 
14453446Smrj 	return (0);
14463446Smrj }
14473446Smrj 
14480Sstevel@tonic-gate void
start_other_cpus(int cprboot)14490Sstevel@tonic-gate start_other_cpus(int cprboot)
14500Sstevel@tonic-gate {
145112004Sjiang.liu@intel.com 	_NOTE(ARGUNUSED(cprboot));
145212004Sjiang.liu@intel.com 
14533446Smrj 	uint_t who;
14543446Smrj 	uint_t bootcpuid = 0;
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	/*
14570Sstevel@tonic-gate 	 * Initialize our own cpu_info.
14580Sstevel@tonic-gate 	 */
14590Sstevel@tonic-gate 	init_cpu_info(CPU);
14600Sstevel@tonic-gate 
14617656SSherry.Moore@Sun.COM 	cmn_err(CE_CONT, "?cpu%d: %s\n", CPU->cpu_id, CPU->cpu_idstr);
14627656SSherry.Moore@Sun.COM 	cmn_err(CE_CONT, "?cpu%d: %s\n", CPU->cpu_id, CPU->cpu_brandstr);
14637656SSherry.Moore@Sun.COM 
14640Sstevel@tonic-gate 	/*
14650Sstevel@tonic-gate 	 * Initialize our syscall handlers
14660Sstevel@tonic-gate 	 */
14670Sstevel@tonic-gate 	init_cpu_syscall(CPU);
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 	/*
14703446Smrj 	 * Take the boot cpu out of the mp_cpus set because we know
14713446Smrj 	 * it's already running.  Add it to the cpu_ready_set for
14723446Smrj 	 * precisely the same reason.
14733446Smrj 	 */
14743446Smrj 	CPUSET_DEL(mp_cpus, bootcpuid);
14753446Smrj 	CPUSET_ADD(cpu_ready_set, bootcpuid);
14763446Smrj 
14773446Smrj 	/*
147812004Sjiang.liu@intel.com 	 * skip the rest of this if
147912004Sjiang.liu@intel.com 	 * . only 1 cpu dectected and system isn't hotplug-capable
148012004Sjiang.liu@intel.com 	 * . not using MP
14810Sstevel@tonic-gate 	 */
148212004Sjiang.liu@intel.com 	if ((CPUSET_ISNULL(mp_cpus) && plat_dr_support_cpu() == 0) ||
148312004Sjiang.liu@intel.com 	    use_mp == 0) {
14840Sstevel@tonic-gate 		if (use_mp == 0)
14850Sstevel@tonic-gate 			cmn_err(CE_CONT, "?***** Not in MP mode\n");
14860Sstevel@tonic-gate 		goto done;
14870Sstevel@tonic-gate 	}
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 	/*
14900Sstevel@tonic-gate 	 * perform such initialization as is needed
14910Sstevel@tonic-gate 	 * to be able to take CPUs on- and off-line.
14920Sstevel@tonic-gate 	 */
14930Sstevel@tonic-gate 	cpu_pause_init();
14940Sstevel@tonic-gate 
14959489SJoe.Bonasera@sun.com 	xc_init_cpu(CPU);		/* initialize processor crosscalls */
14960Sstevel@tonic-gate 
14973446Smrj 	if (mach_cpucontext_init() != 0)
14980Sstevel@tonic-gate 		goto done;
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 	flushes_require_xcalls = 1;
15010Sstevel@tonic-gate 
15022575Snf202958 	/*
15032575Snf202958 	 * We lock our affinity to the master CPU to ensure that all slave CPUs
15042575Snf202958 	 * do their TSC syncs with the same CPU.
15052575Snf202958 	 */
15060Sstevel@tonic-gate 	affinity_set(CPU_CURRENT);
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	for (who = 0; who < NCPU; who++) {
15092006Sandrei 		if (!CPU_IN_SET(mp_cpus, who))
15102006Sandrei 			continue;
15113446Smrj 		ASSERT(who != bootcpuid);
151212004Sjiang.liu@intel.com 
151312004Sjiang.liu@intel.com 		mutex_enter(&cpu_lock);
15143446Smrj 		if (start_cpu(who) != 0)
15153446Smrj 			CPUSET_DEL(mp_cpus, who);
15168803SJonathan.Haslam@Sun.COM 		cpu_state_change_notify(who, CPU_SETUP);
15178803SJonathan.Haslam@Sun.COM 		mutex_exit(&cpu_lock);
15180Sstevel@tonic-gate 	}
15190Sstevel@tonic-gate 
15204581Ssherrym 	/* Free the space allocated to hold the microcode file */
15217605SMark.Johnson@Sun.COM 	ucode_cleanup();
15224581Ssherrym 
15230Sstevel@tonic-gate 	affinity_clear();
15240Sstevel@tonic-gate 
152512004Sjiang.liu@intel.com 	mach_cpucontext_fini();
15262006Sandrei 
15270Sstevel@tonic-gate done:
152811120SMark.Johnson@Sun.COM 	if (get_hwenv() == HW_NATIVE)
152911120SMark.Johnson@Sun.COM 		workaround_errata_end();
153012004Sjiang.liu@intel.com 	cmi_post_mpstartup();
15311642Sgavinm 
153212004Sjiang.liu@intel.com 	if (use_mp && ncpus != boot_max_ncpus) {
153312004Sjiang.liu@intel.com 		cmn_err(CE_NOTE,
153412004Sjiang.liu@intel.com 		    "System detected %d cpus, but "
153512004Sjiang.liu@intel.com 		    "only %d cpu(s) were enabled during boot.",
153612004Sjiang.liu@intel.com 		    boot_max_ncpus, ncpus);
153712004Sjiang.liu@intel.com 		cmn_err(CE_NOTE,
153812004Sjiang.liu@intel.com 		    "Use \"boot-ncpus\" parameter to enable more CPU(s). "
153912004Sjiang.liu@intel.com 		    "See eeprom(1M).");
154012004Sjiang.liu@intel.com 	}
15410Sstevel@tonic-gate }
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate int
mp_cpu_configure(int cpuid)15440Sstevel@tonic-gate mp_cpu_configure(int cpuid)
15450Sstevel@tonic-gate {
154612004Sjiang.liu@intel.com 	cpu_t *cp;
154712004Sjiang.liu@intel.com 
154812004Sjiang.liu@intel.com 	if (use_mp == 0 || plat_dr_support_cpu() == 0) {
154912004Sjiang.liu@intel.com 		return (ENOTSUP);
155012004Sjiang.liu@intel.com 	}
155112004Sjiang.liu@intel.com 
155212004Sjiang.liu@intel.com 	cp = cpu_get(cpuid);
155312004Sjiang.liu@intel.com 	if (cp != NULL) {
155412004Sjiang.liu@intel.com 		return (EALREADY);
155512004Sjiang.liu@intel.com 	}
155612004Sjiang.liu@intel.com 
155712004Sjiang.liu@intel.com 	/*
155812004Sjiang.liu@intel.com 	 * Check if there's at least a Mbyte of kmem available
155912004Sjiang.liu@intel.com 	 * before attempting to start the cpu.
156012004Sjiang.liu@intel.com 	 */
156112004Sjiang.liu@intel.com 	if (kmem_avail() < 1024 * 1024) {
156212004Sjiang.liu@intel.com 		/*
156312004Sjiang.liu@intel.com 		 * Kick off a reap in case that helps us with
156412004Sjiang.liu@intel.com 		 * later attempts ..
156512004Sjiang.liu@intel.com 		 */
156612004Sjiang.liu@intel.com 		kmem_reap();
156712004Sjiang.liu@intel.com 		return (ENOMEM);
156812004Sjiang.liu@intel.com 	}
156912004Sjiang.liu@intel.com 
157012004Sjiang.liu@intel.com 	cp = mp_cpu_configure_common(cpuid, B_FALSE);
157112004Sjiang.liu@intel.com 	ASSERT(cp != NULL && cpu_get(cpuid) == cp);
157212004Sjiang.liu@intel.com 
157312004Sjiang.liu@intel.com 	return (cp != NULL ? 0 : EAGAIN);
15740Sstevel@tonic-gate }
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate int
mp_cpu_unconfigure(int cpuid)15770Sstevel@tonic-gate mp_cpu_unconfigure(int cpuid)
15780Sstevel@tonic-gate {
157912004Sjiang.liu@intel.com 	cpu_t *cp;
158012004Sjiang.liu@intel.com 
158112004Sjiang.liu@intel.com 	if (use_mp == 0 || plat_dr_support_cpu() == 0) {
158212004Sjiang.liu@intel.com 		return (ENOTSUP);
158312004Sjiang.liu@intel.com 	} else if (cpuid < 0 || cpuid >= max_ncpus) {
158412004Sjiang.liu@intel.com 		return (EINVAL);
158512004Sjiang.liu@intel.com 	}
158612004Sjiang.liu@intel.com 
158712004Sjiang.liu@intel.com 	cp = cpu_get(cpuid);
158812004Sjiang.liu@intel.com 	if (cp == NULL) {
158912004Sjiang.liu@intel.com 		return (ENODEV);
159012004Sjiang.liu@intel.com 	}
159112004Sjiang.liu@intel.com 	mp_cpu_unconfigure_common(cp, 0);
159212004Sjiang.liu@intel.com 
159312004Sjiang.liu@intel.com 	return (0);
15940Sstevel@tonic-gate }
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate /*
15970Sstevel@tonic-gate  * Startup function for 'other' CPUs (besides boot cpu).
15982985Sdmick  * Called from real_mode_start.
15991251Skchow  *
160012004Sjiang.liu@intel.com  * WARNING: until CPU_READY is set, mp_startup_common and routines called by
160112004Sjiang.liu@intel.com  * mp_startup_common should not call routines (e.g. kmem_free) that could call
16021251Skchow  * hat_unload which requires CPU_READY to be set.
16030Sstevel@tonic-gate  */
160412004Sjiang.liu@intel.com static void
mp_startup_common(boolean_t boot)160512004Sjiang.liu@intel.com mp_startup_common(boolean_t boot)
16060Sstevel@tonic-gate {
160712004Sjiang.liu@intel.com 	cpu_t *cp = CPU;
1608*13136Skuriakose.kuruvilla@oracle.com 	uchar_t new_x86_featureset[BT_SIZEOFMAP(NUM_X86_FEATURES)];
16099637SRandy.Fishel@Sun.COM 	extern void cpu_event_init_cpu(cpu_t *);
16100Sstevel@tonic-gate 
16112985Sdmick 	/*
16123021Sdmick 	 * We need to get TSC on this proc synced (i.e., any delta
16133021Sdmick 	 * from cpu0 accounted for) as soon as we can, because many
16143021Sdmick 	 * many things use gethrtime/pc_gethrestime, including
16153021Sdmick 	 * interrupts, cmn_err, etc.
16163021Sdmick 	 */
16173021Sdmick 
161812004Sjiang.liu@intel.com 	/* Let the control CPU continue into tsc_sync_master() */
161912004Sjiang.liu@intel.com 	mp_startup_signal(&procset_slave, cp->cpu_id);
16203021Sdmick 
16215084Sjohnlev #ifndef __xpv
16223021Sdmick 	if (tsc_gethrtime_enable)
16233021Sdmick 		tsc_sync_slave();
16245084Sjohnlev #endif
16253021Sdmick 
16263021Sdmick 	/*
16272985Sdmick 	 * Once this was done from assembly, but it's safer here; if
16282985Sdmick 	 * it blocks, we need to be able to swtch() to and from, and
16292985Sdmick 	 * since we get here by calling t_pc, we need to do that call
16302985Sdmick 	 * before swtch() overwrites it.
16312985Sdmick 	 */
16322985Sdmick 	(void) (*ap_mlsetup)();
16332985Sdmick 
1634*13136Skuriakose.kuruvilla@oracle.com 	bzero(new_x86_featureset, BT_SIZEOFMAP(NUM_X86_FEATURES));
1635*13136Skuriakose.kuruvilla@oracle.com 	cpuid_pass1(cp, new_x86_featureset);
16360Sstevel@tonic-gate 
16375084Sjohnlev #ifndef __xpv
16380Sstevel@tonic-gate 	/*
16395159Sjohnlev 	 * Program this cpu's PAT
16400Sstevel@tonic-gate 	 */
164112826Skuriakose.kuruvilla@oracle.com 	if (is_x86_feature(x86_featureset, X86FSET_PAT))
16425159Sjohnlev 		pat_sync();
16435084Sjohnlev #endif
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate 	/*
16463446Smrj 	 * Set up TSC_AUX to contain the cpuid for this processor
16473446Smrj 	 * for the rdtscp instruction.
16483446Smrj 	 */
164912826Skuriakose.kuruvilla@oracle.com 	if (is_x86_feature(x86_featureset, X86FSET_TSCP))
16503446Smrj 		(void) wrmsr(MSR_AMD_TSCAUX, cp->cpu_id);
16513446Smrj 
16523446Smrj 	/*
16530Sstevel@tonic-gate 	 * Initialize this CPU's syscall handlers
16540Sstevel@tonic-gate 	 */
16550Sstevel@tonic-gate 	init_cpu_syscall(cp);
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 	/*
16580Sstevel@tonic-gate 	 * Enable interrupts with spl set to LOCK_LEVEL. LOCK_LEVEL is the
16590Sstevel@tonic-gate 	 * highest level at which a routine is permitted to block on
16600Sstevel@tonic-gate 	 * an adaptive mutex (allows for cpu poke interrupt in case
16610Sstevel@tonic-gate 	 * the cpu is blocked on a mutex and halts). Setting LOCK_LEVEL blocks
16620Sstevel@tonic-gate 	 * device interrupts that may end up in the hat layer issuing cross
16630Sstevel@tonic-gate 	 * calls before CPU_READY is set.
16640Sstevel@tonic-gate 	 */
16653446Smrj 	splx(ipltospl(LOCK_LEVEL));
16663446Smrj 	sti();
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 	/*
16690Sstevel@tonic-gate 	 * Do a sanity check to make sure this new CPU is a sane thing
16700Sstevel@tonic-gate 	 * to add to the collection of processors running this system.
16710Sstevel@tonic-gate 	 *
16720Sstevel@tonic-gate 	 * XXX	Clearly this needs to get more sophisticated, if x86
16730Sstevel@tonic-gate 	 * systems start to get built out of heterogenous CPUs; as is
16740Sstevel@tonic-gate 	 * likely to happen once the number of processors in a configuration
16750Sstevel@tonic-gate 	 * gets large enough.
16760Sstevel@tonic-gate 	 */
167712826Skuriakose.kuruvilla@oracle.com 	if (compare_x86_featureset(x86_featureset, new_x86_featureset) ==
167812826Skuriakose.kuruvilla@oracle.com 	    B_FALSE) {
167912826Skuriakose.kuruvilla@oracle.com 		cmn_err(CE_CONT, "cpu%d: featureset\n", cp->cpu_id);
168012826Skuriakose.kuruvilla@oracle.com 		print_x86_featureset(new_x86_featureset);
16810Sstevel@tonic-gate 		cmn_err(CE_WARN, "cpu%d feature mismatch", cp->cpu_id);
16820Sstevel@tonic-gate 	}
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	/*
16854481Sbholler 	 * We do not support cpus with mixed monitor/mwait support if the
16864481Sbholler 	 * boot cpu supports monitor/mwait.
16874481Sbholler 	 */
168812826Skuriakose.kuruvilla@oracle.com 	if (is_x86_feature(x86_featureset, X86FSET_MWAIT) !=
168912826Skuriakose.kuruvilla@oracle.com 	    is_x86_feature(new_x86_featureset, X86FSET_MWAIT))
16904481Sbholler 		panic("unsupported mixed cpu monitor/mwait support detected");
16914481Sbholler 
16924481Sbholler 	/*
16930Sstevel@tonic-gate 	 * We could be more sophisticated here, and just mark the CPU
16940Sstevel@tonic-gate 	 * as "faulted" but at this point we'll opt for the easier
16959637SRandy.Fishel@Sun.COM 	 * answer of dying horribly.  Provided the boot cpu is ok,
16960Sstevel@tonic-gate 	 * the system can be recovered by booting with use_mp set to zero.
16970Sstevel@tonic-gate 	 */
16980Sstevel@tonic-gate 	if (workaround_errata(cp) != 0)
16990Sstevel@tonic-gate 		panic("critical workaround(s) missing for cpu%d", cp->cpu_id);
17000Sstevel@tonic-gate 
170112004Sjiang.liu@intel.com 	/*
170212004Sjiang.liu@intel.com 	 * We can touch cpu_flags here without acquiring the cpu_lock here
170312004Sjiang.liu@intel.com 	 * because the cpu_lock is held by the control CPU which is running
170412004Sjiang.liu@intel.com 	 * mp_start_cpu_common().
170512004Sjiang.liu@intel.com 	 * Need to clear CPU_QUIESCED flag before calling any function which
170612004Sjiang.liu@intel.com 	 * may cause thread context switching, such as kmem_alloc() etc.
170712004Sjiang.liu@intel.com 	 * The idle thread checks for CPU_QUIESCED flag and loops for ever if
170812004Sjiang.liu@intel.com 	 * it's set. So the startup thread may have no chance to switch back
170912004Sjiang.liu@intel.com 	 * again if it's switched away with CPU_QUIESCED set.
171012004Sjiang.liu@intel.com 	 */
171112004Sjiang.liu@intel.com 	cp->cpu_flags &= ~(CPU_POWEROFF | CPU_QUIESCED);
171212004Sjiang.liu@intel.com 
171313134Skuriakose.kuruvilla@oracle.com 	/*
171413134Skuriakose.kuruvilla@oracle.com 	 * Setup this processor for XSAVE.
171513134Skuriakose.kuruvilla@oracle.com 	 */
171613134Skuriakose.kuruvilla@oracle.com 	if (fp_save_mech == FP_XSAVE) {
171713134Skuriakose.kuruvilla@oracle.com 		xsave_setup_msr(cp);
171813134Skuriakose.kuruvilla@oracle.com 	}
171913134Skuriakose.kuruvilla@oracle.com 
17200Sstevel@tonic-gate 	cpuid_pass2(cp);
17210Sstevel@tonic-gate 	cpuid_pass3(cp);
17220Sstevel@tonic-gate 	(void) cpuid_pass4(cp);
17230Sstevel@tonic-gate 
172412004Sjiang.liu@intel.com 	/*
172512004Sjiang.liu@intel.com 	 * Correct cpu_idstr and cpu_brandstr on target CPU after
172612004Sjiang.liu@intel.com 	 * cpuid_pass1() is done.
172712004Sjiang.liu@intel.com 	 */
172812004Sjiang.liu@intel.com 	(void) cpuid_getidstr(cp, cp->cpu_idstr, CPU_IDSTRLEN);
172912004Sjiang.liu@intel.com 	(void) cpuid_getbrandstr(cp, cp->cpu_brandstr, CPU_IDSTRLEN);
17300Sstevel@tonic-gate 
17316749Ssherrym 	cp->cpu_flags |= CPU_RUNNING | CPU_READY | CPU_EXISTS;
17322575Snf202958 
173312004Sjiang.liu@intel.com 	post_startup_cpu_fixups();
17342575Snf202958 
173512004Sjiang.liu@intel.com 	cpu_event_init_cpu(cp);
17368930SBill.Holler@Sun.COM 
17373029Ssethg 	/*
17383029Ssethg 	 * Enable preemption here so that contention for any locks acquired
173912004Sjiang.liu@intel.com 	 * later in mp_startup_common may be preempted if the thread owning
174012004Sjiang.liu@intel.com 	 * those locks is continuously executing on other CPUs (for example,
174112004Sjiang.liu@intel.com 	 * this CPU must be preemptible to allow other CPUs to pause it during
174212004Sjiang.liu@intel.com 	 * their startup phases).  It's safe to enable preemption here because
174312004Sjiang.liu@intel.com 	 * the CPU state is pretty-much fully constructed.
17443029Ssethg 	 */
17453029Ssethg 	curthread->t_preempt = 0;
17463029Ssethg 
17471482Ssethg 	/* The base spl should still be at LOCK LEVEL here */
17481482Ssethg 	ASSERT(cp->cpu_base_spl == ipltospl(LOCK_LEVEL));
17491482Ssethg 	set_base_spl();		/* Restore the spl to its proper value */
17501482Ssethg 
175112004Sjiang.liu@intel.com 	pghw_physid_create(cp);
17528906SEric.Saxe@Sun.COM 	/*
175312004Sjiang.liu@intel.com 	 * Delegate initialization tasks, which need to access the cpu_lock,
175412004Sjiang.liu@intel.com 	 * to mp_start_cpu_common() because we can't acquire the cpu_lock here
175512004Sjiang.liu@intel.com 	 * during CPU DR operations.
17568906SEric.Saxe@Sun.COM 	 */
175712004Sjiang.liu@intel.com 	mp_startup_signal(&procset_slave, cp->cpu_id);
175812004Sjiang.liu@intel.com 	mp_startup_wait(&procset_master, cp->cpu_id);
17598906SEric.Saxe@Sun.COM 	pg_cmt_cpu_startup(cp);
176012004Sjiang.liu@intel.com 
176112004Sjiang.liu@intel.com 	if (boot) {
176212004Sjiang.liu@intel.com 		mutex_enter(&cpu_lock);
176312004Sjiang.liu@intel.com 		cp->cpu_flags &= ~CPU_OFFLINE;
176412004Sjiang.liu@intel.com 		cpu_enable_intr(cp);
176512004Sjiang.liu@intel.com 		cpu_add_active(cp);
176612004Sjiang.liu@intel.com 		mutex_exit(&cpu_lock);
176712004Sjiang.liu@intel.com 	}
17688906SEric.Saxe@Sun.COM 
17696749Ssherrym 	/* Enable interrupts */
17706749Ssherrym 	(void) spl0();
17718906SEric.Saxe@Sun.COM 
177212004Sjiang.liu@intel.com 	/*
177312004Sjiang.liu@intel.com 	 * Fill out cpu_ucode_info.  Update microcode if necessary.
177412004Sjiang.liu@intel.com 	 */
177512004Sjiang.liu@intel.com 	ucode_check(cp);
17766749Ssherrym 
17775254Sgavinm #ifndef __xpv
17785254Sgavinm 	{
17795254Sgavinm 		/*
17805254Sgavinm 		 * Set up the CPU module for this CPU.  This can't be done
17815254Sgavinm 		 * before this CPU is made CPU_READY, because we may (in
17825254Sgavinm 		 * heterogeneous systems) need to go load another CPU module.
17835254Sgavinm 		 * The act of attempting to load a module may trigger a
17845254Sgavinm 		 * cross-call, which will ASSERT unless this cpu is CPU_READY.
17855254Sgavinm 		 */
17865254Sgavinm 		cmi_hdl_t hdl;
17871414Scindi 
17885254Sgavinm 		if ((hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
17897532SSean.Ye@Sun.COM 		    cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL) {
179012826Skuriakose.kuruvilla@oracle.com 			if (is_x86_feature(x86_featureset, X86FSET_MCA))
17915254Sgavinm 				cmi_mca_init(hdl);
179212004Sjiang.liu@intel.com 			cp->cpu_m.mcpu_cmi_hdl = hdl;
17935254Sgavinm 		}
17945254Sgavinm 	}
17955254Sgavinm #endif /* __xpv */
17961414Scindi 
17970Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
17983446Smrj 		kdi_cpu_init();
17990Sstevel@tonic-gate 
18000Sstevel@tonic-gate 	/*
18010Sstevel@tonic-gate 	 * Setting the bit in cpu_ready_set must be the last operation in
18020Sstevel@tonic-gate 	 * processor initialization; the boot CPU will continue to boot once
18030Sstevel@tonic-gate 	 * it sees this bit set for all active CPUs.
18040Sstevel@tonic-gate 	 */
18050Sstevel@tonic-gate 	CPUSET_ATOMIC_ADD(cpu_ready_set, cp->cpu_id);
18060Sstevel@tonic-gate 
180712004Sjiang.liu@intel.com 	(void) mach_cpu_create_device_node(cp, NULL);
180812004Sjiang.liu@intel.com 
180912004Sjiang.liu@intel.com 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_idstr);
181012004Sjiang.liu@intel.com 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_brandstr);
181112004Sjiang.liu@intel.com 	cmn_err(CE_CONT, "?cpu%d initialization complete - online\n",
18120Sstevel@tonic-gate 	    cp->cpu_id);
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate 	/*
18150Sstevel@tonic-gate 	 * Now we are done with the startup thread, so free it up.
18160Sstevel@tonic-gate 	 */
18170Sstevel@tonic-gate 	thread_exit();
18180Sstevel@tonic-gate 	panic("mp_startup: cannot return");
18190Sstevel@tonic-gate 	/*NOTREACHED*/
18200Sstevel@tonic-gate }
18210Sstevel@tonic-gate 
182212004Sjiang.liu@intel.com /*
182312004Sjiang.liu@intel.com  * Startup function for 'other' CPUs at boot time (besides boot cpu).
182412004Sjiang.liu@intel.com  */
182512004Sjiang.liu@intel.com static void
mp_startup_boot(void)182612004Sjiang.liu@intel.com mp_startup_boot(void)
182712004Sjiang.liu@intel.com {
182812004Sjiang.liu@intel.com 	mp_startup_common(B_TRUE);
182912004Sjiang.liu@intel.com }
183012004Sjiang.liu@intel.com 
183112004Sjiang.liu@intel.com /*
183212004Sjiang.liu@intel.com  * Startup function for hotplug CPUs at runtime.
183312004Sjiang.liu@intel.com  */
183412004Sjiang.liu@intel.com void
mp_startup_hotplug(void)183512004Sjiang.liu@intel.com mp_startup_hotplug(void)
183612004Sjiang.liu@intel.com {
183712004Sjiang.liu@intel.com 	mp_startup_common(B_FALSE);
183812004Sjiang.liu@intel.com }
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate /*
18410Sstevel@tonic-gate  * Start CPU on user request.
18420Sstevel@tonic-gate  */
18430Sstevel@tonic-gate /* ARGSUSED */
18440Sstevel@tonic-gate int
mp_cpu_start(struct cpu * cp)18450Sstevel@tonic-gate mp_cpu_start(struct cpu *cp)
18460Sstevel@tonic-gate {
18470Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
18480Sstevel@tonic-gate 	return (0);
18490Sstevel@tonic-gate }
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate /*
18520Sstevel@tonic-gate  * Stop CPU on user request.
18530Sstevel@tonic-gate  */
18540Sstevel@tonic-gate int
mp_cpu_stop(struct cpu * cp)18550Sstevel@tonic-gate mp_cpu_stop(struct cpu *cp)
18560Sstevel@tonic-gate {
18571389Sdmick 	extern int cbe_psm_timer_mode;
18580Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
18591389Sdmick 
18605084Sjohnlev #ifdef __xpv
18615084Sjohnlev 	/*
18625084Sjohnlev 	 * We can't offline vcpu0.
18635084Sjohnlev 	 */
18645084Sjohnlev 	if (cp->cpu_id == 0)
18655084Sjohnlev 		return (EBUSY);
18665084Sjohnlev #endif
18675084Sjohnlev 
18681389Sdmick 	/*
18691389Sdmick 	 * If TIMER_PERIODIC mode is used, CPU0 is the one running it;
18701389Sdmick 	 * can't stop it.  (This is true only for machines with no TSC.)
18711389Sdmick 	 */
18721389Sdmick 
18731389Sdmick 	if ((cbe_psm_timer_mode == TIMER_PERIODIC) && (cp->cpu_id == 0))
18745084Sjohnlev 		return (EBUSY);
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate 	return (0);
18770Sstevel@tonic-gate }
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate /*
18800Sstevel@tonic-gate  * Take the specified CPU out of participation in interrupts.
18810Sstevel@tonic-gate  */
18820Sstevel@tonic-gate int
cpu_disable_intr(struct cpu * cp)18830Sstevel@tonic-gate cpu_disable_intr(struct cpu *cp)
18840Sstevel@tonic-gate {
18850Sstevel@tonic-gate 	if (psm_disable_intr(cp->cpu_id) != DDI_SUCCESS)
18860Sstevel@tonic-gate 		return (EBUSY);
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate 	cp->cpu_flags &= ~CPU_ENABLE;
18890Sstevel@tonic-gate 	return (0);
18900Sstevel@tonic-gate }
18910Sstevel@tonic-gate 
18920Sstevel@tonic-gate /*
18930Sstevel@tonic-gate  * Allow the specified CPU to participate in interrupts.
18940Sstevel@tonic-gate  */
18950Sstevel@tonic-gate void
cpu_enable_intr(struct cpu * cp)18960Sstevel@tonic-gate cpu_enable_intr(struct cpu *cp)
18970Sstevel@tonic-gate {
18980Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
18990Sstevel@tonic-gate 	cp->cpu_flags |= CPU_ENABLE;
19000Sstevel@tonic-gate 	psm_enable_intr(cp->cpu_id);
19010Sstevel@tonic-gate }
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate void
mp_cpu_faulted_enter(struct cpu * cp)19040Sstevel@tonic-gate mp_cpu_faulted_enter(struct cpu *cp)
19051414Scindi {
190612004Sjiang.liu@intel.com #ifdef __xpv
190712004Sjiang.liu@intel.com 	_NOTE(ARGUNUSED(cp));
190812004Sjiang.liu@intel.com #else
190912004Sjiang.liu@intel.com 	cmi_hdl_t hdl = cp->cpu_m.mcpu_cmi_hdl;
19105254Sgavinm 
19115254Sgavinm 	if (hdl != NULL) {
191212004Sjiang.liu@intel.com 		cmi_hdl_hold(hdl);
191312004Sjiang.liu@intel.com 	} else {
191412004Sjiang.liu@intel.com 		hdl = cmi_hdl_lookup(CMI_HDL_NATIVE, cmi_ntv_hwchipid(cp),
191512004Sjiang.liu@intel.com 		    cmi_ntv_hwcoreid(cp), cmi_ntv_hwstrandid(cp));
191612004Sjiang.liu@intel.com 	}
191712004Sjiang.liu@intel.com 	if (hdl != NULL) {
19185254Sgavinm 		cmi_faulted_enter(hdl);
19195254Sgavinm 		cmi_hdl_rele(hdl);
19205254Sgavinm 	}
19215254Sgavinm #endif
19221414Scindi }
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate void
mp_cpu_faulted_exit(struct cpu * cp)19250Sstevel@tonic-gate mp_cpu_faulted_exit(struct cpu *cp)
19261414Scindi {
192712004Sjiang.liu@intel.com #ifdef __xpv
192812004Sjiang.liu@intel.com 	_NOTE(ARGUNUSED(cp));
192912004Sjiang.liu@intel.com #else
193012004Sjiang.liu@intel.com 	cmi_hdl_t hdl = cp->cpu_m.mcpu_cmi_hdl;
19315254Sgavinm 
19325254Sgavinm 	if (hdl != NULL) {
193312004Sjiang.liu@intel.com 		cmi_hdl_hold(hdl);
193412004Sjiang.liu@intel.com 	} else {
193512004Sjiang.liu@intel.com 		hdl = cmi_hdl_lookup(CMI_HDL_NATIVE, cmi_ntv_hwchipid(cp),
193612004Sjiang.liu@intel.com 		    cmi_ntv_hwcoreid(cp), cmi_ntv_hwstrandid(cp));
193712004Sjiang.liu@intel.com 	}
193812004Sjiang.liu@intel.com 	if (hdl != NULL) {
19395254Sgavinm 		cmi_faulted_exit(hdl);
19405254Sgavinm 		cmi_hdl_rele(hdl);
19415254Sgavinm 	}
19425254Sgavinm #endif
19431414Scindi }
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate /*
19460Sstevel@tonic-gate  * The following two routines are used as context operators on threads belonging
19470Sstevel@tonic-gate  * to processes with a private LDT (see sysi86).  Due to the rarity of such
19480Sstevel@tonic-gate  * processes, these routines are currently written for best code readability and
194912826Skuriakose.kuruvilla@oracle.com  * organization rather than speed.  We could avoid checking x86_featureset at
195012826Skuriakose.kuruvilla@oracle.com  * every context switch by installing different context ops, depending on
195112826Skuriakose.kuruvilla@oracle.com  * x86_featureset, at LDT creation time -- one for each combination of fast
195212826Skuriakose.kuruvilla@oracle.com  * syscall features.
19530Sstevel@tonic-gate  */
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate /*ARGSUSED*/
19560Sstevel@tonic-gate void
cpu_fast_syscall_disable(void * arg)19570Sstevel@tonic-gate cpu_fast_syscall_disable(void *arg)
19580Sstevel@tonic-gate {
195912826Skuriakose.kuruvilla@oracle.com 	if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
196012826Skuriakose.kuruvilla@oracle.com 	    is_x86_feature(x86_featureset, X86FSET_SEP))
19610Sstevel@tonic-gate 		cpu_sep_disable();
196212826Skuriakose.kuruvilla@oracle.com 	if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
196312826Skuriakose.kuruvilla@oracle.com 	    is_x86_feature(x86_featureset, X86FSET_ASYSC))
19640Sstevel@tonic-gate 		cpu_asysc_disable();
19650Sstevel@tonic-gate }
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate /*ARGSUSED*/
19680Sstevel@tonic-gate void
cpu_fast_syscall_enable(void * arg)19690Sstevel@tonic-gate cpu_fast_syscall_enable(void *arg)
19700Sstevel@tonic-gate {
197112826Skuriakose.kuruvilla@oracle.com 	if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
197212826Skuriakose.kuruvilla@oracle.com 	    is_x86_feature(x86_featureset, X86FSET_SEP))
19730Sstevel@tonic-gate 		cpu_sep_enable();
197412826Skuriakose.kuruvilla@oracle.com 	if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
197512826Skuriakose.kuruvilla@oracle.com 	    is_x86_feature(x86_featureset, X86FSET_ASYSC))
19760Sstevel@tonic-gate 		cpu_asysc_enable();
19770Sstevel@tonic-gate }
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate static void
cpu_sep_enable(void)19800Sstevel@tonic-gate cpu_sep_enable(void)
19810Sstevel@tonic-gate {
198212826Skuriakose.kuruvilla@oracle.com 	ASSERT(is_x86_feature(x86_featureset, X86FSET_SEP));
19830Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
19840Sstevel@tonic-gate 
1985770Skucharsk 	wrmsr(MSR_INTC_SEP_CS, (uint64_t)(uintptr_t)KCS_SEL);
19860Sstevel@tonic-gate }
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate static void
cpu_sep_disable(void)19890Sstevel@tonic-gate cpu_sep_disable(void)
19900Sstevel@tonic-gate {
199112826Skuriakose.kuruvilla@oracle.com 	ASSERT(is_x86_feature(x86_featureset, X86FSET_SEP));
19920Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 	/*
19950Sstevel@tonic-gate 	 * Setting the SYSENTER_CS_MSR register to 0 causes software executing
19960Sstevel@tonic-gate 	 * the sysenter or sysexit instruction to trigger a #gp fault.
19970Sstevel@tonic-gate 	 */
19983446Smrj 	wrmsr(MSR_INTC_SEP_CS, 0);
19990Sstevel@tonic-gate }
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate static void
cpu_asysc_enable(void)20020Sstevel@tonic-gate cpu_asysc_enable(void)
20030Sstevel@tonic-gate {
200412826Skuriakose.kuruvilla@oracle.com 	ASSERT(is_x86_feature(x86_featureset, X86FSET_ASYSC));
20050Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
20060Sstevel@tonic-gate 
2007770Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) |
2008770Skucharsk 	    (uint64_t)(uintptr_t)AMD_EFER_SCE);
20090Sstevel@tonic-gate }
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate static void
cpu_asysc_disable(void)20120Sstevel@tonic-gate cpu_asysc_disable(void)
20130Sstevel@tonic-gate {
201412826Skuriakose.kuruvilla@oracle.com 	ASSERT(is_x86_feature(x86_featureset, X86FSET_ASYSC));
20150Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 	/*
20180Sstevel@tonic-gate 	 * Turn off the SCE (syscall enable) bit in the EFER register. Software
20190Sstevel@tonic-gate 	 * executing syscall or sysret with this bit off will incur a #ud trap.
20200Sstevel@tonic-gate 	 */
2021770Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) &
2022770Skucharsk 	    ~((uint64_t)(uintptr_t)AMD_EFER_SCE));
20230Sstevel@tonic-gate }
2024