xref: /titanic_41/usr/src/uts/i86pc/os/mp_startup.c (revision cca8a59fd88a0d1510902558fa587a097a911a1d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5100b72f4Sandrei  * Common Development and Distribution License (the "License").
6100b72f4Sandrei  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21ae115bc7Smrj 
227c478bd9Sstevel@tonic-gate /*
237417cfdeSKuriakose Kuruvilla  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
25a3114836SGerry Liu /*
26a3114836SGerry Liu  * Copyright (c) 2010, Intel Corporation.
27a3114836SGerry Liu  * All rights reserved.
28a3114836SGerry Liu  */
29ebb8ac07SRobert Mustacchi /*
30dd7afb26SPatrick Mooney  * Copyright 2016 Joyent, Inc.
31850ad55aSHans Rosenfeld  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
32ebb8ac07SRobert Mustacchi  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/thread.h>
367c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
37a3114836SGerry Liu #include <sys/cpu.h>
387c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
397c478bd9Sstevel@tonic-gate #include <sys/param.h>
407c478bd9Sstevel@tonic-gate #include <sys/proc.h>
417c478bd9Sstevel@tonic-gate #include <sys/disp.h>
427c478bd9Sstevel@tonic-gate #include <sys/class.h>
437c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
447c478bd9Sstevel@tonic-gate #include <sys/debug.h>
45a3114836SGerry Liu #include <sys/note.h>
467c478bd9Sstevel@tonic-gate #include <sys/asm_linkage.h>
477c478bd9Sstevel@tonic-gate #include <sys/x_call.h>
487c478bd9Sstevel@tonic-gate #include <sys/systm.h>
497c478bd9Sstevel@tonic-gate #include <sys/var.h>
507c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
517c478bd9Sstevel@tonic-gate #include <vm/hat.h>
527c478bd9Sstevel@tonic-gate #include <vm/as.h>
537c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
54ae115bc7Smrj #include <vm/seg_kp.h>
557c478bd9Sstevel@tonic-gate #include <sys/segments.h>
567c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
577c478bd9Sstevel@tonic-gate #include <sys/stack.h>
587c478bd9Sstevel@tonic-gate #include <sys/smp_impldefs.h>
597c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
607c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
617c478bd9Sstevel@tonic-gate #include <sys/traptrace.h>
627c478bd9Sstevel@tonic-gate #include <sys/clock.h>
637c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
64fb2f18f8Sesaxe #include <sys/pg.h>
65fb2f18f8Sesaxe #include <sys/cmt.h>
667c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
677c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
687c478bd9Sstevel@tonic-gate #include <sys/fp.h>
697c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
70ae115bc7Smrj #include <sys/kdi_machimpl.h>
717c478bd9Sstevel@tonic-gate #include <vm/hat_i86.h>
72a3114836SGerry Liu #include <vm/vm_dep.h>
737c478bd9Sstevel@tonic-gate #include <sys/memnode.h>
74ef50d8c0Sesaxe #include <sys/pci_cfgspace.h>
75ae115bc7Smrj #include <sys/mach_mmu.h>
76ae115bc7Smrj #include <sys/sysmacros.h>
77843e1988Sjohnlev #if defined(__xpv)
78843e1988Sjohnlev #include <sys/hypervisor.h>
79843e1988Sjohnlev #endif
807aec1d6eScindi #include <sys/cpu_module.h>
81850ad55aSHans Rosenfeld #include <sys/ontrap.h>
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate struct cpu	cpus[1];			/* CPU data */
847c478bd9Sstevel@tonic-gate struct cpu	*cpu[NCPU] = {&cpus[0]};	/* pointers to all CPUs */
85a3114836SGerry Liu struct cpu	*cpu_free_list;			/* list for released CPUs */
867c478bd9Sstevel@tonic-gate cpu_core_t	cpu_core[NCPU];			/* cpu_core structures */
877c478bd9Sstevel@tonic-gate 
88a3114836SGerry Liu #define	cpu_next_free	cpu_prev
89a3114836SGerry Liu 
907c478bd9Sstevel@tonic-gate /*
91ae115bc7Smrj  * Useful for disabling MP bring-up on a MP capable system.
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate int use_mp = 1;
947c478bd9Sstevel@tonic-gate 
9541791439Sandrei /*
96ae115bc7Smrj  * to be set by a PSM to indicate what cpus
97ae115bc7Smrj  * are sitting around on the system.
9841791439Sandrei  */
99ae115bc7Smrj cpuset_t mp_cpus;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * This variable is used by the hat layer to decide whether or not
1037c478bd9Sstevel@tonic-gate  * critical sections are needed to prevent race conditions.  For sun4m,
1047c478bd9Sstevel@tonic-gate  * this variable is set once enough MP initialization has been done in
1057c478bd9Sstevel@tonic-gate  * order to allow cross calls.
1067c478bd9Sstevel@tonic-gate  */
107ae115bc7Smrj int flushes_require_xcalls;
108a563a037Sbholler 
109a563a037Sbholler cpuset_t cpu_ready_set;		/* initialized in startup() */
1107c478bd9Sstevel@tonic-gate 
111a3114836SGerry Liu static void mp_startup_boot(void);
112a3114836SGerry Liu static void mp_startup_hotplug(void);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static void cpu_sep_enable(void);
1157c478bd9Sstevel@tonic-gate static void cpu_sep_disable(void);
1167c478bd9Sstevel@tonic-gate static void cpu_asysc_enable(void);
1177c478bd9Sstevel@tonic-gate static void cpu_asysc_disable(void);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * Init CPU info - get CPU type info for processor_info system call.
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate void
init_cpu_info(struct cpu * cp)1237c478bd9Sstevel@tonic-gate init_cpu_info(struct cpu *cp)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	processor_info_t *pi = &cp->cpu_type_info;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	/*
1287c478bd9Sstevel@tonic-gate 	 * Get clock-frequency property for the CPU.
1297c478bd9Sstevel@tonic-gate 	 */
1307c478bd9Sstevel@tonic-gate 	pi->pi_clock = cpu_freq;
1317c478bd9Sstevel@tonic-gate 
1325cff7825Smh27603 	/*
1335cff7825Smh27603 	 * Current frequency in Hz.
1345cff7825Smh27603 	 */
135cf74e62bSmh27603 	cp->cpu_curr_clock = cpu_freq_hz;
1365cff7825Smh27603 
13737d22dc0SAnup Pemmaiah 	/*
13837d22dc0SAnup Pemmaiah 	 * Supported frequencies.
13937d22dc0SAnup Pemmaiah 	 */
14037d22dc0SAnup Pemmaiah 	if (cp->cpu_supp_freqs == NULL) {
14137d22dc0SAnup Pemmaiah 		cpu_set_supp_freqs(cp, NULL);
14237d22dc0SAnup Pemmaiah 	}
14337d22dc0SAnup Pemmaiah 
1447c478bd9Sstevel@tonic-gate 	(void) strcpy(pi->pi_processor_type, "i386");
1457c478bd9Sstevel@tonic-gate 	if (fpu_exists)
1467c478bd9Sstevel@tonic-gate 		(void) strcpy(pi->pi_fputypes, "i387 compatible");
1477c478bd9Sstevel@tonic-gate 
148a3114836SGerry Liu 	cp->cpu_idstr = kmem_zalloc(CPU_IDSTRLEN, KM_SLEEP);
149a3114836SGerry Liu 	cp->cpu_brandstr = kmem_zalloc(CPU_IDSTRLEN, KM_SLEEP);
1507c478bd9Sstevel@tonic-gate 
151a3114836SGerry Liu 	/*
152a3114836SGerry Liu 	 * If called for the BSP, cp is equal to current CPU.
153a3114836SGerry Liu 	 * For non-BSPs, cpuid info of cp is not ready yet, so use cpuid info
154a3114836SGerry Liu 	 * of current CPU as default values for cpu_idstr and cpu_brandstr.
155a3114836SGerry Liu 	 * They will be corrected in mp_startup_common() after cpuid_pass1()
156a3114836SGerry Liu 	 * has been invoked on target CPU.
157a3114836SGerry Liu 	 */
158a3114836SGerry Liu 	(void) cpuid_getidstr(CPU, cp->cpu_idstr, CPU_IDSTRLEN);
159a3114836SGerry Liu 	(void) cpuid_getbrandstr(CPU, cp->cpu_brandstr, CPU_IDSTRLEN);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * Configure syscall support on this CPU.
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1662df1fe9cSrandyf void
init_cpu_syscall(struct cpu * cp)1677c478bd9Sstevel@tonic-gate init_cpu_syscall(struct cpu *cp)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	kpreempt_disable();
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate #if defined(__amd64)
1727417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
1737417cfdeSKuriakose Kuruvilla 	    is_x86_feature(x86_featureset, X86FSET_ASYSC)) {
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate #if !defined(__lint)
1767c478bd9Sstevel@tonic-gate 		/*
1777c478bd9Sstevel@tonic-gate 		 * The syscall instruction imposes a certain ordering on
1787c478bd9Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
1797c478bd9Sstevel@tonic-gate 		 * here.
1807c478bd9Sstevel@tonic-gate 		 */
1817c478bd9Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
1827c478bd9Sstevel@tonic-gate 		ASSERT(UDS_SEL == U32CS_SEL + 8);
1837c478bd9Sstevel@tonic-gate 		ASSERT(UCS_SEL == U32CS_SEL + 16);
1847c478bd9Sstevel@tonic-gate #endif
1857c478bd9Sstevel@tonic-gate 		/*
1867c478bd9Sstevel@tonic-gate 		 * Turn syscall/sysret extensions on.
1877c478bd9Sstevel@tonic-gate 		 */
1887c478bd9Sstevel@tonic-gate 		cpu_asysc_enable();
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 		/*
1917c478bd9Sstevel@tonic-gate 		 * Program the magic registers ..
1927c478bd9Sstevel@tonic-gate 		 */
193ae115bc7Smrj 		wrmsr(MSR_AMD_STAR,
194ae115bc7Smrj 		    ((uint64_t)(U32CS_SEL << 16 | KCS_SEL)) << 32);
1950ac7d7d8Skucharsk 		wrmsr(MSR_AMD_LSTAR, (uint64_t)(uintptr_t)sys_syscall);
1960ac7d7d8Skucharsk 		wrmsr(MSR_AMD_CSTAR, (uint64_t)(uintptr_t)sys_syscall32);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 		/*
1997c478bd9Sstevel@tonic-gate 		 * This list of flags is masked off the incoming
2007c478bd9Sstevel@tonic-gate 		 * %rfl when we enter the kernel.
2017c478bd9Sstevel@tonic-gate 		 */
2020ac7d7d8Skucharsk 		wrmsr(MSR_AMD_SFMASK, (uint64_t)(uintptr_t)(PS_IE | PS_T));
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate #endif
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	/*
2077c478bd9Sstevel@tonic-gate 	 * On 32-bit kernels, we use sysenter/sysexit because it's too
2087c478bd9Sstevel@tonic-gate 	 * hard to use syscall/sysret, and it is more portable anyway.
2097c478bd9Sstevel@tonic-gate 	 *
2107c478bd9Sstevel@tonic-gate 	 * On 64-bit kernels on Nocona machines, the 32-bit syscall
2117c478bd9Sstevel@tonic-gate 	 * variant isn't available to 32-bit applications, but sysenter is.
2127c478bd9Sstevel@tonic-gate 	 */
2137417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
2147417cfdeSKuriakose Kuruvilla 	    is_x86_feature(x86_featureset, X86FSET_SEP)) {
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate #if !defined(__lint)
2177c478bd9Sstevel@tonic-gate 		/*
2187c478bd9Sstevel@tonic-gate 		 * The sysenter instruction imposes a certain ordering on
2197c478bd9Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
2207c478bd9Sstevel@tonic-gate 		 * here. See "sysenter" in Intel document 245471-012, "IA-32
2217c478bd9Sstevel@tonic-gate 		 * Intel Architecture Software Developer's Manual Volume 2:
2227c478bd9Sstevel@tonic-gate 		 * Instruction Set Reference"
2237c478bd9Sstevel@tonic-gate 		 */
2247c478bd9Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		ASSERT32(UCS_SEL == ((KCS_SEL + 16) | 3));
2277c478bd9Sstevel@tonic-gate 		ASSERT32(UDS_SEL == UCS_SEL + 8);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 		ASSERT64(U32CS_SEL == ((KCS_SEL + 16) | 3));
2307c478bd9Sstevel@tonic-gate 		ASSERT64(UDS_SEL == U32CS_SEL + 8);
2317c478bd9Sstevel@tonic-gate #endif
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		cpu_sep_enable();
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 		/*
2367c478bd9Sstevel@tonic-gate 		 * resume() sets this value to the base of the threads stack
2377c478bd9Sstevel@tonic-gate 		 * via a context handler.
2387c478bd9Sstevel@tonic-gate 		 */
239ae115bc7Smrj 		wrmsr(MSR_INTC_SEP_ESP, 0);
2400ac7d7d8Skucharsk 		wrmsr(MSR_INTC_SEP_EIP, (uint64_t)(uintptr_t)sys_sysenter);
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	kpreempt_enable();
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
246dd7afb26SPatrick Mooney #if !defined(__xpv)
247dd7afb26SPatrick Mooney /*
248dd7afb26SPatrick Mooney  * Configure per-cpu ID GDT
249dd7afb26SPatrick Mooney  */
250dd7afb26SPatrick Mooney static void
init_cpu_id_gdt(struct cpu * cp)251dd7afb26SPatrick Mooney init_cpu_id_gdt(struct cpu *cp)
252dd7afb26SPatrick Mooney {
253dd7afb26SPatrick Mooney 	/* Write cpu_id into limit field of GDT for usermode retrieval */
254dd7afb26SPatrick Mooney #if defined(__amd64)
255dd7afb26SPatrick Mooney 	set_usegd(&cp->cpu_gdt[GDT_CPUID], SDP_SHORT, NULL, cp->cpu_id,
256dd7afb26SPatrick Mooney 	    SDT_MEMRODA, SEL_UPL, SDP_BYTES, SDP_OP32);
257dd7afb26SPatrick Mooney #elif defined(__i386)
258dd7afb26SPatrick Mooney 	set_usegd(&cp->cpu_gdt[GDT_CPUID], NULL, cp->cpu_id, SDT_MEMRODA,
259dd7afb26SPatrick Mooney 	    SEL_UPL, SDP_BYTES, SDP_OP32);
260dd7afb26SPatrick Mooney #endif
261dd7afb26SPatrick Mooney }
262dd7afb26SPatrick Mooney #endif /* !defined(__xpv) */
263dd7afb26SPatrick Mooney 
2647c478bd9Sstevel@tonic-gate /*
2657c478bd9Sstevel@tonic-gate  * Multiprocessor initialization.
2667c478bd9Sstevel@tonic-gate  *
2677c478bd9Sstevel@tonic-gate  * Allocate and initialize the cpu structure, TRAPTRACE buffer, and the
2687c478bd9Sstevel@tonic-gate  * startup and idle threads for the specified CPU.
269a3114836SGerry Liu  * Parameter boot is true for boot time operations and is false for CPU
270a3114836SGerry Liu  * DR operations.
2717c478bd9Sstevel@tonic-gate  */
272a3114836SGerry Liu static struct cpu *
mp_cpu_configure_common(int cpun,boolean_t boot)273a3114836SGerry Liu mp_cpu_configure_common(int cpun, boolean_t boot)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	struct cpu *cp;
2767c478bd9Sstevel@tonic-gate 	kthread_id_t tp;
2777c478bd9Sstevel@tonic-gate 	caddr_t	sp;
2787c478bd9Sstevel@tonic-gate 	proc_t *procp;
279843e1988Sjohnlev #if !defined(__xpv)
2805b8a6efeSbholler 	extern int idle_cpu_prefer_mwait;
2810e751525SEric Saxe 	extern void cpu_idle_mwait();
282843e1988Sjohnlev #endif
2837c478bd9Sstevel@tonic-gate 	extern void idle();
2840e751525SEric Saxe 	extern void cpu_idle();
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate #ifdef TRAPTRACE
2877c478bd9Sstevel@tonic-gate 	trap_trace_ctl_t *ttc = &trap_trace_ctl[cpun];
2887c478bd9Sstevel@tonic-gate #endif
2897c478bd9Sstevel@tonic-gate 
290a3114836SGerry Liu 	ASSERT(MUTEX_HELD(&cpu_lock));
2917c478bd9Sstevel@tonic-gate 	ASSERT(cpun < NCPU && cpu[cpun] == NULL);
2927c478bd9Sstevel@tonic-gate 
293a3114836SGerry Liu 	if (cpu_free_list == NULL) {
294ae115bc7Smrj 		cp = kmem_zalloc(sizeof (*cp), KM_SLEEP);
295a3114836SGerry Liu 	} else {
296a3114836SGerry Liu 		cp = cpu_free_list;
297a3114836SGerry Liu 		cpu_free_list = cp->cpu_next_free;
298a3114836SGerry Liu 	}
299f98fbcecSbholler 
3003006ae82SFrank Van Der Linden 	cp->cpu_m.mcpu_istamp = cpun << 16;
3013006ae82SFrank Van Der Linden 
302a3114836SGerry Liu 	/* Create per CPU specific threads in the process p0. */
303a3114836SGerry Liu 	procp = &p0;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	/*
3067c478bd9Sstevel@tonic-gate 	 * Initialize the dispatcher first.
3077c478bd9Sstevel@tonic-gate 	 */
3087c478bd9Sstevel@tonic-gate 	disp_cpu_init(cp);
3097c478bd9Sstevel@tonic-gate 
310affbd3ccSkchow 	cpu_vm_data_init(cp);
311affbd3ccSkchow 
3127c478bd9Sstevel@tonic-gate 	/*
3137c478bd9Sstevel@tonic-gate 	 * Allocate and initialize the startup thread for this CPU.
3147c478bd9Sstevel@tonic-gate 	 * Interrupt and process switch stacks get allocated later
3157c478bd9Sstevel@tonic-gate 	 * when the CPU starts running.
3167c478bd9Sstevel@tonic-gate 	 */
3177c478bd9Sstevel@tonic-gate 	tp = thread_create(NULL, 0, NULL, NULL, 0, procp,
3187c478bd9Sstevel@tonic-gate 	    TS_STOPPED, maxclsyspri);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	/*
3217c478bd9Sstevel@tonic-gate 	 * Set state to TS_ONPROC since this thread will start running
3227c478bd9Sstevel@tonic-gate 	 * as soon as the CPU comes online.
3237c478bd9Sstevel@tonic-gate 	 *
3247c478bd9Sstevel@tonic-gate 	 * All the other fields of the thread structure are setup by
3257c478bd9Sstevel@tonic-gate 	 * thread_create().
3267c478bd9Sstevel@tonic-gate 	 */
3277c478bd9Sstevel@tonic-gate 	THREAD_ONPROC(tp, cp);
3287c478bd9Sstevel@tonic-gate 	tp->t_preempt = 1;
3297c478bd9Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
3307c478bd9Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
3317c478bd9Sstevel@tonic-gate 	tp->t_cpu = cp;
3327c478bd9Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/*
335a3114836SGerry Liu 	 * Setup thread to start in mp_startup_common.
3367c478bd9Sstevel@tonic-gate 	 */
3377c478bd9Sstevel@tonic-gate 	sp = tp->t_stk;
3387c478bd9Sstevel@tonic-gate 	tp->t_sp = (uintptr_t)(sp - MINFRAME);
339ae115bc7Smrj #if defined(__amd64)
340ae115bc7Smrj 	tp->t_sp -= STACK_ENTRY_ALIGN;		/* fake a call */
341ae115bc7Smrj #endif
342a3114836SGerry Liu 	/*
343a3114836SGerry Liu 	 * Setup thread start entry point for boot or hotplug.
344a3114836SGerry Liu 	 */
345a3114836SGerry Liu 	if (boot) {
346a3114836SGerry Liu 		tp->t_pc = (uintptr_t)mp_startup_boot;
347a3114836SGerry Liu 	} else {
348a3114836SGerry Liu 		tp->t_pc = (uintptr_t)mp_startup_hotplug;
349a3114836SGerry Liu 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	cp->cpu_id = cpun;
3527c478bd9Sstevel@tonic-gate 	cp->cpu_self = cp;
3537c478bd9Sstevel@tonic-gate 	cp->cpu_thread = tp;
3547c478bd9Sstevel@tonic-gate 	cp->cpu_lwp = NULL;
3557c478bd9Sstevel@tonic-gate 	cp->cpu_dispthread = tp;
3567c478bd9Sstevel@tonic-gate 	cp->cpu_dispatch_pri = DISP_PRIO(tp);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/*
359da43ceabSsethg 	 * cpu_base_spl must be set explicitly here to prevent any blocking
360a3114836SGerry Liu 	 * operations in mp_startup_common from causing the spl of the cpu
361a3114836SGerry Liu 	 * to drop to 0 (allowing device interrupts before we're ready) in
362a3114836SGerry Liu 	 * resume().
363da43ceabSsethg 	 * cpu_base_spl MUST remain at LOCK_LEVEL until the cpu is CPU_READY.
364da43ceabSsethg 	 * As an extra bit of security on DEBUG kernels, this is enforced with
365a3114836SGerry Liu 	 * an assertion in mp_startup_common() -- before cpu_base_spl is set
366a3114836SGerry Liu 	 * to its proper value.
367da43ceabSsethg 	 */
368da43ceabSsethg 	cp->cpu_base_spl = ipltospl(LOCK_LEVEL);
369da43ceabSsethg 
370da43ceabSsethg 	/*
3717c478bd9Sstevel@tonic-gate 	 * Now, initialize per-CPU idle thread for this CPU.
3727c478bd9Sstevel@tonic-gate 	 */
3737c478bd9Sstevel@tonic-gate 	tp = thread_create(NULL, PAGESIZE, idle, NULL, 0, procp, TS_ONPROC, -1);
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	cp->cpu_idle_thread = tp;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	tp->t_preempt = 1;
3787c478bd9Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
3797c478bd9Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
3807c478bd9Sstevel@tonic-gate 	tp->t_cpu = cp;
3817c478bd9Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	/*
384fb2f18f8Sesaxe 	 * Bootstrap the CPU's PG data
385394b433dSesaxe 	 */
386fb2f18f8Sesaxe 	pg_cpu_bootstrap(cp);
387394b433dSesaxe 
388394b433dSesaxe 	/*
389ae115bc7Smrj 	 * Perform CPC initialization on the new CPU.
3907c478bd9Sstevel@tonic-gate 	 */
3917c478bd9Sstevel@tonic-gate 	kcpc_hw_init(cp);
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	/*
3947c478bd9Sstevel@tonic-gate 	 * Allocate virtual addresses for cpu_caddr1 and cpu_caddr2
3957c478bd9Sstevel@tonic-gate 	 * for each CPU.
3967c478bd9Sstevel@tonic-gate 	 */
3977c478bd9Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(cp);
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	/*
400ae115bc7Smrj 	 * Allocate page for new GDT and initialize from current GDT.
4017c478bd9Sstevel@tonic-gate 	 */
402ae115bc7Smrj #if !defined(__lint)
403ae115bc7Smrj 	ASSERT((sizeof (*cp->cpu_gdt) * NGDT) <= PAGESIZE);
404ae115bc7Smrj #endif
4050cfdb603Sjosephb 	cp->cpu_gdt = kmem_zalloc(PAGESIZE, KM_SLEEP);
4060cfdb603Sjosephb 	bcopy(CPU->cpu_gdt, cp->cpu_gdt, (sizeof (*cp->cpu_gdt) * NGDT));
4077c478bd9Sstevel@tonic-gate 
408ae115bc7Smrj #if defined(__i386)
4097c478bd9Sstevel@tonic-gate 	/*
4107c478bd9Sstevel@tonic-gate 	 * setup kernel %gs.
4117c478bd9Sstevel@tonic-gate 	 */
4127c478bd9Sstevel@tonic-gate 	set_usegd(&cp->cpu_gdt[GDT_GS], cp, sizeof (struct cpu) -1, SDT_MEMRWA,
4137c478bd9Sstevel@tonic-gate 	    SEL_KPL, 0, 1);
414ae115bc7Smrj #endif
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	/*
4177c478bd9Sstevel@tonic-gate 	 * If we have more than one node, each cpu gets a copy of IDT
4187c478bd9Sstevel@tonic-gate 	 * local to its node. If this is a Pentium box, we use cpu 0's
4197c478bd9Sstevel@tonic-gate 	 * IDT. cpu 0's IDT has been made read-only to workaround the
4207c478bd9Sstevel@tonic-gate 	 * cmpxchgl register bug
4217c478bd9Sstevel@tonic-gate 	 */
4227c478bd9Sstevel@tonic-gate 	if (system_hardware.hd_nodes && x86_type != X86_TYPE_P5) {
4230cfdb603Sjosephb #if !defined(__lint)
4240cfdb603Sjosephb 		ASSERT((sizeof (*CPU->cpu_idt) * NIDT) <= PAGESIZE);
4250cfdb603Sjosephb #endif
4260cfdb603Sjosephb 		cp->cpu_idt = kmem_zalloc(PAGESIZE, KM_SLEEP);
4270cfdb603Sjosephb 		bcopy(CPU->cpu_idt, cp->cpu_idt, PAGESIZE);
428ae115bc7Smrj 	} else {
4290cfdb603Sjosephb 		cp->cpu_idt = CPU->cpu_idt;
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	/*
433ae115bc7Smrj 	 * alloc space for cpuid info
434ae115bc7Smrj 	 */
435ae115bc7Smrj 	cpuid_alloc_space(cp);
436a3114836SGerry Liu #if !defined(__xpv)
4377417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_MWAIT) &&
4387417cfdeSKuriakose Kuruvilla 	    idle_cpu_prefer_mwait) {
439a3114836SGerry Liu 		cp->cpu_m.mcpu_mwait = cpuid_mwait_alloc(cp);
440a3114836SGerry Liu 		cp->cpu_m.mcpu_idle_cpu = cpu_idle_mwait;
441a3114836SGerry Liu 	} else
442a3114836SGerry Liu #endif
443a3114836SGerry Liu 		cp->cpu_m.mcpu_idle_cpu = cpu_idle;
444a3114836SGerry Liu 
445a3114836SGerry Liu 	init_cpu_info(cp);
446ae115bc7Smrj 
447dd7afb26SPatrick Mooney #if !defined(__xpv)
448dd7afb26SPatrick Mooney 	init_cpu_id_gdt(cp);
449dd7afb26SPatrick Mooney #endif
450dd7afb26SPatrick Mooney 
4512449e17fSsherrym 	/*
4522449e17fSsherrym 	 * alloc space for ucode_info
4532449e17fSsherrym 	 */
4542449e17fSsherrym 	ucode_alloc_space(cp);
455f34a7178SJoe Bonasera 	xc_init_cpu(cp);
4567c478bd9Sstevel@tonic-gate 	hat_cpu_online(cp);
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate #ifdef TRAPTRACE
4597c478bd9Sstevel@tonic-gate 	/*
460ae115bc7Smrj 	 * If this is a TRAPTRACE kernel, allocate TRAPTRACE buffers
4617c478bd9Sstevel@tonic-gate 	 */
4627c478bd9Sstevel@tonic-gate 	ttc->ttc_first = (uintptr_t)kmem_zalloc(trap_trace_bufsize, KM_SLEEP);
4637c478bd9Sstevel@tonic-gate 	ttc->ttc_next = ttc->ttc_first;
4647c478bd9Sstevel@tonic-gate 	ttc->ttc_limit = ttc->ttc_first + trap_trace_bufsize;
4657c478bd9Sstevel@tonic-gate #endif
466a3114836SGerry Liu 
4677c478bd9Sstevel@tonic-gate 	/*
4687c478bd9Sstevel@tonic-gate 	 * Record that we have another CPU.
4697c478bd9Sstevel@tonic-gate 	 */
4707c478bd9Sstevel@tonic-gate 	/*
4717c478bd9Sstevel@tonic-gate 	 * Initialize the interrupt threads for this CPU
4727c478bd9Sstevel@tonic-gate 	 */
473100b72f4Sandrei 	cpu_intr_alloc(cp, NINTR_THREADS);
474a3114836SGerry Liu 
475a3114836SGerry Liu 	cp->cpu_flags = CPU_OFFLINE | CPU_QUIESCED | CPU_POWEROFF;
476a3114836SGerry Liu 	cpu_set_state(cp);
477a3114836SGerry Liu 
4787c478bd9Sstevel@tonic-gate 	/*
4797c478bd9Sstevel@tonic-gate 	 * Add CPU to list of available CPUs.  It'll be on the active list
480a3114836SGerry Liu 	 * after mp_startup_common().
4817c478bd9Sstevel@tonic-gate 	 */
4827c478bd9Sstevel@tonic-gate 	cpu_add_unit(cp);
483ae115bc7Smrj 
484ae115bc7Smrj 	return (cp);
485ae115bc7Smrj }
486ae115bc7Smrj 
487ae115bc7Smrj /*
488a3114836SGerry Liu  * Undo what was done in mp_cpu_configure_common
489ae115bc7Smrj  */
490ae115bc7Smrj static void
mp_cpu_unconfigure_common(struct cpu * cp,int error)491a3114836SGerry Liu mp_cpu_unconfigure_common(struct cpu *cp, int error)
492ae115bc7Smrj {
493a3114836SGerry Liu 	ASSERT(MUTEX_HELD(&cpu_lock));
494ae115bc7Smrj 
495ae115bc7Smrj 	/*
496ae115bc7Smrj 	 * Remove the CPU from the list of available CPUs.
497ae115bc7Smrj 	 */
498ae115bc7Smrj 	cpu_del_unit(cp->cpu_id);
499ae115bc7Smrj 
500ae115bc7Smrj 	if (error == ETIMEDOUT) {
501ae115bc7Smrj 		/*
502ae115bc7Smrj 		 * The cpu was started, but never *seemed* to run any
503ae115bc7Smrj 		 * code in the kernel; it's probably off spinning in its
504ae115bc7Smrj 		 * own private world, though with potential references to
505ae115bc7Smrj 		 * our kmem-allocated IDTs and GDTs (for example).
506ae115bc7Smrj 		 *
507ae115bc7Smrj 		 * Worse still, it may actually wake up some time later,
508ae115bc7Smrj 		 * so rather than guess what it might or might not do, we
509ae115bc7Smrj 		 * leave the fundamental data structures intact.
510ae115bc7Smrj 		 */
511ae115bc7Smrj 		cp->cpu_flags = 0;
512ae115bc7Smrj 		return;
513ae115bc7Smrj 	}
514ae115bc7Smrj 
515ae115bc7Smrj 	/*
516ae115bc7Smrj 	 * At this point, the only threads bound to this CPU should
517ae115bc7Smrj 	 * special per-cpu threads: it's idle thread, it's pause threads,
518ae115bc7Smrj 	 * and it's interrupt threads.  Clean these up.
519ae115bc7Smrj 	 */
520ae115bc7Smrj 	cpu_destroy_bound_threads(cp);
521ae115bc7Smrj 	cp->cpu_idle_thread = NULL;
522ae115bc7Smrj 
523ae115bc7Smrj 	/*
524ae115bc7Smrj 	 * Free the interrupt stack.
525ae115bc7Smrj 	 */
526ae115bc7Smrj 	segkp_release(segkp,
527ae115bc7Smrj 	    cp->cpu_intr_stack - (INTR_STACK_SIZE - SA(MINFRAME)));
528a3114836SGerry Liu 	cp->cpu_intr_stack = NULL;
529ae115bc7Smrj 
530ae115bc7Smrj #ifdef TRAPTRACE
531ae115bc7Smrj 	/*
532ae115bc7Smrj 	 * Discard the trap trace buffer
533ae115bc7Smrj 	 */
534ae115bc7Smrj 	{
535ae115bc7Smrj 		trap_trace_ctl_t *ttc = &trap_trace_ctl[cp->cpu_id];
536ae115bc7Smrj 
537ae115bc7Smrj 		kmem_free((void *)ttc->ttc_first, trap_trace_bufsize);
538ae115bc7Smrj 		ttc->ttc_first = NULL;
539ae115bc7Smrj 	}
540ae115bc7Smrj #endif
541ae115bc7Smrj 
542ae115bc7Smrj 	hat_cpu_offline(cp);
543ae115bc7Smrj 
5442449e17fSsherrym 	ucode_free_space(cp);
5452449e17fSsherrym 
546a3114836SGerry Liu 	/* Free CPU ID string and brand string. */
547a3114836SGerry Liu 	if (cp->cpu_idstr) {
548a3114836SGerry Liu 		kmem_free(cp->cpu_idstr, CPU_IDSTRLEN);
549a3114836SGerry Liu 		cp->cpu_idstr = NULL;
550a3114836SGerry Liu 	}
551a3114836SGerry Liu 	if (cp->cpu_brandstr) {
552a3114836SGerry Liu 		kmem_free(cp->cpu_brandstr, CPU_IDSTRLEN);
553a3114836SGerry Liu 		cp->cpu_brandstr = NULL;
554a3114836SGerry Liu 	}
555a3114836SGerry Liu 
556a3114836SGerry Liu #if !defined(__xpv)
557a3114836SGerry Liu 	if (cp->cpu_m.mcpu_mwait != NULL) {
558a3114836SGerry Liu 		cpuid_mwait_free(cp);
559a3114836SGerry Liu 		cp->cpu_m.mcpu_mwait = NULL;
560a3114836SGerry Liu 	}
561a3114836SGerry Liu #endif
562a3114836SGerry Liu 	cpuid_free_space(cp);
563a3114836SGerry Liu 
5640cfdb603Sjosephb 	if (cp->cpu_idt != CPU->cpu_idt)
5650cfdb603Sjosephb 		kmem_free(cp->cpu_idt, PAGESIZE);
5660cfdb603Sjosephb 	cp->cpu_idt = NULL;
567ae115bc7Smrj 
5680cfdb603Sjosephb 	kmem_free(cp->cpu_gdt, PAGESIZE);
5690cfdb603Sjosephb 	cp->cpu_gdt = NULL;
570ae115bc7Smrj 
571a3114836SGerry Liu 	if (cp->cpu_supp_freqs != NULL) {
572a3114836SGerry Liu 		size_t len = strlen(cp->cpu_supp_freqs) + 1;
573a3114836SGerry Liu 		kmem_free(cp->cpu_supp_freqs, len);
574a3114836SGerry Liu 		cp->cpu_supp_freqs = NULL;
575a3114836SGerry Liu 	}
576a3114836SGerry Liu 
577ae115bc7Smrj 	teardown_vaddr_for_ppcopy(cp);
578ae115bc7Smrj 
579ae115bc7Smrj 	kcpc_hw_fini(cp);
580ae115bc7Smrj 
581ae115bc7Smrj 	cp->cpu_dispthread = NULL;
582ae115bc7Smrj 	cp->cpu_thread = NULL;	/* discarded by cpu_destroy_bound_threads() */
583ae115bc7Smrj 
584ae115bc7Smrj 	cpu_vm_data_destroy(cp);
585ae115bc7Smrj 
586a3114836SGerry Liu 	xc_fini_cpu(cp);
587ae115bc7Smrj 	disp_cpu_fini(cp);
588ae115bc7Smrj 
589a3114836SGerry Liu 	ASSERT(cp != CPU0);
590a3114836SGerry Liu 	bzero(cp, sizeof (*cp));
591a3114836SGerry Liu 	cp->cpu_next_free = cpu_free_list;
592a3114836SGerry Liu 	cpu_free_list = cp;
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate /*
5967c478bd9Sstevel@tonic-gate  * Apply workarounds for known errata, and warn about those that are absent.
5977c478bd9Sstevel@tonic-gate  *
5987c478bd9Sstevel@tonic-gate  * System vendors occasionally create configurations which contain different
5997c478bd9Sstevel@tonic-gate  * revisions of the CPUs that are almost but not exactly the same.  At the
6007c478bd9Sstevel@tonic-gate  * time of writing, this meant that their clock rates were the same, their
6017c478bd9Sstevel@tonic-gate  * feature sets were the same, but the required workaround were -not-
6027c478bd9Sstevel@tonic-gate  * necessarily the same.  So, this routine is invoked on -every- CPU soon
6037c478bd9Sstevel@tonic-gate  * after starting to make sure that the resulting system contains the most
6047c478bd9Sstevel@tonic-gate  * pessimal set of workarounds needed to cope with *any* of the CPUs in the
6057c478bd9Sstevel@tonic-gate  * system.
6067c478bd9Sstevel@tonic-gate  *
607ef50d8c0Sesaxe  * workaround_errata is invoked early in mlsetup() for CPU 0, and in
608a3114836SGerry Liu  * mp_startup_common() for all slave CPUs. Slaves process workaround_errata
609a3114836SGerry Liu  * prior to acknowledging their readiness to the master, so this routine will
610ef50d8c0Sesaxe  * never be executed by multiple CPUs in parallel, thus making updates to
611ef50d8c0Sesaxe  * global data safe.
612ef50d8c0Sesaxe  *
6132201b277Skucharsk  * These workarounds are based on Rev 3.57 of the Revision Guide for
6142201b277Skucharsk  * AMD Athlon(tm) 64 and AMD Opteron(tm) Processors, August 2005.
6157c478bd9Sstevel@tonic-gate  */
6167c478bd9Sstevel@tonic-gate 
617ae115bc7Smrj #if defined(OPTERON_ERRATUM_88)
618ae115bc7Smrj int opteron_erratum_88;		/* if non-zero -> at least one cpu has it */
619ae115bc7Smrj #endif
620ae115bc7Smrj 
6217c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
6227c478bd9Sstevel@tonic-gate int opteron_erratum_91;		/* if non-zero -> at least one cpu has it */
6237c478bd9Sstevel@tonic-gate #endif
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
6267c478bd9Sstevel@tonic-gate int opteron_erratum_93;		/* if non-zero -> at least one cpu has it */
6277c478bd9Sstevel@tonic-gate #endif
6287c478bd9Sstevel@tonic-gate 
629ae115bc7Smrj #if defined(OPTERON_ERRATUM_95)
630ae115bc7Smrj int opteron_erratum_95;		/* if non-zero -> at least one cpu has it */
631ae115bc7Smrj #endif
632ae115bc7Smrj 
6337c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
6347c478bd9Sstevel@tonic-gate int opteron_erratum_100;	/* if non-zero -> at least one cpu has it */
6357c478bd9Sstevel@tonic-gate #endif
6367c478bd9Sstevel@tonic-gate 
637ae115bc7Smrj #if defined(OPTERON_ERRATUM_108)
638ae115bc7Smrj int opteron_erratum_108;	/* if non-zero -> at least one cpu has it */
639ae115bc7Smrj #endif
640ae115bc7Smrj 
6417c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
6427c478bd9Sstevel@tonic-gate int opteron_erratum_109;	/* if non-zero -> at least one cpu has it */
6437c478bd9Sstevel@tonic-gate #endif
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
6467c478bd9Sstevel@tonic-gate int opteron_erratum_121;	/* if non-zero -> at least one cpu has it */
6477c478bd9Sstevel@tonic-gate #endif
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
6507c478bd9Sstevel@tonic-gate int opteron_erratum_122;	/* if non-zero -> at least one cpu has it */
6517c478bd9Sstevel@tonic-gate #endif
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
6547c478bd9Sstevel@tonic-gate int opteron_erratum_123;	/* if non-zero -> at least one cpu has it */
6557c478bd9Sstevel@tonic-gate #endif
6567c478bd9Sstevel@tonic-gate 
6572201b277Skucharsk #if defined(OPTERON_ERRATUM_131)
6582201b277Skucharsk int opteron_erratum_131;	/* if non-zero -> at least one cpu has it */
6592201b277Skucharsk #endif
6607c478bd9Sstevel@tonic-gate 
661ef50d8c0Sesaxe #if defined(OPTERON_WORKAROUND_6336786)
662ef50d8c0Sesaxe int opteron_workaround_6336786;	/* non-zero -> WA relevant and applied */
663ef50d8c0Sesaxe int opteron_workaround_6336786_UP = 0;	/* Not needed for UP */
664ef50d8c0Sesaxe #endif
665ef50d8c0Sesaxe 
666ee88d2b9Skchow #if defined(OPTERON_WORKAROUND_6323525)
667ee88d2b9Skchow int opteron_workaround_6323525;	/* if non-zero -> at least one cpu has it */
668ee88d2b9Skchow #endif
669ee88d2b9Skchow 
670512cf780Skchow #if defined(OPTERON_ERRATUM_298)
671512cf780Skchow int opteron_erratum_298;
672512cf780Skchow #endif
673512cf780Skchow 
6745e54b56dSHans Rosenfeld #if defined(OPTERON_ERRATUM_721)
6755e54b56dSHans Rosenfeld int opteron_erratum_721;
6765e54b56dSHans Rosenfeld #endif
6775e54b56dSHans Rosenfeld 
678ae115bc7Smrj static void
workaround_warning(cpu_t * cp,uint_t erratum)679ae115bc7Smrj workaround_warning(cpu_t *cp, uint_t erratum)
680ae115bc7Smrj {
681ae115bc7Smrj 	cmn_err(CE_WARN, "cpu%d: no workaround for erratum %u",
682ae115bc7Smrj 	    cp->cpu_id, erratum);
683ae115bc7Smrj }
684ae115bc7Smrj 
685ae115bc7Smrj static void
workaround_applied(uint_t erratum)686ae115bc7Smrj workaround_applied(uint_t erratum)
687ae115bc7Smrj {
688ae115bc7Smrj 	if (erratum > 1000000)
689ae115bc7Smrj 		cmn_err(CE_CONT, "?workaround applied for cpu issue #%d\n",
690ae115bc7Smrj 		    erratum);
691ae115bc7Smrj 	else
692ae115bc7Smrj 		cmn_err(CE_CONT, "?workaround applied for cpu erratum #%d\n",
693ae115bc7Smrj 		    erratum);
694ae115bc7Smrj }
695ae115bc7Smrj 
696ae115bc7Smrj static void
msr_warning(cpu_t * cp,const char * rw,uint_t msr,int error)697ae115bc7Smrj msr_warning(cpu_t *cp, const char *rw, uint_t msr, int error)
698ae115bc7Smrj {
699ae115bc7Smrj 	cmn_err(CE_WARN, "cpu%d: couldn't %smsr 0x%x, error %d",
700ae115bc7Smrj 	    cp->cpu_id, rw, msr, error);
701ae115bc7Smrj }
7027c478bd9Sstevel@tonic-gate 
70392564cb1Sesaxe /*
704d2aeaf66SEric Saxe  * Determine the number of nodes in a Hammer / Greyhound / Griffin family
705d2aeaf66SEric Saxe  * system.
70692564cb1Sesaxe  */
70792564cb1Sesaxe static uint_t
opteron_get_nnodes(void)70892564cb1Sesaxe opteron_get_nnodes(void)
70992564cb1Sesaxe {
71092564cb1Sesaxe 	static uint_t nnodes = 0;
71192564cb1Sesaxe 
712d2aeaf66SEric Saxe 	if (nnodes == 0) {
71392564cb1Sesaxe #ifdef	DEBUG
71492564cb1Sesaxe 		uint_t family;
71592564cb1Sesaxe 
716d2aeaf66SEric Saxe 		/*
717d2aeaf66SEric Saxe 		 * This routine uses a PCI config space based mechanism
718d2aeaf66SEric Saxe 		 * for retrieving the number of nodes in the system.
719d2aeaf66SEric Saxe 		 * Device 24, function 0, offset 0x60 as used here is not
720d2aeaf66SEric Saxe 		 * AMD processor architectural, and may not work on processor
721d2aeaf66SEric Saxe 		 * families other than those listed below.
722d2aeaf66SEric Saxe 		 *
723d2aeaf66SEric Saxe 		 * Callers of this routine must ensure that we're running on
724d2aeaf66SEric Saxe 		 * a processor which supports this mechanism.
725d2aeaf66SEric Saxe 		 * The assertion below is meant to catch calls on unsupported
726d2aeaf66SEric Saxe 		 * processors.
727d2aeaf66SEric Saxe 		 */
72892564cb1Sesaxe 		family = cpuid_getfamily(CPU);
729d2aeaf66SEric Saxe 		ASSERT(family == 0xf || family == 0x10 || family == 0x11);
73092564cb1Sesaxe #endif	/* DEBUG */
73192564cb1Sesaxe 
73292564cb1Sesaxe 		/*
73392564cb1Sesaxe 		 * Obtain the number of nodes in the system from
73492564cb1Sesaxe 		 * bits [6:4] of the Node ID register on node 0.
73592564cb1Sesaxe 		 *
73692564cb1Sesaxe 		 * The actual node count is NodeID[6:4] + 1
73792564cb1Sesaxe 		 *
73892564cb1Sesaxe 		 * The Node ID register is accessed via function 0,
73992564cb1Sesaxe 		 * offset 0x60. Node 0 is device 24.
74092564cb1Sesaxe 		 */
74192564cb1Sesaxe 		nnodes = ((pci_getl_func(0, 24, 0, 0x60) & 0x70) >> 4) + 1;
74292564cb1Sesaxe 	}
74392564cb1Sesaxe 	return (nnodes);
74492564cb1Sesaxe }
74592564cb1Sesaxe 
7467c478bd9Sstevel@tonic-gate uint_t
do_erratum_298(struct cpu * cpu)747512cf780Skchow do_erratum_298(struct cpu *cpu)
748512cf780Skchow {
749512cf780Skchow 	static int	osvwrc = -3;
750512cf780Skchow 	extern int	osvw_opteron_erratum(cpu_t *, uint_t);
751512cf780Skchow 
752512cf780Skchow 	/*
753512cf780Skchow 	 * L2 Eviction May Occur During Processor Operation To Set
754512cf780Skchow 	 * Accessed or Dirty Bit.
755512cf780Skchow 	 */
756512cf780Skchow 	if (osvwrc == -3) {
757512cf780Skchow 		osvwrc = osvw_opteron_erratum(cpu, 298);
758512cf780Skchow 	} else {
759512cf780Skchow 		/* osvw return codes should be consistent for all cpus */
760512cf780Skchow 		ASSERT(osvwrc == osvw_opteron_erratum(cpu, 298));
761512cf780Skchow 	}
762512cf780Skchow 
763512cf780Skchow 	switch (osvwrc) {
764512cf780Skchow 	case 0:		/* erratum is not present: do nothing */
765512cf780Skchow 		break;
766512cf780Skchow 	case 1:		/* erratum is present: BIOS workaround applied */
767512cf780Skchow 		/*
768512cf780Skchow 		 * check if workaround is actually in place and issue warning
769512cf780Skchow 		 * if not.
770512cf780Skchow 		 */
771512cf780Skchow 		if (((rdmsr(MSR_AMD_HWCR) & AMD_HWCR_TLBCACHEDIS) == 0) ||
772512cf780Skchow 		    ((rdmsr(MSR_AMD_BU_CFG) & AMD_BU_CFG_E298) == 0)) {
773512cf780Skchow #if defined(OPTERON_ERRATUM_298)
774512cf780Skchow 			opteron_erratum_298++;
775512cf780Skchow #else
776512cf780Skchow 			workaround_warning(cpu, 298);
777512cf780Skchow 			return (1);
778512cf780Skchow #endif
779512cf780Skchow 		}
780512cf780Skchow 		break;
781512cf780Skchow 	case -1:	/* cannot determine via osvw: check cpuid */
782512cf780Skchow 		if ((cpuid_opteron_erratum(cpu, 298) > 0) &&
783512cf780Skchow 		    (((rdmsr(MSR_AMD_HWCR) & AMD_HWCR_TLBCACHEDIS) == 0) ||
784512cf780Skchow 		    ((rdmsr(MSR_AMD_BU_CFG) & AMD_BU_CFG_E298) == 0))) {
785512cf780Skchow #if defined(OPTERON_ERRATUM_298)
786512cf780Skchow 			opteron_erratum_298++;
787512cf780Skchow #else
788512cf780Skchow 			workaround_warning(cpu, 298);
789512cf780Skchow 			return (1);
790512cf780Skchow #endif
791512cf780Skchow 		}
792512cf780Skchow 		break;
793512cf780Skchow 	}
794512cf780Skchow 	return (0);
795512cf780Skchow }
796512cf780Skchow 
797512cf780Skchow uint_t
workaround_errata(struct cpu * cpu)7987c478bd9Sstevel@tonic-gate workaround_errata(struct cpu *cpu)
7997c478bd9Sstevel@tonic-gate {
8007c478bd9Sstevel@tonic-gate 	uint_t missing = 0;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	ASSERT(cpu == CPU);
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	/*LINTED*/
8057c478bd9Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 88) > 0) {
8067c478bd9Sstevel@tonic-gate 		/*
8077c478bd9Sstevel@tonic-gate 		 * SWAPGS May Fail To Read Correct GS Base
8087c478bd9Sstevel@tonic-gate 		 */
8097c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_88)
8107c478bd9Sstevel@tonic-gate 		/*
8117c478bd9Sstevel@tonic-gate 		 * The workaround is an mfence in the relevant assembler code
8127c478bd9Sstevel@tonic-gate 		 */
813ae115bc7Smrj 		opteron_erratum_88++;
8147c478bd9Sstevel@tonic-gate #else
815ae115bc7Smrj 		workaround_warning(cpu, 88);
8167c478bd9Sstevel@tonic-gate 		missing++;
8177c478bd9Sstevel@tonic-gate #endif
8187c478bd9Sstevel@tonic-gate 	}
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 91) > 0) {
8217c478bd9Sstevel@tonic-gate 		/*
8227c478bd9Sstevel@tonic-gate 		 * Software Prefetches May Report A Page Fault
8237c478bd9Sstevel@tonic-gate 		 */
8247c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
8257c478bd9Sstevel@tonic-gate 		/*
8267c478bd9Sstevel@tonic-gate 		 * fix is in trap.c
8277c478bd9Sstevel@tonic-gate 		 */
8287c478bd9Sstevel@tonic-gate 		opteron_erratum_91++;
8297c478bd9Sstevel@tonic-gate #else
830ae115bc7Smrj 		workaround_warning(cpu, 91);
8317c478bd9Sstevel@tonic-gate 		missing++;
8327c478bd9Sstevel@tonic-gate #endif
8337c478bd9Sstevel@tonic-gate 	}
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 93) > 0) {
8367c478bd9Sstevel@tonic-gate 		/*
8377c478bd9Sstevel@tonic-gate 		 * RSM Auto-Halt Restart Returns to Incorrect RIP
8387c478bd9Sstevel@tonic-gate 		 */
8397c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
8407c478bd9Sstevel@tonic-gate 		/*
8417c478bd9Sstevel@tonic-gate 		 * fix is in trap.c
8427c478bd9Sstevel@tonic-gate 		 */
8437c478bd9Sstevel@tonic-gate 		opteron_erratum_93++;
8447c478bd9Sstevel@tonic-gate #else
845ae115bc7Smrj 		workaround_warning(cpu, 93);
8467c478bd9Sstevel@tonic-gate 		missing++;
8477c478bd9Sstevel@tonic-gate #endif
8487c478bd9Sstevel@tonic-gate 	}
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	/*LINTED*/
8517c478bd9Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 95) > 0) {
8527c478bd9Sstevel@tonic-gate 		/*
8537c478bd9Sstevel@tonic-gate 		 * RET Instruction May Return to Incorrect EIP
8547c478bd9Sstevel@tonic-gate 		 */
8557c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_95)
8567c478bd9Sstevel@tonic-gate #if defined(_LP64)
8577c478bd9Sstevel@tonic-gate 		/*
8587c478bd9Sstevel@tonic-gate 		 * Workaround this by ensuring that 32-bit user code and
8597c478bd9Sstevel@tonic-gate 		 * 64-bit kernel code never occupy the same address
8607c478bd9Sstevel@tonic-gate 		 * range mod 4G.
8617c478bd9Sstevel@tonic-gate 		 */
8627c478bd9Sstevel@tonic-gate 		if (_userlimit32 > 0xc0000000ul)
8637c478bd9Sstevel@tonic-gate 			*(uintptr_t *)&_userlimit32 = 0xc0000000ul;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 		/*LINTED*/
8667c478bd9Sstevel@tonic-gate 		ASSERT((uint32_t)COREHEAP_BASE == 0xc0000000u);
867ae115bc7Smrj 		opteron_erratum_95++;
8687c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
8697c478bd9Sstevel@tonic-gate #else
870ae115bc7Smrj 		workaround_warning(cpu, 95);
8717c478bd9Sstevel@tonic-gate 		missing++;
872ae115bc7Smrj #endif
8737c478bd9Sstevel@tonic-gate 	}
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 100) > 0) {
8767c478bd9Sstevel@tonic-gate 		/*
8777c478bd9Sstevel@tonic-gate 		 * Compatibility Mode Branches Transfer to Illegal Address
8787c478bd9Sstevel@tonic-gate 		 */
8797c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
8807c478bd9Sstevel@tonic-gate 		/*
8817c478bd9Sstevel@tonic-gate 		 * fix is in trap.c
8827c478bd9Sstevel@tonic-gate 		 */
8837c478bd9Sstevel@tonic-gate 		opteron_erratum_100++;
8847c478bd9Sstevel@tonic-gate #else
885ae115bc7Smrj 		workaround_warning(cpu, 100);
8867c478bd9Sstevel@tonic-gate 		missing++;
8877c478bd9Sstevel@tonic-gate #endif
8887c478bd9Sstevel@tonic-gate 	}
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	/*LINTED*/
8917c478bd9Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 108) > 0) {
8927c478bd9Sstevel@tonic-gate 		/*
8937c478bd9Sstevel@tonic-gate 		 * CPUID Instruction May Return Incorrect Model Number In
8947c478bd9Sstevel@tonic-gate 		 * Some Processors
8957c478bd9Sstevel@tonic-gate 		 */
8967c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_108)
8977c478bd9Sstevel@tonic-gate 		/*
8987c478bd9Sstevel@tonic-gate 		 * (Our cpuid-handling code corrects the model number on
8997c478bd9Sstevel@tonic-gate 		 * those processors)
9007c478bd9Sstevel@tonic-gate 		 */
9017c478bd9Sstevel@tonic-gate #else
902ae115bc7Smrj 		workaround_warning(cpu, 108);
9037c478bd9Sstevel@tonic-gate 		missing++;
9047c478bd9Sstevel@tonic-gate #endif
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	/*LINTED*/
908ae115bc7Smrj 	if (cpuid_opteron_erratum(cpu, 109) > 0) do {
9097c478bd9Sstevel@tonic-gate 		/*
910fb2caebeSRandy Fishel 		 * Certain Reverse REP MOVS May Produce Unpredictable Behavior
9117c478bd9Sstevel@tonic-gate 		 */
9127c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
913ae115bc7Smrj 		/*
914ae115bc7Smrj 		 * The "workaround" is to print a warning to upgrade the BIOS
915ae115bc7Smrj 		 */
916ae115bc7Smrj 		uint64_t value;
917ae115bc7Smrj 		const uint_t msr = MSR_AMD_PATCHLEVEL;
918ae115bc7Smrj 		int err;
9197c478bd9Sstevel@tonic-gate 
920ae115bc7Smrj 		if ((err = checked_rdmsr(msr, &value)) != 0) {
921ae115bc7Smrj 			msr_warning(cpu, "rd", msr, err);
922ae115bc7Smrj 			workaround_warning(cpu, 109);
923ae115bc7Smrj 			missing++;
924ae115bc7Smrj 		}
925ae115bc7Smrj 		if (value == 0)
9267c478bd9Sstevel@tonic-gate 			opteron_erratum_109++;
9277c478bd9Sstevel@tonic-gate #else
928ae115bc7Smrj 		workaround_warning(cpu, 109);
9297c478bd9Sstevel@tonic-gate 		missing++;
9307c478bd9Sstevel@tonic-gate #endif
931ae115bc7Smrj 	/*CONSTANTCONDITION*/
932ae115bc7Smrj 	} while (0);
933ae115bc7Smrj 
9347c478bd9Sstevel@tonic-gate 	/*LINTED*/
9357c478bd9Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 121) > 0) {
9367c478bd9Sstevel@tonic-gate 		/*
9377c478bd9Sstevel@tonic-gate 		 * Sequential Execution Across Non_Canonical Boundary Caused
9387c478bd9Sstevel@tonic-gate 		 * Processor Hang
9397c478bd9Sstevel@tonic-gate 		 */
9407c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
941ae115bc7Smrj #if defined(_LP64)
9427c478bd9Sstevel@tonic-gate 		/*
9437c478bd9Sstevel@tonic-gate 		 * Erratum 121 is only present in long (64 bit) mode.
9447c478bd9Sstevel@tonic-gate 		 * Workaround is to include the page immediately before the
9457c478bd9Sstevel@tonic-gate 		 * va hole to eliminate the possibility of system hangs due to
9467c478bd9Sstevel@tonic-gate 		 * sequential execution across the va hole boundary.
9477c478bd9Sstevel@tonic-gate 		 */
948ae115bc7Smrj 		if (opteron_erratum_121)
949ae115bc7Smrj 			opteron_erratum_121++;
950ae115bc7Smrj 		else {
9517c478bd9Sstevel@tonic-gate 			if (hole_start) {
9527c478bd9Sstevel@tonic-gate 				hole_start -= PAGESIZE;
9537c478bd9Sstevel@tonic-gate 			} else {
9547c478bd9Sstevel@tonic-gate 				/*
9557c478bd9Sstevel@tonic-gate 				 * hole_start not yet initialized by
9567c478bd9Sstevel@tonic-gate 				 * mmu_init. Initialize hole_start
9577c478bd9Sstevel@tonic-gate 				 * with value to be subtracted.
9587c478bd9Sstevel@tonic-gate 				 */
9597c478bd9Sstevel@tonic-gate 				hole_start = PAGESIZE;
9607c478bd9Sstevel@tonic-gate 			}
9617c478bd9Sstevel@tonic-gate 			opteron_erratum_121++;
9627c478bd9Sstevel@tonic-gate 		}
963ae115bc7Smrj #endif	/* _LP64 */
9647c478bd9Sstevel@tonic-gate #else
965ae115bc7Smrj 		workaround_warning(cpu, 121);
9667c478bd9Sstevel@tonic-gate 		missing++;
9677c478bd9Sstevel@tonic-gate #endif
9687c478bd9Sstevel@tonic-gate 	}
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	/*LINTED*/
971ae115bc7Smrj 	if (cpuid_opteron_erratum(cpu, 122) > 0) do {
9727c478bd9Sstevel@tonic-gate 		/*
973ae115bc7Smrj 		 * TLB Flush Filter May Cause Coherency Problem in
9747c478bd9Sstevel@tonic-gate 		 * Multiprocessor Systems
9757c478bd9Sstevel@tonic-gate 		 */
9767c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
977ae115bc7Smrj 		uint64_t value;
978ae115bc7Smrj 		const uint_t msr = MSR_AMD_HWCR;
979ae115bc7Smrj 		int error;
980ae115bc7Smrj 
9817c478bd9Sstevel@tonic-gate 		/*
9827c478bd9Sstevel@tonic-gate 		 * Erratum 122 is only present in MP configurations (multi-core
9837c478bd9Sstevel@tonic-gate 		 * or multi-processor).
9847c478bd9Sstevel@tonic-gate 		 */
985843e1988Sjohnlev #if defined(__xpv)
986843e1988Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
987843e1988Sjohnlev 			break;
988349b53ddSStuart Maybee 		if (!opteron_erratum_122 && xpv_nr_phys_cpus() == 1)
989843e1988Sjohnlev 			break;
990843e1988Sjohnlev #else
99192564cb1Sesaxe 		if (!opteron_erratum_122 && opteron_get_nnodes() == 1 &&
992ae115bc7Smrj 		    cpuid_get_ncpu_per_chip(cpu) == 1)
993ae115bc7Smrj 			break;
994843e1988Sjohnlev #endif
9957c478bd9Sstevel@tonic-gate 		/* disable TLB Flush Filter */
9967c478bd9Sstevel@tonic-gate 
997ae115bc7Smrj 		if ((error = checked_rdmsr(msr, &value)) != 0) {
998ae115bc7Smrj 			msr_warning(cpu, "rd", msr, error);
999ae115bc7Smrj 			workaround_warning(cpu, 122);
1000ae115bc7Smrj 			missing++;
1001ae115bc7Smrj 		} else {
1002ae115bc7Smrj 			value |= (uint64_t)AMD_HWCR_FFDIS;
1003ae115bc7Smrj 			if ((error = checked_wrmsr(msr, value)) != 0) {
1004ae115bc7Smrj 				msr_warning(cpu, "wr", msr, error);
1005ae115bc7Smrj 				workaround_warning(cpu, 122);
1006ae115bc7Smrj 				missing++;
1007ae115bc7Smrj 			}
1008ae115bc7Smrj 		}
1009ae115bc7Smrj 		opteron_erratum_122++;
10107c478bd9Sstevel@tonic-gate #else
1011ae115bc7Smrj 		workaround_warning(cpu, 122);
10127c478bd9Sstevel@tonic-gate 		missing++;
10137c478bd9Sstevel@tonic-gate #endif
1014ae115bc7Smrj 	/*CONSTANTCONDITION*/
1015ae115bc7Smrj 	} while (0);
1016403c216aSkchow 
10177c478bd9Sstevel@tonic-gate 	/*LINTED*/
1018ae115bc7Smrj 	if (cpuid_opteron_erratum(cpu, 123) > 0) do {
10197c478bd9Sstevel@tonic-gate 		/*
10207c478bd9Sstevel@tonic-gate 		 * Bypassed Reads May Cause Data Corruption of System Hang in
10217c478bd9Sstevel@tonic-gate 		 * Dual Core Processors
10227c478bd9Sstevel@tonic-gate 		 */
1023ae115bc7Smrj #if defined(OPTERON_ERRATUM_123)
1024ae115bc7Smrj 		uint64_t value;
1025ae115bc7Smrj 		const uint_t msr = MSR_AMD_PATCHLEVEL;
1026ae115bc7Smrj 		int err;
1027ae115bc7Smrj 
10287c478bd9Sstevel@tonic-gate 		/*
10297c478bd9Sstevel@tonic-gate 		 * Erratum 123 applies only to multi-core cpus.
10307c478bd9Sstevel@tonic-gate 		 */
1031ae115bc7Smrj 		if (cpuid_get_ncpu_per_chip(cpu) < 2)
1032ae115bc7Smrj 			break;
1033843e1988Sjohnlev #if defined(__xpv)
1034843e1988Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
1035843e1988Sjohnlev 			break;
1036843e1988Sjohnlev #endif
1037ae115bc7Smrj 		/*
1038ae115bc7Smrj 		 * The "workaround" is to print a warning to upgrade the BIOS
1039ae115bc7Smrj 		 */
1040ae115bc7Smrj 		if ((err = checked_rdmsr(msr, &value)) != 0) {
1041ae115bc7Smrj 			msr_warning(cpu, "rd", msr, err);
1042ae115bc7Smrj 			workaround_warning(cpu, 123);
1043ae115bc7Smrj 			missing++;
1044ae115bc7Smrj 		}
1045ae115bc7Smrj 		if (value == 0)
10467c478bd9Sstevel@tonic-gate 			opteron_erratum_123++;
1047ae115bc7Smrj #else
1048ae115bc7Smrj 		workaround_warning(cpu, 123);
1049ae115bc7Smrj 		missing++;
10502201b277Skucharsk 
1051ae115bc7Smrj #endif
1052ae115bc7Smrj 	/*CONSTANTCONDITION*/
1053ae115bc7Smrj 	} while (0);
1054ae115bc7Smrj 
10552201b277Skucharsk 	/*LINTED*/
1056ae115bc7Smrj 	if (cpuid_opteron_erratum(cpu, 131) > 0) do {
10572201b277Skucharsk 		/*
10582201b277Skucharsk 		 * Multiprocessor Systems with Four or More Cores May Deadlock
10592201b277Skucharsk 		 * Waiting for a Probe Response
10602201b277Skucharsk 		 */
1061ae115bc7Smrj #if defined(OPTERON_ERRATUM_131)
1062ae115bc7Smrj 		uint64_t nbcfg;
1063ae115bc7Smrj 		const uint_t msr = MSR_AMD_NB_CFG;
1064ae115bc7Smrj 		const uint64_t wabits =
1065ae115bc7Smrj 		    AMD_NB_CFG_SRQ_HEARTBEAT | AMD_NB_CFG_SRQ_SPR;
1066ae115bc7Smrj 		int error;
1067ae115bc7Smrj 
10682201b277Skucharsk 		/*
10692201b277Skucharsk 		 * Erratum 131 applies to any system with four or more cores.
10702201b277Skucharsk 		 */
1071ae115bc7Smrj 		if (opteron_erratum_131)
1072ae115bc7Smrj 			break;
1073843e1988Sjohnlev #if defined(__xpv)
1074843e1988Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
1075843e1988Sjohnlev 			break;
1076349b53ddSStuart Maybee 		if (xpv_nr_phys_cpus() < 4)
1077843e1988Sjohnlev 			break;
1078843e1988Sjohnlev #else
107992564cb1Sesaxe 		if (opteron_get_nnodes() * cpuid_get_ncpu_per_chip(cpu) < 4)
1080ae115bc7Smrj 			break;
1081843e1988Sjohnlev #endif
10822201b277Skucharsk 		/*
1083ae115bc7Smrj 		 * Print a warning if neither of the workarounds for
1084ae115bc7Smrj 		 * erratum 131 is present.
10852201b277Skucharsk 		 */
1086ae115bc7Smrj 		if ((error = checked_rdmsr(msr, &nbcfg)) != 0) {
1087ae115bc7Smrj 			msr_warning(cpu, "rd", msr, error);
1088ae115bc7Smrj 			workaround_warning(cpu, 131);
1089ae115bc7Smrj 			missing++;
1090ae115bc7Smrj 		} else if ((nbcfg & wabits) == 0) {
10912201b277Skucharsk 			opteron_erratum_131++;
1092cb9f16ebSkchow 		} else {
1093cb9f16ebSkchow 			/* cannot have both workarounds set */
1094cb9f16ebSkchow 			ASSERT((nbcfg & wabits) != wabits);
1095cb9f16ebSkchow 		}
1096ae115bc7Smrj #else
1097ae115bc7Smrj 		workaround_warning(cpu, 131);
1098ae115bc7Smrj 		missing++;
1099ef50d8c0Sesaxe #endif
1100ae115bc7Smrj 	/*CONSTANTCONDITION*/
1101ae115bc7Smrj 	} while (0);
1102ef50d8c0Sesaxe 
1103ef50d8c0Sesaxe 	/*
1104ae115bc7Smrj 	 * This isn't really an erratum, but for convenience the
1105ef50d8c0Sesaxe 	 * detection/workaround code lives here and in cpuid_opteron_erratum.
1106ef50d8c0Sesaxe 	 */
1107ef50d8c0Sesaxe 	if (cpuid_opteron_erratum(cpu, 6336786) > 0) {
1108ae115bc7Smrj #if defined(OPTERON_WORKAROUND_6336786)
1109ef50d8c0Sesaxe 		/*
1110ef50d8c0Sesaxe 		 * Disable C1-Clock ramping on multi-core/multi-processor
1111ef50d8c0Sesaxe 		 * K8 platforms to guard against TSC drift.
1112ef50d8c0Sesaxe 		 */
1113ef50d8c0Sesaxe 		if (opteron_workaround_6336786) {
1114ef50d8c0Sesaxe 			opteron_workaround_6336786++;
1115843e1988Sjohnlev #if defined(__xpv)
1116843e1988Sjohnlev 		} else if ((DOMAIN_IS_INITDOMAIN(xen_info) &&
1117349b53ddSStuart Maybee 		    xpv_nr_phys_cpus() > 1) ||
1118843e1988Sjohnlev 		    opteron_workaround_6336786_UP) {
1119843e1988Sjohnlev 			/*
112092564cb1Sesaxe 			 * XXPV	Hmm.  We can't walk the Northbridges on
1121843e1988Sjohnlev 			 *	the hypervisor; so just complain and drive
1122843e1988Sjohnlev 			 *	on.  This probably needs to be fixed in
1123843e1988Sjohnlev 			 *	the hypervisor itself.
1124843e1988Sjohnlev 			 */
1125843e1988Sjohnlev 			opteron_workaround_6336786++;
1126843e1988Sjohnlev 			workaround_warning(cpu, 6336786);
1127843e1988Sjohnlev #else	/* __xpv */
112892564cb1Sesaxe 		} else if ((opteron_get_nnodes() *
1129ae115bc7Smrj 		    cpuid_get_ncpu_per_chip(cpu) > 1) ||
1130ef50d8c0Sesaxe 		    opteron_workaround_6336786_UP) {
113192564cb1Sesaxe 
113292564cb1Sesaxe 			uint_t	node, nnodes;
1133ae115bc7Smrj 			uint8_t data;
1134ae115bc7Smrj 
113592564cb1Sesaxe 			nnodes = opteron_get_nnodes();
113692564cb1Sesaxe 			for (node = 0; node < nnodes; node++) {
1137ef50d8c0Sesaxe 				/*
1138ef50d8c0Sesaxe 				 * Clear PMM7[1:0] (function 3, offset 0x87)
1139ef50d8c0Sesaxe 				 * Northbridge device is the node id + 24.
1140ef50d8c0Sesaxe 				 */
1141ef50d8c0Sesaxe 				data = pci_getb_func(0, node + 24, 3, 0x87);
1142ef50d8c0Sesaxe 				data &= 0xFC;
1143ef50d8c0Sesaxe 				pci_putb_func(0, node + 24, 3, 0x87, data);
1144ef50d8c0Sesaxe 			}
1145ef50d8c0Sesaxe 			opteron_workaround_6336786++;
1146843e1988Sjohnlev #endif	/* __xpv */
1147ef50d8c0Sesaxe 		}
1148ae115bc7Smrj #else
1149ae115bc7Smrj 		workaround_warning(cpu, 6336786);
1150ae115bc7Smrj 		missing++;
1151ef50d8c0Sesaxe #endif
1152ae115bc7Smrj 	}
1153ee88d2b9Skchow 
1154ee88d2b9Skchow 	/*LINTED*/
1155ee88d2b9Skchow 	/*
1156ee88d2b9Skchow 	 * Mutex primitives don't work as expected.
1157ee88d2b9Skchow 	 */
1158ee88d2b9Skchow 	if (cpuid_opteron_erratum(cpu, 6323525) > 0) {
1159ae115bc7Smrj #if defined(OPTERON_WORKAROUND_6323525)
1160ee88d2b9Skchow 		/*
1161ae115bc7Smrj 		 * This problem only occurs with 2 or more cores. If bit in
1162512cf780Skchow 		 * MSR_AMD_BU_CFG set, then not applicable. The workaround
1163ee88d2b9Skchow 		 * is to patch the semaphone routines with the lfence
1164ee88d2b9Skchow 		 * instruction to provide necessary load memory barrier with
1165ee88d2b9Skchow 		 * possible subsequent read-modify-write ops.
1166ee88d2b9Skchow 		 *
1167ee88d2b9Skchow 		 * It is too early in boot to call the patch routine so
1168ee88d2b9Skchow 		 * set erratum variable to be done in startup_end().
1169ee88d2b9Skchow 		 */
1170ee88d2b9Skchow 		if (opteron_workaround_6323525) {
1171ee88d2b9Skchow 			opteron_workaround_6323525++;
1172843e1988Sjohnlev #if defined(__xpv)
11737417cfdeSKuriakose Kuruvilla 		} else if (is_x86_feature(x86_featureset, X86FSET_SSE2)) {
1174843e1988Sjohnlev 			if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1175843e1988Sjohnlev 				/*
1176843e1988Sjohnlev 				 * XXPV	Use dom0_msr here when extended
1177843e1988Sjohnlev 				 *	operations are supported?
1178843e1988Sjohnlev 				 */
1179349b53ddSStuart Maybee 				if (xpv_nr_phys_cpus() > 1)
1180843e1988Sjohnlev 					opteron_workaround_6323525++;
1181843e1988Sjohnlev 			} else {
1182843e1988Sjohnlev 				/*
1183843e1988Sjohnlev 				 * We have no way to tell how many physical
1184843e1988Sjohnlev 				 * cpus there are, or even if this processor
1185843e1988Sjohnlev 				 * has the problem, so enable the workaround
1186843e1988Sjohnlev 				 * unconditionally (at some performance cost).
1187843e1988Sjohnlev 				 */
1188843e1988Sjohnlev 				opteron_workaround_6323525++;
1189843e1988Sjohnlev 			}
1190843e1988Sjohnlev #else	/* __xpv */
11917417cfdeSKuriakose Kuruvilla 		} else if (is_x86_feature(x86_featureset, X86FSET_SSE2) &&
11927417cfdeSKuriakose Kuruvilla 		    ((opteron_get_nnodes() *
1193ae115bc7Smrj 		    cpuid_get_ncpu_per_chip(cpu)) > 1)) {
119448b2bf45SKit Chow 			if ((xrdmsr(MSR_AMD_BU_CFG) & (UINT64_C(1) << 33)) == 0)
1195ee88d2b9Skchow 				opteron_workaround_6323525++;
1196843e1988Sjohnlev #endif	/* __xpv */
1197ee88d2b9Skchow 		}
1198ae115bc7Smrj #else
1199ae115bc7Smrj 		workaround_warning(cpu, 6323525);
1200ae115bc7Smrj 		missing++;
1201ee88d2b9Skchow #endif
1202ae115bc7Smrj 	}
1203ae115bc7Smrj 
1204512cf780Skchow 	missing += do_erratum_298(cpu);
1205512cf780Skchow 
12065e54b56dSHans Rosenfeld 	if (cpuid_opteron_erratum(cpu, 721) > 0) {
12075e54b56dSHans Rosenfeld #if defined(OPTERON_ERRATUM_721)
1208850ad55aSHans Rosenfeld 		on_trap_data_t otd;
1209850ad55aSHans Rosenfeld 
1210850ad55aSHans Rosenfeld 		if (!on_trap(&otd, OT_DATA_ACCESS))
1211850ad55aSHans Rosenfeld 			wrmsr(MSR_AMD_DE_CFG,
1212850ad55aSHans Rosenfeld 			    rdmsr(MSR_AMD_DE_CFG) | AMD_DE_CFG_E721);
1213850ad55aSHans Rosenfeld 		no_trap();
1214850ad55aSHans Rosenfeld 
12155e54b56dSHans Rosenfeld 		opteron_erratum_721++;
12165e54b56dSHans Rosenfeld #else
12175e54b56dSHans Rosenfeld 		workaround_warning(cpu, 721);
12185e54b56dSHans Rosenfeld 		missing++;
12195e54b56dSHans Rosenfeld #endif
12205e54b56dSHans Rosenfeld 	}
12215e54b56dSHans Rosenfeld 
1222843e1988Sjohnlev #ifdef __xpv
1223843e1988Sjohnlev 	return (0);
1224843e1988Sjohnlev #else
12257c478bd9Sstevel@tonic-gate 	return (missing);
1226843e1988Sjohnlev #endif
12277c478bd9Sstevel@tonic-gate }
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate void
workaround_errata_end()12307c478bd9Sstevel@tonic-gate workaround_errata_end()
12317c478bd9Sstevel@tonic-gate {
1232ae115bc7Smrj #if defined(OPTERON_ERRATUM_88)
1233ae115bc7Smrj 	if (opteron_erratum_88)
1234ae115bc7Smrj 		workaround_applied(88);
1235ae115bc7Smrj #endif
1236ae115bc7Smrj #if defined(OPTERON_ERRATUM_91)
1237ae115bc7Smrj 	if (opteron_erratum_91)
1238ae115bc7Smrj 		workaround_applied(91);
1239ae115bc7Smrj #endif
1240ae115bc7Smrj #if defined(OPTERON_ERRATUM_93)
1241ae115bc7Smrj 	if (opteron_erratum_93)
1242ae115bc7Smrj 		workaround_applied(93);
1243ae115bc7Smrj #endif
1244ae115bc7Smrj #if defined(OPTERON_ERRATUM_95)
1245ae115bc7Smrj 	if (opteron_erratum_95)
1246ae115bc7Smrj 		workaround_applied(95);
1247ae115bc7Smrj #endif
1248ae115bc7Smrj #if defined(OPTERON_ERRATUM_100)
1249ae115bc7Smrj 	if (opteron_erratum_100)
1250ae115bc7Smrj 		workaround_applied(100);
1251ae115bc7Smrj #endif
1252ae115bc7Smrj #if defined(OPTERON_ERRATUM_108)
1253ae115bc7Smrj 	if (opteron_erratum_108)
1254ae115bc7Smrj 		workaround_applied(108);
1255ae115bc7Smrj #endif
12567c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
12577c478bd9Sstevel@tonic-gate 	if (opteron_erratum_109) {
12582201b277Skucharsk 		cmn_err(CE_WARN,
12592201b277Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
12602201b277Skucharsk 		    " processor\nerratum 109 was not detected; updating your"
12612201b277Skucharsk 		    " system's BIOS to a version\ncontaining this"
12622201b277Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
12632201b277Skucharsk 		    " system\noperation may occur.\n");
12647c478bd9Sstevel@tonic-gate 	}
1265ae115bc7Smrj #endif
1266ae115bc7Smrj #if defined(OPTERON_ERRATUM_121)
1267ae115bc7Smrj 	if (opteron_erratum_121)
1268ae115bc7Smrj 		workaround_applied(121);
1269ae115bc7Smrj #endif
1270ae115bc7Smrj #if defined(OPTERON_ERRATUM_122)
1271ae115bc7Smrj 	if (opteron_erratum_122)
1272ae115bc7Smrj 		workaround_applied(122);
1273ae115bc7Smrj #endif
12747c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
12757c478bd9Sstevel@tonic-gate 	if (opteron_erratum_123) {
12762201b277Skucharsk 		cmn_err(CE_WARN,
12772201b277Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
12782201b277Skucharsk 		    " processor\nerratum 123 was not detected; updating your"
12792201b277Skucharsk 		    " system's BIOS to a version\ncontaining this"
12802201b277Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
12812201b277Skucharsk 		    " system\noperation may occur.\n");
12827c478bd9Sstevel@tonic-gate 	}
1283ae115bc7Smrj #endif
12842201b277Skucharsk #if defined(OPTERON_ERRATUM_131)
12852201b277Skucharsk 	if (opteron_erratum_131) {
12862201b277Skucharsk 		cmn_err(CE_WARN,
12872201b277Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
12882201b277Skucharsk 		    " processor\nerratum 131 was not detected; updating your"
12892201b277Skucharsk 		    " system's BIOS to a version\ncontaining this"
12902201b277Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
12912201b277Skucharsk 		    " system\noperation may occur.\n");
12922201b277Skucharsk 	}
1293ae115bc7Smrj #endif
1294ae115bc7Smrj #if defined(OPTERON_WORKAROUND_6336786)
1295ae115bc7Smrj 	if (opteron_workaround_6336786)
1296ae115bc7Smrj 		workaround_applied(6336786);
1297ae115bc7Smrj #endif
1298ae115bc7Smrj #if defined(OPTERON_WORKAROUND_6323525)
1299ae115bc7Smrj 	if (opteron_workaround_6323525)
1300ae115bc7Smrj 		workaround_applied(6323525);
1301ae115bc7Smrj #endif
1302512cf780Skchow #if defined(OPTERON_ERRATUM_298)
1303512cf780Skchow 	if (opteron_erratum_298) {
1304512cf780Skchow 		cmn_err(CE_WARN,
1305512cf780Skchow 		    "BIOS microcode patch for AMD 64/Opteron(tm)"
1306512cf780Skchow 		    " processor\nerratum 298 was not detected; updating your"
1307512cf780Skchow 		    " system's BIOS to a version\ncontaining this"
1308512cf780Skchow 		    " microcode patch is HIGHLY recommended or erroneous"
1309512cf780Skchow 		    " system\noperation may occur.\n");
1310512cf780Skchow 	}
1311512cf780Skchow #endif
13125e54b56dSHans Rosenfeld #if defined(OPTERON_ERRATUM_721)
13135e54b56dSHans Rosenfeld 	if (opteron_erratum_721)
13145e54b56dSHans Rosenfeld 		workaround_applied(721);
13155e54b56dSHans Rosenfeld #endif
13167c478bd9Sstevel@tonic-gate }
13177c478bd9Sstevel@tonic-gate 
1318a3114836SGerry Liu /*
1319a3114836SGerry Liu  * The procset_slave and procset_master are used to synchronize
1320a3114836SGerry Liu  * between the control CPU and the target CPU when starting CPUs.
1321a3114836SGerry Liu  */
1322a3114836SGerry Liu static cpuset_t procset_slave, procset_master;
1323a3114836SGerry Liu 
1324a3114836SGerry Liu static void
mp_startup_wait(cpuset_t * sp,processorid_t cpuid)1325a3114836SGerry Liu mp_startup_wait(cpuset_t *sp, processorid_t cpuid)
1326a3114836SGerry Liu {
1327a3114836SGerry Liu 	cpuset_t tempset;
1328a3114836SGerry Liu 
1329a3114836SGerry Liu 	for (tempset = *sp; !CPU_IN_SET(tempset, cpuid);
1330a3114836SGerry Liu 	    tempset = *(volatile cpuset_t *)sp) {
1331a3114836SGerry Liu 		SMT_PAUSE();
1332a3114836SGerry Liu 	}
1333a3114836SGerry Liu 	CPUSET_ATOMIC_DEL(*(cpuset_t *)sp, cpuid);
1334a3114836SGerry Liu }
1335a3114836SGerry Liu 
1336a3114836SGerry Liu static void
mp_startup_signal(cpuset_t * sp,processorid_t cpuid)1337a3114836SGerry Liu mp_startup_signal(cpuset_t *sp, processorid_t cpuid)
1338a3114836SGerry Liu {
1339a3114836SGerry Liu 	cpuset_t tempset;
1340a3114836SGerry Liu 
1341a3114836SGerry Liu 	CPUSET_ATOMIC_ADD(*(cpuset_t *)sp, cpuid);
1342a3114836SGerry Liu 	for (tempset = *sp; CPU_IN_SET(tempset, cpuid);
1343a3114836SGerry Liu 	    tempset = *(volatile cpuset_t *)sp) {
1344a3114836SGerry Liu 		SMT_PAUSE();
1345a3114836SGerry Liu 	}
1346a3114836SGerry Liu }
1347a3114836SGerry Liu 
1348a3114836SGerry Liu int
mp_start_cpu_common(cpu_t * cp,boolean_t boot)1349a3114836SGerry Liu mp_start_cpu_common(cpu_t *cp, boolean_t boot)
1350a3114836SGerry Liu {
1351a3114836SGerry Liu 	_NOTE(ARGUNUSED(boot));
1352a3114836SGerry Liu 
1353a3114836SGerry Liu 	void *ctx;
1354a3114836SGerry Liu 	int delays;
1355a3114836SGerry Liu 	int error = 0;
1356a3114836SGerry Liu 	cpuset_t tempset;
1357a3114836SGerry Liu 	processorid_t cpuid;
1358a3114836SGerry Liu #ifndef __xpv
1359a3114836SGerry Liu 	extern void cpupm_init(cpu_t *);
1360a3114836SGerry Liu #endif
1361a3114836SGerry Liu 
1362a3114836SGerry Liu 	ASSERT(cp != NULL);
1363a3114836SGerry Liu 	cpuid = cp->cpu_id;
1364a3114836SGerry Liu 	ctx = mach_cpucontext_alloc(cp);
1365a3114836SGerry Liu 	if (ctx == NULL) {
1366a3114836SGerry Liu 		cmn_err(CE_WARN,
1367a3114836SGerry Liu 		    "cpu%d: failed to allocate context", cp->cpu_id);
1368a3114836SGerry Liu 		return (EAGAIN);
1369a3114836SGerry Liu 	}
1370a3114836SGerry Liu 	error = mach_cpu_start(cp, ctx);
1371a3114836SGerry Liu 	if (error != 0) {
1372a3114836SGerry Liu 		cmn_err(CE_WARN,
1373a3114836SGerry Liu 		    "cpu%d: failed to start, error %d", cp->cpu_id, error);
1374a3114836SGerry Liu 		mach_cpucontext_free(cp, ctx, error);
1375a3114836SGerry Liu 		return (error);
1376a3114836SGerry Liu 	}
1377a3114836SGerry Liu 
1378a3114836SGerry Liu 	for (delays = 0, tempset = procset_slave; !CPU_IN_SET(tempset, cpuid);
1379a3114836SGerry Liu 	    delays++) {
1380a3114836SGerry Liu 		if (delays == 500) {
1381a3114836SGerry Liu 			/*
1382a3114836SGerry Liu 			 * After five seconds, things are probably looking
1383a3114836SGerry Liu 			 * a bit bleak - explain the hang.
1384a3114836SGerry Liu 			 */
1385a3114836SGerry Liu 			cmn_err(CE_NOTE, "cpu%d: started, "
1386a3114836SGerry Liu 			    "but not running in the kernel yet", cpuid);
1387a3114836SGerry Liu 		} else if (delays > 2000) {
1388a3114836SGerry Liu 			/*
1389a3114836SGerry Liu 			 * We waited at least 20 seconds, bail ..
1390a3114836SGerry Liu 			 */
1391a3114836SGerry Liu 			error = ETIMEDOUT;
1392a3114836SGerry Liu 			cmn_err(CE_WARN, "cpu%d: timed out", cpuid);
1393a3114836SGerry Liu 			mach_cpucontext_free(cp, ctx, error);
1394a3114836SGerry Liu 			return (error);
1395a3114836SGerry Liu 		}
1396a3114836SGerry Liu 
1397a3114836SGerry Liu 		/*
1398a3114836SGerry Liu 		 * wait at least 10ms, then check again..
1399a3114836SGerry Liu 		 */
1400a3114836SGerry Liu 		delay(USEC_TO_TICK_ROUNDUP(10000));
1401a3114836SGerry Liu 		tempset = *((volatile cpuset_t *)&procset_slave);
1402a3114836SGerry Liu 	}
1403a3114836SGerry Liu 	CPUSET_ATOMIC_DEL(procset_slave, cpuid);
1404a3114836SGerry Liu 
1405a3114836SGerry Liu 	mach_cpucontext_free(cp, ctx, 0);
1406a3114836SGerry Liu 
1407a3114836SGerry Liu #ifndef __xpv
1408a3114836SGerry Liu 	if (tsc_gethrtime_enable)
1409a3114836SGerry Liu 		tsc_sync_master(cpuid);
1410a3114836SGerry Liu #endif
1411a3114836SGerry Liu 
1412a3114836SGerry Liu 	if (dtrace_cpu_init != NULL) {
1413a3114836SGerry Liu 		(*dtrace_cpu_init)(cpuid);
1414a3114836SGerry Liu 	}
1415a3114836SGerry Liu 
1416a3114836SGerry Liu 	/*
1417a3114836SGerry Liu 	 * During CPU DR operations, the cpu_lock is held by current
1418a3114836SGerry Liu 	 * (the control) thread. We can't release the cpu_lock here
1419a3114836SGerry Liu 	 * because that will break the CPU DR logic.
1420a3114836SGerry Liu 	 * On the other hand, CPUPM and processor group initialization
1421a3114836SGerry Liu 	 * routines need to access the cpu_lock. So we invoke those
1422a3114836SGerry Liu 	 * routines here on behalf of mp_startup_common().
1423a3114836SGerry Liu 	 *
1424a3114836SGerry Liu 	 * CPUPM and processor group initialization routines depend
1425a3114836SGerry Liu 	 * on the cpuid probing results. Wait for mp_startup_common()
1426a3114836SGerry Liu 	 * to signal that cpuid probing is done.
1427a3114836SGerry Liu 	 */
1428a3114836SGerry Liu 	mp_startup_wait(&procset_slave, cpuid);
1429a3114836SGerry Liu #ifndef __xpv
1430a3114836SGerry Liu 	cpupm_init(cp);
1431a3114836SGerry Liu #endif
1432a3114836SGerry Liu 	(void) pg_cpu_init(cp, B_FALSE);
1433a3114836SGerry Liu 	cpu_set_state(cp);
1434a3114836SGerry Liu 	mp_startup_signal(&procset_master, cpuid);
1435a3114836SGerry Liu 
1436a3114836SGerry Liu 	return (0);
1437a3114836SGerry Liu }
14387c478bd9Sstevel@tonic-gate 
1439ae115bc7Smrj /*
1440ae115bc7Smrj  * Start a single cpu, assuming that the kernel context is available
1441ae115bc7Smrj  * to successfully start another cpu.
1442ae115bc7Smrj  *
1443ae115bc7Smrj  * (For example, real mode code is mapped into the right place
1444ae115bc7Smrj  * in memory and is ready to be run.)
1445ae115bc7Smrj  */
1446ae115bc7Smrj int
start_cpu(processorid_t who)1447ae115bc7Smrj start_cpu(processorid_t who)
1448ae115bc7Smrj {
1449ae115bc7Smrj 	cpu_t *cp;
1450ae115bc7Smrj 	int error = 0;
1451a3114836SGerry Liu 	cpuset_t tempset;
1452ae115bc7Smrj 
1453ae115bc7Smrj 	ASSERT(who != 0);
1454ae115bc7Smrj 
1455ae115bc7Smrj 	/*
1456ae115bc7Smrj 	 * Check if there's at least a Mbyte of kmem available
1457ae115bc7Smrj 	 * before attempting to start the cpu.
1458ae115bc7Smrj 	 */
1459ae115bc7Smrj 	if (kmem_avail() < 1024 * 1024) {
1460ae115bc7Smrj 		/*
1461ae115bc7Smrj 		 * Kick off a reap in case that helps us with
1462ae115bc7Smrj 		 * later attempts ..
1463ae115bc7Smrj 		 */
1464ae115bc7Smrj 		kmem_reap();
1465ae115bc7Smrj 		return (ENOMEM);
1466ae115bc7Smrj 	}
1467ae115bc7Smrj 
1468a3114836SGerry Liu 	/*
1469a3114836SGerry Liu 	 * First configure cpu.
1470a3114836SGerry Liu 	 */
1471a3114836SGerry Liu 	cp = mp_cpu_configure_common(who, B_TRUE);
1472a3114836SGerry Liu 	ASSERT(cp != NULL);
1473ae115bc7Smrj 
1474ae115bc7Smrj 	/*
1475a3114836SGerry Liu 	 * Then start cpu.
1476ae115bc7Smrj 	 */
1477a3114836SGerry Liu 	error = mp_start_cpu_common(cp, B_TRUE);
1478a3114836SGerry Liu 	if (error != 0) {
1479a3114836SGerry Liu 		mp_cpu_unconfigure_common(cp, error);
1480ae115bc7Smrj 		return (error);
1481ae115bc7Smrj 	}
1482ae115bc7Smrj 
1483ae115bc7Smrj 	mutex_exit(&cpu_lock);
1484a3114836SGerry Liu 	tempset = cpu_ready_set;
1485a3114836SGerry Liu 	while (!CPU_IN_SET(tempset, who)) {
1486a3114836SGerry Liu 		drv_usecwait(1);
1487a3114836SGerry Liu 		tempset = *((volatile cpuset_t *)&cpu_ready_set);
1488ae115bc7Smrj 	}
1489a3114836SGerry Liu 	mutex_enter(&cpu_lock);
1490ae115bc7Smrj 
1491ae115bc7Smrj 	return (0);
1492ae115bc7Smrj }
1493ae115bc7Smrj 
14947c478bd9Sstevel@tonic-gate void
start_other_cpus(int cprboot)14957c478bd9Sstevel@tonic-gate start_other_cpus(int cprboot)
14967c478bd9Sstevel@tonic-gate {
1497a3114836SGerry Liu 	_NOTE(ARGUNUSED(cprboot));
1498a3114836SGerry Liu 
1499ae115bc7Smrj 	uint_t who;
1500ae115bc7Smrj 	uint_t bootcpuid = 0;
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	/*
15037c478bd9Sstevel@tonic-gate 	 * Initialize our own cpu_info.
15047c478bd9Sstevel@tonic-gate 	 */
15057c478bd9Sstevel@tonic-gate 	init_cpu_info(CPU);
15067c478bd9Sstevel@tonic-gate 
1507dd7afb26SPatrick Mooney #if !defined(__xpv)
1508dd7afb26SPatrick Mooney 	init_cpu_id_gdt(CPU);
1509dd7afb26SPatrick Mooney #endif
1510dd7afb26SPatrick Mooney 
151119397407SSherry Moore 	cmn_err(CE_CONT, "?cpu%d: %s\n", CPU->cpu_id, CPU->cpu_idstr);
151219397407SSherry Moore 	cmn_err(CE_CONT, "?cpu%d: %s\n", CPU->cpu_id, CPU->cpu_brandstr);
151319397407SSherry Moore 
15147c478bd9Sstevel@tonic-gate 	/*
15157c478bd9Sstevel@tonic-gate 	 * Initialize our syscall handlers
15167c478bd9Sstevel@tonic-gate 	 */
15177c478bd9Sstevel@tonic-gate 	init_cpu_syscall(CPU);
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 	/*
1520ae115bc7Smrj 	 * Take the boot cpu out of the mp_cpus set because we know
1521ae115bc7Smrj 	 * it's already running.  Add it to the cpu_ready_set for
1522ae115bc7Smrj 	 * precisely the same reason.
1523ae115bc7Smrj 	 */
1524ae115bc7Smrj 	CPUSET_DEL(mp_cpus, bootcpuid);
1525ae115bc7Smrj 	CPUSET_ADD(cpu_ready_set, bootcpuid);
1526ae115bc7Smrj 
1527ae115bc7Smrj 	/*
1528a3114836SGerry Liu 	 * skip the rest of this if
1529a3114836SGerry Liu 	 * . only 1 cpu dectected and system isn't hotplug-capable
1530a3114836SGerry Liu 	 * . not using MP
15317c478bd9Sstevel@tonic-gate 	 */
1532a3114836SGerry Liu 	if ((CPUSET_ISNULL(mp_cpus) && plat_dr_support_cpu() == 0) ||
1533a3114836SGerry Liu 	    use_mp == 0) {
15347c478bd9Sstevel@tonic-gate 		if (use_mp == 0)
15357c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "?***** Not in MP mode\n");
15367c478bd9Sstevel@tonic-gate 		goto done;
15377c478bd9Sstevel@tonic-gate 	}
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 	/*
15407c478bd9Sstevel@tonic-gate 	 * perform such initialization as is needed
15417c478bd9Sstevel@tonic-gate 	 * to be able to take CPUs on- and off-line.
15427c478bd9Sstevel@tonic-gate 	 */
15437c478bd9Sstevel@tonic-gate 	cpu_pause_init();
15447c478bd9Sstevel@tonic-gate 
1545f34a7178SJoe Bonasera 	xc_init_cpu(CPU);		/* initialize processor crosscalls */
15467c478bd9Sstevel@tonic-gate 
1547ae115bc7Smrj 	if (mach_cpucontext_init() != 0)
15487c478bd9Sstevel@tonic-gate 		goto done;
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 	flushes_require_xcalls = 1;
15517c478bd9Sstevel@tonic-gate 
15525205ae23Snf202958 	/*
15535205ae23Snf202958 	 * We lock our affinity to the master CPU to ensure that all slave CPUs
15545205ae23Snf202958 	 * do their TSC syncs with the same CPU.
15555205ae23Snf202958 	 */
15567c478bd9Sstevel@tonic-gate 	affinity_set(CPU_CURRENT);
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	for (who = 0; who < NCPU; who++) {
155941791439Sandrei 		if (!CPU_IN_SET(mp_cpus, who))
15607c478bd9Sstevel@tonic-gate 			continue;
1561ae115bc7Smrj 		ASSERT(who != bootcpuid);
1562b9e93c10SJonathan Haslam 
1563b9e93c10SJonathan Haslam 		mutex_enter(&cpu_lock);
1564a3114836SGerry Liu 		if (start_cpu(who) != 0)
1565a3114836SGerry Liu 			CPUSET_DEL(mp_cpus, who);
1566b9e93c10SJonathan Haslam 		cpu_state_change_notify(who, CPU_SETUP);
1567b9e93c10SJonathan Haslam 		mutex_exit(&cpu_lock);
15687c478bd9Sstevel@tonic-gate 	}
15697c478bd9Sstevel@tonic-gate 
15702449e17fSsherrym 	/* Free the space allocated to hold the microcode file */
1571adc586deSMark Johnson 	ucode_cleanup();
15722449e17fSsherrym 
15737c478bd9Sstevel@tonic-gate 	affinity_clear();
15747c478bd9Sstevel@tonic-gate 
1575a3114836SGerry Liu 	mach_cpucontext_fini();
157641791439Sandrei 
15777c478bd9Sstevel@tonic-gate done:
1578ad09f8b8SMark Johnson 	if (get_hwenv() == HW_NATIVE)
15797c478bd9Sstevel@tonic-gate 		workaround_errata_end();
15803ad553a7Sgavinm 	cmi_post_mpstartup();
1581a3114836SGerry Liu 
1582a3114836SGerry Liu 	if (use_mp && ncpus != boot_max_ncpus) {
1583a3114836SGerry Liu 		cmn_err(CE_NOTE,
1584a3114836SGerry Liu 		    "System detected %d cpus, but "
1585a3114836SGerry Liu 		    "only %d cpu(s) were enabled during boot.",
1586a3114836SGerry Liu 		    boot_max_ncpus, ncpus);
1587a3114836SGerry Liu 		cmn_err(CE_NOTE,
1588a3114836SGerry Liu 		    "Use \"boot-ncpus\" parameter to enable more CPU(s). "
1589a3114836SGerry Liu 		    "See eeprom(1M).");
1590a3114836SGerry Liu 	}
15917c478bd9Sstevel@tonic-gate }
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate int
mp_cpu_configure(int cpuid)15947c478bd9Sstevel@tonic-gate mp_cpu_configure(int cpuid)
15957c478bd9Sstevel@tonic-gate {
1596a3114836SGerry Liu 	cpu_t *cp;
1597a3114836SGerry Liu 
1598a3114836SGerry Liu 	if (use_mp == 0 || plat_dr_support_cpu() == 0) {
1599a3114836SGerry Liu 		return (ENOTSUP);
16007c478bd9Sstevel@tonic-gate 	}
16017c478bd9Sstevel@tonic-gate 
1602a3114836SGerry Liu 	cp = cpu_get(cpuid);
1603a3114836SGerry Liu 	if (cp != NULL) {
1604a3114836SGerry Liu 		return (EALREADY);
1605a3114836SGerry Liu 	}
1606a3114836SGerry Liu 
1607a3114836SGerry Liu 	/*
1608a3114836SGerry Liu 	 * Check if there's at least a Mbyte of kmem available
1609a3114836SGerry Liu 	 * before attempting to start the cpu.
1610a3114836SGerry Liu 	 */
1611a3114836SGerry Liu 	if (kmem_avail() < 1024 * 1024) {
1612a3114836SGerry Liu 		/*
1613a3114836SGerry Liu 		 * Kick off a reap in case that helps us with
1614a3114836SGerry Liu 		 * later attempts ..
1615a3114836SGerry Liu 		 */
1616a3114836SGerry Liu 		kmem_reap();
1617a3114836SGerry Liu 		return (ENOMEM);
1618a3114836SGerry Liu 	}
1619a3114836SGerry Liu 
1620a3114836SGerry Liu 	cp = mp_cpu_configure_common(cpuid, B_FALSE);
1621a3114836SGerry Liu 	ASSERT(cp != NULL && cpu_get(cpuid) == cp);
1622a3114836SGerry Liu 
1623a3114836SGerry Liu 	return (cp != NULL ? 0 : EAGAIN);
1624a3114836SGerry Liu }
1625a3114836SGerry Liu 
16267c478bd9Sstevel@tonic-gate int
mp_cpu_unconfigure(int cpuid)16277c478bd9Sstevel@tonic-gate mp_cpu_unconfigure(int cpuid)
16287c478bd9Sstevel@tonic-gate {
1629a3114836SGerry Liu 	cpu_t *cp;
1630a3114836SGerry Liu 
1631a3114836SGerry Liu 	if (use_mp == 0 || plat_dr_support_cpu() == 0) {
1632a3114836SGerry Liu 		return (ENOTSUP);
1633a3114836SGerry Liu 	} else if (cpuid < 0 || cpuid >= max_ncpus) {
1634a3114836SGerry Liu 		return (EINVAL);
1635a3114836SGerry Liu 	}
1636a3114836SGerry Liu 
1637a3114836SGerry Liu 	cp = cpu_get(cpuid);
1638a3114836SGerry Liu 	if (cp == NULL) {
1639a3114836SGerry Liu 		return (ENODEV);
1640a3114836SGerry Liu 	}
1641a3114836SGerry Liu 	mp_cpu_unconfigure_common(cp, 0);
1642a3114836SGerry Liu 
1643a3114836SGerry Liu 	return (0);
16447c478bd9Sstevel@tonic-gate }
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate /*
16477c478bd9Sstevel@tonic-gate  * Startup function for 'other' CPUs (besides boot cpu).
1648498697c5Sdmick  * Called from real_mode_start.
1649b4b46911Skchow  *
1650a3114836SGerry Liu  * WARNING: until CPU_READY is set, mp_startup_common and routines called by
1651a3114836SGerry Liu  * mp_startup_common should not call routines (e.g. kmem_free) that could call
1652b4b46911Skchow  * hat_unload which requires CPU_READY to be set.
16537c478bd9Sstevel@tonic-gate  */
1654a3114836SGerry Liu static void
mp_startup_common(boolean_t boot)1655a3114836SGerry Liu mp_startup_common(boolean_t boot)
16567c478bd9Sstevel@tonic-gate {
1657a3114836SGerry Liu 	cpu_t *cp = CPU;
1658dfea898aSKuriakose Kuruvilla 	uchar_t new_x86_featureset[BT_SIZEOFMAP(NUM_X86_FEATURES)];
1659a3114836SGerry Liu 	extern void cpu_event_init_cpu(cpu_t *);
16607c478bd9Sstevel@tonic-gate 
1661498697c5Sdmick 	/*
166224a74e86Sdmick 	 * We need to get TSC on this proc synced (i.e., any delta
166324a74e86Sdmick 	 * from cpu0 accounted for) as soon as we can, because many
166424a74e86Sdmick 	 * many things use gethrtime/pc_gethrestime, including
1665fba15cb6SKeith M Wesolowski 	 * interrupts, cmn_err, etc.  Before we can do that, we want to
1666fba15cb6SKeith M Wesolowski 	 * clear TSC if we're on a buggy Sandy/Ivy Bridge CPU, so do that
1667fba15cb6SKeith M Wesolowski 	 * right away.
166824a74e86Sdmick 	 */
1669fba15cb6SKeith M Wesolowski 	bzero(new_x86_featureset, BT_SIZEOFMAP(NUM_X86_FEATURES));
1670fba15cb6SKeith M Wesolowski 	cpuid_pass1(cp, new_x86_featureset);
1671fba15cb6SKeith M Wesolowski 
1672fba15cb6SKeith M Wesolowski 	if (boot && get_hwenv() == HW_NATIVE &&
1673fba15cb6SKeith M Wesolowski 	    cpuid_getvendor(CPU) == X86_VENDOR_Intel &&
1674fba15cb6SKeith M Wesolowski 	    cpuid_getfamily(CPU) == 6 &&
1675fba15cb6SKeith M Wesolowski 	    (cpuid_getmodel(CPU) == 0x2d || cpuid_getmodel(CPU) == 0x3e) &&
1676fba15cb6SKeith M Wesolowski 	    is_x86_feature(new_x86_featureset, X86FSET_TSC)) {
1677fba15cb6SKeith M Wesolowski 		(void) wrmsr(REG_TSC, 0UL);
1678fba15cb6SKeith M Wesolowski 	}
167924a74e86Sdmick 
1680a3114836SGerry Liu 	/* Let the control CPU continue into tsc_sync_master() */
1681a3114836SGerry Liu 	mp_startup_signal(&procset_slave, cp->cpu_id);
168224a74e86Sdmick 
1683843e1988Sjohnlev #ifndef __xpv
168424a74e86Sdmick 	if (tsc_gethrtime_enable)
168524a74e86Sdmick 		tsc_sync_slave();
1686843e1988Sjohnlev #endif
168724a74e86Sdmick 
168824a74e86Sdmick 	/*
1689498697c5Sdmick 	 * Once this was done from assembly, but it's safer here; if
1690498697c5Sdmick 	 * it blocks, we need to be able to swtch() to and from, and
1691498697c5Sdmick 	 * since we get here by calling t_pc, we need to do that call
1692498697c5Sdmick 	 * before swtch() overwrites it.
1693498697c5Sdmick 	 */
1694498697c5Sdmick 	(void) (*ap_mlsetup)();
1695498697c5Sdmick 
1696843e1988Sjohnlev #ifndef __xpv
16977c478bd9Sstevel@tonic-gate 	/*
16981d03c31eSjohnlev 	 * Program this cpu's PAT
16997c478bd9Sstevel@tonic-gate 	 */
17001d03c31eSjohnlev 	pat_sync();
1701843e1988Sjohnlev #endif
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 	/*
1704ae115bc7Smrj 	 * Set up TSC_AUX to contain the cpuid for this processor
1705ae115bc7Smrj 	 * for the rdtscp instruction.
1706ae115bc7Smrj 	 */
17077417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_TSCP))
1708ae115bc7Smrj 		(void) wrmsr(MSR_AMD_TSCAUX, cp->cpu_id);
1709ae115bc7Smrj 
1710ae115bc7Smrj 	/*
17117c478bd9Sstevel@tonic-gate 	 * Initialize this CPU's syscall handlers
17127c478bd9Sstevel@tonic-gate 	 */
17137c478bd9Sstevel@tonic-gate 	init_cpu_syscall(cp);
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 	/*
17167c478bd9Sstevel@tonic-gate 	 * Enable interrupts with spl set to LOCK_LEVEL. LOCK_LEVEL is the
17177c478bd9Sstevel@tonic-gate 	 * highest level at which a routine is permitted to block on
17187c478bd9Sstevel@tonic-gate 	 * an adaptive mutex (allows for cpu poke interrupt in case
17197c478bd9Sstevel@tonic-gate 	 * the cpu is blocked on a mutex and halts). Setting LOCK_LEVEL blocks
17207c478bd9Sstevel@tonic-gate 	 * device interrupts that may end up in the hat layer issuing cross
17217c478bd9Sstevel@tonic-gate 	 * calls before CPU_READY is set.
17227c478bd9Sstevel@tonic-gate 	 */
1723ae115bc7Smrj 	splx(ipltospl(LOCK_LEVEL));
1724ae115bc7Smrj 	sti();
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 	/*
17277c478bd9Sstevel@tonic-gate 	 * Do a sanity check to make sure this new CPU is a sane thing
17287c478bd9Sstevel@tonic-gate 	 * to add to the collection of processors running this system.
17297c478bd9Sstevel@tonic-gate 	 *
17307c478bd9Sstevel@tonic-gate 	 * XXX	Clearly this needs to get more sophisticated, if x86
17317c478bd9Sstevel@tonic-gate 	 * systems start to get built out of heterogenous CPUs; as is
17327c478bd9Sstevel@tonic-gate 	 * likely to happen once the number of processors in a configuration
17337c478bd9Sstevel@tonic-gate 	 * gets large enough.
17347c478bd9Sstevel@tonic-gate 	 */
17357417cfdeSKuriakose Kuruvilla 	if (compare_x86_featureset(x86_featureset, new_x86_featureset) ==
17367417cfdeSKuriakose Kuruvilla 	    B_FALSE) {
17377417cfdeSKuriakose Kuruvilla 		cmn_err(CE_CONT, "cpu%d: featureset\n", cp->cpu_id);
17387417cfdeSKuriakose Kuruvilla 		print_x86_featureset(new_x86_featureset);
17397c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cpu%d feature mismatch", cp->cpu_id);
17407c478bd9Sstevel@tonic-gate 	}
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 	/*
1743f98fbcecSbholler 	 * We do not support cpus with mixed monitor/mwait support if the
1744f98fbcecSbholler 	 * boot cpu supports monitor/mwait.
1745f98fbcecSbholler 	 */
17467417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_MWAIT) !=
17477417cfdeSKuriakose Kuruvilla 	    is_x86_feature(new_x86_featureset, X86FSET_MWAIT))
1748f98fbcecSbholler 		panic("unsupported mixed cpu monitor/mwait support detected");
1749f98fbcecSbholler 
1750f98fbcecSbholler 	/*
17517c478bd9Sstevel@tonic-gate 	 * We could be more sophisticated here, and just mark the CPU
17527c478bd9Sstevel@tonic-gate 	 * as "faulted" but at this point we'll opt for the easier
1753fb2caebeSRandy Fishel 	 * answer of dying horribly.  Provided the boot cpu is ok,
17547c478bd9Sstevel@tonic-gate 	 * the system can be recovered by booting with use_mp set to zero.
17557c478bd9Sstevel@tonic-gate 	 */
17567c478bd9Sstevel@tonic-gate 	if (workaround_errata(cp) != 0)
17577c478bd9Sstevel@tonic-gate 		panic("critical workaround(s) missing for cpu%d", cp->cpu_id);
17587c478bd9Sstevel@tonic-gate 
1759a3114836SGerry Liu 	/*
1760a3114836SGerry Liu 	 * We can touch cpu_flags here without acquiring the cpu_lock here
1761a3114836SGerry Liu 	 * because the cpu_lock is held by the control CPU which is running
1762a3114836SGerry Liu 	 * mp_start_cpu_common().
1763a3114836SGerry Liu 	 * Need to clear CPU_QUIESCED flag before calling any function which
1764a3114836SGerry Liu 	 * may cause thread context switching, such as kmem_alloc() etc.
1765a3114836SGerry Liu 	 * The idle thread checks for CPU_QUIESCED flag and loops for ever if
1766a3114836SGerry Liu 	 * it's set. So the startup thread may have no chance to switch back
1767a3114836SGerry Liu 	 * again if it's switched away with CPU_QUIESCED set.
1768a3114836SGerry Liu 	 */
1769a3114836SGerry Liu 	cp->cpu_flags &= ~(CPU_POWEROFF | CPU_QUIESCED);
1770a3114836SGerry Liu 
17717af88ac7SKuriakose Kuruvilla 	/*
17727af88ac7SKuriakose Kuruvilla 	 * Setup this processor for XSAVE.
17737af88ac7SKuriakose Kuruvilla 	 */
17747af88ac7SKuriakose Kuruvilla 	if (fp_save_mech == FP_XSAVE) {
17757af88ac7SKuriakose Kuruvilla 		xsave_setup_msr(cp);
17767af88ac7SKuriakose Kuruvilla 	}
17777af88ac7SKuriakose Kuruvilla 
17787c478bd9Sstevel@tonic-gate 	cpuid_pass2(cp);
17797c478bd9Sstevel@tonic-gate 	cpuid_pass3(cp);
1780ebb8ac07SRobert Mustacchi 	cpuid_pass4(cp, NULL);
17817c478bd9Sstevel@tonic-gate 
1782a3114836SGerry Liu 	/*
1783a3114836SGerry Liu 	 * Correct cpu_idstr and cpu_brandstr on target CPU after
1784a3114836SGerry Liu 	 * cpuid_pass1() is done.
1785a3114836SGerry Liu 	 */
1786a3114836SGerry Liu 	(void) cpuid_getidstr(cp, cp->cpu_idstr, CPU_IDSTRLEN);
1787a3114836SGerry Liu 	(void) cpuid_getbrandstr(cp, cp->cpu_brandstr, CPU_IDSTRLEN);
17887c478bd9Sstevel@tonic-gate 
1789afbc4541Ssherrym 	cp->cpu_flags |= CPU_RUNNING | CPU_READY | CPU_EXISTS;
17905205ae23Snf202958 
1791e774b42bSBill Holler 	post_startup_cpu_fixups();
1792e774b42bSBill Holler 
1793a3114836SGerry Liu 	cpu_event_init_cpu(cp);
1794a3114836SGerry Liu 
1795aa7b6435Ssethg 	/*
1796aa7b6435Ssethg 	 * Enable preemption here so that contention for any locks acquired
1797a3114836SGerry Liu 	 * later in mp_startup_common may be preempted if the thread owning
1798a3114836SGerry Liu 	 * those locks is continuously executing on other CPUs (for example,
1799a3114836SGerry Liu 	 * this CPU must be preemptible to allow other CPUs to pause it during
1800a3114836SGerry Liu 	 * their startup phases).  It's safe to enable preemption here because
1801a3114836SGerry Liu 	 * the CPU state is pretty-much fully constructed.
1802aa7b6435Ssethg 	 */
1803aa7b6435Ssethg 	curthread->t_preempt = 0;
1804aa7b6435Ssethg 
1805da43ceabSsethg 	/* The base spl should still be at LOCK LEVEL here */
1806da43ceabSsethg 	ASSERT(cp->cpu_base_spl == ipltospl(LOCK_LEVEL));
1807da43ceabSsethg 	set_base_spl();		/* Restore the spl to its proper value */
1808da43ceabSsethg 
18090e751525SEric Saxe 	pghw_physid_create(cp);
1810a3114836SGerry Liu 	/*
1811a3114836SGerry Liu 	 * Delegate initialization tasks, which need to access the cpu_lock,
1812a3114836SGerry Liu 	 * to mp_start_cpu_common() because we can't acquire the cpu_lock here
1813a3114836SGerry Liu 	 * during CPU DR operations.
1814a3114836SGerry Liu 	 */
1815a3114836SGerry Liu 	mp_startup_signal(&procset_slave, cp->cpu_id);
1816a3114836SGerry Liu 	mp_startup_wait(&procset_master, cp->cpu_id);
18170e751525SEric Saxe 	pg_cmt_cpu_startup(cp);
1818a3114836SGerry Liu 
1819a3114836SGerry Liu 	if (boot) {
1820a3114836SGerry Liu 		mutex_enter(&cpu_lock);
1821a3114836SGerry Liu 		cp->cpu_flags &= ~CPU_OFFLINE;
1822a3114836SGerry Liu 		cpu_enable_intr(cp);
1823a3114836SGerry Liu 		cpu_add_active(cp);
18240e751525SEric Saxe 		mutex_exit(&cpu_lock);
1825a3114836SGerry Liu 	}
18260e751525SEric Saxe 
1827afbc4541Ssherrym 	/* Enable interrupts */
1828afbc4541Ssherrym 	(void) spl0();
18290e751525SEric Saxe 
1830a3114836SGerry Liu 	/*
1831a3114836SGerry Liu 	 * Fill out cpu_ucode_info.  Update microcode if necessary.
1832a3114836SGerry Liu 	 */
1833a3114836SGerry Liu 	ucode_check(cp);
1834afbc4541Ssherrym 
183520c794b3Sgavinm #ifndef __xpv
183620c794b3Sgavinm 	{
18377aec1d6eScindi 		/*
183820c794b3Sgavinm 		 * Set up the CPU module for this CPU.  This can't be done
183920c794b3Sgavinm 		 * before this CPU is made CPU_READY, because we may (in
184020c794b3Sgavinm 		 * heterogeneous systems) need to go load another CPU module.
184120c794b3Sgavinm 		 * The act of attempting to load a module may trigger a
184220c794b3Sgavinm 		 * cross-call, which will ASSERT unless this cpu is CPU_READY.
18437aec1d6eScindi 		 */
184420c794b3Sgavinm 		cmi_hdl_t hdl;
18457aec1d6eScindi 
184620c794b3Sgavinm 		if ((hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
1847e4b86885SCheng Sean Ye 		    cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL) {
18487417cfdeSKuriakose Kuruvilla 			if (is_x86_feature(x86_featureset, X86FSET_MCA))
184920c794b3Sgavinm 				cmi_mca_init(hdl);
1850a3114836SGerry Liu 			cp->cpu_m.mcpu_cmi_hdl = hdl;
185120c794b3Sgavinm 		}
185220c794b3Sgavinm 	}
185320c794b3Sgavinm #endif /* __xpv */
18547aec1d6eScindi 
18557c478bd9Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
1856ae115bc7Smrj 		kdi_cpu_init();
18577c478bd9Sstevel@tonic-gate 
1858*cca8a59fSJason King 	(void) mach_cpu_create_device_node(cp, NULL);
1859*cca8a59fSJason King 
18607c478bd9Sstevel@tonic-gate 	/*
18617c478bd9Sstevel@tonic-gate 	 * Setting the bit in cpu_ready_set must be the last operation in
18627c478bd9Sstevel@tonic-gate 	 * processor initialization; the boot CPU will continue to boot once
18637c478bd9Sstevel@tonic-gate 	 * it sees this bit set for all active CPUs.
18647c478bd9Sstevel@tonic-gate 	 */
18657c478bd9Sstevel@tonic-gate 	CPUSET_ATOMIC_ADD(cpu_ready_set, cp->cpu_id);
18667c478bd9Sstevel@tonic-gate 
1867a3114836SGerry Liu 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_idstr);
1868a3114836SGerry Liu 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_brandstr);
1869a3114836SGerry Liu 	cmn_err(CE_CONT, "?cpu%d initialization complete - online\n",
1870a3114836SGerry Liu 	    cp->cpu_id);
1871a3114836SGerry Liu 
18727c478bd9Sstevel@tonic-gate 	/*
18737c478bd9Sstevel@tonic-gate 	 * Now we are done with the startup thread, so free it up.
18747c478bd9Sstevel@tonic-gate 	 */
18757c478bd9Sstevel@tonic-gate 	thread_exit();
18767c478bd9Sstevel@tonic-gate 	panic("mp_startup: cannot return");
18777c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
18787c478bd9Sstevel@tonic-gate }
18797c478bd9Sstevel@tonic-gate 
1880a3114836SGerry Liu /*
1881a3114836SGerry Liu  * Startup function for 'other' CPUs at boot time (besides boot cpu).
1882a3114836SGerry Liu  */
1883a3114836SGerry Liu static void
mp_startup_boot(void)1884a3114836SGerry Liu mp_startup_boot(void)
1885a3114836SGerry Liu {
1886a3114836SGerry Liu 	mp_startup_common(B_TRUE);
1887a3114836SGerry Liu }
1888a3114836SGerry Liu 
1889a3114836SGerry Liu /*
1890a3114836SGerry Liu  * Startup function for hotplug CPUs at runtime.
1891a3114836SGerry Liu  */
1892a3114836SGerry Liu void
mp_startup_hotplug(void)1893a3114836SGerry Liu mp_startup_hotplug(void)
1894a3114836SGerry Liu {
1895a3114836SGerry Liu 	mp_startup_common(B_FALSE);
1896a3114836SGerry Liu }
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate /*
18997c478bd9Sstevel@tonic-gate  * Start CPU on user request.
19007c478bd9Sstevel@tonic-gate  */
19017c478bd9Sstevel@tonic-gate /* ARGSUSED */
19027c478bd9Sstevel@tonic-gate int
mp_cpu_start(struct cpu * cp)19037c478bd9Sstevel@tonic-gate mp_cpu_start(struct cpu *cp)
19047c478bd9Sstevel@tonic-gate {
19057c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
19067c478bd9Sstevel@tonic-gate 	return (0);
19077c478bd9Sstevel@tonic-gate }
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate /*
19107c478bd9Sstevel@tonic-gate  * Stop CPU on user request.
19117c478bd9Sstevel@tonic-gate  */
19127c478bd9Sstevel@tonic-gate int
mp_cpu_stop(struct cpu * cp)19137c478bd9Sstevel@tonic-gate mp_cpu_stop(struct cpu *cp)
19147c478bd9Sstevel@tonic-gate {
1915d90554ebSdmick 	extern int cbe_psm_timer_mode;
19167c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
1917d90554ebSdmick 
1918843e1988Sjohnlev #ifdef __xpv
1919843e1988Sjohnlev 	/*
1920843e1988Sjohnlev 	 * We can't offline vcpu0.
1921843e1988Sjohnlev 	 */
1922843e1988Sjohnlev 	if (cp->cpu_id == 0)
1923843e1988Sjohnlev 		return (EBUSY);
1924843e1988Sjohnlev #endif
1925843e1988Sjohnlev 
1926d90554ebSdmick 	/*
1927d90554ebSdmick 	 * If TIMER_PERIODIC mode is used, CPU0 is the one running it;
1928d90554ebSdmick 	 * can't stop it.  (This is true only for machines with no TSC.)
1929d90554ebSdmick 	 */
1930d90554ebSdmick 
1931d90554ebSdmick 	if ((cbe_psm_timer_mode == TIMER_PERIODIC) && (cp->cpu_id == 0))
1932843e1988Sjohnlev 		return (EBUSY);
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	return (0);
19357c478bd9Sstevel@tonic-gate }
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate /*
19387c478bd9Sstevel@tonic-gate  * Take the specified CPU out of participation in interrupts.
19397c478bd9Sstevel@tonic-gate  */
19407c478bd9Sstevel@tonic-gate int
cpu_disable_intr(struct cpu * cp)19417c478bd9Sstevel@tonic-gate cpu_disable_intr(struct cpu *cp)
19427c478bd9Sstevel@tonic-gate {
19437c478bd9Sstevel@tonic-gate 	if (psm_disable_intr(cp->cpu_id) != DDI_SUCCESS)
19447c478bd9Sstevel@tonic-gate 		return (EBUSY);
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 	cp->cpu_flags &= ~CPU_ENABLE;
19477c478bd9Sstevel@tonic-gate 	return (0);
19487c478bd9Sstevel@tonic-gate }
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate /*
19517c478bd9Sstevel@tonic-gate  * Allow the specified CPU to participate in interrupts.
19527c478bd9Sstevel@tonic-gate  */
19537c478bd9Sstevel@tonic-gate void
cpu_enable_intr(struct cpu * cp)19547c478bd9Sstevel@tonic-gate cpu_enable_intr(struct cpu *cp)
19557c478bd9Sstevel@tonic-gate {
19567c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
19577c478bd9Sstevel@tonic-gate 	cp->cpu_flags |= CPU_ENABLE;
19587c478bd9Sstevel@tonic-gate 	psm_enable_intr(cp->cpu_id);
19597c478bd9Sstevel@tonic-gate }
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate void
mp_cpu_faulted_enter(struct cpu * cp)19627c478bd9Sstevel@tonic-gate mp_cpu_faulted_enter(struct cpu *cp)
19637aec1d6eScindi {
1964a3114836SGerry Liu #ifdef __xpv
1965a3114836SGerry Liu 	_NOTE(ARGUNUSED(cp));
1966a3114836SGerry Liu #else
1967a3114836SGerry Liu 	cmi_hdl_t hdl = cp->cpu_m.mcpu_cmi_hdl;
196820c794b3Sgavinm 
196920c794b3Sgavinm 	if (hdl != NULL) {
1970a3114836SGerry Liu 		cmi_hdl_hold(hdl);
1971a3114836SGerry Liu 	} else {
1972a3114836SGerry Liu 		hdl = cmi_hdl_lookup(CMI_HDL_NATIVE, cmi_ntv_hwchipid(cp),
1973a3114836SGerry Liu 		    cmi_ntv_hwcoreid(cp), cmi_ntv_hwstrandid(cp));
1974a3114836SGerry Liu 	}
1975a3114836SGerry Liu 	if (hdl != NULL) {
197620c794b3Sgavinm 		cmi_faulted_enter(hdl);
197720c794b3Sgavinm 		cmi_hdl_rele(hdl);
197820c794b3Sgavinm 	}
197920c794b3Sgavinm #endif
19807aec1d6eScindi }
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate void
mp_cpu_faulted_exit(struct cpu * cp)19837c478bd9Sstevel@tonic-gate mp_cpu_faulted_exit(struct cpu *cp)
19847aec1d6eScindi {
1985a3114836SGerry Liu #ifdef __xpv
1986a3114836SGerry Liu 	_NOTE(ARGUNUSED(cp));
1987a3114836SGerry Liu #else
1988a3114836SGerry Liu 	cmi_hdl_t hdl = cp->cpu_m.mcpu_cmi_hdl;
198920c794b3Sgavinm 
199020c794b3Sgavinm 	if (hdl != NULL) {
1991a3114836SGerry Liu 		cmi_hdl_hold(hdl);
1992a3114836SGerry Liu 	} else {
1993a3114836SGerry Liu 		hdl = cmi_hdl_lookup(CMI_HDL_NATIVE, cmi_ntv_hwchipid(cp),
1994a3114836SGerry Liu 		    cmi_ntv_hwcoreid(cp), cmi_ntv_hwstrandid(cp));
1995a3114836SGerry Liu 	}
1996a3114836SGerry Liu 	if (hdl != NULL) {
199720c794b3Sgavinm 		cmi_faulted_exit(hdl);
199820c794b3Sgavinm 		cmi_hdl_rele(hdl);
199920c794b3Sgavinm 	}
200020c794b3Sgavinm #endif
20017aec1d6eScindi }
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate /*
20047c478bd9Sstevel@tonic-gate  * The following two routines are used as context operators on threads belonging
20057c478bd9Sstevel@tonic-gate  * to processes with a private LDT (see sysi86).  Due to the rarity of such
20067c478bd9Sstevel@tonic-gate  * processes, these routines are currently written for best code readability and
20077417cfdeSKuriakose Kuruvilla  * organization rather than speed.  We could avoid checking x86_featureset at
20087417cfdeSKuriakose Kuruvilla  * every context switch by installing different context ops, depending on
20097417cfdeSKuriakose Kuruvilla  * x86_featureset, at LDT creation time -- one for each combination of fast
20107417cfdeSKuriakose Kuruvilla  * syscall features.
20117c478bd9Sstevel@tonic-gate  */
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate /*ARGSUSED*/
20147c478bd9Sstevel@tonic-gate void
cpu_fast_syscall_disable(void * arg)20157c478bd9Sstevel@tonic-gate cpu_fast_syscall_disable(void *arg)
20167c478bd9Sstevel@tonic-gate {
20177417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
20187417cfdeSKuriakose Kuruvilla 	    is_x86_feature(x86_featureset, X86FSET_SEP))
20197c478bd9Sstevel@tonic-gate 		cpu_sep_disable();
20207417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
20217417cfdeSKuriakose Kuruvilla 	    is_x86_feature(x86_featureset, X86FSET_ASYSC))
20227c478bd9Sstevel@tonic-gate 		cpu_asysc_disable();
20237c478bd9Sstevel@tonic-gate }
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate /*ARGSUSED*/
20267c478bd9Sstevel@tonic-gate void
cpu_fast_syscall_enable(void * arg)20277c478bd9Sstevel@tonic-gate cpu_fast_syscall_enable(void *arg)
20287c478bd9Sstevel@tonic-gate {
20297417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
20307417cfdeSKuriakose Kuruvilla 	    is_x86_feature(x86_featureset, X86FSET_SEP))
20317c478bd9Sstevel@tonic-gate 		cpu_sep_enable();
20327417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
20337417cfdeSKuriakose Kuruvilla 	    is_x86_feature(x86_featureset, X86FSET_ASYSC))
20347c478bd9Sstevel@tonic-gate 		cpu_asysc_enable();
20357c478bd9Sstevel@tonic-gate }
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate static void
cpu_sep_enable(void)20387c478bd9Sstevel@tonic-gate cpu_sep_enable(void)
20397c478bd9Sstevel@tonic-gate {
20407417cfdeSKuriakose Kuruvilla 	ASSERT(is_x86_feature(x86_featureset, X86FSET_SEP));
20417c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
20427c478bd9Sstevel@tonic-gate 
20430ac7d7d8Skucharsk 	wrmsr(MSR_INTC_SEP_CS, (uint64_t)(uintptr_t)KCS_SEL);
20447c478bd9Sstevel@tonic-gate }
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate static void
cpu_sep_disable(void)20477c478bd9Sstevel@tonic-gate cpu_sep_disable(void)
20487c478bd9Sstevel@tonic-gate {
20497417cfdeSKuriakose Kuruvilla 	ASSERT(is_x86_feature(x86_featureset, X86FSET_SEP));
20507c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 	/*
20537c478bd9Sstevel@tonic-gate 	 * Setting the SYSENTER_CS_MSR register to 0 causes software executing
20547c478bd9Sstevel@tonic-gate 	 * the sysenter or sysexit instruction to trigger a #gp fault.
20557c478bd9Sstevel@tonic-gate 	 */
2056ae115bc7Smrj 	wrmsr(MSR_INTC_SEP_CS, 0);
20577c478bd9Sstevel@tonic-gate }
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate static void
cpu_asysc_enable(void)20607c478bd9Sstevel@tonic-gate cpu_asysc_enable(void)
20617c478bd9Sstevel@tonic-gate {
20627417cfdeSKuriakose Kuruvilla 	ASSERT(is_x86_feature(x86_featureset, X86FSET_ASYSC));
20637c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
20647c478bd9Sstevel@tonic-gate 
20650ac7d7d8Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) |
20660ac7d7d8Skucharsk 	    (uint64_t)(uintptr_t)AMD_EFER_SCE);
20677c478bd9Sstevel@tonic-gate }
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate static void
cpu_asysc_disable(void)20707c478bd9Sstevel@tonic-gate cpu_asysc_disable(void)
20717c478bd9Sstevel@tonic-gate {
20727417cfdeSKuriakose Kuruvilla 	ASSERT(is_x86_feature(x86_featureset, X86FSET_ASYSC));
20737c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	/*
20767c478bd9Sstevel@tonic-gate 	 * Turn off the SCE (syscall enable) bit in the EFER register. Software
20777c478bd9Sstevel@tonic-gate 	 * executing syscall or sysret with this bit off will incur a #ud trap.
20787c478bd9Sstevel@tonic-gate 	 */
20790ac7d7d8Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) &
20800ac7d7d8Skucharsk 	    ~((uint64_t)(uintptr_t)AMD_EFER_SCE));
20817c478bd9Sstevel@tonic-gate }
2082